Files
Holocron/plugins/kyberforge/docs/research/docs/microsoft-apm/instructions-primitive-schema.md
T
Defame1297andClaude Opus 5.5 ff2b8b6c1b docs(kyberforge): complete apm hooks, instructions and prompts research
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
2026-09-28 16:30:26 +00:00

10 KiB

topic, source_keys
topic source_keys
instructions-primitive-schema
apm-cli-installed-source
apm-docs-llms-full
apm-github-repo
context7-microsoft-apm

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 install uses InstructionIntegrator.find_instruction_files(), which calls find_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 compile uses discover_primitives(), which has broader globs (primitives/discovery.py): **/.apm/instructions/*.instructions.md, **/.github/instructions/*.instructions.md, and a bare **/*.instructions.md. Dependencies are searched under instructions/*.instructions.md in 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 in integrate_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_to splits 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_to joins 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 compile prints the warnings, still compiles, and exits 0.
  • apm compile --validate can never fail on primitive errors. _run_validation_mode only exits 1 when validate_primitives returns errors, which never happens. It printed "All primitives validated successfully!" for a file with no description and an empty body. The published docs describe --validate as a "frontmatter + structure check", which overstates it.
  • apm install never calls validate(). An instruction with no description and an empty body deployed silently to both targets.
  • validate_primitives also 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 applyTo scoping 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).

  • applyTo becomes a paths: list, with each glob run through yaml_double_quote.
  • description and 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_files is not consulted (apm#1662), so a hand-written .claude/rules/<same-stem>.md is 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=False for Claude. integrate_instructions_for_target returns early when <project>/.claude/ is not a directory. In a live run with targets: [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.md when .claude/rules/ is populated, and from AGENTS.md when .github/instructions/ is populated. --force-instructions (alias --no-dedup) overrides this. In a live apm compile -t claude,copilot after install, no CLAUDE.md was produced. With -t claude --force-instructions, CLAUDE.md contained a "Global Instructions" section and one ### Files matching \`` section per pattern.
  • Headings show the normalised applyTo string verbatim, including \, escapes (for example Files matching `src/**,a\,b/**` ).
  • compilation.placement.min_instructions_per_file in apm.yml controls when under-populated directories bubble their instructions up to the parent.

Gotchas

  • description never 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 audit checks 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):

  1. 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.
  2. description is a non-empty string. Source: Instruction.validate(); apm only warns during apm compile.
  3. The body is non-empty after trimming whitespace. Source: Instruction.validate(); install deploys empty rules silently.
  4. applyTo is 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.
  5. The stem is unique across the package and its dependencies, because a .claude/rules/<stem>.md collision is overwritten. Source: integrate_instructions_for_target rule-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.