Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import type {
4 EnvironmentId,
5 OrchestrationV2ThreadShell,
6 Project,
7 ThreadId,
8} from "@t3tools/contracts";
9import { ProviderInstanceId, RuntimeRequestId } from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11
12import { projectThreadAwarenessV2 } from "./agentAwareness.ts";
13
14const NOW = "2026-05-22T12:00:00.000Z";
15
16const project = {
17 title: "t3code",
18} satisfies Pick<Project, "title">;
19
20describe("projectThreadAwarenessV2", () => {
21 const updatedAt = DateTime.makeUnsafe(NOW);
22 const v2Thread = (
23 overrides: Partial<
24 Pick<
25 OrchestrationV2ThreadShell,
26 | "activityRunStatus"
27 | "latestRunBackgroundWake"
28 | "latestRunStopRequested"
29 | "status"
30 | "pendingBackgroundTasks"
31 | "pendingRuntimeRequest"
32 | "lineage"
33 | "settledOverride"
34 >
35 > = {},
36 ) => ({
37 id: "thread-2" as ThreadId,
38 lineage: {
39 rootThreadId: "thread-2" as ThreadId,
40 parentThreadId: null,
41 relationshipToParent: null,
42 },
43 title: "Integrate orchestration",
44 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
45 status: "running" as const,
46 pendingRuntimeRequest: null,
47 settledOverride: null,
48 updatedAt,
49 ...overrides,
50 });
51
52 it("does not announce completion after a manual stop", () => {
53 const input = {
54 environmentId: "env-1" as EnvironmentId,
55 project,
56 thread: v2Thread({ status: "completed" }),
57 };
58 expect(projectThreadAwarenessV2(input)?.phase).toBe("completed");
59 expect(
60 projectThreadAwarenessV2({
61 ...input,
62 thread: { ...input.thread, latestRunStopRequested: true },
63 }),
64 ).toBeNull();
65 });
66
67 it.each([
68 ["holds a wake run", true, [{ taskId: "watch", kind: "monitor" as const }], "running"],
69 ["completes a wake run once its monitor ends", true, [], "completed"],
70 [
71 "holds a wake run while a monitor remains beside other work",
72 true,
73 [
74 { taskId: "build", kind: "command" as const },
75 { taskId: "watch", kind: "monitor" as const },
76 ],
77 "running",
78 ],
79 [
80 "completes a wake run with only a command pending",
81 true,
82 [{ taskId: "build", kind: "command" as const }],
83 "completed",
84 ],
85 [
86 "completes a run the user started",
87 false,
88 [{ taskId: "watch", kind: "monitor" as const }],
89 "completed",
90 ],
91 ] as const)("%s while a monitor wakes the thread", (_, latestRunBackgroundWake, tasks, phase) => {
92 expect(
93 projectThreadAwarenessV2({
94 environmentId: "env-1" as EnvironmentId,
95 project,
96 thread: v2Thread({
97 status: "completed",
98 latestRunBackgroundWake,
99 pendingBackgroundTasks: [...tasks],
100 }),
101 })?.phase,
102 ).toBe(phase);
103 });
104
105 it.each([
106 ["running", "subagent"],
107 ["completed", "subagent"],
108 ["failed", "subagent"],
109 ["running", "side chat"],
110 ] as const)("does not publish %s %s activity", (status, kind) => {
111 expect(
112 projectThreadAwarenessV2({
113 environmentId: "env-1" as EnvironmentId,
114 project,
115 thread: v2Thread({
116 status,
117 lineage: {
118 rootThreadId: "parent" as ThreadId,
119 parentThreadId: "parent" as ThreadId,
120 ...(kind === "subagent"
121 ? { relationshipToParent: "subagent" }
122 : { relationshipToParent: "fork", sideChat: true }),
123 },
124 }),
125 }),
126 ).toBeNull();
127 });
128
129 it("leaves a linked environment's task outcome to its parent", () => {
130 const delegatedFrom = {
131 environmentId: "env-parent" as EnvironmentId,
132 threadId: "parent" as ThreadId,
133 title: "Parent",
134 };
135 const phaseFor = (status: "running" | "completed" | "failed") =>
136 projectThreadAwarenessV2({
137 environmentId: "env-1" as EnvironmentId,
138 project,
139 thread: { ...v2Thread({ status }), delegatedFrom },
140 })?.phase ?? null;
141 expect(phaseFor("running")).toBe("running");
142 expect(phaseFor("completed")).toBeNull();
143 expect(phaseFor("failed")).toBeNull();
144 });
145
146 it("still reports outcomes for threads stored with a null delegatedFrom", () => {
147 expect(
148 projectThreadAwarenessV2({
149 environmentId: "env-1" as EnvironmentId,
150 project,
151 // MCP-launched threads store `delegatedFrom: null` at runtime.
152 thread: { ...v2Thread({ status: "completed" }), delegatedFrom: null as never },
153 })?.phase,
154 ).toBe("completed");
155 });
156
157 it("keeps an older activity run visible over a newer cancelled run", () => {
158 expect(
159 projectThreadAwarenessV2({
160 environmentId: "env-1" as EnvironmentId,
161 project,
162 thread: v2Thread({ status: "cancelled", activityRunStatus: "running" }),
163 }),
164 ).toMatchObject({ phase: "running", headline: "Agent is working" });
165 });
166
167 it.each([
168 ["completed", "completed", false],
169 ["failed", "failed", false],
170 ["monitor-held", "completed", true],
171 ] as const)("removes settled %s work", (_, status, latestRunBackgroundWake) => {
172 expect(
173 projectThreadAwarenessV2({
174 environmentId: "env-1" as EnvironmentId,
175 project,
176 thread: v2Thread({
177 status,
178 settledOverride: "settled",
179 latestRunBackgroundWake,
180 pendingBackgroundTasks: [{ taskId: "watch", kind: "monitor" }],
181 }),
182 }),
183 ).toBeNull();
184 });
185
186 it.each(["starting", "running", "waiting"] as const)(
187 "keeps settled %s work visible",
188 (status) => {
189 expect(
190 projectThreadAwarenessV2({
191 environmentId: "env-1" as EnvironmentId,
192 project,
193 thread: v2Thread({ status, settledOverride: "settled" }),
194 }),
195 ).toMatchObject({ phase: status === "starting" ? "starting" : "running" });
196 },
197 );
198
199 it.each(["user_input", "command"] as const)(
200 "keeps a settled terminal thread with a pending %s request",
201 (kind) => {
202 expect(
203 projectThreadAwarenessV2({
204 environmentId: "env-1" as EnvironmentId,
205 project,
206 thread: v2Thread({
207 status: "completed",
208 settledOverride: "settled",
209 pendingRuntimeRequest: {
210 id: RuntimeRequestId.make("request-settled"),
211 kind,
212 createdAt: updatedAt,
213 },
214 }),
215 }),
216 ).toMatchObject({
217 phase: kind === "user_input" ? "waiting_for_input" : "waiting_for_approval",
218 });
219 },
220 );
221
222 it("keeps settled work held open by an awaited monitor", () => {
223 expect(
224 projectThreadAwarenessV2({
225 environmentId: "env-1" as EnvironmentId,
226 project,
227 thread: v2Thread({
228 status: "completed",
229 settledOverride: "settled",
230 pendingBackgroundTasks: [{ taskId: "watch", kind: "monitor", awaited: true }],
231 }),
232 }),
233 ).toMatchObject({ phase: "running" });
234 });
235
236 it.each([
237 ["only a dev server", "completed", [{ taskId: "dev", kind: "command" }]],
238 ["a dev server's watchdog monitor", "completed", [{ taskId: "watch", kind: "monitor" }]],
239 [
240 "a command the agent waits on",
241 "running",
242 [{ taskId: "tests", kind: "command", awaited: true }],
243 ],
244 [
245 "a dev server and a subagent",
246 "running",
247 [
248 { taskId: "dev", kind: "command" },
249 { taskId: "review", kind: "subagent" },
250 ],
251 ],
252 ] as const)("reports a completed run waiting on %s as %s", (_case, phase, tasks) => {
253 expect(
254 projectThreadAwarenessV2({
255 environmentId: "env-1" as EnvironmentId,
256 project,
257 thread: v2Thread({ status: "completed", pendingBackgroundTasks: tasks }),
258 }),
259 ).toMatchObject({ phase });
260 });
261
262 it("reports an async question as input while the run keeps working", () => {
263 const input = {
264 environmentId: "env-1" as EnvironmentId,
265 project,
266 thread: v2Thread({
267 pendingRuntimeRequest: {
268 id: RuntimeRequestId.make("request-async"),
269 kind: "user_input",
270 createdAt: updatedAt,
271 blocking: false,
272 },
273 }),
274 };
275 expect(projectThreadAwarenessV2(input)).toMatchObject({
276 phase: "waiting_for_input",
277 headline: "Waiting for input",
278 });
279 });
280
281 it("does not present authentication refreshes as user approvals", () => {
282 expect(
283 projectThreadAwarenessV2({
284 environmentId: "env-1" as EnvironmentId,
285 project,
286 thread: v2Thread({
287 pendingRuntimeRequest: {
288 id: RuntimeRequestId.make("request-auth-refresh"),
289 kind: "auth_refresh",
290 createdAt: updatedAt,
291 },
292 }),
293 }),
294 ).toMatchObject({ phase: "running", headline: "Agent is working" });
295 });
296});