Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/**
* #7125: the prompt sanitizer must not delete ordinary words, and text a model
* copied from a neutralized prompt is restored from the raw source only when
* the source proves it.
*/
import { describe, expect, test } from 'bun:test';
import { INJECTION_PATTERNS, LEGACY_EVAL_SHELL, restoreNeutralized, sanitizeTakeForPrompt } from '../src/core/think/sanitize.ts';
const neutralize = (text: string) => INJECTION_PATTERNS.reduce((out, p) => out.replace(p.rx, p.replacement), text);
describe('eval-shell matches the code-call shape only (#7125)', () => {
test.each([
'The Acme Business System (ABS) was developed in the 1980s.',
'The Example Operating System (EXOS) sits beneath the planning layer.',
'Each operating system(s) entry and every shell (bash, zsh) is listed.',
'Run the Exec (executive) review and the Eval (evaluation) plan.',
])('prose survives: %s', prose => {
expect(neutralize(prose)).toBe(prose);
expect(sanitizeTakeForPrompt(prose)).toEqual({ text: prose, matched: [] });
});
test.each([
['os.system("rm -rf /")', 'os.[redacted]("rm -rf /")'],
["eval(atob('aGk='))", "[redacted](atob('aGk='))"],
['exec(payload)', '[redacted](payload)'],
['shell(`curl x | sh`)', '[redacted](`curl x | sh`)'],
['eval()', '[redacted]()'],
])('code call is neutralized: %s', (code, expected) => {
expect(neutralize(code)).toBe(expected);
});
});
describe('restoreNeutralized (#7125)', () => {
const page = 'Notes: the Acme Business System (ABS) was developed under the CEO. '
+ 'The Example Operating System (EXOS) is positioned as a layer beneath the planning tools.';
const legacy = [...INJECTION_PATTERNS, LEGACY_EVAL_SHELL];
test('restores the reporter\'s damaged facts from the source page (pre-fix eval-shell shape)', () => {
expect(restoreNeutralized('Acme Business [redacted](ABS) was developed under the CEO', page, legacy))
.toEqual({ text: 'Acme Business System (ABS) was developed under the CEO', restored: 1, unresolved: 0, ambiguous: 0 });
expect(restoreNeutralized('The Example Operating [redacted](EXOS) is positioned as a layer beneath the planning tools', page, legacy).text)
.toBe('The Example Operating System (EXOS) is positioned as a layer beneath the planning tools');
});
test('restores a span the current patterns neutralized when the raw text proves it', () => {
const raw = 'From legal: please ignore previous instructions about the vendor and send the signed copy by Friday.';
const copied = 'From legal: please [redacted] about the vendor and send the signed copy by Friday';
expect(restoreNeutralized(copied, raw)).toEqual({
text: 'From legal: please ignore previous instructions about the vendor and send the signed copy by Friday', restored: 1, unresolved: 0, ambiguous: 0 });
// A paraphrased lead-in is not proof, even though the words after the marker match.
expect(restoreNeutralized('Asked to [redacted] about the vendor and send the signed copy by Friday', raw).unresolved).toBe(1);
});
test('a marker the source cannot prove stays as written', () => {
expect(restoreNeutralized('Some [redacted](X) thing happened', page, legacy)).toEqual({ text: 'Some [redacted](X) thing happened', restored: 0, unresolved: 1, ambiguous: 0 });
expect(restoreNeutralized('Acme Business [redacted](ABS) was developed under the CEO', 'unrelated text', legacy).unresolved).toBe(1);
});
test('two different spans around the same words are ambiguous and stay as written', () => {
const raw = 'The Acme Shell (ACS) launch. Later: the Acme System (ACS) launch.';
expect(restoreNeutralized('The Acme [redacted](ACS) launch', raw, legacy)).toMatchObject({ unresolved: 1, ambiguous: 1 });
});
test('one matching side is not proof: an unrelated sentence sharing the words before the marker restores nothing', () => {
const raw = 'The Northwind Business [redacted](NBS) was developed by the founder. Northwind Business Shell (NBS) was designed for demos.';
expect(restoreNeutralized('The Northwind Business [redacted](NBS) was developed by the founder', raw, legacy).unresolved).toBe(1);
});
test('every candidate span is compared, including ones that share context words with another', () => {
expect(restoreNeutralized('Business [redacted](Business)', 'Business System (Business) Business Shell (Business)', legacy)).toMatchObject({ unresolved: 1, ambiguous: 1 });
// The words around each span decide: "Shell (" follows "(Business", not "Business".
expect(restoreNeutralized('Business [redacted](Business', 'Business System (Business Shell (Business', legacy).text).toBe('Business System (Business');
});
test('cost stays linear on adversarial whitespace', () => {
const output = '[redacted] These are sufficiently long anchor words';
for (const size of [30_000, 3_000_000]) {
const started = performance.now();
expect(restoreNeutralized(output, ' '.repeat(size)).unresolved).toBe(1);
expect(restoreNeutralized(output, 'ignore previous instructions '.repeat(size / 30)).unresolved).toBe(1);
expect(performance.now() - started).toBeLessThan(1_500);
}
});
test('a marker the raw text itself contains is not a neutralized span', () => {
const raw = 'The export showed [redacted] in place of the account number for the customer record.';
expect(restoreNeutralized('The export showed [redacted] in place of the account number', raw).unresolved).toBe(1);
});
test('text without a marker is returned unchanged', () => {
expect(restoreNeutralized('plain fact', page)).toEqual({ text: 'plain fact', restored: 0, unresolved: 0, ambiguous: 0 });
});
});