60a428612c
Memind CI / Test, build, and release guards (push) Has been cancelled
Introduce off/shadow/active compaction for large tkmind read/research and excel report payloads with optional Redis-backed ctx_fetch handles, keeping fail-open behavior when storage is unavailable. Co-authored-by: Cursor <cursoragent@cursor.com>
91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import {
|
|
applyMcpCompaction,
|
|
buildCompactSummary,
|
|
compact,
|
|
createMemoryCompactStore,
|
|
fetchCompactPayload,
|
|
resetMcpCompactStoresForTests,
|
|
resolveMcpCompactMode,
|
|
setDefaultMcpCompactStore,
|
|
} from './mcp-result-compactor.mjs';
|
|
|
|
test('resolveMcpCompactMode defaults to off', () => {
|
|
assert.equal(resolveMcpCompactMode({}), 'off');
|
|
assert.equal(resolveMcpCompactMode({ MEMIND_MCP_COMPACT_MODE: 'shadow' }), 'shadow');
|
|
assert.equal(resolveMcpCompactMode({ MEMIND_MCP_COMPACT_MODE: 'bogus' }), 'off');
|
|
});
|
|
|
|
test('compact is no-op in off mode', async () => {
|
|
const payload = 'x'.repeat(8000);
|
|
const result = await compact(payload, {
|
|
kind: 'tkmind_read',
|
|
mode: 'off',
|
|
budget: 1000,
|
|
});
|
|
assert.equal(result.text, payload);
|
|
assert.equal(result.compacted, false);
|
|
});
|
|
|
|
test('compact keeps original payload in shadow mode', async () => {
|
|
const payload = 'alpha '.repeat(800);
|
|
const result = await compact(payload, {
|
|
kind: 'tkmind_read',
|
|
mode: 'shadow',
|
|
budget: 400,
|
|
});
|
|
assert.equal(result.text, payload);
|
|
assert.equal(result.compacted, false);
|
|
assert.equal(result.mode, 'shadow');
|
|
assert.ok(result.stats.savedRatio > 0);
|
|
assert.ok(String(result.shadowSummary ?? '').length < payload.length);
|
|
});
|
|
|
|
test('compact stores handle and returns envelope in active mode', async () => {
|
|
resetMcpCompactStoresForTests();
|
|
const store = createMemoryCompactStore({ now: () => Date.now() });
|
|
setDefaultMcpCompactStore(store);
|
|
const payload = 'line\n'.repeat(900);
|
|
const result = await compact(payload, {
|
|
kind: 'excel_report',
|
|
mode: 'active',
|
|
budget: 500,
|
|
store,
|
|
});
|
|
assert.equal(result.compacted, true);
|
|
assert.ok(result.handle?.startsWith('mcpctx_'));
|
|
assert.match(result.text, /use ctx_fetch to retrieve full payload/);
|
|
|
|
const fetched = await fetchCompactPayload(result.handle, { store });
|
|
assert.equal(fetched.ok, true);
|
|
assert.equal(fetched.text, payload);
|
|
});
|
|
|
|
test('applyMcpCompaction fail-opens when store unavailable', async () => {
|
|
const payload = 'payload '.repeat(500);
|
|
const result = await applyMcpCompaction(payload, {
|
|
kind: 'tkmind_research_status',
|
|
env: { MEMIND_MCP_COMPACT_MODE: 'active', MEMIND_MCP_COMPACT_BUDGET_CHARS: '200' },
|
|
store: {
|
|
async put() {
|
|
throw new Error('store down');
|
|
},
|
|
async get() {
|
|
return null;
|
|
},
|
|
},
|
|
});
|
|
assert.equal(result.text, payload);
|
|
assert.equal(result.compacted, false);
|
|
assert.equal(result.fallback, 'store_unavailable');
|
|
});
|
|
|
|
test('buildCompactSummary preserves head and tail', () => {
|
|
const summary = buildCompactSummary('0123456789abcdef'.repeat(20), 120);
|
|
assert.match(summary, /^012/);
|
|
assert.match(summary, /cdef$/);
|
|
assert.match(summary, /omitted/);
|
|
});
|