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

agentAwareness.test.ts

233 lines6.7 KB

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", null],
68 ["completed", null],
69 ["failed", "failed"],
70 ] as const)("keeps an idle compaction's %s run quiet unless it fails", (status, phase) => {
71 expect(
72 projectThreadAwarenessV2({
73 environmentId: "env-1" as EnvironmentId,
74 project,
75 thread: { ...v2Thread({ status }), latestRunSilent: true },
76 })?.phase ?? null,
77 ).toBe(phase);
78 });
79​
80 it.each(["running", "completed", "failed"] as const)(
81 "does not publish %s subagent activity",
82 (status) => {
83 expect(
84 projectThreadAwarenessV2({
85 environmentId: "env-1" as EnvironmentId,
86 project,
87 thread: v2Thread({
88 status,
89 lineage: {
90 rootThreadId: "parent" as ThreadId,
91 parentThreadId: "parent" as ThreadId,
92 relationshipToParent: "subagent",
93 },
94 }),
95 }),
96 ).toBeNull();
97 },
98 );
99​
100 it("keeps an older activity run visible over a newer cancelled run", () => {
101 expect(
102 projectThreadAwarenessV2({
103 environmentId: "env-1" as EnvironmentId,
104 project,
105 thread: v2Thread({ status: "cancelled", activityRunStatus: "running" }),
106 }),
107 ).toMatchObject({ phase: "running", headline: "Agent is working" });
108 });
109​
110 it.each(["completed", "failed"] as const)("removes settled %s work", (status) => {
111 expect(
112 projectThreadAwarenessV2({
113 environmentId: "env-1" as EnvironmentId,
114 project,
115 thread: v2Thread({ status, settledOverride: "settled" }),
116 }),
117 ).toBeNull();
118 });
119​
120 it.each(["starting", "running", "waiting"] as const)(
121 "keeps settled %s work visible",
122 (status) => {
123 expect(
124 projectThreadAwarenessV2({
125 environmentId: "env-1" as EnvironmentId,
126 project,
127 thread: v2Thread({ status, settledOverride: "settled" }),
128 }),
129 ).toMatchObject({ phase: status === "starting" ? "starting" : "running" });
130 },
131 );
132​
133 it.each(["user_input", "command"] as const)(
134 "keeps a settled terminal thread with a pending %s request",
135 (kind) => {
136 expect(
137 projectThreadAwarenessV2({
138 environmentId: "env-1" as EnvironmentId,
139 project,
140 thread: v2Thread({
141 status: "completed",
142 settledOverride: "settled",
143 pendingRuntimeRequest: {
144 id: RuntimeRequestId.make("request-settled"),
145 kind,
146 createdAt: updatedAt,
147 },
148 }),
149 }),
150 ).toMatchObject({
151 phase: kind === "user_input" ? "waiting_for_input" : "waiting_for_approval",
152 });
153 },
154 );
155​
156 it("keeps settled work held open by an awaited monitor", () => {
157 expect(
158 projectThreadAwarenessV2({
159 environmentId: "env-1" as EnvironmentId,
160 project,
161 thread: v2Thread({
162 status: "completed",
163 settledOverride: "settled",
164 pendingBackgroundTasks: [{ taskId: "watch", kind: "monitor", awaited: true }],
165 }),
166 }),
167 ).toMatchObject({ phase: "running" });
168 });
169​
170 it.each([
171 ["only a dev server", "completed", [{ taskId: "dev", kind: "command" }]],
172 ["a dev server's watchdog monitor", "completed", [{ taskId: "watch", kind: "monitor" }]],
173 [
174 "a command the agent waits on",
175 "running",
176 [{ taskId: "tests", kind: "command", awaited: true }],
177 ],
178 [
179 "a dev server and a subagent",
180 "running",
181 [
182 { taskId: "dev", kind: "command" },
183 { taskId: "review", kind: "subagent" },
184 ],
185 ],
186 ] as const)("reports a completed run waiting on %s as %s", (_case, phase, tasks) => {
187 expect(
188 projectThreadAwarenessV2({
189 environmentId: "env-1" as EnvironmentId,
190 project,
191 thread: v2Thread({ status: "completed", pendingBackgroundTasks: tasks }),
192 }),
193 ).toMatchObject({ phase });
194 });
195​
196 it("reports an async question as input only after the working run completes", () => {
197 const input = {
198 environmentId: "env-1" as EnvironmentId,
199 project,
200 thread: v2Thread({
201 pendingRuntimeRequest: {
202 id: RuntimeRequestId.make("request-async"),
203 kind: "user_input",
204 createdAt: updatedAt,
205 blocking: false,
206 },
207 }),
208 };
209 expect(projectThreadAwarenessV2(input)).toMatchObject({
210 phase: "running",
211 headline: "Agent is working",
212 });
213 expect(
214 projectThreadAwarenessV2({ ...input, thread: { ...input.thread, status: "completed" } }),
215 ).toMatchObject({ phase: "waiting_for_input", headline: "Waiting for input" });
216 });
217​
218 it("does not present authentication refreshes as user approvals", () => {
219 expect(
220 projectThreadAwarenessV2({
221 environmentId: "env-1" as EnvironmentId,
222 project,
223 thread: v2Thread({
224 pendingRuntimeRequest: {
225 id: RuntimeRequestId.make("request-auth-refresh"),
226 kind: "auth_refresh",
227 createdAt: updatedAt,
228 },
229 }),
230 }),
231 ).toMatchObject({ phase: "running", headline: "Agent is working" });
232 });
233});