Open History to see the last commit
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 };
76}
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
117function taskFixture() {
118 const projection = emptyProjection({
119 type: "thread.created",
120 id: EventId.make("task-fixture"),
121 threadId: parentThreadId,
122 occurredAt: parentCreatedAt,
123 payload: makeParentThread(),
124 });
125 const run: OrchestrationV2Run = {
126 id: RunId.make("initial"),
127 threadId: parentThreadId,
128 ordinal: 1,
129 providerInstanceId: parentProviderInstanceId,
130 modelSelection: parentModelSelection,
131 providerThreadId: null,
132 userMessageId: MessageId.make("prompt"),
133 rootNodeId: NodeId.make("root"),
134 activeAttemptId: RunAttemptId.make("attempt"),
135 status: "completed",
136 requestedAt: parentCreatedAt,
137 startedAt: parentCreatedAt,
138 completedAt: childCreatedAt,
139 checkpointId: null,
140 contextHandoffId: null,
141 };
142 return { projection: { ...projection, runs: [run] }, run };
143}
144
145it("waits for nested work and retains the report across monitor acknowledgements", () => {
146 const { projection, run } = taskFixture();
147 assert.equal(
148 delegatedTaskProgress({ ...projection, subagents: [{ status: "running" }] }).state,
149 "waiting_for_children",
150 );
151 assert.equal(
152 delegatedTaskProgress({ ...projection, subagents: [{ status: "completed" }] }).state,
153 "result_available",
154 );
155 for (const state of ["pending", "claimed"] as const) {
156 assert.equal(
157 delegatedTaskProgress({
158 ...projection,
159 subagents: [{ status: "completed", completionDelivery: { state, observedByRunId: null } }],
160 }).state,
161 "waiting_for_children",
162 );
163 }
164 for (const state of ["acknowledged", "delivered", "disposed"] as const) {
165 assert.equal(
166 delegatedTaskProgress({
167 ...projection,
168 subagents: [{ status: "completed", completionDelivery: { state, observedByRunId: null } }],
169 }).state,
170 "result_available",
171 );
172 }
173 assert.equal(
174 delegatedTaskProgress({
175 ...projection,
176 providerThreads: [{ pendingBackgroundTasks: [{ taskId: "background-audit" }] }],
177 }).state,
178 "waiting_for_children",
179 );
180 const pending = {
181 ...run,
182 id: RunId.make("followup"),
183 ordinal: 2,
184 status: "queued" as const,
185 startedAt: null,
186 };
187 assert.equal(delegatedTaskProgress({ ...projection, runs: [run, pending] }).state, "working");
188 const report = { ...pending, status: "completed" as const, startedAt: parentCreatedAt };
189 const monitor = { ...report, id: RunId.make("monitor"), ordinal: 3 };
190 const artifacts = makeSubagentConversationArtifacts({
191 messageId: MessageId.make("monitor-message"),
192 turnItemId: TurnItemId.make("monitor-item"),
193 threadId: parentThreadId,
194 rootNodeId: NodeId.make("root"),
195 providerThreadId: null,
196 providerTurnId: null,
197 nativeItemRef: null,
198 role: "user",
199 text: "Monitor update",
200 ordinal: 3,
201 now: childCreatedAt,
202 });
203 const progress = delegatedTaskProgress({
204 ...projection,
205 runs: [run, report, monitor],
206 messages: [
207 {
208 ...artifacts.message,
209 runId: monitor.id,
210 notification: {
211 source: { kind: "monitor" },
212 outcome: "updated",
213 summary: "Monitor update",
214 },
215 },
216 ],
217 });
218 assert.equal(progress.state, "result_available");
219 assert.equal(progress.resultRun?.id, report.id);
220});
221
222it("exposes the provider failure rather than a progress message from the failed run", () => {
223 const { projection, run } = taskFixture();
224 const failedRun = { ...run, status: "failed" as const };
225 const artifacts = makeSubagentConversationArtifacts({
226 messageId: MessageId.make("progress"),
227 turnItemId: TurnItemId.make("error"),
228 threadId: parentThreadId,
229 rootNodeId: NodeId.make("root"),
230 providerThreadId: null,
231 providerTurnId: null,
232 nativeItemRef: null,
233 role: "assistant",
234 text: "Starting the audit.",
235 ordinal: 1,
236 now: childCreatedAt,
237 });
238 if (artifacts.turnItem.type !== "assistant_message") throw new Error("Expected assistant item");
239 const failure = {
240 class: "unknown" as const,
241 message: "You've hit your usage limit. Try again Sep 14 at 7:20 PM.",
242 code: null,
243 retryable: false,
244 };
245 const result = subagentResultForRun(
246 {
247 ...projection,
248 messages: [{ ...artifacts.message, runId: run.id }],
249 turnItems: [{ ...artifacts.turnItem, type: "error", runId: run.id, failure }],
250 },
251 failedRun,
252 );
253 assert.equal(result.text, failure.message);
254 assert.equal(result.turnItemId, artifacts.turnItem.id);
255 assert.isNull(result.messageId);
256});