martin-loop 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +398 -362
  3. package/demo/seeded-workspace/README.md +35 -0
  4. package/demo/seeded-workspace/TASKS.md +29 -0
  5. package/demo/seeded-workspace/martin.config.yaml +11 -0
  6. package/demo/seeded-workspace/package.json +8 -0
  7. package/demo/seeded-workspace/src/invoice-summary.js +11 -0
  8. package/demo/seeded-workspace/test/invoice-summary.test.js +20 -0
  9. package/dist/vendor/adapters/claude-cli.d.ts +19 -4
  10. package/dist/vendor/adapters/claude-cli.js +55 -24
  11. package/dist/vendor/adapters/cli-bridge.d.ts +1 -0
  12. package/dist/vendor/adapters/cli-bridge.js +154 -28
  13. package/dist/vendor/adapters/index.d.ts +1 -0
  14. package/dist/vendor/adapters/index.js +1 -0
  15. package/dist/vendor/adapters/verifier-only.d.ts +7 -0
  16. package/dist/vendor/adapters/verifier-only.js +57 -0
  17. package/dist/vendor/cli/index.d.ts +6 -1
  18. package/dist/vendor/cli/index.js +124 -7
  19. package/dist/vendor/contracts/index.d.ts +3 -1
  20. package/dist/vendor/core/compiler.d.ts +2 -0
  21. package/dist/vendor/core/compiler.js +10 -4
  22. package/dist/vendor/core/context-integrity.d.ts +26 -0
  23. package/dist/vendor/core/context-integrity.js +56 -0
  24. package/dist/vendor/core/index.d.ts +5 -2
  25. package/dist/vendor/core/index.js +186 -54
  26. package/dist/vendor/core/policy.d.ts +6 -0
  27. package/docs/distribution/DIRECTORY-SUBMISSIONS.md +89 -0
  28. package/docs/distribution/INTEGRATION-OUTREACH.md +61 -0
  29. package/docs/distribution/UNDER-3-CHALLENGE.md +65 -0
  30. package/docs/oss/CLAUDE-CODE-WALKTHROUGH.md +142 -0
  31. package/docs/oss/EXAMPLES.md +134 -126
  32. package/docs/oss/OSS-BOUNDARY-REPORT.json +109 -113
  33. package/docs/oss/OSS-BOUNDARY-REPORT.md +48 -48
  34. package/docs/oss/QUICKSTART.md +165 -135
  35. package/docs/oss/RALPH-LOOP-SAFETY.md +113 -0
  36. package/docs/oss/README.md +96 -93
  37. package/docs/oss/RELEASE-SURFACE-REPORT.json +45 -45
  38. package/docs/oss/RELEASE-SURFACE-REPORT.md +35 -35
  39. package/package.json +19 -11
package/README.md CHANGED
@@ -1,362 +1,398 @@
1
- <div align="center">
2
-
3
- <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/martinloop-logo.png" alt="MartinLoop" width="260">
4
-
5
- ### A governed runtime for autonomous AI coding agents. ⭐⭐⭐
6
-
7
- [![License: MIT](https://img.shields.io/badge/license-MIT-7c3aed?style=flat-square)](./LICENSE)
8
- [![TypeScript](https://img.shields.io/badge/TypeScript-strict-3178c6?style=flat-square&logo=typescript&logoColor=white)](./tsconfig.base.json)
9
- [![Node](https://img.shields.io/badge/node-%3E%3D20-3c873a?style=flat-square&logo=nodedotjs&logoColor=white)](#quick-start)
10
- [![npm](https://img.shields.io/badge/npm-martin--loop-cc3534?style=flat-square&logo=npm&logoColor=white)](https://www.npmjs.com/package/martin-loop)
11
-
12
- <br>
13
-
14
- **Your overnight AI pipeline estimated $2.40.**
15
- **You woke up to a $65 bill.**
16
- <br> 47 retries. No hard stop. No rollback. No audit trail. Nothing merged.
17
- MartinLoop exists so that never happens again.✅ <br> <br>
18
- If you think autonomous AI coding agents need budgets, brakes, and receipts, ⭐ the repo so more builders can find it.
19
- <br>
20
-
21
- > AI coding agents are useful. Unbounded retry loops are not.
22
- >
23
- > MartinLoop wraps agent runs with budgets, policy checks, verifier gates, rollback evidence, and inspectable run records.
24
- <br>
25
- <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/cli-animated.svg" alt="MartinLoop CLI governed agent run" width="720">
26
-
27
- </div>
28
-
29
- ---
30
-
31
- ## The Problem
32
-
33
- A typical autonomous coding loop keeps attempting work until tests pass. Without a governance layer, that loop can keep spending, mutate files outside the intended scope, lose track of why it failed, and leave teams without a clean audit trail.
34
-
35
- Ralph-style loops are powerful but they attempt ➡️ check ➡️ retry ➡️ repeat, with no strong answer to:
36
-
37
- - What changed?
38
- - What did it cost?
39
- - Why was it allowed?
40
- - Why did it stop?
41
- - Can we inspect or resume it later?
42
-
43
- MartinLoop governs the failure mode.
44
-
45
- ---
46
-
47
- ## The Solution
48
-
49
- Martin Loop wraps AI coding loops with a governance layer.
50
-
51
- It does not try to replace the agent pattern. It makes that pattern safe to run.
52
-
53
- ### What MartinLoop Does Today
54
-
55
- | Capability | Current behavior |
56
- |---|---|
57
- | Budget governance | Enforces `maxUsd`, `softLimitUsd`, `maxIterations`, and `maxTokens`; rejects attempts projected to exceed remaining budget and exits on budget or iteration exhaustion. Hard USD budget caps that stop work before the next attempt breaches policy. |
58
- | Verifier gate | A run only reaches `completed` when the adapter result and verifier state pass. Unsafe verifier commands are blocked before agent execution. |
59
- | Failure taxonomy | Classifies failures across 11 current classes, including hallucination, test regression, scope creep, repo grounding failure, environment mismatch, and budget pressure, that distinguishes real success from unsafe, invalid, or terminal behavior.|
60
- | Safety leash | Evaluates verifier commands, file scope, dependency or migration changes that require approval, and secret-like values in task text. **Policy-as-code**. |
61
- | Rollback evidence | Captures rollback boundaries and restore outcomes for repo-backed attempts when a persistence store is configured. |
62
- | Context distillation | Carries a distilled summary of recent attempts and remaining constraints into subsequent attempts. |
63
- | Run records | The CLI appends JSONL loop records under `~/.martin/runs/<workspaceId>.jsonl`; lower-level stores can also persist contracts, ledgers, and attempt artifacts.
64
-
65
-
66
- ⭐The result is a runtime that can complete good work, refuse unsafe work, stop uneconomical work, and leave evidence behind.✅
67
- ---
68
-
69
- ## The Ralph Loop, explained
70
-
71
- **"Everybody has gotten infatuated with what we call these Ralph Wiggum loops, just like send the thing off and it'll just go figure something out..A, It never figures anything out. And B, you just get this ginormous bill...**" - Chamath Palihapitiya, All-In Podcast #263, March 2026
72
-
73
- ⛔ The **Ralph Loop** is the failure mode where an AI coding agent keeps trying without knowing when it should stop.
74
-
75
- The pattern is simple: attempt the task, run checks, retry on failure, repeat. The problem is not that the loop exists. The problem is that most implementations have no hard budget cap, no signed evidence layer, and no pre-execution control system. They know how to keep trying. They do **not** know when continuing is unsafe, uneconomical, or impossible.
76
-
77
- ✅ Martin Loop solves the Ralph Loop problem by enforcing rules **before** damage happens:
78
-
79
- - it stops the next attempt before budget overspend
80
- - it classifies unsafe or invalid actions before execution
81
- - it appends a structured JSONL audit record for every attempt
82
- - it rolls back failed runs instead of leaving broken state behind
83
- - it reduces runaway token growth with context distillation
84
-
85
- If Ralph ever burned $165.70 on your dime, you're in the right place. Martin stopped him at $4.97 with a full audit trail. LFG! 🚀 Finally a Martin Prince leash for Ralph Wiggums! :)
86
-
87
- <div align="center">
88
- <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/martin-raplph.png.jpg" alt="Martin vs Ralph governed vs ungoverned agent loop" width="240">
89
- </div>
90
-
91
- ### How It WorksFive Layers
92
-
93
- | Layer | What it does |
94
- |---|---|
95
- | **1. Task Contract** | Objective, verifier plan, repo root, allowed/denied paths, acceptance criteria, workspace, project, and budget. |
96
- | **2. Policy & Budget** | Defaults from `martin.config.yaml`; CLI flags override. Budget preflight rejects attempts before execution. |
97
- | **3. Agent Adapters** | Claude CLI, Codex CLI, direct-provider, and stub adapters normalize execution results into the core runtime contract. |
98
- | **4. Safety & Verification** | Verifier commands, file scope, approval-boundary changes, secret-like values, and grounding determine whether work is kept. |
99
- | **5. Persistence** | CLI writes JSONL records under `~/.martin/runs/`. Repo-backed runs can also persist contracts, ledgers, diffs, and rollback artifacts. |
100
-
101
- ---
102
-
103
- ## See It In Action
104
-
105
- Same task, same starting state. MartinLoop completes in one verified attempt at `$2.30`. The uncontrolled loop retries four times, spends `$5.20`, and fails with no audit trail.
106
-
107
- Martin Loop matters because it turns AI coding from an opaque experiment into something that can be governed, replayed, verified, and trusted.
108
-
109
- <div align="center">
110
- <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/side-by-side.svg" alt="Martin vs Ralph governed vs ungoverned agent loop side-by-side benchmark comparison" width="720" height="1080">
111
- </div>
112
-
113
-
114
- Reproducible locally:
115
-
116
- ```sh
117
- pnpm --filter @martin/benchmarks test
118
- pnpm --filter @martin/benchmarks eval
119
- pnpm --filter @martin/benchmarks eval:phase12
120
- ```
121
-
122
- ---
123
-
124
- ## Quick Start
125
-
126
- ```sh
127
- npm install -g martin-loop
128
- ```
129
-
130
- This installs both the `martin-loop` package and the `martin` command alias. The package is currently published on npm as version `0.1.2`.
131
-
132
- ### Public Package Surface
133
-
134
- The frozen public package surface for this release candidate is:
135
-
136
- - Install target: `npm install martin-loop`
137
- - CLI target: `npx martin-loop`
138
- - SDK target: `import { MartinLoop } from "martin-loop"`
139
-
140
- The `martin` command alias is installed for local operator convenience, but the public CLI surface is `npx martin-loop`.
141
-
142
- ### Run a governed task
143
-
144
- ```sh
145
- martin run "fix the auth regression" \
146
- --budget 3.00 \
147
- --verify "pnpm test"
148
- ```
149
-
150
- You can also pass the objective explicitly:
151
-
152
- ```sh
153
- martin run --objective "fix the auth regression" --budget 3.00 --verify "pnpm test"
154
- ```
155
-
156
- For a no-spend repo-local dry run, use the stub adapter:
157
-
158
- ```powershell
159
- $env:MARTIN_LIVE='false'
160
- pnpm run:cli -- run --objective "Summarize the current runtime state" --verify "pnpm --filter @martin/core test"
161
- Remove-Item Env:MARTIN_LIVE
162
- ```
163
-
164
- ### Inspect or resume runs
165
-
166
- ```sh
167
- martin inspect --file ~/.martin/runs/<workspaceId>.jsonl
168
- martin resume <loopId>
169
- ```
170
-
171
- `inspect` prints a portfolio summary for records in the file. `resume` looks up a persisted loop record by ID under `~/.martin/runs/`.
172
-
173
- ---
174
-
175
- ## CLI
176
-
177
- ```text
178
- martin run <objective> [options]
179
-
180
- --objective <text> The task to accomplish, or pass it as the first positional arg
181
- --budget <n> Hard cost cap in USD
182
- --budget-usd <n> Alias for --budget
183
- --soft-limit-usd <n> Soft budget threshold in USD
184
- --verify <cmd> Verifier command after each attempt
185
- --max-iterations <n> Maximum number of attempts
186
- --max-tokens <n> Maximum total token budget
187
- --engine <name> Adapter to use: claude (default) or codex
188
- --model <name> Override the adapter model
189
- --cwd <path> Repo root for the run
190
- --allow-path <glob> Restrict agent writes to this path pattern; repeatable
191
- --deny-path <glob> Block this path pattern; repeatable
192
- --accept <criterion> Add an acceptance criterion; repeatable
193
- --config <path> Path to a martin.config.yaml file
194
- --workspace <id> Workspace ID for the run record
195
- --project <id> Project ID for the run record
196
- --metadata <key=value> Attach metadata to the run record; repeatable
197
- ```
198
-
199
- The public CLI also includes `inspect`, `resume`, and a `bench` redirect that points reviewers to the workspace benchmark harness.
200
-
201
- <div align="center">
202
- <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/cli-static.svg" alt="MartinLoop CLI terminal output" width="720">
203
- </div>
204
-
205
- ---
206
-
207
- ## Policy File
208
-
209
- Drop a `martin.config.yaml` in your repo root to set governance defaults:
210
-
211
- ```yaml
212
- budget:
213
- maxUsd: 5.00
214
- softLimitUsd: 3.75
215
- maxIterations: 5
216
- maxTokens: 40000
217
-
218
- governance:
219
- destructiveActionPolicy: approval
220
- telemetryDestination: local-only
221
- verifierRules:
222
- - pnpm test
223
- ```
224
-
225
- CLI flags override config values when provided.
226
-
227
- ---
228
-
229
- ## TypeScript SDK
230
-
231
- ```sh
232
- npm install martin-loop
233
- ```
234
-
235
- ```typescript
236
- import {
237
- MartinLoop,
238
- createClaudeCliAdapter,
239
- createCodexCliAdapter,
240
- runMartin
241
- } from "martin-loop";
242
-
243
- const loop = new MartinLoop({
244
- adapter: createClaudeCliAdapter({ workingDirectory: process.cwd() }),
245
- defaults: {
246
- workspaceId: "my-workspace",
247
- projectId: "my-project",
248
- budget: {
249
- maxUsd: 3.00,
250
- softLimitUsd: 2.25,
251
- maxIterations: 3,
252
- maxTokens: 20_000
253
- }
254
- }
255
- });
256
-
257
- const result = await loop.run({
258
- task: {
259
- title: "Fix auth regression",
260
- objective: "Fix the failing auth regression tests",
261
- verificationPlan: ["pnpm test"],
262
- repoRoot: process.cwd()
263
- }
264
- });
265
-
266
- console.log(result.decision.status);
267
- ```
268
-
269
- Use Codex instead of Claude by swapping adapters:
270
-
271
- ```typescript
272
- const loop = new MartinLoop({
273
- adapter: createCodexCliAdapter({ workingDirectory: process.cwd() })
274
- });
275
- ```
276
-
277
- The lower-level `runMartin` function is also exported for callers that want to assemble the runtime input directly.
278
-
279
- ---
280
-
281
- ## Workspace Map
282
-
283
- | Package or app | Role |
284
- |---|---|
285
- | `martin-loop` | Root public npm facade that vendors the runtime, CLI, adapters, and contracts into `dist/`. |
286
- | `@martin/contracts` | Shared types for loops, policy, governance, budget, telemetry, and rollback. |
287
- | `@martin/core` | Runtime controller, policy engine, safety leash, grounding, persistence, and rollback logic. |
288
- | `@martin/adapters` | Claude CLI, Codex CLI, direct-provider, and stub adapter surfaces. |
289
- | `@martin/cli` | Local CLI implementation for `run`, `inspect`, `resume`, and the benchmark redirect. |
290
- | `@martin/mcp` | MCP server tools: `martin_run`, `martin_inspect`, and `martin_status`. |
291
- | `benchmarks/` | Workspace-only deterministic benchmark and RC validation harness. |
292
- | `apps/control-plane/` | Hosted control-plane workstream, outside the initial npm package surface. |
293
- | `apps/local-dashboard/` | Local dashboard/read-model viewer, not currently packaged as public npm API. |
294
-
295
- The `@martin/core`, `@martin/adapters`, and `@martin/contracts` package manifests are still private workspace packages; the public install target is the root `martin-loop` facade.
296
-
297
- ---
298
-
299
- ## Development
300
-
301
- Requirements: Node 20+ and pnpm 10.x.
302
-
303
- ```sh
304
- git clone https://github.com/Keesan12/martin-loop.git
305
- cd martin-loop
306
- pnpm install
307
-
308
- pnpm test
309
- pnpm lint
310
- pnpm build
311
- ```
312
-
313
- ```md
314
- Current RC gate commands:
315
-
316
- ```sh
317
- pnpm oss:validate
318
- pnpm public:smoke
319
- pnpm repo:smoke
320
- pnpm rc:validate
321
- pnpm pilot:prep:validate
322
- pnpm release:matrix:local
323
- Caution: Registry Publication
324
-
325
- This package is published through the public martin-loop package surface. Treat registry publication as a guarded release step: verify the RC gate commands, confirm the version follows semantic versioning, and document breaking changes before publishing.
326
-
327
- > **Caution:** This package is live on npm. Treat registry publication as a guarded release step — verify the RC gate commands, confirm semantic versioning, and document breaking changes before publishing.
328
-
329
- The repository is organized as a dual-track workspace: the OSS runtime and package facade are present and published, while the hosted control-plane, local dashboard, and benchmark harness remain gated in private workspace for future release rather than the primary npm package API.
330
-
331
- Helpful docs:
332
-
333
- - [OSS quickstart](./docs/oss/QUICKSTART.md)
334
- - [OSS examples](./docs/oss/EXAMPLES.md)
335
- - [OSS boundary report](./docs/oss/OSS-BOUNDARY-REPORT.md)
336
- - [Release surface report](./docs/oss/RELEASE-SURFACE-REPORT.md)
337
-
338
- ---
339
-
340
- ## Contributing
341
-
342
- ```sh
343
- git checkout -b feat/your-feature
344
- pnpm lint
345
- pnpm test
346
- git commit -m "feat: describe what you built"
347
- git push -u origin feat/your-feature
348
- ```
349
-
350
- Conventional commit prefixes: `feat:`, `fix:`, `chore:`, `docs:`, `refactor:`, and `test:`.
351
-
352
- ---
353
-
354
- <div align="center">
355
-
356
- **⭐Give the repo a star⭐** if you think AI coding needs budgets, brakes, and receipts.
357
-
358
- **MIT Licensed** · [martinloop.com](https://martinloop.com) · [keesan@martinloop.com](mailto:keesan@martinloop.com)
359
-
360
- *"AI coding accountability: completes good work, refuses unsafe work, stops uneconomical work."*
361
-
362
- </div>
1
+ <div align="center">
2
+
3
+ <img src="./docs/assets/martinloop-logo.png" alt="MartinLoop" width="260">
4
+
5
+ ### The cross agent governance layer for autonomous AI coding agents.⭐
6
+
7
+ [![License: MIT](https://img.shields.io/badge/license-MIT-7c3aed?style=flat-square)](./LICENSE)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-3178c6?style=flat-square&logo=typescript&logoColor=white)](./tsconfig.base.json)
9
+ [![Node](https://img.shields.io/badge/node-%3E%3D20-3c873a?style=flat-square&logo=nodedotjs&logoColor=white)](#quick-start)
10
+ [![npm](https://img.shields.io/badge/npm-martin--loop-cc3534?style=flat-square&logo=npm&logoColor=white)](https://www.npmjs.com/package/martin-loop)
11
+
12
+ MartinLoop has been accepted into the NVIDIA Inception program.
13
+
14
+ <br>
15
+
16
+ **Your overnight AI pipeline estimated $2.40.**
17
+ **You woke up to a $65 bill.**
18
+ <br> 47 retries. No hard stop. No rollback. No audit trail. Nothing merged.
19
+ MartinLoop exists so that never happens again.✅ <br> <br>
20
+ If you think autonomous AI coding agents need budgets, brakes, and receipts, Plase star ⭐ the repo so more builders can find it.
21
+ <br>
22
+
23
+ > AI coding agents are useful. Unbounded retry loops are not.
24
+ >
25
+ > MartinLoop wraps agent runs with budgets, policy checks, verifier gates, rollback evidence, and inspectable run records. Built for Enterprise Coding Agents, Agentic Teams, and Autonomous Companies.
26
+ <br>
27
+ <img src="./docs/assets/cli-animated.svg" alt="MartinLoop CLI — governed agent run" width="720">
28
+
29
+ </div>
30
+
31
+ ---
32
+
33
+ ## The Problem
34
+
35
+ A typical autonomous coding loop keeps attempting work until tests pass. Without a governance layer, that loop can keep spending, mutate files outside the intended scope, lose track of why it failed, and leave teams without a clean audit trail.
36
+
37
+ Ralph-style loops are powerful but they attempt ➡️ check ➡️ retry ➡️ repeat, with no strong answer to:
38
+
39
+ - What changed?
40
+ - What did it cost?
41
+ - Why was it allowed?
42
+ - Why did it stop?
43
+ - Can we inspect or resume it later?
44
+
45
+ MartinLoop governs the failure mode.
46
+
47
+ ---
48
+
49
+ ## The Solution
50
+
51
+ Martin Loop wraps AI coding loops with a governance layer.
52
+
53
+ It does not try to replace the agent pattern. It makes that pattern safe to run.
54
+
55
+ ### What MartinLoop Does Today
56
+
57
+ | Capability | Current behavior |
58
+ |---|---|
59
+ | Budget governance | Enforces `maxUsd`, `softLimitUsd`, `maxIterations`, and `maxTokens`; rejects attempts projected to exceed remaining budget and exits on budget or iteration exhaustion. Hard USD budget caps that stop work before the next attempt breaches policy. |
60
+ | Verifier gate | A run only reaches `completed` when the adapter result and verifier state pass. Unsafe verifier commands are blocked before agent execution. |
61
+ | Failure taxonomy | Classifies failures across 11 current classes, including hallucination, test regression, scope creep, repo grounding failure, environment mismatch, and budget pressure, that distinguishes real success from unsafe, invalid, or terminal behavior.|
62
+ | Safety leash | Evaluates verifier commands, file scope, dependency or migration changes that require approval, and secret-like values in task text. **Policy-as-code**. |
63
+ | Context integrity | Scans user prompts and tool output for injection patterns (authority inversion, instruction override, identity redefinition) before any attempt is admitted. Aborts with human escalation on detection. |
64
+ | Rollback evidence | Captures rollback boundaries and restore outcomes for repo-backed attempts when a persistence store is configured. |
65
+ | Context distillation | Carries a distilled summary of recent attempts and remaining constraints into subsequent attempts. |
66
+ | Run records | The CLI appends JSONL loop records under `~/.martin/runs/<workspaceId>.jsonl`; lower-level stores can also persist contracts, ledgers, and attempt artifacts.
67
+
68
+
69
+ The result is a runtime that can complete good work, refuse unsafe work, stop uneconomical work, and leave evidence behind.✅
70
+ ---
71
+
72
+ ## Ralph-Style Loops Need a Control Layer
73
+
74
+ **"Everybody has gotten infatuated with what we call these Ralph Wiggum loops, just like send the thing off and it'll just go figure something out..A, It never figures anything out. And B, you just get this ginormous bill...**" - Chamath Palihapitiya, All-In Podcast #263, March 2026
75
+
76
+ ⛔ The **Ralph Loop** is the failure mode where an AI coding agent keeps trying without knowing when it should stop.
77
+
78
+ The pattern is simple: attempt the task, run checks, retry on failure, repeat. The problem is not that the loop exists. The problem is that most implementations have no hard budget cap, no signed evidence layer, and no pre-execution control system. They know how to keep trying. They do **not** know when continuing is unsafe, uneconomical, or impossible.
79
+
80
+ Martin Loop solves the Ralph Loop problem by enforcing rules **before** damage happens:
81
+
82
+ - it stops the next attempt before budget overspend
83
+ - it classifies unsafe or invalid actions before execution
84
+ - it appends a structured JSONL audit record for every attempt
85
+ - it rolls back failed runs instead of leaving broken state behind
86
+ - it reduces runaway token growth with context distillation
87
+
88
+ If a Ralph-style loop has ever burned budget without producing a verified result, MartinLoop is designed to stop that failure mode before the next unsafe attempt runs. $165.70 on your dime, you're in the right place. Martin stopped him at $40.97 with a full audit trail.
89
+
90
+ <div align="center">
91
+ <img src="./docs/assets/martin-raplph.png.jpg" alt="Martin vs Ralph governed vs ungoverned agent loop" width="240">
92
+ </div>
93
+
94
+ ### How It Works — Five Layers
95
+
96
+ | Layer | What it does |
97
+ |---|---|
98
+ | **1. Task Contract** | Objective, verifier plan, repo root, allowed/denied paths, acceptance criteria, workspace, project, and budget. |
99
+ | **2. Policy & Budget** | Defaults from `martin.config.yaml`; CLI flags override. Budget preflight rejects attempts before execution. |
100
+ | **3. Agent Adapters** | Claude CLI, Codex CLI, direct-provider, and stub adapters normalize execution results into the core runtime contract. |
101
+ | **4. Safety & Verification** | Verifier commands, file scope, approval-boundary changes, secret-like values, and grounding determine whether work is kept. |
102
+ | **5. Persistence** | CLI writes JSONL records under `~/.martin/runs/`. Repo-backed runs can also persist contracts, ledgers, diffs, and rollback artifacts. |
103
+
104
+ ---
105
+
106
+ ## See It In Action
107
+
108
+ Same task, same starting state. MartinLoop completes in one verified attempt at `$2.30`. The uncontrolled loop retries four times, spends `$5.20`, and fails with no audit trail.
109
+
110
+ Martin Loop matters because it turns AI coding from an opaque experiment into something that can be governed, replayed, verified, and trusted.
111
+
112
+ <div align="center">
113
+ <img src="./docs/assets/side-by-side.svg" alt="Martin vs Ralph — governed vs ungoverned agent loop side-by-side benchmark comparison" width="720" height="1080">
114
+ </div>
115
+
116
+
117
+ Reproducible locally:
118
+
119
+ ```sh
120
+ pnpm --filter @martin/benchmarks test
121
+ pnpm --filter @martin/benchmarks eval
122
+ pnpm --filter @martin/benchmarks eval:phase12
123
+ ```
124
+
125
+ Challenge page: [Can your AI coding agent finish this task under $3?](./docs/distribution/UNDER-3-CHALLENGE.md)
126
+
127
+ ---
128
+
129
+ ## Quick Start
130
+
131
+ ```sh
132
+ npm install -g martin-loop
133
+ ```
134
+
135
+ This installs both the `martin-loop` package and the `martin` command alias. The package is currently published on npm as version `0.1.4`.
136
+
137
+ Want a safe sandbox first? Run `npx martin-loop demo` and MartinLoop will copy a disposable local workspace into `./martin-loop-demo`.
138
+
139
+ ### Public Package Surface
140
+
141
+ The frozen public package surface for this release candidate is:
142
+
143
+ - Install target: `npm install martin-loop`
144
+ - CLI target: `npx martin-loop`
145
+ - SDK target: `import { MartinLoop } from "martin-loop"`
146
+ - MCP target (registry-ready package): `npx -y @martinloop/mcp`
147
+
148
+ The `martin` command alias is installed for local operator convenience, but the public CLI surface is `npx martin-loop`.
149
+ The standalone MCP server package is smoke-validated locally with `pnpm --filter @martinloop/mcp smoke:pack` and is ready for registry publication as a separate release step.
150
+
151
+ ### Claude Code MCP install
152
+
153
+ Use the published MCP package directly:
154
+
155
+ - macOS/Linux: `claude mcp add --scope user martin-loop -- npx -y @martinloop/mcp`
156
+ - Windows PowerShell/cmd: `claude mcp add --scope user martin-loop -- cmd /c "npx -y @martinloop/mcp"`
157
+
158
+ If you just want to launch the server manually, the one-line command is:
159
+
160
+ ```sh
161
+ npx @martinloop/mcp
162
+ ```
163
+
164
+ ### Run a governed task
165
+
166
+ ```sh
167
+ martin run "fix the auth regression" \
168
+ --budget 3.00 \
169
+ --verify "pnpm test"
170
+ ```
171
+
172
+ You can also pass the objective explicitly:
173
+
174
+ ```sh
175
+ martin run --objective "fix the auth regression" --budget 3.00 --verify "pnpm test"
176
+ ```
177
+
178
+ For a no-spend repo-local dry run, use the stub adapter:
179
+
180
+ ```powershell
181
+ $env:MARTIN_LIVE='false'
182
+ pnpm run:cli -- run --objective "Summarize the current runtime state" --verify "pnpm --filter @martin/core test"
183
+ Remove-Item Env:MARTIN_LIVE
184
+ ```
185
+
186
+ ### Inspect or resume runs
187
+
188
+ ```sh
189
+ martin inspect --file ~/.martin/runs/<workspaceId>.jsonl
190
+ martin resume <loopId>
191
+ ```
192
+
193
+ `inspect` prints a portfolio summary for records in the file. `resume` looks up a persisted loop record by ID under `~/.martin/runs/`.
194
+
195
+ ---
196
+
197
+ ## CLI
198
+
199
+ ```text
200
+ martin run <objective> [options]
201
+
202
+ --objective <text> The task to accomplish, or pass it as the first positional arg
203
+ --budget <n> Hard cost cap in USD
204
+ --budget-usd <n> Alias for --budget
205
+ --soft-limit-usd <n> Soft budget threshold in USD
206
+ --verify <cmd> Verifier command after each attempt
207
+ --max-iterations <n> Maximum number of attempts
208
+ --max-tokens <n> Maximum total token budget
209
+ --engine <name> Adapter to use: claude (default) or codex
210
+ --model <name> Override the adapter model
211
+ --cwd <path> Repo root for the run
212
+ --allow-path <glob> Restrict agent writes to this path pattern; repeatable
213
+ --deny-path <glob> Block this path pattern; repeatable
214
+ --accept <criterion> Add an acceptance criterion; repeatable
215
+ --config <path> Path to a martin.config.yaml file
216
+ --workspace <id> Workspace ID for the run record
217
+ --project <id> Project ID for the run record
218
+ --metadata <key=value> Attach metadata to the run record; repeatable
219
+ ```
220
+
221
+ The public CLI also includes `demo`, `inspect`, `resume`, and a `bench` redirect that points reviewers to the workspace benchmark harness.
222
+
223
+ <div align="center">
224
+ <img src="./docs/assets/cli-static.svg" alt="MartinLoop CLI terminal output" width="720">
225
+ </div>
226
+
227
+ ---
228
+
229
+ ## Policy File
230
+
231
+ Drop a `martin.config.yaml` in your repo root to set governance defaults:
232
+
233
+ ```yaml
234
+ budget:
235
+ maxUsd: 5.00
236
+ softLimitUsd: 3.75
237
+ maxIterations: 5
238
+ maxTokens: 40000
239
+
240
+ governance:
241
+ destructiveActionPolicy: approval
242
+ telemetryDestination: local-only
243
+ verifierRules:
244
+ - pnpm test
245
+ ```
246
+
247
+ CLI flags override config values when provided.
248
+
249
+ ---
250
+
251
+ ## TypeScript SDK
252
+
253
+ ```sh
254
+ npm install martin-loop
255
+ ```
256
+
257
+ ```typescript
258
+ import {
259
+ MartinLoop,
260
+ createClaudeCliAdapter,
261
+ createCodexCliAdapter,
262
+ runMartin
263
+ } from "martin-loop";
264
+
265
+ const loop = new MartinLoop({
266
+ adapter: createClaudeCliAdapter({ workingDirectory: process.cwd() }),
267
+ defaults: {
268
+ workspaceId: "my-workspace",
269
+ projectId: "my-project",
270
+ budget: {
271
+ maxUsd: 3.00,
272
+ softLimitUsd: 2.25,
273
+ maxIterations: 3,
274
+ maxTokens: 20_000
275
+ }
276
+ }
277
+ });
278
+
279
+ const result = await loop.run({
280
+ task: {
281
+ title: "Fix auth regression",
282
+ objective: "Fix the failing auth regression tests",
283
+ verificationPlan: ["pnpm test"],
284
+ repoRoot: process.cwd()
285
+ }
286
+ });
287
+
288
+ console.log(result.decision.status);
289
+ ```
290
+
291
+ Use Codex instead of Claude by swapping adapters:
292
+
293
+ ```typescript
294
+ const loop = new MartinLoop({
295
+ adapter: createCodexCliAdapter({ workingDirectory: process.cwd() })
296
+ });
297
+ ```
298
+
299
+ The lower-level `runMartin` function is also exported for callers that want to assemble the runtime input directly.
300
+
301
+ ---
302
+
303
+ ## Workspace Map
304
+
305
+ | Package or app | Role |
306
+ |---|---|
307
+ | `martin-loop` | Root public npm facade that vendors the runtime, CLI, adapters, and contracts into `dist/`. |
308
+ | `@martin/contracts` | Shared types for loops, policy, governance, budget, telemetry, and rollback. |
309
+ | `@martin/core` | Runtime controller, policy engine, safety leash, grounding, persistence, and rollback logic. |
310
+ | `@martin/adapters` | Claude CLI, Codex CLI, direct-provider, and stub adapter surfaces. |
311
+ | `@martin/cli` | Local CLI implementation for `run`, `inspect`, `resume`, and the benchmark redirect. |
312
+ | `@martinloop/mcp` | MCP server tools: `martin_run`, `martin_inspect`, and `martin_status`. |
313
+ | `benchmarks/` | Workspace-only deterministic benchmark and RC validation harness. |
314
+ | `apps/control-plane/` | Hosted control-plane workstream, outside the initial npm package surface. |
315
+ | `apps/local-dashboard/` | Local dashboard/read-model viewer, not currently packaged as public npm API. |
316
+
317
+ The `@martin/core`, `@martin/adapters`, and `@martin/contracts` package manifests are still private workspace packages. The public runtime install target is the root `martin-loop` facade, while `@martinloop/mcp` is packaged as a standalone MCP server with vendored internal runtime dependencies for registry publication.
318
+
319
+ ---
320
+ ## Development
321
+
322
+ Requirements:
323
+
324
+ - Node 20+
325
+ - pnpm 10.x
326
+
327
+ ```bash
328
+ git clone https://github.com/Keesan12/martin-loop.git
329
+ cd martin-loop
330
+ pnpm install
331
+
332
+ pnpm test
333
+ pnpm lint
334
+ pnpm build
335
+
336
+ ```
337
+
338
+ Current RC gate commands:
339
+
340
+ ```sh
341
+ pnpm oss:validate
342
+ pnpm public:smoke
343
+ pnpm repo:smoke
344
+ pnpm rc:validate
345
+ pnpm pilot:prep:validate
346
+ pnpm release:matrix:local
347
+ ```
348
+
349
+ > **Caution:** This package is live on npm. Treat registry publication as a guarded release step — verify the RC gate commands, confirm semantic versioning, and document breaking changes before publishing.
350
+
351
+ The repository is organized as a dual-track workspace: the OSS runtime and package facade are present and published, while the hosted control-plane, local dashboard, and benchmark harness remain gated in private workspace for future release rather than the primary npm package API.
352
+
353
+ Helpful docs:
354
+
355
+ - [OSS quickstart](./docs/oss/QUICKSTART.md)
356
+ - [OSS examples](./docs/oss/EXAMPLES.md)
357
+ - [Under-$3 benchmark challenge](./docs/distribution/UNDER-3-CHALLENGE.md)
358
+ - [Directory submission pack](./docs/distribution/DIRECTORY-SUBMISSIONS.md)
359
+ - [Integration outreach pack](./docs/distribution/INTEGRATION-OUTREACH.md)
360
+ - [Claude Code walkthrough](./docs/oss/CLAUDE-CODE-WALKTHROUGH.md)
361
+ - [Ralph-style loop safety guide](./docs/oss/RALPH-LOOP-SAFETY.md)
362
+ - [OSS boundary report](./docs/oss/OSS-BOUNDARY-REPORT.md)
363
+ - [Release surface report](./docs/oss/RELEASE-SURFACE-REPORT.md)
364
+
365
+ ---
366
+
367
+ ## Contributing
368
+
369
+ ```sh
370
+ git checkout -b feat/your-feature
371
+ pnpm lint
372
+ pnpm test
373
+ git commit -m "feat: describe what you built"
374
+ git push -u origin feat/your-feature
375
+ ```
376
+
377
+ Conventional commit prefixes: `feat:`, `fix:`, `chore:`, `docs:`, `refactor:`, and `test:`.
378
+
379
+ ---
380
+
381
+ <div align="center">
382
+
383
+ **⭐Give the repo a star⭐** if you think AI coding needs budgets, brakes, and receipts.
384
+
385
+ **MIT Licensed** · [martinloop.com](https://martinloop.com) · [keesan@martinloop.com](mailto:keesan@martinloop.com)
386
+
387
+ *"AI coding accountability: completes good work, refuses unsafe work, stops uneconomical work."*
388
+
389
+ </div>
390
+
391
+ <div align="center">
392
+ <br>
393
+ <picture>
394
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/nvidia-inception-program.png">
395
+ <img src="https://raw.githubusercontent.com/Keesan12/martin-loop/main/docs/assets/nvidia-inception-program-light.png" alt="NVIDIA Inception Program logo" width="280">
396
+ </picture>
397
+ <br>
398
+ </div>