Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RestartBackgroundNote.test.ts

238 lines8.3 KB

1import { assert, it } from "@effect/vitest";
2import {
3 MessageId,
4 ProviderThreadId,
5 RunAttemptId,
6 RunId,
7 type OrchestrationV2Run,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10​
11import {
12 cancelledRosterTaskWork,
13 cancelledTurnItemWork,
14 isAwaitedWork,
15 isRestartNoteContinuation,
16 pendingRestartCancelledBackgroundWork,
17 restartContinuationNote,
18 mergeRestartCancelledBackgroundWork,
19} from "./RestartBackgroundNote.ts";
20​
21const claudeThread = ProviderThreadId.make("provider-thread:claude");
22const codexThread = ProviderThreadId.make("provider-thread:codex");
23const lost = [{ kind: "subagent" as const, label: "Background subagent test" }];
24​
25function run(
26 ordinal: number,
27 providerThreadId: ProviderThreadId,
28 extra: Partial<OrchestrationV2Run> = {},
29): OrchestrationV2Run {
30 return {
31 id: RunId.make(`run:${ordinal}`),
32 ordinal,
33 providerThreadId,
34 userMessageId: MessageId.make(`message:${ordinal}`),
35 activeAttemptId: RunAttemptId.make(`attempt:${ordinal}`),
36 status: "completed",
37 ...extra,
38 } as OrchestrationV2Run;
40​
41const turnFor = (source: OrchestrationV2Run) => ({
42 runAttemptId: source.activeAttemptId,
43 providerThreadId: source.providerThreadId!,
44 status: "completed" as const,
45});
46​
47it("keeps the note for the provider thread that lost the work across a provider switch", () => {
48 const root = run(1, claudeThread, { restartCancelledBackgroundWork: lost });
49 const onCodex = run(2, codexThread);
50 const backOnClaude = run(3, claudeThread);
51 const pending = (target: OrchestrationV2Run, runs: ReadonlyArray<OrchestrationV2Run>) =>
52 pendingRestartCancelledBackgroundWork({
53 runs,
54 providerTurns: runs.filter((source) => source.id !== target.id).map(turnFor),
55 compactionMessageIds: new Set(),
56 run: target,
57 attempts: [{ id: target.activeAttemptId!, runId: target.id }],
58 });
59​
60 assert.deepEqual(pending(onCodex, [root, onCodex]), []);
61 assert.deepEqual(pending(backOnClaude, [root, onCodex, backOnClaude]), lost);
62 const later = run(4, claudeThread);
63 assert.deepEqual(pending(later, [root, onCodex, backOnClaude, later]), []);
64});
65​
66it("delivers a resumed queued run's note even after a higher-ordinal run", () => {
67 const ranFirst = run(3, claudeThread, {
68 completedAt: DateTime.makeUnsafe("2026-10-03T10:00:00.000Z"),
69 });
70 const resumed = run(2, claudeThread, {
71 completedAt: DateTime.makeUnsafe("2026-10-03T10:05:00.000Z"),
72 restartCancelledBackgroundWork: lost,
73 });
74 const next = run(4, claudeThread);
75 const runs = [resumed, ranFirst, next];
76 assert.deepEqual(
77 pendingRestartCancelledBackgroundWork({
78 runs,
79 providerTurns: [resumed, ranFirst].map(turnFor),
80 compactionMessageIds: new Set(),
81 run: next,
82 attempts: [{ id: next.activeAttemptId!, runId: next.id }],
83 }),
84 lost,
85 );
86});
87​
88// regression: #19750 wake threads whose subagents' background work a restart cut
89it("names a lost subagent by the id the agent resumes it with", () => {
90 const ownShell = cancelledRosterTaskWork({
91 kind: "command",
92 taskId: "shell-1",
93 description: "sleep 150",
94 parentTaskId: "agent-1",
95 });
96 // The agent waits on the subagent, which waits on its shell.
97 assert.deepEqual(ownShell, {
98 kind: "subagent",
99 label: "sleep 150 (subagent agent-1)",
100 id: "agent-1",
101 });
102 assert.isTrue(isAwaitedWork(ownShell));
103 const long = cancelledRosterTaskWork({
104 kind: "command",
105 taskId: "shell-1",
106 description: "d".repeat(400),
107 parentTaskId: "agent-1",
108 }).label;
109 assert.lengthOf(long, 160);
110 assert.isTrue(long.endsWith("(subagent agent-1)"));
111 const longId = cancelledRosterTaskWork({
112 kind: "command",
113 taskId: "shell-1",
114 parentTaskId: "a".repeat(400),
115 }).label;
116 assert.lengthOf(longId, 160);
117 assert.isFalse(isAwaitedWork(cancelledRosterTaskWork({ kind: "command", taskId: "shell-2" })));
118 assert.equal(
119 cancelledTurnItemWork({
120 id: "item-1",
121 type: "subagent",
122 origin: "provider_native",
123 title: "Review",
124 nativeItemRef: { driver: "claudeAgent", nativeId: "agent-1", strength: "strong" },
125 } as never)?.label,
126 "Review (id agent-1)",
127 );
128});
129​
130it("keeps separate cancelled tasks that share a kind and label", () => {
131 const first = { kind: "shell" as const, label: "sleep 20", id: "item-1" };
132 const second = { kind: "shell" as const, label: "sleep 20", id: "item-2" };
133 const merged = mergeRestartCancelledBackgroundWork([first], [second, first]);
134 assert.deepEqual(merged, [first, second]);
135 const legacy = { kind: "shell" as const, label: "sleep 20" };
136 assert.lengthOf(mergeRestartCancelledBackgroundWork([legacy], [legacy]), 1);
137});
138​
139it("does not repeat the note when a steer restarts the run on a new attempt", () => {
140 const root = run(1, claudeThread, { restartCancelledBackgroundWork: lost });
141 const steered = run(2, claudeThread, {
142 activeAttemptId: RunAttemptId.make("attempt:2b"),
143 });
144 const firstAttempt = RunAttemptId.make("attempt:2a");
145 const pending = (runAttemptIds: ReadonlyArray<RunAttemptId>, delivered: boolean) =>
146 pendingRestartCancelledBackgroundWork({
147 runs: [root, steered],
148 providerTurns: [
149 turnFor(root),
150 ...(delivered
151 ? [
152 {
153 runAttemptId: firstAttempt,
154 providerThreadId: claudeThread,
155 status: "completed" as const,
156 },
157 ]
158 : []),
159 ],
160 compactionMessageIds: new Set(),
161 run: steered,
162 attempts: runAttemptIds.map((id) => ({ id, runId: steered.id })),
163 });
164​
165 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], true), []);
166 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], false), lost);
167});
168​
169it("counts a prompted mid-turn or chained continuation as delivering the note", () => {
170 const cut = run(1, claudeThread, { status: "cancelled", restartCancelledBackgroundWork: lost });
171 const cutTurn = { ...turnFor(cut), status: "cancelled" as const };
172 const continuation = run(2, claudeThread, { restartContinuationOfRunId: cut.id });
173 assert.isTrue(isRestartNoteContinuation(continuation, [cut, continuation], [cutTurn], []));
174 assert.isFalse(
175 isRestartNoteContinuation(
176 continuation,
177 [{ ...cut, restartCancelledBackgroundWork: [] }, continuation],
178 [cutTurn],
179 [],
180 ),
181 );
182 const later = run(3, claudeThread);
183 const pending = (delivered: boolean) =>
184 pendingRestartCancelledBackgroundWork({
185 runs: [cut, continuation, later],
186 providerTurns: [cutTurn, ...(delivered ? [turnFor(continuation)] : [])],
187 compactionMessageIds: new Set(),
188 run: later,
189 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
190 });
191 assert.deepEqual(pending(true), []);
192 assert.deepEqual(pending(false), lost);
193​
194 const unstarted = { ...continuation, status: "cancelled" as const };
195 const chained = run(3, claudeThread, { restartContinuationOfRunId: unstarted.id });
196 assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], [cutTurn], []), {
197 work: lost,
198 settled: false,
199 });
200 assert.isTrue(isRestartNoteContinuation(chained, [cut, unstarted, chained], [cutTurn], []));
201 for (const status of ["running", "cancelled"] as const) {
202 const turns = [cutTurn, { ...turnFor(unstarted), status }];
203 assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], turns, []), {
204 work: lost,
205 settled: false,
206 });
207 assert.deepEqual(
208 pendingRestartCancelledBackgroundWork({
209 runs: [cut, unstarted, later],
210 providerTurns: turns,
211 compactionMessageIds: new Set(),
212 run: later,
213 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
214 }),
215 lost,
216 );
217 }
218​
219 const steered = { ...unstarted, activeAttemptId: RunAttemptId.make("attempt:replacement") };
220 const attempts = [{ id: unstarted.activeAttemptId!, runId: steered.id }];
221 const turns = [
222 cutTurn,
223 turnFor(unstarted),
224 { ...turnFor(steered), status: "cancelled" as const },
225 ];
226 assert.deepEqual(restartContinuationNote(steered, [cut, steered], turns, attempts).work, []);
227 assert.isFalse(isRestartNoteContinuation(chained, [cut, steered, chained], turns, attempts));
228 assert.deepEqual(
229 pendingRestartCancelledBackgroundWork({
230 runs: [cut, steered, later],
231 providerTurns: turns,
232 compactionMessageIds: new Set(),
233 run: later,
234 attempts,
235 }),
236 [],
237 );
238});