Skip to content

feat(devtools): declare and enforce inter-package layering rules #433

Closed

Description

@Sinity

Parent

Part of #401. Companion to #429 (executable topology) and #432 (cross-cut manifest). Generalizes the cycle-detection signal from #430/#431.

Problem

devtools verify-cluster-cohesion (per #429) flags cycles between proposed clusters, which surfaces violations like #430 (pipeline → cli) and #431 (cli/commands ↔ cli). But cycle-detection is symmetric: it can't distinguish "surface imports substrate" (allowed) from "substrate imports surface" (forbidden). The architecture doc has a directed layering rule — substrate → products → surfaces is allowed; the reverse is not — but no lint enforces it.

Without an explicit layering manifest, every new cycle-violation needs a human to read it and decide which direction is "wrong." That decision should be encoded once and checked on every commit.

Target

Declare allowed inter-package edges as a YAML graph and lint imports against it.

# docs/plans/layering-rules.yaml
layers:
  substrate:
    packages: [lib, sources, pipeline, storage, schemas]
  products:
    packages: [products]
    may_import: [substrate]
  surfaces:
    packages: [cli, mcp, site, rendering, ui, browser_capture, api]
    may_import: [substrate, products]
  verification:
    packages: [proof, scenarios, showcase, devtools]
    may_import: [substrate, products, surfaces]   # verification reads everything

exceptions:
  - from: pipeline
    to: cli
    reason: "tracked by #430; remove after pipeline → lib/embeddings/ move"
    until: "#430"
  - from: cli/commands
    to: cli
    reason: "tracked by #431; remove after shared-helper subpackage lands"
    until: "#431"

devtools verify-layering walks every polylogue/**/*.py, builds the import graph, and asserts each edge is allowed by the rules. Exceptions are tracked with the issue that's removing them; when the issue closes, the exception's lint run fails until the exception is removed.

Why this is higher-leverage than ad-hoc cycle hunting

  • Direction-aware: a cycle isn't always wrong; a layering violation always is.
  • Forward-looking: catches future violations on the first commit, not after a refactor.
  • Documented invariant: docs/architecture.md says "substrate → surfaces"; the YAML makes that statement executable.
  • Drives smaller refactors: each tracked exception (with until: #NNN) becomes a small focused PR. Layering rules turn verify-layering into a TODO list that depletes as exceptions close.

Scope

Phase 1 — manifest + lint (this issue)

  • Author docs/plans/layering-rules.yaml with the layer hierarchy and current exceptions (start by enumerating real violations from verify-cluster-cohesion output).
  • Build devtools/verify_layering.py (~120 LOC) that loads the YAML, walks imports, and reports forbidden edges + stale exceptions.
  • Wire into devtools verify --quick.
  • Document the layer hierarchy in docs/architecture.md per docs(architecture): refresh placement rules and document cross-cuts #427.

Phase 2 — cross-cut tags (overlap with #432)

The same YAML can carry the cross-cut axis (api: sync|async, layer: read|write) for #432's cross-cut manifest. Decide whether to merge into one file (docs/plans/architecture-rules.yaml) or keep separate (layering-rules.yaml + cross-cuts.yaml).

Phase 3 — proof obligation

Register architecture.layering_holds, architecture.no_stale_exceptions as proof obligations under #432's umbrella so layering becomes first-class evidence.

Acceptance criteria

Phase 1:

  • docs/plans/layering-rules.yaml exists with the documented hierarchy.
  • devtools verify-layering exits 0 against the realized tree (with current exceptions accepted as tracked).
  • Each tracked exception names an issue (until: #NNN); closed-issue exceptions fail the lint.
  • docs/architecture.md documents the hierarchy.

Phase 2:

Phase 3:

  • Layering obligations appear in the verification catalog and route via devtools affected-obligations.

Pattern reuse

The shape — declarative YAML + lint + tracked exceptions with sunset issues — is the same shape as #429 (topology projection) and #432 (cross-cut manifest). This issue applies the pattern to the directional dimension that those don't capture.

Combined, the three issues give Polylogue four kinds of evidence-driven structural specification:

  1. Placement (feat(devtools): make architectural cohesion executable #429): every file is at a known target path.
  2. Cohesion (feat(devtools): make architectural cohesion executable #429 cluster check): proposed subpackages don't have cross-cluster cycles.
  3. Layering (this issue): inter-package imports respect a directed hierarchy.
  4. Cross-cut (feat(devtools): evidence-driven structural specification — cross-cut manifest, witness, dashboard #432): modules respect axis tags (read/write, async/sync, etc.).

Related

Sequencing

Blocked by #429 item 4 (topology obligation family in polylogue/proof/catalog.py). Without obligations, layering violations surface as raw lint output and cannot be routed through devtools affected-obligations — which defeats the point of declaring layering as first-class evidence.

Order of operations:

  1. feat(devtools): make architectural cohesion executable #429 item 4 lands — topology.kernel_rule_holds etc. exist as obligations.
  2. feat(devtools): evidence-driven structural specification — cross-cut manifest, witness, dashboard #432 lands — cross-cut tag coverage reaches the threshold needed for layering rules to reference cross-cut metadata (sync vs async, surface family) when the package alone is ambiguous.
  3. This issue lands — docs/plans/layering-rules.yaml plus devtools verify-layering-rules plus a layering.no_inverted_edges obligation family.

Until #429 item 4 closes, this issue is phase-3 work and should not be picked up.

Acceptance (executable)

devtools verify-layering-rules      # 0 inverted edges
devtools affected-obligations <changed-files>   # layering obligations route correctly

Cross-reference: holistic assurance umbrella

Filed 2026-04-27. This issue is part of the holistic assurance coverage map (#506) which restructures verifiability work around four tracks (stop lying / coverage map / routing-proof-pack / product confidence) and introduces an Oracle primitive (#507), declarative coverage manifests (#508 assurance-domains, #509 oracle-quality), and operator-facing proof packs (#510).

Read in conjunction with #506 to see how this issue fits into the broader confidence model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:devtoolsphase:architecturetheme:verifiabilitytype:feat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions