Skip to content

feat(devtools): make the surface layering rule real via a ratchet baseline - #3452

Merged
Sinity merged 3 commits into
masterfrom
feature/refactor/layering-import-ratchet
Jul 31, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/refactor/layering-import-ratchet

Conversation

@Sinity

@Sinity Sinity commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

docs/plans/layering.yaml's cli/mcp/api/daemon rules gain real
disallow blocks, enforced through a checked-in known-violations ratchet
baseline rather than a mass migration, and devtools verify layering is
wired into the required per-PR lint CI job.

Problem

CLAUDE.md and docs/architecture-spine.md both assert "Surfaces may not
import substrate internals directly (docs/plans/layering.yaml enforces
this)". In the manifest, every substrate rule (storage/pipeline/
sources) carries a real disallow list; every surface rule (cli/mcp/
api/daemon) carries only a description. devtools/verify_layering.py's
rule loop only emits a violation when disallow/allow entries exist, so
the four surface rules were structurally inert — devtools verify layering
reported "No layering violations found" regardless of what surface code
imported.

Measured (via the tool's own _collect_imports, i.e. self-consistent with
what the new rule actually checks): 311 direct substrate imports across
cli (spread across the package)/mcp/api/daemon. A raw git grep -c
line count gives ~409–433 depending on how you count multiple names imported
from one module on one from x import a, b line — both numbers describe the
same real gap, just at different granularity; the tool's own de-duplicated
per-(file, module) count is what the new rule actually enforces against.

Also not previously true: .github/workflows/ci.yml's required lint job
never ran devtools verify layering — it was reachable only through the
local pre-push devtools verify --quick, so a green gh pr checks never
meant this check ran.

Solution

Turning the rule on outright would have produced an immediate, permanent red
gate (311 violations, none of which are a regression — the rule was simply
never checked before). Instead:

  • Added disallow.from: [polylogue/storage, polylogue/pipeline, polylogue/sources] to all four surface rules, mirroring the already-real
    reverse-direction rules.
  • Added ratchet-baseline support to verify_layering.py
    (disallow.baseline: <path>): a checked-in JSON inventory of exact
    (target, file, import) triples. A baselined violation is reported
    separately (informational) and does not fail the gate; anything not
    in the baseline fails. An entry stops being exempt the instant its exact
    triple stops reproducing (moved/renamed imports are not silently
    re-grandfathered), and the tool reports "N baseline entries no longer
    reproduce" so shrinking the file over time is discoverable rather than
    merely theoretically possible.
  • docs/plans/layering-surface-baseline.json: the 311-entry baseline,
    generated by running the tool itself against the new disallow list, not
    hand-typed — so it is byte-for-byte consistent with the matching logic
    that will check it going forward.
  • Wired devtools verify layering into the required per-PR lint CI job.
  • Taught polylogue/verification/manifests/models.py's
    LayeringConstraint pydantic model (extra="forbid") about the new
    baseline field — devtools verify manifests failed on the first pass
    because this schema didn't know the key existed.
  • Corrected CLAUDE.md and docs/architecture-spine.md: the reverse
    direction (substrate must not import surfaces) is real and
    zero-exception; the surface direction is a ratchet over a large
    pre-existing baseline, not a clean boundary — stated as such now.

Shape of the 311 (AC #2)

One-off AST classifier (TYPE_CHECKING-guarded imports vs. imports whose
name reappears in the importing module's own __all__, as a re-export
proxy) over the same import set:

Surface total TYPE_CHECKING-only re-export candidate runtime
cli 121 14 3 104
mcp 16 7 0 9
api 74 17 0 57
daemon 222 6 1 215
total 433 44 4 385

(433 here counts every ImportFrom node incl. duplicates across lines
within a file for the classifier pass; the enforced baseline dedupes to 311
unique (file, module) pairs via _collect_imports.) ~89% (385/433) are
genuine runtime imports of substrate internals from surface code, not
type-only or re-export noise — this is real, if long-tolerated, coupling,
not a false-positive-heavy measurement artifact.

Which AC option was chosen (AC #1)

Baseline/allowlist so the count can only shrink — not "enforce on an
already-clean subset", because no surface package is currently clean
(cli=121→ dedup ~104-121, mcp=16, api=74, daemon=222 all >0 before dedup;
the enforced/deduped counts are smaller but still all nonzero). There is no
clean subset to carve out a zero-exception rule from yet.

Verification

  • devtools verify layering: exits 0, reports "311 pre-existing baselined
    violation(s) exempted". Manually inserting an unbaselined disallowed
    import and re-running: exit 1 (ratchet fires). Removing it: exit 0 again
    (no false positive). Manually inserting a stale baseline entry: "1
    baseline entr(y/ies) no longer reproduce" hint fires without failing.
  • devtools test tests/unit/devtools/test_topology_gates.py: 24 passed
    (renamed one test whose name asserted the now-invalidated "cli importing
    storage is ok" claim; added 5 new tests covering _load_baseline parsing
    and the ratchet's exempt/fail/stale-report behavior end-to-end through
    verify_layering.main()).
  • devtools verify manifests: "manifest consistency checks passed" (after
    the pydantic model fix).
  • mypy --strict on all touched files: no issues.
  • ruff check/format --check: clean.
  • devtools render all --check: no drift.
  • devtools verify ci-workflows: 17 workflow files checked, no errors (one
    pre-existing, unrelated warning about interactive-pty's -n 0 argument).
  • devtools verify --quick: exit_code 0 (full quick lane, rerun after
    rebasing onto the latest origin/master).

Ref polylogue-2ciy

Sinity and others added 2 commits July 31, 2026 14:31
…eline

Problem: docs/plans/layering.yaml's cli/mcp/api/daemon rules carried a
description and no `disallow` key, so devtools/verify_layering.py's rule
loop (only appends a violation when `disallow`/`allow` entries exist) was
structurally unable to flag anything under them -- "Surfaces may not
import substrate internals directly (docs/plans/layering.yaml enforces
this)" (CLAUDE.md, docs/architecture-spine.md) was asserted but not
enforced. Measured: 311 direct substrate imports across the 4 surface
packages via the tool's own per-(file, module) import collector (a raw
`git grep -c` line count gives ~409-433 depending on how you count
multiple names imported from one module on one line -- see below), and
`devtools verify layering` reported "No layering violations found."

Turning the rule on outright would have produced an immediate, permanent
red gate: 311 violations, none of which are wrong today (nothing here
regressed; the rule was simply never checked). A rule that starts red
and can never plausibly go green is not a real gate.

Solution -- ratchet, not a clean-cut boundary:
- Added `disallow.from: [polylogue/storage, polylogue/pipeline,
  polylogue/sources]` to the cli/mcp/api/daemon rules (mirroring the
  already-real reverse-direction rules on storage/pipeline/sources).
- Added `disallow.baseline: <path>` support to verify_layering.py: a
  checked-in JSON inventory of exact (target, file, import) triples,
  generated by running the tool itself against origin/master with the
  new disallow list active. A baselined violation is reported
  separately (informational) and does not fail the gate; anything NOT
  in the baseline fails. The baseline can only shrink safely (an entry
  stops being exempt the moment its exact triple stops reproducing) and
  every new violation is visible in review as a diff to the JSON file.
  Also reports "N baseline entries no longer reproduce" so shrinking the
  file is discoverable, not just theoretically possible.
- `docs/plans/layering-surface-baseline.json`: the 311-entry baseline,
  generated by the tool (not hand-typed) so it is self-consistent with
  the exact matching logic that will check it going forward.
- Wired `devtools verify layering` into the required per-PR `lint` CI
  job (previously only reachable via the local pre-push `devtools
  verify`, so a green `gh pr checks` never meant this ran).
- Corrected CLAUDE.md and docs/architecture-spine.md to state the real
  shape: the reverse direction (substrate must not import surfaces) is
  a real, zero-exception rule; the surface direction is a ratchet over
  a large pre-existing baseline, not a clean boundary.

Shape of the 311 (measured via a one-off AST classifier, see PR body for
the full breakdown): ~44 are TYPE_CHECKING-only (already effectively
safe, no runtime coupling), ~4 look like re-exports (imported name
appears in the importing module's own __all__), and the remaining ~89%
are genuine runtime imports of substrate internals from surface code --
this is not mostly noise, it is real, if long-tolerated, coupling.

Verification:
- devtools verify layering: exits 0, reports "311 pre-existing baselined
  violation(s) exempted"; manually inserting an unbaselined disallowed
  import and re-running confirms exit 1 (ratchet fires); removing it and
  re-running confirms exit 0 again (no false positive).
- Manually inserting a stale baseline entry confirms the "N baseline
  entries no longer reproduce" hint fires without failing the gate.
- devtools test tests/unit/devtools/test_topology_gates.py: 24 passed
  (renamed one test whose name asserted the now-invalidated "cli
  importing storage is ok" claim; added 5 new tests covering
  _load_baseline parsing and the ratchet's exempt/fail/stale-report
  behavior end-to-end through verify_layering.main()).
- mypy --strict on devtools/verify_layering.py and the touched test
  file: no issues.
- ruff check/format: clean.
- devtools render all --check: no drift.
- devtools verify ci-workflows: 17 workflow files checked, no errors
  (one pre-existing, unrelated warning about interactive-pty's `-n 0`
  argument being misparsed as a path).

Ref polylogue-2ciy

Co-Authored-By: Claude <[email protected]>
…ne key

Problem: adding disallow.baseline to layering.yaml (previous commit) broke
devtools verify manifests -- LayeringConstraint's pydantic model had
extra="forbid" and no baseline field, so every rule using the new key
failed with "Extra inputs are not permitted". Missed in the previous commit
because devtools verify layering was run directly rather than the full
devtools verify --quick gate.

What changed: added baseline: str | None = None to LayeringConstraint.

Verification: devtools verify manifests -- "manifest consistency checks
passed". devtools verify --quick -- exit_code 0 (previously 1 on this
step). mypy --strict / ruff check+format on the touched file: clean.

Ref polylogue-2ciy

Co-Authored-By: Claude <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7d2ba54f-507a-49e9-9711-42b2530e4ac6

📥 Commits

Reviewing files that changed from the base of the PR and between decca43 and 61a45ed.

📒 Files selected for processing (9)
  • .beads/issues.jsonl
  • .github/workflows/ci.yml
  • CLAUDE.md
  • devtools/verify_layering.py
  • docs/architecture-spine.md
  • docs/plans/layering-surface-baseline.json
  • docs/plans/layering.yaml
  • polylogue/verification/manifests/models.py
  • tests/unit/devtools/test_topology_gates.py
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor/layering-import-ratchet

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment-only export after adding implementation-trail note to
polylogue-2ciy pointing at PR #3452.

Ref polylogue-2ciy

Co-Authored-By: Claude <[email protected]>
@Sinity
Sinity merged commit 5a010c3 into master Jul 31, 2026
3 checks passed
@Sinity
Sinity deleted the feature/refactor/layering-import-ratchet branch July 31, 2026 12:39
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.

1 participant