mirror of
https://github.com/garrytan/gstack.git
synced 2026-09-13 00:19:03 +02:00
fix(hooks): timeline Stop hook reads a 256KB tail instead of the whole file
The Stop hook runs on EVERY Claude Code turn machine-wide and re-read +
JSON-parsed the entire timeline each time, scaling to the 10MB size cap
(~100-300ms per turn of pure overhead). It now reads only the last 256KB
via fstat + positioned read, discarding the first partial line when the
window starts mid-file.
Semantics: a dangling "started" older than the last 256KB of appends
belongs to a session long gone — beyond repair interest. The window can
never fabricate a dangling entry ("completed" is always appended AFTER its
"started", so any started inside the window has its completion inside the
window too), so idempotency holds. The fail-open contract is unchanged:
exit 0 always, size cap kept, deadline re-checked before the write.
New test: a >256KB timeline where a recent dangling entry still gets
repaired while an old out-of-window dangler is left alone; all existing
fail-open cases pass unchanged.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
aa6c73821f
commit
45b72989e6
@@ -158,6 +158,37 @@ describe('timeline-stop-hook (#2553, F5 fail-open)', () => {
|
||||
expect(runHook('').exitCode).toBe(0);
|
||||
});
|
||||
|
||||
test('tail window (P3): a recent dangling entry in a >256KB timeline is still repaired', () => {
|
||||
const lines: string[] = [];
|
||||
// An old dangling entry that falls OUTSIDE the 256KB tail window —
|
||||
// beyond repair interest by design (its session is long gone).
|
||||
lines.push(JSON.stringify({ skill: 'review', event: 'started', session: 'old-1' }));
|
||||
// >512KB of closed pairs pushes the old entry well past the window while
|
||||
// proving windowed parsing still walks real entries.
|
||||
let n = 0;
|
||||
while (lines.length * 100 < 512 * 1024) {
|
||||
lines.push(
|
||||
JSON.stringify({ skill: 'qa', event: 'started', session: `pad-${n}`, pad: '#'.repeat(40) }),
|
||||
);
|
||||
lines.push(JSON.stringify({ skill: 'qa', event: 'completed', session: `pad-${n}`, outcome: 'success' }));
|
||||
n++;
|
||||
}
|
||||
lines.push(JSON.stringify({ skill: 'ship', event: 'started', session: 'recent-9' }));
|
||||
fs.writeFileSync(timelinePath, lines.join('\n') + '\n');
|
||||
expect(fs.statSync(timelinePath).size).toBeGreaterThan(256 * 1024);
|
||||
|
||||
const r = runHook(stopPayload());
|
||||
expect(r.exitCode).toBe(0);
|
||||
const repairs = timelineEntries().filter((e) => e.source === 'stop-hook');
|
||||
expect(repairs).toHaveLength(1);
|
||||
expect(repairs[0]).toMatchObject({
|
||||
skill: 'ship',
|
||||
event: 'completed',
|
||||
outcome: 'unknown',
|
||||
session: 'recent-9',
|
||||
});
|
||||
});
|
||||
|
||||
test('oversized timeline is skipped, untouched, and still exits 0 (fail-open size cap)', () => {
|
||||
const line = JSON.stringify({ skill: 'qa', event: 'started', session: '66-6' }) + '\n';
|
||||
const filler = '#'.repeat(1024 * 1024);
|
||||
|
||||
Reference in New Issue
Block a user