@jspg-ai/coding-bb 0.0.3-beta.32 → 0.0.3-beta.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/cbb/dev-standards/rules/cbb-ai-behavior.md +6 -5
  2. package/cbb/lib/utils/githook.js +6 -2
  3. package/cbb/worktrees/_shared/scripts/check-unarchived.js +77 -0
  4. package/cbb/worktrees/_shared/scripts/delete-branches.js +147 -0
  5. package/cbb/worktrees/_shared/scripts/deprecate-branches.js +119 -0
  6. package/cbb/worktrees/_shared/scripts/find-target-worktree.js +1 -1
  7. package/cbb/worktrees/_shared/scripts/find-workspace-root.js +1 -1
  8. package/cbb/worktrees/_shared/scripts/install-git-hook.js +1 -1
  9. package/cbb/worktrees/_shared/scripts/merge-branches.js +469 -0
  10. package/cbb/worktrees/_shared/scripts/push-core.js +1 -1
  11. package/cbb/worktrees/_shared/scripts/silence-popup.js +1 -1
  12. package/cbb/worktrees/skills/cbb-local-mode/SKILL.md +46 -10
  13. package/cbb/worktrees/skills/cbb-local-mode/scripts/delete-branches.js +147 -0
  14. package/cbb/worktrees/skills/cbb-local-mode/scripts/deprecate-branches.js +119 -0
  15. package/cbb/worktrees/skills/cbb-local-mode/scripts/install-git-hook.js +1 -1
  16. package/cbb/worktrees/skills/cbb-local-mode/scripts/merge-branches.js +469 -0
  17. package/cbb/worktrees/skills/cbb-local-mode/scripts/silence-popup.js +1 -1
  18. package/cbb/worktrees/skills/cbb-worktree-close/SKILL.md +88 -52
  19. package/cbb/worktrees/skills/cbb-worktree-close/scripts/check-env.js +1 -1
  20. package/cbb/worktrees/skills/cbb-worktree-close/scripts/check-unarchived.js +3 -1
  21. package/cbb/worktrees/skills/cbb-worktree-close/scripts/delete-branches.js +147 -136
  22. package/cbb/worktrees/skills/cbb-worktree-close/scripts/deprecate-branches.js +119 -0
  23. package/cbb/worktrees/skills/cbb-worktree-close/scripts/find-target-worktree.js +1 -1
  24. package/cbb/worktrees/skills/cbb-worktree-close/scripts/find-workspace-root.js +1 -1
  25. package/cbb/worktrees/skills/cbb-worktree-close/scripts/merge-branches.js +469 -0
  26. package/cbb/worktrees/skills/cbb-worktree-close/scripts/safety-check.js +4 -2
  27. package/cbb/worktrees/skills/cbb-worktree-close/scripts/silence-popup.js +1 -1
  28. package/cbb/worktrees/skills/cbb-worktree-init/SKILL.md +13 -10
  29. package/cbb/worktrees/skills/cbb-worktree-init/scripts/install-git-hook.js +1 -1
  30. package/cbb/worktrees/skills/cbb-worktree-init/scripts/push-core.js +1 -1
  31. package/cbb/worktrees/skills/cbb-worktree-init/scripts/silence-popup.js +1 -1
  32. package/cbb/worktrees/skills/cbb-worktree-push/SKILL.md +143 -20
  33. package/cbb/worktrees/skills/cbb-worktree-push/scripts/check-unarchived.js +77 -0
  34. package/cbb/worktrees/skills/cbb-worktree-push/scripts/find-target-worktree.js +1 -1
  35. package/cbb/worktrees/skills/cbb-worktree-push/scripts/find-workspace-root.js +1 -1
  36. package/cbb/worktrees/skills/cbb-worktree-push/scripts/merge-branches.js +469 -0
  37. package/cbb/worktrees/skills/cbb-worktree-push/scripts/push-core.js +1 -1
  38. package/cbb/worktrees/skills/cbb-worktree-push/scripts/silence-popup.js +1 -1
  39. package/config/openspec/schemas/spec-driven/schema.yaml +1 -0
  40. package/package.json +2 -2
  41. package/cbb/worktrees/skills/cbb-worktree-init/scripts/find-workspace-root.js +0 -67
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env node
2
+ require('./silence-popup');
3
+ /**
4
+ * 删除需求分支(共享脚本,真源:cbb/worktrees/_shared/scripts/)
5
+ *
6
+ * 供 cbb-worktree-close(Step 4.4 交付完成后自动删本地 + 远程分支)与
7
+ * cbb-local-mode(Step 5.4 收尾删远程分支)调用。
8
+ *
9
+ * 用法:node delete-branches.js <ws_root> <branch> <mode> # mode: keep / local / local_remote
10
+ *
11
+ * 逐仓库执行:本地分支按合并判定选 -d / -D(squash 合并 -d 失败回退 -D);
12
+ * local_remote 模式追加 git push <remote> --delete。
13
+ * 清理阶段失败报告并继续其余仓库,不回滚(ADR 0007)。
14
+ */
15
+ const { execSync } = require('child_process');
16
+ const fs = require('fs');
17
+ const path = require('path');
18
+
19
+ const wsRoot = process.argv[2];
20
+ const branch = process.argv[3];
21
+ const mode = process.argv[4];
22
+
23
+ if (!wsRoot || !branch || !mode) {
24
+ console.error('Usage: node delete-branches.js <ws_root> <branch> <mode>');
25
+ process.exit(1);
26
+ }
27
+
28
+ if (mode === 'keep') {
29
+ console.log('Keep branch, skip delete');
30
+ console.log('---');
31
+ console.log('BRANCH_DELETE_RESULT', JSON.stringify({ mode: 'keep', ok: true, deleted: [] }));
32
+ process.exit(0);
33
+ }
34
+
35
+ if (mode !== 'local' && mode !== 'local_remote') {
36
+ console.error('mode must be keep / local / local_remote');
37
+ process.exit(1);
38
+ }
39
+
40
+ function run(cmd, cwd, env) {
41
+ try {
42
+ return { ok: true, out: execSync(cmd, { cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, ...(env || {}) } }).trim() };
43
+ } catch (e) {
44
+ return { ok: false, err: (e.stderr || e.message || String(e)).trim() };
45
+ }
46
+ }
47
+
48
+ function detectMain(repoPath) {
49
+ const remote = run('git remote', repoPath);
50
+ if (!remote.ok || !remote.out) return 'main';
51
+ const r = remote.out.split(/\r?\n/)[0];
52
+ const symbolic = run('git symbolic-ref "refs/remotes/' + r + '/HEAD"', repoPath);
53
+ if (symbolic.ok && symbolic.out) {
54
+ return symbolic.out.replace('refs/remotes/' + r + '/', '');
55
+ }
56
+ return 'main';
57
+ }
58
+
59
+ function mergedTo(repoPath, from, to) {
60
+ // 0=merged, 1=unmerged, 2=to-ref-missing
61
+ if (!run('git rev-parse --verify --quiet "' + from + '"', repoPath).ok) return 1;
62
+ if (!run('git rev-parse --verify --quiet "' + to + '"', repoPath).ok) return 2;
63
+ if (run('git merge-base --is-ancestor "' + from + '" "' + to + '" && echo Y', repoPath).out === 'Y') return 0;
64
+ // 跨平台:不用 grep(Windows 无 grep),解析 git cherry 输出(+ 前缀 = 未合并提交)
65
+ const cherry = run('git cherry "' + to + '" "' + from + '"', repoPath);
66
+ if (!cherry.ok) return 1;
67
+ if (cherry.out.split(/\r?\n/).filter(Boolean).some(l => l.indexOf('+') === 0)) return 1;
68
+ return 0;
69
+ }
70
+
71
+ function mergedToMain(repoPath, branchName, main) {
72
+ if (mergedTo(repoPath, branchName, main) === 0) return true;
73
+ if (mergedTo(repoPath, branchName, 'origin/' + main) === 0) return true;
74
+ // release-* transit
75
+ const releaseList = run('git for-each-ref --format="%(refname:short)" "refs/heads/release-*" "refs/remotes/origin/release-*"', repoPath);
76
+ if (releaseList.ok && releaseList.out) {
77
+ const releases = releaseList.out.split(/\r?\n/).filter(Boolean).sort();
78
+ for (const rel of releases) {
79
+ if (mergedTo(repoPath, branchName, rel) === 0) {
80
+ if (mergedTo(repoPath, rel, main) === 0) return true;
81
+ if (mergedTo(repoPath, rel, 'origin/' + main) === 0) return true;
82
+ }
83
+ }
84
+ }
85
+ return false;
86
+ }
87
+
88
+ function deleteBranch(repoPath, name) {
89
+ const checkLocal = run('git branch --list "' + branch + '"', repoPath);
90
+ if (checkLocal.ok && checkLocal.out) {
91
+ const main = detectMain(repoPath);
92
+ const merged = main ? mergedToMain(repoPath, branch, main) : false;
93
+ let flag = merged ? '-d' : '-D';
94
+ const del = run('git branch ' + flag + ' "' + branch + '"', repoPath);
95
+ if (del.ok) {
96
+ console.log('OK ' + name + ' local branch deleted (' + flag + ', ' + (merged ? 'merged' : 'unmerged') + ')');
97
+ } else {
98
+ if (flag === '-d') {
99
+ const fallback = run('git branch -D "' + branch + '"', repoPath);
100
+ if (fallback.ok) {
101
+ console.log('OK ' + name + ' local branch deleted (squash merge, fallback -D)');
102
+ } else {
103
+ console.error('FAIL ' + name + ' local branch delete failed');
104
+ return false;
105
+ }
106
+ } else {
107
+ console.error('FAIL ' + name + ' local branch delete failed');
108
+ return false;
109
+ }
110
+ }
111
+ } else {
112
+ console.log('Skip ' + name + ': no local branch ' + branch);
113
+ }
114
+
115
+ if (mode === 'local_remote') {
116
+ const remote = run('git remote', repoPath);
117
+ if (remote.ok && remote.out) {
118
+ const r = remote.out.split(/\r?\n/)[0];
119
+ const pushDel = run('git push "' + r + '" --delete "' + branch + '"', repoPath, { GIT_TERMINAL_PROMPT: '0' });
120
+ if (pushDel.ok) {
121
+ console.log('OK ' + name + ' remote branch deleted');
122
+ } else {
123
+ console.log('Skip ' + name + ': remote has no ' + branch);
124
+ }
125
+ }
126
+ }
127
+ return true;
128
+ }
129
+
130
+ const configPath = path.join(wsRoot, 'CBB-WORKSPACE.json');
131
+ let config = {};
132
+ try { config = JSON.parse(fs.readFileSync(configPath, 'utf-8')); } catch (e) {}
133
+
134
+ const apps = config.apps || [];
135
+
136
+ console.log('=== Delete Workspace Branch ===');
137
+ deleteBranch(wsRoot, 'workspace');
138
+
139
+ console.log('=== Delete App Branches ===');
140
+ for (const app of apps) {
141
+ const appPath = path.join(wsRoot, '.codespace', app.name);
142
+ deleteBranch(appPath, app.name);
143
+ }
144
+
145
+ console.log('---');
146
+ console.log('BRANCH_DELETE_RESULT', JSON.stringify({ mode, ok: true }));
147
+ process.exit(0);
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env node
2
+ require('./silence-popup');
3
+ /**
4
+ * 收尾合并门禁 —— 废弃分支出口(ADR 0007)
5
+ *
6
+ * 用户在门禁处确认"分支废弃、不再交付"后,把各参与仓库远端的需求分支重命名为
7
+ * <branch>-deprecated(保留历史、去掉占用中的旧名),**不删除任何提交**。
8
+ * 本地分支保持原名不动;未推送的仓库(远端无该分支)跳过。
9
+ *
10
+ * 用法:
11
+ * node deprecate-branches.js <ws_root> <branch> [--apps a,b]
12
+ *
13
+ * 退出码:0 全部成功(含 skipped)/ 1 参数错误 / 2 存在失败
14
+ * 末行:DEPRECATE_RESULT [...]
15
+ */
16
+
17
+ const { execSync } = require('child_process');
18
+ const fs = require('fs');
19
+ const path = require('path');
20
+
21
+ function run(cmd, cwd) {
22
+ try {
23
+ return {
24
+ ok: true,
25
+ out: execSync(cmd, {
26
+ cwd,
27
+ encoding: 'utf-8',
28
+ stdio: ['ignore', 'pipe', 'pipe'],
29
+ timeout: 120000,
30
+ env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
31
+ }).trim(),
32
+ };
33
+ } catch (e) {
34
+ return { ok: false, err: (e.stderr || e.message || String(e)).trim() };
35
+ }
36
+ }
37
+
38
+ function loadApps(wsRoot) {
39
+ try {
40
+ const config = JSON.parse(fs.readFileSync(path.join(wsRoot, 'CBB-WORKSPACE.json'), 'utf-8'));
41
+ return Array.isArray(config.apps) ? config.apps : [];
42
+ } catch (e) {
43
+ return [];
44
+ }
45
+ }
46
+
47
+ /** 对单个仓库执行废弃重命名 */
48
+ function deprecateRepo(name, repo, branch) {
49
+ const newName = branch + '-deprecated';
50
+ const rec = { name, status: 'skipped', detail: null };
51
+ if (!fs.existsSync(path.join(repo, '.git'))) {
52
+ rec.detail = 'no-repo';
53
+ return rec;
54
+ }
55
+ const remote = run('git remote', repo);
56
+ if (!remote.ok || !remote.out) {
57
+ rec.detail = 'no-remote';
58
+ return rec;
59
+ }
60
+ const r = remote.out.split(/\r?\n/)[0];
61
+ // 仅重命名远端确实存在的分支
62
+ const exists = run('git ls-remote --heads "' + r + '" "refs/heads/' + branch + '"', repo);
63
+ if (!exists.ok || !exists.out) {
64
+ rec.detail = 'remote-has-no-branch';
65
+ return rec;
66
+ }
67
+ const pushNew = run('git push "' + r + '" "refs/heads/' + branch + ':refs/heads/' + newName + '"', repo);
68
+ if (!pushNew.ok) {
69
+ rec.status = 'failed';
70
+ rec.detail = 'push-new-failed: ' + (pushNew.err || '').split(/\r?\n/)[0];
71
+ return rec;
72
+ }
73
+ const delOld = run('git push "' + r + '" --delete "' + branch + '"', repo);
74
+ if (!delOld.ok) {
75
+ rec.status = 'failed';
76
+ rec.detail = 'delete-old-failed: ' + (delOld.err || '').split(/\r?\n/)[0];
77
+ return rec;
78
+ }
79
+ rec.status = 'deprecated';
80
+ rec.detail = 'remote ' + branch + ' → ' + newName;
81
+ return rec;
82
+ }
83
+
84
+ function main() {
85
+ const args = process.argv.slice(2);
86
+ const wsRoot = args[0];
87
+ const branch = args[1];
88
+
89
+ if (!wsRoot || !branch || branch.indexOf('--') === 0) {
90
+ console.error('Usage: node deprecate-branches.js <ws_root> <branch> [--apps a,b]');
91
+ process.exit(1);
92
+ }
93
+
94
+ const appsIdx = args.indexOf('--apps');
95
+ const appFilter = appsIdx >= 0
96
+ ? new Set((args[appsIdx + 1] || '').split(',').map(s => s.trim()).filter(Boolean))
97
+ : null;
98
+
99
+ const repos = [{ name: 'workspace', repo: wsRoot }];
100
+ for (const app of loadApps(wsRoot)) {
101
+ if (appFilter && !appFilter.has(app.name)) continue;
102
+ repos.push({ name: app.name, repo: path.join(wsRoot, '.codespace', app.name) });
103
+ }
104
+
105
+ const results = repos.map(rp => deprecateRepo(rp.name, rp.repo, branch));
106
+
107
+ results.forEach(r => {
108
+ console.log(r.status.toUpperCase() + ' ' + r.name + (r.detail ? ' — ' + r.detail : ''));
109
+ });
110
+
111
+ const ok = results.every(r => r.status !== 'failed');
112
+ console.log('---');
113
+ console.log('DEPRECATE_RESULT', JSON.stringify({ branch, new_name: branch + '-deprecated', results }));
114
+ process.exit(ok ? 0 : 2);
115
+ }
116
+
117
+ if (require.main === module) main();
118
+
119
+ module.exports = { run, loadApps, deprecateRepo };
@@ -20,7 +20,7 @@ const HOOK_FILE = 'pre-commit';
20
20
  const BACKUP_SUFFIX = '.cbb-backup';
21
21
 
22
22
  // 与 cbb/lib/utils/githook.js 的 buildPreCommitSource() 输出完全一致(构建时注入)
23
- const PRE_COMMIT_SOURCE = "#!/usr/bin/env node\n// managed-by-cbb v1 —— 分支感知提交防线(ADR 0002)\n// main 上:workspace 仓库只放行控制面白名单,应用仓库全拦;非 main 一律放行。\n// 绕过逃生口仅限用户显式 git commit --no-verify。\nconst { execSync } = require('child_process');\nconst fs = require('fs');\nfunction run(cmd) {\n try { return execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); }\n catch (e) { return null; }\n}\nconst branch = run('git branch --show-current') || '';\n// 主干名:优先远端 HEAD 指向;探测失败按本地 main/master 存在性回退\nlet main = null;\nconst remotes = run('git remote');\nif (remotes) {\n const r = remotes.split(/\\r?\\n/)[0];\n const sym = run('git symbolic-ref \"refs/remotes/' + r + '/HEAD\"');\n if (sym) main = sym.replace('refs/remotes/' + r + '/', '');\n}\nif (!main) {\n if (run('git rev-parse --verify --quiet refs/heads/main')) main = 'main';\n else if (run('git rev-parse --verify --quiet refs/heads/master')) main = 'master';\n}\n// 非主干(含 detached / 探测失败)→ 放行\nif (!main || branch !== main) process.exit(0);\n\n// 主干上的提交:收集 staged 变更(空提交放行)\nconst staged = run('git diff --cached --name-only') || '';\nconst files = staged.split(/\\r?\\n/).filter(Boolean);\nif (files.length === 0) process.exit(0);\n\n// 仓库类型:workspace 仓库 = 检出含 CBB-WORKSPACE.json(或旧名,改名过渡期识别)\nconst isWorkspace = fs.existsSync('CBB-WORKSPACE.json') || fs.existsSync('workspace-config.json');\n\nfunction deny(bad) {\n console.error('[cbb-guard] 主干上拦截载荷提交(ADR 0002):');\n bad.forEach(f => console.error(' ' + f));\n console.error('需求载荷须在需求分支上提交:本地模式先 git checkout -b <需求名>;worktree 模式在 .worktrees/ 工作树内提交。');\n console.error('控制面白名单:CBB-WORKSPACE.json / .gitignore / AGENTS.md / openspec/config.yaml / openspec/schemas/** / .cbb/**');\n console.error('如确需在主干提交,须由用户显式 git commit --no-verify。');\n process.exit(1);\n}\n\nif (isWorkspace) {\n // 旧名仅放行删除条目:更名迁移提交(git mv)会产生旧名删除 + 新名新增两条\n const EXACT = new Set(['CBB-WORKSPACE.json', 'workspace-config.json', '.gitignore', 'AGENTS.md', 'openspec/config.yaml']);\n const PREFIXES = ['openspec/schemas/', '.cbb/'];\n const bad = files.filter(f => !EXACT.has(f) && !PREFIXES.some(p => f.indexOf(p) === 0));\n if (bad.length) deny(bad);\n} else {\n deny(files);\n}\nprocess.exit(0);\n";
23
+ const PRE_COMMIT_SOURCE = "#!/usr/bin/env node\n// managed-by-cbb v1 —— 分支感知提交防线(ADR 0002)\n// main 上:workspace 只放行控制面白名单,应用全拦(merge 提交除外);非 main 一律放行。\n// 绕过逃生口仅限用户显式 git commit --no-verify。\nconst { execSync } = require('child_process');\nconst fs = require('fs');\nfunction run(cmd) {\n try { return execSync(cmd, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); }\n catch (e) { return null; }\n}\nconst branch = run('git branch --show-current') || '';\n// 主干名:优先远端 HEAD 指向;探测失败按本地 main/master 存在性回退\nlet main = null;\nconst remotes = run('git remote');\nif (remotes) {\n const r = remotes.split(/\\r?\\n/)[0];\n const sym = run('git symbolic-ref \"refs/remotes/' + r + '/HEAD\"');\n if (sym) main = sym.replace('refs/remotes/' + r + '/', '');\n}\nif (!main) {\n if (run('git rev-parse --verify --quiet refs/heads/main')) main = 'main';\n else if (run('git rev-parse --verify --quiet refs/heads/master')) main = 'master';\n}\n// 非主干(含 detached / 探测失败)→ 放行\nif (!main || branch !== main) process.exit(0);\n\n// merge 进行中(MERGE_HEAD 存在)→ 显式人工合并提交,放行(ADR 0007 合并门禁)\nif (run('git rev-parse --verify --quiet MERGE_HEAD')) process.exit(0);\n\n// 主干上的提交:收集 staged 变更(空提交放行)\nconst staged = run('git diff --cached --name-only') || '';\nconst files = staged.split(/\\r?\\n/).filter(Boolean);\nif (files.length === 0) process.exit(0);\n\n// 仓库类型:workspace 仓库 = 检出含 CBB-WORKSPACE.json(或旧名,改名过渡期识别)\nconst isWorkspace = fs.existsSync('CBB-WORKSPACE.json') || fs.existsSync('workspace-config.json');\n\nfunction deny(bad) {\n console.error('[cbb-guard] 主干上拦截载荷提交(ADR 0002):');\n bad.forEach(f => console.error(' ' + f));\n console.error('需求载荷须在需求分支上提交:本地模式先 git checkout -b <需求名>;worktree 模式在 .worktrees/ 工作树内提交。');\n console.error('控制面白名单:CBB-WORKSPACE.json / .gitignore / AGENTS.md / openspec/config.yaml / openspec/schemas/** / .cbb/**');\n console.error('如确需在主干提交,须由用户显式 git commit --no-verify。');\n process.exit(1);\n}\n\nif (isWorkspace) {\n // 旧名仅放行删除条目:更名迁移提交(git mv)会产生旧名删除 + 新名新增两条\n const EXACT = new Set(['CBB-WORKSPACE.json', 'workspace-config.json', '.gitignore', 'AGENTS.md', 'openspec/config.yaml']);\n const PREFIXES = ['openspec/schemas/', '.cbb/'];\n const bad = files.filter(f => !EXACT.has(f) && !PREFIXES.some(p => f.indexOf(p) === 0));\n if (bad.length) deny(bad);\n} else {\n deny(files);\n}\nprocess.exit(0);\n";
24
24
 
25
25
  function isCbbHookContent(content) {
26
26
  return typeof content === 'string' && content.includes(HOOK_MARKER);