@akagilnc/pi-workflow-roles 0.1.4021 → 0.1.4035

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.
@@ -11023,28 +11023,95 @@ function recordedRole(payload) {
11023
11023
  if (isTerminalRoleName(payload.projection?.role)) return payload.projection.role;
11024
11024
  return void 0;
11025
11025
  }
11026
+ function rowRank(kind) {
11027
+ switch (kind) {
11028
+ case "accepted":
11029
+ return 4;
11030
+ case "audit-escalation":
11031
+ return 3;
11032
+ case "correctable-rejection":
11033
+ case "infrastructure":
11034
+ return 2;
11035
+ case "candidate":
11036
+ return 1;
11037
+ }
11038
+ }
11039
+ function submissionCallKey(attemptId, toolCallId) {
11040
+ return `${attemptId ?? ""}\0${toolCallId}`;
11041
+ }
11042
+ function rowFromPayload(kind, payload, accepted, roleFallback) {
11043
+ const role = recordedRole(payload) ?? roleFallback;
11044
+ return {
11045
+ ...role === void 0 ? {} : { role },
11046
+ kind,
11047
+ accepted,
11048
+ ...typeof payload.toolCallId === "string" && payload.toolCallId.length > 0 ? { toolCallId: payload.toolCallId } : {}
11049
+ };
11050
+ }
11026
11051
  async function readRecordedSubmissionRows(cwd, runId, homeOrScope) {
11027
11052
  const scope = resolveReadScope(homeOrScope);
11028
11053
  const { owned } = await readOwnedSubmissionRecords(cwd, runId, scope.home);
11029
11054
  const scoped = recordsForAttempt(owned, scope.attemptId);
11030
11055
  const out = [];
11056
+ const indexByCall = /* @__PURE__ */ new Map();
11057
+ const roleByCall = /* @__PURE__ */ new Map();
11058
+ for (const record4 of scoped) {
11059
+ const payload = record4.payload;
11060
+ if (typeof payload?.toolCallId !== "string" || payload.toolCallId.length === 0) continue;
11061
+ const role = recordedRole(payload);
11062
+ if (role !== void 0) {
11063
+ roleByCall.set(submissionCallKey(recordAttemptId(record4), payload.toolCallId), role);
11064
+ }
11065
+ }
11066
+ const take = (row, callKey) => {
11067
+ const toolCallId = row.toolCallId;
11068
+ if (toolCallId !== void 0 && callKey !== void 0) {
11069
+ const existingIndex = indexByCall.get(callKey);
11070
+ if (existingIndex !== void 0) {
11071
+ const existing = out[existingIndex];
11072
+ if (rowRank(row.kind) >= rowRank(existing.kind)) {
11073
+ out[existingIndex] = {
11074
+ ...row,
11075
+ toolCallId,
11076
+ // Keep a previously recovered role when the upgraded row still omits it.
11077
+ ...row.role === void 0 && existing.role !== void 0 ? { role: existing.role } : {}
11078
+ };
11079
+ } else if (existing.role === void 0 && row.role !== void 0) {
11080
+ out[existingIndex] = { ...existing, role: row.role };
11081
+ }
11082
+ return;
11083
+ }
11084
+ indexByCall.set(callKey, out.length);
11085
+ }
11086
+ out.push(row);
11087
+ };
11031
11088
  for (const record4 of scoped) {
11089
+ const attemptId = recordAttemptId(record4);
11090
+ if (record4.kind === "candidate") {
11091
+ const payload2 = record4.payload;
11092
+ if (payload2?.type !== "candidate" || payload2.params === void 0) continue;
11093
+ const callKey2 = typeof payload2.toolCallId === "string" ? submissionCallKey(attemptId, payload2.toolCallId) : void 0;
11094
+ const fallback2 = callKey2 !== void 0 ? roleByCall.get(callKey2) : void 0;
11095
+ take(rowFromPayload("candidate", payload2, payload2.params, fallback2), callKey2);
11096
+ continue;
11097
+ }
11032
11098
  if (record4.kind === "sealed") {
11033
11099
  const payload2 = record4.payload;
11034
11100
  if (payload2?.type !== "sealed" || payload2.accepted === void 0) continue;
11035
- const role2 = recordedRole(payload2);
11036
- if (role2 === void 0) continue;
11037
- out.push({ role: role2, kind: "accepted", accepted: payload2.accepted });
11101
+ const callKey2 = typeof payload2.toolCallId === "string" ? submissionCallKey(attemptId, payload2.toolCallId) : void 0;
11102
+ const fallback2 = callKey2 !== void 0 ? roleByCall.get(callKey2) : void 0;
11103
+ take(rowFromPayload("accepted", payload2, payload2.accepted, fallback2), callKey2);
11038
11104
  continue;
11039
11105
  }
11040
11106
  if (record4.kind !== "outcome") continue;
11041
11107
  const payload = record4.payload;
11042
- if (payload?.type !== "outcome" || payload.outcome !== "audit-escalation" || payload.accepted === void 0) {
11043
- continue;
11044
- }
11045
- const role = recordedRole(payload);
11046
- if (role === void 0) continue;
11047
- out.push({ role, kind: "audit-escalation", accepted: payload.accepted });
11108
+ if (payload?.type !== "outcome" || payload.accepted === void 0) continue;
11109
+ const outcome = payload.outcome;
11110
+ const kind = outcome === "audit-escalation" ? "audit-escalation" : outcome === "correctable-rejection" ? "correctable-rejection" : outcome === "infrastructure" ? "infrastructure" : void 0;
11111
+ if (kind === void 0) continue;
11112
+ const callKey = typeof payload.toolCallId === "string" ? submissionCallKey(attemptId, payload.toolCallId) : void 0;
11113
+ const fallback = callKey !== void 0 ? roleByCall.get(callKey) : void 0;
11114
+ take(rowFromPayload(kind, payload, payload.accepted, fallback), callKey);
11048
11115
  }
11049
11116
  return out;
11050
11117
  }
@@ -11123,6 +11190,7 @@ function createSubmissionLedgerHost(host, outputTools, failInfrastructure2 = (er
11123
11190
  toolCallId,
11124
11191
  toolName: tool.name,
11125
11192
  sequence: ++state.sequence,
11193
+ role,
11126
11194
  params
11127
11195
  });
11128
11196
  let result;
@@ -11148,6 +11216,7 @@ function createSubmissionLedgerHost(host, outputTools, failInfrastructure2 = (er
11148
11216
  outcome: "correctable-rejection",
11149
11217
  code: "typed-bounce",
11150
11218
  diagnostic: error instanceof Error ? error.message : String(error),
11219
+ role,
11151
11220
  accepted: params
11152
11221
  });
11153
11222
  throw error;
@@ -11158,6 +11227,7 @@ function createSubmissionLedgerHost(host, outputTools, failInfrastructure2 = (er
11158
11227
  toolCallId,
11159
11228
  outcome: "infrastructure",
11160
11229
  diagnostic: error instanceof Error ? error.message : String(error),
11230
+ role,
11161
11231
  accepted: params
11162
11232
  });
11163
11233
  throw error;
@@ -11465,7 +11535,7 @@ function knownFailureFromProviderStop(input) {
11465
11535
  const diagnostic = nonEmptyString(input.errorMessage);
11466
11536
  const details = sessionStopDetails(input);
11467
11537
  return {
11468
- cause: hasUpstreamErrorTestimony(input) ? "provider" : "unrecognized",
11538
+ ...hasUpstreamErrorTestimony(input) ? { cause: "provider" } : {},
11469
11539
  ...diagnostic === void 0 ? {} : { diagnostic },
11470
11540
  ...Object.keys(details).length === 0 ? {} : { details }
11471
11541
  };
@@ -13190,15 +13260,6 @@ function isLawfulTypedTerminalOutcome(outcome) {
13190
13260
  function exitCodeForTerminalOutcome(outcome) {
13191
13261
  return isLawfulTypedTerminalOutcome(outcome) ? 0 : 1;
13192
13262
  }
13193
- function lastRolePayloadRecord(payloads) {
13194
- for (let index = payloads.length - 1; index >= 0; index -= 1) {
13195
- const payload = payloads[index];
13196
- if (typeof payload === "object" && payload !== null && !Array.isArray(payload)) {
13197
- return payload;
13198
- }
13199
- }
13200
- return void 0;
13201
- }
13202
13263
  function recommendationNavigatorFact(input) {
13203
13264
  const command = typeof input.modelCommand === "string" && input.modelCommand.trim() !== "" ? input.modelCommand : isPublicCallableRole2(input.next.role) ? renderPublicAkRoleCommand(input.next) : void 0;
13204
13265
  return {
@@ -13213,7 +13274,7 @@ function recommendationNavigatorFact(input) {
13213
13274
  function formatTerminalResult(result) {
13214
13275
  const lines = [];
13215
13276
  lines.push("role outcome status");
13216
- const outcomeStatus = result.roleOutcome.kind === "failure" ? result.roleOutcome.cause : result.roleOutcome.kind === "accepted" ? "accepted" : result.roleOutcome.status;
13277
+ const outcomeStatus = result.roleOutcome.kind === "failure" ? result.roleOutcome.cause ?? "" : result.roleOutcome.kind === "accepted" ? "accepted" : result.roleOutcome.status;
13217
13278
  lines.push(
13218
13279
  `${result.roleOutcome.role} ${result.roleOutcome.kind} ${encodeTerminalField(outcomeStatus)}`
13219
13280
  );
@@ -13299,9 +13360,12 @@ function ledgerReadScope(admitted, scope) {
13299
13360
  }
13300
13361
  function roleOutcomeFromRows(role, rows) {
13301
13362
  const mine = rows.filter((row) => row.role === role);
13302
- if (mine.length === 0) return void 0;
13363
+ const terminal = mine.filter(
13364
+ (row) => row.kind === "accepted" || row.kind === "audit-escalation"
13365
+ );
13366
+ if (terminal.length === 0) return void 0;
13303
13367
  const payloads = mine.map((row) => row.accepted);
13304
- if (mine.some((row) => row.kind === "audit-escalation")) {
13368
+ if (terminal.some((row) => row.kind === "audit-escalation")) {
13305
13369
  return { kind: "audit_escalation", role, status: "audit_escalation", payloads };
13306
13370
  }
13307
13371
  return { kind: "accepted", role, payloads };
@@ -13333,7 +13397,7 @@ async function recordedSubmissionPayloads(admitted, scope) {
13333
13397
  function withSubmissions(terminal, submissions) {
13334
13398
  if (submissions.length === 0) return terminal;
13335
13399
  const roleOutcome = terminal.roleOutcome;
13336
- const withPayloads = roleOutcome.kind === "accepted" || roleOutcome.kind === "audit_escalation" ? { ...roleOutcome, payloads: (roleOutcome.payloads ?? []).length > 0 ? roleOutcome.payloads : submissions } : roleOutcome.kind === "failure" ? { ...roleOutcome, payloads: roleOutcome.payloads ?? submissions } : roleOutcome;
13400
+ const withPayloads = roleOutcome.kind === "accepted" || roleOutcome.kind === "audit_escalation" || roleOutcome.kind === "failure" ? { ...roleOutcome, payloads: submissions } : roleOutcome;
13337
13401
  return { ...terminal, roleOutcome: withPayloads, submissions };
13338
13402
  }
13339
13403
  async function attachRecordedSubmissions(admitted, terminal, scope) {
@@ -13429,7 +13493,7 @@ function thrownIdentity(error) {
13429
13493
  function isTypedActivationError(error) {
13430
13494
  if (!(error instanceof Error)) return false;
13431
13495
  const cause = error.knownCause;
13432
- return cause === "provider" || cause === "activation" || cause === "session" || cause === "output" || cause === "timeout" || cause === "unrecognized";
13496
+ return cause === "provider" || cause === "activation" || cause === "session" || cause === "output" || cause === "timeout";
13433
13497
  }
13434
13498
  function flattenThrownFailureLeaves(error) {
13435
13499
  if (!(error instanceof AggregateError)) {
@@ -13449,7 +13513,7 @@ function projectThrownFailureLeaf(error) {
13449
13513
  }
13450
13514
  return {
13451
13515
  cause: error.knownCause,
13452
- diagnostic: error.message || error.name || "unrecognized exception",
13516
+ diagnostic: error.message || error.name || "exception",
13453
13517
  identity,
13454
13518
  ...error.details === void 0 ? {} : { details: error.details }
13455
13519
  };
@@ -13457,13 +13521,11 @@ function projectThrownFailureLeaf(error) {
13457
13521
  if (error instanceof Error) {
13458
13522
  const identity = thrownIdentity(error);
13459
13523
  return {
13460
- cause: "unrecognized",
13461
- diagnostic: error.message || error.name || "unrecognized exception",
13524
+ diagnostic: error.message || error.name || "exception",
13462
13525
  identity
13463
13526
  };
13464
13527
  }
13465
13528
  return {
13466
- cause: "unrecognized",
13467
13529
  diagnostic: String(error)
13468
13530
  };
13469
13531
  }
@@ -13485,7 +13547,7 @@ function classifyThrownFailure(error) {
13485
13547
  ...leaves.slice(1).map((leaf) => {
13486
13548
  const secondary = projectThrownFailureLeaf(leaf);
13487
13549
  return {
13488
- cause: secondary.cause,
13550
+ ...secondary.cause === void 0 ? {} : { cause: secondary.cause },
13489
13551
  diagnostic: secondary.diagnostic,
13490
13552
  ...secondary.identity === void 0 ? {} : { identity: secondary.identity },
13491
13553
  ...secondary.details === void 0 ? {} : { details: secondary.details }
@@ -13493,7 +13555,7 @@ function classifyThrownFailure(error) {
13493
13555
  })
13494
13556
  ];
13495
13557
  return {
13496
- cause: primary.cause,
13558
+ ...primary.cause === void 0 ? {} : { cause: primary.cause },
13497
13559
  diagnostic: primary.diagnostic,
13498
13560
  ...primary.identity === void 0 ? {} : { identity: primary.identity },
13499
13561
  details: {
@@ -13534,6 +13596,24 @@ function classifyPostAdmissionFailure(input) {
13534
13596
  ...input.knownIdentity === void 0 ? {} : { identity: input.knownIdentity }
13535
13597
  };
13536
13598
  }
13599
+ if (input.knownDiagnostic !== void 0 && input.knownDiagnostic.trim() !== "" || input.knownIdentity !== void 0) {
13600
+ const diagnostic = input.knownDiagnostic !== void 0 && input.knownDiagnostic.trim() !== "" ? input.knownDiagnostic : conciseChildDiagnostic(input.stderr, "role run failed");
13601
+ const { timedOut: _knownTimedOut, ...knownDetails } = input.knownDetails ?? {};
13602
+ const remoteCode = knownDetails.code;
13603
+ return withKnownDetails(
13604
+ {
13605
+ diagnostic,
13606
+ details: {
13607
+ ...knownDetails,
13608
+ ...remoteCode === void 0 ? {} : { code: remoteCode },
13609
+ exitCode: input.code,
13610
+ ...input.timedOut ? { timedOut: true } : {}
13611
+ },
13612
+ ...input.knownIdentity === void 0 ? {} : { identity: input.knownIdentity }
13613
+ },
13614
+ void 0
13615
+ );
13616
+ }
13537
13617
  if (input.timedOut) {
13538
13618
  return withKnownDetails(
13539
13619
  {
@@ -13587,7 +13667,7 @@ function classifyPostAdmissionFailure(input) {
13587
13667
  function explicitInternalKnownFailureClassificationInput(failure2) {
13588
13668
  if (failure2 === void 0) return {};
13589
13669
  return {
13590
- knownCause: failure2.cause,
13670
+ ...failure2.cause === void 0 ? {} : { knownCause: failure2.cause },
13591
13671
  ...failure2.identity === void 0 ? {} : { knownIdentity: failure2.identity },
13592
13672
  ...failure2.diagnostic === void 0 ? {} : { knownDiagnostic: failure2.diagnostic },
13593
13673
  ...failure2.details === void 0 ? {} : { knownDetails: failure2.details }
@@ -13839,10 +13919,12 @@ function complianceFailureFromAuditorVolumes(volumes) {
13839
13919
  if (entry?.type !== "custom" || entry.customType !== AUDITOR_COMPLIANCE_FAILURE_ENTRY_TYPE || !isRecord10(entry.data)) continue;
13840
13920
  const parent = isRecord10(entry.data.parent) ? entry.data.parent : void 0;
13841
13921
  const failure2 = isRecord10(entry.data.failure) ? entry.data.failure : void 0;
13842
- if (parent?.sessionId !== parentId || parent.sessionFile !== sessionFile || parent.attemptEntryId !== attemptEntryId || failure2?.cause !== "provider" && failure2?.cause !== "unrecognized") continue;
13922
+ if (parent?.sessionId !== parentId || parent.sessionFile !== sessionFile || parent.attemptEntryId !== attemptEntryId) continue;
13923
+ if (failure2 === void 0) continue;
13843
13924
  const identity = isRecord10(failure2.identity) ? failure2.identity : void 0;
13925
+ const typedCause = failure2.cause === "provider" || failure2.cause === "activation" || failure2.cause === "session" || failure2.cause === "output" || failure2.cause === "timeout" ? failure2.cause : void 0;
13844
13926
  return {
13845
- cause: failure2.cause === "provider" ? "provider" : "unrecognized",
13927
+ ...typedCause === void 0 ? {} : { cause: typedCause },
13846
13928
  ...identity === void 0 ? {} : { identity: {
13847
13929
  ...typeof identity.name === "string" ? { name: identity.name } : {},
13848
13930
  ...typeof identity.code === "string" || typeof identity.code === "number" ? { code: identity.code } : {}
@@ -14494,8 +14576,6 @@ async function publishDoctorArtifacts(admitted, roleOutcome, coordinates, option
14494
14576
  role: "doctor",
14495
14577
  runId: admitted.runId,
14496
14578
  outcome: roleOutcome,
14497
- ...options.doctorOutput === void 0 ? {} : { receipt: options.doctorOutput },
14498
- // Independent machine fields beside the receipt — not merged into it.
14499
14579
  ...options.cost === void 0 ? {} : { cost: options.cost },
14500
14580
  ...options.auditNoReceipt === void 0 ? {} : { auditNoReceipt: options.auditNoReceipt }
14501
14581
  },
@@ -14553,7 +14633,6 @@ async function settleLawfulDoctorTerminalResult(admitted, authority, scope) {
14553
14633
  sessionDirectory
14554
14634
  );
14555
14635
  }
14556
- const output = lastRolePayloadRecord(sealed.payloads ?? []) ?? {};
14557
14636
  const roleOutcome = sealed;
14558
14637
  const navigator = extractNavigatorFact(entries);
14559
14638
  const cost = extractDoctorCandidateCostFact(entries);
@@ -14563,7 +14642,6 @@ async function settleLawfulDoctorTerminalResult(admitted, authority, scope) {
14563
14642
  roleOutcome,
14564
14643
  coordinates,
14565
14644
  {
14566
- doctorOutput: output,
14567
14645
  ...cost === void 0 ? {} : { cost },
14568
14646
  ...auditNoReceipt === void 0 ? {} : { auditNoReceipt }
14569
14647
  }
@@ -14813,7 +14891,7 @@ async function publishFailureArtifacts(admitted, failure2, authority) {
14813
14891
  kind: "error",
14814
14892
  role: admitted.role,
14815
14893
  runId: admitted.runId,
14816
- cause: failure2.cause,
14894
+ ...failure2.cause === void 0 ? {} : { cause: failure2.cause },
14817
14895
  diagnostic: failure2.diagnostic,
14818
14896
  ...failure2.identity === void 0 ? {} : { identity: failure2.identity },
14819
14897
  ...failure2.details === void 0 ? {} : { details: failure2.details }
@@ -14836,7 +14914,7 @@ async function publishFailureArtifacts(admitted, failure2, authority) {
14836
14914
  sha256: a.sha256,
14837
14915
  byteLength: a.byteLength
14838
14916
  })),
14839
- failureCause: failure2.cause
14917
+ ...failure2.cause === void 0 ? {} : { failureCause: failure2.cause }
14840
14918
  };
14841
14919
  const evidenceWrite = await writeFailureJsonRetainingCause(
14842
14920
  evidenceCandidates,
@@ -14900,7 +14978,7 @@ async function settleFailureTerminalResult(admitted, failure2, authority, option
14900
14978
  const navigator = await extractNavigatorFactFromAdmittedSession(sessionFile);
14901
14979
  const artifacts = await publishFailureArtifacts(admitted, failure2, authority);
14902
14980
  const decisiveFacts = {
14903
- cause: failure2.cause,
14981
+ ...failure2.cause === void 0 ? {} : { cause: failure2.cause },
14904
14982
  diagnostic: failure2.diagnostic
14905
14983
  };
14906
14984
  if (failure2.identity?.name !== void 0) {
@@ -14916,7 +14994,7 @@ async function settleFailureTerminalResult(admitted, failure2, authority, option
14916
14994
  const roleOutcome2 = {
14917
14995
  kind: "failure",
14918
14996
  role: admitted.role,
14919
- cause: failure2.cause,
14997
+ ...failure2.cause === void 0 ? {} : { cause: failure2.cause },
14920
14998
  diagnostic: failure2.diagnostic,
14921
14999
  decisiveFacts
14922
15000
  };
@@ -14933,7 +15011,7 @@ async function settleFailureTerminalResult(admitted, failure2, authority, option
14933
15011
  const roleOutcome = {
14934
15012
  kind: "failure",
14935
15013
  role: admitted.role,
14936
- cause: failure2.cause,
15014
+ ...failure2.cause === void 0 ? {} : { cause: failure2.cause },
14937
15015
  diagnostic: failure2.diagnostic,
14938
15016
  decisiveFacts
14939
15017
  };
@@ -14954,7 +15032,7 @@ function presentFailureTerminal(terminal, io) {
14954
15032
  io.stdout(formatTerminalResult(terminal));
14955
15033
  if (terminal.roleOutcome.kind === "failure") {
14956
15034
  io.stderr(formatFailureStderrDiagnostic({
14957
- cause: terminal.roleOutcome.cause,
15035
+ ...terminal.roleOutcome.cause === void 0 ? {} : { cause: terminal.roleOutcome.cause },
14958
15036
  diagnostic: terminal.roleOutcome.diagnostic
14959
15037
  }));
14960
15038
  return;
@@ -15224,12 +15302,29 @@ function unwrapTurnDispatchedFailure(error) {
15224
15302
  }
15225
15303
  return current;
15226
15304
  }
15305
+ async function attachDispatchExceptionTerminal(admitted, terminal, io) {
15306
+ try {
15307
+ return await attachRecordedSubmissions(
15308
+ {
15309
+ projectRoot: admitted.projectRoot,
15310
+ runId: admitted.runId,
15311
+ runDirectory: admitted.runDirectory
15312
+ },
15313
+ terminal
15314
+ );
15315
+ } catch (error) {
15316
+ io.stderr(
15317
+ `dispatch exception ledger attach failed (best-effort continue): ${describeErrorIdentity(error)}
15318
+ `
15319
+ );
15320
+ return terminal;
15321
+ }
15322
+ }
15227
15323
  function dispatchExceptionFailureTerminal(input) {
15228
15324
  const causeError = unwrapTurnDispatchedFailure(input.causeError);
15229
15325
  const history = input.everyAttemptThrew ? "dispatch threw an exception on every attempt" : "the final dispatch threw an exception";
15230
15326
  const diagnostic = `${history} (${input.endReason}; resumes used ${input.autoResumeAttempts}); last cause: ${describeErrorIdentity(causeError)}`;
15231
15327
  const decisiveFacts = {
15232
- cause: "unrecognized",
15233
15328
  diagnostic,
15234
15329
  resumesUsed: input.autoResumeAttempts,
15235
15330
  dispatchErrorFiles: [...input.errorFiles]
@@ -15250,7 +15345,6 @@ function dispatchExceptionFailureTerminal(input) {
15250
15345
  roleOutcome: {
15251
15346
  kind: "failure",
15252
15347
  role: input.role,
15253
- cause: "unrecognized",
15254
15348
  diagnostic,
15255
15349
  decisiveFacts
15256
15350
  },
@@ -15350,15 +15444,19 @@ async function runWithAutoResumeLoop(options) {
15350
15444
  }
15351
15445
  } else {
15352
15446
  if (autoResumeAttempts >= limit) {
15353
- const terminal = dispatchExceptionFailureTerminal({
15354
- role: options.admitted.role,
15355
- runId: options.admitted.runId,
15356
- causeError: lastThrownError,
15357
- errorFiles: retainedErrorFiles,
15358
- autoResumeAttempts,
15359
- endReason: "auto-resume budget exhausted",
15360
- everyAttemptThrew
15361
- });
15447
+ const terminal = await attachDispatchExceptionTerminal(
15448
+ options.admitted,
15449
+ dispatchExceptionFailureTerminal({
15450
+ role: options.admitted.role,
15451
+ runId: options.admitted.runId,
15452
+ causeError: lastThrownError,
15453
+ errorFiles: retainedErrorFiles,
15454
+ autoResumeAttempts,
15455
+ endReason: "auto-resume budget exhausted",
15456
+ everyAttemptThrew
15457
+ }),
15458
+ options.io
15459
+ );
15362
15460
  await finalizeExceptionRunBestEffort(options.admitted.runDirectory, options.io);
15363
15461
  presentTerminal(terminal, options.io);
15364
15462
  return {
@@ -15367,15 +15465,19 @@ async function runWithAutoResumeLoop(options) {
15367
15465
  };
15368
15466
  }
15369
15467
  if (!await isPrincipalAvailable(options.admitted.principal)) {
15370
- const terminal = dispatchExceptionFailureTerminal({
15371
- role: options.admitted.role,
15372
- runId: options.admitted.runId,
15373
- causeError: lastThrownError,
15374
- errorFiles: retainedErrorFiles,
15375
- autoResumeAttempts,
15376
- endReason: "session principal unavailable before further resume",
15377
- everyAttemptThrew
15378
- });
15468
+ const terminal = await attachDispatchExceptionTerminal(
15469
+ options.admitted,
15470
+ dispatchExceptionFailureTerminal({
15471
+ role: options.admitted.role,
15472
+ runId: options.admitted.runId,
15473
+ causeError: lastThrownError,
15474
+ errorFiles: retainedErrorFiles,
15475
+ autoResumeAttempts,
15476
+ endReason: "session principal unavailable before further resume",
15477
+ everyAttemptThrew
15478
+ }),
15479
+ options.io
15480
+ );
15379
15481
  await finalizeExceptionRunBestEffort(options.admitted.runDirectory, options.io);
15380
15482
  presentTerminal(terminal, options.io);
15381
15483
  return {
@@ -16673,7 +16775,7 @@ function instructionSeatAdapters(options) {
16673
16775
  }) => {
16674
16776
  const infrastructureFailure = await readEngineDetourInfrastructureFailure(sessionFile);
16675
16777
  return infrastructureFailure === void 0 ? result.knownFailure : {
16676
- cause: infrastructureFailure.cause,
16778
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
16677
16779
  diagnostic: infrastructureFailure.diagnostic,
16678
16780
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
16679
16781
  };
@@ -16918,7 +17020,7 @@ function judgeAdapters() {
16918
17020
  resolveRunnerKnownFailure: async ({ result, sessionFile }) => {
16919
17021
  const infrastructureFailure = await readEngineDetourInfrastructureFailure(sessionFile);
16920
17022
  return result.knownFailure ?? (infrastructureFailure === void 0 ? void 0 : {
16921
- cause: infrastructureFailure.cause,
17023
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
16922
17024
  diagnostic: infrastructureFailure.diagnostic,
16923
17025
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
16924
17026
  });
@@ -17230,15 +17332,11 @@ async function runPublicDiarist(argv, env, io, parseDiaristArgv2) {
17230
17332
  ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
17231
17333
  }).then(async (result) => {
17232
17334
  if (admitted.ticketNumber === void 0 && result.admitted !== void 0) {
17233
- const roleOutcome = result.terminal?.roleOutcome;
17234
- const facts = roleOutcome?.kind === "accepted" ? lastRolePayloadRecord(roleOutcome.payloads ?? []) : void 0;
17235
- if (roleOutcome !== void 0 && roleOutcome.kind === "accepted" && facts?.status !== "escalate") {
17236
- const raw = typeof facts?.ticketNumber === "number" ? facts.ticketNumber : typeof facts?.sitian === "object" && facts.sitian !== null && typeof facts.sitian.ticketNumber === "number" ? facts.sitian.ticketNumber : void 0;
17237
- if (typeof raw === "number" && Number.isSafeInteger(raw) && raw >= 1) {
17238
- admitted.ticketNumber = raw;
17239
- if (result.admitted.ticketNumber === void 0) {
17240
- result.admitted.ticketNumber = raw;
17241
- }
17335
+ const fromPages = await readRunTicketNumber(admitted.runDirectory);
17336
+ if (fromPages !== void 0) {
17337
+ await bindAdmittedTicketNumber(admitted, fromPages);
17338
+ if (result.admitted.ticketNumber === void 0) {
17339
+ result.admitted.ticketNumber = fromPages;
17242
17340
  }
17243
17341
  }
17244
17342
  }
@@ -17280,8 +17378,8 @@ var init_diarist_run = __esm({
17280
17378
  init_run_lifecycle();
17281
17379
  init_seat_ticket_binding();
17282
17380
  init_settlement();
17283
- init_terminal();
17284
17381
  init_turn_request();
17382
+ init_run_ticket_number();
17285
17383
  }
17286
17384
  });
17287
17385
 
@@ -19653,6 +19751,24 @@ function navigatorProviderFailureFromDiagnostics(diagnostics) {
19653
19751
  }
19654
19752
  return void 0;
19655
19753
  }
19754
+ function navigatorProviderFailureFromPublicTerminal(outcome) {
19755
+ const facts = outcome.decisiveFacts;
19756
+ const secondary = typeof facts.secondaryEvidence === "object" && facts.secondaryEvidence !== null ? facts.secondaryEvidence : void 0;
19757
+ const httpStatus = typeof secondary?.httpStatus === "number" ? secondary.httpStatus : typeof facts.httpStatus === "number" ? facts.httpStatus : typeof facts.errorCode === "number" ? facts.errorCode : void 0;
19758
+ const fromStatus = navigatorProviderFailureFromStatus(httpStatus);
19759
+ if (fromStatus !== void 0) return fromStatus;
19760
+ const diagnostics = secondary?.diagnostics ?? facts.diagnostics;
19761
+ const fromDiagnostics = navigatorProviderFailureFromDiagnostics(diagnostics);
19762
+ if (fromDiagnostics !== void 0) return fromDiagnostics;
19763
+ const fromCode = navigatorProviderFailureFromError({
19764
+ code: secondary?.code ?? facts.errorCode
19765
+ });
19766
+ if (fromCode !== void 0) return fromCode;
19767
+ if (outcome.cause === "provider") return { source: "transport", cause: "unknown" };
19768
+ const typed = navigatorUnavailableKey(outcome.cause);
19769
+ if (typed !== void 0) return { source: typed, cause: typed };
19770
+ return { source: "unknown", cause: "unknown" };
19771
+ }
19656
19772
  var navigatorProviderFailureSchema = Type20.Object({
19657
19773
  source: Type20.Union([
19658
19774
  Type20.Literal("context"),
@@ -19734,23 +19850,6 @@ async function resolveNavigatorSeatSelection(context) {
19734
19850
  }
19735
19851
 
19736
19852
  // src/navigator-public-session.ts
19737
- init_terminal();
19738
- function providerFailureFromPublicTerminal(outcome) {
19739
- const facts = outcome.decisiveFacts;
19740
- const secondary = typeof facts.secondaryEvidence === "object" && facts.secondaryEvidence !== null ? facts.secondaryEvidence : void 0;
19741
- const httpStatus = typeof secondary?.httpStatus === "number" ? secondary.httpStatus : typeof facts.httpStatus === "number" ? facts.httpStatus : typeof facts.errorCode === "number" ? facts.errorCode : void 0;
19742
- const fromStatus = navigatorProviderFailureFromStatus(httpStatus);
19743
- if (fromStatus !== void 0) return fromStatus;
19744
- const diagnostics = secondary?.diagnostics ?? facts.diagnostics;
19745
- const fromDiagnostics = navigatorProviderFailureFromDiagnostics(diagnostics);
19746
- if (fromDiagnostics !== void 0) return fromDiagnostics;
19747
- const fromCode = navigatorProviderFailureFromError({
19748
- code: secondary?.code ?? facts.errorCode
19749
- });
19750
- if (fromCode !== void 0) return fromCode;
19751
- if (outcome.cause === "provider") return { source: "transport", cause: "transport" };
19752
- return { source: "session", cause: "session" };
19753
- }
19754
19853
  function createNativeNavigatorSessionFactory() {
19755
19854
  return async ({ context, subject, tool }) => {
19756
19855
  const resolved = await resolveNavigatorSeatSelection(context);
@@ -19806,14 +19905,15 @@ function createNativeNavigatorSessionFactory() {
19806
19905
  const outcome = summoned.terminal?.roleOutcome;
19807
19906
  if (outcome === void 0) {
19808
19907
  const detail = summoned.stderr?.trim() || `exit ${summoned.exitCode}`;
19809
- providerFailure = { source: "transport", cause: "transport" };
19908
+ providerFailure = { source: "transport", cause: "unknown" };
19810
19909
  throw navigatorUnavailableError(
19811
- "transport",
19812
- new Error(`Navigator public summon produced no terminal (${detail})`)
19910
+ providerFailure.source,
19911
+ new Error(`Navigator public summon produced no terminal (${detail})`),
19912
+ providerFailure.cause
19813
19913
  );
19814
19914
  }
19815
19915
  if (outcome.kind === "failure") {
19816
- providerFailure = providerFailureFromPublicTerminal(outcome);
19916
+ providerFailure = navigatorProviderFailureFromPublicTerminal(outcome);
19817
19917
  throw navigatorUnavailableError(
19818
19918
  providerFailure.source,
19819
19919
  new Error(outcome.diagnostic),
@@ -19827,21 +19927,22 @@ function createNativeNavigatorSessionFactory() {
19827
19927
  if (outcome.kind !== "accepted") {
19828
19928
  return;
19829
19929
  }
19830
- const candidates = lastRolePayloadRecord(outcome.payloads ?? [])?.candidates;
19831
- if (!Array.isArray(candidates)) {
19832
- return;
19930
+ for (const payload of outcome.payloads ?? []) {
19931
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) continue;
19932
+ const candidates = payload.candidates;
19933
+ if (!Array.isArray(candidates)) continue;
19934
+ await tool.execute(
19935
+ "navigator-public-prepare",
19936
+ { candidates },
19937
+ void 0,
19938
+ void 0,
19939
+ context
19940
+ );
19833
19941
  }
19834
- await tool.execute(
19835
- "navigator-public-prepare",
19836
- { candidates },
19837
- void 0,
19838
- void 0,
19839
- context
19840
- );
19841
19942
  } catch (error) {
19842
19943
  if (error instanceof NavigatorUnavailableError) throw error;
19843
19944
  const fact = navigatorProviderFailureFromError(error);
19844
- providerFailure = fact ?? { source: "transport", cause: "transport" };
19945
+ providerFailure = fact ?? { source: "transport", cause: "unknown" };
19845
19946
  throw navigatorUnavailableError(providerFailure.source, error, providerFailure.cause);
19846
19947
  }
19847
19948
  })();