mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-08-28 05:25:25 +00:00
fix(group): stop reporting what could not be measured as a measurement of zero (#3012)
* fix: surface unreadable group indexes and escape raw NUL bytes in source Two independent diagnostics failures, both of which turn a real error into a confident, benign-looking answer. **Unreadable member repos (#3011).** `syncGroup` wrapped `initLbug` plus all contract extraction for each member in a bare `catch {}` that pushed the repo onto `missingRepos` and discarded the error. A LadybugDB storage-version mismatch therefore surfaced as "repo not found", `group sync` printed `0 contracts, 0 cross-links` and exited 0, and the existing contracts.json was overwritten with an empty registry. The two states need different answers from the operator — a missing repo must be indexed, an unreadable one is usually version skew or a lock — so they are now separate: - the caught error is logged with the repo, group path and lbug path - `unreadableRepos` is tracked alongside `missingRepos` on `SyncResult`, persisted (optionally, so older registries still parse) on `ContractRegistry`, and threaded through `GroupService` sync/status - `group sync` reports both before the cascade counts, since an unread repo is the likely explanation for a small or empty count - `group status` reports unreadable repos separately; calling them "missing" actively misdescribed them - when EVERY configured repo fails to open, the write is skipped: an extraction that read nothing is not evidence the group has no contracts, and replacing a good registry with an empty one loses data while reporting success **Raw NUL bytes (#3010).** `sync.ts` and `free-call-fallback.ts` each used a NUL as a join delimiter, written as a literal 0x00 instead of `\0`. Identical at runtime, but it makes the file test as binary: `file(1)` reports `data`, ugrep returns empty with exit 1 — indistinguishable from "no match", with no message — and BSD grep replaces matching lines with "Binary file ... matches". A search that should hit comes back as a confident "not present". Both now use the escape, and a unit test fails on any raw control byte in src/ so it cannot silently return. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(hygiene): guard every tracked source file against a raw NUL, not just src/ The guard added with the NUL escapes only scanned gitnexus/src for .ts/.tsx. Neither prior recurrence of this defect in this repo was in that scope:b620773b1was gitnexus/bench/cpp-qualified-ns/measure.mjs and38d737bb5was a fixture under gitnexus/test. A guard that cannot see where the bug has actually landed twice is not a guard. Drive the file list from `git ls-files` at the repository root over .ts/.tsx/.js/.jsx/.mjs/.cjs/.mts/.cts — 2483 files instead of 828 — and split the byte class, which is the part that matters: - 0x00 is a hard failure repo-wide. It is the byte git's binary heuristic keys on, so it is the one that costs a file its diff (and, on the base side of a PR, its inline-comment anchors and its three-way merge). - The wider C0 class stays scoped to gitnexus/src. A repo-wide scan finds exactly one hit, test/unit/logger.test.ts:146, and that 0x1b is a legitimate ANSI-escape fixture that is the subject of the test. Widening this half would go red on day one. Read Buffers and scan bytes instead of decoding each file to latin1, through a bounded read pool: 1.5 s for 2483 files, against 8-21 s previously for 828. Add a negative fixture — a planted 0x00 and 0x1b run through the same scanning helper — so a future refactor of the collector cannot leave a permanently green guard, plus an assertion that the collected set still reaches bench/, test/ and .mjs, which goes red if the scope is ever narrowed back. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): report a cross-repo impact built from an incomplete bridge as truncated When a sync cannot read a member repo, that repo's contracts and every cross-link touching them are simply absent from bridge.lbug. Nothing in the impact walk could notice: the only incompleteness channel on a GroupImpactResult is truncationFields(), which is driven by fan-out state (truncatedRepos / localPartial / fanoutTimedOut), and a repo missing from the bridge sets none of them. So `group impact` on a symbol whose one downstream consumer lives in an unreadable repo returned `{ cross: [], truncated: false }` — "complete: nothing in another repo depends on this". That is a wrong answer, not an empty one, for a tool an agent uses to license a delete or a rename. BridgeMeta now records unreadableRepos alongside missingRepos, writeBridge persists it when non-empty, and runGroupImpact folds a non-empty unreadableRepos ∪ missingRepos into truncated / riskEpistemic: 'lower-bound', naming the repos in truncatedRepos. The reason is a new 'incomplete-sync' rather than the existing 'partial' because the remedy differs: 'timeout' and 'partial' are runtime limits the same query can clear on a retry, while this one clears only when `gitnexus group sync` succeeds. Runtime limits still take precedence when both apply, since those are what the caller can act on immediately. The risk VALUE is never clamped down — mergeRisk is monotone in the traversed crossing count, so an incomplete bridge can only under-report. Marking the floor is what makes that legible. Both shape changes are additive and optional, so a bridge written before this still reads. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): say truthfully what a sync did to contracts.json Review follow-ups to the unreadable-repo diagnostics. Every item below is a place where the code still answered a question it could not answer. 1. The CLI announced a write it did not perform. `group sync` printed "Wrote contracts.json (0 contracts, 0 cross-links)" unconditionally, including on the path that deliberately left the file alone. SyncResult now carries registryOutcome ('written' | 'preserved' | 'not-attempted'), the CLI prints from it, and group_sync returns it so an agent that calls group_sync then group_contracts can tell why the counts disagree. 2. Refusing to write anything on total failure threw away the diagnostic describing the run that just happened. `group status` reads contracts.json from disk, so the operator who saw the sync fail and ran status to find out why read the PREVIOUS sync's file: no unreadable list, an old lastSync, a healthy-looking group — or worse, the previous run's unreadable list presented as this one's. The skip is now targeted: contracts, crossLinks, repoSnapshots and generatedAt carry forward verbatim, only missingRepos and unreadableRepos are refreshed. generatedAt stays put because it dates the contracts, which are still the previous run's. With no prior file, or an unparseable one, nothing is written at all. 3. Per-repo extraction is now all-or-nothing. Extractors run in sequence and any one can throw; appending each one's results straight to autoContracts meant a repo whose HTTP extractor succeeded and whose gRPC extractor then failed contributed a partial set to the registry, while the same run told the operator that repo's "contracts are omitted from this sync". 4. readRegistry gains an opt-in strict mode, and syncGroup uses it. The lenient `catch { return []; }` converted "I could not read the registry" into "no repo is registered": every configured repo then resolved to MISSING, the total-failure guard stayed off (it needs a load error), and a good contracts.json was replaced by an empty one at exit 0. That is an unreadable condition reported as missing, one frame above the code this branch fixes. The default stays lenient for the other nine callers; ENOENT stays lenient in both modes. 5. Absence of unreadableRepos keeps meaning "not recorded". The loader spreads the key in only when present instead of defaulting to [], and getStatus passes undefined through, so a legacy registry no longer reads as "the last sync found none unreadable". getStatus also gates both list fields on Array.isArray: it reads through readContractRegistry, which is a bare JSON.parse cast, so a corrupt string in either slot used to reach cli/group.ts and die in .join(', ') — the command whose job is explaining an unreadable thing, crashing on one. 6. Smaller, same theme: the per-repo warning passes the Error itself rather than err.message, so pino keeps the stack; the total-failure warning no longer fires on a dry run, where it described a file the call was never going to touch and which need not exist; the status table's MISSING legend stops re-conflating the two states; the sync warning drops its GITNEXUS_LOG_LEVEL=warn hint, which would only have suppressed output (pino emits warn at the default info level, so the reason was already printed); and the group_sync tool description and its idempotency comment now describe what the tool actually does. Testing. The original four cases could not see the change they were named after. Mutation testing showed two survivors: dropping the === configuredRepoCount conjunct, which turns "every repo failed" into "any repo failed" and would silently freeze contracts.json for a group where one of five repos is skewed; and deleting both logger.warn calls, the stated purpose of the change. Both survived because every case configured exactly one repo and nothing read the log. There is now a two-repo case running the real per-repo loop, an all-missing case, a _captureLogger assertion on the level 40 record, partial-extraction cases, and strict-read cases. All five mutants are killed, each by exactly one test. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): tighten the registry list gates and stop naming a truncation reason on complete results Three follow-ups from the check bot's pass over the previous commits. 1. `detect.includes` was missing from both group-sync test fixtures, so they did not satisfy the `GroupConfig` they claim to construct. It went unnoticed because `tsconfig.json` is src-only; `tsconfig.test.json` reports it. The older of the two fixtures carried the gap in from the original commit. 2. `runGroupImpact` named its truncation reason in a variable computed before the truncated check, so on a fully complete result the variable read 'incomplete-sync'. `truncationFields` discards the reason when `truncated` is false, so nothing surfaced — but a value that is wrong whenever it is unused is a trap for the next reader. Computed inline at the one call site that can consult it, which is also how the neighbouring call sites are written. 3. `Array.isArray` alone let a corrupt registry through. `['app/backend']` and `[{repo:'x'}]` are both arrays, and only the second reaches `cli/group.ts`'s `.join(', ')` — as `[object Object]`, a measurement the operator can read but cannot act on. Both readers now go through one `recordedRepoList` helper that requires an array of strings; anything else is "not recorded", the same as absent. Two more rows in the corrupt-value table cover it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): keep readRegistry's signature, and stop describing unreadableRepos as index-only Two items from the check bot's blocking pass. 1. `readRegistry` gained an optional `opts` parameter last commit. That is source-compatible — every zero-argument call still compiles and behaves identically — but the contract check treats any parameter-list change on a symbol with outside callers as a break, and it is right that the safest version of this change touches that signature not at all. The strict read is now its own export, `readRegistryStrict()`, over a shared private body. `readRegistry()` is byte-identical to what it was; `syncGroup` is the only caller of the strict one, and the mode is legible at the call site instead of hiding in an options bag. 2. `unreadableRepos` is described everywhere as "the index could not be opened". That was accurate before this branch and is not now: making per-repo extraction all-or-nothing means a repo also lands there when an extractor throws partway with the index open fine. The two belong in one bucket because the consequence is one thing — none of that repo's contracts are in this sync — but the docs have to say so, or an operator reads `unreadableRepos` as a storage diagnosis and goes looking at LadybugDB for an extractor bug. Corrected on `ContractRegistry`, `BridgeMeta`, `SyncResult`, the `group_sync` tool description, and the `group sync` console output, which now says "Could not extract contracts from" rather than "Could not read the index for". Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(cli): stop calling an unreadable registry an old one in group status `getStatus` reports `unreadableRepos` as `undefined` for two different reasons: the field is genuinely absent, or it held something that was not a list of repo paths and the shape gate declined to guess. The status line named only the first — "registry predates this field" — so a corrupt value read as a merely old registry. That is the same shape of wrong answer this command exists to stop giving: a condition we could not read, presented as a benign one we understand. The line now names both, and asks for a sync either way, which is the fix in both cases. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): close the three fail-open paths left on the safety boundaries Follow-ups from the re-review of31c2b6e81. All three of its blocking findings reproduce; each is a place where unknown state still resolved to a confident benign answer, which is the one thing this branch exists to stop. 1. Strict registry reading accepted malformed rows. `[{}]` is a JSON array, so it passed the shape check: every configured repo then failed to resolve into `missingRepos`, none produced a load ERROR, the total-failure guard stayed off, and a good contracts.json was replaced with an empty one at exit 0 — the same fail-open the strict mode was added to close, one level down from the file to the rows inside it. Strict mode now requires `name`, `path` and `storagePath` on every row and rejects the WHOLE registry if any row fails. Rejecting rather than filtering is the point: dropping bad rows would report the repos they name as unregistered, which is the same wrong answer again. `indexedAt` / `lastCommit` are deliberately not required — callers already default them, so demanding them would trade a fail-open for a fail-shut on a legitimate legacy registry. 2. A failed bridge publication could make impact look complete. `writeBridge` swaps `bridge.lbug` and writes `meta.json` as two operations, and this branch made that meta load-bearing: `runGroupImpact` derives its truncation fields from it. A sync interrupted between the two steps therefore left a NEW bridge beside the PREVIOUS sync's metadata, and an impact query read that as "complete". Fixed from both ends. The write path removes the old meta before the swap, so the window leaves metadata ABSENT rather than stale. The read path treats absent-or-unparseable meta (`version: 0`) as unknown provenance and reports a floor, which also covers the caught `writeBridge` failure in `syncGroup`. Over-reporting truncation on a bridge that is actually fine is the safe direction, and the next successful sync clears it. 3. `preserved` was returned when there was nothing to preserve. On a group's first all-unreadable sync the outcome was set before the prior registry was read, so the CLI told an operator "the contracts from the previous sync are preserved" about a file that had never existed. Split out as `no-prior-registry`, with its own console message. Also widened the NUL guard to the source languages it claimed to cover. The commit that added it said "every tracked source file" while the collector stopped at the JS/TS family, so a raw NUL in tracked Python, Java, Go, Rust, C/C++, Ruby, PHP, Kotlin, Swift, C# or shell would still have turned those files binary unnoticed. Measured before widening: 2315 non-JS tracked source files, zero hits, so this was an unforced gap rather than a tradeoff. A planted `.py` fixture and a collector-coverage assertion keep it honest. Every fix is mutation-verified: reverting each one individually turns its own tests red (3, 2, 2, 1 and 1 failures respectively), and all pass together. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): record the empty unreadable measurement instead of dropping it Both writers omitted `unreadableRepos` when it was empty, which made the tri-state this branch introduced unreachable in its most common case. `ContractRegistry.unreadableRepos` is optional on the TYPE so a registry written before the field existed still parses, and absence there means "not recorded". But a sync that read every repo successfully HAS measured it, and `[]` is that measurement. Dropping it collapsed "measured, none" into "never recorded", so after every clean sync `gitnexus group status` printed Last sync unreadable repos: not recorded (the registry predates this field, or its value could not be read) Re-run `gitnexus group sync` to record it. about the sync that had just succeeded. The distinction is only worth having if the writer commits to it, so both `contracts.json` and the bridge's `meta.json` now record the field whenever the sync supplied it, `[]` included. The check bot found this on the bridge writer and attributed the consequence to `group status`. The consequence is real but it is not the bridge's: `getStatus` reads `contracts.json` and never touches `BridgeMeta`, whose only consumer is `runGroupImpact` — where absent and empty are already equivalent. So the user-visible half was in the registry writer, one file over from where it was reported, and both are fixed. Also fills in `DetectConfig.includes` (and `workspace_deps`) across the group test fixtures that predate those fields. These are pre-existing on main and are a no-op at runtime — `undefined` and `false` are both falsy at the gate — but they are the same defect the bot flagged as an error in the new fixtures, and `tsconfig.test.json` reported eleven of them. That file is not in CI, which is why they survived; the group tree is now clean of them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * test(group): stop two bridge-metadata tests claiming coverage they do not have Both were named for the swap window and neither injects a swap failure. "drops the previous meta.json before swapping the database file" runs two successful writeBridge calls. Its assertions hold with the removal in either position, because writeBridge overwrites meta.json at the end regardless — so it cannot pin the ordering it is named for. Renamed to what it does cover, the successful-rebuild replacement, with the limit stated in the body rather than left for the next reader to discover. "leaves NO meta.json when the swap fails partway" removes the file by hand after a successful write, so it exercises readBridgeMeta's missing-file contract, not writeBridge. That contract is worth pinning on its own — version 0 is the signal runGroupImpact fails closed on — so the test stays, under a name that says so. The ordering itself is pinned in bridge-meta-swap-window.test.ts, which mocks retryRename to throw on the bridge.lbug swap and asserts the previous sync's metadata cannot survive it. Both renamed tests now point there, so the coverage is findable from the place someone would look for it. No production code changes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * fix(group): pair bridge metadata to its database instead of deleting it The previous commit closed the swap/metadata window by removing meta.json before the database swap, so the window would fail to "absent" rather than "stale". That was the wrong trade, and it destroyed recoverable state. The old database's move to `.bak` sits inside a catch that swallows failures, not just "no existing db". When that rename fails — a held read-only handle does this on Windows, and a long-lived MCP server holds one — the failure is swallowed, the following `tmp -> bridge.lbug` throws, and writeBridge exits with the OLD database still in place and perfectly valid. Its metadata was already deleted. Cross-repo impact then answers "we cannot say" for that group until some future sync succeeds, and if the cause is a held handle or permissions there is no such sync. A working feature, destroyed permanently to close a narrow window. Deleting also only chose which way the window failed; it never closed it. So destroy nothing, and make the pair self-describing instead: writeBridge stamps the database's size and mtime into the metadata it writes, and `bridgeMetaMatchesFile` lets a reader ask whether the two still belong together. `runGroupImpact` treats a mismatch the same as absent metadata — provenance unknown, report a floor. A metadata file left over from an earlier sync cannot match a freshly renamed database, and a sync that fails before the swap leaves a matching pair untouched. Metadata written before the stamp existed is unverifiable rather than stale, and is accepted: failing those closed would mark every pre-existing bridge incomplete, trading a narrow window for a repo-wide regression. The swap-window test now distinguishes the two failure shapes, because they want different answers. When every rename fails the old database never moves, so the surviving metadata still matches it and impact keeps answering from it. When only the final rename fails the old database has already reached `.bak` and no database is in place, so the metadata correctly matches nothing — and `ensureBridgeReady` fails loudly on the absent file, which beats a silent floor. Mutation-verified: reinstating the delete, neutering the pairing check, and dropping the stamp each turn 2, 3 and 3 tests red respectively. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014g48u4WcRZy543Wqp5NhpV * chore: keep TypeScript diffs readable after a NUL leaves the tree Git decides a pair is binary when EITHER blob carries a NUL, and it only sniffs the first 8000 bytes. `gitnexus/src/core/group/sync.ts` carried one at byte 5132 on main. This branch removes it, but the base side still has it, so the file renders as "Binary files differ" in the pull request: no hunks, no inline comments, and no three-way merge — however clean the head side is. A head-side byte guard cannot detect that, by construction, since it only ever sees the working tree. Setting the `diff` attribute stops the heuristic from hiding the change. It does not mark the files binary, does not imply `text`, and does not change how blobs are stored, normalized, or checked out — the root `* text=auto eol=lf` still governs all of that. It affects diff generation and rendering only. Locally this turns the branch's own sync.ts diff from `Bin 17612 -> 25346 bytes` into 154 insertions and 16 deletions. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): answer "provenance unknown" for malformed bridge metadata `readBridgeMeta` guarded the read and the parse but not the SHAPE of what it parsed, then cast the result. `runGroupImpact` spread both repo lists straight into a Set, so a `meta.json` whose `missingRepos` held an object threw a TypeError out of the entire cross-repo query — and threw it from a point after `ensureBridgeReady` had taken the bridge lease and before the `try` whose `finally` releases it, so every such query also leaked a refcount the cached handle could never get back. A malformed file is a reason to answer "we cannot say", never a reason to crash the question. The shape gate now lives where the metadata is read, mirroring the one `service.ts` already applies to the registry's copies of these same two lists. Each list is judged independently: a garbage `unreadableRepos` no longer discards a `missingRepos` that was genuinely measured. A list that was present but unusable is dropped rather than normalized to `[]`, because an unreadable value is not a measurement of zero — the new reader-side `repoListsUnreadable` carries that distinction, and `runGroupImpact` folds it into the same provenance-unknown verdict it already reaches for `version: 0` and for metadata that does not pair with the database beside it. A root that is not an object is closed too. `JSON.parse` succeeds on `null`, `7` and `[]`; the first threw on `.version`, and the other two read `undefined` and sailed through the version gate as if the bridge had been vouched for. Both provenance values moved inside the protected region and are initialized fail-closed, so a future throw between the lease and the walk releases rather than wedges. `repoListsUnreadable` is reader-side only: the sole `writeBridgeMeta` call site builds a fresh literal, so nothing persists it and no schema version moves. Mutation-verified: reverting the shape gate alone turns 4 tests red — the three malformed-list scenarios plus the handle-release regression. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(storage): reject registry rows that cannot identify a repo The strict read's row gate gave `typeof v === 'string'`, and `typeof '' === 'string'`. A row whose `name` was blank therefore passed as resolvable, then matched nothing in `defaultResolveHandle` — putting every configured repo in `missingRepos` and presenting an unusable registry as a clean answer about an empty one. That is the same unreadable-as-missing fail-open the strict mode exists to close, one level further in. A blank `storagePath` is worse than useless: it joins to a relative `lbug` under the current directory, so the sync opens an index that is not the repo's. Both now have to be non-blank after trimming. `path` stays at the bare string check, on the same reasoning that already exempts `indexedAt`/`lastCommit`: require only what resolution depends on to IDENTIFY the repo. This gate rejects the whole registry and the registry is machine-wide, so a field tightened past what identification needs would let one blank value in one row break every group sync on the machine — including groups whose repos all resolve. A blank `path` still yields a working handle; `defaultResolveHandle` does read it, but only for the pool id and `repoPath`, neither of which decides whether the row names a repo. The error now says what is actually wrong instead of naming three fields that are all present. Mutation-verified in both directions: dropping the trim turns the three rejection tests red, and applying the wider fix that was considered and declined — tightening `path` too — turns exactly the counter-case red, so that test genuinely pins the narrow reading rather than passing either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): bound the per-repo contract staging append `autoContracts.push(...repoContracts)` passes every staged contract as a separate argument, and the engine caps how many arguments one call may take. That cap is a function of the host's available stack, so it is a different number on every machine — this one accepts a 125k-element spread and dies at 150k. The spread itself is not new; what it carries is. Before staging, this line appended a single extractor's output as it came back. Staging made it carry the whole repo's, which is enough for a large repo to raise `RangeError: Maximum call stack size exceeded` on the one line whose job is to commit work that just succeeded. The throw lands in the catch below, so the sync reports a repo whose extractors all ran cleanly as one whose index could not be read — a crash wearing the costume of a diagnostic. A bounded loop replaces it: the count a repo can stage is now bounded by memory rather than by how much stack the process happened to get. The guard is structural, not size-based, and deliberately so. A "make the fixture big enough to crash" test passes against unfixed code on any host with a larger stack, which is exactly the guarantee a regression gate cannot give up. It walks the AST and locates the region by role — the `const` staging buffer typed `StoredContract[]`, then the extractor `try` that is a direct statement of the block declaring it — so renaming either identifier keeps it pointed at the same code. `.apply()` is rejected alongside spread, being the same hazard in different syntax. Direct statements only, because `syncGroup` wraps this whole section in its own try/finally for the lease sweep, and that ancestor reads the buffer too. Matching any enclosing `try` pulls in the entire function body — including the two windowed-manifest spreads, which are bounded by the window size and are not what this fixes. Mutation-verified in both directions: restoring the spread turns the gate red naming that line alone; deleting a manifest-window spread, and separately adding a third one, both leave it green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): keep unreadable repos out of manifest contracts too Per-repo staging closed one door: a repo whose extractor threw contributes nothing through the direct path. Deferred manifest resolution was a second door, still open. It derives its known-repo set from the resolved-handle map, which kept an entry for a repo the same run had already declared unreadable — so the sync re-opened that index and resolved symbols against a database it had just told the operator it could not read. Deleting the handle in the catch stops the re-open, but it does not satisfy R2 on its own: `ManifestExtractor` resolves both endpoints of a link and emits a contract for each, and for an endpoint with no executor that contract is still emitted with a synthetic UID. The registry ended up naming a repo the same run reported unreadable. So the emitted output is filtered by ENDPOINT, not by link. Dropping the whole link would delete the healthy partner's contract as well — a repo losing its own output because a neighbour's index would not open, which is wider than the requirement and destroys good data to suppress bad. A cross-link is different: it asserts something about a pair, so if either end is unreadable there is nothing left to anchor it to, and a half-anchored link is exactly the confident-about-what-it-could-not-read answer the registry must not give. Deleting the handle also changed what the operator gets told, so the warning is split. An unreadable repo IS configured; letting it fall into the "references repos not in config.repos" branch states something false and sends the reader to edit group.yaml for a problem only re-indexing fixes. It now gets its own message naming what was actually omitted. Mutation-verified four ways: reverting the endpoint filter turns three scenarios red; the over-broad whole-link variant turns the healthy-partner scenario red and nothing else; removing the handle delete turns the no-re-open scenario red; and reverting the warning split turns the operator- message scenario red. Every assertion reads the written contracts.json rather than the in-memory result. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor(group): keep readBridgeMeta's signature stable across the shape gate The shape gate landed by widening the return type to a reader-only `ReadBridgeMeta extends BridgeMeta`. That is source-compatible — a covariant return, one added optional field, every existing caller unaffected, typecheck and suite clean — but the contract check reads it as a changed signature with a caller left behind, and blocks the merge on it. This branch already hit the same wall on `readRegistry` and settled it the same way: leave the signature alone and make the difference legible some other way. So the flag moves onto `BridgeMeta` itself as an optional, documented, never-persisted field, and `readBridgeMeta` goes back to the exact signature its callers already compile against. That is the better shape here anyway. The reader-only subtype would have split the validation two ways: `openBridgeDbReadOnly` and `bridgeExists` both gate on `meta.version`, and the normalization that comes with the gate is what stops a `version: null` in a hand-edited meta.json from reading as `undefined` and sailing through `version > 0` as though the bridge had been vouched for. One type keeps all three callers behind the same guard. Nothing persists the flag: `writeBridgeMeta`'s only caller builds a fresh literal, so it cannot round-trip to disk. No behavior change — pure type restructuring. 927 tests pass, typecheck clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): stop treating a half-written bridge stamp as a verified match `bridgeMetaMatchesFile` joined its two `undefined` checks with `||`, so metadata carrying a size and no mtime — or the reverse — returned `true`, the same answer it gives a fully verified pair. A stamp is a PAIR. Both halves absent is the legacy shape: metadata written before stamping existed, which cannot be verified either way and is accepted deliberately, because failing it closed would mark every pre-existing bridge incomplete until re-synced. Exactly one half present is not that. Something wrote a stamp and did not finish, which is precisely the condition stamping was added to detect — so the check handed back "verified" for the one shape that most deserves suspicion, and a cross-repo impact query built on it would report a confident answer about a database its metadata cannot vouch for. The two states are now separated: neither half present accepts, exactly one rejects as provenance-unknown, both compare against the file as before. Found by the repository's own contract check, not by the plan. Mutation-verified: restoring the `||` form turns both half-stamp cases red while the legacy and fully-stamped controls stay green, so the pair genuinely pins the distinction rather than passing either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): pair unstamped bridge metadata by write order, before any open Unstamped metadata was waved through: `bridgeMetaMatchesFile` returned "matches" for any pair with no stamp to check, so the stale-meta-beside-a-new-database window stayed open for every bridge written before stamping existed, and `runGroupImpact` spent that metadata's completeness as fact. `writeBridge` renames the database into place and writes the metadata after, so `meta.mtime >= db.mtime` holds for any pair written together — including by builds that predate the stamp. A database strictly newer than the metadata beside it can only come from a swap whose metadata write did not land. That is the fallback now. It is a heuristic on write order, not proof of provenance, and it is wrong in two directions: a stale metadata file touched after the swap still reads as paired, and a pair whose clock stepped backwards between the two writes reads as unpaired. Both are recorded at the code; the second is the safe direction. Equality counts as paired, or a coarse-granularity filesystem would reject every legacy bridge for a reason that is about the filesystem. The verdict is now taken in `ensureBridgeReady` BEFORE the database is opened, and carried on the metadata rather than recomputed afterwards. That ordering is load-bearing, not tidiness. Impact and trace both open the bridge and only then ask about provenance, so on any platform or LadybugDB build where a read-only open advances the file's mtime, every pre-stamp bridge would report provenance-unknown from its first query onward — the exact repo-wide regression this rule was chosen to avoid, arriving as a silent downgrade rather than an error. It does not happen on Linux, which was measured. It cannot be measured on Windows: pinning it by really opening the database needs an in-process write→read reopen of the same bridge.lbug, which is a documented limitation there. Rather than ship a Windows-skipped test and leave the assumption unverified on the platform whose file semantics are most likely to differ, the check moved ahead of the open so no platform has to be trusted. The new guard forces the hostile case on every platform: the open is stubbed to advance the database's mtime, and the verdict must still be "paired". It is registered in the cross-platform list so the Windows and macOS shards run it, and it has a control so it cannot pass vacuously. Two existing fixtures mocked `readBridgeMeta` to return a stamped-era version while never writing a meta.json — a state production cannot reach, since a non-zero version can only come from a file that exists. They now write the metadata their own mock claims to have read, rather than the helper being loosened to accept metadata it cannot stat. Mutation-verified twice: reverting the write-order branch turns both rejection cases red while all four legacy-accept cases stay green, and moving the pairing call back after the open turns the ordering guard red on its own. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor(group): compute cross-repo completeness in one place Three surfaces can return a partial cross-repo answer — impact, trace, and the contract listing — and each decided for itself whether it was complete. Impact carried the structured triple; trace said it in prose, if at all. An agent reading a not-found trace had no machine-readable way to tell "there is no path" from "there may be a path in a repo this sync could not read", which is the difference between an answer and a floor. `crossRepoCompleteness` is now the one computation, and its input deliberately does not name where any of it came from. `BridgeMeta` is not in the signature and must not be: `groupContracts` answers the same question from contracts.json and never opens a bridge, so `version`, `repoListsUnreadable` and `pairedWithDatabase` do not exist on that path. Each caller derives its own `provenanceUnknown` — the bridge callers through `bridgeProvenanceUnknown`, which stays separate for exactly that reason — and passes the boolean in. Scope arrives as a predicate rather than a repo list or a subgroup, so narrowing a query's scope stays a change to one argument at the call site. The trace results now carry `truncated` / `truncationReason` / `riskEpistemic` like impact does. `notes` is untouched; it remains an addition to the machine channel, never the channel. One correction to the approach as written: it said to pass the trace's two endpoint repos as the predicate, but a destination trace declares no `to`. It asks where a call lands, so any member may hold the answer — and an unreadable provider repo is precisely how "no outgoing ContractLink leaves this repo" becomes a wrong answer rather than an empty one. Filtering that path to the `from` repo would have reintroduced the bug this unit exists to close, so it passes every repo and a test pins it. Two pre-existing paths become consistent with the vocabulary as a result: a crossing-capped result now reports `truncationReason: 'partial'` alongside the `truncated` flag it already set, and the destination path's `ambiguous` returns now report the cap its `ok` and `not_found` siblings already reported. Both are additive — no field is removed, and no `truncated` flips from true to false. `truncationFields` returns a discriminated union now, so `truncationReason` reads without a fallback on the branch where it cannot be absent. Mutation-verified: reverting the provenance fold alone — one line in the shared helper — turns 8 tests red across both surfaces, 2 new trace scenarios and 6 existing impact ones, which is the point of there being one helper. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): narrow the incomplete-repo set to the query's declared scope A subgroup-scoped impact query was marked a lower bound by repos it had explicitly excluded. The fan-out already drops every neighbour outside the subgroup, so those repos could not have contributed a crossing to the answer — and a completeness marker that fires on results it does not describe is how a caller learns to ignore the marker. The scope is the query's DECLARED one, not the one the walk reached. An incomplete repo's contracts are absent from the bridge by definition, so it is never in the traversed set; filtering on what was traversed would empty the intersection on every query and silently restore the fail-open this channel exists to close. Declared scope here is the subgroup PLUS the query's own repo, which the approach did not account for. The walk starts from that repo's contracts in the bridge, so when it is the repo the sync could not read there are no crossings to find under any scope — and a subgroup excluding it would have turned that vacuum into a confident "nothing depends on this", for a tool an agent uses to license a delete. That case reported a floor before this change, so narrowing to the subgroup alone would have been a regression. The union only ever widens the in-scope set, so it cannot re-mark a repo the query excluded. Membership goes through the existing `repoInSubgroup` in both clauses, `exact` for the origin equality, rather than growing a second notion of what it means for a repo path to be in scope. Sound only while `MAX_SUPPORTED_CROSS_DEPTH` is 1 — at depth 2 an out-of-scope repo can sit between two in-scope ones — and that constraint is recorded at the intersection. Unscoped queries are byte-for-byte unchanged: `repoInSubgroup` answers true for an absent subgroup, so the intersection is the whole set. Mutation-verified: restoring the unfiltered predicate turns exactly the two scoped cases red while the unscoped control and both in-scope guards stay green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): keep the preserved registry and the bridge from disagreeing A total-failure sync refreshed contracts.json's diagnostic lists and left meta.json alone. But meta.json, not contracts.json, is where runGroupImpact reads completeness from — so the registry said "this sync could not read app/backend" while a cross-repo query answered `{ cross: [], truncated: false }`. Two surfaces describing the same run, one of them wrong, and the wrong one is the machine-readable one an agent uses to license a delete. The preserve path now refreshes the same two fields in the metadata. The database stays untouched: it still holds the contracts being preserved, and rebuilding it here would be the one write that could lose them. Refreshing metadata is not free, though, and the obvious version of it is a fail-open. The rewrite moves meta.json's mtime to now while bridge.lbug's stays old, so an unstamped pair whose database is NEWER than its metadata — the shape the write-order rule exists to reject — would come out of a preserve sync passing the check. Writing "no stamp" does not help; the write-order comparison is exactly what the moved mtime defeats. The verdict has to be recorded in the metadata, because the refresh cannot avoid moving the mtime. So `provenanceUnknown` is persisted whenever the existing pair does not already check out, the existing stamp fields are carried through verbatim rather than dropped, and `bridgeMetaMatchesFile` rejects the marker ahead of both the stamp and the write-order heuristic. A pair that already matched is re-stamped instead, which also upgrades a legacy unstamped-but-paired bridge to an exact stamp. No preserve run can increase the number of pairs that pass the check. The marker self-clears: `writeBridge` builds fresh metadata and never sets it. `BridgeMeta` carries two reader-side fields documented as never persisted, and this is the first code in the repo that reads metadata and writes it back. Both are stripped explicitly before every write. `pairedWithDatabase` is the dangerous one — persisted, it would tell every future reader the pair had been verified — and a test seeds both on disk to pin that neither survives. The write is not wrapped in a catch, unlike writeBridge on the success path. There contracts.json is canonical and already written, so a stale bridge is a recoverable degradation; here the write IS the guard against a confident wrong answer, and swallowing its failure would reinstate the fail-open it closes. `writeContractRegistry` above is unguarded into the same directory for the same reason. A group with neither file writes nothing: `readBridgeMeta` already answers `version: 0` for an absent file, so a written one would say what the absence already says while inventing state for a bridge that has never existed. Mutation-verified three ways: dropping the marker write turns 6 red including both laundering scenarios; moving the marker check below the stamp branches turns the unstamped-laundering case red; removing the field stripping turns the never-persisted test red. Each restored byte-exactly and re-verified. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): report group_contracts' completeness in the shared vocabulary `group_contracts` returned contracts and cross-links and said nothing about whether that listing was the whole story. An agent reading it after a sync that could not open half the group got a confident-looking list with no way to tell it was a floor — the same fail-open the impact path already closed, on a surface that had no channel for the answer at all. It now returns the registry's two diagnostic lists and the structured triple, folded through the same helper the impact and trace surfaces use, so the three cannot drift. The helper takes no `BridgeMeta` precisely so this path — which reads contracts.json and never opens a bridge — can share it. The three registry states stay distinguishable, which is the point: - key absent: the registry predates the field and has no opinion about which indexes opened, so the key is omitted rather than invented as `[]`, and the listing reports a floor. It cannot say which repos the sync failed to read, so it cannot claim to be complete. - key present and empty: measured, clean, not truncated. - key present and populated: the repos, and a floor. `incompleteRepos` is dropped on this surface alone: both lists it derives from are returned verbatim beside it, and a third name for the same repos is drift waiting to happen. The import is lazy, matching `groupImpact` and `groupTrace` in this same class. `cross-impact.js` statically pulls the native LadybugDB binding through `bridge-db.js`, and `service.ts` is loaded by every `gitnexus group` subcommand including ones that touch no database. One fix inside the same file that this unit forced: the registry loader gated `missingRepos` with a bare `Array.isArray`, which admits `[{repo:'x'}]`. That was inert while nothing read the list, but this change both returns it and folds it into the completeness answer — so an unreadable value would have been printed as a repo name and would have flipped `truncated` on garbage. It now uses the same `recordedRepoList` gate `group status` already applies to the same field. `missingRepos` has always been required, so unlike `unreadableRepos` it has no "not recorded" state to preserve and an unreadable value degrades to empty. Mutation-verified: reverting the fold alone turns 14 tests red and leaves the control — the contract and cross-link payload this tool has always returned — green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(cli): stop dropping group contracts' completeness fields on the way out `group contracts --json` destructured `{ contracts, crossLinks }` from the service payload and rebuilt an object from just those two. Everything else the service returned was discarded on the way to stdout — so the completeness fields the MCP tool now carries were invisible at the CLI, and the two surfaces disagreed about the same registry. It prints the payload whole now. A field added to the service reaches `--json` without a matching edit here, which is the point: the re-serialized subset was a second place that had to be remembered, and it was not. The human-readable path gains the same signal in words. A listing built from a sync that could not read part of the group shows counts that are a floor, not a census, and it named neither fact. It now says so and names the repos when the registry recorded them — and says the sync did not record which repos it could read when it did not, because a listing that cannot say what it is missing is still incomplete. Mutation-verified: restoring the re-serialized subset turns the `--json` case and the control red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): tell a missing registry entry apart from an unreadable one `group status` printed MISSING for both "this repo has no row in the registry" and "the registry itself could not be read", so an operator whose registry.json was corrupt was told every repo was unregistered — and sent to re-register them instead of to the one file that was actually broken. The two are now separate. `missing` keeps its old meaning and still flags every unusable repo, so an older consumer is unaffected; `unresolvable` is additive, always present, and carries the reason that produced it. This is the one caller that has to make that distinction, so it takes the strict global-registry read. `readRegistry`'s `catch { return [] }` collapses a malformed registry into an empty one, which is indistinguishable from a genuine absence and is exactly what produced the wrong label. The cost is accepted knowingly and recorded at the call site: the strict read rejects the whole registry when any row fails to identify a repo, so one malformed row renders every member unresolvable — including members whose own rows are fine. That is the honest verdict, and it is reported as an unresolved state rather than a clean one. Choosing between the two labels needs to know whether a row exists at all, which `registryIdentifies` answers by mirroring the two tiers the resolver matches a bare group-config value on — registry name, case-insensitively, and repo path. It deliberately stops short of the hashed-id and partial-name tiers: those exist to be generous about what an operator typed, while this only picks a label, and a looser match would relabel a genuine registry miss as an unresolvable row — the same conflation this change removes, pointed the other way. The plan's third failure mode — a row that resolves but whose storage path cannot be opened — turns out to be unreachable: `loadMeta` returns null on every error and `checkStaleness` catches everything, so nothing after `resolveRepo` inside the try can throw. The reachable per-repo case is `resolveRepo` itself throwing, as it does for two registered clones sharing a name, and that is what the tests drive end to end through the real CLI. The code still handles the plan's case correctly if those helpers ever start throwing. Mutation-verified: reverting the split turns 6 unit and 2 CLI cases red while both controls — a genuine miss, and a healthy group — stay green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(cli): say what the preserve path actually does to contracts.json The sync summary announced "Did NOT write contracts.json" on the branch that writes it. The preserve path rewrites the file — keeping the previous sync's contracts and cross-links, replacing only the two diagnostic lists — so an operator who checked the mtime and found it moved was told the opposite of what had happened, on the command this PR exists to make legible. It now says the previous contracts were kept and names what changed. The no-prior-registry branch is narrowed for the same reason. It claimed nothing at all was written, and that is no longer true either: this path still records the run against an existing bridge's metadata. The claim is now scoped to contracts.json, which is the file it can actually speak for. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): stop the total-failure log promising a preservation that did not happen The warning fired before the prior registry was read, so it could only ever promise one of the two things that might be true — and it promised the wrong one to every group that has never synced: "keeping the contracts from the previous sync" about a file that does not exist. The console line for that same run, driven by `registryOutcome`, said the opposite. It now lives inside the branch, after the read, with one message per outcome chosen at the point the outcome is decided. The log and the console cannot disagree, because the same fact selects both. Both messages keep the warn level and the two repo lists. Mutation-verified: reverting the split turns the no-prior-registry case red while the preserved case — whose claim was already true — stays green. The dry-run test's log filter was also widened to the sentence both messages share, or the new wording would have made that assertion match nothing and pass regardless. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): make the bridge-failure warning describe what the code guarantees The warning after a failed `writeBridge` promised that cross-repo impact would report `truncated` until a sync succeeded. Nothing on that path produces that signal. The swap is the last step: `writeBridge` builds the new database in a staging directory and only then moves the old one aside. A failure during the build therefore leaves the previous sync's `bridge.lbug` exactly where it was, beside the `meta.json` stamped for it — a pair that passes `bridgeMetaMatchesFile` with the previous run's `unreadableRepos`. The next cross-repo query answers `truncated: false` from superseded contracts, which is the opposite of what the operator was told to expect, and worse than being told nothing. The warning now says what is actually true: contracts.json is intact and canonical, the bridge was not replaced, cross-repo queries may still answer from the previous sync's contracts, and nothing marks them as superseded. The metadata is deliberately NOT re-stamped to make the original promise true. That would recreate exactly the metadata/database mis-pairing the stamping on the preserve path exists to prevent, and the comment at the warning records it. The claim is asserted against captured log output rather than left to the state tests. Those check which pairs match and what the preserve path writes; every one of them stays green while this sentence reverts to promising a truncation. An unasserted user-facing branch is the defect class this change is closing, so it does not get to close it while remaining one. No filesystem shape makes the real `writeBridge` fail while `writeContractRegistry` succeeds — they write into the same directory one line apart — so the failure is armed through a pass-through wrapper on the file's existing mock. It delegates byte-for-byte unless a test arms it, and is reset around the new suite. Mutation-verified: restoring the original wording turns its own assertion red and nothing else. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(mcp): name every registry outcome group_sync can actually return The tool's description told agents `registryOutcome` is 'written' or 'preserved'. It has a third reachable value: 'no-prior-registry', returned when nothing could be read AND there was no previous contracts.json to carry forward. An agent calling this tool against a group that has never synced got a value its own tool description said did not exist, and no way to tell it apart from the case where the previous contracts survive. The distinction is the whole point of the value. After 'preserved' there is a registry to read — stale, but real. After 'no-prior-registry' there is nothing on disk at all, so a following group_contracts or group_impact has no registry rather than an old one. Those need different responses from the caller. 'not-attempted' stays undocumented because it is unreachable through this tool, and a guard asserts it stays that way. The code comment above the annotations claimed the preserve path does NOT write contracts.json. It does — it rewrites the file, keeping the previous contracts and cross-links and refreshing only the two diagnostic lists, which the CLI's own summary was corrected to say a few commits ago. Left alone it would have re-seeded the same wrong claim next to the text that now states it correctly. Mutation-verified: deleting the 'no-prior-registry' sentence turns the guard red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(mcp): explain structural incompleteness on the impact tool and status resource The impact tool's GROUP MODE paragraph described one cause of truncation — the fan-out running out of room — and left an agent to assume that was the only one. So a `truncated: true` carrying `truncationReason: 'incomplete-sync'` read as "retry with a smaller scope", when retrying returns the identical floor forever: the repos are absent from the bridge itself, and only a re-sync puts them back. The old text also said the response carries the truncation fields "when it stops early", which is wrong for that case — `truncatedRepos` names repos even when ZERO crossings to them were attempted, because their contracts were never in the bridge to cross to. The paragraph now branches on the reason and gives each its remedy: 'timeout' and 'partial' are runtime limits where a retry or a larger budget can help; 'incomplete-sync' is structural and the remedy is `group_sync`. The reason union is now derived from an exported `as const` array rather than written as a bare type. A type-only union gives a guard nothing to enumerate, so the guard has to hand-list the members — and then it passes forever the moment a fourth is added, which is the exact regression it exists to catch. The guard iterates the runtime array instead. Verified by appending a probe member and watching it go red, then removing it. The resolved type is unchanged; every importer uses `import type` and none needed an edit. The status resource said "Group index / contract staleness" and nothing about the distinctions its payload now carries. It explains all of them: a repo absent from the registry versus one whose entry could not be resolved, and the `unreadableRepos` tri-state where an ABSENT key is not an empty one — absent means the last sync never recorded what it could read, so cross-repo answers for that group are a floor. The description an MCP client actually receives lives in `getResourceTemplates`, not in the context resource's inventory line the plan pointed at. Both now carry the vocabulary, so the two surfaces cannot disagree about the same payload. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * feat(group): serialize group syncs behind a fail-closed per-group lock Two concurrent syncs of one group could lose one another's writes. Both read the prior registry, both built contracts, both wrote — last writer won, and the loser's work was gone with nothing reporting it. A group sync is long and expensive and is exactly the operation whose lost update destroys contracts. `syncGroup` now takes a lock for the whole persist section, acquired exactly once. `acquireIndexLock` is not reentrant, so a second acquisition anywhere below would deadlock the happy path rather than an edge case; `withGroupSyncLock` has one call site and nothing inside it re-acquires. The lock lives on a dedicated `sync-lock` directory inside the group directory, mirroring the registry lock's dedicated directory rather than reusing the resource's own — a lock directory that could collide with a per-repo index slot repeats a bug the registry lock's comment already warns about. It fails CLOSED, which is the opposite of `withRegistryLock` and deliberately so. That one degrades to unlocked because it guards a sub-second JSON merge on a latency-critical path; here running unprotected is the outcome the lock exists to prevent. Three exits are covered: a timeout, an unwritable lock directory, and the lock-free degradation the primitive performs silently. That third exit needed a change in `index-lock.ts`, and it is the one declared exception to keeping this work inside core/group/. `acquireIndexLock` answers a read-only or permission-denied filesystem with a no-op handle that is byte-identical in shape to a real one, so a caller for whom lock-free is not an acceptable outcome could not tell the difference. It now carries an optional `lockFree` marker. The change is additive by construction: no signature moves, no control flow changes, nothing about when or how a lock is taken changes, and every caller that ignores the field behaves exactly as before. A filesystem probe inside the group module was considered and rejected on evidence: `selectBackend` returns `socket` on Linux and Windows, where `acquireViaSocket` never touches the filesystem and this branch cannot occur — so a probe would refuse syncs on the two platforms that never degrade while missing the one that does. The timeout ceiling is a named 600s constant passed explicitly. The magnitude matches the primitive's own analyze-sized default because a group sync is analyze-shaped and a legitimately queued second sync must be able to wait out a full first one. Passing it explicitly is about the override, not the magnitude: `resolveTimeoutMs` resolves `GITNEXUS_INDEX_LOCK_TIMEOUT_MS <= 0` to Infinity, which would turn fail-closed into a hang. Cross-process exclusion is proved with a real spawned holder, not an in-process mock, which cannot demonstrate the property this exists for. The lock-free scenario pins `GITNEXUS_INDEX_LOCK_BACKEND=file` — unpinned it would pass on two of three platforms while measuring nothing — and produces the failure by injecting EACCES on one syscall rather than by chmod, so it runs identically on Windows instead of being skipped there. The CLI reports the failure through pino rather than a bare stderr write, which this package lints as an error to keep that migration moving, and the test reads the `msg` field rather than a raw substring — matching on the raw text would have passed only by accident of quoting and would go green again if the line were downgraded. Nothing is skipped on any platform, and the test is registered for the cross-platform shards. Mutation-verified: removing the lock acquisition turns 6 scenarios red; removing the lock-free rejection turns the degradation scenario red on its own. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): make the sync-lock timeout name a cause it can establish The fail-closed lock surfaced the primitive's own timeout message to users for the first time, and that message says the wait was on "another gitnexus analyze" — a cause its detection path cannot establish. It is the same confident-about-what-it-could-not-determine claim this PR exists to remove, inherited rather than written. The wrapper now throws its own. It names the group, the lock directory, the operation, and the elapsed wait, and it says plainly that nothing was written. The holder clause branches on `holderKnown`. The socket backend exposes no owner metadata and reports a placeholder pid of -1, so on that backend — and on the file backend's malformed or vanished-lock timeouts — the message says the lock stayed held but the backend cannot identify who held it, rather than printing a pid that means nothing. The elapsed wait is measured by the wrapper. `IndexLockTimeoutError` carries only `holder` and `holderKnown`; the figure exists solely inside the string being replaced, so it had to be taken rather than read. One pre-existing assertion changed with it: the timeout case asserted `'Timed out after 600000ms'` from the inherited text, which is precisely the message this replaces. Mutation-verified: restoring the inherited message turns the three assertion cases red and leaves the control — a real acquisition that succeeds — green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): stop a losing sync from downgrading the one that beat it to the lock Serializing is not ordering. Both syncs run extraction outside the critical section, so a total-failure sync that acquires second reads the winner's fresh registry as `prior` and rewrites it with all-unreadable lists. The lock alone does not prevent that — it only decides who goes second, and the loser then overwrites a healthy registry with a description of its own failure. Deterministically, not as a rare interleave. The guard is a compare-and-swap on the registry file's own identity: stat before acquiring, re-stat after, and write nothing when they differ. Identity is presence plus size, mtime and inode — `writeContractRegistry` publishes through write-then-rename, so a real replacement always changes the inode even if size and mtime happen to collide. Deliberately NOT keyed on `generatedAt`, for two independent reasons. It is stamped when the registry object is built, before the lock is acquired, so a winner that waited would write a value older than the loser's start. And the preserve path carries it forward verbatim by design — it dates the contracts, not the write — so after any preserve sync it does not date the write at all, leaving the comparison blind on exactly the pairing this guards. A file-identity compare also needs no cross-process clock agreement. The skip reports the existing `preserved` outcome. Nothing was written and a prior registry was kept, which is what that value already means; a new one would falsify the guard asserting the sync tool's description names every reachable outcome, and would fall through the CLI's outcome chain, which has no fallback. The bridge metadata refresh is skipped too, which the plan did not specify. `refreshPreservedBridgeMeta` stamps THIS run's repo lists into meta.json, and meta.json is where cross-repo impact reads completeness — so writing it would report as unaccounted-for exactly the repos the winning sync had just accounted for. That is the same downgrade being refused, one file over. Skipping both is what makes `preserved` an honest answer here. Mutation-verified: removing the after-stat and the skip turns the three decisive cases red while both non-misfire controls stay green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor(group): run the bridge swap inside the caller's critical section The bridge swap needed the group lock, and could not take it: `syncGroup` already holds it when it calls `writeBridge`, and `acquireIndexLock` is not reentrant. Acquiring inside the swap would deadlock every sync on the happy path rather than on an edge case. So the body splits the way this repo already splits this shape — a lock-free `writeBridgeUnlocked` whose precondition is that the caller holds the lock, and a thin `writeBridge` wrapper that acquires it for direct callers, mirroring `registerRepoUnlocked` / `withRegistryLock`. `syncGroup` calls the inner one; everything else keeps calling `writeBridge` and is now serialized by it. `writeBridge`'s exported signature is byte-identical to before, so no caller changed and nothing about the exported surface moved. The precondition is enforced by a comment naming the single production call site, which is what the existing precedent does. A type could carry it, but the repo's own answer to this question is a comment, and diverging here would make this the odd one out for no additional guarantee. `refreshPreservedBridgeMeta` is deliberately left unsplit. Its one caller is already inside the critical section and it has no test callers, so an acquiring wrapper would be dead code standing in for a guarantee the caller already provides — and moving the lock inside it would be the second acquisition this change exists to avoid. Scope: this delivers writer-writer exclusion only. The reader-side promotion of a leftover `.bak` into place runs on ordinary reads, outside any lock, and is not claimed here — the pairing check remains the reader's defense. Confirmed as live behavior while writing the crash-recovery test, which asserts on file state rather than through `bridgeExists` for exactly that reason. One test file beyond the two the unit named had to change: a suite mocks `bridge-db` to inject a `writeBridge` failure and exercise the bridge-write warning. Once the sync calls `writeBridgeUnlocked`, that fault was being injected into a function the path no longer calls, and the test went red. The mock is repointed. Mutation-verified three ways. Pointing the sync back at the acquiring wrapper deadlocks a single UNCONTENDED sync — the evidence that the nesting defect is real and that this split is what prevents it. Removing the wrapper's acquisition turns the direct-write exclusion case red. Making the lock-free half acquire for itself turns the held-lock case red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(hygiene): reach every tracked text file with the raw-byte guard The guard claimed to protect tracked source from a raw NUL — the byte that makes git classify a file binary and costs it its diff, its inline comments and its three-way merge on GitHub. It matched on an end-anchored extension regex covering the JavaScript family, so most of what this repo tracks was never looked at: JSON, YAML, TOML, Markdown, snapshots, SQL, protobuf, the .NET project files, the shell and batch scripts. Worse, an extension regex cannot reach a file that has none. `Dockerfile`, `CODEOWNERS`, `LICENSE`, the husky hook and every bare dotfile were unreachable by construction — no amount of widening the pattern would have covered them — so a second basename filter had to exist for the claim to be true. It stays an allowlist rather than becoming "everything git tracks", because the repo legitimately tracks binaries whose extensions must stay out. The two filters together now collect every one of the 5000 tracked files except 31 — the 30 native prebuilds and one PNG — and those 31 are exactly the files that carry a NUL. The allowlist no longer has a gap that is not a genuine binary. The planted-fixture cases route through the collector's own predicate rather than straight into the scanner. The pre-existing fixture test bypassed the filter entirely, so it could only ever prove the byte locator worked, never that the collector would hand it the file — which is precisely how the gap survived. Mutation-verified both ways: removing the basename filter drops `.gitignore` and `Dockerfile` from the planted results, and reverting the extension regex drops `.json` and `.md`. One added case is a preservation pin rather than proof — that tracked binary formats stay out passes either way, and guards the allowlist from becoming a denylist later. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(hygiene): stop the byte guard reading the vendored grammar tree Widening the guard to every tracked text format also pulled in the vendored tree-sitter grammars, and those are where the bytes are: four generated `parser.c` files come to 62 MB between them, Kotlin's alone 33.7 MB. Excluding that root drops 76 files but 66% of the bytes the scan reads — 97 MB down to 33 MB. The exclusion is a single anchored prefix, matched case-sensitively with `startsWith`, and both halves of that matter. A `vendor` path-SEGMENT match would also drop first-party fixtures this repo tracks under directories named `vendor` and `Vendor` — a Kotlin one, a PHP one, and three files under gitnexus-web — silently narrowing coverage while the assertion pinned the loss in place. Case-insensitivity would do the same to a `Vendor` directory at the excluded root's own level. The root is named in the guard itself, so the claim that it covers every tracked text file stays honest about the one place it deliberately does not look. The cost comment was wrong and is now measured rather than estimated. It said "the scan is ~10 ms" — ambiguous between locating the byte and reading the files, and stale in its byte basis. Locating is ~14 ms; the reads dominate it by two orders of magnitude, which is the actual reason for the concurrency pool and the actual reason this exclusion is worth having. Every figure was re-derived from the finished file rather than carried over from a draft. The header's claim that `git ls-files` "never descends into vendor" was already false — vendored code is tracked, so all 106 of its files were being reported and read. Corrected here, where the distinction becomes load-bearing. Registered in the cross-platform list first and given a shard weight second. The weight table is only consulted for files already in that list, so a weight entry alone is inert and the shard test filters unregistered keys without complaining. The three-way split stays within 1.01x of ideal. Mutation-verified three ways: a case-insensitive segment match, a case-sensitive segment match, and a case-insensitive anchored prefix each turn an assertion red. The casing half was initially unfalsifiable — nothing tracked is named `gitnexus/Vendor/`, so a tracked-set assertion could not distinguish it. Rather than leave the claim unpinned or invent a fixture, it is pinned on the predicate with a synthetic path; the tracked-set assertions pin the anchoring. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(group): make the strict-read test able to see which read ran The file bound both registry exports to one mock: readRegistry: (...args) => readRegistryMock(...args), readRegistryStrict: (...args) => readRegistryMock(...args), so the case named for the strict read asserted a behavior it could not attribute. Point the production call at the lenient export and every assertion still holds, because the mock answers the same way whichever one is called. That is not a hypothetical. With this file as it was, and `syncGroup` mutated to call `readRegistry` instead of `readRegistryStrict`, all 32 tests passed — the suite was blind to the exact substitution it exists to prevent, and the fix it guards could have been reverted without a single red. The exports now have separate mocks: the lenient one always resolves an empty list, which is its real contract, and only the strict one is armed by the cases that need a failure. The named case also asserts directly that the strict read was called and the lenient one was not, so the attribution is explicit rather than implied by an outcome. No tests added — the unit is about what the existing ones can see. Mutation-verified: the same substitution now turns 24 cases red, including the named one, and everything stays green unmutated. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(group): pin the CLI output branches this PR introduced The three sync outcomes and the status table's new labels had no assertions. Every one of them is a sentence about what happened on disk, and this PR corrected several that were false — a preserve branch that announced it had not written the file it rewrites, a status table that called an unreadable registry a missing entry. Text that describes state, with nothing pinning it, is how those got wrong in the first place. Six cases drive the real CLI end to end, through the two shapes that need no indexed repo: members absent from the registry, and members registered at a storage path with no index file, which makes every repo unreadable. The file header claimed no LadybugDB-backed command was driven end to end; that is no longer true and it now says so. Each branch was suppressed in turn and its assertion goes red — all five that the plan named. One of those mutations first reported PASS, and the cause is worth recording: the string being suppressed also appears inside a neighbouring branch's comment, so the harness silenced the wrong line. That is a bad mutation, not a weak test. The harness now asserts the marker it suppresses is unique before trusting the result, and the redone check goes red. The plan's sixth scenario is already covered by an existing case that asserts both labels in one table, so it is not duplicated. A seventh case was added beyond the plan: without a populated-list case, "prints neither line" would pass just as well against a CLI that never printed that line at all. Adds about 15s of measured spawn time locally; CI runs these against the built dist, which is materially faster per spawn. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(group): assert the MCP payloads by exact shape, not by partial match Nothing asserted what the group tools actually return. The sync response's unreadable list and registry outcome, and the contract listing's incompleteness fields, are documented in the tool descriptions an agent reads — and could have been dropped in a refactor without a single test noticing. The assertions are exact-shape rather than partial. A `toMatchObject` would let a dropped key pass, which is precisely the regression these exist to catch: the failure mode is an absent field, and a partial match is defined not to see one. Absences are additionally asserted explicitly. The tri-state has to survive the response boundary, and it is the reason exact shape matters here more than usual. An absent `unreadableRepos` means the sync never recorded what it could read, so the listing is a floor; an empty list means it measured none; a populated list names them. Collapsing absent into empty turns "we do not know" into "we checked, it is fine" — so a mutation that replaces the conditional spread with `?? []` is covered specifically, not just the outright deletion. Mutation-verified per field: removing either sync forwarding line, deleting the conditional spread, replacing it with the invent-empty form, dropping the truncation triple, or hardcoding the provenance flag each turns an assertion red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(group): stop the bridge input narrowing what unreadableRepos means The same field had three definitions. The registry and the bridge metadata both say it covers a repo this sync could not extract from — an index that would not open, or an extractor that threw partway through, one bucket because the consequence is one thing. The bridge input said only "whose index could not be opened", which describes one cause and silently excludes the other. It now points at the registry's definition instead of restating it a third time. A definition written once and referenced cannot drift; three copies of it already had. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(group): record what the mtime pairing does and does not prove The write-order fallback is a heuristic standing in for provenance, and a future reader deciding whether to lean on it needs to know where it breaks before they do. Both directions are now stated where the function is read rather than only in the plan that introduced it. The false-accept direction is a non-monotonic wall clock — mtime is realtime, so an NTP step back, a snapshot restore, or container skew between the two writes can leave a mis-paired set reading as ordered. Coarse filesystem granularity is explicitly called out as NOT being that hazard, because it looks like it: it collapses a pair written together to equal times, and equal is accepted, which is the right answer for that pair. The false-reject direction is any copy or restore that rewrites the database's mtime after the metadata's. An intact legacy pair is demoted to a lower bound and stays there until a sync re-stamps it, because nothing on the read path can tell it apart from the swap window it imitates. That second direction corrects a claim made while planning this work: that the rule could only ever demote pairs already broken. It cannot. `cp -r` and `rsync` without timestamp preservation both produce it on a healthy group, and saying otherwise where the code is read would leave a future reader to discover it the hard way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(storage): stop a corrupt registry quoting its own bytes into errors `JSON.parse`'s SyntaxError embeds a window of the source around the failure — V8 gives exactly ten characters either side — and the strict read rethrew it untouched. The registry persists HTTPS remote URLs with their userinfo, so a file that breaks next to one puts the credential into the error: Unexpected token 'L', ..."end.git"},LEAKCAN4RY"... is not valid JSON The parse now has its own guarded region and reports the path and the failure class, matching the two corrupt-registry errors already in this function. The original error is discarded — not logged, not attached as `cause`. This codebase's convention elsewhere is to hand the logger the Error so it captures stack and cause, and following that convention here is precisely what would put the byte window into the log. Under MCP stdio that log is written to the client's log file on disk, so the thrown-error channel was never the only one that mattered. The `catch` takes no binding, so the error cannot be reused by accident later. That was not theoretical: a sibling commit routes this message into `unresolvableReason`, which `group status` returns to MCP clients and prints in the CLI table. Every channel was traced — throw, cause, inspect with the full chain, the logger, and both downstream consumers. The leaking shape is narrower than it first appears, and worth recording. The windowed message only fires when the parser fails at a value-start or trailing position; a break inside a quoted string yields an unterminated-string error carrying no window. So a plain mid-URL truncation does not leak — a short write landing over a longer one does, leaving a URL fragment where a value was expected. That is a reachable shape for the one machine-wide file every gitnexus process writes. The test asserts the message still names the path and the corruption class, not only that the secret is absent. Asserting absence alone would stay green if the message became empty. Mutation-verified: restoring the raw rethrow brings the token back verbatim. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(storage): drop the stale lenient call-site count The docstring said keeping `readRegistry`'s signature untouched leaves "its nine other call sites" unaffected. There were thirteen when the discrepancy was noticed and fourteen by the time it was fixed. The same figure appeared in the test file's header. Replaced rather than corrected. A count in prose next to code that moves is a claim that goes stale without anything failing — which is the defect class this change set exists to remove, so re-seeding a fresh number would be repeating it with a longer fuse. The argument was never about the quantity: leaving the signature alone keeps every lenient caller provably unaffected whether there is one or fifty. Also withdrawn while here: the claim that the bridge schema-version guards diverge between call sites. They do not — the two forms are complements for every value a writer can produce, there are three sites rather than the two claimed, and all three agree. Recording a divergence that does not exist would leave a future reader chasing it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * docs(group): add an auditable finding-to-commit map The Definition of Done claims every review finding has exactly one commit and that reverting it reintroduces that finding and no other. Without a map that claim is only checkable by whoever holds the review report, which is one person for a short time. The map lists all 28 primary findings against their commits, the three findings whose suggested fix was deliberately not implemented and what shipped instead, and the four defects found while executing that no reviewer raised. It also records the revert contract honestly. Revertability is dependency-aware, not absolute: the shared completeness helper has three consumers, so reverting it alone does not build. That coupled set is named rather than left for someone to discover mid-revert. Two sections exist because the work produced them, not because the plan asked. Six claims in the plan turned out to be contradicted by the code — among them a scope predicate that would have reintroduced the bug its unit was closing, and an assertion about the mtime rule that was simply wrong. Recording only the findings would leave the impression the plan was followed as written. Five residual risks are listed for the same reason, including that R14 is not met on this PR: the diff attribute works locally but GitHub reads it from the base side, so this PR's own sync.ts stays binary in the web view and every PR after it renders as text. Not under docs/ — that path is gitignored, so a map written there would never reach the PR and the audit it exists for could not be performed by anyone else. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): read a version that is not a version as no provenance Raised by the check bot on this PR, and real — the bot found one symptom of it; the field splits four gates apart, not one. `readBridgeMeta` accepted any numeric `version`, and `0` is this file's word for "no provenance". A parseable but impossible value — negative, fractional — is not a schema version, and each gate that reads the field disagreed about it: ensureBridgeReady `> 0 && !== CURRENT` → opens the bridge openBridgeDbReadOnly `> 0 && !== CURRENT` → opens the bridge bridgeExists `=== 0 || === CURRENT` → says it is not there bridgeProvenanceUnknown `=== 0` → reports the answer complete Four verdicts about one file, and the last one is a fail-open of exactly the class this PR exists to close: a bridge nothing can vouch for, reported as fully accounted for. The suggested fix was to widen the provenance check to `<= 0`. That closes the reported symptom and leaves `bridgeExists` still disagreeing with both openers, so it is fixed at the reader instead: a version that is not a positive integer normalizes to the sentinel the gates were all written against. One change, four gates agreeing by construction, rather than teaching each of them the same new case and hoping the fifth reader remembers. Infinity is covered too, though by the pre-existing type check rather than the range one — JSON cannot carry it, so it arrives as `null`. Recorded at the test so the case is not mistaken for proof of the range check. Mutation-verified: restoring the loose numeric check turns the negative and fractional cases red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): stop a malformed contracts.json reading as an unresolvable registry entry Raised by the check bot on this PR. Its stated mechanism was wrong — `loadMeta` returns null on every error and `checkStaleness` catches everything, so neither can throw — but its conclusion was right, and there is a concrete path it did not name. `readContractRegistry` is a bare `JSON.parse(content) as ContractRegistry` with no shape check, and the snapshot lookup guarded only the registry object: registry?.repoSnapshots[repoPath] The `?.` covers `registry` being null, not `repoSnapshots` being absent. A contracts.json without that field — a legacy file, a hand-edit, a truncated write — throws `TypeError: Cannot read properties of undefined`, which lands in the catch that labels failures as unresolvable GLOBAL-registry entries. So a group whose own contracts file is malformed reported every repo as a broken registry row, sending the operator to repair a file that was fine. An error from one cause presented as another, which is the defect this PR has been removing everywhere else. The optional chain closes the crash. The try is also narrowed to the call that earns the label: only `resolveRepo` sits inside it now, so "did not resolve" describes something that actually failed to resolve rather than whatever else happened to throw nearby. The comment records why the other two calls in that block cannot throw, so the next reader does not have to re-derive it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor(group): give the completeness fold a module no native binding reaches The shared fold ended up in `cross-impact.ts`, which statically imports `bridge-db.ts` and through it the native LadybugDB binding. `groupContracts` therefore reached it through `await import('./cross-impact.js')` — loading that whole module graph to run a Set union and a ternary. Measured: 44-51ms and 8.4MB of RSS on first call, paid once per MCP server and once per `gitnexus group contracts` invocation. `completeness.ts` holds the vocabulary and the fold and imports nothing but types. `service.ts` imports it statically; the lazy import and the comment justifying it both go. `cross-impact.ts` re-exports so the three surfaces still have one import site for the vocabulary. Three other duplications collapse into the same move. `traceCompleteness` was hand-writing `{truncated, truncationReason, riskEpistemic}` — a third writer of the pair `truncationFields` exists to keep mechanically linked (#2787), in the file the consolidation had just touched. It calls the helper now. `recordedRepoList` existed twice, byte-identical, one copy's docblock saying it mirrored the other. That gate is the predicate the whole absent-vs-empty-vs-populated distinction rests on, applied to the same two lists on both the registry and the bridge — tightening one copy would have fixed one surface silently. One definition now. The trace's scope predicate compared repo paths with `===` while its sibling in `cross-impact.ts`, added in the same change, went through `repoInSubgroup` with a comment about not growing a second notion of membership. It had grown one: the helper normalizes separators and strips trailing slashes, so the same group.yaml spelling could be in scope for impact and out of scope for trace. Also here: `registryIdentifies` was a third, weaker copy of the registry's path rule — it skipped `realpath`, so a symlinked row would not match where the real resolver would. It uses `canonicalizePath`/`registryPathEquals` now. `contracts.json` is no longer respelled as a literal in `sync.ts`; `storage.ts` owns the name it reads and writes. And the runtime-truncation predicate is bound once instead of written out at both the flag and the reason, where forgetting the second would label a retry-able answer `incomplete-sync`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(group): give the lost-the-race sync its own outcome instead of overloading preserved A sync that finds contracts.json replaced while it waited for the lock reported `registryOutcome: 'preserved'`. That value already meant something else, and the two differ in exactly the thing the value is for: `preserved` rewrites the file with this run's diagnostics; this path does not touch it and deliberately does not record them. So both surfaces stated something false about disk. The tool description told agents `preserved` means "contracts.json was rewritten ... refreshing only missingRepos/unreadableRepos to describe THIS run (the file changed)". The CLI said "only the unreadable/missing repo lists were refreshed to describe THIS run". On the lost-race branch nothing was written and the log line beside it says so outright. That is the defect class this whole change set removes, reintroduced by the change set itself — and the reasoning recorded at the time makes it worse, not better: a new value was rejected because it "would fall through cli/group.ts's outcome chain, which has no fallback branch". A renderer limitation decided a domain value, and the description then had to cover two states with one sentence that fits one of them. `superseded` is its own outcome now, described in its own words to agents and rendered in its own words at the CLI. The registry on disk is FRESHER than this response's diagnostics, which is the opposite of every other non-written outcome and is why an agent needs to tell them apart. The CLI renders from a `Record` keyed on the union, so the next outcome fails the build here rather than printing nothing — the gap that made folding the state in look like the cheap option. The description guard is scoped per clause rather than over the whole string. It forbade "untouched" anywhere, which was right when one clause could only lie in that direction and wrong now that another clause is accurately untouched. It also asserts the superseded clause says so, or the two collapse back into one word for two states. Found by the quality pass over this branch, not by review. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(group): read bytes and stat through one handle, not two path lookups CodeQL flagged both sites as `js/file-system-race`, high severity, and it is right about the shape. `stat(path)` followed by `readFile(path)` is two independent path resolutions with a window between them — the classic check-then-use race. It also made the assertions weaker than they read. These two tests exist to prove a specific file was left untouched, and two lookups can land on different inodes, so "the bytes and the mtime are both unchanged" was not actually a statement about one file. The distinction is the whole point here rather than a technicality. `snapshotFile` opens the path once and takes both answers from that handle. The race is gone because there is no second lookup, and the assertion now genuinely concerns one inode. I had previously triaged these as below the ruleset's threshold and left them for the repository owner. That was wrong: they carry `security_severity_level: high`, and the branch ruleset gates on `high_or_higher`, so they were blocking the merge rather than sitting under it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Gergő Magyar <gergomagyar@icloud.com> Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
This commit is contained in:
parent
031e123731
commit
2c0fb7753c
39 changed files with 8129 additions and 91 deletions
12
.gitattributes
vendored
12
.gitattributes
vendored
|
|
@ -15,3 +15,15 @@
|
|||
*.so binary
|
||||
*.dll binary
|
||||
*.dylib binary
|
||||
|
||||
# TypeScript sources are always text for diff purposes. Git's binary
|
||||
# heuristic fires when EITHER blob in a pair carries a NUL, so a source
|
||||
# file that carried one on a base commit still renders as "Binary files
|
||||
# differ" — with no hunks and no inline comments — long after the byte
|
||||
# itself is gone from the working tree. A head-side guard cannot see
|
||||
# that, by construction. This does not mark the files binary or change
|
||||
# how they are stored; it only stops the heuristic from hiding a diff.
|
||||
*.ts diff
|
||||
*.tsx diff
|
||||
*.mts diff
|
||||
*.cts diff
|
||||
|
|
|
|||
|
|
@ -65,6 +65,16 @@ export const WINDOWS_WEIGHTS_SEC: Readonly<Record<string, number>> = {
|
|||
'test/integration/antigravity-hook-e2e.test.ts': 7,
|
||||
'test/unit/index-lock.test.ts': 5,
|
||||
'test/unit/setup.test.ts': 5,
|
||||
// ESTIMATE, not a measurement. This file asserts almost nothing; it READS —
|
||||
// one 4893-file pass over every tracked text file, plus an 830-file pass over
|
||||
// `src/`. Measured at 2.3 s and 0.3 s per pass on a virtualised and a local
|
||||
// Linux filesystem respectively, so the cost is entirely per-file open
|
||||
// latency, which is the term Windows inflates most (NTFS plus Defender on
|
||||
// every read). Scaled from the slower Linux figure to keep the split
|
||||
// conservative rather than let the 8 s PER_FILE_OVERHEAD floor under-charge
|
||||
// a file that touches more paths than anything else here. Replace with a real
|
||||
// figure after the first green Windows matrix run.
|
||||
'test/unit/source-control-bytes.test.ts': 15,
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -208,6 +208,18 @@ const SPAWN_CLI = [
|
|||
// exposed a file-backend double-admit race here (#2658 review); the reclaim is
|
||||
// now judgment-verified so a live holder is never displaced.
|
||||
'test/integration/analyze-index-lock-concurrency.test.ts',
|
||||
// The per-group sync lock (R9), same class of guarantee one level up: real
|
||||
// child processes contend for one group's lock while this process runs a real
|
||||
// `syncGroup`, and the CLI case spawns the real command. Everything that
|
||||
// varies here is platform-owned — which backend `selectBackend()` picks
|
||||
// (Windows named pipe / Linux abstract socket / macOS file lock), kernel
|
||||
// auto-release on SIGKILL vs. the file backend's pid-liveness reclaim, and
|
||||
// `mkdir` over an occupied path. The fail-closed cases pin
|
||||
// GITNEXUS_INDEX_LOCK_BACKEND=file so the filesystem branch is exercised on
|
||||
// every OS rather than only where it is the default; no case is skipped on
|
||||
// any platform, because a skipped case turns "a sync that cannot be protected
|
||||
// does not run" into a claim that holds on Ubuntu only.
|
||||
'test/integration/group/group-sync-lock-concurrency.test.ts',
|
||||
// The three `dist/` module-load closure guards, all built on the shared
|
||||
// child-process probe in `test/helpers/module-load-probe.ts`. That probe IS
|
||||
// the platform-varying part: it spawns `process.execPath` in array form,
|
||||
|
|
@ -261,6 +273,28 @@ const FILESYSTEM = [
|
|||
'test/integration/filesystem-walker.test.ts',
|
||||
'test/integration/markdown-processor-crlf.test.ts',
|
||||
'test/integration/ignore-and-skip-e2e.test.ts',
|
||||
// Pins that the bridge pairing verdict is measured before the database is
|
||||
// opened. The property it protects is about mtime behavior across OS and
|
||||
// filesystem, and the alternative — really opening the bridge — cannot run on
|
||||
// Windows at all (in-process write→read reopen of the same bridge.lbug is a
|
||||
// documented limitation). Running it on every platform is the whole point:
|
||||
// Windows is where an unverified assumption about mtime would hurt most.
|
||||
'test/unit/group/bridge-pairing-precedes-open.test.ts',
|
||||
// The raw-control-byte guard reads every tracked text file `git ls-files`
|
||||
// reports — 4893 of them — and decides membership from the git path, which is
|
||||
// always `/`-separated no matter what the host separator is. Both halves of
|
||||
// that are platform-varying: the collector basename-matches with
|
||||
// `path.posix.basename` against `git ls-files -z` output while the reads go
|
||||
// through `path.join`, so on Windows the same string is consumed under two
|
||||
// separator conventions in one pass, and only a real windows-latest run
|
||||
// proves they agree. It is also the file-count-heaviest read loop in the
|
||||
// suite, so it is where a per-file filesystem cost (NTFS + Defender, or
|
||||
// macOS's slower stat path) would show up first. No case is skipped on any
|
||||
// platform: a guard that only holds on Ubuntu is not a guard on the file
|
||||
// whose NUL it exists to catch. Budget: the heaviest single case is one
|
||||
// 4893-file pass — 2.3 s on a slow virtualised filesystem, 0.34 s on a local
|
||||
// disk — against a 30 s testTimeout.
|
||||
'test/unit/source-control-bytes.test.ts',
|
||||
];
|
||||
|
||||
const ALL_CROSS_PLATFORM = [
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// gitnexus/src/cli/group.ts
|
||||
import { createRequire } from 'node:module';
|
||||
import type { Command } from 'commander';
|
||||
import type { RegistryWriteOutcome } from '../core/group/sync.js';
|
||||
import { logger } from '../core/logger.js';
|
||||
|
||||
const _require = createRequire(import.meta.url);
|
||||
|
|
@ -120,16 +121,42 @@ export function registerGroupCommands(program: Command): void {
|
|||
indexStale: boolean;
|
||||
contractsStale: boolean;
|
||||
missing: boolean;
|
||||
/**
|
||||
* Optional here on purpose: a payload produced before the split
|
||||
* carries no such key, and an absent one must degrade to the
|
||||
* label this command has always printed rather than to the new
|
||||
* one — an unrecorded cause is not evidence of a cause.
|
||||
*/
|
||||
unresolvable?: boolean;
|
||||
unresolvableReason?: string;
|
||||
commitsBehind?: number;
|
||||
}
|
||||
>;
|
||||
missingRepos?: string[];
|
||||
unreadableRepos?: string[];
|
||||
};
|
||||
|
||||
console.log(' Repo index / contracts staleness:');
|
||||
for (const [repoPath, row] of Object.entries(st.repos || {})) {
|
||||
if (row.missing) {
|
||||
console.log(` ${repoPath.padEnd(25)} MISSING (not in registry or unreadable)`);
|
||||
// Two different facts with two different remedies: a repo the
|
||||
// registry never heard of is fixed by indexing it, while an entry
|
||||
// the resolver choked on is fixed by repairing the registry.
|
||||
// Printing "no entry in the registry" for the second one states a
|
||||
// cause that was never measured, and points at the wrong repair.
|
||||
if (row.unresolvable) {
|
||||
// The reason can be multi-line — an ambiguous registry names
|
||||
// every colliding clone. Fold it onto this row's line rather
|
||||
// than truncating it: those paths are what the operator acts on,
|
||||
// and a table row that swallows half its own explanation is the
|
||||
// failure this label exists to stop.
|
||||
const why = (row.unresolvableReason ?? 'the registry entry could not be resolved')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
console.log(` ${repoPath.padEnd(25)} UNRESOLVABLE (${why})`);
|
||||
continue;
|
||||
}
|
||||
console.log(` ${repoPath.padEnd(25)} MISSING (no entry in the registry)`);
|
||||
continue;
|
||||
}
|
||||
const idx = row.indexStale
|
||||
|
|
@ -138,6 +165,26 @@ export function registerGroupCommands(program: Command): void {
|
|||
const ctr = row.contractsStale ? ' CONTRACTS_STALE' : '';
|
||||
console.log(` ${repoPath.padEnd(25)} ${idx}${ctr}`);
|
||||
}
|
||||
// `undefined` and `[]` are different answers here: a registry written
|
||||
// before this was tracked has no opinion, while an empty array is a
|
||||
// measurement. Printing nothing for both would let an unmeasured sync
|
||||
// read as evidence that every index opened cleanly.
|
||||
//
|
||||
// `undefined` covers two ways of not knowing — the field is absent, or
|
||||
// it held something that was not a list of repo paths and `getStatus`
|
||||
// declined to guess. Naming only the first would make a corrupt
|
||||
// registry read as a merely old one, which is the same shape of wrong
|
||||
// answer this command exists to stop giving.
|
||||
const unreadable = st.unreadableRepos;
|
||||
if (unreadable === undefined) {
|
||||
console.log(
|
||||
`\n Last sync unreadable repos: not recorded` +
|
||||
`\n (the registry predates this field, or its value could not be read)` +
|
||||
`\n Re-run \`gitnexus group sync\` to record it.`,
|
||||
);
|
||||
} else if (unreadable.length > 0) {
|
||||
console.log(`\n Last sync unreadable repos: ${unreadable.join(', ')}`);
|
||||
}
|
||||
if ((st.missingRepos || []).length > 0) {
|
||||
console.log(`\n Last sync missing repos: ${st.missingRepos!.join(', ')}`);
|
||||
}
|
||||
|
|
@ -158,30 +205,93 @@ export function registerGroupCommands(program: Command): void {
|
|||
const { getGroupDir, getDefaultGitnexusDir } = await import('../core/group/storage.js');
|
||||
const { loadGroupConfig } = await import('../core/group/config-parser.js');
|
||||
const { syncGroup } = await import('../core/group/sync.js');
|
||||
const { GroupSyncLockError } = await import('../core/group/group-lock.js');
|
||||
|
||||
const groupDir = getGroupDir(getDefaultGitnexusDir(), name);
|
||||
const config = await loadGroupConfig(groupDir);
|
||||
|
||||
console.log(`Syncing group "${name}" (${Object.keys(config.repos).length} repos)...\n`);
|
||||
|
||||
const result = await syncGroup(config, {
|
||||
groupDir,
|
||||
allowStale: Boolean(opts.allowStale),
|
||||
verbose: Boolean(opts.verbose),
|
||||
skipEmbeddings: Boolean(opts.skipEmbeddings),
|
||||
exactOnly: Boolean(opts.exactOnly),
|
||||
});
|
||||
let result: Awaited<ReturnType<typeof syncGroup>>;
|
||||
try {
|
||||
result = await syncGroup(config, {
|
||||
groupDir,
|
||||
allowStale: Boolean(opts.allowStale),
|
||||
verbose: Boolean(opts.verbose),
|
||||
skipEmbeddings: Boolean(opts.skipEmbeddings),
|
||||
exactOnly: Boolean(opts.exactOnly),
|
||||
});
|
||||
} catch (err) {
|
||||
// A sync that could not take the group's lock did NOT run and wrote
|
||||
// nothing (R9 fails closed). That is an operator-actionable outcome, not
|
||||
// a crash, so report it as a failed command rather than letting it
|
||||
// surface as an unhandled rejection with a stack trace — commander's
|
||||
// async actions have no error handler, so an uncaught throw here would
|
||||
// print exactly that.
|
||||
if (!(err instanceof GroupSyncLockError)) throw err;
|
||||
logger.error(`⚠️ Did not sync group "${name}": ${err.message}`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (opts.json) {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} else {
|
||||
// Repos we could not read are the most likely explanation for a small
|
||||
// or empty contract count, so they are reported before the counts —
|
||||
// otherwise a run that read nothing looks exactly like a clean run.
|
||||
if (result.unreadableRepos.length > 0) {
|
||||
// No "re-run with GITNEXUS_LOG_LEVEL=warn" hint: the default level is
|
||||
// `info`, and pino emits `warn` (40) at `info` (30), so the reason was
|
||||
// already printed by this same run — raising the level to `warn` would
|
||||
// only suppress the surrounding `info` output.
|
||||
console.log(
|
||||
`\n ⚠️ Could not extract contracts from: ${result.unreadableRepos.join(', ')}` +
|
||||
`\n None of their contracts are included in this sync (the warning above says why),` +
|
||||
`\n or check \`gitnexus doctor\` in the affected repo.`,
|
||||
);
|
||||
}
|
||||
if (result.missingRepos.length > 0) {
|
||||
console.log(
|
||||
`\n ⚠️ Not found in the registry: ${result.missingRepos.join(', ')}` +
|
||||
`\n Index them with \`gitnexus analyze\`, or remove them from group.yaml.`,
|
||||
);
|
||||
}
|
||||
console.log(`\nMatching cascade:`);
|
||||
const exactLinks = result.crossLinks.filter((l) => l.matchType === 'exact');
|
||||
console.log(` exact: ${exactLinks.length} cross-links (confidence 1.0)`);
|
||||
console.log(` unmatched: ${result.unmatched.length} contracts`);
|
||||
console.log(
|
||||
`\nWrote contracts.json (${result.contracts.length} contracts, ${result.crossLinks.length} cross-links)`,
|
||||
);
|
||||
// Driven by what actually happened to the file. This line used to be
|
||||
// unconditional, so a run that deliberately preserved the previous
|
||||
// registry still announced `Wrote contracts.json (0 contracts, 0
|
||||
// cross-links)` — a confident false statement about persisted state, on
|
||||
// the exact path this command exists to make legible.
|
||||
// Exhaustive by construction: a `Record` keyed on the union means a
|
||||
// new outcome fails the build here instead of printing nothing, which
|
||||
// is what previously pushed a distinct state into `preserved` and made
|
||||
// this summary false on one of the two branches it then covered.
|
||||
const OUTCOME_LINE: Record<RegistryWriteOutcome, string | null> = {
|
||||
written:
|
||||
`\nWrote contracts.json (${result.contracts.length} contracts, ` +
|
||||
`${result.crossLinks.length} cross-links)`,
|
||||
preserved:
|
||||
`\nKept the previous contracts.json — no repo in this group could be read.` +
|
||||
`\n Its contracts and cross-links are unchanged; only the unreadable/missing` +
|
||||
`\n repo lists were refreshed to describe THIS run. Fix the repos above and re-run.`,
|
||||
superseded:
|
||||
`\nDid NOT touch contracts.json — no repo in this group could be read, and another` +
|
||||
`\n sync replaced the file while this one waited for the group lock. That sync's` +
|
||||
`\n result stands and this run's repo lists were NOT recorded: they describe a` +
|
||||
`\n group state older than what is on disk. Fix the repos above and re-run.`,
|
||||
'no-prior-registry':
|
||||
`\nDid NOT write contracts.json — no repo in this group could be read,` +
|
||||
`\n and there is no previous contracts.json to fall back on. Fix the repos` +
|
||||
`\n above and re-run.`,
|
||||
// Nothing to say: the caller asked for no write.
|
||||
'not-attempted': null,
|
||||
};
|
||||
const line = OUTCOME_LINE[result.registryOutcome];
|
||||
if (line) console.log(line);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -370,7 +480,7 @@ export function registerGroupCommands(program: Command): void {
|
|||
return;
|
||||
}
|
||||
|
||||
const { contracts, crossLinks } = raw as {
|
||||
const { contracts, crossLinks, truncated, unreadableRepos, missingRepos } = raw as {
|
||||
contracts: Array<{
|
||||
role: string;
|
||||
contractId: string;
|
||||
|
|
@ -384,10 +494,19 @@ export function registerGroupCommands(program: Command): void {
|
|||
confidence: number;
|
||||
contractId: string;
|
||||
}>;
|
||||
truncated?: boolean;
|
||||
unreadableRepos?: string[];
|
||||
missingRepos?: string[];
|
||||
};
|
||||
|
||||
if (opts.json) {
|
||||
console.log(JSON.stringify({ contracts, crossLinks }, null, 2));
|
||||
// The whole payload, not a re-serialized subset. Destructuring the two
|
||||
// fields this command happens to print and rebuilding an object from
|
||||
// them dropped everything else the service returned — which is how the
|
||||
// completeness fields were invisible here while the MCP tool carried
|
||||
// them. Printing `raw` means a field added to the service reaches
|
||||
// `--json` without a matching edit in this file.
|
||||
console.log(JSON.stringify(raw, null, 2));
|
||||
} else {
|
||||
console.log(`Contracts (${contracts.length}):`);
|
||||
for (const c of contracts) {
|
||||
|
|
@ -399,6 +518,19 @@ export function registerGroupCommands(program: Command): void {
|
|||
` ${l.from.repo} -> ${l.to.repo} [${l.matchType}, conf=${l.confidence}] ${l.contractId}`,
|
||||
);
|
||||
}
|
||||
if (truncated) {
|
||||
// Counts above are a floor, not a census. Name the repos when the
|
||||
// registry recorded them, and say so plainly when it did not — a
|
||||
// listing that cannot say what it is missing is still incomplete.
|
||||
const absent = [...(unreadableRepos ?? []), ...(missingRepos ?? [])];
|
||||
console.log(
|
||||
absent.length > 0
|
||||
? `\n⚠️ This listing is incomplete: the last sync could not account for ${absent.join(', ')}.` +
|
||||
`\n Contracts from those repos are absent, so the counts above are a lower bound.`
|
||||
: `\n⚠️ This listing is incomplete: the last sync did not record which repos it could` +
|
||||
`\n read, so the counts above are a lower bound. Re-run group sync.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await backend.dispose().catch(() => {});
|
||||
|
|
|
|||
107
gitnexus/src/core/group/REVIEW-FINDINGS-MAP.md
Normal file
107
gitnexus/src/core/group/REVIEW-FINDINGS-MAP.md
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
# Review findings → commits (PR #3012)
|
||||
|
||||
Every finding raised in review of this PR, and the commit that closes it. The
|
||||
Definition of Done claims each finding has exactly one commit and that reverting
|
||||
that commit reintroduces that finding and no other; this is what makes the claim
|
||||
checkable without the reviewer's report in hand.
|
||||
|
||||
**Not under `docs/`** — that path is gitignored, so a map written there would
|
||||
never reach the PR and nobody but its author could perform the audit. It lives
|
||||
beside the code it describes, as `PIPELINE.md` does.
|
||||
|
||||
## Revert contract
|
||||
|
||||
Revertability is **dependency-aware**. Where one commit extracts a helper that
|
||||
later commits consume, reverting the helper alone does not build. The contract
|
||||
is: reverting a commit reintroduces its own finding and no other _finding_, with
|
||||
its prerequisite commits retained.
|
||||
|
||||
One coupled set exists:
|
||||
|
||||
| Set | Commits | Why coupled |
|
||||
| -------------------------- | --------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
||||
| Shared completeness helper | `4c203ac7b` ← `79f6f5bcb`, `0fe6fc9d4`, `dbc3953b0` | The three consumers call `crossRepoCompleteness`; reverting it alone breaks the build. |
|
||||
|
||||
## Primary findings
|
||||
|
||||
| # | Finding | Commit |
|
||||
| --- | -------------------------------------------------------------------------------- | ----------- |
|
||||
| 1 | Malformed `meta.json` crashes cross-repo impact and leaks the bridge handle | `27b0069f2` |
|
||||
| 2 | Unreadable repos still contribute contracts through deferred manifest resolution | `7037e8441` |
|
||||
| 3 | Strict read accepts a registry row that cannot identify a repo | `5245b22d7` |
|
||||
| 4 | Unstamped bridge metadata is trusted without any check | `94f2a8757` |
|
||||
| 5 | A subgroup-scoped query is marked incomplete by repos it excluded | `79f6f5bcb` |
|
||||
| 6 | The preserved registry and the bridge disagree about the same sync | `4676abf03` |
|
||||
| 7 | Three surfaces compute completeness three different ways | `4c203ac7b` |
|
||||
| 8 | `group_contracts` has no channel for its own completeness | `0fe6fc9d4` |
|
||||
| 9 | `group status` cannot tell a missing entry from an unreadable registry | `a12b846c9` |
|
||||
| 10 | The sync summary describes a write that did not happen that way | `5a668455c` |
|
||||
| 11 | The total-failure log promises preservation where there is nothing to preserve | `c4b356b29` |
|
||||
| 12 | The bridge-failure warning promises a truncation the code never reports | `1df79bb9a` |
|
||||
| 13 | Two concurrent syncs of one group lose each other's writes | `4f07359bf` |
|
||||
| 14 | The bridge swap needs the lock its caller already holds | `3b6215862` |
|
||||
| 15 | The byte guard misses most tracked text files, and all extensionless ones | `07bf8be75` |
|
||||
| 16 | The byte guard reads the vendored grammar tree it does not need to judge | `3ef831a0a` |
|
||||
| 17 | The strict-read test cannot see which registry read ran | `eccc3c682` |
|
||||
| 18 | The CLI branches this PR introduced have no assertions | `535d2ad29` |
|
||||
| 19 | The MCP payloads have no assertions | `2c253b4a8` |
|
||||
| 20 | Corrupt-registry errors quote the file's bytes, credentials included | `24ba2a537` |
|
||||
| 21 | The mtime pairing's limits are recorded nowhere a reader will look | `ca0aca106` |
|
||||
| 22 | The bridge-input docstring narrows what `unreadableRepos` means | `8c930f470` |
|
||||
| 23 | The strict-read docstring's call-site count is wrong | `a95838954` |
|
||||
| 24 | Contract staging crashes on the engine's argument limit | `57eac7558` |
|
||||
| 25 | The sync tool's description names two of three reachable outcomes | `8bfd1a6ab` |
|
||||
| 26 | The impact tool and status resource do not explain incompleteness | `dbc3953b0` |
|
||||
| 27 | A lock timeout blames an `analyze` it cannot establish | `2d2a0119e` |
|
||||
| 28 | A losing sync downgrades the one that beat it to the lock | `e407f05cf` |
|
||||
|
||||
## Findings raised in review and deliberately not implemented as suggested
|
||||
|
||||
| Finding | Suggested fix | What shipped, and why |
|
||||
| ---------------------------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| Unstamped metadata is trusted | Treat every absent stamp as incomplete | Rejected. It would mark every pre-existing bridge a lower bound until re-synced — a repo-wide regression traded for a narrow window. The write-order pairing in `94f2a8757` is the narrower fix. |
|
||||
| Stale bridge signal after a failed write | Re-stamp the metadata so the warning's promise becomes true | Rejected. Re-stamping recreates the metadata/database mis-pairing that stamping exists to prevent. `1df79bb9a` corrects the warning instead. |
|
||||
| Strict row gate | Require all three fields non-blank | Narrowed to `name` and `storagePath`. This gate rejects the whole registry, which is machine-wide, so a field tightened past what identification needs lets one blank value break every group sync on the machine. |
|
||||
|
||||
## Found during execution, not in the review
|
||||
|
||||
| What | Commit |
|
||||
| --------------------------------------------------------------------------------------------- | ----------- |
|
||||
| A half-written bridge stamp read as a verified match (found by the repo's own contract check) | `066f2d802` |
|
||||
| `readBridgeMeta`'s widened return type blocked the merge on contract drift | `a9d281dd4` |
|
||||
| `group contracts --json` discarded every field it did not re-serialize | `b7753575d` |
|
||||
| `sync.ts` renders as a binary diff because the base blob carries a NUL | `1667c24b4` |
|
||||
|
||||
## Corrections to the plan, found while executing it
|
||||
|
||||
Recorded because each was a claim in the plan that the code contradicted.
|
||||
|
||||
| Claim | Reality |
|
||||
| ------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| The strict gate should require the fields "the resolution path consumes" | `defaultResolveHandle` **does** consume `path`. The distinction is what _identifies_ the repo. |
|
||||
| Pass the trace's two endpoint repos as the scope predicate | A destination trace declares no `to`. Narrowing to `from` would report an unreadable provider as "no outgoing link". |
|
||||
| Filter the incomplete set by the subgroup prefix | The query's own repo must stay in scope, or an unreadable origin becomes a confident "nothing depends on this". |
|
||||
| `group status`'s third failure mode is a row that resolves but cannot be opened | Unreachable — `loadMeta` returns `null` on every error and `checkStaleness` catches everything. The reachable case is `resolveRepo` throwing. |
|
||||
| The mtime rule can only demote pairs already broken | False. `cp -r` and `rsync` without `-t` demote an intact pair. Recorded at the code in `ca0aca106`. |
|
||||
| `.scm` files are "edited constantly" here | Every tracked `.scm` is vendored. This repo writes tree-sitter queries inline in TypeScript. |
|
||||
|
||||
## Residual risks, recorded rather than closed
|
||||
|
||||
- **Credentials in the registry.** HTTPS remote URLs are persisted with their
|
||||
userinfo intact. `24ba2a537` stops one channel echoing them; it does not stop
|
||||
them being written. Pre-existing, tracked separately.
|
||||
- **`readRegistryFile`'s read error.** The ENOENT-guarded outer catch still
|
||||
rethrows the raw `fs.readFile` error into `unresolvableReason`. Node embeds
|
||||
the path, not file contents, so no registry bytes leak — but it is the one
|
||||
remaining foreign error object on that path.
|
||||
- **Abstract-socket lock scope.** Linux abstract sockets are
|
||||
network-namespace-scoped, so two containers sharing a bind-mounted group
|
||||
directory do not contend unless the file backend is forced. Recorded at
|
||||
`group-lock.ts`.
|
||||
- **Scope filter at depth > 1.** The declared-scope intersection is sound only
|
||||
while `MAX_SUPPORTED_CROSS_DEPTH` is 1. At depth 2 an out-of-scope repo can
|
||||
sit between two in-scope ones. Recorded at the intersection site.
|
||||
- **R14 is unmet on this PR.** `.gitattributes` makes TypeScript diffs render as
|
||||
text, and it works locally — but GitHub resolves the attribute from the base
|
||||
side, which does not carry it. `sync.ts` renders as binary in this PR's web
|
||||
view and will render as text for every PR after this one merges.
|
||||
|
|
@ -5,12 +5,14 @@ import lbug from '@ladybugdb/core';
|
|||
import type { LbugValue } from '@ladybugdb/core';
|
||||
import type { BridgeHandle, BridgeMeta, StoredContract, CrossLink, RepoSnapshot } from './types.js';
|
||||
import { BRIDGE_SCHEMA_QUERIES, BRIDGE_SCHEMA_VERSION } from './bridge-schema.js';
|
||||
import { recordedRepoList } from './completeness.js';
|
||||
import {
|
||||
closeLbugConnection,
|
||||
openLbugConnection,
|
||||
type LbugConnectionHandle,
|
||||
} from '../lbug/lbug-config.js';
|
||||
import { dedupeContracts, dedupeCrossLinks } from './normalization.js';
|
||||
import { withGroupSyncLock } from './group-lock.js';
|
||||
import { createLogger } from '../logger.js';
|
||||
import { retryRename, writeFileAtomic } from '../../storage/fs-atomic.js';
|
||||
|
||||
|
|
@ -650,13 +652,296 @@ export async function writeBridgeMeta(groupDir: string, meta: BridgeMeta): Promi
|
|||
await writeFileAtomic(path.join(groupDir, 'meta.json'), JSON.stringify(meta, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Does `meta` still describe the `bridge.lbug` sitting next to it?
|
||||
*
|
||||
* `writeBridge` stamps the database's size and mtime into the metadata it
|
||||
* writes, so a metadata file left over from an earlier sync cannot match a
|
||||
* database that was replaced after it. Callers whose answer depends on the
|
||||
* metadata being true of THIS database (cross-repo impact reads completeness
|
||||
* from it) must not treat a mismatch as fact.
|
||||
*
|
||||
* When BOTH halves of the stamp are absent the metadata predates stamping, and
|
||||
* it is judged on the write order of the two files instead — see
|
||||
* {@link unstampedMetaPairsByWriteOrder}. Failing every unstamped metadata
|
||||
* closed would mark all pre-existing bridges as incomplete until re-synced,
|
||||
* trading a narrow window for a repo-wide regression; accepting them all hands
|
||||
* back "verified" for the very window this pairing exists to catch.
|
||||
*
|
||||
* A stamp is a PAIR, so exactly one half present is rejected rather than waved
|
||||
* through. That is not the legacy shape: something wrote a stamp and did not
|
||||
* finish, which is the very condition stamping was added to detect. Joining the
|
||||
* two `undefined` checks with `||` returned "verified" for precisely the shape
|
||||
* that most deserves suspicion.
|
||||
*
|
||||
* Returns `false` when the database itself cannot be stat'd, on either path,
|
||||
* since metadata describing a file that is not there describes nothing.
|
||||
*
|
||||
* The checks are ORDERED by how strong their evidence is, strongest first, and
|
||||
* each later one is reached only because every earlier one had nothing to say.
|
||||
* `provenanceUnknown` therefore comes first: a metadata file whose own writer
|
||||
* says it cannot vouch for the database beside it has settled the question, and
|
||||
* neither the stamp nor the write-order heuristic may overturn that.
|
||||
*
|
||||
* The marker is not decoration. `refreshPreservedBridgeMeta` rewrites this file
|
||||
* atomically without touching the database, which leaves `meta.mtime` newer —
|
||||
* the write order a paired write produces, and the one the unstamped branch
|
||||
* ACCEPTS. Reading the marker after that branch (or not at all) hands back
|
||||
* "verified" for a pair the same code path had just found broken.
|
||||
*/
|
||||
export async function bridgeMetaMatchesFile(groupDir: string, meta: BridgeMeta): Promise<boolean> {
|
||||
if (meta.provenanceUnknown) return false;
|
||||
const stampedSize = meta.bridgeSize !== undefined;
|
||||
const stampedMtime = meta.bridgeMtimeMs !== undefined;
|
||||
if (!stampedSize && !stampedMtime) return unstampedMetaPairsByWriteOrder(groupDir);
|
||||
if (!stampedSize || !stampedMtime) return false;
|
||||
try {
|
||||
const stat = await fsp.stat(path.join(groupDir, 'bridge.lbug'));
|
||||
return stat.size === meta.bridgeSize && stat.mtimeMs === meta.bridgeMtimeMs;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Could the unstamped `meta.json` plausibly have been written by the sync that
|
||||
* put this `bridge.lbug` beside it?
|
||||
*
|
||||
* `writeBridge` renames the database into place and writes the metadata AFTER,
|
||||
* so `meta.mtime >= db.mtime` holds for any pair written together — including
|
||||
* pairs written by builds from before the stamp existed, which is what makes
|
||||
* this usable as back-compat rather than a repo-wide "re-sync everything".
|
||||
* The only way to reach a database strictly NEWER than the metadata beside it
|
||||
* is a swap whose metadata write did not land: the stale-meta-beside-a-new-
|
||||
* database window, whose completeness `runGroupImpact` would otherwise spend as
|
||||
* fact.
|
||||
*
|
||||
* This is a HEURISTIC ON WRITE ORDER, not proof of provenance. It answers "were
|
||||
* these two written in the order a successful sync writes them?", and treats
|
||||
* that as a proxy for "do these two belong together". It is wrong in two
|
||||
* directions, and neither is theoretical:
|
||||
* - FALSE ACCEPT, from a non-monotonic wall clock. `mtimeMs` is realtime, not
|
||||
* monotonic, so an NTP step backwards, a VM snapshot restore or container
|
||||
* clock skew between the database write and the metadata write can leave a
|
||||
* genuinely mis-paired set reading as ordered. Anything that touches the
|
||||
* stale metadata after a swap does the same — a restore from backup, an
|
||||
* editor save, a copy that preserves only the database's times. The STAMP
|
||||
* is what actually closes this; a pair that has one never reaches here.
|
||||
*
|
||||
* Coarse filesystem mtime granularity is NOT this hazard, despite looking
|
||||
* like it: it collapses a pair written together to equal times, and equal
|
||||
* is accepted, which is the correct verdict for that pair.
|
||||
*
|
||||
* - FALSE REJECT, from anything that rewrites the database's mtime after the
|
||||
* metadata's — `cp -r`, `rsync` without `-t`, a machine move, a restore
|
||||
* that replays files in directory order. An intact legacy pair is then
|
||||
* demoted to a lower bound and stays there until the next successful sync
|
||||
* re-stamps it; there is no other recovery, because nothing on the read
|
||||
* path can distinguish it from the swap window it is imitating.
|
||||
*
|
||||
* This direction is the safe one — it degrades an answer to a floor rather
|
||||
* than vouching for one — but it is a real, reachable cost, not a
|
||||
* theoretical one, and it is NOT true that the rule can only ever demote
|
||||
* pairs that were already broken.
|
||||
*
|
||||
* Equality counts as paired. On a filesystem with coarse mtime granularity both
|
||||
* writes land in the same tick, and demanding a strictly newer metadata file
|
||||
* would reject every legacy bridge there for a reason that is about the
|
||||
* filesystem rather than about the bridge.
|
||||
*
|
||||
* A timestamp that cannot be measured is no match, the same convention the
|
||||
* read-only handle cache applies to a bridge it could not stat: a comparison
|
||||
* that could not be made is not a comparison that succeeded.
|
||||
*/
|
||||
async function unstampedMetaPairsByWriteOrder(groupDir: string): Promise<boolean> {
|
||||
try {
|
||||
const [dbStat, metaStat] = await Promise.all([
|
||||
fsp.stat(path.join(groupDir, 'bridge.lbug')),
|
||||
fsp.stat(path.join(groupDir, 'meta.json')),
|
||||
]);
|
||||
return metaStat.mtimeMs >= dbStat.mtimeMs;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read `meta.json`, validating the SHAPE of what it holds.
|
||||
*
|
||||
* The read and the parse have always been guarded — an absent or unparseable
|
||||
* file answers `version: 0`, which every caller already treats as "no
|
||||
* provenance". What was not guarded is a file that parses into something that
|
||||
* is not this shape: `runGroupImpact` spread both repo lists directly into a
|
||||
* `Set`, so a non-iterable there threw a TypeError out of the whole cross-repo
|
||||
* query, from a point where the bridge lease had been taken and not yet
|
||||
* released. A malformed file is a reason to answer "provenance unknown", never
|
||||
* a reason to crash the question.
|
||||
*/
|
||||
export async function readBridgeMeta(groupDir: string): Promise<BridgeMeta> {
|
||||
const unreadable: BridgeMeta = { version: 0, generatedAt: '', missingRepos: [] };
|
||||
let parsed: unknown;
|
||||
try {
|
||||
const content = await fsp.readFile(path.join(groupDir, 'meta.json'), 'utf-8');
|
||||
return JSON.parse(content) as BridgeMeta;
|
||||
parsed = JSON.parse(content);
|
||||
} catch {
|
||||
return { version: 0, generatedAt: '', missingRepos: [] };
|
||||
return unreadable;
|
||||
}
|
||||
// `JSON.parse` succeeds on `null`, `7` and `[]` too, and none of them are
|
||||
// metadata. Reading `.version` off the first of those is a thrown TypeError;
|
||||
// reading it off the others silently yields `undefined`, which passes the
|
||||
// version gate as if the bridge had been vouched for.
|
||||
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return unreadable;
|
||||
|
||||
const raw = parsed as Partial<BridgeMeta>;
|
||||
const missingRepos = recordedRepoList(raw.missingRepos);
|
||||
const unreadableRepos = recordedRepoList(raw.unreadableRepos);
|
||||
// Each list is judged on its own: a file whose `unreadableRepos` is garbage
|
||||
// can still carry a `missingRepos` that was genuinely measured, and throwing
|
||||
// that away would turn one unknown into two.
|
||||
const repoListsUnreadable =
|
||||
(raw.missingRepos !== undefined && missingRepos === undefined) ||
|
||||
(raw.unreadableRepos !== undefined && unreadableRepos === undefined);
|
||||
|
||||
const meta: BridgeMeta = {
|
||||
...raw,
|
||||
// A version that is not a number cannot be compared against
|
||||
// BRIDGE_SCHEMA_VERSION; `0` is this file's existing word for "provenance
|
||||
// unknown", which is exactly what such a file gives us.
|
||||
// `0` is this file's word for "no provenance". A version that is not a
|
||||
// positive integer is not a schema version, and letting one through splits
|
||||
// the four gates that read this field: `ensureBridgeReady` and
|
||||
// `openBridgeDbReadOnly` both compare `> 0 && !== CURRENT` and would open
|
||||
// the bridge, `bridgeExists` compares `=== 0 || === CURRENT` and would say
|
||||
// it is not there, and `bridgeProvenanceUnknown` compares `=== 0` and would
|
||||
// call the answer complete. Normalizing here keeps all four agreeing
|
||||
// instead of teaching each one the same new case.
|
||||
version:
|
||||
Number.isInteger(raw.version) && (raw.version as number) > 0 ? (raw.version as number) : 0,
|
||||
generatedAt: typeof raw.generatedAt === 'string' ? raw.generatedAt : '',
|
||||
missingRepos: missingRepos ?? [],
|
||||
};
|
||||
// Absent, not empty. `unreadableRepos` is optional and "not recorded" is a
|
||||
// distinct state from "measured none", so an unusable value is dropped rather
|
||||
// than carried through — `repoListsUnreadable` is what records that something
|
||||
// was there and could not be read.
|
||||
if (unreadableRepos) meta.unreadableRepos = unreadableRepos;
|
||||
else delete meta.unreadableRepos;
|
||||
if (repoListsUnreadable) meta.repoListsUnreadable = true;
|
||||
return meta;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* refreshPreservedBridgeMeta */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* What a refresh did to `meta.json`.
|
||||
*
|
||||
* - `restamped` — the pair still matched, so the lists were refreshed
|
||||
* and the stamp re-taken from the database on disk.
|
||||
* - `provenance-unknown` — the pair did NOT match (or there is no database to
|
||||
* match), so the lists were refreshed and the metadata
|
||||
* marked as unable to vouch for the file beside it.
|
||||
* - `no-bridge` — neither `meta.json` nor `bridge.lbug` exists, so
|
||||
* there is no pair to keep honest and nothing written.
|
||||
*/
|
||||
export type PreservedBridgeMetaOutcome = 'restamped' | 'provenance-unknown' | 'no-bridge';
|
||||
|
||||
async function fileExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await fsp.access(filePath);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bring `meta.json`'s diagnostic lists up to date with a sync that PRESERVED
|
||||
* the bridge instead of rebuilding it, without ever making the metadata claim
|
||||
* more about the database than it did before.
|
||||
*
|
||||
* `syncGroup`'s total-failure path keeps the previous run's contracts and
|
||||
* deliberately leaves `bridge.lbug` alone — the contracts that bridge holds are
|
||||
* the ones being preserved. But `runGroupImpact` reads completeness from
|
||||
* `meta.json`, not from `contracts.json`, so leaving the metadata alone too left
|
||||
* the two files telling different stories: the registry said "this sync could
|
||||
* not read svc/users" while a cross-repo query answered "complete, nothing
|
||||
* depends on this" (R4/R6).
|
||||
*
|
||||
* The refresh is the whole difficulty. It rewrites `meta.json` atomically, so
|
||||
* the file's mtime becomes now while the database's stays old — which is the
|
||||
* write order a paired write produces, and precisely what
|
||||
* `unstampedMetaPairsByWriteOrder` accepts. Three rules follow, and each of them
|
||||
* is load-bearing:
|
||||
*
|
||||
* 1. Ask `bridgeMetaMatchesFile` FIRST, on the file as it stands. After the
|
||||
* write the question is unanswerable, because the write is what destroys
|
||||
* the evidence.
|
||||
* 2. Re-stamp only when that answer was yes. Re-stamping a pair that already
|
||||
* failed would MANUFACTURE the provenance the failure just denied — the
|
||||
* same metadata/database mis-pairing stamping exists to prevent (KTD6).
|
||||
* 3. When it was no, record `provenanceUnknown` explicitly and carry the
|
||||
* existing stamp fields through verbatim. Writing "no stamp" instead is
|
||||
* worse, not better: an unstamped file is judged on the two file times,
|
||||
* and this write has just put them in the accepting order.
|
||||
*
|
||||
* Nothing here opens, reads, or writes the database. The only `stat` of it
|
||||
* happens on the branch where the pair was just verified.
|
||||
*
|
||||
* NOT SPLIT into locked/unlocked halves the way {@link writeBridge} is, and
|
||||
* deliberately. Its one caller is `syncGroup`'s preserve branch, which is
|
||||
* already inside `withGroupSyncLock` — so this write is ALREADY serialized
|
||||
* against every other sync of the group, and taking the lock here would be the
|
||||
* second acquisition of a non-reentrant primitive that the split exists to
|
||||
* avoid. An acquiring wrapper would therefore have zero production callers,
|
||||
* and no test calls this function at all: it would be dead code standing in for
|
||||
* a guarantee the caller already provides. If a caller outside the critical
|
||||
* section ever appears, it needs the same treatment `writeBridge` got — a
|
||||
* wrapper, not a lock moved down here.
|
||||
*/
|
||||
export async function refreshPreservedBridgeMeta(
|
||||
groupDir: string,
|
||||
diagnostics: { missingRepos: string[]; unreadableRepos: string[] },
|
||||
): Promise<PreservedBridgeMetaOutcome> {
|
||||
const dbPath = path.join(groupDir, 'bridge.lbug');
|
||||
const [metaOnDisk, dbOnDisk] = await Promise.all([
|
||||
fileExists(path.join(groupDir, 'meta.json')),
|
||||
fileExists(dbPath),
|
||||
]);
|
||||
// Nothing on either side of the pair. `readBridgeMeta` already answers
|
||||
// `version: 0` — provenance unknown — for an absent file, so a file written
|
||||
// here would say what the absence already says while inventing state for a
|
||||
// bridge that has never existed.
|
||||
if (!metaOnDisk && !dbOnDisk) return 'no-bridge';
|
||||
|
||||
const existing = await readBridgeMeta(groupDir);
|
||||
const paired = await bridgeMetaMatchesFile(groupDir, existing);
|
||||
|
||||
const refreshed: BridgeMeta = { ...existing, ...diagnostics };
|
||||
// NEVER PERSISTED (see `BridgeMeta`): both are things a READER computes ABOUT
|
||||
// a file, and this is the first code in the repo that reads metadata and
|
||||
// writes it back. `pairedWithDatabase` is the poisonous one — persisted, it
|
||||
// would tell every future reader that the pair had been verified.
|
||||
delete refreshed.repoListsUnreadable;
|
||||
delete refreshed.pairedWithDatabase;
|
||||
|
||||
if (paired) {
|
||||
const stat = await fsp.stat(dbPath).catch(() => null);
|
||||
if (stat) {
|
||||
refreshed.bridgeSize = stat.size;
|
||||
refreshed.bridgeMtimeMs = stat.mtimeMs;
|
||||
await writeBridgeMeta(groupDir, refreshed);
|
||||
return 'restamped';
|
||||
}
|
||||
// The database disappeared between the pairing check and this stat. There
|
||||
// is nothing left to stamp, so fall through and say so rather than write a
|
||||
// stamp describing a file that is gone.
|
||||
}
|
||||
|
||||
refreshed.provenanceUnknown = true;
|
||||
await writeBridgeMeta(groupDir, refreshed);
|
||||
return 'provenance-unknown';
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
|
@ -668,6 +953,22 @@ export interface WriteBridgeInput {
|
|||
crossLinks: CrossLink[];
|
||||
repoSnapshots: Record<string, RepoSnapshot>;
|
||||
missingRepos: string[];
|
||||
/**
|
||||
* Repos this sync could not extract from — see
|
||||
* `ContractRegistry.unreadableRepos` for the full definition, which this
|
||||
* field carries unchanged.
|
||||
*
|
||||
* Deliberately not restated here. The narrower wording this once had ("whose
|
||||
* index could not be opened") described one of the two causes and silently
|
||||
* excluded the other, an extractor that threw partway through — so the same
|
||||
* field meant one thing on the registry, another on the bridge input, and a
|
||||
* third on the result. One definition, referenced twice, cannot drift.
|
||||
*
|
||||
* Recorded in meta.json so cross-repo impact can tell "nothing depends on
|
||||
* this" from "we could not look": the bridge built here is missing every
|
||||
* contract those repos own.
|
||||
*/
|
||||
unreadableRepos?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -702,7 +1003,33 @@ function errMessage(err: unknown): string {
|
|||
}
|
||||
}
|
||||
|
||||
export async function writeBridge(
|
||||
/**
|
||||
* Rebuild `bridge.lbug` and its `meta.json`, ASSUMING THE CALLER ALREADY HOLDS
|
||||
* THE GROUP SYNC LOCK for `groupDir` (R9).
|
||||
*
|
||||
* PRECONDITION — the group lock is held. There is exactly one production call
|
||||
* site, `syncGroup` in sync.ts, and it is already inside
|
||||
* `withGroupSyncLock(groupDir, …)` when it gets here. Enforced by this comment
|
||||
* rather than by a type, matching `registerRepoUnlocked` / `withRegistryLock`
|
||||
* in repo-manager.ts, which splits the same shape for the same reason.
|
||||
*
|
||||
* WHY THE SPLIT EXISTS AT ALL. The swap this function performs — old database
|
||||
* aside, temp database into place, then `meta.json` written as a SECOND
|
||||
* operation — is the write two concurrent syncs can interleave into a pairing
|
||||
* that never existed: one sync's metadata beside the other's database. That
|
||||
* needs mutual exclusion. But taking the lock HERE would be a second
|
||||
* acquisition of a non-reentrant primitive inside a region that already holds
|
||||
* it, and it would hang every single sync on the happy path, not some rare
|
||||
* interleave. So the exclusion is the caller's, and this function only states
|
||||
* the precondition. {@link writeBridge} is the acquiring wrapper for callers
|
||||
* who are not already inside that region.
|
||||
*
|
||||
* SCOPE — writer-writer only. The reader-side promotion of a leftover
|
||||
* `bridge.lbug.bak` runs on ordinary reads, outside anybody's critical section;
|
||||
* `bridgeMetaMatchesFile` remains the reader's defense there and is not
|
||||
* replaced by this lock.
|
||||
*/
|
||||
export async function writeBridgeUnlocked(
|
||||
groupDir: string,
|
||||
input: WriteBridgeInput,
|
||||
): Promise<WriteBridgeReport> {
|
||||
|
|
@ -962,11 +1289,39 @@ export async function writeBridge(
|
|||
}
|
||||
await removeLbugFile(bakPath);
|
||||
|
||||
// 4. Write meta.json
|
||||
// 4. Write the new meta.json, STAMPED WITH THE FILE IT DESCRIBES.
|
||||
//
|
||||
// meta.json carries the bridge's completeness, and since #3011 that is
|
||||
// load-bearing: `runGroupImpact` folds `unreadableRepos ∪ missingRepos`
|
||||
// into its truncation fields. The swap above and this write are two
|
||||
// operations, so a sync that stops between them leaves the previous sync's
|
||||
// meta beside a new database — and reading that as fact is a confidently
|
||||
// wrong answer about the one thing this channel exists to make legible.
|
||||
//
|
||||
// Deleting the old meta before the swap would decide which way that window
|
||||
// fails, but at an unacceptable price: the rename of the old database is
|
||||
// wrapped in a catch that also swallows a FAILED rename (a held read-only
|
||||
// handle does this on Windows), so `writeBridge` can throw with the old,
|
||||
// perfectly good database still in place — and its metadata already gone,
|
||||
// unrecoverably, for as long as the swap keeps failing.
|
||||
//
|
||||
// So destroy nothing and pair the two instead: record the size and mtime of
|
||||
// the database this metadata describes, and let readers check that the pair
|
||||
// still belongs together (`bridgeMetaMatchesFile`). A stale meta cannot match
|
||||
// a freshly renamed database, and a sync that fails before the swap leaves a
|
||||
// matching pair untouched.
|
||||
const finalStat = await fsp.stat(finalPath);
|
||||
await writeBridgeMeta(groupDir, {
|
||||
version: BRIDGE_SCHEMA_VERSION,
|
||||
generatedAt: new Date().toISOString(),
|
||||
bridgeSize: finalStat.size,
|
||||
bridgeMtimeMs: finalStat.mtimeMs,
|
||||
missingRepos: input.missingRepos,
|
||||
// Persisted whenever the caller supplied it, `[]` included: an empty list
|
||||
// is the measurement "this sync accounted for every repo", and it is a
|
||||
// different claim from a bridge that never recorded the field. Omitted
|
||||
// only when the caller passed nothing to record.
|
||||
...(input.unreadableRepos ? { unreadableRepos: input.unreadableRepos } : {}),
|
||||
});
|
||||
|
||||
return report;
|
||||
|
|
@ -982,6 +1337,33 @@ export async function writeBridge(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild `bridge.lbug` and its `meta.json` as the only writer of `groupDir`.
|
||||
*
|
||||
* The acquiring half of the split described on {@link writeBridgeUnlocked}: for
|
||||
* callers that are NOT already inside the group's critical section, this takes
|
||||
* the group sync lock around the whole swap and releases it afterwards. Two
|
||||
* concurrent calls therefore run one after the other, so the `meta.json` left
|
||||
* on disk is stamped for the `bridge.lbug` left on disk instead of for the
|
||||
* loser's, which is the pairing the swap-plus-metadata sequence would otherwise
|
||||
* let them interleave into.
|
||||
*
|
||||
* NOT used by `syncGroup`, and it must not be: that path already holds this
|
||||
* lock, and `acquireIndexLock` is not reentrant, so routing it here would make
|
||||
* every ordinary sync wait out the full `GROUP_SYNC_LOCK_TIMEOUT_MS` ceiling
|
||||
* against itself. It calls {@link writeBridgeUnlocked} directly.
|
||||
*
|
||||
* Fails closed exactly as `withGroupSyncLock` does: if the lock cannot be
|
||||
* acquired, a `GroupSyncLockError` is thrown and NOTHING is written —
|
||||
* `bridge.lbug` and `meta.json` are left as they were.
|
||||
*/
|
||||
export async function writeBridge(
|
||||
groupDir: string,
|
||||
input: WriteBridgeInput,
|
||||
): Promise<WriteBridgeReport> {
|
||||
return withGroupSyncLock(groupDir, () => writeBridgeUnlocked(groupDir, input));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* openBridgeDbReadOnly */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
|
|
|||
137
gitnexus/src/core/group/completeness.ts
Normal file
137
gitnexus/src/core/group/completeness.ts
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/**
|
||||
* The one computation of "is this cross-repo answer complete?" (KTD10), and the
|
||||
* truncation vocabulary it speaks.
|
||||
*
|
||||
* A LEAF MODULE, deliberately, and that is the whole reason it exists apart from
|
||||
* `cross-impact.ts`. Three surfaces need this fold — impact, trace, and the
|
||||
* contract listing — but `cross-impact.ts` statically imports `bridge-db.ts`,
|
||||
* and through it the native LadybugDB binding. `service.ts` therefore had to
|
||||
* reach the fold through `await import('./cross-impact.js')`, which loaded that
|
||||
* entire module graph on the first `group_contracts` of every process — 44-51ms
|
||||
* and 8.4MB of RSS to run a `Set` union and a ternary, once per CLI invocation.
|
||||
*
|
||||
* Nothing here imports anything but types. Keep it that way: the moment this
|
||||
* file gains a runtime import, every consumer pays for it again.
|
||||
*/
|
||||
import type { GroupImpactTruncationReason } from './types.js';
|
||||
|
||||
/**
|
||||
* A union rather than `Pick<GroupImpactResult, ...>` so the two states are
|
||||
* distinguishable by their `truncated` discriminant: a caller that folds these
|
||||
* fields into its own result (see `crossRepoCompleteness`) can then read
|
||||
* `truncationReason` on the truncated branch without a fallback for a value
|
||||
* that cannot be absent there.
|
||||
*/
|
||||
export type TruncationFields =
|
||||
| { truncated: false }
|
||||
| {
|
||||
truncated: true;
|
||||
truncationReason: GroupImpactTruncationReason;
|
||||
riskEpistemic: 'lower-bound';
|
||||
};
|
||||
|
||||
/**
|
||||
* Build the truncation fields every `runGroupImpact` return path shares.
|
||||
*
|
||||
* `riskEpistemic` must follow `truncated` mechanically: it is the marker that
|
||||
* tells a caller the `risk` value is a floor rather than a verdict, and
|
||||
* `mergeRisk` can only under-report once a crossing is dropped. Attaching it at
|
||||
* each return let two of the four paths set `truncated` without it, so a
|
||||
* truncated result read as complete — deriving it in one place is what keeps
|
||||
* the invariant from drifting again (#2787).
|
||||
*/
|
||||
export function truncationFields(
|
||||
truncated: boolean,
|
||||
// Only read on the truncated branch, so the not-truncated call sites omit it
|
||||
// rather than passing a reason that is thrown away.
|
||||
reasonIfTruncated: GroupImpactTruncationReason = 'partial',
|
||||
): TruncationFields {
|
||||
if (!truncated) return { truncated: false };
|
||||
return { truncated: true, truncationReason: reasonIfTruncated, riskEpistemic: 'lower-bound' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything a caller needs in order to say whether a cross-repo answer is
|
||||
* complete — deliberately WITHOUT naming where any of it came from.
|
||||
*
|
||||
* `BridgeMeta` is not in this signature, and must not be: `groupContracts`
|
||||
* answers the same question from `contracts.json` (via
|
||||
* `loadContractRegistryResilient`) and never opens a bridge at all, so
|
||||
* `version` / `repoListsUnreadable` / `pairedWithDatabase` do not exist on that
|
||||
* path. Each caller computes its own `provenanceUnknown` from whatever
|
||||
* provenance IT has and passes the boolean in.
|
||||
*/
|
||||
export interface CrossRepoCompletenessInput {
|
||||
/**
|
||||
* Repos the sync could not extract from, and repos it found no entry for.
|
||||
* Two independent diagnostics with one consequence — none of those repos'
|
||||
* contracts are in the artifact — so they are folded into one set.
|
||||
*/
|
||||
unreadableRepos?: readonly string[];
|
||||
missingRepos?: readonly string[];
|
||||
/** Computed by the caller; see `bridgeProvenanceUnknown` for the bridge one. */
|
||||
provenanceUnknown: boolean;
|
||||
/**
|
||||
* The query's DECLARED scope, not the set of repos the walk happened to
|
||||
* reach: the subgroup filter for an impact query, the two endpoint repos for
|
||||
* a trace, every member for a query that names none. An incomplete repo the
|
||||
* caller never asked about cannot make the caller's answer a floor, and
|
||||
* marking it anyway is how the marker stops meaning anything. Passing the
|
||||
* predicate in — rather than a repo list, or a subgroup — is what keeps
|
||||
* narrowing a scope a call-site change.
|
||||
*/
|
||||
inScope: (repoPath: string) => boolean;
|
||||
}
|
||||
|
||||
/** The structured triple, plus the in-scope repos that produced it. */
|
||||
export type CrossRepoCompleteness = TruncationFields & {
|
||||
/**
|
||||
* In-scope repos absent from the artifact, deduped, in first-seen order.
|
||||
* Empty on a provenance-unknown answer: nothing was measured there, and
|
||||
* inventing names out of an unreadable value is not a measurement.
|
||||
*/
|
||||
incompleteRepos: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* The ONE computation of "is this cross-repo answer complete?" (KTD10).
|
||||
*
|
||||
* Three surfaces can return a partial cross-repo answer — impact, trace, and
|
||||
* the contract listing — and each used to decide for itself, in its own
|
||||
* vocabulary, which is how two of them ended up saying it in prose only. The
|
||||
* answer is the same structured triple `GroupImpactResult` already carries, so
|
||||
* an agent reading any of them learns "complete" vs "floor" the same way.
|
||||
*
|
||||
* `truncationFields` derives `riskEpistemic` from `truncated` mechanically, and
|
||||
* is reused here rather than re-implemented for the same reason it exists: the
|
||||
* marker that says "this is a floor, not a verdict" may never drift away from
|
||||
* the flag that says the answer was cut short (#2787).
|
||||
*/
|
||||
export function crossRepoCompleteness(input: CrossRepoCompletenessInput): CrossRepoCompleteness {
|
||||
const incompleteRepos = [
|
||||
...new Set([...(input.unreadableRepos ?? []), ...(input.missingRepos ?? [])]),
|
||||
].filter((repoPath) => input.inScope(repoPath));
|
||||
return {
|
||||
...truncationFields(input.provenanceUnknown || incompleteRepos.length > 0, 'incomplete-sync'),
|
||||
incompleteRepos,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A recorded repo list is an array of strings. Anything else — a bare string, an
|
||||
* object, an array of objects — is a value we could not read, which is "not
|
||||
* recorded", not "none".
|
||||
*
|
||||
* ONE definition, deliberately. This gate is the predicate the whole
|
||||
* absent-vs-empty-vs-populated distinction rests on, and it applies to the same
|
||||
* two lists on both the registry and the bridge metadata. It lived in two files
|
||||
* verbatim, which meant tightening it — say, to reject blank strings — would
|
||||
* have fixed one surface and silently left the other.
|
||||
*
|
||||
* `Array.isArray` alone is not enough: only an array of strings survives
|
||||
* `cli/group.ts`'s `.join(', ')` as repo paths rather than as `[object Object]`.
|
||||
*/
|
||||
export function recordedRepoList(value: unknown): string[] | undefined {
|
||||
if (!Array.isArray(value)) return undefined;
|
||||
return value.every((entry) => typeof entry === 'string') ? (value as string[]) : undefined;
|
||||
}
|
||||
|
|
@ -7,11 +7,11 @@ import fsp from 'node:fs/promises';
|
|||
import path from 'node:path';
|
||||
import type {
|
||||
BridgeHandle,
|
||||
BridgeMeta,
|
||||
ContractType,
|
||||
CrossRepoImpact,
|
||||
GroupConfig,
|
||||
GroupImpactResult,
|
||||
GroupImpactTruncationReason,
|
||||
MatchType,
|
||||
OutOfScopeLink,
|
||||
} from './types.js';
|
||||
|
|
@ -24,12 +24,23 @@ import {
|
|||
} from './group-path-utils.js';
|
||||
import { getGroupDir } from './storage.js';
|
||||
import {
|
||||
bridgeMetaMatchesFile,
|
||||
closeBridgeDb,
|
||||
getCachedBridgeReadOnly,
|
||||
queryBridge,
|
||||
readBridgeMeta,
|
||||
} from './bridge-db.js';
|
||||
import { BRIDGE_SCHEMA_VERSION } from './bridge-schema.js';
|
||||
// Re-exported so the three surfaces keep one import site for the vocabulary,
|
||||
// while the fold itself stays in a leaf module no native binding reaches.
|
||||
export {
|
||||
truncationFields,
|
||||
crossRepoCompleteness,
|
||||
type TruncationFields,
|
||||
type CrossRepoCompleteness,
|
||||
type CrossRepoCompletenessInput,
|
||||
} from './completeness.js';
|
||||
import { truncationFields, crossRepoCompleteness } from './completeness.js';
|
||||
import { compareCodeUnits } from '../../lib/utils.js';
|
||||
|
||||
// High limit for the local phase of group impact so collectImpactSymbolUids
|
||||
|
|
@ -381,23 +392,30 @@ export function mergeRisk(localRisk: string, cross: CrossRepoImpact[]): string {
|
|||
}
|
||||
|
||||
/**
|
||||
* Build the truncation fields every `runGroupImpact` return path shares.
|
||||
* Is this bridge's metadata unable to say where its contents came from?
|
||||
*
|
||||
* `riskEpistemic` must follow `truncated` mechanically: it is the marker that
|
||||
* tells a caller the `risk` value is a floor rather than a verdict, and
|
||||
* `mergeRisk` can only under-report once a crossing is dropped. Attaching it at
|
||||
* each return let two of the four paths set `truncated` without it, so a
|
||||
* truncated result read as complete — deriving it in one place is what keeps
|
||||
* the invariant from drifting again (#2787).
|
||||
* The three reads are all about a `BridgeMeta` and stay OUT of
|
||||
* `crossRepoCompleteness` on purpose (see its doc): they are how a caller that
|
||||
* opened a bridge computes `provenanceUnknown`, not how every caller does.
|
||||
*
|
||||
* - `version === 0` — no readable meta.json at all (`readBridgeMeta` answers
|
||||
* that for both "absent" and "unparseable");
|
||||
* - `repoListsUnreadable` — a meta.json that parsed but whose repo lists are
|
||||
* not repo lists. A value we could not read is not a measurement of zero,
|
||||
* so it may not be spent as one;
|
||||
* - `pairedWithDatabase === false` — a meta.json that does not describe the
|
||||
* database sitting beside it, which is what a sync interrupted between the
|
||||
* swap and the metadata write leaves behind. Measured by
|
||||
* `ensureBridgeReady` BEFORE the database is opened and carried on the
|
||||
* meta; this only reads the answer (#3012).
|
||||
*
|
||||
* Treating any of them as complete is the fail-open the completeness channel
|
||||
* exists to close.
|
||||
*/
|
||||
function truncationFields(
|
||||
truncated: boolean,
|
||||
// Only read on the truncated branch, so the not-truncated call sites omit it
|
||||
// rather than passing a reason that is thrown away.
|
||||
reasonIfTruncated: GroupImpactTruncationReason = 'partial',
|
||||
): Pick<GroupImpactResult, 'truncated' | 'truncationReason' | 'riskEpistemic'> {
|
||||
if (!truncated) return { truncated: false };
|
||||
return { truncated: true, truncationReason: reasonIfTruncated, riskEpistemic: 'lower-bound' };
|
||||
export function bridgeProvenanceUnknown(meta: BridgeMeta): boolean {
|
||||
return (
|
||||
meta.version === 0 || meta.repoListsUnreadable === true || meta.pairedWithDatabase === false
|
||||
);
|
||||
}
|
||||
|
||||
function addCrossImpact(cross: CrossRepoImpact[], candidate: CrossRepoImpact): void {
|
||||
|
|
@ -418,7 +436,7 @@ function addCrossImpact(cross: CrossRepoImpact[], candidate: CrossRepoImpact): v
|
|||
|
||||
export async function ensureBridgeReady(
|
||||
groupDir: string,
|
||||
): Promise<{ handle: BridgeHandle } | { error: string }> {
|
||||
): Promise<{ handle: BridgeHandle; meta: BridgeMeta } | { error: string }> {
|
||||
const meta = await readBridgeMeta(groupDir);
|
||||
if (meta.version > 0 && meta.version !== BRIDGE_SCHEMA_VERSION) {
|
||||
return {
|
||||
|
|
@ -433,6 +451,13 @@ export async function ensureBridgeReady(
|
|||
error: `No bridge.lbug in this group directory. Run gitnexus group sync (schema ${BRIDGE_SCHEMA_VERSION}).`,
|
||||
};
|
||||
}
|
||||
// Pair the metadata to the database BEFORE opening it, and carry the answer.
|
||||
// An unstamped pair is judged on the two files' write order, so any open that
|
||||
// touched `bridge.lbug`'s mtime would silently convert "legacy but intact"
|
||||
// into "provenance unknown" for every pre-stamp bridge on that platform. This
|
||||
// ordering removes the question rather than betting on the answer.
|
||||
meta.pairedWithDatabase = await bridgeMetaMatchesFile(groupDir, meta);
|
||||
|
||||
// Use the cached read-only handle if available — avoids reopening the same
|
||||
// bridge.lbug in a long-lived MCP server, which fails on Windows because
|
||||
// the OS handle isn't fully released before the next open races in.
|
||||
|
|
@ -442,7 +467,7 @@ export async function ensureBridgeReady(
|
|||
error: `Could not open bridge.lbug read-only (schema ${BRIDGE_SCHEMA_VERSION}). Run gitnexus group sync.`,
|
||||
};
|
||||
}
|
||||
return { handle };
|
||||
return { handle, meta };
|
||||
}
|
||||
|
||||
function rowToNeighbor(r: Record<string, unknown>): BridgeNeighborRow | null {
|
||||
|
|
@ -641,6 +666,25 @@ export async function runGroupImpact(
|
|||
if ('error' in bridgePrep) return { error: bridgePrep.error };
|
||||
|
||||
const handle = bridgePrep.handle;
|
||||
// Repos the sync that built this bridge could not account for. Their
|
||||
// contracts — and every cross-link touching them — are simply absent from
|
||||
// bridge.lbug, and nothing else in this walk can notice that: the only
|
||||
// incompleteness channel on the result is `truncationFields`, driven by
|
||||
// fan-out state. Without folding these in, a query about a symbol whose one
|
||||
// downstream consumer lives in an unreadable repo returns
|
||||
// `{ cross: [], truncated: false }` — "complete: nothing depends on this" —
|
||||
// which is a wrong answer, not an empty one, for a tool an agent uses to
|
||||
// license a delete or a rename.
|
||||
//
|
||||
// The metadata read that answers it (`bridgeProvenanceUnknown`) happens
|
||||
// INSIDE the `try` below, and the flag is initialized fail-closed here only
|
||||
// because it outlives that block. The lease taken by `ensureBridgeReady` is
|
||||
// released by the `finally` and nowhere else, so work done between the lease
|
||||
// and the `try` is work whose every throw leaks a refcount the cached handle
|
||||
// can never get back — which is how a malformed meta.json used to wedge the
|
||||
// handle as well as crash the query. (The repo lists are folded in after the
|
||||
// `finally`, where a throw can no longer strand the lease.)
|
||||
let provenanceUnknown = true;
|
||||
const cross: CrossRepoImpact[] = [];
|
||||
const outOfScope: OutOfScopeLink[] = [];
|
||||
const truncatedRepos: string[] = [];
|
||||
|
|
@ -650,6 +694,8 @@ export async function runGroupImpact(
|
|||
let fanoutTimedOut = false;
|
||||
|
||||
try {
|
||||
provenanceUnknown = bridgeProvenanceUnknown(bridgePrep.meta);
|
||||
|
||||
const neighbors = await resolveBridgeNeighbors(handle, {
|
||||
localRepo: repoPath,
|
||||
uids,
|
||||
|
|
@ -782,7 +828,45 @@ export async function runGroupImpact(
|
|||
const localSum = (local as { summary?: Record<string, number> })?.summary || {};
|
||||
const localRisk = String((local as { risk?: string }).risk ?? 'LOW');
|
||||
const localPartial = Boolean((local as { partial?: boolean }).partial);
|
||||
const truncated = truncatedRepos.length > 0 || localPartial;
|
||||
// The bridge's own incompleteness, in the shared vocabulary, read through
|
||||
// what this query DECLARED. The fan-out above already drops every neighbour
|
||||
// outside `subgroup`, so an incomplete repo the query excluded could not have
|
||||
// contributed a crossing to this answer — marking the answer a floor because
|
||||
// of it makes the marker fire on results it does not describe, which is how a
|
||||
// caller learns to ignore it. An unscoped query passes `subgroup: undefined`,
|
||||
// which `repoInSubgroup` answers true for, so the intersection is the whole
|
||||
// set and that path is byte-for-byte the old behaviour.
|
||||
//
|
||||
// The declared scope is the subgroup PLUS the query's own repo (`exact`
|
||||
// reuses the one membership helper for the equality, rather than growing a
|
||||
// second notion of it): the walk starts from `repoPath`'s contracts in the
|
||||
// bridge, so if THAT is the repo the sync could not read there are no
|
||||
// crossings to find for any scope, and a subgroup excluding it must not turn
|
||||
// that vacuum into a confident "complete".
|
||||
//
|
||||
// Declared scope, not traversed scope: an incomplete repo's contracts are
|
||||
// absent from the bridge by definition, so it is never in the set the walk
|
||||
// reached — filtering on what was traversed would empty the intersection on
|
||||
// every query and silently restore the fail-open.
|
||||
//
|
||||
// Sound only while `MAX_SUPPORTED_CROSS_DEPTH` is 1. At depth 2+ an
|
||||
// out-of-scope repo can sit BETWEEN two in-scope ones, so dropping it would
|
||||
// convert a genuine lower bound into a confident complete answer; widen this
|
||||
// predicate in the same change that raises the depth.
|
||||
const bridge = crossRepoCompleteness({
|
||||
unreadableRepos: bridgePrep.meta.unreadableRepos,
|
||||
missingRepos: bridgePrep.meta.missingRepos,
|
||||
provenanceUnknown,
|
||||
inScope: (candidate) =>
|
||||
repoInSubgroup(candidate, subgroup) || repoInSubgroup(candidate, repoPath, true),
|
||||
});
|
||||
// One predicate, read twice below. Written out at both sites, a third runtime
|
||||
// cause added to the flag and forgotten at the reason would label a
|
||||
// retry-able answer `incomplete-sync` — telling the operator to re-sync for
|
||||
// something a retry fixes. That reason-vs-flag drift is what `truncationFields`
|
||||
// exists to prevent.
|
||||
const runtimeTruncated = truncatedRepos.length > 0 || localPartial;
|
||||
const truncated = runtimeTruncated || bridge.truncated;
|
||||
|
||||
const result: GroupImpactResult = {
|
||||
local,
|
||||
|
|
@ -794,8 +878,17 @@ export async function runGroupImpact(
|
|||
// and under-reporting a blast radius is the unsafe direction (an agent told
|
||||
// LOW proceeds; told CRITICAL it stops). Marking the floor keeps the
|
||||
// warning intact while making the incompleteness legible.
|
||||
...truncationFields(truncated, fanoutTimedOut ? 'timeout' : 'partial'),
|
||||
truncatedRepos: [...new Set(truncatedRepos)],
|
||||
// Runtime limits first — they are what the caller can retry. 'incomplete-sync'
|
||||
// is the remaining cause once nothing was merely cut short, and its remedy is
|
||||
// a different one: re-run `gitnexus group sync`, not the query. Computed
|
||||
// inline because `truncationFields` reads the reason ONLY on the truncated
|
||||
// branch — naming it in a variable invited reading it on the complete path,
|
||||
// where it would say 'incomplete-sync' about a complete result.
|
||||
...truncationFields(
|
||||
truncated,
|
||||
fanoutTimedOut ? 'timeout' : runtimeTruncated ? 'partial' : 'incomplete-sync',
|
||||
),
|
||||
truncatedRepos: [...new Set([...truncatedRepos, ...bridge.incompleteRepos])],
|
||||
summary: {
|
||||
direct: localSum.direct ?? 0,
|
||||
processes_affected: localSum.processes_affected ?? 0,
|
||||
|
|
|
|||
|
|
@ -25,16 +25,29 @@
|
|||
|
||||
import { GroupNotFoundError, loadGroupConfig } from './config-parser.js';
|
||||
import { getGroupDir } from './storage.js';
|
||||
import { ensureBridgeReady, MAX_SUPPORTED_CROSS_DEPTH } from './cross-impact.js';
|
||||
import {
|
||||
bridgeProvenanceUnknown,
|
||||
crossRepoCompleteness,
|
||||
ensureBridgeReady,
|
||||
MAX_SUPPORTED_CROSS_DEPTH,
|
||||
} from './cross-impact.js';
|
||||
import type { CrossRepoCompleteness } from './completeness.js';
|
||||
import { truncationFields } from './completeness.js';
|
||||
import { compareCodeUnits } from '../../lib/utils.js';
|
||||
import { closeBridgeDb, queryBridge } from './bridge-db.js';
|
||||
import { repoInSubgroup } from './group-path-utils.js';
|
||||
import type {
|
||||
GroupPdgFlowHop,
|
||||
GroupRepoHandle,
|
||||
GroupSymbolResolution,
|
||||
GroupToolPort,
|
||||
} from './service.js';
|
||||
import type { BridgeHandle, GroupConfig } from './types.js';
|
||||
import type {
|
||||
BridgeHandle,
|
||||
BridgeMeta,
|
||||
GroupConfig,
|
||||
GroupImpactTruncationReason,
|
||||
} from './types.js';
|
||||
|
||||
// ── Result types (discriminated on `status`) ─────────────────────────────
|
||||
|
||||
|
|
@ -77,7 +90,29 @@ export interface GroupTraceEndpoint {
|
|||
repo: string;
|
||||
}
|
||||
|
||||
export interface GroupTraceOkResult {
|
||||
/**
|
||||
* The incompleteness vocabulary, verbatim from `GroupImpactResult` (KTD10).
|
||||
*
|
||||
* A cross-repo trace and a cross-repo impact can both be cut short by the same
|
||||
* two kinds of cause — a runtime limit inside this walk, or a bridge that never
|
||||
* held part of the group — and an agent must not have to learn a second
|
||||
* vocabulary (or parse a `notes` string) to tell "no path exists" from "we
|
||||
* could not have seen the path". Every field here means exactly what it means
|
||||
* on `GroupImpactResult`; `notes` stays a human-readable ADDITION to them,
|
||||
* never the machine-readable channel.
|
||||
*/
|
||||
export interface GroupTraceCompleteness {
|
||||
/** True when this answer is a floor rather than a verdict. */
|
||||
truncated?: boolean;
|
||||
/** Why, when `truncated` — runtime limit ('partial'/'timeout') before structure. */
|
||||
truncationReason?: GroupImpactTruncationReason;
|
||||
/** Set with `truncated`: the answer under-reports, it never over-reports. */
|
||||
riskEpistemic?: 'lower-bound';
|
||||
/** In-scope repos absent from the bridge; omitted when none were measured. */
|
||||
truncatedRepos?: string[];
|
||||
}
|
||||
|
||||
export interface GroupTraceOkResult extends GroupTraceCompleteness {
|
||||
status: 'ok';
|
||||
group: string;
|
||||
from: GroupTraceEndpoint;
|
||||
|
|
@ -89,7 +124,6 @@ export interface GroupTraceOkResult {
|
|||
edges: TraceEdge[];
|
||||
/** Present only when PDG enrichment ran for at least one segment. */
|
||||
dataFlow?: SegmentDataFlow[];
|
||||
truncated?: boolean;
|
||||
notes: string[];
|
||||
}
|
||||
|
||||
|
|
@ -101,23 +135,23 @@ export interface GroupTraceCandidate {
|
|||
startLine: number;
|
||||
}
|
||||
|
||||
export interface GroupTraceNotFoundResult {
|
||||
/**
|
||||
* `truncated: true` here means the answer is NOT authoritative — either the
|
||||
* crossing cap (`MAX_CROSSINGS_TO_TRY`) was hit so a connecting ContractLink
|
||||
* ranked beyond it may have been skipped, or the bridge itself never held part
|
||||
* of the group. Both read as "unknown", not as "no path exists";
|
||||
* `truncationReason` says which.
|
||||
*/
|
||||
export interface GroupTraceNotFoundResult extends GroupTraceCompleteness {
|
||||
status: 'not_found';
|
||||
group: string;
|
||||
role?: 'from' | 'to';
|
||||
query?: string;
|
||||
/**
|
||||
* True when the answer is NOT authoritative: the crossing cap
|
||||
* (`MAX_CROSSINGS_TO_TRY`) was hit, so a connecting ContractLink ranked beyond
|
||||
* the cap may have been skipped. A consumer should treat this as "unknown",
|
||||
* not "no path exists".
|
||||
*/
|
||||
truncated?: boolean;
|
||||
notes: string[];
|
||||
suggestion?: string;
|
||||
}
|
||||
|
||||
export interface GroupTraceAmbiguousResult {
|
||||
export interface GroupTraceAmbiguousResult extends GroupTraceCompleteness {
|
||||
status: 'ambiguous';
|
||||
group: string;
|
||||
role: 'from' | 'to';
|
||||
|
|
@ -187,6 +221,54 @@ export const TRACE_NOTES = {
|
|||
'The candidates are listed; trace from the exact calling function or pass `to_uid`.',
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Fold this bridge's completeness into the runtime-truncation flag a trace call
|
||||
* site already computed, and answer in the shared vocabulary.
|
||||
*
|
||||
* Precedence mirrors `runGroupImpact`: a runtime limit wins the reason, because
|
||||
* it is the cause the caller can act on (narrow the query, raise maxDepth),
|
||||
* while `'incomplete-sync'` needs a different remedy — `gitnexus group sync` —
|
||||
* and would otherwise mask it.
|
||||
*
|
||||
* Returns `{}` — not `{ truncated: false }` — when the answer is complete, so a
|
||||
* clean trace result keeps the exact shape it has always had.
|
||||
*/
|
||||
function traceCompleteness(
|
||||
bridge: CrossRepoCompleteness,
|
||||
runtimeTruncated: boolean,
|
||||
): GroupTraceCompleteness {
|
||||
const repos = bridge.incompleteRepos.length > 0 ? { truncatedRepos: bridge.incompleteRepos } : {};
|
||||
// Through `truncationFields`, not hand-written: `riskEpistemic` must follow
|
||||
// `truncated` mechanically, and a third writer of that pair is how the
|
||||
// invariant drifts (#2787). The bridge branch re-spreads the helper's own
|
||||
// output rather than naming its fields.
|
||||
if (runtimeTruncated) return { ...truncationFields(true, 'partial'), ...repos };
|
||||
if (!bridge.truncated) return {};
|
||||
const { incompleteRepos: _incompleteRepos, ...fields } = bridge;
|
||||
return { ...fields, ...repos };
|
||||
}
|
||||
|
||||
/**
|
||||
* The trace's declared scope for `crossRepoCompleteness`.
|
||||
*
|
||||
* A symbol-to-symbol trace asks about exactly two repos, so an unreadable third
|
||||
* member cannot make its answer a floor. A DESTINATION trace declares no `to`
|
||||
* at all — the call may land in any member — so every repo is in scope there,
|
||||
* which is why the predicate is built per call site rather than derived from
|
||||
* the endpoints inside the helper.
|
||||
*/
|
||||
function bridgeCompletenessFor(
|
||||
meta: BridgeMeta,
|
||||
inScope: (repoPath: string) => boolean,
|
||||
): CrossRepoCompleteness {
|
||||
return crossRepoCompleteness({
|
||||
unreadableRepos: meta.unreadableRepos,
|
||||
missingRepos: meta.missingRepos,
|
||||
provenanceUnknown: bridgeProvenanceUnknown(meta),
|
||||
inScope,
|
||||
});
|
||||
}
|
||||
|
||||
/** Repo-relative path equality, tolerant of a leading "./" / "/" or a repo prefix. */
|
||||
function sameFile(a: string, b: string): boolean {
|
||||
if (!a || !b) return false;
|
||||
|
|
@ -873,6 +955,23 @@ async function stitchCrossRepo(
|
|||
if (p.pdg) notes.push(TRACE_NOTES.pdgRequested);
|
||||
|
||||
try {
|
||||
// Inside the `try`, like `runGroupImpact`'s equivalent: the lease taken by
|
||||
// `ensureBridgeReady` is released by this block's `finally` and nowhere
|
||||
// else, so anything computed between the lease and the `try` is work whose
|
||||
// every throw would strand a refcount the cached handle never gets back.
|
||||
//
|
||||
// Declared scope = the two endpoint repos. Whether either of them is a repo
|
||||
// this bridge could not read decides whether "no ContractLink connects
|
||||
// them" is a verdict or a floor.
|
||||
const bridge = bridgeCompletenessFor(
|
||||
bridgePrep.meta,
|
||||
// `repoInSubgroup(..., exact)` rather than `===`: it normalizes separators
|
||||
// and strips trailing slashes, which bare equality does not, so the same
|
||||
// group.yaml spelling cannot be in scope for impact and out of scope here.
|
||||
(repoPath) =>
|
||||
repoInSubgroup(repoPath, fromEp.member.repoPath, true) ||
|
||||
repoInSubgroup(repoPath, toEp.member.repoPath, true),
|
||||
);
|
||||
const { crossings, truncated: crossingsTruncated } = await listCrossingsBetween(
|
||||
handle,
|
||||
fromEp.member.repoPath,
|
||||
|
|
@ -883,6 +982,10 @@ async function stitchCrossRepo(
|
|||
return {
|
||||
status: 'not_found',
|
||||
group: p.name,
|
||||
// No crossings at all is exactly the answer a bridge that never held an
|
||||
// endpoint's repo produces, so it is the one that most needs the floor
|
||||
// marker. (Nothing was capped: there were zero rows to cap.)
|
||||
...traceCompleteness(bridge, false),
|
||||
notes,
|
||||
suggestion:
|
||||
'The endpoints live in different repos with no ContractLink between them. ' +
|
||||
|
|
@ -1016,6 +1119,13 @@ async function stitchCrossRepo(
|
|||
hopCount: edges.length,
|
||||
hops: [...hopsA, ...hopsB],
|
||||
edges,
|
||||
// A found path is still an answer from this bridge: if its provenance is
|
||||
// unknown, or an endpoint's repo never made it in, the path may be stale
|
||||
// and it is certainly not the only one. An incompleteness channel that
|
||||
// fires only on the empty answer teaches an agent that a non-empty one
|
||||
// is always complete. The crossing cap is NOT folded in here — a path
|
||||
// that connected is not a capped search — so this site passes `false`.
|
||||
...traceCompleteness(bridge, false),
|
||||
notes,
|
||||
...(dataFlow.length > 0 ? { dataFlow } : {}),
|
||||
};
|
||||
|
|
@ -1028,7 +1138,7 @@ async function stitchCrossRepo(
|
|||
return {
|
||||
status: 'not_found',
|
||||
group: p.name,
|
||||
...(crossingsTruncated ? { truncated: true } : {}),
|
||||
...traceCompleteness(bridge, crossingsTruncated),
|
||||
notes,
|
||||
suggestion: crossingsTruncated
|
||||
? `No connecting crossing among the ${MAX_CROSSINGS_TO_TRY} highest-confidence ` +
|
||||
|
|
@ -1099,6 +1209,12 @@ async function stitchToDestination(
|
|||
if (p.crossDepthClamped) notes.push(TRACE_NOTES.crossDepthClamped);
|
||||
|
||||
try {
|
||||
// Inside the `try` for the lease reason above `stitchCrossRepo`'s copy. A
|
||||
// destination trace declares NO `to`: the call may land in any member, so
|
||||
// every repo is in the query's scope and no incomplete one can be filtered
|
||||
// out. An unreadable provider repo is precisely how "no outgoing
|
||||
// ContractLink leaves this repo" becomes a wrong answer, not an empty one.
|
||||
const bridge = bridgeCompletenessFor(bridgePrep.meta, () => true);
|
||||
const { crossings, truncated } = await listCrossingsFrom(handle, fromEp.member.repoPath);
|
||||
if (crossings.length === 0) {
|
||||
notes.push(TRACE_NOTES.destinationNoLink);
|
||||
|
|
@ -1107,6 +1223,8 @@ async function stitchToDestination(
|
|||
group: p.name,
|
||||
role: 'to',
|
||||
query: p.from_uid ?? p.from,
|
||||
// Zero rows to cap, so only the bridge's own completeness can speak.
|
||||
...traceCompleteness(bridge, false),
|
||||
notes,
|
||||
suggestion: 'Pass a `to` symbol for a symbol-to-symbol trace, or run group_sync.',
|
||||
};
|
||||
|
|
@ -1224,7 +1342,9 @@ async function stitchToDestination(
|
|||
hopCount: edgesA.length + 1,
|
||||
hops: [...hopsA, providerHop],
|
||||
edges: [...edgesA, boundaryEdge],
|
||||
...(truncated ? { truncated: true } : {}),
|
||||
// The cap already marked this result; the bridge's completeness folds
|
||||
// into the same fields rather than beside them.
|
||||
...traceCompleteness(bridge, truncated),
|
||||
notes: resultNotes,
|
||||
};
|
||||
};
|
||||
|
|
@ -1240,6 +1360,8 @@ async function stitchToDestination(
|
|||
group: p.name,
|
||||
role: 'to',
|
||||
candidates: candidatesFrom(precise),
|
||||
// The candidate LIST is what an incomplete bridge shortens here.
|
||||
...traceCompleteness(bridge, truncated),
|
||||
notes: [...notes, TRACE_NOTES.destinationMultiple],
|
||||
};
|
||||
}
|
||||
|
|
@ -1255,6 +1377,7 @@ async function stitchToDestination(
|
|||
group: p.name,
|
||||
role: 'to',
|
||||
candidates: candidatesFrom(fileLevel),
|
||||
...traceCompleteness(bridge, truncated),
|
||||
notes: [...notes, TRACE_NOTES.destinationAmbiguousFile],
|
||||
};
|
||||
}
|
||||
|
|
@ -1265,7 +1388,7 @@ async function stitchToDestination(
|
|||
group: p.name,
|
||||
role: 'to',
|
||||
query: p.from_uid ?? p.from,
|
||||
...(truncated ? { truncated: true } : {}),
|
||||
...traceCompleteness(bridge, truncated),
|
||||
notes,
|
||||
suggestion: 'Trace from the function that issues the HTTP request, or pass a `to` symbol.',
|
||||
};
|
||||
|
|
|
|||
201
gitnexus/src/core/group/group-lock.ts
Normal file
201
gitnexus/src/core/group/group-lock.ts
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
/**
|
||||
* Cross-process single-writer lock for one group's persisted state (R9).
|
||||
*
|
||||
* A group sync ends by REPLACING `contracts.json` and rebuilding `bridge.lbug`
|
||||
* from a snapshot it computed minutes earlier. Two syncs of the same group that
|
||||
* overlap therefore do not merge — the second one's write simply overwrites the
|
||||
* first one's, and whichever finishes last wins with a registry assembled from
|
||||
* repo state the other run never saw. Nothing detects it afterwards: both runs
|
||||
* report success, and the group's contracts silently describe a mixture that was
|
||||
* never true at any instant. This module serializes that section so one sync at
|
||||
* a time can be inside it.
|
||||
*
|
||||
* WHERE THE LOCK LIVES. On a dedicated `sync-lock` directory INSIDE the group
|
||||
* directory — mirroring `withRegistryLock`, which locks a `registry-lock`
|
||||
* directory beside the registry rather than the registry's own directory
|
||||
* (repo-manager.ts). {@link acquireIndexLock} is NOT reentrant and its file
|
||||
* backend writes `analyze.lock` into the directory it is handed, so pointing it
|
||||
* at a directory that some other code path might also lock — or that already
|
||||
* holds a per-repo index slot — reintroduces exactly the collision the registry
|
||||
* lock's own comment warns about. `<groupDir>/sync-lock` is a namespace nothing
|
||||
* else claims: group directories live under `~/.gitnexus/groups/<name>` (or
|
||||
* `$GITNEXUS_HOME`), never under a repo's `.gitnexus[/branches/<slug>]`.
|
||||
*
|
||||
* WHY IT FAILS CLOSED, unlike the registry lock. `withRegistryLock` degrades to
|
||||
* running UNLOCKED on timeout, and that is right for it: it guards a sub-second
|
||||
* JSON read/merge/write on a latency-critical path (`augment` runs on every
|
||||
* editor tool call), and running unlocked is merely the pre-lock status quo. A
|
||||
* group sync is the opposite on every axis — it is long, expensive, operator-
|
||||
* initiated, and its lost update destroys contracts rather than a registry field.
|
||||
* A sync that cannot be protected must not run at all, and there are three
|
||||
* distinct ways it can fail to be protected; all three throw
|
||||
* {@link GroupSyncLockError}:
|
||||
*
|
||||
* 1. TIMEOUT — the holder is still alive when the ceiling elapses.
|
||||
* 2. LOCK-FREE DEGRADATION — `acquireIndexLock` answers a read-only or
|
||||
* permission-denied filesystem with a no-op handle that is byte-identical
|
||||
* to a real one at the API boundary. That is a deliberate tolerance for
|
||||
* `analyze` (an unwritable index dir rejects every write anyway, so the
|
||||
* lock is moot), but here it would hand back a handle that protects
|
||||
* nothing while the sync went on to attempt its writes. The handle now
|
||||
* carries {@link IndexLockHandle.lockFree}, so we can see it and refuse.
|
||||
* 3. ANY OTHER ACQUIRE FAILURE — e.g. `sync-lock` cannot be created because a
|
||||
* regular file already occupies the path. Silently proceeding on an error
|
||||
* we did not anticipate is the same unprotected run under another name.
|
||||
*
|
||||
* WHY THE CEILING IS PASSED EXPLICITLY. The magnitude is not the point — 10
|
||||
* minutes deliberately matches `acquireIndexLock`'s own default, because a group
|
||||
* sync is analyze-shaped and a legitimately queued second sync must be able to
|
||||
* wait out a full first one (the registry lock's 5s is sized for a sub-second
|
||||
* merge and is the wrong model here). The reason to pass it is
|
||||
* `resolveTimeoutMs`: it prefers an explicit argument over
|
||||
* `GITNEXUS_INDEX_LOCK_TIMEOUT_MS`, and that variable's `<= 0` case resolves to
|
||||
* `Number.POSITIVE_INFINITY`. Inheriting it would let an environment turn this
|
||||
* lock's fail-closed timeout into an unbounded hang.
|
||||
*
|
||||
* ACQUIRED EXACTLY ONCE, by `syncGroup`, around its whole persist section.
|
||||
* Nothing it calls beneath that point — `writeContractRegistry`,
|
||||
* `refreshPreservedBridgeMeta`, `writeBridgeUnlocked` — takes this lock; a
|
||||
* second acquisition would deadlock a non-reentrant primitive on the HAPPY
|
||||
* path, not on some edge case. `bridge-db.ts` exports the swap in both forms
|
||||
* for exactly that reason: `writeBridgeUnlocked` for the held-lock caller
|
||||
* (`syncGroup`), and the `writeBridge` wrapper, which acquires here, for direct
|
||||
* callers that are outside the region. The same split `repo-manager.ts` uses
|
||||
* for `registerRepoUnlocked` / `registerRepo`.
|
||||
*
|
||||
* SCOPE CAVEAT (recorded, not solved): the default socket backend uses Linux
|
||||
* abstract sockets, which are network-namespace-scoped. Two containers that
|
||||
* share a bind-mounted group directory but sit in separate netns will NOT
|
||||
* contend, exactly as documented for the index lock itself; forcing
|
||||
* `GITNEXUS_INDEX_LOCK_BACKEND=file` is what covers that deployment.
|
||||
*/
|
||||
import path from 'node:path';
|
||||
import {
|
||||
acquireIndexLock,
|
||||
IndexLockTimeoutError,
|
||||
type IndexLockHandle,
|
||||
} from '../../storage/index-lock.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
/** Lock-directory name inside the group directory. Never the group dir itself. */
|
||||
export const GROUP_SYNC_LOCK_DIRNAME = 'sync-lock';
|
||||
|
||||
/** The dedicated lock namespace for one group: `<groupDir>/sync-lock`. */
|
||||
export const getGroupSyncLockDir = (groupDir: string): string =>
|
||||
path.join(groupDir, GROUP_SYNC_LOCK_DIRNAME);
|
||||
|
||||
/**
|
||||
* Wait ceiling for the group sync lock (10 min). See the module header: the
|
||||
* magnitude matches `acquireIndexLock`'s analyze-sized default on purpose; the
|
||||
* reason it is passed EXPLICITLY is to keep `GITNEXUS_INDEX_LOCK_TIMEOUT_MS`
|
||||
* (whose `<= 0` case means unbounded) from turning fail-closed into a hang.
|
||||
*/
|
||||
export const GROUP_SYNC_LOCK_TIMEOUT_MS = 600_000;
|
||||
|
||||
/** Which of the three fail-closed exits produced a {@link GroupSyncLockError}. */
|
||||
export type GroupSyncLockFailure = 'timeout' | 'lock-free' | 'unavailable';
|
||||
|
||||
/**
|
||||
* A group sync could not be protected, so it did not run. One class for all
|
||||
* three exits so both callers — the CLI command and the MCP service — have a
|
||||
* single thing to catch and report.
|
||||
*/
|
||||
export class GroupSyncLockError extends Error {
|
||||
readonly reason: GroupSyncLockFailure;
|
||||
readonly groupDir: string;
|
||||
constructor(reason: GroupSyncLockFailure, groupDir: string, message: string, cause?: unknown) {
|
||||
super(message, cause === undefined ? undefined : { cause });
|
||||
this.name = 'GroupSyncLockError';
|
||||
this.reason = reason;
|
||||
this.groupDir = groupDir;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run `operation` as the only group sync touching `groupDir`, or throw
|
||||
* {@link GroupSyncLockError} without running it at all.
|
||||
*
|
||||
* The lock is released in a `finally`, so it is dropped whether the operation
|
||||
* succeeds or throws.
|
||||
*/
|
||||
export const withGroupSyncLock = async <T>(
|
||||
groupDir: string,
|
||||
operation: () => Promise<T>,
|
||||
): Promise<T> => {
|
||||
let handle: IndexLockHandle;
|
||||
// The wrapper times the acquisition itself. `IndexLockTimeoutError` carries
|
||||
// `holder` and `holderKnown` and nothing else — the elapsed wait exists only
|
||||
// inside its inherited message string, so the figure has to be measured here
|
||||
// to be reported without that message. `Date.now()` matches how the primitive
|
||||
// measures its own wait.
|
||||
const acquireStartedAt = Date.now();
|
||||
try {
|
||||
handle = await acquireIndexLock(getGroupSyncLockDir(groupDir), {
|
||||
timeoutMs: GROUP_SYNC_LOCK_TIMEOUT_MS,
|
||||
// `acquireIndexLock`'s own `log` texts name an "analyze" holder, which
|
||||
// misattributes a group-sync wait — the same reason `withRegistryLock`
|
||||
// supplies its own line instead of passing `log` through.
|
||||
onWaitStart: () =>
|
||||
logger.info(
|
||||
{ groupDir },
|
||||
'Waiting for another GitNexus process to finish syncing this group…',
|
||||
),
|
||||
});
|
||||
} catch (err) {
|
||||
// The inherited message names "another gitnexus analyze" as the holder —
|
||||
// a cause this detection path cannot establish. Nothing but a group sync
|
||||
// ever locks `<groupDir>/sync-lock` (see the module header), and on the
|
||||
// socket backend the holder is not identifiable at all. Re-word it around
|
||||
// what IS known: which group, which operation, and how long we waited.
|
||||
if (err instanceof IndexLockTimeoutError) {
|
||||
throw new GroupSyncLockError(
|
||||
'timeout',
|
||||
groupDir,
|
||||
`Timed out after ${Date.now() - acquireStartedAt}ms waiting for the sync lock on ` +
|
||||
`group "${path.basename(groupDir)}" (${getGroupSyncLockDir(groupDir)}). ` +
|
||||
// `holderKnown` is false on the socket backend and on the file
|
||||
// backend's malformed/vanished-lock timeouts, where `holder` is a
|
||||
// placeholder (`pid -1`). Presenting that as a real owner would be the
|
||||
// same unestablished claim in a new form.
|
||||
(err.holderKnown
|
||||
? `Held by pid ${err.holder.pid} on ${err.holder.hostname} ` +
|
||||
`(invocation ${err.holder.invocationId}). `
|
||||
: `The lock stayed held for the whole wait, but this lock backend ` +
|
||||
`cannot identify the holder. `) +
|
||||
`Nothing was written and this group was not synced. ` +
|
||||
`Re-run once the other sync of this group has finished.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
throw new GroupSyncLockError(
|
||||
'unavailable',
|
||||
groupDir,
|
||||
`Could not acquire the sync lock for this group (${getGroupSyncLockDir(groupDir)}): ` +
|
||||
`${err instanceof Error ? err.message : String(err)}. Nothing was written.`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
|
||||
if (handle.lockFree) {
|
||||
// A handle that owns nothing. Release it anyway (it is a no-op, but the
|
||||
// contract is that every handle is released) and refuse to run: this sync
|
||||
// would otherwise write `contracts.json` and `bridge.lbug` with no
|
||||
// protection at all against a concurrent sync doing the same.
|
||||
handle.release();
|
||||
throw new GroupSyncLockError(
|
||||
'lock-free',
|
||||
groupDir,
|
||||
`The sync lock for this group could not be created at ` +
|
||||
`${getGroupSyncLockDir(groupDir)} (read-only or permission-denied filesystem), ` +
|
||||
`so this sync cannot be protected against a concurrent one. Nothing was written. ` +
|
||||
`Make the group directory writable and re-run.`,
|
||||
undefined,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return await operation();
|
||||
} finally {
|
||||
handle.release();
|
||||
}
|
||||
};
|
||||
|
|
@ -6,7 +6,16 @@
|
|||
import fsp from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { checkStaleness } from '../git-staleness.js';
|
||||
import { loadMeta, type RepoMeta } from '../../storage/repo-manager.js';
|
||||
import {
|
||||
canonicalizePath,
|
||||
loadMeta,
|
||||
readRegistryStrict,
|
||||
registryPathEquals,
|
||||
type RegistryEntry,
|
||||
type RepoMeta,
|
||||
} from '../../storage/repo-manager.js';
|
||||
import { crossRepoCompleteness } from './completeness.js';
|
||||
import { recordedRepoList } from './completeness.js';
|
||||
import { GroupNotFoundError, loadGroupConfig } from './config-parser.js';
|
||||
import {
|
||||
fileMatchesServicePrefix,
|
||||
|
|
@ -222,6 +231,34 @@ function isCrossLink(raw: unknown): raw is CrossLink {
|
|||
return typeof o.contractId === 'string' && typeof o.type === 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the global registry hold a row for this configured group member?
|
||||
*
|
||||
* Consulted only once resolution has ALREADY failed, to choose which of the
|
||||
* two failures `group status` reports. It mirrors the two tiers
|
||||
* `LocalBackend.resolveRepo` matches a bare group-config value on — the
|
||||
* registry `name`, case-insensitively, and the repo `path` — and deliberately
|
||||
* stops short of its hashed-id and partial-name tiers: those exist to be
|
||||
* generous about what an operator typed, while this predicate only decides
|
||||
* between two labels, and a looser match here would relabel a genuine registry
|
||||
* miss as an unresolvable row. That is the same conflation this reporting
|
||||
* exists to remove, pointed the other way.
|
||||
*/
|
||||
function registryIdentifies(entries: RegistryEntry[], registryName: string): boolean {
|
||||
const wantedName = registryName.toLowerCase();
|
||||
// Path equality goes through the registry's own rule rather than a local
|
||||
// `resolve` + platform-case compare. `canonicalizePath` also follows symlinks,
|
||||
// so a row registered through one and looked up through the other still
|
||||
// matches — and there is one definition of registry path identity instead of
|
||||
// a third, weaker copy of it living in a group module nobody would grep.
|
||||
const wantedPath = canonicalizePath(registryName);
|
||||
return entries.some((entry) => {
|
||||
if (typeof entry.name === 'string' && entry.name.toLowerCase() === wantedName) return true;
|
||||
if (typeof entry.path !== 'string') return false;
|
||||
return registryPathEquals(canonicalizePath(entry.path), wantedPath);
|
||||
});
|
||||
}
|
||||
|
||||
async function loadContractRegistryResilient(
|
||||
groupDir: string,
|
||||
): Promise<
|
||||
|
|
@ -288,6 +325,8 @@ async function loadContractRegistryResilient(
|
|||
}
|
||||
}
|
||||
|
||||
// Bound once: the gate is a full array scan and the ternary below used it twice.
|
||||
const recordedUnreadable = recordedRepoList(base.unreadableRepos);
|
||||
const registry: ContractRegistry = {
|
||||
version: typeof base.version === 'number' ? base.version : 0,
|
||||
generatedAt: typeof base.generatedAt === 'string' ? base.generatedAt : '',
|
||||
|
|
@ -295,7 +334,20 @@ async function loadContractRegistryResilient(
|
|||
base.repoSnapshots && typeof base.repoSnapshots === 'object' && base.repoSnapshots !== null
|
||||
? (base.repoSnapshots as Record<string, { indexedAt: string; lastCommit: string }>)
|
||||
: {},
|
||||
missingRepos: Array.isArray(base.missingRepos) ? (base.missingRepos as string[]) : [],
|
||||
// Same gate as `groupStatus` uses on the same field, for the same reason:
|
||||
// `Array.isArray` alone waves through `[{repo:'x'}]`, and `groupContracts`
|
||||
// now returns this list AND folds it into its completeness answer, so a
|
||||
// value we could not read would be reported as a repo name. `missingRepos`
|
||||
// has always been required, so — unlike `unreadableRepos` below — there is
|
||||
// no "not recorded" state to preserve: an unreadable value degrades to empty.
|
||||
missingRepos: recordedRepoList(base.missingRepos) ?? [],
|
||||
// Spread, not `?? []`. `ContractRegistry.unreadableRepos` documents absence
|
||||
// as "not recorded", and a registry written before the field existed has no
|
||||
// opinion about which indexes were readable. Normalizing that to `[]` hands
|
||||
// the caller "the last sync found none unreadable" — an unmeasured state
|
||||
// rendered as a clean result, which is the same conflation this whole
|
||||
// change removes.
|
||||
...(recordedUnreadable ? { unreadableRepos: recordedUnreadable } : {}),
|
||||
contracts,
|
||||
crossLinks,
|
||||
};
|
||||
|
|
@ -347,18 +399,34 @@ export class GroupService {
|
|||
// MCP server startup entirely and off every non-sync group call. The CLI
|
||||
// already does exactly this at `cli/group.ts`'s sync command.
|
||||
const { syncGroup } = await import('./sync.js');
|
||||
const result = await syncGroup(config, {
|
||||
groupDir,
|
||||
exactOnly: Boolean(params.exactOnly),
|
||||
skipEmbeddings: Boolean(params.skipEmbeddings),
|
||||
allowStale: Boolean(params.allowStale),
|
||||
verbose: Boolean(params.verbose),
|
||||
});
|
||||
const { GroupSyncLockError } = await import('./group-lock.js');
|
||||
let result: Awaited<ReturnType<typeof syncGroup>>;
|
||||
try {
|
||||
result = await syncGroup(config, {
|
||||
groupDir,
|
||||
exactOnly: Boolean(params.exactOnly),
|
||||
skipEmbeddings: Boolean(params.skipEmbeddings),
|
||||
allowStale: Boolean(params.allowStale),
|
||||
verbose: Boolean(params.verbose),
|
||||
});
|
||||
} catch (err) {
|
||||
// Fails closed (R9): this sync could not be protected against a concurrent
|
||||
// one, so it did not run and wrote nothing. Return it through the same
|
||||
// error channel a missing group uses — NEVER as a success payload of zeroes,
|
||||
// which an agent would read as "the group genuinely has no contracts".
|
||||
if (!(err instanceof GroupSyncLockError)) throw err;
|
||||
return { error: err.message };
|
||||
}
|
||||
return {
|
||||
contracts: result.contracts.length,
|
||||
crossLinks: result.crossLinks.length,
|
||||
unmatched: result.unmatched.length,
|
||||
missingRepos: result.missingRepos,
|
||||
unreadableRepos: result.unreadableRepos,
|
||||
// An agent that calls group_sync and then group_contracts a moment later
|
||||
// can otherwise see contract counts that disagree with this payload, with
|
||||
// nothing here explaining why the write was skipped.
|
||||
registryOutcome: result.registryOutcome,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -386,7 +454,38 @@ export class GroupService {
|
|||
);
|
||||
contracts = contracts.filter((c) => !matchedIds.has(`${c.repo}::${c.contractId}`));
|
||||
}
|
||||
const out: Record<string, unknown> = { contracts, crossLinks: registry.crossLinks };
|
||||
// `loadContractRegistryResilient` already applied `recordedRepoList` to
|
||||
// both: `undefined` here is "the last sync recorded no opinion" (a registry
|
||||
// written before the field existed, or a value we could not read), which is
|
||||
// NOT the same answer as the measured empty list.
|
||||
const { unreadableRepos, missingRepos } = registry;
|
||||
// `incompleteRepos` is dropped on this surface only because the two lists it
|
||||
// is derived from are returned verbatim right below; the truncation triple is
|
||||
// the part that has no other channel here.
|
||||
const { incompleteRepos: _incompleteRepos, ...truncation } = crossRepoCompleteness({
|
||||
unreadableRepos,
|
||||
missingRepos,
|
||||
// An unrecorded `unreadableRepos` means this listing cannot say which
|
||||
// repos the sync failed to read — so it cannot claim to be complete.
|
||||
provenanceUnknown: unreadableRepos === undefined,
|
||||
// A contract LISTING declares no scope to intersect with: it is the whole
|
||||
// registry, so every configured repo is in scope by construction. The
|
||||
// `type`/`repo`/`unmatchedOnly` filters above narrow which rows are shown,
|
||||
// not which repos the sync had to read to produce them.
|
||||
inScope: () => true,
|
||||
});
|
||||
const out: Record<string, unknown> = {
|
||||
contracts,
|
||||
crossLinks: registry.crossLinks,
|
||||
missingRepos,
|
||||
// Omitted rather than `[]` when the registry never recorded it — the same
|
||||
// convention `skippedCorrupt` follows below, and the difference between
|
||||
// "the sync measured zero unreadable repos" and "the sync never said".
|
||||
...(unreadableRepos ? { unreadableRepos } : {}),
|
||||
// The structured triple, verbatim from the impact surface (KTD10):
|
||||
// `truncated` always, `truncationReason` + `riskEpistemic` with it.
|
||||
...truncation,
|
||||
};
|
||||
if (skippedCorrupt > 0) out.skippedCorrupt = skippedCorrupt;
|
||||
return out;
|
||||
}
|
||||
|
|
@ -573,17 +672,80 @@ export class GroupService {
|
|||
}
|
||||
const registry = await readContractRegistry(groupDir);
|
||||
|
||||
/**
|
||||
* The STRICT global-registry read, deliberately — this is the one caller
|
||||
* that has to tell "the registry says nothing about this repo" apart from
|
||||
* "the registry could not be read at all", and only the strict mode can.
|
||||
* `readRegistry`'s `catch { return [] }` collapses a malformed registry
|
||||
* into an empty one, which is indistinguishable from a genuine absence and
|
||||
* would report every configured repo as having no entry — the exact
|
||||
* conflation the two labels below exist to remove.
|
||||
*
|
||||
* The consequence is accepted knowingly: the strict read rejects the WHOLE
|
||||
* registry when any single row fails to identify a repo, so one malformed
|
||||
* row renders every member of the group unresolvable, including members
|
||||
* whose own rows are fine. That is the honest verdict — a registry the
|
||||
* resolver cannot trust row-wise cannot be trusted about any row — and it
|
||||
* is reported as an unresolved state, never as a clean one.
|
||||
*
|
||||
* ENOENT is not a failure in either mode: no registry file genuinely means
|
||||
* nothing has been registered yet, so every repo is legitimately missing.
|
||||
*/
|
||||
let registryEntries: RegistryEntry[] | null = null;
|
||||
let registryReadError: string | null = null;
|
||||
try {
|
||||
registryEntries = await readRegistryStrict();
|
||||
} catch (err) {
|
||||
registryReadError = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
|
||||
const repoStatuses: Record<
|
||||
string,
|
||||
{
|
||||
indexStale: boolean;
|
||||
contractsStale: boolean;
|
||||
/**
|
||||
* Unchanged meaning: this repo has no usable status. It stays `true`
|
||||
* for BOTH failures below, so a consumer written before the split
|
||||
* still sees every unusable repo flagged. Reporting an unresolvable
|
||||
* repo as `missing: false` would hand that consumer `indexStale:
|
||||
* false` for a repo nothing was ever read from — a false all-clear.
|
||||
*/
|
||||
missing: boolean;
|
||||
/**
|
||||
* Which failure `missing` means: `false` is a genuine registry miss,
|
||||
* `true` is an entry the resolver could not turn into a repo. Additive
|
||||
* — always present on every row, so an agent can branch on it without
|
||||
* having to treat an absent key as either answer.
|
||||
*/
|
||||
unresolvable: boolean;
|
||||
/** Set only when `unresolvable`; says what could not be resolved. */
|
||||
unresolvableReason?: string;
|
||||
commitsBehind?: number;
|
||||
}
|
||||
> = {};
|
||||
|
||||
for (const [repoPath, registryName] of Object.entries(config.repos)) {
|
||||
if (registryEntries === null) {
|
||||
repoStatuses[repoPath] = {
|
||||
indexStale: false,
|
||||
contractsStale: false,
|
||||
missing: true,
|
||||
unresolvable: true,
|
||||
unresolvableReason: `the global registry could not be read: ${registryReadError}`,
|
||||
};
|
||||
continue;
|
||||
}
|
||||
// Only `resolveRepo` is inside the try that produces the
|
||||
// "did not resolve" label, so the label is earned rather than assumed.
|
||||
// `loadMeta` and `checkStaleness` cannot throw — the first returns null on
|
||||
// every error, the second catches everything — but the reading below them
|
||||
// can, and did: `registry.repoSnapshots` is read off a bare
|
||||
// `JSON.parse(...) as ContractRegistry` with no shape check, so a
|
||||
// contracts.json missing that field threw a TypeError into this catch and
|
||||
// reported every repo as an unresolvable GLOBAL-registry entry. That sent
|
||||
// the operator to repair the wrong file. The optional chain below closes
|
||||
// the crash; this split stops the next one being mislabelled the same way.
|
||||
try {
|
||||
const repoObj = await this.port.resolveRepo(registryName);
|
||||
const meta: Partial<Pick<RepoMeta, 'lastCommit' | 'indexedAt'>> =
|
||||
|
|
@ -593,7 +755,7 @@ export class GroupService {
|
|||
? checkStaleness(repoObj.repoPath, meta.lastCommit)
|
||||
: { isStale: true, commitsBehind: -1 };
|
||||
|
||||
const snapshot = registry?.repoSnapshots[repoPath];
|
||||
const snapshot = registry?.repoSnapshots?.[repoPath];
|
||||
const contractsStale =
|
||||
snapshot && meta.indexedAt ? snapshot.indexedAt !== meta.indexedAt : !snapshot;
|
||||
|
||||
|
|
@ -601,17 +763,45 @@ export class GroupService {
|
|||
indexStale: staleness.isStale,
|
||||
contractsStale: Boolean(contractsStale),
|
||||
missing: false,
|
||||
unresolvable: false,
|
||||
commitsBehind: staleness.commitsBehind,
|
||||
};
|
||||
} catch {
|
||||
repoStatuses[repoPath] = { indexStale: false, contractsStale: false, missing: true };
|
||||
} catch (err) {
|
||||
// The registry read succeeded, so its answer about this row is
|
||||
// trustworthy: a row that is there and still would not resolve is a
|
||||
// different fact from a row that was never there, and the operator's
|
||||
// next move differs (repair the entry vs. index the repo).
|
||||
const known = registryIdentifies(registryEntries, registryName);
|
||||
const reason = err instanceof Error ? err.message : String(err);
|
||||
repoStatuses[repoPath] = {
|
||||
indexStale: false,
|
||||
contractsStale: false,
|
||||
missing: true,
|
||||
unresolvable: known,
|
||||
...(known
|
||||
? { unresolvableReason: `registry entry "${registryName}" did not resolve: ${reason}` }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
group: name,
|
||||
lastSync: registry?.generatedAt || null,
|
||||
missingRepos: registry?.missingRepos || [],
|
||||
// `readContractRegistry` is a bare `JSON.parse(...) as ContractRegistry`,
|
||||
// so both of these are whatever the file happened to hold — the
|
||||
// validation in `loadContractRegistryResilient` never runs on this path.
|
||||
// A `contracts.json` carrying a string here reached `cli/group.ts` and
|
||||
// died in `.join(', ')`, i.e. an unreadable registry crashing the command
|
||||
// whose job is to explain unreadable things.
|
||||
//
|
||||
// `missingRepos` has always been required, so there is no "not recorded"
|
||||
// state to preserve for it — an unreadable value degrades to empty.
|
||||
missingRepos: recordedRepoList(registry?.missingRepos) ?? [],
|
||||
// `unreadableRepos` does have one: absent means "not recorded", not
|
||||
// "none" (see ContractRegistry), and a value we could not read is equally
|
||||
// unrecorded. Reporting either as an empty list is the same conflation.
|
||||
unreadableRepos: recordedRepoList(registry?.unreadableRepos),
|
||||
repos: repoStatuses,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import * as os from 'node:os';
|
|||
import type { ContractRegistry } from './types.js';
|
||||
import { writeFileAtomic } from '../../storage/fs-atomic.js';
|
||||
|
||||
const CONTRACTS_FILE = 'contracts.json';
|
||||
export const CONTRACTS_FILE = 'contracts.json';
|
||||
|
||||
export function getDefaultGitnexusDir(): string {
|
||||
return process.env.GITNEXUS_HOME || path.join(os.homedir(), '.gitnexus');
|
||||
|
|
@ -30,6 +30,11 @@ export function getGroupDir(gitnexusDir: string, groupName: string): string {
|
|||
return path.join(gitnexusDir, 'groups', groupName);
|
||||
}
|
||||
|
||||
/** The registry path, so callers that stat or watch the file do not respell its name. */
|
||||
export function getContractRegistryPath(groupDir: string): string {
|
||||
return path.join(groupDir, CONTRACTS_FILE);
|
||||
}
|
||||
|
||||
export async function writeContractRegistry(
|
||||
groupDir: string,
|
||||
registry: ContractRegistry,
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -100,7 +100,20 @@ export interface ContractRegistry {
|
|||
version: number;
|
||||
generatedAt: string;
|
||||
repoSnapshots: Record<string, RepoSnapshot>;
|
||||
/** Configured repos with no entry in the registry. */
|
||||
missingRepos: string[];
|
||||
/**
|
||||
* Configured repos that ARE registered but that this sync could not extract
|
||||
* from — the index would not open (version skew, lock, corruption), or an
|
||||
* extractor threw partway through. The two are one bucket because the
|
||||
* consequence is one thing: NONE of that repo's contracts are in this
|
||||
* registry. Distinct from `missingRepos`, which is "no entry in the
|
||||
* registry at all" and needs a different answer from the operator.
|
||||
*
|
||||
* Optional so a registry written before this field existed still parses —
|
||||
* absent means "not recorded", not "none".
|
||||
*/
|
||||
unreadableRepos?: string[];
|
||||
contracts: StoredContract[];
|
||||
crossLinks: CrossLink[];
|
||||
}
|
||||
|
|
@ -117,8 +130,24 @@ export interface RepoHandle {
|
|||
storagePath: string;
|
||||
}
|
||||
|
||||
/** Why local impact or fan-out stopped early (e.g. wall-clock budget exhausted). */
|
||||
export type GroupImpactTruncationReason = 'timeout' | 'partial';
|
||||
/**
|
||||
* Why local impact or fan-out stopped early (e.g. wall-clock budget exhausted).
|
||||
*
|
||||
* `'timeout'` and `'partial'` are runtime limits — the same query can succeed on
|
||||
* a retry. `'incomplete-sync'` is structural: the bridge itself was built from a
|
||||
* sync that could not read every configured repo, so those repos' contracts are
|
||||
* absent from every query against it until `gitnexus group sync` succeeds.
|
||||
*
|
||||
* A runtime array rather than a bare type union: every value here has to be
|
||||
* explained on the agent-facing surface that returns it, and only an enumerable
|
||||
* list lets a guard test assert that. A test that hand-lists the members passes
|
||||
* forever once a fourth is added — which is the exact drift the guard exists to
|
||||
* catch, so the list an agent is promised and the list the code can emit have
|
||||
* to come from the same place.
|
||||
*/
|
||||
export const GROUP_IMPACT_TRUNCATION_REASONS = ['timeout', 'partial', 'incomplete-sync'] as const;
|
||||
|
||||
export type GroupImpactTruncationReason = (typeof GROUP_IMPACT_TRUNCATION_REASONS)[number];
|
||||
|
||||
export interface GroupImpactResult {
|
||||
local: unknown;
|
||||
|
|
@ -222,5 +251,110 @@ export interface BridgeHandle {
|
|||
export interface BridgeMeta {
|
||||
version: number;
|
||||
generatedAt: string;
|
||||
/**
|
||||
* Size and mtime of the `bridge.lbug` this metadata was written for, so a
|
||||
* reader can tell whether the two still belong together.
|
||||
*
|
||||
* `writeBridge` replaces the database and writes this file as two operations;
|
||||
* a sync that stops between them leaves the PREVIOUS sync's metadata beside a
|
||||
* new database, and `runGroupImpact` reads completeness from that metadata.
|
||||
* Stamping the pair is what lets `bridgeMetaMatchesFile` reject the mismatch
|
||||
* without anything having to be deleted — deleting the old metadata up front
|
||||
* would lose it permanently on a swap that fails with the old database still
|
||||
* in place, which is a normal Windows outcome when a read-only handle is held.
|
||||
*
|
||||
* Optional: metadata written before this existed carries no stamp. Such a
|
||||
* file is not waved through — `bridgeMetaMatchesFile` falls back to comparing
|
||||
* the two files' modification times, since a successful write orders the
|
||||
* database rename before the metadata write and a database NEWER than the
|
||||
* metadata beside it therefore cannot be the one it describes.
|
||||
*
|
||||
* That fallback proves WRITE ORDER, not provenance, and is wrong in both
|
||||
* directions — a non-monotonic clock can make a mis-paired set read as
|
||||
* ordered, and any copy or restore that rewrites the database's times after
|
||||
* the metadata's demotes an intact legacy pair to a lower bound until the
|
||||
* next sync re-stamps it. A stamped pair never reaches that fallback, which
|
||||
* is the reason to prefer stamping over widening the heuristic. Both
|
||||
* directions are spelled out at `bridgeMetaMatchesFile`.
|
||||
*/
|
||||
bridgeSize?: number;
|
||||
bridgeMtimeMs?: number;
|
||||
/**
|
||||
* Reader-side only: true when `meta.json` parsed but one of its repo lists
|
||||
* held a value that was not a list of repo paths.
|
||||
*
|
||||
* NEVER PERSISTED. `readBridgeMeta` sets it to describe what it found in the
|
||||
* file; `writeBridgeMeta`'s only caller builds a fresh literal, so it cannot
|
||||
* round-trip back to disk. It lives on this interface rather than on a
|
||||
* reader-only subtype so that `readBridgeMeta` keeps the exact signature
|
||||
* every caller already compiles against.
|
||||
*
|
||||
* The unusable value is dropped rather than normalized, so `missingRepos: []`
|
||||
* on such a result is inert filler — this flag, not the empty list, is what
|
||||
* says the bridge's provenance is unknown.
|
||||
*/
|
||||
repoListsUnreadable?: boolean;
|
||||
/**
|
||||
* Reader-side only: did this metadata pair with the `bridge.lbug` beside it,
|
||||
* measured BEFORE anything opened that database?
|
||||
*
|
||||
* NEVER PERSISTED, for the same reason as `repoListsUnreadable`.
|
||||
*
|
||||
* The measurement has to happen before the open, and the answer has to be
|
||||
* carried rather than recomputed. `runGroupImpact` and `runGroupTrace` open
|
||||
* the bridge and only then ask about provenance, so a platform where a
|
||||
* read-only open advances the database's mtime would fail every unstamped
|
||||
* pair the moment it was read — turning back-compat for pre-stamp bridges
|
||||
* into a repo-wide "everything is a lower bound". Whether any given
|
||||
* LadybugDB build and OS does that is not something a reader should have to
|
||||
* know, and it cannot be observed on Windows, where the in-process
|
||||
* write→read reopen this would need is a documented limitation. Ordering the
|
||||
* check ahead of the open makes the question moot on every platform instead
|
||||
* of true on the ones that happen to be testable.
|
||||
*/
|
||||
pairedWithDatabase?: boolean;
|
||||
/**
|
||||
* PERSISTED, unlike the two fields above: the writer of this metadata could
|
||||
* not establish that it describes the `bridge.lbug` beside it, and no reader
|
||||
* may conclude otherwise from the files alone.
|
||||
*
|
||||
* Written by `refreshPreservedBridgeMeta` — the preserve path in `syncGroup`,
|
||||
* which refreshes the diagnostic lists of a bridge it deliberately does NOT
|
||||
* rebuild. That refresh rewrites `meta.json` ATOMICALLY, so this file's mtime
|
||||
* becomes now while the database's stays old; and "metadata newer than the
|
||||
* database beside it" is exactly the write order that
|
||||
* `unstampedMetaPairsByWriteOrder` accepts. A refresh that simply carried the
|
||||
* old fields forward would therefore convert a pair that check had been
|
||||
* REJECTING into one it waves through — laundering unknown provenance into
|
||||
* verified provenance, which is the fail-open this whole channel exists to
|
||||
* close.
|
||||
*
|
||||
* "Just don't write a stamp" is not a substitute, and is worse: an unstamped
|
||||
* metadata file is judged on the two file times, and the refresh has already
|
||||
* moved them into the accepting order. The verdict has to be recorded IN the
|
||||
* file, because the write that records it is itself what destroys the
|
||||
* evidence a reader would otherwise use.
|
||||
*
|
||||
* `bridgeMetaMatchesFile` rejects on this ahead of both the stamp and the
|
||||
* write-order heuristic, so `ensureBridgeReady` answers
|
||||
* `pairedWithDatabase: false` and `bridgeProvenanceUnknown` reports the
|
||||
* cross-repo answer as a lower bound. That is the ONE enforcement point; do
|
||||
* not add a second reader for this field.
|
||||
*
|
||||
* Self-clearing: a successful `writeBridge` builds fresh metadata from a
|
||||
* literal and never sets it, so the next good sync retires the marker without
|
||||
* anything having to delete it.
|
||||
*/
|
||||
provenanceUnknown?: boolean;
|
||||
missingRepos: string[];
|
||||
/**
|
||||
* Configured repos the sync that produced this bridge could not extract from
|
||||
* (see `ContractRegistry.unreadableRepos`). Their contracts and every
|
||||
* cross-link touching them are absent from `bridge.lbug`, so a cross-repo
|
||||
* impact query against this bridge is a lower bound, not a verdict —
|
||||
* `runGroupImpact` folds a non-empty value into its truncation fields for
|
||||
* exactly that reason.
|
||||
* Optional: a bridge written before this field existed does not record it.
|
||||
*/
|
||||
unreadableRepos?: string[];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -787,7 +787,7 @@ export function pickUniqueGlobalCallable(
|
|||
// because the list would then depend on the caller's scope, not just its file.
|
||||
const cacheKey =
|
||||
scopeDefsCache !== undefined && isCallerVisible === undefined
|
||||
? `${name} | ||||