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

agentAwareness.test.ts

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