Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { describe, it, expect } from 'bun:test';
import { truncateObserverAuditInput } from '../../src/utils/observer-audit.js';
const MARKER = '…[TRUNCATED]';
const bytes = (s: string): number => Buffer.byteLength(s, 'utf8');
/** A lone surrogate would make JSON.stringify escape half of a pair. */
function hasLoneSurrogate(s: string): boolean {
for (let i = 0; i < s.length; i++) {
const unit = s.charCodeAt(i);
if (unit >= 0xd800 && unit <= 0xdbff) {
const next = s.charCodeAt(i + 1);
if (!(next >= 0xdc00 && next <= 0xdfff)) return true;
i++;
} else if (unit >= 0xdc00 && unit <= 0xdfff) {
return true;
}
}
return false;
}
describe('observer audit input cap', () => {
it('keeps an input that fits in the byte budget as-is', () => {
const input = { command: 'ls -la' };
expect(truncateObserverAuditInput(input, 4096)).toBe(JSON.stringify(input));
});
it('counts UTF-8 bytes, not UTF-16 code units', () => {
// 4096 CJK characters are 4096 code units but 12,288 bytes: the old
// `.length` check let the whole string through.
const truncated = truncateObserverAuditInput('観'.repeat(4096));
expect(bytes(truncated.slice(0, -MARKER.length))).toBeLessThanOrEqual(4096);
expect(truncated.endsWith(MARKER)).toBe(true);
});
it('does not split a surrogate pair at the cut', () => {
// 'a' * 4095 leaves one byte before the cap, so an emoji cannot fit whole.
const truncated = truncateObserverAuditInput('a'.repeat(4095) + '😀😀');
expect(bytes(truncated.slice(0, -MARKER.length))).toBeLessThanOrEqual(4096);
expect(hasLoneSurrogate(truncated)).toBe(false);
expect(truncated).toBe('a'.repeat(4095) + MARKER);
});
it('keeps a multi-byte character that fits in the last byte slot', () => {
const truncated = truncateObserverAuditInput('a'.repeat(4094) + 'é');
expect(truncated).toBe('a'.repeat(4094) + 'é');
});
it('never returns more than the budget plus the marker', () => {
for (const input of ['é'.repeat(3000), '中'.repeat(2000), '😀'.repeat(2000)]) {
expect(bytes(truncateObserverAuditInput(input, 1024))).toBeLessThanOrEqual(1024 + bytes(MARKER));
}
});
});