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

RestartBackgroundNote.test.ts

133 lines4.8 KB

1import { assert, it } from "@effect/vitest";
2import {
3 MessageId,
4 ProviderThreadId,
5 RunAttemptId,
6 RunId,
7 type OrchestrationV2Run,
8} from "@t3tools/contracts";
9​
10import {
11 cancelledRosterTaskWork,
12 cancelledTurnItemWork,
13 pendingRestartCancelledBackgroundWork,
14 restartCancelledBackgroundWorkNote,
15 mergeRestartCancelledBackgroundWork,
16} from "./RestartBackgroundNote.ts";
17​
18const claudeThread = ProviderThreadId.make("provider-thread:claude");
19const codexThread = ProviderThreadId.make("provider-thread:codex");
20const lost = [{ kind: "subagent" as const, label: "Background subagent test" }];
21​
22function run(
23 ordinal: number,
24 providerThreadId: ProviderThreadId,
25 extra: Partial<OrchestrationV2Run> = {},
26): OrchestrationV2Run {
27 return {
28 id: RunId.make(`run:${ordinal}`),
29 ordinal,
30 providerThreadId,
31 userMessageId: MessageId.make(`message:${ordinal}`),
32 activeAttemptId: RunAttemptId.make(`attempt:${ordinal}`),
33 status: "completed",
34 ...extra,
35 } as OrchestrationV2Run;
37​
38const turnFor = (source: OrchestrationV2Run) => ({
39 runAttemptId: source.activeAttemptId,
40 providerThreadId: source.providerThreadId!,
41 status: "completed" as const,
42});
43​
44it("keeps the note for the provider thread that lost the work across a provider switch", () => {
45 const root = run(1, claudeThread, { restartCancelledBackgroundWork: lost });
46 const onCodex = run(2, codexThread);
47 const backOnClaude = run(3, claudeThread);
48 const pending = (target: OrchestrationV2Run, runs: ReadonlyArray<OrchestrationV2Run>) =>
49 pendingRestartCancelledBackgroundWork({
50 runs,
51 providerTurns: runs.filter((source) => source.id !== target.id).map(turnFor),
52 compactionMessageIds: new Set(),
53 run: target,
54 runAttemptIds: target.activeAttemptId === null ? [] : [target.activeAttemptId],
55 });
56​
57 // Codex never lost the work, so it neither receives nor consumes the note.
58 assert.deepEqual(pending(onCodex, [root, onCodex]), []);
59 assert.deepEqual(pending(backOnClaude, [root, onCodex, backOnClaude]), lost);
60 // Once Claude was told, later Claude turns are not.
61 const later = run(4, claudeThread);
62 assert.deepEqual(pending(later, [root, onCodex, backOnClaude, later]), []);
63});
64​
65it("bounds the note so it cannot crowd out the turn's context", () => {
66 const work = Array.from({ length: 25 }, (_, index) => ({
67 kind: "shell" as const,
68 label: `sleep ${index}`,
69 }));
70 const note = restartCancelledBackgroundWorkNote(work);
71 assert.lengthOf(note.split("\n"), 12);
72 assert.isTrue(note.endsWith("- and 15 more"));
73 const command = cancelledTurnItemWork({
74 type: "command_execution",
75 input: "x".repeat(10_000),
76 title: null,
77 } as never);
78 assert.isAtMost(command?.label.length ?? 0, 160);
79});
80​
81it("bounds roster task labels including a long task id", () => {
82 const work = cancelledRosterTaskWork({
83 kind: "command",
84 taskId: "t".repeat(400),
85 description: "d".repeat(400),
86 });
87 assert.equal(work.kind, "shell");
88 assert.lengthOf(work.label, 160);
89 const idOnly = cancelledRosterTaskWork({ kind: "background_task", taskId: "t".repeat(400) });
90 assert.lengthOf(idOnly.label, 160);
91});
92​
93it("keeps separate cancelled tasks that share a kind and label", () => {
94 const first = { kind: "shell" as const, label: "sleep 20", id: "item-1" };
95 const second = { kind: "shell" as const, label: "sleep 20", id: "item-2" };
96 const merged = mergeRestartCancelledBackgroundWork([first], [second, first]);
97 assert.deepEqual(merged, [first, second]);
98 // Rows recorded before ids existed still collapse by kind + label.
99 const legacy = { kind: "shell" as const, label: "sleep 20" };
100 assert.lengthOf(mergeRestartCancelledBackgroundWork([legacy], [legacy]), 1);
101});
102​
103it("does not repeat the note when a steer restarts the run on a new attempt", () => {
104 const root = run(1, claudeThread, { restartCancelledBackgroundWork: lost });
105 const steered = run(2, claudeThread, {
106 activeAttemptId: RunAttemptId.make("attempt:2b"),
107 });
108 const firstAttempt = RunAttemptId.make("attempt:2a");
109 const pending = (runAttemptIds: ReadonlyArray<RunAttemptId>, delivered: boolean) =>
110 pendingRestartCancelledBackgroundWork({
111 runs: [root, steered],
112 providerTurns: [
113 turnFor(root),
114 ...(delivered
115 ? [
116 {
117 runAttemptId: firstAttempt,
118 providerThreadId: claudeThread,
119 status: "completed" as const,
120 },
121 ]
122 : []),
123 ],
124 compactionMessageIds: new Set(),
125 run: steered,
126 runAttemptIds,
127 });
128​
129 // The first attempt reached the provider with the note; its replacement must not repeat it.
130 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], true), []);
131 // A first attempt that never reached the provider did not deliver it.
132 assert.deepEqual(pending([firstAttempt, RunAttemptId.make("attempt:2b")], false), lost);
133});