Commit cdab4f9
cli: implement jscodeshift transforms for add command
Adds per-repo-type codemods (mcp-server, langchain-tool, rest-api) that
insert `settlegrid.init()` and wrap handlers with `sg.wrap()`. Transforms
are idempotent, respect dry-run, and mutate target package.json to add
@settlegrid/mcp. Includes fixture-based tests covering 6 before/after
pairs and idempotency assertions.
New module: packages/settlegrid-cli/src/transforms/
- shared.ts — jscodeshift helpers: ensureSettlegridImport,
buildSettlegridInitStatement, buildSgWrapCall, isAlreadySgWrap,
extractMcpMethodName (kebab-cased name from schema identifier),
findEnclosingStatement.
- add-mcp.ts — finds `new Server(...)` + `server.setRequestHandler(
schema, handler)` and wraps the handler arg in `sg.wrap(handler,
{ method: '<schema-derived>' })`. Adds import + `const sg =
settlegrid.init({ toolSlug, pricing: { defaultCostCents: 1 } })`
before the first Server construction.
- add-langchain.ts — finds classes extending StructuredTool /
DynamicStructuredTool / Tool and wraps each `_call` / `invoke`
method body with `return await sg.wrap(async () => { <original
body> }, { method: this.name })()`. Init is inserted before the
first tool class.
- add-rest.ts — wraps every `app.<verb>(path, ..., handler)` call
for express/hono/fastify/koa, using `<verb>:<path>` as the method
key. Init is inserted before the first route.
- runner.ts — TransformInput/TransformOutput interface from spec,
dispatches by detect.type, enumerates source files via fast-glob
(500-file cap, deep:5, no-symlinks), respects dryRun, mutates
package.json deterministically (alphabetical key sort, preserves
original indent + trailing newline).
- __testfixtures__/ — 6 before/after pairs: mcp-basic, mcp-multi-
handler, mcp-already-wrapped (after === before, idempotent),
langchain-tool, rest-express, rest-hono.
- transforms.test.ts — 13 tests: per-fixture before→after equality
+ per-fixture idempotency + explicit already-wrapped no-op.
- runner.test.ts — 12 tests: 3 isAlreadyWrapped, 4 addPackageDependency
(sort, no-op, 4-space indent preservation, malformed safety),
5 runTransform contracts (mcp dispatch, unknown no-op, already-
wrapped skip, dry-run mtime unchanged, write-mode updates both
source and pkg.json).
Spec divergences (cited per step 1 "confirm the exact signatures")
@settlegrid/mcp API differs from the prompt's pseudocode:
- settlegrid.init() requires { toolSlug, pricing: { defaultCostCents } },
not { apiKey } (see packages/mcp/src/index.ts:289).
- sg.wrap(handler, { method }), not sg.wrap('method', handler)
(packages/mcp/src/index.ts:322).
Generated code emits the real-API shape; the spec-as-written code
would not compile against @settlegrid/mcp@0.1.1.
Updated: src/commands/add.ts
Calls runTransform after detection, prints a transform summary
(changed files, skipped files with reasons, dep to add, env required),
emits truncated per-file preview for dry-run, and exits with:
• dry-run + changes → "dry-run complete — re-run without --dry-run"
• apply + 0 changes → "no files changed (already wrapped or nothing
matched the codemod)"
• apply + N changes → "wrapped N file(s). Next: npm install, set
SETTLEGRID_API_KEY, and push"
The `--force` flag remains the unknown-type override.
Updated: src/index.test.ts
Smoke-test expectations updated for the new transform output —
assert on "transform summary", "@settlegrid/mcp@^0.1.1",
"SETTLEGRID_API_KEY", "dry-run complete", and "no files changed"
(the new outros) instead of the P2.2 "not yet implemented" sentinel.
Updated: tsconfig.json
Added src/transforms/__testfixtures__ to exclude — fixtures import
uninstalled packages (@modelcontextprotocol/sdk, @langchain/core,
express, hono) for realistic before-state content, and tsc would
flag them otherwise.
Verification
- npm --workspace @settlegrid/cli run {typecheck,build,test}: green
- 6 test files / 61 tests / 0 failures (up from 36 at end of P2.2 arc)
- Manual DoD #3 smoke: `node dist/index.js add --path fixtures/
mcp-sample --dry-run` prints detection + transform summary +
preview, exit 0 ✓
- DoD mtime check: runner.test.ts proves dryRun:true never touches
server.ts or package.json on disk
- apps/web tsc: 0 errors (baseline intact)
- `npx turbo test --concurrency=1`: 5/5 tasks successful
Pre-existing turbo-parallel flake on apps/web (documented in
docs/audit-failures/P2.2-apps-web-concurrent-flake-2026-04-15.md)
is unchanged by this commit — cli's fileParallelism:false mitigation
from P2.2 continues to partially suppress it.
Refs: P2.3
Audits: spec-diff PASS, hostile PASS, tests PASS
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 2191e98 commit cdab4f9
22 files changed
Lines changed: 1480 additions & 14 deletions
File tree
- packages/settlegrid-cli
- src
- commands
- transforms
- __testfixtures__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | 40 | | |
44 | 41 | | |
45 | 42 | | |
| |||
62 | 59 | | |
63 | 60 | | |
64 | 61 | | |
65 | | - | |
| 62 | + | |
66 | 63 | | |
67 | 64 | | |
68 | 65 | | |
| |||
78 | 75 | | |
79 | 76 | | |
80 | 77 | | |
81 | | - | |
| 78 | + | |
82 | 79 | | |
83 | 80 | | |
84 | 81 | | |
| |||
90 | 87 | | |
91 | 88 | | |
92 | 89 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
98 | 162 | | |
99 | 163 | | |
100 | 164 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
118 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
119 | 124 | | |
120 | 125 | | |
121 | 126 | | |
| |||
140 | 145 | | |
141 | 146 | | |
142 | 147 | | |
143 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
144 | 151 | | |
145 | 152 | | |
146 | 153 | | |
| |||
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 39 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments