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,428 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { beforeEach, test } from "node:test";
|
|
7
|
+
import { runBranch, syncBranch, USAGE } from "./branch-sync.mjs";
|
|
8
|
+
|
|
9
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" }).trim();
|
|
10
|
+
const quiet = (lines) => (line) => lines.push(line);
|
|
11
|
+
const run = (command, cwd) => spawnSync(command, { cwd, shell: true, stdio: "pipe" }).status ?? 1;
|
|
12
|
+
|
|
13
|
+
beforeEach((t) => {
|
|
14
|
+
const previous = process.env.QC_GATE_SLOT_DIR;
|
|
15
|
+
const directory = mkdtempSync(path.join(os.tmpdir(), "qc-branch-sync-slots-"));
|
|
16
|
+
process.env.QC_GATE_SLOT_DIR = directory;
|
|
17
|
+
t.after(() => {
|
|
18
|
+
if (previous === undefined) delete process.env.QC_GATE_SLOT_DIR;
|
|
19
|
+
else process.env.QC_GATE_SLOT_DIR = previous;
|
|
20
|
+
rmSync(directory, { recursive: true, force: true });
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("branch usage lists the pause refusal exit", () => {
|
|
25
|
+
assert.match(USAGE, /exit 5.*paus/i);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
for (const target of ["does-not-exist", "outside-git"]) test(`a ${target} target reports an input error rather than a pause`, async (t) => {
|
|
29
|
+
const root = batch(t);
|
|
30
|
+
const worktree = path.join(os.tmpdir(), `${path.basename(root)}-${target}`);
|
|
31
|
+
if (target === "outside-git") {
|
|
32
|
+
mkdirSync(worktree);
|
|
33
|
+
t.after(() => rmSync(worktree, { recursive: true, force: true }));
|
|
34
|
+
}
|
|
35
|
+
const lines = [];
|
|
36
|
+
assert.equal(await runBranch(config(root), ["sync", worktree], {
|
|
37
|
+
say: quiet(lines), runSlot: () => assert.fail("an invalid worktree must never push"),
|
|
38
|
+
}), 1);
|
|
39
|
+
assert.equal(lines.length, 1);
|
|
40
|
+
assert.match(lines[0], target === "does-not-exist" ? /git branch --show-current: failed/ : /not a git repository/i);
|
|
41
|
+
assert.doesNotMatch(lines[0], /pause|qc resume/);
|
|
42
|
+
assert.equal(git(root, "status", "--porcelain"), "");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
for (const paused of [false, true]) test(`a target from another repository is refused when its repository is ${paused ? "paused" : "running"}`, async (t) => {
|
|
46
|
+
const root = batch(t);
|
|
47
|
+
const foreign = batch(t);
|
|
48
|
+
const worktree = path.join(foreign, ".worktree", "run");
|
|
49
|
+
git(foreign, "worktree", "add", "-qb", "foreign-run", worktree);
|
|
50
|
+
if (paused) {
|
|
51
|
+
mkdirSync(path.join(foreign, ".git", "qc"), { recursive: true });
|
|
52
|
+
writeFileSync(path.join(foreign, ".git", "qc", "pause"), "operator investigation\n");
|
|
53
|
+
}
|
|
54
|
+
const head = git(worktree, "rev-parse", "HEAD");
|
|
55
|
+
const lines = [];
|
|
56
|
+
let generated = 0;
|
|
57
|
+
let pushed = 0;
|
|
58
|
+
assert.equal(await runBranch(config(root), ["sync", worktree], {
|
|
59
|
+
say: quiet(lines),
|
|
60
|
+
run: () => { generated += 1; return 0; },
|
|
61
|
+
runSlot: () => { pushed += 1; return 0; },
|
|
62
|
+
}), 1);
|
|
63
|
+
assert.equal(lines.length, 1);
|
|
64
|
+
assert.match(lines[0], /target.*another repository/i);
|
|
65
|
+
assert.equal(generated, 0);
|
|
66
|
+
assert.equal(pushed, 0);
|
|
67
|
+
assert.equal(git(worktree, "rev-parse", "HEAD"), head);
|
|
68
|
+
assert.equal(git(worktree, "status", "--porcelain"), "");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
for (const spelling of ["lowercase drive", "lowercase path", "linked root"]) test(`branch sync accepts the same repository with a ${spelling}`, {
|
|
72
|
+
skip: spelling !== "linked root" && process.platform !== "win32",
|
|
73
|
+
}, async (t) => {
|
|
74
|
+
const root = batch(t);
|
|
75
|
+
const worktree = path.join(root, ".worktree", "run");
|
|
76
|
+
git(root, "worktree", "add", "-qb", "linked-run", worktree);
|
|
77
|
+
const configuredRoot = spelling === "lowercase drive" ? root[0].toLowerCase() + root.slice(1)
|
|
78
|
+
: spelling === "lowercase path" ? root.toLowerCase() : worktree;
|
|
79
|
+
const lines = [];
|
|
80
|
+
assert.equal(await runBranch(config(configuredRoot), ["sync", spelling === "linked root" ? "." : worktree, "--no-push"], {
|
|
81
|
+
say: quiet(lines), run, withSlot: (_words, _cwd, action) => action(),
|
|
82
|
+
runSlot: () => assert.fail("--no-push must never push"),
|
|
83
|
+
}), 0, lines.join("\n"));
|
|
84
|
+
git(worktree, "merge-base", "--is-ancestor", "main", "HEAD");
|
|
85
|
+
assert.equal(git(worktree, "status", "--porcelain"), "");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("a pause refuses sync before branch changes or a push", async (t) => {
|
|
89
|
+
const dir = batch(t);
|
|
90
|
+
const head = git(dir, "rev-parse", "HEAD");
|
|
91
|
+
mkdirSync(path.join(dir, ".git", "qc"), { recursive: true });
|
|
92
|
+
writeFileSync(path.join(dir, ".git", "qc", "pause"), "operator investigation\n");
|
|
93
|
+
const refusal = "An operator paused the kit: operator investigation. Wait for the operator to run `qc resume`.";
|
|
94
|
+
await assert.rejects(() => syncBranch(config(dir), dir), { message: refusal });
|
|
95
|
+
const lines = [];
|
|
96
|
+
assert.equal(await runBranch(config(dir), ["sync", "."], {
|
|
97
|
+
say: quiet(lines), runSlot: () => assert.fail("a paused branch must never push"),
|
|
98
|
+
}), 5);
|
|
99
|
+
assert.deepEqual(lines, [`qc branch: ${refusal}`]);
|
|
100
|
+
assert.equal(git(dir, "rev-parse", "HEAD"), head);
|
|
101
|
+
assert.equal(git(dir, "status", "--porcelain"), "");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("an unreadable pause refuses sync before branch changes or a push", async (t) => {
|
|
105
|
+
const dir = batch(t);
|
|
106
|
+
const head = git(dir, "rev-parse", "HEAD");
|
|
107
|
+
mkdirSync(path.join(dir, ".git", "qc", "pause"), { recursive: true });
|
|
108
|
+
await assert.rejects(() => syncBranch(config(dir), dir), /cannot read the operator pause .*Wait for the operator to run `qc resume`\./);
|
|
109
|
+
const lines = [];
|
|
110
|
+
assert.equal(await runBranch(config(dir), ["sync", "."], {
|
|
111
|
+
say: quiet(lines), runSlot: () => assert.fail("an unreadable pause must never push"),
|
|
112
|
+
}), 5);
|
|
113
|
+
assert.equal(lines.length, 1);
|
|
114
|
+
assert.match(lines[0], /^qc branch: cannot read the operator pause .*Wait for the operator to run `qc resume`\.$/);
|
|
115
|
+
assert.equal(git(dir, "rev-parse", "HEAD"), head);
|
|
116
|
+
assert.equal(git(dir, "status", "--porcelain"), "");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
for (const unreadable of [false, true]) {
|
|
120
|
+
test(`branch sync checks ${unreadable ? "an unreadable" : "a new"} pause again before pushing`, async (t) => {
|
|
121
|
+
const dir = batch(t);
|
|
122
|
+
const lines = [];
|
|
123
|
+
let generated = 0;
|
|
124
|
+
assert.equal(await runBranch(config(dir), ["sync", "."], {
|
|
125
|
+
say: quiet(lines),
|
|
126
|
+
runSlot: () => assert.fail("a pause set during sync must prevent the push"),
|
|
127
|
+
run: (command, cwd) => {
|
|
128
|
+
generated += 1;
|
|
129
|
+
const code = run(command, cwd);
|
|
130
|
+
const file = path.join(dir, ".git", "qc", "pause");
|
|
131
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
132
|
+
if (unreadable) mkdirSync(file);
|
|
133
|
+
else writeFileSync(file, "operator investigation\n");
|
|
134
|
+
return code;
|
|
135
|
+
},
|
|
136
|
+
}), 5);
|
|
137
|
+
assert.equal(generated, 1);
|
|
138
|
+
assert.match(lines.at(-1), /Wait for the operator to run `qc resume`\.$/);
|
|
139
|
+
if (unreadable) assert.match(lines.at(-1), /cannot read the operator pause/);
|
|
140
|
+
else assert.match(lines.at(-1), /An operator paused the kit: operator investigation/);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
test("each branch-sync test uses a private gate-slot folder", () => {
|
|
145
|
+
const directory = process.env.QC_GATE_SLOT_DIR;
|
|
146
|
+
assert.ok(directory);
|
|
147
|
+
assert.match(path.basename(directory), /^qc-branch-sync-slots-/);
|
|
148
|
+
assert.ok(existsSync(directory));
|
|
149
|
+
assert.notEqual(directory, path.join(os.tmpdir(), "architecture-kit", "gate-slots"));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
function commitFile(dir, file, text) {
|
|
153
|
+
mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
|
154
|
+
writeFileSync(path.join(dir, file), text);
|
|
155
|
+
git(dir, "add", ".");
|
|
156
|
+
git(dir, "commit", "-q", "-m", file);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function batch(t, generated = false) {
|
|
160
|
+
const dir = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-branch-sync-")));
|
|
161
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
162
|
+
git(dir, "init", "-q", "-b", "main");
|
|
163
|
+
for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"], ["core.autocrlf", "false"]]) git(dir, "config", key, value);
|
|
164
|
+
commitFile(dir, "db/0001_init.sql", "select 1;\n");
|
|
165
|
+
commitFile(dir, "gen.mjs", 'import { writeFileSync } from "node:fs";\nwriteFileSync("out.generated.txt", "regenerated");\n');
|
|
166
|
+
commitFile(dir, "a.txt", "base\n");
|
|
167
|
+
if (generated) commitFile(dir, "out.generated.txt", "base\n");
|
|
168
|
+
git(dir, "switch", "-q", "-c", "feat/5-mine");
|
|
169
|
+
commitFile(dir, "db/0002_mine.sql", "select 'mine';\n");
|
|
170
|
+
git(dir, "switch", "-q", "main");
|
|
171
|
+
commitFile(dir, "db/0002_theirs.sql", "select 'theirs';\n");
|
|
172
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
173
|
+
return dir;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const config = (dir, command = "node gen.mjs") => ({
|
|
177
|
+
root: dir,
|
|
178
|
+
paths: { migrations: "db" },
|
|
179
|
+
generated: { globs: ["**/*.generated.*"], command },
|
|
180
|
+
worktree: { base: "main" },
|
|
181
|
+
swarm: { isolation: { protectedBranches: ["main"] } },
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("sync merges the base, renumbers its own migrations, and commits generated files", async (t) => {
|
|
185
|
+
const dir = batch(t);
|
|
186
|
+
const said = [];
|
|
187
|
+
assert.deepEqual(await syncBranch(config(dir), dir, { say: quiet(said), run }), { code: 0, changed: true, branch: "feat/5-mine" });
|
|
188
|
+
assert.deepEqual(readdirSync(path.join(dir, "db")).sort(), ["0001_init.sql", "0002_theirs.sql", "0003_mine.sql"]);
|
|
189
|
+
git(dir, "merge-base", "--is-ancestor", "main", "HEAD");
|
|
190
|
+
assert.equal(git(dir, "status", "--porcelain"), "");
|
|
191
|
+
assert.equal(readFileSync(path.join(dir, "out.generated.txt"), "utf8"), "regenerated");
|
|
192
|
+
assert.ok(said.includes("rename db/0002_mine.sql -> 0003_mine.sql"));
|
|
193
|
+
assert.match(git(dir, "log", "-1", "--format=%b"), /Refs #5/);
|
|
194
|
+
const head = git(dir, "rev-parse", "HEAD");
|
|
195
|
+
git(dir, "branch", "published", "HEAD");
|
|
196
|
+
git(dir, "branch", "--set-upstream-to=published");
|
|
197
|
+
assert.deepEqual(await syncBranch(config(dir), dir, { say: quiet(said), run }), { code: 0, changed: false });
|
|
198
|
+
assert.equal(git(dir, "rev-parse", "HEAD"), head);
|
|
199
|
+
assert.ok(said.includes("OK feat/5-mine already holds main"));
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("a source conflict leaves the merge in progress and reports recovery instructions", async (t) => {
|
|
203
|
+
const dir = batch(t);
|
|
204
|
+
commitFile(dir, "a.txt", "mine\n");
|
|
205
|
+
git(dir, "switch", "-q", "main");
|
|
206
|
+
commitFile(dir, "a.txt", "theirs\n");
|
|
207
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
208
|
+
const said = [];
|
|
209
|
+
assert.equal((await syncBranch(config(dir), dir, { say: quiet(said), run })).code, 1);
|
|
210
|
+
assert.ok(existsSync(path.join(dir, ".git", "MERGE_HEAD")));
|
|
211
|
+
assert.match(said.join("\n"), /a\.txt/);
|
|
212
|
+
assert.match(said.join("\n"), /qc migrations renumber --onto main/);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("a generated conflict is resolved and regenerated", async (t) => {
|
|
216
|
+
const dir = batch(t, true);
|
|
217
|
+
commitFile(dir, "out.generated.txt", "mine\n");
|
|
218
|
+
git(dir, "switch", "-q", "main");
|
|
219
|
+
commitFile(dir, "out.generated.txt", "theirs\n");
|
|
220
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
221
|
+
assert.equal((await syncBranch(config(dir), dir, { say: quiet([]), run })).code, 0);
|
|
222
|
+
assert.equal(readFileSync(path.join(dir, "out.generated.txt"), "utf8"), "regenerated");
|
|
223
|
+
assert.equal(git(dir, "status", "--porcelain"), "");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("sync refuses a protected branch", async (t) => {
|
|
227
|
+
const dir = batch(t);
|
|
228
|
+
git(dir, "switch", "-q", "main");
|
|
229
|
+
await assert.rejects(() => syncBranch(config(dir), dir), /protected.*main|main.*protected/);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("sync refuses a dirty tree", async (t) => {
|
|
233
|
+
const dir = batch(t);
|
|
234
|
+
writeFileSync(path.join(dir, "a.txt"), "dirty\n");
|
|
235
|
+
await assert.rejects(() => syncBranch(config(dir), dir), /working tree.*changes|dirty/);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("sync refuses a detached head", async (t) => {
|
|
239
|
+
const dir = batch(t);
|
|
240
|
+
git(dir, "checkout", "-q", "--detach");
|
|
241
|
+
await assert.rejects(() => syncBranch(config(dir), dir), /detached/i);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("sync refuses an unknown base", async (t) => {
|
|
245
|
+
const dir = batch(t);
|
|
246
|
+
await assert.rejects(() => syncBranch(config(dir), dir, { base: "missing-base" }), /missing-base/);
|
|
247
|
+
assert.equal(git(dir, "status", "--porcelain"), "");
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("sync refuses a merge in progress", async (t) => {
|
|
251
|
+
const dir = batch(t);
|
|
252
|
+
git(dir, "merge", "--no-ff", "--no-commit", "main");
|
|
253
|
+
await assert.rejects(() => syncBranch(config(dir), dir), /merge.*in progress/);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("sync throws when the generator fails", async (t) => {
|
|
257
|
+
const dir = batch(t);
|
|
258
|
+
await assert.rejects(() => syncBranch(config(dir, 'node -e "process.exit(1)"'), dir, { say: quiet([]), run }), /generated\.command failed/);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("branch sync pushes a changed branch through a gate slot in its worktree", async (t) => {
|
|
262
|
+
const dir = batch(t);
|
|
263
|
+
const said = [];
|
|
264
|
+
const calls = [];
|
|
265
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
266
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet(said), run, runSlot }), 0);
|
|
267
|
+
assert.deepEqual(calls, [{ words: ["git", "push", "-u", "origin", "feat/5-mine"], cwd: dir }]);
|
|
268
|
+
assert.ok(said.includes("push feat/5-mine"));
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("branch sync with --no-push skips the gate slot", async (t) => {
|
|
272
|
+
const dir = batch(t);
|
|
273
|
+
const calls = [];
|
|
274
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
275
|
+
assert.equal(await runBranch(config(dir), ["sync", dir, "--no-push"], { say: quiet([]), run, runSlot }), 0);
|
|
276
|
+
assert.deepEqual(calls, []);
|
|
277
|
+
git(dir, "merge-base", "--is-ancestor", "main", "HEAD");
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("branch sync skips the gate slot for an up-to-date branch", async (t) => {
|
|
281
|
+
const dir = batch(t);
|
|
282
|
+
git(dir, "merge", "--no-edit", "main");
|
|
283
|
+
git(dir, "branch", "published", "HEAD");
|
|
284
|
+
git(dir, "branch", "--set-upstream-to=published");
|
|
285
|
+
const calls = [];
|
|
286
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
287
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet([]), run, runSlot }), 0);
|
|
288
|
+
assert.deepEqual(calls, []);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("branch sync pushes a branch containing a semicolon as one argument", async (t) => {
|
|
292
|
+
const dir = batch(t);
|
|
293
|
+
const remote = `${dir}-remote.git`;
|
|
294
|
+
t.after(() => rmSync(remote, { recursive: true, force: true }));
|
|
295
|
+
git(dir, "init", "--bare", "-q", remote);
|
|
296
|
+
git(dir, "remote", "add", "origin", remote);
|
|
297
|
+
const branch = process.platform === "win32" ? "feat/5-mine;&ver" : "feat/5-mine;echo>injected";
|
|
298
|
+
git(dir, "branch", "-m", branch);
|
|
299
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet([]), run }), 0);
|
|
300
|
+
assert.equal(existsSync(path.join(dir, "injected")), false);
|
|
301
|
+
assert.equal(git(remote, "rev-parse", `refs/heads/${branch}`), git(dir, "rev-parse", "HEAD"));
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("a resolved source conflict reruns the generator and pushes once", async (t) => {
|
|
305
|
+
const dir = batch(t);
|
|
306
|
+
commitFile(dir, "a.txt", "mine\n");
|
|
307
|
+
git(dir, "switch", "-q", "main");
|
|
308
|
+
commitFile(dir, "a.txt", "theirs\n");
|
|
309
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
310
|
+
assert.equal((await syncBranch(config(dir), dir, { say: quiet([]), run })).code, 1);
|
|
311
|
+
commitFile(dir, "a.txt", "resolved\n");
|
|
312
|
+
const calls = [];
|
|
313
|
+
let generated = 0;
|
|
314
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
315
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], {
|
|
316
|
+
say: quiet([]), runSlot, run: (command, cwd) => { generated += 1; return run(command, cwd); },
|
|
317
|
+
}), 0);
|
|
318
|
+
assert.equal(generated, 1);
|
|
319
|
+
assert.equal(readFileSync(path.join(dir, "out.generated.txt"), "utf8"), "regenerated");
|
|
320
|
+
assert.deepEqual(calls, [{ words: ["git", "push", "-u", "origin", "feat/5-mine"], cwd: dir }]);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("sync holds gate slots for the merge commit, generator, and generated commit", async (t) => {
|
|
324
|
+
const dir = batch(t);
|
|
325
|
+
const calls = [];
|
|
326
|
+
let held = false;
|
|
327
|
+
const withSlot = async (words, cwd, action) => {
|
|
328
|
+
calls.push({ words, cwd });
|
|
329
|
+
held = true;
|
|
330
|
+
try { return await action(); } finally { held = false; }
|
|
331
|
+
};
|
|
332
|
+
await syncBranch(config(dir), dir, {
|
|
333
|
+
say: quiet([]), withSlot,
|
|
334
|
+
run: (command, cwd) => { assert.equal(held, true); return run(command, cwd); },
|
|
335
|
+
});
|
|
336
|
+
assert.deepEqual(calls.map(({ words }) => words.slice(0, 3)), [
|
|
337
|
+
["git", "commit", "-q"], ["node gen.mjs"], ["git", "commit", "-q"],
|
|
338
|
+
]);
|
|
339
|
+
assert.ok(calls.every(({ cwd }) => cwd === dir));
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("a branch ahead of its upstream retries generation and a failed push", async (t) => {
|
|
343
|
+
const dir = batch(t);
|
|
344
|
+
git(dir, "branch", "published", "HEAD");
|
|
345
|
+
git(dir, "branch", "--set-upstream-to=published");
|
|
346
|
+
let generated = 0;
|
|
347
|
+
let pushed = 0;
|
|
348
|
+
const deps = {
|
|
349
|
+
say: quiet([]),
|
|
350
|
+
run: (command, cwd) => { generated += 1; return generated === 1 ? 1 : run(command, cwd); },
|
|
351
|
+
runSlot: () => { pushed += 1; return pushed === 1 ? 7 : 0; },
|
|
352
|
+
};
|
|
353
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], deps), 1);
|
|
354
|
+
assert.equal(pushed, 0);
|
|
355
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], deps), 7);
|
|
356
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], deps), 0);
|
|
357
|
+
assert.equal(generated, 3);
|
|
358
|
+
assert.equal(pushed, 2);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test("a generated conflict without a generator stays unresolved and lists the file", async (t) => {
|
|
362
|
+
const dir = batch(t, true);
|
|
363
|
+
commitFile(dir, "out.generated.txt", "mine\n");
|
|
364
|
+
git(dir, "switch", "-q", "main");
|
|
365
|
+
commitFile(dir, "out.generated.txt", "theirs\n");
|
|
366
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
367
|
+
const said = [];
|
|
368
|
+
assert.equal((await syncBranch(config(dir, null), dir, { say: quiet(said), run })).code, 1);
|
|
369
|
+
assert.match(said.join("\n"), /generated\.command/);
|
|
370
|
+
assert.ok(said.includes(" out.generated.txt"));
|
|
371
|
+
assert.equal(git(dir, "diff", "--name-only", "--diff-filter=U"), "out.generated.txt");
|
|
372
|
+
assert.ok(existsSync(path.join(dir, ".git", "MERGE_HEAD")));
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("branch sync returns one after a conflict and skips the gate slot", async (t) => {
|
|
376
|
+
const dir = batch(t);
|
|
377
|
+
commitFile(dir, "a.txt", "mine\n");
|
|
378
|
+
git(dir, "switch", "-q", "main");
|
|
379
|
+
commitFile(dir, "a.txt", "theirs\n");
|
|
380
|
+
git(dir, "switch", "-q", "feat/5-mine");
|
|
381
|
+
const calls = [];
|
|
382
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
383
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet([]), run, runSlot }), 1);
|
|
384
|
+
assert.deepEqual(calls, []);
|
|
385
|
+
assert.ok(existsSync(path.join(dir, ".git", "MERGE_HEAD")));
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
test("branch sync returns the gate slot's failure code", async (t) => {
|
|
389
|
+
const dir = batch(t);
|
|
390
|
+
const calls = [];
|
|
391
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 7; };
|
|
392
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet([]), run, runSlot }), 7);
|
|
393
|
+
assert.deepEqual(calls, [{ words: ["git", "push", "-u", "origin", "feat/5-mine"], cwd: dir }]);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
for (const argv of [["sync"], ["sync", ".", "extra"], ["merge", "."], [], ["sync", ".", "--base"], ["sync", ".", "--unknown"]]) {
|
|
397
|
+
test(`branch rejects invalid arguments ${JSON.stringify(argv)} with usage`, async () => {
|
|
398
|
+
const said = [];
|
|
399
|
+
const calls = [];
|
|
400
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
401
|
+
assert.equal(await runBranch(config("."), argv, { say: quiet(said), run, runSlot }), 2);
|
|
402
|
+
assert.match(said.join("\n"), /qc branch sync <worktree> \[--base <ref>\] \[--no-push\]/);
|
|
403
|
+
assert.deepEqual(calls, []);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
test("branch sync resolves its worktree against config.root and accepts --base", async (t) => {
|
|
408
|
+
const root = batch(t);
|
|
409
|
+
git(root, "checkout", "-q", "main");
|
|
410
|
+
const dir = path.join(root, ".worktree", "run");
|
|
411
|
+
git(root, "worktree", "add", "-q", dir, "feat/5-mine");
|
|
412
|
+
const cfg = { ...config(dir), root, worktree: { base: "missing-base" } };
|
|
413
|
+
const calls = [];
|
|
414
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
415
|
+
assert.equal(await runBranch(cfg, ["sync", path.relative(root, dir), "--base", "main"], { say: quiet([]), run, runSlot }), 0);
|
|
416
|
+
assert.deepEqual(calls, [{ words: ["git", "push", "-u", "origin", "feat/5-mine"], cwd: dir }]);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
test("branch sync prints a refusal and skips the gate slot", async (t) => {
|
|
420
|
+
const dir = batch(t);
|
|
421
|
+
writeFileSync(path.join(dir, "a.txt"), "dirty\n");
|
|
422
|
+
const said = [];
|
|
423
|
+
const calls = [];
|
|
424
|
+
const runSlot = (words, cwd) => { calls.push({ words, cwd }); return 0; };
|
|
425
|
+
assert.equal(await runBranch(config(dir), ["sync", dir], { say: quiet(said), run, runSlot }), 1);
|
|
426
|
+
assert.match(said.join("\n"), /^qc branch: .*working tree has changes/);
|
|
427
|
+
assert.deepEqual(calls, []);
|
|
428
|
+
});
|
package/src/cli/budget-guard.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The PreToolUse trigger for
|
|
3
|
-
//
|
|
2
|
+
// The PreToolUse trigger for tool-call and context budgets. It spawns no process.
|
|
3
|
+
// The main session gets only the orchestrator's compaction reminder while dispatch is on.
|
|
4
4
|
|
|
5
5
|
import { existsSync } from "node:fs";
|
|
6
6
|
import os from "node:os";
|
|
@@ -10,8 +10,12 @@ import { CONFIG_FILE, contextDefaults, load } from "../config.mjs";
|
|
|
10
10
|
import { budgetOf, budgetVerdict } from "./budget.mjs";
|
|
11
11
|
import { bumpCount, counterFileOf, readState, stateFileOf, writeState } from "./budget-counter.mjs";
|
|
12
12
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
13
|
+
import { budgetsOf } from "./context-budgets.mjs";
|
|
13
14
|
import { contextSettings, contextVerdict, estimateOf, handbackStamp } from "./context-signal.mjs";
|
|
15
|
+
import { dispatchSettings } from "./dispatch.mjs";
|
|
16
|
+
import { budgetRoleOf, orchestratorVerdict, roleBudgetVerdict } from "./role-budget.mjs";
|
|
14
17
|
import { agentTranscriptOf, contextOf, firstPromptOf, firstUsage, lastUsage } from "./transcript-usage.mjs";
|
|
18
|
+
import { checkpointNameOf } from "./checkpoint.mjs";
|
|
15
19
|
|
|
16
20
|
const HANDBACK = "SubagentHandback";
|
|
17
21
|
|
|
@@ -20,14 +24,51 @@ const context = (text) => ({ hookSpecificOutput: { hookEventName: "PreToolUse",
|
|
|
20
24
|
/** The verdict on one tool call. @returns the hook output, or null to let the call run with no message. */
|
|
21
25
|
export function decide(call, tmp = os.tmpdir()) {
|
|
22
26
|
const agentId = call.agent_id;
|
|
23
|
-
if (typeof agentId !== "string" || agentId === "") return
|
|
27
|
+
if (typeof agentId !== "string" || agentId === "") return orchestratorOutput(call, tmp);
|
|
24
28
|
const root = checkoutRootOf(call.cwd ?? process.cwd());
|
|
25
29
|
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
26
30
|
|
|
27
31
|
let swarm;
|
|
28
|
-
let budget;
|
|
29
32
|
try {
|
|
30
33
|
swarm = load(root).swarm;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return context(`Tool-call budget is off: ${error.message}`);
|
|
36
|
+
}
|
|
37
|
+
const output = toolCallOutput(call, root, swarm, tmp);
|
|
38
|
+
const roleOutput = roleBudgetOutput(call, root, swarm, tmp);
|
|
39
|
+
if (output?.hookSpecificOutput?.permissionDecision === "deny") return output;
|
|
40
|
+
if (roleOutput?.hookSpecificOutput?.permissionDecision === "deny") return roleOutput;
|
|
41
|
+
if (!roleOutput || call.tool_name === HANDBACK) return output;
|
|
42
|
+
const existing = output?.hookSpecificOutput?.additionalContext;
|
|
43
|
+
const note = roleOutput.hookSpecificOutput.additionalContext;
|
|
44
|
+
return context(existing ? `${existing}\n${note}` : note);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function orchestratorOutput(call, tmp) {
|
|
48
|
+
try {
|
|
49
|
+
const root = checkoutRootOf(call.cwd ?? process.cwd());
|
|
50
|
+
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
51
|
+
const config = load(root);
|
|
52
|
+
if (!dispatchSettings(config.swarm)) return null;
|
|
53
|
+
const budget = budgetsOf(config).orchestrator;
|
|
54
|
+
if (!budget) return null;
|
|
55
|
+
const session = call.session_id ?? "session";
|
|
56
|
+
const count = bumpCount(counterFileOf(session, "orchestrator", tmp));
|
|
57
|
+
if (count % contextDefaults.every !== 0) return null;
|
|
58
|
+
const tokens = contextOf(lastUsage(call.transcript_path));
|
|
59
|
+
const file = stateFileOf(session, "orchestrator", tmp);
|
|
60
|
+
const state = readState(file);
|
|
61
|
+
const note = orchestratorVerdict({ tokens, compactAt: budget.compactAt, count, state });
|
|
62
|
+
writeState(file, state);
|
|
63
|
+
return note ? context(`${note} Update the checkpoint now: ${checkpointNameOf(root, session)}.`) : null;
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function toolCallOutput(call, root, swarm, tmp) {
|
|
70
|
+
let budget;
|
|
71
|
+
try {
|
|
31
72
|
budget = budgetOf(swarm);
|
|
32
73
|
} catch (error) {
|
|
33
74
|
// A config typo must not stop every agent; the agent sees the error and reports it.
|
|
@@ -36,7 +77,7 @@ export function decide(call, tmp = os.tmpdir()) {
|
|
|
36
77
|
if (budget === null) return null;
|
|
37
78
|
let count;
|
|
38
79
|
try {
|
|
39
|
-
count = bumpCount(counterFileOf(call.session_id ?? "session",
|
|
80
|
+
count = bumpCount(counterFileOf(call.session_id ?? "session", call.agent_id, tmp));
|
|
40
81
|
} catch {
|
|
41
82
|
// A busy or read-only temp folder is the hook's own problem, never a reason to stop the agent.
|
|
42
83
|
return null;
|
|
@@ -46,6 +87,34 @@ export function decide(call, tmp = os.tmpdir()) {
|
|
|
46
87
|
return withSignal(verdict, contextSignal(call, { swarm, count, budget, tmp }), call);
|
|
47
88
|
}
|
|
48
89
|
|
|
90
|
+
// Read after the context signal so its keys survive this write.
|
|
91
|
+
function roleBudgetOutput(call, root, swarm, tmp) {
|
|
92
|
+
try {
|
|
93
|
+
const file = stateFileOf(call.session_id ?? "session", call.agent_id, tmp);
|
|
94
|
+
const state = readState(file);
|
|
95
|
+
let budgets;
|
|
96
|
+
let role;
|
|
97
|
+
try {
|
|
98
|
+
role = budgetRoleOf(call.agent_type, swarm);
|
|
99
|
+
if (!role) return null;
|
|
100
|
+
budgets = budgetsOf(load(root));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (state.budgetError === error.message) return null;
|
|
103
|
+
writeState(file, { ...state, budgetError: error.message });
|
|
104
|
+
return context(`Context budget is off: ${error.message}`);
|
|
105
|
+
}
|
|
106
|
+
if (!role || !budgets[role]) return null;
|
|
107
|
+
const tokens = state.budgetClosed ? undefined : contextOf(lastUsage(agentTranscriptOf(call)));
|
|
108
|
+
const next = { ...state };
|
|
109
|
+
const verdict = roleBudgetVerdict({ role, tokens, budget: budgets[role], call, state: next });
|
|
110
|
+
if (next.budgetReminded !== state.budgetReminded || next.budgetClosed !== state.budgetClosed) writeState(file, next);
|
|
111
|
+
return verdict;
|
|
112
|
+
} catch {
|
|
113
|
+
// A missing transcript or an unwritable state file leaves the role budget off.
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
49
118
|
/** The one note for an agent whose hook input names no transcript, and the state that keeps later calls quiet. */
|
|
50
119
|
function noTranscriptNote(file, state) {
|
|
51
120
|
writeState(file, { ...state, noTranscript: true });
|