UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

227 lines • 9.25 kB
import { packPages } from "./compact-index-pages.js"; import { CompactReadError } from "./compact-read-lease.js"; /** One bounded selection and one optional small page, with a single atomic representation swap. */ export class CompactIndexCompactor { constructor(index, threshold) { this.running = false; this.index = index; this.threshold = threshold; } schedule() { const index = this.index; if (this.running || !index.store.spillAllowed || ((index.overrides?.size ?? 0) - index.pending.size < this.threshold && !index.status().deletionMasks)) return; this.running = true; index.store.enqueue("page_write", async () => { let progress = false; try { progress = await index.bases.cached(() => this.compact()); } finally { this.running = false; } if (progress) this.schedule(); }); } /** Key-ordered ready keys preserve page locality without looking up the pending backlog (senpi#4575). */ *readyEntries(cut) { for (const key of [...this.index.ready].sort()) { if (!this.index.ready.has(key)) continue; const value = this.index.overlays.get(cut.overrides, key); if (value) yield [key, value]; } } async compact() { const i = this.index; if (!i.store.spillAllowed) return false; if (i.pending.size) await i.store.flushValues(); if (!i.store.spillAllowed) return false; const cut = i.freeze(); const selected = new Map(); let touched; const rows = new Map(); const removed = new Set(); const merged = []; const written = []; const loaded = []; const unowned = new Set(); let draft = i.base; i.bases.adjust(draft, 1, 0); try { const entries = this.readyEntries(cut); let exhausted = false; while (selected.size < 128 && !exhausted) { const batch = []; while (batch.length < 128) { const next = entries.next(); if (next.done) { exhausted = true; break; } batch.push(next.value); } if (!batch.length) break; const locations = await i.bases.getMany(cut.base, batch.map(([key]) => key)); for (const [key, value] of batch) { const page = locations.get(key)?.page; if (!page || !touched || page.path === touched.path) { selected.set(key, value); touched ??= page; } if (selected.size === 128) break; } } let small; for (const page of i.pages) { if (page.count < 64 && page.bytes < 32_768) { small = page; break; } } for (const page of small?.path === touched?.path ? [small] : [small, touched]) { if (!page) continue; const data = await i.readPage(page); loaded.push(data); const keyed = []; for (const row of data.rows) { const key = await i.values.read(row.key); if (typeof key !== "string") throw new CompactReadError("ownership"); keyed.push([key, row]); } const locations = await i.bases.getMany(cut.base, keyed.map(([key]) => key)); for (const [key, row] of keyed) { if (locations.get(key)?.page.path === page.path) { rows.set(key, row); removed.add(key); } } } for (const [key, change] of selected) { removed.add(key); if (change.deleted) { rows.delete(key); continue; } let row = change.row ?? rows.get(key); if (row && change.patch.size) { const value = await i.values.merge(row.value, change.patch); merged.push(value.owner); row = { ...row, value }; } if (row) rows.set(key, row); } const ordered = [...rows]; let offset = 0; for (const chunk of packPages(ordered.map(([, row]) => row))) { const data = Buffer.from(JSON.stringify(chunk)); i.peakResidentPages = Math.max(i.peakResidentPages, i.residentPages + 1); const path = await i.store.writeFile(data, "page_write", data.byteLength); if (path === undefined) return false; written.push({ page: { path, bytes: data.byteLength, count: chunk.length, owner: 0, }, keys: ordered.slice(offset, offset + chunk.length).map(([key]) => key), }); unowned.add(written[written.length - 1].page); offset += chunk.length; } if (!i.store.spillAllowed) return false; const changes = new Map(); const owners = []; try { for (const candidate of written) { const kept = []; const refs = []; for (const [offset, key] of candidate.keys.entries()) { const row = rows.get(key); if (!row) throw new CompactReadError("ownership"); // Even a concurrently removed row's on-disk key must survive page traversal. refs.push(row.key.owner); const folded = selected.get(key); const current = i.overlays.get(i.overrides, key); if (folded && current !== folded && (!current || current.deleted || current.row !== folded.row)) continue; refs.push(row.value.owner, row.payload); kept.push([key, offset]); } const file = i.store.ownFile(candidate.page.path, candidate.page.bytes); const page = { ...candidate.page, owner: i.store.text.own([...refs, file]) }; owners.push(page.owner); i.store.adjust(file, -1, 0); unowned.delete(candidate.page); for (const [key, offset] of kept) { changes.set(key, { page, offset }); removed.delete(key); } if (kept.length) candidate.page = page; } for (const key of removed) changes.set(key, undefined); // One sorted pass builds each changed node once; the pages' owners outlive it. const next = await i.bases.apply(draft, changes); i.bases.adjust(draft, -1, 0); draft = next; } finally { for (const owner of owners) i.store.adjust(owner, -1, 0); } await i.bases.flush(); if (!i.store.spillAllowed) return false; const previous = i.base; i.base = draft; draft = undefined; i.bases.adjust(previous, -1, 0); for (const page of i.pages) if (page.path === small?.path || page.path === touched?.path) i.pages.delete(page); for (const { page } of written) if (page.owner) i.pages.add(page); for (const [key, value] of selected) { const current = i.overlays.get(i.overrides, key); if (current === value) i.drop(key); } return selected.size > 0; } catch (error) { if (error instanceof CompactReadError && error.reason === "retired") return false; throw error; } finally { i.bases.adjust(draft, -1, 0); for (const page of loaded) page[Symbol.dispose](); for (const page of unowned) i.store.discard(page.path, page.bytes); for (const owner of merged) i.store.adjust(owner, -1, 0); i.release(cut); } } } //# sourceMappingURL=compact-index-compactor.js.map