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

ThreadBackgroundLiveness.test.ts

229 lines6.7 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 are ignored", () => {
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)).toBeNull();
170 });
171​
172 it("reclassification moves a task between buckets instead of duplicating it", () => {
173 const liveness = ThreadBackgroundLiveness.make();
174 const threadId = "t-live-reclass";
175 // First seen without a taskType: counts as an agent.
176 liveness.recordTaskLiveness({
177 threadId,
178 taskId: "x1",
179 taskType: undefined,
180 status: "running",
181 kind: "started",
182 });
183 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
184 // Later transition reveals it's a shell: downgrade to monitoring, not
185 // a stale duplicate pinning "working".
186 liveness.recordTaskLiveness({
187 threadId,
188 taskId: "x1",
189 taskType: "local_bash",
190 status: "running",
191 kind: "progress",
192 });
193 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
194 // Turning out to be inert or agent-owned drops the prior entry too.
195 liveness.recordTaskLiveness({
196 threadId,
197 taskId: "x1",
198 taskType: "local_bash",
199 status: "running",
200 kind: "progress",
201 agentId: "owner",
202 });
203 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
204 });
205​
206 it("plan tasks are inert; clear removes everything; instances are isolated", () => {
207 const a = ThreadBackgroundLiveness.make();
208 const b = ThreadBackgroundLiveness.make();
209 a.recordTaskLiveness({
210 threadId: "t",
211 taskId: "p1",
212 taskType: "plan",
213 status: undefined,
214 kind: "started",
215 });
216 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
217 a.recordTaskLiveness({
218 threadId: "t",
219 taskId: "a1",
220 taskType: "local_workflow",
221 status: undefined,
222 kind: "started",
223 });
224 expect(a.getThreadBackgroundLiveness("t")).toBe("working");
225 expect(b.getThreadBackgroundLiveness("t")).toBeNull();
226 a.clearThreadLiveness("t");
227 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
228 });
229});