architecture-harness 0.25.0 → 0.26.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/package.json +3 -22
- package/src/cli/ask.mjs +42 -0
- package/src/cli/ask.test.mjs +40 -0
- package/src/cli/branch-sync.mjs +126 -0
- package/src/cli/branch-sync.test.mjs +428 -0
- package/src/cli/budget-guard.mjs +74 -5
- package/src/cli/budget-guard.test.mjs +185 -2
- package/src/cli/budget-report.mjs +85 -0
- package/src/cli/budget-report.test.mjs +173 -0
- package/src/cli/budget.mjs +8 -5
- package/src/cli/check-commands.mjs +123 -11
- package/src/cli/check-commands.test.mjs +360 -6
- package/src/cli/check-feature-switches.test.mjs +110 -0
- package/src/cli/check-full.test.mjs +152 -0
- package/src/cli/check-verdict.mjs +46 -0
- package/src/cli/check-verdict.test.mjs +32 -0
- package/src/cli/check.mjs +137 -4
- package/src/cli/check.test.mjs +45 -0
- package/src/cli/checkpoint.mjs +143 -0
- package/src/cli/checkpoint.test.mjs +146 -0
- package/src/cli/child-run.mjs +7 -2
- package/src/cli/child-run.test.mjs +17 -2
- package/src/cli/ci-failures.mjs +304 -0
- package/src/cli/ci-failures.test.mjs +615 -0
- package/src/cli/ci-preview.mjs +62 -0
- package/src/cli/ci-preview.test.mjs +85 -0
- package/src/cli/ci-state.mjs +25 -29
- package/src/cli/ci-state.test.mjs +174 -2
- package/src/cli/ci-template.test.mjs +86 -0
- package/src/cli/claude-core.test.mjs +37 -0
- package/src/cli/codex-implement.mjs +580 -47
- package/src/cli/codex-implement.test.mjs +1946 -29
- package/src/cli/codex-limit.mjs +85 -0
- package/src/cli/codex-limit.test.mjs +179 -0
- package/src/cli/codex-reset.mjs +78 -0
- package/src/cli/codex-reset.test.mjs +192 -0
- package/src/cli/codex-verify.mjs +230 -0
- package/src/cli/codex-verify.test.mjs +402 -0
- package/src/cli/comment-watch.mjs +391 -0
- package/src/cli/comment-watch.test.mjs +392 -0
- package/src/cli/commit-guard.mjs +219 -0
- package/src/cli/commit-guard.test.mjs +809 -0
- package/src/cli/context-budgets.mjs +47 -0
- package/src/cli/context-budgets.test.mjs +96 -0
- package/src/cli/context-watch.mjs +116 -0
- package/src/cli/context-watch.test.mjs +395 -0
- package/src/cli/contract-history.mjs +1 -1
- package/src/cli/controller-guard.mjs +38 -0
- package/src/cli/controller-guard.test.mjs +66 -2
- package/src/cli/cost-stop.mjs +5 -1
- package/src/cli/cost-stop.test.mjs +26 -0
- package/src/cli/diff-shape.mjs +162 -0
- package/src/cli/diff-shape.test.mjs +200 -0
- package/src/cli/direct-lane.mjs +32 -19
- package/src/cli/direct-lane.test.mjs +21 -3
- package/src/cli/dispatch-guard.mjs +11 -0
- package/src/cli/dispatch-guard.test.mjs +145 -10
- package/src/cli/dispatch-reminder.mjs +23 -10
- package/src/cli/dispatch-reminder.test.mjs +63 -11
- package/src/cli/dispatch-route.mjs +54 -0
- package/src/cli/dispatch-route.test.mjs +101 -0
- package/src/cli/dispatch-slot.mjs +2 -2
- package/src/cli/dispatch-slot.test.mjs +18 -19
- package/src/cli/dispatch.test.mjs +3 -3
- package/src/cli/e2e-affected.mjs +15 -0
- package/src/cli/e2e-affected.test.mjs +73 -0
- package/src/cli/fleet.mjs +180 -0
- package/src/cli/fleet.test.mjs +213 -0
- package/src/cli/gate-slot.mjs +2 -1
- package/src/cli/gh-merge-command.mjs +96 -31
- package/src/cli/gh-merge-command.test.mjs +74 -0
- package/src/cli/gh-run.mjs +64 -3
- package/src/cli/gh-run.test.mjs +66 -1
- package/src/cli/git-hooks.test.mjs +25 -3
- package/src/cli/git-read.mjs +2 -1
- package/src/cli/integrate-tree.mjs +47 -0
- package/src/cli/integrate.mjs +143 -40
- package/src/cli/integrate.test.mjs +347 -1
- package/src/cli/integration-lane.mjs +62 -0
- package/src/cli/integration-lane.test.mjs +213 -0
- package/src/cli/ledger.mjs +4 -2
- package/src/cli/ledger.test.mjs +12 -3
- package/src/cli/legs.mjs +53 -0
- package/src/cli/legs.test.mjs +70 -0
- package/src/cli/lint-changed.mjs +59 -10
- package/src/cli/lint-changed.test.mjs +112 -1
- package/src/cli/locked-tests.mjs +134 -0
- package/src/cli/locked-tests.test.mjs +188 -0
- package/src/cli/merge-guard.mjs +234 -62
- package/src/cli/merge-guard.test.mjs +768 -39
- package/src/cli/metrics.mjs +53 -0
- package/src/cli/metrics.test.mjs +73 -0
- package/src/cli/milestone.mjs +297 -0
- package/src/cli/milestone.test.mjs +294 -0
- package/src/cli/mutate.mjs +203 -0
- package/src/cli/mutate.test.mjs +134 -0
- package/src/cli/output-redaction.mjs +26 -0
- package/src/cli/output-redaction.test.mjs +117 -0
- package/src/cli/overview.mjs +131 -0
- package/src/cli/overview.test.mjs +137 -0
- package/src/cli/pause.mjs +93 -0
- package/src/cli/pause.test.mjs +109 -0
- package/src/cli/plan-check.mjs +84 -12
- package/src/cli/plan-check.test.mjs +152 -2
- package/src/cli/plan-stop.mjs +7 -4
- package/src/cli/plan-stop.test.mjs +87 -0
- package/src/cli/platform-risk.mjs +46 -0
- package/src/cli/platform-risk.test.mjs +38 -0
- package/src/cli/plugin-skills.test.mjs +25 -0
- package/src/cli/pr-guard.mjs +128 -0
- package/src/cli/pr-guard.test.mjs +154 -0
- package/src/cli/promotion-branch.mjs +2 -0
- package/src/cli/qc.mjs +160 -27
- package/src/cli/qc.test.mjs +93 -0
- package/src/cli/report-stop.mjs +5 -0
- package/src/cli/report-stop.test.mjs +23 -1
- package/src/cli/retry-failed.mjs +3 -1
- package/src/cli/review-gate.mjs +24 -6
- package/src/cli/review-gate.test.mjs +116 -0
- package/src/cli/review-promotion.mjs +107 -0
- package/src/cli/review-promotion.test.mjs +251 -0
- package/src/cli/role-budget.mjs +50 -0
- package/src/cli/role-budget.test.mjs +91 -0
- package/src/cli/run-log.mjs +73 -0
- package/src/cli/run-log.test.mjs +121 -0
- package/src/cli/secret-patterns.mjs +16 -0
- package/src/cli/secret-patterns.test.mjs +73 -0
- package/src/cli/setup-block.mjs +24 -0
- package/src/cli/setup-block.test.mjs +96 -0
- package/src/cli/setup-checks.mjs +65 -0
- package/src/cli/setup-checks.test.mjs +158 -0
- package/src/cli/setup-compaction.mjs +55 -0
- package/src/cli/setup-compaction.test.mjs +115 -0
- package/src/cli/setup-skills.mjs +65 -0
- package/src/cli/setup-skills.test.mjs +171 -0
- package/src/cli/setup.mjs +107 -0
- package/src/cli/setup.test.mjs +275 -0
- package/src/cli/skill-length.test.mjs +8 -0
- package/src/cli/spawn-command.mjs +47 -0
- package/src/cli/spawn-command.test.mjs +64 -0
- package/src/cli/task-gate-review.test.mjs +21 -2
- package/src/cli/task-gate.mjs +25 -12
- package/src/cli/task-gate.test.mjs +43 -5
- package/src/cli/tokens.mjs +46 -3
- package/src/cli/tokens.test.mjs +103 -3
- package/src/cli/tools.mjs +49 -0
- package/src/cli/tools.test.mjs +89 -0
- package/src/cli/transcript-usage.mjs +27 -0
- package/src/cli/transcript-usage.test.mjs +34 -0
- package/src/cli/user-config-package.mjs +1 -1
- package/src/cli/user-config-toml.mjs +63 -7
- package/src/cli/user-config.mjs +39 -7
- package/src/cli/user-config.test.mjs +238 -5
- package/src/cli/watch-arm.mjs +39 -0
- package/src/cli/watch-arm.test.mjs +87 -0
- package/src/cli/watch.mjs +80 -35
- package/src/cli/watch.test.mjs +233 -27
- package/src/cli/work-order-guard.mjs +2 -1
- package/src/cli/workflow-hooks.test.mjs +18 -4
- package/src/cli/workflow-settings.mjs +44 -8
- package/src/cli/workflow-settings.test.mjs +124 -1
- package/src/cli/worktree.mjs +14 -1
- package/src/cli/worktree.test.mjs +20 -1
- package/src/config.mjs +174 -7
- package/src/config.test.mjs +327 -5
- package/src/consumer-package.mjs +31 -0
- package/src/consumer-package.test.mjs +54 -0
- package/src/descriptions.mjs +2 -0
- package/src/eslint/index.mjs +12 -0
- package/src/eslint/jsx.mjs +20 -0
- package/src/eslint/preset.mjs +26 -2
- package/src/eslint/preset.test.mjs +104 -2
- package/src/eslint/rules/no-arbitrary-class-value.mjs +32 -0
- package/src/eslint/rules/no-literal-color.mjs +94 -0
- package/src/eslint/rules/no-static-inline-style.mjs +41 -0
- package/src/eslint/rules/one-component-per-file.mjs +70 -0
- package/src/eslint/rules/raw-element-in-primitive.mjs +31 -0
- package/src/eslint/rules/timeless-comment.mjs +92 -0
- package/src/eslint/rules.test.mjs +189 -0
- package/src/eslint/ui-regressions.test.mjs +39 -0
- package/src/gates/contract-operations.mjs +3 -3
- package/src/gates/contract-operations.test.mjs +28 -0
- package/src/gates/feature-switches.mjs +19 -0
- package/src/gates/feature-switches.test.mjs +55 -0
- package/src/gates/plain-language.mjs +396 -0
- package/src/gates/plain-language.test.mjs +210 -0
- package/src/gates/runtime-imports.test.mjs +114 -0
- package/src/gates/saga-key.test.mjs +1 -1
- package/src/global-skills.mjs +9 -0
- package/src/global-skills.test.mjs +53 -0
- package/src/plugin.test.mjs +3 -2
- package/src/ui/class-sites.mjs +38 -6
- package/src/ui/codemod.mjs +4 -1
- package/src/ui/codemod.test.mjs +52 -0
- package/src/ui/components.mjs +5 -3
- package/src/ui/components.test.mjs +8 -0
- package/src/ui/dupes.test.mjs +18 -0
- package/templates/docs/enforcement.md +12 -2
- package/templates/docs/ui.md +33 -0
- package/templates/githooks/pre-commit +1 -0
- package/templates/githooks/pre-push +11 -2
- package/templates/github/workflows/ci.yml +57 -3
- package/user-config/claude/CLAUDE.core.md +28 -0
- package/user-config/claude/settings.shared.json +41 -1
- package/src/cli/screens-discovery.mjs +0 -60
- package/src/cli/screens.mjs +0 -84
- package/src/cli/stack.mjs +0 -14
- package/src/cli/throwaway-db.mjs +0 -127
- package/src/cli/throwaway-db.test.mjs +0 -83
- package/src/screens/auth-context.test.mjs +0 -41
- package/src/screens/auth-run.test.mjs +0 -147
- package/src/screens/auth.mjs +0 -223
- package/src/screens/auth.test.mjs +0 -76
- package/src/screens/capture-idle.test.mjs +0 -27
- package/src/screens/capture.browser-test.mjs +0 -73
- package/src/screens/capture.mjs +0 -175
- package/src/screens/capture.test.mjs +0 -28
- package/src/screens/catalog.browser-test.mjs +0 -32
- package/src/screens/catalog.mjs +0 -72
- package/src/screens/catalog.test.mjs +0 -36
- package/src/screens/chrome.mjs +0 -21
- package/src/screens/compare.mjs +0 -40
- package/src/screens/compare.test.mjs +0 -30
- package/src/screens/discovery.browser-test.mjs +0 -29
- package/src/screens/discovery.mjs +0 -158
- package/src/screens/discovery.test.mjs +0 -27
- package/src/screens/inventory.mjs +0 -69
- package/src/screens/inventory.test.mjs +0 -10
- package/src/screens/map.mjs +0 -63
- package/src/screens/map.test.mjs +0 -35
- package/src/screens/open.browser-test.mjs +0 -50
- package/src/screens/regions.mjs +0 -137
- package/src/screens/regions.test.mjs +0 -38
- package/src/screens/session-files.mjs +0 -39
- package/src/screens/session-files.test.mjs +0 -39
- package/src/screens/setup.browser-test.mjs +0 -49
- package/src/screens/setup.mjs +0 -78
- package/src/screens/stack-auth.browser-test.mjs +0 -30
- package/src/screens/stack-auth.mjs +0 -106
- package/src/screens/stack-auth.test.mjs +0 -65
- package/src/screens/write-guard.mjs +0 -26
- package/src/stack/stack.mjs +0 -131
- package/src/stack/stack.test.mjs +0 -40
- package/src/stack/supervisor.mjs +0 -30
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { secretPatterns } from "./secret-patterns.mjs";
|
|
2
|
+
|
|
3
|
+
const AI_LABEL = "Written by an AI agent with architecture-kit.";
|
|
4
|
+
|
|
5
|
+
/** Index: shares credential patterns and extends private-key headers to whole PEM blocks. */
|
|
6
|
+
const patterns = [
|
|
7
|
+
/-----BEGIN ([A-Z ]*PRIVATE KEY)-----[\s\S]*?(?:-----END \1-----|$)/g,
|
|
8
|
+
...secretPatterns.map(([, pattern]) => new RegExp(pattern.source, `${pattern.flags}g`)),
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
/** Fragment: redact whole credential matches and complete private-key blocks before a public post. */
|
|
12
|
+
export function redactOutput(text) {
|
|
13
|
+
for (const pattern of patterns) text = text.replace(pattern, "[REDACTED]");
|
|
14
|
+
return text;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Pipeline: redact a body and append its configured attribution once. */
|
|
18
|
+
export function postBody(text, config = {}) {
|
|
19
|
+
const redacted = redactOutput(text);
|
|
20
|
+
if (config.post?.aiLabel === false) return redacted;
|
|
21
|
+
let body = redacted.trimEnd();
|
|
22
|
+
while (body === AI_LABEL || body.endsWith(`\n${AI_LABEL}`)) {
|
|
23
|
+
body = body.slice(0, -AI_LABEL.length).trimEnd();
|
|
24
|
+
}
|
|
25
|
+
return body ? `${body}\n\n${AI_LABEL}` : AI_LABEL;
|
|
26
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { redactOutput, postBody } from "./output-redaction.mjs";
|
|
4
|
+
|
|
5
|
+
const label = "Written by an AI agent with architecture-kit.";
|
|
6
|
+
const pemHeader = ["-----BEGIN RSA", "PRIVATE KEY-----"].join(" ");
|
|
7
|
+
const secrets = [
|
|
8
|
+
`AKIA${"A".repeat(16)}`,
|
|
9
|
+
`ASIA${"B".repeat(16)}`,
|
|
10
|
+
`${["aws_secret", "_access_key"].join("")} = ${"a".repeat(40)}`,
|
|
11
|
+
...["p", "o", "u", "s", "r"].map((kind) => `gh${kind}_${"a".repeat(40)}`),
|
|
12
|
+
`github_pat_${"b".repeat(50)}`,
|
|
13
|
+
`${pemHeader}\nkey material\n-----END RSA PRIVATE KEY-----`,
|
|
14
|
+
`sk-ant-${"c".repeat(30)}`,
|
|
15
|
+
...["a", "b", "p", "r", "s"].map((kind) => `xox${kind}-${"d".repeat(20)}`),
|
|
16
|
+
`npm_${"e".repeat(36)}`,
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
test("redaction replaces every shared secret shape and each repeated occurrence", () => {
|
|
20
|
+
for (const secret of secrets) {
|
|
21
|
+
assert.equal(redactOutput(`Before (${secret}), after: ${secret}.`), "Before ([REDACTED]), after: [REDACTED].");
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("redaction removes complete PEM blocks across lines and on one line", () => {
|
|
26
|
+
for (const separator of ["\n", "\r\n", " ", ""]) {
|
|
27
|
+
const key = `${pemHeader}${separator}MIIEowIBAAKCAQEA7b+/abc=${separator}-----END RSA PRIVATE KEY-----`;
|
|
28
|
+
assert.equal(redactOutput(`Before ${key} after`), "Before [REDACTED] after");
|
|
29
|
+
assert.equal(redactOutput(`${key}\n${key}`), "[REDACTED]\n[REDACTED]");
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("redaction consumes an unterminated PEM block through the end of the text", () => {
|
|
34
|
+
for (const kind of ["", "RSA ", "EC ", "ENCRYPTED "]) {
|
|
35
|
+
const key = `-----BEGIN ${kind}PRIVATE KEY-----\nMIIEowIBAAKCAQEA7b+/abc=\ntrailing text`;
|
|
36
|
+
assert.equal(redactOutput(`Before ${key}`), "Before [REDACTED]");
|
|
37
|
+
}
|
|
38
|
+
assert.equal(redactOutput(`${pemHeader}\nmaterial\n-----END EC PRIVATE KEY-----\nmore`), "[REDACTED]");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("redaction finds PEM headers after earlier dashes and adjacent word characters", () => {
|
|
42
|
+
for (const prefix of ["out: ----- x ", "key", "7", "_"]) {
|
|
43
|
+
const key = `${pemHeader}\nmaterial\n-----END RSA PRIVATE KEY-----`;
|
|
44
|
+
assert.equal(redactOutput(`${prefix}${key} after`), `${prefix}[REDACTED] after`);
|
|
45
|
+
assert.equal(redactOutput(`${prefix}${pemHeader}`), `${prefix}[REDACTED]`);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("redaction removes AWS secret assignments including the complete value", () => {
|
|
50
|
+
const value = ["wJalrXUtnFEMI", "K7MDENG", "bPxRfiCYEXAMPLEKEY"].join("/");
|
|
51
|
+
assert.equal(value.length, 40);
|
|
52
|
+
const assignment = `aws_secret_access_key = ${value}`;
|
|
53
|
+
assert.equal(redactOutput(`Before ${assignment}\n${assignment}\nafter`), "Before [REDACTED]\n[REDACTED]\nafter");
|
|
54
|
+
assert.equal(redactOutput(`qc-allow-secret ${assignment}`), "qc-allow-secret [REDACTED]");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("redaction covers prefixed AWS names, CLI whitespace, and arrow assignments", () => {
|
|
58
|
+
const value = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
|
|
59
|
+
for (const [assignment, expected] of [
|
|
60
|
+
[`TF_VAR_aws_secret_access_key=${value}`, "TF_VAR_[REDACTED]"],
|
|
61
|
+
[`PROD_AWS_SECRET_ACCESS_KEY=${value}`, "PROD_[REDACTED]"],
|
|
62
|
+
[`aws configure set aws_secret_access_key ${value}`, "aws configure set [REDACTED]"],
|
|
63
|
+
[`"aws_secret_access_key" => "${value}"`, '"[REDACTED]"'],
|
|
64
|
+
]) {
|
|
65
|
+
assert.equal(redactOutput(`Before ${assignment}\n${assignment}\nafter`), `Before ${expected}\n${expected}\nafter`);
|
|
66
|
+
assert.equal(redactOutput(`qc-allow-secret ${assignment}`), `qc-allow-secret ${expected}`);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("redaction preserves public text and ignores commit allow directives", () => {
|
|
71
|
+
const publicText = "# Public title\n\nRefs #195; ordinary-code_identifier.\r\n";
|
|
72
|
+
assert.equal(redactOutput(publicText), publicText);
|
|
73
|
+
assert.equal(redactOutput(`qc-allow-secret ${secrets[3]}\n${secrets[0]}`), "qc-allow-secret [REDACTED]\n[REDACTED]");
|
|
74
|
+
assert.equal(redactOutput(`qc-allow-secret${secrets[3]}`), "qc-allow-secret[REDACTED]");
|
|
75
|
+
assert.equal(redactOutput(""), "");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("redaction removes AWS operator assignments from the review probes", () => {
|
|
79
|
+
const value = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
|
|
80
|
+
for (const name of ["AWS_SECRET_ACCESS_KEY", "aws_secret_access_key"]) {
|
|
81
|
+
for (const separator of [":=", "?=", "+=", "!=", "-->", "=:=", "<-"]) {
|
|
82
|
+
for (const spacing of ["", " ", "\t"]) {
|
|
83
|
+
const assignment = `${name}${spacing}${separator}${spacing}${value}`;
|
|
84
|
+
assert.equal(redactOutput(`Before ${assignment}\n${assignment}\nafter`), "Before [REDACTED]\n[REDACTED]\nafter");
|
|
85
|
+
assert.equal(redactOutput(`qc-allow-secret ${assignment}`), "qc-allow-secret [REDACTED]");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("bodies redact secrets and append the AI label by default", () => {
|
|
92
|
+
assert.equal(postBody(`Body: ${secrets[3]}`), `Body: [REDACTED]\n\n${label}`);
|
|
93
|
+
assert.equal(postBody(""), label);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("disabling attribution keeps redaction enabled", () => {
|
|
97
|
+
assert.equal(postBody(`Body: ${secrets[3]}`, { post: { aiLabel: false } }), "Body: [REDACTED]");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("attribution occurs once and ends the body across repeated calls and line endings", () => {
|
|
101
|
+
for (const body of [`Body\n\n${label}`, `Body\r\n${label}\r\n`, `Body\n${label}\n${label}`]) {
|
|
102
|
+
const posted = postBody(body);
|
|
103
|
+
assert.ok(posted.endsWith(label));
|
|
104
|
+
assert.equal(posted.split(label).length - 1, 1);
|
|
105
|
+
assert.equal(postBody(posted), posted);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("attribution preserves quoted and leading labels and strips only trailing label lines", () => {
|
|
110
|
+
for (const body of [`Quote: "${label}" was here`, `${label}\nBody`, `Body ${label}`, `${label} extra`]) {
|
|
111
|
+
const posted = `${body}\n\n${label}`;
|
|
112
|
+
assert.equal(postBody(body), posted);
|
|
113
|
+
assert.equal(postBody(`${body}\r\n${label}\r\n\r\n${label}\r\n`), posted);
|
|
114
|
+
assert.equal(postBody(posted), posted);
|
|
115
|
+
}
|
|
116
|
+
assert.equal(postBody(`${label}\r\n${label}\r\n`), label);
|
|
117
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Repository summaries and the debt review watermark; the workflow ledger stays read-only.
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { repoFiles } from "./repo-files.mjs";
|
|
6
|
+
import { commonDirOf } from "./ledger.mjs";
|
|
7
|
+
import { openFailures } from "./ci-failures.mjs";
|
|
8
|
+
import { runGhResult } from "./gh-run.mjs";
|
|
9
|
+
|
|
10
|
+
const line = (text) => String(text).replace(/\s+/g, " ").trim().slice(0, 100);
|
|
11
|
+
const commands = (root, options) => ({
|
|
12
|
+
git: options.git ?? ((args, encoding = "utf8") => {
|
|
13
|
+
const output = execFileSync("git", args, { cwd: root, encoding, maxBuffer: 64 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"] });
|
|
14
|
+
return encoding ? output.trim() : output;
|
|
15
|
+
}),
|
|
16
|
+
gh: options.gh ?? ((args) => {
|
|
17
|
+
const result = runGhResult(args, { cwd: root });
|
|
18
|
+
if (result.status !== 0) throw new Error(result.stderr?.trim() || result.error?.message || "gh failed");
|
|
19
|
+
return result.stdout;
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
const sourceFile = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
23
|
+
const lockfile = /(?:^|\/)(?:package-lock\.json|npm-shrinkwrap\.json|pnpm-lock\.yaml|(?:yarn|bun)\.lockb?|[^/]+\.lock|go\.sum)$/;
|
|
24
|
+
|
|
25
|
+
function decodeText(bytes) {
|
|
26
|
+
try { return bytes.includes(0) ? null : new TextDecoder("utf-8", { fatal: true }).decode(bytes); }
|
|
27
|
+
catch { return null; }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function textFiles(root) {
|
|
31
|
+
const files = new Map();
|
|
32
|
+
for (const file of await repoFiles(root)) {
|
|
33
|
+
if (lockfile.test(file)) continue;
|
|
34
|
+
try {
|
|
35
|
+
const text = decodeText(readFileSync(path.join(root, file)));
|
|
36
|
+
if (text !== null) files.set(file, text);
|
|
37
|
+
} catch { /* Missing files and binary encodings have no text ranking. */ }
|
|
38
|
+
}
|
|
39
|
+
return files;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function churn(git, files, ref) {
|
|
43
|
+
const counts = new Map();
|
|
44
|
+
for (const file of git(["log", "--since=90.days", "--format=", "--name-only", "-z", "--no-renames", ref, "--"]).split("\0")) {
|
|
45
|
+
if (sourceFile.test(file) && files.has(file)) counts.set(file, (counts.get(file) ?? 0) + 1);
|
|
46
|
+
}
|
|
47
|
+
return [...counts].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function buildOverview(root, options = {}) {
|
|
51
|
+
const { git, gh } = commands(root, options), files = await textFiles(root);
|
|
52
|
+
const modules = [...(files.get("docs/architecture.md") ?? "").matchAll(/^#{2,3}\s+(.+)$/gm)].map((match) => match[1]);
|
|
53
|
+
const largest = [...files].filter(([file]) => sourceFile.test(file)).map(([file, text]) => [file, text.trimEnd().split("\n").length])
|
|
54
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
|
|
55
|
+
const branch = git(["branch", "--show-current"]);
|
|
56
|
+
let debt;
|
|
57
|
+
try { debt = JSON.parse(gh(["issue", "list", "--state", "open", "--label", "debt", "--limit", "1000", "--json", "title"])).map((issue) => issue.title); }
|
|
58
|
+
catch (error) { debt = [`unavailable: ${error.message}`]; }
|
|
59
|
+
const sections = [
|
|
60
|
+
["Modules", modules.length ? modules : ["docs/architecture.md has no module headings"]],
|
|
61
|
+
["Largest", largest.map(([file, n]) => `${file}: ${n} lines`)],
|
|
62
|
+
["Churn", churn(git, files, "HEAD").map(([file, n]) => `${file}: ${n} touches`)],
|
|
63
|
+
["CI failures", openFailures(root, branch).map((r) => `${r.file ?? r.job ?? "CI"}: ${r.test} ${r.error ?? ""}`)],
|
|
64
|
+
["Debt", debt],
|
|
65
|
+
];
|
|
66
|
+
return sections.flatMap(([title, items]) => [title, ...(items.length ? items : ["None"]).slice(0, 10).map((item) => `- ${line(item)}`.slice(0, 100))]).join("\n") + "\n";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function debtItems(report) {
|
|
70
|
+
let fenced = false;
|
|
71
|
+
const text = report.split(/\r?\n/).filter((row) => {
|
|
72
|
+
if (/^\s*(```|~~~)/.test(row)) { fenced = !fenced; return false; }
|
|
73
|
+
return !fenced;
|
|
74
|
+
}).join("\n");
|
|
75
|
+
const items = [];
|
|
76
|
+
for (const section of text.split(/^## Debt\s*$/m).slice(1)) {
|
|
77
|
+
const body = section.split(/^#{1,2}\s/m)[0].trim();
|
|
78
|
+
if (body.startsWith("None")) continue;
|
|
79
|
+
for (const match of body.matchAll(/^- (.+)((?:\n(?!- ).*)*)/gm)) {
|
|
80
|
+
const title = match[1].split(".")[0].trim().slice(0, 80);
|
|
81
|
+
if (title) items.push({ title, body: match[0].slice(2).trim() });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return items;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function reviewFile(root) {
|
|
88
|
+
const common = commonDirOf(root);
|
|
89
|
+
if (!common) throw new Error("no git common directory");
|
|
90
|
+
return path.join(common, "qc", "debt-review.json");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function fileDebt(root, report, options = {}) {
|
|
94
|
+
const items = debtItems(readFileSync(path.resolve(root, report), "utf8"));
|
|
95
|
+
const { git, gh } = commands(root, options), file = reviewFile(root);
|
|
96
|
+
const main = git(["rev-parse", "--verify", "origin/main"]);
|
|
97
|
+
const titles = new Set(JSON.parse(gh(["issue", "list", "--state", "open", "--limit", "1000", "--json", "title"])).map((issue) => issue.title));
|
|
98
|
+
for (const item of items) {
|
|
99
|
+
if (titles.has(item.title)) continue;
|
|
100
|
+
gh(["issue", "create", "--label", "debt", "--title", item.title, "--body", item.body]);
|
|
101
|
+
titles.add(item.title);
|
|
102
|
+
}
|
|
103
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
104
|
+
writeFileSync(file, JSON.stringify({ main }) + "\n");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function debtDue(root, options = {}) {
|
|
108
|
+
try {
|
|
109
|
+
const { git } = commands(root, options);
|
|
110
|
+
let main;
|
|
111
|
+
try { ({ main } = JSON.parse(readFileSync(reviewFile(root), "utf8"))); }
|
|
112
|
+
catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
113
|
+
if (main !== undefined) {
|
|
114
|
+
if (!/^[a-f0-9]{40,64}$/.test(main)) throw new Error("invalid debt review SHA");
|
|
115
|
+
git(["merge-base", "--is-ancestor", main, "origin/main"]);
|
|
116
|
+
}
|
|
117
|
+
const subjects = git(["log", "--first-parent", "--merges", "--format=%s", main ? `${main}..origin/main` : "origin/main", "--"]);
|
|
118
|
+
const count = subjects.split("\n").filter((subject) => /(?: from [^\s/]+\/milestone\/\d{4}-\d{2}-\d{2}-\d+$|^Merge branch 'milestone\/\d{4}-\d{2}-\d{2}-\d+'$)/.test(subject)).length;
|
|
119
|
+
if (count < 10) { (options.log ?? console.log)(`not due: ${count} of 10`); return 1; }
|
|
120
|
+
const files = new Set(git(["ls-tree", "-r", "--name-only", "-z", "origin/main"]).split("\0"));
|
|
121
|
+
const top = churn(git, files, "origin/main").find(([file]) => {
|
|
122
|
+
return decodeText(git(["show", `origin/main:${file}`], null)) !== null;
|
|
123
|
+
});
|
|
124
|
+
if (!top) throw new Error("no source text files in the churn window");
|
|
125
|
+
(options.log ?? console.log)(`due: ${line(path.posix.dirname(top[0]))}`);
|
|
126
|
+
return 0;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
(options.error ?? console.error)(`debt: ${line(error.message)}`);
|
|
129
|
+
return 3;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync, existsSync } from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { buildOverview, fileDebt, debtDue } from "./overview.mjs";
|
|
8
|
+
|
|
9
|
+
function fixture(t) {
|
|
10
|
+
const root = mkdtempSync(path.join(os.tmpdir(), "qc-overview-"));
|
|
11
|
+
t.after(() => rmSync(root, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }));
|
|
12
|
+
const git = (args) => execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
13
|
+
git(["init", "-q", "-b", "main"]);
|
|
14
|
+
git(["config", "user.name", "Overview test"]);
|
|
15
|
+
git(["config", "user.email", process.env.GIT_AUTHOR_EMAIL || "overview@example.test"]);
|
|
16
|
+
git(["remote", "add", "origin", "https://github.com/local/repo.git"]);
|
|
17
|
+
git(["commit", "-q", "--allow-empty", "-m", "base"]);
|
|
18
|
+
git(["update-ref", "refs/remotes/origin/main", "HEAD"]);
|
|
19
|
+
const put = (file, text) => { mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); writeFileSync(path.join(root, file), text); };
|
|
20
|
+
return { root, git, put, mark: path.join(root, ".git/qc/debt-review.json") };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test("overview prints at most 60 lines on a repo with 200 files", async (t) => {
|
|
24
|
+
const { root, git, put } = fixture(t);
|
|
25
|
+
for (let i = 0; i < 200; i++) put(`src/mod/file${i}.mjs`, "x\n".repeat(i + 1));
|
|
26
|
+
put("docs/architecture.md", "# Architecture\n" + "## Module\n".repeat(20));
|
|
27
|
+
put("docs/large.md", "docs\n".repeat(500));
|
|
28
|
+
put("package-lock.json", "x\n".repeat(300));
|
|
29
|
+
put("src/binary.bin", Buffer.from([0, 255, 10]));
|
|
30
|
+
put("src/binary.mjs", Buffer.from([0, 255, 10]));
|
|
31
|
+
if (process.platform !== "win32") put("src/line\nbreak.mjs", "x\n".repeat(250));
|
|
32
|
+
git(["add", "."]); git(["commit", "-qm", "files"]);
|
|
33
|
+
put("src/mod/file0.mjs", "changed\n");
|
|
34
|
+
put("docs/large.md", "updated docs\n".repeat(500));
|
|
35
|
+
git(["add", "."]); git(["commit", "-qm", "touch source and docs"]);
|
|
36
|
+
put(".git/qc/ledger.jsonl", [
|
|
37
|
+
{ type: "ci-failure", id: "local", repository: "local/repo", branch: "main", test: "broken\ntest" },
|
|
38
|
+
{ type: "ci-failure", id: "foreign", repository: "other/repo", branch: "main", test: "foreign failure" },
|
|
39
|
+
{ type: "ci-failure", id: "fixed", repository: "local/repo", branch: "main", test: "fixed failure" },
|
|
40
|
+
{ type: "ci-resolution", branch: "main", ids: ["fixed"] },
|
|
41
|
+
].map((r) => JSON.stringify(r)).join("\n") + "\n");
|
|
42
|
+
const output = await buildOverview(root, { gh: () => JSON.stringify(Array.from({ length: 20 }, () => ({ title: "debt\n" + "x".repeat(150) }))) });
|
|
43
|
+
const lines = output.trimEnd().split("\n");
|
|
44
|
+
assert.ok(lines.length <= 60);
|
|
45
|
+
for (const section of ["Modules", "Largest", "Churn", "CI failures", "Debt"]) assert.ok(lines.includes(section));
|
|
46
|
+
assert.ok(lines.every((line) => line.length <= 100));
|
|
47
|
+
for (const section of output.split(/^(?:Modules|Largest|Churn|CI failures|Debt)$/m).slice(1)) assert.ok(section.trim().split("\n").length <= 10);
|
|
48
|
+
const sectionRows = (name, next) => output.split(`${name}\n`)[1].split(`\n${next}\n`)[0].split("\n");
|
|
49
|
+
if (process.platform !== "win32") {
|
|
50
|
+
assert.deepEqual(sectionRows("Largest", "Churn").slice(0, 2), ["- src/line break.mjs: 250 lines", "- src/mod/file199.mjs: 200 lines"]);
|
|
51
|
+
assert.deepEqual(sectionRows("Churn", "CI failures").slice(0, 2), ["- src/mod/file0.mjs: 2 touches", "- src/line break.mjs: 1 touches"]);
|
|
52
|
+
}
|
|
53
|
+
assert.doesNotMatch(sectionRows("Largest", "Churn").join("\n") + sectionRows("Churn", "CI failures").join("\n"), /docs\//);
|
|
54
|
+
assert.match(output, /broken test/);
|
|
55
|
+
assert.doesNotMatch(output, /package-lock|binary\.(?:bin|mjs)|foreign failure|fixed failure/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("debt file skips an item whose title is already an open issue", async (t) => {
|
|
59
|
+
const { root, put, git, mark } = fixture(t);
|
|
60
|
+
const title = "x".repeat(80), calls = [];
|
|
61
|
+
put("report.md", `## Debt\n- Existing. Details\n- New $(touch pwned). More\n continuation\n- New $(touch pwned). Repeated\n- ${title} extra. Body\n## Other\n- Ignore\n`);
|
|
62
|
+
const linked = path.join(root, "linked");
|
|
63
|
+
git(["worktree", "add", "-qb", "review", linked]);
|
|
64
|
+
await fileDebt(linked, path.join(root, "report.md"), { gh: (args) => { calls.push(args); return args[1] === "list" ? '[{"title":"Existing"}]' : "created"; } });
|
|
65
|
+
assert.deepEqual(calls[0], ["issue", "list", "--state", "open", "--limit", "1000", "--json", "title"]);
|
|
66
|
+
assert.deepEqual(calls.slice(1).map((args) => args[args.indexOf("--title") + 1]), ["New $(touch pwned)", title]);
|
|
67
|
+
assert.match(calls[1][calls[1].indexOf("--body") + 1], /continuation/);
|
|
68
|
+
assert.ok(calls.slice(1).every((args) => args.includes("debt")));
|
|
69
|
+
assert.deepEqual(JSON.parse(readFileSync(mark, "utf8")), { main: git(["rev-parse", "origin/main"]) });
|
|
70
|
+
assert.equal(existsSync(path.join(root, "pwned")), false);
|
|
71
|
+
assert.equal(existsSync(path.join(root, ".git/qc/ledger.jsonl")), false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("debt due counts first-parent milestone merges and resets after filing", async (t) => {
|
|
75
|
+
const { root, git, put } = fixture(t), output = [];
|
|
76
|
+
const base = git(["rev-parse", "HEAD"]);
|
|
77
|
+
const check = () => debtDue(root, { log: (line) => output.push(line) });
|
|
78
|
+
assert.equal(await check(), 1); assert.equal(output.pop(), "not due: 0 of 10");
|
|
79
|
+
git(["commit", "-q", "--allow-empty", "-m", "mention milestone/not-a-merge"]);
|
|
80
|
+
git(["checkout", "-qb", "feature/side"]);
|
|
81
|
+
git(["checkout", "-qb", "milestone/side"]); git(["commit", "-q", "--allow-empty", "-m", "side"]);
|
|
82
|
+
git(["checkout", "-q", "feature/side"]); git(["merge", "--no-ff", "-qm", "Merge milestone/side", "milestone/side"]);
|
|
83
|
+
git(["checkout", "-q", "main"]); git(["merge", "--no-ff", "-qm", "Merge feature/side", "feature/side"]);
|
|
84
|
+
git(["update-ref", "refs/remotes/origin/main", "HEAD"]);
|
|
85
|
+
assert.equal(await check(), 1); assert.equal(output.pop(), "not due: 0 of 10");
|
|
86
|
+
for (let i = 0; i < 10; i++) {
|
|
87
|
+
const branch = `feature/milestone/${i}`;
|
|
88
|
+
git(["checkout", "-qb", branch]);
|
|
89
|
+
git(["commit", "-q", "--allow-empty", "-m", "feature"]);
|
|
90
|
+
git(["checkout", "-q", "main"]);
|
|
91
|
+
git(["merge", "--no-ff", "-qm", `Merge branch '${branch}'`, branch]);
|
|
92
|
+
}
|
|
93
|
+
git(["update-ref", "refs/remotes/origin/main", "HEAD"]);
|
|
94
|
+
assert.equal(await check(), 1); assert.equal(output.pop(), "not due: 0 of 10");
|
|
95
|
+
for (let i = 0; i < 10; i++) {
|
|
96
|
+
git(["checkout", "-qb", `milestone/${i}`]);
|
|
97
|
+
put("src/hot/file.mjs", `export const replacement = '�'; // ${i}\n`);
|
|
98
|
+
put("docs/hottest.md", `${i}\n`);
|
|
99
|
+
put("src/aaa/binary.mjs", Buffer.from(i % 2 ? [255, i] : [0, i]));
|
|
100
|
+
put("src/deleted/file.ts", `${i}\n`);
|
|
101
|
+
if (i === 0) put("src/cold/file.ts", "cold\n");
|
|
102
|
+
git(["add", "."]); git(["commit", "-qm", "change"]);
|
|
103
|
+
git(["checkout", "-q", "main"]); git(["merge", "--no-ff", "-qm", (i % 2 ? "Merge pull request #1 from local/milestone/2026-11-23-1" : "Merge branch 'milestone/2026-11-23-1'"), `milestone/${i}`]);
|
|
104
|
+
git(["update-ref", "refs/remotes/origin/main", "HEAD"]);
|
|
105
|
+
if (i === 8) { assert.equal(await check(), 1); assert.equal(output.pop(), "not due: 9 of 10"); }
|
|
106
|
+
}
|
|
107
|
+
git(["rm", "-q", "src/deleted/file.ts"]); git(["commit", "-qm", "remove old source"]);
|
|
108
|
+
git(["update-ref", "refs/remotes/origin/main", "HEAD"]);
|
|
109
|
+
git(["checkout", "-q", "--detach", base]);
|
|
110
|
+
put("src/checkout-only/file.ts", "checkout only\n");
|
|
111
|
+
assert.equal(await check(), 0); assert.equal(output.pop(), "due: src/hot");
|
|
112
|
+
put("report.md", "## Debt\nNone.\n- Ignored\n");
|
|
113
|
+
await fileDebt(root, "report.md", { gh: () => "[]" });
|
|
114
|
+
assert.equal(await check(), 1); assert.equal(output.pop(), "not due: 0 of 10");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("debt commands preserve the watermark on errors and due exits 3", async (t) => {
|
|
118
|
+
const { root, put, mark } = fixture(t);
|
|
119
|
+
put("report.md", "## Debt\n- One. Body\n- Two. Body\n");
|
|
120
|
+
const before = '{"main":"saved"}\n'; put(".git/qc/debt-review.json", before);
|
|
121
|
+
let creates = 0;
|
|
122
|
+
await assert.rejects(() => fileDebt(root, "report.md", { gh: (args) => {
|
|
123
|
+
if (args[1] === "list") return "[]";
|
|
124
|
+
if (++creates === 2) throw new Error("gh failed");
|
|
125
|
+
return "created";
|
|
126
|
+
} }), /gh failed/);
|
|
127
|
+
assert.equal(readFileSync(mark, "utf8"), before);
|
|
128
|
+
await assert.rejects(() => fileDebt(root, "missing.md"), /ENOENT/);
|
|
129
|
+
await assert.rejects(() => fileDebt(root, "report.md", { gh: () => { throw new Error("list failed"); } }), /list failed/);
|
|
130
|
+
assert.equal(creates, 2);
|
|
131
|
+
const errors = [];
|
|
132
|
+
assert.equal(await debtDue(root, { error: (line) => errors.push(line) }), 3);
|
|
133
|
+
assert.ok(errors.length);
|
|
134
|
+
rmSync(mark);
|
|
135
|
+
assert.equal(await debtDue(root, { git: () => { throw new Error("git failed"); }, error: (line) => errors.push(line) }), 3);
|
|
136
|
+
assert.equal(errors.at(-1), "debt: git failed");
|
|
137
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { commonDirOf } from "./ledger.mjs";
|
|
5
|
+
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
6
|
+
|
|
7
|
+
function holdFile(root, branch) {
|
|
8
|
+
if (!branch || branch === "." || branch === ".." || /[\\\x00-\x1f]/.test(branch)) throw new Error("invalid branch for CI hold");
|
|
9
|
+
const common = commonDirOf(checkoutRootOf(root) ?? root);
|
|
10
|
+
if (!common) throw new Error("CI hold needs a Git checkout");
|
|
11
|
+
return path.join(common, "qc", "holds", branch.replaceAll("/", "-"));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function holdBranch(root, branch, reason) {
|
|
15
|
+
const file = holdFile(root, branch);
|
|
16
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
17
|
+
writeFileSync(file, `${reason}\n`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function holdRefusal(root, branch) {
|
|
21
|
+
let reason;
|
|
22
|
+
try {
|
|
23
|
+
reason = readFileSync(holdFile(root, branch), "utf8").trim();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.code === "ENOENT") return null;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
return `Operator: branch ${branch} is on hold: ${reason}. Wait for the operator to run \`qc resume --branch ${branch}\`.`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function pauseFile(root) {
|
|
32
|
+
const common = execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
|
|
33
|
+
cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 5000,
|
|
34
|
+
}).trim();
|
|
35
|
+
return path.join(common, "qc", "pause");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Fragment: read the operator pause shared by all worktrees. */
|
|
39
|
+
export function pauseState(root) {
|
|
40
|
+
const file = pauseFile(root);
|
|
41
|
+
try {
|
|
42
|
+
return readFileSync(file, "utf8").trim() || "operator pause";
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error.code === "ENOENT") return null;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Branch: refuse a command while the operator pause exists. */
|
|
50
|
+
export function pauseRefusal(root) {
|
|
51
|
+
const reason = pauseState(root);
|
|
52
|
+
return reason === null ? null : `An operator paused the kit: ${reason}. Wait for the operator to run \`qc resume\`.`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Trigger: set or inspect the operator pause. */
|
|
56
|
+
export function runPause(root, argv, { say = console.log } = {}) {
|
|
57
|
+
try {
|
|
58
|
+
if (argv.length === 1 && argv[0] === "--status") {
|
|
59
|
+
const reason = pauseState(root);
|
|
60
|
+
say(reason === null ? "running" : `paused: ${reason}`);
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
if (argv.length !== 0 && (argv.length !== 2 || argv[0] !== "--reason" || !argv[1].trim())) {
|
|
64
|
+
say("usage: qc pause [--reason <text>] | qc pause --status");
|
|
65
|
+
return 2;
|
|
66
|
+
}
|
|
67
|
+
const file = pauseFile(root);
|
|
68
|
+
const reason = argv[1]?.trim() || "operator pause";
|
|
69
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
70
|
+
writeFileSync(file, `${reason}\n`);
|
|
71
|
+
say("The kit is paused. Run `qc resume` to lift the pause.");
|
|
72
|
+
return 0;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
say(`qc pause: ${error.message}`);
|
|
75
|
+
return 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Trigger: clear the operator pause. */
|
|
80
|
+
export function runResume(root, argv, { say = console.log } = {}) {
|
|
81
|
+
if (argv.length !== 0 && (argv.length !== 2 || argv[0] !== "--branch" || !argv[1])) {
|
|
82
|
+
say("usage: qc resume [--branch <branch>]");
|
|
83
|
+
return 2;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
rmSync(argv.length ? holdFile(root, argv[1]) : pauseFile(root), { force: true });
|
|
87
|
+
say("running");
|
|
88
|
+
return 0;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
say(`qc resume: ${error.message}`);
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import childProcess, { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
4
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { test } from "node:test";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { pauseRefusal, pauseState, runPause, runResume, holdBranch, holdRefusal } from "./pause.mjs";
|
|
10
|
+
|
|
11
|
+
const QC = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
12
|
+
|
|
13
|
+
function repository(t) {
|
|
14
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), "qc-pause-"));
|
|
15
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
16
|
+
const root = path.join(dir, "repo");
|
|
17
|
+
execFileSync("git", ["init", "-q", root]);
|
|
18
|
+
execFileSync("git", ["-C", root, "-c", "user.name=Test", "-c", "user.email=test@example.com", "-c", "commit.gpgsign=false", "commit", "--allow-empty", "-qm", "init"]);
|
|
19
|
+
return { dir, root };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
test("branch holds share the common Git directory and resume works from a nested worktree folder", (t) => {
|
|
23
|
+
const { dir, root } = repository(t);
|
|
24
|
+
const linked = path.join(dir, "linked");
|
|
25
|
+
execFileSync("git", ["-C", root, "worktree", "add", "-qb", "fix/ci", linked]);
|
|
26
|
+
const nested = path.join(linked, "nested");
|
|
27
|
+
mkdirSync(nested);
|
|
28
|
+
holdBranch(root, "fix/ci", "three failed heads");
|
|
29
|
+
assert.equal(readFileSync(path.join(root, ".git", "qc", "holds", "fix-ci"), "utf8"), "three failed heads\n");
|
|
30
|
+
assert.match(holdRefusal(linked, "fix/ci"), /Operator:/);
|
|
31
|
+
assert.match(holdRefusal(linked, "fix/ci"), /Wait for the operator to run `qc resume --branch fix\/ci`\./);
|
|
32
|
+
assert.match(holdRefusal(nested, "fix/ci"), /three failed heads/);
|
|
33
|
+
assert.equal(holdRefusal(root, "another"), null);
|
|
34
|
+
assert.equal(runResume(nested, ["--branch", "fix/ci"], { say: () => {} }), 0);
|
|
35
|
+
assert.equal(holdRefusal(root, "fix/ci"), null);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("pause and resume share the common git folder across worktrees and nested folders", (t) => {
|
|
39
|
+
const { dir, root } = repository(t);
|
|
40
|
+
const worktree = path.join(dir, "linked");
|
|
41
|
+
execFileSync("git", ["-C", root, "worktree", "add", "-qb", "linked", worktree]);
|
|
42
|
+
const nested = path.join(worktree, "nested");
|
|
43
|
+
mkdirSync(nested);
|
|
44
|
+
const lines = [];
|
|
45
|
+
const say = (line) => lines.push(line);
|
|
46
|
+
assert.equal(pauseState(nested), null);
|
|
47
|
+
assert.equal(runPause(root, ["--reason", "operator investigation"], { say }), 0);
|
|
48
|
+
assert.equal(lines.at(-1), "The kit is paused. Run `qc resume` to lift the pause.");
|
|
49
|
+
assert.equal(readFileSync(path.join(root, ".git", "qc", "pause"), "utf8").trim(), "operator investigation");
|
|
50
|
+
assert.equal(pauseState(nested), "operator investigation");
|
|
51
|
+
assert.match(pauseRefusal(nested), /operator investigation.*qc resume/);
|
|
52
|
+
assert.equal(pauseRefusal(nested), "An operator paused the kit: operator investigation. Wait for the operator to run `qc resume`.");
|
|
53
|
+
assert.equal(runPause(nested, ["--status"], { say }), 0);
|
|
54
|
+
assert.match(lines.at(-1), /paused.*operator investigation/);
|
|
55
|
+
assert.equal(runResume(nested, [], { say }), 0);
|
|
56
|
+
assert.equal(pauseRefusal(root), null);
|
|
57
|
+
assert.equal(runResume(root, [], { say }), 0);
|
|
58
|
+
assert.equal(runPause(root, ["--status"], { say }), 0);
|
|
59
|
+
assert.match(lines.at(-1), /running/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("the CLI pauses and resumes even when the repository config is invalid", (t) => {
|
|
63
|
+
const { root } = repository(t);
|
|
64
|
+
writeFileSync(path.join(root, "qc.config.json"), "invalid JSON");
|
|
65
|
+
const call = (...args) => spawnSync(process.execPath, [QC, ...args], { cwd: root, encoding: "utf8" });
|
|
66
|
+
const paused = call("pause");
|
|
67
|
+
assert.equal(paused.status, 0);
|
|
68
|
+
assert.equal(paused.stdout.trim(), "The kit is paused. Run `qc resume` to lift the pause.");
|
|
69
|
+
assert.match(call("pause", "--status").stdout, /paused.*operator pause/);
|
|
70
|
+
assert.equal(call("resume").status, 0);
|
|
71
|
+
assert.match(call("pause", "--status").stdout, /running/);
|
|
72
|
+
assert.equal(existsSync(path.join(root, ".git", "qc", "pause")), false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("invalid pause and resume arguments leave the pause unchanged", (t) => {
|
|
76
|
+
const { root } = repository(t);
|
|
77
|
+
const say = () => {};
|
|
78
|
+
for (const argv of [["--reason"], ["--unknown"], ["--status", "--reason", "x"], ["--reason", " "]]) {
|
|
79
|
+
assert.equal(runPause(root, argv, { say }), 2);
|
|
80
|
+
assert.equal(pauseState(root), null);
|
|
81
|
+
}
|
|
82
|
+
assert.equal(runPause(root, ["--reason", "keep"], { say }), 0);
|
|
83
|
+
assert.equal(runResume(root, ["--unknown"], { say }), 2);
|
|
84
|
+
assert.equal(pauseState(root), "keep");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("pause read errors refuse operation instead of treating the repository as running", (t) => {
|
|
88
|
+
const { root } = repository(t);
|
|
89
|
+
mkdirSync(path.join(root, ".git", "qc", "pause"), { recursive: true });
|
|
90
|
+
assert.throws(() => pauseState(root));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("the pause git lookup has a bounded timeout", (t) => {
|
|
94
|
+
const { root } = repository(t);
|
|
95
|
+
const original = childProcess.execFileSync;
|
|
96
|
+
const lookup = t.mock.method(childProcess, "execFileSync", (command, args, options) => {
|
|
97
|
+
assert.equal(command, "git");
|
|
98
|
+
assert.deepEqual(args, ["rev-parse", "--path-format=absolute", "--git-common-dir"]);
|
|
99
|
+
assert.ok(options.timeout > 0 && options.timeout <= 5000, "the git lookup needs a timeout within the hook budget");
|
|
100
|
+
return original(command, args, options);
|
|
101
|
+
});
|
|
102
|
+
syncBuiltinESMExports();
|
|
103
|
+
try {
|
|
104
|
+
assert.equal(pauseState(root), null);
|
|
105
|
+
} finally {
|
|
106
|
+
lookup.mock.restore();
|
|
107
|
+
syncBuiltinESMExports();
|
|
108
|
+
}
|
|
109
|
+
});
|