anvil-engineering-workbench 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/LICENSE +17 -0
- package/README.md +778 -3
- package/SECURITY.md +17 -0
- package/dist/ai/claude-code-provider.d.ts +31 -0
- package/dist/ai/claude-code-provider.js +81 -0
- package/dist/ai/claude-code-provider.js.map +1 -0
- package/dist/ai/provider.d.ts +47 -0
- package/dist/ai/provider.js +57 -0
- package/dist/ai/provider.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1945 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/agent-config-release.d.ts +22 -0
- package/dist/core/agent-config-release.js +109 -0
- package/dist/core/agent-config-release.js.map +1 -0
- package/dist/core/agent-config-versioning.d.ts +33 -0
- package/dist/core/agent-config-versioning.js +209 -0
- package/dist/core/agent-config-versioning.js.map +1 -0
- package/dist/core/agent-evaluation-intelligence.d.ts +46 -0
- package/dist/core/agent-evaluation-intelligence.js +191 -0
- package/dist/core/agent-evaluation-intelligence.js.map +1 -0
- package/dist/core/agent-evolution.d.ts +20 -0
- package/dist/core/agent-evolution.js +35 -0
- package/dist/core/agent-evolution.js.map +1 -0
- package/dist/core/agent-experiment-studio.d.ts +27 -0
- package/dist/core/agent-experiment-studio.js +65 -0
- package/dist/core/agent-experiment-studio.js.map +1 -0
- package/dist/core/agent-optimization.d.ts +27 -0
- package/dist/core/agent-optimization.js +63 -0
- package/dist/core/agent-optimization.js.map +1 -0
- package/dist/core/agent-registry.d.ts +9 -0
- package/dist/core/agent-registry.js +95 -0
- package/dist/core/agent-registry.js.map +1 -0
- package/dist/core/agent-security-audit.d.ts +28 -0
- package/dist/core/agent-security-audit.js +260 -0
- package/dist/core/agent-security-audit.js.map +1 -0
- package/dist/core/agent-workspace-context.d.ts +14 -0
- package/dist/core/agent-workspace-context.js +66 -0
- package/dist/core/agent-workspace-context.js.map +1 -0
- package/dist/core/ai-code-review.d.ts +20 -0
- package/dist/core/ai-code-review.js +70 -0
- package/dist/core/ai-code-review.js.map +1 -0
- package/dist/core/anthropic-provider.d.ts +16 -0
- package/dist/core/anthropic-provider.js +85 -0
- package/dist/core/anthropic-provider.js.map +1 -0
- package/dist/core/architecture-map.d.ts +26 -0
- package/dist/core/architecture-map.js +124 -0
- package/dist/core/architecture-map.js.map +1 -0
- package/dist/core/benchmark-engine.d.ts +52 -0
- package/dist/core/benchmark-engine.js +109 -0
- package/dist/core/benchmark-engine.js.map +1 -0
- package/dist/core/benchmark-matrix.d.ts +123 -0
- package/dist/core/benchmark-matrix.js +169 -0
- package/dist/core/benchmark-matrix.js.map +1 -0
- package/dist/core/benchmark-regression.d.ts +54 -0
- package/dist/core/benchmark-regression.js +96 -0
- package/dist/core/benchmark-regression.js.map +1 -0
- package/dist/core/change-intelligence.d.ts +40 -0
- package/dist/core/change-intelligence.js +114 -0
- package/dist/core/change-intelligence.js.map +1 -0
- package/dist/core/code-intelligence.d.ts +20 -0
- package/dist/core/code-intelligence.js +119 -0
- package/dist/core/code-intelligence.js.map +1 -0
- package/dist/core/code-review.d.ts +30 -0
- package/dist/core/code-review.js +93 -0
- package/dist/core/code-review.js.map +1 -0
- package/dist/core/code-search.d.ts +16 -0
- package/dist/core/code-search.js +93 -0
- package/dist/core/code-search.js.map +1 -0
- package/dist/core/container-sandbox.d.ts +20 -0
- package/dist/core/container-sandbox.js +220 -0
- package/dist/core/container-sandbox.js.map +1 -0
- package/dist/core/dashboard-server.d.ts +11 -0
- package/dist/core/dashboard-server.js +1068 -0
- package/dist/core/dashboard-server.js.map +1 -0
- package/dist/core/dependency-graph.d.ts +29 -0
- package/dist/core/dependency-graph.js +90 -0
- package/dist/core/dependency-graph.js.map +1 -0
- package/dist/core/dependency-impact.d.ts +51 -0
- package/dist/core/dependency-impact.js +201 -0
- package/dist/core/dependency-impact.js.map +1 -0
- package/dist/core/dependency-intelligence.d.ts +60 -0
- package/dist/core/dependency-intelligence.js +241 -0
- package/dist/core/dependency-intelligence.js.map +1 -0
- package/dist/core/doctor.d.ts +17 -0
- package/dist/core/doctor.js +239 -0
- package/dist/core/doctor.js.map +1 -0
- package/dist/core/engineering-risk.d.ts +40 -0
- package/dist/core/engineering-risk.js +125 -0
- package/dist/core/engineering-risk.js.map +1 -0
- package/dist/core/evaluation-engine.d.ts +110 -0
- package/dist/core/evaluation-engine.js +182 -0
- package/dist/core/evaluation-engine.js.map +1 -0
- package/dist/core/execution-trace.d.ts +27 -0
- package/dist/core/execution-trace.js +137 -0
- package/dist/core/execution-trace.js.map +1 -0
- package/dist/core/experiment-history.d.ts +31 -0
- package/dist/core/experiment-history.js +64 -0
- package/dist/core/experiment-history.js.map +1 -0
- package/dist/core/experiment-manifest.d.ts +67 -0
- package/dist/core/experiment-manifest.js +100 -0
- package/dist/core/experiment-manifest.js.map +1 -0
- package/dist/core/gemini-provider.d.ts +15 -0
- package/dist/core/gemini-provider.js +87 -0
- package/dist/core/gemini-provider.js.map +1 -0
- package/dist/core/git-intelligence.d.ts +26 -0
- package/dist/core/git-intelligence.js +84 -0
- package/dist/core/git-intelligence.js.map +1 -0
- package/dist/core/hook-engine.d.ts +23 -0
- package/dist/core/hook-engine.js +74 -0
- package/dist/core/hook-engine.js.map +1 -0
- package/dist/core/hosted-agent-provider.d.ts +19 -0
- package/dist/core/hosted-agent-provider.js +133 -0
- package/dist/core/hosted-agent-provider.js.map +1 -0
- package/dist/core/intelligence-timeline.d.ts +41 -0
- package/dist/core/intelligence-timeline.js +158 -0
- package/dist/core/intelligence-timeline.js.map +1 -0
- package/dist/core/model-router.d.ts +59 -0
- package/dist/core/model-router.js +291 -0
- package/dist/core/model-router.js.map +1 -0
- package/dist/core/openai-compatible-provider.d.ts +19 -0
- package/dist/core/openai-compatible-provider.js +151 -0
- package/dist/core/openai-compatible-provider.js.map +1 -0
- package/dist/core/orchestrator.d.ts +94 -0
- package/dist/core/orchestrator.js +263 -0
- package/dist/core/orchestrator.js.map +1 -0
- package/dist/core/patch-application.d.ts +54 -0
- package/dist/core/patch-application.js +504 -0
- package/dist/core/patch-application.js.map +1 -0
- package/dist/core/patch-approval.d.ts +23 -0
- package/dist/core/patch-approval.js +181 -0
- package/dist/core/patch-approval.js.map +1 -0
- package/dist/core/patch-validation.d.ts +12 -0
- package/dist/core/patch-validation.js +112 -0
- package/dist/core/patch-validation.js.map +1 -0
- package/dist/core/permission-policy.d.ts +13 -0
- package/dist/core/permission-policy.js +98 -0
- package/dist/core/permission-policy.js.map +1 -0
- package/dist/core/project-health.d.ts +72 -0
- package/dist/core/project-health.js +79 -0
- package/dist/core/project-health.js.map +1 -0
- package/dist/core/project-init.d.ts +9 -0
- package/dist/core/project-init.js +253 -0
- package/dist/core/project-init.js.map +1 -0
- package/dist/core/project-inspector.d.ts +24 -0
- package/dist/core/project-inspector.js +175 -0
- package/dist/core/project-inspector.js.map +1 -0
- package/dist/core/project-memory.d.ts +22 -0
- package/dist/core/project-memory.js +173 -0
- package/dist/core/project-memory.js.map +1 -0
- package/dist/core/provider-diagnostics.d.ts +12 -0
- package/dist/core/provider-diagnostics.js +73 -0
- package/dist/core/provider-diagnostics.js.map +1 -0
- package/dist/core/provider-response.d.ts +4 -0
- package/dist/core/provider-response.js +46 -0
- package/dist/core/provider-response.js.map +1 -0
- package/dist/core/quality-audit.d.ts +29 -0
- package/dist/core/quality-audit.js +101 -0
- package/dist/core/quality-audit.js.map +1 -0
- package/dist/core/regression-intelligence.d.ts +41 -0
- package/dist/core/regression-intelligence.js +56 -0
- package/dist/core/regression-intelligence.js.map +1 -0
- package/dist/core/remediation-planner.d.ts +43 -0
- package/dist/core/remediation-planner.js +124 -0
- package/dist/core/remediation-planner.js.map +1 -0
- package/dist/core/review-history.d.ts +18 -0
- package/dist/core/review-history.js +68 -0
- package/dist/core/review-history.js.map +1 -0
- package/dist/core/review-planner.d.ts +2 -0
- package/dist/core/review-planner.js +79 -0
- package/dist/core/review-planner.js.map +1 -0
- package/dist/core/run-history-output.d.ts +22 -0
- package/dist/core/run-history-output.js +51 -0
- package/dist/core/run-history-output.js.map +1 -0
- package/dist/core/run-replay.d.ts +9 -0
- package/dist/core/run-replay.js +64 -0
- package/dist/core/run-replay.js.map +1 -0
- package/dist/core/run-store.d.ts +13 -0
- package/dist/core/run-store.js +300 -0
- package/dist/core/run-store.js.map +1 -0
- package/dist/core/sandbox-cleanup-audit.d.ts +14 -0
- package/dist/core/sandbox-cleanup-audit.js +57 -0
- package/dist/core/sandbox-cleanup-audit.js.map +1 -0
- package/dist/core/sandbox-compliance.d.ts +19 -0
- package/dist/core/sandbox-compliance.js +34 -0
- package/dist/core/sandbox-compliance.js.map +1 -0
- package/dist/core/sandbox-lifecycle.d.ts +8 -0
- package/dist/core/sandbox-lifecycle.js +36 -0
- package/dist/core/sandbox-lifecycle.js.map +1 -0
- package/dist/core/sandbox-policy.d.ts +31 -0
- package/dist/core/sandbox-policy.js +80 -0
- package/dist/core/sandbox-policy.js.map +1 -0
- package/dist/core/sandbox-runtime.d.ts +10 -0
- package/dist/core/sandbox-runtime.js +44 -0
- package/dist/core/sandbox-runtime.js.map +1 -0
- package/dist/core/sandbox-security-profiles.d.ts +16 -0
- package/dist/core/sandbox-security-profiles.js +43 -0
- package/dist/core/sandbox-security-profiles.js.map +1 -0
- package/dist/core/skill-registry.d.ts +14 -0
- package/dist/core/skill-registry.js +186 -0
- package/dist/core/skill-registry.js.map +1 -0
- package/dist/core/staged-verification.d.ts +27 -0
- package/dist/core/staged-verification.js +108 -0
- package/dist/core/staged-verification.js.map +1 -0
- package/dist/core/verification-history.d.ts +10 -0
- package/dist/core/verification-history.js +71 -0
- package/dist/core/verification-history.js.map +1 -0
- package/dist/core/verification.d.ts +37 -0
- package/dist/core/verification.js +204 -0
- package/dist/core/verification.js.map +1 -0
- package/dist/core/workflow-store.d.ts +16 -0
- package/dist/core/workflow-store.js +113 -0
- package/dist/core/workflow-store.js.map +1 -0
- package/dist/core/workspace-browser.d.ts +22 -0
- package/dist/core/workspace-browser.js +115 -0
- package/dist/core/workspace-browser.js.map +1 -0
- package/dist/core/workspace-scan-policy.d.ts +2 -0
- package/dist/core/workspace-scan-policy.js +11 -0
- package/dist/core/workspace-scan-policy.js.map +1 -0
- package/dist/core/workspace-tool-broker.d.ts +28 -0
- package/dist/core/workspace-tool-broker.js +70 -0
- package/dist/core/workspace-tool-broker.js.map +1 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/server.d.ts +2 -0
- package/dist/mcp/server.js +342 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/workspace-path.d.ts +2 -0
- package/dist/mcp/workspace-path.js +41 -0
- package/dist/mcp/workspace-path.js.map +1 -0
- package/package.json +67 -5
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1945 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
6
|
+
import { initializeProject } from "./core/project-init.js";
|
|
7
|
+
import { formatArchitectureMap, mapArchitecture } from "./core/architecture-map.js";
|
|
8
|
+
import { formatProjectReport, inspectProject } from "./core/project-inspector.js";
|
|
9
|
+
import { loadAgentRegistry } from "./core/agent-registry.js";
|
|
10
|
+
import { createReviewPlan } from "./core/review-planner.js";
|
|
11
|
+
import { saveRun, listRuns, loadRun, decideTaskApproval, recoverInterruptedRun, withRunExecutionLock } from "./core/run-store.js";
|
|
12
|
+
import { advanceRun } from "./core/orchestrator.js";
|
|
13
|
+
import { formatRunHistoryJson } from "./core/run-history-output.js";
|
|
14
|
+
import { OpenAICompatibleProvider } from "./core/openai-compatible-provider.js";
|
|
15
|
+
import { createHostedAgentProvider } from "./core/hosted-agent-provider.js";
|
|
16
|
+
import { ClaudeCodeProvider } from "./ai/claude-code-provider.js";
|
|
17
|
+
import { startDashboard } from "./core/dashboard-server.js";
|
|
18
|
+
import { auditProject, formatQualityAudit } from "./core/quality-audit.js";
|
|
19
|
+
import { discoverChecks, formatVerificationResult, runVerification } from "./core/verification.js";
|
|
20
|
+
import { compareVerification, formatVerificationComparison, listVerifications, saveVerification } from "./core/verification-history.js";
|
|
21
|
+
import { formatProjectHealthMarkdown, generateProjectHealth } from "./core/project-health.js";
|
|
22
|
+
import { analyzeCodebase, formatCodebaseAnalysis } from "./core/code-intelligence.js";
|
|
23
|
+
import { analyzeGitChanges, formatGitIntelligence } from "./core/git-intelligence.js";
|
|
24
|
+
import { reviewProject, formatCodeReview } from "./core/code-review.js";
|
|
25
|
+
import { generateAIReview, formatAIReview } from "./core/ai-code-review.js";
|
|
26
|
+
import { compareReviews, formatReviewRecord, listReviews, loadReview, saveReview } from "./core/review-history.js";
|
|
27
|
+
import { formatPatchValidation, validateReviewPatch } from "./core/patch-validation.js";
|
|
28
|
+
import { approveReviewPatch, verifyPatchApproval } from "./core/patch-approval.js";
|
|
29
|
+
import { stageApprovedPatch, reviewStagedPatch } from "./core/patch-application.js";
|
|
30
|
+
import { verifyStagedPatch } from "./core/staged-verification.js";
|
|
31
|
+
import { formatSandboxRuntimeReport, parseSandboxRuntimeReport } from "./core/sandbox-runtime.js";
|
|
32
|
+
import { detectDockerRootless } from "./core/container-sandbox.js";
|
|
33
|
+
import { formatSandboxCleanupReport, parseManagedContainers, selectStoppedManagedContainers } from "./core/sandbox-lifecycle.js";
|
|
34
|
+
import { getSandboxSecurityProfile, listSandboxSecurityProfiles } from "./core/sandbox-security-profiles.js";
|
|
35
|
+
import { createSandboxComplianceReport, formatSandboxComplianceReport } from "./core/sandbox-compliance.js";
|
|
36
|
+
import { formatSandboxPolicyValidation, loadSandboxPolicy } from "./core/sandbox-policy.js";
|
|
37
|
+
import { appendSandboxCleanupAudit, formatSandboxCleanupHistory, readSandboxCleanupAudit } from "./core/sandbox-cleanup-audit.js";
|
|
38
|
+
import { formatRegressionReport, generateRegressionReport } from "./core/regression-intelligence.js";
|
|
39
|
+
import { searchWorkspace } from "./core/code-search.js";
|
|
40
|
+
import { buildAgentWorkspaceContext } from "./core/agent-workspace-context.js";
|
|
41
|
+
import { analyzeDependencyImpact, formatDependencyImpact } from "./core/dependency-impact.js";
|
|
42
|
+
import { analyzeEngineeringRisk, formatEngineeringRisk } from "./core/engineering-risk.js";
|
|
43
|
+
import { analyzeChangeIntelligence, formatChangeIntelligence } from "./core/change-intelligence.js";
|
|
44
|
+
import { captureIntelligenceSnapshot, compareLatestIntelligence, formatIntelligenceComparison } from "./core/intelligence-timeline.js";
|
|
45
|
+
import { analyzeDependencies, formatDependencyIntelligence } from "./core/dependency-intelligence.js";
|
|
46
|
+
import { generateRemediationPlan, formatRemediationPlan } from "./core/remediation-planner.js";
|
|
47
|
+
import { addProjectMemory, buildProjectMemoryContext, listProjectMemory, removeProjectMemory } from "./core/project-memory.js";
|
|
48
|
+
import { formatSkillContext, loadSkillRegistry, selectSkills } from "./core/skill-registry.js";
|
|
49
|
+
import { applyBeforeAgentHooks, loadHooks } from "./core/hook-engine.js";
|
|
50
|
+
import { assertSupportedAgentPolicy, loadPermissionPolicy } from "./core/permission-policy.js";
|
|
51
|
+
import { ModelRouter } from "./core/model-router.js";
|
|
52
|
+
import { loadExecutionTrace, saveExecutionTrace } from "./core/execution-trace.js";
|
|
53
|
+
import { createReplayRun } from "./core/run-replay.js";
|
|
54
|
+
import { evaluateRun, evaluateRunWithJudge, formatEvaluationReport, loadEvaluationSuites, saveEvaluationReport, loadEvaluationReport } from "./core/evaluation-engine.js";
|
|
55
|
+
import { createBenchmarkTrial, formatBenchmark, listBenchmarks, recordBenchmarkTrial } from "./core/benchmark-engine.js";
|
|
56
|
+
import { executeBenchmarkMatrix, formatBenchmarkMatrix, loadBenchmarkMatrix, listBenchmarkMatrices, saveBenchmarkMatrix } from "./core/benchmark-matrix.js";
|
|
57
|
+
import { fingerprintAgentConfig } from "./core/benchmark-matrix.js";
|
|
58
|
+
import { loadAgentConfigVersion } from "./core/agent-config-versioning.js";
|
|
59
|
+
import { formatBenchmarkRegression, loadBenchmarkRegression } from "./core/benchmark-regression.js";
|
|
60
|
+
import { buildAgentOptimizationPlan, formatAgentOptimizationPlan } from "./core/agent-optimization.js";
|
|
61
|
+
import { auditAgentConfiguration, formatAgentSecurityReport } from "./core/agent-security-audit.js";
|
|
62
|
+
import { buildAgentEvaluationIntelligence, formatAgentEvaluationIntelligence } from "./core/agent-evaluation-intelligence.js";
|
|
63
|
+
import { assessAgentEvolution, formatAgentEvolutionAssessment } from "./core/agent-evolution.js";
|
|
64
|
+
import { createSkillExperimentMatrix, formatSkillExperimentMatrix, createModelComparisonMatrix, formatModelComparisonMatrix } from "./core/agent-experiment-studio.js";
|
|
65
|
+
import { buildExperimentHistory, formatExperimentHistory } from "./core/experiment-history.js";
|
|
66
|
+
import { captureAgentConfigVersion, compareAgentConfigVersions, formatAgentConfigVersion, listAgentConfigVersions, materializeAgentConfigVersion } from "./core/agent-config-versioning.js";
|
|
67
|
+
import { decideAgentConfigRelease, formatAgentConfigRelease, listAgentConfigReleases, prepareAgentConfigRelease } from "./core/agent-config-release.js";
|
|
68
|
+
import { captureExperimentManifest, compareExperimentManifests, formatExperimentManifest, loadExperimentManifest, saveExperimentManifest } from "./core/experiment-manifest.js";
|
|
69
|
+
function runSandboxProbe(args) {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const child = spawn("docker", args, { shell: false, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
72
|
+
let output = "";
|
|
73
|
+
let settled = false;
|
|
74
|
+
const finish = (error) => {
|
|
75
|
+
if (settled)
|
|
76
|
+
return;
|
|
77
|
+
settled = true;
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
if (error)
|
|
80
|
+
reject(error);
|
|
81
|
+
else
|
|
82
|
+
resolve(output);
|
|
83
|
+
};
|
|
84
|
+
const append = (chunk) => {
|
|
85
|
+
if (output.length < 4096)
|
|
86
|
+
output += chunk.toString().slice(0, 4096 - output.length);
|
|
87
|
+
};
|
|
88
|
+
child.stdout.on("data", append);
|
|
89
|
+
child.stderr.on("data", append);
|
|
90
|
+
child.once("error", error => finish(error));
|
|
91
|
+
child.once("close", code => code === 0 ? finish() : finish(new Error("Docker probe exited with status " + code + ".")));
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
child.kill("SIGTERM");
|
|
94
|
+
finish(new Error("Docker probe timed out after 15 seconds."));
|
|
95
|
+
}, 15_000);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function readAgentTokenBudgets() {
|
|
99
|
+
const raw = process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT;
|
|
100
|
+
if (!raw)
|
|
101
|
+
return undefined;
|
|
102
|
+
const parsed = JSON.parse(raw);
|
|
103
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
104
|
+
throw new Error("FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT must be a JSON object mapping agent IDs to token limits.");
|
|
105
|
+
const budgets = {};
|
|
106
|
+
for (const [agent, value] of Object.entries(parsed)) {
|
|
107
|
+
if (!agent.trim() || !Number.isSafeInteger(value) || value < 1)
|
|
108
|
+
throw new Error("Every per-agent token budget must be a positive integer.");
|
|
109
|
+
budgets[agent] = value;
|
|
110
|
+
}
|
|
111
|
+
return budgets;
|
|
112
|
+
}
|
|
113
|
+
function readAgentDurationBudgets() {
|
|
114
|
+
const raw = process.env.FORGE_MAX_TASK_DURATION_MS_BY_AGENT;
|
|
115
|
+
if (!raw)
|
|
116
|
+
return undefined;
|
|
117
|
+
const parsed = JSON.parse(raw);
|
|
118
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
119
|
+
throw new Error("FORGE_MAX_TASK_DURATION_MS_BY_AGENT must be a JSON object mapping agent IDs to milliseconds.");
|
|
120
|
+
const budgets = {};
|
|
121
|
+
for (const [agent, value] of Object.entries(parsed)) {
|
|
122
|
+
if (!agent.trim() || !Number.isSafeInteger(value) || value < 1)
|
|
123
|
+
throw new Error("Every per-agent duration budget must be a positive integer number of milliseconds.");
|
|
124
|
+
budgets[agent] = value;
|
|
125
|
+
}
|
|
126
|
+
return budgets;
|
|
127
|
+
}
|
|
128
|
+
const VERSION = "1.0.0";
|
|
129
|
+
const HELP = `
|
|
130
|
+
ANVIL — The AI Engineering Workbench
|
|
131
|
+
|
|
132
|
+
Usage:
|
|
133
|
+
anvil <command> [options]
|
|
134
|
+
forge <command> [options]
|
|
135
|
+
|
|
136
|
+
Commands:
|
|
137
|
+
init [path] Create a safe, local-first .forge starter workspace
|
|
138
|
+
inspect [path] Analyze project languages, tooling, dependencies, and scripts
|
|
139
|
+
evaluate RUNID Score a saved run against a local evaluation suite
|
|
140
|
+
benchmark [RUN_ID] [--suite SUITE_ID] [--path PROJECT] Record or view benchmark results
|
|
141
|
+
benchmark-matrix CONFIG.json --suite SUITE_ID --goal "..." [--path PROJECT] Run model comparisons
|
|
142
|
+
benchmark-regression [MATRIX_ID] [--path PROJECT] Compare latest matrix with its baseline
|
|
143
|
+
optimize-agents [--path PROJECT] Recommend evidence-based agent improvements
|
|
144
|
+
agent-intelligence [--suite ID] [--candidate ID] [--limit N] [--json] [--path PROJECT] Score agent effectiveness across benchmark history
|
|
145
|
+
agent-evolution MATRIX_ID [--path PROJECT] Evaluate a controlled baseline/treatment experiment
|
|
146
|
+
experiment-studio <create|compare> [options] Scaffold a skill or multi-model experiment
|
|
147
|
+
experiment-history [--suite ID] [--candidate ID] [--limit N] [--json] [--path PROJECT] Rank historical benchmark candidates
|
|
148
|
+
agent-config <snapshot|list|show|diff|approve> [options] Version and review agent/skill configuration
|
|
149
|
+
agent-release <prepare|list|show|approve|reject> [options] Gate agent configuration promotion
|
|
150
|
+
experiment <capture|show|compare> [options] Capture and compare reproducible benchmark manifests
|
|
151
|
+
quality [path] Generate an evidence-based static engineering quality report
|
|
152
|
+
deps [path] Analyze npm dependencies, licenses, and imported security reports
|
|
153
|
+
remediate [path] Advisory remediation plan for vulnerable and outdated dependencies
|
|
154
|
+
snapshot [path] Capture a local engineering intelligence snapshot
|
|
155
|
+
timeline [path] Compare the latest snapshot with its predecessor
|
|
156
|
+
change-risk [path] Analyze changed-file risk, downstream impact, and test candidates
|
|
157
|
+
risk [path] Rank engineering hotspots using dependency and quality signals
|
|
158
|
+
health [path] Generate a unified engineering score, evidence coverage, and health snapshot
|
|
159
|
+
deep [path] Analyze AST symbols, circular imports, and change impact
|
|
160
|
+
git [path] Analyze changed files, impact, suggested tests, and risk
|
|
161
|
+
impact [path] Analyze dependency change blast radius and undeclared imports
|
|
162
|
+
search QUERY Symbol-aware source code search (use --limit N, --path PROJECT)
|
|
163
|
+
review [path] Review changed source files; add --ai for provider-backed suggestions
|
|
164
|
+
review-history [path] List saved review snapshots
|
|
165
|
+
review-show ID Display one saved review snapshot
|
|
166
|
+
review-compare A B Compare two saved reviews
|
|
167
|
+
patch-check ID N Dry-run validate AI patch N from a saved review
|
|
168
|
+
patch-approve REVIEW_ID N --reviewer NAME Formally record human approval for AI suggestion N
|
|
169
|
+
patch-approval-check ID Verify cryptographic approval validity and expiration
|
|
170
|
+
patch-stage APPROVAL_ID Stage approved patch in an isolated temporary workspace
|
|
171
|
+
patch-review STAGE_ID Review staged patch diff against source without modifying project
|
|
172
|
+
regression [path] Compare verification history with Git changes and impact
|
|
173
|
+
verify --list Discover available npm scripts without running them
|
|
174
|
+
verify SCRIPT Explicitly run one selected npm script and record its result
|
|
175
|
+
patch-verify ID SCRIPT Verify an approved staged patch in a temporary workspace
|
|
176
|
+
sandbox-status Inspect Docker availability and advertised security options
|
|
177
|
+
sandbox-compliance [PROFILE] [IMAGE@sha256:DIGEST] Generate a sandbox compliance report
|
|
178
|
+
sandbox-policy-check POLICY.json Validate a fail-closed sandbox policy file
|
|
179
|
+
sandbox-profiles [NAME] List or inspect built-in sandbox security profiles
|
|
180
|
+
sandbox-cleanup Review stopped FORGE containers; add --apply to remove them
|
|
181
|
+
sandbox-cleanup-history [PATH] Show recorded cleanup activity
|
|
182
|
+
verify-history List saved verification runs for a project
|
|
183
|
+
map [path] Map source modules and import relationships
|
|
184
|
+
agents [path] List built-in and workspace agent roles
|
|
185
|
+
memory <action> Manage local project memory (add, list, remove, context)
|
|
186
|
+
skills [path] List built-in and workspace skills
|
|
187
|
+
hooks [path] List configured declarative lifecycle hooks
|
|
188
|
+
agent-security [path] Audit agent instructions, skills, hooks, and permissions
|
|
189
|
+
plan --goal TEXT Create and save a reviewable draft plan
|
|
190
|
+
run --goal TEXT Run the plan using a configured AI provider
|
|
191
|
+
runs [path] List locally saved run history (add --json for versioned JSON)
|
|
192
|
+
evals [path] List local agent evaluation suites
|
|
193
|
+
trace RUNID Show per-agent execution telemetry
|
|
194
|
+
replay RUNID Create a new run from retryable failed tasks
|
|
195
|
+
resume RUNID Continue pending or approved tasks in a saved run
|
|
196
|
+
approve RUNID TASK_ID Approve a task awaiting human review
|
|
197
|
+
show-run ID Display one saved run (use --path for project directory)
|
|
198
|
+
doctor [path] Run environment health checks (Node, git, Docker, API keys)
|
|
199
|
+
dashboard [path] Start the local read-only web dashboard
|
|
200
|
+
version Print the installed version
|
|
201
|
+
help Show this help message
|
|
202
|
+
|
|
203
|
+
Options:
|
|
204
|
+
--json Output machine-readable JSON
|
|
205
|
+
--help Show command usage
|
|
206
|
+
|
|
207
|
+
Examples:
|
|
208
|
+
forge inspect .
|
|
209
|
+
forge inspect ./my-app --json
|
|
210
|
+
forge map .
|
|
211
|
+
forge agents .
|
|
212
|
+
forge plan --goal "Add authentication safely"
|
|
213
|
+
forge run --goal "Review the API design"
|
|
214
|
+
forge runs .
|
|
215
|
+
forge replay forge-RUN-ID
|
|
216
|
+
forge approve forge-RUN-ID TASK_ID
|
|
217
|
+
forge resume forge-RUN-ID
|
|
218
|
+
forge dashboard .
|
|
219
|
+
`.trim();
|
|
220
|
+
async function main(args) {
|
|
221
|
+
const [command = "help", ...rest] = args;
|
|
222
|
+
switch (command) {
|
|
223
|
+
case "help":
|
|
224
|
+
case "--help":
|
|
225
|
+
case "-h":
|
|
226
|
+
process.stdout.write(`${HELP}\n`);
|
|
227
|
+
return;
|
|
228
|
+
case "version":
|
|
229
|
+
case "--version":
|
|
230
|
+
case "-v":
|
|
231
|
+
process.stdout.write(`forge ${VERSION}\n`);
|
|
232
|
+
return;
|
|
233
|
+
case "init": {
|
|
234
|
+
const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
|
|
235
|
+
try {
|
|
236
|
+
const result = await initializeProject(path.resolve(target));
|
|
237
|
+
process.stdout.write(`FORGE initialized in ${path.resolve(target)}\n`);
|
|
238
|
+
for (const file of result.created)
|
|
239
|
+
process.stdout.write(` created ${file}\n`);
|
|
240
|
+
for (const file of result.skipped)
|
|
241
|
+
process.stdout.write(` kept ${file} (already exists)\n`);
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
245
|
+
process.stderr.write(`FORGE could not initialize this project: ${message}\n`);
|
|
246
|
+
process.exitCode = 1;
|
|
247
|
+
}
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
case "evals": {
|
|
251
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
252
|
+
try {
|
|
253
|
+
const suites = await loadEvaluationSuites(target);
|
|
254
|
+
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");
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
process.stderr.write("Could not load evaluation suites: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
258
|
+
process.exitCode = 1;
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
case "agent-release": {
|
|
263
|
+
const action = rest[0] ?? "list";
|
|
264
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
265
|
+
const pathIndex = rest.indexOf("--path");
|
|
266
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
267
|
+
const args = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path" && all[index - 1] !== "--reviewer" && all[index - 1] !== "--matrix");
|
|
268
|
+
try {
|
|
269
|
+
if (action === "prepare") {
|
|
270
|
+
const [candidateVersionId, baselineVersionId] = args;
|
|
271
|
+
const matrixId = option("--matrix");
|
|
272
|
+
if (!candidateVersionId || !baselineVersionId || !matrixId)
|
|
273
|
+
throw new Error("Usage: forge agent-release prepare CANDIDATE_CONFIG_ID BASELINE_CONFIG_ID --matrix MATRIX_ID --path PROJECT");
|
|
274
|
+
const regression = await loadBenchmarkRegression(target, matrixId);
|
|
275
|
+
const release = await prepareAgentConfigRelease(target, { candidateVersionId, baselineVersionId, regression });
|
|
276
|
+
process.stdout.write(formatAgentConfigRelease(release) + "\nHuman approval is still required. No live files were changed.\n");
|
|
277
|
+
}
|
|
278
|
+
else if (action === "list") {
|
|
279
|
+
const items = await listAgentConfigReleases(target);
|
|
280
|
+
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");
|
|
281
|
+
}
|
|
282
|
+
else if (action === "show") {
|
|
283
|
+
const id = args[0];
|
|
284
|
+
if (!id)
|
|
285
|
+
throw new Error("Usage: forge agent-release show RELEASE_ID --path PROJECT");
|
|
286
|
+
const item = (await listAgentConfigReleases(target)).find(entry => entry.id === id);
|
|
287
|
+
if (!item)
|
|
288
|
+
throw new Error("Release not found: " + id);
|
|
289
|
+
process.stdout.write(formatAgentConfigRelease(item) + "\n");
|
|
290
|
+
}
|
|
291
|
+
else if (action === "approve" || action === "reject") {
|
|
292
|
+
const id = args[0];
|
|
293
|
+
if (!id)
|
|
294
|
+
throw new Error("Usage: forge agent-release approve|reject RELEASE_ID --reviewer NAME --path PROJECT");
|
|
295
|
+
const reviewer = option("--reviewer");
|
|
296
|
+
if (!reviewer?.trim())
|
|
297
|
+
throw new Error("A reviewer is required via --reviewer NAME.");
|
|
298
|
+
const item = await decideAgentConfigRelease(target, id, reviewer, action);
|
|
299
|
+
process.stdout.write(formatAgentConfigRelease(item) + "\n");
|
|
300
|
+
}
|
|
301
|
+
else
|
|
302
|
+
throw new Error("Unknown action. Use prepare, list, show, approve or reject.");
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
process.stderr.write("FORGE agent release failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
306
|
+
process.exitCode = 1;
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
case "agent-config": {
|
|
311
|
+
const action = rest[0] ?? "list";
|
|
312
|
+
const pathIndex = rest.indexOf("--path");
|
|
313
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
314
|
+
const ids = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path" && all[index - 1] !== "--note" && all[index - 1] !== "--reviewer");
|
|
315
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
316
|
+
try {
|
|
317
|
+
if (action === "snapshot") {
|
|
318
|
+
const version = await captureAgentConfigVersion(target, { note: option("--note") ?? "", createdBy: option("--author") ?? "local-user" });
|
|
319
|
+
process.stdout.write(formatAgentConfigVersion(version) + "\n");
|
|
320
|
+
}
|
|
321
|
+
else if (action === "list") {
|
|
322
|
+
const versions = await listAgentConfigVersions(target);
|
|
323
|
+
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");
|
|
324
|
+
}
|
|
325
|
+
else if (action === "show") {
|
|
326
|
+
const id = ids[0];
|
|
327
|
+
if (!id)
|
|
328
|
+
throw new Error("Usage: forge agent-config show CONFIG_ID --path PROJECT");
|
|
329
|
+
process.stdout.write(formatAgentConfigVersion(await loadAgentConfigVersion(target, id)) + "\n");
|
|
330
|
+
}
|
|
331
|
+
else if (action === "diff") {
|
|
332
|
+
if (ids.length < 2)
|
|
333
|
+
throw new Error("Usage: forge agent-config diff BEFORE_ID AFTER_ID --path PROJECT");
|
|
334
|
+
const [before, after] = await Promise.all([loadAgentConfigVersion(target, ids[0]), loadAgentConfigVersion(target, ids[1])]);
|
|
335
|
+
const diff = compareAgentConfigVersions(before, after);
|
|
336
|
+
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");
|
|
337
|
+
}
|
|
338
|
+
else if (action === "approve") {
|
|
339
|
+
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.");
|
|
340
|
+
}
|
|
341
|
+
else
|
|
342
|
+
throw new Error("Unknown action. Use snapshot, list, show or diff.");
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
process.stderr.write("FORGE agent configuration command failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
346
|
+
process.exitCode = 1;
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
case "experiment-history": {
|
|
351
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
352
|
+
const pathIndex = rest.indexOf("--path");
|
|
353
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
354
|
+
const rawLimit = option("--limit");
|
|
355
|
+
const limit = rawLimit === undefined ? undefined : Number(rawLimit);
|
|
356
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1 || limit > 100)) {
|
|
357
|
+
process.stderr.write("FORGE experiment history: --limit must be an integer from 1 to 100.\n");
|
|
358
|
+
process.exitCode = 1;
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
const suiteId = option("--suite");
|
|
363
|
+
const candidateId = option("--candidate");
|
|
364
|
+
const reports = await listBenchmarkMatrices(target);
|
|
365
|
+
const history = buildExperimentHistory(reports, { ...(suiteId ? { suiteId } : {}), ...(candidateId ? { candidateId } : {}), ...(limit !== undefined ? { limit } : {}) });
|
|
366
|
+
process.stdout.write(rest.includes("--json") ? JSON.stringify(history, null, 2) + "\n" : formatExperimentHistory(history) + "\n");
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
process.stderr.write("FORGE experiment history failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
370
|
+
process.exitCode = 1;
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
case "experiment-studio": {
|
|
375
|
+
const action = rest[0] ?? "create";
|
|
376
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
377
|
+
if (action !== "create" && action !== "compare") {
|
|
378
|
+
process.stderr.write("Usage: forge 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");
|
|
379
|
+
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");
|
|
380
|
+
process.exitCode = 1;
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const id = option("--id");
|
|
384
|
+
const name = option("--name");
|
|
385
|
+
if (action === "compare") {
|
|
386
|
+
const rawCandidates = option("--candidates");
|
|
387
|
+
const repeats = Number(option("--repeats") ?? "2");
|
|
388
|
+
const pathIndex = rest.indexOf("--path");
|
|
389
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
390
|
+
if (!id || !name || !rawCandidates) {
|
|
391
|
+
process.stderr.write("Usage: forge experiment-studio compare --id ID --name NAME --candidates id:provider:model,id:provider:model [--repeats 2] [--path PROJECT]\n");
|
|
392
|
+
process.exitCode = 1;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
const configVersions = new Map((option("--config-versions") ?? "").split(",").filter(Boolean).map(item => {
|
|
397
|
+
const separator = item.indexOf("=");
|
|
398
|
+
if (separator < 1 || separator === item.length - 1)
|
|
399
|
+
throw new Error("Each config version must use candidate-id=cfg-xxxxxxxxxxxx format.");
|
|
400
|
+
return [item.slice(0, separator), item.slice(separator + 1)];
|
|
401
|
+
}));
|
|
402
|
+
const candidates = rawCandidates.split(",").map(item => {
|
|
403
|
+
const [candidateId, candidateProvider, ...modelParts] = item.split(":");
|
|
404
|
+
const model = modelParts.join(":");
|
|
405
|
+
if (!candidateId || !candidateProvider || !model)
|
|
406
|
+
throw new Error("Each candidate must use id:provider:model format.");
|
|
407
|
+
if (!["openai", "claude-code", "anthropic", "google", "ollama"].includes(candidateProvider))
|
|
408
|
+
throw new Error("Unsupported candidate provider: " + candidateProvider);
|
|
409
|
+
const configVersionId = configVersions.get(candidateId);
|
|
410
|
+
return { id: candidateId, provider: candidateProvider, model, ...(configVersionId ? { configVersionId } : {}) };
|
|
411
|
+
});
|
|
412
|
+
for (const candidateId of configVersions.keys())
|
|
413
|
+
if (!candidates.some(candidate => candidate.id === candidateId))
|
|
414
|
+
throw new Error("Configuration snapshot references unknown candidate: " + candidateId);
|
|
415
|
+
for (const candidate of candidates)
|
|
416
|
+
if (candidate.configVersionId)
|
|
417
|
+
await loadAgentConfigVersion(target, candidate.configVersionId);
|
|
418
|
+
const matrix = createModelComparisonMatrix({ id, name, repeats, candidates });
|
|
419
|
+
const destination = path.join(target, ".forge", "experiments", id + ".json");
|
|
420
|
+
const { mkdir, writeFile } = await import("node:fs/promises");
|
|
421
|
+
await mkdir(path.dirname(destination), { recursive: true, mode: 0o700 });
|
|
422
|
+
await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
423
|
+
process.stdout.write(formatModelComparisonMatrix(matrix) + "\nSaved: " + path.relative(target, destination) + "\n");
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
process.stderr.write("FORGE model comparison failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
427
|
+
process.exitCode = 1;
|
|
428
|
+
}
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const provider = option("--provider") ?? "openai";
|
|
432
|
+
const model = option("--model");
|
|
433
|
+
const skills = option("--skills");
|
|
434
|
+
const repeats = Number(option("--repeats") ?? "2");
|
|
435
|
+
const pathIndex = rest.indexOf("--path");
|
|
436
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
437
|
+
if (!id || !name || !model || !skills) {
|
|
438
|
+
process.stderr.write("Usage: forge experiment-studio create --id ID --name NAME --provider openai|claude-code --model MODEL --skills skill-a,skill-b [--repeats 2] [--path PROJECT]\n");
|
|
439
|
+
process.exitCode = 1;
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (!["openai", "claude-code", "anthropic", "google", "ollama"].includes(provider)) {
|
|
443
|
+
process.stderr.write("Unsupported provider. Choose openai, claude-code, anthropic, google or ollama.\n");
|
|
444
|
+
process.exitCode = 1;
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
try {
|
|
448
|
+
const skillIds = skills.split(",").map(item => item.trim()).filter(Boolean);
|
|
449
|
+
const matrix = createSkillExperimentMatrix({ id, name, baselineCandidateId: "baseline", treatmentCandidateId: "treatment", provider: provider, model, repeats, skillIds });
|
|
450
|
+
const registry = await loadSkillRegistry(target);
|
|
451
|
+
const unknown = skillIds.filter(skillId => !registry.some(skill => skill.id === skillId));
|
|
452
|
+
if (unknown.length)
|
|
453
|
+
throw new Error("Unknown workspace/built-in skill(s): " + unknown.join(", "));
|
|
454
|
+
const destination = path.join(target, ".forge", "experiments", id + ".json");
|
|
455
|
+
const { mkdir, writeFile } = await import("node:fs/promises");
|
|
456
|
+
await mkdir(path.dirname(destination), { recursive: true, mode: 0o700 });
|
|
457
|
+
await writeFile(destination, JSON.stringify(matrix, null, 2) + "\n", { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
458
|
+
process.stdout.write(formatSkillExperimentMatrix(matrix) + "\nSaved: " + path.relative(target, destination) + "\n");
|
|
459
|
+
}
|
|
460
|
+
catch (error) {
|
|
461
|
+
process.stderr.write("FORGE experiment studio failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
462
|
+
process.exitCode = 1;
|
|
463
|
+
}
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
case "agent-evolution": {
|
|
467
|
+
const matrixId = rest[0];
|
|
468
|
+
const pathIndex = rest.indexOf("--path");
|
|
469
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
470
|
+
if (!matrixId || matrixId.startsWith("--")) {
|
|
471
|
+
process.stderr.write("Usage: forge agent-evolution MATRIX_ID --path PROJECT\n");
|
|
472
|
+
process.exitCode = 1;
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
try {
|
|
476
|
+
const history = await listBenchmarkMatrices(target);
|
|
477
|
+
const report = history.filter(item => item.matrixId === matrixId).sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
|
|
478
|
+
if (!report)
|
|
479
|
+
throw new Error("Benchmark matrix not found: " + matrixId);
|
|
480
|
+
const assessment = assessAgentEvolution(report);
|
|
481
|
+
process.stdout.write(formatAgentEvolutionAssessment(assessment) + "\n");
|
|
482
|
+
if (assessment.status !== "eligible-for-review")
|
|
483
|
+
process.exitCode = assessment.status === "insufficient-evidence" ? 2 : 1;
|
|
484
|
+
}
|
|
485
|
+
catch (error) {
|
|
486
|
+
process.stderr.write("FORGE agent evolution failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
487
|
+
process.exitCode = 1;
|
|
488
|
+
}
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
case "agent-intelligence": {
|
|
492
|
+
if (rest.includes("--help")) {
|
|
493
|
+
process.stdout.write("Usage: forge agent-intelligence [--suite ID] [--candidate ID] [--limit N] [--json] [--path PROJECT]\n" +
|
|
494
|
+
"Score agent effectiveness across benchmark history.\n\n" +
|
|
495
|
+
"Options:\n" +
|
|
496
|
+
" --suite ID Filter to a specific evaluation suite ID.\n" +
|
|
497
|
+
" --candidate ID Filter to a specific candidate ID.\n" +
|
|
498
|
+
" --limit N Number of historical runs to analyze (2–50, default 10).\n" +
|
|
499
|
+
" --json Output the full intelligence report as machine-readable JSON.\n" +
|
|
500
|
+
" --path PROJECT Path to the FORGE project directory (defaults to cwd).\n\n" +
|
|
501
|
+
"Exit codes:\n" +
|
|
502
|
+
" 0 Ready: at least one scorecard was produced.\n" +
|
|
503
|
+
" 1 Error: an unexpected failure occurred.\n" +
|
|
504
|
+
" 2 Insufficient data: not enough benchmark history to generate scorecards.\n");
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const option = (name) => {
|
|
508
|
+
const index = rest.indexOf(name);
|
|
509
|
+
if (index < 0)
|
|
510
|
+
return undefined;
|
|
511
|
+
const next = rest[index + 1];
|
|
512
|
+
return next && !next.startsWith("--") ? next : undefined;
|
|
513
|
+
};
|
|
514
|
+
const pathIndex = rest.indexOf("--path");
|
|
515
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
516
|
+
const rawLimit = option("--limit");
|
|
517
|
+
const limit = rawLimit === undefined ? undefined : Number(rawLimit);
|
|
518
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 2 || limit > 50)) {
|
|
519
|
+
process.stderr.write("FORGE agent intelligence: --limit must be an integer from 2 to 50.\n");
|
|
520
|
+
process.exitCode = 1;
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
const history = await listBenchmarkMatrices(target);
|
|
525
|
+
const suiteId = option("--suite");
|
|
526
|
+
const candidateId = option("--candidate");
|
|
527
|
+
const report = buildAgentEvaluationIntelligence(history, {
|
|
528
|
+
...(suiteId ? { suiteId } : {}),
|
|
529
|
+
...(candidateId ? { candidateId } : {}),
|
|
530
|
+
...(limit !== undefined ? { limit } : {})
|
|
531
|
+
});
|
|
532
|
+
process.stdout.write(rest.includes("--json")
|
|
533
|
+
? JSON.stringify(report, null, 2) + "\n"
|
|
534
|
+
: formatAgentEvaluationIntelligence(report) + "\n");
|
|
535
|
+
if (report.status === "insufficient-data")
|
|
536
|
+
process.exitCode = 2;
|
|
537
|
+
}
|
|
538
|
+
catch (error) {
|
|
539
|
+
process.stderr.write("FORGE agent intelligence failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
540
|
+
process.exitCode = 1;
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
case "optimize-agents": {
|
|
545
|
+
const pathIndex = rest.indexOf("--path");
|
|
546
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
547
|
+
try {
|
|
548
|
+
const matrices = await listBenchmarkMatrices(target);
|
|
549
|
+
const plan = buildAgentOptimizationPlan(matrices);
|
|
550
|
+
process.stdout.write(formatAgentOptimizationPlan(plan) + "\n");
|
|
551
|
+
if (plan.status === "insufficient-history")
|
|
552
|
+
process.exitCode = 2;
|
|
553
|
+
}
|
|
554
|
+
catch (error) {
|
|
555
|
+
process.stderr.write("FORGE adaptive agent optimization failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
556
|
+
process.exitCode = 1;
|
|
557
|
+
}
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
case "benchmark-regression": {
|
|
561
|
+
const targetIndex = rest.indexOf("--path");
|
|
562
|
+
const target = path.resolve(targetIndex >= 0 ? rest[targetIndex + 1] ?? "." : ".");
|
|
563
|
+
const matrixId = rest.find((arg, index) => !arg.startsWith("--") && index === 0);
|
|
564
|
+
try {
|
|
565
|
+
const report = await loadBenchmarkRegression(target, matrixId);
|
|
566
|
+
process.stdout.write(formatBenchmarkRegression(report) + "\n");
|
|
567
|
+
if (report.status === "blocked")
|
|
568
|
+
process.exitCode = 2;
|
|
569
|
+
}
|
|
570
|
+
catch (error) {
|
|
571
|
+
process.stderr.write("FORGE benchmark regression failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
572
|
+
process.exitCode = 1;
|
|
573
|
+
}
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
case "experiment": {
|
|
577
|
+
const action = rest[0] ?? "capture";
|
|
578
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
579
|
+
const pathIndex = rest.indexOf("--path");
|
|
580
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
581
|
+
try {
|
|
582
|
+
if (action === "capture") {
|
|
583
|
+
const matrixFile = option("--matrix-file");
|
|
584
|
+
const suiteId = option("--suite");
|
|
585
|
+
const matrixId = option("--matrix-id");
|
|
586
|
+
const provider = option("--provider") ?? process.env.FORGE_AI_PROVIDER ?? "openai";
|
|
587
|
+
const model = option("--model") ?? process.env.FORGE_AI_MODEL ?? process.env.FORGE_MODEL ?? "gpt-4.1";
|
|
588
|
+
if (!matrixFile || !suiteId || !matrixId)
|
|
589
|
+
throw new Error("Usage: forge experiment capture --matrix-file CONFIG.json --matrix-id ID --suite SUITE_ID [--provider ID] [--model NAME] [--path PROJECT]");
|
|
590
|
+
const matrix = await loadBenchmarkMatrix(path.resolve(matrixFile));
|
|
591
|
+
const suites = await loadEvaluationSuites(target);
|
|
592
|
+
const suite = suites.find(item => item.id === suiteId);
|
|
593
|
+
if (!suite)
|
|
594
|
+
throw new Error("Evaluation suite not found: " + suiteId);
|
|
595
|
+
const fingerprints = [];
|
|
596
|
+
for (const candidate of matrix.candidates) {
|
|
597
|
+
if (!candidate.configVersionId)
|
|
598
|
+
continue;
|
|
599
|
+
const snapshot = await loadAgentConfigVersion(target, candidate.configVersionId);
|
|
600
|
+
fingerprints.push({ candidateId: candidate.id, configVersionId: snapshot.id, fingerprint: fingerprintAgentConfig(snapshot.files) });
|
|
601
|
+
}
|
|
602
|
+
const manifest = await captureExperimentManifest({ projectPath: target, matrixFile: path.resolve(matrixFile), matrixId, suiteId, suite, provider, model, ...(process.env.FORGE_BASE_URL ? { baseUrl: process.env.FORGE_BASE_URL } : {}), agentConfigFingerprints: fingerprints });
|
|
603
|
+
const saved = await saveExperimentManifest(target, manifest);
|
|
604
|
+
process.stdout.write(formatExperimentManifest(manifest) + "\nSaved: " + path.relative(target, saved) + "\n");
|
|
605
|
+
}
|
|
606
|
+
else if (action === "show") {
|
|
607
|
+
const id = rest[1];
|
|
608
|
+
if (!id)
|
|
609
|
+
throw new Error("Usage: forge experiment show EXPERIMENT_ID --path PROJECT");
|
|
610
|
+
process.stdout.write(formatExperimentManifest(await loadExperimentManifest(target, id)) + "\n");
|
|
611
|
+
}
|
|
612
|
+
else if (action === "compare") {
|
|
613
|
+
const ids = rest.slice(1).filter((arg, index, all) => !arg.startsWith("--") && all[index - 1] !== "--path");
|
|
614
|
+
if (ids.length !== 2)
|
|
615
|
+
throw new Error("Usage: forge experiment compare EXPERIMENT_A EXPERIMENT_B --path PROJECT");
|
|
616
|
+
const [a, b] = await Promise.all([loadExperimentManifest(target, ids[0]), loadExperimentManifest(target, ids[1])]);
|
|
617
|
+
const comparison = compareExperimentManifests(a, b);
|
|
618
|
+
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");
|
|
619
|
+
if (!comparison.compatible)
|
|
620
|
+
process.exitCode = 2;
|
|
621
|
+
}
|
|
622
|
+
else
|
|
623
|
+
throw new Error("Unknown action. Use capture, show or compare.");
|
|
624
|
+
}
|
|
625
|
+
catch (error) {
|
|
626
|
+
process.stderr.write("FORGE experiment command failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
627
|
+
process.exitCode = 1;
|
|
628
|
+
}
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
case "benchmark-matrix": {
|
|
632
|
+
const configFile = rest.find(arg => !arg.startsWith("--"));
|
|
633
|
+
const suiteIndex = rest.indexOf("--suite"), goalIndex = rest.indexOf("--goal"), pathIndex = rest.indexOf("--path");
|
|
634
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
635
|
+
const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
|
|
636
|
+
const goalEnd = goalIndex >= 0 ? rest.findIndex((arg, index) => index > goalIndex && arg.startsWith("--")) : -1;
|
|
637
|
+
const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd >= 0 ? goalEnd : rest.length).join(" ").trim() : "";
|
|
638
|
+
if (!configFile || !suiteId || !goal) {
|
|
639
|
+
process.stderr.write("Usage: forge benchmark-matrix CONFIG.json --suite SUITE_ID --goal \"engineering task\" [--path PROJECT]\n");
|
|
640
|
+
process.exitCode = 1;
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
try {
|
|
644
|
+
const config = await loadBenchmarkMatrix(path.resolve(configFile));
|
|
645
|
+
const configVersions = new Map();
|
|
646
|
+
for (const candidate of config.candidates) {
|
|
647
|
+
if (!candidate.configVersionId)
|
|
648
|
+
continue;
|
|
649
|
+
const snapshot = await loadAgentConfigVersion(target, candidate.configVersionId);
|
|
650
|
+
configVersions.set(candidate.id, { id: snapshot.id, fingerprint: fingerprintAgentConfig(snapshot.files) });
|
|
651
|
+
}
|
|
652
|
+
const suites = await loadEvaluationSuites(target);
|
|
653
|
+
const suite = suites.find(item => item.id === suiteId);
|
|
654
|
+
if (!suite)
|
|
655
|
+
throw new Error("Evaluation suite not found: " + suiteId);
|
|
656
|
+
const apiKey = process.env.FORGE_API_KEY ?? process.env.OPENAI_API_KEY ?? "";
|
|
657
|
+
const baseUrl = process.env.FORGE_BASE_URL ?? "https://api.openai.com/v1";
|
|
658
|
+
const report = await executeBenchmarkMatrix(config, async (candidate, _repeat) => {
|
|
659
|
+
const createProvider = (selectedModel) => candidate.provider === "claude-code"
|
|
660
|
+
? new ClaudeCodeProvider({ cwd: target, model: selectedModel })
|
|
661
|
+
: createHostedAgentProvider({ ...process.env, FORGE_AI_PROVIDER: candidate.provider, ...(apiKey ? { FORGE_API_KEY: apiKey } : {}), ...(process.env.FORGE_BASE_URL ? { FORGE_BASE_URL: process.env.FORGE_BASE_URL } : {}) }, selectedModel);
|
|
662
|
+
const router = new ModelRouter({
|
|
663
|
+
fastModel: candidate.model, balancedModel: candidate.model, reasoningModel: candidate.model,
|
|
664
|
+
retryAttempts: Number(process.env.FORGE_MODEL_RETRY_ATTEMPTS ?? 2),
|
|
665
|
+
retryDelayMs: Number(process.env.FORGE_MODEL_RETRY_DELAY_MS ?? 500),
|
|
666
|
+
...(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK ? { maxInputTokensPerTask: Number(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK) } : {}),
|
|
667
|
+
...(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK ? { maxOutputTokensPerTask: Number(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK) } : {}),
|
|
668
|
+
...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN ? { maxEstimatedTokensPerRun: Number(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN) } : {}),
|
|
669
|
+
...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
|
|
670
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS ? { maxTaskDurationMs: Number(process.env.FORGE_MAX_TASK_DURATION_MS) } : {}),
|
|
671
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS_BY_AGENT ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
|
|
672
|
+
createProvider,
|
|
673
|
+
});
|
|
674
|
+
const configWorkspace = await mkdtemp(path.join(os.tmpdir(), "forge-candidate-config-"));
|
|
675
|
+
try {
|
|
676
|
+
const snapshotId = candidate.configVersionId;
|
|
677
|
+
const snapshot = snapshotId ? await materializeAgentConfigVersion(target, snapshotId, configWorkspace) : undefined;
|
|
678
|
+
if (snapshotId && !snapshot?.files.some(file => file.path === "hooks.json")) {
|
|
679
|
+
throw new Error("Configuration snapshot " + snapshotId + " predates policy-hook isolation. Capture a new snapshot before benchmarking this candidate.");
|
|
680
|
+
}
|
|
681
|
+
const configRoot = snapshotId ? configWorkspace : target;
|
|
682
|
+
const run = createReviewPlan(goal);
|
|
683
|
+
const skillRegistry = await loadSkillRegistry(configRoot);
|
|
684
|
+
const agentRegistry = await loadAgentRegistry(configRoot);
|
|
685
|
+
const requestedSkillIds = candidate.skillIds;
|
|
686
|
+
const unknownSkills = (requestedSkillIds ?? []).filter(id => !skillRegistry.some(skill => skill.id === id));
|
|
687
|
+
if (unknownSkills.length)
|
|
688
|
+
throw new Error("Candidate " + candidate.id + " references unknown skill(s): " + unknownSkills.join(", "));
|
|
689
|
+
const selectedSkillsForTask = (task) => requestedSkillIds === undefined ? selectSkills(skillRegistry, task.agent + " " + task.title + " " + task.prompt) : skillRegistry.filter(skill => requestedSkillIds.includes(skill.id));
|
|
690
|
+
const agentGuidanceForTask = (task) => agentRegistry.find(agent => agent.id === task.agent)?.instructions ?? "";
|
|
691
|
+
const permissions = await loadPermissionPolicy(configRoot);
|
|
692
|
+
if (permissions.mode !== "proposal-only" || permissions.allowedTools.length > 0)
|
|
693
|
+
throw new Error("Benchmark candidate " + candidate.id + " requests unsupported tool permissions. FORGE currently enforces proposal-only execution with no enabled tools.");
|
|
694
|
+
const hooks = await loadHooks(configRoot);
|
|
695
|
+
for (const record of run.tasks) {
|
|
696
|
+
const applied = applyBeforeAgentHooks(record.task, hooks);
|
|
697
|
+
record.task = { ...applied.task, prompt: applied.context.length ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n") : applied.task.prompt };
|
|
698
|
+
if (applied.blockedReason) {
|
|
699
|
+
record.status = "blocked";
|
|
700
|
+
record.error = "Blocked by policy hook: " + applied.blockedReason;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const memory = await buildProjectMemoryContext(target, goal);
|
|
704
|
+
const projectContext = memory.text;
|
|
705
|
+
const started = Date.now();
|
|
706
|
+
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") });
|
|
707
|
+
const durationMs = Date.now() - started;
|
|
708
|
+
await saveRun(target, completed);
|
|
709
|
+
await saveExecutionTrace(target, completed.id, router.traces);
|
|
710
|
+
const evaluation = evaluateRun(completed, suite);
|
|
711
|
+
await saveEvaluationReport(target, evaluation);
|
|
712
|
+
return {
|
|
713
|
+
runId: completed.id, score: evaluation.score, passed: evaluation.passed,
|
|
714
|
+
durationMs,
|
|
715
|
+
estimatedTokens: router.traces.reduce((sum, item) => sum + item.inputTokensEstimate + item.outputTokensEstimate, 0),
|
|
716
|
+
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) })) }; }),
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
finally {
|
|
720
|
+
await rm(configWorkspace, { recursive: true, force: true });
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
report.suiteId = suite.id;
|
|
724
|
+
report.goal = goal;
|
|
725
|
+
if (configVersions.size)
|
|
726
|
+
report.candidateProvenance = [...configVersions.entries()].map(([candidateId, item]) => ({ candidateId, configVersionId: item.id, fingerprint: item.fingerprint }));
|
|
727
|
+
const experimentManifest = await captureExperimentManifest({
|
|
728
|
+
projectPath: target, matrixFile: path.resolve(configFile), matrixId: config.id, suiteId: suite.id, suite,
|
|
729
|
+
provider: "matrix", model: "candidate-specific",
|
|
730
|
+
...(process.env.FORGE_BASE_URL ? { baseUrl: process.env.FORGE_BASE_URL } : {}),
|
|
731
|
+
agentConfigFingerprints: report.candidateProvenance ?? []
|
|
732
|
+
});
|
|
733
|
+
const manifestPath = await saveExperimentManifest(target, experimentManifest);
|
|
734
|
+
report.experimentManifest = { id: experimentManifest.id, fingerprint: experimentManifest.fingerprint };
|
|
735
|
+
const saved = await saveBenchmarkMatrix(target, report);
|
|
736
|
+
process.stdout.write("Experiment manifest: " + experimentManifest.id + " · sha256:" + experimentManifest.fingerprint + "\nManifest saved: " + path.relative(target, manifestPath) + "\n");
|
|
737
|
+
process.stdout.write(formatBenchmarkMatrix(report) + "\nSaved: " + path.relative(target, saved) + "\n");
|
|
738
|
+
if (report.results.some(item => item.status === "failed") || report.results.some(item => !item.passed))
|
|
739
|
+
process.exitCode = 2;
|
|
740
|
+
}
|
|
741
|
+
catch (error) {
|
|
742
|
+
process.stderr.write("FORGE benchmark matrix failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
743
|
+
process.exitCode = 1;
|
|
744
|
+
}
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
case "benchmark": {
|
|
748
|
+
const pathIndex = rest.indexOf("--path"), suiteIndex = rest.indexOf("--suite");
|
|
749
|
+
const runId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path" && rest[index - 1] !== "--suite");
|
|
750
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
751
|
+
const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
|
|
752
|
+
try {
|
|
753
|
+
if (!runId) {
|
|
754
|
+
const documents = await listBenchmarks(target);
|
|
755
|
+
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");
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
const report = await loadEvaluationReport(target, runId, suiteId);
|
|
759
|
+
const trace = await loadExecutionTrace(target, runId);
|
|
760
|
+
const document = await recordBenchmarkTrial(target, createBenchmarkTrial(report, trace));
|
|
761
|
+
process.stdout.write(formatBenchmark(document) + "\n");
|
|
762
|
+
}
|
|
763
|
+
catch (error) {
|
|
764
|
+
process.stderr.write("FORGE benchmark failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
765
|
+
process.exitCode = 1;
|
|
766
|
+
}
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
case "evaluate": {
|
|
770
|
+
const runId = rest.find(arg => !arg.startsWith("--"));
|
|
771
|
+
const pathIndex = rest.indexOf("--path"), suiteIndex = rest.indexOf("--suite");
|
|
772
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
773
|
+
const suiteId = suiteIndex >= 0 ? rest[suiteIndex + 1] : undefined;
|
|
774
|
+
if (!runId) {
|
|
775
|
+
process.stderr.write("Usage: forge evaluate RUNID [--suite SUITE_ID] [--path PROJECT]\n");
|
|
776
|
+
process.exitCode = 1;
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
try {
|
|
780
|
+
const suites = await loadEvaluationSuites(target);
|
|
781
|
+
const suite = suiteId ? suites.find(item => item.id === suiteId) : suites[0];
|
|
782
|
+
if (!suite)
|
|
783
|
+
throw new Error(suiteId ? "Evaluation suite not found: " + suiteId : "No evaluation suite found. Add one under .forge/evals/.");
|
|
784
|
+
const run = await loadRun(target, runId);
|
|
785
|
+
const judgeEnabled = rest.includes("--judge");
|
|
786
|
+
let report;
|
|
787
|
+
if (judgeEnabled) {
|
|
788
|
+
const providerChoice = process.env.FORGE_AI_PROVIDER ?? "openai";
|
|
789
|
+
const model = process.env.FORGE_EVAL_JUDGE_MODEL ?? process.env.FORGE_AI_MODEL ?? process.env.FORGE_MODEL ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1");
|
|
790
|
+
const apiKey = process.env.FORGE_API_KEY ?? process.env.OPENAI_API_KEY ?? "";
|
|
791
|
+
const baseUrl = process.env.FORGE_BASE_URL ?? "https://api.openai.com/v1";
|
|
792
|
+
if (providerChoice !== "claude-code" && !apiKey)
|
|
793
|
+
throw new Error("Set FORGE_API_KEY or OPENAI_API_KEY to use model-judged evaluation.");
|
|
794
|
+
const judgeProvider = providerChoice === "claude-code" ? new ClaudeCodeProvider({ cwd: target, model }) : new OpenAICompatibleProvider({ apiKey, model, baseUrl });
|
|
795
|
+
report = await evaluateRunWithJudge(run, suite, judgeProvider);
|
|
796
|
+
}
|
|
797
|
+
else
|
|
798
|
+
report = evaluateRun(run, suite);
|
|
799
|
+
const reportPath = await saveEvaluationReport(target, report);
|
|
800
|
+
process.stdout.write(formatEvaluationReport(report) + "\n");
|
|
801
|
+
if ("judgedCases" in report) {
|
|
802
|
+
process.stdout.write("Model judge: " + report.judge.provider + (report.judge.model ? " / " + report.judge.model : "") + " · " + report.judge.evaluatedCases + " cases\n");
|
|
803
|
+
for (const item of report.judgedCases)
|
|
804
|
+
process.stdout.write(" " + item.caseId + " · " + item.result.score + "/100 · " + item.result.rationale + "\n");
|
|
805
|
+
}
|
|
806
|
+
process.stdout.write("Saved: " + reportPath + "\n");
|
|
807
|
+
if (!report.passed || ("judge" in report && !report.judge.passed))
|
|
808
|
+
process.exitCode = 2;
|
|
809
|
+
}
|
|
810
|
+
catch (error) {
|
|
811
|
+
process.stderr.write("FORGE evaluation failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
812
|
+
process.exitCode = 1;
|
|
813
|
+
}
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
case "agents": {
|
|
817
|
+
const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
|
|
818
|
+
try {
|
|
819
|
+
const agents = await loadAgentRegistry(path.resolve(target));
|
|
820
|
+
process.stdout.write("FORGE agent registry\n\n");
|
|
821
|
+
for (const agent of agents) {
|
|
822
|
+
process.stdout.write(` ${agent.id.padEnd(20)} ${agent.name} [${agent.source}]\n ${agent.description}\n`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
catch (error) {
|
|
826
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
827
|
+
process.stderr.write(`FORGE could not load agents: ${message}\n`);
|
|
828
|
+
process.exitCode = 1;
|
|
829
|
+
}
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
case "skills": {
|
|
833
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
834
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--") && arg !== option("--show") && arg !== option("--match")) ?? ".");
|
|
835
|
+
try {
|
|
836
|
+
const skills = await loadSkillRegistry(target);
|
|
837
|
+
const showId = option("--show");
|
|
838
|
+
const matchGoal = option("--match");
|
|
839
|
+
if (showId) {
|
|
840
|
+
const skill = skills.find(item => item.id === showId);
|
|
841
|
+
if (!skill)
|
|
842
|
+
throw new Error("Skill not found: " + showId);
|
|
843
|
+
process.stdout.write((rest.includes("--json") ? JSON.stringify(skill, null, 2) : formatSkillContext([skill])) + "\n");
|
|
844
|
+
}
|
|
845
|
+
else if (matchGoal !== undefined) {
|
|
846
|
+
const selected = selectSkills(skills, matchGoal);
|
|
847
|
+
process.stdout.write(rest.includes("--json") ? JSON.stringify(selected, null, 2) + "\n" :
|
|
848
|
+
selected.length ? selected.map(skill => skill.id + " " + skill.name + " — " + skill.description).join("\n") + "\n" : "No skills matched that goal.\n");
|
|
849
|
+
}
|
|
850
|
+
else if (rest.includes("--json")) {
|
|
851
|
+
process.stdout.write(JSON.stringify(skills, null, 2) + "\n");
|
|
852
|
+
}
|
|
853
|
+
else {
|
|
854
|
+
process.stdout.write("FORGE skill registry (" + skills.length + ")\n\n");
|
|
855
|
+
for (const skill of skills)
|
|
856
|
+
process.stdout.write(" " + skill.id.padEnd(24) + " " + skill.name + " [" + skill.source + "]\n " + skill.description + "\n");
|
|
857
|
+
process.stdout.write("\nUse --show ID to inspect instructions or --match \"GOAL\" to preview task selection.\n");
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
catch (error) {
|
|
861
|
+
process.stderr.write("Could not load skills: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
862
|
+
process.exitCode = 1;
|
|
863
|
+
}
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
case "agent-security": {
|
|
867
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
868
|
+
try {
|
|
869
|
+
const report = await auditAgentConfiguration(target);
|
|
870
|
+
process.stdout.write(rest.includes("--json")
|
|
871
|
+
? JSON.stringify(report, null, 2) + "\n"
|
|
872
|
+
: formatAgentSecurityReport(report) + "\n");
|
|
873
|
+
if (report.summary.critical > 0)
|
|
874
|
+
process.exitCode = 2;
|
|
875
|
+
}
|
|
876
|
+
catch (error) {
|
|
877
|
+
process.stderr.write("FORGE agent security audit failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
878
|
+
process.exitCode = 1;
|
|
879
|
+
}
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
case "hooks": {
|
|
883
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
884
|
+
try {
|
|
885
|
+
const hooks = await loadHooks(target);
|
|
886
|
+
process.stdout.write(hooks.length ? hooks.map(hook => hook.id + " [" + hook.event + "] " + hook.action + ": " + hook.message).join("\n") + "\n" : "No lifecycle hooks configured.\n");
|
|
887
|
+
}
|
|
888
|
+
catch (error) {
|
|
889
|
+
process.stderr.write("Could not load hooks: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
890
|
+
process.exitCode = 1;
|
|
891
|
+
}
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
case "memory": {
|
|
895
|
+
const [action = "list", ...memoryArgs] = rest;
|
|
896
|
+
const targetIndex = memoryArgs.findIndex(arg => !arg.startsWith("--"));
|
|
897
|
+
const target = path.resolve(targetIndex >= 0 ? memoryArgs[targetIndex] ?? "." : ".");
|
|
898
|
+
const option = (name) => { const index = memoryArgs.indexOf(name); return index >= 0 ? memoryArgs[index + 1] : undefined; };
|
|
899
|
+
try {
|
|
900
|
+
if (action === "list") {
|
|
901
|
+
const entries = await listProjectMemory(target);
|
|
902
|
+
process.stdout.write(entries.length ? entries.map(entry => `${entry.id} [${entry.kind}] ${entry.content}`).join("\n") + "\n" : "No project memory entries.\n");
|
|
903
|
+
}
|
|
904
|
+
else if (action === "add") {
|
|
905
|
+
const kind = option("--kind");
|
|
906
|
+
const content = option("--content");
|
|
907
|
+
const allowed = ["decision", "architecture", "constraint", "preference", "fact"];
|
|
908
|
+
if (!kind || !allowed.includes(kind) || !content)
|
|
909
|
+
throw new Error("Usage: forge memory add [path] --kind decision|architecture|constraint|preference|fact --content TEXT [--tags a,b]");
|
|
910
|
+
const tags = option("--tags")?.split(",") ?? [];
|
|
911
|
+
const entry = await addProjectMemory(target, { kind, content, tags });
|
|
912
|
+
process.stdout.write(`Saved ${entry.id} to .forge/memory.json\n`);
|
|
913
|
+
}
|
|
914
|
+
else if (action === "remove") {
|
|
915
|
+
const id = memoryArgs.find(arg => arg.startsWith("mem-"));
|
|
916
|
+
if (!id)
|
|
917
|
+
throw new Error("Usage: forge memory remove [path] mem-ID");
|
|
918
|
+
const removed = await removeProjectMemory(target, id);
|
|
919
|
+
process.stdout.write(removed ? `Removed ${id}.\n` : `Memory entry ${id} was not found.\n`);
|
|
920
|
+
}
|
|
921
|
+
else if (action === "context") {
|
|
922
|
+
const query = option("--query") ?? "";
|
|
923
|
+
const context = await buildProjectMemoryContext(target, query);
|
|
924
|
+
process.stdout.write(context.text || "No relevant project memory.\n");
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
throw new Error("Memory actions: add, list, remove, context.");
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
catch (error) {
|
|
931
|
+
process.stderr.write("FORGE memory command failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
932
|
+
process.exitCode = 1;
|
|
933
|
+
}
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
case "plan": {
|
|
937
|
+
const goalIndex = rest.indexOf("--goal");
|
|
938
|
+
const pathIndex = rest.indexOf("--path");
|
|
939
|
+
const goalEnd = pathIndex >= 0 ? pathIndex : rest.length;
|
|
940
|
+
const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd).join(" ").trim() : "";
|
|
941
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
942
|
+
try {
|
|
943
|
+
const plan = createReviewPlan(goal);
|
|
944
|
+
const saved = await saveRun(target, plan);
|
|
945
|
+
process.stdout.write(`${JSON.stringify(plan, null, 2)}\n`);
|
|
946
|
+
process.stdout.write(`\nDraft plan saved to ${path.relative(target, saved)}. No AI provider was called and no files or commands were executed.\n`);
|
|
947
|
+
}
|
|
948
|
+
catch (error) {
|
|
949
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
950
|
+
process.stderr.write(`FORGE could not create a plan: ${message}\n`);
|
|
951
|
+
process.exitCode = 1;
|
|
952
|
+
}
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
case "replay": {
|
|
956
|
+
const runId = rest[0] ?? "";
|
|
957
|
+
const pathIndex = rest.indexOf("--path");
|
|
958
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
959
|
+
try {
|
|
960
|
+
if (!runId)
|
|
961
|
+
throw new Error("Usage: forge replay RUN_ID [--path PROJECT]");
|
|
962
|
+
const source = await loadRun(target, runId);
|
|
963
|
+
const replay = createReplayRun(source);
|
|
964
|
+
const saved = await saveRun(target, replay);
|
|
965
|
+
process.stdout.write("Created replay " + replay.id + " from " + runId + ".\n");
|
|
966
|
+
process.stdout.write("Saved: " + path.relative(target, saved) + "\n");
|
|
967
|
+
process.stdout.write("Run `forge resume " + replay.id + " --path " + target + "` when ready to call the configured provider.\n");
|
|
968
|
+
}
|
|
969
|
+
catch (error) {
|
|
970
|
+
process.stderr.write("Could not create replay: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
971
|
+
process.exitCode = 1;
|
|
972
|
+
}
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
case "approve": {
|
|
976
|
+
const runId = rest[0] ?? "";
|
|
977
|
+
const taskId = rest[1] ?? "";
|
|
978
|
+
const pathIndex = rest.indexOf("--path");
|
|
979
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
980
|
+
const reviewerIndex = rest.indexOf("--reviewer");
|
|
981
|
+
const reasonIndex = rest.indexOf("--reason");
|
|
982
|
+
try {
|
|
983
|
+
if (!runId || !taskId)
|
|
984
|
+
throw new Error("Usage: forge approve RUN_ID TASK_ID [--reviewer NAME] [--reason TEXT] [--path PROJECT]");
|
|
985
|
+
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);
|
|
986
|
+
process.stdout.write("Approved " + taskId + " in " + runId + ". Resume explicitly with `forge resume " + runId + "`.\n");
|
|
987
|
+
process.stdout.write("Run status: " + updated.status + "\n");
|
|
988
|
+
}
|
|
989
|
+
catch (error) {
|
|
990
|
+
process.stderr.write("Could not approve task: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
991
|
+
process.exitCode = 1;
|
|
992
|
+
}
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
case "resume": {
|
|
996
|
+
const runId = rest[0] ?? "";
|
|
997
|
+
const pathIndex = rest.indexOf("--path");
|
|
998
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
999
|
+
try {
|
|
1000
|
+
if (!runId)
|
|
1001
|
+
throw new Error("Usage: forge resume RUN_ID [--path PROJECT]");
|
|
1002
|
+
const permissions = await loadPermissionPolicy(target);
|
|
1003
|
+
const providerChoice = (process.env.FORGE_AI_PROVIDER ?? "openai").toLowerCase();
|
|
1004
|
+
assertSupportedAgentPolicy(permissions, providerChoice, "FORGE resume");
|
|
1005
|
+
await withRunExecutionLock(target, runId, async () => {
|
|
1006
|
+
const savedRun = await loadRun(target, runId);
|
|
1007
|
+
if (savedRun.status === "running") {
|
|
1008
|
+
const recovered = await recoverInterruptedRun(target, runId);
|
|
1009
|
+
process.stdout.write("Recovered interrupted run " + runId + ": " + recovered.status + "\n");
|
|
1010
|
+
process.stdout.write("Any task that was running has an unknown provider outcome and was marked failed. FORGE did not replay provider requests. Review the run before explicitly retrying failed work.\n");
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
1013
|
+
if (savedRun.status !== "pending" && savedRun.status !== "awaiting_approval")
|
|
1014
|
+
throw new Error("Only pending or approval-cleared runs can be resumed. Use `forge replay` only after reviewing failed tasks.");
|
|
1015
|
+
const model = process.env.FORGE_AI_MODEL ?? process.env.FORGE_MODEL;
|
|
1016
|
+
const baseModel = model ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1-mini");
|
|
1017
|
+
const createProvider = (selectedModel) => providerChoice === "claude-code" ? new ClaudeCodeProvider({ cwd: target, model: selectedModel }) : createHostedAgentProvider({ ...process.env, FORGE_AI_PROVIDER: providerChoice }, selectedModel, { workspacePath: target, permissionPolicy: permissions });
|
|
1018
|
+
let priorTraceEntries = [];
|
|
1019
|
+
try {
|
|
1020
|
+
priorTraceEntries = (await loadExecutionTrace(target, runId)).entries;
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
if (!(error && typeof error === "object" && "code" in error && error.code === "ENOENT"))
|
|
1024
|
+
throw error;
|
|
1025
|
+
}
|
|
1026
|
+
const traceAgentUsage = priorTraceEntries.reduce((usage, entry) => { usage[entry.agent] = (usage[entry.agent] ?? 0) + entry.inputTokensEstimate + entry.outputTokensEstimate; return usage; }, {});
|
|
1027
|
+
const traceRunUsage = priorTraceEntries.reduce((total, entry) => total + entry.inputTokensEstimate + entry.outputTokensEstimate, 0);
|
|
1028
|
+
const priorAgentUsage = savedRun.tokenUsage?.estimatedTokensByAgent ?? traceAgentUsage;
|
|
1029
|
+
const priorRunUsage = savedRun.tokenUsage?.estimatedTokensUsed ?? traceRunUsage;
|
|
1030
|
+
const router = new ModelRouter({ fastModel: process.env.FORGE_MODEL_FAST ?? baseModel, balancedModel: process.env.FORGE_MODEL_BALANCED ?? baseModel, reasoningModel: process.env.FORGE_MODEL_REASONING ?? baseModel, ...(process.env.FORGE_FALLBACK_MODEL ? { fallbackModel: process.env.FORGE_FALLBACK_MODEL } : {}), retryAttempts: Number(process.env.FORGE_MODEL_RETRY_ATTEMPTS ?? 2), retryDelayMs: Number(process.env.FORGE_MODEL_RETRY_DELAY_MS ?? 500), ...(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK ? { maxInputTokensPerTask: Number(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK) } : {}), ...(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK ? { maxOutputTokensPerTask: Number(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK) } : {}), ...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN ? { maxEstimatedTokensPerRun: Number(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN) } : {}),
|
|
1031
|
+
...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
|
|
1032
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS ? { maxTaskDurationMs: Number(process.env.FORGE_MAX_TASK_DURATION_MS) } : {}),
|
|
1033
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS_BY_AGENT ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
|
|
1034
|
+
initialEstimatedTokensUsed: priorRunUsage, initialEstimatedTokensByAgent: priorAgentUsage, createProvider });
|
|
1035
|
+
const memory = await buildProjectMemoryContext(target, savedRun.goal);
|
|
1036
|
+
// Re-check current workspace policy before resuming any task that has not completed.
|
|
1037
|
+
// Previously applied hook guidance is kept in the saved prompt and is not appended twice.
|
|
1038
|
+
const resumeHooks = await loadHooks(target);
|
|
1039
|
+
for (const record of savedRun.tasks) {
|
|
1040
|
+
if (record.status !== "pending" && record.status !== "awaiting_approval")
|
|
1041
|
+
continue;
|
|
1042
|
+
const applied = applyBeforeAgentHooks(record.task, resumeHooks);
|
|
1043
|
+
const additions = applied.context.filter(message => !record.task.prompt.includes(message));
|
|
1044
|
+
record.task = {
|
|
1045
|
+
...applied.task,
|
|
1046
|
+
prompt: additions.length ? record.task.prompt + "\n\nHook guidance:\n" + additions.join("\n") : record.task.prompt,
|
|
1047
|
+
};
|
|
1048
|
+
if (applied.blockedReason) {
|
|
1049
|
+
record.status = "blocked";
|
|
1050
|
+
record.error = "Blocked by current policy hook: " + applied.blockedReason;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
const currentTokenUsage = () => {
|
|
1054
|
+
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 } });
|
|
1055
|
+
return usage;
|
|
1056
|
+
};
|
|
1057
|
+
let persistedTraceEntries = 0;
|
|
1058
|
+
const persistRunSnapshot = async (snapshot) => {
|
|
1059
|
+
await saveRun(target, { ...snapshot, tokenUsage: currentTokenUsage() });
|
|
1060
|
+
const newEntries = router.traces.slice(persistedTraceEntries);
|
|
1061
|
+
if (newEntries.length) {
|
|
1062
|
+
await saveExecutionTrace(target, snapshot.id, newEntries);
|
|
1063
|
+
persistedTraceEntries = router.traces.length;
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
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 });
|
|
1067
|
+
const saved = await saveRun(target, { ...completed, tokenUsage: currentTokenUsage() });
|
|
1068
|
+
const tracePath = await saveExecutionTrace(target, completed.id, router.traces.slice(persistedTraceEntries));
|
|
1069
|
+
process.stdout.write("Resumed " + runId + ": " + completed.status + "\n");
|
|
1070
|
+
process.stdout.write("Saved: " + path.relative(target, saved) + "\nTrace: " + path.relative(target, tracePath) + "\n");
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
catch (error) {
|
|
1074
|
+
process.stderr.write("Could not resume run: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1075
|
+
process.exitCode = 1;
|
|
1076
|
+
}
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
case "trace": {
|
|
1080
|
+
const runId = rest[0];
|
|
1081
|
+
const pathIndex = rest.indexOf("--path");
|
|
1082
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1083
|
+
if (!runId) {
|
|
1084
|
+
process.stderr.write("Usage: forge trace RUNID [--path PROJECT]\n");
|
|
1085
|
+
process.exitCode = 1;
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
try {
|
|
1089
|
+
const trace = await loadExecutionTrace(target, runId);
|
|
1090
|
+
process.stdout.write("FORGE execution trace: " + runId + "\n");
|
|
1091
|
+
for (const entry of trace.entries)
|
|
1092
|
+
process.stdout.write(" " + entry.taskId + " [" + entry.tier + "] " + entry.model + " | " + entry.durationMs + " ms | " + entry.outcome + " | ~" + entry.inputTokensEstimate + "/" + entry.outputTokensEstimate + " tokens\n");
|
|
1093
|
+
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");
|
|
1094
|
+
process.stdout.write("Provider health:\n");
|
|
1095
|
+
for (const provider of trace.providers ?? [])
|
|
1096
|
+
process.stdout.write(" " + provider.name + ": " + provider.requests + " requests, " + provider.successes + " successes, " + provider.fallbacks + " fallbacks, " + provider.failures + " failures, " + provider.retries + " retries, " + provider.durationMs + " ms\n");
|
|
1097
|
+
}
|
|
1098
|
+
catch (error) {
|
|
1099
|
+
process.stderr.write("Could not read execution trace: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1100
|
+
process.exitCode = 1;
|
|
1101
|
+
}
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
1104
|
+
case "runs": {
|
|
1105
|
+
if (rest.includes("--help")) {
|
|
1106
|
+
process.stdout.write("Usage: forge runs [path] [--json] [--limit N] [--status STATUS]\nLists valid saved run records. Use --json for the versioned machine-readable summary, --limit N for the newest N runs, and --status to filter by run state.\n");
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const limitIndex = rest.indexOf("--limit");
|
|
1110
|
+
const statusIndex = rest.indexOf("--status");
|
|
1111
|
+
const rawLimit = limitIndex >= 0 ? rest[limitIndex + 1] : undefined;
|
|
1112
|
+
const rawStatus = statusIndex >= 0 ? rest[statusIndex + 1] : undefined;
|
|
1113
|
+
const limit = limitIndex < 0 ? undefined : Number(rawLimit);
|
|
1114
|
+
const positionalPaths = rest.filter((arg, index) => !arg.startsWith("--") &&
|
|
1115
|
+
!(limitIndex >= 0 && index === limitIndex + 1) &&
|
|
1116
|
+
!(statusIndex >= 0 && index === statusIndex + 1));
|
|
1117
|
+
const target = positionalPaths[0] ?? ".";
|
|
1118
|
+
try {
|
|
1119
|
+
if (positionalPaths.length > 1) {
|
|
1120
|
+
throw new Error("Usage: forge runs [path] [--json] [--limit N] [--status STATUS]");
|
|
1121
|
+
}
|
|
1122
|
+
for (const option of ["--json", "--limit", "--status"]) {
|
|
1123
|
+
if (rest.filter(arg => arg === option).length > 1) {
|
|
1124
|
+
throw new Error(`${option} may only be specified once.`);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
const allowedOptions = new Set(["--json", "--limit", "--status"]);
|
|
1128
|
+
const unknownOption = rest.find(arg => arg.startsWith("--") && !allowedOptions.has(arg));
|
|
1129
|
+
if (unknownOption)
|
|
1130
|
+
throw new Error(`Unknown option: ${unknownOption}.`);
|
|
1131
|
+
if (limitIndex >= 0 && (!rawLimit || rawLimit.startsWith("--") || !Number.isSafeInteger(limit) || limit < 1)) {
|
|
1132
|
+
throw new Error("--limit requires a positive safe integer.");
|
|
1133
|
+
}
|
|
1134
|
+
const validStatuses = ["pending", "awaiting_approval", "running", "completed", "failed", "cancelled"];
|
|
1135
|
+
if (statusIndex >= 0 && (!rawStatus || rawStatus.startsWith("--") || !validStatuses.includes(rawStatus))) {
|
|
1136
|
+
throw new Error(`--status requires one of: ${validStatuses.join(", ")}.`);
|
|
1137
|
+
}
|
|
1138
|
+
const allRuns = await listRuns(path.resolve(target));
|
|
1139
|
+
const filteredRuns = rawStatus ? allRuns.filter(run => run.status === rawStatus) : allRuns;
|
|
1140
|
+
const runs = limit === undefined ? filteredRuns : filteredRuns.slice(0, limit);
|
|
1141
|
+
if (rest.includes("--json")) {
|
|
1142
|
+
process.stdout.write(formatRunHistoryJson(runs));
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
process.stdout.write(runs.length ? runs.map(run => `${run.id} ${run.status.padEnd(18)} ${run.goal}`).join("\n") + "\n" : "No saved FORGE runs.\n");
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
catch (error) {
|
|
1149
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1150
|
+
process.stderr.write(`Could not list run history: ${message}\n`);
|
|
1151
|
+
process.exitCode = 1;
|
|
1152
|
+
}
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
case "show-run": {
|
|
1156
|
+
if (rest.includes("--help")) {
|
|
1157
|
+
process.stdout.write("Usage: forge show-run RUN_ID [--path PROJECT]\nDisplays one validated saved run record.\n");
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
const pathIndices = rest.flatMap((arg, index) => arg === "--path" ? [index] : []);
|
|
1161
|
+
const pathIndex = pathIndices[0] ?? -1;
|
|
1162
|
+
const pathValue = pathIndex >= 0 ? rest[pathIndex + 1] : undefined;
|
|
1163
|
+
const positional = rest.filter((arg, index) => !arg.startsWith("--") && !(pathIndex >= 0 && index === pathIndex + 1));
|
|
1164
|
+
const target = pathValue ?? ".";
|
|
1165
|
+
try {
|
|
1166
|
+
if (pathIndices.length > 1)
|
|
1167
|
+
throw new Error("--path may only be specified once.");
|
|
1168
|
+
if (rest.some(arg => arg.startsWith("--") && arg !== "--path")) {
|
|
1169
|
+
throw new Error(`Unknown option: ${rest.find(arg => arg.startsWith("--") && arg !== "--path")}.`);
|
|
1170
|
+
}
|
|
1171
|
+
if (pathIndex >= 0 && (!pathValue || pathValue.startsWith("--"))) {
|
|
1172
|
+
throw new Error("--path requires a project directory.");
|
|
1173
|
+
}
|
|
1174
|
+
if (positional.length !== 1)
|
|
1175
|
+
throw new Error("Usage: forge show-run RUN_ID [--path PROJECT]");
|
|
1176
|
+
const id = positional[0];
|
|
1177
|
+
const run = await loadRun(path.resolve(target), id);
|
|
1178
|
+
process.stdout.write(`${JSON.stringify(run, null, 2)}\n`);
|
|
1179
|
+
}
|
|
1180
|
+
catch (error) {
|
|
1181
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1182
|
+
process.stderr.write(`Could not load run: ${message}\n`);
|
|
1183
|
+
process.exitCode = 1;
|
|
1184
|
+
}
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
case "run": {
|
|
1188
|
+
const goalIndex = rest.indexOf("--goal");
|
|
1189
|
+
const pathIndex = rest.indexOf("--path");
|
|
1190
|
+
const goalEnd = pathIndex >= 0 ? pathIndex : rest.length;
|
|
1191
|
+
const goal = goalIndex >= 0 ? rest.slice(goalIndex + 1, goalEnd).join(" ").trim() : "";
|
|
1192
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1193
|
+
try {
|
|
1194
|
+
const permissions = await loadPermissionPolicy(target);
|
|
1195
|
+
const model = process.env.FORGE_AI_MODEL ?? process.env.FORGE_MODEL;
|
|
1196
|
+
const providerChoice = (process.env.FORGE_AI_PROVIDER ?? "openai").toLowerCase();
|
|
1197
|
+
assertSupportedAgentPolicy(permissions, providerChoice, "FORGE run");
|
|
1198
|
+
const baseModel = model ?? (providerChoice === "claude-code" ? "sonnet" : "gpt-4.1-mini");
|
|
1199
|
+
const apiKey = process.env.FORGE_API_KEY ?? process.env.OPENAI_API_KEY ?? "";
|
|
1200
|
+
const baseUrl = process.env.FORGE_BASE_URL ?? "https://api.openai.com/v1";
|
|
1201
|
+
const createProvider = (selectedModel) => {
|
|
1202
|
+
if (providerChoice === "claude-code")
|
|
1203
|
+
return new ClaudeCodeProvider({ cwd: target, model: selectedModel });
|
|
1204
|
+
if (["openai", "anthropic", "google", "ollama"].includes(providerChoice)) {
|
|
1205
|
+
return createHostedAgentProvider({ ...process.env, FORGE_AI_PROVIDER: providerChoice }, selectedModel, { workspacePath: target, permissionPolicy: permissions });
|
|
1206
|
+
}
|
|
1207
|
+
throw new Error("Unsupported FORGE_AI_PROVIDER. Choose claude-code, openai, anthropic, google or ollama.");
|
|
1208
|
+
};
|
|
1209
|
+
const router = new ModelRouter({
|
|
1210
|
+
fastModel: process.env.FORGE_MODEL_FAST ?? baseModel,
|
|
1211
|
+
balancedModel: process.env.FORGE_MODEL_BALANCED ?? baseModel,
|
|
1212
|
+
reasoningModel: process.env.FORGE_MODEL_REASONING ?? baseModel,
|
|
1213
|
+
...(process.env.FORGE_FALLBACK_MODEL ? { fallbackModel: process.env.FORGE_FALLBACK_MODEL } : {}),
|
|
1214
|
+
retryAttempts: Number(process.env.FORGE_MODEL_RETRY_ATTEMPTS ?? 2),
|
|
1215
|
+
retryDelayMs: Number(process.env.FORGE_MODEL_RETRY_DELAY_MS ?? 500),
|
|
1216
|
+
createProvider,
|
|
1217
|
+
...(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK ? { maxInputTokensPerTask: Number(process.env.FORGE_MAX_INPUT_TOKENS_PER_TASK) } : {}),
|
|
1218
|
+
...(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK ? { maxOutputTokensPerTask: Number(process.env.FORGE_MAX_OUTPUT_TOKENS_PER_TASK) } : {}),
|
|
1219
|
+
...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN ? { maxEstimatedTokensPerRun: Number(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_RUN) } : {}),
|
|
1220
|
+
...(process.env.FORGE_MAX_ESTIMATED_TOKENS_PER_AGENT ? { maxEstimatedTokensPerAgent: readAgentTokenBudgets() } : {}),
|
|
1221
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS ? { maxTaskDurationMs: Number(process.env.FORGE_MAX_TASK_DURATION_MS) } : {}),
|
|
1222
|
+
...(process.env.FORGE_MAX_TASK_DURATION_MS_BY_AGENT ? { maxTaskDurationMsByAgent: readAgentDurationBudgets() } : {}),
|
|
1223
|
+
});
|
|
1224
|
+
const provider = router;
|
|
1225
|
+
const plan = createReviewPlan(goal);
|
|
1226
|
+
const skills = selectSkills(await loadSkillRegistry(target), goal);
|
|
1227
|
+
const skillContext = formatSkillContext(skills);
|
|
1228
|
+
const hooks = await loadHooks(target);
|
|
1229
|
+
for (const record of plan.tasks) {
|
|
1230
|
+
const applied = applyBeforeAgentHooks(record.task, hooks);
|
|
1231
|
+
record.task = { ...applied.task, prompt: applied.context.length ? applied.task.prompt + "\n\nHook guidance:\n" + applied.context.join("\n") : applied.task.prompt };
|
|
1232
|
+
if (applied.blockedReason) {
|
|
1233
|
+
record.status = "blocked";
|
|
1234
|
+
record.error = "Blocked by policy hook: " + applied.blockedReason;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
process.stdout.write("Starting " + plan.id + " with provider " + provider.name + (model ? " (model " + model + ")" : "") + "\n");
|
|
1238
|
+
if (skills.length)
|
|
1239
|
+
process.stdout.write("Selected skills: " + skills.map(skill => skill.id).join(", ") + "\n");
|
|
1240
|
+
if (hooks.length)
|
|
1241
|
+
process.stdout.write("Loaded policy hooks: " + hooks.length + "\n");
|
|
1242
|
+
const memory = await buildProjectMemoryContext(target, goal);
|
|
1243
|
+
const projectContext = [memory.text, skillContext].filter(Boolean).join("\n\n");
|
|
1244
|
+
const completed = await advanceRun(plan, provider, new Set(), { maxConcurrency: 4, projectContext, projectContextForTask: task => buildAgentWorkspaceContext(target, task) });
|
|
1245
|
+
const saved = await saveRun(target, completed);
|
|
1246
|
+
const tracePath = await saveExecutionTrace(target, completed.id, router.traces);
|
|
1247
|
+
process.stdout.write(`Run status: ${completed.status}\nSaved history: ${path.relative(target, saved)}\nExecution trace: ${path.relative(target, tracePath)}\n`);
|
|
1248
|
+
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 });
|
|
1249
|
+
process.stdout.write(`Estimated tokens: ${totals.input} in / ${totals.output} out | Agent time: ${totals.duration} ms | Fallbacks: ${totals.fallback}\n`);
|
|
1250
|
+
for (const task of completed.tasks)
|
|
1251
|
+
process.stdout.write(` ${task.status.padEnd(18)} ${task.task.agent}: ${task.task.title}\n`);
|
|
1252
|
+
if (completed.status === "failed")
|
|
1253
|
+
process.exitCode = 1;
|
|
1254
|
+
}
|
|
1255
|
+
catch (error) {
|
|
1256
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1257
|
+
process.stderr.write(`FORGE run failed: ${message}\n`);
|
|
1258
|
+
process.exitCode = 1;
|
|
1259
|
+
}
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
case "deps": {
|
|
1263
|
+
if (rest.includes("--help")) {
|
|
1264
|
+
process.stdout.write("Usage: forge deps [path] [--audit-report FILE] [--outdated-report FILE] [--json]\n");
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
1268
|
+
const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--audit-report" && rest[index - 1] !== "--outdated-report") ?? ".");
|
|
1269
|
+
try {
|
|
1270
|
+
const auditReport = option("--audit-report"), outdatedReport = option("--outdated-report");
|
|
1271
|
+
const report = await analyzeDependencies(target, { ...(auditReport ? { auditReport } : {}), ...(outdatedReport ? { outdatedReport } : {}) });
|
|
1272
|
+
process.stdout.write(rest.includes("--json") ? JSON.stringify(report, null, 2) + "\n" : formatDependencyIntelligence(report) + "\n");
|
|
1273
|
+
}
|
|
1274
|
+
catch (error) {
|
|
1275
|
+
process.stderr.write("FORGE dependency analysis failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1276
|
+
process.exitCode = 1;
|
|
1277
|
+
}
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
case "snapshot": {
|
|
1281
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1282
|
+
try {
|
|
1283
|
+
const comparison = await captureIntelligenceSnapshot(target);
|
|
1284
|
+
process.stdout.write(formatIntelligenceComparison(comparison) + "\n");
|
|
1285
|
+
}
|
|
1286
|
+
catch (error) {
|
|
1287
|
+
process.stderr.write("FORGE snapshot failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1288
|
+
process.exitCode = 1;
|
|
1289
|
+
}
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
case "timeline": {
|
|
1293
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1294
|
+
try {
|
|
1295
|
+
const comparison = await compareLatestIntelligence(target);
|
|
1296
|
+
process.stdout.write(comparison ? formatIntelligenceComparison(comparison) + "\n" : "No engineering snapshots found. Run forge snapshot . first.\n");
|
|
1297
|
+
}
|
|
1298
|
+
catch (error) {
|
|
1299
|
+
process.stderr.write("FORGE timeline failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1300
|
+
process.exitCode = 1;
|
|
1301
|
+
}
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
case "change-risk": {
|
|
1305
|
+
const pathIndex = rest.indexOf("--path"), baseIndex = rest.indexOf("--base");
|
|
1306
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
|
|
1307
|
+
const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
|
|
1308
|
+
const json = rest.includes("--json");
|
|
1309
|
+
try {
|
|
1310
|
+
const report = await analyzeChangeIntelligence(target, base);
|
|
1311
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatChangeIntelligence(report) + "\n");
|
|
1312
|
+
}
|
|
1313
|
+
catch (error) {
|
|
1314
|
+
process.stderr.write("FORGE change-risk analysis failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1315
|
+
process.exitCode = 1;
|
|
1316
|
+
}
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
case "risk": {
|
|
1320
|
+
const pathIndex = rest.indexOf("--path");
|
|
1321
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1322
|
+
const json = rest.includes("--json");
|
|
1323
|
+
try {
|
|
1324
|
+
const report = await analyzeEngineeringRisk(target);
|
|
1325
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatEngineeringRisk(report) + "\n");
|
|
1326
|
+
}
|
|
1327
|
+
catch (error) {
|
|
1328
|
+
process.stderr.write("FORGE engineering risk analysis failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1329
|
+
process.exitCode = 1;
|
|
1330
|
+
}
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
case "impact": {
|
|
1334
|
+
const pathIndex = rest.indexOf("--path"), baseIndex = rest.indexOf("--base");
|
|
1335
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
|
|
1336
|
+
const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
|
|
1337
|
+
const json = rest.includes("--json");
|
|
1338
|
+
try {
|
|
1339
|
+
const report = await analyzeDependencyImpact(target, base);
|
|
1340
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatDependencyImpact(report) + "\n");
|
|
1341
|
+
}
|
|
1342
|
+
catch (error) {
|
|
1343
|
+
process.stderr.write("FORGE dependency impact analysis failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1344
|
+
process.exitCode = 1;
|
|
1345
|
+
}
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
case "search": {
|
|
1349
|
+
const pathIndex = rest.indexOf("--path"), limitIndex = rest.indexOf("--limit");
|
|
1350
|
+
const optionValues = new Set();
|
|
1351
|
+
if (pathIndex >= 0)
|
|
1352
|
+
optionValues.add(pathIndex + 1);
|
|
1353
|
+
if (limitIndex >= 0)
|
|
1354
|
+
optionValues.add(limitIndex + 1);
|
|
1355
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1356
|
+
const query = rest.filter((arg, index) => !arg.startsWith("--") && !optionValues.has(index)).join(" ").trim();
|
|
1357
|
+
const limit = limitIndex >= 0 ? Number(rest[limitIndex + 1]) : 80;
|
|
1358
|
+
try {
|
|
1359
|
+
const report = await searchWorkspace(target, query, limit);
|
|
1360
|
+
process.stdout.write(JSON.stringify(report, null, 2) + "\n");
|
|
1361
|
+
}
|
|
1362
|
+
catch (error) {
|
|
1363
|
+
process.stderr.write("FORGE code search failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1364
|
+
process.exitCode = 1;
|
|
1365
|
+
}
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
case "dashboard": {
|
|
1369
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1370
|
+
const portIndex = rest.indexOf("--port");
|
|
1371
|
+
const portValue = portIndex >= 0 ? Number(rest[portIndex + 1]) : 4173;
|
|
1372
|
+
if (!Number.isInteger(portValue) || portValue < 0 || portValue > 65535) {
|
|
1373
|
+
process.stderr.write("Dashboard port must be an integer from 0 to 65535.\n");
|
|
1374
|
+
process.exitCode = 1;
|
|
1375
|
+
return;
|
|
1376
|
+
}
|
|
1377
|
+
try {
|
|
1378
|
+
const dashboard = await startDashboard({ projectPath: target, port: portValue });
|
|
1379
|
+
const address = dashboard.server.address();
|
|
1380
|
+
const actualPort = address && typeof address !== "string" ? address.port : portValue;
|
|
1381
|
+
process.stdout.write(`FORGE dashboard: http://127.0.0.1:${actualPort}\nProject: ${target}\nPress Ctrl+C to stop.\n`);
|
|
1382
|
+
const shutdown = () => dashboard.server.close(() => process.exit(0));
|
|
1383
|
+
process.once("SIGINT", shutdown);
|
|
1384
|
+
process.once("SIGTERM", shutdown);
|
|
1385
|
+
}
|
|
1386
|
+
catch (error) {
|
|
1387
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1388
|
+
process.stderr.write(`Could not start dashboard: ${message}\n`);
|
|
1389
|
+
process.exitCode = 1;
|
|
1390
|
+
}
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
case "verify": {
|
|
1394
|
+
const listOnly = rest.includes("--list");
|
|
1395
|
+
const pathIndex = rest.indexOf("--path");
|
|
1396
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1397
|
+
const script = rest.find(arg => !arg.startsWith("--"));
|
|
1398
|
+
try {
|
|
1399
|
+
if (listOnly) {
|
|
1400
|
+
const checks = await discoverChecks(target);
|
|
1401
|
+
process.stdout.write(checks.length ? checks.map(check => `${check.name.padEnd(20)} ${check.command}`).join("\n") + "\n" : "No selectable npm scripts found.\n");
|
|
1402
|
+
return;
|
|
1403
|
+
}
|
|
1404
|
+
if (!script)
|
|
1405
|
+
throw new Error("Choose one script explicitly. Usage: forge verify SCRIPT [--path PROJECT], or use --list to discover scripts.");
|
|
1406
|
+
const result = await runVerification(target, script);
|
|
1407
|
+
const comparison = await compareVerification(target, result);
|
|
1408
|
+
await saveVerification(target, result);
|
|
1409
|
+
process.stdout.write(`${formatVerificationResult(result)}\n\n${formatVerificationComparison(comparison)}\n`);
|
|
1410
|
+
process.stdout.write(`\nSaved verification history: ${path.relative(target, path.join(target, ".forge", "verifications", result.id + ".json"))}\n`);
|
|
1411
|
+
if (result.status !== "passed")
|
|
1412
|
+
process.exitCode = 1;
|
|
1413
|
+
}
|
|
1414
|
+
catch (error) {
|
|
1415
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1416
|
+
process.stderr.write(`FORGE verification failed: ${message}\n`);
|
|
1417
|
+
process.exitCode = 1;
|
|
1418
|
+
}
|
|
1419
|
+
return;
|
|
1420
|
+
}
|
|
1421
|
+
case "sandbox-profiles": {
|
|
1422
|
+
const selected = rest[0];
|
|
1423
|
+
try {
|
|
1424
|
+
const profiles = selected ? [getSandboxSecurityProfile(selected)] : listSandboxSecurityProfiles();
|
|
1425
|
+
for (const profile of profiles) {
|
|
1426
|
+
process.stdout.write([
|
|
1427
|
+
profile.name + " — " + profile.description,
|
|
1428
|
+
" network=" + profile.network + ", readOnlyRoot=" + profile.readOnlyRoot +
|
|
1429
|
+
", dropAllCapabilities=" + profile.dropAllCapabilities +
|
|
1430
|
+
", noNewPrivileges=" + profile.noNewPrivileges,
|
|
1431
|
+
" memory=" + profile.maxMemory + ", cpus=" + profile.maxCpus +
|
|
1432
|
+
", processes=" + profile.maxProcesses +
|
|
1433
|
+
", requireRootless=" + profile.requireRootless,
|
|
1434
|
+
"",
|
|
1435
|
+
].join("\n"));
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
catch (error) {
|
|
1439
|
+
process.stderr.write("FORGE sandbox profiles failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1440
|
+
process.exitCode = 1;
|
|
1441
|
+
}
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
case "sandbox-cleanup-history": {
|
|
1445
|
+
const target = path.resolve(rest[0] ?? ".");
|
|
1446
|
+
try {
|
|
1447
|
+
process.stdout.write(formatSandboxCleanupHistory(await readSandboxCleanupAudit(target)) + "\n");
|
|
1448
|
+
}
|
|
1449
|
+
catch (error) {
|
|
1450
|
+
process.stderr.write("FORGE sandbox cleanup history failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1451
|
+
process.exitCode = 1;
|
|
1452
|
+
}
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
case "sandbox-cleanup": {
|
|
1456
|
+
const apply = rest.includes("--apply");
|
|
1457
|
+
const unknown = rest.filter(arg => arg !== "--apply");
|
|
1458
|
+
try {
|
|
1459
|
+
if (unknown.length)
|
|
1460
|
+
throw new Error("Usage: forge sandbox-cleanup [--apply]. Cleanup is dry-run unless --apply is explicit.");
|
|
1461
|
+
const output = await runSandboxProbe([
|
|
1462
|
+
"ps", "-a", "--filter", "label=com.forge.managed=true",
|
|
1463
|
+
"--format", "{{.ID}}\\t{{.Status}}\\t{{.Names}}",
|
|
1464
|
+
]);
|
|
1465
|
+
const managed = parseManagedContainers(output);
|
|
1466
|
+
const candidates = selectStoppedManagedContainers(managed);
|
|
1467
|
+
const removed = [];
|
|
1468
|
+
if (apply) {
|
|
1469
|
+
for (const container of candidates) {
|
|
1470
|
+
await runSandboxProbe(["rm", container.id]);
|
|
1471
|
+
removed.push(container.id);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
await appendSandboxCleanupAudit(process.cwd(), {
|
|
1475
|
+
id: "cleanup-" + Date.now().toString(36),
|
|
1476
|
+
timestamp: new Date().toISOString(),
|
|
1477
|
+
mode: apply ? "apply" : "dry-run",
|
|
1478
|
+
candidates: candidates.map(({ id, name, status }) => ({ id, name, status })),
|
|
1479
|
+
removed,
|
|
1480
|
+
});
|
|
1481
|
+
process.stdout.write(formatSandboxCleanupReport(candidates, removed, apply) + "\n");
|
|
1482
|
+
}
|
|
1483
|
+
catch (error) {
|
|
1484
|
+
process.stderr.write("FORGE sandbox cleanup failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1485
|
+
process.exitCode = 1;
|
|
1486
|
+
}
|
|
1487
|
+
return;
|
|
1488
|
+
}
|
|
1489
|
+
case "sandbox-policy-check": {
|
|
1490
|
+
const policyPath = rest[0];
|
|
1491
|
+
if (!policyPath) {
|
|
1492
|
+
process.stderr.write("Usage: forge sandbox-policy-check POLICY.json\n");
|
|
1493
|
+
process.exitCode = 1;
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
const result = await loadSandboxPolicy(policyPath);
|
|
1497
|
+
process.stdout.write(formatSandboxPolicyValidation(result) + "\n");
|
|
1498
|
+
if (!result.valid)
|
|
1499
|
+
process.exitCode = 1;
|
|
1500
|
+
return;
|
|
1501
|
+
}
|
|
1502
|
+
case "sandbox-compliance": {
|
|
1503
|
+
const profileName = rest[0] ?? "balanced";
|
|
1504
|
+
const image = rest[1] ?? "";
|
|
1505
|
+
try {
|
|
1506
|
+
const profile = getSandboxSecurityProfile(profileName);
|
|
1507
|
+
const imageDigestPinned = /^.+@sha256:[a-f0-9]{64}$/.test(image);
|
|
1508
|
+
let runtimeAvailable = false;
|
|
1509
|
+
let rootless = null;
|
|
1510
|
+
try {
|
|
1511
|
+
const [version, security] = await Promise.all([
|
|
1512
|
+
runSandboxProbe(["--version"]),
|
|
1513
|
+
runSandboxProbe(["info", "--format", "{{json .SecurityOptions}}"]),
|
|
1514
|
+
]);
|
|
1515
|
+
runtimeAvailable = Boolean(version.trim());
|
|
1516
|
+
rootless = detectDockerRootless(security);
|
|
1517
|
+
}
|
|
1518
|
+
catch {
|
|
1519
|
+
runtimeAvailable = false;
|
|
1520
|
+
}
|
|
1521
|
+
const report = createSandboxComplianceReport(profile, {
|
|
1522
|
+
available: runtimeAvailable,
|
|
1523
|
+
rootless,
|
|
1524
|
+
imageDigestPinned,
|
|
1525
|
+
});
|
|
1526
|
+
process.stdout.write(formatSandboxComplianceReport(report) + "\n");
|
|
1527
|
+
if (report.status === "non-compliant")
|
|
1528
|
+
process.exitCode = 1;
|
|
1529
|
+
}
|
|
1530
|
+
catch (error) {
|
|
1531
|
+
process.stderr.write("FORGE sandbox compliance failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1532
|
+
process.exitCode = 1;
|
|
1533
|
+
}
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
case "sandbox-status": {
|
|
1537
|
+
const runtime = "docker";
|
|
1538
|
+
try {
|
|
1539
|
+
const [version, security] = await Promise.all([
|
|
1540
|
+
runSandboxProbe(["--version"]),
|
|
1541
|
+
runSandboxProbe(["info", "--format", "{{json .SecurityOptions}}"]),
|
|
1542
|
+
]);
|
|
1543
|
+
const report = parseSandboxRuntimeReport(runtime, version, security);
|
|
1544
|
+
process.stdout.write(formatSandboxRuntimeReport(report) + "\n");
|
|
1545
|
+
}
|
|
1546
|
+
catch (error) {
|
|
1547
|
+
const report = parseSandboxRuntimeReport(runtime, "", "");
|
|
1548
|
+
report.warnings.push(error instanceof Error ? error.message : String(error));
|
|
1549
|
+
process.stdout.write(formatSandboxRuntimeReport(report) + "\n");
|
|
1550
|
+
process.exitCode = 1;
|
|
1551
|
+
}
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
case "patch-verify": {
|
|
1555
|
+
const pathIndex = rest.indexOf("--path");
|
|
1556
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1557
|
+
const installDependencies = rest.includes("--install-dependencies");
|
|
1558
|
+
const sandboxImageIndex = rest.indexOf("--sandbox-image");
|
|
1559
|
+
const sandboxImage = sandboxImageIndex >= 0 ? rest[sandboxImageIndex + 1] : undefined;
|
|
1560
|
+
const requireRootless = rest.includes("--require-rootless");
|
|
1561
|
+
const profileIndex = rest.indexOf("--profile");
|
|
1562
|
+
const securityProfile = profileIndex >= 0 ? rest[profileIndex + 1] : undefined;
|
|
1563
|
+
const policyIndex = rest.indexOf("--policy");
|
|
1564
|
+
const sandboxPolicyPath = policyIndex >= 0 ? rest[policyIndex + 1] : undefined;
|
|
1565
|
+
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));
|
|
1566
|
+
const stageId = argsWithoutPath.find(arg => !arg.startsWith("--"));
|
|
1567
|
+
const script = argsWithoutPath.find((arg, index) => !arg.startsWith("--") && index > 0);
|
|
1568
|
+
try {
|
|
1569
|
+
if (!stageId || !script)
|
|
1570
|
+
throw new Error("Usage: forge patch-verify PATCH_STAGE_ID SCRIPT [--install-dependencies] [--sandbox-image IMAGE@sha256:DIGEST] [--require-rootless] [--profile balanced|hardened|rootless-required] [--policy POLICY.json] [--path PROJECT]");
|
|
1571
|
+
if (sandboxImageIndex >= 0 && !sandboxImage)
|
|
1572
|
+
throw new Error("--sandbox-image requires an image pinned by sha256 digest.");
|
|
1573
|
+
if (requireRootless && !sandboxImage)
|
|
1574
|
+
throw new Error("--require-rootless requires --sandbox-image container execution.");
|
|
1575
|
+
if (profileIndex >= 0 && !securityProfile)
|
|
1576
|
+
throw new Error("--profile requires a profile name.");
|
|
1577
|
+
if (securityProfile && !["balanced", "hardened", "rootless-required"].includes(securityProfile))
|
|
1578
|
+
throw new Error("Unknown sandbox security profile: " + securityProfile);
|
|
1579
|
+
if (securityProfile && !sandboxImage)
|
|
1580
|
+
throw new Error("--profile requires --sandbox-image container execution.");
|
|
1581
|
+
if (policyIndex >= 0 && (!sandboxPolicyPath || sandboxPolicyPath.startsWith("--")))
|
|
1582
|
+
throw new Error("--policy requires a policy file path.");
|
|
1583
|
+
if (sandboxPolicyPath && !sandboxImage)
|
|
1584
|
+
throw new Error("--policy requires --sandbox-image container execution.");
|
|
1585
|
+
const result = await verifyStagedPatch(target, stageId, script, {
|
|
1586
|
+
installDependencies,
|
|
1587
|
+
...(sandboxImage ? { sandboxImage } : {}),
|
|
1588
|
+
...(requireRootless ? { requireRootless } : {}),
|
|
1589
|
+
...(securityProfile ? { securityProfile: securityProfile } : {}),
|
|
1590
|
+
...(sandboxPolicyPath ? { sandboxPolicyPath } : {}),
|
|
1591
|
+
});
|
|
1592
|
+
process.stdout.write("# FORGE Staged Patch Verification\n\n" +
|
|
1593
|
+
"- **Stage:** `" + result.stageId + "`\n" +
|
|
1594
|
+
"- **Patch file:** `" + result.file + "`\n" +
|
|
1595
|
+
"- **Staged SHA-256:** `" + result.stagedSha256 + "`\n" +
|
|
1596
|
+
"- **Snapshot files:** `" + result.snapshotFileCount + "`\n" +
|
|
1597
|
+
"- **Snapshot bytes:** `" + result.snapshotBytes + "`\n" +
|
|
1598
|
+
(result.dependencyInstall ? "- **Dependencies:** **" + result.dependencyInstall.status.toUpperCase() + "** (`" + result.dependencyInstall.command + "`)\n" : "") +
|
|
1599
|
+
(sandboxImage ? "- **Execution:** **CONTAINER** (`" + sandboxImage + "`)\n" : "- **Execution:** **LOCAL HOST**\n") +
|
|
1600
|
+
"- **Verification:** **" + result.verification.status.toUpperCase() + "**\n" +
|
|
1601
|
+
"- **Duration:** `" + result.verification.durationMs + " ms`\n\n" +
|
|
1602
|
+
"## Output\n\n```\n" + (result.verification.output || "(No output captured.)") + "\n```\n\n" +
|
|
1603
|
+
"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");
|
|
1604
|
+
if (result.verification.status !== "passed")
|
|
1605
|
+
process.exitCode = 1;
|
|
1606
|
+
}
|
|
1607
|
+
catch (error) {
|
|
1608
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1609
|
+
process.stderr.write("FORGE staged verification failed: " + message + "\n");
|
|
1610
|
+
process.exitCode = 1;
|
|
1611
|
+
}
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
case "verify-history": {
|
|
1615
|
+
const targetIndex = rest.indexOf("--path");
|
|
1616
|
+
const target = path.resolve(targetIndex >= 0 ? rest[targetIndex + 1] ?? "." : ".");
|
|
1617
|
+
try {
|
|
1618
|
+
const history = await listVerifications(target);
|
|
1619
|
+
if (!history.length) {
|
|
1620
|
+
process.stdout.write("No saved verification runs.\n");
|
|
1621
|
+
return;
|
|
1622
|
+
}
|
|
1623
|
+
process.stdout.write(history.map(item => `${item.startedAt} ${item.status.padEnd(12)} ${item.script.padEnd(16)} ${item.durationMs} ms ${item.id}`).join("\n") + "\n");
|
|
1624
|
+
}
|
|
1625
|
+
catch (error) {
|
|
1626
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1627
|
+
process.stderr.write(`Could not load verification history: ${message}\n`);
|
|
1628
|
+
process.exitCode = 1;
|
|
1629
|
+
}
|
|
1630
|
+
return;
|
|
1631
|
+
}
|
|
1632
|
+
case "review": {
|
|
1633
|
+
const json = rest.includes("--json");
|
|
1634
|
+
const all = rest.includes("--all");
|
|
1635
|
+
const pathIndex = rest.indexOf("--path");
|
|
1636
|
+
const baseIndex = rest.indexOf("--base");
|
|
1637
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base" && rest[index - 1] !== "--path") ?? ".");
|
|
1638
|
+
const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
|
|
1639
|
+
try {
|
|
1640
|
+
const report = await reviewProject(target, { base, all });
|
|
1641
|
+
let ai;
|
|
1642
|
+
if (rest.includes("--ai")) {
|
|
1643
|
+
const apiKey = process.env.FORGE_API_KEY ?? process.env.OPENAI_API_KEY ?? "";
|
|
1644
|
+
if (!apiKey)
|
|
1645
|
+
throw new Error("Set FORGE_API_KEY (or OPENAI_API_KEY) before using --ai.");
|
|
1646
|
+
const provider = new OpenAICompatibleProvider({ apiKey, model: process.env.FORGE_MODEL ?? "gpt-4.1-mini", baseUrl: process.env.FORGE_BASE_URL ?? "https://api.openai.com/v1" });
|
|
1647
|
+
ai = await generateAIReview(report, provider);
|
|
1648
|
+
}
|
|
1649
|
+
const saved = await saveReview(target, report, ai);
|
|
1650
|
+
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");
|
|
1651
|
+
}
|
|
1652
|
+
catch (error) {
|
|
1653
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1654
|
+
process.stderr.write(`FORGE code review failed: ${message}\n`);
|
|
1655
|
+
process.exitCode = 1;
|
|
1656
|
+
}
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1659
|
+
case "review-history": {
|
|
1660
|
+
const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? ".");
|
|
1661
|
+
try {
|
|
1662
|
+
const records = await listReviews(target);
|
|
1663
|
+
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");
|
|
1664
|
+
}
|
|
1665
|
+
catch (error) {
|
|
1666
|
+
process.stderr.write("FORGE review history failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1667
|
+
process.exitCode = 1;
|
|
1668
|
+
}
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
case "review-show": {
|
|
1672
|
+
const id = rest.find(arg => !arg.startsWith("--")) ?? "";
|
|
1673
|
+
const pathIndex = rest.indexOf("--path");
|
|
1674
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1675
|
+
try {
|
|
1676
|
+
const record = await loadReview(target, id);
|
|
1677
|
+
process.stdout.write(formatReviewRecord(record) + "\n");
|
|
1678
|
+
}
|
|
1679
|
+
catch (error) {
|
|
1680
|
+
process.stderr.write("FORGE could not load review: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1681
|
+
process.exitCode = 1;
|
|
1682
|
+
}
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
case "review-compare": {
|
|
1686
|
+
const ids = rest.filter((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path");
|
|
1687
|
+
const pathIndex = rest.indexOf("--path");
|
|
1688
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1689
|
+
try {
|
|
1690
|
+
if (ids.length < 2)
|
|
1691
|
+
throw new Error("Usage: forge review-compare REVIEW_ID_A REVIEW_ID_B --path PROJECT");
|
|
1692
|
+
const [a, b] = await Promise.all([loadReview(target, ids[0]), loadReview(target, ids[1])]);
|
|
1693
|
+
const diff = compareReviews(a, b);
|
|
1694
|
+
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");
|
|
1695
|
+
}
|
|
1696
|
+
catch (error) {
|
|
1697
|
+
process.stderr.write("FORGE review comparison failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1698
|
+
process.exitCode = 1;
|
|
1699
|
+
}
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
case "regression": {
|
|
1703
|
+
const pathIndex = rest.indexOf("--path");
|
|
1704
|
+
const baseIndex = rest.indexOf("--base");
|
|
1705
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--base") ?? ".");
|
|
1706
|
+
const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
|
|
1707
|
+
const json = rest.includes("--json");
|
|
1708
|
+
try {
|
|
1709
|
+
const report = await generateRegressionReport(target, base);
|
|
1710
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatRegressionReport(report) + "\n");
|
|
1711
|
+
if (report.summary.regressions > 0)
|
|
1712
|
+
process.exitCode = 1;
|
|
1713
|
+
}
|
|
1714
|
+
catch (error) {
|
|
1715
|
+
process.stderr.write("FORGE regression analysis failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1716
|
+
process.exitCode = 1;
|
|
1717
|
+
}
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
case "patch-review": {
|
|
1721
|
+
const pathIndex = rest.indexOf("--path");
|
|
1722
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1723
|
+
const stageId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
|
|
1724
|
+
try {
|
|
1725
|
+
if (!stageId)
|
|
1726
|
+
throw new Error("Usage: forge patch-review STAGE_ID --path PROJECT");
|
|
1727
|
+
const review = await reviewStagedPatch(target, stageId);
|
|
1728
|
+
process.stdout.write(["# FORGE 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");
|
|
1729
|
+
}
|
|
1730
|
+
catch (error) {
|
|
1731
|
+
process.stderr.write("FORGE staged patch review failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1732
|
+
process.exitCode = 1;
|
|
1733
|
+
}
|
|
1734
|
+
return;
|
|
1735
|
+
}
|
|
1736
|
+
case "patch-stage": {
|
|
1737
|
+
const pathIndex = rest.indexOf("--path");
|
|
1738
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1739
|
+
const approvalId = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
|
|
1740
|
+
try {
|
|
1741
|
+
if (!approvalId)
|
|
1742
|
+
throw new Error("Usage: forge patch-stage APPROVAL_ID --path PROJECT");
|
|
1743
|
+
const staged = await stageApprovedPatch(target, approvalId);
|
|
1744
|
+
process.stdout.write(["# FORGE 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");
|
|
1745
|
+
}
|
|
1746
|
+
catch (error) {
|
|
1747
|
+
process.stderr.write("FORGE patch staging failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1748
|
+
process.exitCode = 1;
|
|
1749
|
+
}
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1752
|
+
case "patch-approve": {
|
|
1753
|
+
const pathIndex = rest.indexOf("--path");
|
|
1754
|
+
const reviewerIndex = rest.indexOf("--reviewer");
|
|
1755
|
+
const reasonIndex = rest.indexOf("--reason");
|
|
1756
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1757
|
+
const values = rest.filter((arg, index) => !arg.startsWith("--") && index !== pathIndex + 1 && index !== reviewerIndex + 1 && index !== reasonIndex + 1);
|
|
1758
|
+
const reviewId = values[0] ?? "";
|
|
1759
|
+
const suggestionNumber = Number(values[1]);
|
|
1760
|
+
const reviewer = reviewerIndex >= 0 ? rest[reviewerIndex + 1] ?? "" : "";
|
|
1761
|
+
const reason = reasonIndex >= 0 ? rest[reasonIndex + 1] : undefined;
|
|
1762
|
+
try {
|
|
1763
|
+
if (!reviewId || !Number.isInteger(suggestionNumber) || suggestionNumber < 1 || !reviewer.trim()) {
|
|
1764
|
+
throw new Error("Usage: forge patch-approve REVIEW_ID SUGGESTION_NUMBER --reviewer NAME [--reason TEXT] --path PROJECT");
|
|
1765
|
+
}
|
|
1766
|
+
const approval = await approveReviewPatch(target, reviewId, suggestionNumber, reviewer, reason);
|
|
1767
|
+
process.stdout.write(["# FORGE 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");
|
|
1768
|
+
}
|
|
1769
|
+
catch (error) {
|
|
1770
|
+
process.stderr.write("FORGE patch approval failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1771
|
+
process.exitCode = 1;
|
|
1772
|
+
}
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1775
|
+
case "patch-approval-check": {
|
|
1776
|
+
const id = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
|
|
1777
|
+
const pathIndex = rest.indexOf("--path");
|
|
1778
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1779
|
+
try {
|
|
1780
|
+
if (!id)
|
|
1781
|
+
throw new Error("Usage: forge patch-approval-check APPROVAL_ID --path PROJECT");
|
|
1782
|
+
const result = await verifyPatchApproval(target, id);
|
|
1783
|
+
process.stdout.write((result.valid ? "VALID: " : "INVALID: ") + result.reason + "\n");
|
|
1784
|
+
if (!result.valid)
|
|
1785
|
+
process.exitCode = 1;
|
|
1786
|
+
}
|
|
1787
|
+
catch (error) {
|
|
1788
|
+
process.stderr.write("FORGE patch approval check failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1789
|
+
process.exitCode = 1;
|
|
1790
|
+
}
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
case "patch-check": {
|
|
1794
|
+
const id = rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--path") ?? "";
|
|
1795
|
+
const pathIndex = rest.indexOf("--path");
|
|
1796
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : ".");
|
|
1797
|
+
const numberArg = rest.find((arg, index) => index > 0 && !arg.startsWith("--") && rest[index - 1] !== id && rest[index - 1] !== "--path");
|
|
1798
|
+
try {
|
|
1799
|
+
if (!id || !numberArg)
|
|
1800
|
+
throw new Error("Usage: forge patch-check REVIEW_ID SUGGESTION_NUMBER --path PROJECT");
|
|
1801
|
+
const record = await loadReview(target, id);
|
|
1802
|
+
const result = await validateReviewPatch(target, record, Number(numberArg));
|
|
1803
|
+
process.stdout.write(formatPatchValidation(result) + "\n");
|
|
1804
|
+
if (result.status !== "applicable")
|
|
1805
|
+
process.exitCode = 1;
|
|
1806
|
+
}
|
|
1807
|
+
catch (error) {
|
|
1808
|
+
process.stderr.write("FORGE patch validation failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1809
|
+
process.exitCode = 1;
|
|
1810
|
+
}
|
|
1811
|
+
return;
|
|
1812
|
+
}
|
|
1813
|
+
case "git": {
|
|
1814
|
+
const json = rest.includes("--json");
|
|
1815
|
+
const pathIndex = rest.indexOf("--path");
|
|
1816
|
+
const baseIndex = rest.indexOf("--base");
|
|
1817
|
+
const target = path.resolve(pathIndex >= 0 ? rest[pathIndex + 1] ?? "." : rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1818
|
+
const base = baseIndex >= 0 ? rest[baseIndex + 1] ?? "HEAD" : "HEAD";
|
|
1819
|
+
try {
|
|
1820
|
+
const report = await analyzeGitChanges(target, base);
|
|
1821
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatGitIntelligence(report) + "\n");
|
|
1822
|
+
}
|
|
1823
|
+
catch (error) {
|
|
1824
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1825
|
+
process.stderr.write(`FORGE Git analysis failed: ${message}\n`);
|
|
1826
|
+
process.exitCode = 1;
|
|
1827
|
+
}
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
case "deep": {
|
|
1831
|
+
const json = rest.includes("--json");
|
|
1832
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1833
|
+
try {
|
|
1834
|
+
const report = await analyzeCodebase(target);
|
|
1835
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatCodebaseAnalysis(report) + "\n");
|
|
1836
|
+
}
|
|
1837
|
+
catch (error) {
|
|
1838
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1839
|
+
process.stderr.write(`FORGE deep analysis failed: ${message}\n`);
|
|
1840
|
+
process.exitCode = 1;
|
|
1841
|
+
}
|
|
1842
|
+
return;
|
|
1843
|
+
}
|
|
1844
|
+
case "remediate": {
|
|
1845
|
+
if (rest.includes("--help")) {
|
|
1846
|
+
process.stdout.write("Usage: forge remediate [path] [--audit-report FILE] [--outdated-report FILE] [--json]\n");
|
|
1847
|
+
return;
|
|
1848
|
+
}
|
|
1849
|
+
const option = (name) => { const index = rest.indexOf(name); return index >= 0 ? rest[index + 1] : undefined; };
|
|
1850
|
+
const target = path.resolve(rest.find((arg, index) => !arg.startsWith("--") && rest[index - 1] !== "--audit-report" && rest[index - 1] !== "--outdated-report") ?? ".");
|
|
1851
|
+
try {
|
|
1852
|
+
const auditReport = option("--audit-report"), outdatedReport = option("--outdated-report");
|
|
1853
|
+
const plan = await generateRemediationPlan(target, { ...(auditReport ? { auditReport } : {}), ...(outdatedReport ? { outdatedReport } : {}) });
|
|
1854
|
+
process.stdout.write(rest.includes("--json") ? JSON.stringify(plan, null, 2) + "\n" : formatRemediationPlan(plan) + "\n");
|
|
1855
|
+
}
|
|
1856
|
+
catch (error) {
|
|
1857
|
+
process.stderr.write("FORGE remediation planning failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1858
|
+
process.exitCode = 1;
|
|
1859
|
+
}
|
|
1860
|
+
return;
|
|
1861
|
+
}
|
|
1862
|
+
case "health": {
|
|
1863
|
+
const json = rest.includes("--json");
|
|
1864
|
+
const target = path.resolve(rest.find(arg => !arg.startsWith("--")) ?? ".");
|
|
1865
|
+
try {
|
|
1866
|
+
const report = await generateProjectHealth(target);
|
|
1867
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatProjectHealthMarkdown(report) + "\n");
|
|
1868
|
+
}
|
|
1869
|
+
catch (error) {
|
|
1870
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1871
|
+
process.stderr.write(`FORGE health report failed: ${message}\n`);
|
|
1872
|
+
process.exitCode = 1;
|
|
1873
|
+
}
|
|
1874
|
+
return;
|
|
1875
|
+
}
|
|
1876
|
+
case "quality": {
|
|
1877
|
+
if (rest.includes("--help")) {
|
|
1878
|
+
process.stdout.write("Usage: forge quality [path] [--json]\n");
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
const json = rest.includes("--json");
|
|
1882
|
+
const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
|
|
1883
|
+
try {
|
|
1884
|
+
const report = await auditProject(path.resolve(target));
|
|
1885
|
+
process.stdout.write(json ? `${JSON.stringify(report, null, 2)}\n` : `${formatQualityAudit(report)}\n`);
|
|
1886
|
+
}
|
|
1887
|
+
catch (error) {
|
|
1888
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1889
|
+
process.stderr.write(`FORGE quality audit failed: ${message}\n`);
|
|
1890
|
+
process.exitCode = 1;
|
|
1891
|
+
}
|
|
1892
|
+
return;
|
|
1893
|
+
}
|
|
1894
|
+
case "inspect":
|
|
1895
|
+
case "map": {
|
|
1896
|
+
if (rest.includes("--help")) {
|
|
1897
|
+
process.stdout.write(`Usage: forge ${command} [path] [--json]\n`);
|
|
1898
|
+
return;
|
|
1899
|
+
}
|
|
1900
|
+
const json = rest.includes("--json");
|
|
1901
|
+
const target = rest.find(arg => !arg.startsWith("--")) ?? ".";
|
|
1902
|
+
try {
|
|
1903
|
+
if (command === "inspect") {
|
|
1904
|
+
const report = await inspectProject(path.resolve(target));
|
|
1905
|
+
process.stdout.write(json ? `${JSON.stringify(report, null, 2)}\n` : `${formatProjectReport(report)}\n`);
|
|
1906
|
+
}
|
|
1907
|
+
else {
|
|
1908
|
+
const report = await mapArchitecture(path.resolve(target));
|
|
1909
|
+
process.stdout.write(json ? `${JSON.stringify(report, null, 2)}\n` : `${formatArchitectureMap(report)}\n`);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
catch (error) {
|
|
1913
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1914
|
+
process.stderr.write(`FORGE could not ${command} this project: ${message}\n`);
|
|
1915
|
+
process.exitCode = 1;
|
|
1916
|
+
}
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
case "doctor": {
|
|
1920
|
+
if (rest.includes("--help")) {
|
|
1921
|
+
process.stdout.write("Usage: forge doctor [path] [--json]\n Run environment health checks.\n");
|
|
1922
|
+
return;
|
|
1923
|
+
}
|
|
1924
|
+
const json = rest.includes("--json");
|
|
1925
|
+
const target = rest.find(arg => !arg.startsWith("--"));
|
|
1926
|
+
try {
|
|
1927
|
+
const { runDoctorChecks, formatDoctorReport } = await import("./core/doctor.js");
|
|
1928
|
+
const report = await runDoctorChecks(target ? path.resolve(target) : undefined);
|
|
1929
|
+
process.stdout.write(json ? JSON.stringify(report, null, 2) + "\n" : formatDoctorReport(report) + "\n");
|
|
1930
|
+
if (report.overallStatus === "critical")
|
|
1931
|
+
process.exitCode = 1;
|
|
1932
|
+
}
|
|
1933
|
+
catch (error) {
|
|
1934
|
+
process.stderr.write("FORGE doctor failed: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
1935
|
+
process.exitCode = 1;
|
|
1936
|
+
}
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
default:
|
|
1940
|
+
process.stderr.write(`Unknown command: ${command}\nRun "forge help" to see available commands.\n`);
|
|
1941
|
+
process.exitCode = 1;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
void main(process.argv.slice(2));
|
|
1945
|
+
//# sourceMappingURL=cli.js.map
|