perf(vfs): repairContainment's reconcile is one paged edge walk, not one graph call per file
Some checks failed
CI / Node 22 (push) Successful in 12m19s
CI / Node 24 (push) Successful in 12m24s
CI / Integration + conformance (Node 22) (push) Failing after 16m58s
CI / Bun (latest) (push) Successful in 12m24s

Pass 2 issued one awaited related({ to }) per VFS entity — O(entities)
serialized graph calls, measured in whole minutes on large brains. Now a
single paged walk over every Contains edge (type-only, 1,000 per page)
feeds an in-memory group-by-target, and only actual defects mutate. The
verdicts are unchanged: a stale parent's edge is removed, a missing edge
is restored, duplicates cannot survive, and user knowledge edges are
never touched.

Pinned in tests/integration/vfs-containment-batched.test.ts: exact
removed/restored counts on a seeded defect tree, tree correctness after
the repair, user edges untouched, and the cost shape — related() call
count independent of the entity count.
This commit is contained in:
David Snelling 2026-09-01 12:48:38 -07:00
parent 5e3b343a0e
commit 3e60aded36
2 changed files with 141 additions and 1 deletions

View file

@ -2295,6 +2295,31 @@ export class VirtualFileSystem implements IVirtualFileSystem {
cursor = page.nextCursor
}
// Pass 2: ONE paged walk over every Contains edge, grouped by target in
// memory. The earlier shape issued one awaited related({ to }) per VFS
// entity — O(entities) serialized graph calls, measured in whole minutes
// on large brains. This shape is O(edges / page) calls regardless of how
// many entities exist; mutations alone stay per-defect.
const incomingByTarget = new Map<string, Relation<any>[]>()
{
const pageSize = 1000
let pageOffset = 0
for (;;) {
const page = await this.brain.related({
type: VerbType.Contains,
limit: pageSize,
offset: pageOffset
})
for (const edge of page) {
const bucket = incomingByTarget.get(edge.to)
if (bucket) bucket.push(edge)
else incomingByTarget.set(edge.to, [edge])
}
if (page.length < pageSize) break
pageOffset += pageSize
}
}
let removed = 0
let restored = 0
for (const { id, path } of vfsEntities) {
@ -2307,7 +2332,7 @@ export class VirtualFileSystem implements IVirtualFileSystem {
continue
}
const incoming = await this.brain.related({ to: id, type: VerbType.Contains })
const incoming = incomingByTarget.get(id) ?? []
let expectedSeen = false
for (const edge of incoming) {
const isVfsEdge = edge.subtype === 'vfs-contains' || (edge.metadata as any)?.isVFS === true