Skip to content
Source
Sign inSign up

NativeChildWorkQuery.test.ts

119 lines4.0 KB

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