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

ThreadBackgroundLiveness.test.ts

199 lines6.0 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 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 expect(liveness.getThreadBackgroundLiveness("thread")).toBeNull();
29 });
30​
31 it("agents present as working; monitors as monitoring; agents win", () => {
32 const liveness = ThreadBackgroundLiveness.make();
33 const threadId = "t-live-1";
34 liveness.recordTaskLiveness({
35 threadId,
36 taskId: "m1",
37 taskType: "local_bash",
38 status: undefined,
39 kind: "started",
40 });
41 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
42 liveness.recordTaskLiveness({
43 threadId,
44 taskId: "a1",
45 taskType: "subagent",
46 status: undefined,
47 kind: "started",
48 });
49 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
50 liveness.recordTaskLiveness({
51 threadId,
52 taskId: "a1",
53 taskType: "subagent",
54 status: "completed",
55 kind: "completed",
56 });
57 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
58 liveness.recordTaskLiveness({
59 threadId,
60 taskId: "m1",
61 taskType: "local_bash",
62 status: "completed",
63 kind: "completed",
64 });
65 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
66 });
67​
68 it("terminal rows without a taskType still clear monitor entries", () => {
69 const liveness = ThreadBackgroundLiveness.make();
70 const threadId = "t-live-2";
71 liveness.recordTaskLiveness({
72 threadId,
73 taskId: "m1",
74 taskType: "local_bash",
75 status: undefined,
76 kind: "started",
77 });
78 // Terminal tick arrives with no taskType (common on task.completed).
79 liveness.recordTaskLiveness({
80 threadId,
81 taskId: "m1",
82 taskType: undefined,
83 status: "completed",
84 kind: "completed",
85 });
86 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
87 });
88​
89 it("nested agents (agentId + agent taskType) still count toward liveness", () => {
90 const liveness = ThreadBackgroundLiveness.make();
91 const threadId = "t-live-nested";
92 liveness.recordTaskLiveness({
93 threadId,
94 taskId: "n1",
95 taskType: "local_agent",
96 status: undefined,
97 kind: "started",
98 agentId: "owner",
99 });
100 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
101 liveness.recordTaskLiveness({
102 threadId,
103 taskId: "n1",
104 taskType: "local_agent",
105 status: "completed",
106 kind: "completed",
107 agentId: "owner",
108 });
109 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
110 });
111​
112 it("untyped rows count as agents; idle is not live; agent-owned tasks are ignored", () => {
113 const liveness = ThreadBackgroundLiveness.make();
114 const threadId = "t-live-3";
115 liveness.recordTaskLiveness({
116 threadId,
117 taskId: "wf:1",
118 taskType: undefined,
119 status: "running",
120 kind: "progress",
121 });
122 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
123 liveness.recordTaskLiveness({
124 threadId,
125 taskId: "wf:1",
126 taskType: undefined,
127 status: "idle",
128 kind: "updated",
129 });
130 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
131 liveness.recordTaskLiveness({
132 threadId,
133 taskId: "sh:1",
134 taskType: "local_bash",
135 status: undefined,
136 kind: "started",
137 agentId: "owner",
138 });
139 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
140 });
141​
142 it("reclassification moves a task between buckets instead of duplicating it", () => {
143 const liveness = ThreadBackgroundLiveness.make();
144 const threadId = "t-live-reclass";
145 // First seen without a taskType: counts as an agent.
146 liveness.recordTaskLiveness({
147 threadId,
148 taskId: "x1",
149 taskType: undefined,
150 status: "running",
151 kind: "started",
152 });
153 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("working");
154 // Later transition reveals it's a shell: downgrade to monitoring, not
155 // a stale duplicate pinning "working".
156 liveness.recordTaskLiveness({
157 threadId,
158 taskId: "x1",
159 taskType: "local_bash",
160 status: "running",
161 kind: "progress",
162 });
163 expect(liveness.getThreadBackgroundLiveness(threadId)).toBe("monitoring");
164 // Turning out to be inert or agent-owned drops the prior entry too.
165 liveness.recordTaskLiveness({
166 threadId,
167 taskId: "x1",
168 taskType: "local_bash",
169 status: "running",
170 kind: "progress",
171 agentId: "owner",
172 });
173 expect(liveness.getThreadBackgroundLiveness(threadId)).toBeNull();
174 });
175​
176 it("plan tasks are inert; clear removes everything; instances are isolated", () => {
177 const a = ThreadBackgroundLiveness.make();
178 const b = ThreadBackgroundLiveness.make();
179 a.recordTaskLiveness({
180 threadId: "t",
181 taskId: "p1",
182 taskType: "plan",
183 status: undefined,
184 kind: "started",
185 });
186 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
187 a.recordTaskLiveness({
188 threadId: "t",
189 taskId: "a1",
190 taskType: "local_workflow",
191 status: undefined,
192 kind: "started",
193 });
194 expect(a.getThreadBackgroundLiveness("t")).toBe("working");
195 expect(b.getThreadBackgroundLiveness("t")).toBeNull();
196 a.clearThreadLiveness("t");
197 expect(a.getThreadBackgroundLiveness("t")).toBeNull();
198 });
199});