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

SubagentProjection.test.ts

284 lines9.2 KB

1import { assert, it } from "@effect/vitest";
2import {
3 type ModelSelection,
4 NodeId,
5 RunId,
6 RunAttemptId,
7 MessageId,
8 EventId,
9 TurnItemId,
10 type OrchestrationV2Run,
11 type OrchestrationV2AppThread,
12 ProjectId,
13 ProviderInstanceId,
14 ProviderThreadId,
15 ThreadId,
16} from "@t3tools/contracts";
17import * as DateTime from "effect/DateTime";
18​
19import {
20 makeSubagentChildThread,
21 delegatedTaskProgress,
22 subagentResultForRun,
23 makeSubagentConversationArtifacts,
24} from "./SubagentProjection.ts";
25​
26import { emptyProjection } from "./ProjectionStore.ts";
27​
28const parentThreadId = ThreadId.make("thread:subagent-snoozed-parent");
29const childThreadId = ThreadId.make("thread:subagent-awake-child");
30const parentProviderInstanceId = ProviderInstanceId.make("codex");
31const childProviderInstanceId = ProviderInstanceId.make("claude");
32const parentModelSelection = {
33 instanceId: parentProviderInstanceId,
34 model: "gpt-5.4",
35} satisfies ModelSelection;
36const childModelSelection = {
37 instanceId: childProviderInstanceId,
38 model: "claude-opus-4-1",
39} satisfies ModelSelection;
40const parentCreatedAt = DateTime.makeUnsafe("2026-07-24T09:00:00.000Z");
41const snoozedAt = DateTime.makeUnsafe("2026-07-24T09:05:00.000Z");
42const snoozedUntil = DateTime.makeUnsafe("2026-07-25T09:00:00.000Z");
43const childCreatedAt = DateTime.makeUnsafe("2026-07-24T09:10:00.000Z");
44​
45function makeParentThread(): OrchestrationV2AppThread {
46 return {
47 createdBy: "user",
48 creationSource: "web",
49 id: parentThreadId,
50 projectId: ProjectId.make("project:subagent-snooze"),
51 title: "Snoozed parent",
52 providerInstanceId: parentProviderInstanceId,
53 modelSelection: parentModelSelection,
54 runtimeMode: "full-access",
55 interactionMode: "plan",
56 branch: "feature/source",
57 worktreePath: "/tmp/source-worktree",
58 activeProviderThreadId: ProviderThreadId.make("provider-thread:subagent-snoozed-parent"),
59 lineage: {
60 parentThreadId: null,
61 relationshipToParent: null,
62 rootThreadId: parentThreadId,
63 },
64 forkedFrom: null,
65 createdAt: parentCreatedAt,
66 updatedAt: snoozedAt,
67 archivedAt: null,
68 settledOverride: null,
69 settledAt: null,
70 lastVisitedAt: null,
71 snoozedUntil,
72 snoozedAt,
73 deletedAt: null,
74 historyOrigin: "v1_import",
75 };
77​
78it("keeps a subagent child awake when its parent thread is snoozed", () => {
79 const parentThread = makeParentThread();
80 const childProviderThreadId = ProviderThreadId.make("provider-thread:subagent-awake-child");
81 const parentNodeId = NodeId.make("node:subagent-parent");
82 const childThread = makeSubagentChildThread({
83 parentThread,
84 childThreadId,
85 parentNodeId,
86 activeProviderThreadId: childProviderThreadId,
87 providerInstanceId: childProviderInstanceId,
88 modelSelection: childModelSelection,
89 title: "Awake child",
90 now: childCreatedAt,
91 createdBy: "agent",
92 creationSource: "provider",
93 });
94​
95 assert.isNull(childThread.snoozedUntil);
96 assert.isNull(childThread.snoozedAt);
97 assert.equal(childThread.projectId, parentThread.projectId);
98 assert.equal(childThread.runtimeMode, parentThread.runtimeMode);
99 assert.equal(childThread.interactionMode, parentThread.interactionMode);
100 assert.equal(childThread.branch, parentThread.branch);
101 assert.equal(childThread.worktreePath, parentThread.worktreePath);
102 assert.equal(childThread.providerInstanceId, childProviderInstanceId);
103 assert.deepEqual(childThread.modelSelection, childModelSelection);
104 assert.equal(childThread.activeProviderThreadId, childProviderThreadId);
105 assert.isUndefined(childThread.historyOrigin);
106 assert.deepEqual(childThread.lineage, {
107 parentThreadId,
108 relationshipToParent: "subagent",
109 rootThreadId: parentThreadId,
110 });
111 assert.deepEqual(childThread.forkedFrom, {
112 type: "node",
113 nodeId: parentNodeId,
114 });
115});
116​
117it("attributes native subagent prompts to their parent thread", () => {
118 for (const role of ["user", "assistant"] as const) {
119 const artifacts = makeSubagentConversationArtifacts({
120 messageId: MessageId.make(`native-${role}`),
121 turnItemId: TurnItemId.make(`native-${role}`),
122 threadId: childThreadId,
123 senderThreadId: parentThreadId,
124 rootNodeId: NodeId.make("child-root"),
125 providerThreadId: null,
126 providerTurnId: null,
127 nativeItemRef: null,
128 role,
129 text: role === "user" ? "Review the changes" : "Review complete",
130 ordinal: 100,
131 now: childCreatedAt,
132 });
133 assert.equal(artifacts.message.threadId, childThreadId);
134 assert.equal(artifacts.message.senderThreadId, role === "user" ? parentThreadId : undefined);
135 if (artifacts.turnItem.type === "user_message") {
136 assert.equal(artifacts.turnItem.senderThreadId, parentThreadId);
137 }
138 }
139});
140​
141function taskFixture() {
142 const projection = emptyProjection({
143 type: "thread.created",
144 id: EventId.make("task-fixture"),
145 threadId: parentThreadId,
146 occurredAt: parentCreatedAt,
147 payload: makeParentThread(),
148 });
149 const run: OrchestrationV2Run = {
150 id: RunId.make("initial"),
151 threadId: parentThreadId,
152 ordinal: 1,
153 providerInstanceId: parentProviderInstanceId,
154 modelSelection: parentModelSelection,
155 providerThreadId: null,
156 userMessageId: MessageId.make("prompt"),
157 rootNodeId: NodeId.make("root"),
158 activeAttemptId: RunAttemptId.make("attempt"),
159 status: "completed",
160 requestedAt: parentCreatedAt,
161 startedAt: parentCreatedAt,
162 completedAt: childCreatedAt,
163 checkpointId: null,
164 contextHandoffId: null,
165 };
166 return { projection: { ...projection, runs: [run] }, run };
168​
169it("waits for nested work and retains the report across monitor acknowledgements", () => {
170 const { projection, run } = taskFixture();
171 assert.equal(
172 delegatedTaskProgress({ ...projection, subagents: [{ status: "running" }] }).state,
173 "waiting_for_children",
174 );
175 assert.equal(
176 delegatedTaskProgress({ ...projection, subagents: [{ status: "completed" }] }).state,
177 "result_available",
178 );
179 assert.equal(
180 delegatedTaskProgress({ ...projection, subagents: [{ status: "idle" }] }).state,
181 "result_available",
182 );
183 for (const state of ["pending", "claimed"] as const) {
184 assert.equal(
185 delegatedTaskProgress({
186 ...projection,
187 subagents: [{ status: "completed", completionDelivery: { state, observedByRunId: null } }],
188 }).state,
189 "waiting_for_children",
190 );
191 }
192 for (const state of ["acknowledged", "delivered", "disposed"] as const) {
193 assert.equal(
194 delegatedTaskProgress({
195 ...projection,
196 subagents: [{ status: "completed", completionDelivery: { state, observedByRunId: null } }],
197 }).state,
198 "result_available",
199 );
200 }
201 assert.equal(
202 delegatedTaskProgress({
203 ...projection,
204 providerThreads: [{ pendingBackgroundTasks: [{ taskId: "background-audit" }] }],
205 }).state,
206 "waiting_for_children",
207 );
208 const pending = {
209 ...run,
210 id: RunId.make("followup"),
211 ordinal: 2,
212 status: "queued" as const,
213 startedAt: null,
214 };
215 assert.equal(delegatedTaskProgress({ ...projection, runs: [run, pending] }).state, "working");
216 const report = { ...pending, status: "completed" as const, startedAt: parentCreatedAt };
217 const monitor = { ...report, id: RunId.make("monitor"), ordinal: 3 };
218 const artifacts = makeSubagentConversationArtifacts({
219 messageId: MessageId.make("monitor-message"),
220 turnItemId: TurnItemId.make("monitor-item"),
221 threadId: parentThreadId,
222 rootNodeId: NodeId.make("root"),
223 providerThreadId: null,
224 providerTurnId: null,
225 nativeItemRef: null,
226 role: "user",
227 text: "Monitor update",
228 ordinal: 3,
229 now: childCreatedAt,
230 });
231 const progress = delegatedTaskProgress({
232 ...projection,
233 runs: [run, report, monitor],
234 messages: [
235 {
236 ...artifacts.message,
237 runId: monitor.id,
238 notification: {
239 source: { kind: "monitor" },
240 outcome: "updated",
241 summary: "Monitor update",
242 },
243 },
244 ],
245 });
246 assert.equal(progress.state, "result_available");
247 assert.equal(progress.resultRun?.id, report.id);
248});
249​
250it("exposes the provider failure rather than a progress message from the failed run", () => {
251 const { projection, run } = taskFixture();
252 const failedRun = { ...run, status: "failed" as const };
253 const artifacts = makeSubagentConversationArtifacts({
254 messageId: MessageId.make("progress"),
255 turnItemId: TurnItemId.make("error"),
256 threadId: parentThreadId,
257 rootNodeId: NodeId.make("root"),
258 providerThreadId: null,
259 providerTurnId: null,
260 nativeItemRef: null,
261 role: "assistant",
262 text: "Starting the audit.",
263 ordinal: 1,
264 now: childCreatedAt,
265 });
266 if (artifacts.turnItem.type !== "assistant_message") throw new Error("Expected assistant item");
267 const failure = {
268 class: "unknown" as const,
269 message: "You've hit your usage limit. Try again Sep 14 at 7:20 PM.",
270 code: null,
271 retryable: false,
272 };
273 const result = subagentResultForRun(
274 {
275 ...projection,
276 messages: [{ ...artifacts.message, runId: run.id }],
277 turnItems: [{ ...artifacts.turnItem, type: "error", runId: run.id, failure }],
278 },
279 failedRun,
280 );
281 assert.equal(result.text, failure.message);
282 assert.equal(result.turnItemId, artifacts.turnItem.id);
283 assert.isNull(result.messageId);
284});