stitchkit 0.132.1 → 0.132.3
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.
- package/CHANGELOG.md +16 -0
- package/dist/agent-executor/acp/v1-handlers.d.ts +2 -1
- package/dist/agent-executor/acp/v1-handlers.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-permission.d.ts +2 -0
- package/dist/agent-executor/acp/v1-permission.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-probe.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-session-options.d.ts +19 -0
- package/dist/agent-executor/acp/v1-session-options.d.ts.map +1 -0
- package/dist/agent-executor/acp/v1-session.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-turn.d.ts +7 -1
- package/dist/agent-executor/acp/v1-turn.d.ts.map +1 -1
- package/dist/agent-executor/claude/session.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/controls.d.ts +1 -0
- package/dist/agent-executor/claude-sdk/controls.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/query-loop.d.ts +6 -1
- package/dist/agent-executor/claude-sdk/query-loop.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/query-manager.d.ts +40 -0
- package/dist/agent-executor/claude-sdk/query-manager.d.ts.map +1 -0
- package/dist/agent-executor/claude-sdk/query-options.d.ts +3 -1
- package/dist/agent-executor/claude-sdk/query-options.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/session.d.ts.map +1 -1
- package/dist/agent-executor/codex/handlers.d.ts +1 -1
- package/dist/agent-executor/codex/handlers.d.ts.map +1 -1
- package/dist/agent-executor/codex/session.d.ts.map +1 -1
- package/dist/agent-executor/codex/shared-server.d.ts.map +1 -1
- package/dist/agent-executor/codex/shared-thread.d.ts +4 -2
- package/dist/agent-executor/codex/shared-thread.d.ts.map +1 -1
- package/dist/agent-executor/codex/thread-open.d.ts.map +1 -1
- package/dist/agent-runtime/branch-observations.d.ts +5 -5
- package/dist/entrypoints/agent-executor/acp.js +305 -160
- package/dist/entrypoints/agent-executor/claude-sdk.js +269 -171
- package/dist/entrypoints/agent-executor/claude.js +153 -70
- package/dist/entrypoints/agent-executor/codex.js +78 -28
- package/package.json +1 -1
|
@@ -98,15 +98,16 @@ function requireMethod(query, name) {
|
|
|
98
98
|
return fn;
|
|
99
99
|
}
|
|
100
100
|
function createClaudeSdkControls(host) {
|
|
101
|
+
const guarded = (promise) => raceAbort(promise, host.signal).catch((error) => {
|
|
102
|
+
host.ensureAlive();
|
|
103
|
+
throw error;
|
|
104
|
+
});
|
|
101
105
|
const activeQuery = async () => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (host.isClosed())
|
|
106
|
-
throw new Error("The Claude SDK session is closed");
|
|
106
|
+
host.ensureAlive();
|
|
107
|
+
const active = await guarded(host.ensureQuery());
|
|
108
|
+
host.ensureAlive();
|
|
107
109
|
return active.query;
|
|
108
110
|
};
|
|
109
|
-
const guarded = (promise) => raceAbort(promise, host.signal);
|
|
110
111
|
return {
|
|
111
112
|
async setModel(model) {
|
|
112
113
|
const query = await activeQuery();
|
|
@@ -163,8 +164,7 @@ function createClaudeSdkControls(host) {
|
|
|
163
164
|
}
|
|
164
165
|
throw error;
|
|
165
166
|
}
|
|
166
|
-
|
|
167
|
-
throw new Error("The Claude SDK session is closed");
|
|
167
|
+
host.ensureAlive();
|
|
168
168
|
return normalizeClaudeSdkUsageRead(raw, host.recordPlanLimits);
|
|
169
169
|
},
|
|
170
170
|
async mcpServerStatus() {
|
|
@@ -452,6 +452,11 @@ function createSdkCanUseTool(state) {
|
|
|
452
452
|
resolve: finish
|
|
453
453
|
});
|
|
454
454
|
if (turn.pending.has(interactionId)) {
|
|
455
|
+
if (state.isClosed()) {
|
|
456
|
+
turn.pending.delete(interactionId);
|
|
457
|
+
finish({ behavior: "deny", message: "The session was closed", interrupt: true });
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
455
460
|
options.signal.addEventListener("abort", onToolAbort, { once: true });
|
|
456
461
|
if (state.onPermissionRequest !== undefined) {
|
|
457
462
|
const meta = {
|
|
@@ -486,15 +491,26 @@ function createSdkCanUseTool(state) {
|
|
|
486
491
|
}
|
|
487
492
|
});
|
|
488
493
|
}
|
|
489
|
-
function sdkTurnHandle(turn, query) {
|
|
494
|
+
function sdkTurnHandle(turn, query, ensureAlive) {
|
|
490
495
|
return {
|
|
491
496
|
deltas: turn.channel,
|
|
492
497
|
async interrupt() {
|
|
498
|
+
if (turn.settled)
|
|
499
|
+
return;
|
|
500
|
+
try {
|
|
501
|
+
ensureAlive();
|
|
502
|
+
} catch {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
493
505
|
turn.state.interruptRequested = true;
|
|
494
506
|
drainEngineRequests(turn, stoppedPermission("The turn was interrupted"));
|
|
495
507
|
await query.interrupt();
|
|
496
508
|
},
|
|
497
509
|
async respond(interactionId, rawReply) {
|
|
510
|
+
ensureAlive();
|
|
511
|
+
if (turn.lost !== undefined) {
|
|
512
|
+
throw turn.lost.error instanceof Error ? turn.lost.error : new Error(String(turn.lost.error));
|
|
513
|
+
}
|
|
498
514
|
const waiting = turn.pending.get(interactionId);
|
|
499
515
|
if (waiting === undefined) {
|
|
500
516
|
throw new TypeError(`Unknown interaction ${interactionId} for this turn`);
|
|
@@ -525,6 +541,8 @@ function runSdkQueryLoop(input) {
|
|
|
525
541
|
(async () => {
|
|
526
542
|
try {
|
|
527
543
|
for await (const record of input.handle.query) {
|
|
544
|
+
if (input.isClosed())
|
|
545
|
+
break;
|
|
528
546
|
const turn = input.getTurn();
|
|
529
547
|
const verdict = classifyClaudeRecord(record, turn?.state ?? newClaudeTurnState("between-turns"));
|
|
530
548
|
if (verdict.kind === "session") {
|
|
@@ -554,14 +572,24 @@ function runSdkQueryLoop(input) {
|
|
|
554
572
|
if (turn !== undefined && input.onTurnUsage !== undefined) {
|
|
555
573
|
invokeSafely(() => input.onTurnUsage?.(summary));
|
|
556
574
|
}
|
|
575
|
+
if (turn !== undefined)
|
|
576
|
+
turn.settled = true;
|
|
557
577
|
endEngineTurn(turn, stoppedPermission("The turn ended"), verdict.deltas);
|
|
558
578
|
input.clearTurn();
|
|
559
579
|
} else if (verdict.kind === "unhandled") {
|
|
560
580
|
offerTurnDelta(turn?.channel, verdict.delta);
|
|
561
581
|
}
|
|
562
582
|
}
|
|
583
|
+
if (input.isClosed()) {
|
|
584
|
+
input.handle.close();
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
563
587
|
throw new Error(input.getTurn() !== undefined ? "The Agent SDK query ended during the turn" : "The Agent SDK query ended while idle");
|
|
564
588
|
} catch (error) {
|
|
589
|
+
if (input.isClosed()) {
|
|
590
|
+
input.handle.close();
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
565
593
|
input.onExit(input.handle, error);
|
|
566
594
|
}
|
|
567
595
|
})();
|
|
@@ -696,6 +724,28 @@ function validateClaudeSdkConfig(config) {
|
|
|
696
724
|
assertTimerMs("verifyDelegation.timeoutMs", config.verifyDelegation.timeoutMs);
|
|
697
725
|
}
|
|
698
726
|
}
|
|
727
|
+
function validateClaudeSdkOpenInput(input) {
|
|
728
|
+
const hasNativeThreadId = typeof input.nativeThreadId === "string" && input.nativeThreadId.trim().length > 0;
|
|
729
|
+
if (input.resumeSessionAt !== undefined) {
|
|
730
|
+
if (typeof input.resumeSessionAt !== "string" || input.resumeSessionAt.trim().length === 0) {
|
|
731
|
+
throw new TypeError("Claude SDK open() requires a non-empty resumeSessionAt UUID when provided");
|
|
732
|
+
}
|
|
733
|
+
if (!hasNativeThreadId) {
|
|
734
|
+
throw new TypeError("Claude SDK open() requires nativeThreadId when resumeSessionAt is provided");
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (input.resumeDropsTurn !== undefined) {
|
|
738
|
+
if (typeof input.resumeDropsTurn !== "string" || input.resumeDropsTurn.trim().length === 0) {
|
|
739
|
+
throw new TypeError("Claude SDK open() requires a non-empty resumeDropsTurn prompt UUID when provided");
|
|
740
|
+
}
|
|
741
|
+
if (!hasNativeThreadId) {
|
|
742
|
+
throw new TypeError("Claude SDK open() requires nativeThreadId when resumeDropsTurn is provided");
|
|
743
|
+
}
|
|
744
|
+
if (input.resumeSessionAt === undefined) {
|
|
745
|
+
throw new TypeError("Claude SDK open() requires resumeSessionAt when resumeDropsTurn is provided");
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
699
749
|
function hasDelegationVerification(check) {
|
|
700
750
|
return check?.requireHooksApplied === true || check?.requireNoMcpServers === true;
|
|
701
751
|
}
|
|
@@ -854,68 +904,12 @@ async function replayClaudeSdkFlagSettings(query, overlay, signal) {
|
|
|
854
904
|
|
|
855
905
|
// src/agent-executor/claude-sdk/session.ts
|
|
856
906
|
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
}
|
|
860
|
-
function createSdkStartTurn(input) {
|
|
861
|
-
return imageTurnStarter({
|
|
862
|
-
driverLabel: "The Agent SDK driver",
|
|
863
|
-
busyMessage: "A turn is already running in this Claude session",
|
|
864
|
-
imageOptions: input.config,
|
|
865
|
-
signal: input.signal,
|
|
866
|
-
check(turnInput) {
|
|
867
|
-
if (input.isClosed())
|
|
868
|
-
throw new Error("The Claude SDK session is closed");
|
|
869
|
-
if (input.getTurn() !== undefined || input.isStarting())
|
|
870
|
-
throw new Error("A turn is already running in this Claude session");
|
|
871
|
-
if (!isUuid(turnInput.clientId))
|
|
872
|
-
throw new TypeError("The Agent SDK driver needs a UUID client id");
|
|
873
|
-
},
|
|
874
|
-
async dispatch(turnInput, parts) {
|
|
875
|
-
const clientId = turnInput.clientId;
|
|
876
|
-
if (!isUuid(clientId))
|
|
877
|
-
throw new TypeError("The Agent SDK driver needs a UUID client id");
|
|
878
|
-
input.setStarting(true);
|
|
879
|
-
let active;
|
|
880
|
-
try {
|
|
881
|
-
active = await input.ensureQuery(false);
|
|
882
|
-
} finally {
|
|
883
|
-
input.setStarting(false);
|
|
884
|
-
}
|
|
885
|
-
if (input.isClosed())
|
|
886
|
-
throw new Error("The Claude SDK session is closed");
|
|
887
|
-
if (input.getTurn() !== undefined)
|
|
888
|
-
throw new Error("A turn is already running in this Claude session");
|
|
889
|
-
const turn = {
|
|
890
|
-
state: newClaudeTurnState(turnInput.turnId),
|
|
891
|
-
channel: createTurnDeltaChannel(),
|
|
892
|
-
pending: new Map
|
|
893
|
-
};
|
|
894
|
-
input.setTurn(turn);
|
|
895
|
-
try {
|
|
896
|
-
active.send({
|
|
897
|
-
type: "user",
|
|
898
|
-
message: { role: "user", content: claudeUserContent(parts) },
|
|
899
|
-
parent_tool_use_id: null,
|
|
900
|
-
uuid: clientId,
|
|
901
|
-
session_id: input.sessionId
|
|
902
|
-
});
|
|
903
|
-
input.markResumable();
|
|
904
|
-
active.startLoop();
|
|
905
|
-
return sdkTurnHandle(turn, active.query);
|
|
906
|
-
} catch (error) {
|
|
907
|
-
if (input.getTurn() === turn)
|
|
908
|
-
input.setTurn(undefined);
|
|
909
|
-
throw error;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
});
|
|
913
|
-
}
|
|
907
|
+
|
|
908
|
+
// src/agent-executor/claude-sdk/query-manager.ts
|
|
914
909
|
async function launchSingleSdkQuery(input) {
|
|
915
|
-
|
|
916
|
-
throw new Error("The Claude SDK session is closed");
|
|
917
|
-
input.signal.throwIfAborted();
|
|
910
|
+
input.ensureAlive();
|
|
918
911
|
emitStartupPhase(input.onStartupPhase, "spawning");
|
|
912
|
+
input.ensureAlive();
|
|
919
913
|
const pushed = pushablePrompt();
|
|
920
914
|
let query;
|
|
921
915
|
try {
|
|
@@ -965,6 +959,7 @@ async function launchSingleSdkQuery(input) {
|
|
|
965
959
|
handle,
|
|
966
960
|
profileId: input.profileId,
|
|
967
961
|
limits: input.limits,
|
|
962
|
+
isClosed: input.isClosed,
|
|
968
963
|
getTurn: input.getTurn,
|
|
969
964
|
clearTurn: input.clearTurn,
|
|
970
965
|
...input.config.onEvent !== undefined && { onEvent: input.config.onEvent },
|
|
@@ -985,16 +980,13 @@ async function launchSingleSdkQuery(input) {
|
|
|
985
980
|
if (input.flagSettingsOverlay !== undefined) {
|
|
986
981
|
await replayClaudeSdkFlagSettings(query, input.flagSettingsOverlay, input.signal);
|
|
987
982
|
}
|
|
988
|
-
|
|
989
|
-
throw input.isClosed() ? new Error("The Claude SDK session is closed") : abortReason(input.signal);
|
|
990
|
-
}
|
|
983
|
+
input.ensureAlive();
|
|
991
984
|
} catch (error) {
|
|
992
985
|
input.setInFlightClose(undefined);
|
|
993
986
|
input.recordPid(undefined);
|
|
994
987
|
closeHandle();
|
|
995
988
|
throw error;
|
|
996
989
|
}
|
|
997
|
-
input.setInFlightClose(undefined);
|
|
998
990
|
return handle;
|
|
999
991
|
}
|
|
1000
992
|
function notifyQueryExit(callback, error, context) {
|
|
@@ -1002,27 +994,154 @@ function notifyQueryExit(callback, error, context) {
|
|
|
1002
994
|
return;
|
|
1003
995
|
invokeSafely(() => callback(error, context));
|
|
1004
996
|
}
|
|
1005
|
-
function
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
997
|
+
function createSdkQueryManager(state) {
|
|
998
|
+
let opening;
|
|
999
|
+
const onExit = (exited, error) => {
|
|
1000
|
+
const ownsSession = state.getRunning() === exited;
|
|
1001
|
+
const shouldNotify = ownsSession && state.isOpened() && !state.isClosed();
|
|
1002
|
+
const turn = ownsSession ? state.getTurn() : undefined;
|
|
1003
|
+
const duringTurn = turn !== undefined;
|
|
1004
|
+
if (ownsSession) {
|
|
1005
|
+
state.setRunning(undefined);
|
|
1006
|
+
state.clearTurn();
|
|
1007
|
+
state.recordPid(undefined);
|
|
1010
1008
|
}
|
|
1011
|
-
if (
|
|
1012
|
-
|
|
1009
|
+
if (turn !== undefined) {
|
|
1010
|
+
turn.settled = true;
|
|
1011
|
+
turn.lost = { error };
|
|
1013
1012
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
if (
|
|
1017
|
-
|
|
1013
|
+
loseEngineTurn(turn, stoppedPermission("The Claude SDK query ended"), error);
|
|
1014
|
+
exited.close();
|
|
1015
|
+
if (shouldNotify) {
|
|
1016
|
+
notifyQueryExit(state.config.onQueryExit, error, {
|
|
1017
|
+
duringTurn,
|
|
1018
|
+
nativeThreadId: state.sessionId
|
|
1019
|
+
});
|
|
1018
1020
|
}
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
+
};
|
|
1022
|
+
return async (startLoopImmediately) => {
|
|
1023
|
+
state.ensureAlive();
|
|
1024
|
+
const active = state.getRunning();
|
|
1025
|
+
if (active !== undefined) {
|
|
1026
|
+
if (startLoopImmediately)
|
|
1027
|
+
active.startLoop();
|
|
1028
|
+
return active;
|
|
1021
1029
|
}
|
|
1022
|
-
if (
|
|
1023
|
-
|
|
1030
|
+
if (opening === undefined) {
|
|
1031
|
+
const resumeState = state.getResume();
|
|
1032
|
+
const liveConfig = state.getLiveConfig();
|
|
1033
|
+
const promise = launchSingleSdkQuery({
|
|
1034
|
+
config: state.config,
|
|
1035
|
+
cwd: state.cwd,
|
|
1036
|
+
env: { ...state.env },
|
|
1037
|
+
profileId: state.profileId,
|
|
1038
|
+
limits: state.limits,
|
|
1039
|
+
sessionId: state.sessionId,
|
|
1040
|
+
resume: resumeState.resume,
|
|
1041
|
+
...resumeState.resumeSessionAt !== undefined && {
|
|
1042
|
+
resumeSessionAt: resumeState.resumeSessionAt
|
|
1043
|
+
},
|
|
1044
|
+
...resumeState.resumeDropsTurn !== undefined && {
|
|
1045
|
+
resumeDropsTurn: resumeState.resumeDropsTurn
|
|
1046
|
+
},
|
|
1047
|
+
activeModel: liveConfig.activeModel,
|
|
1048
|
+
activePermissionMode: liveConfig.activePermissionMode,
|
|
1049
|
+
flagSettingsOverlay: liveConfig.flagSettingsOverlay,
|
|
1050
|
+
signal: state.signal,
|
|
1051
|
+
canUseTool: state.canUseTool,
|
|
1052
|
+
isClosed: state.isClosed,
|
|
1053
|
+
ensureAlive: state.ensureAlive,
|
|
1054
|
+
getTurn: state.getTurn,
|
|
1055
|
+
clearTurn: state.clearTurn,
|
|
1056
|
+
setInFlightClose: state.setInFlightClose,
|
|
1057
|
+
recordPid: state.recordPid,
|
|
1058
|
+
onPidExit: state.onPidExit,
|
|
1059
|
+
...state.onStartupPhase !== undefined && { onStartupPhase: state.onStartupPhase },
|
|
1060
|
+
onExit
|
|
1061
|
+
}).then((handle) => {
|
|
1062
|
+
if (state.isClosed()) {
|
|
1063
|
+
if (state.getInFlightClose() === handle.close)
|
|
1064
|
+
state.setInFlightClose(undefined);
|
|
1065
|
+
state.recordPid(undefined);
|
|
1066
|
+
handle.close();
|
|
1067
|
+
state.ensureAlive();
|
|
1068
|
+
throw new Error("The Claude SDK session is closed");
|
|
1069
|
+
}
|
|
1070
|
+
state.setRunning(handle);
|
|
1071
|
+
if (state.getInFlightClose() === handle.close)
|
|
1072
|
+
state.setInFlightClose(undefined);
|
|
1073
|
+
return handle;
|
|
1074
|
+
}).finally(() => {
|
|
1075
|
+
if (opening === promise)
|
|
1076
|
+
opening = undefined;
|
|
1077
|
+
});
|
|
1078
|
+
opening = promise;
|
|
1024
1079
|
}
|
|
1025
|
-
|
|
1080
|
+
const handle = await opening;
|
|
1081
|
+
state.ensureAlive();
|
|
1082
|
+
if (startLoopImmediately)
|
|
1083
|
+
handle.startLoop();
|
|
1084
|
+
return handle;
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// src/agent-executor/claude-sdk/session.ts
|
|
1089
|
+
function isUuid(value) {
|
|
1090
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
|
1091
|
+
}
|
|
1092
|
+
function createSdkStartTurn(input) {
|
|
1093
|
+
return imageTurnStarter({
|
|
1094
|
+
driverLabel: "The Agent SDK driver",
|
|
1095
|
+
busyMessage: "A turn is already running in this Claude session",
|
|
1096
|
+
imageOptions: input.config,
|
|
1097
|
+
signal: input.signal,
|
|
1098
|
+
check(turnInput) {
|
|
1099
|
+
input.ensureAlive();
|
|
1100
|
+
if (input.getTurn() !== undefined || input.isStarting())
|
|
1101
|
+
throw new Error("A turn is already running in this Claude session");
|
|
1102
|
+
if (!isUuid(turnInput.clientId))
|
|
1103
|
+
throw new TypeError("The Agent SDK driver needs a UUID client id");
|
|
1104
|
+
},
|
|
1105
|
+
async dispatch(turnInput, parts) {
|
|
1106
|
+
const clientId = turnInput.clientId;
|
|
1107
|
+
if (!isUuid(clientId))
|
|
1108
|
+
throw new TypeError("The Agent SDK driver needs a UUID client id");
|
|
1109
|
+
input.setStarting(true);
|
|
1110
|
+
let active;
|
|
1111
|
+
try {
|
|
1112
|
+
active = await input.ensureQuery(false);
|
|
1113
|
+
} finally {
|
|
1114
|
+
input.setStarting(false);
|
|
1115
|
+
}
|
|
1116
|
+
input.ensureAlive();
|
|
1117
|
+
if (input.getTurn() !== undefined)
|
|
1118
|
+
throw new Error("A turn is already running in this Claude session");
|
|
1119
|
+
const turn = {
|
|
1120
|
+
state: newClaudeTurnState(turnInput.turnId),
|
|
1121
|
+
channel: createTurnDeltaChannel(),
|
|
1122
|
+
pending: new Map,
|
|
1123
|
+
settled: false
|
|
1124
|
+
};
|
|
1125
|
+
input.setTurn(turn);
|
|
1126
|
+
try {
|
|
1127
|
+
active.send({
|
|
1128
|
+
type: "user",
|
|
1129
|
+
message: { role: "user", content: claudeUserContent(parts) },
|
|
1130
|
+
parent_tool_use_id: null,
|
|
1131
|
+
uuid: clientId,
|
|
1132
|
+
session_id: input.sessionId
|
|
1133
|
+
});
|
|
1134
|
+
input.markResumable();
|
|
1135
|
+
active.startLoop();
|
|
1136
|
+
return sdkTurnHandle(turn, active.query, input.ensureAlive);
|
|
1137
|
+
} catch (error) {
|
|
1138
|
+
turn.settled = true;
|
|
1139
|
+
if (input.getTurn() === turn)
|
|
1140
|
+
input.setTurn(undefined);
|
|
1141
|
+
throw error;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
});
|
|
1026
1145
|
}
|
|
1027
1146
|
function recordSdkPlanLimitsFailure(limits, profileId, error) {
|
|
1028
1147
|
const rawMessage = error instanceof Error ? error.message : String(error);
|
|
@@ -1048,21 +1167,37 @@ async function openClaudeSdkSession(config, limits, input) {
|
|
|
1048
1167
|
let current;
|
|
1049
1168
|
let startingTurn = false;
|
|
1050
1169
|
let opened = false;
|
|
1170
|
+
let closing = false;
|
|
1051
1171
|
let closed = false;
|
|
1172
|
+
let closeReason;
|
|
1052
1173
|
let providerPid;
|
|
1053
1174
|
let activeModel = config.model;
|
|
1054
1175
|
let activePermissionMode = config.permissionMode ?? "default";
|
|
1055
1176
|
let flagSettingsOverlay;
|
|
1056
1177
|
let running;
|
|
1057
|
-
let opening;
|
|
1058
1178
|
let inFlightClose;
|
|
1179
|
+
const linked = sessionAbort(input.signal);
|
|
1180
|
+
const isClosed = () => closing || closed || input.signal?.aborted === true || linked.signal.aborted;
|
|
1181
|
+
const ensureSessionAlive = () => {
|
|
1182
|
+
if (closing)
|
|
1183
|
+
throw new Error("The Claude SDK session is closed");
|
|
1184
|
+
if (input.signal?.aborted)
|
|
1185
|
+
throw abortReason(input.signal);
|
|
1186
|
+
if (linked.signal.aborted)
|
|
1187
|
+
throw abortReason(linked.signal);
|
|
1188
|
+
if (closed)
|
|
1189
|
+
throw closeReason ?? new Error("The Claude SDK session is closed");
|
|
1190
|
+
};
|
|
1059
1191
|
const shutdown = (reason) => {
|
|
1060
1192
|
if (closed)
|
|
1061
1193
|
return;
|
|
1062
1194
|
closed = true;
|
|
1195
|
+
closeReason = reason;
|
|
1063
1196
|
providerPid = undefined;
|
|
1064
1197
|
const turn = current;
|
|
1065
1198
|
current = undefined;
|
|
1199
|
+
if (turn !== undefined)
|
|
1200
|
+
turn.settled = true;
|
|
1066
1201
|
loseEngineTurn(turn, stoppedPermission("The session was closed"), reason);
|
|
1067
1202
|
const pendingClose = inFlightClose;
|
|
1068
1203
|
inFlightClose = undefined;
|
|
@@ -1071,94 +1206,55 @@ async function openClaudeSdkSession(config, limits, input) {
|
|
|
1071
1206
|
running = undefined;
|
|
1072
1207
|
active?.close();
|
|
1073
1208
|
};
|
|
1074
|
-
const linked = sessionAbort(input.signal);
|
|
1075
1209
|
linked.signal.addEventListener("abort", () => shutdown(abortReason(linked.signal)), {
|
|
1076
1210
|
once: true
|
|
1077
1211
|
});
|
|
1078
1212
|
const canUseTool = createSdkCanUseTool({
|
|
1079
1213
|
getTurn: () => current,
|
|
1080
|
-
isClosed
|
|
1214
|
+
isClosed,
|
|
1081
1215
|
...config.onPermissionRequest !== undefined && {
|
|
1082
1216
|
onPermissionRequest: config.onPermissionRequest
|
|
1083
1217
|
}
|
|
1084
1218
|
});
|
|
1085
|
-
const ensureQuery =
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
providerPid = pid;
|
|
1123
|
-
},
|
|
1124
|
-
onPidExit: (exitedPid) => {
|
|
1125
|
-
if (providerPid === exitedPid)
|
|
1126
|
-
providerPid = undefined;
|
|
1127
|
-
},
|
|
1128
|
-
...input.onStartupPhase !== undefined && { onStartupPhase: input.onStartupPhase },
|
|
1129
|
-
onExit: (exited, error) => {
|
|
1130
|
-
const ownsSession = running === exited;
|
|
1131
|
-
const shouldNotify = ownsSession && opened && !closed && !linked.signal.aborted;
|
|
1132
|
-
const turn = ownsSession ? current : undefined;
|
|
1133
|
-
const duringTurn = turn !== undefined;
|
|
1134
|
-
if (ownsSession) {
|
|
1135
|
-
running = undefined;
|
|
1136
|
-
current = undefined;
|
|
1137
|
-
providerPid = undefined;
|
|
1138
|
-
}
|
|
1139
|
-
loseEngineTurn(turn, stoppedPermission("The Claude SDK query ended"), error);
|
|
1140
|
-
exited.close();
|
|
1141
|
-
if (shouldNotify) {
|
|
1142
|
-
notifyQueryExit(config.onQueryExit, error, {
|
|
1143
|
-
duringTurn,
|
|
1144
|
-
nativeThreadId: sessionId
|
|
1145
|
-
});
|
|
1146
|
-
}
|
|
1147
|
-
}
|
|
1148
|
-
}).then((handle) => {
|
|
1149
|
-
running = handle;
|
|
1150
|
-
return handle;
|
|
1151
|
-
}).finally(() => {
|
|
1152
|
-
if (opening === promise)
|
|
1153
|
-
opening = undefined;
|
|
1154
|
-
});
|
|
1155
|
-
opening = promise;
|
|
1219
|
+
const ensureQuery = createSdkQueryManager({
|
|
1220
|
+
config,
|
|
1221
|
+
cwd: input.cwd,
|
|
1222
|
+
env,
|
|
1223
|
+
profileId: profile.profileId,
|
|
1224
|
+
limits,
|
|
1225
|
+
sessionId,
|
|
1226
|
+
signal: linked.signal,
|
|
1227
|
+
canUseTool,
|
|
1228
|
+
...input.onStartupPhase !== undefined && { onStartupPhase: input.onStartupPhase },
|
|
1229
|
+
isOpened: () => opened,
|
|
1230
|
+
isClosed,
|
|
1231
|
+
ensureAlive: ensureSessionAlive,
|
|
1232
|
+
getResume: () => ({
|
|
1233
|
+
resume,
|
|
1234
|
+
...pendingResumeSessionAt !== undefined && { resumeSessionAt: pendingResumeSessionAt },
|
|
1235
|
+
...pendingResumeDropsTurn !== undefined && { resumeDropsTurn: pendingResumeDropsTurn }
|
|
1236
|
+
}),
|
|
1237
|
+
getLiveConfig: () => ({ activeModel, activePermissionMode, flagSettingsOverlay }),
|
|
1238
|
+
getTurn: () => current,
|
|
1239
|
+
clearTurn: () => {
|
|
1240
|
+
current = undefined;
|
|
1241
|
+
},
|
|
1242
|
+
getRunning: () => running,
|
|
1243
|
+
setRunning: (handle) => {
|
|
1244
|
+
running = handle;
|
|
1245
|
+
},
|
|
1246
|
+
getInFlightClose: () => inFlightClose,
|
|
1247
|
+
setInFlightClose: (fn) => {
|
|
1248
|
+
inFlightClose = fn;
|
|
1249
|
+
},
|
|
1250
|
+
recordPid: (pid) => {
|
|
1251
|
+
providerPid = pid;
|
|
1252
|
+
},
|
|
1253
|
+
onPidExit: (exitedPid) => {
|
|
1254
|
+
if (providerPid === exitedPid)
|
|
1255
|
+
providerPid = undefined;
|
|
1156
1256
|
}
|
|
1157
|
-
|
|
1158
|
-
if (startLoopImmediately)
|
|
1159
|
-
handle.startLoop();
|
|
1160
|
-
return handle;
|
|
1161
|
-
};
|
|
1257
|
+
});
|
|
1162
1258
|
if (hasDelegationVerification(config.verifyDelegation)) {
|
|
1163
1259
|
try {
|
|
1164
1260
|
await ensureQuery(true);
|
|
@@ -1173,7 +1269,7 @@ async function openClaudeSdkSession(config, limits, input) {
|
|
|
1173
1269
|
config,
|
|
1174
1270
|
sessionId,
|
|
1175
1271
|
signal: linked.signal,
|
|
1176
|
-
|
|
1272
|
+
ensureAlive: ensureSessionAlive,
|
|
1177
1273
|
getTurn: () => current,
|
|
1178
1274
|
setTurn: (turn) => {
|
|
1179
1275
|
current = turn;
|
|
@@ -1191,7 +1287,8 @@ async function openClaudeSdkSession(config, limits, input) {
|
|
|
1191
1287
|
});
|
|
1192
1288
|
const controls = createClaudeSdkControls({
|
|
1193
1289
|
signal: linked.signal,
|
|
1194
|
-
isClosed
|
|
1290
|
+
isClosed,
|
|
1291
|
+
ensureAlive: ensureSessionAlive,
|
|
1195
1292
|
ensureQuery: () => ensureQuery(true),
|
|
1196
1293
|
setActiveModel: (model) => {
|
|
1197
1294
|
activeModel = model;
|
|
@@ -1216,7 +1313,8 @@ async function openClaudeSdkSession(config, limits, input) {
|
|
|
1216
1313
|
startTurn,
|
|
1217
1314
|
...controls,
|
|
1218
1315
|
async close() {
|
|
1219
|
-
|
|
1316
|
+
closing = true;
|
|
1317
|
+
shutdown(new Error("The Claude SDK session is closed"));
|
|
1220
1318
|
linked.abort();
|
|
1221
1319
|
linked.release();
|
|
1222
1320
|
}
|