@@ -110,6 +110,8 @@ import * as Ref from "effect/Ref"; |
110 | 110 | import * as Schema from "effect/Schema"; |
111 | 111 | import * as Semaphore from "effect/Semaphore"; |
112 | 112 | import * as Stream from "effect/Stream"; |
| 113 | +import * as ChildProcess from "effect/process/ChildProcess"; |
| 114 | +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
113 | 115 |
|
114 | 116 | import { resolveAttachmentPath } from "../../attachmentStore.ts"; |
115 | 117 | import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts"; |
@@ -145,6 +147,7 @@ import { |
145 | 147 | } from "../../provider/claudeUsageLimits.ts"; |
146 | 148 | import type { ManagedServerProvider } from "@t3tools/provider-core/server/snapshot"; |
147 | 149 | import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; |
| 150 | +import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe"; |
148 | 151 | import { T3_CODE_ORCHESTRATION_INSTRUCTIONS } from "@t3tools/provider-core/server/orchestrationInstructions"; |
149 | 152 | import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions"; |
150 | 153 | import { |
@@ -517,6 +520,7 @@ export interface ClaudeAgentSdkSubagentLookupInput { |
517 | 520 | readonly dir: string | null; |
518 | 521 | readonly threadId: ThreadId; |
519 | 522 | readonly providerSessionId: OrchestrationV2ProviderSession["id"]; |
| 523 | + readonly environment?: NodeJS.ProcessEnv; |
520 | 524 | } |
521 | 525 |
|
522 | 526 | const isClaudeAgentSdkQueryRunnerError = Schema.is(ClaudeAgentSdkQueryRunnerError); |
@@ -533,6 +537,13 @@ function queryRunnerError(cause: unknown, method: string): ClaudeAgentSdkQueryRu |
533 | 537 | : new ClaudeAgentSdkQueryRunnerError({ cause, method }); |
534 | 538 | } |
535 | 539 |
|
| 540 | +const encodeClaudeHistoryOptions = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
| 541 | +const decodeClaudeSubagentMessages = Schema.decodeUnknownEffect( |
| 542 | + Schema.fromJsonString( |
| 543 | + Schema.Array(Schema.Struct({ parent_tool_use_id: Schema.NullOr(Schema.String) })), |
| 544 | + ), |
| 545 | +); |
| 546 | + |
536 | 547 | function closeClaudeQuery(queryRuntime: ClaudeQuery) { |
537 | 548 | return Effect.try({ |
538 | 549 | try: () => queryRuntime.close(), |
@@ -743,15 +754,76 @@ export function makeClaudeAgentSdkProtocolLogger(input: { |
743 | 754 | export const layerQueryRunner: Layer.Layer< |
744 | 755 | ClaudeAgentSdkQueryRunner, |
745 | 756 | never, |
746 | | - Crypto.Crypto | ProviderEventLoggers.ProviderEventLoggers |
| 757 | + | ChildProcessSpawner.ChildProcessSpawner |
| 758 | + | Crypto.Crypto |
| 759 | + | Path.Path |
| 760 | + | ProviderEventLoggers.ProviderEventLoggers |
747 | 761 | > = Layer.effect( |
748 | 762 | ClaudeAgentSdkQueryRunner, |
749 | 763 | Effect.gen(function* () { |
750 | 764 | const crypto = yield* Crypto.Crypto; |
| 765 | + const path = yield* Path.Path; |
| 766 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
751 | 767 | const { native: nativeEventLogger } = yield* ProviderEventLoggers.ProviderEventLoggers; |
752 | 768 | const agentScope = yield* AgentScope; |
753 | 769 | const platform = yield* HostProcess.Platform; |
754 | 770 |
|
| 771 | + const readSubagentMessages = Effect.fn("ClaudeAgentSdkQueryRunner.readSubagentMessages")( |
| 772 | + function* (input: ClaudeAgentSdkSubagentLookupInput) { |
| 773 | + const options = { ...(input.dir === null ? {} : { dir: input.dir }), limit: 1 }; |
| 774 | + const hostEnvironment = yield* HostProcess.Environment; |
| 775 | + if ( |
| 776 | + input.environment === undefined || |
| 777 | + input.environment.CLAUDE_CONFIG_DIR === hostEnvironment.CLAUDE_CONFIG_DIR |
| 778 | + ) { |
| 779 | + return yield* Effect.tryPromise(() => |
| 780 | + getSubagentMessages(input.sessionId, input.agentId, options), |
| 781 | + ); |
| 782 | + } |
| 783 | + |
| 784 | + // SDK history reads process.env. Isolate the instance home in the existing worker. |
| 785 | + const executablePath = yield* HostProcess.ExecutablePath; |
| 786 | + const workerArguments = (yield* HostProcess.IsExecutable) |
| 787 | + ? ["__claude-history"] |
| 788 | + : [ |
| 789 | + yield* path.fromFileUrl( |
| 790 | + new URL( |
| 791 | + import.meta.url.endsWith(".ts") |
| 792 | + ? "../../claude-history-worker.ts" |
| 793 | + : "./claude-history-worker.mjs", |
| 794 | + import.meta.url, |
| 795 | + ), |
| 796 | + ), |
| 797 | + ]; |
| 798 | + const result = yield* spawnAndCollect( |
| 799 | + executablePath, |
| 800 | + ChildProcess.make( |
| 801 | + executablePath, |
| 802 | + [ |
| 803 | + ...workerArguments, |
| 804 | + "getSubagentMessages", |
| 805 | + input.sessionId, |
| 806 | + encodeClaudeHistoryOptions({ ...options, agentId: input.agentId }), |
| 807 | + ], |
| 808 | + { env: { ...input.environment, ELECTRON_RUN_AS_NODE: "1" } }, |
| 809 | + ), |
| 810 | + ).pipe( |
| 811 | + Effect.timeout("30 seconds"), |
| 812 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 813 | + ); |
| 814 | + if (result.code !== 0) { |
| 815 | + return yield* new ClaudeAgentSdkQueryRunnerError({ |
| 816 | + method: "getSubagentMessages", |
| 817 | + cause: |
| 818 | + result.stderr.trim() || |
| 819 | + new Error(`Claude history worker exited with ${result.code}.`), |
| 820 | + }); |
| 821 | + } |
| 822 | + return yield* decodeClaudeSubagentMessages(result.stdout); |
| 823 | + }, |
| 824 | + Effect.mapError((cause) => queryRunnerError(cause, "getSubagentMessages")), |
| 825 | + ); |
| 826 | + |
755 | 827 | return ClaudeAgentSdkQueryRunner.of({ |
756 | 828 | allocateSessionId: crypto |
757 | 829 | .randomUUIDv4() |
@@ -1046,14 +1118,7 @@ export const layerQueryRunner: Layer.Layer< |
1046 | 1118 | agentId: input.agentId, |
1047 | 1119 | }, |
1048 | 1120 | }); |
1049 | | - const messages = yield* Effect.tryPromise({ |
1050 | | - try: () => |
1051 | | - getSubagentMessages(input.sessionId, input.agentId, { |
1052 | | - ...(input.dir === null ? {} : { dir: input.dir }), |
1053 | | - limit: 1, |
1054 | | - }), |
1055 | | - catch: (cause) => queryRunnerError(cause, "getSubagentMessages"), |
1056 | | - }); |
| 1121 | + const messages = yield* readSubagentMessages(input); |
1057 | 1122 | const toolUseId = messages[0]?.parent_tool_use_id ?? null; |
1058 | 1123 | yield* logProtocolEvent({ |
1059 | 1124 | direction: "incoming", |
@@ -4484,6 +4549,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( |
4484 | 4549 | dir: resume.context.input.runtimePolicy.cwd, |
4485 | 4550 | threadId: resume.context.input.threadId, |
4486 | 4551 | providerSessionId: input.providerSessionId, |
| 4552 | + environment: adapterOptions.environment, |
4487 | 4553 | }) |
4488 | 4554 | .pipe( |
4489 | 4555 | Effect.catch(() => |
|