Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/module-targets-root-only.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@timothycrooker/ai-context-core": minor
"@timothycrooker/ai-context-templates": patch
---

Reject module `targets` that name a non-root target instead of silently dropping the module.

`modulesBody()` is only ever called with `"root"` — scoped outputs (`apps/<pkg>/AGENTS.md`, `apps/<pkg>/CLAUDE.md`, `.claude/rules/*.md`) are composed entirely from a scope's includes. A module declaring `targets: [mobile]` passed validation, because `mobile` is a real key in `manifest.targets`, and was then discarded at render time. Its body reached no generated file and the build exited 0.

`loadModules` now fails with `AICTX_CONFIG_INVALID` and points at the fix: move the content to a rule file and add it to that scope's includes. Typo'd targets still get the distinct `unknown target` error.

The meta-skill's `authoring-modules.md` and `manifest-schema.md` documented the behavior that was never implemented (`api` → `apps/api/AGENTS.md`); both now state that `root` is the only legal module target and describe the rule-file route for package-scoped content.
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Rules:

- `targets.root` must exist.
- Every non-root target must be generated by at least one scope mapping.
- Modules compose into `root` only. A module whose `targets` frontmatter names a
non-root target is a build error — scoped outputs are built from scope
includes, so such a module would otherwise be dropped silently. Put
package-specific content in a rule file and add it to that scope's includes.

## `.ai/context/scopes.json`

Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ export function loadModules(cwd: string, manifest: Manifest): ContextModule[] {
`Module '${file}' references unknown target '${target}'`
);
}
// Modules compose into the root output only. Scoped outputs are built
// entirely from scope includes, so a non-root target would be accepted
// here and then silently dropped at render time.
if (target !== "root") {
throw new ContextError(
"AICTX_CONFIG_INVALID",
`Module '${file}' targets '${target}', but modules only compose into the root output. ` +
`Move this content to a rule file and add it to the '${target}' scope's includes in the scope manifest.`
);
}
}

const orderValue = meta.order;
Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ describe("config validation", () => {
expect(() => loadModules(cwd, manifest)).toThrow(ContextError);
});

it("rejects a module targeting a scoped target instead of silently dropping it", () => {
const cwd = copyFixture();
const modulePath = path.join(cwd, ".ai/context/modules/010-context-system.md");
fs.writeFileSync(
modulePath,
["---", "id: context-system", "targets:", " - api", "order: 10", "---", "module body"].join(
"\n"
),
"utf8"
);

const manifest = loadManifest(cwd);
// 'api' IS a valid manifest target, so the unknown-target check passes. Only
// the root output composes modules, so accepting this would drop the body.
expect(() => loadModules(cwd, manifest)).toThrow(/only compose into the root output/);
});

it("still reports an unknown target distinctly from a scoped one", () => {
const cwd = copyFixture();
const modulePath = path.join(cwd, ".ai/context/modules/010-context-system.md");
fs.writeFileSync(
modulePath,
[
"---",
"id: context-system",
"targets:",
" - typo",
"order: 10",
"---",
"module body"
].join("\n"),
"utf8"
);

const manifest = loadManifest(cwd);
expect(() => loadModules(cwd, manifest)).toThrow(/unknown target 'typo'/);
});

it("rejects duplicate scope ids", () => {
const cwd = copyFixture();
const scopePath = path.join(cwd, ".ai/context/scopes.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,15 @@ Must match the filename with the leading `NNN-` prefix and `.md` extension strip

### `targets`

Array of target IDs that appear as keys in `.ai/context/manifest.json`. The most common values:
**`root` is the only legal value.** Modules compose into the repo-root `AGENTS.md` / `CLAUDE.md` and nowhere else.

| Value | Meaning |
|---|---|
| `root` | Emitted into the repo-root `AGENTS.md` / `CLAUDE.md` |
| `api` | Emitted into `apps/api/AGENTS.md` (if that target is defined) |
| `web` | Emitted into `apps/web/AGENTS.md` (if that target is defined) |

A module can list multiple targets:
```yaml
targets:
- root
- api
- web
```

Scoped outputs (`apps/<pkg>/AGENTS.md`, `apps/<pkg>/CLAUDE.md`, `.claude/rules/*.md`) are built entirely from a scope's includes — modules never feed them. Naming a scoped target here is a build error, not a no-op: see "Content for one package" below.

### `order`

Integer used as a secondary sort key when multiple modules have the same numeric prefix. Typically mirrors the numeric prefix (e.g. `order: 10` for `010-*.md`).
Expand All @@ -61,10 +54,19 @@ Integer used as a secondary sort key when multiple modules have the same numeric
At build time, the kit:
1. Reads all `*.md` files in `modulesDir`
2. Parses frontmatter from each
3. Groups modules by target
4. Sorts each group by `order` (ascending)
5. Concatenates bodies into the generated output, separated by blank lines
6. Writes the result to each target's output path
3. Sorts by `order` (ascending), then `id`
4. Concatenates bodies, separated by blank lines
5. Writes the result to the `root` target's output path and to `claudeOutput`

## Content for one package

Modules are global. Guidance that only applies inside one package does not go in a module — it goes in a rule file wired to that package's scope:

1. Write the content to `.ai/rules/<name>.md` (no frontmatter — rule files are plain markdown).
2. Add that path to the scope's `codexIncludes` and `claudeIncludes` in `.ai/context/scopes.json`.
3. Run `ai-context build`.

The content then lands in that scope's `AGENTS.md` and `CLAUDE.md` (and its `.claude/rules/*.md` file, if the scope defines one). See `authoring-scopes.md`.

## Complete working example

Expand Down Expand Up @@ -93,7 +95,8 @@ This is a TypeScript monorepo for the Acme loan origination platform.

| Mistake | Fix |
|---|---|
| `targets: [api]` but `api` isn't in `manifest.json` | Add the target to manifest, or use `root` |
| `targets: [api]` — expecting the body to reach `apps/api/AGENTS.md` | Modules only compose into the root output. Move the content to `.ai/rules/` and add it to the `api` scope's includes |
| `targets: [api]` but `api` isn't in `manifest.json` | Use `root` |
| `id: project-overview` in `050-architecture.md` | `id` must match the filename stem: `architecture` |
| Frontmatter missing entirely | Every module must have `---` delimited frontmatter |
| Module over 80 lines | Split into two modules or move domain detail to a rule file under `.ai/rules/` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Map of target ID → output path (relative to repo root). **Must include a `root
}
```

The `root` target is the global AGENTS.md that every agent sees. Scoped targets are written when modules list that target ID in their `targets` frontmatter field.
The `root` target is the global AGENTS.md that every agent sees, and it is the only target modules compose into. Scoped targets are written from their scope's includes in `scopes.json` — every non-root target must be produced by some scope, or the build fails.

### `claudeOutput` (optional, string)

Expand Down
Loading