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,47 @@
|
|
|
1
|
+
import { defaults } from "../config.mjs";
|
|
2
|
+
|
|
3
|
+
const roles = Object.keys(defaults.budgets);
|
|
4
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
|
|
6
|
+
function invalid(key, want, value) {
|
|
7
|
+
throw new Error(`${key} in qc.config.json must be ${want}, got ${JSON.stringify(value)}`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function positiveWhole(key, value) {
|
|
11
|
+
if (!Number.isInteger(value) || value < 1) invalid(key, "a positive whole number", value);
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function budgetsOf(config) {
|
|
16
|
+
const budgets = config.budgets === undefined ? defaults.budgets : config.budgets;
|
|
17
|
+
if (!isObject(budgets)) invalid("budgets", "an object", budgets);
|
|
18
|
+
for (const role of Object.keys(budgets)) {
|
|
19
|
+
if (!roles.includes(role)) invalid(`budgets.${role}`, `a known role (${roles.join(", ")})`, budgets[role]);
|
|
20
|
+
}
|
|
21
|
+
const result = {};
|
|
22
|
+
for (const role of roles) {
|
|
23
|
+
const value = budgets[role] === undefined ? defaults.budgets[role] : budgets[role];
|
|
24
|
+
if (value === null) {
|
|
25
|
+
result[role] = null;
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (!isObject(value)) invalid(`budgets.${role}`, "an object or null", value);
|
|
29
|
+
const keys = Object.keys(defaults.budgets[role]);
|
|
30
|
+
for (const key of Object.keys(value)) {
|
|
31
|
+
if (!keys.includes(key)) invalid(`budgets.${role}.${key}`, `a known key (${keys.join(", ")})`, value[key]);
|
|
32
|
+
}
|
|
33
|
+
const resolved = { ...defaults.budgets[role], ...value };
|
|
34
|
+
if (role === "codex" && resolved.compactAt === null) {
|
|
35
|
+
const fallback = config.codex === undefined ? defaults.codex.compactTokens : config.codex?.compactTokens;
|
|
36
|
+
resolved.compactAt = positiveWhole("codex.compactTokens", fallback);
|
|
37
|
+
}
|
|
38
|
+
for (const key of keys) {
|
|
39
|
+
positiveWhole(`budgets.${role}.${key}`, resolved[key]);
|
|
40
|
+
}
|
|
41
|
+
if (resolved.remindAt >= resolved.handoffAt) {
|
|
42
|
+
invalid(`budgets.${role}.remindAt`, `smaller than handoffAt (${resolved.handoffAt})`, resolved.remindAt);
|
|
43
|
+
}
|
|
44
|
+
result[role] = resolved;
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { defaults, merge } from "../config.mjs";
|
|
4
|
+
import { budgetsOf } from "./context-budgets.mjs";
|
|
5
|
+
|
|
6
|
+
const configWith = (budgets) => merge(defaults, { budgets });
|
|
7
|
+
|
|
8
|
+
test("budgetsOf resolves the default budgets for every role", () => {
|
|
9
|
+
assert.deepEqual(budgetsOf(defaults), {
|
|
10
|
+
orchestrator: { compactAt: 200000 },
|
|
11
|
+
implementer: { remindAt: 110000, handoffAt: 150000 },
|
|
12
|
+
reviewer: { remindAt: 80000, handoffAt: 120000 },
|
|
13
|
+
codex: { compactAt: 300000, toolOutputTokens: 4000 },
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("unknown keys inside every budget role name the key in the error", () => {
|
|
18
|
+
for (const role of ["orchestrator", "implementer", "reviewer", "codex"]) {
|
|
19
|
+
assert.throws(() => budgetsOf(configWith({ [role]: { handOffAt: 200000 } })), {
|
|
20
|
+
message: `budgets.${role}.handOffAt in qc.config.json must be a known key (${Object.keys(defaults.budgets[role]).join(", ")}), got 200000`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a null role turns its budget off", () => {
|
|
26
|
+
for (const role of ["orchestrator", "implementer", "reviewer", "codex"]) {
|
|
27
|
+
assert.equal(budgetsOf(configWith({ [role]: null }))[role], null);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("budgets.implementer.remindAt must be a positive whole number", () => {
|
|
32
|
+
assert.throws(() => budgetsOf(configWith({ implementer: { remindAt: 0 } })), {
|
|
33
|
+
message: "budgets.implementer.remindAt in qc.config.json must be a positive whole number, got 0",
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("each budget number must be a positive whole number", () => {
|
|
38
|
+
const fields = {
|
|
39
|
+
orchestrator: ["compactAt"],
|
|
40
|
+
implementer: ["remindAt", "handoffAt"],
|
|
41
|
+
reviewer: ["remindAt", "handoffAt"],
|
|
42
|
+
codex: ["compactAt", "toolOutputTokens"],
|
|
43
|
+
};
|
|
44
|
+
for (const [role, keys] of Object.entries(fields)) {
|
|
45
|
+
for (const key of keys) {
|
|
46
|
+
for (const value of [0, -1, 1.5, "1000", false, [], {}]) {
|
|
47
|
+
assert.throws(() => budgetsOf(configWith({ [role]: { [key]: value } })), {
|
|
48
|
+
message: `budgets.${role}.${key} in qc.config.json must be a positive whole number, got ${JSON.stringify(value)}`,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (role !== "codex" || key !== "compactAt") {
|
|
52
|
+
assert.throws(() => budgetsOf(configWith({ [role]: { [key]: null } })), {
|
|
53
|
+
message: `budgets.${role}.${key} in qc.config.json must be a positive whole number, got null`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("remindAt must be smaller than handoffAt", () => {
|
|
61
|
+
for (const [role, handoffAt] of [["implementer", 150000], ["reviewer", 120000]]) {
|
|
62
|
+
for (const remindAt of [handoffAt, handoffAt + 1]) {
|
|
63
|
+
assert.throws(() => budgetsOf(configWith({ [role]: { remindAt } })), {
|
|
64
|
+
message: `budgets.${role}.remindAt in qc.config.json must be smaller than handoffAt (${handoffAt}), got ${remindAt}`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("an unknown role error lists the supported roles", () => {
|
|
71
|
+
assert.throws(() => budgetsOf(configWith({ planner: {} })), {
|
|
72
|
+
message: "budgets.planner in qc.config.json must be a known role (orchestrator, implementer, reviewer, codex), got {}",
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("budgets.codex must be an object or null", () => {
|
|
77
|
+
for (const value of ["big", 10, false, []]) {
|
|
78
|
+
assert.throws(() => budgetsOf(configWith({ codex: value })), {
|
|
79
|
+
message: `budgets.codex in qc.config.json must be an object or null, got ${JSON.stringify(value)}`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a null codex compactAt uses codex.compactTokens", () => {
|
|
85
|
+
const config = merge(defaults, { codex: { compactTokens: 250000 }, budgets: { codex: { compactAt: null } } });
|
|
86
|
+
assert.equal(budgetsOf(config).codex.compactAt, 250000);
|
|
87
|
+
assert.equal(config.budgets.codex.compactAt, null);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a bad compactTokens fallback names codex.compactTokens", () => {
|
|
91
|
+
for (const value of [0, -5, 1.5, "big", null]) {
|
|
92
|
+
assert.throws(() => budgetsOf(merge(defaults, { codex: { compactTokens: value } })), {
|
|
93
|
+
message: `codex.compactTokens in qc.config.json must be a positive whole number, got ${JSON.stringify(value)}`,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The context-watch trigger reminds the main session to compact at a clean break.
|
|
3
|
+
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { mkdirSync, readFileSync, renameSync, rmdirSync, statSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { defaults, load } from "../config.mjs";
|
|
10
|
+
import { writeState } from "./budget-counter.mjs";
|
|
11
|
+
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
12
|
+
import { budgetsOf } from "./context-budgets.mjs";
|
|
13
|
+
import { dispatchSettings } from "./dispatch.mjs";
|
|
14
|
+
import { contextOf, lastUsage } from "./transcript-usage.mjs";
|
|
15
|
+
import { checkpointNameOf, checkpointSettingsAt } from "./checkpoint.mjs";
|
|
16
|
+
|
|
17
|
+
export const START = defaults.budgets.orchestrator.compactAt;
|
|
18
|
+
export const STEP = 50_000;
|
|
19
|
+
export const TELL_USER = 300_000;
|
|
20
|
+
|
|
21
|
+
export function decide(call, { tmp = os.tmpdir() } = {}) {
|
|
22
|
+
if (call.agent_id || !call.transcript_path) return null;
|
|
23
|
+
let budget;
|
|
24
|
+
try {
|
|
25
|
+
const cwd = call.cwd ?? process.cwd();
|
|
26
|
+
const config = load(checkoutRootOf(cwd) ?? cwd);
|
|
27
|
+
if (dispatchSettings(config.swarm)) return null;
|
|
28
|
+
budget = budgetsOf(config).orchestrator;
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (!budget) return null;
|
|
33
|
+
const tokens = contextOf(lastUsage(call.transcript_path));
|
|
34
|
+
if (!Number.isFinite(tokens)) return null;
|
|
35
|
+
const step = tokens < budget.compactAt ? -1 : Math.floor((tokens - budget.compactAt) / STEP);
|
|
36
|
+
const directory = path.join(tmp, "claude-context-watch");
|
|
37
|
+
const file = path.join(directory, `${String(call.session_id ?? "").replace(/[^A-Za-z0-9_-]/g, "_")}.json`);
|
|
38
|
+
const lock = `${file}.lock`;
|
|
39
|
+
mkdirSync(directory, { recursive: true });
|
|
40
|
+
try {
|
|
41
|
+
mkdirSync(lock);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error.code !== "EEXIST") throw error;
|
|
44
|
+
try {
|
|
45
|
+
if (Date.now() - statSync(lock).mtimeMs <= 30_000) return null;
|
|
46
|
+
const stale = `${lock}.${randomUUID()}`;
|
|
47
|
+
renameSync(lock, stale);
|
|
48
|
+
if (Date.now() - statSync(stale).mtimeMs <= 30_000) {
|
|
49
|
+
rmdirSync(stale);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
rmdirSync(stale);
|
|
53
|
+
} catch (claimError) {
|
|
54
|
+
if (claimError.code === "ENOENT") return null;
|
|
55
|
+
throw claimError;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
mkdirSync(lock);
|
|
59
|
+
} catch (retryError) {
|
|
60
|
+
if (retryError.code === "EEXIST") return null;
|
|
61
|
+
throw retryError;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
let kept = -1;
|
|
65
|
+
try {
|
|
66
|
+
try {
|
|
67
|
+
kept = JSON.parse(readFileSync(file, "utf8")).step ?? -1;
|
|
68
|
+
} catch {
|
|
69
|
+
// A session without readable state starts below the first step.
|
|
70
|
+
}
|
|
71
|
+
// Only a compaction below compactAt resets the thresholds already noted.
|
|
72
|
+
if (step > kept || (step === -1 && kept !== -1)) writeState(file, { step });
|
|
73
|
+
} finally {
|
|
74
|
+
try {
|
|
75
|
+
rmdirSync(lock);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (error.code !== "ENOENT") throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (step <= kept) return null;
|
|
81
|
+
const k = Math.round(tokens / 1000);
|
|
82
|
+
const cwd = call.cwd ?? process.cwd();
|
|
83
|
+
const checkpoint = checkpointSettingsAt(cwd)
|
|
84
|
+
? ` Update the checkpoint now: ${checkpointNameOf(cwd, call.session_id)}.`
|
|
85
|
+
: "";
|
|
86
|
+
const result = {
|
|
87
|
+
hookSpecificOutput: {
|
|
88
|
+
hookEventName: call.hook_event_name,
|
|
89
|
+
additionalContext: `Context is about ${k}K tokens. At the next clean break (a task, review, or merge just finished, nothing mid-fix), update the resume notes and ask the user to run /compact before you continue.${checkpoint}`,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
if (tokens >= TELL_USER) result.systemMessage = `Context ${k}K tokens: Claude will ask you to /compact at the next clean break.`;
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function readStdin() {
|
|
97
|
+
let data = "";
|
|
98
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
99
|
+
return data;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
103
|
+
if (isEntryPoint) {
|
|
104
|
+
let call = null;
|
|
105
|
+
try {
|
|
106
|
+
call = JSON.parse(await readStdin());
|
|
107
|
+
} catch {
|
|
108
|
+
// Invalid hook input has no session to notify.
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const result = call ? decide(call) : null;
|
|
112
|
+
if (result) process.stdout.write(JSON.stringify(result));
|
|
113
|
+
} catch (error) {
|
|
114
|
+
process.stderr.write(`context-watch: ${String(error.message ?? error).replace(/[\r\n]+/g, " ")}\n`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import fs, { appendFileSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
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 { decide, START, STEP, TELL_USER } from "./context-watch.mjs";
|
|
10
|
+
|
|
11
|
+
function fixture(t, sessionId = "s") {
|
|
12
|
+
const tmp = mkdtempSync(path.join(os.tmpdir(), "context-watch-test-"));
|
|
13
|
+
t.after(() => rmSync(tmp, { recursive: true, force: true }));
|
|
14
|
+
const transcript = path.join(tmp, "transcript.jsonl");
|
|
15
|
+
mkdirSync(path.join(tmp, ".git"));
|
|
16
|
+
const call = { cwd: tmp, session_id: sessionId, transcript_path: transcript, hook_event_name: "PostToolUse" };
|
|
17
|
+
let request = 0;
|
|
18
|
+
return {
|
|
19
|
+
tmp,
|
|
20
|
+
call,
|
|
21
|
+
state: path.join(tmp, "claude-context-watch", `${sessionId.replace(/[^A-Za-z0-9_-]/g, "_")}.json`),
|
|
22
|
+
at(tokens, overrides = {}) {
|
|
23
|
+
request += 1;
|
|
24
|
+
appendFileSync(transcript, `${JSON.stringify({
|
|
25
|
+
type: "assistant",
|
|
26
|
+
timestamp: "2026-01-01T00:00:00Z",
|
|
27
|
+
message: {
|
|
28
|
+
id: `m${request}`,
|
|
29
|
+
model: "m",
|
|
30
|
+
usage: { input_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: tokens - 1 },
|
|
31
|
+
},
|
|
32
|
+
})}\n`);
|
|
33
|
+
return decide({ ...call, ...overrides }, { tmp });
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("dispatch owns compaction even with invalid budgets", (t) => {
|
|
39
|
+
const f = fixture(t);
|
|
40
|
+
writeFileSync(path.join(f.tmp, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} }, budgets: false }));
|
|
41
|
+
assert.equal(f.at(400_000), null);
|
|
42
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("without dispatch the watch starts at the configured orchestrator budget", (t) => {
|
|
46
|
+
const f = fixture(t);
|
|
47
|
+
writeFileSync(path.join(f.tmp, "qc.config.json"), JSON.stringify({ budgets: { orchestrator: { compactAt: 400_000 } } }));
|
|
48
|
+
const nested = path.join(f.tmp, "nested");
|
|
49
|
+
mkdirSync(nested);
|
|
50
|
+
assert.equal(f.at(399_999, { cwd: nested }), null);
|
|
51
|
+
assert.match(f.at(400_000, { cwd: nested }).hookSpecificOutput.additionalContext, /400K/);
|
|
52
|
+
assert.equal(f.at(449_999, { cwd: nested }), null);
|
|
53
|
+
assert.match(f.at(450_000, { cwd: nested }).hookSpecificOutput.additionalContext, /450K/);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a disabled orchestrator budget keeps the watch silent", (t) => {
|
|
57
|
+
const f = fixture(t);
|
|
58
|
+
writeFileSync(path.join(f.tmp, "qc.config.json"), JSON.stringify({ budgets: { orchestrator: null } }));
|
|
59
|
+
assert.equal(f.at(400_000), null);
|
|
60
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("compaction reminder tells the session to update its checkpoint now", (t) => {
|
|
64
|
+
const f = fixture(t);
|
|
65
|
+
writeFileSync(path.join(f.tmp, "qc.config.json"), "{}");
|
|
66
|
+
const note = f.at(400_000).hookSpecificOutput.additionalContext;
|
|
67
|
+
assert.match(note, /Update the checkpoint now/);
|
|
68
|
+
assert.ok(note.includes(path.join(f.tmp, ".git", "qc", "checkpoints", "s.md")));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("without checkpoint settings the reminder omits the checkpoint", (t) => {
|
|
72
|
+
const f = fixture(t);
|
|
73
|
+
const note = f.at(400_000).hookSpecificOutput.additionalContext;
|
|
74
|
+
assert.match(note, /compact/);
|
|
75
|
+
assert.doesNotMatch(note, /checkpoint/i);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("an invalid Git pointer preserves the compaction reminder", (t) => {
|
|
79
|
+
const f = fixture(t);
|
|
80
|
+
fs.rmdirSync(path.join(f.tmp, ".git"));
|
|
81
|
+
writeFileSync(path.join(f.tmp, ".git"), "invalid pointer");
|
|
82
|
+
writeFileSync(path.join(f.tmp, "qc.config.json"), "{}");
|
|
83
|
+
const note = f.at(400_000).hookSpecificOutput.additionalContext;
|
|
84
|
+
assert.match(note, /compact/);
|
|
85
|
+
assert.match(note, /Update the checkpoint now: the session checkpoint/);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("state changes use an atomic replacement", (t) => {
|
|
89
|
+
const f = fixture(t);
|
|
90
|
+
f.at(400_000);
|
|
91
|
+
f.at(100_000);
|
|
92
|
+
const originalWrite = fs.writeFileSync;
|
|
93
|
+
let writes = 0;
|
|
94
|
+
t.mock.method(fs, "writeFileSync", (file, ...args) => {
|
|
95
|
+
if (path.dirname(String(file)) === path.dirname(f.state)) {
|
|
96
|
+
assert.notEqual(file, f.state);
|
|
97
|
+
writes += 1;
|
|
98
|
+
assert.doesNotThrow(() => JSON.parse(readFileSync(f.state, "utf8")));
|
|
99
|
+
}
|
|
100
|
+
return originalWrite(file, ...args);
|
|
101
|
+
});
|
|
102
|
+
syncBuiltinESMExports();
|
|
103
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
104
|
+
const result = f.at(400_000);
|
|
105
|
+
t.mock.restoreAll();
|
|
106
|
+
syncBuiltinESMExports();
|
|
107
|
+
assert.ok(result);
|
|
108
|
+
assert.equal(writes, 1);
|
|
109
|
+
assert.equal(JSON.parse(readFileSync(f.state, "utf8")).step >= 0, true);
|
|
110
|
+
assert.deepEqual(readdirSync(path.dirname(f.state)), [path.basename(f.state)]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("overlapping hooks note the same threshold once", (t) => {
|
|
114
|
+
const f = fixture(t);
|
|
115
|
+
f.at(400_000);
|
|
116
|
+
f.at(100_000);
|
|
117
|
+
const originalRead = fs.readFileSync;
|
|
118
|
+
let nested;
|
|
119
|
+
let entered = false;
|
|
120
|
+
t.mock.method(fs, "readFileSync", (file, ...args) => {
|
|
121
|
+
const data = originalRead(file, ...args);
|
|
122
|
+
if (file === f.state && !entered) {
|
|
123
|
+
entered = true;
|
|
124
|
+
nested = f.at(400_000);
|
|
125
|
+
}
|
|
126
|
+
return data;
|
|
127
|
+
});
|
|
128
|
+
syncBuiltinESMExports();
|
|
129
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
130
|
+
const result = f.at(400_000);
|
|
131
|
+
t.mock.restoreAll();
|
|
132
|
+
syncBuiltinESMExports();
|
|
133
|
+
assert.ok(result);
|
|
134
|
+
assert.equal(entered, true);
|
|
135
|
+
assert.equal(nested, null);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a lock older than 30 seconds expires and the next call takes it", (t) => {
|
|
139
|
+
const f = fixture(t);
|
|
140
|
+
const lock = `${f.state}.lock`;
|
|
141
|
+
mkdirSync(lock, { recursive: true });
|
|
142
|
+
const old = new Date(Date.now() - 31_000);
|
|
143
|
+
utimesSync(lock, old, old);
|
|
144
|
+
assert.ok(f.at(210_000));
|
|
145
|
+
assert.deepEqual(JSON.parse(readFileSync(f.state, "utf8")), { step: 0 });
|
|
146
|
+
assert.equal(fs.existsSync(lock), false);
|
|
147
|
+
assert.equal(f.at(210_000), null);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("a lock released before stat stays silent", (t) => {
|
|
151
|
+
const f = fixture(t);
|
|
152
|
+
const lock = `${f.state}.lock`;
|
|
153
|
+
mkdirSync(lock, { recursive: true });
|
|
154
|
+
const originalStat = fs.statSync;
|
|
155
|
+
t.mock.method(fs, "statSync", (file, ...args) => {
|
|
156
|
+
if (file === lock) {
|
|
157
|
+
fs.rmdirSync(lock);
|
|
158
|
+
throw Object.assign(new Error("released"), { code: "ENOENT" });
|
|
159
|
+
}
|
|
160
|
+
return originalStat(file, ...args);
|
|
161
|
+
});
|
|
162
|
+
syncBuiltinESMExports();
|
|
163
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
164
|
+
const result = f.at(210_000);
|
|
165
|
+
t.mock.restoreAll();
|
|
166
|
+
syncBuiltinESMExports();
|
|
167
|
+
assert.equal(result, null);
|
|
168
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("stale lock removal preserves another hook's replacement lock", (t) => {
|
|
172
|
+
const f = fixture(t);
|
|
173
|
+
const lock = `${f.state}.lock`;
|
|
174
|
+
mkdirSync(lock, { recursive: true });
|
|
175
|
+
const old = new Date(Date.now() - 31_000);
|
|
176
|
+
utimesSync(lock, old, old);
|
|
177
|
+
const originalRemove = fs.rmdirSync;
|
|
178
|
+
let removed;
|
|
179
|
+
t.mock.method(fs, "rmdirSync", (file, ...args) => {
|
|
180
|
+
assert.notEqual(file, lock);
|
|
181
|
+
assert.ok(String(file).startsWith(`${lock}.`));
|
|
182
|
+
removed = file;
|
|
183
|
+
mkdirSync(lock);
|
|
184
|
+
return originalRemove(file, ...args);
|
|
185
|
+
});
|
|
186
|
+
syncBuiltinESMExports();
|
|
187
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
188
|
+
const result = f.at(210_000);
|
|
189
|
+
t.mock.restoreAll();
|
|
190
|
+
syncBuiltinESMExports();
|
|
191
|
+
assert.equal(result, null);
|
|
192
|
+
assert.ok(removed);
|
|
193
|
+
assert.equal(fs.existsSync(removed), false);
|
|
194
|
+
assert.equal(fs.existsSync(lock), true);
|
|
195
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("a competing stale lock claim stays silent", (t) => {
|
|
199
|
+
const f = fixture(t);
|
|
200
|
+
const lock = `${f.state}.lock`;
|
|
201
|
+
mkdirSync(lock, { recursive: true });
|
|
202
|
+
const old = new Date(Date.now() - 31_000);
|
|
203
|
+
utimesSync(lock, old, old);
|
|
204
|
+
const originalRename = fs.renameSync;
|
|
205
|
+
let claimed = false;
|
|
206
|
+
t.mock.method(fs, "renameSync", (from, to) => {
|
|
207
|
+
if (from === lock) {
|
|
208
|
+
claimed = true;
|
|
209
|
+
fs.rmdirSync(lock);
|
|
210
|
+
throw Object.assign(new Error("claimed"), { code: "ENOENT" });
|
|
211
|
+
}
|
|
212
|
+
return originalRename(from, to);
|
|
213
|
+
});
|
|
214
|
+
syncBuiltinESMExports();
|
|
215
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
216
|
+
const result = f.at(210_000);
|
|
217
|
+
t.mock.restoreAll();
|
|
218
|
+
syncBuiltinESMExports();
|
|
219
|
+
assert.equal(result, null);
|
|
220
|
+
assert.equal(claimed, true);
|
|
221
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("a lock that turns fresh after the stale rename is deleted without restoring it", (t) => {
|
|
225
|
+
const f = fixture(t);
|
|
226
|
+
const lock = `${f.state}.lock`;
|
|
227
|
+
mkdirSync(lock, { recursive: true });
|
|
228
|
+
const old = new Date(Date.now() - 31_000);
|
|
229
|
+
utimesSync(lock, old, old);
|
|
230
|
+
const originalRename = fs.renameSync;
|
|
231
|
+
let moved;
|
|
232
|
+
let restored = false;
|
|
233
|
+
t.mock.method(fs, "renameSync", (from, to) => {
|
|
234
|
+
if (from === lock) {
|
|
235
|
+
moved = to;
|
|
236
|
+
originalRename(from, to);
|
|
237
|
+
const fresh = new Date();
|
|
238
|
+
utimesSync(to, fresh, fresh);
|
|
239
|
+
return;
|
|
240
|
+
} else if (from === moved) {
|
|
241
|
+
assert.equal(to, lock);
|
|
242
|
+
restored = true;
|
|
243
|
+
}
|
|
244
|
+
return originalRename(from, to);
|
|
245
|
+
});
|
|
246
|
+
syncBuiltinESMExports();
|
|
247
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
248
|
+
const result = f.at(210_000);
|
|
249
|
+
t.mock.restoreAll();
|
|
250
|
+
syncBuiltinESMExports();
|
|
251
|
+
assert.equal(result, null);
|
|
252
|
+
assert.equal(restored, false);
|
|
253
|
+
assert.equal(fs.existsSync(lock), false);
|
|
254
|
+
assert.equal(fs.existsSync(moved), false);
|
|
255
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("release ignores a lock that is already gone", (t) => {
|
|
259
|
+
const f = fixture(t);
|
|
260
|
+
const lock = `${f.state}.lock`;
|
|
261
|
+
const originalRead = fs.readFileSync;
|
|
262
|
+
let released = false;
|
|
263
|
+
t.mock.method(fs, "readFileSync", (file, ...args) => {
|
|
264
|
+
if (file === f.state) {
|
|
265
|
+
fs.rmdirSync(lock);
|
|
266
|
+
released = true;
|
|
267
|
+
}
|
|
268
|
+
return originalRead(file, ...args);
|
|
269
|
+
});
|
|
270
|
+
syncBuiltinESMExports();
|
|
271
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
272
|
+
const result = f.at(210_000);
|
|
273
|
+
t.mock.restoreAll();
|
|
274
|
+
syncBuiltinESMExports();
|
|
275
|
+
assert.ok(result);
|
|
276
|
+
assert.equal(released, true);
|
|
277
|
+
assert.equal(fs.existsSync(lock), false);
|
|
278
|
+
assert.deepEqual(JSON.parse(readFileSync(f.state, "utf8")), { step: 0 });
|
|
279
|
+
assert.equal(f.at(210_000), null);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test("a lock at most 30 seconds old stays held", (t) => {
|
|
283
|
+
const f = fixture(t);
|
|
284
|
+
const lock = `${f.state}.lock`;
|
|
285
|
+
mkdirSync(lock, { recursive: true });
|
|
286
|
+
const modified = statSync(lock).mtimeMs;
|
|
287
|
+
t.mock.method(Date, "now", () => modified + 30_000);
|
|
288
|
+
assert.equal(f.at(210_000), null);
|
|
289
|
+
assert.equal(fs.existsSync(lock), true);
|
|
290
|
+
assert.equal(fs.existsSync(f.state), false);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
for (const operation of ["mkdirSync", "rmdirSync"]) {
|
|
294
|
+
test(`the entry point reports a ${operation} lock error and exits zero without a reminder`, (t) => {
|
|
295
|
+
const f = fixture(t);
|
|
296
|
+
f.at(210_000);
|
|
297
|
+
rmSync(f.state);
|
|
298
|
+
const preload = `import fs from "node:fs";
|
|
299
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
300
|
+
const original = fs.${operation};
|
|
301
|
+
fs.${operation} = (file, ...args) => {
|
|
302
|
+
if (String(file).endsWith(".lock")) throw new Error("lock failed\\nsecond line");
|
|
303
|
+
return original(file, ...args);
|
|
304
|
+
};
|
|
305
|
+
syncBuiltinESMExports();`;
|
|
306
|
+
const result = spawnSync(process.execPath, [
|
|
307
|
+
"--import", `data:text/javascript,${encodeURIComponent(preload)}`,
|
|
308
|
+
fileURLToPath(new URL("./context-watch.mjs", import.meta.url)),
|
|
309
|
+
], {
|
|
310
|
+
input: JSON.stringify(f.call), encoding: "utf8",
|
|
311
|
+
env: { ...process.env, TMPDIR: f.tmp, TMP: f.tmp, TEMP: f.tmp },
|
|
312
|
+
});
|
|
313
|
+
assert.equal(result.status, 0, result.stderr);
|
|
314
|
+
assert.equal(result.stdout, "");
|
|
315
|
+
assert.match(result.stderr, /^context-watch: lock failed second line\r?\n$/);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
test("a lower step above compactAt preserves the threshold already noted", (t) => {
|
|
320
|
+
const f = fixture(t);
|
|
321
|
+
assert.ok(f.at(400_000));
|
|
322
|
+
assert.equal(f.at(350_000), null);
|
|
323
|
+
assert.equal(f.at(400_000), null);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("keeps the context budgets", () => {
|
|
327
|
+
assert.equal(START, 200_000);
|
|
328
|
+
assert.equal(STEP, 50_000);
|
|
329
|
+
assert.equal(TELL_USER, 300_000);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test("speaks once per rising step and resets after compaction", (t) => {
|
|
333
|
+
const f = fixture(t);
|
|
334
|
+
assert.equal(f.at(199_999), null);
|
|
335
|
+
const first = f.at(210_000);
|
|
336
|
+
assert.deepEqual(first, {
|
|
337
|
+
hookSpecificOutput: {
|
|
338
|
+
hookEventName: "PostToolUse",
|
|
339
|
+
additionalContext:
|
|
340
|
+
"Context is about 210K tokens. At the next clean break (a task, review, or merge just finished, nothing mid-fix), update the resume notes and ask the user to run /compact before you continue.",
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
assert.equal(f.at(210_000), null);
|
|
344
|
+
assert.match(f.at(250_001).hookSpecificOutput.additionalContext, /250K.*\/compact/);
|
|
345
|
+
assert.equal(f.at(300_000).systemMessage, "Context 300K tokens: Claude will ask you to /compact at the next clean break.");
|
|
346
|
+
assert.equal(f.at(100_000), null);
|
|
347
|
+
assert.equal(JSON.parse(readFileSync(f.state, "utf8")).step, -1);
|
|
348
|
+
assert.match(f.at(210_000).hookSpecificOutput.additionalContext, /210K.*\/compact/);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("uses the call's event name and speaks at the exact start", (t) => {
|
|
352
|
+
const f = fixture(t);
|
|
353
|
+
const result = f.at(200_000, { hook_event_name: "UserPromptSubmit" });
|
|
354
|
+
assert.equal(result.hookSpecificOutput.hookEventName, "UserPromptSubmit");
|
|
355
|
+
assert.match(result.hookSpecificOutput.additionalContext, /200K/);
|
|
356
|
+
assert.equal(Object.hasOwn(result, "systemMessage"), false);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test("keeps subagent calls silent without changing the main session's step", (t) => {
|
|
360
|
+
const f = fixture(t);
|
|
361
|
+
assert.equal(f.at(300_000, { agent_id: "a1" }), null);
|
|
362
|
+
assert.match(f.at(210_000, { agent_id: "" }).hookSpecificOutput.additionalContext, /210K/);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test("stays silent without a transcript or usage", (t) => {
|
|
366
|
+
const f = fixture(t);
|
|
367
|
+
assert.equal(decide({ session_id: "s" }, { tmp: f.tmp }), null);
|
|
368
|
+
assert.equal(decide(f.call, { tmp: f.tmp }), null);
|
|
369
|
+
writeFileSync(f.call.transcript_path, `${JSON.stringify({ type: "user", message: { content: "hello" } })}\n`);
|
|
370
|
+
assert.equal(decide(f.call, { tmp: f.tmp }), null);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("sanitizes the session id and writes only when the step changes", (t) => {
|
|
374
|
+
const f = fixture(t, "s/../a.b:1_-Z");
|
|
375
|
+
f.at(210_000);
|
|
376
|
+
assert.deepEqual(JSON.parse(readFileSync(f.state, "utf8")), { step: 0 });
|
|
377
|
+
utimesSync(f.state, 1, 1);
|
|
378
|
+
const before = statSync(f.state).mtimeMs;
|
|
379
|
+
assert.equal(f.at(249_999), null);
|
|
380
|
+
assert.equal(statSync(f.state).mtimeMs, before);
|
|
381
|
+
assert.equal(f.at(199_999), null);
|
|
382
|
+
assert.deepEqual(JSON.parse(readFileSync(f.state, "utf8")), { step: -1 });
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test("registers the context watch for prompts and all tool calls", () => {
|
|
386
|
+
const { hooks } = JSON.parse(readFileSync(new URL("../../../../hooks/hooks.json", import.meta.url), "utf8"));
|
|
387
|
+
const command = 'node "${CLAUDE_PLUGIN_ROOT}/packages/qc-harness/src/cli/context-watch.mjs"';
|
|
388
|
+
for (const event of ["UserPromptSubmit", "PostToolUse"]) {
|
|
389
|
+
const group = hooks[event]?.find((entry) => entry.hooks.some((hook) => hook.command === command));
|
|
390
|
+
assert.ok(group, `${event} has a context watch group`);
|
|
391
|
+
assert.deepEqual(group.hooks, [{ type: "command", command, timeout: 10 }]);
|
|
392
|
+
if (event === "PostToolUse") assert.equal(group.matcher, "*");
|
|
393
|
+
else assert.equal(Object.hasOwn(group, "matcher"), false);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
@@ -32,7 +32,7 @@ export async function contractHistory(root, { base, contract, legacy, fields, ex
|
|
|
32
32
|
const contractText = at(contract);
|
|
33
33
|
let operations = [];
|
|
34
34
|
try {
|
|
35
|
-
const found = contractText === null ? [] : await contractOperations(contractText);
|
|
35
|
+
const found = contractText === null ? [] : await contractOperations(contractText, { root });
|
|
36
36
|
operations = found.map((operation) => ({
|
|
37
37
|
id: operation.id,
|
|
38
38
|
method: operation.method,
|