Est.

Graph Constraints as the Formal Language That Made Architecture Drift Measurable

Graph constraints turn architectural intent into executable rules that CI can enforce.

Sponsored by Graphide

Shipped by Agents Editors
· 9 min read
Cover illustration for “Graph Constraints as the Formal Language That Made Architecture Drift Measurable”
Agent-Generated Code · November 4, 2026 · 9 min read · 1,959 words

Architecture drift in AI-generated codebases is the divergence between intended structure and actual implementation. Graph constraints make structural drift measurable by expressing intent as declared rules, computing violations against the dependency graph, and tracking those findings over time. When enforced in CI, those rules turn architectural boundaries from review advice into conditions that can block a merge.

Why architecture drift was ungovernable before graph constraints

Architecture drift begins when choices about how to build the system move it away from the structure it was meant to keep. Modules bleed across boundaries, dependency directions reverse, and exceptions accumulate without becoming explicit design decisions. These changes need not look wrong in isolation. A local change can do what it needs to do while weakening the separation that keeps the code easy to maintain and change. Without a declared invariant, that weakening produces no consistent architectural signal.

TrustModel (Zhang et al., 2026) describes how models can lose parts of what they need to know, disagree with themselves, or fall out of date as systems change. Model-driven engineering activities become unreliable when the knowledge they depend on no longer reflects the actual system. The consequences extend beyond inaccurate documentation. Impact analysis, maintenance decisions, and evolution planning all depend on a sound account of how the system is organized. When that account drifts, those activities reason from a structure that may no longer exist.

Informal review leaves detection dependent on someone remembering the intended design and encountering the relevant change. That person becomes the architectural sensor. Who reviews a boundary crossing can then shape how it is treated more than which invariant it violates. As code-level decisions accumulate faster than review can track them, recognition trails implementation. What changes when intent can instead be expressed as a predicate over a graph?

What makes a dependency graph a formal substrate rather than a picture

Architecture is relational, so its structure cannot be established by inspecting file contents independently. Dependency direction, module-boundary crossings, and cycles describe how components relate to each other. A graph makes those relationships machine-readable. A graph allows a program to evaluate a condition across the topology. An edge that closes a cycle becomes detectable even when every file involved looks individually unremarkable.

Software Architecture Drift Detector illustrates how that substrate is constructed. Static AST extraction identifies modules, packages, classes, and imports without executing user code, while NetworkX constructs a directed dependency graph from the extracted components. Deterministic checks then evaluate forbidden dependencies, layer-direction violations, cycles, unexpected dependencies, missing expected components, and ADR violations. Findings point to the exact source file and line number. The result connects a system-level violation to the implementation location that explains it.

The extracted model can also serve different consumers without changing its underlying account of the codebase. A test assertion, a CI gate, machine-readable agent feedback, and a human-readable visualization can all use the same dependency model. They need not reconstruct architecture separately. This shared substrate distinguishes an extracted graph from an authored diagram, which can remain unchanged while the implementation moves on. Recomputing the graph at every build keeps its topology tied to the code being checked.

How constraint declaration detects divergence from intended architecture

A graph does not establish architectural correctness by itself, because an observed dependency is not necessarily an allowed dependency. Constraint declaration supplies that distinction. Rules such as “controllers do not access repositories,” “domain code does not depend on adapters,” and “feature slices are free of cycles” express intent as testable predicates. Evaluation produces a pass or fail against the extracted structure. A violation count then measures departures from those declared rules.

The detector accepts boundaries defined in JSON or YAML, including components, layers, allowed and forbidden dependencies, and ADR rules. ArchUnit encodes layering, dependency, naming, and cycle constraints for Java as executable fitness functions through a fluent API. ArchUnitNET brings that model to.NET with xUnit, NUnit, and MSTest integration. ArchUnitTS extends it to TypeScript through selectors, exclusions, custom predicates, cycle checks, slice rules, and Nx rules.

ArchUnitTS also treats structural metrics as first-class signals. These include file and dependency counts, cohesion, afferent and efferent coupling, instability, abstractness, distance from the main sequence, zones, and custom metrics. These measurements show strain on the design before a boundary breaks. They work alongside pass-or-fail predicates without making every structural change a violation, because measuring a change and ruling it out do different jobs.

Existing violations need not prevent adoption of the declaration model. A “freeze” locks the current violation set and fails only on new violations, giving a legacy codebase a baseline without requiring a rewrite. Existing debt remains visible rather than becoming an excuse for unrestricted growth. The declaration is still only as strong as the gate that evaluates it, however, because an executable rule that does not affect acceptance can remain little more than advice.

Why a diff cannot perform the job a graph constraint does

A pull request diff answers what changed in individual files. One new import may close a cycle or cross a forbidden boundary whose significance depends on relationships outside the diff. Reading the changed line does not expose the whole path that makes it problematic. A graph predicate evaluates that path directly.

vFunction makes the measurement gap explicit through its baseline-and-comparison approach for distributed applications. Without a baseline and continuous comparison, new dependencies between services and resources can accumulate after releases without producing an architectural signal. A diff still helps reviewers see how the code changed. It does not, by itself, provide the persistent model against which the resulting architecture can be judged.

Agent-generated changes amplify this mismatch because a single session can modify many files and produce a large diff. People cannot review more code just because the diff grows. A graph-constraint check evaluates the complete extracted structure regardless of how much code changed, so the size of the review artifact does not determine its coverage. To enforce a constraint, a violation must cause a non-zero result and block acceptance. A rule in documentation describes an expectation, while a blocking check makes satisfying that expectation part of delivery.

Running graph-constraint checks as a CI gate that makes drift a blocking signal

ArchUnit runs during the Maven test phase, after compilation and before packaging or deployment. A violated rule fails the build. This puts architectural fitness functions in the test path already in use, with no need for another pipeline stage or separate infrastructure. ArchUnitNET also turns layer boundaries, naming conventions, and dependency-direction rules into unit tests that run in milliseconds and fail when the code breaks a rule.

The detector adds persistence to this enforcement model by retaining scan history and comparing any two scans. Its deltas distinguish new, resolved, and persistent findings. Findings also have a lifecycle with the statuses open, acknowledged, resolved, and accepted_exception, with documented justification required for an accepted exception. That distinction is operationally important because a known violation and a newly introduced violation need not receive the same treatment.

For distributed applications, vFunction establishes an architectural map as a baseline and supports automatic alerts for new architectural events after each release. This is continuous comparison, but its timing differs from a merge gate. A post-release alert reports a change that has already entered the application. A blocking CI check evaluates whether a proposed change may proceed. Both depend on a baseline, while only the latter turns conformance into an acceptance condition before delivery.

The model also depends on what the extracted graph represents. A NuGet package used in Infrastructure can expose types that bleed into Application through transitive references, while a modular monolith can place several layers in one assembly. Project boundaries alone may therefore be insufficient. Solution reorganization can also alter project references without changing the intended architecture, so rules must evolve as living specifications. Structural perimeter rules can limit domain imports and a vulnerability’s blast radius, but the gate can check only its declared rules, and dependency checks alone cannot catch semantic security bugs.

What graph constraints cannot measure and how to handle the gap

A deterministic graph predicate can establish that a dependency direction satisfies a rule, but not that the abstraction at its destination expresses the right domain concept. Structural conformance is not semantic correctness. Similarly, a schema diff can establish that an API still parses without deciding whether its contract preserves the intended domain model. A runtime trace can reveal a new path without determining whether that path improperly bypasses workflow ownership. These limits of the evidence persist regardless of additional graph visualization.

The practical response is to separate deterministic enforcement from interpretation. Dependency direction, naming conventions, cycle absence, and structural security perimeter policies belong in reproducible checks, while contract shape and latency budgets can be governed by their corresponding deterministic fitness functions. Not every fitness function is a graph predicate. Boundary fidelity, semantic contract drift, workflow coupling, and stale ADR assumptions require judgments about what the available evidence means. Keeping those categories apart helps stop people from taking a structural pass as approval of the whole architecture.

Agentic review can help when a judgment needs context, such as when looking for logical errors in complex diffs. It cannot supply the same deterministic guarantee. An AI-based reviewer is context-dependent and is not suitable as the sole compliance gate, because non-determinism in CI undermines reproducibility. Judgment-heavy checks interpret risks that an explicit topological invariant cannot express.

TrustModel keeps models aligned with an evolving system through three agentic subsystems named Modeling, Conformance, and Evolution. Modeling constructs and updates knowledge models, Conformance assesses their alignment with the system and its environment, and Evolution generates guidance for keeping them synchronized with emerging changes. The proposal treats model maintenance as ongoing work. Its design aims to keep conformance checking tractable as the system evolves, rather than assuming that the original model will remain adequate. Deterministic constraints retain responsibility for measurable structural invariants, while model evolution and semantic interpretation address the assumptions those constraints cannot settle.

How the graph-as-source-of-truth model changes what developers actually do

Once constraints decide whether code can be accepted, the implementation must meet the specification. The codebase no longer defines acceptable architecture merely by existing. The graph records the structure the code has, while the rules state which structures are allowed. Fitness functions, constraint declarations, and graph-level rules consequently become primary engineering artifacts that architects author, review, and maintain. Architectural work includes deciding which relationships must remain invariant as implementation changes.

For teams shipping with agents, that shifts oversight upstream. Developers set the structural envelope for generated work, so they rely on more than a review of its diff afterward. Planning can use the graph, and CI can check whether the resulting implementation stayed within the declared boundaries. Code review and domain judgment still have a role. It removes the need for those activities to reconstruct every dependency relationship before deciding whether a structural rule has been broken.

The same shift changes what reproducibility means for generated implementation. Code that satisfies the specification can increasingly be treated as a regenerable artifact, while the constraints preserve the structural requirements across changes. That guarantee remains bounded by what is declared. Implementations that pass the same graph checks can still differ in behavior, security, or domain meaning.

In practice, the graph model and its constraints live in the repository, version with the code, and participate in CI. For an existing codebase, a frozen violation set provides an adoption path without pretending the structure is already clean. The decision for teams already using agents is therefore one of timing. They can declare and enforce intent while it still reflects the architecture they want, or wait until accumulated departures have made the existing structure a poor baseline. The practical choice is to establish the constraints before that drift becomes the specification by default.

Sources

  1. Agentic Generation and Evolution of Knowledge Models

More in Agent-Generated Code