@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.
- package/dist/agent-loop.d.ts.map +1 -1
- package/dist/agent-loop.js +16 -12
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +1 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +2 -2
- package/dist/agent.js.map +1 -1
- package/dist/session/session-manager.d.ts +4 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manager.js +45 -4
- package/dist/session/session-manager.js.map +1 -1
- package/dist/tool-failure-memory.d.ts +7 -1
- package/dist/tool-failure-memory.d.ts.map +1 -1
- package/dist/tool-failure-memory.js +62 -34
- package/dist/tool-failure-memory.js.map +1 -1
- package/dist/tool-failure-recovery-gate.d.ts +21 -10
- package/dist/tool-failure-recovery-gate.d.ts.map +1 -1
- package/dist/tool-failure-recovery-gate.js +194 -127
- package/dist/tool-failure-recovery-gate.js.map +1 -1
- package/dist/types.d.ts +23 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -2
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
|
@@ -19,6 +19,8 @@ export interface ToolFailureMemoryRecord {
|
|
|
19
19
|
phase: ToolFailurePhase;
|
|
20
20
|
failureCode: string;
|
|
21
21
|
diagnostic?: string;
|
|
22
|
+
/** Bounded tool-owned source evidence needed to construct a changed operation. */
|
|
23
|
+
evidence?: string;
|
|
22
24
|
correction: string;
|
|
23
25
|
attemptMemory?: "discard";
|
|
24
26
|
}
|
|
@@ -37,8 +39,12 @@ export interface ToolFailureDirectiveDetails {
|
|
|
37
39
|
}
|
|
38
40
|
export type ToolFailureResultDetails = ToolFailureMemoryDetails | ToolFailureDirectiveDetails;
|
|
39
41
|
export type ToolFailureMemoryTracker = Map<string, ToolFailureMemoryRecord>;
|
|
42
|
+
/** Normalize and bound tool-owned evidence at every live and persisted ingress. */
|
|
43
|
+
export declare function sanitizeToolFailureEvidence(value: unknown): string | undefined;
|
|
40
44
|
export declare function normalizeToolSignature(pairs: Array<[string, unknown]>): string;
|
|
41
45
|
export declare function getToolExecutionKey(tool: string, args: unknown): string;
|
|
46
|
+
/** Read the four hash words already encoded by this module's exact execution-key owner. */
|
|
47
|
+
export declare function getToolExecutionKeyHashParts(executionKey: string): readonly [number, number, number, number];
|
|
42
48
|
export declare function getToolFailureRecordExecutionKey(record: ToolFailureMemoryRecord): string | undefined;
|
|
43
49
|
export declare function getUnresolvedToolFailure(tracker: ToolFailureMemoryTracker, tool: string, args: unknown): ToolFailureMemoryRecord | undefined;
|
|
44
50
|
export declare function readVisibleToolFailureCode(result: ToolResultMessage): string | undefined;
|
|
@@ -74,7 +80,7 @@ export interface ToolFailureTelemetry {
|
|
|
74
80
|
/** Read only bounded failure identity and guidance; operation arguments never cross this telemetry boundary. */
|
|
75
81
|
export declare function readToolFailureTelemetry(details: unknown): ToolFailureTelemetry | undefined;
|
|
76
82
|
export declare function createToolFailureMemoryTracker(messages: AgentMessage[]): ToolFailureMemoryTracker;
|
|
77
|
-
export declare function rememberToolFailure(tracker: ToolFailureMemoryTracker, tool: string, args: unknown, state: ToolFailureState, failureCode: string, correction: string, diagnostic?: string, phase?: ToolFailurePhase): ToolFailureMemoryRecord;
|
|
83
|
+
export declare function rememberToolFailure(tracker: ToolFailureMemoryTracker, tool: string, args: unknown, state: ToolFailureState, failureCode: string, correction: string, diagnostic?: string, phase?: ToolFailurePhase, evidence?: string): ToolFailureMemoryRecord;
|
|
78
84
|
export declare function clearToolFailure(tracker: ToolFailureMemoryTracker, tool: string, args: unknown): void;
|
|
79
85
|
export declare function createToolFailureResult(record: ToolFailureMemoryRecord, terminate?: boolean): AgentToolResult<ToolFailureResultDetails>;
|
|
80
86
|
export declare function createRepeatedToolFailureResult(record: ToolFailureMemoryRecord): AgentToolResult<ToolFailureMemoryDetails>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-failure-memory.d.ts","sourceRoot":"","sources":["../src/tool-failure-memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,gBAAgB,EACrB,MAAM,wCAAwC,CAAC;AAChD,OAAO,KAAK,EAAoB,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAKnF,OAAO,KAAK,EAAE,YAAY,EAAiB,eAAe,EAAE,MAAM,YAAY,CAAC;AAG/E,QAAA,MAAM,2BAA2B,IAAI,CAAC;AACtC,QAAA,MAAM,8BAA8B,IAAI,CAAC;AACzC,QAAA,MAAM,0BAA0B,eAAoC,CAAC;
|
|
1
|
+
{"version":3,"file":"tool-failure-memory.d.ts","sourceRoot":"","sources":["../src/tool-failure-memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,gBAAgB,EACrB,MAAM,wCAAwC,CAAC;AAChD,OAAO,KAAK,EAAoB,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAKnF,OAAO,KAAK,EAAE,YAAY,EAAiB,eAAe,EAAE,MAAM,YAAY,CAAC;AAG/E,QAAA,MAAM,2BAA2B,IAAI,CAAC;AACtC,QAAA,MAAM,8BAA8B,IAAI,CAAC;AACzC,QAAA,MAAM,0BAA0B,eAAoC,CAAC;AAmBrE,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,UAAU,CAAC;AAErD,MAAM,WAAW,uBAAuB;IACvC,OAAO,EAAE,OAAO,2BAA2B,CAAC;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,qFAAqF;IACrF,QAAQ,CAAC,CAAC,0BAA0B,CAAC,CAAC,EAAE,MAAM,CAAC;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,gBAAgB,CAAC;IACxB,KAAK,EAAE,gBAAgB,CAAC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CACxD,MAAM,EAAE,uBAAuB,EAC/B,UAAU,EAAE,MAAM,GAChB,eAAe,CAAC,wBAAwB,CAAC,CAkB3C;AAcD,wBAAgB,0BAA0B,CACzC,QAAQ,EAAE,YAAY,EAAE,EACxB,YAAY,EAAE,MAAM,GAClB;IAAE,QAAQ,EAAE,YAAY,EAAE,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAgDpD"}
|
|
@@ -8,6 +8,8 @@ const MAX_OPERATION_CHARS = 240;
|
|
|
8
8
|
const MAX_FAILURE_CODE_CHARS = 48;
|
|
9
9
|
const MAX_DIAGNOSTIC_CHARS = 240;
|
|
10
10
|
const MAX_CORRECTION_CHARS = 320;
|
|
11
|
+
const MAX_TOOL_FAILURE_EVIDENCE_CHARS = 1_600;
|
|
12
|
+
const MAX_ACTIVE_FAILURE_EVIDENCE_CHARS = 2_400;
|
|
11
13
|
const MAX_TOOL_NAME_CHARS = 64;
|
|
12
14
|
const TOOL_SIGNATURE_HEX_CHARS = 32;
|
|
13
15
|
const MAX_ACTIVE_FAILURES = 8;
|
|
@@ -25,6 +27,13 @@ function truncate(value, maxChars) {
|
|
|
25
27
|
end--;
|
|
26
28
|
return `${value.slice(0, end)}…`;
|
|
27
29
|
}
|
|
30
|
+
/** Normalize and bound tool-owned evidence at every live and persisted ingress. */
|
|
31
|
+
export function sanitizeToolFailureEvidence(value) {
|
|
32
|
+
if (typeof value !== "string")
|
|
33
|
+
return undefined;
|
|
34
|
+
const sanitized = sanitizeBinaryOutput(value).trim();
|
|
35
|
+
return sanitized ? truncate(sanitized, MAX_TOOL_FAILURE_EVIDENCE_CHARS) : undefined;
|
|
36
|
+
}
|
|
28
37
|
function truncateMiddle(value, maxChars) {
|
|
29
38
|
if (value.length <= maxChars)
|
|
30
39
|
return value;
|
|
@@ -312,6 +321,20 @@ function getToolFailureKey(tool, args) {
|
|
|
312
321
|
export function getToolExecutionKey(tool, args) {
|
|
313
322
|
return toolOperationKey(tool, args, false);
|
|
314
323
|
}
|
|
324
|
+
/** Read the four hash words already encoded by this module's exact execution-key owner. */
|
|
325
|
+
export function getToolExecutionKeyHashParts(executionKey) {
|
|
326
|
+
const separator = executionKey.lastIndexOf(":");
|
|
327
|
+
const signature = executionKey.slice(separator + 1);
|
|
328
|
+
if (separator < 0 || signature.length !== TOOL_SIGNATURE_HEX_CHARS || !/^[0-9a-f]+$/.test(signature)) {
|
|
329
|
+
throw new TypeError("Tool execution key has an invalid hash signature.");
|
|
330
|
+
}
|
|
331
|
+
return [
|
|
332
|
+
Number.parseInt(signature.slice(0, 8), 16),
|
|
333
|
+
Number.parseInt(signature.slice(8, 16), 16),
|
|
334
|
+
Number.parseInt(signature.slice(16, 24), 16),
|
|
335
|
+
Number.parseInt(signature.slice(24, 32), 16),
|
|
336
|
+
];
|
|
337
|
+
}
|
|
315
338
|
export function getToolFailureRecordExecutionKey(record) {
|
|
316
339
|
return record[TOOL_FAILURE_EXECUTION_KEY];
|
|
317
340
|
}
|
|
@@ -569,6 +592,7 @@ function readFailureRecord(details) {
|
|
|
569
592
|
return undefined;
|
|
570
593
|
}
|
|
571
594
|
const diagnostic = typeof candidate.diagnostic === "string" ? truncate(candidate.diagnostic, MAX_DIAGNOSTIC_CHARS) : undefined;
|
|
595
|
+
const evidence = sanitizeToolFailureEvidence(candidate.evidence);
|
|
572
596
|
const retainedCorrection = typeof candidate.correction === "string" ? truncate(candidate.correction, MAX_CORRECTION_CHARS) : undefined;
|
|
573
597
|
const correction = candidate.state === "failed" && retainedCorrection === LEGACY_GENERIC_EXECUTION_CORRECTION
|
|
574
598
|
? fallbackFailureGuidance("failed", diagnostic !== undefined, "execution")
|
|
@@ -592,6 +616,7 @@ function readFailureRecord(details) {
|
|
|
592
616
|
phase,
|
|
593
617
|
failureCode: boundedFailureCode(candidate.failureCode),
|
|
594
618
|
diagnostic,
|
|
619
|
+
evidence,
|
|
595
620
|
correction,
|
|
596
621
|
};
|
|
597
622
|
}
|
|
@@ -736,6 +761,7 @@ function analyzeToolFailureContext(messages) {
|
|
|
736
761
|
phase: retained?.phase ?? assessment?.phase ?? inferToolFailurePhase(state, "tool_error"),
|
|
737
762
|
failureCode: retained?.failureCode ?? assessment?.failureCode ?? "tool_error",
|
|
738
763
|
diagnostic: retained?.diagnostic ?? assessment?.diagnostic,
|
|
764
|
+
evidence: retained?.evidence,
|
|
739
765
|
correction: retained?.correction ??
|
|
740
766
|
assessment?.guidance ??
|
|
741
767
|
fallbackFailureGuidance(state, false, inferToolFailurePhase(state, "tool_error")),
|
|
@@ -826,59 +852,50 @@ function analyzeToolFailureContext(messages) {
|
|
|
826
852
|
export function createToolFailureMemoryTracker(messages) {
|
|
827
853
|
return new Map(analyzeToolFailureContext(messages).activeRecords.map((record) => [record.failureKey, record]));
|
|
828
854
|
}
|
|
829
|
-
export function rememberToolFailure(tracker, tool, args, state, failureCode, correction, diagnostic, phase = inferToolFailurePhase(state, failureCode)) {
|
|
855
|
+
export function rememberToolFailure(tracker, tool, args, state, failureCode, correction, diagnostic, phase = inferToolFailurePhase(state, failureCode), evidence) {
|
|
830
856
|
let kindCount = 1;
|
|
831
857
|
for (const previous of tracker.values()) {
|
|
832
858
|
if (previous.tool === tool) {
|
|
833
859
|
kindCount = Math.max(kindCount, (previous.kindMistakes ?? previous.occurrence) + 1);
|
|
834
860
|
}
|
|
835
861
|
}
|
|
836
|
-
|
|
862
|
+
const isDiscard = getToolExecutionAttemptMemory(failureCode) === "discard";
|
|
863
|
+
if (isDiscard) {
|
|
837
864
|
for (const [failureKey, previous] of tracker) {
|
|
838
865
|
if (previous.tool === tool && previous.failureCode === failureCode)
|
|
839
866
|
tracker.delete(failureKey);
|
|
840
867
|
}
|
|
841
|
-
return {
|
|
842
|
-
version: TOOL_FAILURE_MEMORY_VERSION,
|
|
843
|
-
failureKey: `directive:${boundedFailureCode(failureCode)}`,
|
|
844
|
-
[TOOL_FAILURE_EXECUTION_KEY]: getToolExecutionKey(tool, args),
|
|
845
|
-
tool: truncate(tool, MAX_TOOL_NAME_CHARS),
|
|
846
|
-
operation: "[discarded]",
|
|
847
|
-
occurrence: 1,
|
|
848
|
-
kindMistakes: kindCount,
|
|
849
|
-
mistakeKind: tool,
|
|
850
|
-
state,
|
|
851
|
-
phase,
|
|
852
|
-
failureCode: boundedFailureCode(failureCode),
|
|
853
|
-
diagnostic: diagnostic ? truncate(diagnostic, MAX_DIAGNOSTIC_CHARS) : undefined,
|
|
854
|
-
correction: truncate(correction, MAX_CORRECTION_CHARS),
|
|
855
|
-
attemptMemory: "discard",
|
|
856
|
-
};
|
|
857
868
|
}
|
|
858
|
-
const identity = operationIdentity(tool, args);
|
|
859
|
-
const previous = tracker.get(identity.failureKey);
|
|
869
|
+
const identity = isDiscard ? undefined : operationIdentity(tool, args);
|
|
870
|
+
const previous = identity ? tracker.get(identity.failureKey) : undefined;
|
|
860
871
|
const record = {
|
|
861
872
|
version: TOOL_FAILURE_MEMORY_VERSION,
|
|
862
|
-
failureKey: identity.failureKey
|
|
863
|
-
[TOOL_FAILURE_EXECUTION_KEY]: identity.executionKey,
|
|
864
|
-
tool: identity.tool,
|
|
865
|
-
operation: identity.operation,
|
|
866
|
-
occurrence: (previous?.occurrence ?? 0) + 1,
|
|
873
|
+
failureKey: identity ? identity.failureKey : `directive:${boundedFailureCode(failureCode)}`,
|
|
874
|
+
[TOOL_FAILURE_EXECUTION_KEY]: identity ? identity.executionKey : getToolExecutionKey(tool, args),
|
|
875
|
+
tool: identity ? identity.tool : truncate(tool, MAX_TOOL_NAME_CHARS),
|
|
876
|
+
operation: identity ? identity.operation : "[discarded]",
|
|
877
|
+
occurrence: isDiscard ? 1 : (previous?.occurrence ?? 0) + 1,
|
|
867
878
|
kindMistakes: kindCount,
|
|
868
879
|
mistakeKind: tool,
|
|
869
880
|
state,
|
|
870
881
|
phase,
|
|
871
882
|
failureCode: boundedFailureCode(failureCode),
|
|
872
883
|
diagnostic: diagnostic ? truncate(diagnostic, MAX_DIAGNOSTIC_CHARS) : undefined,
|
|
884
|
+
evidence: sanitizeToolFailureEvidence(evidence),
|
|
873
885
|
correction: truncate(correction, MAX_CORRECTION_CHARS),
|
|
886
|
+
...(isDiscard ? { attemptMemory: "discard" } : {}),
|
|
874
887
|
};
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
888
|
+
if (isDiscard)
|
|
889
|
+
return record;
|
|
890
|
+
if (identity) {
|
|
891
|
+
tracker.delete(identity.failureKey);
|
|
892
|
+
tracker.set(identity.failureKey, record);
|
|
893
|
+
while (tracker.size > MAX_TRACKED_FAILURES) {
|
|
894
|
+
const oldest = tracker.keys().next().value;
|
|
895
|
+
if (oldest === undefined)
|
|
896
|
+
break;
|
|
897
|
+
tracker.delete(oldest);
|
|
898
|
+
}
|
|
882
899
|
}
|
|
883
900
|
return record;
|
|
884
901
|
}
|
|
@@ -894,7 +911,7 @@ function failureGuidance(record) {
|
|
|
894
911
|
? { repair: record.correction }
|
|
895
912
|
: { next_action: record.correction };
|
|
896
913
|
}
|
|
897
|
-
function formatRecordJson(record, includeOperation = false) {
|
|
914
|
+
function formatRecordJson(record, includeOperation = false, evidence = record.evidence) {
|
|
898
915
|
const guidance = failureGuidance(record);
|
|
899
916
|
return JSON.stringify({
|
|
900
917
|
...mandatoryToolFailureRecoveryMetadata(),
|
|
@@ -908,6 +925,7 @@ function formatRecordJson(record, includeOperation = false) {
|
|
|
908
925
|
...(includeOperation ? { operation: record.operation } : {}),
|
|
909
926
|
failure_code: record.failureCode,
|
|
910
927
|
...(record.diagnostic ? { diagnostic: record.diagnostic } : {}),
|
|
928
|
+
...(evidence ? { evidence } : {}),
|
|
911
929
|
...guidance,
|
|
912
930
|
...(record.attemptMemory === "discard" ? { attempt_memory: "discarded" } : {}),
|
|
913
931
|
});
|
|
@@ -1006,7 +1024,17 @@ export function sanitizeToolFailureContext(messages, systemPrompt) {
|
|
|
1006
1024
|
const kindSummary = Object.entries(analysis.kindMistakesSummary)
|
|
1007
1025
|
.map(([kind, count]) => `${kind}:${count}`)
|
|
1008
1026
|
.join(", ");
|
|
1009
|
-
|
|
1027
|
+
let remainingEvidenceChars = MAX_ACTIVE_FAILURE_EVIDENCE_CHARS;
|
|
1028
|
+
const evidenceByFailureKey = new Map();
|
|
1029
|
+
for (let index = records.length - 1; index >= 0 && remainingEvidenceChars > 1; index--) {
|
|
1030
|
+
const record = records[index];
|
|
1031
|
+
if (!record.evidence)
|
|
1032
|
+
continue;
|
|
1033
|
+
const retained = truncate(record.evidence, remainingEvidenceChars);
|
|
1034
|
+
evidenceByFailureKey.set(record.failureKey, retained);
|
|
1035
|
+
remainingEvidenceChars -= retained.length;
|
|
1036
|
+
}
|
|
1037
|
+
const lines = records.map((record) => escapePromptData(formatRecordJson(record, true, evidenceByFailureKey.get(record.failureKey) ?? null)));
|
|
1010
1038
|
for (const directive of analysis.activeDirectives) {
|
|
1011
1039
|
lines.push(escapePromptData(JSON.stringify({
|
|
1012
1040
|
...mandatoryToolFailureRecoveryMetadata(),
|