Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(breaker): hash the toolKey so long-prefix Bash commands don't collide

Snehitha4f486405dfe6
diff --git a/docs/pr-evidence/breaker-key-after.png b/docs/pr-evidence/breaker-key-after.png
new file mode 100644
index 00000000..340dce8b
Binary files /dev/null and b/docs/pr-evidence/breaker-key-after.png differ
diff --git a/docs/pr-evidence/breaker-key-before.png b/docs/pr-evidence/breaker-key-before.png
new file mode 100644
index 00000000..a5589814
Binary files /dev/null and b/docs/pr-evidence/breaker-key-before.png differ
diff --git a/src/main/breaker.ts b/src/main/breaker.ts
index a2e9718b..373fa995 100644
--- a/src/main/breaker.ts
+++ b/src/main/breaker.ts
@@ -22,6 +22,8 @@
* de-escalates a level per healthy beat (recovery), and `hardStop` is OFF by
* default — without it the ladder caps at `constrained` and never kills.
*/
+
+import { createHash } from 'node:crypto';
import type { CircuitBreakerConfig } from './config';
import type { AgentUsageSample } from './usage';
@@ -192,17 +194,25 @@ export class CircuitBreaker {
private toolKey(toolName: string | undefined, toolInput: unknown): string {
// Truncating replacer: a Write/Edit tool_input carries the whole file body
// (up to MBs), and this runs synchronously inside the hook reply path on
- // EVERY PostToolUse — serializing it all only to keep 200 chars was a
+ // EVERY PostToolUse — serializing it all only to keep a short key was a
// multi-MB transient allocation per large write. Capping each string field
- // bounds the work while keeping the key semantics (a repeat of the same
- // call still yields the same key; distinct calls still differ within the
- // first 200 chars far more often than full serialization ever mattered).
+ // bounds the work.
+ //
+ // The key is a HASH of that capped serialization, not a slice of it (#377):
+ // the old `inp.slice(0, 200)` collided on Bash commands sharing a long
+ // identical preamble (absolute paths, a cd, an interpreter invocation) —
+ // nine DIFFERENT measurements in a row read as nine identical calls and
+ // constrained the agent. The per-string cap is 4096 so late-differing
+ // inputs still reach the hash with something to distinguish; a cap of 250
+ // would make two commands first differing at char 312 hash identically.
+ // Genuinely identical calls still key identically, so every real loop
+ // caught before is still caught.
let inp = '';
try {
inp = JSON.stringify(toolInput, (_k, v) =>
- typeof v === 'string' && v.length > 250 ? v.slice(0, 250) : v) ?? '';
+ typeof v === 'string' && v.length > 4096 ? v.slice(0, 4096) : v) ?? '';
} catch { inp = String(toolInput); }
- return `${toolName ?? '?'}:${inp.slice(0, 200)}`;
+ return `${toolName ?? '?'}:${createHash('sha256').update(inp).digest('hex')}`;
}
// ── periodic evaluation (called by the heartbeat beat) ────────────────────
diff --git a/test/breaker.test.cjs b/test/breaker.test.cjs
index ca08ce3a..48a9e80f 100644
--- a/test/breaker.test.cjs
+++ b/test/breaker.test.cjs
@@ -2,8 +2,8 @@
/**
* Circuit-breaker policy tests. Self-contained, no test framework — run with
* `node test/breaker.test.cjs` (mirrors test/agent-provider.test.cjs). breaker.ts
- * only has type-only imports, so it transpiles standalone with the bundled
- * `typescript` compiler.
+ * imports only node builtins (node:crypto), so it transpiles standalone with the
+ * bundled `typescript` compiler and runs under plain node.
*
* Focus: the no-progress false-positive fixes (upstream issue #109 + fleet
* evidence — compaction / inbox-ack bursts and background work tripping
@@ -210,6 +210,44 @@ test('huge inputs differing early still count as distinct calls', () => {
assert.equal(d.state.level, 'healthy', `reason: ${d.state.reason}`);
});
+// #377: Bash commands routinely open with a long identical preamble (absolute
+// paths, a cd, an interpreter invocation). Under the old `slice(0, 200)` key,
+// commands first differing past that horizon collided, and nine DIFFERENT
+// measurements in a row constrained the agent doing the most careful work.
+test('inputs differing LATE (past the old 200-char horizon) count as distinct calls', () => {
+ const b = makeBreaker();
+ // ~310-char shared preamble, in the shape of the reported repro
+ const preamble = 'SP=/private/tmp/claude-501/-Users-me-Documents-Projects/0000000-0000-0000-0000-000000000000/scratchpad\n'
+ + "python3 - <<'PY'\nimport json\n"
+ + 'SP="/private/tmp/claude-501/-Users-me-Documents-Projects/0000000-0000-0000-0000-000000000000/scratchpad"\n'
+ + 'doc = json.load(open(SP + "/edit.otio"))\n';
+ for (let i = 0; i < 9; i++) {
+ b.recordToolUse('a', 'Bash', { command: `${preamble}print(measure_${i}(doc))\nPY` });
+ }
+ const d = beat(b, 'a', null, true, T0);
+ assert.equal(d.state.level, 'healthy', `reason: ${d.state.reason}`);
+});
+
+test('identical long-preamble calls still trip the loop arm (no strictness lost)', () => {
+ const b = makeBreaker();
+ const cmd = 'SP=/private/tmp/claude-501/very/long/identical/path\n' + 'x'.repeat(400);
+ for (let i = 0; i < 8; i++) b.recordToolUse('a', 'Bash', { command: cmd });
+ const d = beat(b, 'a', null, true, T0);
+ assert.equal(d.state.level, 'steering', `reason: ${d.state.reason}`);
+});
+
+test('strings capped at 4096 still key equal when they differ only past the cap', () => {
+ // The cap bounds serialization work on the hook reply path; inputs that
+ // differ only beyond it are indistinguishable BY DESIGN (bounded work wins
+ // at that size), and must at least behave consistently: same key, counted
+ // as repeats — never a crash or a flapping key.
+ const b = makeBreaker();
+ const head = 'y'.repeat(5000);
+ for (let i = 0; i < 8; i++) b.recordToolUse('a', 'Bash', { command: head + `tail_${i}` });
+ const d = beat(b, 'a', null, true, T0);
+ assert.equal(d.state.level, 'steering', `reason: ${d.state.reason}`);
+});
+
// ── recovery still works ─────────────────────────────────────────────────────
test('a healthy beat de-escalates one level', () => {