Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* Ambient-writeback gate (WP4/G): every rule and reason is pinned here with
* RULE-DISTINCT inputs (OV2-3) — "Thanks" pins too_short (rule 2 fires
* before the ack lexicon, deliberately: the hermetic zero-LLM guarantee),
* a ≥20-char pure ack pins ack_or_greeting, a CJK 6-char fact passes via
* the lowered floor (F6). The reason vocabulary is frozen (additive only) —
* heartbeat entries and skip sidecars carry these codes.
*/
import { describe, test, expect } from 'bun:test';
import {
gateWritebackTurn,
normalizeTurnText,
turnHash24,
MIN_TURN_CHARS,
MIN_TURN_CHARS_CJK,
MAX_TURN_CHARS,
} from '../../src/core/facts/writeback-gate.ts';
describe('gateWritebackTurn — skip rules, first hit wins', () => {
test('empty / non-string → empty', () => {
expect(gateWritebackTurn('')).toEqual({ ok: false, reason: 'empty' });
expect(gateWritebackTurn(' \n ')).toEqual({ ok: false, reason: 'empty' });
expect(gateWritebackTurn(undefined)).toEqual({ ok: false, reason: 'empty' });
});
test('"Thanks" → too_short (rule 2 precedes the lexicon — the hermetic guarantee)', () => {
expect(gateWritebackTurn('Thanks')).toEqual({ ok: false, reason: 'too_short' });
expect(gateWritebackTurn('ok')).toEqual({ ok: false, reason: 'too_short' });
expect(gateWritebackTurn('yes please')).toEqual({ ok: false, reason: 'too_short' });
});
test('≥20-char pure acknowledgement → ack_or_greeting (rule-distinct pin)', () => {
expect(gateWritebackTurn('okay sounds good thanks so much!')).toEqual({ ok: false, reason: 'ack_or_greeting' });
expect(gateWritebackTurn('Good morning, thanks a lot')).toEqual({ ok: false, reason: 'ack_or_greeting' });
});
test('CJK floor (F6): a 6-char CJK fact passes; a 6-char CJK ack-length blob under 10 chars does not', () => {
// "I am allergic to peanuts" — 6 chars, information-dense.
const r = gateWritebackTurn('我对花生过敏');
expect(r.ok).toBe(false); // 6 < 10 — still under the CJK floor
const r2 = gateWritebackTurn('我对花生严重过敏,随身带肾上腺素笔');
expect(r2.ok).toBe(true);
expect(MIN_TURN_CHARS_CJK).toBeLessThan(MIN_TURN_CHARS);
});
test('slash command → slash_command', () => {
expect(gateWritebackTurn('/compact and then continue with the review')).toEqual({ ok: false, reason: 'slash_command' });
});
test('questions carrying no new facts → question_only; a fact+question mix passes', () => {
expect(gateWritebackTurn('What time is the standup tomorrow? Can you check the calendar?')).toEqual({ ok: false, reason: 'question_only' });
const mixed = gateWritebackTurn('I moved the standup to 9am. Can you update the calendar?');
expect(mixed.ok).toBe(true);
});
test('quoted / tool output → quoted_or_tool_output; quoted material with substantive user residue passes', () => {
const quoted = ['> Alice wrote: the deploy failed', '> and the logs show a timeout'].join('\n');
expect(gateWritebackTurn(quoted)).toEqual({ ok: false, reason: 'quoted_or_tool_output' });
const fenced = '```\nERROR: connection refused at db.example:5432\nstack trace line line line\n```';
expect(gateWritebackTurn(fenced)).toEqual({ ok: false, reason: 'quoted_or_tool_output' });
const indented = Array.from({ length: 4 }, (_, i) => ` tool output row ${i}`).join('\n');
expect(gateWritebackTurn(indented)).toEqual({ ok: false, reason: 'quoted_or_tool_output' });
const withResidue = 'I decided we are switching the staging db to Postgres 17.\n> Alice wrote: the deploy failed';
expect(gateWritebackTurn(withResidue).ok).toBe(true);
});
test('#8410: an accepted turn banks and hashes only the unquoted lines; fenced lines stay', () => {
const own = 'No, we deploy on Tuesdays and staging uses Postgres 17.';
const quoted = gateWritebackTurn(`> The deploy moves to Fridays.\n\n${own}`);
expect(quoted).toEqual({ ok: true, normalized: own, hash24: turnHash24(own) });
const backtick = gateWritebackTurn(`${own}\n\`\`\`\`\n> code line\n\`\`\`\n> still code\n\`\`\`\`\n> quoted after`);
expect(backtick.ok && backtick.normalized).toBe(`${own} \`\`\`\` > code line \`\`\` > still code \`\`\`\``);
const tilde = gateWritebackTurn(`${own}\n~~~text\n> tilde code\n~~~\n> quoted after`);
expect(tilde.ok && tilde.normalized).toBe(`${own} ~~~text > tilde code ~~~`);
const comparison = gateWritebackTurn(`${own}\n>90% of our traffic is EU.\n>> The deploy moves to Fridays.`);
expect(comparison.ok && comparison.normalized).toBe(`${own} >90% of our traffic is EU.`);
});
test('#8410: a turn that is only quoted lines plus a tilde-fenced paste is quoted material', () => {
const turn = '> Alice wrote: the deploy failed\n~~~\nERROR: connection refused at db.example:5432\nstack trace line line line\n~~~';
expect(gateWritebackTurn(turn)).toEqual({ ok: false, reason: 'quoted_or_tool_output' });
});
test('bulk paste over the cap → bulk_paste (imported text needs an explicit ask)', () => {
const big = 'a decision was made. '.repeat(500); // > 8000 chars
expect(big.length).toBeGreaterThan(MAX_TURN_CHARS);
expect(gateWritebackTurn(big)).toEqual({ ok: false, reason: 'bulk_paste' });
});
});
describe('gateWritebackTurn — ok path: normalization + idempotency hash', () => {
test('substantive statement passes with NFC/collapsed normalization and a stable 24-hex hash', () => {
const a = gateWritebackTurn('I prefer dark mode\n in every editor.');
const b = gateWritebackTurn('I prefer dark mode in every editor.');
if (!a.ok || !b.ok) throw new Error('expected ok');
expect(a.normalized).toBe('I prefer dark mode in every editor.');
expect(a.hash24).toBe(b.hash24); // whitespace shape does not change the key
expect(a.hash24).toMatch(/^[0-9a-f]{24}$/);
});
test('different turns → different hashes; helpers agree with the gate', () => {
const a = gateWritebackTurn('I prefer dark mode in every editor.');
const c = gateWritebackTurn('I have a mild cough today, nothing serious.');
if (!a.ok || !c.ok) throw new Error('expected ok');
expect(a.hash24).not.toBe(c.hash24);
expect(turnHash24(normalizeTurnText('I prefer dark mode in every editor.'))).toBe(a.hash24);
});
});
describe('gateWritebackTurn — Claude Code paste blocks (#5812)', () => {
const PASTE = (body: string, id = '2830') => `<pasted_content id="${id}">\n${body}\n</pasted_content id="${id}">`;
const EMAIL = 'The offsite moves to March and the budget is final, per the finance team.';
test('a paste-only turn → pasted_content (nothing banked)', () => {
expect(gateWritebackTurn(`\n\n${PASTE(EMAIL)}\n`)).toEqual({ ok: false, reason: 'pasted_content' });
expect(gateWritebackTurn(`${PASTE(EMAIL, '1')}\n\n${PASTE(EMAIL, '2')}`)).toEqual({ ok: false, reason: 'pasted_content' });
});
test('a mixed turn banks only the user\'s own words', () => {
const r = gateWritebackTurn(`Please remember this note I got: \n\n${PASTE(EMAIL)}\n\n and also I prefer dark roast coffee.`);
if (!r.ok) throw new Error(`expected ok, got ${r.reason}`);
expect(r.normalized).toBe('Please remember this note I got: and also I prefer dark roast coffee.');
expect(JSON.stringify(r)).not.toContain('finance team');
expect(JSON.stringify(r)).not.toContain('pasted_content');
});
test('an unclosed paste tail is stripped too', () => {
const r = gateWritebackTurn(`I moved our weekly sync to Tuesdays at 10am.\n\n<pasted_content id="9">\n${EMAIL}`);
if (!r.ok) throw new Error(`expected ok, got ${r.reason}`);
expect(r.normalized).toBe('I moved our weekly sync to Tuesdays at 10am.');
});
test('the remaining rules run on the user\'s own words', () => {
expect(gateWritebackTurn(`thanks ${PASTE(EMAIL)}`)).toEqual({ ok: false, reason: 'too_short' });
expect(gateWritebackTurn(`Can you summarize this email for me please? ${PASTE(EMAIL)}`)).toEqual({ ok: false, reason: 'question_only' });
});
test('CJK own words keep the CJK floor after the strip', () => {
const r = gateWritebackTurn(`我对花生严重过敏,随身带肾上腺素笔 ${PASTE(EMAIL)}`);
if (!r.ok) throw new Error(`expected ok, got ${r.reason}`);
expect(r.normalized).toBe('我对花生严重过敏,随身带肾上腺素笔');
});
test('a multi-megabyte paste is still bulk_paste (length check before any scan)', () => {
const big = `I prefer dark roast coffee. ${PASTE('x'.repeat(3 * 1024 * 1024))}`;
expect(gateWritebackTurn(big)).toEqual({ ok: false, reason: 'bulk_paste' });
});
});