Design
Internal spec and implementation plan. The README is the user intro; this file is for architecture, detailed interfaces, and decisions.
Goals
- Compose canonical
AGENTS.mdfrom small fragments. - Deterministic outputs with nearest‑wins routing and explicit overrides.
- Zero‑config defaults; ergonomic overrides when needed.
- Fast, safe, CI‑friendly; clear exit codes and reporting.
- Installable and runnable across npm/pnpm/yarn/bun.
Non‑Goals
- No network access by default.
- No runtime code execution from fragments (imports are file content only).
- No editor extensions in core (document hooks, not implement them).
Stack & Compatibility
- Runtime: Bun 1.x (Node APIs compatible); support Node 18+ via published package.
- CLI: yargs for command/flag parsing.
- Tests:
bun test. - Bundling/Publish: bunup; ship ESM with a small CJS shim if needed; add
binentry for CLI. - Invocation:
npx/pnpm dlx/yarn dlx/bunxshould work.
Architecture
- CLI (
src/cli)- yargs command modules:
init,compose,report,watch,help,version. - Minimal options; avoid advanced flags in v0.
- yargs command modules:
- Core (
src/core)config.ts: discover/load/merge config; defaults.discovery.ts: globbing, filter viaincludeFiles, ignore rules.directives.ts: parse HTML comment directives and apply per‑fragment.compose.ts: target resolution, ordering, annotation, truncation, write.report.ts: compute summaries and JSON payloads.logger.ts: leveled logs;--silentflag.fs.ts: IO helpers (safe write, banner insertion, header detection).
- Watcher (
src/watch)fs.watchwith debounced rebuild; fallback polling where needed.
CLI Spec
-
agents-md init- If a config or generated
AGENTS.mdis present, print a reminder to runagents-md composeand exit. - If
CLAUDE.mdexists, move it toproject.agents.mdand editCLAUDE.mdto onliner@AGENTS.md. - Move any existing
AGENTS.mdfiles' content to<"project" | directory-name>.agents.md. - Create a config file
agents-md.config.tswith defaults. - Run
agents-md composeto generateAGENTS.mdfiles from inputs.
- If a config or generated
-
agents-md compose- No flags in v0. Uses config/includes for discovery.
-
agents-md report- Human view
- Report on the current state:
AGENTS.mdfiles: - Directory tree for allAGENTS.mdfiles - Character size and token predictions - if oversized - Warn with colorized output on over-sizeAGENTS.mdand list their sourced inputs in descending order of size.- Bad comment directives
- Missing imports
- Bad comment directives
- JSON view for machine readability
- Human view
-
agents-md watch- Rebuild on changes quietly;
--verboseprints logs.
- Rebuild on changes quietly;
Exit codes: 0 ok, 1 error, 2 invalid config, 4 limit violation.
Quick Start
-
Install with your preferred package manager, e.g.
bun install agents-md -D -
Initialize with:
bun agents-md initThis will move any existing
AGENTS.mdorCLAUDE.mdfiles to<"project" if root | directory-name>.agents.mdand scaffold a config fileagents-md.config.tsto get started quickly. If a config or generatedAGENTS.mdalready exists, it prints a reminder to runagents-md composeinstead.Important:
AGENTS.mdfiles are now codegen and managed by agents-md. We SHOULD NOT HAND-WRITEAGENTS.mdfiles anymore. -
Write new documents or reshape existing ones in any of the following path formats:
**/agents-md/*.md**/*.agents.md**/<customised-directories>/*.md**/*.<customised-file-formats>
These files will be included in the composition process.
-
Compose
AGENTS.mdfiles with:bun agents-md composeThis will codegen
AGENTS.mdfile(s) from our inputs.
If we want to have multiple AGENTS.md files for dynamic location-based context, simply add a new empty AGENTS.md file in any target directory and rerun bun agents-md compose.
Config Schema (TypeScript)
// agents-md.config.ts
export type AgentsMdConfig = {
// Discovery
include?: string[]; // ['**/agents-md/**/*.md','**/*.agents.md']
exclude?: string[]; // ['**/node_modules/**','**/.git/**']
includeFiles?: (ctx: { path: string; cwd: string }) => boolean;
// Targets and routing
defaultTarget?: 'nearest' | 'root';
// Composition behavior
annotateSources?: boolean; // wrap fragments with `<!-- source: ... priority=n -->` and `<!-- /source: ... -->`
truncate?: { atChars?: number; strategy?: 'end' | 'middle' };
// Limits
limits?: {
warnSourceChars?: number;
maxSourceChars?: number;
warnOutputChars?: number;
maxOutputChars?: number;
};
}
export interface Fragment {
path: string;
content: string;
directives: Directive;
}
export interface Directive {
target?: string;
imports?: { path: string; line: number }[];
priority?: number;
title?: string;
}
export type TargetSelector = 'nearest' | 'root' | { path: string };
export interface Output {
path: string; // target file path (AGENTS.md)
chars: number; // output size
sources: { path?: string; chars: number }[];
}
export interface JsonReport {
outputs: Output[];
totals: { outputs: number; chars: number; sources: number };
limits?: { violated: boolean; details: string[] };
}
Config discovery: look for agents-md.config.ts, then .mjs/.js; ignore local overrides unless explicitly supported in future.
Markdown Directives (Spec)
- Syntax: HTML comments,
agents-md:prefix, space/comma separatedkey=valuepairs. - Supported keys:
target:nearest|root| relative dir (e.g.,../docs) | named target.import:@<relative-path>; multiple allowed; processed in order.priority: integer; higher numbers surface earlier among same target.title: string; optional section heading hint (no auto‑insertion by core).
- Precedence: directives >
defaultTarget. - Errors: missing import files (warn), circular imports (error), unknown target (warn).
Composition Algorithm
- Discover fragments via
includeglobs andincludeFilesfilter. - Parse directives (per fragment); resolve
imports; detect cycles; inline imported content before host content. - Determine target for each fragment: directive → nearest directory with
AGENTS.md→ root. - Group by target; sort within target by
priority(desc) then path. - Compose content; optionally annotate sources; apply truncation; check limits.
- Write outputs with banner header.
Banner header: <!-- Generated by agents-md: DO NOT EDIT DIRECTLY. Edit *.agents.md fragments instead. Higher priority fragments appear first and win conflicts. -->.
Init & Migration
- Move existing
AGENTS.mdorCLAUDE.mdinto*.agents.mdfragments. - Create
agents-md.config.tswith defaults if missing. - Optionally rewrite
CLAUDE.mdto@AGENTS.mdfor interop. - Create an empty root
AGENTS.mdwith a banner if none exists. - Never overwrite hand‑edited files without backup (e.g.,
.bak).
Reporting
- Human: tree of targets, sizes (k chars), token estimates (heuristic), top‑N sources by size, warnings.
- JSON (
--json):JsonReportabove; stable keys, deterministic ordering.
Watch Mode
- Use
fs.watchrecursively where supported; debounce (e.g., 100–300ms). - On change: re-run discovery for affected subtree; incremental compose for impacted targets only.
- Ignore writes to generated
AGENTS.mdoutputs to avoid infinite rebuild loops.
Limits & Truncation
- Limits apply to source fragments and final outputs; warn vs hard fail based on thresholds.
- Truncation strategies:
end(simple cut) ormiddle(keep head+tail with ellipsis marker).
Determinism
- Sorting is stable and deterministic across platforms.
Safety & Idempotency
- Writes only change files when content differs.
- Avoid advanced flags in v0; changes are only written when content differs.
- Generated banner prevents accidental hand edits.
Testing Strategy (bun test)
- Unit: directives parser, discovery filters, ordering, limits.
- Integration: compose on fixtures (single/multi‑target, imports, errors).
- CLI: yargs command wiring, exit codes.
- Snapshot: output content with source annotations and banners.
Packaging & Distribution
- Build with bunup: ESM bundle for library; CLI entry with Node shebang.
package.json:type: module,exportsfor ESM; addmainCJS shim if required.bin: { "agents-md": "dist/cli.js" }.- Engines:
node >=18 || bun >=1.
- Publish to npm for cross‑ecosystem install.
Telemetry & Privacy
- No telemetry. No network by default. Plugins run locally.
Roadmap
- Official plugin kit (JSDoc, TS types, DB schemas).
- Per‑target summaries and tokenization adapters.
- Cross‑target references and lightweight templating.
- Watch perf improvements and editor integrations.
- Dedupe strategies (content/path) — future option if needed.
- Import paths not working
- Comment directive paths must start with
@for Claude Code compatibility - Use relative paths from the importing file's directory
- Check file exists: ``
- Comment directive paths must start with
Roadmap
- Provider framework for more dynamic content. E.g. jsdoc extractions.
- Better hot reloading supports for ecosystem tools
- MCP cross-directory reference support
Credits
- OpenAI's efforts on
AGENTS.mdstandardization. - Inspired by Ruler for distribution to agents that don't read
AGENTS.mddirectly.