@caupulican/pi-agent-core 0.93.2 → 0.93.4

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.
@@ -1,14 +1,12 @@
1
1
  import { getToolExecutionUnchangedRetryLimit } from "@caupulican/pi-ai/tool-repair-registry";
2
- import { forEachPairedToolResult, getToolExecutionKey, getToolFailureRecordExecutionKey, isClosedOperationFailureCode, isPromptScopedFailureCode, readVisibleToolFailureCode, restoreToolFailureRecord, } from "./tool-failure-memory.js";
2
+ import { forEachPairedToolResult, getToolExecutionKey, getToolExecutionKeyHashParts, getToolFailureRecordExecutionKey, isClosedOperationFailureCode, isPromptScopedFailureCode, readVisibleToolFailureCode, restoreToolFailureRecord, sanitizeToolFailureEvidence, } from "./tool-failure-memory.js";
3
3
  import { isAgentToolFailureRecoveryAuthority } from "./types.js";
4
4
  const MAX_BLOCKED_REPLAYS_PER_FAILURE = 2;
5
5
  const BASE_FAILURE_EXECUTIONS_PER_OPERATION = 1;
6
6
  const MAX_RECOVERY_PROBES_PER_OPERATION = 1;
7
7
  const MAX_REJECTIONS_PER_OPERATION = 4;
8
- export const TOOL_FAILURE_RECOVERY_ACCOUNTING_WAVE_SIZE = 4;
9
- const MAX_FAILURES_PER_FAMILY = TOOL_FAILURE_RECOVERY_ACCOUNTING_WAVE_SIZE;
10
- const MAX_FAILURES_PER_RUN = 12;
11
- const MAX_RECOVERY_STATES = 64;
8
+ const MAX_HOT_RECOVERY_STATES = 64;
9
+ const SEEN_EXECUTION_FILTER_BYTES = 64 * 1024;
12
10
  const MAX_RECOVERY_TARGETS = 8;
13
11
  const MAX_RECOVERY_ACTIONS = 8;
14
12
  const MAX_TARGET_KIND_CHARS = 64;
@@ -16,6 +14,38 @@ const MAX_TARGET_SCOPE_CHARS = 32_768;
16
14
  const MAX_ACTION_INSTRUCTION_CHARS = 160;
17
15
  const MAX_RECOVERY_GUIDANCE_CHARS = 320;
18
16
  const TARGET_KIND_PATTERN = /^[a-z0-9][a-z0-9._:-]*$/;
17
+ /**
18
+ * Bounded negative lookup for exact execution identities.
19
+ *
20
+ * A miss proves the operation has not failed while this gate has been alive. A hit only means
21
+ * "possibly seen" and must be verified against the transcript, so collisions can cost a scan but
22
+ * can never deny an execution. Keeping this separate from the hot state cache lets old exact
23
+ * circuits survive eviction without retaining one live object graph per historical operation.
24
+ */
25
+ class SeenExecutionFilter {
26
+ constructor() {
27
+ this.bits = new Uint8Array(SEEN_EXECUTION_FILTER_BYTES);
28
+ }
29
+ add(executionKey) {
30
+ for (const hash of getToolExecutionKeyHashParts(executionKey))
31
+ this.set(hash);
32
+ }
33
+ mightContain(executionKey) {
34
+ for (const hash of getToolExecutionKeyHashParts(executionKey)) {
35
+ if (!this.has(hash))
36
+ return false;
37
+ }
38
+ return true;
39
+ }
40
+ set(hash) {
41
+ const bit = (hash >>> 0) % (SEEN_EXECUTION_FILTER_BYTES * 8);
42
+ this.bits[bit >>> 3] |= 1 << (bit & 7);
43
+ }
44
+ has(hash) {
45
+ const bit = (hash >>> 0) % (SEEN_EXECUTION_FILTER_BYTES * 8);
46
+ return (this.bits[bit >>> 3] & (1 << (bit & 7))) !== 0;
47
+ }
48
+ }
19
49
  /**
20
50
  * Owns execution admission and bounded unresolved-failure budgets.
21
51
  *
@@ -24,88 +54,77 @@ const TARGET_KIND_PATTERN = /^[a-z0-9][a-z0-9._:-]*$/;
24
54
  * successful repair evidence with byte-exact scope can reopen one probe. Argument text and hooks have
25
55
  * no recovery authority.
26
56
  *
27
- * Run-level halt and family/run counters stay on the current run. Per-operation execution budget and
28
- * circuit are reconstructed from the transcript when a new run starts with an empty gate, so an
29
- * already-exhausted identical operation is not re-executed after a user turn or session resume.
57
+ * Run-level halt stays on the current run, but admission is never denied because unrelated operations
58
+ * happened to fail. A bounded hot cache carries active per-operation state. A fixed-size negative
59
+ * lookup sends an evicted exact replay back to the transcript for authoritative reconstruction, so
60
+ * cache pressure cannot either stop new work or reopen an already-exhausted operation.
30
61
  */
31
62
  export class ToolFailureRecoveryGate {
32
63
  constructor() {
33
64
  this.statesByExecutionKey = new Map();
34
- this.failuresByFamily = new Map();
35
- this.totalFailures = 0;
65
+ this.seenFailedExecutions = new SeenExecutionFilter();
66
+ /** Exact successes not yet present in the transcript snapshot consulted by admission. */
67
+ this.resolvedBeforeTranscriptCommit = new Set();
68
+ this.transcriptMessages = [];
69
+ this.transcriptLength = 0;
70
+ this.restoredFromTranscript = false;
36
71
  }
37
72
  isEmpty() {
38
- return this.statesByExecutionKey.size === 0 && this.halted === undefined && this.totalFailures === 0;
73
+ return this.statesByExecutionKey.size === 0 && this.halted === undefined;
39
74
  }
40
75
  restoreFromMessages(messages) {
41
- if (!this.isEmpty())
76
+ this.trackTranscript(messages);
77
+ if (this.restoredFromTranscript || !this.isEmpty())
42
78
  return;
79
+ this.restoredFromTranscript = true;
43
80
  forEachPairedToolResult(messages, ({ tool, args, executionKey, result }) => {
44
- if (!result.isError) {
45
- const existing = this.statesByExecutionKey.get(executionKey);
46
- if (existing)
47
- this.clearResolvedState(executionKey, existing);
48
- return;
49
- }
50
- const restored = restoreToolFailureRecord(result, tool, args);
51
- const visibleCode = readVisibleToolFailureCode(result);
52
- if (isPromptScopedFailureCode(visibleCode) || isPromptScopedFailureCode(restored.failureCode)) {
81
+ const reduction = this.reduceTranscriptResult(this.statesByExecutionKey.get(executionKey), tool, args, result);
82
+ if (reduction.kind === "resolved") {
83
+ this.clearResolvedState(executionKey);
53
84
  return;
54
85
  }
55
- const state = this.ensureRestoredState(executionKey, restored);
56
- if (!state)
57
- return false;
58
- if (isClosedOperationFailureCode(visibleCode)) {
59
- state.operationCircuitOpen = true;
60
- state.blockedReplays = MAX_BLOCKED_REPLAYS_PER_FAILURE;
61
- state.recoveryAvailable = false;
86
+ if (reduction.kind === "ignored")
62
87
  return;
63
- }
64
- if (visibleCode === "repeated_failed_operation") {
65
- state.blockedReplays++;
66
- return;
67
- }
68
- state.reservedExecutions++;
69
- state.failures++;
88
+ this.seenFailedExecutions.add(executionKey);
89
+ this.retainHotState(executionKey, reduction.state);
70
90
  });
71
91
  }
72
- planFailure(failedTool, args, failureCode, availableTools, reservation) {
73
- const targets = readFailureTargets(failedTool, args, failureCode);
92
+ planFailure(failedTool, args, failure, availableTools, reservation) {
93
+ const targets = readFailureTargets(failedTool, args, failure.failureCode);
74
94
  const actions = readAvailableRecoveryActions(availableTools, targets);
75
- const unchangedRetryRemaining = this.hasUnchangedRetryRemaining(failedTool, args, failureCode, reservation);
95
+ const unchangedRetryRemaining = this.hasUnchangedRetryRemaining(failedTool, args, failure.failureCode, reservation);
96
+ const evidence = readFailureEvidence(failedTool, args, failure);
76
97
  return {
77
98
  targets,
78
- guidance: formatRecoveryGuidance(failureCode, actions, unchangedRetryRemaining),
99
+ guidance: formatRecoveryGuidance(failure.failureCode, actions, unchangedRetryRemaining),
100
+ ...(evidence ? { evidence } : {}),
79
101
  };
80
102
  }
81
- admit(tool, args, record) {
82
- const stateCapacityHalt = this.halted;
83
- if (stateCapacityHalt) {
103
+ admit(tool, args, record, messages = this.transcriptMessages) {
104
+ this.trackTranscript(messages);
105
+ const runHalt = this.halted;
106
+ if (runHalt) {
84
107
  return {
85
108
  kind: "blocked",
86
- record: stateCapacityHalt.record,
109
+ record: runHalt.record,
87
110
  exhausted: true,
88
111
  scope: "run",
89
- diagnostic: stateCapacityHalt.diagnostic,
112
+ diagnostic: runHalt.diagnostic,
90
113
  };
91
114
  }
92
115
  const executionKey = getToolExecutionKey(tool.name, args);
93
- let state = this.statesByExecutionKey.get(executionKey);
116
+ if (this.resolvedBeforeTranscriptCommit.has(executionKey))
117
+ return { kind: "allowed" };
118
+ let state = this.getHotState(executionKey);
119
+ if (!state && this.seenFailedExecutions.mightContain(executionKey)) {
120
+ state = this.restoreOperationFromTranscript(executionKey);
121
+ }
94
122
  if (!state && record && getToolFailureRecordExecutionKey(record) === executionKey) {
95
123
  state = this.getOrCreateState(executionKey, record, readFailureTargets(tool, args, record.failureCode));
96
124
  }
97
- if (this.halted) {
98
- return {
99
- kind: "blocked",
100
- record: this.halted.record,
101
- exhausted: true,
102
- scope: "run",
103
- diagnostic: this.halted.diagnostic,
104
- };
105
- }
106
125
  if (!state)
107
126
  return { kind: "allowed" };
108
- if (record)
127
+ if (record && getToolFailureRecordExecutionKey(record) === executionKey)
109
128
  state.record = record;
110
129
  if (state.operationCircuitOpen) {
111
130
  if (state.recoveryAvailable && state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION) {
@@ -116,6 +135,10 @@ export class ToolFailureRecoveryGate {
116
135
  state.blockedReplays = 0;
117
136
  return { kind: "allowed", reservation: { executionKey } };
118
137
  }
138
+ if (usesOperationLocalExhaustion(tool)) {
139
+ const diagnostic = `Operation recovery circuit remains closed after replay of ${state.record.failureCode}.`;
140
+ return { kind: "blocked", record: state.record, exhausted: true, scope: "operation", diagnostic };
141
+ }
119
142
  const diagnostic = `Run recovery circuit opened after replay of an operation whose local circuit was already open for ${state.record.failureCode}.`;
120
143
  this.halted = { record: state.record, diagnostic };
121
144
  return { kind: "blocked", record: state.record, exhausted: true, scope: "run", diagnostic };
@@ -148,7 +171,7 @@ export class ToolFailureRecoveryGate {
148
171
  this.observeSuccess(effect.tool, effect.args, effect.evidenceResult);
149
172
  return undefined;
150
173
  }
151
- this.observeFailure(effect.record, effect.args, effect.targets ?? [], effect.reservation);
174
+ this.observeFailure(effect.tool, effect.record, effect.args, effect.targets ?? [], effect.reservation);
152
175
  return this.halted;
153
176
  }
154
177
  isHalted() {
@@ -168,55 +191,18 @@ export class ToolFailureRecoveryGate {
168
191
  : 1;
169
192
  return executionsIncludingCurrent < BASE_FAILURE_EXECUTIONS_PER_OPERATION + retryLimit;
170
193
  }
171
- ensureRestoredState(executionKey, record) {
172
- const existing = this.statesByExecutionKey.get(executionKey);
173
- if (existing) {
174
- existing.record = record;
175
- return existing;
176
- }
177
- if (this.statesByExecutionKey.size >= MAX_RECOVERY_STATES)
178
- return undefined;
179
- const state = {
180
- record,
181
- recoveryTargets: [],
182
- reservedExecutions: 0,
183
- failures: 0,
184
- failureFamilyCounts: new Map(),
185
- recoveryProbes: 0,
186
- blockedReplays: 0,
187
- recoveryAvailable: false,
188
- operationCircuitOpen: false,
189
- };
190
- this.statesByExecutionKey.set(executionKey, state);
191
- return state;
192
- }
193
194
  getOrCreateState(executionKey, record, targets) {
194
- const existing = this.statesByExecutionKey.get(executionKey);
195
+ const existing = this.getHotState(executionKey);
195
196
  if (existing)
196
197
  return existing;
197
- const state = {
198
- record,
199
- recoveryTargets: targets,
200
- reservedExecutions: 0,
201
- failures: 0,
202
- failureFamilyCounts: new Map(),
203
- recoveryProbes: 0,
204
- blockedReplays: 0,
205
- recoveryAvailable: false,
206
- operationCircuitOpen: false,
207
- };
208
- if (this.statesByExecutionKey.size >= MAX_RECOVERY_STATES) {
209
- this.halted = {
210
- record,
211
- diagnostic: `Recovery circuit opened at the ${MAX_RECOVERY_STATES}-operation state bound.`,
212
- };
213
- return state;
214
- }
215
- this.statesByExecutionKey.set(executionKey, state);
198
+ const state = createFailureRecoveryState(record, targets);
199
+ this.retainHotState(executionKey, state);
216
200
  return state;
217
201
  }
218
- observeFailure(record, args, targets, reservation) {
202
+ observeFailure(tool, record, args, targets, reservation) {
219
203
  const executionKey = getToolExecutionKey(record.tool, args);
204
+ this.resolvedBeforeTranscriptCommit.delete(executionKey);
205
+ this.seenFailedExecutions.add(executionKey);
220
206
  const state = this.getOrCreateState(executionKey, record, targets);
221
207
  if (this.halted)
222
208
  return;
@@ -227,43 +213,32 @@ export class ToolFailureRecoveryGate {
227
213
  if (reservation?.executionKey !== executionKey)
228
214
  state.reservedExecutions++;
229
215
  state.failures++;
230
- this.totalFailures++;
231
- const familyKey = `${record.tool}\0${record.failureCode}`;
232
- const familyFailures = (this.failuresByFamily.get(familyKey) ?? 0) + 1;
233
- this.failuresByFamily.set(familyKey, familyFailures);
234
- state.failureFamilyCounts.set(familyKey, (state.failureFamilyCounts.get(familyKey) ?? 0) + 1);
235
216
  const operationFailureLimit = record.state === "failed"
236
217
  ? BASE_FAILURE_EXECUTIONS_PER_OPERATION +
237
218
  getToolExecutionUnchangedRetryLimit(record.failureCode) +
238
219
  MAX_RECOVERY_PROBES_PER_OPERATION
239
220
  : MAX_REJECTIONS_PER_OPERATION;
240
221
  if (state.failures >= operationFailureLimit) {
222
+ if (usesOperationLocalExhaustion(tool)) {
223
+ state.operationCircuitOpen = true;
224
+ return;
225
+ }
241
226
  this.halted = {
242
227
  record,
243
228
  diagnostic: `Recovery circuit opened after ${state.failures} failed outcomes for one operation.`,
244
229
  };
245
- return;
246
- }
247
- if (familyFailures >= MAX_FAILURES_PER_FAMILY) {
248
- this.halted = {
249
- record,
250
- diagnostic: `Recovery circuit opened after ${familyFailures} failures in one tool failure family.`,
251
- };
252
- return;
253
- }
254
- if (this.totalFailures >= MAX_FAILURES_PER_RUN) {
255
- this.halted = {
256
- record,
257
- diagnostic: `Recovery circuit opened after ${this.totalFailures} tool failures in one run.`,
258
- };
259
230
  }
260
231
  }
261
232
  observeSuccess(tool, args, result) {
262
233
  const successfulExecutionKey = getToolExecutionKey(tool.name, args);
234
+ // Tool results are appended to the transcript after the current execution batch completes.
235
+ // Until then the last persisted failure is stale authority: remember the exact success so a
236
+ // later sequential call in this same batch cannot resurrect that failure from the transcript.
237
+ this.resolvedBeforeTranscriptCommit.add(successfulExecutionKey);
263
238
  const evidenceTargets = readRecoveryEvidenceTargets(tool, args, result);
264
239
  for (const [executionKey, state] of this.statesByExecutionKey) {
265
240
  if (executionKey === successfulExecutionKey) {
266
- this.clearResolvedState(executionKey, state);
241
+ this.clearResolvedState(executionKey);
267
242
  continue;
268
243
  }
269
244
  if (state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION &&
@@ -273,18 +248,110 @@ export class ToolFailureRecoveryGate {
273
248
  }
274
249
  }
275
250
  }
276
- clearResolvedState(executionKey, state) {
251
+ clearResolvedState(executionKey) {
252
+ this.statesByExecutionKey.delete(executionKey);
253
+ }
254
+ trackTranscript(messages) {
255
+ const tail = messages[messages.length - 1];
256
+ if (messages !== this.transcriptMessages ||
257
+ messages.length !== this.transcriptLength ||
258
+ tail !== this.transcriptTail) {
259
+ // An advanced transcript now authoritatively records completed results. The temporary
260
+ // exact-success overlay is bounded to one uncommitted execution batch.
261
+ this.resolvedBeforeTranscriptCommit.clear();
262
+ }
263
+ this.transcriptMessages = messages;
264
+ this.transcriptLength = messages.length;
265
+ this.transcriptTail = tail;
266
+ }
267
+ getHotState(executionKey) {
268
+ const state = this.statesByExecutionKey.get(executionKey);
269
+ if (!state)
270
+ return undefined;
271
+ this.statesByExecutionKey.delete(executionKey);
272
+ this.statesByExecutionKey.set(executionKey, state);
273
+ return state;
274
+ }
275
+ retainHotState(executionKey, state) {
277
276
  this.statesByExecutionKey.delete(executionKey);
278
- this.totalFailures = Math.max(0, this.totalFailures - state.failures);
279
- for (const [familyKey, stateFailures] of state.failureFamilyCounts) {
280
- const remaining = (this.failuresByFamily.get(familyKey) ?? 0) - stateFailures;
281
- if (remaining > 0)
282
- this.failuresByFamily.set(familyKey, remaining);
283
- else
284
- this.failuresByFamily.delete(familyKey);
277
+ this.statesByExecutionKey.set(executionKey, state);
278
+ while (this.statesByExecutionKey.size > MAX_HOT_RECOVERY_STATES) {
279
+ const oldest = this.statesByExecutionKey.keys().next().value;
280
+ if (oldest === undefined)
281
+ break;
282
+ this.statesByExecutionKey.delete(oldest);
283
+ }
284
+ }
285
+ restoreOperationFromTranscript(executionKey) {
286
+ let restoredState;
287
+ forEachPairedToolResult(this.transcriptMessages, ({ tool, args, executionKey: candidateKey, result }) => {
288
+ if (candidateKey !== executionKey)
289
+ return;
290
+ const reduction = this.reduceTranscriptResult(restoredState, tool, args, result);
291
+ if (reduction.kind === "resolved") {
292
+ restoredState = undefined;
293
+ return;
294
+ }
295
+ if (reduction.kind === "failed")
296
+ restoredState = reduction.state;
297
+ });
298
+ if (restoredState)
299
+ this.retainHotState(executionKey, restoredState);
300
+ return restoredState;
301
+ }
302
+ reduceTranscriptResult(state, tool, args, result) {
303
+ if (!result.isError)
304
+ return { kind: "resolved" };
305
+ const record = restoreToolFailureRecord(result, tool, args);
306
+ const visibleCode = readVisibleToolFailureCode(result);
307
+ if (isPromptScopedFailureCode(visibleCode) || isPromptScopedFailureCode(record.failureCode)) {
308
+ return { kind: "ignored", state };
309
+ }
310
+ const next = state ?? createFailureRecoveryState(record, []);
311
+ next.record = record;
312
+ if (isClosedOperationFailureCode(visibleCode)) {
313
+ next.operationCircuitOpen = true;
314
+ next.blockedReplays = MAX_BLOCKED_REPLAYS_PER_FAILURE;
315
+ next.recoveryAvailable = false;
316
+ return { kind: "failed", state: next };
285
317
  }
318
+ if (visibleCode === "repeated_failed_operation") {
319
+ next.blockedReplays++;
320
+ return { kind: "failed", state: next };
321
+ }
322
+ next.reservedExecutions++;
323
+ next.failures++;
324
+ return { kind: "failed", state: next };
286
325
  }
287
326
  }
327
+ function usesOperationLocalExhaustion(tool) {
328
+ return tool?.failureRecovery?.exhaustionScope === "operation";
329
+ }
330
+ function readFailureEvidence(tool, args, failure) {
331
+ try {
332
+ const contract = tool.failureRecovery;
333
+ const getEvidence = contract?.getFailureEvidence;
334
+ if (!getEvidence)
335
+ return undefined;
336
+ const evidence = Reflect.apply(getEvidence, contract, [args, failure]);
337
+ return sanitizeToolFailureEvidence(evidence);
338
+ }
339
+ catch {
340
+ return undefined;
341
+ }
342
+ }
343
+ function createFailureRecoveryState(record, recoveryTargets) {
344
+ return {
345
+ record,
346
+ recoveryTargets,
347
+ reservedExecutions: 0,
348
+ failures: 0,
349
+ recoveryProbes: 0,
350
+ blockedReplays: 0,
351
+ recoveryAvailable: false,
352
+ operationCircuitOpen: false,
353
+ };
354
+ }
288
355
  function readFailureTargets(tool, args, failureCode) {
289
356
  try {
290
357
  const contract = tool.failureRecovery;
@@ -1 +1 @@
1
- {"version":3,"file":"tool-failure-recovery-gate.js","sourceRoot":"","sources":["../src/tool-failure-recovery-gate.ts"],"names":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AC,CAAC,GAAG,aAAa,GAAG,CAAC,CAAC,CAAC,EAAE,mBAAmB,SAAS,IAAI,SAAS,EAAE,EACtF,2BAA2B,CAC3B,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,KAAa,EAAE,QAAgB;IAChD,IAAI,KAAK,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC3C,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC;AAC3C,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAoC,EAAE,KAAqC;IACtG,OAAO,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,CAAC;AACrG,CAAC;AAED,SAAS,uBAAuB,CAC/B,IAA+C,EAC/C,KAAgD;IAEhD,IAAI,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM;QAAE,OAAO,uBAAuB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,kBAAkB,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;AAChG,CAAC","sourcesContent":["import { getToolExecutionUnchangedRetryLimit } from \"@caupulican/pi-ai/tool-repair-registry\";\nimport {\n\tforEachPairedToolResult,\n\tgetToolExecutionKey,\n\tgetToolFailureRecordExecutionKey,\n\tisClosedOperationFailureCode,\n\tisPromptScopedFailureCode,\n\treadVisibleToolFailureCode,\n\trestoreToolFailureRecord,\n\ttype ToolFailureMemoryRecord,\n} from \"./tool-failure-memory.ts\";\nimport type {\n\tAgentMessage,\n\tAgentTool,\n\tAgentToolFailureRecoveryAuthority,\n\tAgentToolFailureRecoveryTarget,\n\tAgentToolResult,\n} from \"./types.ts\";\nimport { isAgentToolFailureRecoveryAuthority } from \"./types.ts\";\n\nconst MAX_BLOCKED_REPLAYS_PER_FAILURE = 2;\nconst BASE_FAILURE_EXECUTIONS_PER_OPERATION = 1;\nconst MAX_RECOVERY_PROBES_PER_OPERATION = 1;\nconst MAX_REJECTIONS_PER_OPERATION = 4;\nexport const TOOL_FAILURE_RECOVERY_ACCOUNTING_WAVE_SIZE = 4;\nconst MAX_FAILURES_PER_FAMILY = TOOL_FAILURE_RECOVERY_ACCOUNTING_WAVE_SIZE;\nconst MAX_FAILURES_PER_RUN = 12;\nconst MAX_RECOVERY_STATES = 64;\nconst MAX_RECOVERY_TARGETS = 8;\nconst MAX_RECOVERY_ACTIONS = 8;\nconst MAX_TARGET_KIND_CHARS = 64;\nconst MAX_TARGET_SCOPE_CHARS = 32_768;\nconst MAX_ACTION_INSTRUCTION_CHARS = 160;\nconst MAX_RECOVERY_GUIDANCE_CHARS = 320;\nconst TARGET_KIND_PATTERN = /^[a-z0-9][a-z0-9._:-]*$/;\n\nexport interface ToolFailureExecutionReservation {\n\texecutionKey: string;\n}\n\nexport interface ToolFailureRecoveryPlan {\n\ttargets: readonly AgentToolFailureRecoveryTarget[];\n\tguidance: string;\n}\n\nexport type ToolFailureRecoveryGateEffect =\n\t| {\n\t\t\tkind: \"failure\";\n\t\t\trecord: ToolFailureMemoryRecord;\n\t\t\targs: unknown;\n\t\t\ttargets?: readonly AgentToolFailureRecoveryTarget[];\n\t\t\treservation?: ToolFailureExecutionReservation;\n\t }\n\t| { kind: \"success\"; tool: AgentTool<any>; args: unknown; evidenceResult: AgentToolResult<any> };\n\nexport type ToolFailureRecoveryAdmission =\n\t| { kind: \"allowed\"; reservation?: ToolFailureExecutionReservation }\n\t| { kind: \"blocked\"; record: ToolFailureMemoryRecord; exhausted: false; diagnostic?: string }\n\t| {\n\t\t\tkind: \"blocked\";\n\t\t\trecord: ToolFailureMemoryRecord;\n\t\t\texhausted: true;\n\t\t\tscope: \"operation\" | \"run\";\n\t\t\tdiagnostic?: string;\n\t };\n\ninterface FailureRecoveryState {\n\trecord: ToolFailureMemoryRecord;\n\trecoveryTargets: readonly AgentToolFailureRecoveryTarget[];\n\treservedExecutions: number;\n\tfailures: number;\n\tfailureFamilyCounts: Map<string, number>;\n\trecoveryProbes: number;\n\tblockedReplays: number;\n\trecoveryAvailable: boolean;\n\toperationCircuitOpen: boolean;\n}\n\ninterface AvailableRecoveryAction {\n\ttoolName: string;\n\tkind: \"correct\" | \"repair\";\n\tinstruction: string;\n}\n\ninterface ParsedRecoveryAction {\n\tkind: \"correct\" | \"repair\";\n\tauthority: AgentToolFailureRecoveryAuthority;\n\ttargetKind: string;\n\tinstruction: string;\n\tgetEvidence?: (params: unknown, result: AgentToolResult<unknown>) => unknown;\n}\n\nexport interface ToolFailureRecoveryHalt {\n\trecord: ToolFailureMemoryRecord;\n\tdiagnostic: string;\n}\n\n/**\n * Owns execution admission and bounded unresolved-failure budgets.\n *\n * Recovery authority is exact and tool-owned. A failed tool declares opaque backend-specific targets;\n * a loaded recovery tool may teach actions only for the same authority and target kind. Only raw\n * successful repair evidence with byte-exact scope can reopen one probe. Argument text and hooks have\n * no recovery authority.\n *\n * Run-level halt and family/run counters stay on the current run. Per-operation execution budget and\n * circuit are reconstructed from the transcript when a new run starts with an empty gate, so an\n * already-exhausted identical operation is not re-executed after a user turn or session resume.\n */\nexport class ToolFailureRecoveryGate {\n\tprivate readonly statesByExecutionKey = new Map<string, FailureRecoveryState>();\n\tprivate readonly failuresByFamily = new Map<string, number>();\n\tprivate totalFailures = 0;\n\tprivate halted: ToolFailureRecoveryHalt | undefined;\n\n\tisEmpty(): boolean {\n\t\treturn this.statesByExecutionKey.size === 0 && this.halted === undefined && this.totalFailures === 0;\n\t}\n\n\trestoreFromMessages(messages: readonly AgentMessage[]): void {\n\t\tif (!this.isEmpty()) return;\n\t\tforEachPairedToolResult(messages, ({ tool, args, executionKey, result }) => {\n\t\t\tif (!result.isError) {\n\t\t\t\tconst existing = this.statesByExecutionKey.get(executionKey);\n\t\t\t\tif (existing) this.clearResolvedState(executionKey, existing);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst restored = restoreToolFailureRecord(result, tool, args);\n\t\t\tconst visibleCode = readVisibleToolFailureCode(result);\n\t\t\tif (isPromptScopedFailureCode(visibleCode) || isPromptScopedFailureCode(restored.failureCode)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst state = this.ensureRestoredState(executionKey, restored);\n\t\t\tif (!state) return false;\n\t\t\tif (isClosedOperationFailureCode(visibleCode)) {\n\t\t\t\tstate.operationCircuitOpen = true;\n\t\t\t\tstate.blockedReplays = MAX_BLOCKED_REPLAYS_PER_FAILURE;\n\t\t\t\tstate.recoveryAvailable = false;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (visibleCode === \"repeated_failed_operation\") {\n\t\t\t\tstate.blockedReplays++;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstate.reservedExecutions++;\n\t\t\tstate.failures++;\n\t\t});\n\t}\n\n\tplanFailure(\n\t\tfailedTool: AgentTool<any>,\n\t\targs: unknown,\n\t\tfailureCode: string,\n\t\tavailableTools: readonly AgentTool<any>[],\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): ToolFailureRecoveryPlan {\n\t\tconst targets = readFailureTargets(failedTool, args, failureCode);\n\t\tconst actions = readAvailableRecoveryActions(availableTools, targets);\n\t\tconst unchangedRetryRemaining = this.hasUnchangedRetryRemaining(failedTool, args, failureCode, reservation);\n\t\treturn {\n\t\t\ttargets,\n\t\t\tguidance: formatRecoveryGuidance(failureCode, actions, unchangedRetryRemaining),\n\t\t};\n\t}\n\n\tadmit(\n\t\ttool: AgentTool<any>,\n\t\targs: unknown,\n\t\trecord: ToolFailureMemoryRecord | undefined,\n\t): ToolFailureRecoveryAdmission {\n\t\tconst stateCapacityHalt = this.halted;\n\t\tif (stateCapacityHalt) {\n\t\t\treturn {\n\t\t\t\tkind: \"blocked\",\n\t\t\t\trecord: stateCapacityHalt.record,\n\t\t\t\texhausted: true,\n\t\t\t\tscope: \"run\",\n\t\t\t\tdiagnostic: stateCapacityHalt.diagnostic,\n\t\t\t};\n\t\t}\n\n\t\tconst executionKey = getToolExecutionKey(tool.name, args);\n\t\tlet state = this.statesByExecutionKey.get(executionKey);\n\t\tif (!state && record && getToolFailureRecordExecutionKey(record) === executionKey) {\n\t\t\tstate = this.getOrCreateState(executionKey, record, readFailureTargets(tool, args, record.failureCode));\n\t\t}\n\t\tif (this.halted) {\n\t\t\treturn {\n\t\t\t\tkind: \"blocked\",\n\t\t\t\trecord: this.halted.record,\n\t\t\t\texhausted: true,\n\t\t\t\tscope: \"run\",\n\t\t\t\tdiagnostic: this.halted.diagnostic,\n\t\t\t};\n\t\t}\n\t\tif (!state) return { kind: \"allowed\" };\n\n\t\tif (record) state.record = record;\n\t\tif (state.operationCircuitOpen) {\n\t\t\tif (state.recoveryAvailable && state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION) {\n\t\t\t\tstate.operationCircuitOpen = false;\n\t\t\t\tstate.recoveryAvailable = false;\n\t\t\t\tstate.recoveryProbes++;\n\t\t\t\tstate.reservedExecutions++;\n\t\t\t\tstate.blockedReplays = 0;\n\t\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t\t}\n\t\t\tconst diagnostic = `Run recovery circuit opened after replay of an operation whose local circuit was already open for ${state.record.failureCode}.`;\n\t\t\tthis.halted = { record: state.record, diagnostic };\n\t\t\treturn { kind: \"blocked\", record: state.record, exhausted: true, scope: \"run\", diagnostic };\n\t\t}\n\t\tconst automaticExecutionLimit =\n\t\t\tBASE_FAILURE_EXECUTIONS_PER_OPERATION + getToolExecutionUnchangedRetryLimit(state.record.failureCode);\n\t\tif (state.reservedExecutions < automaticExecutionLimit) {\n\t\t\tstate.reservedExecutions++;\n\t\t\tstate.blockedReplays = 0;\n\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t}\n\t\tif (state.recoveryAvailable && state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION) {\n\t\t\tstate.recoveryAvailable = false;\n\t\t\tstate.recoveryProbes++;\n\t\t\tstate.reservedExecutions++;\n\t\t\tstate.blockedReplays = 0;\n\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t}\n\n\t\tstate.blockedReplays++;\n\t\tif (state.blockedReplays >= MAX_BLOCKED_REPLAYS_PER_FAILURE) {\n\t\t\tstate.operationCircuitOpen = true;\n\t\t\tconst diagnostic = `Operation recovery circuit opened after ${state.blockedReplays} blocked replays of ${state.record.failureCode}.`;\n\t\t\treturn { kind: \"blocked\", record: state.record, exhausted: true, scope: \"operation\", diagnostic };\n\t\t}\n\t\treturn { kind: \"blocked\", record: state.record, exhausted: false };\n\t}\n\n\tapply(effect: ToolFailureRecoveryGateEffect | undefined): ToolFailureRecoveryHalt | undefined {\n\t\tif (!effect || this.halted) return undefined;\n\t\tif (effect.kind === \"success\") {\n\t\t\tthis.observeSuccess(effect.tool, effect.args, effect.evidenceResult);\n\t\t\treturn undefined;\n\t\t}\n\t\tthis.observeFailure(effect.record, effect.args, effect.targets ?? [], effect.reservation);\n\t\treturn this.halted;\n\t}\n\n\tisHalted(): boolean {\n\t\treturn this.halted !== undefined;\n\t}\n\n\tgetHalt(): ToolFailureRecoveryHalt | undefined {\n\t\treturn this.halted;\n\t}\n\n\tprivate hasUnchangedRetryRemaining(\n\t\ttool: AgentTool<any>,\n\t\targs: unknown,\n\t\tfailureCode: string,\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): boolean {\n\t\tconst retryLimit = getToolExecutionUnchangedRetryLimit(failureCode);\n\t\tif (retryLimit === 0) return false;\n\t\tconst executionKey = getToolExecutionKey(tool.name, args);\n\t\tconst state = this.statesByExecutionKey.get(executionKey);\n\t\tconst executionsIncludingCurrent = state\n\t\t\t? state.reservedExecutions + (reservation?.executionKey === executionKey ? 0 : 1)\n\t\t\t: 1;\n\t\treturn executionsIncludingCurrent < BASE_FAILURE_EXECUTIONS_PER_OPERATION + retryLimit;\n\t}\n\n\tprivate ensureRestoredState(\n\t\texecutionKey: string,\n\t\trecord: ToolFailureMemoryRecord,\n\t): FailureRecoveryState | undefined {\n\t\tconst existing = this.statesByExecutionKey.get(executionKey);\n\t\tif (existing) {\n\t\t\texisting.record = record;\n\t\t\treturn existing;\n\t\t}\n\t\tif (this.statesByExecutionKey.size >= MAX_RECOVERY_STATES) return undefined;\n\t\tconst state: FailureRecoveryState = {\n\t\t\trecord,\n\t\t\trecoveryTargets: [],\n\t\t\treservedExecutions: 0,\n\t\t\tfailures: 0,\n\t\t\tfailureFamilyCounts: new Map(),\n\t\t\trecoveryProbes: 0,\n\t\t\tblockedReplays: 0,\n\t\t\trecoveryAvailable: false,\n\t\t\toperationCircuitOpen: false,\n\t\t};\n\t\tthis.statesByExecutionKey.set(executionKey, state);\n\t\treturn state;\n\t}\n\n\tprivate getOrCreateState(\n\t\texecutionKey: string,\n\t\trecord: ToolFailureMemoryRecord,\n\t\ttargets: readonly AgentToolFailureRecoveryTarget[],\n\t): FailureRecoveryState {\n\t\tconst existing = this.statesByExecutionKey.get(executionKey);\n\t\tif (existing) return existing;\n\t\tconst state: FailureRecoveryState = {\n\t\t\trecord,\n\t\t\trecoveryTargets: targets,\n\t\t\treservedExecutions: 0,\n\t\t\tfailures: 0,\n\t\t\tfailureFamilyCounts: new Map(),\n\t\t\trecoveryProbes: 0,\n\t\t\tblockedReplays: 0,\n\t\t\trecoveryAvailable: false,\n\t\t\toperationCircuitOpen: false,\n\t\t};\n\t\tif (this.statesByExecutionKey.size >= MAX_RECOVERY_STATES) {\n\t\t\tthis.halted = {\n\t\t\t\trecord,\n\t\t\t\tdiagnostic: `Recovery circuit opened at the ${MAX_RECOVERY_STATES}-operation state bound.`,\n\t\t\t};\n\t\t\treturn state;\n\t\t}\n\t\tthis.statesByExecutionKey.set(executionKey, state);\n\t\treturn state;\n\t}\n\n\tprivate observeFailure(\n\t\trecord: ToolFailureMemoryRecord,\n\t\targs: unknown,\n\t\ttargets: readonly AgentToolFailureRecoveryTarget[],\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): void {\n\t\tconst executionKey = getToolExecutionKey(record.tool, args);\n\t\tconst state = this.getOrCreateState(executionKey, record, targets);\n\t\tif (this.halted) return;\n\t\tstate.record = record;\n\t\tstate.recoveryTargets = targets;\n\t\tstate.recoveryAvailable = false;\n\t\tstate.blockedReplays = 0;\n\t\tif (reservation?.executionKey !== executionKey) state.reservedExecutions++;\n\n\t\tstate.failures++;\n\t\tthis.totalFailures++;\n\t\tconst familyKey = `${record.tool}\\0${record.failureCode}`;\n\t\tconst familyFailures = (this.failuresByFamily.get(familyKey) ?? 0) + 1;\n\t\tthis.failuresByFamily.set(familyKey, familyFailures);\n\t\tstate.failureFamilyCounts.set(familyKey, (state.failureFamilyCounts.get(familyKey) ?? 0) + 1);\n\n\t\tconst operationFailureLimit =\n\t\t\trecord.state === \"failed\"\n\t\t\t\t? BASE_FAILURE_EXECUTIONS_PER_OPERATION +\n\t\t\t\t\tgetToolExecutionUnchangedRetryLimit(record.failureCode) +\n\t\t\t\t\tMAX_RECOVERY_PROBES_PER_OPERATION\n\t\t\t\t: MAX_REJECTIONS_PER_OPERATION;\n\t\tif (state.failures >= operationFailureLimit) {\n\t\t\tthis.halted = {\n\t\t\t\trecord,\n\t\t\t\tdiagnostic: `Recovery circuit opened after ${state.failures} failed outcomes for one operation.`,\n\t\t\t};\n\t\t\treturn;\n\t\t}\n\t\tif (familyFailures >= MAX_FAILURES_PER_FAMILY) {\n\t\t\tthis.halted = {\n\t\t\t\trecord,\n\t\t\t\tdiagnostic: `Recovery circuit opened after ${familyFailures} failures in one tool failure family.`,\n\t\t\t};\n\t\t\treturn;\n\t\t}\n\t\tif (this.totalFailures >= MAX_FAILURES_PER_RUN) {\n\t\t\tthis.halted = {\n\t\t\t\trecord,\n\t\t\t\tdiagnostic: `Recovery circuit opened after ${this.totalFailures} tool failures in one run.`,\n\t\t\t};\n\t\t}\n\t}\n\n\tprivate observeSuccess(tool: AgentTool<any>, args: unknown, result: AgentToolResult<any>): void {\n\t\tconst successfulExecutionKey = getToolExecutionKey(tool.name, args);\n\t\tconst evidenceTargets = readRecoveryEvidenceTargets(tool, args, result);\n\t\tfor (const [executionKey, state] of this.statesByExecutionKey) {\n\t\t\tif (executionKey === successfulExecutionKey) {\n\t\t\t\tthis.clearResolvedState(executionKey, state);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (\n\t\t\t\tstate.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION &&\n\t\t\t\thasSharedRecoveryTarget(state.recoveryTargets, evidenceTargets)\n\t\t\t) {\n\t\t\t\tstate.recoveryAvailable = true;\n\t\t\t\tstate.blockedReplays = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate clearResolvedState(executionKey: string, state: FailureRecoveryState): void {\n\t\tthis.statesByExecutionKey.delete(executionKey);\n\t\tthis.totalFailures = Math.max(0, this.totalFailures - state.failures);\n\t\tfor (const [familyKey, stateFailures] of state.failureFamilyCounts) {\n\t\t\tconst remaining = (this.failuresByFamily.get(familyKey) ?? 0) - stateFailures;\n\t\t\tif (remaining > 0) this.failuresByFamily.set(familyKey, remaining);\n\t\t\telse this.failuresByFamily.delete(familyKey);\n\t\t}\n\t}\n}\n\nfunction readFailureTargets(\n\ttool: AgentTool<any>,\n\targs: unknown,\n\tfailureCode: string,\n): readonly AgentToolFailureRecoveryTarget[] {\n\ttry {\n\t\tconst contract = tool.failureRecovery;\n\t\tconst getTargets = contract?.getFailureTargets;\n\t\tif (!getTargets) return [];\n\t\tconst targets = Reflect.apply(getTargets, contract, [args, { failureCode }]);\n\t\treturn sanitizeRecoveryTargets(targets);\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nfunction sanitizeRecoveryTargets(value: unknown): readonly AgentToolFailureRecoveryTarget[] {\n\tif (!Array.isArray(value)) return [];\n\tconst targets: AgentToolFailureRecoveryTarget[] = [];\n\tfor (const candidate of value) {\n\t\tif (targets.length >= MAX_RECOVERY_TARGETS) break;\n\t\tif (!candidate || typeof candidate !== \"object\" || Array.isArray(candidate)) continue;\n\t\tconst { authority, kind, scope } = candidate as { authority?: unknown; kind?: unknown; scope?: unknown };\n\t\tif (!isAgentToolFailureRecoveryAuthority(authority) || !validTargetKind(kind) || !validTargetScope(scope)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (targets.some((target) => sameRecoveryTarget(target, { authority, kind, scope }))) continue;\n\t\ttargets.push({ authority, kind, scope });\n\t}\n\treturn targets;\n}\n\nfunction validTargetKind(value: unknown): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= MAX_TARGET_KIND_CHARS &&\n\t\tTARGET_KIND_PATTERN.test(value)\n\t);\n}\n\nfunction validTargetScope(value: unknown): value is string {\n\treturn (\n\t\ttypeof value === \"string\" && value.length > 0 && value.length <= MAX_TARGET_SCOPE_CHARS && !value.includes(\"\\0\")\n\t);\n}\n\nfunction readAvailableRecoveryActions(\n\ttools: readonly AgentTool<any>[],\n\ttargets: readonly AgentToolFailureRecoveryTarget[],\n): readonly AvailableRecoveryAction[] {\n\tif (targets.length === 0) return [];\n\tconst actions: AvailableRecoveryAction[] = [];\n\tconst seen = new Set<string>();\n\tfor (const tool of tools) {\n\t\tfor (const candidate of readDeclaredRecoveryActions(tool)) {\n\t\t\tif (actions.length >= MAX_RECOVERY_ACTIONS) return actions;\n\t\t\tconst action = parseRecoveryAction(candidate);\n\t\t\tif (\n\t\t\t\t!action ||\n\t\t\t\t!targets.some((target) => target.authority === action.authority && target.kind === action.targetKind)\n\t\t\t) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst instruction = truncate(action.instruction.trim(), MAX_ACTION_INSTRUCTION_CHARS);\n\t\t\tif (!instruction) continue;\n\t\t\tconst key = `${tool.name}\\0${action.kind}\\0${instruction}`;\n\t\t\tif (seen.has(key)) continue;\n\t\t\tseen.add(key);\n\t\t\tactions.push({ toolName: tool.name, kind: action.kind, instruction });\n\t\t}\n\t}\n\treturn actions;\n}\n\nfunction readDeclaredRecoveryActions(tool: AgentTool<any>): readonly unknown[] {\n\ttry {\n\t\tconst declared = tool.failureRecovery?.actions;\n\t\tif (!Array.isArray(declared)) return [];\n\t\tconst actions: unknown[] = [];\n\t\tconst count = Math.min(declared.length, MAX_RECOVERY_ACTIONS);\n\t\tfor (let index = 0; index < count; index++) actions.push(declared[index]);\n\t\treturn actions;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nfunction parseRecoveryAction(value: unknown): ParsedRecoveryAction | undefined {\n\ttry {\n\t\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n\t\tconst candidate = value as {\n\t\t\tkind?: unknown;\n\t\t\tauthority?: unknown;\n\t\t\ttargetKind?: unknown;\n\t\t\tinstruction?: unknown;\n\t\t\tgetEvidence?: unknown;\n\t\t};\n\t\tif (\n\t\t\t!isAgentToolFailureRecoveryAuthority(candidate.authority) ||\n\t\t\t!validTargetKind(candidate.targetKind) ||\n\t\t\ttypeof candidate.instruction !== \"string\"\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\tif (candidate.kind === \"correct\") {\n\t\t\treturn {\n\t\t\t\tkind: candidate.kind,\n\t\t\t\tauthority: candidate.authority,\n\t\t\t\ttargetKind: candidate.targetKind,\n\t\t\t\tinstruction: candidate.instruction,\n\t\t\t};\n\t\t}\n\t\tif (candidate.kind !== \"repair\" || typeof candidate.getEvidence !== \"function\") return undefined;\n\t\tconst getEvidence = candidate.getEvidence;\n\t\treturn {\n\t\t\tkind: candidate.kind,\n\t\t\tauthority: candidate.authority,\n\t\t\ttargetKind: candidate.targetKind,\n\t\t\tinstruction: candidate.instruction,\n\t\t\tgetEvidence: (params, result) => Reflect.apply(getEvidence, value, [params, result]),\n\t\t};\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction readRecoveryEvidenceTargets(\n\ttool: AgentTool<any>,\n\targs: unknown,\n\tresult: AgentToolResult<any>,\n): readonly AgentToolFailureRecoveryTarget[] {\n\tconst targets: AgentToolFailureRecoveryTarget[] = [];\n\tfor (const candidate of readDeclaredRecoveryActions(tool)) {\n\t\tconst action = parseRecoveryAction(candidate);\n\t\tif (targets.length >= MAX_RECOVERY_TARGETS || !action || action.kind !== \"repair\" || !action.getEvidence) {\n\t\t\tcontinue;\n\t\t}\n\t\tlet scopes: unknown;\n\t\ttry {\n\t\t\tscopes = action.getEvidence(args, result);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!Array.isArray(scopes)) continue;\n\t\tfor (const scope of scopes) {\n\t\t\tif (targets.length >= MAX_RECOVERY_TARGETS) break;\n\t\t\tif (!validTargetScope(scope)) continue;\n\t\t\tconst target = { authority: action.authority, kind: action.targetKind, scope };\n\t\t\tif (!targets.some((candidate) => sameRecoveryTarget(candidate, target))) targets.push(target);\n\t\t}\n\t}\n\treturn targets;\n}\n\nfunction formatRecoveryGuidance(\n\tfailureCode: string,\n\tactions: readonly AvailableRecoveryAction[],\n\tunchangedRetryRemaining: boolean,\n): string {\n\tconst hasTimeoutRetryPolicy = getToolExecutionUnchangedRetryLimit(failureCode) > 0;\n\tconst timeoutPolicy = hasTimeoutRetryPolicy\n\t\t? unchangedRetryRemaining\n\t\t\t? \"Timeout policy allows 1 unchanged retry; if it fails, never retry unchanged.\"\n\t\t\t: \"Timeout unchanged retry exhausted; never retry unchanged.\"\n\t\t: undefined;\n\tif (actions.length === 0) {\n\t\tif (timeoutPolicy) return `${timeoutPolicy} Change/narrow operation, or report blocker.`;\n\t\treturn \"No loaded tool declares recovery. Never retry unchanged. Use materially different operation justified by diagnostic/schema, or report blocker.\";\n\t}\n\tconst available = actions.map((action) => `${action.toolName} ${action.kind}: ${action.instruction}`).join(\" \");\n\tconst hasRepair = actions.some((action) => action.kind === \"repair\");\n\tconst authority = hasRepair\n\t\t? \"Only exact matching repair evidence grants 1 probe; else change operation.\"\n\t\t: \"Actions require changed operation; unchanged remains blocked.\";\n\treturn truncate(\n\t\t`${timeoutPolicy ? `${timeoutPolicy} ` : \"\"}Loaded actions: ${available} ${authority}`,\n\t\tMAX_RECOVERY_GUIDANCE_CHARS,\n\t);\n}\n\nfunction truncate(value: string, maxChars: number): string {\n\tif (value.length <= maxChars) return value;\n\treturn `${value.slice(0, maxChars - 1)}…`;\n}\n\nfunction sameRecoveryTarget(left: AgentToolFailureRecoveryTarget, right: AgentToolFailureRecoveryTarget): boolean {\n\treturn left.authority === right.authority && left.kind === right.kind && left.scope === right.scope;\n}\n\nfunction hasSharedRecoveryTarget(\n\tleft: readonly AgentToolFailureRecoveryTarget[],\n\tright: readonly AgentToolFailureRecoveryTarget[],\n): boolean {\n\tif (left.length > right.length) return hasSharedRecoveryTarget(right, left);\n\treturn left.some((target) => right.some((candidate) => sameRecoveryTarget(target, candidate)));\n}\n"]}
1
+ {"version":3,"file":"tool-failure-recovery-gate.js","sourceRoot":"","sources":["../src/tool-failure-recovery-gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAE,MAAM,wCAAwC,CAAC;AAE7F,OAAO,EACN,uBAAuB,EACvB,mBAAmB,EACnB,4BAA4B,EAC5B,gCAAgC,EAChC,4BAA4B,EAC5B,yBAAyB,EACzB,0BAA0B,EAC1B,wBAAwB,EACxB,2BAA2B,GAE3B,MAAM,0BAA0B,CAAC;AASlC,OAAO,EAAE,mCAAmC,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,+BAA+B,GAAG,CAAC,CAAC;AAC1C,MAAM,qCAAqC,GAAG,CAAC,CAAC;AAChD,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAC5C,MAAM,4BAA4B,GAAG,CAAC,CAAC;AACvC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AACnC,MAAM,2BAA2B,GAAG,EAAE,GAAG,IAAI,CAAC;AAC9C,MAAM,oBA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AAC,CAAC;IACrE,MAAM,SAAS,GAAG,SAAS;QAC1B,CAAC,CAAC,4EAA4E;QAC9E,CAAC,CAAC,+DAA+D,CAAC;IACnE,OAAO,QAAQ,CACd,GAAG,aAAa,CAAC,CAAC,CAAC,GAAG,aAAa,GAAG,CAAC,CAAC,CAAC,EAAE,mBAAmB,SAAS,IAAI,SAAS,EAAE,EACtF,2BAA2B,CAC3B,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,KAAa,EAAE,QAAgB;IAChD,IAAI,KAAK,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC3C,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC;AAC3C,CAAC;AAED,SAAS,kBAAkB,CAAC,IAAoC,EAAE,KAAqC;IACtG,OAAO,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,CAAC;AACrG,CAAC;AAED,SAAS,uBAAuB,CAC/B,IAA+C,EAC/C,KAAgD;IAEhD,IAAI,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM;QAAE,OAAO,uBAAuB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,kBAAkB,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;AAChG,CAAC","sourcesContent":["import { getToolExecutionUnchangedRetryLimit } from \"@caupulican/pi-ai/tool-repair-registry\";\nimport type { ToolResultMessage } from \"@caupulican/pi-ai/types\";\nimport {\n\tforEachPairedToolResult,\n\tgetToolExecutionKey,\n\tgetToolExecutionKeyHashParts,\n\tgetToolFailureRecordExecutionKey,\n\tisClosedOperationFailureCode,\n\tisPromptScopedFailureCode,\n\treadVisibleToolFailureCode,\n\trestoreToolFailureRecord,\n\tsanitizeToolFailureEvidence,\n\ttype ToolFailureMemoryRecord,\n} from \"./tool-failure-memory.ts\";\nimport type {\n\tAgentMessage,\n\tAgentTool,\n\tAgentToolFailureEvidenceContext,\n\tAgentToolFailureRecoveryAuthority,\n\tAgentToolFailureRecoveryTarget,\n\tAgentToolResult,\n} from \"./types.ts\";\nimport { isAgentToolFailureRecoveryAuthority } from \"./types.ts\";\n\nconst MAX_BLOCKED_REPLAYS_PER_FAILURE = 2;\nconst BASE_FAILURE_EXECUTIONS_PER_OPERATION = 1;\nconst MAX_RECOVERY_PROBES_PER_OPERATION = 1;\nconst MAX_REJECTIONS_PER_OPERATION = 4;\nconst MAX_HOT_RECOVERY_STATES = 64;\nconst SEEN_EXECUTION_FILTER_BYTES = 64 * 1024;\nconst MAX_RECOVERY_TARGETS = 8;\nconst MAX_RECOVERY_ACTIONS = 8;\nconst MAX_TARGET_KIND_CHARS = 64;\nconst MAX_TARGET_SCOPE_CHARS = 32_768;\nconst MAX_ACTION_INSTRUCTION_CHARS = 160;\nconst MAX_RECOVERY_GUIDANCE_CHARS = 320;\nconst TARGET_KIND_PATTERN = /^[a-z0-9][a-z0-9._:-]*$/;\n\nexport interface ToolFailureExecutionReservation {\n\texecutionKey: string;\n}\n\nexport interface ToolFailureRecoveryPlan {\n\ttargets: readonly AgentToolFailureRecoveryTarget[];\n\tguidance: string;\n\tevidence?: string;\n}\n\nexport type ToolFailureRecoveryGateEffect =\n\t| {\n\t\t\tkind: \"failure\";\n\t\t\ttool?: AgentTool<any>;\n\t\t\trecord: ToolFailureMemoryRecord;\n\t\t\targs: unknown;\n\t\t\ttargets?: readonly AgentToolFailureRecoveryTarget[];\n\t\t\treservation?: ToolFailureExecutionReservation;\n\t }\n\t| { kind: \"success\"; tool: AgentTool<any>; args: unknown; evidenceResult: AgentToolResult<any> };\n\nexport type ToolFailureRecoveryAdmission =\n\t| { kind: \"allowed\"; reservation?: ToolFailureExecutionReservation }\n\t| { kind: \"blocked\"; record: ToolFailureMemoryRecord; exhausted: false; diagnostic?: string }\n\t| {\n\t\t\tkind: \"blocked\";\n\t\t\trecord: ToolFailureMemoryRecord;\n\t\t\texhausted: true;\n\t\t\tscope: \"operation\" | \"run\";\n\t\t\tdiagnostic?: string;\n\t };\n\ninterface FailureRecoveryState {\n\trecord: ToolFailureMemoryRecord;\n\trecoveryTargets: readonly AgentToolFailureRecoveryTarget[];\n\treservedExecutions: number;\n\tfailures: number;\n\trecoveryProbes: number;\n\tblockedReplays: number;\n\trecoveryAvailable: boolean;\n\toperationCircuitOpen: boolean;\n}\n\ntype TranscriptRecoveryReduction =\n\t| { kind: \"resolved\" }\n\t| { kind: \"ignored\"; state: FailureRecoveryState | undefined }\n\t| { kind: \"failed\"; state: FailureRecoveryState };\n\ninterface AvailableRecoveryAction {\n\ttoolName: string;\n\tkind: \"correct\" | \"repair\";\n\tinstruction: string;\n}\n\ninterface ParsedRecoveryAction {\n\tkind: \"correct\" | \"repair\";\n\tauthority: AgentToolFailureRecoveryAuthority;\n\ttargetKind: string;\n\tinstruction: string;\n\tgetEvidence?: (params: unknown, result: AgentToolResult<unknown>) => unknown;\n}\n\nexport interface ToolFailureRecoveryHalt {\n\trecord: ToolFailureMemoryRecord;\n\tdiagnostic: string;\n}\n\n/**\n * Bounded negative lookup for exact execution identities.\n *\n * A miss proves the operation has not failed while this gate has been alive. A hit only means\n * \"possibly seen\" and must be verified against the transcript, so collisions can cost a scan but\n * can never deny an execution. Keeping this separate from the hot state cache lets old exact\n * circuits survive eviction without retaining one live object graph per historical operation.\n */\nclass SeenExecutionFilter {\n\tprivate readonly bits = new Uint8Array(SEEN_EXECUTION_FILTER_BYTES);\n\n\tadd(executionKey: string): void {\n\t\tfor (const hash of getToolExecutionKeyHashParts(executionKey)) this.set(hash);\n\t}\n\n\tmightContain(executionKey: string): boolean {\n\t\tfor (const hash of getToolExecutionKeyHashParts(executionKey)) {\n\t\t\tif (!this.has(hash)) return false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tprivate set(hash: number): void {\n\t\tconst bit = (hash >>> 0) % (SEEN_EXECUTION_FILTER_BYTES * 8);\n\t\tthis.bits[bit >>> 3] |= 1 << (bit & 7);\n\t}\n\n\tprivate has(hash: number): boolean {\n\t\tconst bit = (hash >>> 0) % (SEEN_EXECUTION_FILTER_BYTES * 8);\n\t\treturn (this.bits[bit >>> 3] & (1 << (bit & 7))) !== 0;\n\t}\n}\n\n/**\n * Owns execution admission and bounded unresolved-failure budgets.\n *\n * Recovery authority is exact and tool-owned. A failed tool declares opaque backend-specific targets;\n * a loaded recovery tool may teach actions only for the same authority and target kind. Only raw\n * successful repair evidence with byte-exact scope can reopen one probe. Argument text and hooks have\n * no recovery authority.\n *\n * Run-level halt stays on the current run, but admission is never denied because unrelated operations\n * happened to fail. A bounded hot cache carries active per-operation state. A fixed-size negative\n * lookup sends an evicted exact replay back to the transcript for authoritative reconstruction, so\n * cache pressure cannot either stop new work or reopen an already-exhausted operation.\n */\nexport class ToolFailureRecoveryGate {\n\tprivate readonly statesByExecutionKey = new Map<string, FailureRecoveryState>();\n\tprivate readonly seenFailedExecutions = new SeenExecutionFilter();\n\t/** Exact successes not yet present in the transcript snapshot consulted by admission. */\n\tprivate readonly resolvedBeforeTranscriptCommit = new Set<string>();\n\tprivate transcriptMessages: readonly AgentMessage[] = [];\n\tprivate transcriptLength = 0;\n\tprivate transcriptTail: AgentMessage | undefined;\n\tprivate restoredFromTranscript = false;\n\tprivate halted: ToolFailureRecoveryHalt | undefined;\n\n\tisEmpty(): boolean {\n\t\treturn this.statesByExecutionKey.size === 0 && this.halted === undefined;\n\t}\n\n\trestoreFromMessages(messages: readonly AgentMessage[]): void {\n\t\tthis.trackTranscript(messages);\n\t\tif (this.restoredFromTranscript || !this.isEmpty()) return;\n\t\tthis.restoredFromTranscript = true;\n\t\tforEachPairedToolResult(messages, ({ tool, args, executionKey, result }) => {\n\t\t\tconst reduction = this.reduceTranscriptResult(this.statesByExecutionKey.get(executionKey), tool, args, result);\n\t\t\tif (reduction.kind === \"resolved\") {\n\t\t\t\tthis.clearResolvedState(executionKey);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (reduction.kind === \"ignored\") return;\n\t\t\tthis.seenFailedExecutions.add(executionKey);\n\t\t\tthis.retainHotState(executionKey, reduction.state);\n\t\t});\n\t}\n\n\tplanFailure(\n\t\tfailedTool: AgentTool<any>,\n\t\targs: unknown,\n\t\tfailure: AgentToolFailureEvidenceContext,\n\t\tavailableTools: readonly AgentTool<any>[],\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): ToolFailureRecoveryPlan {\n\t\tconst targets = readFailureTargets(failedTool, args, failure.failureCode);\n\t\tconst actions = readAvailableRecoveryActions(availableTools, targets);\n\t\tconst unchangedRetryRemaining = this.hasUnchangedRetryRemaining(\n\t\t\tfailedTool,\n\t\t\targs,\n\t\t\tfailure.failureCode,\n\t\t\treservation,\n\t\t);\n\t\tconst evidence = readFailureEvidence(failedTool, args, failure);\n\t\treturn {\n\t\t\ttargets,\n\t\t\tguidance: formatRecoveryGuidance(failure.failureCode, actions, unchangedRetryRemaining),\n\t\t\t...(evidence ? { evidence } : {}),\n\t\t};\n\t}\n\n\tadmit(\n\t\ttool: AgentTool<any>,\n\t\targs: unknown,\n\t\trecord: ToolFailureMemoryRecord | undefined,\n\t\tmessages: readonly AgentMessage[] = this.transcriptMessages,\n\t): ToolFailureRecoveryAdmission {\n\t\tthis.trackTranscript(messages);\n\t\tconst runHalt = this.halted;\n\t\tif (runHalt) {\n\t\t\treturn {\n\t\t\t\tkind: \"blocked\",\n\t\t\t\trecord: runHalt.record,\n\t\t\t\texhausted: true,\n\t\t\t\tscope: \"run\",\n\t\t\t\tdiagnostic: runHalt.diagnostic,\n\t\t\t};\n\t\t}\n\n\t\tconst executionKey = getToolExecutionKey(tool.name, args);\n\t\tif (this.resolvedBeforeTranscriptCommit.has(executionKey)) return { kind: \"allowed\" };\n\t\tlet state = this.getHotState(executionKey);\n\t\tif (!state && this.seenFailedExecutions.mightContain(executionKey)) {\n\t\t\tstate = this.restoreOperationFromTranscript(executionKey);\n\t\t}\n\t\tif (!state && record && getToolFailureRecordExecutionKey(record) === executionKey) {\n\t\t\tstate = this.getOrCreateState(executionKey, record, readFailureTargets(tool, args, record.failureCode));\n\t\t}\n\t\tif (!state) return { kind: \"allowed\" };\n\n\t\tif (record && getToolFailureRecordExecutionKey(record) === executionKey) state.record = record;\n\t\tif (state.operationCircuitOpen) {\n\t\t\tif (state.recoveryAvailable && state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION) {\n\t\t\t\tstate.operationCircuitOpen = false;\n\t\t\t\tstate.recoveryAvailable = false;\n\t\t\t\tstate.recoveryProbes++;\n\t\t\t\tstate.reservedExecutions++;\n\t\t\t\tstate.blockedReplays = 0;\n\t\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t\t}\n\t\t\tif (usesOperationLocalExhaustion(tool)) {\n\t\t\t\tconst diagnostic = `Operation recovery circuit remains closed after replay of ${state.record.failureCode}.`;\n\t\t\t\treturn { kind: \"blocked\", record: state.record, exhausted: true, scope: \"operation\", diagnostic };\n\t\t\t}\n\t\t\tconst diagnostic = `Run recovery circuit opened after replay of an operation whose local circuit was already open for ${state.record.failureCode}.`;\n\t\t\tthis.halted = { record: state.record, diagnostic };\n\t\t\treturn { kind: \"blocked\", record: state.record, exhausted: true, scope: \"run\", diagnostic };\n\t\t}\n\t\tconst automaticExecutionLimit =\n\t\t\tBASE_FAILURE_EXECUTIONS_PER_OPERATION + getToolExecutionUnchangedRetryLimit(state.record.failureCode);\n\t\tif (state.reservedExecutions < automaticExecutionLimit) {\n\t\t\tstate.reservedExecutions++;\n\t\t\tstate.blockedReplays = 0;\n\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t}\n\t\tif (state.recoveryAvailable && state.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION) {\n\t\t\tstate.recoveryAvailable = false;\n\t\t\tstate.recoveryProbes++;\n\t\t\tstate.reservedExecutions++;\n\t\t\tstate.blockedReplays = 0;\n\t\t\treturn { kind: \"allowed\", reservation: { executionKey } };\n\t\t}\n\n\t\tstate.blockedReplays++;\n\t\tif (state.blockedReplays >= MAX_BLOCKED_REPLAYS_PER_FAILURE) {\n\t\t\tstate.operationCircuitOpen = true;\n\t\t\tconst diagnostic = `Operation recovery circuit opened after ${state.blockedReplays} blocked replays of ${state.record.failureCode}.`;\n\t\t\treturn { kind: \"blocked\", record: state.record, exhausted: true, scope: \"operation\", diagnostic };\n\t\t}\n\t\treturn { kind: \"blocked\", record: state.record, exhausted: false };\n\t}\n\n\tapply(effect: ToolFailureRecoveryGateEffect | undefined): ToolFailureRecoveryHalt | undefined {\n\t\tif (!effect || this.halted) return undefined;\n\t\tif (effect.kind === \"success\") {\n\t\t\tthis.observeSuccess(effect.tool, effect.args, effect.evidenceResult);\n\t\t\treturn undefined;\n\t\t}\n\t\tthis.observeFailure(effect.tool, effect.record, effect.args, effect.targets ?? [], effect.reservation);\n\t\treturn this.halted;\n\t}\n\n\tisHalted(): boolean {\n\t\treturn this.halted !== undefined;\n\t}\n\n\tgetHalt(): ToolFailureRecoveryHalt | undefined {\n\t\treturn this.halted;\n\t}\n\n\tprivate hasUnchangedRetryRemaining(\n\t\ttool: AgentTool<any>,\n\t\targs: unknown,\n\t\tfailureCode: string,\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): boolean {\n\t\tconst retryLimit = getToolExecutionUnchangedRetryLimit(failureCode);\n\t\tif (retryLimit === 0) return false;\n\t\tconst executionKey = getToolExecutionKey(tool.name, args);\n\t\tconst state = this.statesByExecutionKey.get(executionKey);\n\t\tconst executionsIncludingCurrent = state\n\t\t\t? state.reservedExecutions + (reservation?.executionKey === executionKey ? 0 : 1)\n\t\t\t: 1;\n\t\treturn executionsIncludingCurrent < BASE_FAILURE_EXECUTIONS_PER_OPERATION + retryLimit;\n\t}\n\n\tprivate getOrCreateState(\n\t\texecutionKey: string,\n\t\trecord: ToolFailureMemoryRecord,\n\t\ttargets: readonly AgentToolFailureRecoveryTarget[],\n\t): FailureRecoveryState {\n\t\tconst existing = this.getHotState(executionKey);\n\t\tif (existing) return existing;\n\t\tconst state = createFailureRecoveryState(record, targets);\n\t\tthis.retainHotState(executionKey, state);\n\t\treturn state;\n\t}\n\n\tprivate observeFailure(\n\t\ttool: AgentTool<any> | undefined,\n\t\trecord: ToolFailureMemoryRecord,\n\t\targs: unknown,\n\t\ttargets: readonly AgentToolFailureRecoveryTarget[],\n\t\treservation: ToolFailureExecutionReservation | undefined,\n\t): void {\n\t\tconst executionKey = getToolExecutionKey(record.tool, args);\n\t\tthis.resolvedBeforeTranscriptCommit.delete(executionKey);\n\t\tthis.seenFailedExecutions.add(executionKey);\n\t\tconst state = this.getOrCreateState(executionKey, record, targets);\n\t\tif (this.halted) return;\n\t\tstate.record = record;\n\t\tstate.recoveryTargets = targets;\n\t\tstate.recoveryAvailable = false;\n\t\tstate.blockedReplays = 0;\n\t\tif (reservation?.executionKey !== executionKey) state.reservedExecutions++;\n\n\t\tstate.failures++;\n\n\t\tconst operationFailureLimit =\n\t\t\trecord.state === \"failed\"\n\t\t\t\t? BASE_FAILURE_EXECUTIONS_PER_OPERATION +\n\t\t\t\t\tgetToolExecutionUnchangedRetryLimit(record.failureCode) +\n\t\t\t\t\tMAX_RECOVERY_PROBES_PER_OPERATION\n\t\t\t\t: MAX_REJECTIONS_PER_OPERATION;\n\t\tif (state.failures >= operationFailureLimit) {\n\t\t\tif (usesOperationLocalExhaustion(tool)) {\n\t\t\t\tstate.operationCircuitOpen = true;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.halted = {\n\t\t\t\trecord,\n\t\t\t\tdiagnostic: `Recovery circuit opened after ${state.failures} failed outcomes for one operation.`,\n\t\t\t};\n\t\t}\n\t}\n\n\tprivate observeSuccess(tool: AgentTool<any>, args: unknown, result: AgentToolResult<any>): void {\n\t\tconst successfulExecutionKey = getToolExecutionKey(tool.name, args);\n\t\t// Tool results are appended to the transcript after the current execution batch completes.\n\t\t// Until then the last persisted failure is stale authority: remember the exact success so a\n\t\t// later sequential call in this same batch cannot resurrect that failure from the transcript.\n\t\tthis.resolvedBeforeTranscriptCommit.add(successfulExecutionKey);\n\t\tconst evidenceTargets = readRecoveryEvidenceTargets(tool, args, result);\n\t\tfor (const [executionKey, state] of this.statesByExecutionKey) {\n\t\t\tif (executionKey === successfulExecutionKey) {\n\t\t\t\tthis.clearResolvedState(executionKey);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (\n\t\t\t\tstate.recoveryProbes < MAX_RECOVERY_PROBES_PER_OPERATION &&\n\t\t\t\thasSharedRecoveryTarget(state.recoveryTargets, evidenceTargets)\n\t\t\t) {\n\t\t\t\tstate.recoveryAvailable = true;\n\t\t\t\tstate.blockedReplays = 0;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate clearResolvedState(executionKey: string): void {\n\t\tthis.statesByExecutionKey.delete(executionKey);\n\t}\n\n\tprivate trackTranscript(messages: readonly AgentMessage[]): void {\n\t\tconst tail = messages[messages.length - 1];\n\t\tif (\n\t\t\tmessages !== this.transcriptMessages ||\n\t\t\tmessages.length !== this.transcriptLength ||\n\t\t\ttail !== this.transcriptTail\n\t\t) {\n\t\t\t// An advanced transcript now authoritatively records completed results. The temporary\n\t\t\t// exact-success overlay is bounded to one uncommitted execution batch.\n\t\t\tthis.resolvedBeforeTranscriptCommit.clear();\n\t\t}\n\t\tthis.transcriptMessages = messages;\n\t\tthis.transcriptLength = messages.length;\n\t\tthis.transcriptTail = tail;\n\t}\n\n\tprivate getHotState(executionKey: string): FailureRecoveryState | undefined {\n\t\tconst state = this.statesByExecutionKey.get(executionKey);\n\t\tif (!state) return undefined;\n\t\tthis.statesByExecutionKey.delete(executionKey);\n\t\tthis.statesByExecutionKey.set(executionKey, state);\n\t\treturn state;\n\t}\n\n\tprivate retainHotState(executionKey: string, state: FailureRecoveryState): void {\n\t\tthis.statesByExecutionKey.delete(executionKey);\n\t\tthis.statesByExecutionKey.set(executionKey, state);\n\t\twhile (this.statesByExecutionKey.size > MAX_HOT_RECOVERY_STATES) {\n\t\t\tconst oldest = this.statesByExecutionKey.keys().next().value;\n\t\t\tif (oldest === undefined) break;\n\t\t\tthis.statesByExecutionKey.delete(oldest);\n\t\t}\n\t}\n\n\tprivate restoreOperationFromTranscript(executionKey: string): FailureRecoveryState | undefined {\n\t\tlet restoredState: FailureRecoveryState | undefined;\n\t\tforEachPairedToolResult(this.transcriptMessages, ({ tool, args, executionKey: candidateKey, result }) => {\n\t\t\tif (candidateKey !== executionKey) return;\n\t\t\tconst reduction = this.reduceTranscriptResult(restoredState, tool, args, result);\n\t\t\tif (reduction.kind === \"resolved\") {\n\t\t\t\trestoredState = undefined;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (reduction.kind === \"failed\") restoredState = reduction.state;\n\t\t});\n\t\tif (restoredState) this.retainHotState(executionKey, restoredState);\n\t\treturn restoredState;\n\t}\n\n\tprivate reduceTranscriptResult(\n\t\tstate: FailureRecoveryState | undefined,\n\t\ttool: string,\n\t\targs: unknown,\n\t\tresult: ToolResultMessage,\n\t): TranscriptRecoveryReduction {\n\t\tif (!result.isError) return { kind: \"resolved\" };\n\t\tconst record = restoreToolFailureRecord(result, tool, args);\n\t\tconst visibleCode = readVisibleToolFailureCode(result);\n\t\tif (isPromptScopedFailureCode(visibleCode) || isPromptScopedFailureCode(record.failureCode)) {\n\t\t\treturn { kind: \"ignored\", state };\n\t\t}\n\t\tconst next = state ?? createFailureRecoveryState(record, []);\n\t\tnext.record = record;\n\t\tif (isClosedOperationFailureCode(visibleCode)) {\n\t\t\tnext.operationCircuitOpen = true;\n\t\t\tnext.blockedReplays = MAX_BLOCKED_REPLAYS_PER_FAILURE;\n\t\t\tnext.recoveryAvailable = false;\n\t\t\treturn { kind: \"failed\", state: next };\n\t\t}\n\t\tif (visibleCode === \"repeated_failed_operation\") {\n\t\t\tnext.blockedReplays++;\n\t\t\treturn { kind: \"failed\", state: next };\n\t\t}\n\t\tnext.reservedExecutions++;\n\t\tnext.failures++;\n\t\treturn { kind: \"failed\", state: next };\n\t}\n}\n\nfunction usesOperationLocalExhaustion(tool: AgentTool<any> | undefined): boolean {\n\treturn tool?.failureRecovery?.exhaustionScope === \"operation\";\n}\n\nfunction readFailureEvidence(\n\ttool: AgentTool<any>,\n\targs: unknown,\n\tfailure: AgentToolFailureEvidenceContext,\n): string | undefined {\n\ttry {\n\t\tconst contract = tool.failureRecovery;\n\t\tconst getEvidence = contract?.getFailureEvidence;\n\t\tif (!getEvidence) return undefined;\n\t\tconst evidence = Reflect.apply(getEvidence, contract, [args, failure]);\n\t\treturn sanitizeToolFailureEvidence(evidence);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction createFailureRecoveryState(\n\trecord: ToolFailureMemoryRecord,\n\trecoveryTargets: readonly AgentToolFailureRecoveryTarget[],\n): FailureRecoveryState {\n\treturn {\n\t\trecord,\n\t\trecoveryTargets,\n\t\treservedExecutions: 0,\n\t\tfailures: 0,\n\t\trecoveryProbes: 0,\n\t\tblockedReplays: 0,\n\t\trecoveryAvailable: false,\n\t\toperationCircuitOpen: false,\n\t};\n}\n\nfunction readFailureTargets(\n\ttool: AgentTool<any>,\n\targs: unknown,\n\tfailureCode: string,\n): readonly AgentToolFailureRecoveryTarget[] {\n\ttry {\n\t\tconst contract = tool.failureRecovery;\n\t\tconst getTargets = contract?.getFailureTargets;\n\t\tif (!getTargets) return [];\n\t\tconst targets = Reflect.apply(getTargets, contract, [args, { failureCode }]);\n\t\treturn sanitizeRecoveryTargets(targets);\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nfunction sanitizeRecoveryTargets(value: unknown): readonly AgentToolFailureRecoveryTarget[] {\n\tif (!Array.isArray(value)) return [];\n\tconst targets: AgentToolFailureRecoveryTarget[] = [];\n\tfor (const candidate of value) {\n\t\tif (targets.length >= MAX_RECOVERY_TARGETS) break;\n\t\tif (!candidate || typeof candidate !== \"object\" || Array.isArray(candidate)) continue;\n\t\tconst { authority, kind, scope } = candidate as { authority?: unknown; kind?: unknown; scope?: unknown };\n\t\tif (!isAgentToolFailureRecoveryAuthority(authority) || !validTargetKind(kind) || !validTargetScope(scope)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (targets.some((target) => sameRecoveryTarget(target, { authority, kind, scope }))) continue;\n\t\ttargets.push({ authority, kind, scope });\n\t}\n\treturn targets;\n}\n\nfunction validTargetKind(value: unknown): value is string {\n\treturn (\n\t\ttypeof value === \"string\" &&\n\t\tvalue.length > 0 &&\n\t\tvalue.length <= MAX_TARGET_KIND_CHARS &&\n\t\tTARGET_KIND_PATTERN.test(value)\n\t);\n}\n\nfunction validTargetScope(value: unknown): value is string {\n\treturn (\n\t\ttypeof value === \"string\" && value.length > 0 && value.length <= MAX_TARGET_SCOPE_CHARS && !value.includes(\"\\0\")\n\t);\n}\n\nfunction readAvailableRecoveryActions(\n\ttools: readonly AgentTool<any>[],\n\ttargets: readonly AgentToolFailureRecoveryTarget[],\n): readonly AvailableRecoveryAction[] {\n\tif (targets.length === 0) return [];\n\tconst actions: AvailableRecoveryAction[] = [];\n\tconst seen = new Set<string>();\n\tfor (const tool of tools) {\n\t\tfor (const candidate of readDeclaredRecoveryActions(tool)) {\n\t\t\tif (actions.length >= MAX_RECOVERY_ACTIONS) return actions;\n\t\t\tconst action = parseRecoveryAction(candidate);\n\t\t\tif (\n\t\t\t\t!action ||\n\t\t\t\t!targets.some((target) => target.authority === action.authority && target.kind === action.targetKind)\n\t\t\t) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst instruction = truncate(action.instruction.trim(), MAX_ACTION_INSTRUCTION_CHARS);\n\t\t\tif (!instruction) continue;\n\t\t\tconst key = `${tool.name}\\0${action.kind}\\0${instruction}`;\n\t\t\tif (seen.has(key)) continue;\n\t\t\tseen.add(key);\n\t\t\tactions.push({ toolName: tool.name, kind: action.kind, instruction });\n\t\t}\n\t}\n\treturn actions;\n}\n\nfunction readDeclaredRecoveryActions(tool: AgentTool<any>): readonly unknown[] {\n\ttry {\n\t\tconst declared = tool.failureRecovery?.actions;\n\t\tif (!Array.isArray(declared)) return [];\n\t\tconst actions: unknown[] = [];\n\t\tconst count = Math.min(declared.length, MAX_RECOVERY_ACTIONS);\n\t\tfor (let index = 0; index < count; index++) actions.push(declared[index]);\n\t\treturn actions;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nfunction parseRecoveryAction(value: unknown): ParsedRecoveryAction | undefined {\n\ttry {\n\t\tif (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n\t\tconst candidate = value as {\n\t\t\tkind?: unknown;\n\t\t\tauthority?: unknown;\n\t\t\ttargetKind?: unknown;\n\t\t\tinstruction?: unknown;\n\t\t\tgetEvidence?: unknown;\n\t\t};\n\t\tif (\n\t\t\t!isAgentToolFailureRecoveryAuthority(candidate.authority) ||\n\t\t\t!validTargetKind(candidate.targetKind) ||\n\t\t\ttypeof candidate.instruction !== \"string\"\n\t\t) {\n\t\t\treturn undefined;\n\t\t}\n\t\tif (candidate.kind === \"correct\") {\n\t\t\treturn {\n\t\t\t\tkind: candidate.kind,\n\t\t\t\tauthority: candidate.authority,\n\t\t\t\ttargetKind: candidate.targetKind,\n\t\t\t\tinstruction: candidate.instruction,\n\t\t\t};\n\t\t}\n\t\tif (candidate.kind !== \"repair\" || typeof candidate.getEvidence !== \"function\") return undefined;\n\t\tconst getEvidence = candidate.getEvidence;\n\t\treturn {\n\t\t\tkind: candidate.kind,\n\t\t\tauthority: candidate.authority,\n\t\t\ttargetKind: candidate.targetKind,\n\t\t\tinstruction: candidate.instruction,\n\t\t\tgetEvidence: (params, result) => Reflect.apply(getEvidence, value, [params, result]),\n\t\t};\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction readRecoveryEvidenceTargets(\n\ttool: AgentTool<any>,\n\targs: unknown,\n\tresult: AgentToolResult<any>,\n): readonly AgentToolFailureRecoveryTarget[] {\n\tconst targets: AgentToolFailureRecoveryTarget[] = [];\n\tfor (const candidate of readDeclaredRecoveryActions(tool)) {\n\t\tconst action = parseRecoveryAction(candidate);\n\t\tif (targets.length >= MAX_RECOVERY_TARGETS || !action || action.kind !== \"repair\" || !action.getEvidence) {\n\t\t\tcontinue;\n\t\t}\n\t\tlet scopes: unknown;\n\t\ttry {\n\t\t\tscopes = action.getEvidence(args, result);\n\t\t} catch {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!Array.isArray(scopes)) continue;\n\t\tfor (const scope of scopes) {\n\t\t\tif (targets.length >= MAX_RECOVERY_TARGETS) break;\n\t\t\tif (!validTargetScope(scope)) continue;\n\t\t\tconst target = { authority: action.authority, kind: action.targetKind, scope };\n\t\t\tif (!targets.some((candidate) => sameRecoveryTarget(candidate, target))) targets.push(target);\n\t\t}\n\t}\n\treturn targets;\n}\n\nfunction formatRecoveryGuidance(\n\tfailureCode: string,\n\tactions: readonly AvailableRecoveryAction[],\n\tunchangedRetryRemaining: boolean,\n): string {\n\tconst hasTimeoutRetryPolicy = getToolExecutionUnchangedRetryLimit(failureCode) > 0;\n\tconst timeoutPolicy = hasTimeoutRetryPolicy\n\t\t? unchangedRetryRemaining\n\t\t\t? \"Timeout policy allows 1 unchanged retry; if it fails, never retry unchanged.\"\n\t\t\t: \"Timeout unchanged retry exhausted; never retry unchanged.\"\n\t\t: undefined;\n\tif (actions.length === 0) {\n\t\tif (timeoutPolicy) return `${timeoutPolicy} Change/narrow operation, or report blocker.`;\n\t\treturn \"No loaded tool declares recovery. Never retry unchanged. Use materially different operation justified by diagnostic/schema, or report blocker.\";\n\t}\n\tconst available = actions.map((action) => `${action.toolName} ${action.kind}: ${action.instruction}`).join(\" \");\n\tconst hasRepair = actions.some((action) => action.kind === \"repair\");\n\tconst authority = hasRepair\n\t\t? \"Only exact matching repair evidence grants 1 probe; else change operation.\"\n\t\t: \"Actions require changed operation; unchanged remains blocked.\";\n\treturn truncate(\n\t\t`${timeoutPolicy ? `${timeoutPolicy} ` : \"\"}Loaded actions: ${available} ${authority}`,\n\t\tMAX_RECOVERY_GUIDANCE_CHARS,\n\t);\n}\n\nfunction truncate(value: string, maxChars: number): string {\n\tif (value.length <= maxChars) return value;\n\treturn `${value.slice(0, maxChars - 1)}…`;\n}\n\nfunction sameRecoveryTarget(left: AgentToolFailureRecoveryTarget, right: AgentToolFailureRecoveryTarget): boolean {\n\treturn left.authority === right.authority && left.kind === right.kind && left.scope === right.scope;\n}\n\nfunction hasSharedRecoveryTarget(\n\tleft: readonly AgentToolFailureRecoveryTarget[],\n\tright: readonly AgentToolFailureRecoveryTarget[],\n): boolean {\n\tif (left.length > right.length) return hasSharedRecoveryTarget(right, left);\n\treturn left.some((target) => right.some((candidate) => sameRecoveryTarget(target, candidate)));\n}\n"]}