diff --git a/changelog.d/fix-autoplan-after-2-1-317-compaction.md b/changelog.d/fix-autoplan-after-2-1-317-compaction.md new file mode 100644 index 000000000..065b01049 --- /dev/null +++ b/changelog.d/fix-autoplan-after-2-1-317-compaction.md @@ -0,0 +1 @@ +- **/autoplan no longer gets stuck between phases after Claude Code 2.1.317 compacts a long session.** On 2.1.317 a compaction's boundary names a parent record that is never written to the session file, so the guard's journal reader dropped every record after the compaction and refused each later phase entry on every retry. The boundary now continues from the last record the compaction kept in place (`compactMetadata.preservedSegment.tailUuid`). Boundaries from 2.1.300 and earlier, a tail that is not flushed yet, and lists that name no record of this session read exactly as before. Reported and fixed by @vilano93 in #4200. diff --git a/docs/autoplan-guard-troubleshooting.md b/docs/autoplan-guard-troubleshooting.md index c30bfc455..22bb45be5 100644 --- a/docs/autoplan-guard-troubleshooting.md +++ b/docs/autoplan-guard-troubleshooting.md @@ -69,7 +69,7 @@ journal's directory is the same folder as the project, spelled differently. `unrecognized_shape:compaction_unreachable` means records after a Claude Code compaction hang from a compact boundary whose logical parent loops back instead of reaching the session root, in a layout gstack does not read yet -(Claude Code 2.1.300's preserved segment is read). Earlier gstack versions +(the preserved segments of Claude Code 2.1.300 and 2.1.317 are read). Earlier gstack versions dropped those records and denied the next phase with `close_incomplete`. Please report the detail and version. diff --git a/lib/claude-journal-records.ts b/lib/claude-journal-records.ts index beed342c3..5170ae015 100644 --- a/lib/claude-journal-records.ts +++ b/lib/claude-journal-records.ts @@ -236,7 +236,7 @@ export interface RecordMeta { subtype?: string; attachmentType?: string; version?: string; - /** A compact boundary's preserved message uuids, ending with its logical parent (Claude Code 2.1.300). */ + /** A compact boundary's preserved message uuids, ending with the preserved segment's tail. */ preserved?: readonly string[]; } @@ -248,16 +248,17 @@ const sessionStartRecord = (r: Record): boolean => r.type === 'atta const PRESERVED_MAX = 1024; /** * Claude Code 2.1.300 keeps a compaction's last records in place and lists them in - * compactMetadata.preservedMessages, ending with the segment's tail, which is also the - * boundary's logicalParentUuid. Only such a list of native uuids is read. + * compactMetadata.preservedMessages, ending with the segment's tail. On 2.1.300 the tail + * is the boundary's logicalParentUuid; on 2.1.317 the logical parent is a record the file + * never receives. Only a list of native uuids ending with preservedSegment.tailUuid is read. */ function preservedUuids(r: Record): readonly string[] | undefined { if (r.type !== 'system' || r.subtype !== 'compact_boundary' || r.parentUuid !== null || !nativeUuid(r.logicalParentUuid)) return; const meta = r.compactMetadata; if (!object(meta) || !object(meta.preservedSegment) || !object(meta.preservedMessages)) return; const uuids = meta.preservedMessages.uuids; - if (meta.preservedSegment.tailUuid !== r.logicalParentUuid || !Array.isArray(uuids) || - uuids.length > PRESERVED_MAX || uuids.at(-1) !== r.logicalParentUuid || !uuids.every(nativeUuid)) return; + if (!Array.isArray(uuids) || !uuids.length || uuids.length > PRESERVED_MAX || + uuids.at(-1) !== meta.preservedSegment.tailUuid || !uuids.every(nativeUuid)) return; return uuids; } @@ -317,6 +318,10 @@ export function ownedCausalOrder(metas: readonly RecordMeta[], cwd: string): Own // compact summary, so the boundary's logical parent descends from the boundary itself. // Such a boundary continues from the last preserved record that is a node of this file // and not its own descendant. With none, the cycle stays and supplies no ownership. + // Claude Code 2.1.317 names a logical parent the file never receives, and the list ends + // with the last record kept in place: that boundary continues the same way. A logical + // parent that is the list's own tail may just be unflushed and stays unresolved, and a + // boundary that opens the file stays a resumed fork's root. const descends = (id: string | undefined, from: RecordMeta): boolean => { for (const visited = new Set(); id;) { const m = byId.get(id); @@ -327,8 +332,11 @@ export function ownedCausalOrder(metas: readonly RecordMeta[], cwd: string): Own return false; }; const resumed = new Map(); - for (const m of nodes) if (m.preserved && descends(m.parent, m)) { - const anchor = m.preserved.slice(0, -1).findLast(id => byId.get(id) !== undefined && !descends(id, m)); + const fileHead = metas.find(m => m.uuid); + for (const m of nodes) { + if (!m.preserved || m.parent === undefined) continue; + const continues = m.preserved.at(-1) === m.parent ? descends(m.parent, m) : m !== fileHead && byUuid.get(m.parent) === undefined; + const anchor = continues ? m.preserved.findLast(id => byId.get(id) !== undefined && !descends(id, m)) : undefined; if (anchor) resumed.set(m, anchor); } const parentOf = (m: RecordMeta) => resumed.get(m) ?? m.parent; diff --git a/test/autoplan-compaction-preserved-segment.test.ts b/test/autoplan-compaction-preserved-segment.test.ts index e4578f884..f9991ff5e 100644 --- a/test/autoplan-compaction-preserved-segment.test.ts +++ b/test/autoplan-compaction-preserved-segment.test.ts @@ -20,6 +20,7 @@ import { randomUUID } from 'node:crypto'; import { guardFixture, section, codeOf, denial, type GuardFixture } from './helpers/autoplan-guard-fixture'; import { readOwnedClaudePublicTranscript } from '../lib/claude-public-transcript'; import { OWNED_READ_APPROVAL } from '../autoplan/bin/owned-read'; +import { ownedCausalOrder, recordMeta } from '../lib/claude-journal-records'; type Row = Record; const fixtures: GuardFixture[] = []; @@ -158,6 +159,106 @@ describe('Claude Code 2.1.300 compaction with a preserved segment', () => { }); }); +/** + * Claude Code 2.1.317 (#4200): the boundary's logicalParentUuid names a record the + * file never receives, and the preserved list ends with the segment's tail, the last + * record kept in place before the boundary. Followed literally, the boundary is an + * orphan, every later record was dropped, and each phase entry was refused on every + * retry. Such a boundary continues from that tail. + */ +const shape317 = (boundary: Row) => { + const { preservedSegment, preservedMessages } = boundary.compactMetadata; + boundary.version = '2.1.317'; + boundary.logicalParentUuid = randomUUID(); + preservedMessages.uuids.pop(); preservedMessages.allUuids.pop(); + preservedSegment.tailUuid = preservedMessages.uuids.at(-1); +}; +const transcriptOf = (f: GuardFixture) => readOwnedClaudePublicTranscript(f.transcript, f.cwd, f.sessionId).transcript; + +describe('Claude Code 2.1.317 compaction whose logical parent is never written', () => { + test('records written after the boundary stay owned', () => { + const f = fixture(); compact300(f, { shape: shape317 }); + expect(toolIds(f)).toEqual(['init', 'entry', 'close-head', 'close', expect.stringMatching(/^noop-/)]); + }); + + test('the next phase entry passes once the close packet is read in full after the compaction', async () => { + const f = fixture(); compact300(f, { shape: shape317 }); + expect(await f.hook(nextPhase(f))).toEqual(OWNED_READ_APPROVAL); + }); + + test('three such compactions in one session stay owned and the next phase entry passes', async () => { + const f = fixture(); + for (const splitAt of ['entry', 'close-head', 'close']) compact300(f, { splitAt, shape: shape317 }); + expect(toolIds(f)).toEqual(['init', 'entry', 'close-head', 'close', expect.stringMatching(/^noop-/)]); + expect(await f.hook(nextPhase(f))).toEqual(OWNED_READ_APPROVAL); + }); + + // Holds: each shape below is refused or reported today and must stay so. + test('a 2.1.300 tail that is the logical parent and not flushed yet still supplies no ownership', async () => { + const f = fixture(); + const { tail } = compact300(f); + write(f, rowsOf(f).filter(r => r.uuid !== tail)); + expect(toolIds(f)).not.toContain('close'); + expect(codeOf(await f.hook(nextPhase(f)))).toBe('close_incomplete'); + }); + + test('a preserved list whose last entry is not the segment tail does not continue the boundary', async () => { + const f = fixture(); compact300(f, { shape: b => { shape317(b); b.compactMetadata.preservedSegment.tailUuid = randomUUID(); } }); + expect(toolIds(f)).not.toContain('close'); + expect(codeOf(await f.hook(nextPhase(f)))).toBe('close_incomplete'); + }); + + test('a preserved list naming no record of this file does not continue the boundary', async () => { + const f = fixture(); compact300(f, { shape: b => { shape317(b); + const ids = [randomUUID(), randomUUID()]; b.compactMetadata.preservedMessages.uuids = ids; b.compactMetadata.preservedSegment.tailUuid = ids[1]; } }); + expect(toolIds(f)).not.toContain('close'); + expect(codeOf(await f.hook(nextPhase(f)))).toBe('close_incomplete'); + }); + + test('a preserved tail that is a subagent record does not continue the boundary', async () => { + const f = fixture(); + const agentRecord = randomUUID(); + compact300(f, { shape: b => { shape317(b); + b.compactMetadata.preservedMessages.uuids = [agentRecord]; b.compactMetadata.preservedSegment.tailUuid = agentRecord; } }); + const rows = rowsOf(f), at = rows.findIndex(r => r.subtype === 'compact_boundary'), last = rows[at - 1]!; + rows.splice(at, 0, { ...last, uuid: agentRecord, parentUuid: last.uuid, isSidechain: true, agentId: 'a1', + type: 'attachment', attachment: { type: 'reminder' }, message: undefined }); + write(f, rows); + expect(toolIds(f)).not.toContain('close'); + expect(codeOf(await f.hook(nextPhase(f)))).toBe('close_incomplete'); + }); + + test('a boundary with an absent logical parent and no compactMetadata still drops what follows', async () => { + const f = fixture(); compact300(f, { shape: b => { b.logicalParentUuid = randomUUID(); delete b.compactMetadata; } }); + expect(toolIds(f)).not.toContain('close'); + expect(codeOf(await f.hook(nextPhase(f)))).toBe('close_incomplete'); + }); + + test('a 2.1.300 boundary that cycles back but whose list omits its logical parent is still reported as an unrecognized shape', async () => { + const f = fixture(); compact300(f, { shape: b => { const { preservedSegment, preservedMessages } = b.compactMetadata; + preservedMessages.uuids.pop(); preservedSegment.tailUuid = preservedMessages.uuids.at(-1); } }); + expect(toolIds(f)).not.toContain('close'); + expect(transcriptOf(f)).toMatchObject({ status: 'missing', reason: 'unrecognized_shape:compaction_unreachable' }); + expectUnrecognizedCompaction(f, await f.hook(nextPhase(f))); + }); + + test('a resumed fork that opens with such a boundary stays a genesis root', () => { + const kept = randomUUID(), boundary = randomUUID(); + const row = (uuid: string, parentUuid: string | null, extra: Row) => + ({ uuid, parentUuid, isSidechain: false, cwd: '/w', timestamp: '2026-11-09T00:00:00.000Z', ...extra }); + const shaped = [ + row(boundary, null, { type: 'system', subtype: 'compact_boundary', logicalParentUuid: randomUUID(), + compactMetadata: { preservedSegment: { tailUuid: kept }, preservedMessages: { uuids: [kept] } } }), + row(kept, randomUUID(), { type: 'attachment', attachment: { type: 'reminder' } }), + row(randomUUID(), boundary, { type: 'user', message: { role: 'user', content: 'continued' } }), + ]; + const orderOf = (rows: Row[]) => ownedCausalOrder(rows.map((r, i) => recordMeta(r, i)), '/w'); + const control = orderOf(shaped.map(r => r.uuid === boundary ? { ...r, compactMetadata: undefined } : r)); + expect(control).toEqual({ order: [0, 2], root: boundary }); + expect(orderOf(shaped)).toEqual(control); + }); +}); + /** The guard allows the call with a visible warning and logs the unverified decision; it never denies with close_incomplete. */ function expectUnrecognizedCompaction(f: GuardFixture, output: any) { expect(denial(output)).toBeUndefined();