Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,14 @@ function summarySeqOfCompaction(events: readonly SessionEvent[], compactionId: s
return null
}

// Memoized on the append-only snapshot array (stable within one tool call, see
// sessionEventsOf): identity+length never goes stale; avoids O(B^2*N) rebuilds (#109).
const blockLedgerCache = new WeakMap<readonly SessionEvent[], { len: number; ledger: AcpBlockLedgerEntry[] }>()

/** Rebuild the block ledger from the durable log (no kernel state needed). */
export function rebuildBlockLedger(events: readonly SessionEvent[]): AcpBlockLedgerEntry[] {
const cached = blockLedgerCache.get(events)
if (cached !== undefined && cached.len === events.length) return cached.ledger
const ledger: AcpBlockLedgerEntry[] = []
for (const event of events) {
if (event.type !== 'compaction/summary') continue
Expand Down Expand Up @@ -512,6 +518,7 @@ export function rebuildBlockLedger(events: readonly SessionEvent[]): AcpBlockLed
createdAt: event.time,
})
}
blockLedgerCache.set(events, { len: events.length, ledger })
return ledger
}

Expand Down
21 changes: 21 additions & 0 deletions tests/region.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ test('M5: findOpenTurn / assertNoActiveCompaction track the durable lock', () =>
assertNoActiveCompaction(session.events)
})

test('M5: rebuildBlockLedger is idempotent across repeated calls on one snapshot (issue #109)', () => {
const session = buildTextSession(8)
const c1 = runCompactionTransaction(session, {
start: 1, end: 3, shadowedSeqs: [1, 2, 3],
summary: [{ type: 'text', text: 'first summary detail' }],
shadowedTokenCount: 100, provider: 'p', model: 'm',
})
const c2 = runCompactionTransaction(session, {
start: 4, end: 6, shadowedSeqs: [4, 5, 6],
summary: [{ type: 'text', text: 'second summary detail' }],
shadowedTokenCount: 200, provider: 'p', model: 'm',
})
const events = session.events
const first = rebuildBlockLedger(events)
const second = rebuildBlockLedger(events)
assert.equal(first.length, 2)
assert.deepEqual(second, first, 'repeated calls on the same snapshot return identical ledger')
assert.equal(first[0]!.blockId, c1.compactionId)
assert.equal(first[1]!.blockId, c2.compactionId)
})

test('M5: runCompactionTransaction lands the four events and shadows the range', () => {
const session = buildTextSession(6)
const { compactionId, seqs } = runCompactionTransaction(session, {
Expand Down