zcode-app-cli 3.6.5-8 → 3.6.5-9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zcode-app-cli",
3
- "version": "3.6.5-8",
3
+ "version": "3.6.5-9",
4
4
  "description": "Unofficial terminal client for the ZCode agent runtime",
5
5
  "keywords": [
6
6
  "agent",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "appVersion": "3.6.5",
3
3
  "cliVersion": "0.16.1",
4
- "extractedAt": "2026-08-03T10:44:25.622Z",
4
+ "extractedAt": "2026-08-05T08:29:22.231Z",
5
5
  "sha512": "B4zIhJPTC/F/M4QzPSCuM7WPNY1KuoTS3U1phtQgEXuS8uyNhOBjg5b7H/1Q0i/5g74Xh24nyXEwjdvK94tgFQ==",
6
6
  "source": "https://cdn-zcode.z.ai/zcode/electron/releases/3.6.5/linux-x64/ZCode-3.6.5-linux-x64.deb",
7
7
  "tui": {
@@ -157317,6 +157317,7 @@ function normalizeEvent(value) {
157317
157317
  const field = body[key];
157318
157318
  return Array.isArray(field) ? field.filter((item) => typeof item === "string") : void 0;
157319
157319
  };
157320
+ const envelopeString = (key) => asString(body[key]) ?? asString(value[key]) ?? (params && asString(params[key]));
157320
157321
  return {
157321
157322
  type,
157322
157323
  kind: asString(body.kind) ?? runtimeToolKind(type),
@@ -157329,6 +157330,8 @@ function normalizeEvent(value) {
157329
157330
  ].includes(asString(body.field) ?? "") ? asString(body.field) : void 0,
157330
157331
  messageId: asString(body.messageId) ?? asString(body.messageID) ?? asString(body.assistantMessageId) ?? part?.messageId,
157331
157332
  partId: asString(body.partId) ?? asString(body.partID) ?? part?.partId,
157333
+ turnId: envelopeString("turnId") ?? envelopeString("turnID"),
157334
+ targetTurnId: envelopeString("targetTurnId") ?? envelopeString("targetTurnID"),
157332
157335
  inputId: asString(body.inputId) ?? asString(body.inputID),
157333
157336
  pendingInputId: asString(body.pendingInputId) ?? asString(body.pendingInputID),
157334
157337
  pendingInputIds: strings("pendingInputIds") ?? strings("pendingInputIDs"),
@@ -157340,6 +157343,9 @@ function normalizeEvent(value) {
157340
157343
  input: body.input ?? toolCall?.input ?? (part?.type === "tool" ? part.input : void 0),
157341
157344
  result: body.result ?? body.output ?? (part?.type === "tool" ? part.output : void 0),
157342
157345
  error: error ?? (part?.type === "tool" ? part.error : void 0),
157346
+ errorCode: asString(body.errorCode),
157347
+ errorPhase: asString(body.errorPhase),
157348
+ exceptionType: asString(body.exceptionType),
157343
157349
  message: asString(body.message) ?? (errorRecord && asString(errorRecord.message)),
157344
157350
  progress: {
157345
157351
  elapsedMs: number("elapsedMs"),
@@ -157379,6 +157385,9 @@ function normalizeEvent(value) {
157379
157385
  raw: value
157380
157386
  };
157381
157387
  }
157388
+ function isModelCancellationEvent(event) {
157389
+ return event.type === "model_request_failed" && (event.reason?.toLowerCase() === "cancelled" || event.errorCode?.toLowerCase() === "model_request_cancelled" || event.exceptionType?.toLowerCase() === "aborterror");
157390
+ }
157382
157391
  function responseText(value) {
157383
157392
  if (!isRecord(value)) return void 0;
157384
157393
  return asString(value.response) ?? asString(value.message) ?? asString(value.text);
@@ -160942,6 +160951,8 @@ var InputQueue = class {
160942
160951
  callbacks;
160943
160952
  pendingSteers = [];
160944
160953
  queuedFollowUps = [];
160954
+ pendingResolutions = /* @__PURE__ */ new Map();
160955
+ completedTurnIds = /* @__PURE__ */ new Set();
160945
160956
  autoSendEnabled = true;
160946
160957
  constructor(callbacks) {
160947
160958
  this.callbacks = callbacks;
@@ -160966,6 +160977,9 @@ var InputQueue = class {
160966
160977
  hasPendingSteers() {
160967
160978
  return this.pendingSteers.length > 0;
160968
160979
  }
160980
+ admittedPendingInputIds() {
160981
+ return this.pendingSteers.flatMap(({ admitted, pendingInputId }) => admitted && pendingInputId ? [pendingInputId] : []);
160982
+ }
160969
160983
  get autoSend() {
160970
160984
  return this.autoSendEnabled;
160971
160985
  }
@@ -160975,10 +160989,13 @@ var InputQueue = class {
160975
160989
  resetAutoSend() {
160976
160990
  this.autoSendEnabled = true;
160977
160991
  }
160978
- trackSteer(submission, inputId) {
160992
+ trackSteer(submission, inputId, targetTurnId, pendingInputId) {
160979
160993
  const pending = {
160994
+ admitted: false,
160980
160995
  inputId,
160981
- submission
160996
+ pendingInputId,
160997
+ submission,
160998
+ targetTurnId
160982
160999
  };
160983
161000
  this.pendingSteers.push(pending);
160984
161001
  this.syncView();
@@ -160987,10 +161004,18 @@ var InputQueue = class {
160987
161004
  findSteer(inputId) {
160988
161005
  return inputId ? this.pendingSteers.find((pending) => pending.inputId === inputId) : void 0;
160989
161006
  }
160990
- associateSteer(inputId, pendingInputId) {
161007
+ associateSteer(inputId, pendingInputId, targetTurnId) {
160991
161008
  const pending = this.pendingSteers.find((candidate) => candidate.inputId === inputId || candidate.pendingInputId === pendingInputId);
160992
- if (!pending || pending.pendingInputId === pendingInputId) return;
161009
+ if (!pending || !this.matchesTurn(pending.targetTurnId, targetTurnId)) return;
161010
+ pending.admitted = true;
160993
161011
  pending.pendingInputId = pendingInputId;
161012
+ pending.targetTurnId ??= targetTurnId;
161013
+ const resolution = this.pendingResolutions.get(pendingInputId);
161014
+ if (resolution && this.matchesTurn(pending.targetTurnId, resolution.targetTurnId)) {
161015
+ this.pendingResolutions.delete(pendingInputId);
161016
+ this.settleSteer(pending, resolution);
161017
+ return;
161018
+ }
160994
161019
  this.syncView();
160995
161020
  }
160996
161021
  removeSteer(inputId) {
@@ -160998,45 +161023,90 @@ var InputQueue = class {
160998
161023
  const index = this.pendingSteers.findIndex((pending) => pending.inputId === inputId);
160999
161024
  if (index < 0) return void 0;
161000
161025
  const [pending] = this.pendingSteers.splice(index, 1);
161026
+ if (pending?.pendingInputId) this.pendingResolutions.delete(pending.pendingInputId);
161027
+ else if (!this.pendingSteers.some((candidate) => !candidate.pendingInputId)) this.pendingResolutions.clear();
161001
161028
  this.syncView();
161002
161029
  return pending;
161003
161030
  }
161004
- takeSteer(pendingInputId) {
161005
- const index = this.pendingSteers.findIndex((pending) => pending.pendingInputId === pendingInputId);
161031
+ requeuePendingSteers(reason = "turn_ended", targetTurnId, mergeForImmediateSubmission = false, pendingInputReservationId) {
161032
+ if (targetTurnId) this.rememberCompletedTurn(targetTurnId);
161033
+ const discarded = this.pendingSteers.splice(0);
161034
+ for (const pending of discarded) {
161035
+ if (pending.targetTurnId) this.rememberCompletedTurn(pending.targetTurnId);
161036
+ if (pending.pendingInputId) this.pendingResolutions.delete(pending.pendingInputId);
161037
+ }
161038
+ this.pendingResolutions.clear();
161039
+ if (discarded.length === 0) return 0;
161040
+ if (mergeForImmediateSubmission) {
161041
+ const [first] = discarded;
161042
+ const pendingInputIds = discarded.flatMap(({ admitted, pendingInputId }) => admitted && pendingInputId ? [pendingInputId] : []);
161043
+ this.queuedFollowUps.unshift({
161044
+ ...first.submission,
161045
+ displayInput: discarded.map(({ submission }) => submission.displayInput).join("\n"),
161046
+ input: discarded.map(({ submission }) => submission.input).join("\n"),
161047
+ ...pendingInputIds.length > 0 && pendingInputReservationId ? { pendingInputReservationId } : {},
161048
+ ...pendingInputIds.length > 0 ? { pendingInputIds } : {},
161049
+ recordHistory: false,
161050
+ secrets: discarded.flatMap(({ submission }) => submission.secrets)
161051
+ });
161052
+ } else this.queuedFollowUps.push(...discarded.map(({ admitted, pendingInputId, submission }) => ({
161053
+ ...submission,
161054
+ ...admitted && pendingInputId ? { pendingInputIds: [pendingInputId] } : {},
161055
+ recordHistory: false
161056
+ })));
161057
+ this.syncView();
161058
+ if (!mergeForImmediateSubmission) this.callbacks.onSteerDiscarded(discarded.length, reason);
161059
+ return discarded.length;
161060
+ }
161061
+ takeSteer(pendingInputId, targetTurnId) {
161062
+ const index = this.pendingSteers.findIndex((pending) => pending.pendingInputId === pendingInputId && this.matchesTurn(pending.targetTurnId, targetTurnId));
161006
161063
  if (index < 0) return void 0;
161007
161064
  const [pending] = this.pendingSteers.splice(index, 1);
161008
161065
  return pending;
161009
161066
  }
161010
161067
  handleLifecycleEvent(event) {
161011
- if (event.type === "turn_steer_queued" || event.type === "turn.steerQueued") {
161012
- if (event.inputId && event.pendingInputId) this.associateSteer(event.inputId, event.pendingInputId);
161013
- return true;
161014
- }
161015
- if (event.type === "turn_steer_drained" || event.type === "turn.steerDrained") {
161016
- this.commitSteers(event.pendingInputIds ?? [], event.injectedMessageIds ?? []);
161068
+ const queued = event.type === "turn_steer_queued" || event.type === "turn.steerQueued";
161069
+ const drained = event.type === "turn_steer_drained" || event.type === "turn.steerDrained";
161070
+ const discarded = event.type === "turn_steer_discarded" || event.type === "turn.steerDiscarded";
161071
+ if (!queued && !drained && !discarded) return false;
161072
+ if (event.targetTurnId && this.completedTurnIds.has(event.targetTurnId)) return true;
161073
+ if (queued) {
161074
+ if (event.inputId && event.pendingInputId) this.associateSteer(event.inputId, event.pendingInputId, event.targetTurnId);
161017
161075
  return true;
161018
161076
  }
161019
- if (event.type === "turn_steer_discarded" || event.type === "turn.steerDiscarded") {
161020
- this.discardSteers(event.pendingInputIds ?? [], event.reason);
161077
+ if (drained) {
161078
+ this.commitSteers(event.pendingInputIds ?? [], event.injectedMessageIds ?? [], event.targetTurnId);
161021
161079
  return true;
161022
161080
  }
161023
- return false;
161081
+ this.discardSteers(event.pendingInputIds ?? [], event.reason, event.targetTurnId);
161082
+ return true;
161024
161083
  }
161025
- commitSteers(pendingInputIds, messageIds) {
161084
+ commitSteers(pendingInputIds, messageIds, targetTurnId) {
161026
161085
  const committed = pendingInputIds.flatMap((pendingInputId, index) => {
161027
- const pending = this.takeSteer(pendingInputId);
161028
- return pending ? [{
161086
+ const pending = this.takeSteer(pendingInputId, targetTurnId);
161087
+ if (pending) return [{
161029
161088
  messageId: messageIds[index],
161030
161089
  displayInput: pending.submission.displayInput
161031
- }] : [];
161090
+ }];
161091
+ this.rememberResolution(pendingInputId, {
161092
+ kind: "committed",
161093
+ messageId: messageIds[index],
161094
+ targetTurnId
161095
+ });
161096
+ return [];
161032
161097
  });
161033
161098
  if (committed.length === 0) return;
161034
161099
  this.callbacks.onSteerCommitted(committed);
161035
161100
  this.syncView();
161036
161101
  }
161037
- discardSteers(pendingInputIds, reason) {
161102
+ discardSteers(pendingInputIds, reason, targetTurnId) {
161038
161103
  const discarded = pendingInputIds.flatMap((pendingInputId) => {
161039
- const pending = this.takeSteer(pendingInputId);
161104
+ const pending = this.takeSteer(pendingInputId, targetTurnId);
161105
+ if (!pending) this.rememberResolution(pendingInputId, {
161106
+ kind: "discarded",
161107
+ reason,
161108
+ targetTurnId
161109
+ });
161040
161110
  return pending ? [pending] : [];
161041
161111
  });
161042
161112
  if (discarded.length === 0) return;
@@ -161047,6 +161117,37 @@ var InputQueue = class {
161047
161117
  this.syncView();
161048
161118
  this.callbacks.onSteerDiscarded(discarded.length, reason);
161049
161119
  }
161120
+ settleSteer(pending, resolution) {
161121
+ const index = this.pendingSteers.indexOf(pending);
161122
+ if (index < 0) return;
161123
+ this.pendingSteers.splice(index, 1);
161124
+ if (resolution.kind === "committed") this.callbacks.onSteerCommitted([{
161125
+ messageId: resolution.messageId,
161126
+ displayInput: pending.submission.displayInput
161127
+ }]);
161128
+ else {
161129
+ this.queuedFollowUps.push({
161130
+ ...pending.submission,
161131
+ recordHistory: false
161132
+ });
161133
+ this.callbacks.onSteerDiscarded(1, resolution.reason);
161134
+ }
161135
+ this.syncView();
161136
+ }
161137
+ rememberResolution(pendingInputId, resolution) {
161138
+ if (!this.pendingSteers.some((pending) => !pending.pendingInputId && this.matchesTurn(pending.targetTurnId, resolution.targetTurnId))) return;
161139
+ this.pendingResolutions.set(pendingInputId, resolution);
161140
+ }
161141
+ matchesTurn(left, right) {
161142
+ return left === void 0 || right === void 0 || left === right;
161143
+ }
161144
+ rememberCompletedTurn(turnId) {
161145
+ this.completedTurnIds.add(turnId);
161146
+ if (this.completedTurnIds.size > 32) {
161147
+ const oldest = this.completedTurnIds.values().next().value;
161148
+ if (oldest) this.completedTurnIds.delete(oldest);
161149
+ }
161150
+ }
161050
161151
  syncView() {
161051
161152
  this.callbacks.onStateChanged({
161052
161153
  pendingSteers: this.pendingSteers.map(({ submission }) => submission.displayInput),
@@ -163505,6 +163606,7 @@ function modelRetryProgress(event, phase) {
163505
163606
  const doubleEscapeTimeoutMs = 800;
163506
163607
  const customProviderHelpCommand = "__zcode_custom_provider_help__";
163507
163608
  const rewindEscapeHint = "Esc again to rewind conversation";
163609
+ const recentSteerCommitGuardMs = 400;
163508
163610
  function shouldUseNoBrowserForLogin(env = process.env, platform = process.platform) {
163509
163611
  if (env.SSH_CONNECTION?.trim() || env.SSH_TTY?.trim()) return true;
163510
163612
  if (platform !== "linux") return false;
@@ -163573,6 +163675,14 @@ var ZCodeTui = class {
163573
163675
  queuedSelectionCommand;
163574
163676
  inputQueue;
163575
163677
  turnAbortController;
163678
+ steerAbortControllers = /* @__PURE__ */ new Set();
163679
+ primaryTurnActive = false;
163680
+ primaryTurnInputId;
163681
+ activeTurnId;
163682
+ turnEpoch = 0;
163683
+ activeTurnEpoch;
163684
+ pendingSteerInterrupt;
163685
+ recentSteerCommit;
163576
163686
  currentThinking;
163577
163687
  currentThinkingPartId;
163578
163688
  presentationRegistry = new TurnPresentationRegistry();
@@ -163658,6 +163768,10 @@ var ZCodeTui = class {
163658
163768
  this.completeThinking();
163659
163769
  this.assistantStream.breakSegment();
163660
163770
  for (const { messageId, displayInput } of entries) this.addUserMessage(displayInput, 0, messageId);
163771
+ if (!this.inputQueue.hasPendingSteers() && this.activeTurnEpoch !== void 0) this.recentSteerCommit = {
163772
+ at: Date.now(),
163773
+ turnEpoch: this.activeTurnEpoch
163774
+ };
163661
163775
  },
163662
163776
  onSteerDiscarded: (count, reason) => {
163663
163777
  const detail = reason ? ` (${reason.replaceAll("_", " ")})` : "";
@@ -163919,7 +164033,10 @@ var ZCodeTui = class {
163919
164033
  if (this.attachmentBar.isActive()) return void 0;
163920
164034
  if (this.choiceDepth > 0) return void 0;
163921
164035
  if (this.rewindFlowActive) return { consume: true };
163922
- if (!matchesKey(data, "escape")) this.clearRewindEscape();
164036
+ if (!matchesKey(data, "escape")) {
164037
+ this.clearRewindEscape();
164038
+ this.recentSteerCommit = void 0;
164039
+ }
163923
164040
  if (matchesKey(data, "up") && this.canEnterAttachmentSelection()) {
163924
164041
  this.enterAttachmentSelection();
163925
164042
  return { consume: true };
@@ -163992,6 +164109,7 @@ var ZCodeTui = class {
163992
164109
  }
163993
164110
  if (matchesKey(data, "ctrl+c")) {
163994
164111
  if (this.turnAbortController) {
164112
+ this.pendingSteerInterrupt = void 0;
163995
164113
  this.turnAbortController.abort();
163996
164114
  this.updateActivity("cancelling…");
163997
164115
  } else if (this.editor.getText()) this.editor.setText("");
@@ -164005,8 +164123,17 @@ var ZCodeTui = class {
164005
164123
  if (matchesKey(data, "escape")) {
164006
164124
  if (this.turnAbortController) {
164007
164125
  this.clearRewindEscape();
164008
- this.turnAbortController.abort();
164009
- this.updateActivity("cancelling…");
164126
+ const pendingSteer = this.inputQueue.hasPendingSteers();
164127
+ if (!pendingSteer && this.consumeRecentSteerCommitGuard()) {
164128
+ this.addNotice("Message received; Esc was ignored. Press Esc again to interrupt.", "muted");
164129
+ return { consume: true };
164130
+ }
164131
+ if (pendingSteer) this.requestPendingSteerInterrupt();
164132
+ else {
164133
+ this.pendingSteerInterrupt = void 0;
164134
+ this.turnAbortController.abort();
164135
+ this.updateActivity("cancelling…");
164136
+ }
164010
164137
  return { consume: true };
164011
164138
  }
164012
164139
  if (this.transcript.searchStatus() || this.transcript.cursorStatus()) {
@@ -164110,7 +164237,22 @@ var ZCodeTui = class {
164110
164237
  }
164111
164238
  this.transcript.clearSearch();
164112
164239
  this.transcript.clearCursor();
164113
- const steering = this.activeSubmissions > 0;
164240
+ const steering = this.primaryTurnActive;
164241
+ if (!steering && this.activeSubmissions > 0) {
164242
+ this.inputQueue.queueFollowUp({
164243
+ ...submission,
164244
+ recordHistory: false
164245
+ });
164246
+ return;
164247
+ }
164248
+ const turnEpoch = steering ? this.activeTurnEpoch : ++this.turnEpoch;
164249
+ if (turnEpoch === void 0) {
164250
+ this.inputQueue.queueFollowUp({
164251
+ ...submission,
164252
+ recordHistory: false
164253
+ });
164254
+ return;
164255
+ }
164114
164256
  const attachments = !steering && !input.startsWith("/") ? [...this.pendingAttachments] : [];
164115
164257
  if (steering && !this.options.sendInput) {
164116
164258
  this.inputQueue.queueFollowUp({
@@ -164122,15 +164264,20 @@ var ZCodeTui = class {
164122
164264
  if (attachments.length > 0) this.clearPendingAttachments(false);
164123
164265
  const abortController = new AbortController();
164124
164266
  const inputId = `input_${crypto.randomUUID()}`;
164267
+ const pendingInputId = steering ? `pending_${crypto.randomUUID()}` : void 0;
164125
164268
  const callOptions = {
164126
164269
  abortSignal: abortController.signal,
164127
164270
  delivery: steering ? "steer_active_turn" : "start_turn",
164271
+ expectedTurnId: steering ? this.activeTurnId : void 0,
164128
164272
  inputId,
164273
+ pendingInputReservationId: queuedSubmission?.pendingInputReservationId,
164274
+ pendingInputId,
164129
164275
  queryId: `query_${crypto.randomUUID()}`,
164130
- onEvent: (event) => this.onEvent(event),
164276
+ onEvent: (event) => this.onEvent(event, turnEpoch),
164131
164277
  requestPermission: (request, context) => this.requestPermission(request, context)
164132
164278
  };
164133
- const pendingSteer = steering ? this.inputQueue.trackSteer(submission, inputId) : void 0;
164279
+ const pendingSteer = steering ? this.inputQueue.trackSteer(submission, inputId, callOptions.expectedTurnId, pendingInputId) : void 0;
164280
+ if (steering) this.recentSteerCommit = void 0;
164134
164281
  if (!steering) this.addUserMessage(submission.displayInput, attachments.length);
164135
164282
  if (submission.pending) this.addNotice([
164136
164283
  submission.pending.primary,
@@ -164138,8 +164285,16 @@ var ZCodeTui = class {
164138
164285
  submission.pending.help
164139
164286
  ].filter(Boolean).join("\n"), "muted");
164140
164287
  if (!steering) this.beginTurn(submission.displayInput);
164141
- if (!steering) this.turnAbortController = abortController;
164142
- this.activeSubmissions += 1;
164288
+ if (!steering) {
164289
+ this.turnAbortController = abortController;
164290
+ this.primaryTurnActive = true;
164291
+ this.primaryTurnInputId = inputId;
164292
+ this.activeTurnId = void 0;
164293
+ this.activeTurnEpoch = turnEpoch;
164294
+ this.pendingSteerInterrupt = void 0;
164295
+ this.recentSteerCommit = void 0;
164296
+ this.activeSubmissions += 1;
164297
+ } else this.steerAbortControllers.add(abortController);
164143
164298
  if (!steering || submission.status) this.updateActivity(submission.status ?? "working…");
164144
164299
  const notificationEligible = !steering && !input.startsWith("/");
164145
164300
  if (notificationEligible) this.pendingTurnNotification = "completed";
@@ -164152,9 +164307,13 @@ var ZCodeTui = class {
164152
164307
  await this.handleResult(result, true, settingTargetForCommand(input));
164153
164308
  accepted = true;
164154
164309
  } else {
164155
- const outcome = await this.options.sendInput(promptInput(input, attachments), callOptions);
164310
+ const preparedInput = promptInput(input, attachments);
164311
+ const outcome = queuedSubmission?.pendingInputIds?.length && this.options.promoteQueuedInput ? await this.options.promoteQueuedInput(preparedInput, queuedSubmission.pendingInputIds, callOptions) : await this.options.sendInput(preparedInput, callOptions);
164312
+ if (steering && this.activeTurnEpoch !== turnEpoch) return;
164156
164313
  const disposition = await this.handleSendOutcome(outcome);
164157
- if (pendingSteer && disposition.pendingInputId) this.inputQueue.associateSteer(pendingSteer.inputId, disposition.pendingInputId);
164314
+ if (steering && this.activeTurnEpoch !== turnEpoch) return;
164315
+ if (steering && disposition.targetTurnId) this.activeTurnId = disposition.targetTurnId;
164316
+ if (pendingSteer && disposition.pendingInputId) this.inputQueue.associateSteer(pendingSteer.inputId, disposition.pendingInputId, disposition.targetTurnId);
164158
164317
  accepted = disposition.accepted;
164159
164318
  if (!accepted && steering) {
164160
164319
  const retained = this.inputQueue.removeSteer(pendingSteer?.inputId);
@@ -164171,11 +164330,15 @@ var ZCodeTui = class {
164171
164330
  }
164172
164331
  if (!accepted && notificationEligible) this.pendingTurnNotification = void 0;
164173
164332
  } catch (error) {
164174
- if (abortController.signal.aborted) {
164333
+ if (steering && this.activeTurnEpoch !== turnEpoch) return;
164334
+ const interruptedForSteer = !steering && this.isPendingSteerInterrupt(turnEpoch, abortController);
164335
+ if (abortController.signal.aborted || interruptedForSteer) {
164175
164336
  unfinishedToolState = "cancelled";
164176
- if (!steering) this.inputQueue.autoSend = false;
164177
- if (notificationEligible) this.pendingTurnNotification = void 0;
164178
- this.addNotice(submission.cancelStatus ?? "Turn cancelled.", "muted");
164337
+ if (!steering) {
164338
+ this.inputQueue.autoSend = false;
164339
+ if (notificationEligible) this.pendingTurnNotification = void 0;
164340
+ this.addNotice(interruptedForSteer ? "Model interrupted to submit steer instructions." : submission.cancelStatus ?? "Turn cancelled.", "muted");
164341
+ }
164179
164342
  } else {
164180
164343
  unfinishedToolState = "failed";
164181
164344
  if (!steering) this.inputQueue.autoSend = false;
@@ -164183,7 +164346,7 @@ var ZCodeTui = class {
164183
164346
  let steerRecoveryDetail;
164184
164347
  if (steering) {
164185
164348
  const retained = this.inputQueue.findSteer(pendingSteer?.inputId);
164186
- if (retained?.pendingInputId) steerRecoveryDetail = "Steer remains queued for the active turn.";
164349
+ if (retained?.admitted) steerRecoveryDetail = "Steer remains queued for the active turn.";
164187
164350
  else if (retained) {
164188
164351
  const removed = this.inputQueue.removeSteer(pendingSteer?.inputId);
164189
164352
  if (removed) this.inputQueue.queueFollowUp({
@@ -164200,38 +164363,100 @@ var ZCodeTui = class {
164200
164363
  this.addNotice(steering ? `${detail}\n${steerRecoveryDetail}` : detail, steering ? "warning" : "error");
164201
164364
  }
164202
164365
  } finally {
164203
- this.activeSubmissions = Math.max(0, this.activeSubmissions - 1);
164204
- if (this.turnAbortController === abortController) this.turnAbortController = void 0;
164205
- if (this.activeSubmissions === 0) {
164206
- nextCommand = this.queuedSelectionCommand;
164207
- this.queuedSelectionCommand = void 0;
164208
- if (!nextCommand && this.inputQueue.autoSend) {
164209
- nextCommand = this.inputQueue.takeNextFollowUp();
164210
- if (nextCommand) {
164211
- this.pendingTurnNotification = void 0;
164212
- this.pendingTurnNotificationDetail = "";
164213
- }
164214
- }
164215
- this.finishTurn(unfinishedToolState);
164216
- }
164366
+ if (steering) this.steerAbortControllers.delete(abortController);
164367
+ else nextCommand = this.finishPrimaryTurnSubmission(turnEpoch, abortController, unfinishedToolState);
164217
164368
  this.refreshGoal();
164218
164369
  this.refreshSessionUsage();
164219
164370
  }
164220
164371
  if (nextCommand) await this.submit(nextCommand.input, nextCommand);
164221
164372
  }
164373
+ finishPrimaryTurnSubmission(turnEpoch, abortController, unfinishedToolState) {
164374
+ this.activeSubmissions = Math.max(0, this.activeSubmissions - 1);
164375
+ if (this.activeTurnEpoch !== turnEpoch) return void 0;
164376
+ const turnFinishState = abortController.signal.aborted ? "cancelled" : unfinishedToolState;
164377
+ const recoveryReason = turnFinishState === "cancelled" ? "turn_cancelled" : turnFinishState === "failed" ? "turn_failed" : "turn_ended";
164378
+ const targetTurnId = this.activeTurnId;
164379
+ const pendingSteerInterrupt = this.isPendingSteerInterrupt(turnEpoch, abortController) ? this.pendingSteerInterrupt : void 0;
164380
+ const autoSendRecoveredSteers = turnFinishState === "cancelled" && pendingSteerInterrupt !== void 0;
164381
+ this.primaryTurnActive = false;
164382
+ this.primaryTurnInputId = void 0;
164383
+ this.activeTurnId = void 0;
164384
+ this.activeTurnEpoch = void 0;
164385
+ this.pendingSteerInterrupt = void 0;
164386
+ this.recentSteerCommit = void 0;
164387
+ if (this.turnAbortController === abortController) this.turnAbortController = void 0;
164388
+ for (const controller of this.steerAbortControllers) controller.abort();
164389
+ this.steerAbortControllers.clear();
164390
+ const recoveredSteerCount = this.inputQueue.requeuePendingSteers(recoveryReason, targetTurnId, autoSendRecoveredSteers, pendingSteerInterrupt?.reservationId);
164391
+ if (autoSendRecoveredSteers && recoveredSteerCount > 0) this.inputQueue.resetAutoSend();
164392
+ let nextCommand = this.queuedSelectionCommand;
164393
+ this.queuedSelectionCommand = void 0;
164394
+ if (!nextCommand && this.inputQueue.autoSend) {
164395
+ nextCommand = this.inputQueue.takeNextFollowUp();
164396
+ if (nextCommand) {
164397
+ this.pendingTurnNotification = void 0;
164398
+ this.pendingTurnNotificationDetail = "";
164399
+ }
164400
+ }
164401
+ this.finishTurn(turnFinishState);
164402
+ return nextCommand;
164403
+ }
164404
+ requestPendingSteerInterrupt() {
164405
+ const abortController = this.turnAbortController;
164406
+ const turnEpoch = this.activeTurnEpoch;
164407
+ if (!abortController || turnEpoch === void 0) return;
164408
+ if (this.isPendingSteerInterrupt(turnEpoch, abortController)) return;
164409
+ const request = {
164410
+ abortController,
164411
+ reservationId: `steer_interrupt_${crypto.randomUUID()}`,
164412
+ turnEpoch
164413
+ };
164414
+ this.pendingSteerInterrupt = request;
164415
+ this.updateActivity("submitting steer…");
164416
+ const interruptTurn = this.options.interruptTurn;
164417
+ if (!interruptTurn) {
164418
+ abortController.abort();
164419
+ return;
164420
+ }
164421
+ interruptTurn({
164422
+ pendingInputIds: this.inputQueue.admittedPendingInputIds(),
164423
+ reason: "TUI interrupted the active model step to submit steer instructions.",
164424
+ reservationId: request.reservationId
164425
+ }).then((outcome) => {
164426
+ if (!this.isPendingSteerInterrupt(turnEpoch, abortController)) return;
164427
+ if (!isRecord(outcome) || asString(outcome.kind) !== "stopped") abortController.abort();
164428
+ }).catch(() => {
164429
+ if (this.isPendingSteerInterrupt(turnEpoch, abortController)) abortController.abort();
164430
+ });
164431
+ }
164432
+ isPendingSteerInterrupt(turnEpoch, abortController) {
164433
+ const pending = this.pendingSteerInterrupt;
164434
+ return pending?.turnEpoch === turnEpoch && pending.abortController === abortController;
164435
+ }
164436
+ consumeRecentSteerCommitGuard() {
164437
+ const recent = this.recentSteerCommit;
164438
+ this.recentSteerCommit = void 0;
164439
+ return recent !== void 0 && recent.turnEpoch === this.activeTurnEpoch && Date.now() - recent.at <= recentSteerCommitGuardMs;
164440
+ }
164222
164441
  async handleSendOutcome(outcome) {
164223
164442
  if (!isRecord(outcome)) return { accepted: true };
164224
164443
  const kind = asString(outcome.kind);
164225
164444
  if (kind === "started_turn") {
164226
164445
  await this.handleResult(outcome.result);
164227
- return { accepted: true };
164446
+ const result = isRecord(outcome.result) ? outcome.result : void 0;
164447
+ return {
164448
+ accepted: true,
164449
+ targetTurnId: asString(result?.turnId) ?? asString(result?.targetTurnId) ?? asString(outcome.turnId) ?? asString(outcome.targetTurnId)
164450
+ };
164228
164451
  }
164229
164452
  if (kind === "queued") return {
164230
164453
  accepted: true,
164231
- pendingInputId: asString(outcome.pendingInputId) ?? asString(outcome.pendingInputID)
164454
+ pendingInputId: asString(outcome.pendingInputId) ?? asString(outcome.pendingInputID),
164455
+ targetTurnId: asString(outcome.turnId) ?? asString(outcome.turnID) ?? asString(outcome.targetTurnId)
164232
164456
  };
164233
164457
  else if (kind === "rejected") return {
164234
164458
  accepted: false,
164459
+ targetTurnId: asString(outcome.activeTurnId) ?? asString(outcome.targetTurnId),
164235
164460
  reason: asString(outcome.reason) ?? "unknown reason"
164236
164461
  };
164237
164462
  return { accepted: true };
@@ -164271,10 +164496,14 @@ var ZCodeTui = class {
164271
164496
  if (isRecord(result.workflowPanel)) await this.showWorkflowPanel(result.workflowPanel);
164272
164497
  if (isRecord(result.selection)) await this.showSelection(result.selection);
164273
164498
  }
164274
- onEvent(value) {
164499
+ onEvent(value, turnEpoch) {
164275
164500
  this.debugEvent("session", value);
164501
+ if (turnEpoch !== void 0 && turnEpoch !== this.activeTurnEpoch) return;
164276
164502
  const event = normalizeEvent(value);
164277
164503
  if (!event) return;
164504
+ const steerQueued = event.type === "turn_steer_queued" || event.type === "turn.steerQueued";
164505
+ if (this.turnAbortController && steerQueued && event.inputId && this.inputQueue.findSteer(event.inputId) && event.targetTurnId && !this.activeTurnId) this.activeTurnId = event.targetTurnId;
164506
+ if ((event.type === "turn_started" || event.type === "turn.started") && event.inputId === this.primaryTurnInputId) this.activeTurnId = event.turnId ?? event.targetTurnId ?? this.activeTurnId;
164278
164507
  if (runtimeRefreshNeeded(event)) this.scheduleRuntimeRefresh();
164279
164508
  if (this.inputQueue.handleLifecycleEvent(event)) {
164280
164509
  this.requestStreamRender();
@@ -164372,12 +164601,14 @@ var ZCodeTui = class {
164372
164601
  detail: event.message
164373
164602
  });
164374
164603
  } else if (event.type === "model_request_failed") {
164375
- this.updateActivity(event.retryable === true ? "model request failed · waiting to retry…" : "model request failed…", false);
164376
- if (event.retryable !== true) this.addSystemEvent({
164377
- tone: "error",
164378
- title: "Model request failed",
164379
- detail: event.message
164380
- });
164604
+ if (!isModelCancellationEvent(event)) {
164605
+ this.updateActivity(event.retryable === true ? "model request failed · waiting to retry…" : "model request failed…", false);
164606
+ if (event.retryable !== true) this.addSystemEvent({
164607
+ tone: "error",
164608
+ title: "Model request failed",
164609
+ detail: event.message
164610
+ });
164611
+ }
164381
164612
  } else if (event.type === "model_stream_stalled") {
164382
164613
  this.updateActivity("model stream stalled · waiting to retry…", false);
164383
164614
  const idle = event.idleMs !== void 0 ? `idle ${Math.ceil(event.idleMs / 1e3)}s` : void 0;
@@ -166399,7 +166630,10 @@ var ZCodeTui = class {
166399
166630
  stop() {
166400
166631
  if (this.stopped) return;
166401
166632
  this.stopped = true;
166633
+ this.pendingSteerInterrupt = void 0;
166402
166634
  this.turnAbortController?.abort();
166635
+ for (const controller of this.steerAbortControllers) controller.abort();
166636
+ this.steerAbortControllers.clear();
166403
166637
  this.updateCheckAbortController?.abort();
166404
166638
  if (this.turnTimer) clearInterval(this.turnTimer);
166405
166639
  if (this.rewindEscapeTimer) clearTimeout(this.rewindEscapeTimer);