@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.
@@ -23166,28 +23166,95 @@ function recordedRole(payload) {
23166
23166
  if (isTerminalRoleName(payload.projection?.role)) return payload.projection.role;
23167
23167
  return void 0;
23168
23168
  }
23169
+ function rowRank(kind) {
23170
+ switch (kind) {
23171
+ case "accepted":
23172
+ return 4;
23173
+ case "audit-escalation":
23174
+ return 3;
23175
+ case "correctable-rejection":
23176
+ case "infrastructure":
23177
+ return 2;
23178
+ case "candidate":
23179
+ return 1;
23180
+ }
23181
+ }
23182
+ function submissionCallKey(attemptId, toolCallId) {
23183
+ return `${attemptId ?? ""}\0${toolCallId}`;
23184
+ }
23185
+ function rowFromPayload(kind, payload, accepted, roleFallback) {
23186
+ const role = recordedRole(payload) ?? roleFallback;
23187
+ return {
23188
+ ...role === void 0 ? {} : { role },
23189
+ kind,
23190
+ accepted,
23191
+ ...typeof payload.toolCallId === "string" && payload.toolCallId.length > 0 ? { toolCallId: payload.toolCallId } : {}
23192
+ };
23193
+ }
23169
23194
  async function readRecordedSubmissionRows(cwd, runId, homeOrScope) {
23170
23195
  const scope = resolveReadScope(homeOrScope);
23171
23196
  const { owned } = await readOwnedSubmissionRecords(cwd, runId, scope.home);
23172
23197
  const scoped = recordsForAttempt(owned, scope.attemptId);
23173
23198
  const out = [];
23199
+ const indexByCall = /* @__PURE__ */ new Map();
23200
+ const roleByCall = /* @__PURE__ */ new Map();
23201
+ for (const record4 of scoped) {
23202
+ const payload = record4.payload;
23203
+ if (typeof payload?.toolCallId !== "string" || payload.toolCallId.length === 0) continue;
23204
+ const role = recordedRole(payload);
23205
+ if (role !== void 0) {
23206
+ roleByCall.set(submissionCallKey(recordAttemptId(record4), payload.toolCallId), role);
23207
+ }
23208
+ }
23209
+ const take = (row, callKey) => {
23210
+ const toolCallId = row.toolCallId;
23211
+ if (toolCallId !== void 0 && callKey !== void 0) {
23212
+ const existingIndex = indexByCall.get(callKey);
23213
+ if (existingIndex !== void 0) {
23214
+ const existing = out[existingIndex];
23215
+ if (rowRank(row.kind) >= rowRank(existing.kind)) {
23216
+ out[existingIndex] = {
23217
+ ...row,
23218
+ toolCallId,
23219
+ // Keep a previously recovered role when the upgraded row still omits it.
23220
+ ...row.role === void 0 && existing.role !== void 0 ? { role: existing.role } : {}
23221
+ };
23222
+ } else if (existing.role === void 0 && row.role !== void 0) {
23223
+ out[existingIndex] = { ...existing, role: row.role };
23224
+ }
23225
+ return;
23226
+ }
23227
+ indexByCall.set(callKey, out.length);
23228
+ }
23229
+ out.push(row);
23230
+ };
23174
23231
  for (const record4 of scoped) {
23232
+ const attemptId = recordAttemptId(record4);
23233
+ if (record4.kind === "candidate") {
23234
+ const payload2 = record4.payload;
23235
+ if (payload2?.type !== "candidate" || payload2.params === void 0) continue;
23236
+ const callKey2 = typeof payload2.toolCallId === "string" ? submissionCallKey(attemptId, payload2.toolCallId) : void 0;
23237
+ const fallback2 = callKey2 !== void 0 ? roleByCall.get(callKey2) : void 0;
23238
+ take(rowFromPayload("candidate", payload2, payload2.params, fallback2), callKey2);
23239
+ continue;
23240
+ }
23175
23241
  if (record4.kind === "sealed") {
23176
23242
  const payload2 = record4.payload;
23177
23243
  if (payload2?.type !== "sealed" || payload2.accepted === void 0) continue;
23178
- const role2 = recordedRole(payload2);
23179
- if (role2 === void 0) continue;
23180
- out.push({ role: role2, kind: "accepted", accepted: payload2.accepted });
23244
+ const callKey2 = typeof payload2.toolCallId === "string" ? submissionCallKey(attemptId, payload2.toolCallId) : void 0;
23245
+ const fallback2 = callKey2 !== void 0 ? roleByCall.get(callKey2) : void 0;
23246
+ take(rowFromPayload("accepted", payload2, payload2.accepted, fallback2), callKey2);
23181
23247
  continue;
23182
23248
  }
23183
23249
  if (record4.kind !== "outcome") continue;
23184
23250
  const payload = record4.payload;
23185
- if (payload?.type !== "outcome" || payload.outcome !== "audit-escalation" || payload.accepted === void 0) {
23186
- continue;
23187
- }
23188
- const role = recordedRole(payload);
23189
- if (role === void 0) continue;
23190
- out.push({ role, kind: "audit-escalation", accepted: payload.accepted });
23251
+ if (payload?.type !== "outcome" || payload.accepted === void 0) continue;
23252
+ const outcome = payload.outcome;
23253
+ const kind = outcome === "audit-escalation" ? "audit-escalation" : outcome === "correctable-rejection" ? "correctable-rejection" : outcome === "infrastructure" ? "infrastructure" : void 0;
23254
+ if (kind === void 0) continue;
23255
+ const callKey = typeof payload.toolCallId === "string" ? submissionCallKey(attemptId, payload.toolCallId) : void 0;
23256
+ const fallback = callKey !== void 0 ? roleByCall.get(callKey) : void 0;
23257
+ take(rowFromPayload(kind, payload, payload.accepted, fallback), callKey);
23191
23258
  }
23192
23259
  return out;
23193
23260
  }
@@ -23391,7 +23458,7 @@ function knownFailureFromProviderStop(input) {
23391
23458
  const diagnostic = nonEmptyString(input.errorMessage);
23392
23459
  const details = sessionStopDetails(input);
23393
23460
  return {
23394
- cause: hasUpstreamErrorTestimony(input) ? "provider" : "unrecognized",
23461
+ ...hasUpstreamErrorTestimony(input) ? { cause: "provider" } : {},
23395
23462
  ...diagnostic === void 0 ? {} : { diagnostic },
23396
23463
  ...Object.keys(details).length === 0 ? {} : { details }
23397
23464
  };
@@ -23756,15 +23823,6 @@ function isLawfulTypedTerminalOutcome(outcome) {
23756
23823
  function exitCodeForTerminalOutcome(outcome) {
23757
23824
  return isLawfulTypedTerminalOutcome(outcome) ? 0 : 1;
23758
23825
  }
23759
- function lastRolePayloadRecord(payloads) {
23760
- for (let index = payloads.length - 1; index >= 0; index -= 1) {
23761
- const payload = payloads[index];
23762
- if (typeof payload === "object" && payload !== null && !Array.isArray(payload)) {
23763
- return payload;
23764
- }
23765
- }
23766
- return void 0;
23767
- }
23768
23826
  function recommendationNavigatorFact(input) {
23769
23827
  const command = typeof input.modelCommand === "string" && input.modelCommand.trim() !== "" ? input.modelCommand : isPublicCallableRole2(input.next.role) ? renderPublicAkRoleCommand(input.next) : void 0;
23770
23828
  return {
@@ -23779,7 +23837,7 @@ function recommendationNavigatorFact(input) {
23779
23837
  function formatTerminalResult(result2) {
23780
23838
  const lines = [];
23781
23839
  lines.push("role outcome status");
23782
- const outcomeStatus = result2.roleOutcome.kind === "failure" ? result2.roleOutcome.cause : result2.roleOutcome.kind === "accepted" ? "accepted" : result2.roleOutcome.status;
23840
+ const outcomeStatus = result2.roleOutcome.kind === "failure" ? result2.roleOutcome.cause ?? "" : result2.roleOutcome.kind === "accepted" ? "accepted" : result2.roleOutcome.status;
23783
23841
  lines.push(
23784
23842
  `${result2.roleOutcome.role} ${result2.roleOutcome.kind} ${encodeTerminalField(outcomeStatus)}`
23785
23843
  );
@@ -23865,9 +23923,12 @@ function ledgerReadScope(admitted, scope) {
23865
23923
  }
23866
23924
  function roleOutcomeFromRows(role, rows) {
23867
23925
  const mine = rows.filter((row) => row.role === role);
23868
- if (mine.length === 0) return void 0;
23926
+ const terminal = mine.filter(
23927
+ (row) => row.kind === "accepted" || row.kind === "audit-escalation"
23928
+ );
23929
+ if (terminal.length === 0) return void 0;
23869
23930
  const payloads = mine.map((row) => row.accepted);
23870
- if (mine.some((row) => row.kind === "audit-escalation")) {
23931
+ if (terminal.some((row) => row.kind === "audit-escalation")) {
23871
23932
  return { kind: "audit_escalation", role, status: "audit_escalation", payloads };
23872
23933
  }
23873
23934
  return { kind: "accepted", role, payloads };
@@ -23899,7 +23960,7 @@ async function recordedSubmissionPayloads(admitted, scope) {
23899
23960
  function withSubmissions(terminal, submissions) {
23900
23961
  if (submissions.length === 0) return terminal;
23901
23962
  const roleOutcome = terminal.roleOutcome;
23902
- 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;
23963
+ const withPayloads = roleOutcome.kind === "accepted" || roleOutcome.kind === "audit_escalation" || roleOutcome.kind === "failure" ? { ...roleOutcome, payloads: submissions } : roleOutcome;
23903
23964
  return { ...terminal, roleOutcome: withPayloads, submissions };
23904
23965
  }
23905
23966
  async function attachRecordedSubmissions(admitted, terminal, scope) {
@@ -24000,7 +24061,7 @@ function thrownIdentity(error) {
24000
24061
  function isTypedActivationError(error) {
24001
24062
  if (!(error instanceof Error)) return false;
24002
24063
  const cause = error.knownCause;
24003
- return cause === "provider" || cause === "activation" || cause === "session" || cause === "output" || cause === "timeout" || cause === "unrecognized";
24064
+ return cause === "provider" || cause === "activation" || cause === "session" || cause === "output" || cause === "timeout";
24004
24065
  }
24005
24066
  function flattenThrownFailureLeaves(error) {
24006
24067
  if (!(error instanceof AggregateError)) {
@@ -24020,7 +24081,7 @@ function projectThrownFailureLeaf(error) {
24020
24081
  }
24021
24082
  return {
24022
24083
  cause: error.knownCause,
24023
- diagnostic: error.message || error.name || "unrecognized exception",
24084
+ diagnostic: error.message || error.name || "exception",
24024
24085
  identity,
24025
24086
  ...error.details === void 0 ? {} : { details: error.details }
24026
24087
  };
@@ -24028,13 +24089,11 @@ function projectThrownFailureLeaf(error) {
24028
24089
  if (error instanceof Error) {
24029
24090
  const identity = thrownIdentity(error);
24030
24091
  return {
24031
- cause: "unrecognized",
24032
- diagnostic: error.message || error.name || "unrecognized exception",
24092
+ diagnostic: error.message || error.name || "exception",
24033
24093
  identity
24034
24094
  };
24035
24095
  }
24036
24096
  return {
24037
- cause: "unrecognized",
24038
24097
  diagnostic: String(error)
24039
24098
  };
24040
24099
  }
@@ -24056,7 +24115,7 @@ function classifyThrownFailure(error) {
24056
24115
  ...leaves.slice(1).map((leaf) => {
24057
24116
  const secondary = projectThrownFailureLeaf(leaf);
24058
24117
  return {
24059
- cause: secondary.cause,
24118
+ ...secondary.cause === void 0 ? {} : { cause: secondary.cause },
24060
24119
  diagnostic: secondary.diagnostic,
24061
24120
  ...secondary.identity === void 0 ? {} : { identity: secondary.identity },
24062
24121
  ...secondary.details === void 0 ? {} : { details: secondary.details }
@@ -24064,7 +24123,7 @@ function classifyThrownFailure(error) {
24064
24123
  })
24065
24124
  ];
24066
24125
  return {
24067
- cause: primary.cause,
24126
+ ...primary.cause === void 0 ? {} : { cause: primary.cause },
24068
24127
  diagnostic: primary.diagnostic,
24069
24128
  ...primary.identity === void 0 ? {} : { identity: primary.identity },
24070
24129
  details: {
@@ -24105,6 +24164,24 @@ function classifyPostAdmissionFailure(input) {
24105
24164
  ...input.knownIdentity === void 0 ? {} : { identity: input.knownIdentity }
24106
24165
  };
24107
24166
  }
24167
+ if (input.knownDiagnostic !== void 0 && input.knownDiagnostic.trim() !== "" || input.knownIdentity !== void 0) {
24168
+ const diagnostic = input.knownDiagnostic !== void 0 && input.knownDiagnostic.trim() !== "" ? input.knownDiagnostic : conciseChildDiagnostic(input.stderr, "role run failed");
24169
+ const { timedOut: _knownTimedOut, ...knownDetails } = input.knownDetails ?? {};
24170
+ const remoteCode = knownDetails.code;
24171
+ return withKnownDetails(
24172
+ {
24173
+ diagnostic,
24174
+ details: {
24175
+ ...knownDetails,
24176
+ ...remoteCode === void 0 ? {} : { code: remoteCode },
24177
+ exitCode: input.code,
24178
+ ...input.timedOut ? { timedOut: true } : {}
24179
+ },
24180
+ ...input.knownIdentity === void 0 ? {} : { identity: input.knownIdentity }
24181
+ },
24182
+ void 0
24183
+ );
24184
+ }
24108
24185
  if (input.timedOut) {
24109
24186
  return withKnownDetails(
24110
24187
  {
@@ -24158,7 +24235,7 @@ function classifyPostAdmissionFailure(input) {
24158
24235
  function explicitInternalKnownFailureClassificationInput(failure) {
24159
24236
  if (failure === void 0) return {};
24160
24237
  return {
24161
- knownCause: failure.cause,
24238
+ ...failure.cause === void 0 ? {} : { knownCause: failure.cause },
24162
24239
  ...failure.identity === void 0 ? {} : { knownIdentity: failure.identity },
24163
24240
  ...failure.diagnostic === void 0 ? {} : { knownDiagnostic: failure.diagnostic },
24164
24241
  ...failure.details === void 0 ? {} : { knownDetails: failure.details }
@@ -24410,10 +24487,12 @@ function complianceFailureFromAuditorVolumes(volumes) {
24410
24487
  if (entry?.type !== "custom" || entry.customType !== AUDITOR_COMPLIANCE_FAILURE_ENTRY_TYPE || !isRecord9(entry.data)) continue;
24411
24488
  const parent = isRecord9(entry.data.parent) ? entry.data.parent : void 0;
24412
24489
  const failure = isRecord9(entry.data.failure) ? entry.data.failure : void 0;
24413
- if (parent?.sessionId !== parentId || parent.sessionFile !== sessionFile || parent.attemptEntryId !== attemptEntryId || failure?.cause !== "provider" && failure?.cause !== "unrecognized") continue;
24490
+ if (parent?.sessionId !== parentId || parent.sessionFile !== sessionFile || parent.attemptEntryId !== attemptEntryId) continue;
24491
+ if (failure === void 0) continue;
24414
24492
  const identity = isRecord9(failure.identity) ? failure.identity : void 0;
24493
+ const typedCause = failure.cause === "provider" || failure.cause === "activation" || failure.cause === "session" || failure.cause === "output" || failure.cause === "timeout" ? failure.cause : void 0;
24415
24494
  return {
24416
- cause: failure.cause === "provider" ? "provider" : "unrecognized",
24495
+ ...typedCause === void 0 ? {} : { cause: typedCause },
24417
24496
  ...identity === void 0 ? {} : { identity: {
24418
24497
  ...typeof identity.name === "string" ? { name: identity.name } : {},
24419
24498
  ...typeof identity.code === "string" || typeof identity.code === "number" ? { code: identity.code } : {}
@@ -25017,8 +25096,7 @@ async function publishCoderArtifacts(admitted, roleOutcome, coordinates, options
25017
25096
  role: "coder",
25018
25097
  runId: admitted.runId,
25019
25098
  phase: admitted.phase,
25020
- outcome: roleOutcome,
25021
- ...options.coderOutput === void 0 ? {} : { receipt: options.coderOutput }
25099
+ outcome: roleOutcome
25022
25100
  },
25023
25101
  null,
25024
25102
  2
@@ -25095,7 +25173,6 @@ async function settleLawfulCoderTerminalResult(admitted, authority, options = {}
25095
25173
  const ledgerOutcome = await closedLedgerOutcome(admitted, "coder", scope);
25096
25174
  if (ledgerOutcome === void 0) return void 0;
25097
25175
  const roleOutcome = ledgerOutcome;
25098
- const output = ledgerOutcome.kind === "accepted" ? lastRolePayloadRecord(ledgerOutcome.payloads ?? []) : void 0;
25099
25176
  const coordinates = coordinatesFromAdmitted(authority, admitted);
25100
25177
  const entries = await readLawfulSettlementEntries(coordinates.sessionFile) ?? [];
25101
25178
  const navigator = extractNavigatorFact(entries);
@@ -25104,7 +25181,6 @@ async function settleLawfulCoderTerminalResult(admitted, authority, options = {}
25104
25181
  roleOutcome,
25105
25182
  coordinates,
25106
25183
  {
25107
- ...output === void 0 ? {} : { coderOutput: output },
25108
25184
  ...options.methodProvenance === void 0 ? {} : { methodProvenance: options.methodProvenance }
25109
25185
  }
25110
25186
  );
@@ -25158,8 +25234,7 @@ async function publishFixerArtifacts(admitted, roleOutcome, coordinates, options
25158
25234
  role: "fixer",
25159
25235
  runId: admitted.runId,
25160
25236
  phase: admitted.phase,
25161
- outcome: roleOutcome,
25162
- ...options.fixerOutput === void 0 ? {} : { receipt: options.fixerOutput }
25237
+ outcome: roleOutcome
25163
25238
  },
25164
25239
  null,
25165
25240
  2
@@ -25206,7 +25281,6 @@ async function settleLawfulFixerTerminalResult(admitted, authority, options, sco
25206
25281
  const ledgerOutcome = await closedLedgerOutcome(admitted, "fixer", scope);
25207
25282
  if (ledgerOutcome === void 0) return void 0;
25208
25283
  const roleOutcome = ledgerOutcome;
25209
- const output = ledgerOutcome.kind === "accepted" ? lastRolePayloadRecord(ledgerOutcome.payloads ?? []) : void 0;
25210
25284
  const coordinates = coordinatesFromAdmitted(authority, admitted);
25211
25285
  const { sessionDirectory, sessionFile } = coordinates;
25212
25286
  const entries = await readLawfulSettlementEntries(sessionFile) ?? [];
@@ -25222,7 +25296,6 @@ async function settleLawfulFixerTerminalResult(admitted, authority, options, sco
25222
25296
  roleOutcome,
25223
25297
  coordinates,
25224
25298
  {
25225
- ...output === void 0 ? {} : { fixerOutput: output },
25226
25299
  methodProvenance: options.methodProvenance,
25227
25300
  methodInvocations
25228
25301
  }
@@ -25237,7 +25310,7 @@ async function settleLawfulFixerTerminalResult(admitted, authority, options, sco
25237
25310
  sessionDirectory
25238
25311
  );
25239
25312
  }
25240
- async function publishCollectorArtifacts(admitted, roleOutcome, coordinates, options = {}) {
25313
+ async function publishCollectorArtifacts(admitted, roleOutcome, coordinates) {
25241
25314
  await appendRunAttemptHistory({ role: admitted.role, runId: admitted.runId, sessionFile: coordinates.sessionFile }, roleOutcome);
25242
25315
  const artifactsDir = await ensureRunArtifactsDir(admitted.runDirectory);
25243
25316
  const reportPath = join21(artifactsDir, "report.json");
@@ -25248,8 +25321,7 @@ async function publishCollectorArtifacts(admitted, roleOutcome, coordinates, opt
25248
25321
  {
25249
25322
  role: "collector",
25250
25323
  runId: admitted.runId,
25251
- outcome: roleOutcome,
25252
- ...options.collectorReceipt === void 0 ? {} : { receipt: options.collectorReceipt }
25324
+ outcome: roleOutcome
25253
25325
  },
25254
25326
  null,
25255
25327
  2
@@ -25310,14 +25382,12 @@ async function settleLawfulCollectorTerminalResult(admitted, authority, scope) {
25310
25382
  }
25311
25383
  return void 0;
25312
25384
  }
25313
- const receipt = lastRolePayloadRecord(roleOutcome.payloads ?? []) ?? {};
25314
25385
  const accepted = roleOutcome;
25315
25386
  const navigator = extractNavigatorFact(entries);
25316
25387
  const artifacts = await publishCollectorArtifacts(
25317
25388
  admitted,
25318
25389
  accepted,
25319
- coordinates,
25320
- { collectorReceipt: receipt }
25390
+ coordinates
25321
25391
  );
25322
25392
  return attachRecordedSubmissions(
25323
25393
  admitted,
@@ -25370,8 +25440,6 @@ async function publishDoctorArtifacts(admitted, roleOutcome, coordinates, option
25370
25440
  role: "doctor",
25371
25441
  runId: admitted.runId,
25372
25442
  outcome: roleOutcome,
25373
- ...options.doctorOutput === void 0 ? {} : { receipt: options.doctorOutput },
25374
- // Independent machine fields beside the receipt — not merged into it.
25375
25443
  ...options.cost === void 0 ? {} : { cost: options.cost },
25376
25444
  ...options.auditNoReceipt === void 0 ? {} : { auditNoReceipt: options.auditNoReceipt }
25377
25445
  },
@@ -25429,7 +25497,6 @@ async function settleLawfulDoctorTerminalResult(admitted, authority, scope) {
25429
25497
  sessionDirectory
25430
25498
  );
25431
25499
  }
25432
- const output = lastRolePayloadRecord(sealed.payloads ?? []) ?? {};
25433
25500
  const roleOutcome = sealed;
25434
25501
  const navigator = extractNavigatorFact(entries);
25435
25502
  const cost = extractDoctorCandidateCostFact(entries);
@@ -25439,7 +25506,6 @@ async function settleLawfulDoctorTerminalResult(admitted, authority, scope) {
25439
25506
  roleOutcome,
25440
25507
  coordinates,
25441
25508
  {
25442
- doctorOutput: output,
25443
25509
  ...cost === void 0 ? {} : { cost },
25444
25510
  ...auditNoReceipt === void 0 ? {} : { auditNoReceipt }
25445
25511
  }
@@ -25629,8 +25695,7 @@ async function publishReviewerArtifacts(admitted, roleOutcome, coordinates, opti
25629
25695
  {
25630
25696
  role: "reviewer",
25631
25697
  runId: admitted.runId,
25632
- outcome: roleOutcome,
25633
- ...options.reviewerReceipt === void 0 ? {} : { receipt: options.reviewerReceipt }
25698
+ outcome: roleOutcome
25634
25699
  },
25635
25700
  null,
25636
25701
  2
@@ -25678,7 +25743,6 @@ async function settleLawfulReviewerTerminalResult(admitted, authority, options,
25678
25743
  const coordinates = coordinatesFromAdmitted(authority, admitted);
25679
25744
  const { sessionDirectory, sessionFile } = coordinates;
25680
25745
  const entries = await readLawfulSettlementEntries(sessionFile) ?? [];
25681
- const receipt = lastRolePayloadRecord(sealed.payloads ?? []);
25682
25746
  const roleOutcome = sealed;
25683
25747
  const navigator = extractNavigatorFact(entries);
25684
25748
  const methodInvocations = extractReviewerMethodInvocations(entries, {
@@ -25692,7 +25756,6 @@ async function settleLawfulReviewerTerminalResult(admitted, authority, options,
25692
25756
  roleOutcome,
25693
25757
  coordinates,
25694
25758
  {
25695
- ...receipt === void 0 ? {} : { reviewerReceipt: receipt },
25696
25759
  methodProvenance: options.methodProvenance,
25697
25760
  methodInvocations
25698
25761
  }
@@ -25737,8 +25800,7 @@ async function publishMergerArtifacts(admitted, roleOutcome, coordinates, option
25737
25800
  {
25738
25801
  role: "merger",
25739
25802
  runId: admitted.runId,
25740
- outcome: roleOutcome,
25741
- ...options.mergerOutput === void 0 ? {} : { receipt: options.mergerOutput }
25803
+ outcome: roleOutcome
25742
25804
  },
25743
25805
  null,
25744
25806
  2
@@ -25810,7 +25872,6 @@ async function settleLawfulMergerTerminalResult(admitted, authority, options, sc
25810
25872
  accepted,
25811
25873
  coordinates,
25812
25874
  {
25813
- mergerOutput: lastRolePayloadRecord(accepted.payloads ?? []) ?? {},
25814
25875
  methodProvenance: options.methodProvenance,
25815
25876
  methodInvocations
25816
25877
  }
@@ -25943,7 +26004,7 @@ async function publishFailureArtifacts(admitted, failure, authority) {
25943
26004
  kind: "error",
25944
26005
  role: admitted.role,
25945
26006
  runId: admitted.runId,
25946
- cause: failure.cause,
26007
+ ...failure.cause === void 0 ? {} : { cause: failure.cause },
25947
26008
  diagnostic: failure.diagnostic,
25948
26009
  ...failure.identity === void 0 ? {} : { identity: failure.identity },
25949
26010
  ...failure.details === void 0 ? {} : { details: failure.details }
@@ -25966,7 +26027,7 @@ async function publishFailureArtifacts(admitted, failure, authority) {
25966
26027
  sha256: a.sha256,
25967
26028
  byteLength: a.byteLength
25968
26029
  })),
25969
- failureCause: failure.cause
26030
+ ...failure.cause === void 0 ? {} : { failureCause: failure.cause }
25970
26031
  };
25971
26032
  const evidenceWrite = await writeFailureJsonRetainingCause(
25972
26033
  evidenceCandidates,
@@ -26030,7 +26091,7 @@ async function settleFailureTerminalResult(admitted, failure, authority, options
26030
26091
  const navigator = await extractNavigatorFactFromAdmittedSession(sessionFile);
26031
26092
  const artifacts = await publishFailureArtifacts(admitted, failure, authority);
26032
26093
  const decisiveFacts = {
26033
- cause: failure.cause,
26094
+ ...failure.cause === void 0 ? {} : { cause: failure.cause },
26034
26095
  diagnostic: failure.diagnostic
26035
26096
  };
26036
26097
  if (failure.identity?.name !== void 0) {
@@ -26046,7 +26107,7 @@ async function settleFailureTerminalResult(admitted, failure, authority, options
26046
26107
  const roleOutcome2 = {
26047
26108
  kind: "failure",
26048
26109
  role: admitted.role,
26049
- cause: failure.cause,
26110
+ ...failure.cause === void 0 ? {} : { cause: failure.cause },
26050
26111
  diagnostic: failure.diagnostic,
26051
26112
  decisiveFacts
26052
26113
  };
@@ -26063,7 +26124,7 @@ async function settleFailureTerminalResult(admitted, failure, authority, options
26063
26124
  const roleOutcome = {
26064
26125
  kind: "failure",
26065
26126
  role: admitted.role,
26066
- cause: failure.cause,
26127
+ ...failure.cause === void 0 ? {} : { cause: failure.cause },
26067
26128
  diagnostic: failure.diagnostic,
26068
26129
  decisiveFacts
26069
26130
  };
@@ -26084,7 +26145,7 @@ function presentFailureTerminal(terminal, io) {
26084
26145
  io.stdout(formatTerminalResult(terminal));
26085
26146
  if (terminal.roleOutcome.kind === "failure") {
26086
26147
  io.stderr(formatFailureStderrDiagnostic({
26087
- cause: terminal.roleOutcome.cause,
26148
+ ...terminal.roleOutcome.cause === void 0 ? {} : { cause: terminal.roleOutcome.cause },
26088
26149
  diagnostic: terminal.roleOutcome.diagnostic
26089
26150
  }));
26090
26151
  return;
@@ -26572,12 +26633,29 @@ function unwrapTurnDispatchedFailure(error) {
26572
26633
  }
26573
26634
  return current;
26574
26635
  }
26636
+ async function attachDispatchExceptionTerminal(admitted, terminal, io) {
26637
+ try {
26638
+ return await attachRecordedSubmissions(
26639
+ {
26640
+ projectRoot: admitted.projectRoot,
26641
+ runId: admitted.runId,
26642
+ runDirectory: admitted.runDirectory
26643
+ },
26644
+ terminal
26645
+ );
26646
+ } catch (error) {
26647
+ io.stderr(
26648
+ `dispatch exception ledger attach failed (best-effort continue): ${describeErrorIdentity(error)}
26649
+ `
26650
+ );
26651
+ return terminal;
26652
+ }
26653
+ }
26575
26654
  function dispatchExceptionFailureTerminal(input) {
26576
26655
  const causeError = unwrapTurnDispatchedFailure(input.causeError);
26577
26656
  const history = input.everyAttemptThrew ? "dispatch threw an exception on every attempt" : "the final dispatch threw an exception";
26578
26657
  const diagnostic = `${history} (${input.endReason}; resumes used ${input.autoResumeAttempts}); last cause: ${describeErrorIdentity(causeError)}`;
26579
26658
  const decisiveFacts = {
26580
- cause: "unrecognized",
26581
26659
  diagnostic,
26582
26660
  resumesUsed: input.autoResumeAttempts,
26583
26661
  dispatchErrorFiles: [...input.errorFiles]
@@ -26598,7 +26676,6 @@ function dispatchExceptionFailureTerminal(input) {
26598
26676
  roleOutcome: {
26599
26677
  kind: "failure",
26600
26678
  role: input.role,
26601
- cause: "unrecognized",
26602
26679
  diagnostic,
26603
26680
  decisiveFacts
26604
26681
  },
@@ -26698,15 +26775,19 @@ async function runWithAutoResumeLoop(options) {
26698
26775
  }
26699
26776
  } else {
26700
26777
  if (autoResumeAttempts >= limit) {
26701
- const terminal = dispatchExceptionFailureTerminal({
26702
- role: options.admitted.role,
26703
- runId: options.admitted.runId,
26704
- causeError: lastThrownError,
26705
- errorFiles: retainedErrorFiles,
26706
- autoResumeAttempts,
26707
- endReason: "auto-resume budget exhausted",
26708
- everyAttemptThrew
26709
- });
26778
+ const terminal = await attachDispatchExceptionTerminal(
26779
+ options.admitted,
26780
+ dispatchExceptionFailureTerminal({
26781
+ role: options.admitted.role,
26782
+ runId: options.admitted.runId,
26783
+ causeError: lastThrownError,
26784
+ errorFiles: retainedErrorFiles,
26785
+ autoResumeAttempts,
26786
+ endReason: "auto-resume budget exhausted",
26787
+ everyAttemptThrew
26788
+ }),
26789
+ options.io
26790
+ );
26710
26791
  await finalizeExceptionRunBestEffort(options.admitted.runDirectory, options.io);
26711
26792
  presentTerminal(terminal, options.io);
26712
26793
  return {
@@ -26715,15 +26796,19 @@ async function runWithAutoResumeLoop(options) {
26715
26796
  };
26716
26797
  }
26717
26798
  if (!await isPrincipalAvailable(options.admitted.principal)) {
26718
- const terminal = dispatchExceptionFailureTerminal({
26719
- role: options.admitted.role,
26720
- runId: options.admitted.runId,
26721
- causeError: lastThrownError,
26722
- errorFiles: retainedErrorFiles,
26723
- autoResumeAttempts,
26724
- endReason: "session principal unavailable before further resume",
26725
- everyAttemptThrew
26726
- });
26799
+ const terminal = await attachDispatchExceptionTerminal(
26800
+ options.admitted,
26801
+ dispatchExceptionFailureTerminal({
26802
+ role: options.admitted.role,
26803
+ runId: options.admitted.runId,
26804
+ causeError: lastThrownError,
26805
+ errorFiles: retainedErrorFiles,
26806
+ autoResumeAttempts,
26807
+ endReason: "session principal unavailable before further resume",
26808
+ everyAttemptThrew
26809
+ }),
26810
+ options.io
26811
+ );
26727
26812
  await finalizeExceptionRunBestEffort(options.admitted.runDirectory, options.io);
26728
26813
  presentTerminal(terminal, options.io);
26729
26814
  return {
@@ -27862,7 +27947,7 @@ function instructionSeatAdapters(options) {
27862
27947
  }) => {
27863
27948
  const infrastructureFailure = await readEngineDetourInfrastructureFailure(sessionFile);
27864
27949
  return infrastructureFailure === void 0 ? result2.knownFailure : {
27865
- cause: infrastructureFailure.cause,
27950
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
27866
27951
  diagnostic: infrastructureFailure.diagnostic,
27867
27952
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
27868
27953
  };
@@ -28169,7 +28254,7 @@ function collectorAdapters() {
28169
28254
  resolveRunnerKnownFailure: async ({ result: result2, sessionFile }) => {
28170
28255
  const infrastructureFailure = await readCollectorInfrastructureFailure(sessionFile);
28171
28256
  return result2.knownFailure ?? (infrastructureFailure === void 0 ? void 0 : {
28172
- cause: infrastructureFailure.cause,
28257
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
28173
28258
  diagnostic: infrastructureFailure.diagnostic,
28174
28259
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
28175
28260
  });
@@ -28560,7 +28645,7 @@ function judgeAdapters() {
28560
28645
  resolveRunnerKnownFailure: async ({ result: result2, sessionFile }) => {
28561
28646
  const infrastructureFailure = await readEngineDetourInfrastructureFailure(sessionFile);
28562
28647
  return result2.knownFailure ?? (infrastructureFailure === void 0 ? void 0 : {
28563
- cause: infrastructureFailure.cause,
28648
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
28564
28649
  diagnostic: infrastructureFailure.diagnostic,
28565
28650
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
28566
28651
  });
@@ -28872,15 +28957,11 @@ async function runPublicDiarist(argv, env, io, parseDiaristArgv2) {
28872
28957
  ...env.engine === void 0 ? {} : { effectiveEngine: env.engine }
28873
28958
  }).then(async (result2) => {
28874
28959
  if (admitted.ticketNumber === void 0 && result2.admitted !== void 0) {
28875
- const roleOutcome = result2.terminal?.roleOutcome;
28876
- const facts = roleOutcome?.kind === "accepted" ? lastRolePayloadRecord(roleOutcome.payloads ?? []) : void 0;
28877
- if (roleOutcome !== void 0 && roleOutcome.kind === "accepted" && facts?.status !== "escalate") {
28878
- 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;
28879
- if (typeof raw === "number" && Number.isSafeInteger(raw) && raw >= 1) {
28880
- admitted.ticketNumber = raw;
28881
- if (result2.admitted.ticketNumber === void 0) {
28882
- result2.admitted.ticketNumber = raw;
28883
- }
28960
+ const fromPages = await readRunTicketNumber(admitted.runDirectory);
28961
+ if (fromPages !== void 0) {
28962
+ await bindAdmittedTicketNumber(admitted, fromPages);
28963
+ if (result2.admitted.ticketNumber === void 0) {
28964
+ result2.admitted.ticketNumber = fromPages;
28884
28965
  }
28885
28966
  }
28886
28967
  }
@@ -28922,8 +29003,8 @@ var init_diarist_run = __esm({
28922
29003
  init_run_lifecycle();
28923
29004
  init_seat_ticket_binding();
28924
29005
  init_settlement();
28925
- init_terminal();
28926
29006
  init_turn_request();
29007
+ init_run_ticket_number();
28927
29008
  }
28928
29009
  });
28929
29010
 
@@ -29261,6 +29342,16 @@ function buildCountersignTurnRequest(admitted, options) {
29261
29342
  options
29262
29343
  );
29263
29344
  }
29345
+ function courtDiaristEscalated(roleOutcome) {
29346
+ if (roleOutcome === void 0) return false;
29347
+ if (roleOutcome.kind === "audit_escalation") return true;
29348
+ if (roleOutcome.kind !== "accepted") return false;
29349
+ return (roleOutcome.payloads ?? []).some((payload) => {
29350
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return false;
29351
+ const record4 = payload;
29352
+ return record4.status === "escalate" || record4.countersignStatus === "escalate";
29353
+ });
29354
+ }
29264
29355
  async function invokeCourtDiarist(input, env, io) {
29265
29356
  const quietIo = {
29266
29357
  stdout() {
@@ -29286,13 +29377,10 @@ async function invokeCourtDiarist(input, env, io) {
29286
29377
  ...env.hostAdapters === void 0 ? {} : { hostAdapters: env.hostAdapters }
29287
29378
  });
29288
29379
  const roleOutcome = result2.terminal?.roleOutcome;
29289
- if (roleOutcome !== void 0 && (roleOutcome.kind === "accepted" || roleOutcome.kind === "audit_escalation")) {
29290
- const facts = lastRolePayloadRecord(roleOutcome.payloads ?? []);
29291
- const status = typeof facts?.status === "string" ? facts.status : typeof facts?.countersignStatus === "string" ? facts.countersignStatus : roleOutcome.kind === "audit_escalation" ? "escalate" : void 0;
29292
- if (status === "escalate") {
29293
- const reason = typeof facts?.reason === "string" ? facts.reason : "diarist escalated without reason";
29294
- return { identity: { kind: "escalate", reason } };
29295
- }
29380
+ if (courtDiaristEscalated(roleOutcome)) {
29381
+ return {
29382
+ identity: { kind: "escalate" }
29383
+ };
29296
29384
  }
29297
29385
  if (result2.exitCode !== 0) {
29298
29386
  const diagnostic = roleOutcome?.kind === "failure" ? roleOutcome.diagnostic : result2.stderr?.trim() || `exit ${result2.exitCode}`;
@@ -29305,9 +29393,13 @@ async function invokeCourtDiarist(input, env, io) {
29305
29393
  }
29306
29394
  const asserted = result2.admitted?.ticketNumber;
29307
29395
  if (typeof asserted === "number" && Number.isSafeInteger(asserted) && asserted >= 1) {
29308
- return { identity: { kind: "ticket", ticketNumber: asserted } };
29396
+ return {
29397
+ identity: { kind: "ticket", ticketNumber: asserted }
29398
+ };
29309
29399
  }
29310
- return { identity: { kind: "unbound" } };
29400
+ return {
29401
+ identity: { kind: "unbound" }
29402
+ };
29311
29403
  }
29312
29404
  async function runCountersignCourtDiaristStation(admitted, env, io) {
29313
29405
  if (env.runCourtDiaristStation !== void 0) {
@@ -29328,7 +29420,7 @@ async function runCountersignCourtDiaristStation(admitted, env, io) {
29328
29420
  );
29329
29421
  if (outcome.identity.kind === "escalate") {
29330
29422
  throw new StationChildExhaustedError(
29331
- `court diarist station escalated (cannot identify court target): ${outcome.identity.reason}`
29423
+ "court diarist station escalated (cannot identify court target)"
29332
29424
  );
29333
29425
  }
29334
29426
  if (outcome.failedWithoutEscalate !== void 0) {
@@ -29388,7 +29480,7 @@ async function runPublicCountersign(argv, env, io, parseCountersignArgv2) {
29388
29480
  code: null,
29389
29481
  stderr: "",
29390
29482
  thrown: new Error(
29391
- `court diarist station escalated (cannot identify court target): ${outcome.identity.reason}`
29483
+ "court diarist station escalated (cannot identify court target)"
29392
29484
  )
29393
29485
  },
29394
29486
  countersignAdapters(),
@@ -29534,7 +29626,6 @@ var init_countersign_run = __esm({
29534
29626
  init_run_lifecycle();
29535
29627
  init_seat_ticket_binding();
29536
29628
  init_settlement();
29537
- init_terminal();
29538
29629
  init_turn_request();
29539
29630
  }
29540
29631
  });
@@ -30019,7 +30110,7 @@ function reviewerAdapters(packageRoot2, methodMaterial) {
30019
30110
  resolveRunnerKnownFailure: async ({ result: result2, sessionFile }) => {
30020
30111
  const infrastructureFailure = await readEngineDetourInfrastructureFailure(sessionFile);
30021
30112
  return infrastructureFailure === void 0 ? result2.knownFailure : {
30022
- cause: infrastructureFailure.cause,
30113
+ ...infrastructureFailure.cause === void 0 ? {} : { cause: infrastructureFailure.cause },
30023
30114
  diagnostic: infrastructureFailure.diagnostic,
30024
30115
  ...infrastructureFailure.identity === void 0 ? {} : { identity: infrastructureFailure.identity }
30025
30116
  };
@@ -33296,10 +33387,10 @@ async function runAkRole(argv, env) {
33296
33387
  if (error.cause !== void 0) {
33297
33388
  const detail = formatErrorCauseDetail(error.cause);
33298
33389
  if (detail.trim().length > 0) {
33299
- label = `${label || error.name || "unrecognized exception"}; cause: ${detail}`;
33390
+ label = `${label || error.name || "exception"}; cause: ${detail}`;
33300
33391
  }
33301
33392
  }
33302
- io.stderr(formatCliDiagnostic(label || error.name || "unrecognized exception"));
33393
+ io.stderr(formatCliDiagnostic(label || error.name || "exception"));
33303
33394
  return { exitCode: 1 };
33304
33395
  }
33305
33396
  io.stderr(formatCliDiagnostic(String(error)));