Open History to see the last commit
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;
39}
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 assert.deepEqual(ownShell, {
97 kind: "subagent",
98 label: "sleep 150 (subagent agent-1)",
99 id: "agent-1",
100 });
101 assert.isTrue(isAwaitedWork(ownShell));
102 const long = cancelledRosterTaskWork({
103 kind: "command",
104 taskId: "shell-1",
105 description: "d".repeat(400),
106 parentTaskId: "agent-1",
107 }).label;
108 assert.lengthOf(long, 160);
109 assert.isTrue(long.endsWith("(subagent agent-1)"));
110 const longId = cancelledRosterTaskWork({
111 kind: "command",
112 taskId: "shell-1",
113 parentTaskId: "a".repeat(400),
114 }).label;
115 assert.lengthOf(longId, 160);
116 assert.isFalse(isAwaitedWork(cancelledRosterTaskWork({ kind: "command", taskId: "shell-2" })));
117 assert.isTrue(
118 isAwaitedWork(cancelledRosterTaskWork({ kind: "command", taskId: "tests", awaited: true })),
119 );
120 assert.isFalse(isAwaitedWork(cancelledRosterTaskWork({ kind: "monitor", taskId: "watchdog" })));
121 assert.isTrue(
122 isAwaitedWork(cancelledRosterTaskWork({ kind: "monitor", taskId: "ci", awaited: true })),
123 );
124 assert.equal(
125 cancelledTurnItemWork({
126 id: "item-1",
127 type: "subagent",
128 origin: "provider_native",
129 title: "Review",
130 nativeItemRef: { driver: "claudeAgent", nativeId: "agent-1", strength: "strong" },
131 } as never)?.label,
132 "Review (id agent-1)",
133 );
134});
135
136it("keeps separate cancelled tasks that share a kind and label", () => {
137 const first = { kind: "shell" as const, label: "sleep 20", id: "item-1" };
138 const second = { kind: "shell" as const, label: "sleep 20", id: "item-2" };
139 const merged = mergeRestartCancelledBackgroundWork([first], [second, first]);
140 assert.deepEqual(merged, [first, second]);
141 const legacy = { kind: "shell" as const, label: "sleep 20" };
142 assert.lengthOf(mergeRestartCancelledBackgroundWork([legacy], [legacy]), 1);
143});
144
145it("does not repeat the note when a steer restarts the run on a new attempt", () => {
146 const root = run(1, claudeThread, { restartCancelledBackgroundWork: lost });
147 const steered = run(2, claudeThread, {
148 activeAttemptId: RunAttemptId.make("attempt:2b"),
149 });
150 const firstAttempt = RunAttemptId.make("attempt:2a");
151 const pending = (runAttemptIds: ReadonlyArray<RunAttemptId>, delivered: boolean) =>
152 pendingRestartCancelledBackgroundWork({
153 runs: [root, steered],
154 providerTurns: [
155 turnFor(root),
156 ...(delivered
157 ? [
158 {
159 runAttemptId: firstAttempt,
160 providerThreadId: claudeThread,
161 status: "completed" as const,
162 },
163 ]
164 : []),
165 ],
166 compactionMessageIds: new Set(),
167 run: steered,
168 attempts: runAttemptIds.map((id) => ({ id, runId: steered.id })),
169 });
170
171 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], true), []);
172 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], false), lost);
173});
174
175it("counts a prompted mid-turn or chained continuation as delivering the note", () => {
176 const cut = run(1, claudeThread, { status: "cancelled", restartCancelledBackgroundWork: lost });
177 const cutTurn = { ...turnFor(cut), status: "cancelled" as const };
178 const continuation = run(2, claudeThread, { restartContinuationOfRunId: cut.id });
179 assert.isTrue(isRestartNoteContinuation(continuation, [cut, continuation], [cutTurn], []));
180 assert.isFalse(
181 isRestartNoteContinuation(
182 continuation,
183 [{ ...cut, restartCancelledBackgroundWork: [] }, continuation],
184 [cutTurn],
185 [],
186 ),
187 );
188 const later = run(3, claudeThread);
189 const pending = (delivered: boolean) =>
190 pendingRestartCancelledBackgroundWork({
191 runs: [cut, continuation, later],
192 providerTurns: [cutTurn, ...(delivered ? [turnFor(continuation)] : [])],
193 compactionMessageIds: new Set(),
194 run: later,
195 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
196 });
197 assert.deepEqual(pending(true), []);
198 assert.deepEqual(pending(false), lost);
199
200 const unstarted = { ...continuation, status: "cancelled" as const };
201 const chained = run(3, claudeThread, { restartContinuationOfRunId: unstarted.id });
202 assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], [cutTurn], []), {
203 work: lost,
204 settled: false,
205 });
206 assert.isTrue(isRestartNoteContinuation(chained, [cut, unstarted, chained], [cutTurn], []));
207 for (const status of ["running", "cancelled"] as const) {
208 const turns = [cutTurn, { ...turnFor(unstarted), status }];
209 assert.deepEqual(restartContinuationNote(unstarted, [cut, unstarted], turns, []), {
210 work: lost,
211 settled: false,
212 });
213 assert.deepEqual(
214 pendingRestartCancelledBackgroundWork({
215 runs: [cut, unstarted, later],
216 providerTurns: turns,
217 compactionMessageIds: new Set(),
218 run: later,
219 attempts: [{ id: later.activeAttemptId!, runId: later.id }],
220 }),
221 lost,
222 );
223 }
224
225 const steered = { ...unstarted, activeAttemptId: RunAttemptId.make("attempt:replacement") };
226 const attempts = [{ id: unstarted.activeAttemptId!, runId: steered.id }];
227 const turns = [
228 cutTurn,
229 turnFor(unstarted),
230 { ...turnFor(steered), status: "cancelled" as const },
231 ];
232 assert.deepEqual(restartContinuationNote(steered, [cut, steered], turns, attempts).work, []);
233 assert.isFalse(isRestartNoteContinuation(chained, [cut, steered, chained], turns, attempts));
234 assert.deepEqual(
235 pendingRestartCancelledBackgroundWork({
236 runs: [cut, steered, later],
237 providerTurns: turns,
238 compactionMessageIds: new Set(),
239 run: later,
240 attempts,
241 }),
242 [],
243 );
244});