@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 3.7 KB
Raw
1 import { assert } from "@effect/vitest";
2 import type { OrchestrationV2ThreadProjection, ProviderReplayTranscript } from "@t3tools/contracts";
3
4 import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts";
5 import {
6 assertBaseProjection,
7 assertProviderNativeSubagentRootTurns,
8 assertSemanticProjectionIntegrity,
9 assertUserMessagesInclude,
10 backgroundNotifications,
11 OPENCODE2_BACKGROUND_PROMPT,
12 projectionFor,
13 } from "../shared.ts";
14
15 const runTexts = (projection: OrchestrationV2ThreadProjection, runId: string | undefined) =>
16 projection.turnItems.flatMap((item) =>
17 item.runId === runId && item.type === "assistant_message" ? [item.text.trim()] : [],
18 );
19
20 export function assertOpenCode2BackgroundOutput(
21 result: OrchestratorV2ScenarioResult,
22 transcript: ProviderReplayTranscript,
23 ) {
24 assertBaseProjection({
25 result,
26 transcript,
27 runCount: 2,
28 runStatuses: ["completed", "completed"],
29 });
30 const projection = projectionFor(result, transcript.scenario);
31 assertSemanticProjectionIntegrity(projection);
32 assertProviderNativeSubagentRootTurns(result);
33 assertUserMessagesInclude(projection, [OPENCODE2_BACKGROUND_PROMPT]);
34 const [rootRun, wakeRun] = projection.runs;
35
36 assert.lengthOf(projection.subagents, 1);
37 const subagent = projection.subagents[0]!;
38 assert.deepInclude(subagent, {
39 origin: "provider_native",
40 status: "completed",
41 runId: rootRun!.id,
42 title: "Sleep and reply",
43 result: "CHILD_OK",
44 });
45 assert.deepEqual(runTexts(projection, rootRun?.id), ["PARENT_OK"]);
46
47 const indexOf = (predicate: (event: (typeof result.domainEvents)[number]) => boolean) =>
48 result.domainEvents.findIndex(predicate);
49 const rootWaiting = indexOf(
50 (event) =>
51 event.type === "run.updated" &&
52 event.payload.id === rootRun?.id &&
53 event.payload.status === "waiting",
54 );
55 const subagentDone = indexOf(
56 (event) =>
57 event.type === "turn-item.updated" &&
58 event.payload.type === "subagent" &&
59 event.payload.status === "completed",
60 );
61 const wakeStarted = indexOf(
62 (event) => event.type === "run.updated" && event.payload.id === wakeRun?.id,
63 );
64 assert.isAtLeast(rootWaiting, 0);
65 assert.isAbove(subagentDone, rootWaiting, "run 1 ends its turn before its subagent ends");
66 assert.isAbove(wakeStarted, subagentDone, "the wake follows the subagent's end");
67
68 const child = result.projections.get(subagent.childThreadId!);
69 assert.isDefined(child);
70 assert.isTrue(
71 child.turnItems.some(
72 (item) => item.type === "command_execution" && item.input.includes("sleep 20"),
73 ),
74 );
75 assert.isTrue(
76 child.turnItems.some((item) => item.type === "assistant_message" && item.text === "CHILD_OK"),
77 );
78 assert.isFalse(
79 projection.turnItems.some((item) => item.type === "command_execution"),
80 "the child's shell call must not land on the parent",
81 );
82
83 const wakeMessage = projection.messages.find((message) => message.id === wakeRun?.userMessageId);
84 assert.equal(`${wakeMessage?.createdBy}:${wakeMessage?.creationSource}`, "agent:provider");
85 assert.include(wakeMessage?.text ?? "", "CHILD_OK");
86 assert.deepEqual(backgroundNotifications(projection), [
87 {
88 summary: 'Subagent "Sleep and reply" finished',
89 outcome: "completed",
90 source: { kind: "subagent", childThreadId: subagent.childThreadId! },
91 },
92 ]);
93 assert.deepEqual(runTexts(projection, wakeRun?.id), [
94 "The background subagent finished and returned `CHILD_OK`.",
95 ]);
96
97 const shell = result.shellSnapshot.threads.find((thread) => thread.id === projection.thread.id);
98 assert.deepEqual(shell?.pendingBackgroundTasks ?? [], []);
99 assert.isFalse(
100 projection.turnItems.some((item) => item.status === "running" || item.status === "waiting"),
101 );
102 }