Adds a 500KB rotation gate to .claude/logs/subagent-spawns.jsonl
before each append. Observed during the watchdog investigation that closed
in PR #1952: the file had accumulated 7,213 entries (~850KB)
with no rotation mechanism. Fix is ~12 LoC + 2 tests.
subagent-validator.ts appends to the spawn log on every
SubagentStart hook fire. No size check, no rotation. File grows
unbounded across all sessions for the repo.
Real numbers from the orchestkit repo: 7,213 entries ร ~120 bytes โ 850KB, accumulated over weeks of dev work.
// At the top of logSpawn(), before the append:
// Rotation threshold: 500KB (~4,000 entries โ well above any single
// session's churn). Best-effort; never blocks the hook.
const SPAWN_LOG_ROTATE_BYTES = 500 * 1024;
try {
if (existsSync(trackingLog)
&& statSync(trackingLog).size > SPAWN_LOG_ROTATE_BYTES) {
const ts = new Date().toISOString().replace(/[:.]/g, '-');
renameSync(trackingLog, `${trackingLog}.old.${ts}`);
}
} catch { /* swallow โ rotation is opportunistic */ }
bufferWrite(trackingLog, `${JSON.stringify(entry)}\n`); // existing line
| State | Trigger | Result |
|---|---|---|
| File < 500KB | SubagentStart fires | Append normally |
| File > 500KB | SubagentStart fires | Rename to .old.<ISO-ts>, then append to a fresh file |
| rename fails (FS error) | SubagentStart fires | Swallow error, append to existing file (no data loss) |
| File | Status | Purpose |
|---|---|---|
src/hooks/src/subagent-start/subagent-validator.ts | MOD | +13 LoC rotation block + import bump |
src/hooks/src/__tests__/subagent-start/subagent-validator.test.ts | MOD | +2 tests: rotates above threshold; no-op below |
docs/fix--spawn-log-rotation-1956/playground.html | NEW | this page |
rotates spawn log when it exceeds 500KB (issue #1956) โ mocks statSync to report 600KB, asserts renameSync was called with the .old.<ts> pattern.does not rotate when spawn log is under threshold โ mocks 100KB, asserts renameSync NOT called.