Ai agents architecture contract

From rjmurillo/ai-agents · View interactive version

Ai agents architecture contract is an AI agent skill from the GitHub repository rjmurillo/ai-agents. Load-bearing design decisions for this repo as a contract you check before changing anything. Covers the asymmetric generation seam, source-of-truth per tree, hook runtime failure policy, memory tiers, plugin surfaces, invariants, and known-weak points. Use when you say `which tree is canonical`, `a

How to use this skill

Skill file

AI Agents Architecture Contract

<!-- vendor-portability: contributor-facing knowledge pack for the rjmurillo/ai-agents repo itself; intentionally references upstream paths because its audience is repo contributors, not plugin consumers. Declared paths: .claude/lib, build/generate_agents.py, build/scripts/build_all.py, build/scripts/check_plugin_manifest_parity.py, build/scripts/detect_agent_drift.py, build/scripts/generate_dispatcher.py, build/scripts/validate_plugin_version_bump.py, scripts/validation/validate_hook_anchoring.py, templates/agents -->

This skill is the map of design decisions that hold this repository up: what is canonical, what is generated, which invariants are enforced by gates, and where the structure is honestly weak. Read it before any change that touches more than one tree, any hook, any plugin surface, or any generated file. The repo's governance runs on observable evidence, not trust: active requirements name evidence that exists independent of any committed session log, so almost every claim below is backed by a gate you can run.

Triggers

Process

Phase 1: Locate the source of truth before touching any file

The generation seam is ASYMMETRIC. There is no single "templates in, everything out" pipeline. Two seams coexist (ADR-072 corrected this premise explicitly: "the seam is asymmetric"):

  1. Agents: ADR-109 B1 made templates/agents/ canonical for every agent tree. <stem>.claude.md.tmpl renders src/claude/agents/<stem>.md, <stem>.copilot.md.tmpl renders src/copilot-cli/agents/<stem>.md, which .github/agents/ links to, and <stem>.shared.md renders src/vs-code-agents/ and feeds docs/agent-catalog.md. .claude/agents/ links to src/claude/agents/ (ADR-119, status proposed 2026-10-17, implemented: true: the symlinks exist, the decision is not yet accepted) (see references/source-of-truth.md).
  2. Rules, skills, hooks: ADR-109 B2 to B4 made templates/rules/, templates/skills/, and templates/hooks/ canonical for the Claude tree. rule_templates.py, skill_templates.py, and hook_templates.py render into src/claude/, and binplace copies hooks and settings into .claude/ and links the rest (ADR-119, proposed). A shipped skill's other files are hand-maintained under src/claude/skills/<name>/. Generators emit other harnesses' mirrors. There is no commands class: ADR-064 converted every command into a skill and deleted the commands-to-skills bridge, so skills are the single user-invocable surface and check_commands_retired.py blocks a command coming back.

The source-of-truth table (artifact class, canonical source, generated output, mechanism) is in references/source-of-truth.md. For one path, run the generator-path instrument (see the Scripts section); it prints role, class, canonical source, plugin tree, install tree, and the regenerate command from the binplace rows. It does not cover paths no binplace row names, such as build/.

Direction rule: generators read canonical, write mirrors. Outside the binplace step, they never write .claude/ (binplace rows and ADR-108 template-owned skill files are allowlisted). That invariant is enforced in code: assert_no_claude_writes in build/scripts/build_all.py, applied under the comment "REQ-003-010: enforce .claude/ no-write invariant" (the guard-lines instrument prints current line numbers); any generator write under .claude/ prints REQ-003-010 VIOLATION and exits 2. ADR-108 carves out the template-owned skill files under .claude/skills/, passed to the guard as an allowlist; binplace is the other writer. When a drift check goes red, the output shows a difference, not a direction. Always answer "which side is canonical?" from the table or the generator-path instrument before editing either side. An agent once "fixed" drift by editing the source to match the generated tree (2025-12-15 incident, commit reverted); the archaeology lives in ai-agents-failure-archaeology.

build/scripts/detect_agent_drift.py is not a template-drift check: it is a weekly similarity audit between agent trees and can pass while a tree is wrong. It scores src/claude against src/vs-code-agents, and .claude/agents against .github/agents, NOT against templates. The measured trip point and the 18-section allowlist are in references/drift-detector.md.

Phase 2: Load the load-bearing decisions

The decision table (decision, record, note, why) is in references/adr-decisions.md. Read ADR status from the frontmatter with the adr-status instrument; it prints status, superseded-by, and implemented for every ADR this skill cites. These supersessions change what is binding:

The command does not say whether a mechanism is still enforced. Check the gate, not the status.

Two meta-patterns bind these together:

Name-based dispatch everywhere. Nothing static-imports an agent, skill, command, or hook. Agents are invoked by subagent_type string, skills by frontmatter name, commands by filename, hooks by command-path strings inside .claude/settings.json, and the Copilot dispatcher resolves shims from hooks.json order ("the authoritative registered set, NOT a directory listing", generate_dispatcher.py docstring). Consequence: "no caller found" does NOT mean dead code. Grep and LSP reference searches will show zero callers for live, load-bearing files. Before deleting anything, check string references (settings.json, hooks.json, frontmatter, prose) and run orphan-ref-validator; for history, use chestertons-fence.

Verification-based governance. Every rule that matters is paired with a gate that produces an inspectable artifact: no-write invariant (exit 2 plus audit entry in the generation audit log), drift (build_all.py --check, generate_agents.py --validate; the lib trees are covered by the same build_all.py --check, ADR-109 B5), plugin version-field prohibition (build/scripts/validate_plugin_version_bump.py), install parity (validate_install_parity.py), hook anchoring (scripts/validation/validate_hook_anchoring.py). The current doctrine: labels such as "MANDATORY" are not enforcement. Each active requirement must name evidence that exists without requiring a committed session log. When you add a rule, add its gate; a rule without a gate is a wish. ai-agents-change-control covers how gates sequence into the change process.

Phase 3: Understand the hook runtime and its failure policy

Registration is split by consumer. These source files are intentionally not

parity twins:

| Surface | File | Registered (re-verified 2026-10-31) |

|---|---|---|

| Claude Code direct | .claude/settings.json hooks key | 5 events, 8 groups |

| Vendored plugin source | .claude/hooks/hooks.json | 2 events, 2 groups |

| Copilot CLI mirror | src/copilot-cli/hooks/ plus its hooks.json | 0 events, 0 registrations |

Run the hook-counts instrument for the events and registrations in each file. It counts registrations only; it does not say whether a hook is repository-only or vendored.

Failure policy is PER FAMILY, not global. Do not copy a policy across families:

| Family | Policy | Where stated |

|---|---|---|

| Local Claude hooks | Follow each event contract. SessionStart cannot block; Stop may return a block decision | .claude/settings.json; agent-harness-reference |

| Generated/released hook artifacts | Prevention-first and loud: validate anchoring before release, then surface escaped launcher failures | ADR-066 D1, ADR-071 |

| Copilot dispatcher | Retired (ADR-097): all three PreToolUse shims (invoke_require_subagent_model, invoke_serena_memory_scope_guard, invoke_serena_worktree_scope_guard) are deleted, and with them the generated gate dispatcher itself. src/copilot-cli/hooks/hooks.json carries "hooks": {}; no _dispatch.py ships. The generator still owns the policy for a future re-add | Deleted manifests; build/scripts/generate_dispatcher.py |

Why the split fails loud on shipped artifacts (the 33-day silent-no-op incident), why older ADRs (ADR-008, ADR-033, ADR-035) still read fail-open, and why one dispatcher per event persists (ADR-068, Copilot CLI version history) are in references/hook-runtime.md. SessionStart hooks cannot block regardless. Harness exit and timeout details are owned by agent-harness-reference.

Phase 4: Understand the memory architecture

Architectural consequence: memories are load-bearing runtime inputs, not documentation. Deleting or renaming a .serena/memories/ file changes hook behavior in live sessions.

Phase 5: Know the plugin and product surfaces

Two Claude-format plugin.json manifests ship (src/claude, src/copilot-cli), and neither carries a version (ADR-109 B6

retired the third, .claude/.claude-plugin/plugin.json, which is absent, since .claude/ is

now the binplaced dogfood copy, not a separately marketplace-listed root):

| Tree | Plugin name | Role |

|---|---|---|

| src/claude/.claude-plugin/plugin.json | project-toolkit | canonical Claude Code manifest, compiled from templates; the sole Claude Code plugin root the marketplace lists |

| src/copilot-cli/.claude-plugin/plugin.json | project-toolkit | generated Copilot CLI mirror of the same plugin |

Other hosts ship their own manifests, outside this contract: src/codex/plugin.json, src/cursor/.cursor-plugin/plugin.json, and src/hermes/project-toolkit-delivery/plugin.json, src/hermes/project-toolkit-knowledge/plugin.json, src/hermes/project-toolkit-quality/plugin.json, and src/hermes/project-toolkit-authoring/plugin.json.

No manifest may carry a version field, and neither may a marketplace entry.

With the field absent, Claude Code resolves freshness from the git commit SHA,

which moves on every merge; with it present, freshness pins to the string until

someone hand-bumps it. Enforced by the push hook plus

.github/workflows/validate-plugin-version-bump.yml (ADR-092, which superseded

ADR-091, which superseded ADR-079). Marketplaces: .claude-plugin/marketplace.json (lists

one Claude-side plugin, project-toolkit, since ADR-109 B6 retired the

claude-agents entry) and .github/plugin/marketplace.json;

parity checked by build/scripts/check_plugin_manifest_parity.py. The npm

surface is packages/ai-agents-cli (package @rjmurillo/ai-agents, bin

ai-agents; read its package.json for the current version). Release mechanics

belong to ai-agents-generation-and-release. Run the version-gate instrument to check for a version field; it exits 1 on a hit.

Phase 6: Check the invariants table before you change anything

| Invariant | Enforced by | Breaks what if violated |

|---|---|---|

| Generators never write .claude/ (except the ADR-108 template-owned skill files) | assert_no_claude_writes in build_all.py (guard-lines instrument), exit 2 | Canonical tree gets silently overwritten by its own mirror; source of truth inverts |

| Generated trees match sources | build_all.py --check, generate_agents.py --validate, drift CI | Harness mirrors ship stale behavior; the two fix paths diverge |

| .claude/lib/ matches scripts/ packages | build_all.py --check | Plugin-distributed hooks import different code than the tested originals |

| No version field in any manifest or marketplace entry | push hook plus validate-plugin-version-bump.yml | Freshness pins to a hand-bumped string instead of the commit SHA, and the line conflicts across every concurrent plugin PR (ADR-092) |

| Generated hooks anchor to repo root, never cwd | scripts/validation/validate_hook_anchoring.py, runtime-contract tests | The 33-day silent-no-op class: hooks silently no-op in every customer install |

| No em/en dashes, block-style YAML arrays, no generated-file headers | universal.md MUST NOT 4/5, dash guards, bot reviewers | One review thread per violation, every PR |

| Hand-synced siblings change together | validate_install_parity.py (co-change, one direction: a solo src/claude/ edit is exempt) | Self-hosted copies diverge from shipped ones |

| Retrieval precedes reasoning | ADR-106, memory gates | Agents re-fight settled battles (see ai-agents-failure-archaeology) |

Phase 7: Account for the known-weak points

State these plainly when working near them; do not design as if they were sound. The dated evidence and consequence for each are in references/weak-points.md.

Anti-Patterns

Verification

Before relying on or amending this contract:

Scripts

Run any row with ( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/ai-agents-architecture-contract/scripts/instruments.py --repo . run <id>; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 ). To list the rows and check each exists, write list in place of run <id> inside the locator (exit 1 when a row is missing). Pass generator-path its path after --, still inside the locator: run generator-path -- <repo-path>.

| Id | Answers |

|---|---|

| adr-status | Status, superseded-by, implemented flag of each cited ADR (contract_probe.py adrs) |

| guard-lines | Definition and call-site lines of assert_no_claude_writes (contract_probe.py guard) |

| hook-counts | Events and registrations per hook manifest |

| generator-path | Canonical source, plugin tree, install tree, regenerate command for one path |

| version-gate | Whether any manifest or marketplace entry carries a version |

| claims | Which cited paths and test node ids no longer exist |

| drift-self-check | Whether this skill drifted from reverify.json |

Exit codes for instruments.py and contract_probe.py: 0 ok, 1 when something is found (a missing instrument file, an absent ADR, no guard definition), 2 bad arguments or configuration, 3 the target failed to start or a file was unreadable. run passes the child exit code through. Each answer covers existence and counts only. Whether a mechanism still does its job stays a judgment call from the prose above.

Provenance and Maintenance

Authored 2026-07-03, refreshed 2026-10-31 against 42e409325; facts re-verified against the working tree on 2026-10-31. reverify.json holds the machine-checkable claims; the drift command below runs them:

( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/skill-bootstrap/scripts/bootstrap_drift.py check --repo . --skills-root .claude/skills --name ai-agents-architecture-contract --require; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 )

The per-claim source and re-verify command index is in references/provenance.md.

Maintenance rule: when a reverify.json check or a row in references/provenance.md fails, fix this skill (SKILL.md and the affected reference) in the same PR as the change that broke it, and label anything newly proposed as proposed. Sibling map: pipeline operation ai-agents-generation-and-release, triage ai-agents-debugging-playbook, harness facts agent-harness-reference, flags ai-agents-config-catalog, change process ai-agents-change-control, history ai-agents-failure-archaeology, evidence bar ai-agents-validation-and-qa.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.