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

AntigravityChildWork.test.ts

344 lines13.5 KB

1// @effect-diagnostics nodeBuiltinImport:off -- fixtures name transcript files by URL.
2import * as NodeURL from "node:url";
3​
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { expect, it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Path from "effect/Path";
9​
10import {
11 ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
12 antigravitySubagentLaunches,
13 describeAntigravitySubagentLaunches,
14 readAntigravityChildWork,
15} from "./AntigravityChildWork.ts";
16import { sanitizeAntigravityToolPayload } from "./AntigravityProtocol.ts";
17import {
18 autoApproved,
19 CHILD,
20 invokeStep,
21 LAUNCH,
22 makeProfile,
23 plannerStep,
24 ROOT,
25 transcriptLines,
26} from "./AntigravityChildWork.testkit.ts";
27​
28const read = (
29 profileDirectory: string,
30 nativeTaskId: string,
31 launch: { readonly title: string | null; readonly prompt: string } = {
32 title: null,
33 prompt: "Readme check",
34 },
35 siblings: ReadonlyArray<{
36 readonly nativeTaskId: string;
37 readonly title: string | null;
38 readonly prompt: string;
39 }> = [],
40) =>
41 readAntigravityChildWork({
42 profileDirectory,
43 rootSessionId: ROOT,
44 nativeTaskId,
45 launch,
46 siblings,
47 });
48​
49it.layer(NodeServices.layer)("readAntigravityChildWork", (it) => {
50 it.effect("follows an auto-approved call id through its planner step to the record", () =>
51 Effect.gen(function* () {
52 const { profile } = yield* makeProfile({ steps: autoApproved });
53 const [child, ...rest] = yield* read(profile, `${ROOT}:1`);
54 expect(rest).toEqual([]);
55 expect(child).toMatchObject({
56 conversationId: CHILD,
57 role: LAUNCH.role,
58 prompt: LAUNCH.prompt,
59 truncated: false,
60 });
61 // The launch envelope repeats the prompt shown above, so the steps start with the child's work.
62 expect(child?.steps?.map((step) => step.text ?? step.toolCalls[0]?.name)).toEqual([
63 "view_file",
64 "2: child read marker 4242",
65 "marker 4242 found",
66 ]);
67 expect(child?.steps?.[0]?.toolCalls[0]?.input).toBe('{"AbsolutePath":"/project/README.md"}');
68 }),
69 );
70​
71 it.effect("needs proof that the call id belongs to the parent's record", () =>
72 Effect.gen(function* () {
73 const { profile } = yield* makeProfile({
74 steps: (logUri) => [
75 ...autoApproved(logUri),
76 [5, 15, plannerStep(["call-b", "call-c"])],
77 [
78 6,
79 127,
80 invokeStep({ callId: "call-b", launches: [LAUNCH], children: [{ id: CHILD, logUri }] }),
81 ],
82 [
83 7,
84 127,
85 invokeStep({ callId: "call-c", launches: [LAUNCH], children: [{ id: CHILD, logUri }] }),
86 ],
87 ],
88 });
89 // Another session's id, a step that is not the planner call, and parallel calls that share one id.
90 expect(yield* read(profile, `00000000-0000-4000-8000-000000000000:1`)).toEqual([]);
91 expect(yield* read(profile, `${ROOT}:2`)).toEqual([]);
92 expect(yield* read(profile, `${ROOT}:5`)).toEqual([]);
93 }),
94 );
95​
96 it.effect("matches a permission-prompt call by its launch text, only when unique", () =>
97 Effect.gen(function* () {
98 const other = { role: "Test Runner", prompt: "Run the tests" };
99 const long = { role: "Long Reader", prompt: `Read this:\n${"x".repeat(9_000)}` };
100 // A role's leading byte order mark is part of its text.
101 const marked = { role: "\uFEFFMarked", prompt: "Check the mark" };
102 const { profile } = yield* makeProfile({
103 steps: (logUri) => [
104 [
105 2,
106 127,
107 invokeStep({ callId: "x", launches: [LAUNCH], children: [{ id: CHILD, logUri }] }),
108 ],
109 [
110 8,
111 127,
112 invokeStep({ callId: "w", launches: [long], children: [{ id: CHILD, logUri }] }),
113 ],
114 [
115 10,
116 127,
117 invokeStep({ callId: "v", launches: [marked], children: [{ id: CHILD, logUri }] }),
118 ],
119 [
120 4,
121 127,
122 invokeStep({ callId: "y", launches: [other], children: [{ id: CHILD, logUri }] }),
123 ],
124 [
125 6,
126 127,
127 invokeStep({ callId: "z", launches: [other], children: [{ id: CHILD, logUri }] }),
128 ],
129 ],
130 });
131 const opaque = "35bea692fa95447fbdae6333fec5515f";
132 const matched = yield* read(profile, opaque, describeAntigravitySubagentLaunches([LAUNCH]));
133 expect(matched.map((child) => child.role)).toEqual([LAUNCH.role]);
134 // T3 knows a second batch with this text that the agent has not recorded yet.
135 const twin = { nativeTaskId: "f00d", ...describeAntigravitySubagentLaunches([LAUNCH]) };
136 expect(
137 yield* read(profile, opaque, describeAntigravitySubagentLaunches([LAUNCH]), [twin]),
138 ).toEqual([]);
139 // T3 stores the launch from the bounded tool payload, which keeps only a long prompt's tail.
140 const stored = antigravitySubagentLaunches(
141 sanitizeAntigravityToolPayload({ Subagents: [{ Role: long.role, Prompt: long.prompt }] }),
142 );
143 const longMatch = yield* read(profile, opaque, describeAntigravitySubagentLaunches(stored!));
144 expect(longMatch.map((child) => child.prompt)).toEqual([long.prompt]);
145 expect(
146 yield* read(profile, opaque, describeAntigravitySubagentLaunches([marked])),
147 ).toHaveLength(1);
148 expect(yield* read(profile, opaque, { title: "Marked", prompt: marked.prompt })).toEqual([]);
149 expect(yield* read(profile, opaque, describeAntigravitySubagentLaunches([other]))).toEqual(
150 [],
151 );
152 expect(yield* read(profile, opaque, { title: null, prompt: "Readme check" })).toEqual([]);
153 }),
154 );
155​
156 it.effect("leaves an auto-approved batch's record out of a launch-text match", () =>
157 Effect.gen(function* () {
158 const { profile } = yield* makeProfile({ steps: autoApproved });
159 const launch = describeAntigravitySubagentLaunches([LAUNCH]);
160 const opaque = "35bea692fa95447fbdae6333fec5515f";
161 expect(yield* read(profile, opaque, launch)).toHaveLength(1);
162 // An earlier auto-approved batch stored no launch text, but its planner step claims the record.
163 const auto = {
164 nativeTaskId: `${ROOT}:1`,
165 title: ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
166 prompt: "",
167 };
168 expect(yield* read(profile, opaque, launch, [auto])).toEqual([]);
169 // A sibling that cannot be ruled out refuses the match: its planner step is unreadable,
170 // or it is a permission batch whose launch text is missing or cut.
171 for (const sibling of [
172 { ...auto, nativeTaskId: `${ROOT}:99` },
173 { nativeTaskId: "0ld", title: ANTIGRAVITY_SUBAGENT_BATCH_TITLE, prompt: "Running" },
174 // Its stored launch was cut, so it may be this same launch.
175 { nativeTaskId: "cut", title: LAUNCH.role, prompt: "[Earlier output truncated]\n\nmarker" },
176 { nativeTaskId: "cut", title: "[Earlier output truncated]\n\nReader", prompt: "x" },
177 { nativeTaskId: "cut", title: null, prompt: "[Earlier output truncated]\n\nx" },
178 ]) {
179 expect(yield* read(profile, opaque, launch, [sibling])).toEqual([]);
180 }
181 // A cut prompt under different roles is another launch.
182 const other = {
183 nativeTaskId: "cut",
184 title: "Test Runner",
185 prompt: "[Earlier output truncated]\n\nx",
186 };
187 expect(yield* read(profile, opaque, launch, [other])).toHaveLength(1);
188 }),
189 );
190​
191 it.effect("reads no transcript outside the child's own profile path", () =>
192 Effect.gen(function* () {
193 const fs = yield* FileSystem.FileSystem;
194 const path = yield* Path.Path;
195 const outside = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-outside-" });
196 const secret = path.join(outside, "transcript.jsonl");
197 yield* fs.writeFileString(secret, JSON.stringify(transcriptLines[3]) + "\n");
198 const named = yield* makeProfile({
199 steps: () => autoApproved(NodeURL.pathToFileURL(secret).href),
200 });
201 const [namedChild] = yield* read(named.profile, `${ROOT}:1`);
202 expect(namedChild?.role).toBe(LAUNCH.role);
203 expect(namedChild?.steps).toBeNull();
204​
205 const linked = yield* makeProfile({ steps: autoApproved });
206 yield* fs.remove(linked.transcriptPath);
207 yield* fs.symlink(secret, linked.transcriptPath);
208 const [linkedChild] = yield* read(linked.profile, `${ROOT}:1`);
209 expect(linkedChild?.steps).toBeNull();
210 }),
211 );
212​
213 it.effect("reads only the tail of a large transcript", () =>
214 Effect.gen(function* () {
215 const filler = JSON.stringify({
216 step_index: 0,
217 type: "VIEW_FILE",
218 status: "DONE",
219 content: "x".repeat(10_000),
220 });
221 const { profile } = yield* makeProfile({
222 steps: autoApproved,
223 transcript: `${Array.from({ length: 40 }, () => filler).join("\n")}\n${JSON.stringify(transcriptLines[3])}\n`,
224 });
225 const [child] = yield* read(profile, `${ROOT}:1`);
226 expect(child?.truncated).toBe(true);
227 expect(child?.steps?.length).toBeLessThan(40);
228 expect(child?.steps?.at(-1)?.text).toBe("marker 4242 found");
229 expect(child?.steps?.[0]?.text?.length).toBeLessThanOrEqual(4_001);
230 }),
231 );
232​
233 it.effect("refuses links between children and sessions inside the same profile", () =>
234 Effect.gen(function* () {
235 const fs = yield* FileSystem.FileSystem;
236 const path = yield* Path.Path;
237 const sibling = "b468c0e1d2f34a5b6c7d8e9f0a1b2c3d";
238 // The record names this child's literal path, but the child directory links to a sibling.
239 const linked = yield* makeProfile({ steps: autoApproved });
240 const brain = path.join(linked.profile, "antigravity-acp", "brain");
241 yield* fs.rename(path.join(brain, CHILD), path.join(brain, sibling));
242 yield* fs.symlink(path.join(brain, sibling), path.join(brain, CHILD));
243 const [child] = yield* read(linked.profile, `${ROOT}:1`);
244 expect(child?.role).toBe(LAUNCH.role);
245 expect(child?.steps).toBeNull();
246​
247 // This session's database links to another session's records.
248 const other = yield* makeProfile({ steps: autoApproved });
249 const conversations = path.join(other.profile, "antigravity-acp", "conversations");
250 const peer = path.join(conversations, "00000000-0000-4000-8000-000000000000.db");
251 yield* fs.rename(path.join(conversations, `${ROOT}.db`), peer);
252 yield* fs.symlink(peer, path.join(conversations, `${ROOT}.db`));
253 expect(yield* read(other.profile, `${ROOT}:1`)).toEqual([]);
254 }),
255 );
256​
257 it.effect("declines a launch-text match it cannot prove unique within its scan bounds", () =>
258 Effect.gen(function* () {
259 const record = (callId: string, logUri: string) =>
260 invokeStep({ callId, launches: [LAUNCH], children: [{ id: CHILD, logUri }] });
261 const other = { role: "Test Runner", prompt: "Run the tests" };
262 const filler = (idx: number, logUri: string) =>
263 [
264 idx,
265 127,
266 invokeStep({ callId: `f${idx}`, launches: [other], children: [{ id: CHILD, logUri }] }),
267 ] as const;
268 const opaque = "35bea692fa95447fbdae6333fec5515f";
269 const launch = describeAntigravitySubagentLaunches([LAUNCH]);
270 const cases = [
271 // More records than one open may scan.
272 (logUri: string) => [
273 [1, 127, record("x", logUri)] as const,
274 ...Array.from({ length: 200 }, (_, index) => filler(index + 2, logUri)),
275 ],
276 // An oversized record could hold an identical launch.
277 (logUri: string) => [
278 [1, 127, record("x", logUri)] as const,
279 [2, 127, new Uint8Array(1024 * 1024 + 1)] as const,
280 ],
281 // So could one this reader cannot decode.
282 (logUri: string) => [
283 [1, 127, record("x", logUri)] as const,
284 [2, 127, Uint8Array.from([0xff, 0xff])] as const,
285 ],
286 ];
287 for (const steps of cases) {
288 const { profile } = yield* makeProfile({ steps });
289 expect(yield* read(profile, opaque, launch)).toEqual([]);
290 }
291 }),
292 );
293​
294 it.effect("keeps the batch view for a batch over the child limit", () =>
295 Effect.gen(function* () {
296 const { profile } = yield* makeProfile({
297 steps: (logUri) => [
298 [1, 15, plannerStep(["call-a"])],
299 [
300 2,
301 127,
302 invokeStep({
303 callId: "call-a",
304 launches: Array.from({ length: 17 }, () => LAUNCH),
305 children: Array.from({ length: 17 }, () => ({ id: CHILD, logUri })),
306 }),
307 ],
308 ],
309 });
310 expect(yield* read(profile, `${ROOT}:1`)).toEqual([]);
311 }),
312 );
313​
314 it.effect("shares one transcript budget across the batch", () =>
315 Effect.gen(function* () {
316 const filler = JSON.stringify({
317 step_index: 0,
318 type: "VIEW_FILE",
319 status: "DONE",
320 content: "x".repeat(1_000),
321 });
322 const transcript = `${Array.from({ length: 300 }, () => filler).join("\n")}\n`;
323 const { profile } = yield* makeProfile({
324 transcript,
325 steps: (logUri) => [
326 [1, 15, plannerStep(["call-a"])],
327 [
328 2,
329 127,
330 invokeStep({
331 callId: "call-a",
332 launches: Array.from({ length: 16 }, () => LAUNCH),
333 children: Array.from({ length: 16 }, () => ({ id: CHILD, logUri })),
334 }),
335 ],
336 ],
337 });
338 const children = yield* read(profile, `${ROOT}:1`);
339 expect(children).toHaveLength(16);
340 expect(children.every((child) => child.truncated)).toBe(true);
341 expect(JSON.stringify(children).length).toBeLessThan(600 * 1024);
342 }),
343 );
344});