Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(codex): reuse shared recall compressor (#4445)

Xinmin Zeng58bafa5ba178
diff --git a/examples/claude-code-memory-plugin/scripts/shared/recall-compress-core.mjs b/examples/claude-code-memory-plugin/scripts/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/claude-code-memory-plugin/scripts/shared/recall-compress-core.mjs
+++ b/examples/claude-code-memory-plugin/scripts/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/codex-memory-plugin/DESIGN.md b/examples/codex-memory-plugin/DESIGN.md
index d9493c836..d6f674c75 100644
--- a/examples/codex-memory-plugin/DESIGN.md
+++ b/examples/codex-memory-plugin/DESIGN.md
@@ -388,6 +388,7 @@ Env var overrides for tuning without rebuilding:
| `OPENVIKING_RECALL_COMPRESS_MODEL` | unset | custom first-choice compressor model; `off` disables compression |
| `OPENVIKING_RECALL_COMPRESS_THINKING` | unset | custom `model_reasoning_effort`; `default` means omit override; alias `OPENVIKING_RECALL_COMPRESS_REASONING_EFFORT` |
| `OPENVIKING_RECALL_COMPRESS_BASE_URL` | unset | custom API base URL for the nested `codex exec` compressor |
+| `OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS` | `1500` | skip the nested compressor below this recalled-context size; `0` always compresses |
| `OPENVIKING_RECALL_COMPRESS_DETECT_ON_STARTUP` | `1` | recreate/cache compressor profile during every `SessionStart` |
| `OPENVIKING_RECALL_COMPRESS_DETECT_TIMEOUT_MS` | `15000` | per-candidate compressor probe timeout |
| `OPENVIKING_RECALL_COMPRESS_DETECT_TTL_MS` | `604800000` (7 days) | cache TTL used by `UserPromptSubmit` reads |
@@ -441,9 +442,12 @@ Model availability is re-probed at every `SessionStart`, not in every
`UserPromptSubmit`. Recreating the profile on each session start catches
cross-session env/config changes. The detector writes
`recall-compressor-profile.json` under `OPENVIKING_CODEX_STATE_DIR` and
-auto-recall reads that cache. Cache misses in auto-recall use the first
-candidate directly and fall back to deterministic digest if `codex exec`
-fails.
+auto-recall reads that cache. Before resolving a profile, auto-recall passes
+the injection-ready context through the shared recall-compression core. The
+shared core admits only blocks at or above the configured minimum and reuses a
+digest for an identical query/context/URI set; only an eligible cache miss
+launches `codex exec`. Compressor failures fall back to the deterministic
+digest.
Fallback order:
diff --git a/examples/codex-memory-plugin/README.md b/examples/codex-memory-plugin/README.md
index c024a13f6..ba80926fb 100644
--- a/examples/codex-memory-plugin/README.md
+++ b/examples/codex-memory-plugin/README.md
@@ -252,7 +252,7 @@ On `resume`, the script skips commit/sweep. It still injects the profile block.
{ "hookSpecificOutput": { "hookEventName": "UserPromptSubmit", "additionalContext": "<openviking-context source=\"auto-recall\" format=\"digest\">\nOpenViking memory digest:\n- ...\n</openviking-context>" } }
```
-Codex injects `additionalContext` into the model turn, so memories arrive without an extra tool call. By default the hook runs a Codex compression pass over recalled candidates before injection, dropping weakly-related memories and preserving only a short digest. If the compressor returns `NO_RELEVANT_MEMORY`, empty text, or non-digest chatter, the hook emits `{}` and injects nothing. The whole hook has its own `OPENVIKING_RECALL_TIMEOUT_MS` deadline (default 120s); the bundled `hooks.json` gives Codex 130s so the script can return `{}` before Codex kills it. Digests may keep `viking://` source URIs and point the model at the OpenViking MCP `read`/`search` tools for details when the inline bullet is intentionally short. The outer `<openviking-context ...>` wrapper is deterministic, not compressor-generated; capture strips it to distinguish recalled context from the user's prompt. Set `OPENVIKING_RECALL_COMPRESS=0` to fall back to deterministic short formatting.
+Codex injects `additionalContext` into the model turn, so memories arrive without an extra tool call. By default, recalled context below `OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS` is injected directly; larger blocks pass through the shared relevance compressor, and an identical query/context pair reuses its cached digest. If the compressor returns `NO_RELEVANT_MEMORY`, empty text, or non-digest chatter, the hook emits `{}` and injects nothing. The whole hook has its own `OPENVIKING_RECALL_TIMEOUT_MS` deadline (default 120s); the bundled `hooks.json` gives Codex 130s so the script can return `{}` before Codex kills it. Digests keep validated `viking://` source URIs and point the model at the OpenViking MCP `read`/`search` tools for details when the inline bullet is intentionally short. The outer `<openviking-context ...>` wrapper is deterministic, not compressor-generated; capture strips it to distinguish recalled context from the user's prompt. Set `OPENVIKING_RECALL_COMPRESS=0` to fall back to deterministic short formatting.
The compressor profile is recreated on every `SessionStart` and cached under `OPENVIKING_CODEX_STATE_DIR` so cross-session config changes are picked up but each `UserPromptSubmit` does not probe models. Default fallback order:
@@ -270,6 +270,7 @@ Config knobs:
| `OPENVIKING_RECALL_COMPRESS_MODEL` | unset | Custom first-choice compressor model. Set `off` to disable compression. |
| `OPENVIKING_RECALL_COMPRESS_THINKING` | unset | Custom `model_reasoning_effort`; `default` omits the Codex config override. Alias: `OPENVIKING_RECALL_COMPRESS_REASONING_EFFORT`. |
| `OPENVIKING_RECALL_COMPRESS_BASE_URL` | unset | Base URL for the nested compressor's provider. Use this when `--ignore-user-config` prevents the compressor from reading the main Codex provider configuration. |
+| `OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS` | `1500` | Skip the nested compressor below this recalled-context size. Set `0` to compress every non-empty result. |
| `OPENVIKING_RECALL_COMPRESS_DETECT_ON_STARTUP` | `1` | Recreate/cache compressor profile in `SessionStart`. |
| `OPENVIKING_RECALL_COMPRESS_DETECT_TIMEOUT_MS` | `15000` | Per-candidate startup probe timeout. |
| `OPENVIKING_RECALL_COMPRESS_DETECT_TTL_MS` | `604800000` | Cache TTL used by `UserPromptSubmit` when reading the latest profile. |
@@ -286,8 +287,8 @@ unless explicitly configured, so the plugin follows the server instead of copyin
values such as `limit=10` or `max_tokens=1600`. An explicit legacy `recallLimit`
is converted to per-category coding quotas, not a final result cap. Values
from 1 through 5 therefore produce an effective total quota of 6, one retrieval
-slot for each coding domain. Local `codex exec` compression is
-unchanged and still runs on top of whichever path answered.
+slot for each coding domain. Eligible cache misses still use local `codex exec`
+compression on top of whichever path answered.
Client-side knobs can also live in `~/.openviking/ovcli.conf` under
`plugin` (shared) or `plugin.codex` (this harness only), or in the workspace
diff --git a/examples/codex-memory-plugin/scripts/auto-recall.mjs b/examples/codex-memory-plugin/scripts/auto-recall.mjs
index fe4f3b24b..768744341 100644
--- a/examples/codex-memory-plugin/scripts/auto-recall.mjs
+++ b/examples/codex-memory-plugin/scripts/auto-recall.mjs
@@ -25,13 +25,14 @@ import {
loadCachedRecallCompressorProfile,
markRecallCompressorRuntimeFailed,
} from "./recall-compressor-profile.mjs";
-import { deriveOvSessionId } from "./session-state.mjs";
+import { deriveOvSessionId, getStateDir } from "./session-state.mjs";
import {
buildRecallEndpointBody,
fetchAssembledContext,
normalizeContextEntry,
postRecall,
} from "./shared/recall-core.mjs";
+import { compressRecallContext } from "./shared/recall-compress-core.mjs";
import { resolveEffectivePeerId } from "./shared/workspace-peer.mjs";
let cfg = loadConfig();
@@ -42,6 +43,7 @@ let emitted = false;
let activeCompressor = null;
let recallDeadline = null;
const DEFAULT_FINAL_RECALL_CHARS = 6500;
+const RECALL_DIGEST_CACHE_PATH = join(getStateDir(), "recall-digest.json");
function output(obj, exitAfter = false) {
if (emitted) return;
@@ -387,19 +389,6 @@ function sanitizeInjectedText(text) {
.replace(/<\/?openviking-context\b[^>]*>/gi, "openviking context marker");
}
-function isNoRelevantMemory(text) {
- const value = String(text || "")
- .trim()
- .replace(/^openviking memory digest:\s*/i, "")
- .trim();
- return !value || /^NO_RELEVANT_MEMORY\.?$/i.test(value) || /^no (?:directly )?relevant memor(?:y|ies)\.?$/i.test(value);
-}
-
-function hasDigestSignal(text) {
- const body = String(text || "").replace(/^openviking memory digest:\s*/i, "").trim();
- return /(^|\n)\s*[-*]\s+\S/.test(body) || /\bviking:\/\//i.test(body);
-}
-
function appendMcpRetrievalHint(text) {
const value = String(text || "").trim();
if (!/\bviking:\/\//i.test(value) || /OpenViking MCP/i.test(value)) return value;
@@ -414,17 +403,19 @@ function fallbackDigest(items) {
return lines.length > 0 ? appendMcpRetrievalHint(`OpenViking memory digest:\n${lines.join("\n")}`) : "";
}
-function normalizeCompressedContext(text) {
- let value = String(text || "").trim();
- if (!value) return "";
- value = value.replace(/^```(?:text|markdown)?\s*/i, "").replace(/\s*```$/i, "").trim();
- value = sanitizeInjectedText(value);
- if (isNoRelevantMemory(value)) return "";
- if (!value.toLowerCase().startsWith("openviking memory digest:")) {
- value = `OpenViking memory digest:\n${value}`;
- }
- if (!hasDigestSignal(value)) return "";
- return truncateText(appendMcpRetrievalHint(value), 4000);
+function fallbackCompressionInput(items) {
+ const perItemChars = Math.max(
+ 500,
+ Math.floor(cfg.recallCompressMaxInputChars / Math.max(1, items.length)),
+ );
+ return JSON.stringify({
+ memories: items.map((item) => ({
+ uri: item.uri,
+ category: item.category || "memory",
+ score: item.score,
+ text: truncateText(item.text, perItemChars),
+ })),
+ }, null, 2);
}
async function getRecallCompressorProfile() {
@@ -522,46 +513,43 @@ async function runCodexCompressor(prompt, profile) {
}
}
-async function compressMemoryContext(userPrompt, items) {
+async function compressMemoryContext(userPrompt, rendered, items, shortContext = rendered) {
if (!cfg.recallCompress) return null;
- const profile = await getRecallCompressorProfile();
- if (!profile.enabled) {
- log("compress_skip", { reason: "profile disabled", profile });
+ const input = String(rendered || "").trim() || fallbackDigest(items);
+ if (!input) return "";
+
+ let profile = null;
+ try {
+ const compression = await compressRecallContext({
+ query: userPrompt,
+ rendered: input,
+ shortContext,
+ entries: items,
+ cfg,
+ cachePath: RECALL_DIGEST_CACHE_PATH,
+ now: Date.now(),
+ // Short inputs and cache hits return before host model state is needed.
+ runCompressor: async (prompt) => {
+ profile = await getRecallCompressorProfile();
+ if (!profile.enabled) {
+ log("compress_skip", { reason: "profile disabled", profile });
+ return "";
+ }
+ return await runCodexCompressor(prompt, profile) ?? "";
+ },
+ });
+ log("compressed", {
+ status: compression.status,
+ inputCount: items.length,
+ chars: compression.context.length,
+ profile,
+ });
+ if (compression.status === "failed") return null;
+ return appendMcpRetrievalHint(compression.context);
+ } catch (err) {
+ logError("compress", err);
return null;
}
- const perItemChars = Math.max(500, Math.floor(cfg.recallCompressMaxInputChars / Math.max(1, items.length)));
- const payload = {
- user_prompt: userPrompt,
- max_bullets: cfg.recallCompressMaxBullets,
- memories: items.map((item) => ({
- uri: item.uri,
- category: item.category || "memory",
- score: item.score,
- text: truncateText(item.text, perItemChars),
- })),
- };
- const prompt = `You are a memory relevance compressor for a Codex UserPromptSubmit hook.
-
-Task:
-- Keep only memories directly useful for answering the user's current prompt.
-- Drop stale, generic, duplicate, merely adjacent, or operationally unrelated memories.
-- Compress to at most ${cfg.recallCompressMaxBullets} short bullets.
-- Preserve concrete facts, dates, paths, repo names, commands, and user preferences.
-- Include the source viking:// URI when the agent may need to inspect more detail.
-- If the answer needs detail beyond the bullet, say to use OpenViking MCP read/search with the cited viking:// URI if needed.
-- Do not include XML/HTML wrappers.
-- Do not mention that you filtered memories.
-- Output either "OpenViking memory digest:" followed by useful bullets, or exactly: NO_RELEVANT_MEMORY.
-- If no memory is directly useful, output exactly: NO_RELEVANT_MEMORY.
-
-Input JSON:
-${JSON.stringify(payload, null, 2)}
-`;
- const raw = await runCodexCompressor(prompt, profile);
- if (raw === null) return null;
- const compressed = normalizeCompressedContext(raw);
- log("compressed", { inputCount: items.length, chars: compressed.length, profile });
- return compressed;
}
async function main() {
@@ -625,7 +613,7 @@ async function main() {
return;
}
const compressedContext = endpointRecall.items.length > 0
- ? await compressMemoryContext(userPrompt, endpointRecall.items)
+ ? await compressMemoryContext(userPrompt, endpointRecall.context, endpointRecall.items)
: null;
const endpointFallback = cfg.recallCompress && endpointRecall.items.length > 0
? fallbackDigest(endpointRecall.items)
@@ -699,8 +687,14 @@ async function main() {
}),
);
- const compressedContext = await compressMemoryContext(userPrompt, memoryItems);
- const memoryContext = compressedContext === null ? fallbackDigest(memoryItems) : compressedContext;
+ const fallbackContext = fallbackDigest(memoryItems);
+ const compressedContext = await compressMemoryContext(
+ userPrompt,
+ fallbackCompressionInput(memoryItems),
+ memoryItems,
+ fallbackContext,
+ );
+ const memoryContext = compressedContext === null ? fallbackContext : compressedContext;
emit(memoryContext);
}
diff --git a/examples/codex-memory-plugin/scripts/auto-recall.test.mjs b/examples/codex-memory-plugin/scripts/auto-recall.test.mjs
index 43335585b..e365c32ed 100644
--- a/examples/codex-memory-plugin/scripts/auto-recall.test.mjs
+++ b/examples/codex-memory-plugin/scripts/auto-recall.test.mjs
@@ -118,12 +118,14 @@ async function withFakeCodex(output, fn, { exitCode = 0 } = {}) {
);
const callLog = join(binDir, "calls.log");
const argsLog = join(binDir, "args.log");
+ const promptLog = join(binDir, "prompt.log");
const fakeCodex = `#!/usr/bin/env node
const fs = require("node:fs");
const outputFlagIndex = process.argv.indexOf("--output-last-message");
const outputPath = outputFlagIndex >= 0 ? process.argv[outputFlagIndex + 1] : "";
-fs.readFileSync(0);
+const prompt = fs.readFileSync(0, "utf8");
+fs.writeFileSync(process.env.FAKE_CODEX_PROMPT_LOG, prompt);
fs.appendFileSync(process.env.FAKE_CODEX_CALL_LOG, "called\\n");
fs.appendFileSync(process.env.FAKE_CODEX_ARGS_LOG, process.argv.slice(2).join("\\n") + "\\n");
@@ -140,10 +142,12 @@ fs.writeFileSync(outputPath, process.env.FAKE_CODEX_OUTPUT || "");
return await fn({
callLog,
argsLog,
+ promptLog,
env: {
PATH: `${binDir}${delimiter}${process.env.PATH}`,
FAKE_CODEX_CALL_LOG: callLog,
FAKE_CODEX_ARGS_LOG: argsLog,
+ FAKE_CODEX_PROMPT_LOG: promptLog,
FAKE_CODEX_EXIT_CODE: String(exitCode),
FAKE_CODEX_OUTPUT: output,
},
@@ -160,11 +164,12 @@ async function runEndpointCompressionCase({
compressorOutput,
exitCode = 0,
extraEnv = {},
+ stateDir: providedStateDir = "",
}) {
- const stateDir = await mkdtemp(join(tmpdir(), "ov-auto-recall-endpoint-compress-"));
+ const stateDir = providedStateDir || await mkdtemp(join(tmpdir(), "ov-auto-recall-endpoint-compress-"));
let requestBody = null;
try {
- return await withFakeCodex(compressorOutput, async ({ callLog, argsLog, env }) => {
+ return await withFakeCodex(compressorOutput, async ({ callLog, argsLog, promptLog, env }) => {
const result = await withMockOpenViking(async (req, res) => {
const url = new URL(req.url, "http://127.0.0.1");
if (req.method === "GET" && url.pathname === "/health") {
@@ -208,15 +213,17 @@ async function runEndpointCompressionCase({
));
const compressorCallLog = await readFile(callLog, "utf-8").catch(() => "");
const compressorArgs = await readFile(argsLog, "utf-8").catch(() => "");
+ const compressorPrompt = await readFile(promptLog, "utf-8").catch(() => "");
return {
output: JSON.parse(result.stdout.trim()),
compressorCalls: compressorCallLog.trim().split("\n").filter(Boolean).length,
compressorArgs: compressorArgs.trim().split("\n").filter(Boolean),
+ compressorPrompt,
requestBody,
};
}, { exitCode });
} finally {
- await rm(stateDir, { recursive: true, force: true });
+ if (!providedStateDir) await rm(stateDir, { recursive: true, force: true });
}
}
@@ -387,6 +394,7 @@ test("auto-recall applies the relevance compressor to server recall entries", as
},
rendered: "<memory_group>Unrelated remembered detail</memory_group>",
compressorOutput: "NO_RELEVANT_MEMORY",
+ extraEnv: { OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS: "0" },
});
assert.deepEqual(result.output, {});
@@ -394,6 +402,126 @@ test("auto-recall applies the relevance compressor to server recall entries", as
assert.equal(result.requestBody.max_chars, 18000);
});
+test("auto-recall skips the compressor for a bounded short recall", async () => {
+ const result = await runEndpointCompressionCase({
+ prompt: "Which editor do I prefer?",
+ entry: {
+ uri: "viking://user/zeus/memories/preferences/editor.md",
+ score: 0.91,
+ type: "preferences",
+ mode: "summary",
+ summary: "Use Vim",
+ },
+ rendered: '<memory uri="viking://user/zeus/memories/preferences/editor.md">Use Vim</memory>',
+ compressorOutput: "NO_RELEVANT_MEMORY",
+ });
+
+ assert.equal(result.compressorCalls, 0);
+ assert.match(result.output.hookSpecificOutput.additionalContext, /Use Vim/);
+});
+
+test("auto-recall reuses a cached digest for an identical recall", async () => {
+ const uri = "viking://user/zeus/memories/events/retry.md";
+ const stateDir = await mkdtemp(join(tmpdir(), "ov-auto-recall-cache-"));
+ const options = {
+ prompt: "How should retries work?",
+ entry: { uri, score: 0.91, type: "events", mode: "full", summary: "Retry with backoff" },
+ rendered: `<memory uri="${uri}">${"Retry with exponential backoff. ".repeat(80)}</memory>`,
+ compressorOutput: `- Retry with exponential backoff. source: ${uri}`,
+ stateDir,
+ };
+ try {
+ const first = await runEndpointCompressionCase(options);
+ const second = await runEndpointCompressionCase(options);
+ assert.equal(first.compressorCalls, 1);
+ assert.equal(second.compressorCalls, 0);
+ assert.match(second.output.hookSpecificOutput.additionalContext, /exponential backoff/);
+ } finally {
+ await rm(stateDir, { recursive: true, force: true });
+ }
+});
+
+test("auto-recall gives the compressor full content from the raw-search fallback", async () => {
+ const stateDir = await mkdtemp(join(tmpdir(), "ov-auto-recall-raw-compress-"));
+ const memories = Array.from({ length: 6 }, (_, index) => ({
+ uri: `viking://user/zeus/memories/events/raw-${index}.md`,
+ level: 2,
+ score: 0.9 - (index * 0.01),
+ category: "events",
+ abstract: `raw fallback memory ${index}`,
+ }));
+ const sentinels = memories.map((_, index) => `DETAIL_AFTER_320_${index}`);
+
+ try {
+ await withFakeCodex(
+ `- kept raw detail source: ${memories[0].uri}`,
+ async ({ promptLog, env }) => {
+ const result = await withMockOpenViking(async (req, res) => {
+ const url = new URL(req.url, "http://127.0.0.1");
+ if (req.method === "GET" && url.pathname === "/health") {
+ writeJson(res, { status: "ok", result: { ok: true } });
+ return;
+ }
+ if (req.method === "POST" && url.pathname === "/api/v1/search/recall") {
+ writeStatusJson(res, 404, { status: "error", error: "not found" });
+ return;
+ }
+ if (req.method === "POST" && url.pathname === "/api/v1/search/search") {
+ const body = await readRequestBody(req);
+ if (body.mode === "context") {
+ writeStatusJson(res, 400, {
+ status: "error",
+ error: "Extra inputs are not permitted: mode",
+ });
+ return;
+ }
+ const isMemoryScope = body.target_uri === "viking://user/zeus/memories";
+ writeJson(res, {
+ status: "ok",
+ result: { memories: isMemoryScope ? memories : [], skills: [] },
+ });
+ return;
+ }
+ if (req.method === "GET" && url.pathname === "/api/v1/content/read") {
+ const index = memories.findIndex((item) => item.uri === url.searchParams.get("uri"));
+ writeJson(res, {
+ status: "ok",
+ result: `${"x".repeat(320)}${sentinels[index]}${"y".repeat(80)}`,
+ });
+ return;
+ }
+ writeStatusJson(res, 404, { status: "error", error: "not found" });
+ }, async (baseUrl) => runAutoRecall(
+ { prompt: "use the raw fallback details", session_id: "codex:raw-compress" },
+ {
+ ...env,
+ OPENVIKING_AUTO_RECALL: "1",
+ OPENVIKING_CODEX_STATE_DIR: stateDir,
+ OPENVIKING_STATE_DIR: stateDir,
+ OPENVIKING_CONFIG_FILE: join(stateDir, "missing-ov.conf"),
+ OPENVIKING_CLI_CONFIG_FILE: join(stateDir, "missing-ovcli.conf"),
+ OPENVIKING_CREDENTIAL_SOURCE: "env",
+ OPENVIKING_RECALL_COMPRESS: "1",
+ OPENVIKING_RECALL_LIMIT: "6",
+ OPENVIKING_RECALL_TIMEOUT_MS: "10000",
+ OPENVIKING_MIN_QUERY_LENGTH: "1",
+ OPENVIKING_SCORE_THRESHOLD: "0",
+ OPENVIKING_TIMEOUT_MS: "5000",
+ OPENVIKING_URL: baseUrl,
+ OPENVIKING_USER: "zeus",
+ },
+ ));
+
+ const compressorPrompt = await readFile(promptLog, "utf8");
+ for (const sentinel of sentinels) assert.match(compressorPrompt, new RegExp(sentinel));
+ assert.match(JSON.parse(result.stdout).hookSpecificOutput.additionalContext, /kept raw detail/);
+ },
+ );
+ } finally {
+ await rm(stateDir, { recursive: true, force: true });
+ }
+});
+
test("auto-recall passes the configured compressor base URL to Codex", async () => {
const result = await runEndpointCompressionCase({
prompt: "Explain HTTP 429",
@@ -406,7 +534,10 @@ test("auto-recall passes the configured compressor base URL to Codex", async ()
},
rendered: "<memory_group>Retry with backoff</memory_group>",
compressorOutput: "NO_RELEVANT_MEMORY",
- extraEnv: { OPENVIKING_RECALL_COMPRESS_BASE_URL: "https://compressor.example/v1" },
+ extraEnv: {
+ OPENVIKING_RECALL_COMPRESS_BASE_URL: "https://compressor.example/v1",
+ OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS: "0",
+ },
});
assert.ok(result.compressorArgs.includes('model_provider="openviking_compressor"'));
@@ -427,6 +558,7 @@ test("auto-recall falls back to a bounded deterministic digest when endpoint com
rendered: "<memory_group>Use Vim</memory_group>",
compressorOutput: "",
exitCode: 1,
+ extraEnv: { OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS: "0" },
});
assert.match(result.output.hookSpecificOutput.additionalContext, /Use Vim/);
@@ -462,7 +594,10 @@ syncBuiltinESMExports();
},
rendered: "<memory_group>Use Vim</memory_group>",
compressorOutput: "unused",
- extraEnv: { NODE_OPTIONS: `--require=${preloadPath}` },
+ extraEnv: {
+ NODE_OPTIONS: `--require=${preloadPath}`,
+ OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS: "0",
+ },
});
assert.match(result.output.hookSpecificOutput.additionalContext, /Use Vim/);
diff --git a/examples/codex-memory-plugin/scripts/config.mjs b/examples/codex-memory-plugin/scripts/config.mjs
index b3df254d5..90761af0d 100644
--- a/examples/codex-memory-plugin/scripts/config.mjs
+++ b/examples/codex-memory-plugin/scripts/config.mjs
@@ -35,6 +35,7 @@
* OPENVIKING_RECALL_TIMEOUT_MS, OPENVIKING_RECALL_COMPRESS_TIMEOUT_MS
* OPENVIKING_RECALL_COMPRESS_MODEL, OPENVIKING_RECALL_COMPRESS_THINKING
* OPENVIKING_RECALL_COMPRESS_BASE_URL
+ * OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS
* OPENVIKING_RECALL_LIMIT, OPENVIKING_SCORE_THRESHOLD
* OPENVIKING_WORKSPACE_PEER, OPENVIKING_RECALL_PEER_SCOPE
* OPENVIKING_NO_AUTO_INJECT, OPENVIKING_PROFILE_TOKEN_BUDGET
@@ -230,6 +231,10 @@ export function loadConfig(cwd = process.cwd()) {
process.env.OPENVIKING_RECALL_COMPRESS_DETECT_TTL_MS,
num(cx.recallCompressDetectTtlMs, 604800000),
))),
+ recallCompressMinInputChars: Math.max(0, Math.floor(num(
+ process.env.OPENVIKING_RECALL_COMPRESS_MIN_INPUT_CHARS,
+ num(cx.recallCompressMinInputChars, 1500),
+ ))),
recallCompressMaxInputChars: Math.max(1000, Math.floor(num(
process.env.OPENVIKING_RECALL_COMPRESS_MAX_INPUT_CHARS,
num(cx.recallCompressMaxInputChars, 18000),
diff --git a/examples/codex-memory-plugin/scripts/shared/recall-compress-core.mjs b/examples/codex-memory-plugin/scripts/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/codex-memory-plugin/scripts/shared/recall-compress-core.mjs
+++ b/examples/codex-memory-plugin/scripts/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/dsh-memory-plugin/shared/recall-compress-core.mjs b/examples/dsh-memory-plugin/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/dsh-memory-plugin/shared/recall-compress-core.mjs
+++ b/examples/dsh-memory-plugin/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/memory-plugin-shared/lib/recall-compress-core.mjs b/examples/memory-plugin-shared/lib/recall-compress-core.mjs
index d173e5dc4..7557e1501 100644
--- a/examples/memory-plugin-shared/lib/recall-compress-core.mjs
+++ b/examples/memory-plugin-shared/lib/recall-compress-core.mjs
@@ -85,10 +85,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -148,6 +154,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -157,7 +164,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -170,7 +179,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -184,9 +194,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/memory-plugin-shared/recall-compress-core.test.mjs b/examples/memory-plugin-shared/recall-compress-core.test.mjs
index 3d6b4c309..8ea278965 100644
--- a/examples/memory-plugin-shared/recall-compress-core.test.mjs
+++ b/examples/memory-plugin-shared/recall-compress-core.test.mjs
@@ -1,6 +1,6 @@
import test from "node:test";
import assert from "node:assert/strict";
-import { mkdtemp } from "node:fs/promises";
+import { mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
@@ -28,6 +28,36 @@ test("compressed context keeps only citations to served URIs", () => {
);
});
+test("URI repair validates every citation while preserving served paths with spaces", () => {
+ const plain = "viking://user/u/a.md";
+ const served = "viking://user/u/memories/preferences/cross runtime.md";
+ assert.equal(repairDigestUris(`- raw source: ${served}`, [served]), `- raw source: ${served}`);
+ assert.equal(
+ repairDigestUris("- escaped source: viking://user/u/memories/preferences/cross%20runtime.md", [served]),
+ `- escaped source: ${served}`,
+ );
+ assert.equal(repairDigestUris(`- near miss source: ${plain}.evil`, [plain]), `- near miss source: ${plain}`);
+ assert.equal(repairDigestUris(`- mixed source: ${plain} and viking://evil/x.md`, [plain]), "");
+ assert.equal(repairDigestUris(`- mixed source: ${served} and viking://evil/x.md`, [served]), "");
+});
+
+test("short compression input returns the caller's bounded context", async () => {
+ let called = false;
+ const result = await compressRecallContext({
+ query: "q",
+ rendered: "full compressor input",
+ shortContext: "bounded display context",
+ cfg: { recallCompressMinInputChars: 100 },
+ runCompressor: async () => {
+ called = true;
+ return "NO_RELEVANT_MEMORY";
+ },
+ });
+
+ assert.deepEqual(result, { status: "ok", context: "bounded display context" });
+ assert.equal(called, false);
+});
+
test("compression cache is reused only for the same request", async () => {
const cachePath = await tempPath("digest.json");
const rendered = `<memory uri="viking://a">${"x".repeat(2000)}</memory>`;
@@ -44,11 +74,16 @@ test("compression cache is reused only for the same request", async () => {
await compressRecallContext({
query: "first", rendered, entries, runCompressor, cachePath,
});
+ const cached = JSON.parse(await readFile(cachePath, "utf8"));
+ await writeFile(cachePath, JSON.stringify({ ...cached, digest: "OpenViking memory digest:" }));
+ await compressRecallContext({
+ query: "first", rendered, entries, runCompressor, cachePath,
+ });
await compressRecallContext({
query: "different", rendered, entries, runCompressor, cachePath,
});
- assert.equal(calls, 2);
+ assert.equal(calls, 3);
});
test("unusable compressor output triggers the caller fallback", async () => {
@@ -62,6 +97,17 @@ test("unusable compressor output triggers the caller fallback", async () => {
assert.deepEqual(result, { status: "failed", context: "" });
});
+test("compression fails when URI repair rejects every bullet", async () => {
+ const result = await compressRecallContext({
+ query: "q",
+ rendered: `<memory uri="viking://served/a.md">${"x".repeat(2000)}</memory>`,
+ entries: [{ uri: "viking://served/a.md" }],
+ runCompressor: async () => "- invented source: viking://unrelated/fake.md",
+ });
+
+ assert.deepEqual(result, { status: "failed", context: "" });
+});
+
test("NO_RELEVANT_MEMORY is a successful empty compression result", async () => {
const result = await compressRecallContext({
query: "q",
diff --git a/examples/opencode-plugin/lib/shared/recall-compress-core.mjs b/examples/opencode-plugin/lib/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/opencode-plugin/lib/shared/recall-compress-core.mjs
+++ b/examples/opencode-plugin/lib/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/pi-coding-agent-extension/shared/recall-compress-core.mjs b/examples/pi-coding-agent-extension/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/pi-coding-agent-extension/shared/recall-compress-core.mjs
+++ b/examples/pi-coding-agent-extension/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });
diff --git a/examples/zcode-memory-plugin/scripts/shared/recall-compress-core.mjs b/examples/zcode-memory-plugin/scripts/shared/recall-compress-core.mjs
index 6f8fe485b..1ced4cf4d 100644
--- a/examples/zcode-memory-plugin/scripts/shared/recall-compress-core.mjs
+++ b/examples/zcode-memory-plugin/scripts/shared/recall-compress-core.mjs
@@ -86,10 +86,16 @@ export function repairDigestUris(digest, validUris = []) {
lines.push(line);
continue;
}
+ let tokenized = line;
+ for (const uri of [...valid].sort((a, b) => b.length - a.length)) {
+ if (/\s/.test(uri)) tokenized = tokenized.split(uri).join(encodeURI(uri));
+ }
let dropped = false;
- const repaired = line.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
- if (validSet.has(uri)) return uri;
- const nearest = nearestUri(uri, valid);
+ const repaired = tokenized.replace(/viking:\/\/[^\s<>"')\]]+/g, (uri) => {
+ let decoded = uri;
+ try { decoded = decodeURI(uri); } catch { /* keep the original candidate */ }
+ if (validSet.has(decoded)) return decoded;
+ const nearest = nearestUri(decoded, valid);
if (nearest) return nearest;
dropped = true;
return uri;
@@ -149,6 +155,7 @@ async function writeCache(path, value) {
export async function compressRecallContext({
query,
rendered,
+ shortContext = rendered,
entries = [],
cfg = {},
runCompressor,
@@ -158,7 +165,9 @@ export async function compressRecallContext({
const input = String(rendered || "").trim();
if (!input) return { status: COMPRESS_EMPTY, context: "" };
const minChars = Math.max(0, Number(cfg.recallCompressMinInputChars ?? 1500));
- if (input.length < minChars) return { status: COMPRESS_OK, context: input };
+ if (input.length < minChars) {
+ return { status: COMPRESS_OK, context: String(shortContext ?? input).trim() };
+ }
const maxInputChars = Math.max(1000, Number(cfg.recallCompressMaxInputChars || 18000));
const maxBullets = Math.max(1, Number(cfg.recallCompressMaxBullets || 6));
@@ -171,7 +180,8 @@ export async function compressRecallContext({
});
const cached = await readCache(cachePath);
if (cached?.key === key && typeof cached.digest === "string") {
- return { status: COMPRESS_OK, context: cached.digest };
+ const digest = normalizeCompressedContext(cached.digest, 4000, maxBullets);
+ if (digest) return { status: COMPRESS_OK, context: digest };
}
const prompt = buildRecallCompressionPrompt({
@@ -185,9 +195,10 @@ export async function compressRecallContext({
if (!normalized) return { status: COMPRESS_EMPTY, context: "" };
const validUris = entries.map((entry) => entry?.uri).filter(Boolean);
- const digest = repairDigestUris(normalized, validUris.length
+ const repaired = repairDigestUris(normalized, validUris.length
? validUris
: (input.match(/viking:\/\/[^\s<>"']+/g) || []));
+ const digest = normalizeCompressedContext(repaired, 4000, maxBullets);
if (!digest) return { status: COMPRESS_FAILED, context: "" };
await writeCache(cachePath, { key, digest, updatedAt: now || 0 });