@bbcsoftwarefactory/cli 0.1.0 → 1.1.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/dist/audit-ledger.d.ts +2 -0
- package/dist/audit-ledger.js +71 -0
- package/dist/bbc-install.d.ts +2 -0
- package/dist/bbc-install.js +1250 -0
- package/dist/bbc.d.ts +2 -0
- package/dist/bbc.js +124 -0
- package/dist/bigo/complexity.d.ts +18 -0
- package/dist/bigo/complexity.js +139 -0
- package/dist/bigo/functions.d.ts +5 -0
- package/dist/bigo/functions.js +174 -0
- package/dist/bigo/keywords.d.ts +1 -0
- package/dist/bigo/keywords.js +43 -0
- package/dist/bigo/lexical.d.ts +14 -0
- package/dist/bigo/lexical.js +69 -0
- package/dist/bigo/source-files.d.ts +1 -0
- package/dist/bigo/source-files.js +80 -0
- package/dist/change-lane/classification.d.ts +41 -0
- package/dist/change-lane/classification.js +104 -0
- package/dist/change-lane/git.d.ts +8 -0
- package/dist/change-lane/git.js +105 -0
- package/dist/check-issue-claim.d.ts +2 -0
- package/dist/check-issue-claim.js +146 -0
- package/dist/check-worktree-guard.d.ts +2 -0
- package/dist/check-worktree-guard.js +114 -0
- package/dist/classify-change-lane.d.ts +2 -0
- package/dist/classify-change-lane.js +128 -0
- package/dist/diff-scope/git-diff.d.ts +25 -0
- package/dist/diff-scope/git-diff.js +146 -0
- package/dist/error-scan.d.ts +3 -0
- package/dist/error-scan.js +216 -0
- package/dist/export-public.d.ts +9 -0
- package/dist/export-public.js +274 -0
- package/dist/extract-diff-scope.d.ts +2 -0
- package/dist/extract-diff-scope.js +111 -0
- package/dist/fetch-issue-attachments.d.ts +22 -0
- package/dist/fetch-issue-attachments.js +325 -0
- package/dist/issue-claim/evaluation.d.ts +66 -0
- package/dist/issue-claim/evaluation.js +259 -0
- package/dist/issue-claim/loaders.d.ts +5 -0
- package/dist/issue-claim/loaders.js +116 -0
- package/dist/list-zed-commands.d.ts +7 -0
- package/dist/list-zed-commands.js +288 -0
- package/dist/measure-bigo.d.ts +2 -0
- package/dist/measure-bigo.js +84 -0
- package/dist/plan-issue-triage.d.ts +3 -0
- package/dist/plan-issue-triage.js +404 -0
- package/dist/question-gate/ledger.d.ts +5 -0
- package/dist/question-gate/ledger.js +115 -0
- package/dist/question-gate/rules.d.ts +40 -0
- package/dist/question-gate/rules.js +483 -0
- package/dist/recap/board.d.ts +5 -0
- package/dist/recap/board.js +384 -0
- package/dist/recap/git-and-github.d.ts +11 -0
- package/dist/recap/git-and-github.js +620 -0
- package/dist/recap/report-and-hud.d.ts +2 -0
- package/dist/recap/report-and-hud.js +294 -0
- package/dist/recap/time-and-refs.d.ts +27 -0
- package/dist/recap/time-and-refs.js +148 -0
- package/dist/recap.d.ts +2 -0
- package/dist/recap.js +217 -0
- package/dist/render-workflow.d.ts +2 -0
- package/dist/render-workflow.js +142 -0
- package/dist/resolve-containers.d.ts +2 -0
- package/dist/resolve-containers.js +96 -0
- package/dist/review-loop.d.ts +7 -0
- package/dist/review-loop.js +622 -0
- package/dist/save-counts.d.ts +2 -0
- package/dist/save-counts.js +191 -0
- package/dist/summarize-logs.d.ts +2 -0
- package/dist/summarize-logs.js +41 -0
- package/dist/sweep-github.d.ts +10 -0
- package/dist/sweep-github.js +453 -0
- package/dist/sweep-issues.d.ts +56 -0
- package/dist/sweep-issues.js +650 -0
- package/dist/sweep.d.ts +2 -0
- package/dist/sweep.js +301 -0
- package/dist/triage-question.d.ts +2 -0
- package/dist/triage-question.js +189 -0
- package/dist/validate-skills.d.ts +2 -0
- package/dist/validate-skills.js +48 -0
- package/dist/watch-live.d.ts +7 -0
- package/dist/watch-live.js +83 -0
- package/dist/watcher/config.d.ts +11 -0
- package/dist/watcher/config.js +265 -0
- package/dist/watcher/live-classifier.d.ts +52 -0
- package/dist/watcher/live-classifier.js +510 -0
- package/dist/watcher/safe-fs.d.ts +9 -0
- package/dist/watcher/safe-fs.js +159 -0
- package/dist/watcher/stream.d.ts +27 -0
- package/dist/watcher/stream.js +340 -0
- package/dist/watcher/summarizer.d.ts +19 -0
- package/dist/watcher/summarizer.js +121 -0
- package/dist/wave3/process.d.ts +14 -0
- package/dist/wave3/process.js +84 -0
- package/dist/wave4/process.d.ts +14 -0
- package/dist/wave4/process.js +128 -0
- package/dist/workflow/render.d.ts +13 -0
- package/dist/workflow/render.js +31 -0
- package/dist/workflow/schema.d.ts +25 -0
- package/dist/workflow/schema.js +99 -0
- package/dist/workflow-auditor/audit.d.ts +27 -0
- package/dist/workflow-auditor/audit.js +583 -0
- package/dist/workflow-auditor/ledger-io.d.ts +5 -0
- package/dist/workflow-auditor/ledger-io.js +99 -0
- package/dist/workflow-auditor/skills-validator.d.ts +10 -0
- package/dist/workflow-auditor/skills-validator.js +533 -0
- package/dist/worktree-guard/evaluation.d.ts +19 -0
- package/dist/worktree-guard/evaluation.js +84 -0
- package/dist/worktree-guard/git.d.ts +3 -0
- package/dist/worktree-guard/git.js +61 -0
- package/package.json +32 -33
- package/src/bbc.ts +0 -39
- package/src/bigo/complexity.ts +0 -138
- package/src/bigo/functions.ts +0 -183
- package/src/bigo/keywords.ts +0 -41
- package/src/bigo/lexical.ts +0 -68
- package/src/bigo/source-files.ts +0 -80
- package/src/change-lane/classification.ts +0 -128
- package/src/change-lane/git.ts +0 -110
- package/src/classify-change-lane.ts +0 -140
- package/src/diff-scope/git-diff.ts +0 -161
- package/src/extract-diff-scope.ts +0 -130
- package/src/measure-bigo.ts +0 -86
- package/src/render-workflow.ts +0 -144
- package/src/workflow/render.ts +0 -40
- package/src/workflow/schema.ts +0 -141
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
declare const plans: {
|
|
2
|
+
trivial: {
|
|
3
|
+
test_mode: string;
|
|
4
|
+
required_stages: string[];
|
|
5
|
+
skipped_stages: string[];
|
|
6
|
+
extra_checks: string[];
|
|
7
|
+
};
|
|
8
|
+
standard: {
|
|
9
|
+
test_mode: string;
|
|
10
|
+
required_stages: string[];
|
|
11
|
+
skipped_stages: string[];
|
|
12
|
+
extra_checks: string[];
|
|
13
|
+
};
|
|
14
|
+
'high-risk': {
|
|
15
|
+
test_mode: string;
|
|
16
|
+
required_stages: string[];
|
|
17
|
+
skipped_stages: never[];
|
|
18
|
+
extra_checks: string[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
type Lane = keyof typeof plans;
|
|
22
|
+
export interface ChangeSet {
|
|
23
|
+
files: readonly string[];
|
|
24
|
+
added: number;
|
|
25
|
+
removed: number;
|
|
26
|
+
}
|
|
27
|
+
export interface LaneReport {
|
|
28
|
+
lane: Lane;
|
|
29
|
+
reasons: readonly string[];
|
|
30
|
+
files_changed: number;
|
|
31
|
+
lines_added: number;
|
|
32
|
+
lines_removed: number;
|
|
33
|
+
lines_changed: number;
|
|
34
|
+
files: readonly string[];
|
|
35
|
+
test_mode: string;
|
|
36
|
+
required_stages: readonly string[];
|
|
37
|
+
skipped_stages: readonly string[];
|
|
38
|
+
extra_checks: readonly string[];
|
|
39
|
+
}
|
|
40
|
+
export declare function classify(change: ChangeSet, windows: boolean): LaneReport;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Domain
|
|
3
|
+
* Contains: Change-lane policies and report construction.
|
|
4
|
+
* Rules: Risk takes precedence over file extensions; thresholds are strict.
|
|
5
|
+
* BigO: O(n) score:3
|
|
6
|
+
* keywords: [ChangeLanePolicy, LaneReport]
|
|
7
|
+
* GOAL: Classify paths and line totals without reading Git or the worktree.
|
|
8
|
+
*/
|
|
9
|
+
import { win32 } from 'node:path';
|
|
10
|
+
const largeLines = 300;
|
|
11
|
+
const largeFiles = 10;
|
|
12
|
+
const trivialExtensions = new Set(['.md', '.mdx', '.txt', '.svg', '.excalidraw', '.png', '.jpg', '.jpeg', '.webp', '.gif']);
|
|
13
|
+
const trivialNames = new Set([
|
|
14
|
+
'CHANGELOG.md', 'README.md', 'AGENTS.md', 'INSTALLATION.md', 'CONTRIBUTING.md',
|
|
15
|
+
'SUPPORT.md', 'SECURITY.md', 'LICENSE', 'NOTICE',
|
|
16
|
+
]);
|
|
17
|
+
const riskPatterns = [
|
|
18
|
+
/(^|\/)skills\/watcher-observability\//i,
|
|
19
|
+
/(^|\/)skills\/tool-depot\//i,
|
|
20
|
+
/(^|\/)skills\/workflow-auditor\/scripts\/audit-ledger\.py$/i,
|
|
21
|
+
/(^|\/)skills\/question-gate\/scripts\/triage-question\.py$/i,
|
|
22
|
+
/(^|\/)skills\/delivery-coordinator\/scripts\/check-worktree-guard\.py$/i,
|
|
23
|
+
/(^|\/)tools\/export-public\.py$/i,
|
|
24
|
+
/(^|\/)packages\/cli\/src\/(?:audit-ledger|triage-question|check-worktree-guard|export-public)\.ts$/i,
|
|
25
|
+
/(^|\/)packages\/cli\/src\/(?:workflow-auditor|question-gate|worktree-guard)\//i,
|
|
26
|
+
/(^|\/)\.github\/workflows\//i,
|
|
27
|
+
/(^|\/)(?:auth|oauth|jwt|session|token|crypto|secret|permission|rbac|security|migration|migrations|prisma|schema|deploy|infra)(?:\/|[._-]|$)/i,
|
|
28
|
+
/\.sql$/i,
|
|
29
|
+
];
|
|
30
|
+
const plans = {
|
|
31
|
+
trivial: {
|
|
32
|
+
test_mode: 'proportional',
|
|
33
|
+
required_stages: ['specify', 'implement', 'quality', 'docs', 'commit_pr', 'review'],
|
|
34
|
+
skipped_stages: ['requirements-spec-full-prd', 'risk-scout', 'reproduce-runtime-red', 'verify-runtime-green'],
|
|
35
|
+
extra_checks: ['documentation-steward', 'bbc validate-skills'],
|
|
36
|
+
},
|
|
37
|
+
standard: {
|
|
38
|
+
test_mode: 'tdd',
|
|
39
|
+
required_stages: ['specify', 'reproduce', 'implement', 'verify', 'quality', 'commit_pr', 'review'],
|
|
40
|
+
skipped_stages: ['risk-scout'],
|
|
41
|
+
extra_checks: ['code-quality-gate'],
|
|
42
|
+
},
|
|
43
|
+
'high-risk': {
|
|
44
|
+
test_mode: 'tdd',
|
|
45
|
+
required_stages: [
|
|
46
|
+
'specify', 'risk-scout', 'reproduce', 'implement', 'verify', 'quality', 'commit_pr', 'review', 'residual_risk',
|
|
47
|
+
],
|
|
48
|
+
skipped_stages: [],
|
|
49
|
+
extra_checks: ['risk-scout', 'code-quality-gate', 'human-security-review'],
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
function normalize(path) {
|
|
53
|
+
return path.replaceAll('\\', '/');
|
|
54
|
+
}
|
|
55
|
+
function isHighRisk(path) {
|
|
56
|
+
// Python re.I adds these four case equivalents for ASCII letters in the patterns.
|
|
57
|
+
const norm = normalize(path)
|
|
58
|
+
.replace(/[İı]/g, 'i').replace(/ſ/g, 's').replace(/K/g, 'k');
|
|
59
|
+
// Python's $ also accepts the position just before a final newline.
|
|
60
|
+
const subject = norm.endsWith('\n') ? norm.slice(0, -1) : norm;
|
|
61
|
+
return riskPatterns.some((pattern) => pattern.test(subject));
|
|
62
|
+
}
|
|
63
|
+
function isTrivial(path, windows) {
|
|
64
|
+
const norm = normalize(path);
|
|
65
|
+
const local = windows ? norm.slice(win32.parse(norm).root.length) : norm;
|
|
66
|
+
// pathlib ignores trailing slash and "." components, but does not collapse "..".
|
|
67
|
+
const parts = local.split('/');
|
|
68
|
+
const name = parts.filter((part) => part !== '' && part !== '.').at(-1) ?? '';
|
|
69
|
+
const dot = name.lastIndexOf('.');
|
|
70
|
+
return trivialNames.has(name) || (dot > 0 && trivialExtensions.has(name.slice(dot).toLowerCase()));
|
|
71
|
+
}
|
|
72
|
+
function decide({ files, added, removed }, windows) {
|
|
73
|
+
if (files.length === 0)
|
|
74
|
+
return ['trivial', ['No changed files detected.']];
|
|
75
|
+
const hits = files.filter(isHighRisk);
|
|
76
|
+
const lines = added + removed;
|
|
77
|
+
const reasons = [];
|
|
78
|
+
if (hits.length > 0)
|
|
79
|
+
reasons.push(`Touched high-risk path(s): ${hits.slice(0, 5).join(', ')}`);
|
|
80
|
+
if (lines > largeLines)
|
|
81
|
+
reasons.push(`Large diff (${lines} lines > ${largeLines}).`);
|
|
82
|
+
if (files.length > largeFiles)
|
|
83
|
+
reasons.push(`Broad change (${files.length} files > ${largeFiles}).`);
|
|
84
|
+
if (reasons.length > 0)
|
|
85
|
+
return ['high-risk', reasons];
|
|
86
|
+
if (files.every((path) => isTrivial(path, windows))) {
|
|
87
|
+
return ['trivial', ['Only documentation, markdown, or visual assets changed.']];
|
|
88
|
+
}
|
|
89
|
+
return ['standard', ['Scoped code or skill change without high-risk paths or oversized diff.']];
|
|
90
|
+
}
|
|
91
|
+
export function classify(change, windows) {
|
|
92
|
+
const { files, added, removed } = change;
|
|
93
|
+
const [lane, reasons] = decide(change, windows);
|
|
94
|
+
return {
|
|
95
|
+
lane,
|
|
96
|
+
reasons,
|
|
97
|
+
files_changed: files.length,
|
|
98
|
+
lines_added: added,
|
|
99
|
+
lines_removed: removed,
|
|
100
|
+
lines_changed: added + removed,
|
|
101
|
+
files,
|
|
102
|
+
...plans[lane],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Infrastructure
|
|
3
|
+
* Contains: Git diff and working-tree statistics adapter.
|
|
4
|
+
* Rules: Read only; use Git's NUL-delimited paths and ignore untracked non-files.
|
|
5
|
+
* BigO: O(n log n) score:2
|
|
6
|
+
* keywords: [GitDiff, ChangeStatistics]
|
|
7
|
+
* GOAL: Collect the paths and line counts consumed by the classifier.
|
|
8
|
+
*/
|
|
9
|
+
import { spawnSync } from 'node:child_process';
|
|
10
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
11
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
12
|
+
const diff = ['-c', 'core.quotePath=false', 'diff', '--no-ext-diff', '--no-textconv'];
|
|
13
|
+
function git(args) {
|
|
14
|
+
const result = spawnSync('git', args, {
|
|
15
|
+
env: { ...process.env, GIT_OPTIONAL_LOCKS: '0', GIT_NO_LAZY_FETCH: '1' },
|
|
16
|
+
timeout: 10_000,
|
|
17
|
+
maxBuffer: Number.MAX_SAFE_INTEGER,
|
|
18
|
+
});
|
|
19
|
+
return result.status === 0 && !result.error ? result.stdout : undefined;
|
|
20
|
+
}
|
|
21
|
+
function diffArguments(selection) {
|
|
22
|
+
switch (selection.mode) {
|
|
23
|
+
case 'working':
|
|
24
|
+
return git(['rev-parse', '--verify', 'HEAD']) === undefined ? ['--staged'] : ['HEAD'];
|
|
25
|
+
case 'staged':
|
|
26
|
+
return ['--staged'];
|
|
27
|
+
case 'range':
|
|
28
|
+
return [selection.value];
|
|
29
|
+
case 'head':
|
|
30
|
+
return [`HEAD~${selection.value}..HEAD`];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function paths(raw) {
|
|
34
|
+
try {
|
|
35
|
+
return decoder.decode(raw).split('\0').filter(Boolean);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function isRegularFile(path) {
|
|
42
|
+
try {
|
|
43
|
+
return statSync(path).isFile();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function untrackedPaths() {
|
|
50
|
+
const raw = git(['-c', 'core.quotePath=false', 'ls-files', '--others', '--exclude-standard', '-z']);
|
|
51
|
+
if (raw === undefined)
|
|
52
|
+
return [];
|
|
53
|
+
return paths(raw)?.filter(isRegularFile);
|
|
54
|
+
}
|
|
55
|
+
function trackedCounts(args, files) {
|
|
56
|
+
if (files.length === 0)
|
|
57
|
+
return [0, 0];
|
|
58
|
+
const raw = git([...diff, ...args, '--numstat', '--', ...files]);
|
|
59
|
+
if (raw === undefined)
|
|
60
|
+
return [0, 0];
|
|
61
|
+
let added = 0;
|
|
62
|
+
let removed = 0;
|
|
63
|
+
for (const row of raw.toString('utf8').split('\n')) {
|
|
64
|
+
const [plus = '', minus = ''] = row.split('\t', 2);
|
|
65
|
+
if (/^[0-9]+$/.test(plus))
|
|
66
|
+
added += Number(plus);
|
|
67
|
+
if (/^[0-9]+$/.test(minus))
|
|
68
|
+
removed += Number(minus);
|
|
69
|
+
}
|
|
70
|
+
return [added, removed];
|
|
71
|
+
}
|
|
72
|
+
function countNewlines(path) {
|
|
73
|
+
let data;
|
|
74
|
+
try {
|
|
75
|
+
data = readFileSync(path);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
let lines = 0;
|
|
81
|
+
for (const byte of data) {
|
|
82
|
+
if (byte === 0x0a)
|
|
83
|
+
lines++;
|
|
84
|
+
}
|
|
85
|
+
return lines;
|
|
86
|
+
}
|
|
87
|
+
function sortedFiles(tracked, untracked) {
|
|
88
|
+
// UTF-8 byte sorting preserves Python's code-point order for valid UTF-8 names.
|
|
89
|
+
return Array.from(new Set([...tracked, ...untracked]), (file) => ({ file, bytes: Buffer.from(file) }))
|
|
90
|
+
.sort((a, b) => Buffer.compare(a.bytes, b.bytes))
|
|
91
|
+
.map(({ file }) => file);
|
|
92
|
+
}
|
|
93
|
+
export function readGitChanges(selection) {
|
|
94
|
+
const args = diffArguments(selection);
|
|
95
|
+
const names = git([...diff, ...args, '--name-only', '-z']);
|
|
96
|
+
if (names === undefined)
|
|
97
|
+
return undefined;
|
|
98
|
+
const tracked = paths(names);
|
|
99
|
+
const untracked = selection.mode === 'working' ? untrackedPaths() : [];
|
|
100
|
+
if (tracked === undefined || untracked === undefined)
|
|
101
|
+
return undefined;
|
|
102
|
+
const [added, removed] = trackedCounts(args, tracked);
|
|
103
|
+
const untrackedLines = untracked.reduce((sum, path) => sum + countNewlines(path), 0);
|
|
104
|
+
return { files: sortedFiles(tracked, untracked), added: added + untrackedLines, removed };
|
|
105
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Interface
|
|
3
|
+
* Contains: CLI argument parsing and JSON report emission for check-issue-claim.
|
|
4
|
+
* Rules: Match check-issue-claim.py stdout/exit codes byte-for-byte.
|
|
5
|
+
* BigO: O(n) score:3
|
|
6
|
+
* keywords: [IssueClaimCli, ClaimArguments]
|
|
7
|
+
* GOAL: Run offline issue-claim checks (CL1-CL12) from the bbc CLI.
|
|
8
|
+
*/
|
|
9
|
+
import { evaluateClaim, parseIsoTime, STOP_VERDICTS, UnusableClaimError, } from "./issue-claim/evaluation.js";
|
|
10
|
+
import { loadBranches, loadEvents, loadIssue, loadPulls } from "./issue-claim/loaders.js";
|
|
11
|
+
export const checkIssueClaimUsage = 'bbc check-issue-claim --phase check|verify|release|status --issue-json <file> [options]';
|
|
12
|
+
const PHASES = new Set(['check', 'verify', 'release', 'status']);
|
|
13
|
+
const DEFAULT_LABEL = 'status:in-progress';
|
|
14
|
+
const DEFAULT_STALE_DAYS = 7;
|
|
15
|
+
const HELP = 'Usage: bbc check-issue-claim --phase check|verify|release|status\n' +
|
|
16
|
+
' --issue-json <file> gh api repos/<o>/<r>/issues/<n> (or gh issue view <n> --json ...)\n' +
|
|
17
|
+
' --pulls-json <file> gh api --paginate --slurp "repos/<o>/<r>/pulls?state=open&per_page=100"\n' +
|
|
18
|
+
' --branches <file> git ls-remote --heads <remote> "feat/<n>-*"\n' +
|
|
19
|
+
' --events-json <file> gh api --paginate --slurp repos/<o>/<r>/issues/<n>/events (verify)\n' +
|
|
20
|
+
' --claimant <login> GitHub login of the person (check, verify, release)\n' +
|
|
21
|
+
' --branch <feat/<n>-<slug>> the branch this delivery will use (check, verify)\n' +
|
|
22
|
+
' [--repo <owner/repo>] [--label status:in-progress] [--stale-days 7] [--now <ISO-8601>]\n' +
|
|
23
|
+
'Exit 0: proceed (free, claimed-by-you, released, claimed, unclaimed); 1: stop; 2: unusable.';
|
|
24
|
+
const FLAG_KEYS = {
|
|
25
|
+
'--phase': 'phase',
|
|
26
|
+
'--issue-json': 'issue',
|
|
27
|
+
'--pulls-json': 'pulls',
|
|
28
|
+
'--branches': 'branches',
|
|
29
|
+
'--events-json': 'events',
|
|
30
|
+
'--claimant': 'claimant',
|
|
31
|
+
'--branch': 'branch',
|
|
32
|
+
'--repo': 'repo',
|
|
33
|
+
'--label': 'label',
|
|
34
|
+
'--stale-days': 'stale_days',
|
|
35
|
+
'--now': 'now',
|
|
36
|
+
};
|
|
37
|
+
function writeJsonOut(value) {
|
|
38
|
+
const escaped = JSON.stringify(value, null, 2).replace(/[\u007f-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
39
|
+
process.stdout.write(`${escaped}\n`);
|
|
40
|
+
}
|
|
41
|
+
function parseArgs(argv) {
|
|
42
|
+
const raw = {
|
|
43
|
+
phase: null,
|
|
44
|
+
issue: null,
|
|
45
|
+
pulls: null,
|
|
46
|
+
branches: null,
|
|
47
|
+
events: null,
|
|
48
|
+
claimant: null,
|
|
49
|
+
branch: null,
|
|
50
|
+
repo: null,
|
|
51
|
+
label: DEFAULT_LABEL,
|
|
52
|
+
stale_days: String(DEFAULT_STALE_DAYS),
|
|
53
|
+
now: null,
|
|
54
|
+
};
|
|
55
|
+
for (let i = 0; i < argv.length; i++) {
|
|
56
|
+
const arg = argv[i] ?? '';
|
|
57
|
+
if (arg === '-h' || arg === '--help')
|
|
58
|
+
return null;
|
|
59
|
+
const key = FLAG_KEYS[arg];
|
|
60
|
+
const value = argv[i + 1];
|
|
61
|
+
if (key === undefined || value === undefined) {
|
|
62
|
+
throw new UnusableClaimError(`Unexpected argument ${arg}; see --help.`);
|
|
63
|
+
}
|
|
64
|
+
raw[key] = value;
|
|
65
|
+
i++;
|
|
66
|
+
}
|
|
67
|
+
const phase = raw.phase ?? null;
|
|
68
|
+
if (phase === null || !PHASES.has(phase)) {
|
|
69
|
+
throw new UnusableClaimError('--phase must be one of: check, verify, release, status.');
|
|
70
|
+
}
|
|
71
|
+
const issuePath = raw.issue ?? null;
|
|
72
|
+
if (issuePath === null) {
|
|
73
|
+
throw new UnusableClaimError('--issue-json is required.');
|
|
74
|
+
}
|
|
75
|
+
const pullsPath = raw.pulls ?? null;
|
|
76
|
+
const branchesPath = raw.branches ?? null;
|
|
77
|
+
if (phase !== 'release' && (pullsPath === null || branchesPath === null)) {
|
|
78
|
+
throw new UnusableClaimError(`--pulls-json and --branches are required for ${phase}.`);
|
|
79
|
+
}
|
|
80
|
+
const claimant = raw.claimant ?? null;
|
|
81
|
+
if (phase !== 'status' && !claimant) {
|
|
82
|
+
throw new UnusableClaimError(`--claimant is required for ${phase}.`);
|
|
83
|
+
}
|
|
84
|
+
const branch = raw.branch ?? null;
|
|
85
|
+
if ((phase === 'check' || phase === 'verify') && !branch) {
|
|
86
|
+
throw new UnusableClaimError(`--branch is required for ${phase}.`);
|
|
87
|
+
}
|
|
88
|
+
const staleRaw = raw.stale_days ?? '';
|
|
89
|
+
if (!/^-?\d+$/.test(staleRaw)) {
|
|
90
|
+
throw new UnusableClaimError('--stale-days must be an integer.');
|
|
91
|
+
}
|
|
92
|
+
const staleDays = Number.parseInt(staleRaw, 10);
|
|
93
|
+
const now = raw.now ? parseIsoTime(raw.now) : Date.now();
|
|
94
|
+
if (now === null) {
|
|
95
|
+
throw new UnusableClaimError('--now must be an ISO-8601 time.');
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
issuePath,
|
|
99
|
+
pullsPath,
|
|
100
|
+
branchesPath,
|
|
101
|
+
eventsPath: raw.events ?? null,
|
|
102
|
+
options: {
|
|
103
|
+
phase: phase,
|
|
104
|
+
claimant,
|
|
105
|
+
branch,
|
|
106
|
+
repo: raw.repo ?? null,
|
|
107
|
+
label: raw.label ?? DEFAULT_LABEL,
|
|
108
|
+
stale_days: staleDays,
|
|
109
|
+
now,
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export function checkIssueClaim(argv) {
|
|
114
|
+
try {
|
|
115
|
+
const parsed = parseArgs(argv);
|
|
116
|
+
if (parsed === null) {
|
|
117
|
+
process.stdout.write(`${HELP}\n`);
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
const issue = loadIssue(parsed.issuePath);
|
|
121
|
+
const pulls = parsed.pullsPath ? loadPulls(parsed.pullsPath) : [];
|
|
122
|
+
const branches = parsed.branchesPath ? loadBranches(parsed.branchesPath) : [];
|
|
123
|
+
const events = parsed.eventsPath ? loadEvents(parsed.eventsPath) : null;
|
|
124
|
+
const { verdict, findings, holders } = evaluateClaim(issue, pulls, branches, events, parsed.options);
|
|
125
|
+
const proceed = !STOP_VERDICTS.has(verdict);
|
|
126
|
+
writeJsonOut({
|
|
127
|
+
verdict,
|
|
128
|
+
proceed,
|
|
129
|
+
phase: parsed.options.phase,
|
|
130
|
+
issue: issue.number,
|
|
131
|
+
claimant: parsed.options.claimant,
|
|
132
|
+
holders,
|
|
133
|
+
findings,
|
|
134
|
+
});
|
|
135
|
+
return proceed ? 0 : 1;
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
const detail = error instanceof UnusableClaimError ? error.message : String(error);
|
|
139
|
+
writeJsonOut({
|
|
140
|
+
verdict: 'unusable',
|
|
141
|
+
proceed: false,
|
|
142
|
+
findings: [{ rule: 'CL1', verdict: 'unusable', detail }],
|
|
143
|
+
});
|
|
144
|
+
return 2;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Interface
|
|
3
|
+
* Contains: CLI argument parsing, stdin reading, and JSON formatting for check-worktree-guard.
|
|
4
|
+
* Rules: Match check-worktree-guard.py stdout/stderr/exit codes byte-for-byte.
|
|
5
|
+
* BigO: O(n) score:3
|
|
6
|
+
* keywords: [WorktreeGuardCli, HookStdin]
|
|
7
|
+
* GOAL: Enforce linked feat/<task> worktree isolation from the bbc CLI.
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync } from 'node:fs';
|
|
10
|
+
import { EOL } from 'node:os';
|
|
11
|
+
import { evaluateGitInfo, extractPayloadPath, formatHookOutput, } from "./worktree-guard/evaluation.js";
|
|
12
|
+
import { findExistingDirectory, probeGitWorktree } from "./worktree-guard/git.js";
|
|
13
|
+
export const checkWorktreeGuardUsage = 'bbc check-worktree-guard [--path <dir>] [--target-path <file>] [--stdin --format json|claude-hook|cursor-hook|codex-hook]';
|
|
14
|
+
const HELP = 'Usage: bbc check-worktree-guard [--path <dir>] [--target-path <file>] [--stdin --format json|claude-hook|cursor-hook|codex-hook]\n' +
|
|
15
|
+
'Exit 0: allowed linked feat/<task> worktree; 1: blocked (primary/non-feat); 2: unusable.';
|
|
16
|
+
const VALID_FORMATS = new Set(['json', 'claude-hook', 'cursor-hook', 'codex-hook']);
|
|
17
|
+
function escapeAscii(json) {
|
|
18
|
+
return json.replace(/[\u007f-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
19
|
+
}
|
|
20
|
+
function pythonJsonPretty(value) {
|
|
21
|
+
return escapeAscii(JSON.stringify(value, null, 2));
|
|
22
|
+
}
|
|
23
|
+
function pythonJsonCompact(value) {
|
|
24
|
+
if (value === null || typeof value !== 'object')
|
|
25
|
+
return escapeAscii(JSON.stringify(value));
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return `[${value.map(pythonJsonCompact).join(', ')}]`;
|
|
28
|
+
const entries = Object.entries(value);
|
|
29
|
+
return `{${entries.map(([key, val]) => `${escapeAscii(JSON.stringify(key))}: ${pythonJsonCompact(val)}`).join(', ')}}`;
|
|
30
|
+
}
|
|
31
|
+
function writeOut(line) {
|
|
32
|
+
process.stdout.write(`${line}\n`);
|
|
33
|
+
}
|
|
34
|
+
function parseArgs(argv) {
|
|
35
|
+
let pathArg;
|
|
36
|
+
let targetPath;
|
|
37
|
+
let useStdin = false;
|
|
38
|
+
let format = 'json';
|
|
39
|
+
for (let i = 0; i < argv.length; i++) {
|
|
40
|
+
const arg = argv[i] ?? '';
|
|
41
|
+
if (arg === '-h' || arg === '--help')
|
|
42
|
+
return { kind: 'help' };
|
|
43
|
+
if (arg === '--stdin') {
|
|
44
|
+
useStdin = true;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (arg === '--path' || arg === '--target-path' || arg === '--format') {
|
|
48
|
+
const value = argv[i + 1];
|
|
49
|
+
if (value === undefined || value === '')
|
|
50
|
+
return { kind: 'error', message: `error: ${arg} requires a value` };
|
|
51
|
+
if (arg === '--path')
|
|
52
|
+
pathArg = value;
|
|
53
|
+
else if (arg === '--target-path')
|
|
54
|
+
targetPath = value;
|
|
55
|
+
else {
|
|
56
|
+
if (!VALID_FORMATS.has(value))
|
|
57
|
+
return { kind: 'error', message: `error: invalid --format: ${value}` };
|
|
58
|
+
format = value;
|
|
59
|
+
}
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
return { kind: 'error', message: `error: unknown option: ${arg}` };
|
|
64
|
+
}
|
|
65
|
+
return { kind: 'run', pathArg, targetPath, useStdin, format };
|
|
66
|
+
}
|
|
67
|
+
function evaluatePath(pathStr) {
|
|
68
|
+
const directory = findExistingDirectory(pathStr);
|
|
69
|
+
if (directory === undefined) {
|
|
70
|
+
return { report: { decision: 'unusable', reason: `Path does not exist: ${pathStr}` }, status: 2 };
|
|
71
|
+
}
|
|
72
|
+
const info = probeGitWorktree(directory);
|
|
73
|
+
if (info === undefined) {
|
|
74
|
+
return { report: { decision: 'unusable', reason: 'Target directory is not inside a Git worktree.' }, status: 2 };
|
|
75
|
+
}
|
|
76
|
+
return evaluateGitInfo(info);
|
|
77
|
+
}
|
|
78
|
+
export function checkWorktreeGuard(argv) {
|
|
79
|
+
const parsed = parseArgs(argv);
|
|
80
|
+
if (parsed.kind === 'help') {
|
|
81
|
+
writeOut(HELP);
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
if (parsed.kind === 'error') {
|
|
85
|
+
process.stderr.write(`${parsed.message}${EOL}`);
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
let resolvedTarget = parsed.targetPath || parsed.pathArg;
|
|
89
|
+
if (parsed.useStdin) {
|
|
90
|
+
const rawStdin = readFileSync(0, 'utf8').trim();
|
|
91
|
+
if (rawStdin !== '') {
|
|
92
|
+
let payload;
|
|
93
|
+
try {
|
|
94
|
+
payload = JSON.parse(rawStdin);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
writeOut(pythonJsonCompact({ decision: 'unusable', reason: 'Invalid JSON on stdin.' }));
|
|
98
|
+
return 2;
|
|
99
|
+
}
|
|
100
|
+
const extracted = extractPayloadPath(payload);
|
|
101
|
+
if (extracted !== undefined && !parsed.targetPath)
|
|
102
|
+
resolvedTarget = extracted;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!resolvedTarget)
|
|
106
|
+
resolvedTarget = '.';
|
|
107
|
+
const { report, status } = evaluatePath(resolvedTarget);
|
|
108
|
+
if (parsed.format === 'json') {
|
|
109
|
+
writeOut(pythonJsonPretty(report));
|
|
110
|
+
return status;
|
|
111
|
+
}
|
|
112
|
+
writeOut(pythonJsonCompact(formatHookOutput(report, status, parsed.format)));
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Interface
|
|
3
|
+
* Contains: Change-lane command parsing and output formatting.
|
|
4
|
+
* Rules: Emit the legacy report format, with native CLI help and read-only Git arguments.
|
|
5
|
+
* BigO: O(n) score:3
|
|
6
|
+
* keywords: [ClassifyChangeLane, LaneArguments]
|
|
7
|
+
* GOAL: Translate a local change selection into a lane report and process status.
|
|
8
|
+
*/
|
|
9
|
+
import { EOL } from 'node:os';
|
|
10
|
+
import { classify } from "./change-lane/classification.js";
|
|
11
|
+
import { readGitChanges } from "./change-lane/git.js";
|
|
12
|
+
export const classifyChangeLaneUsage = 'bbc classify-change-lane [--working|--staged|--range A..B|--head N|--files <p1>...] [--format json|hud]';
|
|
13
|
+
function error(message) {
|
|
14
|
+
return { kind: 'error', message };
|
|
15
|
+
}
|
|
16
|
+
function explicitPaths(args, from) {
|
|
17
|
+
const files = [];
|
|
18
|
+
let next = from;
|
|
19
|
+
while (next < args.length) {
|
|
20
|
+
const path = args[next];
|
|
21
|
+
if (path === undefined || path.startsWith('--'))
|
|
22
|
+
break;
|
|
23
|
+
files.push(path);
|
|
24
|
+
next++;
|
|
25
|
+
}
|
|
26
|
+
return { files, next };
|
|
27
|
+
}
|
|
28
|
+
const selectionFlags = new Set(['--working', '--staged', '--range', '--head', '--files']);
|
|
29
|
+
function selectionError(selection) {
|
|
30
|
+
if (selection.mode === 'head' && !/^[1-9][0-9]*$/.test(selection.value)) {
|
|
31
|
+
return `--head requires a positive integer: ${selection.value}`;
|
|
32
|
+
}
|
|
33
|
+
if (selection.mode === 'range' && selection.value.startsWith('-')) {
|
|
34
|
+
return '--range must not start with -';
|
|
35
|
+
}
|
|
36
|
+
if (selection.mode === 'files' && selection.files.length === 0) {
|
|
37
|
+
return '--files requires at least one path';
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
function replacementError(selection, flag) {
|
|
42
|
+
if (!selection || !flag || !selectionFlags.has(flag))
|
|
43
|
+
return undefined;
|
|
44
|
+
if (flag !== `--${selection.mode}`)
|
|
45
|
+
return `cannot combine --${selection.mode} with ${flag}`;
|
|
46
|
+
// Keep the existing conflict diagnostic, but never let a same-mode flag hide an invalid value.
|
|
47
|
+
return selectionError(selection);
|
|
48
|
+
}
|
|
49
|
+
function parse(args) {
|
|
50
|
+
let selection;
|
|
51
|
+
let format;
|
|
52
|
+
for (let i = 0; i < args.length; i++) {
|
|
53
|
+
const arg = args[i];
|
|
54
|
+
const value = args[i + 1];
|
|
55
|
+
const invalidReplacement = replacementError(selection, arg);
|
|
56
|
+
if (invalidReplacement !== undefined)
|
|
57
|
+
return error(invalidReplacement);
|
|
58
|
+
switch (arg) {
|
|
59
|
+
case '-h':
|
|
60
|
+
case '--help':
|
|
61
|
+
return { kind: 'help' };
|
|
62
|
+
case '--working':
|
|
63
|
+
case '--staged':
|
|
64
|
+
selection = { mode: arg === '--working' ? 'working' : 'staged' };
|
|
65
|
+
break;
|
|
66
|
+
case '--range':
|
|
67
|
+
case '--head':
|
|
68
|
+
if (!value)
|
|
69
|
+
return error(`${arg} requires an argument`);
|
|
70
|
+
selection = { mode: arg === '--range' ? 'range' : 'head', value };
|
|
71
|
+
i++;
|
|
72
|
+
break;
|
|
73
|
+
case '--files': {
|
|
74
|
+
const { files, next } = explicitPaths(args, i + 1);
|
|
75
|
+
selection = { mode: 'files', files };
|
|
76
|
+
i = next - 1;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case '--format':
|
|
80
|
+
if (format !== undefined)
|
|
81
|
+
return error('--format may only be specified once');
|
|
82
|
+
if (value !== 'json' && value !== 'hud')
|
|
83
|
+
return error('--format must be json or hud');
|
|
84
|
+
format = value;
|
|
85
|
+
i++;
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
return error(`unknown option: ${arg}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
selection ??= { mode: 'working' };
|
|
92
|
+
const invalidSelection = selectionError(selection);
|
|
93
|
+
if (invalidSelection !== undefined)
|
|
94
|
+
return error(invalidSelection);
|
|
95
|
+
return { kind: 'run', selection, format: format ?? 'json' };
|
|
96
|
+
}
|
|
97
|
+
const badges = { trivial: '🟢', standard: '🟡', 'high-risk': '🔴' };
|
|
98
|
+
function formatReport(report, format) {
|
|
99
|
+
if (format === 'hud') {
|
|
100
|
+
const { lane, files_changed: files, lines_changed: lines, reasons } = report;
|
|
101
|
+
return `🛣️ Lane: ${badges[lane]} ${lane} (${files} files, ${lines} lines) — ${reasons[0] ?? ''}`;
|
|
102
|
+
}
|
|
103
|
+
// Python json.dumps defaults to ASCII and escapes even DEL.
|
|
104
|
+
return JSON.stringify(report, null, 2).replace(/[\u007f-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
105
|
+
}
|
|
106
|
+
function fail(message, status) {
|
|
107
|
+
process.stderr.write(`error: ${message}\n`.replaceAll('\n', EOL));
|
|
108
|
+
return status;
|
|
109
|
+
}
|
|
110
|
+
export function classifyChangeLane(args) {
|
|
111
|
+
const parsed = parse(args);
|
|
112
|
+
if (parsed.kind === 'help') {
|
|
113
|
+
process.stdout.write(`Usage: ${classifyChangeLaneUsage}\nClassifies a change into trivial, standard, or high-risk.\n`
|
|
114
|
+
+ 'Do not combine different selection modes or repeat --format.\n');
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
if (parsed.kind === 'error')
|
|
118
|
+
return fail(parsed.message, 1);
|
|
119
|
+
const { selection, format } = parsed;
|
|
120
|
+
const change = selection.mode === 'files'
|
|
121
|
+
? { files: selection.files, added: 0, removed: 0 }
|
|
122
|
+
: readGitChanges(selection);
|
|
123
|
+
if (change === undefined)
|
|
124
|
+
return fail('failed to inspect git diff', 2);
|
|
125
|
+
const report = classify(change, process.platform === 'win32');
|
|
126
|
+
process.stdout.write(`${formatReport(report, format)}\n`);
|
|
127
|
+
return 0;
|
|
128
|
+
}
|