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

ThreadBackgroundLiveness.test.ts

239 lines7.1 KB

1import { describe, expect, it } from "vite-plus/test";
2import * as ThreadBackgroundLiveness from "./ThreadBackgroundLiveness.ts";
3​
4describe("ThreadBackgroundLiveness", () => {
5 it("does not let status-free progress or metadata restart an idle task", () => {
6 const liveness = ThreadBackgroundLiveness.make();
7 liveness.recordTaskLiveness({
8 threadId: "thread",
9 taskId: "task",
10 taskType: undefined,
11 status: undefined,
12 kind: "started",
13 });
14 liveness.recordTaskLiveness({
15 threadId: "thread",
16 taskId: "task",
17 taskType: undefined,
18 status: "idle",
19 kind: "updated",
20 });
21 liveness.recordTaskLiveness({
22 threadId: "thread",
23 taskId: "task",
24 taskType: undefined,
25 status: undefined,
26 kind: "progress",
27 });
28 liveness.recordTaskLiveness({
29 threadId: "thread",
30 taskId: "task",
31 taskType: undefined,
32 status: undefined,
33 kind: "updated",
34 });
35 expect(liveness.getThreadBackgroundLiveness("thread")).toBeNull();
36​
37 liveness.recordTaskLiveness({
38 threadId: "thread",
39 taskId: "completed-task",
40 taskType: undefined,
41 status: undefined,
42 kind: "started",
43 });
44 liveness.recordTaskLiveness({
45 threadId: "thread",
46 taskId: "completed-task",
47 taskType: undefined,
48 status: "completed",
49 kind: "completed",
50 });
51 liveness.recordTaskLiveness({
52 threadId: "thread",
53 taskId: "completed-task",
54 taskType: undefined,
55 status: undefined,
56 kind: "updated",
57 });
58 expect(liveness.getThreadBackgroundLiveness("thread")).toBeNull();
59 });
60​
61 it("agents present as working; monitors as monitoring; agents win", () => {
62 const liveness = ThreadBackgroundLiveness.make();
63 const threadId = "t-live-1";
64 liveness.recordTaskLiveness({
65 threadId,
66 taskId: "m1",
67 taskType: "local_bash",
68 status: undefined,
69 kind: "started",
70 });
71 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
72 liveness.recordTaskLiveness({
73 threadId,
74 taskId: "a1",
75 taskType: "subagent",
76 status: undefined,
77 kind: "started",
78 });
79 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
80 liveness.recordTaskLiveness({
81 threadId,
82 taskId: "a1",
83 taskType: "subagent",
84 status: "completed",
85 kind: "completed",
86 });
87 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
88 liveness.recordTaskLiveness({
89 threadId,
90 taskId: "m1",
91 taskType: "local_bash",
92 status: "completed",
93 kind: "completed",
94 });
95 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
96 });
97​
98 it("terminal rows without a taskType still clear monitor entries", () => {
99 const liveness = ThreadBackgroundLiveness.make();
100 const threadId = "t-live-2";
101 liveness.recordTaskLiveness({
102 threadId,
103 taskId: "m1",
104 taskType: "local_bash",
105 status: undefined,
106 kind: "started",
107 });
108 // Terminal tick arrives with no taskType (common on task.completed).
109 liveness.recordTaskLiveness({
110 threadId,
111 taskId: "m1",
112 taskType: undefined,
113 status: "completed",
114 kind: "completed",
115 });
116 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
117 });
118​
119 it("nested agents (agentId + agent taskType) still count toward liveness", () => {
120 const liveness = ThreadBackgroundLiveness.make();
121 const threadId = "t-live-nested";
122 liveness.recordTaskLiveness({
123 threadId,
124 taskId: "n1",
125 taskType: "local_agent",
126 status: undefined,
127 kind: "started",
128 agentId: "owner",
129 });
130 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
131 liveness.recordTaskLiveness({
132 threadId,
133 taskId: "n1",
134 taskType: "local_agent",
135 status: "completed",
136 kind: "completed",
137 agentId: "owner",
138 });
139 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
140 });
141​
142 it("untyped rows count as agents; idle is not live; agent-owned tasks remain live", () => {
143 const liveness = ThreadBackgroundLiveness.make();
144 const threadId = "t-live-3";
145 liveness.recordTaskLiveness({
146 threadId,
147 taskId: "wf:1",
148 taskType: undefined,
149 status: "running",
150 kind: "progress",
151 });
152 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
153 liveness.recordTaskLiveness({
154 threadId,
155 taskId: "wf:1",
156 taskType: undefined,
157 status: "idle",
158 kind: "updated",
159 });
160 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
161 liveness.recordTaskLiveness({
162 threadId,
163 taskId: "sh:1",
164 taskType: "local_bash",
165 status: undefined,
166 kind: "started",
167 agentId: "owner",
168 });
169 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
170 liveness.recordTaskLiveness({
171 threadId,
172 taskId: "sh:1",
173 taskType: "local_bash",
174 status: "completed",
175 kind: "completed",
176 agentId: "owner",
177 });
178 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
179 });
180​
181 it("reclassification moves a task between buckets instead of duplicating it", () => {
182 const liveness = ThreadBackgroundLiveness.make();
183 const threadId = "t-live-reclass";
184 // First seen without a taskType: counts as an agent.
185 liveness.recordTaskLiveness({
186 threadId,
187 taskId: "x1",
188 taskType: undefined,
189 status: "running",
190 kind: "started",
191 });
192 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
193 // Later transition reveals it's a shell: downgrade to monitoring, not
194 // a stale duplicate pinning "working".
195 liveness.recordTaskLiveness({
196 threadId,
197 taskId: "x1",
198 taskType: "local_bash",
199 status: "running",
200 kind: "progress",
201 });
202 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
203 // Turning out to be inert drops the prior entry too; an agent-owned shell
204 // remains independently live until its own terminal transition.
205 liveness.recordTaskLiveness({
206 threadId,
207 taskId: "x1",
208 taskType: "local_bash",
209 status: "running",
210 kind: "progress",
211 agentId: "owner",
212 });
213 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
214 });
215​
216 it("plan tasks are inert; clear removes everything; instances are isolated", () => {
217 const a = ThreadBackgroundLiveness.make();
218 const b = ThreadBackgroundLiveness.make();
219 a.recordTaskLiveness({
220 threadId: "t",
221 taskId: "p1",
222 taskType: "plan",
223 status: undefined,
224 kind: "started",
225 });
226 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
227 a.recordTaskLiveness({
228 threadId: "t",
229 taskId: "a1",
230 taskType: "local_workflow",
231 status: undefined,
232 kind: "started",
233 });
234 expect(a.getThreadBackgroundLiveness("t")).toBe("working");
235 expect(b.getThreadBackgroundLiveness("t")).toBeNull();
236 a.clearThreadLiveness("t");
237 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
238 });
239});