@unblocklabs/unblock-memory 0.12.3 → 0.13.0
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/README.md +19 -18
- package/dist/src/analysis.js +1 -1
- package/dist/src/cluster-review.d.ts +2 -0
- package/dist/src/cluster-review.js +3 -3
- package/dist/src/config.d.ts +9 -6
- package/dist/src/config.js +31 -20
- package/dist/src/curation.d.ts +1 -1
- package/dist/src/decision-client.d.ts +39 -0
- package/dist/src/decision-client.js +74 -0
- package/dist/src/{typesafe.d.ts → decision-ranking.d.ts} +15 -18
- package/dist/src/decision-ranking.js +154 -0
- package/dist/src/{typesafe-review.d.ts → decision-review.d.ts} +13 -9
- package/dist/src/decision-review.js +192 -0
- package/dist/src/diagnostics.d.ts +5 -5
- package/dist/src/emotion-baselines.d.ts +28 -3
- package/dist/src/emotion-baselines.js +309 -113
- package/dist/src/evidence-review.d.ts +22 -8
- package/dist/src/evidence-review.js +4 -4
- package/dist/src/extraction-chunk.d.ts +3 -0
- package/dist/src/extraction-chunk.js +23 -5
- package/dist/src/extraction-model.d.ts +2 -0
- package/dist/src/extraction-model.js +73 -48
- package/dist/src/extraction-runtime.d.ts +3 -1
- package/dist/src/extraction-runtime.js +43 -16
- package/dist/src/extraction-source.d.ts +1 -1
- package/dist/src/extraction-source.js +19 -11
- package/dist/src/extraction-store.d.ts +28 -3
- package/dist/src/extraction-store.js +145 -23
- package/dist/src/extraction-worker.d.ts +1 -0
- package/dist/src/extraction-worker.js +246 -93
- package/dist/src/inside-out-runtime.js +41 -29
- package/dist/src/inside-out-store.d.ts +2 -2
- package/dist/src/inside-out-store.js +110 -69
- package/dist/src/inside-out.d.ts +10 -9
- package/dist/src/inside-out.js +190 -105
- package/dist/src/manager.d.ts +22 -9
- package/dist/src/manager.js +275 -169
- package/dist/src/memory-search.d.ts +4 -2
- package/dist/src/memory-search.js +5 -5
- package/dist/src/memory-whisperer.d.ts +1 -1
- package/dist/src/memory-whisperer.js +183 -125
- package/dist/src/my-turn.d.ts +1 -1
- package/dist/src/my-turn.js +87 -68
- package/dist/src/people-background.d.ts +4 -0
- package/dist/src/people-background.js +29 -6
- package/dist/src/people-dossier-review.d.ts +20 -8
- package/dist/src/people-dossier-review.js +11 -8
- package/dist/src/people-hooks.js +127 -22
- package/dist/src/people-learning-cron.d.ts +1 -1
- package/dist/src/people-learning-cron.js +106 -57
- package/dist/src/people-learning-inventory.d.ts +1 -0
- package/dist/src/people-learning-inventory.js +281 -171
- package/dist/src/people-learning-prompt.js +1 -1
- package/dist/src/people-learning-runtime.d.ts +1 -1
- package/dist/src/people-learning-runtime.js +360 -203
- package/dist/src/people-learning-store.d.ts +27 -4
- package/dist/src/people-learning-store.js +312 -96
- package/dist/src/people-primer-tool.js +8 -6
- package/dist/src/people-primer.d.ts +3 -1
- package/dist/src/people-primer.js +61 -44
- package/dist/src/people-store.d.ts +18 -21
- package/dist/src/people-store.js +186 -63
- package/dist/src/people-tools.js +136 -92
- package/dist/src/plugin.js +16 -16
- package/dist/src/quality-audit.d.ts +2 -0
- package/dist/src/quality-audit.js +8 -7
- package/dist/src/response-audit.d.ts +1 -1
- package/dist/src/response-audit.js +76 -43
- package/dist/src/response-episodes.d.ts +1 -1
- package/dist/src/response-episodes.js +12 -17
- package/dist/src/response-judge.d.ts +151 -145
- package/dist/src/response-judge.js +535 -204
- package/dist/src/response-reviews.d.ts +29 -1
- package/dist/src/response-reviews.js +110 -13
- package/dist/src/response-runtime.js +8 -8
- package/dist/src/response-stages.d.ts +50 -53
- package/dist/src/response-stages.js +10 -4
- package/dist/src/response-store.d.ts +7 -6
- package/dist/src/response-store.js +332 -210
- package/dist/src/review-tools.d.ts +8 -2
- package/dist/src/review-tools.js +18 -18
- package/dist/src/runtime.d.ts +7 -4
- package/dist/src/runtime.js +624 -98
- package/dist/src/safety-whisperer.d.ts +2 -2
- package/dist/src/safety-whisperer.js +88 -71
- package/dist/src/session-sync.d.ts +3 -1
- package/dist/src/session-sync.js +200 -95
- package/dist/src/skill-whisperer.d.ts +1 -1
- package/dist/src/skill-whisperer.js +52 -30
- package/dist/src/training-gate.d.ts +8 -21
- package/dist/src/training-gate.js +32 -35
- package/dist/src/training-judge.d.ts +9 -16
- package/dist/src/training-judge.js +5 -5
- package/dist/src/training-queries.d.ts +13 -8
- package/dist/src/training-queries.js +370 -228
- package/dist/src/training-retrieval.d.ts +14 -24
- package/dist/src/training-retrieval.js +182 -136
- package/dist/src/training-runtime.d.ts +3 -0
- package/dist/src/training-runtime.js +76 -18
- package/dist/src/training-store.d.ts +18 -3
- package/dist/src/training-store.js +138 -50
- package/dist/src/training.js +8 -11
- package/dist/src/whisperer-context.d.ts +14 -0
- package/dist/src/whisperer-context.js +22 -1
- package/docs/configuration.md +76 -63
- package/docs/extraction.md +9 -7
- package/docs/memory-training.md +7 -6
- package/docs/people-learning.md +2 -2
- package/docs/peoplesql.md +6 -6
- package/docs/response-audit.md +10 -8
- package/docs/retrieval.md +40 -37
- package/openclaw.plugin.json +40 -26
- package/package.json +3 -2
- package/skills/memory-curator/SKILL.md +1 -1
- package/skills/people-whisperer/SKILL.md +1 -1
- package/dist/src/typesafe-client.d.ts +0 -30
- package/dist/src/typesafe-client.js +0 -92
- package/dist/src/typesafe-review.js +0 -154
- package/dist/src/typesafe.js +0 -147
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { MEMORY_PASSAGE_CHARS, duplicateMemoryPassage } from "./memory-passage.js";
|
|
3
|
-
import { judgeMemoryPassage, memoryUsefulnessRequest } from "./
|
|
3
|
+
import { judgeMemoryPassage, memoryUsefulnessRequest } from "./decision-ranking.js";
|
|
4
4
|
import { startTiming, retrievalParts, unaccounted } from "./retrieval-timing.js";
|
|
5
5
|
export function memoryPassageId(text) {
|
|
6
6
|
return createHash("sha256").update(text.replace(/\s+/gu, " ").trim()).digest("hex");
|
|
@@ -49,14 +49,14 @@ export async function searchMemory(manager, queries, options) {
|
|
|
49
49
|
const fallback = (reason) => ({
|
|
50
50
|
...timings, ...observation, rankingMode: "retrieval", fallbackReason: reason,
|
|
51
51
|
// Stable retrieval-score ordering (fused RRF scores from discovery) before maxResults truncation.
|
|
52
|
-
// These retrieval scores are not
|
|
52
|
+
// These retrieval scores are not decision model usefulness probabilities.
|
|
53
53
|
results: [...candidates].sort((a, b) => b.score - a.score).slice(0, options.maxResults ?? 5),
|
|
54
54
|
});
|
|
55
55
|
if (!options.apiKey) {
|
|
56
56
|
signal?.throwIfAborted();
|
|
57
57
|
if (options.allowRetrievalFallback)
|
|
58
58
|
return fallback("key_unavailable");
|
|
59
|
-
return { ...timings, ...observation, rankingMode: "
|
|
59
|
+
return { ...timings, ...observation, rankingMode: "decision", results: [] };
|
|
60
60
|
}
|
|
61
61
|
const maxJudge = typeof options.maxJudgeCandidates === "number" && Number.isFinite(options.maxJudgeCandidates)
|
|
62
62
|
? Math.max(0, Math.floor(options.maxJudgeCandidates))
|
|
@@ -75,7 +75,7 @@ export async function searchMemory(manager, queries, options) {
|
|
|
75
75
|
excerpt: hit.snippet, corpus: hit.corpus, sourcePath: hit.path,
|
|
76
76
|
dates: [...new Set(hit.sessionMessages?.flatMap(message => message.timestamp ? [message.timestamp] : [])
|
|
77
77
|
?? (hit.messageTimestamp ? [hit.messageTimestamp] : []))],
|
|
78
|
-
}, asOf), { ...options, apiKey });
|
|
78
|
+
}, asOf, options.model), { ...options, apiKey });
|
|
79
79
|
signal?.throwIfAborted();
|
|
80
80
|
observation.requestsSucceeded++;
|
|
81
81
|
options.onJudgment?.({ candidateIndex, elapsedMs: performance.now() - requestStarted });
|
|
@@ -99,6 +99,6 @@ export async function searchMemory(manager, queries, options) {
|
|
|
99
99
|
observation.judgeMs = timings.judgeMs ?? 0;
|
|
100
100
|
if (options.allowRetrievalFallback && judgePool.length && !observation.requestsSucceeded)
|
|
101
101
|
return fallback("all_judgments_failed");
|
|
102
|
-
return { ...timings, ...observation, rankingMode: "
|
|
102
|
+
return { ...timings, ...observation, rankingMode: "decision", results: judged.filter(hit => hit.score >= (options.minUsefulness ?? 0.7))
|
|
103
103
|
.sort((a, b) => b.score - a.score).slice(0, options.maxResults ?? 5) };
|
|
104
104
|
}
|
|
@@ -10,5 +10,5 @@ type MemoryWhispererRuntime = {
|
|
|
10
10
|
manager: MemoryCandidateManager | null;
|
|
11
11
|
}>;
|
|
12
12
|
};
|
|
13
|
-
export declare function registerMemoryWhisperer(api: OpenClawPluginApi, runtime: MemoryWhispererRuntime, config: UnblockMemoryConfig["memoryWhisperer"],
|
|
13
|
+
export declare function registerMemoryWhisperer(api: OpenClawPluginApi, runtime: MemoryWhispererRuntime, config: UnblockMemoryConfig["memoryWhisperer"], decisionModel: UnblockMemoryConfig["decisionModel"], diagnostics?: WhispererDiagnostics): Parameters<typeof api.on<"before_prompt_build">>[1] | undefined;
|
|
14
14
|
export {};
|
|
@@ -1,20 +1,104 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { complementaryIndices, reviewMemoryRedundancy } from "./
|
|
1
|
+
import { resolveDecisionCredential, DecisionRequestError } from "./decision-client.js";
|
|
2
|
+
import { complementaryIndices, reviewMemoryRedundancy } from "./decision-review.js";
|
|
3
3
|
import { ApiQueryGenerator, compactJudgeConversation, fallbackQueries, QueryApiError, queryConversation } from "./query-generator.js";
|
|
4
4
|
import { QueryInputBudgetError } from "./query-contract.js";
|
|
5
5
|
import { searchMemory, memoryPassageId } from "./memory-search.js";
|
|
6
6
|
import { startTiming, timeStage } from "./retrieval-timing.js";
|
|
7
|
+
import { abortable, abortedByTimeout } from "./abortable.js";
|
|
7
8
|
const MAX_JUDGE_CANDIDATES = 6;
|
|
8
9
|
const MAX_GENERATION_TIMEOUT_MS = 1_500;
|
|
9
10
|
const MAX_PAYLOAD_CHARS = 5_000;
|
|
11
|
+
const MAX_SESSIONS = 1_000;
|
|
12
|
+
const MAX_SEEN_RUNS = 64;
|
|
10
13
|
function encodeHints(items) {
|
|
11
14
|
return JSON.stringify(items).replace(/</gu, "\\u003c").replace(/>/gu, "\\u003e");
|
|
12
15
|
}
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
function failureFields(error) {
|
|
17
|
+
return error instanceof DecisionRequestError || error instanceof QueryApiError
|
|
18
|
+
? { errorCode: error.code, httpStatus: error.status }
|
|
19
|
+
: { errorCode: "unexpected" };
|
|
20
|
+
}
|
|
21
|
+
async function generateQueriesWithFallback(params) {
|
|
22
|
+
const { generator, conversation, timeoutMs, signal, turn, agentId, diagnostics, log } = params;
|
|
23
|
+
turn.stage = "generation";
|
|
24
|
+
const generationStarted = performance.now();
|
|
25
|
+
const endGeneration = startTiming("generationMs", turn.measurement, signal);
|
|
26
|
+
const generationTimeoutMs = Math.min(MAX_GENERATION_TIMEOUT_MS, Math.max(1, Math.floor(timeoutMs / 2)));
|
|
27
|
+
const generationSignal = AbortSignal.any([signal, AbortSignal.timeout(generationTimeoutMs)]);
|
|
28
|
+
try {
|
|
29
|
+
const queries = await generator.generate(conversation, generationSignal);
|
|
30
|
+
if (signal.aborted)
|
|
31
|
+
return;
|
|
32
|
+
diagnostics?.record(agentId, "memory", "queries_generated");
|
|
33
|
+
return queries;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (signal.aborted)
|
|
37
|
+
return;
|
|
38
|
+
turn.queryFallback = true;
|
|
39
|
+
log("warn", "generation_fallback", {
|
|
40
|
+
...failureFields(error),
|
|
41
|
+
elapsedMs: performance.now() - generationStarted,
|
|
42
|
+
timeoutMs: generationTimeoutMs,
|
|
43
|
+
});
|
|
44
|
+
diagnostics?.record(agentId, "memory", "generation_fallback");
|
|
45
|
+
return fallbackQueries(conversation);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
endGeneration();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function selectComplementaryHints(params) {
|
|
52
|
+
const { ranked, config, decisionModel, apiKey, signal, turn, agentId, diagnostics, log } = params;
|
|
53
|
+
let selected = ranked.slice(0, config.maxHints);
|
|
54
|
+
if (!config.complementaryHints || config.maxHints <= 1 || ranked.length <= 1)
|
|
55
|
+
return selected;
|
|
56
|
+
turn.stage = "redundancy";
|
|
57
|
+
try {
|
|
58
|
+
const pairs = await reviewMemoryRedundancy({
|
|
59
|
+
apiKey, endpoint: decisionModel.endpoint, model: decisionModel.model, timeoutMs: decisionModel.timeoutMs, signal,
|
|
60
|
+
excerpts: ranked.map(candidate => candidate.excerpt),
|
|
61
|
+
});
|
|
62
|
+
selected = complementaryIndices(ranked.length, pairs, config.maxHints).map(index => ranked[index]);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
// Preserve the original useful candidates when the optional refinement is unavailable.
|
|
66
|
+
if (!signal.aborted) {
|
|
67
|
+
diagnostics?.record(agentId, "memory", "redundancy_unavailable");
|
|
68
|
+
log("warn", "redundancy_unavailable", failureFields(error));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return selected;
|
|
72
|
+
}
|
|
73
|
+
function renderBoundedHints(selected) {
|
|
74
|
+
// Bound the complete injected payload without slicing individual excerpts into misleading prefixes.
|
|
75
|
+
const emitted = [];
|
|
76
|
+
const hints = [];
|
|
77
|
+
let rendered = "";
|
|
78
|
+
for (const candidate of selected) {
|
|
79
|
+
const nextHint = {
|
|
80
|
+
source: candidate.hit.path,
|
|
81
|
+
lines: `${candidate.hit.startLine}-${candidate.hit.endLine}`,
|
|
82
|
+
body: candidate.excerpt,
|
|
83
|
+
};
|
|
84
|
+
const nextRendered = encodeHints([...hints, nextHint]);
|
|
85
|
+
if (nextRendered.length > MAX_PAYLOAD_CHARS)
|
|
86
|
+
continue;
|
|
87
|
+
emitted.push(candidate);
|
|
88
|
+
hints.push(nextHint);
|
|
89
|
+
rendered = nextRendered;
|
|
90
|
+
}
|
|
91
|
+
return { emitted, rendered };
|
|
92
|
+
}
|
|
93
|
+
export function registerMemoryWhisperer(api, runtime, config, decisionModel, diagnostics) {
|
|
94
|
+
if (!config.enabled || !decisionModel.enabled)
|
|
15
95
|
return;
|
|
16
96
|
const sessions = new Map();
|
|
17
97
|
const generator = new ApiQueryGenerator(config.api);
|
|
98
|
+
const touchSession = (key, state) => {
|
|
99
|
+
sessions.delete(key);
|
|
100
|
+
sessions.set(key, state);
|
|
101
|
+
};
|
|
18
102
|
const beforePrompt = async (event, context) => {
|
|
19
103
|
const { agentId, runId, sessionId, sessionKey } = context;
|
|
20
104
|
const scope = sessionId || sessionKey;
|
|
@@ -25,164 +109,129 @@ export function registerMemoryWhisperer(api, runtime, config, typesafe, diagnost
|
|
|
25
109
|
return;
|
|
26
110
|
const key = JSON.stringify([agentId, scope]);
|
|
27
111
|
const previous = sessions.get(key);
|
|
28
|
-
if (previous
|
|
29
|
-
|
|
112
|
+
if (previous) {
|
|
113
|
+
touchSession(key, previous);
|
|
114
|
+
if (previous.runId === runId)
|
|
115
|
+
return previous.result;
|
|
116
|
+
// Ignore delayed prompt rebuilds for already-superseded runs so they never replace the latest run.
|
|
117
|
+
if (previous.seenRuns.has(runId))
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
30
120
|
const currentUserMessage = typeof event.currentUserMessage === "string" ? event.currentUserMessage : undefined;
|
|
31
121
|
const rawCurrent = currentUserMessage !== undefined ? currentUserMessage : event.prompt;
|
|
32
122
|
if (!rawCurrent.replace(/^\s*From: [^\r\n]+ \([^()\r\n]+\)\s*$/, "").trim())
|
|
33
123
|
return;
|
|
34
124
|
const started = performance.now();
|
|
35
125
|
const asOf = new Date().toISOString();
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
126
|
+
const turn = {
|
|
127
|
+
stage: "input",
|
|
128
|
+
reason: "completed",
|
|
129
|
+
queryFallback: false,
|
|
130
|
+
requestsSucceeded: 0,
|
|
131
|
+
requestsFailed: 0,
|
|
132
|
+
measurement: { outcome: "skipped", elapsedMs: 0 },
|
|
133
|
+
};
|
|
134
|
+
const log = (level, eventName, fields) => api.logger[level]("unblock-memory memory_whisperer " + JSON.stringify({ event: eventName, agentId, runId, sessionId, stage: turn.stage, ...fields }));
|
|
43
135
|
previous?.controller.abort();
|
|
136
|
+
if (!previous && sessions.size >= MAX_SESSIONS) {
|
|
137
|
+
const oldestKey = sessions.keys().next().value;
|
|
138
|
+
sessions.get(oldestKey)?.controller.abort();
|
|
139
|
+
sessions.delete(oldestKey);
|
|
140
|
+
}
|
|
141
|
+
const seenRuns = previous?.seenRuns ?? new Set();
|
|
142
|
+
seenRuns.add(runId);
|
|
143
|
+
if (seenRuns.size > MAX_SEEN_RUNS)
|
|
144
|
+
seenRuns.delete(seenRuns.values().next().value);
|
|
44
145
|
const state = {
|
|
45
146
|
agentId, sessionId, sessionKey, runId, turn: (previous?.turn ?? 0) + 1,
|
|
46
|
-
controller: new AbortController(), recent: previous?.recent ?? new Map(),
|
|
147
|
+
controller: new AbortController(), recent: previous?.recent ?? new Map(), seenRuns,
|
|
47
148
|
};
|
|
48
|
-
|
|
49
|
-
for (const [id,
|
|
50
|
-
if (state.turn -
|
|
149
|
+
touchSession(key, state);
|
|
150
|
+
for (const [id, recordedTurn] of state.recent) {
|
|
151
|
+
if (state.turn - recordedTurn > config.cooldownTurns)
|
|
51
152
|
state.recent.delete(id);
|
|
52
153
|
}
|
|
53
154
|
const { signal } = state.controller;
|
|
54
|
-
|
|
55
|
-
const timer = setTimeout(() => { timedOut = true; state.controller.abort(); }, config.timeoutMs);
|
|
56
|
-
let onAbort = () => { };
|
|
57
|
-
const aborted = new Promise(resolve => {
|
|
58
|
-
onAbort = () => resolve(undefined);
|
|
59
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
60
|
-
});
|
|
155
|
+
const timer = setTimeout(() => state.controller.abort(new DOMException("Memory deadline", "TimeoutError")), config.timeoutMs);
|
|
61
156
|
const run = async () => {
|
|
62
157
|
const conversation = queryConversation(event.prompt, event.messages, currentUserMessage);
|
|
63
|
-
stage = "credentials";
|
|
64
|
-
const apiKey = await timeStage("setupMs", measurement, signal, () =>
|
|
158
|
+
turn.stage = "credentials";
|
|
159
|
+
const apiKey = await timeStage("setupMs", turn.measurement, signal, () => resolveDecisionCredential(decisionModel));
|
|
65
160
|
if (signal.aborted)
|
|
66
161
|
return;
|
|
67
162
|
if (!apiKey) {
|
|
68
|
-
reason = "missing_key";
|
|
163
|
+
turn.reason = "missing_key";
|
|
69
164
|
diagnostics?.record(agentId, "memory", "missing_key");
|
|
70
165
|
return;
|
|
71
166
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (signal.aborted)
|
|
81
|
-
return;
|
|
82
|
-
diagnostics?.record(agentId, "memory", "queries_generated");
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
if (signal.aborted)
|
|
86
|
-
return;
|
|
87
|
-
queryFallback = true;
|
|
88
|
-
queries = fallbackQueries(conversation);
|
|
89
|
-
log("warn", "generation_fallback", { ...failureFields(error), elapsedMs: performance.now() - generationStarted,
|
|
90
|
-
timeoutMs: generationTimeoutMs });
|
|
91
|
-
diagnostics?.record(agentId, "memory", "generation_fallback");
|
|
92
|
-
}
|
|
93
|
-
finally {
|
|
94
|
-
endGeneration();
|
|
95
|
-
}
|
|
96
|
-
queryCount = 2;
|
|
97
|
-
stage = "retrieval";
|
|
98
|
-
const { manager } = await timeStage("setupMs", measurement, signal, () => runtime.getMemorySearchManager({ cfg: api.config, agentId }));
|
|
167
|
+
const queries = await generateQueriesWithFallback({
|
|
168
|
+
generator, conversation, timeoutMs: config.timeoutMs, signal, turn, agentId, diagnostics, log,
|
|
169
|
+
});
|
|
170
|
+
if (!queries || signal.aborted)
|
|
171
|
+
return;
|
|
172
|
+
turn.queryCount = 2;
|
|
173
|
+
turn.stage = "retrieval";
|
|
174
|
+
const { manager } = await timeStage("setupMs", turn.measurement, signal, () => runtime.getMemorySearchManager({ cfg: api.config, agentId }));
|
|
99
175
|
if (signal.aborted)
|
|
100
176
|
return;
|
|
101
177
|
if (!manager) {
|
|
102
|
-
reason = "unavailable";
|
|
178
|
+
turn.reason = "unavailable";
|
|
103
179
|
diagnostics?.record(agentId, "memory", "unavailable");
|
|
104
180
|
return;
|
|
105
181
|
}
|
|
106
182
|
const search = await searchMemory(manager, queries, {
|
|
107
|
-
corpora, signal, apiKey, timeoutMs:
|
|
108
|
-
asOf, timings: measurement, minUsefulness: config.minUsefulness, maxResults: 4,
|
|
183
|
+
corpora, signal, apiKey, endpoint: decisionModel.endpoint, model: decisionModel.model, timeoutMs: decisionModel.timeoutMs, conversation: compactJudgeConversation(conversation),
|
|
184
|
+
asOf, timings: turn.measurement, minUsefulness: config.minUsefulness, maxResults: 4,
|
|
109
185
|
maxJudgeCandidates: MAX_JUDGE_CANDIDATES, excludedPassages: state.recent,
|
|
110
186
|
onCandidates: observation => {
|
|
111
|
-
measurement.retrievalMs = observation.retrievalMs;
|
|
112
|
-
measurement.candidates = observation.candidates;
|
|
113
|
-
measurement.eligible = observation.eligible;
|
|
114
|
-
measurement.outcome = "empty";
|
|
115
|
-
stage = "judgment";
|
|
187
|
+
turn.measurement.retrievalMs = observation.retrievalMs;
|
|
188
|
+
turn.measurement.candidates = observation.candidates;
|
|
189
|
+
turn.measurement.eligible = observation.eligible;
|
|
190
|
+
turn.measurement.outcome = "empty";
|
|
191
|
+
turn.stage = "judgment";
|
|
116
192
|
},
|
|
117
193
|
onJudgment: ({ candidateIndex, elapsedMs, error }) => {
|
|
118
|
-
const fields = { stage: "judgment", candidateIndex, elapsedMs, timeoutMs:
|
|
194
|
+
const fields = { stage: "judgment", candidateIndex, elapsedMs, timeoutMs: decisionModel.timeoutMs };
|
|
119
195
|
if (error !== undefined) {
|
|
120
|
-
requestsFailed++;
|
|
196
|
+
turn.requestsFailed++;
|
|
121
197
|
diagnostics?.record(agentId, "memory", "judge_candidate_failed");
|
|
122
198
|
log("warn", "candidate_failed", { ...fields, ...failureFields(error) });
|
|
123
199
|
}
|
|
124
200
|
else {
|
|
125
|
-
requestsSucceeded++;
|
|
201
|
+
turn.requestsSucceeded++;
|
|
126
202
|
log("info", "candidate_completed", fields);
|
|
127
203
|
}
|
|
128
204
|
},
|
|
129
205
|
});
|
|
130
206
|
if (signal.aborted || sessions.get(key) !== state)
|
|
131
207
|
return;
|
|
132
|
-
measurement.judgeMs = search.judgeMs;
|
|
208
|
+
turn.measurement.judgeMs = search.judgeMs;
|
|
133
209
|
if (!search.eligible) {
|
|
134
|
-
reason = "no_candidates";
|
|
210
|
+
turn.reason = "no_candidates";
|
|
135
211
|
diagnostics?.record(agentId, "memory", "no_candidates");
|
|
136
212
|
return;
|
|
137
213
|
}
|
|
138
214
|
if (!search.requestsSucceeded) {
|
|
139
|
-
reason = "all_candidates_failed";
|
|
140
|
-
measurement.outcome = "failed";
|
|
215
|
+
turn.reason = "all_candidates_failed";
|
|
216
|
+
turn.measurement.outcome = "failed";
|
|
141
217
|
diagnostics?.record(agentId, "memory", "failed");
|
|
142
218
|
return;
|
|
143
219
|
}
|
|
144
220
|
const ranked = search.results.map(hit => ({ hit, excerpt: hit.snippet, id: memoryPassageId(hit.snippet) }));
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
reason = "rejected";
|
|
221
|
+
if (!ranked.length) {
|
|
222
|
+
turn.reason = "rejected";
|
|
148
223
|
diagnostics?.record(agentId, "memory", "rejected");
|
|
149
224
|
return;
|
|
150
225
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const pairs = await reviewMemoryRedundancy({ apiKey, timeoutMs: typesafe.timeoutMs, signal,
|
|
155
|
-
excerpts: ranked.map(candidate => candidate.excerpt) });
|
|
156
|
-
selected = complementaryIndices(ranked.length, pairs, config.maxHints).map(index => ranked[index]);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
// Preserve the original useful candidates when the optional refinement is unavailable.
|
|
160
|
-
if (!signal.aborted) {
|
|
161
|
-
diagnostics?.record(agentId, "memory", "redundancy_unavailable");
|
|
162
|
-
log("warn", "redundancy_unavailable", failureFields(error));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
226
|
+
const selected = await selectComplementaryHints({
|
|
227
|
+
ranked, config, decisionModel, apiKey, signal, turn, agentId, diagnostics, log,
|
|
228
|
+
});
|
|
166
229
|
if (signal.aborted || sessions.get(key) !== state)
|
|
167
230
|
return;
|
|
168
|
-
stage = "render";
|
|
169
|
-
|
|
170
|
-
const emitted = [];
|
|
171
|
-
const hints = [];
|
|
172
|
-
let rendered = "";
|
|
173
|
-
for (const candidate of selected) {
|
|
174
|
-
const nextHint = {
|
|
175
|
-
source: candidate.hit.path, lines: `${candidate.hit.startLine}-${candidate.hit.endLine}`, body: candidate.excerpt,
|
|
176
|
-
};
|
|
177
|
-
const nextRendered = encodeHints([...hints, nextHint]);
|
|
178
|
-
if (nextRendered.length > MAX_PAYLOAD_CHARS)
|
|
179
|
-
continue;
|
|
180
|
-
emitted.push(candidate);
|
|
181
|
-
hints.push(nextHint);
|
|
182
|
-
rendered = nextRendered;
|
|
183
|
-
}
|
|
231
|
+
turn.stage = "render";
|
|
232
|
+
const { emitted, rendered } = renderBoundedHints(selected);
|
|
184
233
|
if (!emitted.length) {
|
|
185
|
-
reason = "payload_limit";
|
|
234
|
+
turn.reason = "payload_limit";
|
|
186
235
|
diagnostics?.record(agentId, "memory", "payload_limit");
|
|
187
236
|
return;
|
|
188
237
|
}
|
|
@@ -190,46 +239,55 @@ export function registerMemoryWhisperer(api, runtime, config, typesafe, diagnost
|
|
|
190
239
|
state.recent.set(candidate.id, state.turn);
|
|
191
240
|
diagnostics?.record(agentId, "memory", "emitted");
|
|
192
241
|
const appendContext = `<memory>\n${rendered}\n</memory>`;
|
|
193
|
-
measurement.outcome = "ok";
|
|
194
|
-
reason = "emitted";
|
|
195
|
-
measurement.results = emitted.length;
|
|
196
|
-
measurement.contextChars = appendContext.length;
|
|
242
|
+
turn.measurement.outcome = "ok";
|
|
243
|
+
turn.reason = "emitted";
|
|
244
|
+
turn.measurement.results = emitted.length;
|
|
245
|
+
turn.measurement.contextChars = appendContext.length;
|
|
197
246
|
return { appendContext };
|
|
198
247
|
};
|
|
199
248
|
// Prompt rebuilds need the same context, not another retrieval or an empty hook result.
|
|
200
249
|
state.result = (async () => {
|
|
201
250
|
try {
|
|
202
|
-
return await
|
|
251
|
+
return await abortable(run(), signal);
|
|
203
252
|
}
|
|
204
253
|
catch (error) {
|
|
254
|
+
if (signal.aborted)
|
|
255
|
+
return;
|
|
205
256
|
if (error instanceof QueryInputBudgetError) {
|
|
206
|
-
reason = "input_too_large";
|
|
207
|
-
measurement.outcome = "skipped";
|
|
257
|
+
turn.reason = "input_too_large";
|
|
258
|
+
turn.measurement.outcome = "skipped";
|
|
208
259
|
diagnostics?.record(agentId, "memory", "input_too_large");
|
|
209
260
|
return;
|
|
210
261
|
}
|
|
211
|
-
reason = "stage_failed";
|
|
212
|
-
measurement.outcome = "failed";
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
log("warn", "stage_failed", failureFields(error));
|
|
216
|
-
}
|
|
262
|
+
turn.reason = "stage_failed";
|
|
263
|
+
turn.measurement.outcome = "failed";
|
|
264
|
+
diagnostics?.record(agentId, "memory", "failed");
|
|
265
|
+
log("warn", "stage_failed", failureFields(error));
|
|
217
266
|
return;
|
|
218
267
|
}
|
|
219
268
|
finally {
|
|
220
|
-
measurement.elapsedMs = performance.now() - started;
|
|
269
|
+
turn.measurement.elapsedMs = performance.now() - started;
|
|
270
|
+
const timedOut = abortedByTimeout(signal);
|
|
221
271
|
if (signal.aborted) {
|
|
222
|
-
measurement.outcome = timedOut ? "timed_out" : "cancelled";
|
|
223
|
-
reason = timedOut ? "deadline" : "cancelled";
|
|
272
|
+
turn.measurement.outcome = timedOut ? "timed_out" : "cancelled";
|
|
273
|
+
turn.reason = timedOut ? "deadline" : "cancelled";
|
|
224
274
|
}
|
|
225
|
-
diagnostics?.measureMemory(agentId, measurement);
|
|
226
|
-
log("info", "completed", {
|
|
227
|
-
|
|
228
|
-
|
|
275
|
+
diagnostics?.measureMemory(agentId, turn.measurement);
|
|
276
|
+
log("info", "completed", {
|
|
277
|
+
...turn.measurement,
|
|
278
|
+
reason: turn.reason,
|
|
279
|
+
queryCount: turn.queryCount,
|
|
280
|
+
queryFallback: turn.queryFallback,
|
|
281
|
+
requestsSucceeded: turn.requestsSucceeded,
|
|
282
|
+
requestsFailed: turn.requestsFailed,
|
|
283
|
+
judgedCandidates: turn.requestsSucceeded,
|
|
284
|
+
partial: turn.requestsSucceeded > 0 && turn.requestsFailed > 0,
|
|
285
|
+
timeoutMs: config.timeoutMs,
|
|
286
|
+
requestTimeoutMs: decisionModel.timeoutMs,
|
|
287
|
+
});
|
|
229
288
|
if (signal.aborted)
|
|
230
289
|
diagnostics?.record(agentId, "memory", timedOut ? "timed_out" : "cancelled");
|
|
231
290
|
clearTimeout(timer);
|
|
232
|
-
signal.removeEventListener("abort", onAbort);
|
|
233
291
|
// Stop sibling provider/worker work after an error or an empty early result too.
|
|
234
292
|
state.controller.abort();
|
|
235
293
|
}
|
package/dist/src/my-turn.d.ts
CHANGED
|
@@ -9,4 +9,4 @@ export type MyTurnConfig = {
|
|
|
9
9
|
}>;
|
|
10
10
|
};
|
|
11
11
|
export declare function resolveMyTurn(value: unknown): MyTurnConfig;
|
|
12
|
-
export declare function registerMyTurn(api: OpenClawPluginApi, config: MyTurnConfig,
|
|
12
|
+
export declare function registerMyTurn(api: OpenClawPluginApi, config: MyTurnConfig, decisionModel: UnblockMemoryConfig["decisionModel"], diagnostics?: WhispererDiagnostics): Parameters<typeof api.on<"before_prompt_build">>[1] | undefined;
|