agentlock-harness 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +211 -2
- package/README.zh-CN.md +211 -0
- package/RELEASE_NOTES.md +35 -0
- package/bin/agentlock-harness.mjs +249 -0
- package/harness-templates/en-US/common/CLAUDE.md.tmpl +7 -0
- package/harness-templates/en-US/common/GEMINI.md.tmpl +7 -0
- package/harness-templates/en-US/common/agentlock-harness.json.tmpl +45 -0
- package/harness-templates/en-US/common/claude-settings.json +36 -0
- package/harness-templates/en-US/common/codex-config.toml.tmpl +11 -0
- package/harness-templates/en-US/common/copilot-instructions.md.tmpl +3 -0
- package/harness-templates/en-US/common/gemini-settings.json +42 -0
- package/harness-templates/en-US/common/git-hooks/pre-commit +24 -0
- package/harness-templates/en-US/common/git-hooks/pre-push +11 -0
- package/harness-templates/en-US/common/harness-gitignore +3 -0
- package/harness-templates/en-US/common/hooks/post-edit-check.mjs +57 -0
- package/harness-templates/en-US/common/hooks/pre-tool-guard.mjs +122 -0
- package/harness-templates/en-US/common/hooks/rules-drift-check.mjs +69 -0
- package/harness-templates/en-US/common/hooks/staged-guard.mjs +197 -0
- package/harness-templates/en-US/common/hooks/stop-gate.mjs +164 -0
- package/harness-templates/en-US/common/rules/00-testing-integrity.md +11 -0
- package/harness-templates/en-US/common/rules/01-security-boundaries.md +11 -0
- package/harness-templates/en-US/common/skills/atomic-tdd/SKILL.md +21 -0
- package/harness-templates/en-US/common/skills/brainstorming/SKILL.md +24 -0
- package/harness-templates/en-US/common/skills/code-grounding/SKILL.md +20 -0
- package/harness-templates/en-US/common/skills/cross-model-review/SKILL.md +22 -0
- package/harness-templates/en-US/common/skills/worktree-isolation/SKILL.md +19 -0
- package/harness-templates/en-US/dual-core/HARNESS_GUIDE.md +19 -0
- package/harness-templates/en-US/dual-core/scripts/cross-review.mjs +141 -0
- package/harness-templates/en-US/enterprise-platform/ci/agentlock-harness-gate.yml +32 -0
- package/harness-templates/en-US/enterprise-platform/governance/mcp-gateway.template.json +18 -0
- package/harness-templates/en-US/enterprise-platform/governance/policy.json +26 -0
- package/harness-templates/en-US/ralph-factory/PROMPT.loop.md.tmpl +12 -0
- package/harness-templates/en-US/ralph-factory/SPRINT_CONTRACT.md.tmpl +25 -0
- package/harness-templates/en-US/ralph-factory/sandbox/Dockerfile.sandbox +13 -0
- package/harness-templates/en-US/ralph-factory/sandbox/docker-compose.sandbox.yml +22 -0
- package/harness-templates/en-US/ralph-factory/sandbox/run-sandbox-loop.sh +8 -0
- package/harness-templates/zh-CN/common/CLAUDE.md.tmpl +7 -0
- package/harness-templates/zh-CN/common/GEMINI.md.tmpl +7 -0
- package/harness-templates/zh-CN/common/agentlock-harness.json.tmpl +45 -0
- package/harness-templates/zh-CN/common/claude-settings.json +36 -0
- package/harness-templates/zh-CN/common/codex-config.toml.tmpl +11 -0
- package/harness-templates/zh-CN/common/copilot-instructions.md.tmpl +3 -0
- package/harness-templates/zh-CN/common/gemini-settings.json +42 -0
- package/harness-templates/zh-CN/common/git-hooks/pre-commit +24 -0
- package/harness-templates/zh-CN/common/git-hooks/pre-push +11 -0
- package/harness-templates/zh-CN/common/harness-gitignore +3 -0
- package/harness-templates/zh-CN/common/hooks/post-edit-check.mjs +57 -0
- package/harness-templates/zh-CN/common/hooks/pre-tool-guard.mjs +122 -0
- package/harness-templates/zh-CN/common/hooks/rules-drift-check.mjs +69 -0
- package/harness-templates/zh-CN/common/hooks/staged-guard.mjs +197 -0
- package/harness-templates/zh-CN/common/hooks/stop-gate.mjs +164 -0
- package/harness-templates/zh-CN/common/rules/00-testing-integrity.md +11 -0
- package/harness-templates/zh-CN/common/rules/01-security-boundaries.md +11 -0
- package/harness-templates/zh-CN/common/skills/atomic-tdd/SKILL.md +21 -0
- package/harness-templates/zh-CN/common/skills/brainstorming/SKILL.md +24 -0
- package/harness-templates/zh-CN/common/skills/code-grounding/SKILL.md +20 -0
- package/harness-templates/zh-CN/common/skills/cross-model-review/SKILL.md +22 -0
- package/harness-templates/zh-CN/common/skills/worktree-isolation/SKILL.md +19 -0
- package/harness-templates/zh-CN/dual-core/HARNESS_GUIDE.md +19 -0
- package/harness-templates/zh-CN/dual-core/scripts/cross-review.mjs +141 -0
- package/harness-templates/zh-CN/enterprise-platform/ci/agentlock-harness-gate.yml +32 -0
- package/harness-templates/zh-CN/enterprise-platform/governance/mcp-gateway.template.json +18 -0
- package/harness-templates/zh-CN/enterprise-platform/governance/policy.json +26 -0
- package/harness-templates/zh-CN/ralph-factory/PROMPT.loop.md.tmpl +12 -0
- package/harness-templates/zh-CN/ralph-factory/SPRINT_CONTRACT.md.tmpl +25 -0
- package/harness-templates/zh-CN/ralph-factory/sandbox/Dockerfile.sandbox +13 -0
- package/harness-templates/zh-CN/ralph-factory/sandbox/docker-compose.sandbox.yml +22 -0
- package/harness-templates/zh-CN/ralph-factory/sandbox/run-sandbox-loop.sh +8 -0
- package/package.json +64 -4
- package/scripts/bootstrap.ps1 +13 -0
- package/scripts/bootstrap.sh +23 -0
- package/src/cli/colors.mjs +42 -0
- package/src/cli/commands/adopt.mjs +378 -0
- package/src/cli/commands/change.mjs +92 -0
- package/src/cli/commands/doctor.mjs +56 -0
- package/src/cli/commands/eval.mjs +193 -0
- package/src/cli/commands/hooks.mjs +97 -0
- package/src/cli/commands/init.mjs +231 -0
- package/src/cli/commands/passthrough.mjs +27 -0
- package/src/cli/commands/ralph.mjs +208 -0
- package/src/cli/commands/rules.mjs +73 -0
- package/src/cli/commands/stats.mjs +71 -0
- package/src/cli/commands/task.mjs +123 -0
- package/src/cli/commands/ui.mjs +84 -0
- package/src/cli/commands/verify.mjs +267 -0
- package/src/cli/prompt.mjs +55 -0
- package/src/core/adopt.mjs +807 -0
- package/src/core/agent-runner.mjs +118 -0
- package/src/core/audit.mjs +181 -0
- package/src/core/catalog.mjs +160 -0
- package/src/core/change-flow.mjs +344 -0
- package/src/core/detector.mjs +141 -0
- package/src/core/doc-drift.mjs +94 -0
- package/src/core/eval-fixtures.mjs +676 -0
- package/src/core/eval-frontier-fixtures.mjs +657 -0
- package/src/core/eval-hard-fixtures.mjs +923 -0
- package/src/core/eval-runner.mjs +754 -0
- package/src/core/eval-telemetry.mjs +338 -0
- package/src/core/harness-writer.mjs +286 -0
- package/src/core/installer.mjs +42 -0
- package/src/core/rules-compiler.mjs +356 -0
- package/src/core/task-ledger.mjs +327 -0
- package/src/core/version.mjs +10 -0
- package/src/ui/public/app.mjs +2199 -0
- package/src/ui/public/index.html +203 -0
- package/src/ui/public/styles.css +1200 -0
- package/src/ui/server.mjs +457 -0
- package/src/ui/service.mjs +860 -0
- package/templates/en-US/AGENTS.md +38 -0
- package/templates/en-US/DOCS_MAINTENANCE.md +65 -0
- package/templates/en-US/MIGRATION.md +46 -0
- package/templates/en-US/NEW_PROJECT_CHECKLIST.md +46 -0
- package/templates/en-US/README.md +41 -0
- package/templates/en-US/SESSION.md +29 -0
- package/templates/en-US/docs/00-context.md +38 -0
- package/templates/en-US/docs/01-vision/README.md +36 -0
- package/templates/en-US/docs/02-requirements/PRD.md +24 -0
- package/templates/en-US/docs/02-requirements/glossary.md +20 -0
- package/templates/en-US/docs/02-requirements/use-cases.md +36 -0
- package/templates/en-US/docs/03-architecture/composite-ai-framework.md +36 -0
- package/templates/en-US/docs/03-architecture/integration-architecture.md +36 -0
- package/templates/en-US/docs/03-architecture/system-architecture.md +16 -0
- package/templates/en-US/docs/04-design/README.md +21 -0
- package/templates/en-US/docs/04-design/a2a-integration-spec.md +36 -0
- package/templates/en-US/docs/04-design/agent-specs.md +36 -0
- package/templates/en-US/docs/04-design/data-model.md +18 -0
- package/templates/en-US/docs/04-design/external-api-spec.md +26 -0
- package/templates/en-US/docs/04-design/mcp-interface-spec.md +36 -0
- package/templates/en-US/docs/04-design/phase1-minimal-implementation-interfaces.md +36 -0
- package/templates/en-US/docs/04-design/responsibility-model.md +36 -0
- package/templates/en-US/docs/04-design/runtime-control-plane-spec.md +36 -0
- package/templates/en-US/docs/04-design/safety-governance.md +18 -0
- package/templates/en-US/docs/04-design/sample-scenarios-architecture-refinement.md +20 -0
- package/templates/en-US/docs/04-design/skill-syntax-spec.md +36 -0
- package/templates/en-US/docs/04-design/storage-schema.md +18 -0
- package/templates/en-US/docs/04-design/task-capability-architecture-and-evolution.md +36 -0
- package/templates/en-US/docs/05-project/adr/ADR-000-template.md +32 -0
- package/templates/en-US/docs/05-project/adr/README.md +15 -0
- package/templates/en-US/docs/05-project/doc-code-map.md +17 -0
- package/templates/en-US/docs/05-project/evolution-backlog.md +14 -0
- package/templates/en-US/docs/05-project/feature-registry.md +16 -0
- package/templates/en-US/docs/05-project/master-delivery-tracker.md +14 -0
- package/templates/en-US/docs/05-project/risk-register.md +36 -0
- package/templates/en-US/docs/05-project/roadmap.md +36 -0
- package/templates/en-US/docs/05-project/stage-view.md +14 -0
- package/templates/en-US/docs/05-project/tech-stack.md +36 -0
- package/templates/en-US/docs/06-dev-guide/SPEC.md +18 -0
- package/templates/en-US/docs/06-dev-guide/coding-standards.md +36 -0
- package/templates/en-US/docs/06-dev-guide/deployment-architecture.md +20 -0
- package/templates/en-US/docs/06-dev-guide/project-scaffold.md +16 -0
- package/templates/en-US/docs/06-dev-guide/testing-strategy.md +42 -0
- package/templates/en-US/docs/07-frontend/README.md +36 -0
- package/templates/en-US/docs/07-frontend/user-personas.md +36 -0
- package/templates/en-US/docs/08-process/README.md +27 -0
- package/templates/en-US/docs/08-process/ai-collaboration-protocol.md +28 -0
- package/templates/en-US/docs/08-process/autopilot.md +18 -0
- package/templates/en-US/docs/08-process/code-grounding.md +32 -0
- package/templates/en-US/docs/08-process/context-loading-strategy.md +35 -0
- package/templates/en-US/docs/08-process/conventions.md +75 -0
- package/templates/en-US/docs/08-process/debugging.md +25 -0
- package/templates/en-US/docs/08-process/design-gate.md +22 -0
- package/templates/en-US/docs/08-process/git-protocol.md +20 -0
- package/templates/en-US/docs/08-process/multi-agent-orchestration.md +27 -0
- package/templates/en-US/docs/08-process/planning.md +18 -0
- package/templates/en-US/docs/08-process/project-intake.md +47 -0
- package/templates/en-US/docs/08-process/reflection.md +18 -0
- package/templates/en-US/docs/08-process/review.md +30 -0
- package/templates/en-US/docs/08-process/session-handoff.md +27 -0
- package/templates/en-US/docs/08-process/tdd.md +24 -0
- package/templates/en-US/docs/08-process/tool-execution.md +32 -0
- package/templates/en-US/docs/08-process/workflow-contract.md +46 -0
- package/templates/en-US/docs/08-process/workflow.md +23 -0
- package/templates/en-US/docs/README.md +58 -0
- package/templates/en-US/docs/design-checklist.md +36 -0
- package/templates/en-US/docs/discussions/README.md +13 -0
- package/templates/en-US/docs/plans/README.md +13 -0
- package/templates/en-US/docs/proposed-design/README.md +13 -0
- package/templates/en-US/docs/temp-reference/README.md +11 -0
- package/templates/en-US/docs.config.json +46 -0
- package/templates/en-US/profiles/README.md +23 -0
- package/templates/en-US/profiles/agent-platform/README.md +23 -0
- package/templates/en-US/profiles/cli-tool/README.md +23 -0
- package/templates/en-US/profiles/library-sdk/README.md +23 -0
- package/templates/en-US/profiles/multi-agent/README.md +23 -0
- package/templates/en-US/profiles/profiles.json +31 -0
- package/templates/en-US/profiles/software-automation/README.md +23 -0
- package/templates/en-US/profiles/web-saas/README.md +23 -0
- package/templates/en-US/scripts/README.md +89 -0
- package/templates/en-US/scripts/doc-find.mjs +156 -0
- package/templates/en-US/scripts/doc-map.mjs +50 -0
- package/templates/en-US/scripts/doc-tools.mjs +311 -0
- package/templates/en-US/scripts/init-docs.mjs +214 -0
- package/templates/en-US/scripts/validate-docs.mjs +141 -0
- package/templates/zh-CN/AGENTS.md +38 -0
- package/templates/zh-CN/DOCS_MAINTENANCE.md +77 -0
- package/templates/zh-CN/MIGRATION.md +45 -0
- package/templates/zh-CN/NEW_PROJECT_CHECKLIST.md +54 -0
- package/templates/zh-CN/README.md +55 -0
- package/templates/zh-CN/SESSION.md +29 -0
- package/templates/zh-CN/docs/00-context.md +42 -0
- package/templates/zh-CN/docs/01-vision/README.md +33 -0
- package/templates/zh-CN/docs/02-requirements/PRD.md +26 -0
- package/templates/zh-CN/docs/02-requirements/glossary.md +35 -0
- package/templates/zh-CN/docs/02-requirements/use-cases.md +31 -0
- package/templates/zh-CN/docs/03-architecture/composite-ai-framework.md +27 -0
- package/templates/zh-CN/docs/03-architecture/integration-architecture.md +30 -0
- package/templates/zh-CN/docs/03-architecture/system-architecture.md +26 -0
- package/templates/zh-CN/docs/04-design/README.md +21 -0
- package/templates/zh-CN/docs/04-design/a2a-integration-spec.md +45 -0
- package/templates/zh-CN/docs/04-design/agent-specs.md +46 -0
- package/templates/zh-CN/docs/04-design/data-model.md +18 -0
- package/templates/zh-CN/docs/04-design/external-api-spec.md +34 -0
- package/templates/zh-CN/docs/04-design/mcp-interface-spec.md +44 -0
- package/templates/zh-CN/docs/04-design/phase1-minimal-implementation-interfaces.md +38 -0
- package/templates/zh-CN/docs/04-design/responsibility-model.md +37 -0
- package/templates/zh-CN/docs/04-design/runtime-control-plane-spec.md +47 -0
- package/templates/zh-CN/docs/04-design/safety-governance.md +18 -0
- package/templates/zh-CN/docs/04-design/sample-scenarios-architecture-refinement.md +38 -0
- package/templates/zh-CN/docs/04-design/skill-syntax-spec.md +43 -0
- package/templates/zh-CN/docs/04-design/storage-schema.md +18 -0
- package/templates/zh-CN/docs/04-design/task-capability-architecture-and-evolution.md +46 -0
- package/templates/zh-CN/docs/05-project/adr/ADR-000-template.md +34 -0
- package/templates/zh-CN/docs/05-project/adr/README.md +35 -0
- package/templates/zh-CN/docs/05-project/doc-code-map.md +19 -0
- package/templates/zh-CN/docs/05-project/evolution-backlog.md +14 -0
- package/templates/zh-CN/docs/05-project/feature-registry.md +16 -0
- package/templates/zh-CN/docs/05-project/master-delivery-tracker.md +14 -0
- package/templates/zh-CN/docs/05-project/risk-register.md +21 -0
- package/templates/zh-CN/docs/05-project/roadmap.md +31 -0
- package/templates/zh-CN/docs/05-project/stage-view.md +14 -0
- package/templates/zh-CN/docs/05-project/tech-stack.md +36 -0
- package/templates/zh-CN/docs/06-dev-guide/SPEC.md +24 -0
- package/templates/zh-CN/docs/06-dev-guide/coding-standards.md +29 -0
- package/templates/zh-CN/docs/06-dev-guide/deployment-architecture.md +22 -0
- package/templates/zh-CN/docs/06-dev-guide/project-scaffold.md +16 -0
- package/templates/zh-CN/docs/06-dev-guide/testing-strategy.md +46 -0
- package/templates/zh-CN/docs/07-frontend/README.md +29 -0
- package/templates/zh-CN/docs/07-frontend/user-personas.md +27 -0
- package/templates/zh-CN/docs/08-process/README.md +29 -0
- package/templates/zh-CN/docs/08-process/ai-collaboration-protocol.md +30 -0
- package/templates/zh-CN/docs/08-process/autopilot.md +18 -0
- package/templates/zh-CN/docs/08-process/code-grounding.md +40 -0
- package/templates/zh-CN/docs/08-process/context-loading-strategy.md +35 -0
- package/templates/zh-CN/docs/08-process/conventions.md +77 -0
- package/templates/zh-CN/docs/08-process/debugging.md +27 -0
- package/templates/zh-CN/docs/08-process/design-gate.md +22 -0
- package/templates/zh-CN/docs/08-process/git-protocol.md +20 -0
- package/templates/zh-CN/docs/08-process/multi-agent-orchestration.md +27 -0
- package/templates/zh-CN/docs/08-process/planning.md +18 -0
- package/templates/zh-CN/docs/08-process/project-intake.md +53 -0
- package/templates/zh-CN/docs/08-process/reflection.md +18 -0
- package/templates/zh-CN/docs/08-process/review.md +30 -0
- package/templates/zh-CN/docs/08-process/session-handoff.md +29 -0
- package/templates/zh-CN/docs/08-process/tdd.md +24 -0
- package/templates/zh-CN/docs/08-process/tool-execution.md +34 -0
- package/templates/zh-CN/docs/08-process/workflow-contract.md +48 -0
- package/templates/zh-CN/docs/08-process/workflow.md +23 -0
- package/templates/zh-CN/docs/README.md +62 -0
- package/templates/zh-CN/docs/design-checklist.md +18 -0
- package/templates/zh-CN/docs/discussions/README.md +36 -0
- package/templates/zh-CN/docs/plans/README.md +15 -0
- package/templates/zh-CN/docs/proposed-design/README.md +41 -0
- package/templates/zh-CN/docs/temp-reference/README.md +13 -0
- package/templates/zh-CN/docs.config.json +46 -0
- package/templates/zh-CN/profiles/README.md +23 -0
- package/templates/zh-CN/profiles/agent-platform/README.md +23 -0
- package/templates/zh-CN/profiles/cli-tool/README.md +23 -0
- package/templates/zh-CN/profiles/library-sdk/README.md +23 -0
- package/templates/zh-CN/profiles/multi-agent/README.md +23 -0
- package/templates/zh-CN/profiles/profiles.json +31 -0
- package/templates/zh-CN/profiles/software-automation/README.md +23 -0
- package/templates/zh-CN/profiles/web-saas/README.md +23 -0
- package/templates/zh-CN/scripts/README.md +97 -0
- package/templates/zh-CN/scripts/doc-find.mjs +156 -0
- package/templates/zh-CN/scripts/doc-map.mjs +50 -0
- package/templates/zh-CN/scripts/doc-tools.mjs +311 -0
- package/templates/zh-CN/scripts/init-docs.mjs +214 -0
- package/templates/zh-CN/scripts/validate-docs.mjs +141 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentLock Harness v3.3 — Multi-CLI Token, Prompt-Cache & Tool-Call Telemetry Parser
|
|
3
|
+
*
|
|
4
|
+
* Normalizes native structured JSON/JSONL outputs across:
|
|
5
|
+
* - Codex CLI (`codex exec --json` JSONL events: `turn.completed.usage`, `item.completed`)
|
|
6
|
+
* - Claude Code (`claude -p --output-format json`: `usage`, `num_turns`, `total_cost_usd`)
|
|
7
|
+
* - Gemini CLI (`gemini -p --output-format json`: `stats.models.*.tokens`, `stats.tools.totalCalls`)
|
|
8
|
+
*
|
|
9
|
+
* Billing Semantics Note:
|
|
10
|
+
* - Codex & Gemini report `input_tokens` / `prompt` INCLUSIVE of cached reads.
|
|
11
|
+
* - Claude Code reports `input_tokens` EXCLUSIVE of `cache_creation_input_tokens` and `cache_read_input_tokens`.
|
|
12
|
+
* All drivers are normalized to a single comparable schema:
|
|
13
|
+
* - inputTokens = total input tokens (uncached + cached reads)
|
|
14
|
+
* - uncachedInputTokens = fresh non-cached input tokens (includes Claude cache creation)
|
|
15
|
+
* - cachedInputTokens = prompt cache read hits
|
|
16
|
+
* - outputTokens = generated output tokens (+ reasoning/thoughts when reported separately)
|
|
17
|
+
* - totalTokens = inputTokens + outputTokens
|
|
18
|
+
* - billableEquivTokens = uncachedInputTokens + outputTokens (high-cost effective tokens)
|
|
19
|
+
* - cacheHitPct = percentage of inputTokens served from prompt cache
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const num = (v) => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : 0);
|
|
23
|
+
|
|
24
|
+
function emptyTelemetry() {
|
|
25
|
+
return {
|
|
26
|
+
available: false,
|
|
27
|
+
inputTokens: 0,
|
|
28
|
+
uncachedInputTokens: 0,
|
|
29
|
+
cachedInputTokens: 0,
|
|
30
|
+
outputTokens: 0,
|
|
31
|
+
totalTokens: 0,
|
|
32
|
+
billableEquivTokens: 0,
|
|
33
|
+
cacheHitPct: 0,
|
|
34
|
+
cacheInvalidations: 0,
|
|
35
|
+
toolCalls: null,
|
|
36
|
+
turns: null,
|
|
37
|
+
costUsd: null,
|
|
38
|
+
extractedError: null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function finalizeTelemetry(t) {
|
|
43
|
+
if (!t.available) return t;
|
|
44
|
+
const inputTokens = Math.round(t.inputTokens);
|
|
45
|
+
const cachedInputTokens = Math.min(inputTokens, Math.round(t.cachedInputTokens));
|
|
46
|
+
const uncachedInputTokens = Math.max(0, Math.round(t.uncachedInputTokens));
|
|
47
|
+
const outputTokens = Math.round(t.outputTokens);
|
|
48
|
+
const totalTokens = Math.max(inputTokens + outputTokens, Math.round(t.totalTokens));
|
|
49
|
+
const billableEquivTokens = uncachedInputTokens + outputTokens;
|
|
50
|
+
const cacheHitPct = inputTokens > 0 ? Math.round((cachedInputTokens / inputTokens) * 100) : 0;
|
|
51
|
+
return {
|
|
52
|
+
available: true,
|
|
53
|
+
inputTokens,
|
|
54
|
+
uncachedInputTokens,
|
|
55
|
+
cachedInputTokens,
|
|
56
|
+
outputTokens,
|
|
57
|
+
totalTokens,
|
|
58
|
+
billableEquivTokens,
|
|
59
|
+
cacheHitPct,
|
|
60
|
+
cacheInvalidations: Math.round(num(t.cacheInvalidations)),
|
|
61
|
+
toolCalls: typeof t.toolCalls === "number" ? Math.round(t.toolCalls) : null,
|
|
62
|
+
turns: typeof t.turns === "number" ? Math.round(t.turns) : null,
|
|
63
|
+
costUsd: typeof t.costUsd === "number" && Number.isFinite(t.costUsd) ? Number(t.costUsd.toFixed(6)) : null,
|
|
64
|
+
extractedError: t.extractedError || null,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function parseJsonLoose(raw) {
|
|
69
|
+
const trimmed = String(raw || "").trim();
|
|
70
|
+
if (!trimmed) return null;
|
|
71
|
+
try {
|
|
72
|
+
return JSON.parse(trimmed);
|
|
73
|
+
} catch {
|
|
74
|
+
// Some CLIs print a banner or log line before/after the JSON payload; extract outermost {...}
|
|
75
|
+
const first = trimmed.indexOf("{");
|
|
76
|
+
const last = trimmed.lastIndexOf("}");
|
|
77
|
+
if (first !== -1 && last > first) {
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(trimmed.slice(first, last + 1));
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function parseCodexTelemetry(stdout) {
|
|
89
|
+
const out = emptyTelemetry();
|
|
90
|
+
const lines = String(stdout || "").split(/\r?\n/);
|
|
91
|
+
|
|
92
|
+
let turnCompletedCount = 0;
|
|
93
|
+
let prevCachedTokens = 0;
|
|
94
|
+
let toolCalls = 0;
|
|
95
|
+
let sawAnyEvent = false;
|
|
96
|
+
let fallbackCumulativeUsage = null;
|
|
97
|
+
|
|
98
|
+
for (const rawLine of lines) {
|
|
99
|
+
const line = rawLine.trim();
|
|
100
|
+
if (!line || !line.startsWith("{")) continue;
|
|
101
|
+
let ev;
|
|
102
|
+
try {
|
|
103
|
+
ev = JSON.parse(line);
|
|
104
|
+
} catch {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (!ev || typeof ev !== "object") continue;
|
|
108
|
+
sawAnyEvent = true;
|
|
109
|
+
|
|
110
|
+
const type = String(ev.type || "");
|
|
111
|
+
if (type === "error" && ev.message) {
|
|
112
|
+
out.extractedError = String(ev.message).slice(0, 300);
|
|
113
|
+
} else if (type === "turn.failed" && (ev.error?.message || ev.message)) {
|
|
114
|
+
out.extractedError = String(ev.error?.message || ev.message).slice(0, 300);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Modern Codex CLI (`codex exec --json`): `turn.completed` carries per-turn `usage`
|
|
118
|
+
if (type === "turn.completed" && ev.usage && typeof ev.usage === "object") {
|
|
119
|
+
turnCompletedCount += 1;
|
|
120
|
+
const u = ev.usage;
|
|
121
|
+
const inp = num(u.input_tokens);
|
|
122
|
+
const cached = num(u.cached_input_tokens ?? u.input_tokens_details?.cached_tokens);
|
|
123
|
+
const outTok = num(u.output_tokens);
|
|
124
|
+
const uncached = Math.max(0, inp - cached);
|
|
125
|
+
|
|
126
|
+
out.available = true;
|
|
127
|
+
out.inputTokens += inp;
|
|
128
|
+
out.cachedInputTokens += cached;
|
|
129
|
+
out.uncachedInputTokens += uncached;
|
|
130
|
+
if (turnCompletedCount > 1 && prevCachedTokens >= 1000 && cached < prevCachedTokens * 0.8) {
|
|
131
|
+
out.cacheInvalidations += 1;
|
|
132
|
+
}
|
|
133
|
+
prevCachedTokens = cached;
|
|
134
|
+
out.outputTokens += outTok;
|
|
135
|
+
out.totalTokens += inp + outTok;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Legacy Codex event format (`token_count` with cumulative `total_token_usage`)
|
|
139
|
+
if (type === "event_msg" && ev.payload?.type === "token_count" && ev.payload?.info?.total_token_usage) {
|
|
140
|
+
fallbackCumulativeUsage = ev.payload.info.total_token_usage;
|
|
141
|
+
} else if (type === "token_count" && ev.info?.total_token_usage) {
|
|
142
|
+
fallbackCumulativeUsage = ev.info.total_token_usage;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Count completed tool / command / file operations
|
|
146
|
+
if (type === "item.completed" && ev.item && typeof ev.item === "object") {
|
|
147
|
+
const itemType = String(ev.item.type || "");
|
|
148
|
+
if (
|
|
149
|
+
itemType === "command_execution" ||
|
|
150
|
+
itemType === "file_change" ||
|
|
151
|
+
itemType === "mcp_tool_call" ||
|
|
152
|
+
itemType === "web_search" ||
|
|
153
|
+
itemType === "function_call" ||
|
|
154
|
+
itemType === "local_shell_call"
|
|
155
|
+
) {
|
|
156
|
+
toolCalls += 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!out.available && fallbackCumulativeUsage) {
|
|
162
|
+
const u = fallbackCumulativeUsage;
|
|
163
|
+
const inp = num(u.input_tokens);
|
|
164
|
+
const cached = num(u.cached_input_tokens);
|
|
165
|
+
const outTok = num(u.output_tokens);
|
|
166
|
+
out.available = true;
|
|
167
|
+
out.inputTokens = inp;
|
|
168
|
+
out.cachedInputTokens = cached;
|
|
169
|
+
out.uncachedInputTokens = Math.max(0, inp - cached);
|
|
170
|
+
out.outputTokens = outTok;
|
|
171
|
+
out.totalTokens = num(u.total_tokens) || inp + outTok;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (sawAnyEvent) {
|
|
175
|
+
out.toolCalls = toolCalls;
|
|
176
|
+
out.turns = turnCompletedCount > 0 ? turnCompletedCount : null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return finalizeTelemetry(out);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function parseClaudeTelemetry(stdout) {
|
|
183
|
+
const out = emptyTelemetry();
|
|
184
|
+
const data = parseJsonLoose(stdout);
|
|
185
|
+
if (!data || typeof data !== "object") return out;
|
|
186
|
+
|
|
187
|
+
// Handle single result object (`--output-format json`)
|
|
188
|
+
const resultObj = Array.isArray(data)
|
|
189
|
+
? [...data].reverse().find((item) => item && item.type === "result") || data[data.length - 1]
|
|
190
|
+
: data;
|
|
191
|
+
|
|
192
|
+
if (!resultObj || typeof resultObj !== "object") return out;
|
|
193
|
+
|
|
194
|
+
if (resultObj.is_error && resultObj.result) {
|
|
195
|
+
out.extractedError = String(resultObj.result).slice(0, 300);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const u = resultObj.usage;
|
|
199
|
+
if (u && typeof u === "object") {
|
|
200
|
+
const freshIn = num(u.input_tokens);
|
|
201
|
+
const cacheCreate = num(u.cache_creation_input_tokens);
|
|
202
|
+
const cacheRead = num(u.cache_read_input_tokens);
|
|
203
|
+
const outTok = num(u.output_tokens);
|
|
204
|
+
const totalIn = freshIn + cacheCreate + cacheRead;
|
|
205
|
+
|
|
206
|
+
out.available = true;
|
|
207
|
+
out.inputTokens = totalIn;
|
|
208
|
+
out.cachedInputTokens = cacheRead;
|
|
209
|
+
out.uncachedInputTokens = freshIn + cacheCreate;
|
|
210
|
+
out.outputTokens = outTok;
|
|
211
|
+
out.totalTokens = totalIn + outTok;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (typeof resultObj.num_turns === "number") out.turns = resultObj.num_turns;
|
|
215
|
+
if (typeof resultObj.total_cost_usd === "number") out.costUsd = resultObj.total_cost_usd;
|
|
216
|
+
|
|
217
|
+
// Count tool_use blocks if full message array was returned
|
|
218
|
+
if (Array.isArray(data)) {
|
|
219
|
+
let calls = 0;
|
|
220
|
+
for (const msg of data) {
|
|
221
|
+
const content = msg?.message?.content;
|
|
222
|
+
if (Array.isArray(content)) {
|
|
223
|
+
for (const block of content) {
|
|
224
|
+
if (block?.type === "tool_use") calls += 1;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
out.toolCalls = calls;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return finalizeTelemetry(out);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function parseGeminiTelemetry(stdout) {
|
|
235
|
+
const out = emptyTelemetry();
|
|
236
|
+
const data = parseJsonLoose(stdout);
|
|
237
|
+
if (!data || typeof data !== "object") return out;
|
|
238
|
+
|
|
239
|
+
if (data.error?.message) {
|
|
240
|
+
out.extractedError = String(data.error.message).slice(0, 300);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const models = data.stats?.models;
|
|
244
|
+
if (models && typeof models === "object") {
|
|
245
|
+
let totalIn = 0;
|
|
246
|
+
let totalCached = 0;
|
|
247
|
+
let totalOut = 0;
|
|
248
|
+
let totalSum = 0;
|
|
249
|
+
let turns = 0;
|
|
250
|
+
let found = false;
|
|
251
|
+
|
|
252
|
+
for (const mStat of Object.values(models)) {
|
|
253
|
+
if (!mStat || typeof mStat !== "object") continue;
|
|
254
|
+
if (mStat.api && typeof mStat.api.totalRequests === "number") {
|
|
255
|
+
turns += mStat.api.totalRequests;
|
|
256
|
+
}
|
|
257
|
+
const tk = mStat.tokens;
|
|
258
|
+
if (!tk || typeof tk !== "object") continue;
|
|
259
|
+
found = true;
|
|
260
|
+
const inp = num(tk.prompt ?? tk.input);
|
|
261
|
+
const cached = num(tk.cached);
|
|
262
|
+
const outTok = num(tk.candidates ?? tk.output ?? tk.response) + num(tk.thoughts);
|
|
263
|
+
const tot = num(tk.total) || inp + outTok;
|
|
264
|
+
|
|
265
|
+
totalIn += inp;
|
|
266
|
+
totalCached += cached;
|
|
267
|
+
totalOut += outTok;
|
|
268
|
+
totalSum += tot;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (found) {
|
|
272
|
+
out.available = true;
|
|
273
|
+
out.inputTokens = totalIn;
|
|
274
|
+
out.cachedInputTokens = Math.min(totalIn, totalCached);
|
|
275
|
+
out.uncachedInputTokens = Math.max(0, totalIn - out.cachedInputTokens);
|
|
276
|
+
out.outputTokens = totalOut;
|
|
277
|
+
out.totalTokens = Math.max(totalIn + totalOut, totalSum);
|
|
278
|
+
out.turns = turns > 0 ? turns : null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (typeof data.stats?.tools?.totalCalls === "number") {
|
|
283
|
+
out.toolCalls = data.stats.tools.totalCalls;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return finalizeTelemetry(out);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function parseDriverTelemetry(agent, stdout) {
|
|
290
|
+
if (agent === "codex") return parseCodexTelemetry(stdout);
|
|
291
|
+
if (agent === "claude") return parseClaudeTelemetry(stdout);
|
|
292
|
+
if (agent === "gemini") return parseGeminiTelemetry(stdout);
|
|
293
|
+
return emptyTelemetry();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function aggregateTelemetry(list) {
|
|
297
|
+
const valid = (list || []).filter((t) => t && t.available);
|
|
298
|
+
if (valid.length === 0) return emptyTelemetry();
|
|
299
|
+
|
|
300
|
+
const sum = emptyTelemetry();
|
|
301
|
+
sum.available = true;
|
|
302
|
+
let hasToolCalls = false;
|
|
303
|
+
let hasTurns = false;
|
|
304
|
+
let hasCost = false;
|
|
305
|
+
|
|
306
|
+
for (const t of valid) {
|
|
307
|
+
sum.inputTokens += num(t.inputTokens);
|
|
308
|
+
sum.uncachedInputTokens += num(t.uncachedInputTokens);
|
|
309
|
+
sum.cachedInputTokens += num(t.cachedInputTokens);
|
|
310
|
+
sum.outputTokens += num(t.outputTokens);
|
|
311
|
+
sum.totalTokens += num(t.totalTokens);
|
|
312
|
+
sum.cacheInvalidations += num(t.cacheInvalidations);
|
|
313
|
+
if (typeof t.toolCalls === "number") {
|
|
314
|
+
hasToolCalls = true;
|
|
315
|
+
sum.toolCalls = (sum.toolCalls || 0) + t.toolCalls;
|
|
316
|
+
}
|
|
317
|
+
if (typeof t.turns === "number") {
|
|
318
|
+
hasTurns = true;
|
|
319
|
+
sum.turns = (sum.turns || 0) + t.turns;
|
|
320
|
+
}
|
|
321
|
+
if (typeof t.costUsd === "number") {
|
|
322
|
+
hasCost = true;
|
|
323
|
+
sum.costUsd = (sum.costUsd || 0) + t.costUsd;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (!hasToolCalls) sum.toolCalls = null;
|
|
328
|
+
if (!hasTurns) sum.turns = null;
|
|
329
|
+
if (!hasCost) sum.costUsd = null;
|
|
330
|
+
return finalizeTelemetry(sum);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function formatTokenCount(n) {
|
|
334
|
+
if (typeof n !== "number" || !Number.isFinite(n)) return "—";
|
|
335
|
+
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`;
|
|
336
|
+
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
|
|
337
|
+
return String(Math.round(n));
|
|
338
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentLock Harness 文件写入器(v2.2 全生态 6 宿主 + 路径级规则源版):
|
|
3
|
+
* 1. 拒绝项目内外符号链接写入(Lexical symlink check)
|
|
4
|
+
* 2. 原子临时文件 + renameSync 落盘,内容未变则跳过写回以保 mtime
|
|
5
|
+
* 3. 双语模板支持(harness-templates/zh-CN & harness-templates/en-US)
|
|
6
|
+
* 4. 6 宿主原生适配:Claude Code / Codex CLI / Gemini CLI & Antigravity / Cursor / GitHub Copilot
|
|
7
|
+
* 5. 内置路径级单一规则源 `.agentlock-harness/rules/*.md` 与通用 Git Hooks 兜底
|
|
8
|
+
*/
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { VALID_HOSTS } from "./catalog.mjs";
|
|
14
|
+
|
|
15
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
export const REPO_ROOT = path.resolve(__dirname, "../..");
|
|
17
|
+
export const HARNESS_TEMPLATES_ROOT = path.join(REPO_ROOT, "harness-templates");
|
|
18
|
+
export const HARNESS_CONFIG_FILE = "agentlock-harness.json";
|
|
19
|
+
export const HARNESS_MANIFEST_FILE = ".agentlock-harness.manifest.json";
|
|
20
|
+
export const HARNESS_DIR = ".agentlock-harness";
|
|
21
|
+
|
|
22
|
+
export const sha256 = (content) => createHash("sha256").update(content).digest("hex");
|
|
23
|
+
|
|
24
|
+
export function renderString(raw, vars) {
|
|
25
|
+
return raw.replace(/\{\{\s*([A-Z0-9_]+)\s*\}\}/g, (_, key) =>
|
|
26
|
+
Object.prototype.hasOwnProperty.call(vars, key) ? String(vars[key]) : `{{${key}}}`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function assertSafeDestination(root, relPath) {
|
|
31
|
+
const absRoot = path.resolve(root);
|
|
32
|
+
const absTarget = path.resolve(absRoot, relPath);
|
|
33
|
+
const rel = path.relative(absRoot, absTarget);
|
|
34
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
35
|
+
throw new Error(`Refusing path outside target root: ${relPath}`);
|
|
36
|
+
}
|
|
37
|
+
let current = absTarget;
|
|
38
|
+
while (current !== path.dirname(absRoot)) {
|
|
39
|
+
const stat = fs.lstatSync(current, { throwIfNoEntry: false });
|
|
40
|
+
if (stat?.isSymbolicLink()) {
|
|
41
|
+
throw new Error(`Refusing symlink destination: ${relPath}`);
|
|
42
|
+
}
|
|
43
|
+
if (current !== absTarget && stat && !stat.isDirectory()) {
|
|
44
|
+
throw new Error(`Target parent is not a directory: ${relPath}`);
|
|
45
|
+
}
|
|
46
|
+
if (current === absRoot) break;
|
|
47
|
+
const parent = path.dirname(current);
|
|
48
|
+
if (parent === current) break;
|
|
49
|
+
current = parent;
|
|
50
|
+
}
|
|
51
|
+
return absTarget;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function atomicWrite(absPath, content, { executable = false } = {}) {
|
|
55
|
+
fs.mkdirSync(path.dirname(absPath), { recursive: true });
|
|
56
|
+
const tmpPath = `${absPath}.alh-${randomUUID()}`;
|
|
57
|
+
try {
|
|
58
|
+
fs.writeFileSync(tmpPath, content, { encoding: "utf-8", flag: "wx" });
|
|
59
|
+
if (executable) {
|
|
60
|
+
try {
|
|
61
|
+
fs.chmodSync(tmpPath, 0o755);
|
|
62
|
+
} catch {
|
|
63
|
+
// Windows no-op
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
fs.renameSync(tmpPath, absPath);
|
|
67
|
+
} finally {
|
|
68
|
+
if (fs.existsSync(tmpPath)) {
|
|
69
|
+
try {
|
|
70
|
+
fs.unlinkSync(tmpPath);
|
|
71
|
+
} catch {
|
|
72
|
+
// ignore cleanup error
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function collectHarnessFiles(modeId, vars, locale = "zh-CN", hosts = VALID_HOSTS, { includeSkills = true, includeStarterRules = true } = {}) {
|
|
79
|
+
const files = new Map();
|
|
80
|
+
if (modeId === "docs-only") return files;
|
|
81
|
+
|
|
82
|
+
const hostSet = new Set(Array.isArray(hosts) && hosts.length ? hosts : VALID_HOSTS);
|
|
83
|
+
const locDir = path.join(HARNESS_TEMPLATES_ROOT, locale === "en-US" ? "en-US" : "zh-CN");
|
|
84
|
+
const addTemplate = (srcRel, destRel, { kind = "managed", executable = false } = {}) => {
|
|
85
|
+
const srcPath = path.join(locDir, srcRel);
|
|
86
|
+
const raw = fs.readFileSync(srcPath, "utf-8");
|
|
87
|
+
const content = srcRel.endsWith(".tmpl") ? renderString(raw, vars) : raw;
|
|
88
|
+
files.set(destRel, { content, kind, executable });
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Core config
|
|
92
|
+
addTemplate("common/agentlock-harness.json.tmpl", HARNESS_CONFIG_FILE, { kind: "merge-config" });
|
|
93
|
+
|
|
94
|
+
// Multi-Host Native Adapters
|
|
95
|
+
if (hostSet.has("claude")) {
|
|
96
|
+
addTemplate("common/CLAUDE.md.tmpl", "CLAUDE.md", { kind: "seed" });
|
|
97
|
+
addTemplate("common/claude-settings.json", ".claude/settings.json", { kind: "seed" });
|
|
98
|
+
}
|
|
99
|
+
if (hostSet.has("codex")) {
|
|
100
|
+
addTemplate("common/codex-config.toml.tmpl", ".codex/config.toml", { kind: "seed" });
|
|
101
|
+
}
|
|
102
|
+
if (hostSet.has("gemini")) {
|
|
103
|
+
addTemplate("common/GEMINI.md.tmpl", "GEMINI.md", { kind: "seed" });
|
|
104
|
+
addTemplate("common/gemini-settings.json", ".gemini/settings.json", { kind: "seed" });
|
|
105
|
+
}
|
|
106
|
+
if (hostSet.has("copilot")) {
|
|
107
|
+
addTemplate("common/copilot-instructions.md.tmpl", ".github/copilot-instructions.md", { kind: "seed" });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Starter Path-Scoped Rules (Single Source of Truth -> compiled by alh rules compile)
|
|
111
|
+
if (includeStarterRules) {
|
|
112
|
+
addTemplate("common/rules/00-testing-integrity.md", `${HARNESS_DIR}/rules/00-testing-integrity.md`, { kind: "seed" });
|
|
113
|
+
addTemplate("common/rules/01-security-boundaries.md", `${HARNESS_DIR}/rules/01-security-boundaries.md`, { kind: "seed" });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// L2 Anti-Rationalization Hardened Skills (opt-out via --no-skills for zero-tax dumbbell mode)
|
|
117
|
+
const skills = includeSkills ? ["brainstorming", "code-grounding", "atomic-tdd", "cross-model-review", "worktree-isolation"] : [];
|
|
118
|
+
for (const s of skills) {
|
|
119
|
+
addTemplate(`common/skills/${s}/SKILL.md`, `.agents/skills/${s}/SKILL.md`, { kind: "managed" });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// L4 Deterministic Hooks & Universal Git Hooks fallback
|
|
123
|
+
addTemplate("common/hooks/pre-tool-guard.mjs", `${HARNESS_DIR}/hooks/pre-tool-guard.mjs`, { kind: "managed", executable: true });
|
|
124
|
+
addTemplate("common/hooks/post-edit-check.mjs", `${HARNESS_DIR}/hooks/post-edit-check.mjs`, { kind: "managed", executable: true });
|
|
125
|
+
addTemplate("common/hooks/stop-gate.mjs", `${HARNESS_DIR}/hooks/stop-gate.mjs`, { kind: "managed", executable: true });
|
|
126
|
+
addTemplate("common/hooks/staged-guard.mjs", `${HARNESS_DIR}/hooks/staged-guard.mjs`, { kind: "managed", executable: true });
|
|
127
|
+
addTemplate("common/hooks/rules-drift-check.mjs", `${HARNESS_DIR}/hooks/rules-drift-check.mjs`, { kind: "managed", executable: true });
|
|
128
|
+
addTemplate("common/git-hooks/pre-commit", `${HARNESS_DIR}/git-hooks/pre-commit`, { kind: "managed", executable: true });
|
|
129
|
+
addTemplate("common/git-hooks/pre-push", `${HARNESS_DIR}/git-hooks/pre-push`, { kind: "managed", executable: true });
|
|
130
|
+
addTemplate("dual-core/scripts/cross-review.mjs", `${HARNESS_DIR}/scripts/cross-review.mjs`, { kind: "managed", executable: true });
|
|
131
|
+
addTemplate("dual-core/HARNESS_GUIDE.md", `${HARNESS_DIR}/HARNESS_GUIDE.md`, { kind: "managed" });
|
|
132
|
+
addTemplate("common/harness-gitignore", `${HARNESS_DIR}/.gitignore`, { kind: "seed" });
|
|
133
|
+
|
|
134
|
+
// Preset 2: Ralph Factory
|
|
135
|
+
if (modeId === "ralph-factory" || modeId === "enterprise-platform") {
|
|
136
|
+
addTemplate("ralph-factory/PROMPT.loop.md.tmpl", `${HARNESS_DIR}/PROMPT.loop.md`, { kind: "seed" });
|
|
137
|
+
addTemplate("ralph-factory/SPRINT_CONTRACT.md.tmpl", `${HARNESS_DIR}/SPRINT_CONTRACT.md`, { kind: "seed" });
|
|
138
|
+
addTemplate("ralph-factory/sandbox/Dockerfile.sandbox", `${HARNESS_DIR}/sandbox/Dockerfile.sandbox`, { kind: "seed" });
|
|
139
|
+
addTemplate("ralph-factory/sandbox/docker-compose.sandbox.yml", `${HARNESS_DIR}/sandbox/docker-compose.sandbox.yml`, { kind: "seed" });
|
|
140
|
+
addTemplate("ralph-factory/sandbox/run-sandbox-loop.sh", `${HARNESS_DIR}/sandbox/run-sandbox-loop.sh`, { kind: "managed", executable: true });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Preset 3: Enterprise Platform
|
|
144
|
+
if (modeId === "enterprise-platform") {
|
|
145
|
+
addTemplate("enterprise-platform/governance/policy.json", `${HARNESS_DIR}/governance/policy.json`, { kind: "seed" });
|
|
146
|
+
addTemplate("enterprise-platform/governance/mcp-gateway.template.json", `${HARNESS_DIR}/governance/mcp-gateway.template.json`, { kind: "seed" });
|
|
147
|
+
addTemplate("enterprise-platform/ci/agentlock-harness-gate.yml", ".github/workflows/agentlock-harness-gate.yml", { kind: "seed" });
|
|
148
|
+
|
|
149
|
+
const lockEntries = skills.map((name) => {
|
|
150
|
+
const rel = `.agents/skills/${name}/SKILL.md`;
|
|
151
|
+
return {
|
|
152
|
+
name,
|
|
153
|
+
path: rel,
|
|
154
|
+
sha256: sha256(files.get(rel).content),
|
|
155
|
+
source: `agentlock-harness-v3.2-${locale}`,
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
const policyText =
|
|
159
|
+
locale === "en-US"
|
|
160
|
+
? "Pin every skill with SHA-256 checksums; reject unverified floating branches to prevent prompt supply-chain poisoning."
|
|
161
|
+
: "所有技能必须锁定 SHA-256 校验和,禁止拉取未核验浮动分支以防提示词供应链投毒。";
|
|
162
|
+
const lockJson =
|
|
163
|
+
JSON.stringify({ lockfileVersion: 1, policy: policyText, skills: lockEntries }, null, 2) + "\n";
|
|
164
|
+
files.set(`${HARNESS_DIR}/governance/skills-lock.json`, { content: lockJson, kind: "managed", executable: false });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return files;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function applyHarnessFiles(
|
|
171
|
+
targetRoot,
|
|
172
|
+
modeId,
|
|
173
|
+
vars,
|
|
174
|
+
{ locale = "zh-CN", hosts = VALID_HOSTS, includeSkills = true, includeStarterRules = true, write = false, update = false, force = false } = {}
|
|
175
|
+
) {
|
|
176
|
+
const manifestPath = path.join(targetRoot, HARNESS_MANIFEST_FILE);
|
|
177
|
+
const prevManifest =
|
|
178
|
+
update && fs.existsSync(manifestPath) ? JSON.parse(fs.readFileSync(manifestPath, "utf-8")) : null;
|
|
179
|
+
|
|
180
|
+
const mergedHosts = [...new Set([...(prevManifest?.hosts || []), ...(hosts || VALID_HOSTS)])];
|
|
181
|
+
const effectiveHosts = update && prevManifest?.hosts ? mergedHosts : hosts || VALID_HOSTS;
|
|
182
|
+
|
|
183
|
+
const desired = collectHarnessFiles(modeId, vars, locale, effectiveHosts, { includeSkills, includeStarterRules });
|
|
184
|
+
const actions = [];
|
|
185
|
+
const conflicts = [];
|
|
186
|
+
const nextFilesManifest = { ...(prevManifest?.files || {}) };
|
|
187
|
+
|
|
188
|
+
for (const [relPath, spec] of desired.entries()) {
|
|
189
|
+
const absPath = assertSafeDestination(targetRoot, relPath);
|
|
190
|
+
const exists = fs.existsSync(absPath);
|
|
191
|
+
|
|
192
|
+
if (!exists) {
|
|
193
|
+
const desiredHash = sha256(spec.content);
|
|
194
|
+
actions.push({ path: relPath, action: "create", kind: spec.kind, content: spec.content, executable: spec.executable, hash: desiredHash });
|
|
195
|
+
nextFilesManifest[relPath] = { kind: spec.kind === "merge-config" ? "seed" : spec.kind, sha256: desiredHash };
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const currentContent = fs.readFileSync(absPath, "utf-8");
|
|
200
|
+
const currentHash = sha256(currentContent);
|
|
201
|
+
|
|
202
|
+
if (spec.kind === "merge-config" && update && !force) {
|
|
203
|
+
try {
|
|
204
|
+
const existingJson = JSON.parse(currentContent);
|
|
205
|
+
const merged = {
|
|
206
|
+
...existingJson,
|
|
207
|
+
schemaVersion: "3.2",
|
|
208
|
+
mode: modeId,
|
|
209
|
+
profile: vars.PROFILE_ID || existingJson.profile,
|
|
210
|
+
locale: locale || existingJson.locale,
|
|
211
|
+
hosts: effectiveHosts,
|
|
212
|
+
};
|
|
213
|
+
const mergedStr = JSON.stringify(merged, null, 2) + "\n";
|
|
214
|
+
const mergedHash = sha256(mergedStr);
|
|
215
|
+
if (mergedHash === currentHash) {
|
|
216
|
+
actions.push({ path: relPath, action: "unchanged", kind: "seed", hash: currentHash });
|
|
217
|
+
} else {
|
|
218
|
+
actions.push({ path: relPath, action: "update", kind: "seed", content: mergedStr, executable: false, hash: mergedHash });
|
|
219
|
+
}
|
|
220
|
+
nextFilesManifest[relPath] = { kind: "seed", sha256: mergedHash };
|
|
221
|
+
continue;
|
|
222
|
+
} catch {
|
|
223
|
+
conflicts.push({ path: relPath, reason: "invalid-existing-json" });
|
|
224
|
+
actions.push({ path: relPath, action: "conflict", kind: "seed" });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const desiredHash = sha256(spec.content);
|
|
230
|
+
if (currentHash === desiredHash) {
|
|
231
|
+
actions.push({ path: relPath, action: "unchanged", kind: spec.kind, hash: desiredHash });
|
|
232
|
+
nextFilesManifest[relPath] = { kind: spec.kind === "merge-config" ? "seed" : spec.kind, sha256: desiredHash };
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if ((spec.kind === "seed" || spec.kind === "merge-config") && !force) {
|
|
237
|
+
actions.push({ path: relPath, action: "keep-seed", kind: "seed", hash: currentHash });
|
|
238
|
+
nextFilesManifest[relPath] = prevManifest?.files?.[relPath] || { kind: "seed", sha256: currentHash };
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const prevEntry = prevManifest?.files?.[relPath];
|
|
243
|
+
if (force || (update && prevEntry && prevEntry.kind === "managed" && prevEntry.sha256 === currentHash)) {
|
|
244
|
+
actions.push({ path: relPath, action: "update", kind: spec.kind, content: spec.content, executable: spec.executable, hash: desiredHash });
|
|
245
|
+
nextFilesManifest[relPath] = { kind: spec.kind, sha256: desiredHash };
|
|
246
|
+
} else {
|
|
247
|
+
conflicts.push({ path: relPath, reason: prevEntry ? "locally-modified-managed-asset" : "untracked-existing-file" });
|
|
248
|
+
actions.push({ path: relPath, action: "conflict", kind: spec.kind });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const writtenFiles = [];
|
|
253
|
+
if (write && conflicts.length === 0 && desired.size > 0) {
|
|
254
|
+
for (const item of actions) {
|
|
255
|
+
if (item.action === "create" || item.action === "update") {
|
|
256
|
+
const absPath = assertSafeDestination(targetRoot, item.path);
|
|
257
|
+
atomicWrite(absPath, item.content, { executable: item.executable });
|
|
258
|
+
writtenFiles.push(item.path);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const manifestObj = {
|
|
262
|
+
version: 2,
|
|
263
|
+
schemaVersion: "3.2",
|
|
264
|
+
locale,
|
|
265
|
+
mode: modeId,
|
|
266
|
+
hosts: effectiveHosts,
|
|
267
|
+
files: Object.fromEntries(Object.entries(nextFilesManifest).sort(([a], [b]) => (a < b ? -1 : 1))),
|
|
268
|
+
};
|
|
269
|
+
const manifestStr = JSON.stringify(manifestObj, null, 2) + "\n";
|
|
270
|
+
const safeManifest = assertSafeDestination(targetRoot, HARNESS_MANIFEST_FILE);
|
|
271
|
+
if (!fs.existsSync(safeManifest) || fs.readFileSync(safeManifest, "utf-8") !== manifestStr) {
|
|
272
|
+
atomicWrite(safeManifest, manifestStr);
|
|
273
|
+
writtenFiles.push(HARNESS_MANIFEST_FILE);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
mode: modeId,
|
|
279
|
+
locale,
|
|
280
|
+
hosts: effectiveHosts,
|
|
281
|
+
write,
|
|
282
|
+
conflicts,
|
|
283
|
+
actions: actions.map(({ content, ...rest }) => rest),
|
|
284
|
+
writtenFiles,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { ok, warn, fail, info } from "../cli/colors.mjs";
|
|
3
|
+
|
|
4
|
+
export function resolveInstallCommand(tool, pkgManagers) {
|
|
5
|
+
let cmd = tool.installCmd;
|
|
6
|
+
if (cmd.startsWith("pnpm add -g ") && !pkgManagers.pnpm && pkgManagers.npm) {
|
|
7
|
+
cmd = cmd.replace("pnpm add -g ", "npm install -g ");
|
|
8
|
+
}
|
|
9
|
+
return cmd;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function installMissingTools(missingTools, pkgManagers, { dryRun = false, promptSession = null } = {}) {
|
|
13
|
+
const receipts = [];
|
|
14
|
+
for (const tool of missingTools) {
|
|
15
|
+
const cmd = resolveInstallCommand(tool, pkgManagers);
|
|
16
|
+
if (dryRun) {
|
|
17
|
+
info(`[DRY-RUN] Would install ${tool.name}: ${cmd}`);
|
|
18
|
+
receipts.push({ id: tool.id, status: "dry-run", cmd });
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (promptSession) {
|
|
23
|
+
const agreed = await promptSession.confirm(`是否自动安装 ${tool.name}?(命令: ${cmd})`, true);
|
|
24
|
+
if (!agreed) {
|
|
25
|
+
warn(`已跳过 ${tool.name},稍后可手动执行: ${cmd}`);
|
|
26
|
+
receipts.push({ id: tool.id, status: "skipped", cmd });
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
info(`正在安装 ${tool.name} ...`);
|
|
32
|
+
const run = spawnSync(cmd, { stdio: "inherit", shell: true });
|
|
33
|
+
if (run.status === 0) {
|
|
34
|
+
ok(`${tool.name} 安装成功`);
|
|
35
|
+
receipts.push({ id: tool.id, status: "installed", cmd });
|
|
36
|
+
} else {
|
|
37
|
+
fail(`${tool.name} 安装失败 (退出码 ${run.status}),请手动执行: ${cmd}`);
|
|
38
|
+
receipts.push({ id: tool.id, status: "failed", cmd });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return receipts;
|
|
42
|
+
}
|