Skip to content

fix(grounding): read root grounds_to beside a mex map and consolidate on write (#226) - #242

Merged
theyashasvipandey merged 1 commit into
mainfrom
fix/226-root-grounds-to
Sep 24, 2026
Merged

theyashasvipandey merged 1 commit into
mainfrom
fix/226-root-grounds-to

Conversation

@theyashasvipandey

Copy link
Copy Markdown
Collaborator

Closes #226.

Problem

A file could keep groundings both in a root grounds_to and under mex.grounds_to, and setup produced that shape itself: population writes root groundings, then Wiki migration gives multi-entity files a mex: map without moving them. check then read only mex.grounds_to and wiki for-code dropped the root entries.

Change

  • Read the union. extractGroundings returns both keys, deduplicated by node. On a same-node conflict the mex.grounds_to entry is returned. The rule lives in src/wiki/markdown/grounding-stores.ts, shared by check and the Wiki codec.
  • Keep one store on write. writeGroundings on a mixed file moves root entries under mex.grounds_to and removes the root key in a single edit; the rest of the file is byte-identical and a second write is a no-op. A root entry that was not in effect (a conflict, or one that fails validation) stays at the root rather than being deleted.
  • Wiki. The codec attaches root groundings to the file-level entity, never a section entity. A file with only section entities keeps them unattributed and reported.
  • Migration. Root groundings move to the file-level entity however many sections the file has, including files an earlier run already adopted. That uses a new set-grounding option, absorbRootGroundings, which moves existing values without re-deriving them from the graph, so old bodyHash baselines survive. It is refused on a section entity and if it would change the groundings it moves. An ordinary set-grounding now replaces the root key too.
  • Diagnostic. check and wiki validate report GROUNDING_MIXED_SHAPE: info for a split, warning for a same-node conflict. The check side needs no graph.
  • Setup. The population and mex graph ground prompts say to write under mex.grounds_to once a file has a mex: map; the migration change stops setup producing the shape.

Survey (real scaffolds, before → after)

scaffold groundings seen by check seen by wiki for-code
Hono 24 20 → 24 11 → 15
Project set up with 0.8.2 42 38 → 42 18 → 22
Older populated project 27 24 → 27 22 → 25
Another older project 16 16 → 16 16 → 16

for-code was measured with both CLI builds on copies; check counts apply the old and new read rules and are confirmed on Hono by the drift run below. The remaining root groundings are in files with no file-level entity (a decisions log with only section entities, and custom context files migration never adopted). check reads them; the Wiki can't attach them without guessing a section.

CLI reproduction (Hono copy)

Edited the body of compose() and a comment inside errorHandler, refreshed, ran mex check:

  • Before: GROUNDING_DRIFT only in patterns/debug-request-pipeline.md.
  • After: drift also in context/architecture.md and context/conventions.md, plus info GROUNDING_MIXED_SHAPE for both.
  • wiki migrate then moved the 4 split groundings, kept their original bodyHash (drift still reported), wiki validate no longer reports the shape, and a second migrate changes no bytes.

Tests

  • New test/grounding-mixed-shape.test.ts (22 tests) and 4 operation tests: extraction on root-only, mex-only, mixed, duplicate and conflict; consolidating writes (byte-identical, idempotent, CRLF); the compose()/errorHandler drift scenario against a real graph; for-code attaching to the file-level entity only; migration of adopted files including conflict and malformed entries; setup finalization leaving no mixed file.
  • 24 of the new or changed tests fail on the original source; the drift test fails with drift found only in the pattern file.
  • Existing expectations changed on purpose: legacy/grounds-to-single.md now expects GROUNDING_MIXED_SHAPE, and the adversarial tier-3 test now expects a file-level move (the old "left at the root" assertions cover a section-only file).
  • Typecheck is clean; the Wiki, operations, markdown, drift, sync, setup e2e and graph grounding suites pass locally. The full suite has not completed locally (the run was stopped for low memory), so CI is the first full run.

Accepted cost

The first write to a split file moves its root groundings and, on an adopted file, bumps the entity revision, so that file shows a frontmatter diff. A file-level entity now also carries the file's root groundings.

… on write (#226)

check and wiki for-code read both grounding stores; writes, wiki migrate
and set-grounding fold root groundings into the file-level entity's
mex.grounds_to, never a section. Report GROUNDING_MIXED_SHAPE (info,
warning on a same-node conflict). Setup no longer produces the shape.
@theyashasvipandey
theyashasvipandey merged commit 975f152 into main Sep 24, 2026
9 checks passed
@theyashasvipandey
theyashasvipandey deleted the fix/226-root-grounds-to branch September 24, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Root-level grounds_to is invisible to check and wiki for-code in files that also have a mex: map

1 participant