+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
425 lines
13 KiB
TypeScript
425 lines
13 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts";
|
|
|
|
import {
|
|
canDetachThreadProviderSession,
|
|
canForkProjectedAssistantItem,
|
|
deriveThreadQueueWorkflowState,
|
|
resolveLatestMergeBackRun,
|
|
threadSupportsProviderHandoff,
|
|
} from "./threadWorkflows.ts";
|
|
|
|
const capabilities = (input?: {
|
|
readonly queued?: boolean;
|
|
readonly steer?: boolean;
|
|
readonly restartSteer?: boolean;
|
|
readonly nativeFork?: boolean;
|
|
readonly portableFork?: boolean;
|
|
}) =>
|
|
({
|
|
turns: {
|
|
supportsQueuedMessages: input?.queued ?? false,
|
|
supportsActiveSteering: input?.steer ?? false,
|
|
supportsSteeringByInterruptRestart: input?.restartSteer ?? false,
|
|
},
|
|
threads: {
|
|
canForkThread: input?.nativeFork ?? false,
|
|
canForkFromTurn: input?.nativeFork ?? false,
|
|
},
|
|
identity: { nativeThreadIds: input?.nativeFork ? "strong" : "none" },
|
|
context: { supportsFullThreadHandoff: input?.portableFork ?? false },
|
|
}) as never;
|
|
|
|
describe("thread workflows", () => {
|
|
it("allows a completed thread to switch providers after its session detaches", () => {
|
|
const projection = {
|
|
thread: {
|
|
id: "thread",
|
|
activeProviderThreadId: "provider-thread",
|
|
modelSelection: { instanceId: "cursor", model: "grok-4.7" },
|
|
},
|
|
runs: [{ id: "run", status: "completed" }],
|
|
providerSessions: [],
|
|
providerThreads: [
|
|
{
|
|
id: "provider-thread",
|
|
appThreadId: "thread",
|
|
providerInstanceId: "cursor",
|
|
providerSessionId: "detached-session",
|
|
nativeThreadRef: { driver: "cursor", nativeId: "agent-123", strength: "strong" },
|
|
},
|
|
],
|
|
} as unknown as OrchestrationV2ThreadProjection;
|
|
|
|
expect(threadSupportsProviderHandoff(projection)).toBe(true);
|
|
expect(
|
|
threadSupportsProviderHandoff({
|
|
...projection,
|
|
runs: [{ ...projection.runs[0]!, status: "running" }],
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
threadSupportsProviderHandoff({
|
|
...projection,
|
|
providerThreads: [{ ...projection.providerThreads[0]!, nativeThreadRef: null }],
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("sorts queued messages and gates reorder and promotion from capabilities", () => {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
|
|
runs: [
|
|
{
|
|
id: "active",
|
|
status: "running",
|
|
providerThreadId: "provider-thread",
|
|
activeAttemptId: "attempt-active",
|
|
ordinal: 1,
|
|
},
|
|
{ id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 },
|
|
{
|
|
id: "first",
|
|
status: "queued",
|
|
userMessageId: "message-first",
|
|
ordinal: 2,
|
|
queuePosition: 1,
|
|
},
|
|
],
|
|
messages: [
|
|
{
|
|
id: "message-first",
|
|
text: "First",
|
|
attachments: [
|
|
{
|
|
type: "image",
|
|
id: "attachment-first",
|
|
name: "first.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 64,
|
|
},
|
|
],
|
|
},
|
|
{ id: "message-later", text: "Later" },
|
|
],
|
|
providerTurns: [
|
|
{
|
|
id: "provider-turn-active",
|
|
runAttemptId: "attempt-active",
|
|
status: "running",
|
|
},
|
|
],
|
|
providerThreads: [
|
|
{
|
|
id: "provider-thread",
|
|
appThreadId: "thread",
|
|
providerSessionId: "provider-session",
|
|
},
|
|
],
|
|
providerSessions: [
|
|
{
|
|
id: "provider-session",
|
|
status: "running",
|
|
capabilities: capabilities({ queued: true, restartSteer: true }),
|
|
},
|
|
],
|
|
} as never);
|
|
|
|
expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
|
|
["first", "First"],
|
|
["later", "Later"],
|
|
]);
|
|
expect(state.queuedRuns.map(({ attachments }) => attachments.map(({ id }) => id))).toEqual([
|
|
["attachment-first"],
|
|
[],
|
|
]);
|
|
expect(state.activeRun?.id).toBe("active");
|
|
expect(state.canReorder).toBe(true);
|
|
expect(state.canPromoteToSteer).toBe(true);
|
|
});
|
|
|
|
it("keeps held messages visible and clears the hold when they leave the queue", () => {
|
|
for (const status of ["queued", "cancelled", "starting"] as const) {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: null },
|
|
runs: [{ id: "held", status, userMessageId: "message", ordinal: 1, queueHeld: true }],
|
|
messages: [{ id: "message", text: "Saved message" }],
|
|
providerThreads: [],
|
|
providerTurns: [],
|
|
providerSessions: [],
|
|
} as never);
|
|
expect(state.isHeld).toBe(status === "queued");
|
|
expect(state.queuedRuns.map(({ text }) => text)).toEqual(
|
|
status === "queued" ? ["Saved message"] : [],
|
|
);
|
|
}
|
|
});
|
|
|
|
it("hides automatic completion delivery from the visible queue", () => {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: null },
|
|
runs: [
|
|
{
|
|
id: "provider-wake",
|
|
status: "queued",
|
|
userMessageId: "provider-wake",
|
|
ordinal: 4,
|
|
queuePosition: 3,
|
|
},
|
|
{
|
|
id: "automatic",
|
|
status: "queued",
|
|
userMessageId: "message-automatic",
|
|
ordinal: 2,
|
|
queuePosition: 1,
|
|
},
|
|
{
|
|
id: "visible",
|
|
status: "queued",
|
|
userMessageId: "message-visible",
|
|
ordinal: 3,
|
|
queuePosition: 2,
|
|
},
|
|
],
|
|
messages: [
|
|
{
|
|
id: "provider-wake",
|
|
text: "Model-facing text",
|
|
notification: {
|
|
source: { kind: "monitor" },
|
|
outcome: "updated",
|
|
summary: "Monitor updated",
|
|
},
|
|
},
|
|
{
|
|
id: "message-automatic",
|
|
text: "A delegated task reached a terminal state.",
|
|
delegatedCompletion: {
|
|
generation: 1,
|
|
parentRunId: "run:parent",
|
|
taskIds: ["task:child"],
|
|
},
|
|
},
|
|
{ id: "message-visible", text: "Visible queued message" },
|
|
],
|
|
providerTurns: [],
|
|
providerThreads: [],
|
|
providerSessions: [],
|
|
} as never);
|
|
|
|
expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
|
|
["visible", "Visible queued message"],
|
|
]);
|
|
});
|
|
|
|
it("removes only the promoted head from the visible queue", () => {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
|
|
runs: [
|
|
{
|
|
id: "promoted",
|
|
status: "starting",
|
|
userMessageId: "message-promoted",
|
|
providerThreadId: "provider-thread",
|
|
ordinal: 2,
|
|
queuePosition: null,
|
|
},
|
|
{
|
|
id: "still-queued",
|
|
status: "queued",
|
|
userMessageId: "message-still-queued",
|
|
providerThreadId: "provider-thread",
|
|
ordinal: 3,
|
|
queuePosition: 2,
|
|
},
|
|
],
|
|
messages: [
|
|
{ id: "message-promoted", text: "Run now" },
|
|
{ id: "message-still-queued", text: "Wait longer" },
|
|
],
|
|
providerTurns: [],
|
|
providerThreads: [],
|
|
providerSessions: [],
|
|
} as never);
|
|
|
|
expect(state.activeRun?.id).toBe("promoted");
|
|
expect(state.queuedRuns.map(({ run, text }) => [run.id, text])).toEqual([
|
|
["still-queued", "Wait longer"],
|
|
]);
|
|
});
|
|
|
|
it.each(["preparing", "starting", "waiting"] as const)(
|
|
"does not promote queued work into a %s run",
|
|
(status) => {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
|
|
runs: [
|
|
{
|
|
id: "active",
|
|
status,
|
|
providerThreadId: "provider-thread",
|
|
activeAttemptId: "attempt-active",
|
|
ordinal: 1,
|
|
},
|
|
{ id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
|
|
],
|
|
messages: [{ id: "message", text: "Queued" }],
|
|
providerTurns: [
|
|
{
|
|
id: "provider-turn-active",
|
|
runAttemptId: "attempt-active",
|
|
status: status === "waiting" ? "completed" : "starting",
|
|
},
|
|
],
|
|
providerThreads: [
|
|
{
|
|
id: "provider-thread",
|
|
appThreadId: "thread",
|
|
providerSessionId: "provider-session",
|
|
},
|
|
],
|
|
providerSessions: [
|
|
{
|
|
id: "provider-session",
|
|
status: "running",
|
|
capabilities: capabilities({ queued: true, steer: true }),
|
|
},
|
|
],
|
|
} as never);
|
|
|
|
expect(state.canPromoteToSteer).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("does not promote queued work until the running provider turn is projected", () => {
|
|
const state = deriveThreadQueueWorkflowState({
|
|
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
|
|
runs: [
|
|
{
|
|
id: "active",
|
|
status: "running",
|
|
providerThreadId: "provider-thread",
|
|
activeAttemptId: "attempt-active",
|
|
ordinal: 1,
|
|
},
|
|
{ id: "queued", status: "queued", userMessageId: "message", ordinal: 2 },
|
|
],
|
|
messages: [{ id: "message", text: "Queued" }],
|
|
providerTurns: [],
|
|
providerThreads: [
|
|
{
|
|
id: "provider-thread",
|
|
appThreadId: "thread",
|
|
providerSessionId: "provider-session",
|
|
},
|
|
],
|
|
providerSessions: [
|
|
{
|
|
id: "provider-session",
|
|
status: "running",
|
|
capabilities: capabilities({ queued: true, steer: true }),
|
|
},
|
|
],
|
|
} as never);
|
|
|
|
expect(state.canPromoteToSteer).toBe(false);
|
|
});
|
|
|
|
it("does not expose known unsupported queue or fork actions", () => {
|
|
const projection = {
|
|
thread: { id: "thread", activeProviderThreadId: "provider-thread" },
|
|
runs: [{ id: "queued", status: "queued", userMessageId: "message", ordinal: 1 }],
|
|
messages: [],
|
|
providerThreads: [
|
|
{
|
|
id: "provider-thread",
|
|
appThreadId: "thread",
|
|
providerSessionId: "provider-session",
|
|
},
|
|
],
|
|
providerSessions: [
|
|
{
|
|
id: "provider-session",
|
|
status: "ready",
|
|
capabilities: capabilities(),
|
|
},
|
|
],
|
|
} as never;
|
|
const queue = deriveThreadQueueWorkflowState(projection);
|
|
const projectedItem = {
|
|
item: { type: "assistant_message", runId: "run", status: "completed" },
|
|
} as never;
|
|
|
|
expect(queue.canReorder).toBe(false);
|
|
expect(queue.canPromoteToSteer).toBe(false);
|
|
expect(canForkProjectedAssistantItem({ projectedItem, capabilities: capabilities() })).toBe(
|
|
false,
|
|
);
|
|
expect(canDetachThreadProviderSession(projection)).toBe(true);
|
|
});
|
|
|
|
it("allows native, portable, and capability-unknown exact-run forks", () => {
|
|
const projectedItem = {
|
|
item: { type: "assistant_message", runId: "run", status: "completed" },
|
|
} as never;
|
|
|
|
expect(
|
|
canForkProjectedAssistantItem({
|
|
projectedItem,
|
|
capabilities: capabilities({ nativeFork: true }),
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
canForkProjectedAssistantItem({
|
|
projectedItem,
|
|
capabilities: capabilities({ portableFork: true }),
|
|
}),
|
|
).toBe(true);
|
|
expect(canForkProjectedAssistantItem({ projectedItem })).toBe(true);
|
|
expect(
|
|
canForkProjectedAssistantItem({
|
|
projectedItem: {
|
|
item: { type: "assistant_message", runId: "run", status: "running" },
|
|
} as never,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("merges the newest provider-finished run while checkpoint capture is pending", () => {
|
|
const projection = {
|
|
runs: [
|
|
{ id: "newest-queued", status: "queued", ordinal: 3 },
|
|
{ id: "older-completed", status: "completed", ordinal: 1 },
|
|
{ id: "newest-finished", status: "waiting", ordinal: 2 },
|
|
],
|
|
} as never;
|
|
|
|
expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
|
|
});
|
|
|
|
it("does not let a stale completed run later in storage order hide the waiting checkpoint", () => {
|
|
const projection = {
|
|
runs: [
|
|
{ id: "newest-finished", status: "waiting", ordinal: 2 },
|
|
{ id: "older-completed", status: "completed", ordinal: 1 },
|
|
],
|
|
} as never;
|
|
|
|
expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished");
|
|
});
|
|
|
|
it.each(["preparing", "starting", "running"] as const)(
|
|
"does not merge older history while a newer run is %s",
|
|
(status) => {
|
|
const projection = {
|
|
runs: [
|
|
{ id: "older-completed", status: "completed", ordinal: 1 },
|
|
{ id: "newer-active", status, ordinal: 2 },
|
|
],
|
|
} as never;
|
|
|
|
expect(resolveLatestMergeBackRun(projection)).toBeNull();
|
|
},
|
|
);
|
|
});
|