anvil-engineering-workbench 1.0.9 → 1.1.1

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 (163) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +23 -18
  3. package/dist/ai/claude-code-provider.js +55 -9
  4. package/dist/ai/claude-code-provider.js.map +1 -1
  5. package/dist/ai/provider.js +1 -1
  6. package/dist/ai/provider.js.map +1 -1
  7. package/dist/cli.js +850 -278
  8. package/dist/cli.js.map +1 -1
  9. package/dist/core/adr-engine.js +9 -11
  10. package/dist/core/adr-engine.js.map +1 -1
  11. package/dist/core/agent-config-release.js +29 -13
  12. package/dist/core/agent-config-release.js.map +1 -1
  13. package/dist/core/agent-config-versioning.js +32 -17
  14. package/dist/core/agent-config-versioning.js.map +1 -1
  15. package/dist/core/agent-evaluation-intelligence.js +30 -38
  16. package/dist/core/agent-evaluation-intelligence.js.map +1 -1
  17. package/dist/core/agent-evolution.js +109 -20
  18. package/dist/core/agent-evolution.js.map +1 -1
  19. package/dist/core/agent-experiment-studio.js +19 -11
  20. package/dist/core/agent-experiment-studio.js.map +1 -1
  21. package/dist/core/agent-optimization.js +69 -14
  22. package/dist/core/agent-optimization.js.map +1 -1
  23. package/dist/core/agent-registry.js +87 -15
  24. package/dist/core/agent-registry.js.map +1 -1
  25. package/dist/core/agent-security-audit.js +21 -21
  26. package/dist/core/agent-security-audit.js.map +1 -1
  27. package/dist/core/agent-workspace-context.js +37 -6
  28. package/dist/core/agent-workspace-context.js.map +1 -1
  29. package/dist/core/ai-code-review.js +60 -12
  30. package/dist/core/ai-code-review.js.map +1 -1
  31. package/dist/core/anthropic-provider.js.map +1 -1
  32. package/dist/core/architecture-map.js +87 -24
  33. package/dist/core/architecture-map.js.map +1 -1
  34. package/dist/core/benchmark-engine.js +89 -21
  35. package/dist/core/benchmark-engine.js.map +1 -1
  36. package/dist/core/benchmark-matrix.js +215 -41
  37. package/dist/core/benchmark-matrix.js.map +1 -1
  38. package/dist/core/benchmark-regression.js +124 -18
  39. package/dist/core/benchmark-regression.js.map +1 -1
  40. package/dist/core/change-intelligence.js +102 -43
  41. package/dist/core/change-intelligence.js.map +1 -1
  42. package/dist/core/code-intelligence.js +40 -13
  43. package/dist/core/code-intelligence.js.map +1 -1
  44. package/dist/core/code-review.d.ts +8 -0
  45. package/dist/core/code-review.js +214 -19
  46. package/dist/core/code-review.js.map +1 -1
  47. package/dist/core/code-search.js +79 -39
  48. package/dist/core/code-search.js.map +1 -1
  49. package/dist/core/container-sandbox.js +87 -23
  50. package/dist/core/container-sandbox.js.map +1 -1
  51. package/dist/core/dashboard-server.js +379 -93
  52. package/dist/core/dashboard-server.js.map +1 -1
  53. package/dist/core/dependency-graph.js +75 -13
  54. package/dist/core/dependency-graph.js.map +1 -1
  55. package/dist/core/dependency-impact.js +132 -33
  56. package/dist/core/dependency-impact.js.map +1 -1
  57. package/dist/core/dependency-intelligence.js +153 -56
  58. package/dist/core/dependency-intelligence.js.map +1 -1
  59. package/dist/core/doctor.js +3 -3
  60. package/dist/core/doctor.js.map +1 -1
  61. package/dist/core/engineering-risk.js +75 -40
  62. package/dist/core/engineering-risk.js.map +1 -1
  63. package/dist/core/env-compat.js.map +1 -1
  64. package/dist/core/evaluation-engine.js +188 -42
  65. package/dist/core/evaluation-engine.js.map +1 -1
  66. package/dist/core/execution-trace.js +83 -29
  67. package/dist/core/execution-trace.js.map +1 -1
  68. package/dist/core/experiment-history.js +70 -20
  69. package/dist/core/experiment-history.js.map +1 -1
  70. package/dist/core/experiment-manifest.js +92 -13
  71. package/dist/core/experiment-manifest.js.map +1 -1
  72. package/dist/core/gemini-provider.js.map +1 -1
  73. package/dist/core/git-intelligence.js +68 -16
  74. package/dist/core/git-intelligence.js.map +1 -1
  75. package/dist/core/hook-engine.js +7 -3
  76. package/dist/core/hook-engine.js.map +1 -1
  77. package/dist/core/hosted-agent-provider.js +48 -8
  78. package/dist/core/hosted-agent-provider.js.map +1 -1
  79. package/dist/core/intelligence-timeline.js +92 -43
  80. package/dist/core/intelligence-timeline.js.map +1 -1
  81. package/dist/core/mcp-setup.js +12 -12
  82. package/dist/core/mcp-setup.js.map +1 -1
  83. package/dist/core/model-router.js +153 -37
  84. package/dist/core/model-router.js.map +1 -1
  85. package/dist/core/openai-compatible-provider.js +41 -10
  86. package/dist/core/openai-compatible-provider.js.map +1 -1
  87. package/dist/core/orchestrator.js +28 -23
  88. package/dist/core/orchestrator.js.map +1 -1
  89. package/dist/core/patch-application.js +88 -24
  90. package/dist/core/patch-application.js.map +1 -1
  91. package/dist/core/patch-approval.js +43 -15
  92. package/dist/core/patch-approval.js.map +1 -1
  93. package/dist/core/patch-validation.js +93 -15
  94. package/dist/core/patch-validation.js.map +1 -1
  95. package/dist/core/permission-policy.js +8 -5
  96. package/dist/core/permission-policy.js.map +1 -1
  97. package/dist/core/policy-engine.d.ts +96 -0
  98. package/dist/core/policy-engine.js +447 -0
  99. package/dist/core/policy-engine.js.map +1 -0
  100. package/dist/core/project-cleaner.js +143 -129
  101. package/dist/core/project-cleaner.js.map +1 -1
  102. package/dist/core/project-health.js +183 -21
  103. package/dist/core/project-health.js.map +1 -1
  104. package/dist/core/project-init.js +32 -20
  105. package/dist/core/project-init.js.map +1 -1
  106. package/dist/core/project-inspector.js +81 -32
  107. package/dist/core/project-inspector.js.map +1 -1
  108. package/dist/core/project-memory.js +41 -18
  109. package/dist/core/project-memory.js.map +1 -1
  110. package/dist/core/provider-diagnostics.js +65 -12
  111. package/dist/core/provider-diagnostics.js.map +1 -1
  112. package/dist/core/quality-audit.js +167 -28
  113. package/dist/core/quality-audit.js.map +1 -1
  114. package/dist/core/regression-intelligence.js +88 -8
  115. package/dist/core/regression-intelligence.js.map +1 -1
  116. package/dist/core/remediation-planner.js +112 -40
  117. package/dist/core/remediation-planner.js.map +1 -1
  118. package/dist/core/review-history.js +39 -7
  119. package/dist/core/review-history.js.map +1 -1
  120. package/dist/core/run-history-output.js.map +1 -1
  121. package/dist/core/run-replay.js +12 -10
  122. package/dist/core/run-replay.js.map +1 -1
  123. package/dist/core/run-store.js +48 -21
  124. package/dist/core/run-store.js.map +1 -1
  125. package/dist/core/sandbox-cleanup-audit.js +1 -1
  126. package/dist/core/sandbox-cleanup-audit.js.map +1 -1
  127. package/dist/core/sandbox-compliance.js +53 -9
  128. package/dist/core/sandbox-compliance.js.map +1 -1
  129. package/dist/core/sandbox-lifecycle.js +1 -1
  130. package/dist/core/sandbox-lifecycle.js.map +1 -1
  131. package/dist/core/sandbox-policy.js +27 -3
  132. package/dist/core/sandbox-policy.js.map +1 -1
  133. package/dist/core/sandbox-runtime.js +9 -5
  134. package/dist/core/sandbox-runtime.js.map +1 -1
  135. package/dist/core/sandbox-security-profiles.js +31 -12
  136. package/dist/core/sandbox-security-profiles.js.map +1 -1
  137. package/dist/core/sarif-formatter.d.ts +12 -4
  138. package/dist/core/sarif-formatter.js +262 -57
  139. package/dist/core/sarif-formatter.js.map +1 -1
  140. package/dist/core/security-scanner.js +40 -32
  141. package/dist/core/security-scanner.js.map +1 -1
  142. package/dist/core/skill-registry.js +83 -21
  143. package/dist/core/skill-registry.js.map +1 -1
  144. package/dist/core/staged-verification.js +25 -10
  145. package/dist/core/staged-verification.js.map +1 -1
  146. package/dist/core/verification-history.js +12 -7
  147. package/dist/core/verification-history.js.map +1 -1
  148. package/dist/core/verification.js +50 -19
  149. package/dist/core/verification.js.map +1 -1
  150. package/dist/core/workflow-store.js +16 -4
  151. package/dist/core/workflow-store.js.map +1 -1
  152. package/dist/core/workspace-browser.js +57 -10
  153. package/dist/core/workspace-browser.js.map +1 -1
  154. package/dist/core/workspace-scan-policy.js +26 -4
  155. package/dist/core/workspace-scan-policy.js.map +1 -1
  156. package/dist/core/workspace-tool-broker.d.ts +1 -1
  157. package/dist/core/workspace-tool-broker.js +25 -4
  158. package/dist/core/workspace-tool-broker.js.map +1 -1
  159. package/dist/mcp/server.js +148 -55
  160. package/dist/mcp/server.js.map +1 -1
  161. package/dist/mcp/workspace-path.js +2 -2
  162. package/dist/mcp/workspace-path.js.map +1 -1
  163. package/package.json +8 -5
package/dist/cli.js CHANGED
@@ -10,7 +10,7 @@ import { formatArchitectureMap, mapArchitecture } from "./core/architecture-map.
10
10
  import { formatProjectReport, inspectProject } from "./core/project-inspector.js";
11
11
  import { loadAgentRegistry } from "./core/agent-registry.js";
12
12
  import { createReviewPlan } from "./core/review-planner.js";
13
- import { saveRun, listRuns, loadRun, decideTaskApproval, recoverInterruptedRun, withRunExecutionLock } from "./core/run-store.js";
13
+ import { saveRun, listRuns, loadRun, decideTaskApproval, recoverInterruptedRun, withRunExecutionLock, } from "./core/run-store.js";
14
14
  import { advanceRun } from "./core/orchestrator.js";
15
15
  import { formatRunHistoryJson } from "./core/run-history-output.js";
16
16
  import { OpenAICompatibleProvider } from "./core/openai-compatible-provider.js";
@@ -19,7 +19,7 @@ import { ClaudeCodeProvider } from "./ai/claude-code-provider.js";
19
19
  import { startDashboard } from "./core/dashboard-server.js";
20
20
  import { auditProject, formatQualityAudit } from "./core/quality-audit.js";
21
21
  import { discoverChecks, formatVerificationResult, runVerification } from "./core/verification.js";
22
- import { compareVerification, formatVerificationComparison, listVerifications, saveVerification } from "./core/verification-history.js";
22
+ import { compareVerification, formatVerificationComparison, listVerifications, saveVerification, } from "./core/verification-history.js";
23
23
  import { formatProjectHealthMarkdown, generateProjectHealth } from "./core/project-health.js";
24
24
  import { analyzeCodebase, formatCodebaseAnalysis } from "./core/code-intelligence.js";
25
25
  import { analyzeGitChanges, formatGitIntelligence } from "./core/git-intelligence.js";
@@ -32,45 +32,46 @@ import { stageApprovedPatch, reviewStagedPatch } from "./core/patch-application.
32
32
  import { verifyStagedPatch } from "./core/staged-verification.js";
33
33
  import { formatSandboxRuntimeReport, parseSandboxRuntimeReport } from "./core/sandbox-runtime.js";
34
34
  import { detectDockerRootless } from "./core/container-sandbox.js";
35
- import { formatSandboxCleanupReport, parseManagedContainers, selectStoppedManagedContainers } from "./core/sandbox-lifecycle.js";
35
+ import { formatSandboxCleanupReport, parseManagedContainers, selectStoppedManagedContainers, } from "./core/sandbox-lifecycle.js";
36
36
  import { getSandboxSecurityProfile, listSandboxSecurityProfiles } from "./core/sandbox-security-profiles.js";
37
37
  import { createSandboxComplianceReport, formatSandboxComplianceReport } from "./core/sandbox-compliance.js";
38
38
  import { formatSandboxPolicyValidation, loadSandboxPolicy } from "./core/sandbox-policy.js";
39
- import { appendSandboxCleanupAudit, formatSandboxCleanupHistory, readSandboxCleanupAudit } from "./core/sandbox-cleanup-audit.js";
39
+ import { appendSandboxCleanupAudit, formatSandboxCleanupHistory, readSandboxCleanupAudit, } from "./core/sandbox-cleanup-audit.js";
40
40
  import { formatRegressionReport, generateRegressionReport } from "./core/regression-intelligence.js";
41
41
  import { searchWorkspace } from "./core/code-search.js";
42
42
  import { buildAgentWorkspaceContext } from "./core/agent-workspace-context.js";
43
43
  import { analyzeDependencyImpact, formatDependencyImpact } from "./core/dependency-impact.js";
44
44
  import { analyzeEngineeringRisk, formatEngineeringRisk } from "./core/engineering-risk.js";
45
45
  import { analyzeChangeIntelligence, formatChangeIntelligence } from "./core/change-intelligence.js";
46
- import { captureIntelligenceSnapshot, compareLatestIntelligence, formatIntelligenceComparison } from "./core/intelligence-timeline.js";
46
+ import { captureIntelligenceSnapshot, compareLatestIntelligence, formatIntelligenceComparison, } from "./core/intelligence-timeline.js";
47
47
  import { analyzeDependencies, formatDependencyIntelligence } from "./core/dependency-intelligence.js";
48
48
  import { generateRemediationPlan, formatRemediationPlan } from "./core/remediation-planner.js";
49
- import { addProjectMemory, buildProjectMemoryContext, listProjectMemory, removeProjectMemory } from "./core/project-memory.js";
49
+ import { addProjectMemory, buildProjectMemoryContext, listProjectMemory, removeProjectMemory, } from "./core/project-memory.js";
50
50
  import { setupHarnessMcp, resolveMcpServerScript } from "./core/mcp-setup.js";
51
51
  import { initAdrDirectory, listAdrs, createAdr, syncMemoryToAdrs } from "./core/adr-engine.js";
52
52
  import { scanProjectSecurity, applySecurityFixes, formatSecurityReport } from "./core/security-scanner.js";
53
+ import { evaluateGate, generateBaselineJson } from "./core/policy-engine.js";
53
54
  import { formatSkillContext, loadSkillRegistry, selectSkills } from "./core/skill-registry.js";
54
55
  import { applyBeforeAgentHooks, loadHooks } from "./core/hook-engine.js";
55
56
  import { assertSupportedAgentPolicy, loadPermissionPolicy } from "./core/permission-policy.js";
56
57
  import { ModelRouter } from "./core/model-router.js";
57
58
  import { loadExecutionTrace, saveExecutionTrace } from "./core/execution-trace.js";
58
59
  import { createReplayRun } from "./core/run-replay.js";
59
- import { evaluateRun, evaluateRunWithJudge, formatEvaluationReport, loadEvaluationSuites, saveEvaluationReport, loadEvaluationReport } from "./core/evaluation-engine.js";
60
- import { createBenchmarkTrial, formatBenchmark, listBenchmarks, recordBenchmarkTrial } from "./core/benchmark-engine.js";
61
- import { executeBenchmarkMatrix, formatBenchmarkMatrix, loadBenchmarkMatrix, listBenchmarkMatrices, saveBenchmarkMatrix } from "./core/benchmark-matrix.js";
60
+ import { evaluateRun, evaluateRunWithJudge, formatEvaluationReport, loadEvaluationSuites, saveEvaluationReport, loadEvaluationReport, } from "./core/evaluation-engine.js";
61
+ import { createBenchmarkTrial, formatBenchmark, listBenchmarks, recordBenchmarkTrial, } from "./core/benchmark-engine.js";
62
+ import { executeBenchmarkMatrix, formatBenchmarkMatrix, loadBenchmarkMatrix, listBenchmarkMatrices, saveBenchmarkMatrix, } from "./core/benchmark-matrix.js";
62
63
  import { fingerprintAgentConfig } from "./core/benchmark-matrix.js";
63
64
  import { loadAgentConfigVersion } from "./core/agent-config-versioning.js";
64
65
  import { formatBenchmarkRegression, loadBenchmarkRegression } from "./core/benchmark-regression.js";
65
66
  import { buildAgentOptimizationPlan, formatAgentOptimizationPlan } from "./core/agent-optimization.js";
66
67
  import { auditAgentConfiguration, formatAgentSecurityReport } from "./core/agent-security-audit.js";
67
- import { buildAgentEvaluationIntelligence, formatAgentEvaluationIntelligence } from "./core/agent-evaluation-intelligence.js";
68
+ import { buildAgentEvaluationIntelligence, formatAgentEvaluationIntelligence, } from "./core/agent-evaluation-intelligence.js";
68
69
  import { assessAgentEvolution, formatAgentEvolutionAssessment } from "./core/agent-evolution.js";
69
- import { createSkillExperimentMatrix, formatSkillExperimentMatrix, createModelComparisonMatrix, formatModelComparisonMatrix } from "./core/agent-experiment-studio.js";
70
+ import { createSkillExperimentMatrix, formatSkillExperimentMatrix, createModelComparisonMatrix, formatModelComparisonMatrix, } from "./core/agent-experiment-studio.js";
70
71
  import { buildExperimentHistory, formatExperimentHistory } from "./core/experiment-history.js";
71
- import { captureAgentConfigVersion, compareAgentConfigVersions, formatAgentConfigVersion, listAgentConfigVersions, materializeAgentConfigVersion } from "./core/agent-config-versioning.js";
72
- import { decideAgentConfigRelease, formatAgentConfigRelease, listAgentConfigReleases, prepareAgentConfigRelease } from "./core/agent-config-release.js";
73
- import { captureExperimentManifest, compareExperimentManifests, formatExperimentManifest, loadExperimentManifest, saveExperimentManifest } from "./core/experiment-manifest.js";
72
+ import { captureAgentConfigVersion, compareAgentConfigVersions, formatAgentConfigVersion, listAgentConfigVersions, materializeAgentConfigVersion, } from "./core/agent-config-versioning.js";
73
+ import { decideAgentConfigRelease, formatAgentConfigRelease, listAgentConfigReleases, prepareAgentConfigRelease, } from "./core/agent-config-release.js";
74
+ import { captureExperimentManifest, compareExperimentManifests, formatExperimentManifest, loadExperimentManifest, saveExperimentManifest, } from "./core/experiment-manifest.js";
74
75
  function runSandboxProbe(args) {
75
76
  return new Promise((resolve, reject) => {
76
77
  const child = spawn("docker", args, { shell: false, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
@@ -92,8 +93,8 @@ function runSandboxProbe(args) {
92
93
  };
93
94
  child.stdout.on("data", append);
94
95
  child.stderr.on("data", append);
95
- child.once("error", error => finish(error));
96
- child.once("close", code => code === 0 ? finish() : finish(new Error("Docker probe exited with status " + code + ".")));
96
+ child.once("error", (error) => finish(error));
97
+ child.once("close", (code) => code === 0 ? finish() : finish(new Error("Docker probe exited with status " + code + ".")));
97
98
  const timer = setTimeout(() => {
98
99
  child.kill("SIGTERM");
99
100
  finish(new Error("Docker probe timed out after 15 seconds."));
@@ -247,7 +248,7 @@ async function main(args) {
247
248
  process.stdout.write(`anvil ${VERSION}\n`);
248
249
  return;
249
250
  case "init": {
250
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
251
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
251
252
  try {
252
253
  const result = await initializeProject(path.resolve(target));
253
254
  process.stdout.write(`ANVIL initialized in ${path.resolve(target)}\n`);
@@ -264,10 +265,13 @@ async function main(args) {
264
265
  return;
265
266
  }
266
267
  case "evals": {
267
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
268
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
268
269
  try {
269
270
  const suites = await loadEvaluationSuites(target);
270
- process.stdout.write(suites.length ? suites.map(suite => suite.id + " " + suite.name + " · " + suite.cases.length + " cases").join("\n") + "\n" : "No evaluation suites. Add JSON suites under .forge/evals/.\n");
271
+ process.stdout.write(suites.length
272
+ ? suites.map((suite) => suite.id + " " + suite.name + " · " + suite.cases.length + " cases").join("\n") +
273
+ "\n"
274
+ : "No evaluation suites. Add JSON suites under .forge/evals/.\n");
271
275
  }
272
276
  catch (error) {
273
277
  process.stderr.write("Could not load evaluation suites: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -277,10 +281,18 @@ async function main(args) {
277
281
  }
278
282
  case "agent-release": {
279
283
  const action = rest[0] ?? "list";
280
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
284
+ const option = (name) => {
285
+ const index = rest.indexOf(name);
286
+ return index >= 0 ? rest[index + 1] : undefined;
287
+ };
281
288
  const pathIndex = rest.indexOf("--path");
282
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
283
- const args = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path" && all[index - 1] !== "--reviewer" && all[index - 1] !== "--matrix");
289
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
290
+ const args = rest
291
+ .slice(1)
292
+ .filter((arg, index, all) => !arg.startsWith("--") &&
293
+ all[index - 1] !== "--path" &&
294
+ all[index - 1] !== "--reviewer" &&
295
+ all[index - 1] !== "--matrix");
284
296
  try {
285
297
  if (action === "prepare") {
286
298
  const [candidateVersionId, baselineVersionId] = args;
@@ -288,18 +300,32 @@ async function main(args) {
288
300
  if (!candidateVersionId || !baselineVersionId || !matrixId)
289
301
  throw new Error("Usage: anvil agent-release prepare CANDIDATE_CONFIG_ID BASELINE_CONFIG_ID --matrix MATRIX_ID --path PROJECT");
290
302
  const regression = await loadBenchmarkRegression(target, matrixId);
291
- const release = await prepareAgentConfigRelease(target, { candidateVersionId, baselineVersionId, regression });
303
+ const release = await prepareAgentConfigRelease(target, {
304
+ candidateVersionId,
305
+ baselineVersionId,
306
+ regression,
307
+ });
292
308
  process.stdout.write(formatAgentConfigRelease(release) + "\nHuman approval is still required. No live files were changed.\n");
293
309
  }
294
310
  else if (action === "list") {
295
311
  const items = await listAgentConfigReleases(target);
296
- process.stdout.write(items.length ? items.map(item => item.id + " " + item.status.padEnd(17) + " " + item.candidateVersionId + " " + item.benchmarkStatus).join("\n") + "\n" : "No agent configuration releases found.\n");
312
+ process.stdout.write(items.length
313
+ ? items
314
+ .map((item) => item.id +
315
+ " " +
316
+ item.status.padEnd(17) +
317
+ " " +
318
+ item.candidateVersionId +
319
+ " " +
320
+ item.benchmarkStatus)
321
+ .join("\n") + "\n"
322
+ : "No agent configuration releases found.\n");
297
323
  }
298
324
  else if (action === "show") {
299
325
  const id = args[0];
300
326
  if (!id)
301
327
  throw new Error("Usage: anvil agent-release show RELEASE_ID --path PROJECT");
302
- const item = (await listAgentConfigReleases(target)).find(entry => entry.id === id);
328
+ const item = (await listAgentConfigReleases(target)).find((entry) => entry.id === id);
303
329
  if (!item)
304
330
  throw new Error("Release not found: " + id);
305
331
  process.stdout.write(formatAgentConfigRelease(item) + "\n");
@@ -326,17 +352,39 @@ async function main(args) {
326
352
  case "agent-config": {
327
353
  const action = rest[0] ?? "list";
328
354
  const pathIndex = rest.indexOf("--path");
329
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
330
- const ids = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path" && all[index - 1] !== "--note" && all[index - 1] !== "--reviewer");
331
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
355
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
356
+ const ids = rest
357
+ .slice(1)
358
+ .filter((arg, index, all) => !arg.startsWith("--") &&
359
+ all[index - 1] !== "--path" &&
360
+ all[index - 1] !== "--note" &&
361
+ all[index - 1] !== "--reviewer");
362
+ const option = (name) => {
363
+ const index = rest.indexOf(name);
364
+ return index >= 0 ? rest[index + 1] : undefined;
365
+ };
332
366
  try {
333
367
  if (action === "snapshot") {
334
- const version = await captureAgentConfigVersion(target, { note: option("--note") ?? "", createdBy: option("--author") ?? "local-user" });
368
+ const version = await captureAgentConfigVersion(target, {
369
+ note: option("--note") ?? "",
370
+ createdBy: option("--author") ?? "local-user",
371
+ });
335
372
  process.stdout.write(formatAgentConfigVersion(version) + "\n");
336
373
  }
337
374
  else if (action === "list") {
338
375
  const versions = await listAgentConfigVersions(target);
339
- process.stdout.write(versions.length ? versions.map(item => item.id + " " + item.status.padEnd(9) + " " + item.createdAt + " " + item.files.length + " files").join("\n") + "\n" : "No agent configuration snapshots found. Run forge agent-config snapshot first.\n");
376
+ process.stdout.write(versions.length
377
+ ? versions
378
+ .map((item) => item.id +
379
+ " " +
380
+ item.status.padEnd(9) +
381
+ " " +
382
+ item.createdAt +
383
+ " " +
384
+ item.files.length +
385
+ " files")
386
+ .join("\n") + "\n"
387
+ : "No agent configuration snapshots found. Run forge agent-config snapshot first.\n");
340
388
  }
341
389
  else if (action === "show") {
342
390
  const id = ids[0];
@@ -347,9 +395,18 @@ async function main(args) {
347
395
  else if (action === "diff") {
348
396
  if (ids.length < 2)
349
397
  throw new Error("Usage: anvil agent-config diff BEFORE_ID AFTER_ID --path PROJECT");
350
- const [before, after] = await Promise.all([loadAgentConfigVersion(target, ids[0]), loadAgentConfigVersion(target, ids[1])]);
398
+ const [before, after] = await Promise.all([
399
+ loadAgentConfigVersion(target, ids[0]),
400
+ loadAgentConfigVersion(target, ids[1]),
401
+ ]);
351
402
  const diff = compareAgentConfigVersions(before, after);
352
- process.stdout.write(["Agent configuration diff", "Added: " + (diff.added.join(", ") || "none"), "Changed: " + (diff.changed.join(", ") || "none"), "Removed: " + (diff.removed.join(", ") || "none"), "Unchanged: " + diff.unchanged.length].join("\n") + "\n");
403
+ process.stdout.write([
404
+ "Agent configuration diff",
405
+ "Added: " + (diff.added.join(", ") || "none"),
406
+ "Changed: " + (diff.changed.join(", ") || "none"),
407
+ "Removed: " + (diff.removed.join(", ") || "none"),
408
+ "Unchanged: " + diff.unchanged.length,
409
+ ].join("\n") + "\n");
353
410
  }
354
411
  else if (action === "approve") {
355
412
  throw new Error("Direct snapshot approval is disabled. Use forge agent-release prepare with a release-ready benchmark matrix, then forge agent-release approve RELEASE_ID --reviewer NAME.");
@@ -364,9 +421,12 @@ async function main(args) {
364
421
  return;
365
422
  }
366
423
  case "experiment-history": {
367
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
424
+ const option = (name) => {
425
+ const index = rest.indexOf(name);
426
+ return index >= 0 ? rest[index + 1] : undefined;
427
+ };
368
428
  const pathIndex = rest.indexOf("--path");
369
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
429
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
370
430
  const rawLimit = option("--limit");
371
431
  const limit = rawLimit === undefined ? undefined : Number(rawLimit);
372
432
  if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1 || limit > 100)) {
@@ -378,7 +438,11 @@ async function main(args) {
378
438
  const suiteId = option("--suite");
379
439
  const candidateId = option("--candidate");
380
440
  const reports = await listBenchmarkMatrices(target);
381
- const history = buildExperimentHistory(reports, { ...(suiteId ? { suiteId } : {}), ...(candidateId ? { candidateId } : {}), ...(limit !== undefined ? { limit } : {}) });
441
+ const history = buildExperimentHistory(reports, {
442
+ ...(suiteId ? { suiteId } : {}),
443
+ ...(candidateId ? { candidateId } : {}),
444
+ ...(limit !== undefined ? { limit } : {}),
445
+ });
382
446
  process.stdout.write(rest.includes("--json") ? JSON.stringify(history, null, 2) + "\n" : formatExperimentHistory(history) + "\n");
383
447
  }
384
448
  catch (error) {
@@ -389,7 +453,10 @@ async function main(args) {
389
453
  }
390
454
  case "experiment-studio": {
391
455
  const action = rest[0] ?? "create";
392
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
456
+ const option = (name) => {
457
+ const index = rest.indexOf(name);
458
+ return index >= 0 ? rest[index + 1] : undefined;
459
+ };
393
460
  if (action !== "create" && action !== "compare") {
394
461
  process.stderr.write("Usage: anvil experiment-studio compare --id ID --name NAME --candidates id:provider:model,id:provider:model [--config-versions id=cfg-id,id=cfg-id] [--repeats 2] [--path PROJECT]\n");
395
462
  process.stderr.write(" or: forge experiment-studio create --id ID --name NAME --provider PROVIDER --model MODEL --skills skill-a,skill-b [--repeats 2] [--path PROJECT]\n");
@@ -402,20 +469,23 @@ async function main(args) {
402
469
  const rawCandidates = option("--candidates");
403
470
  const repeats = Number(option("--repeats") ?? "2");
404
471
  const pathIndex = rest.indexOf("--path");
405
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
472
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
406
473
  if (!id || !name || !rawCandidates) {
407
474
  process.stderr.write("Usage: anvil experiment-studio compare --id ID --name NAME --candidates id:provider:model,id:provider:model [--repeats 2] [--path PROJECT]\n");
408
475
  process.exitCode = 1;
409
476
  return;
410
477
  }
411
478
  try {
412
- const configVersions = new Map((option("--config-versions") ?? "").split(",").filter(Boolean).map(item => {
479
+ const configVersions = new Map((option("--config-versions") ?? "")
480
+ .split(",")
481
+ .filter(Boolean)
482
+ .map((item) => {
413
483
  const separator = item.indexOf("=");
414
484
  if (separator < 1 || separator === item.length - 1)
415
485
  throw new Error("Each config version must use candidate-id=cfg-xxxxxxxxxxxx format.");
416
486
  return [item.slice(0, separator), item.slice(separator + 1)];
417
487
  }));
418
- const candidates = rawCandidates.split(",").map(item => {
488
+ const candidates = rawCandidates.split(",").map((item) => {
419
489
  const [candidateId, candidateProvider, ...modelParts] = item.split(":");
420
490
  const model = modelParts.join(":");
421
491
  if (!candidateId || !candidateProvider || !model)
@@ -423,10 +493,15 @@ async function main(args) {
423
493
  if (!["openai", "claude-code", "anthropic", "google", "ollama"].includes(candidateProvider))
424
494
  throw new Error("Unsupported candidate provider: " + candidateProvider);
425
495
  const configVersionId = configVersions.get(candidateId);
426
- return { id: candidateId, provider: candidateProvider, model, ...(configVersionId ? { configVersionId } : {}) };
496
+ return {
497
+ id: candidateId,
498
+ provider: candidateProvider,
499
+ model,
500
+ ...(configVersionId ? { configVersionId } : {}),
501
+ };
427
502
  });
428
503
  for (const candidateId of configVersions.keys())
429
- if (!candidates.some(candidate => candidate.id === candidateId))
504
+ if (!candidates.some((candidate) => candidate.id === candidateId))
430
505
  throw new Error("Configuration snapshot references unknown candidate: " + candidateId);
431
506
  for (const candidate of candidates)
432
507
  if (candidate.configVersionId)
@@ -435,7 +510,11 @@ async function main(args) {
435
510
  const destination = path.join(target, ".forge", "experiments", id + ".json");
436
511
  const { mkdir, writeFile } = await import("node:fs/promises");
437
512
  await mkdir(path.dirname(destination), { recursive: true, mode: 0o700 });
438
- await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", { encoding: "utf8", flag: "wx", mode: 0o600 });
513
+ await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", {
514
+ encoding: "utf8",
515
+ flag: "wx",
516
+ mode: 0o600,
517
+ });
439
518
  process.stdout.write(formatModelComparisonMatrix(matrix) + "\nSaved: " + path.relative(target, destination) + "\n");
440
519
  }
441
520
  catch (error) {
@@ -449,7 +528,7 @@ async function main(args) {
449
528
  const skills = option("--skills");
450
529
  const repeats = Number(option("--repeats") ?? "2");
451
530
  const pathIndex = rest.indexOf("--path");
452
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
531
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
453
532
  if (!id || !name || !model || !skills) {
454
533
  process.stderr.write("Usage: anvil experiment-studio create --id ID --name NAME --provider openai|claude-code --model MODEL --skills skill-a,skill-b [--repeats 2] [--path PROJECT]\n");
455
534
  process.exitCode = 1;
@@ -461,16 +540,32 @@ async function main(args) {
461
540
  return;
462
541
  }
463
542
  try {
464
- const skillIds = skills.split(",").map(item => item.trim()).filter(Boolean);
465
- const matrix = createSkillExperimentMatrix({ id, name, baselineCandidateId: "baseline", treatmentCandidateId: "treatment", provider: provider, model, repeats, skillIds });
543
+ const skillIds = skills
544
+ .split(",")
545
+ .map((item) => item.trim())
546
+ .filter(Boolean);
547
+ const matrix = createSkillExperimentMatrix({
548
+ id,
549
+ name,
550
+ baselineCandidateId: "baseline",
551
+ treatmentCandidateId: "treatment",
552
+ provider: provider,
553
+ model,
554
+ repeats,
555
+ skillIds,
556
+ });
466
557
  const registry = await loadSkillRegistry(target);
467
- const unknown = skillIds.filter(skillId => !registry.some(skill => skill.id === skillId));
558
+ const unknown = skillIds.filter((skillId) => !registry.some((skill) => skill.id === skillId));
468
559
  if (unknown.length)
469
560
  throw new Error("Unknown workspace/built-in skill(s): " + unknown.join(", "));
470
561
  const destination = path.join(target, ".forge", "experiments", id + ".json");
471
562
  const { mkdir, writeFile } = await import("node:fs/promises");
472
563
  await mkdir(path.dirname(destination), { recursive: true, mode: 0o700 });
473
- await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", { encoding: "utf8", flag: "wx", mode: 0o600 });
564
+ await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", {
565
+ encoding: "utf8",
566
+ flag: "wx",
567
+ mode: 0o600,
568
+ });
474
569
  process.stdout.write(formatSkillExperimentMatrix(matrix) + "\nSaved: " + path.relative(target, destination) + "\n");
475
570
  }
476
571
  catch (error) {
@@ -482,7 +577,7 @@ async function main(args) {
482
577
  case "agent-evolution": {
483
578
  const matrixId = rest[0];
484
579
  const pathIndex = rest.indexOf("--path");
485
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
580
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
486
581
  if (!matrixId || matrixId.startsWith("--")) {
487
582
  process.stderr.write("Usage: anvil agent-evolution MATRIX_ID --path PROJECT\n");
488
583
  process.exitCode = 1;
@@ -490,7 +585,9 @@ async function main(args) {
490
585
  }
491
586
  try {
492
587
  const history = await listBenchmarkMatrices(target);
493
- const report = history.filter(item => item.matrixId === matrixId).sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
588
+ const report = history
589
+ .filter((item) => item.matrixId === matrixId)
590
+ .sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
494
591
  if (!report)
495
592
  throw new Error("Benchmark matrix not found: " + matrixId);
496
593
  const assessment = assessAgentEvolution(report);
@@ -528,7 +625,7 @@ async function main(args) {
528
625
  return next && !next.startsWith("--") ? next : undefined;
529
626
  };
530
627
  const pathIndex = rest.indexOf("--path");
531
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
628
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
532
629
  const rawLimit = option("--limit");
533
630
  const limit = rawLimit === undefined ? undefined : Number(rawLimit);
534
631
  if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 2 || limit > 50)) {
@@ -543,7 +640,7 @@ async function main(args) {
543
640
  const report = buildAgentEvaluationIntelligence(history, {
544
641
  ...(suiteId ? { suiteId } : {}),
545
642
  ...(candidateId ? { candidateId } : {}),
546
- ...(limit !== undefined ? { limit } : {})
643
+ ...(limit !== undefined ? { limit } : {}),
547
644
  });
548
645
  process.stdout.write(rest.includes("--json")
549
646
  ? JSON.stringify(report, null, 2) + "\n"
@@ -559,7 +656,7 @@ async function main(args) {
559
656
  }
560
657
  case "optimize-agents": {
561
658
  const pathIndex = rest.indexOf("--path");
562
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
659
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
563
660
  try {
564
661
  const matrices = await listBenchmarkMatrices(target);
565
662
  const plan = buildAgentOptimizationPlan(matrices);
@@ -575,7 +672,7 @@ async function main(args) {
575
672
  }
576
673
  case "benchmark-regression": {
577
674
  const targetIndex = rest.indexOf("--path");
578
- const target = path.resolve(targetIndex >= 0 ? rest[targetIndex + 1] ?? "." : ".");
675
+ const target = path.resolve(targetIndex >= 0 ? (rest[targetIndex + 1] ?? ".") : ".");
579
676
  const matrixId = rest.find((arg, index) => !arg.startsWith("--") && index === 0);
580
677
  try {
581
678
  const report = await loadBenchmarkRegression(target, matrixId);
@@ -591,21 +688,26 @@ async function main(args) {
591
688
  }
592
689
  case "experiment": {
593
690
  const action = rest[0] ?? "capture";
594
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
691
+ const option = (name) => {
692
+ const index = rest.indexOf(name);
693
+ return index >= 0 ? rest[index + 1] : undefined;
694
+ };
595
695
  const pathIndex = rest.indexOf("--path");
596
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
696
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
597
697
  try {
598
698
  if (action === "capture") {
599
699
  const matrixFile = option("--matrix-file");
600
700
  const suiteId = option("--suite");
601
701
  const matrixId = option("--matrix-id");
602
702
  const provider = option("--provider") ?? getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai");
603
- const model = option("--model") ?? getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ?? getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL", "gpt-4.1");
703
+ const model = option("--model") ??
704
+ getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ??
705
+ getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL", "gpt-4.1");
604
706
  if (!matrixFile || !suiteId || !matrixId)
605
707
  throw new Error("Usage: anvil experiment capture --matrix-file CONFIG.json --matrix-id ID --suite SUITE_ID [--provider ID] [--model NAME] [--path PROJECT]");
606
708
  const matrix = await loadBenchmarkMatrix(path.resolve(matrixFile));
607
709
  const suites = await loadEvaluationSuites(target);
608
- const suite = suites.find(item => item.id === suiteId);
710
+ const suite = suites.find((item) => item.id === suiteId);
609
711
  if (!suite)
610
712
  throw new Error("Evaluation suite not found: " + suiteId);
611
713
  const fingerprints = [];
@@ -613,10 +715,24 @@ async function main(args) {
613
715
  if (!candidate.configVersionId)
614
716
  continue;
615
717
  const snapshot = await loadAgentConfigVersion(target, candidate.configVersionId);
616
- fingerprints.push({ candidateId: candidate.id, configVersionId: snapshot.id, fingerprint: fingerprintAgentConfig(snapshot.files) });
718
+ fingerprints.push({
719
+ candidateId: candidate.id,
720
+ configVersionId: snapshot.id,
721
+ fingerprint: fingerprintAgentConfig(snapshot.files),
722
+ });
617
723
  }
618
724
  const baseUrl = getAnvilEnv("ANVIL_BASE_URL", "FORGE_BASE_URL");
619
- const manifest = await captureExperimentManifest({ projectPath: target, matrixFile: path.resolve(matrixFile), matrixId, suiteId, suite, provider, model, ...(baseUrl ? { baseUrl } : {}), agentConfigFingerprints: fingerprints });
725
+ const manifest = await captureExperimentManifest({
726
+ projectPath: target,
727
+ matrixFile: path.resolve(matrixFile),
728
+ matrixId,
729
+ suiteId,
730
+ suite,
731
+ provider,
732
+ model,
733
+ ...(baseUrl ? { baseUrl } : {}),
734
+ agentConfigFingerprints: fingerprints,
735
+ });
620
736
  const saved = await saveExperimentManifest(target, manifest);
621
737
  process.stdout.write(formatExperimentManifest(manifest) + "\nSaved: " + path.relative(target, saved) + "\n");
622
738
  }
@@ -630,9 +746,18 @@ async function main(args) {
630
746
  const ids = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path");
631
747
  if (ids.length !== 2)
632
748
  throw new Error("Usage: anvil experiment compare EXPERIMENT_A EXPERIMENT_B --path PROJECT");
633
- const [a, b] = await Promise.all([loadExperimentManifest(target, ids[0]), loadExperimentManifest(target, ids[1])]);
749
+ const [a, b] = await Promise.all([
750
+ loadExperimentManifest(target, ids[0]),
751
+ loadExperimentManifest(target, ids[1]),
752
+ ]);
634
753
  const comparison = compareExperimentManifests(a, b);
635
- process.stdout.write("Experiment reproducibility: " + (comparison.compatible ? "COMPATIBLE" : "NOT COMPARABLE") + "\n" + (comparison.differences.length ? comparison.differences.map(item => " - " + item).join("\n") : " Critical manifest fields match.") + "\n");
754
+ process.stdout.write("Experiment reproducibility: " +
755
+ (comparison.compatible ? "COMPATIBLE" : "NOT COMPARABLE") +
756
+ "\n" +
757
+ (comparison.differences.length
758
+ ? comparison.differences.map((item) => " - " + item).join("\n")
759
+ : " Critical manifest fields match.") +
760
+ "\n");
636
761
  if (!comparison.compatible)
637
762
  process.exitCode = 2;
638
763
  }
@@ -646,14 +771,19 @@ async function main(args) {
646
771
  return;
647
772
  }
648
773
  case "benchmark-matrix": {
649
- const configFile = rest.find(arg => !arg.startsWith("--"));
774
+ const configFile = rest.find((arg) => !arg.startsWith("--"));
650
775
  const suiteIndex = rest.indexOf("--suite"), goalIndex = rest.indexOf("--goal"), pathIndex = rest.indexOf("--path");
651
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
776
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
652
777
  const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
653
778
  const goalEnd = goalIndex >= 0 ? rest.findIndex((arg, index) => index > goalIndex && arg.startsWith("--")) : -1;
654
- const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd >= 0 ? goalEnd : rest.length).join(" ").trim() : "";
779
+ const goal = goalIndex >= 0
780
+ ? rest
781
+ .slice(goalIndex + 1, goalEnd >= 0 ? goalEnd : rest.length)
782
+ .join(" ")
783
+ .trim()
784
+ : "";
655
785
  if (!configFile || !suiteId || !goal) {
656
- process.stderr.write("Usage: anvil benchmark-matrix CONFIG.json --suite SUITE_ID --goal \"engineering task\" [--path PROJECT]\n");
786
+ process.stderr.write('Usage: anvil benchmark-matrix CONFIG.json --suite SUITE_ID --goal "engineering task" [--path PROJECT]\n');
657
787
  process.exitCode = 1;
658
788
  return;
659
789
  }
@@ -667,7 +797,7 @@ async function main(args) {
667
797
  configVersions.set(candidate.id, { id: snapshot.id, fingerprint: fingerprintAgentConfig(snapshot.files) });
668
798
  }
669
799
  const suites = await loadEvaluationSuites(target);
670
- const suite = suites.find(item => item.id === suiteId);
800
+ const suite = suites.find((item) => item.id === suiteId);
671
801
  if (!suite)
672
802
  throw new Error("Evaluation suite not found: " + suiteId);
673
803
  const apiKey = getAnvilEnv("ANVIL_API_KEY", "FORGE_API_KEY") ?? process.env.OPENAI_API_KEY ?? "";
@@ -675,43 +805,82 @@ async function main(args) {
675
805
  const report = await executeBenchmarkMatrix(config, async (candidate, _repeat) => {
676
806
  const createProvider = (selectedModel) => candidate.provider === "claude-code"
677
807
  ? new ClaudeCodeProvider({ cwd: target, model: selectedModel })
678
- : createHostedAgentProvider({ ...process.env, ANVIL_AI_PROVIDER: candidate.provider, FORGE_AI_PROVIDER: candidate.provider, ...(apiKey ? { ANVIL_API_KEY: apiKey, FORGE_API_KEY: apiKey } : {}), ...(baseUrl ? { ANVIL_BASE_URL: baseUrl, FORGE_BASE_URL: baseUrl } : {}) }, selectedModel);
808
+ : createHostedAgentProvider({
809
+ ...process.env,
810
+ ANVIL_AI_PROVIDER: candidate.provider,
811
+ FORGE_AI_PROVIDER: candidate.provider,
812
+ ...(apiKey ? { ANVIL_API_KEY: apiKey, FORGE_API_KEY: apiKey } : {}),
813
+ ...(baseUrl ? { ANVIL_BASE_URL: baseUrl, FORGE_BASE_URL: baseUrl } : {}),
814
+ }, selectedModel);
679
815
  const router = new ModelRouter({
680
- fastModel: candidate.model, balancedModel: candidate.model, reasoningModel: candidate.model,
816
+ fastModel: candidate.model,
817
+ balancedModel: candidate.model,
818
+ reasoningModel: candidate.model,
681
819
  retryAttempts: Number(getAnvilEnv("ANVIL_MODEL_RETRY_ATTEMPTS", "FORGE_MODEL_RETRY_ATTEMPTS", "2")),
682
820
  retryDelayMs: Number(getAnvilEnv("ANVIL_MODEL_RETRY_DELAY_MS", "FORGE_MODEL_RETRY_DELAY_MS", "500")),
683
- ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK") ? { maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")) } : {}),
684
- ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK") ? { maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")) } : {}),
685
- ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN") ? { maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")) } : {}),
686
- ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT") ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
687
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS") ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) } : {}),
688
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT") ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
821
+ ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")
822
+ ? {
823
+ maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")),
824
+ }
825
+ : {}),
826
+ ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")
827
+ ? {
828
+ maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")),
829
+ }
830
+ : {}),
831
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")
832
+ ? {
833
+ maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")),
834
+ }
835
+ : {}),
836
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT")
837
+ ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() }
838
+ : {}),
839
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")
840
+ ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) }
841
+ : {}),
842
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT")
843
+ ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() }
844
+ : {}),
689
845
  createProvider,
690
846
  });
691
847
  const configWorkspace = await mkdtemp(path.join(os.tmpdir(), "forge-candidate-config-"));
692
848
  try {
693
849
  const snapshotId = candidate.configVersionId;
694
- const snapshot = snapshotId ? await materializeAgentConfigVersion(target, snapshotId, configWorkspace) : undefined;
695
- if (snapshotId && !snapshot?.files.some(file => file.path === "hooks.json")) {
696
- throw new Error("Configuration snapshot " + snapshotId + " predates policy-hook isolation. Capture a new snapshot before benchmarking this candidate.");
850
+ const snapshot = snapshotId
851
+ ? await materializeAgentConfigVersion(target, snapshotId, configWorkspace)
852
+ : undefined;
853
+ if (snapshotId && !snapshot?.files.some((file) => file.path === "hooks.json")) {
854
+ throw new Error("Configuration snapshot " +
855
+ snapshotId +
856
+ " predates policy-hook isolation. Capture a new snapshot before benchmarking this candidate.");
697
857
  }
698
858
  const configRoot = snapshotId ? configWorkspace : target;
699
859
  const run = createReviewPlan(goal);
700
860
  const skillRegistry = await loadSkillRegistry(configRoot);
701
861
  const agentRegistry = await loadAgentRegistry(configRoot);
702
862
  const requestedSkillIds = candidate.skillIds;
703
- const unknownSkills = (requestedSkillIds ?? []).filter(id => !skillRegistry.some(skill => skill.id === id));
863
+ const unknownSkills = (requestedSkillIds ?? []).filter((id) => !skillRegistry.some((skill) => skill.id === id));
704
864
  if (unknownSkills.length)
705
865
  throw new Error("Candidate " + candidate.id + " references unknown skill(s): " + unknownSkills.join(", "));
706
- const selectedSkillsForTask = (task) => requestedSkillIds === undefined ? selectSkills(skillRegistry, task.agent + " " + task.title + " " + task.prompt) : skillRegistry.filter(skill => requestedSkillIds.includes(skill.id));
707
- const agentGuidanceForTask = (task) => agentRegistry.find(agent => agent.id === task.agent)?.instructions ?? "";
866
+ const selectedSkillsForTask = (task) => requestedSkillIds === undefined
867
+ ? selectSkills(skillRegistry, task.agent + " " + task.title + " " + task.prompt)
868
+ : skillRegistry.filter((skill) => requestedSkillIds.includes(skill.id));
869
+ const agentGuidanceForTask = (task) => agentRegistry.find((agent) => agent.id === task.agent)?.instructions ?? "";
708
870
  const permissions = await loadPermissionPolicy(configRoot);
709
871
  if (permissions.mode !== "proposal-only" || permissions.allowedTools.length > 0)
710
- throw new Error("Benchmark candidate " + candidate.id + " requests unsupported tool permissions. FORGE currently enforces proposal-only execution with no enabled tools.");
872
+ throw new Error("Benchmark candidate " +
873
+ candidate.id +
874
+ " requests unsupported tool permissions. FORGE currently enforces proposal-only execution with no enabled tools.");
711
875
  const hooks = await loadHooks(configRoot);
712
876
  for (const record of run.tasks) {
713
877
  const applied = applyBeforeAgentHooks(record.task, hooks);
714
- record.task = { ...applied.task, prompt: applied.context.length ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n") : applied.task.prompt };
878
+ record.task = {
879
+ ...applied.task,
880
+ prompt: applied.context.length
881
+ ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n")
882
+ : applied.task.prompt,
883
+ };
715
884
  if (applied.blockedReason) {
716
885
  record.status = "blocked";
717
886
  record.error = "Blocked by policy hook: " + applied.blockedReason;
@@ -720,17 +889,49 @@ async function main(args) {
720
889
  const memory = await buildProjectMemoryContext(target, goal);
721
890
  const projectContext = memory.text;
722
891
  const started = Date.now();
723
- const completed = await advanceRun(run, router, new Set(), { maxConcurrency: 4, projectContext, projectContextForTask: async (task) => [agentGuidanceForTask(task), formatSkillContext(selectedSkillsForTask(task)), await buildAgentWorkspaceContext(target, task)].filter(Boolean).join("\n\n") });
892
+ const completed = await advanceRun(run, router, new Set(), {
893
+ maxConcurrency: 4,
894
+ projectContext,
895
+ projectContextForTask: async (task) => [
896
+ agentGuidanceForTask(task),
897
+ formatSkillContext(selectedSkillsForTask(task)),
898
+ await buildAgentWorkspaceContext(target, task),
899
+ ]
900
+ .filter(Boolean)
901
+ .join("\n\n"),
902
+ });
724
903
  const durationMs = Date.now() - started;
725
904
  await saveRun(target, completed);
726
905
  await saveExecutionTrace(target, completed.id, router.traces);
727
906
  const evaluation = evaluateRun(completed, suite);
728
907
  await saveEvaluationReport(target, evaluation);
729
908
  return {
730
- runId: completed.id, score: evaluation.score, passed: evaluation.passed,
909
+ runId: completed.id,
910
+ score: evaluation.score,
911
+ passed: evaluation.passed,
731
912
  durationMs,
732
913
  estimatedTokens: router.traces.reduce((sum, item) => sum + item.inputTokensEstimate + item.outputTokensEstimate, 0),
733
- agentScores: evaluation.cases.filter(item => item.status === "scored").map(item => { const task = completed.tasks.find(record => record.task.id === item.taskId)?.task; const appliedSkills = task ? selectedSkillsForTask(task) : []; return { agent: item.agent, caseId: item.caseId, score: item.score, passed: item.passed, skillIds: appliedSkills.map(skill => skill.id), failedCriteria: item.criteria.filter(criterion => !criterion.passed).map(criterion => ({ id: criterion.id, failedChecks: criterion.checks.filter(check => !check.passed).map(check => check.rule + ": " + check.detail) })) }; }),
914
+ agentScores: evaluation.cases
915
+ .filter((item) => item.status === "scored")
916
+ .map((item) => {
917
+ const task = completed.tasks.find((record) => record.task.id === item.taskId)?.task;
918
+ const appliedSkills = task ? selectedSkillsForTask(task) : [];
919
+ return {
920
+ agent: item.agent,
921
+ caseId: item.caseId,
922
+ score: item.score,
923
+ passed: item.passed,
924
+ skillIds: appliedSkills.map((skill) => skill.id),
925
+ failedCriteria: item.criteria
926
+ .filter((criterion) => !criterion.passed)
927
+ .map((criterion) => ({
928
+ id: criterion.id,
929
+ failedChecks: criterion.checks
930
+ .filter((check) => !check.passed)
931
+ .map((check) => check.rule + ": " + check.detail),
932
+ })),
933
+ };
934
+ }),
734
935
  };
735
936
  }
736
937
  finally {
@@ -740,19 +941,34 @@ async function main(args) {
740
941
  report.suiteId = suite.id;
741
942
  report.goal = goal;
742
943
  if (configVersions.size)
743
- report.candidateProvenance = [...configVersions.entries()].map(([candidateId, item]) => ({ candidateId, configVersionId: item.id, fingerprint: item.fingerprint }));
944
+ report.candidateProvenance = [...configVersions.entries()].map(([candidateId, item]) => ({
945
+ candidateId,
946
+ configVersionId: item.id,
947
+ fingerprint: item.fingerprint,
948
+ }));
744
949
  const experimentManifest = await captureExperimentManifest({
745
- projectPath: target, matrixFile: path.resolve(configFile), matrixId: config.id, suiteId: suite.id, suite,
746
- provider: "matrix", model: "candidate-specific",
950
+ projectPath: target,
951
+ matrixFile: path.resolve(configFile),
952
+ matrixId: config.id,
953
+ suiteId: suite.id,
954
+ suite,
955
+ provider: "matrix",
956
+ model: "candidate-specific",
747
957
  ...(process.env.FORGE_BASE_URL ? { baseUrl: process.env.FORGE_BASE_URL } : {}),
748
- agentConfigFingerprints: report.candidateProvenance ?? []
958
+ agentConfigFingerprints: report.candidateProvenance ?? [],
749
959
  });
750
960
  const manifestPath = await saveExperimentManifest(target, experimentManifest);
751
961
  report.experimentManifest = { id: experimentManifest.id, fingerprint: experimentManifest.fingerprint };
752
962
  const saved = await saveBenchmarkMatrix(target, report);
753
- process.stdout.write("Experiment manifest: " + experimentManifest.id + " · sha256:" + experimentManifest.fingerprint + "\nManifest saved: " + path.relative(target, manifestPath) + "\n");
963
+ process.stdout.write("Experiment manifest: " +
964
+ experimentManifest.id +
965
+ " · sha256:" +
966
+ experimentManifest.fingerprint +
967
+ "\nManifest saved: " +
968
+ path.relative(target, manifestPath) +
969
+ "\n");
754
970
  process.stdout.write(formatBenchmarkMatrix(report) + "\nSaved: " + path.relative(target, saved) + "\n");
755
- if (report.results.some(item => item.status === "failed") || report.results.some(item => !item.passed))
971
+ if (report.results.some((item) => item.status === "failed") || report.results.some((item) => !item.passed))
756
972
  process.exitCode = 2;
757
973
  }
758
974
  catch (error) {
@@ -764,12 +980,14 @@ async function main(args) {
764
980
  case "benchmark": {
765
981
  const pathIndex = rest.indexOf("--path"), suiteIndex = rest.indexOf("--suite");
766
982
  const runId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path" && rest[index - 1] !== "--suite");
767
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
983
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
768
984
  const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
769
985
  try {
770
986
  if (!runId) {
771
987
  const documents = await listBenchmarks(target);
772
- process.stdout.write(documents.length ? documents.map(document => formatBenchmark(document)).join("\n\n") + "\n" : "No benchmark history. Evaluate and record a run with: forge benchmark RUN_ID --path .\n");
988
+ process.stdout.write(documents.length
989
+ ? documents.map((document) => formatBenchmark(document)).join("\n\n") + "\n"
990
+ : "No benchmark history. Evaluate and record a run with: forge benchmark RUN_ID --path .\n");
773
991
  return;
774
992
  }
775
993
  const report = await loadEvaluationReport(target, runId, suiteId);
@@ -784,9 +1002,9 @@ async function main(args) {
784
1002
  return;
785
1003
  }
786
1004
  case "evaluate": {
787
- const runId = rest.find(arg => !arg.startsWith("--"));
1005
+ const runId = rest.find((arg) => !arg.startsWith("--"));
788
1006
  const pathIndex = rest.indexOf("--path"), suiteIndex = rest.indexOf("--suite");
789
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1007
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
790
1008
  const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
791
1009
  if (!runId) {
792
1010
  process.stderr.write("Usage: anvil evaluate RUNID [--suite SUITE_ID] [--path PROJECT]\n");
@@ -795,20 +1013,27 @@ async function main(args) {
795
1013
  }
796
1014
  try {
797
1015
  const suites = await loadEvaluationSuites(target);
798
- const suite = suiteId ? suites.find(item => item.id === suiteId) : suites[0];
1016
+ const suite = suiteId ? suites.find((item) => item.id === suiteId) : suites[0];
799
1017
  if (!suite)
800
- throw new Error(suiteId ? "Evaluation suite not found: " + suiteId : "No evaluation suite found. Add one under .forge/evals/.");
1018
+ throw new Error(suiteId
1019
+ ? "Evaluation suite not found: " + suiteId
1020
+ : "No evaluation suite found. Add one under .forge/evals/.");
801
1021
  const run = await loadRun(target, runId);
802
1022
  const judgeEnabled = rest.includes("--judge");
803
1023
  let report;
804
1024
  if (judgeEnabled) {
805
1025
  const providerChoice = getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai");
806
- const model = getAnvilEnv("ANVIL_EVAL_JUDGE_MODEL", "FORGE_EVAL_JUDGE_MODEL") ?? getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ?? getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL") ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1");
1026
+ const model = getAnvilEnv("ANVIL_EVAL_JUDGE_MODEL", "FORGE_EVAL_JUDGE_MODEL") ??
1027
+ getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ??
1028
+ getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL") ??
1029
+ (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1");
807
1030
  const apiKey = getAnvilEnv("ANVIL_API_KEY", "FORGE_API_KEY") ?? process.env.OPENAI_API_KEY ?? "";
808
1031
  const baseUrl = getAnvilEnv("ANVIL_BASE_URL", "FORGE_BASE_URL", "https://api.openai.com/v1");
809
1032
  if (providerChoice !== "claude-code" && !apiKey)
810
1033
  throw new Error("Set ANVIL_API_KEY, FORGE_API_KEY, or OPENAI_API_KEY to use model-judged evaluation.");
811
- const judgeProvider = providerChoice === "claude-code" ? new ClaudeCodeProvider({ cwd: target, model }) : new OpenAICompatibleProvider({ apiKey, model, baseUrl });
1034
+ const judgeProvider = providerChoice === "claude-code"
1035
+ ? new ClaudeCodeProvider({ cwd: target, model })
1036
+ : new OpenAICompatibleProvider({ apiKey, model, baseUrl });
812
1037
  report = await evaluateRunWithJudge(run, suite, judgeProvider);
813
1038
  }
814
1039
  else
@@ -816,7 +1041,12 @@ async function main(args) {
816
1041
  const reportPath = await saveEvaluationReport(target, report);
817
1042
  process.stdout.write(formatEvaluationReport(report) + "\n");
818
1043
  if ("judgedCases" in report) {
819
- process.stdout.write("Model judge: " + report.judge.provider + (report.judge.model ? " / " + report.judge.model : "") + " · " + report.judge.evaluatedCases + " cases\n");
1044
+ process.stdout.write("Model judge: " +
1045
+ report.judge.provider +
1046
+ (report.judge.model ? " / " + report.judge.model : "") +
1047
+ " · " +
1048
+ report.judge.evaluatedCases +
1049
+ " cases\n");
820
1050
  for (const item of report.judgedCases)
821
1051
  process.stdout.write(" " + item.caseId + " · " + item.result.score + "/100 · " + item.result.rationale + "\n");
822
1052
  }
@@ -831,7 +1061,7 @@ async function main(args) {
831
1061
  return;
832
1062
  }
833
1063
  case "agents": {
834
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
1064
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
835
1065
  try {
836
1066
  const agents = await loadAgentRegistry(path.resolve(target));
837
1067
  process.stdout.write("FORGE agent registry\n\n");
@@ -847,22 +1077,28 @@ async function main(args) {
847
1077
  return;
848
1078
  }
849
1079
  case "skills": {
850
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
851
- const target = path.resolve(rest.find(arg => !arg.startsWith("--") && arg !== option("--show") && arg !== option("--match")) ?? ".");
1080
+ const option = (name) => {
1081
+ const index = rest.indexOf(name);
1082
+ return index >= 0 ? rest[index + 1] : undefined;
1083
+ };
1084
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--") && arg !== option("--show") && arg !== option("--match")) ?? ".");
852
1085
  try {
853
1086
  const skills = await loadSkillRegistry(target);
854
1087
  const showId = option("--show");
855
1088
  const matchGoal = option("--match");
856
1089
  if (showId) {
857
- const skill = skills.find(item => item.id === showId);
1090
+ const skill = skills.find((item) => item.id === showId);
858
1091
  if (!skill)
859
1092
  throw new Error("Skill not found: " + showId);
860
1093
  process.stdout.write((rest.includes("--json") ? JSON.stringify(skill, null, 2) : formatSkillContext([skill])) + "\n");
861
1094
  }
862
1095
  else if (matchGoal !== undefined) {
863
1096
  const selected = selectSkills(skills, matchGoal);
864
- process.stdout.write(rest.includes("--json") ? JSON.stringify(selected, null, 2) + "\n" :
865
- selected.length ? selected.map(skill => skill.id + " " + skill.name + " — " + skill.description).join("\n") + "\n" : "No skills matched that goal.\n");
1097
+ process.stdout.write(rest.includes("--json")
1098
+ ? JSON.stringify(selected, null, 2) + "\n"
1099
+ : selected.length
1100
+ ? selected.map((skill) => skill.id + " " + skill.name + " — " + skill.description).join("\n") + "\n"
1101
+ : "No skills matched that goal.\n");
866
1102
  }
867
1103
  else if (rest.includes("--json")) {
868
1104
  process.stdout.write(JSON.stringify(skills, null, 2) + "\n");
@@ -871,7 +1107,7 @@ async function main(args) {
871
1107
  process.stdout.write("FORGE skill registry (" + skills.length + ")\n\n");
872
1108
  for (const skill of skills)
873
1109
  process.stdout.write(" " + skill.id.padEnd(24) + " " + skill.name + " [" + skill.source + "]\n " + skill.description + "\n");
874
- process.stdout.write("\nUse --show ID to inspect instructions or --match \"GOAL\" to preview task selection.\n");
1110
+ process.stdout.write('\nUse --show ID to inspect instructions or --match "GOAL" to preview task selection.\n');
875
1111
  }
876
1112
  }
877
1113
  catch (error) {
@@ -881,12 +1117,10 @@ async function main(args) {
881
1117
  return;
882
1118
  }
883
1119
  case "agent-security": {
884
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
1120
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
885
1121
  try {
886
1122
  const report = await auditAgentConfiguration(target);
887
- process.stdout.write(rest.includes("--json")
888
- ? JSON.stringify(report, null, 2) + "\n"
889
- : formatAgentSecurityReport(report) + "\n");
1123
+ process.stdout.write(rest.includes("--json") ? JSON.stringify(report, null, 2) + "\n" : formatAgentSecurityReport(report) + "\n");
890
1124
  if (report.summary.critical > 0)
891
1125
  process.exitCode = 2;
892
1126
  }
@@ -899,7 +1133,7 @@ async function main(args) {
899
1133
  case "hook":
900
1134
  case "hooks": {
901
1135
  if (rest[0] === "install" || rest.includes("--install")) {
902
- const target = path.resolve(rest.find(arg => !arg.startsWith("--") && arg !== "install" && arg !== "pre-commit") ?? ".");
1136
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--") && arg !== "install" && arg !== "pre-commit") ?? ".");
903
1137
  try {
904
1138
  const { installPrecommitHook } = await import("./core/project-cleaner.js");
905
1139
  const result = await installPrecommitHook(target);
@@ -913,10 +1147,13 @@ async function main(args) {
913
1147
  }
914
1148
  return;
915
1149
  }
916
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
1150
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
917
1151
  try {
918
1152
  const hooks = await loadHooks(target);
919
- process.stdout.write(hooks.length ? hooks.map(hook => hook.id + " [" + hook.event + "] " + hook.action + ": " + hook.message).join("\n") + "\n" : "No lifecycle hooks configured.\n");
1153
+ process.stdout.write(hooks.length
1154
+ ? hooks.map((hook) => hook.id + " [" + hook.event + "] " + hook.action + ": " + hook.message).join("\n") +
1155
+ "\n"
1156
+ : "No lifecycle hooks configured.\n");
920
1157
  }
921
1158
  catch (error) {
922
1159
  process.stderr.write("Could not load hooks: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -926,19 +1163,24 @@ async function main(args) {
926
1163
  }
927
1164
  case "memory": {
928
1165
  const [action = "list", ...memoryArgs] = rest;
929
- const option = (name) => { const index = memoryArgs.indexOf(name); return index >= 0 ? memoryArgs[index + 1] : undefined; };
1166
+ const option = (name) => {
1167
+ const index = memoryArgs.indexOf(name);
1168
+ return index >= 0 ? memoryArgs[index + 1] : undefined;
1169
+ };
930
1170
  const skipValues = new Set();
931
1171
  for (let i = 0; i < memoryArgs.length; i++) {
932
1172
  if (memoryArgs[i]?.startsWith("--"))
933
1173
  skipValues.add(i + 1);
934
1174
  }
935
1175
  const positional = memoryArgs.filter((arg, i) => !arg.startsWith("--") && !skipValues.has(i));
936
- const targetPath = positional.find(arg => !arg.startsWith("mem-")) ?? ".";
1176
+ const targetPath = positional.find((arg) => !arg.startsWith("mem-")) ?? ".";
937
1177
  const target = path.resolve(targetPath);
938
1178
  try {
939
1179
  if (action === "list") {
940
1180
  const entries = await listProjectMemory(target);
941
- process.stdout.write(entries.length ? entries.map(entry => `${entry.id} [${entry.kind}] ${entry.content}`).join("\n") + "\n" : "No project memory entries.\n");
1181
+ process.stdout.write(entries.length
1182
+ ? entries.map((entry) => `${entry.id} [${entry.kind}] ${entry.content}`).join("\n") + "\n"
1183
+ : "No project memory entries.\n");
942
1184
  }
943
1185
  else if (action === "add") {
944
1186
  const kind = option("--kind");
@@ -951,7 +1193,7 @@ async function main(args) {
951
1193
  process.stdout.write(`Saved ${entry.id} to project memory\n`);
952
1194
  }
953
1195
  else if (action === "remove") {
954
- const id = memoryArgs.find(arg => arg.startsWith("mem-"));
1196
+ const id = memoryArgs.find((arg) => arg.startsWith("mem-"));
955
1197
  if (!id)
956
1198
  throw new Error("Usage: anvil memory remove [path] mem-ID");
957
1199
  const removed = await removeProjectMemory(target, id);
@@ -974,7 +1216,10 @@ async function main(args) {
974
1216
  }
975
1217
  case "mcp": {
976
1218
  const [subcommand = "info", ...mcpArgs] = rest;
977
- const option = (name) => { const index = mcpArgs.indexOf(name); return index >= 0 ? mcpArgs[index + 1] : undefined; };
1219
+ const option = (name) => {
1220
+ const index = mcpArgs.indexOf(name);
1221
+ return index >= 0 ? mcpArgs[index + 1] : undefined;
1222
+ };
978
1223
  const skipValues = new Set();
979
1224
  for (let i = 0; i < mcpArgs.length; i++) {
980
1225
  if (mcpArgs[i]?.startsWith("--"))
@@ -1028,7 +1273,10 @@ Subcommands:
1028
1273
  return;
1029
1274
  }
1030
1275
  const [action = "list", ...adrArgs] = rest;
1031
- const option = (name) => { const index = adrArgs.indexOf(name); return index >= 0 ? adrArgs[index + 1] : undefined; };
1276
+ const option = (name) => {
1277
+ const index = adrArgs.indexOf(name);
1278
+ return index >= 0 ? adrArgs[index + 1] : undefined;
1279
+ };
1032
1280
  const skipValues = new Set();
1033
1281
  for (let i = 0; i < adrArgs.length; i++) {
1034
1282
  if (adrArgs[i]?.startsWith("--"))
@@ -1039,12 +1287,14 @@ Subcommands:
1039
1287
  if (action === "init") {
1040
1288
  const target = path.resolve(positional[0] ?? ".");
1041
1289
  const res = await initAdrDirectory(target);
1042
- process.stdout.write(res.created ? `Initialized Architecture Decision Records in ${res.path}\n` : `ADR directory already initialized in ${res.path}\n`);
1290
+ process.stdout.write(res.created
1291
+ ? `Initialized Architecture Decision Records in ${res.path}\n`
1292
+ : `ADR directory already initialized in ${res.path}\n`);
1043
1293
  }
1044
1294
  else if (action === "new") {
1045
1295
  const title = positional[0];
1046
1296
  if (!title)
1047
- throw new Error("Usage: anvil adr new \"Title of decision\" [path] [--status Proposed|Accepted]");
1297
+ throw new Error('Usage: anvil adr new "Title of decision" [path] [--status Proposed|Accepted]');
1048
1298
  const target = path.resolve(positional[1] ?? ".");
1049
1299
  const status = (option("--status") ?? "Accepted");
1050
1300
  const context = option("--context");
@@ -1056,7 +1306,9 @@ Subcommands:
1056
1306
  const target = path.resolve(positional[0] ?? ".");
1057
1307
  const res = await syncMemoryToAdrs(target);
1058
1308
  process.stdout.write(res.syncedCount > 0
1059
- ? `Synced ${res.syncedCount} decisions from project memory to docs/adr/:\n` + res.createdAdrs.map(a => ` - [${a.id}] ${a.filename}`).join("\n") + "\n"
1309
+ ? `Synced ${res.syncedCount} decisions from project memory to docs/adr/:\n` +
1310
+ res.createdAdrs.map((a) => ` - [${a.id}] ${a.filename}`).join("\n") +
1311
+ "\n"
1060
1312
  : `All project memory decisions are already synced to docs/adr/.\n`);
1061
1313
  }
1062
1314
  else if (action === "list") {
@@ -1097,14 +1349,16 @@ Options:
1097
1349
  }
1098
1350
  const isJson = rest.includes("--json");
1099
1351
  const shouldFix = rest.includes("--fix");
1100
- const positional = rest.filter(arg => !arg.startsWith("--"));
1352
+ const positional = rest.filter((arg) => !arg.startsWith("--"));
1101
1353
  const target = path.resolve(positional[0] ?? ".");
1102
1354
  try {
1103
1355
  const report = await scanProjectSecurity(target);
1104
1356
  if (shouldFix && report.findings.length > 0) {
1105
1357
  const fixResult = await applySecurityFixes(target, report.findings);
1106
1358
  if (fixResult.fixedCount > 0) {
1107
- process.stdout.write(`Applied ${fixResult.fixedCount} security fix(es):\n` + fixResult.details.map(d => ` ✓ ${d}`).join("\n") + "\n\n");
1359
+ process.stdout.write(`Applied ${fixResult.fixedCount} security fix(es):\n` +
1360
+ fixResult.details.map((d) => ` ✓ ${d}`).join("\n") +
1361
+ "\n\n");
1108
1362
  }
1109
1363
  }
1110
1364
  if (isJson) {
@@ -1132,11 +1386,13 @@ and code quality rules against policies. Emits human-readable reports, machine-r
1132
1386
  or OASIS SARIF v2.1.0 for PR comments and GitHub Security tab integration.
1133
1387
 
1134
1388
  Options:
1135
- --sarif PATH Export findings to an OASIS SARIF v2.1.0 report file
1136
- --policy PATH Custom JSON policy file (defaults to .anvil/policy.json if present)
1137
- --fail-on-warnings Fail gate (exit 1) on medium-severity warnings as well as critical/high errors
1138
- --json Output full gate evaluation in machine-readable JSON format
1139
- --help Show this help message
1389
+ --sarif PATH Export findings to an OASIS SARIF v2.1.0 report file
1390
+ --policy PATH Custom JSON policy file (defaults to .anvil/policy.json if present)
1391
+ --baseline PATH Baseline JSON file to suppress known existing findings
1392
+ --generate-baseline PATH Export current findings as a baseline JSON file
1393
+ --fail-on-warnings Fail gate (exit 1) on medium-severity warnings as well as critical/high errors
1394
+ --json Output full gate evaluation in machine-readable JSON format
1395
+ --help Show this help message
1140
1396
 
1141
1397
  Exit codes:
1142
1398
  0 Gate passed: all policy thresholds and security checks satisfied.
@@ -1155,13 +1411,36 @@ Exit codes:
1155
1411
  const failOnWarnings = rest.includes("--fail-on-warnings");
1156
1412
  const sarifPath = option("--sarif");
1157
1413
  const policyPath = option("--policy");
1158
- const positional = rest.filter(arg => !arg.startsWith("--") && arg !== sarifPath && arg !== policyPath);
1414
+ const baselinePath = option("--baseline");
1415
+ const generateBaselinePath = option("--generate-baseline");
1416
+ const positional = rest.filter((arg) => !arg.startsWith("--") &&
1417
+ arg !== sarifPath &&
1418
+ arg !== policyPath &&
1419
+ arg !== baselinePath &&
1420
+ arg !== generateBaselinePath);
1159
1421
  const target = path.resolve(positional[0] ?? ".");
1160
1422
  try {
1161
- const securityReport = await scanProjectSecurity(target);
1162
- const reviewReport = await reviewProject(target, { all: true });
1423
+ const evaluation = await evaluateGate(target, {
1424
+ policyPath,
1425
+ baselinePath,
1426
+ failOnWarnings,
1427
+ });
1428
+ if (generateBaselinePath) {
1429
+ const outResolved = path.resolve(generateBaselinePath);
1430
+ await mkdir(path.dirname(outResolved), { recursive: true });
1431
+ await writeFile(outResolved, generateBaselineJson(evaluation.activeFindings), "utf8");
1432
+ if (!isJson) {
1433
+ process.stdout.write(`Generated baseline with ${evaluation.activeFindings.length} findings to ${path.relative(process.cwd(), outResolved)}\n`);
1434
+ }
1435
+ }
1163
1436
  if (sarifPath) {
1164
- const sarifOutput = generateSarif({ securityReport, reviewReport, targetRoot: target });
1437
+ const sarifOutput = generateSarif({
1438
+ activeFindings: evaluation.activeFindings,
1439
+ suppressedFindings: evaluation.suppressedFindings,
1440
+ inlineSuppressions: evaluation.inlineSuppressions,
1441
+ filterSuppressed: true,
1442
+ targetRoot: target,
1443
+ });
1165
1444
  const outResolved = path.resolve(sarifPath);
1166
1445
  await mkdir(path.dirname(outResolved), { recursive: true });
1167
1446
  await writeFile(outResolved, sarifOutput, "utf8");
@@ -1169,42 +1448,58 @@ Exit codes:
1169
1448
  process.stdout.write(`Exported SARIF report to ${path.relative(process.cwd(), outResolved)}\n`);
1170
1449
  }
1171
1450
  }
1172
- const criticalCount = securityReport.summary.critical;
1173
- const highCount = securityReport.summary.high + reviewReport.summary.high;
1174
- const mediumCount = securityReport.summary.medium + reviewReport.summary.medium;
1175
- const passed = criticalCount === 0 && highCount === 0 && (!failOnWarnings || mediumCount === 0);
1176
1451
  if (isJson) {
1177
1452
  process.stdout.write(JSON.stringify({
1178
- gate: passed ? "PASSED" : "FAILED",
1453
+ gate: evaluation.passed ? "PASSED" : "FAILED",
1179
1454
  target,
1180
- summary: {
1181
- critical: criticalCount,
1182
- high: highCount,
1183
- medium: mediumCount,
1184
- low: securityReport.summary.low + reviewReport.summary.low
1185
- },
1186
- security: securityReport,
1187
- review: reviewReport
1455
+ policySource: evaluation.policySource,
1456
+ baselineSource: evaluation.baselineSource,
1457
+ violations: evaluation.violations,
1458
+ warnings: evaluation.warnings,
1459
+ summary: evaluation.summary,
1460
+ activeFindings: evaluation.activeFindings,
1461
+ suppressedFindings: evaluation.suppressedFindings,
1462
+ inlineSuppressions: evaluation.inlineSuppressions,
1188
1463
  }, null, 2) + "\n");
1189
1464
  }
1190
1465
  else {
1191
- process.stdout.write([
1466
+ const lines = [
1192
1467
  `# ANVIL CI Quality & Security Gate`,
1193
1468
  `Target: ${target}`,
1194
- `Status: ${passed ? "PASSED" : "FAILED"}`,
1195
- ``,
1196
- `Findings Summary:`,
1197
- ` Critical: ${criticalCount}`,
1198
- ` High: ${highCount}`,
1199
- ` Medium: ${mediumCount}`,
1200
- ` Low: ${securityReport.summary.low + reviewReport.summary.low}`,
1201
- ``,
1202
- passed
1203
- ? `✓ Gate passed: 0 critical/high findings detected.`
1204
- : `✗ Gate failed: ${criticalCount + highCount} critical or high findings require remediation before merging.`
1205
- ].join("\n") + "\n");
1469
+ `Status: ${evaluation.passed ? "PASSED" : "FAILED"}`,
1470
+ `Policy: ${evaluation.policySource}`,
1471
+ ];
1472
+ if (evaluation.baselineSource) {
1473
+ lines.push(`Baseline: ${evaluation.baselineSource} (${evaluation.summary.suppressedByBaseline} findings suppressed)`);
1474
+ }
1475
+ lines.push(``, `Findings Summary:`, ` Critical: ${evaluation.summary.critical}`, ` High: ${evaluation.summary.high}`, ` Medium: ${evaluation.summary.medium}`, ` Low: ${evaluation.summary.low}`);
1476
+ if (evaluation.summary.suppressedByBaseline > 0) {
1477
+ lines.push(` Suppressed by baseline: ${evaluation.summary.suppressedByBaseline}`);
1478
+ }
1479
+ if (evaluation.summary.inlineSuppressions > 0) {
1480
+ lines.push(` Inline suppressions: ${evaluation.summary.inlineSuppressions}`);
1481
+ }
1482
+ lines.push(``);
1483
+ if (evaluation.warnings && evaluation.warnings.length > 0) {
1484
+ lines.push(`Advisory Notices:`);
1485
+ for (const w of evaluation.warnings) {
1486
+ lines.push(` ⚠ ${w}`);
1487
+ }
1488
+ lines.push(``);
1489
+ }
1490
+ if (evaluation.violations.length > 0) {
1491
+ lines.push(`Policy Violations:`);
1492
+ for (const v of evaluation.violations) {
1493
+ lines.push(` ✗ ${v}`);
1494
+ }
1495
+ lines.push(``);
1496
+ }
1497
+ lines.push(evaluation.passed
1498
+ ? `✓ Gate passed: all policy thresholds satisfied.`
1499
+ : `✗ Gate failed: ${evaluation.violations.length} policy violations detected.`);
1500
+ process.stdout.write(lines.join("\n") + "\n");
1206
1501
  }
1207
- if (!passed) {
1502
+ if (!evaluation.passed) {
1208
1503
  process.exitCode = 1;
1209
1504
  }
1210
1505
  }
@@ -1218,8 +1513,13 @@ Exit codes:
1218
1513
  const goalIndex = rest.indexOf("--goal");
1219
1514
  const pathIndex = rest.indexOf("--path");
1220
1515
  const goalEnd = pathIndex >= 0 ? pathIndex : rest.length;
1221
- const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd).join(" ").trim() : "";
1222
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1516
+ const goal = goalIndex >= 0
1517
+ ? rest
1518
+ .slice(goalIndex + 1, goalEnd)
1519
+ .join(" ")
1520
+ .trim()
1521
+ : "";
1522
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1223
1523
  try {
1224
1524
  const plan = createReviewPlan(goal);
1225
1525
  const saved = await saveRun(target, plan);
@@ -1236,7 +1536,7 @@ Exit codes:
1236
1536
  case "replay": {
1237
1537
  const runId = rest[0] ?? "";
1238
1538
  const pathIndex = rest.indexOf("--path");
1239
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1539
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1240
1540
  try {
1241
1541
  if (!runId)
1242
1542
  throw new Error("Usage: anvil replay RUN_ID [--path PROJECT]");
@@ -1257,13 +1557,13 @@ Exit codes:
1257
1557
  const runId = rest[0] ?? "";
1258
1558
  const taskId = rest[1] ?? "";
1259
1559
  const pathIndex = rest.indexOf("--path");
1260
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1560
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1261
1561
  const reviewerIndex = rest.indexOf("--reviewer");
1262
1562
  const reasonIndex = rest.indexOf("--reason");
1263
1563
  try {
1264
1564
  if (!runId || !taskId)
1265
1565
  throw new Error("Usage: anvil approve RUN_ID TASK_ID [--reviewer NAME] [--reason TEXT] [--path PROJECT]");
1266
- const updated = await decideTaskApproval(target, runId, taskId, "approved", reviewerIndex >= 0 ? rest[reviewerIndex + 1] ?? "local-user" : "local-user", reasonIndex >= 0 ? rest.slice(reasonIndex + 1, pathIndex >= 0 ? pathIndex : rest.length).join(" ") : undefined);
1566
+ const updated = await decideTaskApproval(target, runId, taskId, "approved", reviewerIndex >= 0 ? (rest[reviewerIndex + 1] ?? "local-user") : "local-user", reasonIndex >= 0 ? rest.slice(reasonIndex + 1, pathIndex >= 0 ? pathIndex : rest.length).join(" ") : undefined);
1267
1567
  process.stdout.write("Approved " + taskId + " in " + runId + ". Resume explicitly with `forge resume " + runId + "`.\n");
1268
1568
  process.stdout.write("Run status: " + updated.status + "\n");
1269
1569
  }
@@ -1276,12 +1576,12 @@ Exit codes:
1276
1576
  case "resume": {
1277
1577
  const runId = rest[0] ?? "";
1278
1578
  const pathIndex = rest.indexOf("--path");
1279
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1579
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1280
1580
  try {
1281
1581
  if (!runId)
1282
1582
  throw new Error("Usage: anvil resume RUN_ID [--path PROJECT]");
1283
1583
  const permissions = await loadPermissionPolicy(target);
1284
- const providerChoice = (getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai")).toLowerCase();
1584
+ const providerChoice = getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai").toLowerCase();
1285
1585
  assertSupportedAgentPolicy(permissions, providerChoice, "ANVIL resume");
1286
1586
  await withRunExecutionLock(target, runId, async () => {
1287
1587
  const savedRun = await loadRun(target, runId);
@@ -1295,7 +1595,9 @@ Exit codes:
1295
1595
  throw new Error("Only pending or approval-cleared runs can be resumed. Use `anvil replay` only after reviewing failed tasks.");
1296
1596
  const model = getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ?? getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL");
1297
1597
  const baseModel = model ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1-mini");
1298
- const createProvider = (selectedModel) => providerChoice === "claude-code" ? new ClaudeCodeProvider({ cwd: target, model: selectedModel }) : createHostedAgentProvider({ ...process.env, ANVIL_AI_PROVIDER: providerChoice, FORGE_AI_PROVIDER: providerChoice }, selectedModel, { workspacePath: target, permissionPolicy: permissions });
1598
+ const createProvider = (selectedModel) => providerChoice === "claude-code"
1599
+ ? new ClaudeCodeProvider({ cwd: target, model: selectedModel })
1600
+ : createHostedAgentProvider({ ...process.env, ANVIL_AI_PROVIDER: providerChoice, FORGE_AI_PROVIDER: providerChoice }, selectedModel, { workspacePath: target, permissionPolicy: permissions });
1299
1601
  let priorTraceEntries = [];
1300
1602
  try {
1301
1603
  priorTraceEntries = (await loadExecutionTrace(target, runId)).entries;
@@ -1304,17 +1606,48 @@ Exit codes:
1304
1606
  if (!(error && typeof error === "object" && "code" in error && error.code === "ENOENT"))
1305
1607
  throw error;
1306
1608
  }
1307
- const traceAgentUsage = priorTraceEntries.reduce((usage, entry) => { usage[entry.agent] = (usage[entry.agent] ?? 0) + entry.inputTokensEstimate + entry.outputTokensEstimate; return usage; }, {});
1609
+ const traceAgentUsage = priorTraceEntries.reduce((usage, entry) => {
1610
+ usage[entry.agent] = (usage[entry.agent] ?? 0) + entry.inputTokensEstimate + entry.outputTokensEstimate;
1611
+ return usage;
1612
+ }, {});
1308
1613
  const traceRunUsage = priorTraceEntries.reduce((total, entry) => total + entry.inputTokensEstimate + entry.outputTokensEstimate, 0);
1309
1614
  const priorAgentUsage = savedRun.tokenUsage?.estimatedTokensByAgent ?? traceAgentUsage;
1310
1615
  const priorRunUsage = savedRun.tokenUsage?.estimatedTokensUsed ?? traceRunUsage;
1311
1616
  const resumeFallback = getAnvilEnv("ANVIL_FALLBACK_MODEL", "FORGE_FALLBACK_MODEL");
1312
1617
  const router = new ModelRouter({
1313
- fastModel: getAnvilEnv("ANVIL_MODEL_FAST", "FORGE_MODEL_FAST") ?? baseModel, balancedModel: getAnvilEnv("ANVIL_MODEL_BALANCED", "FORGE_MODEL_BALANCED") ?? baseModel, reasoningModel: getAnvilEnv("ANVIL_MODEL_REASONING", "FORGE_MODEL_REASONING") ?? baseModel, ...(resumeFallback ? { fallbackModel: resumeFallback } : {}), retryAttempts: Number(getAnvilEnv("ANVIL_MODEL_RETRY_ATTEMPTS", "FORGE_MODEL_RETRY_ATTEMPTS", "2")), retryDelayMs: Number(getAnvilEnv("ANVIL_MODEL_RETRY_DELAY_MS", "FORGE_MODEL_RETRY_DELAY_MS", "500")), ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK") ? { maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")) } : {}), ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK") ? { maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")) } : {}), ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN") ? { maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")) } : {}),
1314
- ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT") ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
1315
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS") ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) } : {}),
1316
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT") ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
1317
- initialEstimatedTokensUsed: priorRunUsage, initialEstimatedTokensByAgent: priorAgentUsage, createProvider
1618
+ fastModel: getAnvilEnv("ANVIL_MODEL_FAST", "FORGE_MODEL_FAST") ?? baseModel,
1619
+ balancedModel: getAnvilEnv("ANVIL_MODEL_BALANCED", "FORGE_MODEL_BALANCED") ?? baseModel,
1620
+ reasoningModel: getAnvilEnv("ANVIL_MODEL_REASONING", "FORGE_MODEL_REASONING") ?? baseModel,
1621
+ ...(resumeFallback ? { fallbackModel: resumeFallback } : {}),
1622
+ retryAttempts: Number(getAnvilEnv("ANVIL_MODEL_RETRY_ATTEMPTS", "FORGE_MODEL_RETRY_ATTEMPTS", "2")),
1623
+ retryDelayMs: Number(getAnvilEnv("ANVIL_MODEL_RETRY_DELAY_MS", "FORGE_MODEL_RETRY_DELAY_MS", "500")),
1624
+ ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")
1625
+ ? {
1626
+ maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")),
1627
+ }
1628
+ : {}),
1629
+ ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")
1630
+ ? {
1631
+ maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")),
1632
+ }
1633
+ : {}),
1634
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")
1635
+ ? {
1636
+ maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")),
1637
+ }
1638
+ : {}),
1639
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT")
1640
+ ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() }
1641
+ : {}),
1642
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")
1643
+ ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) }
1644
+ : {}),
1645
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT")
1646
+ ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() }
1647
+ : {}),
1648
+ initialEstimatedTokensUsed: priorRunUsage,
1649
+ initialEstimatedTokensByAgent: priorAgentUsage,
1650
+ createProvider,
1318
1651
  });
1319
1652
  const memory = await buildProjectMemoryContext(target, savedRun.goal);
1320
1653
  // Re-check current workspace policy before resuming any task that has not completed.
@@ -1324,10 +1657,12 @@ Exit codes:
1324
1657
  if (record.status !== "pending" && record.status !== "awaiting_approval")
1325
1658
  continue;
1326
1659
  const applied = applyBeforeAgentHooks(record.task, resumeHooks);
1327
- const additions = applied.context.filter(message => !record.task.prompt.includes(message));
1660
+ const additions = applied.context.filter((message) => !record.task.prompt.includes(message));
1328
1661
  record.task = {
1329
1662
  ...applied.task,
1330
- prompt: additions.length ? record.task.prompt + "\n\nHook guidance:\n" + additions.join("\n") : record.task.prompt,
1663
+ prompt: additions.length
1664
+ ? record.task.prompt + "\n\nHook guidance:\n" + additions.join("\n")
1665
+ : record.task.prompt,
1331
1666
  };
1332
1667
  if (applied.blockedReason) {
1333
1668
  record.status = "blocked";
@@ -1335,7 +1670,17 @@ Exit codes:
1335
1670
  }
1336
1671
  }
1337
1672
  const currentTokenUsage = () => {
1338
- const usage = router.traces.reduce((total, entry) => { total.estimatedTokensUsed += entry.inputTokensEstimate + entry.outputTokensEstimate; total.estimatedTokensByAgent[entry.agent] = (total.estimatedTokensByAgent[entry.agent] ?? 0) + entry.inputTokensEstimate + entry.outputTokensEstimate; return total; }, { estimatedTokensUsed: priorRunUsage, estimatedTokensByAgent: { ...priorAgentUsage } });
1673
+ const usage = router.traces.reduce((total, entry) => {
1674
+ total.estimatedTokensUsed += entry.inputTokensEstimate + entry.outputTokensEstimate;
1675
+ total.estimatedTokensByAgent[entry.agent] =
1676
+ (total.estimatedTokensByAgent[entry.agent] ?? 0) +
1677
+ entry.inputTokensEstimate +
1678
+ entry.outputTokensEstimate;
1679
+ return total;
1680
+ }, {
1681
+ estimatedTokensUsed: priorRunUsage,
1682
+ estimatedTokensByAgent: { ...priorAgentUsage },
1683
+ });
1339
1684
  return usage;
1340
1685
  };
1341
1686
  let persistedTraceEntries = 0;
@@ -1347,7 +1692,14 @@ Exit codes:
1347
1692
  persistedTraceEntries = router.traces.length;
1348
1693
  }
1349
1694
  };
1350
- const completed = await advanceRun(savedRun, router, new Set(), { maxConcurrency: 4, projectContext: memory.text, projectContextForTask: task => buildAgentWorkspaceContext(target, task), shouldCancel: async () => (await loadRun(target, runId)).status === "cancelled", onTaskSettled: persistRunSnapshot, onUpdate: persistRunSnapshot });
1695
+ const completed = await advanceRun(savedRun, router, new Set(), {
1696
+ maxConcurrency: 4,
1697
+ projectContext: memory.text,
1698
+ projectContextForTask: (task) => buildAgentWorkspaceContext(target, task),
1699
+ shouldCancel: async () => (await loadRun(target, runId)).status === "cancelled",
1700
+ onTaskSettled: persistRunSnapshot,
1701
+ onUpdate: persistRunSnapshot,
1702
+ });
1351
1703
  const saved = await saveRun(target, { ...completed, tokenUsage: currentTokenUsage() });
1352
1704
  const tracePath = await saveExecutionTrace(target, completed.id, router.traces.slice(persistedTraceEntries));
1353
1705
  process.stdout.write("Resumed " + runId + ": " + completed.status + "\n");
@@ -1363,7 +1715,7 @@ Exit codes:
1363
1715
  case "trace": {
1364
1716
  const runId = rest[0];
1365
1717
  const pathIndex = rest.indexOf("--path");
1366
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1718
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1367
1719
  if (!runId) {
1368
1720
  process.stderr.write("Usage: anvil trace RUNID [--path PROJECT]\n");
1369
1721
  process.exitCode = 1;
@@ -1373,11 +1725,49 @@ Exit codes:
1373
1725
  const trace = await loadExecutionTrace(target, runId);
1374
1726
  process.stdout.write("FORGE execution trace: " + runId + "\n");
1375
1727
  for (const entry of trace.entries)
1376
- process.stdout.write(" " + entry.taskId + " [" + entry.tier + "] " + entry.model + " | " + entry.durationMs + " ms | " + entry.outcome + " | ~" + entry.inputTokensEstimate + "/" + entry.outputTokensEstimate + " tokens\n");
1377
- process.stdout.write("Totals: " + trace.totals.tasks + " tasks, " + trace.totals.estimatedInputTokens + " input tokens, " + trace.totals.estimatedOutputTokens + " output tokens, " + trace.totals.fallbacks + " fallbacks, " + trace.totals.failures + " failures\n");
1728
+ process.stdout.write(" " +
1729
+ entry.taskId +
1730
+ " [" +
1731
+ entry.tier +
1732
+ "] " +
1733
+ entry.model +
1734
+ " | " +
1735
+ entry.durationMs +
1736
+ " ms | " +
1737
+ entry.outcome +
1738
+ " | ~" +
1739
+ entry.inputTokensEstimate +
1740
+ "/" +
1741
+ entry.outputTokensEstimate +
1742
+ " tokens\n");
1743
+ process.stdout.write("Totals: " +
1744
+ trace.totals.tasks +
1745
+ " tasks, " +
1746
+ trace.totals.estimatedInputTokens +
1747
+ " input tokens, " +
1748
+ trace.totals.estimatedOutputTokens +
1749
+ " output tokens, " +
1750
+ trace.totals.fallbacks +
1751
+ " fallbacks, " +
1752
+ trace.totals.failures +
1753
+ " failures\n");
1378
1754
  process.stdout.write("Provider health:\n");
1379
1755
  for (const provider of trace.providers ?? [])
1380
- process.stdout.write(" " + provider.name + ": " + provider.requests + " requests, " + provider.successes + " successes, " + provider.fallbacks + " fallbacks, " + provider.failures + " failures, " + provider.retries + " retries, " + provider.durationMs + " ms\n");
1756
+ process.stdout.write(" " +
1757
+ provider.name +
1758
+ ": " +
1759
+ provider.requests +
1760
+ " requests, " +
1761
+ provider.successes +
1762
+ " successes, " +
1763
+ provider.fallbacks +
1764
+ " fallbacks, " +
1765
+ provider.failures +
1766
+ " failures, " +
1767
+ provider.retries +
1768
+ " retries, " +
1769
+ provider.durationMs +
1770
+ " ms\n");
1381
1771
  }
1382
1772
  catch (error) {
1383
1773
  process.stderr.write("Could not read execution trace: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -1404,29 +1794,34 @@ Exit codes:
1404
1794
  throw new Error("Usage: anvil runs [path] [--json] [--limit N] [--status STATUS]");
1405
1795
  }
1406
1796
  for (const option of ["--json", "--limit", "--status"]) {
1407
- if (rest.filter(arg => arg === option).length > 1) {
1797
+ if (rest.filter((arg) => arg === option).length > 1) {
1408
1798
  throw new Error(`${option} may only be specified once.`);
1409
1799
  }
1410
1800
  }
1411
1801
  const allowedOptions = new Set(["--json", "--limit", "--status"]);
1412
- const unknownOption = rest.find(arg => arg.startsWith("--") && !allowedOptions.has(arg));
1802
+ const unknownOption = rest.find((arg) => arg.startsWith("--") && !allowedOptions.has(arg));
1413
1803
  if (unknownOption)
1414
1804
  throw new Error(`Unknown option: ${unknownOption}.`);
1415
1805
  if (limitIndex >= 0 && (!rawLimit || rawLimit.startsWith("--") || !Number.isSafeInteger(limit) || limit < 1)) {
1416
1806
  throw new Error("--limit requires a positive safe integer.");
1417
1807
  }
1418
1808
  const validStatuses = ["pending", "awaiting_approval", "running", "completed", "failed", "cancelled"];
1419
- if (statusIndex >= 0 && (!rawStatus || rawStatus.startsWith("--") || !validStatuses.includes(rawStatus))) {
1809
+ if (statusIndex >= 0 &&
1810
+ (!rawStatus ||
1811
+ rawStatus.startsWith("--") ||
1812
+ !validStatuses.includes(rawStatus))) {
1420
1813
  throw new Error(`--status requires one of: ${validStatuses.join(", ")}.`);
1421
1814
  }
1422
1815
  const allRuns = await listRuns(path.resolve(target));
1423
- const filteredRuns = rawStatus ? allRuns.filter(run => run.status === rawStatus) : allRuns;
1816
+ const filteredRuns = rawStatus ? allRuns.filter((run) => run.status === rawStatus) : allRuns;
1424
1817
  const runs = limit === undefined ? filteredRuns : filteredRuns.slice(0, limit);
1425
1818
  if (rest.includes("--json")) {
1426
1819
  process.stdout.write(formatRunHistoryJson(runs));
1427
1820
  }
1428
1821
  else {
1429
- process.stdout.write(runs.length ? runs.map(run => `${run.id} ${run.status.padEnd(18)} ${run.goal}`).join("\n") + "\n" : "No saved FORGE runs.\n");
1822
+ process.stdout.write(runs.length
1823
+ ? runs.map((run) => `${run.id} ${run.status.padEnd(18)} ${run.goal}`).join("\n") + "\n"
1824
+ : "No saved FORGE runs.\n");
1430
1825
  }
1431
1826
  }
1432
1827
  catch (error) {
@@ -1441,7 +1836,7 @@ Exit codes:
1441
1836
  process.stdout.write("Usage: anvil show-run RUN_ID [--path PROJECT]\nDisplays one validated saved run record.\n");
1442
1837
  return;
1443
1838
  }
1444
- const pathIndices = rest.flatMap((arg, index) => arg === "--path" ? [index] : []);
1839
+ const pathIndices = rest.flatMap((arg, index) => (arg === "--path" ? [index] : []));
1445
1840
  const pathIndex = pathIndices[0] ?? -1;
1446
1841
  const pathValue = pathIndex >= 0 ? rest[pathIndex + 1] : undefined;
1447
1842
  const positional = rest.filter((arg, index) => !arg.startsWith("--") && !(pathIndex >= 0 && index === pathIndex + 1));
@@ -1449,8 +1844,8 @@ Exit codes:
1449
1844
  try {
1450
1845
  if (pathIndices.length > 1)
1451
1846
  throw new Error("--path may only be specified once.");
1452
- if (rest.some(arg => arg.startsWith("--") && arg !== "--path")) {
1453
- throw new Error(`Unknown option: ${rest.find(arg => arg.startsWith("--") && arg !== "--path")}.`);
1847
+ if (rest.some((arg) => arg.startsWith("--") && arg !== "--path")) {
1848
+ throw new Error(`Unknown option: ${rest.find((arg) => arg.startsWith("--") && arg !== "--path")}.`);
1454
1849
  }
1455
1850
  if (pathIndex >= 0 && (!pathValue || pathValue.startsWith("--"))) {
1456
1851
  throw new Error("--path requires a project directory.");
@@ -1472,12 +1867,17 @@ Exit codes:
1472
1867
  const goalIndex = rest.indexOf("--goal");
1473
1868
  const pathIndex = rest.indexOf("--path");
1474
1869
  const goalEnd = pathIndex >= 0 ? pathIndex : rest.length;
1475
- const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd).join(" ").trim() : "";
1476
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1870
+ const goal = goalIndex >= 0
1871
+ ? rest
1872
+ .slice(goalIndex + 1, goalEnd)
1873
+ .join(" ")
1874
+ .trim()
1875
+ : "";
1876
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1477
1877
  try {
1478
1878
  const permissions = await loadPermissionPolicy(target);
1479
1879
  const model = getAnvilEnv("ANVIL_AI_MODEL", "FORGE_AI_MODEL") ?? getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL");
1480
- const providerChoice = (getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai")).toLowerCase();
1880
+ const providerChoice = getAnvilEnv("ANVIL_AI_PROVIDER", "FORGE_AI_PROVIDER", "openai").toLowerCase();
1481
1881
  assertSupportedAgentPolicy(permissions, providerChoice, "ANVIL run");
1482
1882
  const baseModel = model ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1-mini");
1483
1883
  const apiKey = getAnvilEnv("ANVIL_API_KEY", "FORGE_API_KEY") ?? process.env.OPENAI_API_KEY ?? "";
@@ -1499,12 +1899,30 @@ Exit codes:
1499
1899
  retryAttempts: Number(getAnvilEnv("ANVIL_MODEL_RETRY_ATTEMPTS", "FORGE_MODEL_RETRY_ATTEMPTS", "2")),
1500
1900
  retryDelayMs: Number(getAnvilEnv("ANVIL_MODEL_RETRY_DELAY_MS", "FORGE_MODEL_RETRY_DELAY_MS", "500")),
1501
1901
  createProvider,
1502
- ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK") ? { maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")) } : {}),
1503
- ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK") ? { maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")) } : {}),
1504
- ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN") ? { maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")) } : {}),
1505
- ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT") ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
1506
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS") ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) } : {}),
1507
- ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT") ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
1902
+ ...(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")
1903
+ ? {
1904
+ maxInputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_INPUT_TOKENS_PER_TASK", "FORGE_MAX_INPUT_TOKENS_PER_TASK")),
1905
+ }
1906
+ : {}),
1907
+ ...(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")
1908
+ ? {
1909
+ maxOutputTokensPerTask: Number(getAnvilEnv("ANVIL_MAX_OUTPUT_TOKENS_PER_TASK", "FORGE_MAX_OUTPUT_TOKENS_PER_TASK")),
1910
+ }
1911
+ : {}),
1912
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")
1913
+ ? {
1914
+ maxEstimatedTokensPerRun: Number(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_RUN", "FORGE_MAX_ESTIMATED_TOKENS_PER_RUN")),
1915
+ }
1916
+ : {}),
1917
+ ...(getAnvilEnv("ANVIL_MAX_ESTIMATED_TOKENS_PER_AGENT", "FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT")
1918
+ ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() }
1919
+ : {}),
1920
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")
1921
+ ? { maxTaskDurationMs: Number(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS", "FORGE_MAX_TASK_DURATION_MS")) }
1922
+ : {}),
1923
+ ...(getAnvilEnv("ANVIL_MAX_TASK_DURATION_MS_BY_AGENT", "FORGE_MAX_TASK_DURATION_MS_BY_AGENT")
1924
+ ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() }
1925
+ : {}),
1508
1926
  });
1509
1927
  const provider = router;
1510
1928
  const plan = createReviewPlan(goal);
@@ -1513,7 +1931,12 @@ Exit codes:
1513
1931
  const hooks = await loadHooks(target);
1514
1932
  for (const record of plan.tasks) {
1515
1933
  const applied = applyBeforeAgentHooks(record.task, hooks);
1516
- record.task = { ...applied.task, prompt: applied.context.length ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n") : applied.task.prompt };
1934
+ record.task = {
1935
+ ...applied.task,
1936
+ prompt: applied.context.length
1937
+ ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n")
1938
+ : applied.task.prompt,
1939
+ };
1517
1940
  if (applied.blockedReason) {
1518
1941
  record.status = "blocked";
1519
1942
  record.error = "Blocked by policy hook: " + applied.blockedReason;
@@ -1521,16 +1944,25 @@ Exit codes:
1521
1944
  }
1522
1945
  process.stdout.write("Starting " + plan.id + " with provider " + provider.name + (model ? " (model " + model + ")" : "") + "\n");
1523
1946
  if (skills.length)
1524
- process.stdout.write("Selected skills: " + skills.map(skill => skill.id).join(", ") + "\n");
1947
+ process.stdout.write("Selected skills: " + skills.map((skill) => skill.id).join(", ") + "\n");
1525
1948
  if (hooks.length)
1526
1949
  process.stdout.write("Loaded policy hooks: " + hooks.length + "\n");
1527
1950
  const memory = await buildProjectMemoryContext(target, goal);
1528
1951
  const projectContext = [memory.text, skillContext].filter(Boolean).join("\n\n");
1529
- const completed = await advanceRun(plan, provider, new Set(), { maxConcurrency: 4, projectContext, projectContextForTask: task => buildAgentWorkspaceContext(target, task) });
1952
+ const completed = await advanceRun(plan, provider, new Set(), {
1953
+ maxConcurrency: 4,
1954
+ projectContext,
1955
+ projectContextForTask: (task) => buildAgentWorkspaceContext(target, task),
1956
+ });
1530
1957
  const saved = await saveRun(target, completed);
1531
1958
  const tracePath = await saveExecutionTrace(target, completed.id, router.traces);
1532
1959
  process.stdout.write(`Run status: ${completed.status}\nSaved history: ${path.relative(target, saved)}\nExecution trace: ${path.relative(target, tracePath)}\n`);
1533
- const totals = router.traces.reduce((sum, trace) => ({ input: sum.input + trace.inputTokensEstimate, output: sum.output + trace.outputTokensEstimate, duration: sum.duration + trace.durationMs, fallback: sum.fallback + (trace.outcome === "fallback" ? 1 : 0) }), { input: 0, output: 0, duration: 0, fallback: 0 });
1960
+ const totals = router.traces.reduce((sum, trace) => ({
1961
+ input: sum.input + trace.inputTokensEstimate,
1962
+ output: sum.output + trace.outputTokensEstimate,
1963
+ duration: sum.duration + trace.durationMs,
1964
+ fallback: sum.fallback + (trace.outcome === "fallback" ? 1 : 0),
1965
+ }), { input: 0, output: 0, duration: 0, fallback: 0 });
1534
1966
  process.stdout.write(`Estimated tokens: ${totals.input} in / ${totals.output} out | Agent time: ${totals.duration} ms | Fallbacks: ${totals.fallback}\n`);
1535
1967
  for (const task of completed.tasks)
1536
1968
  process.stdout.write(` ${task.status.padEnd(18)} ${task.task.agent}: ${task.task.title}\n`);
@@ -1549,11 +1981,17 @@ Exit codes:
1549
1981
  process.stdout.write("Usage: anvil deps [path] [--audit-report FILE] [--outdated-report FILE] [--json]\n");
1550
1982
  return;
1551
1983
  }
1552
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
1984
+ const option = (name) => {
1985
+ const index = rest.indexOf(name);
1986
+ return index >= 0 ? rest[index + 1] : undefined;
1987
+ };
1553
1988
  const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--audit-report" && rest[index - 1] !== "--outdated-report") ?? ".");
1554
1989
  try {
1555
1990
  const auditReport = option("--audit-report"), outdatedReport = option("--outdated-report");
1556
- const report = await analyzeDependencies(target, { ...(auditReport ? { auditReport } : {}), ...(outdatedReport ? { outdatedReport } : {}) });
1991
+ const report = await analyzeDependencies(target, {
1992
+ ...(auditReport ? { auditReport } : {}),
1993
+ ...(outdatedReport ? { outdatedReport } : {}),
1994
+ });
1557
1995
  process.stdout.write(rest.includes("--json") ? JSON.stringify(report, null, 2) + "\n" : formatDependencyIntelligence(report) + "\n");
1558
1996
  }
1559
1997
  catch (error) {
@@ -1563,7 +2001,7 @@ Exit codes:
1563
2001
  return;
1564
2002
  }
1565
2003
  case "snapshot": {
1566
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
2004
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
1567
2005
  try {
1568
2006
  const comparison = await captureIntelligenceSnapshot(target);
1569
2007
  process.stdout.write(formatIntelligenceComparison(comparison) + "\n");
@@ -1575,10 +2013,12 @@ Exit codes:
1575
2013
  return;
1576
2014
  }
1577
2015
  case "timeline": {
1578
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
2016
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
1579
2017
  try {
1580
2018
  const comparison = await compareLatestIntelligence(target);
1581
- process.stdout.write(comparison ? formatIntelligenceComparison(comparison) + "\n" : "No engineering snapshots found. Run forge snapshot . first.\n");
2019
+ process.stdout.write(comparison
2020
+ ? formatIntelligenceComparison(comparison) + "\n"
2021
+ : "No engineering snapshots found. Run forge snapshot . first.\n");
1582
2022
  }
1583
2023
  catch (error) {
1584
2024
  process.stderr.write("FORGE timeline failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -1588,8 +2028,10 @@ Exit codes:
1588
2028
  }
1589
2029
  case "change-risk": {
1590
2030
  const pathIndex = rest.indexOf("--path"), baseIndex = rest.indexOf("--base");
1591
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
1592
- const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
2031
+ const target = path.resolve(pathIndex >= 0
2032
+ ? (rest[pathIndex + 1] ?? ".")
2033
+ : (rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? "."));
2034
+ const base = baseIndex >= 0 ? (rest[baseIndex + 1] ?? "HEAD") : "HEAD";
1593
2035
  const json = rest.includes("--json");
1594
2036
  try {
1595
2037
  const report = await analyzeChangeIntelligence(target, base);
@@ -1603,7 +2045,7 @@ Exit codes:
1603
2045
  }
1604
2046
  case "risk": {
1605
2047
  const pathIndex = rest.indexOf("--path");
1606
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find(arg => !arg.startsWith("--")) ?? ".");
2048
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : (rest.find((arg) => !arg.startsWith("--")) ?? "."));
1607
2049
  const json = rest.includes("--json");
1608
2050
  try {
1609
2051
  const report = await analyzeEngineeringRisk(target);
@@ -1617,8 +2059,10 @@ Exit codes:
1617
2059
  }
1618
2060
  case "impact": {
1619
2061
  const pathIndex = rest.indexOf("--path"), baseIndex = rest.indexOf("--base");
1620
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
1621
- const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
2062
+ const target = path.resolve(pathIndex >= 0
2063
+ ? (rest[pathIndex + 1] ?? ".")
2064
+ : (rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? "."));
2065
+ const base = baseIndex >= 0 ? (rest[baseIndex + 1] ?? "HEAD") : "HEAD";
1622
2066
  const json = rest.includes("--json");
1623
2067
  try {
1624
2068
  const report = await analyzeDependencyImpact(target, base);
@@ -1637,8 +2081,11 @@ Exit codes:
1637
2081
  optionValues.add(pathIndex + 1);
1638
2082
  if (limitIndex >= 0)
1639
2083
  optionValues.add(limitIndex + 1);
1640
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1641
- const query = rest.filter((arg, index) => !arg.startsWith("--") && !optionValues.has(index)).join(" ").trim();
2084
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2085
+ const query = rest
2086
+ .filter((arg, index) => !arg.startsWith("--") && !optionValues.has(index))
2087
+ .join(" ")
2088
+ .trim();
1642
2089
  const limit = limitIndex >= 0 ? Number(rest[limitIndex + 1]) : 80;
1643
2090
  try {
1644
2091
  const report = await searchWorkspace(target, query, limit);
@@ -1651,7 +2098,7 @@ Exit codes:
1651
2098
  return;
1652
2099
  }
1653
2100
  case "dashboard": {
1654
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
2101
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
1655
2102
  const portIndex = rest.indexOf("--port");
1656
2103
  const portValue = portIndex >= 0 ? Number(rest[portIndex + 1]) : 4173;
1657
2104
  if (!Number.isInteger(portValue) || portValue < 0 || portValue > 65535) {
@@ -1670,7 +2117,11 @@ Exit codes:
1670
2117
  break;
1671
2118
  }
1672
2119
  catch (err) {
1673
- if (err && typeof err === "object" && "code" in err && err.code === "EADDRINUSE" && p < portValue + 9)
2120
+ if (err &&
2121
+ typeof err === "object" &&
2122
+ "code" in err &&
2123
+ err.code === "EADDRINUSE" &&
2124
+ p < portValue + 9)
1674
2125
  continue;
1675
2126
  throw err;
1676
2127
  }
@@ -1698,12 +2149,14 @@ Exit codes:
1698
2149
  case "verify": {
1699
2150
  const listOnly = rest.includes("--list");
1700
2151
  const pathIndex = rest.indexOf("--path");
1701
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
1702
- const script = rest.find(arg => !arg.startsWith("--"));
2152
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2153
+ const script = rest.find((arg) => !arg.startsWith("--"));
1703
2154
  try {
1704
2155
  if (listOnly) {
1705
2156
  const checks = await discoverChecks(target);
1706
- process.stdout.write(checks.length ? checks.map(check => `${check.name.padEnd(20)} ${check.command}`).join("\n") + "\n" : "No selectable npm scripts found.\n");
2157
+ process.stdout.write(checks.length
2158
+ ? checks.map((check) => `${check.name.padEnd(20)} ${check.command}`).join("\n") + "\n"
2159
+ : "No selectable npm scripts found.\n");
1707
2160
  return;
1708
2161
  }
1709
2162
  if (!script)
@@ -1730,12 +2183,22 @@ Exit codes:
1730
2183
  for (const profile of profiles) {
1731
2184
  process.stdout.write([
1732
2185
  profile.name + " — " + profile.description,
1733
- " network=" + profile.network + ", readOnlyRoot=" + profile.readOnlyRoot +
1734
- ", dropAllCapabilities=" + profile.dropAllCapabilities +
1735
- ", noNewPrivileges=" + profile.noNewPrivileges,
1736
- " memory=" + profile.maxMemory + ", cpus=" + profile.maxCpus +
1737
- ", processes=" + profile.maxProcesses +
1738
- ", requireRootless=" + profile.requireRootless,
2186
+ " network=" +
2187
+ profile.network +
2188
+ ", readOnlyRoot=" +
2189
+ profile.readOnlyRoot +
2190
+ ", dropAllCapabilities=" +
2191
+ profile.dropAllCapabilities +
2192
+ ", noNewPrivileges=" +
2193
+ profile.noNewPrivileges,
2194
+ " memory=" +
2195
+ profile.maxMemory +
2196
+ ", cpus=" +
2197
+ profile.maxCpus +
2198
+ ", processes=" +
2199
+ profile.maxProcesses +
2200
+ ", requireRootless=" +
2201
+ profile.requireRootless,
1739
2202
  "",
1740
2203
  ].join("\n"));
1741
2204
  }
@@ -1759,13 +2222,17 @@ Exit codes:
1759
2222
  }
1760
2223
  case "sandbox-cleanup": {
1761
2224
  const apply = rest.includes("--apply");
1762
- const unknown = rest.filter(arg => arg !== "--apply");
2225
+ const unknown = rest.filter((arg) => arg !== "--apply");
1763
2226
  try {
1764
2227
  if (unknown.length)
1765
2228
  throw new Error("Usage: anvil sandbox-cleanup [--apply]. Cleanup is dry-run unless --apply is explicit.");
1766
2229
  const output = await runSandboxProbe([
1767
- "ps", "-a", "--filter", "label=com.forge.managed=true",
1768
- "--format", "{{.ID}}\\t{{.Status}}\\t{{.Names}}",
2230
+ "ps",
2231
+ "-a",
2232
+ "--filter",
2233
+ "label=com.forge.managed=true",
2234
+ "--format",
2235
+ "{{.ID}}\\t{{.Status}}\\t{{.Names}}",
1769
2236
  ]);
1770
2237
  const managed = parseManagedContainers(output);
1771
2238
  const candidates = selectStoppedManagedContainers(managed);
@@ -1858,7 +2325,7 @@ Exit codes:
1858
2325
  }
1859
2326
  case "patch-verify": {
1860
2327
  const pathIndex = rest.indexOf("--path");
1861
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2328
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1862
2329
  const installDependencies = rest.includes("--install-dependencies");
1863
2330
  const sandboxImageIndex = rest.indexOf("--sandbox-image");
1864
2331
  const sandboxImage = sandboxImageIndex >= 0 ? rest[sandboxImageIndex + 1] : undefined;
@@ -1867,8 +2334,17 @@ Exit codes:
1867
2334
  const securityProfile = profileIndex >= 0 ? rest[profileIndex + 1] : undefined;
1868
2335
  const policyIndex = rest.indexOf("--policy");
1869
2336
  const sandboxPolicyPath = policyIndex >= 0 ? rest[policyIndex + 1] : undefined;
1870
- const argsWithoutPath = rest.filter((arg, index) => arg !== "--path" && !(pathIndex >= 0 && index === pathIndex + 1) && arg !== "--install-dependencies" && arg !== "--sandbox-image" && !(sandboxImageIndex >= 0 && index === sandboxImageIndex + 1) && arg !== "--require-rootless" && arg !== "--profile" && !(profileIndex >= 0 && index === profileIndex + 1) && arg !== "--policy" && !(policyIndex >= 0 && index === policyIndex + 1));
1871
- const stageId = argsWithoutPath.find(arg => !arg.startsWith("--"));
2337
+ const argsWithoutPath = rest.filter((arg, index) => arg !== "--path" &&
2338
+ !(pathIndex >= 0 && index === pathIndex + 1) &&
2339
+ arg !== "--install-dependencies" &&
2340
+ arg !== "--sandbox-image" &&
2341
+ !(sandboxImageIndex >= 0 && index === sandboxImageIndex + 1) &&
2342
+ arg !== "--require-rootless" &&
2343
+ arg !== "--profile" &&
2344
+ !(profileIndex >= 0 && index === profileIndex + 1) &&
2345
+ arg !== "--policy" &&
2346
+ !(policyIndex >= 0 && index === policyIndex + 1));
2347
+ const stageId = argsWithoutPath.find((arg) => !arg.startsWith("--"));
1872
2348
  const script = argsWithoutPath.find((arg, index) => !arg.startsWith("--") && index > 0);
1873
2349
  try {
1874
2350
  if (!stageId || !script)
@@ -1891,21 +2367,51 @@ Exit codes:
1891
2367
  installDependencies,
1892
2368
  ...(sandboxImage ? { sandboxImage } : {}),
1893
2369
  ...(requireRootless ? { requireRootless } : {}),
1894
- ...(securityProfile ? { securityProfile: securityProfile } : {}),
2370
+ ...(securityProfile
2371
+ ? { securityProfile: securityProfile }
2372
+ : {}),
1895
2373
  ...(sandboxPolicyPath ? { sandboxPolicyPath } : {}),
1896
2374
  });
1897
2375
  process.stdout.write("# ANVIL Staged Patch Verification\n\n" +
1898
- "- **Stage:** `" + result.stageId + "`\n" +
1899
- "- **Patch file:** `" + result.file + "`\n" +
1900
- "- **Staged SHA-256:** `" + result.stagedSha256 + "`\n" +
1901
- "- **Snapshot files:** `" + result.snapshotFileCount + "`\n" +
1902
- "- **Snapshot bytes:** `" + result.snapshotBytes + "`\n" +
1903
- (result.dependencyInstall ? "- **Dependencies:** **" + result.dependencyInstall.status.toUpperCase() + "** (`" + result.dependencyInstall.command + "`)\n" : "") +
1904
- (sandboxImage ? "- **Execution:** **CONTAINER** (`" + sandboxImage + "`)\n" : "- **Execution:** **LOCAL HOST**\n") +
1905
- "- **Verification:** **" + result.verification.status.toUpperCase() + "**\n" +
1906
- "- **Duration:** `" + result.verification.durationMs + " ms`\n\n" +
1907
- "## Output\n\n```\n" + (result.verification.output || "(No output captured.)") + "\n```\n\n" +
1908
- "Temporary workspace was removed after verification. " + (sandboxImage ? "The container runtime and host kernel remain trusted." : "Project scripts ran on the local host; this is not an OS-level sandbox.") + "\n");
2376
+ "- **Stage:** `" +
2377
+ result.stageId +
2378
+ "`\n" +
2379
+ "- **Patch file:** `" +
2380
+ result.file +
2381
+ "`\n" +
2382
+ "- **Staged SHA-256:** `" +
2383
+ result.stagedSha256 +
2384
+ "`\n" +
2385
+ "- **Snapshot files:** `" +
2386
+ result.snapshotFileCount +
2387
+ "`\n" +
2388
+ "- **Snapshot bytes:** `" +
2389
+ result.snapshotBytes +
2390
+ "`\n" +
2391
+ (result.dependencyInstall
2392
+ ? "- **Dependencies:** **" +
2393
+ result.dependencyInstall.status.toUpperCase() +
2394
+ "** (`" +
2395
+ result.dependencyInstall.command +
2396
+ "`)\n"
2397
+ : "") +
2398
+ (sandboxImage
2399
+ ? "- **Execution:** **CONTAINER** (`" + sandboxImage + "`)\n"
2400
+ : "- **Execution:** **LOCAL HOST**\n") +
2401
+ "- **Verification:** **" +
2402
+ result.verification.status.toUpperCase() +
2403
+ "**\n" +
2404
+ "- **Duration:** `" +
2405
+ result.verification.durationMs +
2406
+ " ms`\n\n" +
2407
+ "## Output\n\n```\n" +
2408
+ (result.verification.output || "(No output captured.)") +
2409
+ "\n```\n\n" +
2410
+ "Temporary workspace was removed after verification. " +
2411
+ (sandboxImage
2412
+ ? "The container runtime and host kernel remain trusted."
2413
+ : "Project scripts ran on the local host; this is not an OS-level sandbox.") +
2414
+ "\n");
1909
2415
  if (result.verification.status !== "passed")
1910
2416
  process.exitCode = 1;
1911
2417
  }
@@ -1918,14 +2424,16 @@ Exit codes:
1918
2424
  }
1919
2425
  case "verify-history": {
1920
2426
  const targetIndex = rest.indexOf("--path");
1921
- const target = path.resolve(targetIndex >= 0 ? rest[targetIndex + 1] ?? "." : ".");
2427
+ const target = path.resolve(targetIndex >= 0 ? (rest[targetIndex + 1] ?? ".") : ".");
1922
2428
  try {
1923
2429
  const history = await listVerifications(target);
1924
2430
  if (!history.length) {
1925
2431
  process.stdout.write("No saved verification runs.\n");
1926
2432
  return;
1927
2433
  }
1928
- process.stdout.write(history.map(item => `${item.startedAt} ${item.status.padEnd(12)} ${item.script.padEnd(16)} ${item.durationMs} ms ${item.id}`).join("\n") + "\n");
2434
+ process.stdout.write(history
2435
+ .map((item) => `${item.startedAt} ${item.status.padEnd(12)} ${item.script.padEnd(16)} ${item.durationMs} ms ${item.id}`)
2436
+ .join("\n") + "\n");
1929
2437
  }
1930
2438
  catch (error) {
1931
2439
  const message = error instanceof Error ? error.message : String(error);
@@ -1939,8 +2447,10 @@ Exit codes:
1939
2447
  const all = rest.includes("--all");
1940
2448
  const pathIndex = rest.indexOf("--path");
1941
2449
  const baseIndex = rest.indexOf("--base");
1942
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base" && rest[index - 1] !== "--path") ?? ".");
1943
- const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
2450
+ const target = path.resolve(pathIndex >= 0
2451
+ ? (rest[pathIndex + 1] ?? ".")
2452
+ : (rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base" && rest[index - 1] !== "--path") ?? "."));
2453
+ const base = baseIndex >= 0 ? (rest[baseIndex + 1] ?? "HEAD") : "HEAD";
1944
2454
  try {
1945
2455
  const report = await reviewProject(target, { base, all });
1946
2456
  let ai;
@@ -1948,11 +2458,21 @@ Exit codes:
1948
2458
  const apiKey = getAnvilEnv("ANVIL_API_KEY", "FORGE_API_KEY") ?? process.env.OPENAI_API_KEY ?? "";
1949
2459
  if (!apiKey)
1950
2460
  throw new Error("Set ANVIL_API_KEY, FORGE_API_KEY, or OPENAI_API_KEY before using --ai.");
1951
- const provider = new OpenAICompatibleProvider({ apiKey, model: getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL", "gpt-4.1-mini"), baseUrl: getAnvilEnv("ANVIL_BASE_URL", "FORGE_BASE_URL", "https://api.openai.com/v1") });
2461
+ const provider = new OpenAICompatibleProvider({
2462
+ apiKey,
2463
+ model: getAnvilEnv("ANVIL_MODEL", "FORGE_MODEL", "gpt-4.1-mini"),
2464
+ baseUrl: getAnvilEnv("ANVIL_BASE_URL", "FORGE_BASE_URL", "https://api.openai.com/v1"),
2465
+ });
1952
2466
  ai = await generateAIReview(report, provider);
1953
2467
  }
1954
2468
  const saved = await saveReview(target, report, ai);
1955
- process.stdout.write(json ? JSON.stringify({ ...saved, reviewId: saved.id }, null, 2) + "\n" : formatCodeReview(report) + (ai ? "\n\n" + formatAIReview(ai) : "") + "\n\nSaved review: " + saved.id + "\n");
2469
+ process.stdout.write(json
2470
+ ? JSON.stringify({ ...saved, reviewId: saved.id }, null, 2) + "\n"
2471
+ : formatCodeReview(report) +
2472
+ (ai ? "\n\n" + formatAIReview(ai) : "") +
2473
+ "\n\nSaved review: " +
2474
+ saved.id +
2475
+ "\n");
1956
2476
  }
1957
2477
  catch (error) {
1958
2478
  const message = error instanceof Error ? error.message : String(error);
@@ -1965,7 +2485,11 @@ Exit codes:
1965
2485
  const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? ".");
1966
2486
  try {
1967
2487
  const records = await listReviews(target);
1968
- process.stdout.write(records.length ? records.map(r => r.id + " " + r.createdAt + " " + r.report.findings.length + " static findings").join("\n") + "\n" : "No saved reviews found.\n");
2488
+ process.stdout.write(records.length
2489
+ ? records
2490
+ .map((r) => r.id + " " + r.createdAt + " " + r.report.findings.length + " static findings")
2491
+ .join("\n") + "\n"
2492
+ : "No saved reviews found.\n");
1969
2493
  }
1970
2494
  catch (error) {
1971
2495
  process.stderr.write("ANVIL review history failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -1974,9 +2498,9 @@ Exit codes:
1974
2498
  return;
1975
2499
  }
1976
2500
  case "review-show": {
1977
- const id = rest.find(arg => !arg.startsWith("--")) ?? "";
2501
+ const id = rest.find((arg) => !arg.startsWith("--")) ?? "";
1978
2502
  const pathIndex = rest.indexOf("--path");
1979
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2503
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1980
2504
  try {
1981
2505
  const record = await loadReview(target, id);
1982
2506
  process.stdout.write(formatReviewRecord(record) + "\n");
@@ -1990,13 +2514,21 @@ Exit codes:
1990
2514
  case "review-compare": {
1991
2515
  const ids = rest.filter((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path");
1992
2516
  const pathIndex = rest.indexOf("--path");
1993
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2517
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
1994
2518
  try {
1995
2519
  if (ids.length < 2)
1996
2520
  throw new Error("Usage: anvil review-compare REVIEW_ID_A REVIEW_ID_B --path PROJECT");
1997
2521
  const [a, b] = await Promise.all([loadReview(target, ids[0]), loadReview(target, ids[1])]);
1998
2522
  const diff = compareReviews(a, b);
1999
- process.stdout.write(["Review comparison", "Added (" + diff.added.length + ")", ...diff.added.map(x => "+ " + x), "Resolved (" + diff.resolved.length + ")", ...diff.resolved.map(x => "- " + x), "Persistent (" + diff.persistent.length + ")", ...diff.persistent.map(x => "= " + x)].join("\n") + "\n");
2523
+ process.stdout.write([
2524
+ "Review comparison",
2525
+ "Added (" + diff.added.length + ")",
2526
+ ...diff.added.map((x) => "+ " + x),
2527
+ "Resolved (" + diff.resolved.length + ")",
2528
+ ...diff.resolved.map((x) => "- " + x),
2529
+ "Persistent (" + diff.persistent.length + ")",
2530
+ ...diff.persistent.map((x) => "= " + x),
2531
+ ].join("\n") + "\n");
2000
2532
  }
2001
2533
  catch (error) {
2002
2534
  process.stderr.write("ANVIL review comparison failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -2007,8 +2539,10 @@ Exit codes:
2007
2539
  case "regression": {
2008
2540
  const pathIndex = rest.indexOf("--path");
2009
2541
  const baseIndex = rest.indexOf("--base");
2010
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
2011
- const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
2542
+ const target = path.resolve(pathIndex >= 0
2543
+ ? (rest[pathIndex + 1] ?? ".")
2544
+ : (rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? "."));
2545
+ const base = baseIndex >= 0 ? (rest[baseIndex + 1] ?? "HEAD") : "HEAD";
2012
2546
  const json = rest.includes("--json");
2013
2547
  try {
2014
2548
  const report = await generateRegressionReport(target, base);
@@ -2024,13 +2558,22 @@ Exit codes:
2024
2558
  }
2025
2559
  case "patch-review": {
2026
2560
  const pathIndex = rest.indexOf("--path");
2027
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2561
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2028
2562
  const stageId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
2029
2563
  try {
2030
2564
  if (!stageId)
2031
2565
  throw new Error("Usage: anvil patch-review STAGE_ID --path PROJECT");
2032
2566
  const review = await reviewStagedPatch(target, stageId);
2033
- process.stdout.write(["# ANVIL Staged Patch Review", "- Stage: " + review.stageId, "- File: " + review.file, "- Source SHA-256: " + review.sourceSha256, "- Staged SHA-256: " + review.stagedSha256, "- Source unchanged: " + review.unchanged, "", review.diff].join("\n") + "\n");
2567
+ process.stdout.write([
2568
+ "# ANVIL Staged Patch Review",
2569
+ "- Stage: " + review.stageId,
2570
+ "- File: " + review.file,
2571
+ "- Source SHA-256: " + review.sourceSha256,
2572
+ "- Staged SHA-256: " + review.stagedSha256,
2573
+ "- Source unchanged: " + review.unchanged,
2574
+ "",
2575
+ review.diff,
2576
+ ].join("\n") + "\n");
2034
2577
  }
2035
2578
  catch (error) {
2036
2579
  process.stderr.write("ANVIL staged patch review failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -2040,13 +2583,23 @@ Exit codes:
2040
2583
  }
2041
2584
  case "patch-stage": {
2042
2585
  const pathIndex = rest.indexOf("--path");
2043
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2586
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2044
2587
  const approvalId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
2045
2588
  try {
2046
2589
  if (!approvalId)
2047
2590
  throw new Error("Usage: anvil patch-stage APPROVAL_ID --path PROJECT");
2048
2591
  const staged = await stageApprovedPatch(target, approvalId);
2049
- process.stdout.write(["# ANVIL Isolated Patch Stage", "- Stage: " + staged.id, "- Approval: " + staged.approvalId, "- File: " + staged.file, "- Staged copy: " + staged.stagedFile, "- SHA-256: " + staged.stagedSha256, "", "The original project file was not modified.", "No tests or commands were executed."].join("\n") + "\n");
2592
+ process.stdout.write([
2593
+ "# ANVIL Isolated Patch Stage",
2594
+ "- Stage: " + staged.id,
2595
+ "- Approval: " + staged.approvalId,
2596
+ "- File: " + staged.file,
2597
+ "- Staged copy: " + staged.stagedFile,
2598
+ "- SHA-256: " + staged.stagedSha256,
2599
+ "",
2600
+ "The original project file was not modified.",
2601
+ "No tests or commands were executed.",
2602
+ ].join("\n") + "\n");
2050
2603
  }
2051
2604
  catch (error) {
2052
2605
  process.stderr.write("ANVIL patch staging failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -2058,18 +2611,31 @@ Exit codes:
2058
2611
  const pathIndex = rest.indexOf("--path");
2059
2612
  const reviewerIndex = rest.indexOf("--reviewer");
2060
2613
  const reasonIndex = rest.indexOf("--reason");
2061
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2614
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2062
2615
  const values = rest.filter((arg, index) => !arg.startsWith("--") && index !== pathIndex + 1 && index !== reviewerIndex + 1 && index !== reasonIndex + 1);
2063
2616
  const reviewId = values[0] ?? "";
2064
2617
  const suggestionNumber = Number(values[1]);
2065
- const reviewer = reviewerIndex >= 0 ? rest[reviewerIndex + 1] ?? "" : "";
2618
+ const reviewer = reviewerIndex >= 0 ? (rest[reviewerIndex + 1] ?? "") : "";
2066
2619
  const reason = reasonIndex >= 0 ? rest[reasonIndex + 1] : undefined;
2067
2620
  try {
2068
2621
  if (!reviewId || !Number.isInteger(suggestionNumber) || suggestionNumber < 1 || !reviewer.trim()) {
2069
2622
  throw new Error("Usage: anvil patch-approve REVIEW_ID SUGGESTION_NUMBER --reviewer NAME [--reason TEXT] --path PROJECT");
2070
2623
  }
2071
2624
  const approval = await approveReviewPatch(target, reviewId, suggestionNumber, reviewer, reason);
2072
- process.stdout.write(["# ANVIL Patch Approval", "- Approval ID: " + approval.id, "- Review: " + approval.reviewId, "- Suggestion: " + approval.suggestionNumber, "- File: " + approval.file, "- Reviewer: " + approval.reviewer, "- Approved: " + approval.approvedAt, "- Expires: " + approval.expiresAt, "- Patch SHA-256: " + approval.patchSha256, "- Source SHA-256: " + approval.sourceSha256, "", "No source files were modified."].join("\n") + "\n");
2625
+ process.stdout.write([
2626
+ "# ANVIL Patch Approval",
2627
+ "- Approval ID: " + approval.id,
2628
+ "- Review: " + approval.reviewId,
2629
+ "- Suggestion: " + approval.suggestionNumber,
2630
+ "- File: " + approval.file,
2631
+ "- Reviewer: " + approval.reviewer,
2632
+ "- Approved: " + approval.approvedAt,
2633
+ "- Expires: " + approval.expiresAt,
2634
+ "- Patch SHA-256: " + approval.patchSha256,
2635
+ "- Source SHA-256: " + approval.sourceSha256,
2636
+ "",
2637
+ "No source files were modified.",
2638
+ ].join("\n") + "\n");
2073
2639
  }
2074
2640
  catch (error) {
2075
2641
  process.stderr.write("ANVIL patch approval failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
@@ -2080,7 +2646,7 @@ Exit codes:
2080
2646
  case "patch-approval-check": {
2081
2647
  const id = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
2082
2648
  const pathIndex = rest.indexOf("--path");
2083
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2649
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2084
2650
  try {
2085
2651
  if (!id)
2086
2652
  throw new Error("Usage: anvil patch-approval-check APPROVAL_ID --path PROJECT");
@@ -2098,7 +2664,7 @@ Exit codes:
2098
2664
  case "patch-check": {
2099
2665
  const id = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
2100
2666
  const pathIndex = rest.indexOf("--path");
2101
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
2667
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : ".");
2102
2668
  const numberArg = rest.find((arg, index) => index > 0 && !arg.startsWith("--") && rest[index - 1] !== id && rest[index - 1] !== "--path");
2103
2669
  try {
2104
2670
  if (!id || !numberArg)
@@ -2119,8 +2685,8 @@ Exit codes:
2119
2685
  const json = rest.includes("--json");
2120
2686
  const pathIndex = rest.indexOf("--path");
2121
2687
  const baseIndex = rest.indexOf("--base");
2122
- const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find(arg => !arg.startsWith("--")) ?? ".");
2123
- const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
2688
+ const target = path.resolve(pathIndex >= 0 ? (rest[pathIndex + 1] ?? ".") : (rest.find((arg) => !arg.startsWith("--")) ?? "."));
2689
+ const base = baseIndex >= 0 ? (rest[baseIndex + 1] ?? "HEAD") : "HEAD";
2124
2690
  try {
2125
2691
  const report = await analyzeGitChanges(target, base);
2126
2692
  process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatGitIntelligence(report) + "\n");
@@ -2134,7 +2700,7 @@ Exit codes:
2134
2700
  }
2135
2701
  case "deep": {
2136
2702
  const json = rest.includes("--json");
2137
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
2703
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
2138
2704
  try {
2139
2705
  const report = await analyzeCodebase(target);
2140
2706
  process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatCodebaseAnalysis(report) + "\n");
@@ -2151,11 +2717,17 @@ Exit codes:
2151
2717
  process.stdout.write("Usage: anvil remediate [path] [--audit-report FILE] [--outdated-report FILE] [--json]\n");
2152
2718
  return;
2153
2719
  }
2154
- const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
2720
+ const option = (name) => {
2721
+ const index = rest.indexOf(name);
2722
+ return index >= 0 ? rest[index + 1] : undefined;
2723
+ };
2155
2724
  const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--audit-report" && rest[index - 1] !== "--outdated-report") ?? ".");
2156
2725
  try {
2157
2726
  const auditReport = option("--audit-report"), outdatedReport = option("--outdated-report");
2158
- const plan = await generateRemediationPlan(target, { ...(auditReport ? { auditReport } : {}), ...(outdatedReport ? { outdatedReport } : {}) });
2727
+ const plan = await generateRemediationPlan(target, {
2728
+ ...(auditReport ? { auditReport } : {}),
2729
+ ...(outdatedReport ? { outdatedReport } : {}),
2730
+ });
2159
2731
  process.stdout.write(rest.includes("--json") ? JSON.stringify(plan, null, 2) + "\n" : formatRemediationPlan(plan) + "\n");
2160
2732
  }
2161
2733
  catch (error) {
@@ -2166,7 +2738,7 @@ Exit codes:
2166
2738
  }
2167
2739
  case "health": {
2168
2740
  const json = rest.includes("--json");
2169
- const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
2741
+ const target = path.resolve(rest.find((arg) => !arg.startsWith("--")) ?? ".");
2170
2742
  try {
2171
2743
  const report = await generateProjectHealth(target);
2172
2744
  process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatProjectHealthMarkdown(report) + "\n");
@@ -2190,7 +2762,7 @@ Exit codes:
2190
2762
  const fix = isFix || rest.includes("--fix");
2191
2763
  const verify = rest.includes("--verify");
2192
2764
  const checkOrphans = !rest.includes("--no-orphans");
2193
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
2765
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
2194
2766
  try {
2195
2767
  const { cleanProject, formatCleanReport } = await import("./core/project-cleaner.js");
2196
2768
  const report = await cleanProject(path.resolve(target), { apply, fix, checkOrphans, verify });
@@ -2209,7 +2781,7 @@ Exit codes:
2209
2781
  return;
2210
2782
  }
2211
2783
  const json = rest.includes("--json");
2212
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
2784
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
2213
2785
  try {
2214
2786
  const { undoLastClean, formatUndoReport } = await import("./core/project-cleaner.js");
2215
2787
  const report = await undoLastClean(path.resolve(target));
@@ -2231,7 +2803,7 @@ Exit codes:
2231
2803
  return;
2232
2804
  }
2233
2805
  const json = rest.includes("--json");
2234
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
2806
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
2235
2807
  try {
2236
2808
  const report = await auditProject(path.resolve(target));
2237
2809
  process.stdout.write(json ? `${JSON.stringify(report, null, 2)}\n` : `${formatQualityAudit(report)}\n`);
@@ -2250,7 +2822,7 @@ Exit codes:
2250
2822
  return;
2251
2823
  }
2252
2824
  const json = rest.includes("--json");
2253
- const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
2825
+ const target = rest.find((arg) => !arg.startsWith("--")) ?? ".";
2254
2826
  try {
2255
2827
  if (command === "inspect") {
2256
2828
  const report = await inspectProject(path.resolve(target));
@@ -2277,7 +2849,7 @@ Exit codes:
2277
2849
  }
2278
2850
  const json = rest.includes("--json");
2279
2851
  const doFix = rest.includes("--fix");
2280
- const target = rest.find(arg => !arg.startsWith("--"));
2852
+ const target = rest.find((arg) => !arg.startsWith("--"));
2281
2853
  try {
2282
2854
  const { runDoctorChecks, fixDoctorIssues, formatDoctorReport } = await import("./core/doctor.js");
2283
2855
  if (doFix) {