โ™ป Spawn-log rotation โ€” fix #1956

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.

๐Ÿฉบ Before

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.

๐Ÿ”ง The change

// 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

๐Ÿ” Lifecycle

StateTriggerResult
File < 500KBSubagentStart firesAppend normally
File > 500KBSubagentStart firesRename to .old.<ISO-ts>, then append to a fresh file
rename fails (FS error)SubagentStart firesSwallow error, append to existing file (no data loss)

๐Ÿ“ฆ PR shape

FileStatusPurpose
src/hooks/src/subagent-start/subagent-validator.tsMOD+13 LoC rotation block + import bump
src/hooks/src/__tests__/subagent-start/subagent-validator.test.tsMOD+2 tests: rotates above threshold; no-op below
docs/fix--spawn-log-rotation-1956/playground.htmlNEWthis page

๐Ÿ›ก Safety properties

๐Ÿงช Tests