Appendix A - 3. Dynamic context injection

The Structure of Dynamic context injection — its shape at a glance:

One slicing operator runs in two directions. Forward maps target files to the constraints that will govern them and pushes them into the brief; reverse maps a diff's line ranges to the findings it introduced and feeds a self-heal gate.

flowchart LR
  Files[/Target files/] -->|forward: files → constraints| Slicer{{Slicing operator}}
  Reg[(Constraint registries)] --> Slicer
  Slicer -->|forward| Brief[Injected into brief]
  Diff[/git diff ranges/] -->|reverse: diff → findings| Slicer
  Slicer -->|reverse| Heal[Self-heal gate]

Accessible description: a slicing operator reads the constraint registries and runs in two directions — forward, mapping target files to the constraints injected into the brief before the agent writes, and reverse, mapping a diff's line ranges to the findings it introduced, which feed a self-heal gate.

Projected from the catalogue entry context-and-dispatch / Dynamic context injection.

On this page: Intent · Motivation · Applicability · Structure · Sample Code · Consequences · Example use within DocAble · Related Patterns

Intent

Intent — Map the files an agent is about to touch to the exact constraints that govern those files (lints, conventions, component boundaries, tests) and inject that subset into the agent's brief before it writes code, moving detection left of the cheapest CI gate.

Motivation

The recurring failure class is constraint under-specification at dispatch time. A coding agent lacks the tacit knowledge an experienced engineer has of which rules apply to a given change, so it makes plausible edits that violate them, then spends rounds (and tokens) discovering and repairing the violations. Call it "pinball." A layered validation hierarchy (pre-commit → merge check → deploy gate) makes it worse: context is lost between where the agent authored the change and where the failure surfaces. Across many concurrent agents, this wasted work multiplies.

Applicability

  1. File-addressable constraints. Every constraint declares its scope (file patterns / component tags) and lives in a registry you can query by file. Anything not addressable by file scope cannot be sliced.
  2. Self-documenting constraints. Each rule carries a name, docstring, and an actionable "How to fix" line. This is the decisive prerequisite: the value of an injected constraint is bounded by the agent's ability to act on it, and its inverse, if you can't clearly explain a rule to an agent, the rule's own defense value is questionable.
  3. A slicing operator at two granularities, file-scope (forward) and line-range (reverse), built as multiple adapters, one per registry (lint fleet, component registry, banned-API list, test corpus, doc index).
  4. An injection point in the dispatch pipeline (the brief-authoring step) plus the on-demand CLI.

Structure

The Structure diagram appears at the top of this page.

Sample Code

The forward slicer intersects each registered constraint's file-scope against the agent's target files and renders the matches (name, docstring, fix-hint) into the brief. The value of an injected constraint is bounded by the agent's ability to act on it, so the fix-hint is not optional.

import fnmatch

# each constraint declares the file globs it governs + how to fix it — a self-documenting registry
CONSTRAINTS = [
    {"name": "no-raw-lib", "scope": ["model/**"], "fix": "route mutation through the typed seam"},
    {"name": "typed-new-files", "scope": ["**/*.py"], "fix": "add the file to the strict type set"},
]

def constraints_for(files: list[str]) -> list[dict]:
    """Forward slice: which declared constraints govern *these* files?"""
    hits = []
    for c in CONSTRAINTS:
        if any(fnmatch.fnmatch(f, pat) for f in files for pat in c["scope"]):
            hits.append(c)
    return hits

def render_block(files: list[str]) -> str:
    lines = ["## Constraints governing your target files"]
    for c in constraints_for(files):
        lines.append(f"- **{c['name']}** — fix: {c['fix']}")
    return "\n".join(lines)

if __name__ == "__main__":
    import sys
    print(render_block(sys.argv[1:]))   # paste the block into the brief before dispatch

Consequences

Example use within DocAble

Contents
© James C. Davis, 2026–present