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