@akagilnc/pi-workflow-roles 0.1.5382 → 0.1.5406

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +2 -0
  2. package/README.zh-CN.md +2 -0
  3. package/dist/acp-host/production-host.js +2523 -1880
  4. package/dist/archivist-record-pointer.js +38 -0
  5. package/dist/compliance-transport.js +5 -0
  6. package/dist/doctor-auditor.js +1 -0
  7. package/dist/engine-detour-usage.js +6 -6
  8. package/dist/headless-host/production-host.js +1097 -454
  9. package/dist/judge-role.js +14 -1
  10. package/dist/migrate-book-topology.js +190 -74
  11. package/dist/navigator-attendance.js +10 -9
  12. package/dist/navigator-invocation-identity.js +18 -0
  13. package/dist/pi/role-turn-host.js +9 -0
  14. package/dist/public-cli/cli.js +1 -0
  15. package/dist/public-cli/instruction-seat-run.js +250 -44
  16. package/dist/public-cli/main.js +929 -323
  17. package/dist/public-cli/post-admission.js +427 -244
  18. package/dist/public-cli/settlement.js +111 -48
  19. package/dist/public-cli/terminal.js +12 -1
  20. package/dist/public-cli/turn-request.js +3 -0
  21. package/dist/receipt-delivery-policy.js +152 -17
  22. package/dist/submission-errors.js +11 -1
  23. package/dist/submission-gate.js +27 -4
  24. package/package.json +1 -1
  25. package/src/archivist-record-pointer.ts +48 -0
  26. package/src/compliance-transport.ts +10 -2
  27. package/src/doctor-auditor.ts +3 -0
  28. package/src/engine-detour-usage.ts +7 -6
  29. package/src/external-host-turn-loop.ts +24 -12
  30. package/src/gatekeeper-role.ts +2 -2
  31. package/src/host-contracts.ts +7 -0
  32. package/src/judge-role.ts +24 -5
  33. package/src/navigator-attendance.ts +17 -12
  34. package/src/navigator-invocation-identity.ts +31 -0
  35. package/src/pi/adapter.ts +33 -8
  36. package/src/pi/role-turn-host.ts +7 -0
  37. package/src/public-cli/cli.ts +1 -0
  38. package/src/public-cli/instruction-seat-run.ts +283 -49
  39. package/src/public-cli/post-admission.ts +556 -324
  40. package/src/public-cli/settlement.ts +112 -50
  41. package/src/public-cli/terminal.ts +16 -4
  42. package/src/public-cli/turn-request.ts +10 -1
  43. package/src/receipt-delivery-policy.ts +209 -17
  44. package/src/role-envelope.ts +5 -1
  45. package/src/role-runtime-dependencies.ts +1 -0
  46. package/src/role-runtime.ts +65 -21
  47. package/src/submission-correctable-error.ts +5 -2
  48. package/src/submission-errors.ts +12 -1
  49. package/src/submission-gate.ts +34 -4
  50. package/src/worker-role.ts +19 -7
  51. package/src/worker-submission-gates.ts +29 -7
@@ -27,7 +27,7 @@ import { REVIEWER_OUTPUT_TOOL_NAME, } from "../package-contracts/reviewer-output
27
27
  import { packagedAuditToolName, packagedDurableOfficerEntry, packagedRoleAcceptedOutputTool, packagedRoleMetadata, packagedSkipsGateOnInfrastructureStage, } from "../packaged-role-registry.js";
28
28
  import { SECRETARIAT_COUNTERSIGN_TERMINAL_FACT_KEY, SECRETARIAT_GATE_OFFICER_ENTRY_TYPE, } from "../secretariat-contracts.js";
29
29
  import { classifyPackagedRoleTerminalResult, findLatestDurablePackagedRoleTerminal, hasNavigatorInfrastructureFailureBase, isAcceptedPackagedRoleTerminalResult, NAVIGATOR_ROUTE_PLAYBOOK_FAILURE_ENTRY, } from "../navigator-invocation-identity.js";
30
- import { NO_RECEIPT_LIFECYCLE_ENTRY_TYPE, RECEIPT_DELIVERY_TURN_LIMIT, noReceiptLifecycleFacts, parseNoReceiptLifecycleFacts, } from "../receipt-delivery-policy.js";
30
+ import { NO_RECEIPT_LIFECYCLE_ENTRY_TYPE, noReceiptLifecycleFacts, parseNoReceiptLifecycleFacts, priorReceiptContinuation, receiptAttemptPointer, } from "../receipt-delivery-policy.js";
31
31
  import { roleRunArtifactsDirectory } from "../role-run-placement.js";
32
32
  import { rewriteRunDirectoryPathValue } from "../role-run-relocation.js";
33
33
  import { listSeamOwnedUniqueErrorFacePaths, RUN_TERMINAL_ARTIFACT_FILES, RUN_TERMINAL_ERROR_FALLBACK_RELATIVE_PATHS, } from "../run-terminal-artifacts.js";
@@ -139,13 +139,100 @@ export async function attachRecordedSubmissions(admitted, terminal, scope) {
139
139
  * Does not invent an output failure for an empty ledger.
140
140
  * #953: empty public terminal face — clear every reader-adoptable prior face.
141
141
  */
142
- export async function settleHostEndedNoReceipt(admitted, authority, scope) {
142
+ /**
143
+ * Lifecycle fact the in-process runtime already wrote for this run and attempt.
144
+ * Absent, malformed, or a previous attempt's bytes are not a count.
145
+ */
146
+ function lifecycleFactsFromEntry(entry) {
147
+ const customType = entry.customType ?? entry.message?.customType;
148
+ if (customType !== NO_RECEIPT_LIFECYCLE_ENTRY_TYPE)
149
+ return undefined;
150
+ return entry.data ?? entry.message?.details;
151
+ }
152
+ async function readCurrentAttemptNoReceiptFacts(admitted, authority, scope) {
153
+ const { sessionFile } = coordinatesFromAdmitted(authority, admitted);
154
+ const entries = await readBoundSessionEntries(sessionFile).catch(() => undefined);
155
+ if (entries === undefined)
156
+ return undefined;
157
+ const scopeId = scope?.invocationScopeId?.trim() ?? "";
158
+ if (scopeId.length > 0) {
159
+ const pointer = receiptAttemptPointer(admitted.runDirectory, scopeId);
160
+ let selected;
161
+ for (const entry of entries) {
162
+ const raw = lifecycleFactsFromEntry(entry);
163
+ if (raw === undefined)
164
+ continue;
165
+ try {
166
+ const facts = parseNoReceiptLifecycleFacts(raw);
167
+ if (facts.runPointer !== admitted.runDirectory)
168
+ continue;
169
+ if (facts.attemptPointer !== pointer)
170
+ continue;
171
+ if (selected === undefined || facts.deliveryTurns >= selected.deliveryTurns) {
172
+ selected = facts;
173
+ }
174
+ }
175
+ catch {
176
+ // A malformed historical entry is not this call's count.
177
+ }
178
+ }
179
+ return selected;
180
+ }
181
+ let attemptStart = 0;
182
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
183
+ if (entries[index]?.type === "message" && entries[index]?.message?.role === "user") {
184
+ attemptStart = index;
185
+ break;
186
+ }
187
+ }
188
+ const lifecycleEntry = entries.slice(attemptStart).reverse().find((entry) => lifecycleFactsFromEntry(entry) !== undefined);
189
+ const raw = lifecycleEntry === undefined ? undefined : lifecycleFactsFromEntry(lifecycleEntry);
190
+ if (raw === undefined)
191
+ return undefined;
192
+ let facts;
193
+ try {
194
+ facts = parseNoReceiptLifecycleFacts(raw);
195
+ }
196
+ catch {
197
+ return undefined;
198
+ }
199
+ if (facts.runPointer !== admitted.runDirectory)
200
+ return undefined;
201
+ if (facts.attemptPointer !== receiptAttemptPointer(admitted.runDirectory))
202
+ return undefined;
203
+ return facts;
204
+ }
205
+ export async function settleHostEndedNoReceipt(admitted, authority, scope,
206
+ /**
207
+ * #1132: delivery requests this run actually issued. Zero stays zero when
208
+ * nothing was sent and this attempt has no lifecycle fact. A fact already
209
+ * written for this run and attempt supplies the count when it is larger.
210
+ * The budget is never the count, and another loop's resumes are not催交.
211
+ */
212
+ issuedDeliveryRequests = 0) {
213
+ const scopeId = scope?.invocationScopeId?.trim() ?? "";
214
+ const persisted = scopeId.length > 0
215
+ ? undefined
216
+ : await readCurrentAttemptNoReceiptFacts(admitted, authority, scope);
217
+ let terminalToolCalled = persisted?.terminalToolCalled ?? false;
218
+ let rejectedReceipts = persisted?.rejectedReceipts ?? [];
219
+ let recordedTurns = persisted?.deliveryTurns ?? 0;
220
+ if (scopeId.length > 0) {
221
+ const { sessionFile } = coordinatesFromAdmitted(authority, admitted);
222
+ const entries = await readBoundSessionEntries(sessionFile).catch(() => undefined);
223
+ if (entries !== undefined) {
224
+ const prior = priorReceiptContinuation(entries, scopeId);
225
+ terminalToolCalled = prior.terminalToolCalled;
226
+ rejectedReceipts = prior.rejectedReceipts;
227
+ recordedTurns = prior.deliveryTurns;
228
+ }
229
+ }
143
230
  const facts = noReceiptLifecycleFacts({
144
- terminalToolCalled: false,
145
- rejectedReceipts: [],
146
- deliveryTurns: RECEIPT_DELIVERY_TURN_LIMIT,
231
+ terminalToolCalled,
232
+ rejectedReceipts,
233
+ deliveryTurns: Math.max(issuedDeliveryRequests, recordedTurns),
147
234
  runPointer: admitted.runDirectory,
148
- attemptPointer: `current:${admitted.runDirectory}`,
235
+ attemptPointer: receiptAttemptPointer(admitted.runDirectory, scope?.invocationScopeId),
149
236
  });
150
237
  const coordinates = coordinatesFromAdmitted(authority, admitted);
151
238
  await clearOppositeTerminalArtifactFace(admitted.runDirectory);
@@ -2286,48 +2373,24 @@ export async function settleFailureTerminalResult(admitted, failure, authority,
2286
2373
  // current-attempt fact. Transcript reconstruction must not turn arbitrary output
2287
2374
  // failures (or bytes retained from a prior resume attempt) into exit zero.
2288
2375
  if (failure.cause === "output") {
2289
- const entries = await readBoundSessionEntries(sessionFile).catch(() => undefined);
2290
- if (entries !== undefined) {
2291
- let attemptStart = 0;
2292
- for (let index = entries.length - 1; index >= 0; index -= 1) {
2293
- if (entries[index]?.type === "message" && entries[index]?.message?.role === "user") {
2294
- attemptStart = index;
2295
- break;
2296
- }
2297
- }
2298
- const lifecycleEntry = entries.slice(attemptStart).reverse().find((entry) => entry.customType === NO_RECEIPT_LIFECYCLE_ENTRY_TYPE || entry.message?.customType === NO_RECEIPT_LIFECYCLE_ENTRY_TYPE);
2299
- const raw = lifecycleEntry?.data ?? lifecycleEntry?.message?.details;
2300
- if (raw !== undefined) {
2301
- // Catch only covers lifecycle-byte parse. Clear / navigator / gate I/O
2302
- // after a valid parse must keep their real failure identity (#953).
2303
- let facts;
2304
- try {
2305
- facts = parseNoReceiptLifecycleFacts(raw);
2306
- }
2307
- catch {
2308
- /* malformed lifecycle bytes remain the existing nonzero output failure */
2309
- }
2310
- if (facts !== undefined &&
2311
- facts.runPointer === admitted.runDirectory &&
2312
- facts.attemptPointer === `current:${admitted.runDirectory}`) {
2313
- let decisiveFacts = facts;
2314
- // #478: no_receipt is still a public Terminal — project accepted gate facts.
2315
- // #953: empty public terminal face — clear every reader-adoptable prior face.
2316
- await clearOppositeTerminalArtifactFace(admitted.runDirectory);
2317
- return withOptionalGateProjection({
2318
- roleOutcome: {
2319
- kind: "no_receipt",
2320
- role: admitted.role,
2321
- status: "no-accepted-receipt",
2322
- ...facts,
2323
- decisiveFacts,
2324
- },
2325
- navigator: await extractNavigatorFactFromAdmittedSession(sessionFile),
2326
- artifacts: [],
2327
- runId: admitted.runId,
2328
- }, sessionDirectory, detourGateContext(admitted, options));
2329
- }
2330
- }
2376
+ const facts = await readCurrentAttemptNoReceiptFacts(admitted, authority, options);
2377
+ if (facts !== undefined) {
2378
+ const decisiveFacts = facts;
2379
+ // #478: no_receipt is still a public Terminal — project accepted gate facts.
2380
+ // #953: empty public terminal face — clear every reader-adoptable prior face.
2381
+ await clearOppositeTerminalArtifactFace(admitted.runDirectory);
2382
+ return withOptionalGateProjection({
2383
+ roleOutcome: {
2384
+ kind: "no_receipt",
2385
+ role: admitted.role,
2386
+ status: "no-accepted-receipt",
2387
+ ...facts,
2388
+ decisiveFacts,
2389
+ },
2390
+ navigator: await extractNavigatorFactFromAdmittedSession(sessionFile),
2391
+ artifacts: [],
2392
+ runId: admitted.runId,
2393
+ }, sessionDirectory, detourGateContext(admitted, options));
2331
2394
  }
2332
2395
  }
2333
2396
  // Exact-session attendance only — never infer no-advice from caller omission.
@@ -69,6 +69,17 @@ export function formatTerminalResult(result) {
69
69
  if (result.roleOutcome.kind === "failure") {
70
70
  lines.push(`diagnostic\t${encodeTerminalField(result.roleOutcome.diagnostic)}`);
71
71
  }
72
+ // Accepted volumes do not dump decisiveFacts unless a failed attempt is
73
+ // recorded. The unsettled-direction fact still has to be visible on its own.
74
+ // audit_escalation already dumps every decisiveFact below.
75
+ if (result.roleOutcome.kind === "accepted"
76
+ && result.roleOutcome.decisiveFacts?.directionUnsettled === true) {
77
+ lines.push("fact\tdirectionUnsettled\ttrue");
78
+ const subsequent = result.roleOutcome.decisiveFacts.subsequentAudit;
79
+ if (typeof subsequent === "string") {
80
+ lines.push(`fact\tsubsequentAudit\t${encodeTerminalField(subsequent)}`);
81
+ }
82
+ }
72
83
  if (result.roleOutcome.kind === "failure" || result.roleOutcome.kind === "no_receipt" || result.roleOutcome.kind === "audit_escalation" || result.roleOutcome.decisiveFacts?.failedAttempts !== undefined) {
73
84
  const facts = result.roleOutcome.kind === "accepted"
74
85
  ? { failedAttempts: result.roleOutcome.decisiveFacts?.failedAttempts }
@@ -140,7 +151,7 @@ export function formatTerminalResult(result) {
140
151
  else {
141
152
  const payloads = result.roleOutcome.kind === "accepted" ||
142
153
  result.roleOutcome.kind === "audit_escalation"
143
- ? coalesceSubmissionRows(result.roleOutcome.payloads, result.submissions)
154
+ ? coalesceSubmissionRows(result.submissions, result.roleOutcome.payloads)
144
155
  : result.submissions ?? [];
145
156
  for (let i = payloads.length - 1; i >= 0; i -= 1) {
146
157
  const payload = payloads[i];
@@ -46,6 +46,9 @@ export function projectRoleTurnRequest(admitted, roleDetails, options) {
46
46
  ? {}
47
47
  : { invocationScopeId: options.invocationScopeId }),
48
48
  ...(options.stationChild === undefined ? {} : { stationChild: options.stationChild }),
49
+ ...(options.deliveryRequestLimit === undefined
50
+ ? {}
51
+ : { deliveryRequestLimit: options.deliveryRequestLimit }),
49
52
  };
50
53
  }
51
54
  /**
@@ -1,8 +1,33 @@
1
+ import { AUTO_RESUME_LIMIT } from "./public-cli/run-lifecycle.js";
2
+ import { parseAutoResumeLimit } from "./public-cli/config.js";
1
3
  import { isRecord } from "./unknown-value.js";
2
- const RECEIPT_DELIVERY_TURN_LIMIT = 2;
3
4
  const NO_RECEIPT_LIFECYCLE_ENTRY_TYPE = "ak-no-receipt-lifecycle";
5
+ const RECEIPT_DELIVERY_REQUEST_ENTRY = "ak-receipt-delivery-request";
6
+ const RECEIPT_REJECTION_ENTRY = "ak-receipt-rejection";
7
+ function receiptAttemptPointer(runPointer, invocationScopeId) {
8
+ const scope = invocationScopeId?.trim() ?? "";
9
+ if (scope.length > 0) return `invocation:${scope}`;
10
+ return `current:${runPointer}`;
11
+ }
12
+ const RECEIPT_DELIVERY_LIMIT_ENV = "AK_ROLE_RECEIPT_DELIVERY_LIMIT";
13
+ function deliveryLimitFromConfig(value) {
14
+ return parseAutoResumeLimit(value ?? AUTO_RESUME_LIMIT);
15
+ }
16
+ function deliveryLimitFromEnv(env) {
17
+ const raw = env[RECEIPT_DELIVERY_LIMIT_ENV];
18
+ if (raw === void 0 || raw.trim() === "") return deliveryLimitFromConfig(void 0);
19
+ const parsed = Number(raw);
20
+ if (!Number.isInteger(parsed)) return deliveryLimitFromConfig(void 0);
21
+ return deliveryLimitFromConfig(parsed);
22
+ }
23
+ function deliveryTurnsFact(value) {
24
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
25
+ throw new TypeError("deliveryTurns must be a non-negative integer");
26
+ }
27
+ return value;
28
+ }
4
29
  function parseNoReceiptLifecycleFacts(input) {
5
- if (!isRecord(input) || typeof input.terminalToolCalled !== "boolean" || input.deliveryTurns !== RECEIPT_DELIVERY_TURN_LIMIT || input.sessionCompletion !== "settled-without-accepted-receipt" || input.acceptedReceipt !== false || typeof input.runPointer !== "string" || input.runPointer.trim() === "" || typeof input.attemptPointer !== "string" || input.attemptPointer.trim() === "" || !Array.isArray(input.rejectedReceipts) || !input.rejectedReceipts.every((item) => isRecord(item) && typeof item.reason === "string")) {
30
+ if (!isRecord(input) || typeof input.terminalToolCalled !== "boolean" || input.sessionCompletion !== "settled-without-accepted-receipt" || input.acceptedReceipt !== false || typeof input.runPointer !== "string" || input.runPointer.trim() === "" || typeof input.attemptPointer !== "string" || input.attemptPointer.trim() === "" || !Array.isArray(input.rejectedReceipts) || !input.rejectedReceipts.every((item) => isRecord(item) && typeof item.reason === "string")) {
6
31
  throw new TypeError("malformed no-receipt lifecycle facts");
7
32
  }
8
33
  return {
@@ -11,7 +36,7 @@ function parseNoReceiptLifecycleFacts(input) {
11
36
  reason: item.reason,
12
37
  diagnosticAvailable: item.reason.trim() !== ""
13
38
  })),
14
- deliveryTurns: RECEIPT_DELIVERY_TURN_LIMIT,
39
+ deliveryTurns: deliveryTurnsFact(input.deliveryTurns),
15
40
  sessionCompletion: "settled-without-accepted-receipt",
16
41
  runPointer: input.runPointer,
17
42
  attemptPointer: input.attemptPointer,
@@ -19,28 +44,33 @@ function parseNoReceiptLifecycleFacts(input) {
19
44
  };
20
45
  }
21
46
  function noReceiptLifecycleFacts(input) {
22
- if (input.deliveryTurns !== RECEIPT_DELIVERY_TURN_LIMIT) {
23
- throw new TypeError("no-receipt lifecycle requires an exhausted delivery budget");
24
- }
25
47
  return {
26
48
  terminalToolCalled: input.terminalToolCalled,
27
49
  rejectedReceipts: input.rejectedReceipts.map(({ reason }) => ({
28
50
  reason,
29
51
  diagnosticAvailable: reason.trim() !== ""
30
52
  })),
31
- deliveryTurns: RECEIPT_DELIVERY_TURN_LIMIT,
53
+ deliveryTurns: deliveryTurnsFact(input.deliveryTurns),
32
54
  sessionCompletion: "settled-without-accepted-receipt",
33
55
  runPointer: input.runPointer,
34
56
  attemptPointer: input.attemptPointer,
35
57
  acceptedReceipt: false
36
58
  };
37
59
  }
38
- function createReceiptDeliveryPolicy() {
60
+ function createReceiptDeliveryPolicy(limit) {
61
+ const deliveryLimit = deliveryLimitFromConfig(limit);
39
62
  let accepted = false;
63
+ let closed = false;
40
64
  let terminalToolCalled = false;
41
65
  let deliveryTurns = 0;
66
+ let budgetSpent = 0;
42
67
  const rejectedReceipts = [];
68
+ const spend = () => {
69
+ if (budgetSpent < deliveryLimit) budgetSpent += 1;
70
+ };
43
71
  return {
72
+ /** The effective ceiling this budget spends (#1132 — never re-read). */
73
+ limit: deliveryLimit,
44
74
  recordAccepted() {
45
75
  accepted = true;
46
76
  terminalToolCalled = true;
@@ -49,27 +79,46 @@ function createReceiptDeliveryPolicy() {
49
79
  stopForInfrastructure() {
50
80
  accepted = true;
51
81
  },
82
+ /**
83
+ * Record the rejection this turn already produced.
84
+ * The fact does not spend a budget. The seam that sends the next prompt
85
+ * owns that count.
86
+ */
52
87
  recordRejected(reason) {
53
88
  terminalToolCalled = true;
54
89
  rejectedReceipts.push({ reason, diagnosticAvailable: reason.trim() !== "" });
55
- deliveryTurns = Math.min(RECEIPT_DELIVERY_TURN_LIMIT, deliveryTurns + 1);
56
90
  },
57
91
  recordDeliveryRequest() {
58
- deliveryTurns = Math.min(RECEIPT_DELIVERY_TURN_LIMIT, deliveryTurns + 1);
92
+ deliveryTurns += 1;
93
+ spend();
59
94
  },
60
95
  /**
61
- * A nested session settled without an accepted receipt after spending its own
62
- * delivery budget. Adopt its facts and close this budget: another prompt would
63
- * open an independent session, not a delivery request on the settled one.
96
+ * Stop asking. The issued count stays whatever was actually sent (#1132 —
97
+ * do not fill the remainder up to the ceiling).
98
+ */
99
+ closeBudget() {
100
+ closed = true;
101
+ },
102
+ /**
103
+ * A nested session settled without an accepted receipt. Close so this layer
104
+ * does not open another prompt on that session. The issued count stays the
105
+ * larger of the two seams: a nested zero must not wipe prompts this layer
106
+ * already sent, and adding the two counts would bill one send twice.
64
107
  */
65
108
  recordNestedNoReceipt(facts) {
66
109
  terminalToolCalled = terminalToolCalled || facts.terminalToolCalled;
67
110
  rejectedReceipts.push(...facts.rejectedReceipts);
68
- deliveryTurns = RECEIPT_DELIVERY_TURN_LIMIT;
111
+ deliveryTurns = Math.max(deliveryTurns, facts.deliveryTurns);
112
+ closed = true;
69
113
  },
70
114
  nextAction() {
71
115
  if (accepted) return "accepted";
72
- return deliveryTurns < RECEIPT_DELIVERY_TURN_LIMIT ? "request-delivery" : "no-receipt";
116
+ if (closed) return "no-receipt";
117
+ return budgetSpent < deliveryLimit ? "request-delivery" : "no-receipt";
118
+ },
119
+ /** Delivery requests actually issued so far (#1132). */
120
+ issuedDeliveryRequests() {
121
+ return deliveryTurns;
73
122
  },
74
123
  /** Current typed delivery facts. No prose. */
75
124
  deliveryState() {
@@ -82,13 +131,99 @@ function createReceiptDeliveryPolicy() {
82
131
  },
83
132
  facts(binding) {
84
133
  return noReceiptLifecycleFacts({ terminalToolCalled, rejectedReceipts: [...rejectedReceipts], deliveryTurns, ...binding });
134
+ },
135
+ /**
136
+ * Continue a public call after the in-memory policy was rebuilt.
137
+ * Sends already issued spend the same budget; copied rejections keep
138
+ * the fact and do not spend again.
139
+ */
140
+ continueIssued(prior) {
141
+ for (const receipt of prior.rejectedReceipts) {
142
+ this.recordRejected(receipt.reason);
143
+ }
144
+ if (prior.terminalToolCalled === true) terminalToolCalled = true;
145
+ for (let index = 0; index < prior.deliveryTurns; index += 1) {
146
+ this.recordDeliveryRequest();
147
+ }
85
148
  }
86
149
  };
87
150
  }
151
+ function entryRecord(entry) {
152
+ return isRecord(entry) ? entry : void 0;
153
+ }
154
+ function customTypeOf(record, message) {
155
+ return record.customType ?? message?.customType;
156
+ }
157
+ function dataOf(record, message) {
158
+ return record.data ?? message?.details;
159
+ }
160
+ function scopeTag(record) {
161
+ const message = isRecord(record.message) ? record.message : void 0;
162
+ const customType = customTypeOf(record, message);
163
+ const data = dataOf(record, message);
164
+ if ((customType === RECEIPT_DELIVERY_REQUEST_ENTRY || customType === RECEIPT_REJECTION_ENTRY) && isRecord(data) && typeof data.invocationScopeId === "string" && data.invocationScopeId.trim() !== "") {
165
+ return data.invocationScopeId;
166
+ }
167
+ if (customType !== NO_RECEIPT_LIFECYCLE_ENTRY_TYPE) return void 0;
168
+ try {
169
+ const facts = parseNoReceiptLifecycleFacts(data);
170
+ const prefix = "invocation:";
171
+ if (!facts.attemptPointer.startsWith(prefix)) return void 0;
172
+ const scope = facts.attemptPointer.slice(prefix.length);
173
+ return scope.trim() === "" ? void 0 : scope;
174
+ } catch {
175
+ return void 0;
176
+ }
177
+ }
178
+ function priorReceiptContinuation(entries, invocationScopeId) {
179
+ const pointer = receiptAttemptPointer("", invocationScopeId);
180
+ let entryTurns = 0;
181
+ let factTurns = 0;
182
+ let rejectedReceipts = [];
183
+ let snapshotTerminal = false;
184
+ for (const entry of entries) {
185
+ const record = entryRecord(entry);
186
+ if (record === void 0) continue;
187
+ const tagged = scopeTag(record);
188
+ if (tagged !== void 0) {
189
+ const message = isRecord(record.message) ? record.message : void 0;
190
+ const customType = customTypeOf(record, message);
191
+ if (tagged !== invocationScopeId) continue;
192
+ if (customType === RECEIPT_DELIVERY_REQUEST_ENTRY) entryTurns += 1;
193
+ if (customType === RECEIPT_REJECTION_ENTRY) {
194
+ const data = dataOf(record, message);
195
+ if (isRecord(data) && typeof data.reason === "string") rejectedReceipts.push({ reason: data.reason });
196
+ }
197
+ if (customType !== NO_RECEIPT_LIFECYCLE_ENTRY_TYPE) continue;
198
+ try {
199
+ const facts = parseNoReceiptLifecycleFacts(dataOf(record, message));
200
+ if (facts.attemptPointer !== pointer) continue;
201
+ factTurns = Math.max(factTurns, facts.deliveryTurns);
202
+ if (facts.rejectedReceipts.length >= rejectedReceipts.length) {
203
+ rejectedReceipts = facts.rejectedReceipts.map(({ reason }) => ({ reason }));
204
+ }
205
+ snapshotTerminal ||= facts.terminalToolCalled;
206
+ } catch {
207
+ }
208
+ continue;
209
+ }
210
+ }
211
+ return {
212
+ deliveryTurns: Math.max(entryTurns, factTurns),
213
+ rejectedReceipts,
214
+ terminalToolCalled: snapshotTerminal || rejectedReceipts.length > 0
215
+ };
216
+ }
88
217
  export {
89
218
  NO_RECEIPT_LIFECYCLE_ENTRY_TYPE,
90
- RECEIPT_DELIVERY_TURN_LIMIT,
219
+ RECEIPT_DELIVERY_LIMIT_ENV,
220
+ RECEIPT_DELIVERY_REQUEST_ENTRY,
221
+ RECEIPT_REJECTION_ENTRY,
91
222
  createReceiptDeliveryPolicy,
223
+ deliveryLimitFromConfig,
224
+ deliveryLimitFromEnv,
92
225
  noReceiptLifecycleFacts,
93
- parseNoReceiptLifecycleFacts
226
+ parseNoReceiptLifecycleFacts,
227
+ priorReceiptContinuation,
228
+ receiptAttemptPointer
94
229
  };
@@ -44,10 +44,20 @@ export class WorkerPrefixReminderError extends Error {
44
44
  this.name = "WorkerPrefixReminderError";
45
45
  }
46
46
  }
47
+ /**
48
+ * ADR 0066/0070 one-shot commit and prefix reminders.
49
+ * They stay a correction the same session may make, and they do not occupy
50
+ * the configured re-ask or 催交 budget (#1132).
51
+ */
52
+ export function isOneShotWorkerReminderCode(code) {
53
+ return code === "worker_commit_reminder" || code === "worker_prefix_reminder";
54
+ }
47
55
  export class WorkerUnfinishedReasonReminderError extends Error {
48
56
  code = "worker_unfinished_reason_reminder";
49
57
  constructor() {
50
- super("本次 unfinished 回执未含 reason;本接缝缺由至多打回两次。");
58
+ // The ceiling is the configured unfinished-reason limit. This text reaches
59
+ // the model and does not restate a count.
60
+ super("本次 unfinished 回执未含 reason;请补上 reason 后再交。");
51
61
  this.name = "WorkerUnfinishedReasonReminderError";
52
62
  }
53
63
  }
@@ -1,6 +1,7 @@
1
1
  import { bookDirectOfficerRunPointer } from "./archivist-record-pointer.js";
2
2
  import { GatekeeperDecisionError, officerConclusionReask, projectGatekeeperRun, } from "./gatekeeper-role.js";
3
3
  import { receivedDiscriminator } from "./submission-errors.js";
4
+ import { deliveryLimitFromConfig } from "./receipt-delivery-policy.js";
4
5
  /** Received discriminator of the latest payload, retained verbatim for re-ask. */
5
6
  export function latestQueueStatus(terminal) {
6
7
  const outcome = terminal?.roleOutcome;
@@ -54,7 +55,7 @@ export function createDefaultGateOfficerSummon(options) {
54
55
  * Book a typed pointer under parent session/auditor-roles (archivist-owned write).
55
56
  * Offline mocks without a real session leave no nested volume (lawful zero).
56
57
  */
57
- function bookDirectOfficerPointer(context, officer, result, summoned) {
58
+ function bookDirectOfficerPointer(context, officer, result, summoned, toolCallId) {
58
59
  if (result.status !== "converged"
59
60
  && result.status !== "continue"
60
61
  && result.status !== "escalate"
@@ -77,16 +78,20 @@ function bookDirectOfficerPointer(context, officer, result, summoned) {
77
78
  ...(typeof summoned.runDirectory === "string" && summoned.runDirectory.trim() !== ""
78
79
  ? { runDirectory: summoned.runDirectory }
79
80
  : {}),
81
+ ...(toolCallId.trim() === "" ? {} : { submissionToolCallId: toolCallId }),
80
82
  });
81
83
  }
82
84
  /**
83
85
  * Shared envelope: project gate, book officer pointer, map onto host actions.
84
- * Review loop has no round cap (#753 no-round-cap).
86
+ * A real converged / continue / escalate returns immediately. An unreadable
87
+ * conclusion reasks that officer against the configured ceiling; exhaustion
88
+ * returns the receipt actually received.
85
89
  * On converged, returns the officer snapshot (receipt + nested runId) for seat projection.
86
90
  */
87
91
  export async function requireSubmissionGate(options) {
88
92
  let reask;
89
- // No round cap — end only on converged, continue/escalate, or real failure.
93
+ let reasksSpent = 0;
94
+ const reaskLimit = deliveryLimitFromConfig(options.autoResumeLimit);
90
95
  const summonOfficer = options.summonOfficer ??
91
96
  createDefaultGateOfficerSummon({
92
97
  cwd: options.context.cwd ?? process.cwd(),
@@ -104,7 +109,7 @@ export async function requireSubmissionGate(options) {
104
109
  // Envelope-owned pointer book. Failure is host infrastructure — single face.
105
110
  if (projected.summoned !== undefined) {
106
111
  try {
107
- bookDirectOfficerPointer(options.context, projected.officer, gatekeeper, projected.summoned);
112
+ bookDirectOfficerPointer(options.context, projected.officer, gatekeeper, projected.summoned, options.toolCallId);
108
113
  }
109
114
  catch (error) {
110
115
  options.hostActions.failInfrastructure(error, options.context, options.toolCallId);
@@ -131,6 +136,24 @@ export async function requireSubmissionGate(options) {
131
136
  };
132
137
  }
133
138
  if (gatekeeper.status === "needs_reask") {
139
+ if (reasksSpent >= reaskLimit) {
140
+ return {
141
+ status: "needs_reask",
142
+ officer: projected.officer,
143
+ receipt: gatekeeper.receipt,
144
+ ...(typeof gatekeeper.runId === "string" && gatekeeper.runId.trim() !== ""
145
+ ? { runId: gatekeeper.runId }
146
+ : {}),
147
+ ...(typeof projected.summoned?.runDirectory === "string"
148
+ && projected.summoned.runDirectory.trim() !== ""
149
+ ? { runDirectory: projected.summoned.runDirectory }
150
+ : {}),
151
+ ...(projected.summoned?.terminal === undefined
152
+ ? {}
153
+ : { terminal: projected.summoned.terminal }),
154
+ };
155
+ }
156
+ reasksSpent += 1;
134
157
  reask = officerConclusionReask(gatekeeper.receivedStatus);
135
158
  continue;
136
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akagilnc/pi-workflow-roles",
3
- "version": "0.1.5382",
3
+ "version": "0.1.5406",
4
4
  "description": "Soul-bound workflow roles for Pi",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -1,5 +1,7 @@
1
1
  import { mkdirSync, writeFileSync } from "node:fs";
2
+ import { readFile } from "node:fs/promises";
2
3
  import { dirname, join } from "node:path";
4
+ import { isRecord } from "./unknown-value.ts";
3
5
 
4
6
  /** Typed parent-side pointer to an independent officer run 正本 (ADR 0079 / #675). */
5
7
  export const DIRECT_OFFICER_RUN_POINTER_KIND = "direct-officer-run-pointer" as const;
@@ -12,6 +14,11 @@ export type DirectOfficerRunPointer = {
12
14
  readonly sessionFile: string;
13
15
  /** Officer run directory when known. */
14
16
  readonly runDirectory?: string;
17
+ /**
18
+ * Parent submission tool call this officer run was summoned for.
19
+ * Absent on pointers booked before that binding existed.
20
+ */
21
+ readonly submissionToolCallId?: string;
15
22
  };
16
23
 
17
24
  /**
@@ -28,9 +35,11 @@ export function bookDirectOfficerRunPointer(options: {
28
35
  readonly officer: "inspector" | "notary" | "auditor" | "countersign";
29
36
  readonly sessionFile: string;
30
37
  readonly runDirectory?: string;
38
+ readonly submissionToolCallId?: string;
31
39
  }): DirectOfficerRunPointer {
32
40
  const nest = join(dirname(options.parentSessionFile), "auditor-roles");
33
41
  mkdirSync(nest, { recursive: true });
42
+ const submissionToolCallId = options.submissionToolCallId?.trim() ?? "";
34
43
  const pointer: DirectOfficerRunPointer = {
35
44
  version: 1,
36
45
  kind: DIRECT_OFFICER_RUN_POINTER_KIND,
@@ -39,6 +48,7 @@ export function bookDirectOfficerRunPointer(options: {
39
48
  ...(options.runDirectory !== undefined && options.runDirectory.trim() !== ""
40
49
  ? { runDirectory: options.runDirectory }
41
50
  : {}),
51
+ ...(submissionToolCallId === "" ? {} : { submissionToolCallId }),
42
52
  };
43
53
  writeFileSync(
44
54
  join(nest, `${options.officer}.pointer.json`),
@@ -47,3 +57,41 @@ export function bookDirectOfficerRunPointer(options: {
47
57
  );
48
58
  return pointer;
49
59
  }
60
+
61
+ /**
62
+ * Read the one officer pointer. Missing or unreadable is not a pass:
63
+ * the caller re-runs the gate.
64
+ */
65
+ export async function readDirectOfficerRunPointer(
66
+ parentSessionFile: string,
67
+ officer: DirectOfficerRunPointer["officer"],
68
+ ): Promise<DirectOfficerRunPointer | undefined> {
69
+ try {
70
+ const raw: unknown = JSON.parse(await readFile(
71
+ join(dirname(parentSessionFile), "auditor-roles", `${officer}.pointer.json`),
72
+ "utf8",
73
+ ));
74
+ if (!isRecord(raw) || raw.kind !== DIRECT_OFFICER_RUN_POINTER_KIND || raw.version !== 1) {
75
+ return undefined;
76
+ }
77
+ if (raw.officer !== officer) return undefined;
78
+ if (typeof raw.sessionFile !== "string" || raw.sessionFile.trim() === "") return undefined;
79
+ const runDirectory = typeof raw.runDirectory === "string" && raw.runDirectory.trim() !== ""
80
+ ? raw.runDirectory
81
+ : undefined;
82
+ const submissionToolCallId = typeof raw.submissionToolCallId === "string"
83
+ && raw.submissionToolCallId.trim() !== ""
84
+ ? raw.submissionToolCallId
85
+ : undefined;
86
+ return {
87
+ version: 1,
88
+ kind: DIRECT_OFFICER_RUN_POINTER_KIND,
89
+ officer,
90
+ sessionFile: raw.sessionFile,
91
+ ...(runDirectory === undefined ? {} : { runDirectory }),
92
+ ...(submissionToolCallId === undefined ? {} : { submissionToolCallId }),
93
+ };
94
+ } catch {
95
+ return undefined;
96
+ }
97
+ }