stitchkit 0.134.0 → 0.134.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +6 -6
  3. package/dist/agent-executor/claude/cli-driver.d.ts +6 -0
  4. package/dist/agent-executor/claude/cli-driver.d.ts.map +1 -1
  5. package/dist/agent-executor/claude/session.d.ts +2 -0
  6. package/dist/agent-executor/claude/session.d.ts.map +1 -1
  7. package/dist/agent-executor/claude-sdk/driver.d.ts +9 -1
  8. package/dist/agent-executor/claude-sdk/driver.d.ts.map +1 -1
  9. package/dist/agent-executor/claude-sdk/engine-spawn.d.ts +18 -5
  10. package/dist/agent-executor/claude-sdk/engine-spawn.d.ts.map +1 -1
  11. package/dist/agent-executor/claude-sdk/probe.d.ts.map +1 -1
  12. package/dist/agent-executor/claude-sdk/query-manager.d.ts.map +1 -1
  13. package/dist/agent-executor/claude-sdk/query-options.d.ts +16 -0
  14. package/dist/agent-executor/claude-sdk/query-options.d.ts.map +1 -1
  15. package/dist/agent-executor/claude-sdk/session.d.ts +1 -0
  16. package/dist/agent-executor/claude-sdk/session.d.ts.map +1 -1
  17. package/dist/agent-runtime/harness-contract.d.ts +5 -5
  18. package/dist/agent-runtime/harness-contract.d.ts.map +1 -1
  19. package/dist/agent-runtime/runtime.d.ts +13 -13
  20. package/dist/agent-runtime/runtime.d.ts.map +1 -1
  21. package/dist/agent-runtime/sandbox-process-macos.d.ts +5 -4
  22. package/dist/agent-runtime/sandbox-process-macos.d.ts.map +1 -1
  23. package/dist/agent-runtime/sandbox-process-spec.d.ts +6 -0
  24. package/dist/agent-runtime/sandbox-process-spec.d.ts.map +1 -1
  25. package/dist/agent-runtime/sandbox-process-wrapper.d.ts +2 -1
  26. package/dist/agent-runtime/sandbox-process-wrapper.d.ts.map +1 -1
  27. package/dist/agent-runtime/sql-rows.d.ts +10 -10
  28. package/dist/agent-runtime/sqlite-schema.d.ts +2 -2
  29. package/dist/agent-runtime/sqlite.d.ts +1 -1
  30. package/dist/agent-runtime/store-conformance-lifecycle.d.ts +20 -20
  31. package/dist/agent-runtime/store-conformance-support.d.ts +10 -10
  32. package/dist/agent-runtime/terminal-commit.d.ts +10 -10
  33. package/dist/application/grammy.d.ts +1 -1
  34. package/dist/browser/contract-identity.d.ts +1 -1
  35. package/dist/browser/http-fetch.d.ts +1 -1
  36. package/dist/browser/socket-io-contract.d.ts +1 -1
  37. package/dist/contract/bridge-envelope.d.ts +1 -1
  38. package/dist/contract/client-types.d.ts +1 -1
  39. package/dist/contract/define.d.ts +2 -2
  40. package/dist/contract/errors-factory.d.ts +1 -1
  41. package/dist/contract/fingerprint.d.ts +1 -1
  42. package/dist/device-relay/hub.d.ts +1 -1
  43. package/dist/entrypoints/agent-executor/claude-sdk.js +323 -153
  44. package/dist/entrypoints/agent-executor/claude.js +40 -26
  45. package/dist/entrypoints/agent-runtime/postgres.js +3 -3
  46. package/dist/entrypoints/agent-runtime/sandbox.js +21 -16
  47. package/dist/entrypoints/agent-runtime/sqlite/bun.js +1 -1
  48. package/dist/entrypoints/agent-runtime/sqlite/node.js +1 -1
  49. package/dist/entrypoints/bin/upgrade-cli.js +1 -1
  50. package/dist/entrypoints/files/packaging.js +2 -2
  51. package/dist/entrypoints/handler.js +2 -2
  52. package/dist/entrypoints/node.js +2 -2
  53. package/dist/entrypoints/server.js +2 -2
  54. package/dist/entrypoints/tools/mcp.js +3 -3
  55. package/dist/entrypoints/tools/toolkit.js +3 -3
  56. package/dist/entrypoints/tools.js +2 -2
  57. package/dist/files/native-packaging-layout.d.ts +1 -1
  58. package/dist/{index-654rhe5d.js → index-7mjsb7zv.js} +5 -1
  59. package/dist/{index-kdw0n92g.js → index-a09r9ff0.js} +1 -1
  60. package/dist/{index-epyj7ptp.js → index-a84wj6y8.js} +4 -4
  61. package/dist/{index-qymek778.js → index-m2b49tvc.js} +76 -22
  62. package/dist/{index-mrng24wq.js → index-zag9sjpd.js} +1 -1
  63. package/dist/internal/postgres-query.d.ts +1 -1
  64. package/dist/react/query-client.d.ts +1 -1
  65. package/dist/server/bridge-host.d.ts +1 -1
  66. package/dist/server/bun.d.ts +1 -1
  67. package/dist/server/endpoint-dispatch.d.ts.map +1 -1
  68. package/dist/server/middleware/cors.d.ts +3 -3
  69. package/dist/server/middleware/cors.d.ts.map +1 -1
  70. package/dist/server/socket-io-config.d.ts +1 -1
  71. package/dist/service-host/identity.d.ts +1 -1
  72. package/dist/service-host/systemd-inspect.d.ts +1 -1
  73. package/dist/testing/manual-clock.d.ts +1 -1
  74. package/dist/tools/connections/types.d.ts +1 -1
  75. package/dist/tools/mcp/handler.d.ts +1 -1
  76. package/dist/tools/mcp/lazy-discovery.d.ts +1 -1
  77. package/dist/tools/mcp/mount.d.ts +1 -1
  78. package/dist/tools/mcp/stdio.d.ts +1 -1
  79. package/llms/agent-executor-claude-sdk.txt +1 -1
  80. package/llms/agent-executor-claude.txt +1 -1
  81. package/llms/agent-runtime-browser.txt +1 -1
  82. package/llms/agent-runtime-sandbox.txt +12 -8
  83. package/llms/agent-runtime-sqlite-bun.txt +1 -1
  84. package/llms/agent-runtime.2.txt +1 -1
  85. package/llms/agent-runtime.3.txt +4 -4
  86. package/llms/agent-runtime.txt +3 -3
  87. package/llms/application.2.txt +4 -4
  88. package/llms/application.txt +4 -4
  89. package/llms/cli.2.txt +27 -6
  90. package/llms/cli.txt +2 -2
  91. package/llms/contract.txt +1 -1
  92. package/llms/files-packaging.txt +1 -1
  93. package/llms/geo.txt +1 -1
  94. package/llms/live.txt +1 -1
  95. package/llms/oauth.txt +4 -4
  96. package/llms/observability.txt +3 -3
  97. package/llms/primitives.txt +1 -1
  98. package/llms/process.2.txt +7 -7
  99. package/llms/react.txt +4 -4
  100. package/llms/server.2.txt +7 -7
  101. package/llms/server.3.txt +5 -5
  102. package/llms/server.4.txt +6 -6
  103. package/llms/server.txt +13 -5
  104. package/llms/stitchkit.2.txt +3 -3
  105. package/llms/stitchkit.3.txt +1 -1
  106. package/llms/stitchkit.txt +4 -4
  107. package/llms/telegram.txt +1 -1
  108. package/llms/testing.txt +4 -4
  109. package/llms/tools-connections.txt +1 -1
  110. package/llms/tools-toolkit.txt +3 -1
  111. package/llms/tools.2.txt +6 -6
  112. package/llms/tools.3.txt +8 -8
  113. package/llms/upgrading-0.106.0-0.126.0.txt +1 -1
  114. package/llms/upgrading-0.128.0-0.134.0.txt +7 -1
  115. package/llms/upgrading-0.37.0-0.53.0.txt +7 -7
  116. package/llms/upgrading-0.69.0-0.95.0.txt +1 -1
  117. package/llms/upgrading-0.96.0-0.105.0.txt +1 -1
  118. package/llms-full.txt +163 -122
  119. package/llms.txt +8 -8
  120. package/package.json +1 -1
@@ -8,6 +8,8 @@ import {
8
8
  } from "../../index-6kwdfb9m.js";
9
9
  import {
10
10
  PlanLimitsSchema2,
11
+ assertEngineWrapCommand,
12
+ resolveWrappedEngineCommand,
11
13
  engineProfile,
12
14
  engineEnvironment2,
13
15
  nonEmptyString,
@@ -62,7 +64,8 @@ import {
62
64
  } from "../../index-e1y74yra.js";
63
65
  import {
64
66
  abortReason,
65
- raceAbort
67
+ raceAbort,
68
+ linkAbortSignals
66
69
  } from "../../index-rptj3qnc.js";
67
70
  // src/agent-executor/claude-sdk/controls.ts
68
71
  import { z } from "zod";
@@ -576,168 +579,167 @@ function runSdkQueryLoop(input) {
576
579
  })();
577
580
  }
578
581
 
579
- // src/agent-executor/claude-sdk/probe.ts
580
- var DEFAULT_PROBE_TIMEOUT_MS = 30000;
581
- function createNeverYieldingPrompt(signal) {
582
- return {
583
- [Symbol.asyncIterator]() {
584
- return {
585
- next() {
586
- if (signal.aborted) {
587
- return Promise.resolve({ value: undefined, done: true });
588
- }
589
- return new Promise((resolve) => {
590
- signal.addEventListener("abort", () => resolve({ value: undefined, done: true }), {
591
- once: true
592
- });
593
- });
594
- },
595
- return() {
596
- return Promise.resolve({ value: undefined, done: true });
597
- }
598
- };
599
- }
600
- };
601
- }
602
- function buildProbeOptions(input) {
603
- const { config } = input;
604
- return {
605
- cwd: input.cwd,
606
- env: input.env,
607
- abortController: input.abortController,
608
- persistSession: false,
609
- settings: { disableAllHooks: true },
610
- strictMcpConfig: true,
611
- mcpServers: {},
612
- allowedTools: [],
613
- canUseTool: async () => ({
614
- behavior: "deny",
615
- message: "Capability probe does not execute tools",
616
- interrupt: true
617
- }),
618
- ...config.pathToClaudeCodeExecutable !== undefined && {
619
- pathToClaudeCodeExecutable: config.pathToClaudeCodeExecutable
620
- },
621
- ...config.settingSources !== undefined && { settingSources: config.settingSources },
622
- ...config.extraArgs !== undefined && { extraArgs: config.extraArgs },
623
- ...config.stderr !== undefined && { stderr: config.stderr },
624
- ...config.spawnClaudeCodeProcess !== undefined && {
625
- spawnClaudeCodeProcess: config.spawnClaudeCodeProcess
626
- }
627
- };
628
- }
629
- async function probeClaudeSdkCapabilities(config, limits, input) {
630
- const ambient = config.ambient ?? process.env;
631
- const profile = engineProfile("claude-sdk", input.profile, ambient);
632
- if (input.timeoutMs !== undefined) {
633
- assertTimerMs("timeoutMs", input.timeoutMs);
634
- }
635
- input.signal?.throwIfAborted();
636
- const timeoutMs = input.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
637
- const env = engineEnvironment2(profile, ambient);
638
- return withSignalDeadline(timeoutMs, input.signal, () => new Error(`Claude SDK capability probe timed out after ${timeoutMs} ms`), async (deadlineSignal) => {
639
- const probeAbort = new AbortController;
640
- const onDeadlineAbort = () => probeAbort.abort(deadlineSignal.reason);
641
- if (deadlineSignal.aborted) {
642
- onDeadlineAbort();
643
- } else {
644
- deadlineSignal.addEventListener("abort", onDeadlineAbort, { once: true });
645
- }
646
- let query;
647
- try {
648
- query = config.query({
649
- prompt: createNeverYieldingPrompt(probeAbort.signal),
650
- options: buildProbeOptions({
651
- config,
652
- cwd: input.cwd,
653
- env,
654
- abortController: probeAbort
655
- })
656
- });
657
- const initFn = query.initializationResult;
658
- const modelsFn = query.supportedModels;
659
- const commandsFn = query.supportedCommands;
660
- const accountFn = query.accountInfo;
661
- if (typeof initFn !== "function" && typeof modelsFn !== "function" && typeof commandsFn !== "function" && typeof accountFn !== "function") {
662
- throw new Error("The configured Claude SDK query does not expose capability probe RPCs");
663
- }
664
- const initializationResult = typeof initFn === "function" ? await raceAbort(initFn.call(query), deadlineSignal) : undefined;
665
- const usageFn = input.includeUsage === true ? query.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET : undefined;
666
- const [models, commands, account, rawUsage] = await Promise.all([
667
- typeof modelsFn === "function" ? raceAbort(modelsFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.models),
668
- typeof commandsFn === "function" ? raceAbort(commandsFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.commands),
669
- typeof accountFn === "function" ? raceAbort(accountFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.account),
670
- typeof usageFn === "function" ? raceAbort(usageFn.call(query, { skipBehaviors: true }), deadlineSignal) : Promise.resolve(undefined)
671
- ]);
672
- let usage;
673
- if (input.includeUsage === true) {
674
- usage = typeof usageFn === "function" && rawUsage !== undefined ? normalizeClaudeSdkUsageRead(rawUsage, (planLimits) => {
675
- limits.set(profile.profileId, planLimits);
676
- }) : unpublishedClaudeSdkUsageRead();
677
- }
678
- return {
679
- ...initializationResult !== undefined && { initializationResult },
680
- ...models !== undefined && { models },
681
- ...commands !== undefined && { commands },
682
- ...account !== undefined && { account },
683
- ...usage !== undefined && { usage }
684
- };
685
- } finally {
686
- deadlineSignal.removeEventListener("abort", onDeadlineAbort);
687
- if (!probeAbort.signal.aborted)
688
- probeAbort.abort();
689
- closeQuerySafely(query, config.onObserverError);
690
- }
691
- });
692
- }
693
-
694
582
  // src/agent-executor/claude-sdk/engine-spawn.ts
695
583
  import { EventEmitter } from "node:events";
696
584
  import { Readable, Writable } from "node:stream";
697
585
  var ENGINE_STDIN_BYTES = 256 * 1024 * 1024;
698
- function spawnOwnedClaudeCode(options, hooks) {
699
- const input = new TransformStream;
700
- const env = {};
701
- for (const [key, value] of Object.entries(options.env))
702
- if (value !== undefined)
703
- env[key] = value;
704
- const command = streamNativeCommand2({
705
- executable: options.command,
706
- args: [...options.args],
707
- cwd: options.cwd ?? process.cwd(),
708
- env,
709
- envPolicy: "declared-only",
710
- ownerLoss: "terminate",
711
- stdin: input.readable,
712
- maxStdinBytes: ENGINE_STDIN_BYTES,
713
- signal: options.signal,
714
- onLeaderStarted: ({ pid }) => hooks.onLeader(pid)
715
- });
586
+ function drainStderr(stderr, onStderr) {
716
587
  (async () => {
717
588
  const decoder = new TextDecoder;
718
589
  try {
719
- for await (const chunk of command.stderr) {
590
+ for await (const chunk of stderr) {
720
591
  const text = decoder.decode(chunk, { stream: true });
721
592
  if (text.length > 0)
722
- invokeQuietly(hooks.onStderr, text);
593
+ invokeQuietly(onStderr, text);
723
594
  }
724
595
  } catch {}
725
596
  })();
597
+ }
598
+ async function pipeStdout(source, destination) {
599
+ const writer = destination.getWriter();
600
+ try {
601
+ for await (const chunk of source) {
602
+ await writer.write(chunk);
603
+ }
604
+ await writer.close().catch(() => {
605
+ return;
606
+ });
607
+ } catch (error) {
608
+ await writer.abort(error).catch(() => {
609
+ return;
610
+ });
611
+ } finally {
612
+ writer.releaseLock();
613
+ }
614
+ }
615
+ function spawnOwnedClaudeCode(options, hooks) {
616
+ const input = new TransformStream;
617
+ const env = {};
618
+ for (const [key, value] of Object.entries(options.env))
619
+ if (value !== undefined)
620
+ env[key] = value;
726
621
  const events = new EventEmitter;
727
622
  let exitCode = null;
728
623
  let signalCode = null;
729
624
  let killed = false;
730
- const exited = command.result.then((result) => {
731
- exitCode = result.exitCode;
732
- signalCode = isSignal(result.signal) ? result.signal : null;
733
- events.emit("exit", exitCode, signalCode);
734
- }, (error) => {
625
+ const emitFailure = (error) => {
735
626
  if (events.listenerCount("error") > 0)
736
627
  events.emit("error", error instanceof Error ? error : new Error(String(error)));
737
628
  events.emit("exit", null, null);
629
+ };
630
+ if (hooks.wrapCommand === undefined) {
631
+ const command = streamNativeCommand2({
632
+ executable: options.command,
633
+ args: [...options.args],
634
+ cwd: options.cwd ?? process.cwd(),
635
+ env,
636
+ envPolicy: "declared-only",
637
+ ownerLoss: "terminate",
638
+ stdin: input.readable,
639
+ maxStdinBytes: ENGINE_STDIN_BYTES,
640
+ signal: options.signal,
641
+ onLeaderStarted: ({ pid }) => hooks.onLeader(pid)
642
+ });
643
+ drainStderr(command.stderr, hooks.onStderr);
644
+ const exited = command.result.then((result) => {
645
+ exitCode = result.exitCode;
646
+ signalCode = isSignal(result.signal) ? result.signal : null;
647
+ events.emit("exit", exitCode, signalCode);
648
+ }, emitFailure);
649
+ const stdin = Writable.fromWeb(input.writable);
650
+ const stdout = Readable.from(command.stdout, { objectMode: false });
651
+ stdin.on("error", () => {
652
+ return;
653
+ });
654
+ stdout.on("error", () => {
655
+ return;
656
+ });
657
+ const spawned = {
658
+ stdin,
659
+ stdout,
660
+ get killed() {
661
+ return killed;
662
+ },
663
+ get exitCode() {
664
+ return exitCode;
665
+ },
666
+ get signalCode() {
667
+ return signalCode;
668
+ },
669
+ kill() {
670
+ if (!killed) {
671
+ killed = true;
672
+ command.stop().catch(() => {
673
+ return;
674
+ });
675
+ }
676
+ return true;
677
+ },
678
+ on: listen((event, listener) => events.on(event, listener)),
679
+ once: listen((event, listener) => events.once(event, listener)),
680
+ off: listen((event, listener) => events.off(event, listener))
681
+ };
682
+ return { process: spawned, exited, wrapSettled: Promise.resolve() };
683
+ }
684
+ const output = new TransformStream;
685
+ const wrapAbort = new AbortController;
686
+ const linkedWrap = linkAbortSignals(options.signal, hooks.signal, wrapAbort.signal);
687
+ const wrapReady = Promise.withResolvers();
688
+ wrapReady.promise.catch(() => {
689
+ return;
738
690
  });
691
+ let activeCommand;
692
+ const exited = (async () => {
693
+ try {
694
+ const wrapped = await resolveWrappedEngineCommand({
695
+ executable: options.command,
696
+ args: [...options.args],
697
+ cwd: options.cwd ?? process.cwd(),
698
+ env,
699
+ signal: linkedWrap.signal
700
+ }, hooks.wrapCommand, "createClaudeSdkDriver", linkedWrap.signal);
701
+ if (killed || linkedWrap.signal.aborted) {
702
+ throw linkedWrap.signal.aborted ? abortReason(linkedWrap.signal) : new Error("Claude Code spawn was stopped");
703
+ }
704
+ const command = streamNativeCommand2({
705
+ executable: wrapped.executable,
706
+ args: [...wrapped.args],
707
+ cwd: wrapped.cwd,
708
+ env: wrapped.env,
709
+ envPolicy: "declared-only",
710
+ ownerLoss: "terminate",
711
+ stdin: input.readable,
712
+ maxStdinBytes: ENGINE_STDIN_BYTES,
713
+ signal: options.signal,
714
+ onLeaderStarted: ({ pid }) => hooks.onLeader(pid)
715
+ });
716
+ activeCommand = command;
717
+ if (killed)
718
+ command.stop().catch(() => {
719
+ return;
720
+ });
721
+ wrapReady.resolve();
722
+ drainStderr(command.stderr, hooks.onStderr);
723
+ const stdoutDone = pipeStdout(command.stdout, output.writable);
724
+ const [result] = await Promise.all([command.result, stdoutDone]);
725
+ exitCode = result.exitCode;
726
+ signalCode = isSignal(result.signal) ? result.signal : null;
727
+ events.emit("exit", exitCode, signalCode);
728
+ } catch (error) {
729
+ wrapReady.reject(error);
730
+ await output.writable.abort(error).catch(() => {
731
+ return;
732
+ });
733
+ await input.readable.cancel(error).catch(() => {
734
+ return;
735
+ });
736
+ emitFailure(error);
737
+ } finally {
738
+ linkedWrap.release();
739
+ }
740
+ })();
739
741
  const stdin = Writable.fromWeb(input.writable);
740
- const stdout = Readable.from(command.stdout, { objectMode: false });
742
+ const stdout = Readable.from(output.readable, { objectMode: false });
741
743
  stdin.on("error", () => {
742
744
  return;
743
745
  });
@@ -759,9 +761,13 @@ function spawnOwnedClaudeCode(options, hooks) {
759
761
  kill() {
760
762
  if (!killed) {
761
763
  killed = true;
762
- command.stop().catch(() => {
763
- return;
764
- });
764
+ if (activeCommand !== undefined) {
765
+ activeCommand.stop().catch(() => {
766
+ return;
767
+ });
768
+ } else if (!wrapAbort.signal.aborted) {
769
+ wrapAbort.abort(new Error("Claude Code spawn was stopped"));
770
+ }
765
771
  }
766
772
  return true;
767
773
  },
@@ -769,7 +775,7 @@ function spawnOwnedClaudeCode(options, hooks) {
769
775
  once: listen((event, listener) => events.once(event, listener)),
770
776
  off: listen((event, listener) => events.off(event, listener))
771
777
  };
772
- return { process: spawned, exited };
778
+ return { process: spawned, exited, wrapSettled: wrapReady.promise };
773
779
  }
774
780
  function listen(register) {
775
781
  function registered(event, listener) {
@@ -791,6 +797,10 @@ var DEFAULT_DELEGATION_TIMEOUT_MS = 30000;
791
797
  var FLAG_SETTINGS_REPLAY_TIMEOUT_MS = 30000;
792
798
  function validateClaudeSdkConfig(config) {
793
799
  validateImageOptions(config);
800
+ assertEngineWrapCommand(config.wrapCommand, "createClaudeSdkDriver");
801
+ if (config.wrapCommand !== undefined && config.spawnClaudeCodeProcess !== undefined) {
802
+ throw new TypeError("[stitchkit] createClaudeSdkDriver: wrapCommand cannot be combined with spawnClaudeCodeProcess");
803
+ }
794
804
  if (config.supportedDialogKinds !== undefined && config.supportedDialogKinds.length > 0 && config.onUserDialog === undefined) {
795
805
  throw new TypeError("ClaudeSdkDriverConfig.supportedDialogKinds requires onUserDialog when non-empty");
796
806
  }
@@ -829,9 +839,10 @@ function readPositivePid(spawned) {
829
839
  const raw = Reflect.get(spawned, "pid");
830
840
  return isPositiveSafeInteger(raw) ? raw : undefined;
831
841
  }
832
- function buildSpawnHook(config, recordPid, onPidExit, trackExit) {
842
+ function buildClaudeSdkSpawnHook(input) {
843
+ const { config, recordPid, onPidExit, trackExit, trackWrapSettled } = input;
833
844
  const custom = config.spawnClaudeCodeProcess;
834
- if (custom === undefined && config.onProviderPid === undefined)
845
+ if (custom === undefined && config.onProviderPid === undefined && config.wrapCommand === undefined)
835
846
  return;
836
847
  if (custom === undefined) {
837
848
  return (spawnOpts) => {
@@ -839,6 +850,8 @@ function buildSpawnHook(config, recordPid, onPidExit, trackExit) {
839
850
  const owned = {};
840
851
  const spawned = spawnOwnedClaudeCode(spawnOpts, {
841
852
  onStderr: config.stderr,
853
+ ...config.wrapCommand !== undefined && { wrapCommand: config.wrapCommand },
854
+ ...input.signal !== undefined && { signal: input.signal },
842
855
  onLeader: (pid) => {
843
856
  leader = pid;
844
857
  recordPid(pid);
@@ -857,6 +870,7 @@ function buildSpawnHook(config, recordPid, onPidExit, trackExit) {
857
870
  onPidExit(leader);
858
871
  });
859
872
  trackExit(spawned.exited);
873
+ trackWrapSettled(spawned.wrapSettled);
860
874
  return spawned.process;
861
875
  };
862
876
  }
@@ -883,7 +897,14 @@ function buildSpawnHook(config, recordPid, onPidExit, trackExit) {
883
897
  }
884
898
  function buildClaudeSdkQueryOptions(input) {
885
899
  const { config } = input;
886
- const spawnHook = buildSpawnHook(config, input.recordPid, input.onPidExit, input.trackExit);
900
+ const spawnHook = buildClaudeSdkSpawnHook({
901
+ config,
902
+ ...input.signal !== undefined && { signal: input.signal },
903
+ recordPid: input.recordPid,
904
+ onPidExit: input.onPidExit,
905
+ trackExit: input.trackExit,
906
+ trackWrapSettled: input.trackWrapSettled
907
+ });
887
908
  return {
888
909
  cwd: input.cwd,
889
910
  env: input.env,
@@ -979,6 +1000,7 @@ async function launchSingleSdkQuery(input) {
979
1000
  emitStartupPhase(input.onStartupPhase, "spawning", input.config.onObserverError);
980
1001
  input.ensureAlive();
981
1002
  const pushed = pushablePrompt();
1003
+ const wrapSettlements = [];
982
1004
  let query;
983
1005
  try {
984
1006
  query = input.config.query({
@@ -987,6 +1009,7 @@ async function launchSingleSdkQuery(input) {
987
1009
  config: input.config,
988
1010
  cwd: input.cwd,
989
1011
  env: input.env,
1012
+ signal: input.signal,
990
1013
  canUseTool: input.canUseTool,
991
1014
  sessionId: input.sessionId,
992
1015
  resume: input.resume,
@@ -1000,7 +1023,8 @@ async function launchSingleSdkQuery(input) {
1000
1023
  activePermissionMode: input.activePermissionMode,
1001
1024
  recordPid: input.recordPid,
1002
1025
  onPidExit: input.onPidExit,
1003
- trackExit: input.trackExit
1026
+ trackExit: input.trackExit,
1027
+ trackWrapSettled: (settled) => wrapSettlements.push(settled)
1004
1028
  })
1005
1029
  });
1006
1030
  } catch (error) {
@@ -1045,6 +1069,9 @@ async function launchSingleSdkQuery(input) {
1045
1069
  };
1046
1070
  input.setInFlightClose(closeHandle);
1047
1071
  try {
1072
+ if (wrapSettlements.length > 0) {
1073
+ await raceAbort(Promise.all(wrapSettlements), input.signal);
1074
+ }
1048
1075
  if (hasDelegationVerification(input.config.verifyDelegation)) {
1049
1076
  emitStartupPhase(input.onStartupPhase, "verifying", input.config.onObserverError);
1050
1077
  await verifyClaudeSdkDelegation(query, input.config.verifyDelegation, input.signal);
@@ -1158,7 +1185,7 @@ function isUuid(value) {
1158
1185
  return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
1159
1186
  }
1160
1187
  var ENGINE_EXIT_WAIT_MS = 1e4;
1161
- async function waitForEngineExits(exits) {
1188
+ async function waitForClaudeSdkEngineExits(exits) {
1162
1189
  if (exits.size === 0)
1163
1190
  return;
1164
1191
  await raceAbort(Promise.all(exits), AbortSignal.timeout(ENGINE_EXIT_WAIT_MS)).catch(() => {
@@ -1329,13 +1356,16 @@ async function openClaudeSdkSession(config, limits, input) {
1329
1356
  exited.then(() => engineExits.delete(exited));
1330
1357
  }
1331
1358
  });
1332
- if (hasDelegationVerification(config.verifyDelegation)) {
1359
+ if (hasDelegationVerification(config.verifyDelegation) || config.wrapCommand !== undefined) {
1333
1360
  try {
1334
1361
  await ensureQuery(true);
1335
1362
  } catch (error) {
1336
1363
  shutdown(error);
1337
1364
  linked.abort();
1338
1365
  linked.release();
1366
+ await waitForClaudeSdkEngineExits(engineExits).catch(() => {
1367
+ return;
1368
+ });
1339
1369
  throw error;
1340
1370
  }
1341
1371
  }
@@ -1391,10 +1421,150 @@ async function openClaudeSdkSession(config, limits, input) {
1391
1421
  shutdown(new Error("The Claude SDK session is closed"));
1392
1422
  linked.abort();
1393
1423
  linked.release();
1394
- await waitForEngineExits(engineExits);
1424
+ await waitForClaudeSdkEngineExits(engineExits);
1425
+ }
1426
+ };
1427
+ }
1428
+
1429
+ // src/agent-executor/claude-sdk/probe.ts
1430
+ var DEFAULT_PROBE_TIMEOUT_MS = 30000;
1431
+ function createNeverYieldingPrompt(signal) {
1432
+ return {
1433
+ [Symbol.asyncIterator]() {
1434
+ return {
1435
+ next() {
1436
+ if (signal.aborted) {
1437
+ return Promise.resolve({ value: undefined, done: true });
1438
+ }
1439
+ return new Promise((resolve) => {
1440
+ signal.addEventListener("abort", () => resolve({ value: undefined, done: true }), {
1441
+ once: true
1442
+ });
1443
+ });
1444
+ },
1445
+ return() {
1446
+ return Promise.resolve({ value: undefined, done: true });
1447
+ }
1448
+ };
1395
1449
  }
1396
1450
  };
1397
1451
  }
1452
+ function buildProbeOptions(input) {
1453
+ const { config } = input;
1454
+ const probeConfig = { ...config, onProviderPid: undefined };
1455
+ const spawnHook = buildClaudeSdkSpawnHook({
1456
+ config: probeConfig,
1457
+ signal: input.signal,
1458
+ recordPid: () => {
1459
+ return;
1460
+ },
1461
+ onPidExit: () => {
1462
+ return;
1463
+ },
1464
+ trackExit: input.trackExit,
1465
+ trackWrapSettled: input.trackWrapSettled
1466
+ });
1467
+ return {
1468
+ cwd: input.cwd,
1469
+ env: input.env,
1470
+ abortController: input.abortController,
1471
+ persistSession: false,
1472
+ settings: { disableAllHooks: true },
1473
+ strictMcpConfig: true,
1474
+ mcpServers: {},
1475
+ allowedTools: [],
1476
+ canUseTool: async () => ({
1477
+ behavior: "deny",
1478
+ message: "Capability probe does not execute tools",
1479
+ interrupt: true
1480
+ }),
1481
+ ...config.pathToClaudeCodeExecutable !== undefined && {
1482
+ pathToClaudeCodeExecutable: config.pathToClaudeCodeExecutable
1483
+ },
1484
+ ...config.settingSources !== undefined && { settingSources: config.settingSources },
1485
+ ...config.extraArgs !== undefined && { extraArgs: config.extraArgs },
1486
+ ...config.stderr !== undefined && { stderr: config.stderr },
1487
+ ...spawnHook !== undefined && { spawnClaudeCodeProcess: spawnHook }
1488
+ };
1489
+ }
1490
+ async function probeClaudeSdkCapabilities(config, limits, input) {
1491
+ const ambient = config.ambient ?? process.env;
1492
+ const profile = engineProfile("claude-sdk", input.profile, ambient);
1493
+ if (input.timeoutMs !== undefined) {
1494
+ assertTimerMs("timeoutMs", input.timeoutMs);
1495
+ }
1496
+ input.signal?.throwIfAborted();
1497
+ const timeoutMs = input.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
1498
+ const env = engineEnvironment2(profile, ambient);
1499
+ return withSignalDeadline(timeoutMs, input.signal, () => new Error(`Claude SDK capability probe timed out after ${timeoutMs} ms`), async (deadlineSignal) => {
1500
+ const probeAbort = new AbortController;
1501
+ const onDeadlineAbort = () => probeAbort.abort(deadlineSignal.reason);
1502
+ if (deadlineSignal.aborted) {
1503
+ onDeadlineAbort();
1504
+ } else {
1505
+ deadlineSignal.addEventListener("abort", onDeadlineAbort, { once: true });
1506
+ }
1507
+ let query;
1508
+ const probeExits = new Set;
1509
+ const wrapSettlements = [];
1510
+ try {
1511
+ query = config.query({
1512
+ prompt: createNeverYieldingPrompt(probeAbort.signal),
1513
+ options: buildProbeOptions({
1514
+ config,
1515
+ cwd: input.cwd,
1516
+ env,
1517
+ abortController: probeAbort,
1518
+ signal: deadlineSignal,
1519
+ trackExit: (exited) => {
1520
+ probeExits.add(exited);
1521
+ exited.then(() => probeExits.delete(exited));
1522
+ },
1523
+ trackWrapSettled: (settled) => wrapSettlements.push(settled)
1524
+ })
1525
+ });
1526
+ if (wrapSettlements.length > 0) {
1527
+ await raceAbort(Promise.all(wrapSettlements), deadlineSignal);
1528
+ }
1529
+ const initFn = query.initializationResult;
1530
+ const modelsFn = query.supportedModels;
1531
+ const commandsFn = query.supportedCommands;
1532
+ const accountFn = query.accountInfo;
1533
+ if (typeof initFn !== "function" && typeof modelsFn !== "function" && typeof commandsFn !== "function" && typeof accountFn !== "function") {
1534
+ throw new Error("The configured Claude SDK query does not expose capability probe RPCs");
1535
+ }
1536
+ const initializationResult = typeof initFn === "function" ? await raceAbort(initFn.call(query), deadlineSignal) : undefined;
1537
+ const usageFn = input.includeUsage === true ? query.usage_EXPERIMENTAL_MAY_CHANGE_DO_NOT_RELY_ON_THIS_API_YET : undefined;
1538
+ const [models, commands, account, rawUsage] = await Promise.all([
1539
+ typeof modelsFn === "function" ? raceAbort(modelsFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.models),
1540
+ typeof commandsFn === "function" ? raceAbort(commandsFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.commands),
1541
+ typeof accountFn === "function" ? raceAbort(accountFn.call(query), deadlineSignal) : Promise.resolve(initializationResult?.account),
1542
+ typeof usageFn === "function" ? raceAbort(usageFn.call(query, { skipBehaviors: true }), deadlineSignal) : Promise.resolve(undefined)
1543
+ ]);
1544
+ let usage;
1545
+ if (input.includeUsage === true) {
1546
+ usage = typeof usageFn === "function" && rawUsage !== undefined ? normalizeClaudeSdkUsageRead(rawUsage, (planLimits) => {
1547
+ limits.set(profile.profileId, planLimits);
1548
+ }) : unpublishedClaudeSdkUsageRead();
1549
+ }
1550
+ return {
1551
+ ...initializationResult !== undefined && { initializationResult },
1552
+ ...models !== undefined && { models },
1553
+ ...commands !== undefined && { commands },
1554
+ ...account !== undefined && { account },
1555
+ ...usage !== undefined && { usage }
1556
+ };
1557
+ } finally {
1558
+ deadlineSignal.removeEventListener("abort", onDeadlineAbort);
1559
+ if (!probeAbort.signal.aborted)
1560
+ probeAbort.abort();
1561
+ closeQuerySafely(query, config.onObserverError);
1562
+ await waitForClaudeSdkEngineExits(probeExits).catch(() => {
1563
+ return;
1564
+ });
1565
+ }
1566
+ });
1567
+ }
1398
1568
 
1399
1569
  // src/agent-executor/claude-sdk/driver.ts
1400
1570
  function createClaudeSdkDriver(config) {