The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
perf(gateguard): skip plain character runs when lexing code
Jeffrey Montoyaeab92ed21d8f
diff --git a/scripts/lib/gateguard-code-lexer.js b/scripts/lib/gateguard-code-lexer.jsindex ab855f492..29676d51f 100644--- a/scripts/lib/gateguard-code-lexer.js+++ b/scripts/lib/gateguard-code-lexer.js@@ -75,7 +75,43 @@ function endsWithContinuation(code) {return code[end - 1] === '\\';}+const CODE_SPECIALS = new WeakMap();++function escapeClass(chars) {+ return chars.replace(/[\\\]^-]/g, ch => `\\${ch}`);+}++function codeSpecials(spec) {+ let specials = CODE_SPECIALS.get(spec);+ if (!specials) {+ const chars = `\n\r/${spec.lineComment === '#' ? '#' : ''}${spec.quotes || ''}${spec.forbiddenCode || ''}${spec.jsx !== undefined ? '<-' : ''}${spec.rustChars ? "'" : ''}`;+ specials = { pattern: new RegExp(`[${escapeClass(chars)}]`, 'g'), chars: new Set(chars) };+ CODE_SPECIALS.set(spec, specials);+ }+ return specials;+}++function nextSpecial(pattern, text, i) {+ pattern.lastIndex = i;+ const match = pattern.exec(text);+ return match ? match.index : text.length;+}++const LINE_BREAK = /[\n\r]/g;+const BLOCK_SPECIALS = /[\n\r*/]/g;+const STRING_SPECIALS = new Map();++function stringSpecials(quote) {+ let pattern = STRING_SPECIALS.get(quote);+ if (!pattern) {+ pattern = new RegExp(`[\\n\\r\\\\${escapeClass(quote)}]`, 'g');+ STRING_SPECIALS.set(quote, pattern);+ }+ return pattern;+}+function lexCodeLines(text, spec) {+ const specials = codeSpecials(spec);const lines = [];let code = '';let state = 'code';@@ -118,8 +154,15 @@ function lexCodeLines(text, spec) {if (ch === '\r' && text[i + 1] === '\n') i++;continue;}- if (state === 'line') continue;+ if (state === 'line') {+ i = nextSpecial(LINE_BREAK, text, i) - 1;+ continue;+ }if (state === 'block') {+ if (ch !== '*' && ch !== '/') {+ i = nextSpecial(BLOCK_SPECIALS, text, i + 1) - 1;+ continue;+ }if (ch === '*' && text[i + 1] === '/') {endComment(i);state = 'code';@@ -131,6 +174,14 @@ function lexCodeLines(text, spec) {continue;}if (state === 'string') {+ if (ch !== '\\' && ch !== quote) {+ const end = nextSpecial(stringSpecials(quote), text, i + 1);+ const run = text.slice(i, end);+ append(run);+ stringBody += run;+ i = end - 1;+ continue;+ }append(ch);if (ch === '\\') {const next = text[i + 1];@@ -145,6 +196,12 @@ function lexCodeLines(text, spec) {}continue;}+ if (!specials.chars.has(ch)) {+ const end = nextSpecial(specials.pattern, text, i + 1);+ append(text.slice(i, end));+ i = end - 1;+ continue;+ }if (spec.lineComment === '//' && ch === '/' && text[i + 1] === '/') {state = 'line';commentStart = i + 2;diff --git a/tests/lib/gateguard-code-lexer.test.js b/tests/lib/gateguard-code-lexer.test.jsindex e40908ac3..8b9e9de6b 100644--- a/tests/lib/gateguard-code-lexer.test.js+++ b/tests/lib/gateguard-code-lexer.test.js@@ -35,6 +35,26 @@ test('a comment-only change to C-family code is trivial', () => {assert.strictEqual(isTrivialEdit('a(); // x', 'a(); // y', JS), true);});+test('comment markers inside a string are code', () => {+ assert.strictEqual(isTrivialEdit('const s = "a // b";', 'const s = "a // c";', JS), false);+ assert.strictEqual(isTrivialEdit("const s = 'x /* y */ z';", "const s = 'x /* q */ z';", JS), false);+});++test('an escaped quote does not end a string', () => {+ assert.strictEqual(isTrivialEdit('const s = "a\\" // b";', 'const s = "a\\" // c";', JS), false);+});++test('a long block comment with stars and slashes stays a comment', () => {+ const before = 'a();\n/* one * two / three\n * four ** five\n */\nb();';+ const after = 'a();\n/* one * two / 3\n * 4 ** five\n */\nb();';+ assert.strictEqual(isTrivialEdit(before, after, JS), true);+});++test('a line comment ending in CRLF keeps the next line as code', () => {+ assert.strictEqual(isTrivialEdit('a(); // x\r\nb();', 'a(); // y\r\nc();', JS), false);+ assert.strictEqual(isTrivialEdit('a(); // x\r\nb();', 'a(); // y\r\nb();', JS), true);+});+test('a code change is not trivial', () => {assert.strictEqual(isTrivialEdit('a();', 'b();', JS), false);});