Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6
7import * as ServerConfig from "../config.ts";
8import { resolveAntigravityInstanceDirectories } from "../provider/antigravityAuthSupport.ts";
9import {
10 ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
11 describeAntigravitySubagentLaunches,
12} from "../provider/acp/AntigravityChildWork.ts";
13import {
14 CHILD,
15 invokeStep,
16 LAUNCH,
17 makeProfile,
18 ROOT,
19} from "../provider/acp/AntigravityChildWork.testkit.ts";
20import * as NativeChildWorkQuery from "./NativeChildWorkQuery.ts";
21import * as ThreadManagement from "./ThreadManagementService.ts";
22
23const instanceId = ProviderInstanceId.make("antigravity");
24const childThreadId = ThreadId.make("thread-child");
25const launch = describeAntigravitySubagentLaunches([LAUNCH]);
26
27// A permission-prompt batch in session ROOT, plus an older batch with no launch text in `olderRun`.
28const records = (olderRun: string) => ({
29 thread: {
30 id: childThreadId,
31 creationSource: "provider",
32 lineage: { parentThreadId: "thread-parent", relationshipToParent: "subagent" },
33 forkedFrom: { type: "node", nodeId: "subagent-new" },
34 },
35 subagents: [
36 {
37 id: "subagent-new",
38 childThreadId,
39 driver: "antigravity",
40 origin: "provider_native",
41 providerInstanceId: instanceId,
42 runId: "run-root",
43 nativeTaskRef: { nativeId: "35bea692fa95447fbdae6333fec5515f" },
44 ...launch,
45 },
46 {
47 id: "subagent-old",
48 childThreadId: "thread-old",
49 driver: "antigravity",
50 origin: "provider_native",
51 providerInstanceId: instanceId,
52 runId: olderRun,
53 nativeTaskRef: { nativeId: "0ld" },
54 title: ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
55 prompt: "Running",
56 },
57 ],
58 runs: [
59 { id: "run-root", providerThreadId: "provider-root" },
60 { id: "run-root-earlier", providerThreadId: "provider-root" },
61 { id: "run-other", providerThreadId: "provider-other" },
62 ],
63 providerThreads: [
64 { id: "provider-root", providerInstanceId: instanceId, nativeThreadRef: { nativeId: ROOT } },
65 {
66 id: "provider-other",
67 providerInstanceId: instanceId,
68 nativeThreadRef: { nativeId: "00000000-0000-4000-8000-000000000000" },
69 },
70 ],
71});
72
73const readChildren = (olderRun: string) =>
74 Effect.gen(function* () {
75 const query = yield* NativeChildWorkQuery.NativeChildWorkQuery;
76 return (yield* query.getNativeChildWork({ threadId: childThreadId })).children;
77 }).pipe(
78 Effect.provide(
79 NativeChildWorkQuery.layer.pipe(
80 Layer.provide(
81 Layer.mock(ThreadManagement.ThreadManagementService)({
82 getThreadRecords: () => Effect.succeed(records(olderRun) as never),
83 }),
84 ),
85 ),
86 ),
87 );
88
89it.layer(
90 Layer.mergeAll(
91 NodeServices.layer,
92 ServerConfig.layerTest(process.cwd(), { prefix: "native-child-work-" }).pipe(
93 Layer.provide(NodeServices.layer),
94 ),
95 ),
96)("NativeChildWorkQuery", (it) => {
97 it.effect("weighs only batches from the same agent session", () =>
98 Effect.gen(function* () {
99 const { stateDir } = yield* ServerConfig.ServerConfig;
100 const { profile } = yield* resolveAntigravityInstanceDirectories(stateDir, instanceId);
101 yield* makeProfile({
102 profile,
103 steps: (logUri) => [
104 [
105 2,
106 127,
107 invokeStep({ callId: "x", launches: [LAUNCH], children: [{ id: CHILD, logUri }] }),
108 ],
109 ],
110 });
111 const fromOtherSession = yield* readChildren("run-other");
112 expect(fromOtherSession.map((child) => child.conversationId)).toEqual([CHILD]);
113 // An earlier run in the same session may own the only record.
114 expect(yield* readChildren("run-root-earlier")).toEqual([]);
115 }),
116 );
117});