Skip to content

Commit 1bcca3a

Browse files
feat(agents): PM-aware MCP spawn via agents init --mcp (#154)
* feat(agents): PM-aware MCP spawn via agents init --mcp Resolve codemap CLI invocation from the project package manager so MCP config works when codemap is a devDependency, not only on PATH. * fix(action): resolve WORKING_DIRECTORY from GITHUB_WORKSPACE Address PR review: absolute project root for detect-pm when the Action stage cd's elsewhere; TS↔mjs sync test; docs/plan lifecycle; test gaps. * ci: run scripts/*.test.mjs in check and CI Wire Action helper and TS↔mjs sync tests into test:scripts, lint-staged, and the CI test job; harden working-directory against .. segments. * docs(action): list yarn@berry in agent output description * test: address PR review nits for MCP invocation resolve Broaden TS↔mjs sync coverage, add pnpm e2e for agents init --mcp, dedupe agent validation in detect-pm, and align changeset wording with CLI prefix. * fix: close PR review findings and yarn@berry autodetect Extract Action working-directory resolution to a tested shell helper, broaden resolver/sync/e2e coverage, map detector yarn+berry to yarn@berry for dlx, and align consumer docs/CLI copy. * docs(agents): fix dlx vs local spawn examples in MCP section
1 parent 535986f commit 1bcca3a

32 files changed

Lines changed: 1396 additions & 264 deletions

.agents/rules/agents-tier-system.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Genuinely cross-cutting. Apply to every turn regardless of file:
3333
- `pr-comment-fact-check` — fires the fact-check skill on PR-comment intent triggers
3434
- `preserve-comments` — never silently delete TODOs / commented-out code
3535
- `tracer-bullets` — small end-to-end slices, not horizontal layers
36+
- `consumer-surfaces` — consumer-facing text describes user-visible behavior only
3637
- `verify-after-each-step` — run the project's checks per milestone, not at commit time
3738

3839
### Tier 2 — Auto-attached technical rules (this rule's tier)

.agents/rules/concise-comments.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ For every comment you wrote, ask: **"Could a teammate (human or AI) re-derive th
3030
- Section headers in short files (`// === Helpers ===`).
3131
- Author / date stamps (git tracks this).
3232
- Multi-line prose where one clause does the job.
33+
- **Prose inside JSDoc** when `@param` / `@returns` / `@typedef` already carry the meaning — types stay; narrating them does not.
34+
35+
## Exception: JSDoc as types (`.mjs`, `@ts-check`)
36+
37+
Untyped JS has no `.ts` surface — **`@typedef`, `@param`, `@returns`, and inline `@type` are the type system; keep them.** Apply the decision test only to **prose** in those blocks (keep non-obvious _why_ like `bunx` vs `bun x`; cut restatements of param names or return shapes).
3338

3439
## Reconcile with `preserve-comments`
3540

.agents/rules/consumer-surfaces.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
description: Consumer-facing surfaces must describe user-visible behavior only — no maintainer internals, CI wiring, or implementation file names.
3+
alwaysApply: true
4+
---
5+
6+
# Consumer surfaces
7+
8+
Consumers install **`@stainless-code/codemap`** and interact through CLI, MCP, HTTP, and bundled agent templates. They must only see **what to run** and **what it does** — never how this repo implements it.
9+
10+
## Consumer surfaces (write for users)
11+
12+
| Surface | Audience |
13+
| ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
14+
| `templates/agent-content/**` | Served live via `codemap skill` / `codemap rule`, `codemap://skill` / `codemap://rule`, MCP `instructions`, HTTP resources |
15+
| `templates/agents/**` | Copied into consumer projects by `codemap agents init` |
16+
| **`.changeset/*.md` body** | Release notes → `CHANGELOG.md` on npm |
17+
| **Root `README.md` (install / usage)** | npm landing page |
18+
| **CLI help text and user-facing errors** | Terminal output |
19+
20+
`docs/agents.md` is maintainer reference linked from bundled README — keep **MCP wiring / init** sections consumer-accurate; implementation tables belong in maintainer sections, not in served agent-content.
21+
22+
## Maintainer-only (never leak into consumer surfaces)
23+
24+
- Internal refactors, CI / GitHub Action wiring, dual-file sync (`*.ts``*.mjs`)
25+
- `scripts/`, `docs/plans/`, `docs/research/`, module paths under `src/`
26+
- Dogfood paths (`bun src/index.ts`) — maintainer workflow only; consumers use PM-aware spawn from `agents init --mcp`
27+
- “We moved X to Y” unless the **user-visible** command or output changed
28+
29+
## When editing consumer surfaces
30+
31+
1. **Behavior, not implementation** — e.g. “PM-aware MCP spawn via `agents init --mcp`”, not “`resolveCodemapCliInvocation` in `codemap-invocation.ts`”.
32+
2. **Changesets** — user-visible outcome only; no Action refactors, detect-pm delegation, or sync comments between source files.
33+
3. **Served skill / rule** — no hardcoded `{command: "codemap"}` unless documenting legacy manual wiring; prefer `codemap agents init --mcp` and PM-specific examples (`npx`, `pnpm exec`, `yarn exec`, `bunx`, dlx).
34+
4. **Cross-ref maintainer detail** — plans, architecture internals, and contributor tables stay in `docs/` or `.agents/` skills; link from consumer text only when the user must follow a public doc (e.g. [agents.md § MCP wiring](https://github.com/stainless-code/codemap/blob/main/docs/agents.md#mcp-wiring-via-agents-init)).
35+
36+
## Decision test
37+
38+
Before shipping text on a consumer surface: **“Would a user who only `npm i @stainless-code/codemap` care?”** If no → cut it or move it to a maintainer doc.
39+
40+
Related: [`write-a-skill`](../skills/write-a-skill/SKILL.md) (maintainer vs shipped templates), [`docs-governance`](./docs-governance.md) Rule 10 (agent-content layers).

.agents/rules/docs-governance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ alwaysApply: false
88

99
Before authoring or editing any doc in this repo, **read the [`docs-governance` skill](../skills/docs-governance/SKILL.md)** for the full reference. This rule is the priming layer.
1010

11-
The canonical Rules (1–10) live in [`docs/README.md`](../../docs/README.md) — cite them by number; never restate them.
11+
The canonical Rules (1–10) live in [`docs/README.md`](../../docs/README.md) — cite them by number; never restate them. Consumer-surface policy: [`.agents/rules/consumer-surfaces.md`](./consumer-surfaces.md) (Rule 10 sub-bullet).
1212

1313
## Surface tiers (which subset of governance applies)
1414

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@stainless-code/codemap": patch
3+
---
4+
5+
`codemap agents init --mcp` writes PM-aware MCP spawn commands (e.g. `npx codemap`, `pnpm exec codemap`, `yarn exec codemap`, `bunx codemap`, or dlx `@stainless-code/codemap@latest`) instead of assuming global `codemap` on PATH.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.agents/rules/consumer-surfaces.md

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ jobs:
9292
- name: Run unit tests with coverage
9393
run: bun run test:coverage
9494

95+
- name: Run scripts tests (Action helpers + TS↔mjs sync)
96+
run: bun run test:scripts
97+
9598
- name: Golden query regression (fixtures/minimal)
9699
run: bun run test:golden
97100

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ codemap context --compact --for "refactor auth" # JSON envelope + i
5555
codemap ingest-coverage coverage/coverage-final.json --json # Istanbul / LCOV (auto-detected) → coverage table; joins with symbols
5656
NODE_V8_COVERAGE=.cov bun test && codemap ingest-coverage .cov --runtime --json # V8 protocol (per-process dumps); local-only
5757
codemap agents init # scaffold .agents/ rules + skills
58-
codemap agents init --mcp # project MCP config (see docs/agents.md)
58+
codemap agents init --mcp # PM-aware project MCP config (see docs/agents.md)
5959
codemap apply rename-preview --params old=foo,new=bar --dry-run # preview recipe-driven edits (substrate executor)
6060
```
6161

@@ -246,7 +246,7 @@ codemap --files src/a.ts src/b.tsx
246246
# Scaffold .agents/ from bundled templates — full matrix: docs/agents.md
247247
codemap agents init
248248
codemap agents init --force
249-
codemap agents init --mcp # project MCP config for supported IDEs
249+
codemap agents init --mcp # PM-aware project MCP config (see docs/agents.md)
250250
codemap agents init --interactive # -i; IDE wiring + symlink vs copy
251251
```
252252

action.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ inputs:
8686

8787
outputs:
8888
agent:
89-
description: "Resolved package manager (npm / pnpm / yarn / bun)."
89+
description: "Resolved package manager (npm / pnpm / yarn / yarn@berry / bun)."
9090
value: ${{ steps.detect-pm.outputs.agent }}
9191
exec:
9292
description: "Shell-ready command used to invoke codemap."
@@ -140,14 +140,18 @@ runs:
140140
env:
141141
PACKAGE_MANAGER: ${{ inputs.package-manager }}
142142
VERSION: ${{ inputs.version }}
143-
WORKING_DIRECTORY: ${{ inputs.working-directory }}
144143
run: |
144+
ACTION_STAGE="$RUNNER_TEMP/codemap-action"
145+
WORK_DIR="${{ inputs.working-directory }}"
146+
RESOLVED_WORKDIR="$(bash "${{ github.action_path }}/scripts/action-resolve-working-directory.sh" "$GITHUB_WORKSPACE" "$WORK_DIR")"
145147
# Action runs without its own node_modules; install the detector lazily.
146148
# Pinned to a known version so consumers get reproducible builds.
147-
npm install --no-save --prefix "$RUNNER_TEMP/codemap-action" package-manager-detector@1.6.0 >/dev/null
148-
cp "${{ github.action_path }}/scripts/detect-pm.mjs" "$RUNNER_TEMP/codemap-action/detect-pm.mjs"
149-
cd "$RUNNER_TEMP/codemap-action"
150-
node detect-pm.mjs
149+
npm install --no-save --prefix "$ACTION_STAGE" package-manager-detector@1.6.0
150+
cp "${{ github.action_path }}/scripts/detect-pm.mjs" \
151+
"${{ github.action_path }}/scripts/codemap-invocation.mjs" \
152+
"$ACTION_STAGE/"
153+
cd "$ACTION_STAGE"
154+
WORKING_DIRECTORY="$RESOLVED_WORKDIR" node detect-pm.mjs
151155
152156
- name: Validate inputs (mode + flag interactions)
153157
if: steps.gate.outputs.skip != 'true'

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These rules are normative — cite them by number in PR review. Ordered by how o
4444
- **Auto-flows (no template edit needed)** — recipe additions (`templates/recipes/*.{sql,md}`), schema additions (`src/db.ts` `createTables()`). Both surfaces via `*.gen.md` renderers in `src/application/agent-content.ts`; `codemap skill` re-assembles every call, while MCP/HTTP memoize the assembled skill/rule/schema/mcp-instructions body per server process.
4545
- **Narrative changes** — new CLI flag / output mode / MCP tool / HTTP route / output-shape change → edit the relevant hand-written section in **`templates/agent-content/skill/*.md`** (single source of truth; `codemap skill` (CLI), `codemap://skill` (MCP), and `GET /resources/{encoded-uri}` against `codemap serve` (HTTP) all serve the same assembled body).
4646
- **Pointer-shape changes** (frontmatter schema, fetch instructions, marker comments) → edit `templates/agents/{rules/codemap,skills/codemap/SKILL}.md` AND bump `EXPECTED_POINTER_VERSION` in `agent-content.ts` so consumers see the staleness nag and re-run `codemap agents init --force`.
47+
- **Consumer-only surfaces**[`templates/agent-content/`](../templates/agent-content/), [`templates/agents/`](../templates/agents/), [`.changeset/`](../.changeset/) bodies, and user-facing CLI text describe **user-visible behavior** only. No maintainer internals (Action wiring, `src/` module names, dual-file sync, dogfood spawn paths). Full policy: [`.agents/rules/consumer-surfaces.md`](../.agents/rules/consumer-surfaces.md).
4748

4849
This repo's `.agents/{rules/codemap,skills/codemap/SKILL}.md` are thin pointers too (regenerate via `bun src/index.ts agents init --force` if pointer shape drifts) — they used to be the dev-side "second copy" to keep in sync; that obligation is gone.
4950

0 commit comments

Comments
 (0)