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

SubagentProjection.test.ts

286 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: [
205 { pendingBackgroundTasks: [{ taskId: "background-audit", kind: "command" }] },
206 ],
207 }).state,
208 "waiting_for_children",
209 );
210 const pending = {
211 ...run,
212 id: RunId.make("followup"),
213 ordinal: 2,
214 status: "queued" as const,
215 startedAt: null,
216 };
217 assert.equal(delegatedTaskProgress({ ...projection, runs: [run, pending] }).state, "working");
218 const report = { ...pending, status: "completed" as const, startedAt: parentCreatedAt };
219 const monitor = { ...report, id: RunId.make("monitor"), ordinal: 3 };
220 const artifacts = makeSubagentConversationArtifacts({
221 messageId: MessageId.make("monitor-message"),
222 turnItemId: TurnItemId.make("monitor-item"),
223 threadId: parentThreadId,
224 rootNodeId: NodeId.make("root"),
225 providerThreadId: null,
226 providerTurnId: null,
227 nativeItemRef: null,
228 role: "user",
229 text: "Monitor update",
230 ordinal: 3,
231 now: childCreatedAt,
232 });
233 const progress = delegatedTaskProgress({
234 ...projection,
235 runs: [run, report, monitor],
236 messages: [
237 {
238 ...artifacts.message,
239 runId: monitor.id,
240 notification: {
241 source: { kind: "monitor" },
242 outcome: "updated",
243 summary: "Monitor update",
244 },
245 },
246 ],
247 });
248 assert.equal(progress.state, "result_available");
249 assert.equal(progress.resultRun?.id, report.id);
250});
251​
252it("exposes the provider failure rather than a progress message from the failed run", () => {
253 const { projection, run } = taskFixture();
254 const failedRun = { ...run, status: "failed" as const };
255 const artifacts = makeSubagentConversationArtifacts({
256 messageId: MessageId.make("progress"),
257 turnItemId: TurnItemId.make("error"),
258 threadId: parentThreadId,
259 rootNodeId: NodeId.make("root"),
260 providerThreadId: null,
261 providerTurnId: null,
262 nativeItemRef: null,
263 role: "assistant",
264 text: "Starting the audit.",
265 ordinal: 1,
266 now: childCreatedAt,
267 });
268 if (artifacts.turnItem.type !== "assistant_message") throw new Error("Expected assistant item");
269 const failure = {
270 class: "unknown" as const,
271 message: "You've hit your usage limit. Try again Sep 14 at 7:20 PM.",
272 code: null,
273 retryable: false,
274 };
275 const result = subagentResultForRun(
276 {
277 ...projection,
278 messages: [{ ...artifacts.message, runId: run.id }],
279 turnItems: [{ ...artifacts.turnItem, type: "error", runId: run.id, failure }],
280 },
281 failedRun,
282 );
283 assert.equal(result.text, failure.message);
284 assert.equal(result.turnItemId, artifacts.turnItem.id);
285 assert.isNull(result.messageId);
286});