Repository navigation
Description
Problem
.claude/rules/*.md is discovered by nothing in mecatl. An exhaustive grep for a rules directory across engine/, internal/, and cmd/ returns only permission-config rules: YAML keys and prose. The only mentions of the path are in mecatl's own .claude/agents/*.md bodies and in docs/harnesses/*, which describe it correctly as a Claude Code feature:
.claude/rules/provides path-scoped rules (a per-glob equivalent).
—docs/harnesses/02-twelve-patterns.md:57
So it is a documented gap, never an implemented one. Unlike agents/skills/commands (which exist but are walled in the root module — see #328), there is no implementation anywhere to graduate. This one has to be built.
Why it bites
prompt.RootAssembler reads exactly two files at the workspace root — AGENTS.md, else CLAUDE.md (engine/prompt/builder.go:310-313). Claude Code's official guidance is to keep CLAUDE.md under ~200 lines and split the rest into .claude/rules/*.md (docs/harnesses/03-claude-code-architecture.md:312). A repo that follows that guidance and then runs mecatl silently loses every rule it split out. Not a warning — a silent no-op.
Concretely, jerm-dro/fogburn's CLAUDE.md asserts:
Project rules live in
.claude/rules/. They are auto-loaded: a rule with nopaths:frontmatter always applies; a path-scoped rule applies only when working in the matching files.
True of Claude Code, false of mecatl. Its .claude/rules/testing.md — 55 lines of real Go test policy (testify over t.Fatalf, flat test bodies, tier-by-build-tag) — never reaches the model. The repo's CLAUDE.md is actively lying to the agent about its own context, and there is no diagnostic that would tell anyone.
Proposal: a RulesSource port + a Claude-compatible FS implementation, both in engine/
Mirror the shape already established by SkillSource / AgentSource / CommandSource: a small consumer-defined port in the engine core, a filesystem adapter beside it, wired at the composition root.
1. The port — engine/tool.RulesSource (or engine/prompt)
A logical, path-free seam so a non-filesystem backend (a gRPC driver, a DB) can satisfy it — same discipline as tool.SkillSource:
// A rule is a body plus the OPTIONAL path globs that scope it.
type Rule struct {
Name string // logical id, e.g. "testing"
Body string // the markdown body, frontmatter stripped
Paths []string // glob patterns from `paths:` frontmatter; empty = always applies
}
type RulesSource interface {
ListRules(ctx context.Context) ([]Rule, error)
}2. A prompt.RulesAssembler implementing InstructionAssembler
The seam already exists and is explicitly documented for this (engine/prompt/instructions.go:14-18: "the seam that makes scoped context assembly (pattern 2) pluggable … so a richer adapter … can be wired at the composition root without touching the loop"). Compose it via the existing MultiAssembler in buildInstructionAssembler (internal/app/build.go:2483-2500), after RootAssembler. Fail-soft like SoulAssembler: a source error contributes nothing and never aborts a run.
3. engine/adapter/rulesfs — the Claude-compatible implementation
- Reads
<workspace>/.mecatl/rules/*.mdand<workspace>/.claude/rules/*.md, plus user-tier$XDG_CONFIG_HOME/mecatl/rulesand~/.claude/rules— the same four-tier precedence-orderedResolveSourcesshape asskills.ResolveOptions/agents.ResolveOptions. - Parses YAML frontmatter for
paths:(reuse the sameSplitFrontmatterhelper the agents/skills discovery uses). - Trust-gated identically:
IncludeProjectTierhonouringTrustProject. A rule body steers the model exactly likeAGENTS.md, so it belongs in the same trust class —internal/app/agentdefs.go:1207andinternal/app/build.go:3736are the precedent, including the WARN when the project tier is withheld.
The hard design question: eager vs. lazy
This needs deciding before implementation, and it is why the issue is scoped as it is.
Instructions.Assemble is invoked exactly once per run, under r.fragmentsOnce (engine/agent/loop.go:1744-1755), and the resulting fragments are prepended to every turn as a byte-stable prefix. That is deliberate — it is what keeps the turn-0 prefix cache-stable — but it means a path-scoped rule cannot be lazily activated through this seam. By the time the model touches foo_test.go, assembly has already happened and the prefix is frozen.
Two viable shapes:
- (a) Eager, all rules — recommended for v1. Inject every discovered rule at turn 0,
paths:included, each fenced with its globs stated as a condition the model applies itself ("these apply when working in**/*_test.go"). Cheap, cache-stable, zero loop changes, and strictly better than today's silent drop. Costs context proportional to total rule bytes — so cap it (byte ceiling + count ceiling, fail-soft with a WARN, mirroring the soul's byte cap). - (b) Lazy, genuinely path-triggered. Matches Claude Code's semantics but requires a new injection point after a tool call reveals which files are in play — the
PostToolUsehook phase (engine/governance/hookevent.go:20) is the nearest existing seam, but it rewrites tool results rather than appending context, and any post-turn-0 injection perturbs the cache-stable prefix invariant (prompt-cache gauntlet ACP: carry the originating tool-call id on hook events #6). Out of scope here; file separately if wanted.
Recommend (a), with the Paths field carried through the port from day one so (b) is additive later, not a breaking change.
Scope
In scope: the RulesSource port; prompt.RulesAssembler (eager, fail-soft, byte-capped); engine/adapter/rulesfs (four-tier resolve, frontmatter parse, trust-gated project tier); depguard + layering entries; composition wiring in internal/app behind a flag pair consistent with its siblings (--rules-dir repeatable / --rules-conventional); a startup diagnostic when rules are discovered and when the project tier is withheld; COMPATIBILITY.md + CHANGELOG.
Out of scope: lazy path-triggered activation (above); a remote rules-source-url gRPC driver (additive later, the port makes it possible); any @import syntax; a RuleDraft writable counterpart.
Acceptance
- A workspace with
.claude/rules/testing.mdgets that body into turn-0 context, provenance-marked, as a user-role message (never system — same rule as project instructions,engine/prompt/builder.go:315-320). - A rule with
paths:frontmatter is injected with its globs stated; a rule without one is unconditional. - Project-tier rules are withheld on an untrusted workspace, with the WARN its siblings emit; user-tier and explicit
--rules-dirstay active. - Over the byte cap → truncated or dropped with a WARN, never an aborted run.
- No source, an unreadable dir, or malformed frontmatter → no fragment, no error.
- No new non-stdlib dep in
engine/go.mod.
Payoff
.claude/rules/ is the convention Claude Code's own docs push users toward once CLAUDE.md outgrows 200 lines. Today mecatl drops it on the floor in silence, so any repo that followed that advice is running with a fraction of its stated policy — and cannot tell. This closes the last unimplemented .claude/* convention, and does it through a public port so an engine importer gets it too.
Sibling of #328 (agents/skills graduation). Both are prerequisites for an engine importer having Claude-convention parity.
Activity
test(e2e): cover the untrusted-workspace rules path (#329)
feat(prompt): RulesSource port + .claude/rules discovery (#329) (#334)