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.
Files changed (34) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/agent-executor/acp/v1-handlers.d.ts +2 -1
  3. package/dist/agent-executor/acp/v1-handlers.d.ts.map +1 -1
  4. package/dist/agent-executor/acp/v1-permission.d.ts +2 -0
  5. package/dist/agent-executor/acp/v1-permission.d.ts.map +1 -1
  6. package/dist/agent-executor/acp/v1-probe.d.ts.map +1 -1
  7. package/dist/agent-executor/acp/v1-session-options.d.ts +19 -0
  8. package/dist/agent-executor/acp/v1-session-options.d.ts.map +1 -0
  9. package/dist/agent-executor/acp/v1-session.d.ts.map +1 -1
  10. package/dist/agent-executor/acp/v1-turn.d.ts +7 -1
  11. package/dist/agent-executor/acp/v1-turn.d.ts.map +1 -1
  12. package/dist/agent-executor/claude/session.d.ts.map +1 -1
  13. package/dist/agent-executor/claude-sdk/controls.d.ts +1 -0
  14. package/dist/agent-executor/claude-sdk/controls.d.ts.map +1 -1
  15. package/dist/agent-executor/claude-sdk/query-loop.d.ts +6 -1
  16. package/dist/agent-executor/claude-sdk/query-loop.d.ts.map +1 -1
  17. package/dist/agent-executor/claude-sdk/query-manager.d.ts +40 -0
  18. package/dist/agent-executor/claude-sdk/query-manager.d.ts.map +1 -0
  19. package/dist/agent-executor/claude-sdk/query-options.d.ts +3 -1
  20. package/dist/agent-executor/claude-sdk/query-options.d.ts.map +1 -1
  21. package/dist/agent-executor/claude-sdk/session.d.ts.map +1 -1
  22. package/dist/agent-executor/codex/handlers.d.ts +1 -1
  23. package/dist/agent-executor/codex/handlers.d.ts.map +1 -1
  24. package/dist/agent-executor/codex/session.d.ts.map +1 -1
  25. package/dist/agent-executor/codex/shared-server.d.ts.map +1 -1
  26. package/dist/agent-executor/codex/shared-thread.d.ts +4 -2
  27. package/dist/agent-executor/codex/shared-thread.d.ts.map +1 -1
  28. package/dist/agent-executor/codex/thread-open.d.ts.map +1 -1
  29. package/dist/agent-runtime/branch-observations.d.ts +5 -5
  30. package/dist/entrypoints/agent-executor/acp.js +305 -160
  31. package/dist/entrypoints/agent-executor/claude-sdk.js +269 -171
  32. package/dist/entrypoints/agent-executor/claude.js +153 -70
  33. package/dist/entrypoints/agent-executor/codex.js +78 -28
  34. package/package.json +1 -1
@@ -51,6 +51,9 @@ import {
51
51
  mergeRateLimitEvent2
52
52
  } from "../../index-athc412x.js";
53
53
  import"../../index-pzhqydxb.js";
54
+ import {
55
+ abortReason
56
+ } from "../../index-rptj3qnc.js";
54
57
 
55
58
  // src/agent-executor/claude/cli-driver.ts
56
59
  import { z } from "zod";
@@ -66,15 +69,23 @@ var LOST = {
66
69
  behavior: "deny",
67
70
  message: "The Claude CLI was lost"
68
71
  };
69
- function claudeTurnHandle(turn, send) {
72
+ function claudeTurnHandle(turn, send, ensureAlive) {
70
73
  return {
71
74
  deltas: turn.channel,
72
75
  async interrupt() {
76
+ if (turn.settled)
77
+ return;
78
+ try {
79
+ ensureAlive();
80
+ } catch {
81
+ return;
82
+ }
73
83
  turn.state.interruptRequested = true;
74
84
  drainEngineRequests(turn, () => INTERRUPTED);
75
85
  send(JSON.stringify(claudeInterrupt(randomUUID())));
76
86
  },
77
87
  async respond(interactionId, rawReply) {
88
+ ensureAlive();
78
89
  const waiting = turn.pending.get(interactionId);
79
90
  if (waiting === undefined) {
80
91
  throw new TypeError(`Unknown interaction ${interactionId} for this turn`);
@@ -83,67 +94,19 @@ function claudeTurnHandle(turn, send) {
83
94
  }
84
95
  };
85
96
  }
86
- async function openClaudeSession(config, input) {
87
- const { executable, prefix, ambient, limits } = config;
88
- const profile = engineProfile("claude-cli", input.profile, ambient);
89
- input.signal?.throwIfAborted();
90
- const env = engineEnvironment2(profile, ambient);
91
- const sessionId = input.nativeThreadId ?? randomUUID();
92
- const transcript = claudeTranscriptPath2({ home: profile.home, cwd: input.cwd, sessionId });
93
- const resume = input.nativeThreadId !== undefined && await claudeTranscriptExists(transcript);
94
- const linked = sessionAbort(input.signal);
95
- let engine;
96
- emitStartupPhase(input.onStartupPhase, "spawning");
97
- try {
98
- engine = spawnEngineProcess2({
99
- executable,
100
- args: [
101
- ...prefix,
102
- "-p",
103
- "--input-format",
104
- "stream-json",
105
- "--output-format",
106
- "stream-json",
107
- "--include-partial-messages",
108
- "--replay-user-messages",
109
- "--verbose",
110
- "--permission-prompts",
111
- "host",
112
- "--permission-prompt-tool",
113
- "stdio",
114
- "--permission-mode",
115
- config.permissionMode,
116
- ...resume ? ["--resume", sessionId] : ["--session-id", sessionId],
117
- ...config.model !== undefined ? ["--model", config.model] : []
118
- ],
119
- cwd: input.cwd,
120
- env,
121
- signal: linked.signal,
122
- maxLineBytes: CLAUDE_CLI_MAX_LINE_BYTES
123
- });
124
- } catch (error) {
125
- linked.release();
126
- throw error;
127
- }
128
- let current;
129
- let dead;
130
- const fail = (error) => {
131
- const failed = error instanceof Error ? error : new Error(String(error));
132
- dead = failed;
133
- const turn = current;
134
- current = undefined;
135
- loseEngineTurn(turn, () => LOST, failed);
136
- linked.abort();
137
- };
138
- const reading = (async () => {
97
+ function startClaudeReadingLoop(state) {
98
+ const { engine, profileId, limits } = state;
99
+ return (async () => {
139
100
  try {
140
101
  for await (const line of engine.lines) {
141
- const turn = current;
102
+ if (state.isStopped())
103
+ continue;
104
+ const turn = state.getTurn();
142
105
  const verdict = classifyClaudeRecord(JSON.parse(line), turn?.state ?? newClaudeTurnState("between-turns"));
143
106
  switch (verdict.kind) {
144
107
  case "limits":
145
- limits.set(profile.profileId, mergeRateLimitEvent2({
146
- previous: limits.get(profile.profileId),
108
+ limits.set(profileId, mergeRateLimitEvent2({
109
+ previous: limits.get(profileId),
147
110
  windows: claudePlanWindows2(verdict.info),
148
111
  now: new Date
149
112
  }));
@@ -155,7 +118,7 @@ async function openClaudeSession(config, input) {
155
118
  case "request": {
156
119
  const { delta, input, toolName, suppressSessionRule } = verdict;
157
120
  const answer = (decision) => {
158
- if (dead === undefined)
121
+ if (!state.isStopped() && state.getDead() === undefined)
159
122
  engine.send(JSON.stringify(claudePermissionResponse(delta.interactionId, decision)));
160
123
  };
161
124
  if (turn === undefined)
@@ -173,12 +136,14 @@ async function openClaudeSession(config, input) {
173
136
  }
174
137
  case "unhandled":
175
138
  offerTurnDelta(turn?.channel, verdict.delta);
176
- if (verdict.refuse !== undefined)
139
+ if (verdict.refuse !== undefined && !state.isStopped())
177
140
  engine.send(JSON.stringify(claudeControlError(verdict.refuse.requestId, verdict.refuse.message)));
178
141
  break;
179
142
  case "end":
143
+ if (turn !== undefined)
144
+ turn.settled = true;
180
145
  endEngineTurn(turn, () => ENDED, verdict.deltas);
181
- current = undefined;
146
+ state.clearTurn(turn);
182
147
  break;
183
148
  case "event":
184
149
  case "session":
@@ -186,22 +151,131 @@ async function openClaudeSession(config, input) {
186
151
  break;
187
152
  }
188
153
  }
189
- fail(new Error(`The Claude CLI exited during the turn: ${engine.stderrTail().slice(-2000)}`));
154
+ state.onExit(new Error(`The Claude CLI exited during the turn: ${engine.stderrTail().slice(-2000)}`));
190
155
  } catch (error) {
191
- fail(error);
156
+ state.onExit(error);
192
157
  await engine.stop().catch(() => {
193
158
  return;
194
159
  });
195
160
  }
196
161
  })();
162
+ }
163
+ function createClaudeSessionClose(state) {
164
+ let closePromise;
165
+ return () => {
166
+ if (closePromise !== undefined)
167
+ return closePromise;
168
+ state.markClosing();
169
+ state.settleTurn(new Error("The Claude CLI session is closed"));
170
+ state.abortLinked();
171
+ closePromise = stopConnection({ engine: state.engine, reading: state.reading }).finally(() => {
172
+ state.releaseLinked();
173
+ });
174
+ return closePromise;
175
+ };
176
+ }
177
+ async function openClaudeSession(config, input) {
178
+ const { executable, prefix, ambient, limits } = config;
179
+ const profile = engineProfile("claude-cli", input.profile, ambient);
180
+ input.signal?.throwIfAborted();
181
+ const env = engineEnvironment2(profile, ambient);
182
+ const sessionId = input.nativeThreadId ?? randomUUID();
183
+ const transcript = claudeTranscriptPath2({ home: profile.home, cwd: input.cwd, sessionId });
184
+ const resume = input.nativeThreadId !== undefined && await claudeTranscriptExists(transcript);
185
+ const linked = sessionAbort(input.signal);
186
+ let engine;
187
+ emitStartupPhase(input.onStartupPhase, "spawning");
188
+ try {
189
+ linked.signal.throwIfAborted();
190
+ engine = spawnEngineProcess2({
191
+ executable,
192
+ args: [
193
+ ...prefix,
194
+ "-p",
195
+ "--input-format",
196
+ "stream-json",
197
+ "--output-format",
198
+ "stream-json",
199
+ "--include-partial-messages",
200
+ "--replay-user-messages",
201
+ "--verbose",
202
+ "--permission-prompts",
203
+ "host",
204
+ "--permission-prompt-tool",
205
+ "stdio",
206
+ "--permission-mode",
207
+ config.permissionMode,
208
+ ...resume ? ["--resume", sessionId] : ["--session-id", sessionId],
209
+ ...config.model !== undefined ? ["--model", config.model] : []
210
+ ],
211
+ cwd: input.cwd,
212
+ env,
213
+ signal: linked.signal,
214
+ maxLineBytes: CLAUDE_CLI_MAX_LINE_BYTES
215
+ });
216
+ } catch (error) {
217
+ linked.release();
218
+ throw error;
219
+ }
220
+ let current;
221
+ let dead;
222
+ let closing = false;
223
+ const isStopped = () => closing || input.signal?.aborted === true || linked.signal.aborted;
224
+ const ensureAlive = () => {
225
+ if (closing)
226
+ throw new Error("The Claude CLI session is closed");
227
+ if (dead !== undefined)
228
+ throw dead;
229
+ if (input.signal?.aborted)
230
+ throw abortReason(input.signal);
231
+ if (linked.signal.aborted)
232
+ throw abortReason(linked.signal);
233
+ };
234
+ const settleTurn = (reason) => {
235
+ const turn = current;
236
+ current = undefined;
237
+ if (turn !== undefined)
238
+ turn.settled = true;
239
+ loseEngineTurn(turn, () => LOST, reason);
240
+ };
241
+ linked.signal.addEventListener("abort", () => {
242
+ if (closing || dead !== undefined)
243
+ return;
244
+ settleTurn(abortReason(linked.signal));
245
+ }, { once: true });
246
+ const reading = startClaudeReadingLoop({
247
+ engine,
248
+ profileId: profile.profileId,
249
+ limits,
250
+ isStopped,
251
+ getDead: () => dead,
252
+ getTurn: () => current,
253
+ clearTurn: (turn) => {
254
+ if (current === turn)
255
+ current = undefined;
256
+ },
257
+ onExit: (error) => {
258
+ if (closing) {
259
+ settleTurn(new Error("The Claude CLI session is closed"));
260
+ return;
261
+ }
262
+ if (input.signal?.aborted) {
263
+ settleTurn(abortReason(input.signal));
264
+ return;
265
+ }
266
+ const failed = error instanceof Error ? error : new Error(String(error));
267
+ dead = failed;
268
+ settleTurn(failed);
269
+ linked.abort();
270
+ }
271
+ });
197
272
  const startTurn = imageTurnStarter({
198
273
  driverLabel: "The Claude CLI driver",
199
274
  busyMessage: "A turn is already running in this Claude session",
200
275
  imageOptions: config,
201
276
  signal: linked.signal,
202
277
  check() {
203
- if (dead !== undefined)
204
- throw dead;
278
+ ensureAlive();
205
279
  if (current !== undefined)
206
280
  throw new Error("A turn is already running in this Claude session");
207
281
  },
@@ -215,26 +289,35 @@ async function openClaudeSession(config, input) {
215
289
  const turn = {
216
290
  state: newClaudeTurnState(turnInput.turnId),
217
291
  channel: createTurnDeltaChannel(),
218
- pending: new Map
292
+ pending: new Map,
293
+ settled: false
219
294
  };
220
295
  current = turn;
221
296
  try {
222
297
  engine.send(serialized);
223
298
  } catch (error) {
299
+ turn.settled = true;
224
300
  if (current === turn)
225
301
  current = undefined;
226
302
  throw error;
227
303
  }
228
- return claudeTurnHandle(turn, (line) => engine.send(line));
304
+ return claudeTurnHandle(turn, (line) => engine.send(line), ensureAlive);
229
305
  }
230
306
  });
307
+ const close = createClaudeSessionClose({
308
+ engine,
309
+ reading,
310
+ markClosing: () => {
311
+ closing = true;
312
+ },
313
+ settleTurn,
314
+ abortLinked: () => linked.abort(),
315
+ releaseLinked: () => linked.release()
316
+ });
231
317
  return {
232
318
  nativeThreadId: sessionId,
233
319
  startTurn,
234
- async close() {
235
- linked.abort();
236
- await stopConnection({ engine, reading });
237
- }
320
+ close
238
321
  };
239
322
  }
240
323
 
@@ -119,6 +119,7 @@ import {
119
119
  import {
120
120
  abortReason,
121
121
  raceAbort,
122
+ linkAbortSignals,
122
123
  withLinkedAbortSignals
123
124
  } from "../../index-rptj3qnc.js";
124
125
 
@@ -2380,6 +2381,13 @@ function createCodexStartTurn(input) {
2380
2381
  nativeTurnId,
2381
2382
  deltas: turn.channel,
2382
2383
  async interrupt() {
2384
+ if (input.getTurn() !== turn)
2385
+ return;
2386
+ try {
2387
+ input.ensureAlive();
2388
+ } catch {
2389
+ return;
2390
+ }
2383
2391
  interruptLocalCodexTurn(turn, nativeTurnId);
2384
2392
  await input.peer.request("turn/interrupt", {
2385
2393
  threadId: input.threadId,
@@ -2933,7 +2941,7 @@ function handleCodexRequest(state, method, params, id) {
2933
2941
  resolve
2934
2942
  });
2935
2943
  });
2936
- if (state.serverRequestDelivery === "all" && state.onServerRequest !== undefined && turn.pending.has(interactionId)) {
2944
+ if (state.serverRequestDelivery === "all" && state.onServerRequest !== undefined && turn.pending.has(interactionId) && isHandlersAlive(state)) {
2937
2945
  const observer = state.onServerRequest;
2938
2946
  invokeSafely(() => observer({
2939
2947
  interactionId,
@@ -2984,6 +2992,8 @@ function handleCodexRequest(state, method, params, id) {
2984
2992
  if (state.externalPending.get(interactionId) !== entry)
2985
2993
  return;
2986
2994
  state.externalPending.delete(interactionId);
2995
+ if (!isHandlersAlive(state))
2996
+ return;
2987
2997
  if (error instanceof JsonRpcError2)
2988
2998
  reject(error);
2989
2999
  };
@@ -3006,8 +3016,23 @@ function handleCodexRequest(state, method, params, id) {
3006
3016
  });
3007
3017
  }
3008
3018
  });
3009
- if (typeof outcome === "object" && outcome !== null && "then" in outcome && typeof outcome.then === "function") {
3010
- Promise.resolve(outcome).catch(onObserverError);
3019
+ const thenable = typeof outcome === "object" && outcome !== null && "then" in outcome && typeof outcome.then === "function";
3020
+ if (!isHandlersAlive(state)) {
3021
+ if (state.externalPending.get(interactionId) === entry) {
3022
+ state.externalPending.delete(interactionId);
3023
+ }
3024
+ if (thenable)
3025
+ Promise.resolve(outcome).catch(() => {
3026
+ return;
3027
+ });
3028
+ return;
3029
+ }
3030
+ if (thenable) {
3031
+ Promise.resolve(outcome).then(() => {
3032
+ if (!isHandlersAlive(state) && state.externalPending.get(interactionId) === entry) {
3033
+ state.externalPending.delete(interactionId);
3034
+ }
3035
+ }, onObserverError);
3011
3036
  }
3012
3037
  } catch (error) {
3013
3038
  onObserverError(error);
@@ -3151,6 +3176,8 @@ async function openCodexSession(config, input) {
3151
3176
  throw new Error("The Codex session is closed");
3152
3177
  if (dead !== undefined)
3153
3178
  throw dead;
3179
+ if (input.signal?.aborted)
3180
+ throw abortReason(input.signal);
3154
3181
  if (linked.signal.aborted)
3155
3182
  throw abortReason(linked.signal);
3156
3183
  };
@@ -3173,13 +3200,15 @@ async function openCodexSession(config, input) {
3173
3200
  serverRequestDelivery: config.serverRequestDelivery
3174
3201
  }
3175
3202
  }), (error) => {
3176
- const shouldNotify = opened && !closing && input.signal?.aborted !== true;
3177
- dead = error;
3203
+ const abortedByCaller = input.signal?.aborted === true;
3204
+ const shouldNotify = opened && !closing && !abortedByCaller;
3205
+ if (!closing && !abortedByCaller)
3206
+ dead = error;
3178
3207
  const turn = current;
3179
3208
  current = undefined;
3180
3209
  discardStartingTurn(turn);
3181
3210
  externalPending.clear();
3182
- loseEngineTurn(turn, codexStopped, error);
3211
+ loseEngineTurn(turn, codexStopped, abortedByCaller && input.signal !== undefined ? abortReason(input.signal) : error);
3183
3212
  linked.abort();
3184
3213
  if (shouldNotify)
3185
3214
  invokeSafely(() => config.onDisconnect?.(error));
@@ -3299,6 +3328,8 @@ async function openCodexControlConnection(config, input) {
3299
3328
  throw new Error("The Codex control connection is closed");
3300
3329
  if (dead !== undefined)
3301
3330
  throw dead;
3331
+ if (input.signal?.aborted)
3332
+ throw abortReason(input.signal);
3302
3333
  if (linked.signal.aborted)
3303
3334
  throw abortReason(linked.signal);
3304
3335
  };
@@ -3323,8 +3354,10 @@ async function openCodexControlConnection(config, input) {
3323
3354
  serverRequestDelivery: config.serverRequestDelivery
3324
3355
  }
3325
3356
  }), (error) => {
3326
- const shouldNotify = opened && !closing && input.signal?.aborted !== true;
3327
- dead = error;
3357
+ const abortedByCaller = input.signal?.aborted === true;
3358
+ const shouldNotify = opened && !closing && !abortedByCaller;
3359
+ if (!closing && !abortedByCaller)
3360
+ dead = error;
3328
3361
  externalPending.clear();
3329
3362
  linked.abort();
3330
3363
  if (shouldNotify)
@@ -3420,6 +3453,10 @@ function buildSharedThreadSession(params) {
3420
3453
  if (slot.closed)
3421
3454
  return;
3422
3455
  slot.closed = true;
3456
+ if (slot.dead !== undefined) {
3457
+ slot.releaseThreadSignal();
3458
+ return;
3459
+ }
3423
3460
  const turn = slot.current;
3424
3461
  slot.current = undefined;
3425
3462
  discardStartingTurn(turn);
@@ -3434,16 +3471,15 @@ function buildSharedThreadSession(params) {
3434
3471
  }
3435
3472
  };
3436
3473
  threadAbort.signal.addEventListener("abort", () => {
3437
- if (slot.closed)
3474
+ if (slot.closed || slot.dead !== undefined)
3438
3475
  return;
3439
- slot.closed = true;
3440
3476
  const reason = abortReason(threadAbort.signal);
3441
- slot.dead = slot.dead ?? (reason instanceof Error ? reason : new Error(String(reason)));
3477
+ slot.dead = { reason };
3442
3478
  const turn = slot.current;
3443
3479
  slot.current = undefined;
3444
3480
  discardStartingTurn(turn);
3445
3481
  slot.externalPending.clear();
3446
- loseEngineTurn(turn, codexStopped, slot.dead);
3482
+ loseEngineTurn(turn, codexStopped, reason);
3447
3483
  state.threads.delete(threadId);
3448
3484
  slot.releaseThreadSignal();
3449
3485
  bestEffortUnsubscribe(state, threadId);
@@ -3478,16 +3514,23 @@ async function openSharedCodexThread(state, threadInput) {
3478
3514
  if (state.threads.size + state.openingThreads >= state.maxThreads) {
3479
3515
  throw new RangeError(`[stitchkit] CodexSharedServer.openThread: active thread limit (${state.maxThreads}) reached`);
3480
3516
  }
3481
- const threadAbort = sessionAbort(threadInput.signal);
3482
- const onServerAbort = () => threadAbort.abort();
3483
- state.serverSignal.addEventListener("abort", onServerAbort, { once: true });
3517
+ if (threadInput.signal?.aborted)
3518
+ throw abortReason(threadInput.signal);
3519
+ if (state.serverSignal.aborted)
3520
+ throw abortReason(state.serverSignal);
3521
+ const ownThreadAbort = new AbortController;
3522
+ const threadLink = linkAbortSignals(threadInput.signal, state.serverSignal, ownThreadAbort.signal);
3484
3523
  let signalReleased = false;
3485
3524
  const releaseThreadSignal = () => {
3486
3525
  if (signalReleased)
3487
3526
  return;
3488
3527
  signalReleased = true;
3489
- state.serverSignal.removeEventListener("abort", onServerAbort);
3490
- threadAbort.release();
3528
+ threadLink.release();
3529
+ };
3530
+ const threadAbort = {
3531
+ signal: threadLink.signal,
3532
+ abort: (reason) => ownThreadAbort.abort(reason),
3533
+ release: releaseThreadSignal
3491
3534
  };
3492
3535
  state.openingThreads += 1;
3493
3536
  if (threadInput.nativeThreadId !== undefined) {
@@ -3499,6 +3542,8 @@ async function openSharedCodexThread(state, threadInput) {
3499
3542
  imageOptions: state.config,
3500
3543
  signal: threadAbort.signal,
3501
3544
  ensureAlive: () => {
3545
+ if (threadInput.signal?.aborted)
3546
+ throw abortReason(threadInput.signal);
3502
3547
  threadAbort.signal.throwIfAborted();
3503
3548
  state.ensureServerAlive();
3504
3549
  },
@@ -3540,8 +3585,10 @@ async function openSharedCodexThread(state, threadInput) {
3540
3585
  if (slot.closed)
3541
3586
  throw new Error("The Codex session is closed");
3542
3587
  if (slot.dead !== undefined)
3543
- throw slot.dead;
3588
+ throw slot.dead.reason;
3544
3589
  state.ensureServerAlive();
3590
+ if (threadInput.signal?.aborted)
3591
+ throw abortReason(threadInput.signal);
3545
3592
  if (threadAbort.signal.aborted)
3546
3593
  throw abortReason(threadAbort.signal);
3547
3594
  };
@@ -3552,7 +3599,7 @@ async function openSharedCodexThread(state, threadInput) {
3552
3599
  ensureAlive,
3553
3600
  closed: false,
3554
3601
  dead: undefined,
3555
- abortThread: () => threadAbort.abort(),
3602
+ abortThread: (reason) => threadAbort.abort(reason),
3556
3603
  releaseThreadSignal,
3557
3604
  handlers: createCodexHandlers({
3558
3605
  profileId: state.profile.profileId,
@@ -3588,7 +3635,7 @@ async function openSharedCodexThread(state, threadInput) {
3588
3635
  state.ensureServerAlive();
3589
3636
  threadAbort.signal.throwIfAborted();
3590
3637
  if (slot.dead !== undefined)
3591
- throw slot.dead;
3638
+ throw slot.dead.reason;
3592
3639
  const activeModelProvider = threadContext.modelProvider ?? dynamicOverrides?.modelProvider ?? threadInput.forkFrom?.modelProvider ?? state.config.modelProvider;
3593
3640
  return buildSharedThreadSession({
3594
3641
  state,
@@ -3646,6 +3693,8 @@ async function openCodexSharedServer(config, baseCapabilities, input) {
3646
3693
  throw new Error("The Codex shared server is closed");
3647
3694
  if (serverDead !== undefined)
3648
3695
  throw serverDead;
3696
+ if (input.signal?.aborted)
3697
+ throw abortReason(input.signal);
3649
3698
  if (linked.signal.aborted)
3650
3699
  throw abortReason(linked.signal);
3651
3700
  };
@@ -3666,16 +3715,15 @@ async function openCodexSharedServer(config, baseCapabilities, input) {
3666
3715
  serverRequestDelivery: config.serverRequestDelivery
3667
3716
  }
3668
3717
  });
3669
- const failAllThreads = (error) => {
3718
+ const failAllThreads = (reason) => {
3670
3719
  for (const slot of [...threads.values()]) {
3671
- slot.closed = true;
3672
- slot.dead = error;
3720
+ slot.dead = slot.dead ?? { reason };
3673
3721
  const turn = slot.current;
3674
3722
  slot.current = undefined;
3675
3723
  discardStartingTurn(turn);
3676
3724
  slot.externalPending.clear();
3677
- loseEngineTurn(turn, codexStopped, error);
3678
- slot.abortThread();
3725
+ loseEngineTurn(turn, codexStopped, reason);
3726
+ slot.abortThread(reason);
3679
3727
  slot.releaseThreadSignal();
3680
3728
  }
3681
3729
  threads.clear();
@@ -3704,10 +3752,12 @@ async function openCodexSharedServer(config, baseCapabilities, input) {
3704
3752
  return globalHandlers.onRequest(method, params, id);
3705
3753
  }
3706
3754
  }, (error) => {
3707
- const shouldNotify = opened && !serverClosing && input.signal?.aborted !== true;
3708
- serverDead = error;
3755
+ const abortedByCaller = input.signal?.aborted === true;
3756
+ const shouldNotify = opened && !serverClosing && !abortedByCaller;
3757
+ if (!serverClosing && !abortedByCaller)
3758
+ serverDead = error;
3709
3759
  globalExternalPending.clear();
3710
- failAllThreads(error);
3760
+ failAllThreads(abortedByCaller && input.signal !== undefined ? abortReason(input.signal) : error);
3711
3761
  linked.abort();
3712
3762
  if (shouldNotify)
3713
3763
  invokeSafely(() => config.onDisconnect?.(error));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stitchkit",
3
- "version": "0.132.1",
3
+ "version": "0.132.3",
4
4
  "description": "Contract-first backend framework — one defineContract() into an HTTP API, MCP tools, AI-agent tools and a typed client. Bun and Node.",
5
5
  "keywords": [
6
6
  "bun",