reasonweave 0.0.0-stage → 1.3.2
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/AGENTS.md +101 -0
- package/INSTALL.md +258 -0
- package/README.md +280 -2
- package/bin/lab.mjs +888 -0
- package/constitution.md +55 -0
- package/docs/CONSUMER-PROMPTS.md +474 -0
- package/docs/backlogs/01-forward-wait-self-detach.md +18 -0
- package/docs/backlogs/02-decouple-wait-and-daemon-for-agent-hosts.md +35 -0
- package/docs/backlogs/03-baton-wait-multi-id-lightweight-pubsub.md +34 -0
- package/docs/backlogs/04-adr-cross-repo-automated-dispatch-boundary.md +19 -0
- package/docs/backlogs/05-feat-units-worktree-isolation-engine.md +23 -0
- package/docs/backlogs/06-feat-blind-audit-automated-clean-runner.md +22 -0
- package/docs/backlogs/07-feat-attention-throttle-silent-pipe.md +23 -0
- package/docs/backlogs/08-future-units-shadow-ledger.md +22 -0
- package/docs/backlogs/09-feat-agent-baton-core-convergence-and-resilience.md +42 -0
- package/docs/backlogs/10-feat-anti-orphan-deterministic-wiring-governance.md +35 -0
- package/docs/backlogs/11-perf-orphan-index-fs-read.md +16 -0
- package/docs/backlogs/12-feat-vibe-security-anti-suppression-and-orphan-deps.md +43 -0
- package/docs/backlogs/13-feat-sdd-reppit-tiered-guardrails.md +40 -0
- package/docs/backlogs/14-feat-universal-cli-contract-and-hermetic-test-fixtures.md +48 -0
- package/docs/backlogs/15-feat-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +44 -0
- package/docs/backlogs/16-feat-micro-engineering-skills-and-sdd-baton-fusion.md +52 -0
- package/docs/codegraph-readiness-check.md +49 -0
- package/group/architecture/adr/0001-handoff-async-wait-and-reactive-wakeup.md +87 -0
- package/group/architecture/adr/0002-de-ritualize-handoff-prompt-for-intra-repo.md +53 -0
- package/group/architecture/adr/0003-attention-throttle-and-workspace-driver.md +65 -0
- package/group/architecture/adr/0004-spec-guard-and-drift-prevention.md +90 -0
- package/group/architecture/adr/0005-global-orphan-scavenger-and-read-only-invariants.md +94 -0
- package/group/architecture/adr/0006-triad-engineering-defense-and-mutation-sensitivity.md +68 -0
- package/group/architecture/adr/0007-agent-baton-state-machine-defense-and-workspace-jail-spec.md +86 -0
- package/group/architecture/adr/0008-agent-baton-core-convergence-and-heterogeneous-session-resilience.md +133 -0
- package/group/architecture/adr/0009-anti-orphan-deterministic-wiring-governance-and-federated-guardrails.md +108 -0
- package/group/architecture/adr/0010-agent-baton-identity-security-cryptographic-credentials-and-prompt-anti-confusion.md +119 -0
- package/group/architecture/adr/0011-vibe-security-anti-suppression-and-dependency-reachability.md +161 -0
- package/group/architecture/adr/0012-spec-driven-development-reppit-and-tiered-guardrails.md +130 -0
- package/group/architecture/adr/0013-universal-cli-contract-hermetic-testing-and-defensive-probes.md +110 -0
- package/group/architecture/adr/0014-reasonweave-rebranding-and-doc-code-synchronization-invariant.md +108 -0
- package/group/architecture/adr/0015-micro-engineering-skills-and-dual-core-gate-invariant.md +127 -0
- package/group/architecture/adr/INDEX.md +29 -0
- package/group/architecture/adr/templates/adr-template.md +33 -0
- package/group/architecture/dependency-graph.md +60 -0
- package/group/contracts/npm-packaging.md +69 -0
- package/group/product/intent.md +24 -0
- package/package.json +93 -4
- package/scripts/sync-all-consumers.mjs +174 -0
- package/specs/RFC-20261110-SPEC-GUARD-UNIT.md +353 -0
- package/specs/templates/01-research.md.template +23 -0
- package/specs/templates/02-proposals.md.template +22 -0
- package/specs/templates/03-spec.md.template +33 -0
- package/specs/templates/04-tasks.md.template +29 -0
- package/specs/templates/05-evidence.md.template +29 -0
- package/specs/templates/README.md +26 -0
- package/units/adr/AGENTS.md +85 -0
- package/units/adr/CHANGELOG.md +20 -0
- package/units/adr/README.md +88 -0
- package/units/adr/VERSION.md +10 -0
- package/units/adr/docs/existing-project-prompt.md +24 -0
- package/units/adr/docs/new-project-prompt.md +36 -0
- package/units/adr/scripts/adr-init.sh +101 -0
- package/units/adr/templates/INDEX.md +19 -0
- package/units/adr/templates/adr-frozen.md +33 -0
- package/units/adr/templates/adr-light.md +56 -0
- package/units/adr/templates/adr-standard.md +61 -0
- package/units/backlog/AGENTS.md +53 -0
- package/units/backlog/AUTONOMOUS-GOVERNANCE.md +98 -0
- package/units/backlog/CHANGELOG.md +37 -0
- package/units/backlog/README.md +124 -0
- package/units/backlog/VERSION.md +11 -0
- package/units/backlog/docs/existing-project-ironrules-prompt.md +31 -0
- package/units/backlog/docs/existing-project-prompt.md +24 -0
- package/units/backlog/docs/new-project-prompt.md +19 -0
- package/units/backlog/templates/entry.md +39 -0
- package/units/backlog/templates/project-ironrules.md +36 -0
- package/units/handoffs/AGENT-GUIDE.md +118 -0
- package/units/handoffs/AGENTS.md +28 -0
- package/units/handoffs/CHANGELOG.md +498 -0
- package/units/handoffs/CLAUDE.md +28 -0
- package/units/handoffs/COMMANDS.md +187 -0
- package/units/handoffs/DESIGN.md +74 -0
- package/units/handoffs/README.md +456 -0
- package/units/handoffs/RESPONSE-TEMPLATE.md +138 -0
- package/units/handoffs/VERSION.md +1 -0
- package/units/handoffs/handoff-status.mjs +1017 -0
- package/units/handoffs/handoff.mjs +5675 -0
- package/units/orphan/CHANGELOG.md +118 -0
- package/units/orphan/DESIGN.md +484 -0
- package/units/orphan/README.md +143 -0
- package/units/orphan/VERSION.md +1 -0
- package/units/orphan/adapters/android/index.mjs +140 -0
- package/units/orphan/adapters/python/index.mjs +232 -0
- package/units/orphan/adapters/web/index.mjs +280 -0
- package/units/orphan/bin/lab-orphan.mjs +298 -0
- package/units/orphan/core/blob-reader.mjs +142 -0
- package/units/orphan/core/classifier.mjs +261 -0
- package/units/orphan/core/cleaner.mjs +166 -0
- package/units/orphan/core/diff-checker.mjs +341 -0
- package/units/orphan/core/inverted-index.mjs +124 -0
- package/units/orphan/core/orphan-deps.mjs +192 -0
- package/units/orphan/core/protocol-s1-s3.mjs +194 -0
- package/units/orphan/core/solver.mjs +217 -0
- package/units/orphan/index.mjs +105 -0
- package/units/orphan/templates/ci/orphan-guard.yml +36 -0
- package/units/orphan/templates/pre-commit.d/30-orphan-guard +34 -0
- package/units/orphan/templates/wiring/README.md +51 -0
- package/units/orphan/templates/wiring/miniprogram-route-guard.js.template +80 -0
- package/units/orphan/templates/wiring/pre-commit-node.sh.template +73 -0
- package/units/orphan/templates/wiring/project-agents-gemini-templates.md +60 -0
- package/units/orphan/templates/wiring/python-parity-test.py.template +100 -0
- package/units/orphan/templates/wiring/rust-exhaustive-match.md +65 -0
- package/units/orphan/templates/wiring/swift-periphery.md +56 -0
- package/units/orphan/tests/adapters.test.mjs +243 -0
- package/units/orphan/tests/orphan-deps.test.mjs +131 -0
- package/units/orphan/tests/phase3-gates.test.mjs +135 -0
- package/units/playbook/README.md +106 -0
- package/units/playbook/ZSPEC-AGENT-GUIDE.md +99 -0
- package/units/playbook/docs/01-architecture.md +99 -0
- package/units/playbook/docs/02-bootstrap.md +126 -0
- package/units/playbook/docs/03-units.md +91 -0
- package/units/playbook/docs/04-constitution.md +113 -0
- package/units/playbook/docs/05-adr.md +33 -0
- package/units/playbook/docs/06-spec-kit.md +76 -0
- package/units/playbook/docs/07-backlog.md +30 -0
- package/units/playbook/docs/08-codegraph.md +79 -0
- package/units/playbook/docs/09-skills.md +101 -0
- package/units/playbook/docs/10-handoff.md +72 -0
- package/units/playbook/docs/11-evidence.md +163 -0
- package/units/playbook/docs/12-gates.md +124 -0
- package/units/playbook/docs/13-parallel.md +115 -0
- package/units/playbook/docs/14-dependency-graph.md +86 -0
- package/units/playbook/docs/15-memory.md +153 -0
- package/units/playbook/docs/16-change-control.md +121 -0
- package/units/playbook/docs/17-prompts.md +121 -0
- package/units/playbook/docs/18-shift-left.md +97 -0
- package/units/playbook/docs/19-anti-orphan-vertical-slicing.md +102 -0
- package/units/playbook/docs/20-vibe-security-and-anti-suppression.md +88 -0
- package/units/playbook/docs/21-spec-driven-reppit-closed-loop.md +88 -0
- package/units/playbook/examples/owisel.md +80 -0
- package/units/playbook/references/Agent-Engineering-Framework-AEF-v1.0.md +2033 -0
- package/units/playbook/references/links.md +103 -0
- package/units/playbook/templates/AGENTS.root.md +44 -0
- package/units/playbook/templates/AGENTS.unit.md +52 -0
- package/units/playbook/templates/adr/INDEX.md +17 -0
- package/units/playbook/templates/adr/NNNN-template.md +56 -0
- package/units/playbook/templates/backlogs/entry.md +30 -0
- package/units/playbook/templates/constitution.md +93 -0
- package/units/playbook/templates/evidence/README.md +36 -0
- package/units/playbook/templates/group/architecture/dependency-graph.md +48 -0
- package/units/playbook/templates/group/architecture/system.md +59 -0
- package/units/playbook/templates/group/contracts/api.md +67 -0
- package/units/playbook/templates/group/product/intent.md +30 -0
- package/units/playbook/templates/group/product/requirements.md +48 -0
- package/units/playbook/templates/unit-skeleton/README.md +35 -0
- package/units/preflight/AGENTS.md +45 -0
- package/units/preflight/CHANGELOG.md +55 -0
- package/units/preflight/INSTALL.md +65 -0
- package/units/preflight/README.md +104 -0
- package/units/preflight/VERSION.md +3 -0
- package/units/preflight/docs/audit-requirements.md +114 -0
- package/units/preflight/docs/existing-project-prompt.md +25 -0
- package/units/preflight/docs/new-project-prompt.md +25 -0
- package/units/preflight/docs/transfers/CONSUMER-INSTALL.md +40 -0
- package/units/preflight/docs/transfers/CONSUMER-SELFCHECK.md +51 -0
- package/units/preflight/docs/transfers/README.md +39 -0
- package/units/preflight/docs/transfers/prospero.md +26 -0
- package/units/preflight/templates/check.sh +43 -0
- package/units/preflight/templates/pre-commit +55 -0
- package/units/preflight/templates/pre-commit.d/20-preflight +15 -0
- package/units/preflight/templates/preflight.conf.example +21 -0
- package/units/preflight/templates/preflight.sh +189 -0
- package/units/preflight/templates/wrap-up.md +9 -0
- package/units/review-gate/AGENTS.md +42 -0
- package/units/review-gate/CHANGELOG.md +58 -0
- package/units/review-gate/README.md +118 -0
- package/units/review-gate/VERSION.md +3 -0
- package/units/review-gate/clean-runner.mjs +350 -0
- package/units/review-gate/docs/existing-project-prompt.md +28 -0
- package/units/review-gate/docs/new-project-prompt.md +38 -0
- package/units/review-gate/docs/transfers/iggle.md +34 -0
- package/units/review-gate/docs/transfers/prospero.md +35 -0
- package/units/review-gate/docs/transfers/swoop.md +36 -0
- package/units/review-gate/docs/transfers/yiqiyihuihua.md +35 -0
- package/units/review-gate/templates/.semgrepignore +37 -0
- package/units/review-gate/templates/pre-commit +130 -0
- package/units/review-gate/templates/scan.sh +158 -0
- package/units/skills/AGENTS.md +17 -0
- package/units/skills/CHANGELOG.md +18 -0
- package/units/skills/README.md +63 -0
- package/units/skills/VERSION.md +1 -0
- package/units/skills/bin/review-package.mjs +99 -0
- package/units/skills/bin/task-brief.mjs +102 -0
- package/units/skills/catalog/brainstorming/SKILL.md +75 -0
- package/units/skills/catalog/brainstorming/references/design-doc-template.md +28 -0
- package/units/skills/catalog/systematic-debugging/SKILL.md +101 -0
- package/units/skills/catalog/systematic-debugging/references/root-cause-tracing.md +23 -0
- package/units/skills/catalog/test-driven-development/SKILL.md +88 -0
- package/units/skills/catalog/test-driven-development/references/writing-good-tests.md +13 -0
- package/units/skills/catalog/verification-before-completion/SKILL.md +73 -0
- package/units/skills/skills.mjs +181 -0
- package/units/skills/tests/pipeline.test.mjs +131 -0
- package/units/skills/tests/skill-contract.test.mjs +67 -0
- package/units/skills/tests/skills-cli.test.mjs +107 -0
- package/units/spec-guard/AGENTS.md +43 -0
- package/units/spec-guard/CHANGELOG.md +88 -0
- package/units/spec-guard/README.md +148 -0
- package/units/spec-guard/VERSION.md +1 -0
- package/units/spec-guard/bin/spec-guard.mjs +431 -0
- package/units/spec-guard/core/adr-parser.mjs +540 -0
- package/units/spec-guard/core/matrix-checker.mjs +587 -0
- package/units/spec-guard/core/mutation-runner.mjs +267 -0
- package/units/spec-guard/core/scanner.mjs +333 -0
- package/units/spec-guard/core/telemetry_authenticator.mjs +484 -0
- package/units/spec-guard/core/wiring_tracer.mjs +306 -0
- package/units/spec-guard/templates/annotations/ContractOnly.kt +19 -0
- package/units/spec-guard/templates/annotations/Invariant.kt +16 -0
- package/units/spec-guard/templates/annotations/invariant.go +19 -0
- package/units/spec-guard/templates/annotations/invariant.py +38 -0
- package/units/spec-guard/templates/annotations/invariant.ts +39 -0
- package/units/spec-guard/templates/ci/spec-guard-audit.yml +23 -0
- package/units/spec-guard/templates/pre-commit.d/15-spec-guard +72 -0
- package/units/spec-guard/templates/spec-matrix.overlay.yml +70 -0
- package/units/spec-guard/tests/spec-guard.test.mjs +716 -0
- package/units/spec-guard/tests/telemetry_authenticator.test.mjs +633 -0
- package/units/worktree/AGENTS.md +22 -0
- package/units/worktree/CHANGELOG.md +14 -0
- package/units/worktree/README.md +65 -0
- package/units/worktree/VERSION.md +1 -0
- package/units/worktree/worktree.mjs +352 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.md +322 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# units/worktree — Agent 物理隔离调度引擎
|
|
2
|
+
|
|
3
|
+
**版本**:v0.1.0
|
|
4
|
+
**定位**:多 Agent 并发物理隔离与环境沙盒的基础设施引擎。
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. 产生背景
|
|
9
|
+
|
|
10
|
+
在多 Agent 协同体系中,多工单并行开发或审计若直接在同一个主目录中修改,会导致严重的冲突:
|
|
11
|
+
- **Git 索引冲突**:不同会话互相覆盖暂存区;
|
|
12
|
+
- **撤销污染**:一个会话执行 `git checkout .` 或 `git clean` 会意外抹去另一个会话的代码;
|
|
13
|
+
- **上下文幻觉**:Agent 误读其他并发会话产生的半成品文件。
|
|
14
|
+
|
|
15
|
+
`units/worktree` 基于 Git 原生 `git worktree` 机制,为每个任务分配物理级别隔离的工作目录,彻底解决主目录冲突问题。
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 2. 核心特性
|
|
20
|
+
|
|
21
|
+
1. **零配置极速分配(Allocate)**:
|
|
22
|
+
- 自动在 `.worktrees/<ticket-id>` 创建独立物理检出;
|
|
23
|
+
- 自动在项目的 `.gitignore` 中追加 `.worktrees/`,保障主分支整洁;
|
|
24
|
+
- 分配独立的临时分支 `ticket/<ticket-id>`。
|
|
25
|
+
2. **依赖共享软链接(Smart Cache Linker)**:
|
|
26
|
+
- 自动检测并共享 Node (`node_modules`)、Rust (`target`)、Python (`.venv`);
|
|
27
|
+
- 杜绝多目录重复全量下载与编译依赖,节省大量磁盘空间与初始化时间。
|
|
28
|
+
3. **安全闭环回收(Prune)**:
|
|
29
|
+
- 工单验收合并后,一键安全卸载工作目录与分支;
|
|
30
|
+
- 未提交改动保护:防止误删代码,需显式 `--force`。
|
|
31
|
+
4. **Handoff 微内核驱动对接**:
|
|
32
|
+
- 遵循 ADR-0003,作为标准 Workspace Provider 实现,可供 Baton 调度引擎调用。
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 3. CLI 命令指南
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# 1. 为工单分配隔离工作区
|
|
40
|
+
baton-worktree allocate 05-feat-login
|
|
41
|
+
|
|
42
|
+
# 2. 列出当前活跃的隔离区
|
|
43
|
+
baton-worktree list
|
|
44
|
+
|
|
45
|
+
# 3. 查看特定隔离区内的 Git 状态
|
|
46
|
+
baton-worktree status 05-feat-login
|
|
47
|
+
|
|
48
|
+
# 4. 任务完成安全回收
|
|
49
|
+
baton-worktree prune 05-feat-login
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 4. 目录结构约定
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
<project-root>/
|
|
58
|
+
├── .gitignore # 自动声明 .worktrees/
|
|
59
|
+
├── .worktrees/ # 物理隔离目录(Git 忽略)
|
|
60
|
+
│ ├── ticket-101/ # 工单 101 的独立工作树 (分支: ticket/ticket-101)
|
|
61
|
+
│ │ ├── node_modules -> ../../node_modules (软链共享)
|
|
62
|
+
│ │ └── ...
|
|
63
|
+
│ └── ticket-102/ # 工单 102 的独立工作树
|
|
64
|
+
└── ... (主工作区)
|
|
65
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v0.1.0 — 2026-11-09
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* baton-worktree / units/worktree
|
|
4
|
+
* 基于 Git Worktree 的物理目录隔离调度引擎
|
|
5
|
+
* AEF / Lab Agent Engineering Toolkit
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { execSync } from 'node:child_process';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
|
|
13
|
+
const SCRIPT_ROOT = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
const WORKTREE_VERSION = (() => {
|
|
15
|
+
try {
|
|
16
|
+
const vPath = path.join(SCRIPT_ROOT, 'VERSION.md');
|
|
17
|
+
if (fs.existsSync(vPath)) {
|
|
18
|
+
const line = fs.readFileSync(vPath, 'utf8').trim().split('\n')[0];
|
|
19
|
+
const m = line.match(/v?(\d+\.\d+\.\d+)/);
|
|
20
|
+
if (m) return m[1];
|
|
21
|
+
}
|
|
22
|
+
} catch {}
|
|
23
|
+
return '0.1.0';
|
|
24
|
+
})();
|
|
25
|
+
|
|
26
|
+
const args = process.argv.slice(2);
|
|
27
|
+
const command = args[0] || 'help';
|
|
28
|
+
|
|
29
|
+
function runCmd(cmd, options = {}) {
|
|
30
|
+
try {
|
|
31
|
+
return execSync(cmd, {
|
|
32
|
+
encoding: 'utf-8',
|
|
33
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
34
|
+
...options,
|
|
35
|
+
}).trim();
|
|
36
|
+
} catch (err) {
|
|
37
|
+
if (options.ignoreError) return null;
|
|
38
|
+
const stderr = err.stderr ? err.stderr.toString() : '';
|
|
39
|
+
throw new Error(`Command failed: ${cmd}\n${stderr || err.message}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function getRepoRoot() {
|
|
44
|
+
const root = runCmd('git rev-parse --show-toplevel', { ignoreError: true });
|
|
45
|
+
if (!root) {
|
|
46
|
+
console.error('❌ 错误: 当前目录不在 Git 仓库管理下,无法使用 worktree 隔离。');
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
return path.resolve(root);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sanitizeId(id) {
|
|
53
|
+
if (!id) return '';
|
|
54
|
+
return String(id).trim().replace(/[^a-zA-Z0-9._-]/g, '-');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function ensureGitignore(repoRoot) {
|
|
58
|
+
const gitignorePath = path.join(repoRoot, '.gitignore');
|
|
59
|
+
const entry = '.worktrees/';
|
|
60
|
+
if (!fs.existsSync(gitignorePath)) {
|
|
61
|
+
fs.writeFileSync(gitignorePath, `# Baton / Lab Worktrees\n${entry}\n`, 'utf-8');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const content = fs.readFileSync(gitignorePath, 'utf-8');
|
|
65
|
+
const lines = content.split('\n').map((l) => l.trim());
|
|
66
|
+
if (!lines.includes('.worktrees/') && !lines.includes('.worktrees')) {
|
|
67
|
+
const appendContent = content.endsWith('\n')
|
|
68
|
+
? `\n# Baton / Lab Worktrees\n${entry}\n`
|
|
69
|
+
: `\n\n# Baton / Lab Worktrees\n${entry}\n`;
|
|
70
|
+
fs.appendFileSync(gitignorePath, appendContent, 'utf-8');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function setupSharedCaches(repoRoot, targetPath) {
|
|
75
|
+
const sharedDirs = ['node_modules', 'target', '.venv'];
|
|
76
|
+
const mounted = [];
|
|
77
|
+
|
|
78
|
+
for (const dir of sharedDirs) {
|
|
79
|
+
const srcPath = path.join(repoRoot, dir);
|
|
80
|
+
const destPath = path.join(targetPath, dir);
|
|
81
|
+
|
|
82
|
+
if (fs.existsSync(srcPath) && !fs.existsSync(destPath)) {
|
|
83
|
+
try {
|
|
84
|
+
fs.symlinkSync(srcPath, destPath, 'dir');
|
|
85
|
+
mounted.push(dir);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
// 软链接创建失败时降级忽略,不阻断主流程
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return mounted;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function allocate(ticketId, options = {}) {
|
|
95
|
+
const cleanId = sanitizeId(ticketId);
|
|
96
|
+
if (!cleanId) {
|
|
97
|
+
console.error('❌ 错误: 必须指定工单 ID (ticket-id)。');
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const repoRoot = getRepoRoot();
|
|
102
|
+
ensureGitignore(repoRoot);
|
|
103
|
+
|
|
104
|
+
const worktreesDir = path.join(repoRoot, '.worktrees');
|
|
105
|
+
if (!fs.existsSync(worktreesDir)) {
|
|
106
|
+
fs.mkdirSync(worktreesDir, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const targetPath = path.join(worktreesDir, cleanId);
|
|
110
|
+
const branchName = `ticket/${cleanId}`;
|
|
111
|
+
const baseBranch = options.base || 'HEAD';
|
|
112
|
+
|
|
113
|
+
// 1. 检查是否已经存在于 git worktree 清单
|
|
114
|
+
const worktreeList = runCmd('git worktree list --porcelain', { ignoreError: true }) || '';
|
|
115
|
+
const isAlreadyAllocated = worktreeList.includes(`worktree ${targetPath}`);
|
|
116
|
+
|
|
117
|
+
if (isAlreadyAllocated && fs.existsSync(targetPath)) {
|
|
118
|
+
const mounted = setupSharedCaches(repoRoot, targetPath);
|
|
119
|
+
if (options.json) {
|
|
120
|
+
console.log(JSON.stringify({ status: 'existing', path: targetPath, branch: branchName, mounted }));
|
|
121
|
+
} else {
|
|
122
|
+
console.log(`ℹ️ 工单隔离区已存在: \x1b[36m${targetPath}\x1b[0m (分支: ${branchName})`);
|
|
123
|
+
}
|
|
124
|
+
return targetPath;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 2. 检查分支是否已存在
|
|
128
|
+
const branchExists = runCmd(`git rev-parse --verify refs/heads/${branchName}`, { ignoreError: true });
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
if (branchExists) {
|
|
132
|
+
runCmd(`git worktree add "${targetPath}" "${branchName}"`);
|
|
133
|
+
} else {
|
|
134
|
+
runCmd(`git worktree add -b "${branchName}" "${targetPath}" "${baseBranch}"`);
|
|
135
|
+
}
|
|
136
|
+
} catch (err) {
|
|
137
|
+
console.error(`❌ 分配 Worktree 失败: ${err.message}`);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 3. 挂载共享依赖缓存
|
|
142
|
+
const mounted = setupSharedCaches(repoRoot, targetPath);
|
|
143
|
+
|
|
144
|
+
if (options.json) {
|
|
145
|
+
console.log(JSON.stringify({ status: 'allocated', path: targetPath, branch: branchName, mounted }));
|
|
146
|
+
} else {
|
|
147
|
+
console.log(`\n✅ 成功分配物理隔离工作区:`);
|
|
148
|
+
console.log(` • 路径: \x1b[32m${targetPath}\x1b[0m`);
|
|
149
|
+
console.log(` • 分支: \x1b[36m${branchName}\x1b[0m (基于 ${baseBranch})`);
|
|
150
|
+
if (mounted.length > 0) {
|
|
151
|
+
console.log(` • 依赖缓存共享软链: ${mounted.join(', ')}`);
|
|
152
|
+
}
|
|
153
|
+
console.log(`\n提示: 针对该工单的修改应在上述隔离目录内执行。\n`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return targetPath;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function prune(ticketId, options = {}) {
|
|
160
|
+
const cleanId = sanitizeId(ticketId);
|
|
161
|
+
if (!cleanId) {
|
|
162
|
+
console.error('❌ 错误: 必须指定工单 ID (ticket-id)。');
|
|
163
|
+
process.exit(1);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const repoRoot = getRepoRoot();
|
|
167
|
+
const targetPath = path.join(repoRoot, '.worktrees', cleanId);
|
|
168
|
+
const branchName = `ticket/${cleanId}`;
|
|
169
|
+
|
|
170
|
+
// 1. 检查是否存在该 worktree
|
|
171
|
+
const worktreeList = runCmd('git worktree list --porcelain', { ignoreError: true }) || '';
|
|
172
|
+
const existsInList = worktreeList.includes(`worktree ${targetPath}`);
|
|
173
|
+
|
|
174
|
+
if (existsInList || fs.existsSync(targetPath)) {
|
|
175
|
+
const forceFlag = options.force ? '--force' : '';
|
|
176
|
+
try {
|
|
177
|
+
runCmd(`git worktree remove ${forceFlag} "${targetPath}"`);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (options.force) {
|
|
180
|
+
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
181
|
+
runCmd('git worktree prune', { ignoreError: true });
|
|
182
|
+
} else {
|
|
183
|
+
console.error(`❌ 回收失败 (存在未提交改动): ${err.message}\n若确认丢弃,请加 --force 强制回收。`);
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 2. 清理分支
|
|
190
|
+
const branchExists = runCmd(`git rev-parse --verify refs/heads/${branchName}`, { ignoreError: true });
|
|
191
|
+
if (branchExists) {
|
|
192
|
+
const delFlag = options.force ? '-D' : '-d';
|
|
193
|
+
try {
|
|
194
|
+
runCmd(`git branch ${delFlag} "${branchName}"`);
|
|
195
|
+
} catch {
|
|
196
|
+
// 若无法安全删除分支且未加 force,提示用户
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
runCmd('git worktree prune', { ignoreError: true });
|
|
201
|
+
|
|
202
|
+
if (options.json) {
|
|
203
|
+
console.log(JSON.stringify({ status: 'pruned', ticketId: cleanId }));
|
|
204
|
+
} else {
|
|
205
|
+
console.log(`\n🧹 已成功回收工单 [${cleanId}] 的隔离工作区与临时分支。`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function list(options = {}) {
|
|
210
|
+
const repoRoot = getRepoRoot();
|
|
211
|
+
const rawList = runCmd('git worktree list --porcelain', { ignoreError: true }) || '';
|
|
212
|
+
const entries = [];
|
|
213
|
+
const blocks = rawList.split('\n\n').filter(Boolean);
|
|
214
|
+
|
|
215
|
+
const worktreeBase = path.join(repoRoot, '.worktrees');
|
|
216
|
+
|
|
217
|
+
for (const block of blocks) {
|
|
218
|
+
const lines = block.split('\n');
|
|
219
|
+
let wtPath = '';
|
|
220
|
+
let head = '';
|
|
221
|
+
let branch = '';
|
|
222
|
+
|
|
223
|
+
for (const line of lines) {
|
|
224
|
+
if (line.startsWith('worktree ')) wtPath = line.replace('worktree ', '').trim();
|
|
225
|
+
if (line.startsWith('HEAD ')) head = line.replace('HEAD ', '').trim();
|
|
226
|
+
if (line.startsWith('branch ')) branch = line.replace('branch ', '').trim();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (wtPath.startsWith(worktreeBase)) {
|
|
230
|
+
const ticketId = path.basename(wtPath);
|
|
231
|
+
entries.push({ ticketId, path: wtPath, head: head.substring(0, 7), branch });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (options.json) {
|
|
236
|
+
console.log(JSON.stringify(entries, null, 2));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
console.log(`\n📋 当前活动 Agent 隔离工作区 (${entries.length} 个):`);
|
|
241
|
+
if (entries.length === 0) {
|
|
242
|
+
console.log(' (暂无活动的隔离工作区)\n');
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
console.log('─'.repeat(70));
|
|
247
|
+
for (const e of entries) {
|
|
248
|
+
console.log(`• 工单 ID: \x1b[36m${e.ticketId.padEnd(16)}\x1b[0m 分支: ${e.branch || 'detached'} (${e.head})`);
|
|
249
|
+
console.log(` 物理路径: ${e.path}`);
|
|
250
|
+
}
|
|
251
|
+
console.log('─'.repeat(70) + '\n');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function status(ticketId) {
|
|
255
|
+
const cleanId = sanitizeId(ticketId);
|
|
256
|
+
const repoRoot = getRepoRoot();
|
|
257
|
+
const targetPath = path.join(repoRoot, '.worktrees', cleanId);
|
|
258
|
+
|
|
259
|
+
if (!fs.existsSync(targetPath)) {
|
|
260
|
+
console.error(`❌ 未找到工单 [${cleanId}] 的隔离目录: ${targetPath}`);
|
|
261
|
+
process.exit(1);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
console.log(`\n🔍 工单 [${cleanId}] 隔离区改动状态 (${targetPath}):`);
|
|
265
|
+
const statusOut = runCmd(`git -C "${targetPath}" status --short`);
|
|
266
|
+
if (!statusOut) {
|
|
267
|
+
console.log(' (工作目录干净,无未提交改动)\n');
|
|
268
|
+
} else {
|
|
269
|
+
console.log(statusOut + '\n');
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function cleanAll(options = {}) {
|
|
274
|
+
const repoRoot = getRepoRoot();
|
|
275
|
+
const worktreeBase = path.join(repoRoot, '.worktrees');
|
|
276
|
+
if (!fs.existsSync(worktreeBase)) {
|
|
277
|
+
console.log('ℹ️ 无隔离目录需要清理。');
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const items = fs.readdirSync(worktreeBase);
|
|
282
|
+
console.log(`🧹 正在扫描并清理 ${items.length} 个工作区...`);
|
|
283
|
+
for (const item of items) {
|
|
284
|
+
prune(item, options);
|
|
285
|
+
}
|
|
286
|
+
console.log('✅ 全部清理完毕。\n');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function showHelp() {
|
|
290
|
+
console.log(`
|
|
291
|
+
Baton Worktree CLI — Agent 物理隔离调度引擎 (units/worktree v${WORKTREE_VERSION})
|
|
292
|
+
|
|
293
|
+
用法:
|
|
294
|
+
baton-worktree allocate <ticket-id> [--base <branch>] [--json]
|
|
295
|
+
为指定工单分配独立的物理工作区 (.worktrees/<ticket-id>),
|
|
296
|
+
自动维护 .gitignore 并挂载依赖缓存软链。
|
|
297
|
+
|
|
298
|
+
baton-worktree prune <ticket-id> [--force] [--json]
|
|
299
|
+
安全回收指定工单的隔离工作区与关联分支。
|
|
300
|
+
|
|
301
|
+
baton-worktree list [--json]
|
|
302
|
+
列出当前仓库所有由 Agent 隔离的工作区。
|
|
303
|
+
|
|
304
|
+
baton-worktree status <ticket-id>
|
|
305
|
+
检视指定隔离区内的代码变更状态。
|
|
306
|
+
|
|
307
|
+
baton-worktree clean-all [--force]
|
|
308
|
+
清理当前仓库所有的 Agent 隔离区。
|
|
309
|
+
|
|
310
|
+
baton-worktree help
|
|
311
|
+
显示帮助信息。
|
|
312
|
+
`);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// CLI 解析分发
|
|
316
|
+
const isJson = args.includes('--json');
|
|
317
|
+
const isForce = args.includes('--force');
|
|
318
|
+
const baseIndex = args.indexOf('--base');
|
|
319
|
+
const baseBranch = baseIndex !== -1 ? args[baseIndex + 1] : undefined;
|
|
320
|
+
|
|
321
|
+
switch (command) {
|
|
322
|
+
case '--version':
|
|
323
|
+
case '-v':
|
|
324
|
+
case 'version':
|
|
325
|
+
console.log(`baton-worktree CLI · v${WORKTREE_VERSION} (units/worktree)`);
|
|
326
|
+
process.exit(0);
|
|
327
|
+
break;
|
|
328
|
+
case 'allocate':
|
|
329
|
+
case 'alloc':
|
|
330
|
+
allocate(args[1], { base: baseBranch, json: isJson });
|
|
331
|
+
break;
|
|
332
|
+
case 'prune':
|
|
333
|
+
case 'rm':
|
|
334
|
+
prune(args[1], { force: isForce, json: isJson });
|
|
335
|
+
break;
|
|
336
|
+
case 'list':
|
|
337
|
+
case 'ls':
|
|
338
|
+
list({ json: isJson });
|
|
339
|
+
break;
|
|
340
|
+
case 'status':
|
|
341
|
+
status(args[1]);
|
|
342
|
+
break;
|
|
343
|
+
case 'clean-all':
|
|
344
|
+
cleanAll({ force: isForce });
|
|
345
|
+
break;
|
|
346
|
+
case 'help':
|
|
347
|
+
case '--help':
|
|
348
|
+
case '-h':
|
|
349
|
+
default:
|
|
350
|
+
showHelp();
|
|
351
|
+
break;
|
|
352
|
+
}
|