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 | "latestRunStopRequested"
28 | "status"
29 | "pendingBackgroundTasks"
30 | "pendingRuntimeRequest"
31 | "lineage"
32 | "settledOverride"
33 >
34 > = {},
35 ) => ({
36 id: "thread-2" as ThreadId,
37 lineage: {
38 rootThreadId: "thread-2" as ThreadId,
39 parentThreadId: null,
40 relationshipToParent: null,
41 },
42 title: "Integrate orchestration",
43 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
44 status: "running" as const,
45 pendingRuntimeRequest: null,
46 settledOverride: null,
47 updatedAt,
48 ...overrides,
49 });
50
51 it("does not announce completion after a manual stop", () => {
52 const input = {
53 environmentId: "env-1" as EnvironmentId,
54 project,
55 thread: v2Thread({ status: "completed" }),
56 };
57 expect(projectThreadAwarenessV2(input)?.phase).toBe("completed");
58 expect(
59 projectThreadAwarenessV2({
60 ...input,
61 thread: { ...input.thread, latestRunStopRequested: true },
62 }),
63 ).toBeNull();
64 });
65
66 it.each([
67 ["running", "subagent"],
68 ["completed", "subagent"],
69 ["failed", "subagent"],
70 ["running", "side chat"],
71 ] as const)("does not publish %s %s activity", (status, kind) => {
72 expect(
73 projectThreadAwarenessV2({
74 environmentId: "env-1" as EnvironmentId,
75 project,
76 thread: v2Thread({
77 status,
78 lineage: {
79 rootThreadId: "parent" as ThreadId,
80 parentThreadId: "parent" as ThreadId,
81 ...(kind === "subagent"
82 ? { relationshipToParent: "subagent" }
83 : { relationshipToParent: "fork", sideChat: true }),
84 },
85 }),
86 }),
87 ).toBeNull();
88 });
89
90 it("keeps an older activity run visible over a newer cancelled run", () => {
91 expect(
92 projectThreadAwarenessV2({
93 environmentId: "env-1" as EnvironmentId,
94 project,
95 thread: v2Thread({ status: "cancelled", activityRunStatus: "running" }),
96 }),
97 ).toMatchObject({ phase: "running", headline: "Agent is working" });
98 });
99
100 it.each(["completed", "failed"] as const)("removes settled %s work", (status) => {
101 expect(
102 projectThreadAwarenessV2({
103 environmentId: "env-1" as EnvironmentId,
104 project,
105 thread: v2Thread({ status, settledOverride: "settled" }),
106 }),
107 ).toBeNull();
108 });
109
110 it.each(["starting", "running", "waiting"] as const)(
111 "keeps settled %s work visible",
112 (status) => {
113 expect(
114 projectThreadAwarenessV2({
115 environmentId: "env-1" as EnvironmentId,
116 project,
117 thread: v2Thread({ status, settledOverride: "settled" }),
118 }),
119 ).toMatchObject({ phase: status === "starting" ? "starting" : "running" });
120 },
121 );
122
123 it.each(["user_input", "command"] as const)(
124 "keeps a settled terminal thread with a pending %s request",
125 (kind) => {
126 expect(
127 projectThreadAwarenessV2({
128 environmentId: "env-1" as EnvironmentId,
129 project,
130 thread: v2Thread({
131 status: "completed",
132 settledOverride: "settled",
133 pendingRuntimeRequest: {
134 id: RuntimeRequestId.make("request-settled"),
135 kind,
136 createdAt: updatedAt,
137 },
138 }),
139 }),
140 ).toMatchObject({
141 phase: kind === "user_input" ? "waiting_for_input" : "waiting_for_approval",
142 });
143 },
144 );
145
146 it("keeps settled work held open by an awaited monitor", () => {
147 expect(
148 projectThreadAwarenessV2({
149 environmentId: "env-1" as EnvironmentId,
150 project,
151 thread: v2Thread({
152 status: "completed",
153 settledOverride: "settled",
154 pendingBackgroundTasks: [{ taskId: "watch", kind: "monitor", awaited: true }],
155 }),
156 }),
157 ).toMatchObject({ phase: "running" });
158 });
159
160 it.each([
161 ["only a dev server", "completed", [{ taskId: "dev", kind: "command" }]],
162 ["a dev server's watchdog monitor", "completed", [{ taskId: "watch", kind: "monitor" }]],
163 [
164 "a command the agent waits on",
165 "running",
166 [{ taskId: "tests", kind: "command", awaited: true }],
167 ],
168 [
169 "a dev server and a subagent",
170 "running",
171 [
172 { taskId: "dev", kind: "command" },
173 { taskId: "review", kind: "subagent" },
174 ],
175 ],
176 ] as const)("reports a completed run waiting on %s as %s", (_case, phase, tasks) => {
177 expect(
178 projectThreadAwarenessV2({
179 environmentId: "env-1" as EnvironmentId,
180 project,
181 thread: v2Thread({ status: "completed", pendingBackgroundTasks: tasks }),
182 }),
183 ).toMatchObject({ phase });
184 });
185
186 it("reports an async question as input while the run keeps working", () => {
187 const input = {
188 environmentId: "env-1" as EnvironmentId,
189 project,
190 thread: v2Thread({
191 pendingRuntimeRequest: {
192 id: RuntimeRequestId.make("request-async"),
193 kind: "user_input",
194 createdAt: updatedAt,
195 blocking: false,
196 },
197 }),
198 };
199 expect(projectThreadAwarenessV2(input)).toMatchObject({
200 phase: "waiting_for_input",
201 headline: "Waiting for input",
202 });
203 });
204
205 it("does not present authentication refreshes as user approvals", () => {
206 expect(
207 projectThreadAwarenessV2({
208 environmentId: "env-1" as EnvironmentId,
209 project,
210 thread: v2Thread({
211 pendingRuntimeRequest: {
212 id: RuntimeRequestId.make("request-auth-refresh"),
213 kind: "auth_refresh",
214 createdAt: updatedAt,
215 },
216 }),
217 }),
218 ).toMatchObject({ phase: "running", headline: "Agent is working" });
219 });
220});