Re-verify the three primitive schema docs against the installed apm-cli 0.28.0 source and live installs, and add an authoring checklist to each. Corrections to the earlier docs: - Copilot hooks are not reshaped: events are renamed, paths rewritten and version: 1 added, but command/timeout are not renamed to bash/timeoutSec. - A malformed .claude/settings.json is overwritten on install, losing user content. - Instruction validate() messages are warnings only; apm compile --validate never fails on them. Refs #94 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01KkT7RSDwDbmrM9T34b6sTi
10 KiB
topic, source_keys
| topic | source_keys | ||||
|---|---|---|---|---|---|
| instructions-primitive-schema |
|
This file is checked against the installed apm-cli 0.28.0 source (primitives/models.py, primitives/parser.py, primitives/discovery.py, utils/patterns.py, integration/instruction_integrator.py, integration/base_integrator.py, integration/targets.py, compilation/agents_compiler.py, commands/compile/cli.py) and against a live apm install / apm compile of a scratch package (2026-09-28).
File location, naming, discovery
APM finds instruction files in two different ways, depending on the command.
apm installusesInstructionIntegrator.find_instruction_files(), which callsfind_files_by_glob(pkg, "*.instructions.md", subdirs=[".apm/instructions"]). It searches the package root and.apm/instructions/. The search is non-recursive, so files in subdirectories of.apm/instructions/are never deployed. Symlinks and hardlinks (link count > 1) are rejected.apm compileusesdiscover_primitives(), which has broader globs (primitives/discovery.py):**/.apm/instructions/*.instructions.md,**/.github/instructions/*.instructions.md, and a bare**/*.instructions.md. Dependencies are searched underinstructions/*.instructions.mdin both.apm/and.github/. In a live compile, the deployed.github/instructions/copies were not double-counted: 4 sources gave "Validated 4 primitives". The exact dedupe mechanism was not traced.- The deployed stem is the filename minus
.instructions.md(_extract_primitive_name; rename loop inintegrate_instructions_for_target).
Frontmatter and the Instruction model
primitives/parser._parse_instruction builds Instruction with these fields:
| Field | Source | Notes |
|---|---|---|
description |
description: |
Defaults to "" |
apply_to |
applyTo: |
Normalised by normalize_apply_to |
author |
author: |
Optional |
version |
version: |
Optional |
content |
the body |
Any other frontmatter key is ignored by the model. On Copilot, such keys still survive the verbatim copy.
applyTo grammar (utils/patterns.py):
- Scalar string. Either a single glob or a comma-separated list.
parse_apply_tosplits only on top-level commas, so brace groups stay intact:"**/*.py, **/*.{pyi,pyx}"gives["**/*.py", "**/*.{pyi,pyx}"]. Each segment is stripped of whitespace. Empty segments are dropped, so leading, trailing and doubled commas are tolerated. - YAML sequence.
normalize_apply_tojoins non-null, non-empty entries with,. A literal top-level comma inside an entry is escaped as\,, so"a,b/**"survives as a single glob. You can also write\,by hand in a scalar value. - Missing,
null, an empty string or an empty list all produce"", which means unconditional. - A non-string scalar (for example a number) is passed through
str(). There is no glob syntax validation anywhere.
Instruction.validate() messages (exact text). All three are appended to one list:
"Missing 'description' in frontmatter""No 'applyTo' pattern specified -- instruction will apply globally""Empty content"(a whitespace-only body counts as empty)
Correction to the earlier version of this doc: none of these is a hard error. AgentsCompiler.validate_primitives() turns every message into a warning (self.warnings.append(f"{file_path}: {error}")) and always returns []. Consequences, all verified live:
apm compileprints the warnings, still compiles, and exits 0.apm compile --validatecan never fail on primitive errors._run_validation_modeonly exits 1 whenvalidate_primitivesreturns errors, which never happens. It printed "All primitives validated successfully!" for a file with nodescriptionand an empty body. The published docs describe--validateas a "frontmatter + structure check", which overstates it.apm installnever callsvalidate(). An instruction with no description and an empty body deployed silently to both targets.validate_primitivesalso emits broken-markdown-link warnings (validate_link_targets) during compile.
Per-target mapping (apm install)
Copilot: verbatim. PrimitiveMapping("instructions", ".instructions.md", "github_instructions"), where copy_instruction does link resolution plus LF normalisation. In a live run, diff against the source was empty. Frontmatter is kept byte-for-byte, including a YAML-list applyTo. The published docs say Copilot splits comma-lists natively. Unverified: whether Copilot honours a YAML-list applyTo. Prefer the scalar comma form for Copilot.
Copilot user scope (~/.copilot/) uses user_primitive_overrides → copilot_user_instructions, handled by _integrate_copilot_user_instructions:
- Every instruction's body is frontmatter-stripped, so
applyToscoping is lost. - The bodies are concatenated into
~/.copilot/copilot-instructions.md, inside<!-- apm:source:<pkg> -->sections under an APM header. - A pre-existing user-authored file without the header is a collision. APM skips it with a warning unless you pass
--force.
Claude: .claude/rules/<stem>.md via _convert_to_claude_rules. Mapping: PrimitiveMapping("rules", ".md", "claude_rules", output_compare=True).
applyTobecomes apaths:list, with each glob run throughyaml_double_quote.descriptionand all other keys are dropped.- With no
applyTo, the output has no frontmatter and the body is left-stripped. - An escaped-comma glob comes out unescaped (
a\,b/**→"a,b/**").
Live output:
---
paths:
- "**/*.py"
- "**/*.{pyi,pyx}"
---
Use type hints.
- Ownership. Rule-dir files are APM-owned per file. An existing file at the target path is compared against the transformed output: it is adopted if identical and rewritten otherwise.
managed_filesis not consulted (apm#1662), so a hand-written.claude/rules/<same-stem>.mdis overwritten. - Claude user scope.
~/.claude/rules/, or$CLAUDE_CONFIG_DIR/rules/if that variable is set. All Claude primitives are supported at user scope. auto_create=Falsefor Claude.integrate_instructions_for_targetreturns early when<project>/.claude/is not a directory. In a live run withtargets: [claude, copilot]declared and no.claude/, the directory was created and the rules were deployed anyway. Which step creates it was not traced; the command integrator is a likely candidate.
| Target | Output path | Transform |
|---|---|---|
| Copilot (project) | .github/instructions/<stem>.instructions.md |
Verbatim |
| Copilot (user) | ~/.copilot/copilot-instructions.md |
Bodies concatenated; frontmatter stripped |
| Claude (project/user) | .claude/rules/<stem>.md |
applyTo → paths:; everything else dropped; no frontmatter if unconditional |
Other rule formats (RULE_FORMATS) work the same way: cursor_rules produces .mdc with globs: and derives description from the body when it is missing, windsurf_rules, kiro_steering, and antigravity_rules.
apm compile: fold-in to AGENTS.md / CLAUDE.md
compilation/distributed_compiler.py and context_optimizer.optimize_instruction_placement group instruction bodies into AGENTS.md and CLAUDE.md files, placed per directory according to the applyTo patterns.
- Dedup. Instructions are omitted from
CLAUDE.mdwhen.claude/rules/is populated, and fromAGENTS.mdwhen.github/instructions/is populated.--force-instructions(alias--no-dedup) overrides this. In a liveapm compile -t claude,copilotafter install, noCLAUDE.mdwas produced. With-t claude --force-instructions,CLAUDE.mdcontained a "Global Instructions" section and one### Files matching \`` section per pattern. - Headings show the normalised
applyTostring verbatim, including\,escapes (for exampleFiles matching `src/**,a\,b/**`). compilation.placement.min_instructions_per_fileinapm.ymlcontrols when under-populated directories bubble their instructions up to the parent.
Gotchas
descriptionnever reaches Claude. It is kept for Copilot and is used as the index text in Cursor rules and compiled context. Keep the rationale for a rule in the body if Claude readers need it.- Copilot user scope loses
applyTo. Every instruction becomes global there. - A glob is never validated. A typo gives a rule that silently never binds.
apm auditchecks deployed rules for hidden Unicode and drift. It does not validate frontmatter.
Authoring checklist
Must (an author skill enforces these; an audit skill checks them):
- The path is
.apm/instructions/<stem>.instructions.md, directly in that directory with no subdirectories, and is not a symlink. Source:find_instruction_files,find_files_by_glob. descriptionis a non-empty string. Source:Instruction.validate(); apm only warns duringapm compile.- The body is non-empty after trimming whitespace. Source:
Instruction.validate(); install deploys empty rules silently. applyTois either absent (intentionally global) or a non-empty glob / comma-list. Use top-level commas only as separators, and put alternation inside{}. Source:parse_apply_to,has_top_level_comma.- The stem is unique across the package and its dependencies, because a
.claude/rules/<stem>.mdcollision is overwritten. Source:integrate_instructions_for_targetrule-dir ownership.
Should:
6. Use the scalar string form of applyTo rather than a YAML list, because Copilot receives the source verbatim. Source: copy_instruction. Copilot's handling of list values is unverified.
7. Omitting applyTo should be a deliberate choice. Without it, the file is always-on in Claude rules and is folded into AGENTS.md/CLAUDE.md global sections. Source: _convert_to_claude_rules; Instruction.validate() warning text.
8. Keep frontmatter to description and applyTo, plus the optional author and version. No target consumes other keys, and Claude drops them. Source: _parse_instruction, _convert_to_claude_rules.
9. Keep relative markdown links resolvable from the source file. Source: validate_link_targets, resolve_links.
Audit-only (apm does not check these): glob syntax validity; globs that match nothing in the repo; duplicate stems; description quality; YAML-list applyTo on Copilot-targeted packages. apm compile --validate cannot be relied on as a gate.