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

agentAwareness.test.ts

180 lines5.0 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import type {
4 EnvironmentId,
5 OrchestrationProjectShell,
6 OrchestrationThreadShell,
7 ThreadId,
8 TurnId,
9} from "@t3tools/contracts";
10import { ProviderInstanceId } from "@t3tools/contracts";
11​
12import { projectThreadAwareness } from "./agentAwareness.ts";
13​
14const NOW = "2026-05-22T12:00:00.000Z";
15​
16const project = {
17 title: "t3code",
18} satisfies Pick<OrchestrationProjectShell, "title">;
19​
20function thread(
21 overrides: Partial<OrchestrationThreadShell> = {},
22): Pick<
23 OrchestrationThreadShell,
24 | "id"
25 | "title"
26 | "modelSelection"
27 | "session"
28 | "latestTurn"
29 | "updatedAt"
30 | "hasPendingApprovals"
31 | "hasPendingUserInput"
32> {
33 return {
34 id: "thread-1" as ThreadId,
35 title: "Fix failing CI",
36 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
37 session: null,
38 latestTurn: null,
39 updatedAt: NOW,
40 hasPendingApprovals: false,
41 hasPendingUserInput: false,
42 ...overrides,
43 };
45​
46describe("projectThreadAwareness", () => {
47 it("returns null for idle threads without an active awareness state", () => {
48 expect(
49 projectThreadAwareness({
50 environmentId: "env-1" as EnvironmentId,
51 project,
52 thread: thread(),
53 }),
54 ).toBeNull();
55 });
56​
57 it("prioritizes approval requests over running state", () => {
58 const state = projectThreadAwareness({
59 environmentId: "env-1" as EnvironmentId,
60 project,
61 thread: thread({
62 hasPendingApprovals: true,
63 session: {
64 threadId: "thread-1" as ThreadId,
65 status: "running",
66 providerName: "Codex",
67 runtimeMode: "full-access",
68 activeTurnId: "turn-1" as TurnId,
69 lastError: null,
70 updatedAt: NOW,
71 },
72 }),
73 });
74​
75 expect(state?.phase).toBe("waiting_for_approval");
76 expect(state?.headline).toBe("Approval needed");
77 });
78​
79 it("projects running provider sessions", () => {
80 const state = projectThreadAwareness({
81 environmentId: "env-1" as EnvironmentId,
82 project,
83 thread: thread({
84 session: {
85 threadId: "thread-1" as ThreadId,
86 status: "running",
87 providerName: "Codex",
88 runtimeMode: "full-access",
89 activeTurnId: "turn-1" as TurnId,
90 lastError: null,
91 updatedAt: NOW,
92 },
93 }),
94 });
95​
96 expect(state).toMatchObject({
97 phase: "running",
98 headline: "Agent is working",
99 detail: "Codex is active.",
100 modelTitle: "gpt-5.4",
101 deepLink: "/threads/env-1/thread-1",
102 });
103 });
104​
105 it("projects completed turns as completed even when teardown settled them as interrupted", () => {
106 const finishedTurn = {
107 turnId: "turn-1" as TurnId,
108 state: "interrupted" as const,
109 requestedAt: NOW,
110 startedAt: NOW,
111 completedAt: NOW,
112 assistantMessageId: null,
113 };
114 const state = projectThreadAwareness({
115 environmentId: "env-1" as EnvironmentId,
116 project,
117 thread: thread({ latestTurn: finishedTurn }),
118 });
119​
120 // Session teardown settles still-running turns by session status, and
121 // that write can race turn.completed; the completion timestamp is the
122 // durable signal. Without this the thread resolves to null persistently
123 // and gets tombstoned off the lock-screen card instead of showing Done.
124 expect(state?.phase).toBe("completed");
125​
126 const trulyInterrupted = projectThreadAwareness({
127 environmentId: "env-1" as EnvironmentId,
128 project,
129 thread: thread({ latestTurn: { ...finishedTurn, completedAt: null } }),
130 });
131 expect(trulyInterrupted).toBeNull();
132 });
133​
134 it("projects ready sessions with no materialized turn as completed", () => {
135 // Quick threads without code changes never get a checkpoint, so the SQL
136 // shell has no latestTurn row and latest_turn_id is cleared when the
137 // session settles; the ready session is the only completion signal left.
138 const state = projectThreadAwareness({
139 environmentId: "env-1" as EnvironmentId,
140 project,
141 thread: thread({
142 session: {
143 threadId: "thread-1" as ThreadId,
144 status: "ready",
145 providerName: "Codex",
146 runtimeMode: "full-access",
147 activeTurnId: null,
148 lastError: null,
149 updatedAt: NOW,
150 },
151 }),
152 });
153​
154 expect(state?.phase).toBe("completed");
155 });
156​
157 it("projects failures with the session error detail", () => {
158 const state = projectThreadAwareness({
159 environmentId: "env-1" as EnvironmentId,
160 project,
161 thread: thread({
162 session: {
163 threadId: "thread-1" as ThreadId,
164 status: "error",
165 providerName: "Codex",
166 runtimeMode: "full-access",
167 activeTurnId: null,
168 lastError: "Provider process exited.",
169 updatedAt: NOW,
170 },
171 }),
172 });
173​
174 expect(state).toMatchObject({
175 phase: "failed",
176 headline: "Agent failed",
177 detail: "Provider process exited.",
178 });
179 });
180});