arkgate 2.9.2 → 2.11.0

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 (50) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.md +12 -2
  3. package/SECURITY.md +3 -4
  4. package/bin/ark-check.mjs +41 -16
  5. package/bin/ark-mcp.mjs +335 -111
  6. package/bin/ark.mjs +35 -5
  7. package/bin/lib/agent-gates.mjs +161 -8
  8. package/bin/lib/architecture-scan.mjs +23 -1
  9. package/bin/lib/auto-patch.mjs +264 -0
  10. package/bin/lib/baseline-key.mjs +17 -0
  11. package/bin/lib/config-warnings.mjs +22 -0
  12. package/bin/lib/core-layers.mjs +7 -0
  13. package/bin/lib/core-ratchet.mjs +3 -7
  14. package/bin/lib/doctor-plan.mjs +83 -5
  15. package/bin/lib/port-proof.mjs +309 -0
  16. package/bin/lib/prepare-write.mjs +130 -0
  17. package/bin/lib/remediation.mjs +21 -0
  18. package/bin/lib/safety-diagnostics.mjs +263 -0
  19. package/bin/lib/scan-files.mjs +51 -6
  20. package/bin/lib/violations.mjs +3 -3
  21. package/dist/index.cjs +115 -11
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +5 -3
  24. package/dist/index.d.ts +5 -3
  25. package/dist/index.js +115 -11
  26. package/dist/index.js.map +1 -1
  27. package/dist/nestjs/index.cjs +18 -5
  28. package/dist/nestjs/index.cjs.map +1 -1
  29. package/dist/nestjs/index.d.cts +1 -1
  30. package/dist/nestjs/index.d.ts +1 -1
  31. package/dist/nestjs/index.js +18 -5
  32. package/dist/nestjs/index.js.map +1 -1
  33. package/dist/runtime/index.cjs +115 -11
  34. package/dist/runtime/index.cjs.map +1 -1
  35. package/dist/runtime/index.d.cts +1 -1
  36. package/dist/runtime/index.d.ts +1 -1
  37. package/dist/runtime/index.js +115 -11
  38. package/dist/runtime/index.js.map +1 -1
  39. package/dist/{types-D6Q8WHes.d.cts → types-BZ17b9i5.d.cts} +5 -1
  40. package/dist/{types-D6Q8WHes.d.ts → types-BZ17b9i5.d.ts} +5 -1
  41. package/docs/agent-guide.md +15 -1
  42. package/docs/ai-gates.md +63 -5
  43. package/docs/enthusiast/how-to-agent-gates.md +8 -0
  44. package/docs/enthusiast/reference-commands.md +1 -1
  45. package/docs/package-surface.md +2 -2
  46. package/docs/production-hardening.md +5 -0
  47. package/package.json +6 -2
  48. package/server.json +2 -2
  49. package/templates/skills/ark-explain.md +1 -1
  50. package/templates/skills/ark-loop.md +2 -1
@@ -941,7 +941,11 @@ interface WorkflowStep<P extends SagaContext = SagaContext> {
941
941
  onEvent?: IntentName;
942
942
  retry?: RetryPolicy;
943
943
  timeoutMs?: number;
944
- execute: (payload: P, bus: EventBus) => MaybePromise<Partial<P> | void>;
944
+ /**
945
+ * Execute one step. The signal is aborted when `timeoutMs` elapses; implementations
946
+ * performing I/O must pass it to the underlying client for cooperative cancellation.
947
+ */
948
+ execute: (payload: P, bus: EventBus, signal: AbortSignal) => MaybePromise<Partial<P> | void>;
945
949
  compensate?: (payload: P, bus: EventBus, error?: unknown) => MaybePromise<void>;
946
950
  }
947
951
  interface WorkflowStartTrigger<P extends SagaContext = SagaContext> {
@@ -941,7 +941,11 @@ interface WorkflowStep<P extends SagaContext = SagaContext> {
941
941
  onEvent?: IntentName;
942
942
  retry?: RetryPolicy;
943
943
  timeoutMs?: number;
944
- execute: (payload: P, bus: EventBus) => MaybePromise<Partial<P> | void>;
944
+ /**
945
+ * Execute one step. The signal is aborted when `timeoutMs` elapses; implementations
946
+ * performing I/O must pass it to the underlying client for cooperative cancellation.
947
+ */
948
+ execute: (payload: P, bus: EventBus, signal: AbortSignal) => MaybePromise<Partial<P> | void>;
945
949
  compensate?: (payload: P, bus: EventBus, error?: unknown) => MaybePromise<void>;
946
950
  }
947
951
  interface WorkflowStartTrigger<P extends SagaContext = SagaContext> {
@@ -176,7 +176,21 @@ reference, and explanation for the full path (recommend → init → gallery →
176
176
  3. Run `ark init --archetype <id> --yes`, `--apply-policy-pack enthusiast-<preset>`, or `ark init --preset <preset> --yes` when no `ark.config.json` exists.
177
177
  4. Optional: `--write-plan` for `ark-adoption-plan.json`; copy a gallery starter from `examples/README.md`.
178
178
  5. Use `/ark-place` or `ark_place` for individual files after the contract exists.
179
- 6. Verify with `ark-check --root . --config ark.config.json --strict-config`.
179
+ 6. Verify with `ark-check --root . --config ark.config.json --strict`.
180
+
181
+ ### Write protocol (2.10+ / Track W)
182
+
183
+ Prefer preparing the write before the host commits it to disk:
184
+
185
+ | Surface | Role |
186
+ |---------|------|
187
+ | MCP **`ark_prepare_write`** | Place + constrain + validate + optional `autoPatch` + `judgmentBrief` + contentHash in one call |
188
+ | Write-gate **`autoPatch`** | Mechanical-safe **import type** rewrites only; post-patch revalidation green or discarded |
189
+ | PreToolUse **`--hook-repair`** | On deny: `ARK_REPAIR_JSON` / `ARK_AUTOPATCH_JSON` on stderr (still exit 2 — never silent write) |
190
+ | Doctor **`writePath`** | Reports `repair` \| `reject-only` \| `mcp-only` \| `none` for installed gates |
191
+
192
+ Port-proof inject binding is **judgment** for auto-apply (signature/arity change), not write-path autoPatch.
193
+ Full reference: [ai-gates.md](ai-gates.md). Loop-cost harness: `npm run eval:loop-cost`.
180
194
 
181
195
  Do not invent layers outside the 11-layer profile or named presets. Unrecognized
182
196
  directories (`utils/`, `lib/`) must be classified explicitly via `/ark-contract`.
package/docs/ai-gates.md CHANGED
@@ -38,6 +38,11 @@ GitHub Actions, `AGENTS.md`, a Codex TOML snippet under `docs/`, and (when
38
38
  selected) Grok Build project files under `.grok/`. It skips existing files unless
39
39
  you pass `--force`, so review and commit only the templates that match your project.
40
40
 
41
+ **Doctor (W5):** `ark-check --doctor --json` includes `doctor.writePath`
42
+ (`mode`: `repair` | `reject-only` | `mcp-only` | `none`, plus `prepareWrite` /
43
+ `autoPatch` flags) so leads can see whether the write path is repair-capable or
44
+ still reject-only.
45
+
41
46
  If your project uses Codex or Grok, treat MCP registration as part of the default
42
47
  setup, not an optional extra. Ark works best when the agent can read `ark://manifest`
43
48
  before it writes code; that is the fast path to avoiding architecture drift during
@@ -52,6 +57,40 @@ violations relative to the file's current on-disk state, so files with pre-exist
52
57
  (baselined) violations stay editable — they just can't get worse. New files block on
53
58
  every violation.
54
59
 
60
+ ### Opt-in repair payload (W4)
61
+
62
+ Default is **hard block with prose** on stderr. Hosts that can re-inject a fixed write
63
+ can enable a **machine-readable repair payload** (still exit `2` — **never** silent write):
64
+
65
+ | Enable | Effect on deny |
66
+ |--------|----------------|
67
+ | `--hook-repair` | Emit `ARK_REPAIR_JSON:…` and, when available, `ARK_AUTOPATCH_JSON:…` on stderr |
68
+ | `ARK_HOOK_REPAIR=1` | Same as `--hook-repair` (env, no template rewrite) |
69
+
70
+ `ARK_REPAIR_JSON` shape (stable additive):
71
+
72
+ ```json
73
+ {
74
+ "mode": "repair",
75
+ "decision": "deny",
76
+ "filePath": "src/domain/use.ts",
77
+ "layer": "DomainModel",
78
+ "autoPatch": {
79
+ "source": "import type { Row } from '../infra/types-only';\n…",
80
+ "remediationKind": "import-type-from-pure-type-module",
81
+ "confidence": 0.85,
82
+ "valid": true
83
+ }
84
+ }
85
+ ```
86
+
87
+ When no mechanical-safe patch applies, `autoPatch` is `null` (host still re-reasons or uses
88
+ `ark_prepare_write` / judgment). Grok deny JSON also includes `autoPatch` + `"repair": true`
89
+ when repair mode is on.
90
+
91
+ `--install-agent-gates` writes Claude/Grok PreToolUse commands with `--hook-repair` enabled.
92
+ Reject-only installs: drop `--hook-repair` (or unset `ARK_HOOK_REPAIR`).
93
+
55
94
  Add to your project's `.claude/settings.json`:
56
95
 
57
96
  ```json
@@ -63,7 +102,7 @@ Add to your project's `.claude/settings.json`:
63
102
  "hooks": [
64
103
  {
65
104
  "type": "command",
66
- "command": "npx ark-mcp --hook --root \"$CLAUDE_PROJECT_DIR\" --config ark.config.json"
105
+ "command": "npx ark-mcp --hook --hook-repair --root \"$CLAUDE_PROJECT_DIR\" --config ark.config.json"
67
106
  }
68
107
  ]
69
108
  }
@@ -114,7 +153,7 @@ Layers:
114
153
  - PersistenceAdapters: src/adapters/persistence/**
115
154
  Rules: 10 denied layer edge(s). Full contract: ark://manifest MCP resource.
116
155
  Baseline: 3 frozen violation(s) — only NEW violations fail; do not add to them.
117
- After edits run: npx ark-check --root . --config ark.config.json --strict-config
156
+ After edits run: npx ark-check --root . --config ark.config.json --strict
118
157
  ```
119
158
 
120
159
  The hook belongs in the **project's** `.claude/settings.json` (that's what
@@ -124,10 +163,11 @@ prints nothing and exits 0, so non-Ark projects are untouched.
124
163
 
125
164
  ## Claude Code — MCP server (contract discovery + on-demand validation)
126
165
 
127
- The MCP server exposes a resource and four tools agents can use proactively:
166
+ The MCP server exposes a resource and tools agents can use proactively (not an exhaustive list — `tools/list` is authoritative):
128
167
 
129
168
  - **`ark://manifest`** (resource) — the machine-readable architecture contract (layers + rules), so the agent can read the architecture before generating code.
130
- - **`validate_code`** (tool) — validates a snippet against the architecture on demand (the write-path gate).
169
+ - **`validate_code`** (tool) — validates a snippet against the architecture on demand (the write-path gate). May return additive **`autoPatch`** (W1) for mechanical-safe import-type rewrites.
170
+ - **`ark_prepare_write`** (tool) — **W2:** place + constrain + validate + optional autoPatch + judgmentBrief + contentHash in one call (composes `ark_place` + write gate).
131
171
  - **`ark_place`** (tool) — given a target file path, returns its layer, forbidden globals, and which layers it may / must not import. Call it *before* writing a new file so generated code lands in a governed location.
132
172
  - **`ark_check`** (tool) — runs the full architecture check and returns structured violations (applies the baseline automatically when one exists).
133
173
  - **`ark_coverage`** (tool) — per-layer file counts, the full unclassified-file list, and layers whose patterns match nothing.
@@ -350,9 +390,27 @@ Prefer keeping editor + CI on the same `ark.config.json` — do not maintain a p
350
390
  Whatever the agent side does, gate the merge:
351
391
 
352
392
  ```yaml
353
- - run: npx ark-check --root . --config ark.config.json --strict-config
393
+ - run: npx ark-check --root . --config ark.config.json --strict
394
+ ```
395
+
396
+ The `--strict` profile also requires the generated CI/write gates and fails on safety
397
+ diagnostics. Configure reviewed exceptions explicitly:
398
+
399
+ ```json
400
+ {
401
+ "dynamicImportAllowlist": ["src/plugins/loader.ts"],
402
+ "safety": {
403
+ "maxTsSuppressions": 0,
404
+ "maxAnyCasts": 0,
405
+ "allowInMemory": false,
406
+ "allowDisabledPeerIsolation": false
407
+ }
408
+ }
354
409
  ```
355
410
 
411
+ `ark-check --doctor --json` reports counts under `doctor.safety`. An `any` cast is
412
+ reported as lost static assurance; it does not imply that a runtime schema was bypassed.
413
+
356
414
  Adopting Ark on an existing codebase with violations? Freeze them once and ratchet down:
357
415
 
358
416
  ```bash
@@ -9,6 +9,10 @@ npx arkgate-check --install-agent-gates --tools claude,cursor,codex,grok
9
9
  Installs:
10
10
 
11
11
  - Write-gate hook configuration (Claude / Grok PreToolUse; Cursor advisory + MCP)
12
+ — PreToolUse uses **`--hook-repair`** (W4): on deny, stderr may include
13
+ `ARK_REPAIR_JSON` / `ARK_AUTOPATCH_JSON` for hosts that re-inject a patch.
14
+ Still exit 2 / hard block; never silent write. Drop `--hook-repair` for
15
+ reject-only prose, or set `ARK_HOOK_REPAIR=0` and omit the flag.
12
16
  - MCP server entry (`.mcp.json`, Cursor/Codex/Grok equivalents)
13
17
  - `/ark-*` skills including **`/ark-architect`**, **`/ark-autopilot`**, **`/ark-loop`**
14
18
  (with current `mechanical-safe` remediation kinds)
@@ -35,6 +39,10 @@ npx arkgate-check --doctor
35
39
  npx arkgate-check --require-gates
36
40
  ```
37
41
 
42
+ Doctor JSON (`--doctor --json`) includes **`writePath`**: whether PreToolUse is
43
+ `repair` (machine-readable autoPatch on deny), `reject-only`, `mcp-only`, or
44
+ `none` — plus `prepareWrite` / `autoPatch` booleans for leads.
45
+
38
46
  After upgrading the package, refresh skills so agents see the latest plan kinds:
39
47
 
40
48
  ```bash
@@ -52,7 +52,7 @@ arkgate-check --watch
52
52
  | `class` | Agent may auto-apply? | Examples (`remediationKind`) |
53
53
  |---------|----------------------|------------------------------|
54
54
  | `mechanical-safe` | Yes (validate + rollback) | `type-only-import-move`, `pure-type-file-relocate`, `import-type-from-pure-type-module`, `import-type-of-type-exports` |
55
- | `judgment` | No — propose | value imports, ports, infra relocate, cycles |
55
+ | `judgment` | No — propose | free value-import uses, multi-file ports, **W6 port-proof inject** (arity change), infra relocate, cycles |
56
56
  | `deferred` | No | unclear shape |
57
57
 
58
58
  ## Violation JSON (enthusiast fields)
@@ -16,10 +16,10 @@ This document is the consumer contract for **what is stable** vs **what is opt-i
16
16
  |---------|----------------|-----------------|
17
17
  | **CLI** | `arkgate` / `arkgate-check` (aliases `ark` / `ark-check`) | Flags and human text may improve; **JSON output shapes** for `--json` (check, doctor, plan, coverage, recommend) are stable within a major. Additive fields OK; removals/renames are major. |
18
18
  | **MCP tools** | `arkgate-mcp` / `ark://…` resources | Tool names and primary argument shapes are stable within a major. |
19
- | **`ark.config.json`** | Layer globs, rules, include/exclude, forbiddenGlobals, intent prefixes | Schema fields documented in playbooks/examples are stable; new optional fields may appear. |
19
+ | **`ark.config.json`** | Layer globs, rules, include/exclude, forbiddenGlobals, intent prefixes, `peerIsolation`, `dynamicImportAllowlist`, `safety` thresholds | Schema fields documented in playbooks/examples are stable; new optional fields may appear. |
20
20
  | **Agent skills** | `/ark-*` templates installed by `--install-agent-gates` | Skill *names* and “default flow” are stable; internal skill prose may evolve. |
21
21
  | **ESLint subpath** | `arkgate/eslint` | Config-driven layer/import rules; loads consumer `ark.config.json`. |
22
- | **GitHub Action** | `pedroknigge/arkgate` (see `action.yml`) | Inputs for running the check stay stable within a major. |
22
+ | **GitHub Action** | `pedroknigge/arkgate` (see `action.yml`) | The `uses:` tag/SHA selects the checker source; `version` remains an optional exact npm compatibility override. |
23
23
 
24
24
  Gates need **no application code imports**. Most projects only use the CLI + MCP + config.
25
25
 
@@ -37,6 +37,11 @@ When implementing Ark store interfaces in production, cover these guarantees exp
37
37
  - Observability: failed writes and dispatches are visible to operators.
38
38
  - Migration: schema changes for stored records are versioned.
39
39
 
40
+ Workflow `timeoutMs` uses cooperative cancellation: ArkGate aborts the `AbortSignal`
41
+ passed as the third `execute` argument, but JavaScript cannot stop an operation that
42
+ ignores that signal. Production steps must pass it to network/database clients and keep
43
+ external effects idempotent; otherwise work may finish after the workflow was marked failed.
44
+
40
45
  ## Interface Targets
41
46
 
42
47
  | Concern | Interface |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arkgate",
3
- "version": "2.9.2",
3
+ "version": "2.11.0",
4
4
  "description": "ArkGate — architecture co-pilot for AI TypeScript (write gate, CI gate, plan/loop)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -66,9 +66,11 @@
66
66
  "dev": "tsup --watch",
67
67
  "test": "vitest",
68
68
  "test:run": "vitest run",
69
+ "test:coverage": "vitest run --coverage --coverage.reporter=text-summary --coverage.reporter=json-summary",
69
70
  "typecheck": "tsc --noEmit",
70
71
  "security:audit": "npm audit --omit=dev --audit-level=high",
71
- "check:architecture": "node bin/ark-check.mjs --root . --config ark.config.json --strict-config",
72
+ "check:architecture": "node bin/ark-check.mjs --root . --config ark.config.json --strict",
73
+ "check:js": "node scripts/check-js-syntax.mjs",
72
74
  "generate:layer-match": "node scripts/generate-layer-match.mjs",
73
75
  "check:layer-match": "node scripts/generate-layer-match.mjs --check",
74
76
  "generate:cli-pure": "node scripts/generate-cli-pure.mjs",
@@ -79,6 +81,7 @@
79
81
  "eval:agent": "node eval/run.mjs",
80
82
  "eval:corpus": "node eval/validate-corpus.mjs",
81
83
  "eval:comparative": "node eval/comparative-run.mjs",
84
+ "eval:loop-cost": "node eval/loop-cost-run.mjs",
82
85
  "clean": "rm -rf dist",
83
86
  "release:npm": "node scripts/release-npm.mjs",
84
87
  "prepack": "npm run build"
@@ -110,6 +113,7 @@
110
113
  "devDependencies": {
111
114
  "@nestjs/common": "^11.1.27",
112
115
  "@types/node": "^20.14.0",
116
+ "@vitest/coverage-v8": "^3.2.6",
113
117
  "reflect-metadata": "^0.2.2",
114
118
  "rxjs": "^7.8.2",
115
119
  "tsup": "^8.1.0",
package/server.json CHANGED
@@ -6,12 +6,12 @@
6
6
  "url": "https://github.com/pedroknigge/arkgate",
7
7
  "source": "github"
8
8
  },
9
- "version": "2.9.2",
9
+ "version": "2.11.0",
10
10
  "packages": [
11
11
  {
12
12
  "registryType": "npm",
13
13
  "identifier": "arkgate",
14
- "version": "2.9.2",
14
+ "version": "2.11.0",
15
15
  "runtimeHint": "npx",
16
16
  "transport": {
17
17
  "type": "stdio"
@@ -66,7 +66,7 @@ forensics, baseline taxonomy) for tech leads.
66
66
 
67
67
  When explaining the **plan**, name the four `mechanical-safe` remediation kinds only
68
68
  (type-only move, pure-type file relocate, `import type` of pure-type modules,
69
- `import-type-of-type-exports` for named type exports from mixed modules) — everything
69
+ `import-type-of-type-exports` for named type exports from mixed modules; W6 port-proof inject is judgment when proof holds) — everything
70
70
  else is judgment/deferred and must not be auto-applied.
71
71
 
72
72
  ## Spoken / written explanation
@@ -65,8 +65,9 @@ feature dirs, plan clusters), you **may** dispatch **subagents**:
65
65
  | `pure-type-file-relocate` | Relocate pure-type file to owning layer (or rename out of false Domain globs) |
66
66
  | `import-type-from-pure-type-module` | Convert value import of pure-type module to `import type` |
67
67
  | `import-type-of-type-exports` | Convert value-syntax named import/export of type-only exports from a mixed module to `import type` / `export type` |
68
+ | *(none for port-proof)* | **W6** `port-proof-inject-binding` is **judgment** (arity change) — propose inject; do not auto-apply |
68
69
 
69
- Never auto: value imports (including mixed bindings with values), dynamic import/require, forbidden globals, cycles, infra moves.
70
+ Never auto: free value uses of imports, multi-import files, dynamic import/require, forbidden globals, cycles, port-proof inject, multi-file adapter scaffolding without proof.
70
71
 
71
72
  ## Steps
72
73