@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 127 lines 5.16 KB
Raw
1 import {
2 isProviderNativeSubagentThread,
3 OrchestrationV2GetNativeChildWorkError,
4 type OrchestrationV2GetNativeChildWorkInput,
5 type OrchestrationV2GetNativeChildWorkResult,
6 } from "@t3tools/contracts";
7 import * as Context from "effect/Context";
8 import * as Digest from "effect/crypto/Digest";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Path from "effect/Path";
12
13 import * as ServerConfig from "../config.ts";
14 import { readAntigravityChildWork } from "../provider/acp/AntigravityChildWork.ts";
15 import { resolveAntigravityInstanceDirectories } from "../provider/antigravityAuthSupport.ts";
16 import * as ThreadManagement from "./ThreadManagementService.ts";
17
18 /** Reads what a provider-native subagent's children did, on demand. Other providers stream real child sessions. */
19 export class NativeChildWorkQuery extends Context.Service<
20 NativeChildWorkQuery,
21 {
22 readonly getNativeChildWork: (
23 input: OrchestrationV2GetNativeChildWorkInput,
24 ) => Effect.Effect<
25 OrchestrationV2GetNativeChildWorkResult,
26 OrchestrationV2GetNativeChildWorkError
27 >;
28 }
29 >()("t3/orchestration-v2/NativeChildWorkQuery") {}
30
31 const NO_CHILDREN: OrchestrationV2GetNativeChildWorkResult = { children: [] };
32
33 const make = Effect.gen(function* () {
34 const threads = yield* ThreadManagement.ThreadManagementService;
35 const { stateDir } = yield* ServerConfig.ServerConfig;
36 const digest = yield* Digest.Digest;
37 const path = yield* Path.Path;
38
39 const getNativeChildWork = Effect.fn("NativeChildWorkQuery.getNativeChildWork")(
40 function* (input: OrchestrationV2GetNativeChildWorkInput) {
41 const { thread } = yield* threads.getThreadRecords(input.threadId, []);
42 const parentThreadId = thread.lineage.parentThreadId;
43 if (
44 !isProviderNativeSubagentThread(thread) ||
45 parentThreadId === null ||
46 thread.forkedFrom?.type !== "node"
47 ) {
48 return NO_CHILDREN;
49 }
50 const subagentId = thread.forkedFrom.nodeId;
51 const { subagents } = yield* threads.getThreadRecords(parentThreadId, ["subagents"]);
52 const subagent = subagents.find(
53 (candidate) => candidate.id === subagentId && candidate.childThreadId === thread.id,
54 );
55 const nativeTaskId = subagent?.nativeTaskRef?.nativeId;
56 if (
57 subagent?.driver !== "antigravity" ||
58 subagent.origin !== "provider_native" ||
59 subagent.runId === null ||
60 !nativeTaskId
61 ) {
62 return NO_CHILDREN;
63 }
64 const others = subagents.filter(
65 (candidate) =>
66 candidate.id !== subagent.id &&
67 candidate.driver === "antigravity" &&
68 candidate.origin === "provider_native" &&
69 candidate.providerInstanceId === subagent.providerInstanceId,
70 );
71 const { runs, providerThreads } = yield* threads.getThreadRecords(
72 parentThreadId,
73 ["runs", "providerThreads"],
74 { runIds: [subagent.runId, ...others.flatMap((other) => other.runId ?? [])] },
75 );
76 const { attempts } = yield* threads.getThreadRecords(parentThreadId, ["attempts"], {
77 attemptIds: runs.flatMap((run) => run.activeAttemptId ?? []),
78 });
79 const providerThreadOf = (runId: string | null) => {
80 const providerThreadId = runs.find((run) => run.id === runId)?.providerThreadId;
81 return providerThreads.find((candidate) => candidate.id === providerThreadId);
82 };
83 // Rollback moves the provider thread to a fresh session; a batch's records stay where it ran.
84 const sessionOf = (runId: string | null) => {
85 const attemptId = runs.find((run) => run.id === runId)?.activeAttemptId;
86 return (
87 attempts.find((attempt) => attempt.id === attemptId)?.nativeThreadId ??
88 providerThreadOf(runId)?.nativeThreadRef?.nativeId
89 );
90 };
91 const providerThread = providerThreadOf(subagent.runId);
92 const rootSessionId = sessionOf(subagent.runId);
93 if (!rootSessionId || providerThread?.providerInstanceId !== subagent.providerInstanceId) {
94 return NO_CHILDREN;
95 }
96 const directories = yield* resolveAntigravityInstanceDirectories(
97 stateDir,
98 subagent.providerInstanceId,
99 ).pipe(Effect.provideService(Digest.Digest, digest), Effect.provideService(Path.Path, path));
100 const children = yield* readAntigravityChildWork({
101 profileDirectory: directories.profile,
102 rootSessionId,
103 nativeTaskId,
104 launch: { title: subagent.title, prompt: subagent.prompt },
105 // Only batches from the same agent session can share its records.
106 siblings: others.flatMap((other) => {
107 const id = other.nativeTaskRef?.nativeId;
108 return id && sessionOf(other.runId) === rootSessionId
109 ? [{ nativeTaskId: id, title: other.title, prompt: other.prompt }]
110 : [];
111 }),
112 });
113 return { children };
114 },
115 (effect, input) =>
116 effect.pipe(
117 Effect.mapError(
118 (cause) =>
119 new OrchestrationV2GetNativeChildWorkError({ threadId: input.threadId, cause }),
120 ),
121 ),
122 );
123
124 return NativeChildWorkQuery.of({ getNativeChildWork });
125 });
126
127 export const layer = Layer.effect(NativeChildWorkQuery, make);