enhance(#4285): resolve context-monitor fire-points from .planning/config.json (#4366)

* enhance(#4285): resolve context-monitor fire-points from .planning/config.json

The monitor's WARNING (35%) and CRITICAL (25%) fire-points were module
constants, so the only way to tune them was editing gsd-context-monitor.js —
a file in the MANAGED hooks registry, whose body the next install re-stages,
silently discarding the edit. The alternative was turning the safety net off.

Both are now readable from the config block the hook already opens:
hooks.context_warning_threshold and hooks.context_critical_threshold. Absent
keys resolve to today's 35/25, so every existing project is byte-identical.

Resolution is total and never throws — this hook must not block the tool call
it rides in on. A value is usable only if Number.isFinite (type-strict, so the
string "30" and true are rejected) and inside the 0-100 domain of the
remaining_percentage it is compared against; anything else falls back to the
default. The PAIR falls back together: critical >= warning has no coherent
reading, and honouring one side silently picks which of the operator's two
numbers to discard. That also covers a single override contradicting the other
key's default.

config-set validates the domain per key so accept and honour agree, but
deliberately does not enforce the pair — it writes one key per call, so a
two-step retune is transiently inconsistent on disk and refusing it there
would block a legitimate configuration.

Registration follows the statusline.show_git precedent: schema manifest plus
src/config.cts validation, not config-defaults.manifest.json and not
buildNewProjectConfig — emitting 35/25 into every new project would pin the
defaults at creation time for a setting nobody has tuned.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DsUAawHKUy9pCpnye1Jzd2

* enhance(#4285): address Codex review — per-key fallback docs, discriminating tests

Codex full-PR review (gpt-6-astra, read-only) returned five findings. Each was
verified against source before acting; all five are real.

1. docs/CONFIGURATION.md described the wrong fallback. An out-of-domain value
   falls back PER KEY; both defaults apply only when the RESOLVED pair violates
   critical < warning. warning 150 with critical 30 resolves to 35/30, not
   35/25 — at remaining 28 that difference changes the severity emitted. The
   table now states the two rules in the order they compose, and
   docs/context-monitor.md gains the same worked example.

2. The inconsistent-pair test could not prove the CRITICAL side reverts: its
   pair was 20/25, and 25 is already the default, so an implementation that
   reset only `warning` passed it. A 45/50 pair — both halves away from their
   defaults — now pins each side with its own reading, and an equal 45/45 pair
   pins that the rule is strict (`<`, not `<=`).

3. The rejection table's rows could not tell rejection from acceptance: an
   accepted -5 pairs with the default critical 25, trips the pair check, and
   produces the same silence. Two rows now separate those: a below-domain
   critical must escalate remaining 20 to CRITICAL (proving -5 was rejected,
   not honoured), and an unusable critical beside a usable warning 45 must
   still fire WARNING at remaining 40 (proving per-key fallback rather than
   reset-both). The over-claiming comments are narrowed to what each row
   actually shows.

4. Scope, reproduced rather than assumed: config-set writes through
   planningDir(), so under GSD_WORKSTREAM it lands in
   .planning/workstreams/<name>/config.json while this hook reads only
   <cwd>/.planning/config.json. That is the pre-existing root-only scope
   hooks.context_warnings has always had, but this PR advertises the setter
   route, so both docs now say the keys are root-project settings.

5. Four other English docs still stated 35/25 as fixed: the REQ-CTX-02/03
   requirements fragment, ARCHITECTURE.md's hook table and threshold table,
   and INVENTORY.md's hook row. All now name them as defaults and point at the
   config keys; docs/FEATURES.md is regenerated from its fragment via
   scripts/gen-features.cjs --write, not hand-edited.

Four new mutations, each reverted after: resetting only the warning half on an
inconsistent pair (1 red), resetting both on any unusable key (1), dropping the
>= 0 bound (1), and accepting critical == warning (1). perf-317 is 116/0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DsUAawHKUy9pCpnye1Jzd2

* enhance(#4285): tighten claims after Codex round 2 — scoped paths, one more discriminator

Confirmation round found no runtime defect and confirmed the five round-1 fixes
landed. Four precision items, all real, all fixed here.

1. The scoped-write note named the wrong path for GSD_PROJECT. planningDir()
   composes three distinct shapes, confirmed by running config-set under each:
   .planning/<project>/config.json, .planning/workstreams/<ws>/config.json, and
   .planning/<project>/workstreams/<ws>/config.json. docs/context-monitor.md
   now tabulates all four cases instead of collapsing them into one.

2. The 45/50 silence row asserted empty stdout without pinning the exit code.
   runMonitorRaw turns a spawn failure, a non-zero exit or a timeout into empty
   stdout as well, so the row could have passed on a dead child. It asserts
   exitCode === 0 first now, like the equal-pair row already did.

3. The sibling row's message claimed it proved critical fell back to 25. It
   does not: coercing '30' to 30 yields WARNING at remaining 40 too, so the row
   pins the WARNING side surviving and nothing more. Message narrowed, and a
   new row reads the same config at remaining 28, where the two candidate
   resolutions diverge — rejected gives (45, 25) and WARNING, coerced gives
   (45, 30) and CRITICAL. Mutation-verified: swapping Number.isFinite for the
   coercing global reds it.

4. "Accept and honour must agree" was too absolute in the src/config.cts and
   tests/config.test.cjs comments. The agreement holds on the DOMAIN and per
   key: an accepted value can still lose to the hook's pair check at read time,
   and a scoped write never reaches the hook at all. Likewise a two-step retune
   only CAN be transiently inconsistent — 35/25 to 20/10 is valid throughout if
   critical moves first — so the docs now say what a setter-side pair check
   would actually cost: rejecting that intermediate write and forcing an order.

The same over-absolute phrasing is in b7d179c89's message, which is left as
written rather than rewriting history; this commit and the PR body carry the
precise claim.

perf-317 117/0, config 192/0, config-field-docs 47/0, features-index-gate 84/0,
lint:ci clean cold.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DsUAawHKUy9pCpnye1Jzd2

* chore(#4285): add changeset

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DsUAawHKUy9pCpnye1Jzd2

* enhance(#4285): address review — planning-config rows, resolveThresholds properties

Two Minor findings from the maintainer review, no behaviour change.

Minor 1: gsd-core/references/planning-config.md's "Hook Fields" table gains
rows for hooks.context_warning_threshold and hooks.context_critical_threshold,
in that table's 5-column form, carrying the same per-key-fallback,
pair-reversion and root-config-scope claims docs/CONFIGURATION.md already
makes. hooks.workflow_guard's absence from that table is pre-existing and
out of scope here.

Minor 2: resolveThresholds() gets fast-check property coverage, which ADR 456
requires of a threshold/limit contract. Reaching it needed a require-time
seam: the resolver was previously observable only by spawning the hook, and a
subprocess per case cannot drive 200 runs — the same conclusion CONTEXT-INDEX
records for the ROADMAP Requirements parser. The stdin adapter therefore moves
into main() behind `require.main === module`, mirroring
gsd-cursor-subagent-start.js and gsd-statusline.js, and module.exports exposes
the resolver plus both default constants so a test asserts fallback against
the source of truth rather than a second copy of 35/25. Spawned behaviour is
unchanged: the 10s stdin timeout still arms per invocation (stdinTimeout is
now a module-scope let assigned in main(), still cleared by the end handler),
and the try/catch crash(ON_CRASH) path is untouched.

Seven properties: totality, ordering, exactness, togetherness, non-vacuity,
per-key fallback, non-object argument. Exactness is stated PER KEY — a mixed
result (one key honoured, one fallen back) is legal and is the documented
contract; the property falsified a per-pair phrasing of it in 4 runs.

Verified: cold lint:ci 0; perf-317 file 125/0; seven mutations killed and
restored, one of which (upper bound widened to 120) is invisible to the 17
hand-written cases and caught only by a property.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZw5UhR474YLyE4knjHrte

* enhance(#4285): close the Codex-found gap in the property coverage

Codex whole-PR review of round 3 returned no Blocker and no Major. Two items,
both in the tests added this round, both verified against source before acting.

Minor — the per-key fallback property was asymmetric: it required a usable
warning to survive an unusable critical, but never the reverse. A resolver
that reverted BOTH keys the moment warning was unusable passed all seven
properties. Reproduced exactly: that mutant answers 35/25 for
{warning: 150, critical: 30} where the resolver answers 35/30, and the file
stayed green at 125/0. The mirrored property closes it — with the mutant
re-applied it is now the single failing row, and it is the only row that
fails, so it is load-bearing rather than incidental.

Nit — the ordering property's comment credited it with catching a
half-honoured pair, which it does not: 45/50 "repaired" by resetting only
critical yields 45/25, perfectly ordered. That case belongs to togetherness.
The same comment claimed the behavioural rows sample an inconsistent pair at
exactly one point; stale — they cover 20/25, 45/50 and the 45/45 equality
boundary. Both claims corrected in place.

Verified: cold lint:ci 0; perf-317 file 126/0; the mutant above killed by the
new property alone and the hook restored byte-identical afterwards.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZw5UhR474YLyE4knjHrte

* enhance(#4285): name the installed-monitor prerequisite; close the negative-critical gap

Second Codex whole-PR pass, run because the base moved: the author's three
"Update branch" merges pulled ~26 upstream commits in, so the previously
reviewed diff sat on a base that no longer exists. No Blocker, no Major, two
Minor — both verified against source before acting.

Minor 1, and only reachable because of what the merge brought in: #2586
(03738824d) landed in that window and stops staging
hooks/gsd-context-monitor.js for Codex, since the metrics bridge it reads is
written only by hooks/gsd-statusline.js, which Codex never installs
(bin/install.js: "gsd-context-monitor.js is deliberately NOT copied for
Codex"). These two keys are read by that hook and nothing else, so on such a
runtime config-set stores and validates them and nothing consumes them — a
claim the docs this PR adds did not make. docs/context-monitor.md now carries
the explanation and both key tables carry a clause pointing at it; the FEATURES
and INVENTORY entries already link through to those two files, so they are not
edited again. The changeset says it too, because it is user-facing.

Accepting the keys on every runtime is kept deliberately: config is shared
across runtimes, so validation stays runtime-independent and the runtime
caveat lives in documentation rather than in the setter.

Minor 2: the per-key fallback property's junk generator had no negative arm,
though its mirror did — and that asymmetry hid a gap. A resolver reverting
BOTH keys whenever critical is negative answers 35/25 for {45, -5} where the
resolver answers 45/25, and it passed all 126 tests. With the negative arm
added it is the single failing row.

Verified: cold lint:ci 0; perf-317 126/0; both mutants above killed and the
hook restored byte-identical; 538/0 across the config, changeset, doc-parity
and emitted-attribution gates.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZw5UhR474YLyE4knjHrte

* enhance(#4285): refuse the two dead threshold endpoints; resolve absent keys

Maintainer review round 2 raised two Minors and a nit.

Minor 1 — `hooks.context_warning_threshold: 0` was accepted and stored but can
never take effect: `critical < warning` must hold and both sides are clamped to
0-100, so nothing can sit below a warning of 0. Verifying it surfaced the MIRROR
case the review did not name: `critical: 100` is equally dead, since nothing can
sit above it. Both confirmed against the real resolver for partners {absent, 0,
50, 100}, with 0.001 and 99.999 honoured as controls.

`config-set` now refuses both, because storing a value the reader always
discards is the accept-then-discard shape this codebase refuses elsewhere. The
hook is unchanged and still total — it degrades to defaults rather than
throwing, so a project that already carries one of these on disk still loads.
The old "accepts the domain bounds 0 and 100" row asserted the misleading half
and is replaced by tables that make the asymmetry the point (0 is legal for
critical and illegal for warning; 100 is the reverse), plus a control row so
"refuse both endpoints outright" would not pass in its place.

Minor 2 — the keys are absent from config-defaults.manifest.json /
buildNewProjectConfig where the sibling `hooks.context_warnings` lives. Kept
that way: buildNewProjectConfig writes a hooks object into every NEW project's
config.json, which would freeze today's fire-points as an explicit per-project
override everywhere — the opposite of this PR's premise. But the underlying
complaint was real, so the actual symptom is fixed: `config-get` on an absent
key returned "Key not found" while the hook silently used 35/25. It now resolves
through SCHEMA_DEFAULTS. Restated rather than derived because CONFIG_DEFAULTS is
re-exported flattened and has no `hooks` member at runtime; the one resulting
copy of 35/25 outside the hook is pinned against the hook's exported constants
by a drift test (red-checked: moving the literal to 40 reds it).

Nit — PR-body counts unverifiable from the diff. Noted, no code change.

Codex round 3 then found a broken doc link (`context-monitor.md` resolved
inside gsd-core/references/, where it does not exist; the emitted tree's own
convention is `../../docs/...`) and a stale comment still describing the
manifest-derived approach I had backed out. Both fixed. It also corrected my
rationale on a point of fact: manifest entries alone would NOT have reached new
project configs, since buildNewProjectConfig builds its own literal — the
freezing argument applies to that function, not to the manifest. The comment now
says so rather than running the two together.

Verified: cold lint:ci 0; full suite 36,082 / 0 fail before these two fixes,
config + perf-317 321/0 after; drift pin red-checked.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZw5UhR474YLyE4knjHrte

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Tom Boucher <trekkie@nomorestars.com>
This commit is contained in:
Behruz Nassre Esfahani
2026-09-08 21:31:14 -07:00
committed by GitHub
parent 6c5e11049b
commit 3ad75a6d59
13 changed files with 919 additions and 22 deletions

View File

@@ -3181,3 +3181,127 @@ describe('references/checkpoints.md documents the flag', () => {
});
});
}
describe('config-set hooks.context_warning_threshold / hooks.context_critical_threshold (#4285)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
runGsdTools('config-ensure-section', tmpDir, { HOME: tmpDir, USERPROFILE: tmpDir });
});
afterEach(() => {
cleanup(tmpDir);
});
// Registration: before #4285 both keys were rejected outright as unknown, so
// the only way to tune a fire-point was editing the managed hook file.
test('both keys are accepted by config-set and persisted', () => {
for (const [key, value] of [
['hooks.context_warning_threshold', 45],
['hooks.context_critical_threshold', 30],
]) {
const result = runGsdTools(`config-set ${key} ${value}`, tmpDir);
assert.ok(result.success, `config-set ${key} failed: ${result.error}`);
const config = readConfig(tmpDir);
assert.strictEqual(config.hooks[key.split('.')[1]], value);
}
});
// The domain the hook compares against (remaining_percentage) is 0-100
// inclusive, but the two ENDPOINTS that can never form a valid pair are
// refused rather than stored (#4285 review). `critical < warning` must hold at
// read time and both sides are clamped to 0-100, so `warning: 0` has no legal
// partner below it and `critical: 100` has none above it. Each is discarded by
// the hook for EVERY value of the other key, so storing one would report a
// tuning that can never take effect.
//
// Stated as a pair of tables rather than one row per case, because the
// asymmetry is the point: 0 is legal for critical and illegal for warning, and
// 100 is the reverse. A single "bounds are inclusive" row (which this replaces)
// asserted the misleading half and was what let the dead value through.
test('refuses the two endpoints that can never form a valid pair', () => {
for (const [key, dead] of [
['hooks.context_warning_threshold', 0],
['hooks.context_critical_threshold', 100],
]) {
const before = JSON.stringify(readConfig(tmpDir));
const result = runGsdTools(`config-set ${key} ${dead}`, tmpDir);
assert.ok(!result.success, `config-set ${key} ${dead} must fail — no partner value can satisfy the pair check`);
assert.strictEqual(JSON.stringify(readConfig(tmpDir)), before,
`${key}: a refused write must leave the config byte-identical`);
}
});
test('accepts the endpoints that ARE reachable, and the values just inside the dead ones', () => {
// The controls for the row above. Without these, refusing 0 and 100
// outright would pass it just as well as refusing only the dead pairing.
for (const [key, live] of [
['hooks.context_warning_threshold', 100], // warning 100 pairs with the default critical 25
['hooks.context_critical_threshold', 0], // critical 0 pairs with the default warning 35
['hooks.context_warning_threshold', 0.001], // just inside the dead endpoint
['hooks.context_critical_threshold', 99.999],
]) {
const result = runGsdTools(`config-set ${key} ${live}`, tmpDir);
assert.ok(result.success, `config-set ${key} ${live} must succeed: ${result.error}`);
assert.strictEqual(readConfig(tmpDir).hooks[key.split('.')[1]], live);
}
});
// The query surface must agree with the reader: an ABSENT key resolves to the
// hook's own default rather than "Key not found" (#4285 review). SCHEMA_DEFAULTS
// necessarily restates 35/25 outside the hook — the hook is a standalone
// subprocess on a hot path and cannot read a manifest to learn its own
// defaults — so this row pins the two copies against each other. If either
// moves alone, this goes red.
test('an absent threshold resolves to the hook\'s own constant, not "Key not found"', () => {
const monitor = require('../hooks/gsd-context-monitor.js');
for (const [key, constant] of [
['hooks.context_warning_threshold', monitor.WARNING_THRESHOLD],
['hooks.context_critical_threshold', monitor.CRITICAL_THRESHOLD],
]) {
const result = runGsdTools(`config-get ${key}`, tmpDir);
assert.ok(result.success, `config-get ${key} failed: ${result.error}`);
assert.strictEqual(Number(String(result.output).trim()), constant,
`${key} must resolve to the hook's own default (${constant}); a drift here means ` +
'SCHEMA_DEFAULTS and the hook constants disagree');
}
});
// Accept and honour must agree ON THE DOMAIN: the hook falls back to its
// default for a value outside 0-100, so config-set reporting success on such
// a value would tell the operator a tuning took effect when it did not. The
// agreement is per-key and no wider — an accepted value can still lose to the
// hook's pair check at read time, and a scoped write (GSD_PROJECT /
// GSD_WORKSTREAM) lands in a config the hook does not read at all.
test('rejects values the hook would discard, and leaves the config untouched', () => {
const before = JSON.stringify(readConfig(tmpDir));
for (const value of ['101', '-1', 'high', 'true']) {
for (const key of ['hooks.context_warning_threshold', 'hooks.context_critical_threshold']) {
const result = runGsdTools(`config-set ${key} ${value}`, tmpDir);
assert.ok(!result.success, `config-set ${key} ${value} must be rejected, not silently stored`);
}
}
assert.strictEqual(JSON.stringify(readConfig(tmpDir)), before,
'a rejected config-set must not have written anything');
});
// Deliberate non-guard, documented in docs/context-monitor.md: config-set
// writes ONE key per call, so a two-step retune (warning first, then
// critical) is transiently inconsistent on disk. Refusing it here would block
// a legitimate configuration; the hook resolves the pair at read time
// instead, falling back to both defaults while it is inconsistent.
test('does NOT enforce the pair ordering across two calls', () => {
const warn = runGsdTools('config-set hooks.context_warning_threshold 20', tmpDir);
assert.ok(warn.success, `config-set must accept a warning threshold below the default critical: ${warn.error}`);
const crit = runGsdTools('config-set hooks.context_critical_threshold 10', tmpDir);
assert.ok(crit.success, `config-set must accept the second half of the retune: ${crit.error}`);
const config = readConfig(tmpDir);
assert.strictEqual(config.hooks.context_warning_threshold, 20);
assert.strictEqual(config.hooks.context_critical_threshold, 10);
});
});

View File

@@ -14,6 +14,7 @@
* - #2289 — output-envelope allowlist; side effects still run on silent events
* - #1974 — one-time critical-session breadcrumb
* - #3709 — PreCompact clears the warn sentinel AND the metrics bridge
* - #4285 — WARNING/CRITICAL fire-points resolve from .planning/config.json
* Extend this list when folding in the next one.
*/
@@ -41,19 +42,36 @@ const tmpDir = os.tmpdir();
* @param {number} [opts.usedPct] - used_pct for bridge file
* @param {boolean} [opts.writeWarn] - if true, write a warn sentinel before spawn
* @param {object} [opts.warnData] - content for warn sentinel (defaults to first-warn-like data)
* @param {object} [opts.planningConfig] - when given, run in a throwaway project dir
* holding this object as .planning/config.json (#4285). Mutually exclusive with
* `cwd`, which the caller no longer chooses; the dir is removed on the way out.
* @returns {{ exitCode: number, stdout: string }}
*/
function runMonitorRaw(opts) {
const {
sessionId,
cwd = tmpDir,
writeMetrics = false,
remaining = 20,
usedPct = 80,
writeWarn = false,
warnData = null,
planningConfig = null,
} = opts;
// A staged config needs a project dir of its own; without one the caller's
// cwd (tmpDir by default) is used exactly as before.
const stagedCwd = planningConfig === null
? null
: fs.mkdtempSync(path.join(tmpDir, 'gsd-4285-cfg-'));
if (stagedCwd !== null) {
fs.mkdirSync(path.join(stagedCwd, '.planning'), { recursive: true });
fs.writeFileSync(
path.join(stagedCwd, '.planning', 'config.json'),
JSON.stringify(planningConfig)
);
}
const cwd = stagedCwd ?? opts.cwd ?? tmpDir;
const metricsPath = path.join(tmpDir, `claude-ctx-${sessionId}.json`);
const warnPath = path.join(tmpDir, `claude-ctx-${sessionId}-warned.json`);
@@ -90,6 +108,7 @@ function runMonitorRaw(opts) {
} finally {
try { fs.unlinkSync(metricsPath); } catch { /* already absent */ }
try { fs.unlinkSync(warnPath); } catch { /* already absent */ }
if (stagedCwd !== null) cleanup(stagedCwd);
}
return { exitCode, stdout };
@@ -2493,3 +2512,528 @@ describe('#3709 round 3: DEBOUNCE_CALLS and STALE_SECONDS at their limits', () =
});
});
// ─── #4285: WARNING/CRITICAL fire-points resolve from .planning/config.json ───
describe('#4285 regression: context-monitor thresholds resolve from .planning/config.json', () => {
// Why these are behavioural spawns rather than unit calls on resolveThresholds:
// the value under test is not the resolver's return, it is WHICH fire-point the
// running hook compares `remaining_percentage` against. A unit test on the
// resolver would pass even if the resolved pair were never threaded to the two
// comparison sites, which is the whole of the change.
const sid = (tag) => `test-4285-${tag}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const severityOf = (stdout) => JSON.parse(stdout)?.hookSpecificOutput?.severity;
test('a raised warning threshold fires where the default is silent', () => {
// remaining 40 is ABOVE the default 35 → the hook is silent by default.
// The control below proves that; without it this test would pass on a hook
// that emits for every reading.
const control = runMonitorRaw({ sessionId: sid('raise-control'), writeMetrics: true, remaining: 40, usedPct: 60 });
assert.strictEqual(control.stdout, '',
'control: remaining 40 must be silent under the default 35 threshold — ' +
'if this emits, the test below proves nothing about the config key');
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('raise'),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: { hooks: { context_warning_threshold: 45 } },
});
assert.strictEqual(exitCode, 0, 'resolving a configured threshold must not fail the hook');
assert.match(stdout, /CONTEXT WARNING/,
'a configured warning threshold of 45 must fire at remaining 40; still silent means ' +
'the hook is comparing against the hardcoded 35');
assert.strictEqual(severityOf(stdout), 'warning', 'crossing only the warning point is not CRITICAL');
});
test('a raised critical threshold escalates a reading the default calls WARNING', () => {
// remaining 32: default resolves warning (32 <= 35, 32 > 25). With critical
// moved to 35 the same reading is CRITICAL — so this pins the critical key
// specifically, not just "some threshold was read".
const control = runMonitorRaw({ sessionId: sid('crit-control'), writeMetrics: true, remaining: 32, usedPct: 68 });
assert.strictEqual(severityOf(control.stdout), 'warning',
'control: remaining 32 is a WARNING under the defaults');
const { stdout } = runMonitorRaw({
sessionId: sid('crit'),
writeMetrics: true,
remaining: 32,
usedPct: 68,
planningConfig: { hooks: { context_warning_threshold: 45, context_critical_threshold: 35 } },
});
assert.strictEqual(severityOf(stdout), 'critical',
'a configured critical threshold of 35 must escalate remaining 32 to CRITICAL');
});
test('a lowered warning threshold silences a reading the default warns on', () => {
// The opposite direction — proves the key moves the fire-point rather than
// only ever adding warnings.
const control = runMonitorRaw({ sessionId: sid('lower-control'), writeMetrics: true, remaining: 30, usedPct: 70 });
assert.match(control.stdout, /CONTEXT WARNING/,
'control: remaining 30 warns under the defaults');
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('lower'),
writeMetrics: true,
remaining: 30,
usedPct: 70,
planningConfig: { hooks: { context_warning_threshold: 20, context_critical_threshold: 10 } },
});
assert.strictEqual(exitCode, 0, 'hook exits 0 when the configured thresholds silence it');
assert.strictEqual(stdout, '',
'with the pair moved to 20/10, remaining 30 is above the warning point and must be silent');
});
test('an unusable value falls back to that key\'s default rather than throwing', () => {
// One row per rejection reason, each run at remaining 40, where the default
// is silent and the honoured value (45) is not. Silence here is NOT a unique
// signature of rejection — Codex review of this PR showed that an accepted
// out-of-domain value can reach the same silence through the pair check
// instead (an accepted -5 pairs with the default critical 25, which is
// >= -5, so both revert and 40 is silent again). So this table proves
// "unusable input never fires early and never throws"; the two rows BELOW
// are what separate per-key fallback from honouring the value.
const rejected = [
['string', '45'],
['above domain', 150],
['negative', -5],
['null', null],
['boolean', true],
['array', [45]],
['object', {}],
];
for (const [label, value] of rejected) {
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid(`bad-${label.replace(/\W+/g, '-')}`),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: { hooks: { context_warning_threshold: value } },
});
assert.strictEqual(exitCode, 0, `${label}: an unusable threshold must never fail the hook`);
assert.strictEqual(stdout, '',
`${label}: an unusable threshold must fall back to the default 35, leaving remaining 40 silent`);
}
});
test('100 is inside the domain, not rejected as out of range', () => {
// The bound is inclusive on the top. 100 warns at every reading; if the
// range check were `< 100` this would fall back to 35 and go silent at
// remaining 40, which is exactly what the rejection table above asserts for
// a genuinely out-of-domain 150.
const { stdout } = runMonitorRaw({
sessionId: sid('bound-100'),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: { hooks: { context_warning_threshold: 100 } },
});
assert.match(stdout, /CONTEXT WARNING/,
'a warning threshold of 100 is in-domain and must fire at remaining 40');
});
test('an inconsistent pair falls back to BOTH defaults, not to the usable half', () => {
// warning 20 / critical 25 is inconsistent (critical >= warning). Honouring
// the warning half alone would leave remaining 30 SILENT; falling back to
// both defaults warns. NOTE the limit of this row, raised by Codex review:
// critical 25 IS the default here, so it cannot show that the CRITICAL side
// reverts — an implementation that reset only `warning` would pass it. The
// 45/50 block below is what pins both halves.
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('pair'),
writeMetrics: true,
remaining: 30,
usedPct: 70,
planningConfig: { hooks: { context_warning_threshold: 20, context_critical_threshold: 25 } },
});
assert.strictEqual(exitCode, 0, 'an inconsistent pair must never fail the hook');
assert.match(stdout, /CONTEXT WARNING/,
'an inconsistent pair resolves to the defaults (35/25), which warn at remaining 30; ' +
'silence here would mean the warning half was honoured on its own');
assert.strictEqual(severityOf(stdout), 'warning',
'the default critical (25) is below remaining 30, so the fallback pair yields WARNING');
});
test('a single override is checked against the OTHER key\'s default', () => {
// Same rule as the row above, reached with one key set instead of two — the
// case the docs call out, because it is the one an operator hits by accident.
const { stdout } = runMonitorRaw({
sessionId: sid('single'),
writeMetrics: true,
remaining: 30,
usedPct: 70,
planningConfig: { hooks: { context_warning_threshold: 20 } },
});
assert.match(stdout, /CONTEXT WARNING/,
'warning 20 against the default critical 25 is inconsistent and resolves to 35/25, ' +
'which warns at remaining 30');
});
test('an invalid key falls back alone — the sibling override survives', () => {
// Codex review: nothing above separated per-key fallback from a
// reset-BOTH implementation. Warning 45 is usable, critical is not.
// Per key -> (45, 25): remaining 40 is <= 45 and > 25, so WARNING.
// Reset both -> (35, 25): remaining 40 is above 35, so SILENCE.
const { stdout } = runMonitorRaw({
sessionId: sid('sibling'),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: { hooks: { context_warning_threshold: 45, context_critical_threshold: '30' } },
});
assert.match(stdout, /CONTEXT WARNING/,
'an unusable critical must not drag the usable warning override down with it');
// What this row pins is the WARNING side surviving. It does NOT by itself
// prove critical became 25: coercing '30' to 30 would also yield WARNING at
// remaining 40 (Codex review, round 2). The next row settles that.
assert.strictEqual(severityOf(stdout), 'warning',
'remaining 40 is above any resolved critical here, so the severity is the warning rung');
});
test('a numeric-looking STRING is rejected, not coerced', () => {
// Same config as the row above, read at 28 — the reading that separates the
// two candidate resolutions:
// rejected -> (45, 25): 28 > 25, so WARNING.
// coerced -> (45, 30): 28 <= 30, so CRITICAL.
const { stdout } = runMonitorRaw({
sessionId: sid('string-critical'),
writeMetrics: true,
remaining: 28,
usedPct: 72,
planningConfig: { hooks: { context_warning_threshold: 45, context_critical_threshold: '30' } },
});
assert.strictEqual(severityOf(stdout), 'warning',
"'critical' at remaining 28 means the string '30' was coerced into a fire-point; " +
'Number.isFinite must reject it and leave critical at its default 25');
});
test('a below-domain critical is rejected rather than honoured', () => {
// Codex review: the -5 row in the table above cannot tell rejection from
// acceptance. Here it can. Warning 45 with critical -5:
// rejected -> (45, 25): remaining 20 is <= 25, so CRITICAL.
// honoured -> (45, -5): remaining 20 is above -5, so merely WARNING.
const { stdout } = runMonitorRaw({
sessionId: sid('neg-critical'),
writeMetrics: true,
remaining: 20,
usedPct: 80,
planningConfig: { hooks: { context_warning_threshold: 45, context_critical_threshold: -5 } },
});
assert.strictEqual(severityOf(stdout), 'critical',
'a negative critical must fall back to 25 and escalate remaining 20; ' +
"'warning' here means -5 was honoured as a fire-point");
});
describe('an inconsistent pair of TWO configured values reverts both', () => {
// Codex review: the 20/25 row cannot prove the critical side resets, because
// 25 IS the default — an implementation that reset only `warning` would pass
// it. 45/50 is inconsistent with BOTH halves away from their defaults, so
// each reading below fails a different partial implementation.
const pair = { context_warning_threshold: 45, context_critical_threshold: 50 };
test('the warning half reverts: remaining 40 is silent', () => {
// Reverted -> warning 35, and 40 > 35 -> silence.
// Warning 45 preserved -> 40 <= 45 -> a warning would fire.
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('pair45-40'), writeMetrics: true, remaining: 40, usedPct: 60,
planningConfig: { hooks: { ...pair } },
});
// exitCode first: the helper turns a spawn failure, a non-zero exit or a
// timeout into empty stdout too, so asserting silence alone would pass on
// a crashed child (Codex review, round 2).
assert.strictEqual(exitCode, 0, 'the silence below must come from the threshold, not from a dead child');
assert.strictEqual(stdout, '',
'output at remaining 40 means the configured warning 45 survived an inconsistent pair');
});
test('the critical half reverts: remaining 32 is WARNING, not CRITICAL', () => {
// Reverted -> critical 25, and 32 > 25 -> severity 'warning'.
// Critical 50 preserved -> 32 <= 50 -> severity 'critical'.
const { stdout } = runMonitorRaw({
sessionId: sid('pair45-32'), writeMetrics: true, remaining: 32, usedPct: 68,
planningConfig: { hooks: { ...pair } },
});
assert.match(stdout, /CONTEXT WARNING/, 'the default warning 35 must fire at remaining 32');
assert.strictEqual(severityOf(stdout), 'warning',
"'critical' at remaining 32 means the configured critical 50 survived an inconsistent pair");
});
});
test('an EQUAL pair is inconsistent too — critical must fire strictly deeper', () => {
// The boundary of the pair rule: `critical < warning`, not `<=`. With 45/45
// honoured, remaining 40 would be <= 45 on BOTH tests and every warning
// would arrive pre-escalated to CRITICAL. Rejected, both revert to 35/25 and
// remaining 40 is above the warning point.
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('equal-pair'),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: { hooks: { context_warning_threshold: 45, context_critical_threshold: 45 } },
});
assert.strictEqual(exitCode, 0, 'an equal pair must never fail the hook');
assert.strictEqual(stdout, '',
'an equal pair must revert to 35/25, leaving remaining 40 silent');
});
test('context_warnings:false still wins over configured thresholds', () => {
// A project that tuned the thresholds and later switched warnings off stays
// silent: the disable exit is unconditional, so no configured fire-point can
// resurrect it. NOT an ordering guard — measured: moving the resolution
// above the disable check leaves this row green, because allow() exits
// either way. The ordering is a cost choice (don't resolve on a path that
// exits), not an observable, so nothing here pins it.
const { exitCode, stdout } = runMonitorRaw({
sessionId: sid('disabled'),
writeMetrics: true,
remaining: 40,
usedPct: 60,
planningConfig: {
hooks: {
context_warnings: false,
context_warning_threshold: 45,
context_critical_threshold: 30,
},
},
});
assert.strictEqual(exitCode, 0, 'hook exits 0 when warnings are disabled');
assert.strictEqual(stdout, '',
'context_warnings:false must silence the hook even when a threshold would have fired');
});
});
// ─── #4285: resolveThresholds domain invariants (property-based) ─────────────
const fc = require('./helpers/fast-check-setup.cjs');
const {
resolveThresholds,
WARNING_THRESHOLD,
CRITICAL_THRESHOLD,
} = require(MONITOR_PATH);
describe('#4285 properties: resolveThresholds holds its domain invariants for arbitrary input', () => {
// The spawn-based rows above pin that the RESOLVED pair reaches the two
// comparison sites. They cannot pin the resolver over its numeric domain:
// each case costs a subprocess, and only pairs that change an observable
// severity are visible at all. These properties cover the other half — the
// resolver as a total function — per ADR 456 (threshold/limit contracts get
// fast-check coverage) and the seam rule the repo already applies elsewhere
// (CONTEXT-INDEX, on the ROADMAP Requirements parser: a closure reachable
// only by spawning the CLI is one "no fast-check property can do", so it is
// exported and driven directly instead). No path literal here on purpose —
// the docs-guard lint reads a bare one as this file guarding a shipped doc.
const DEFAULTS = { warning: WARNING_THRESHOLD, critical: CRITICAL_THRESHOLD };
// Deliberately wider than the accepted domain: NaN/±Infinity (fc.double's
// default), out-of-range and negative reals, and non-numbers of every shape.
const anyThreshold = fc.oneof(
{ weight: 6, arbitrary: fc.double() },
{ weight: 3, arbitrary: fc.double({ min: -1000, max: 1000, noNaN: true }) },
{ weight: 1, arbitrary: fc.anything() },
);
const isDefaults = (r) => r.warning === DEFAULTS.warning && r.critical === DEFAULTS.critical;
test('the defaults themselves satisfy the invariant they are the fallback for', () => {
// If this ever goes red the constants have drifted into the state the
// resolver rejects, and every fallback below would return a nonsense pair.
assert.ok(Number.isFinite(WARNING_THRESHOLD) && WARNING_THRESHOLD >= 0 && WARNING_THRESHOLD <= 100);
assert.ok(Number.isFinite(CRITICAL_THRESHOLD) && CRITICAL_THRESHOLD >= 0 && CRITICAL_THRESHOLD <= 100);
assert.ok(CRITICAL_THRESHOLD < WARNING_THRESHOLD);
});
test('totality: no input throws, and the pair is always two finite numbers in [0, 100]', () => {
fc.assert(
fc.property(anyThreshold, anyThreshold, (w, c) => {
const r = resolveThresholds({ context_warning_threshold: w, context_critical_threshold: c });
return (
Number.isFinite(r.warning) && r.warning >= 0 && r.warning <= 100 &&
Number.isFinite(r.critical) && r.critical >= 0 && r.critical <= 100
);
}),
);
});
test('ordering: the resolved pair always satisfies critical < warning', () => {
// This property enforces ORDERING only, and nothing more. It is NOT the
// one that catches a half-honoured pair: a resolver that "repaired" 45/50
// by resetting only critical returns 45/25, which is perfectly ordered and
// sails through here. `togetherness` below is what pins simultaneous
// reversion. (Both claims corrected after Codex review of this round — the
// comment previously credited this property with catching that case, and
// also claimed the behavioural rows sample an inconsistent pair at exactly
// one point, which is stale: they cover 20/25, 45/50 and the 45/45
// equality boundary.)
fc.assert(
fc.property(anyThreshold, anyThreshold, (w, c) => {
const r = resolveThresholds({ context_warning_threshold: w, context_critical_threshold: c });
return r.critical < r.warning;
}),
);
});
test('exactness: each side is the configured value or that key\'s default — never a third number', () => {
// Rules out a resolver that "repairs" an out-of-domain or inconsistent
// input by clamping or nudging it: 150 must become 35, not 100.
//
// Stated per key rather than per pair, because a MIXED result is legal and
// is the documented per-key fallback — warning 150 with critical 0 resolves
// to {35, 0}, which is neither "the configured pair" nor "the defaults".
// (Found by this property on its first run, against a per-pair phrasing.)
fc.assert(
fc.property(anyThreshold, anyThreshold, (w, c) => {
const r = resolveThresholds({ context_warning_threshold: w, context_critical_threshold: c });
return (
(r.warning === w || r.warning === WARNING_THRESHOLD) &&
(r.critical === c || r.critical === CRITICAL_THRESHOLD)
);
}),
);
});
test('togetherness: an inconsistent RESOLVED pair reverts both sides, not the offending one', () => {
// The complement of the property above: mixing is legal only while the
// resulting pair stays ordered. Once it does not, the result is the exact
// defaults — no half-honoured pair survives.
fc.assert(
fc.property(
fc.double({ min: 0, max: 100, noNaN: true }),
fc.double({ min: 0, max: 100, noNaN: true }),
(a, b) => {
fc.pre(b >= a); // in-domain but inconsistent: critical >= warning
return isDefaults(resolveThresholds({
context_warning_threshold: a,
context_critical_threshold: b,
}));
},
),
);
});
test('non-vacuity: every in-domain consistent pair is honoured verbatim', () => {
// Without this, `resolveThresholds = () => DEFAULTS` passes all three
// properties above. This is the one that makes them mean something.
fc.assert(
fc.property(
fc.double({ min: 0, max: 100, noNaN: true }),
fc.double({ min: 0, max: 100, noNaN: true }),
(a, b) => {
const warning = Math.max(a, b);
const critical = Math.min(a, b);
fc.pre(critical < warning);
const r = resolveThresholds({
context_warning_threshold: warning,
context_critical_threshold: critical,
});
return r.warning === warning && r.critical === critical;
},
),
);
});
test('per-key fallback: one unusable key does not discard the other usable one', () => {
// Pins the per-key half of the contract the docs promise. warning is left
// at a value that stays consistent with the default critical (25), so a
// resolved pair survives and the honoured half is observable.
fc.assert(
fc.property(
fc.double({ min: 26, max: 100, noNaN: true }),
// The negative arm is not decoration: without it a resolver that
// reverts BOTH keys whenever critical is negative passes every other
// property (verified — it answers 35/25 for {45, -5} where the
// resolver answers 45/25). Found by Codex review of this round; the
// mirrored property below already carried its negative arm, and the
// asymmetry between the two is exactly what hid the gap.
fc.oneof(fc.string(), fc.boolean(), fc.constant(null), fc.constant(undefined), fc.constant(NaN), fc.constant(Infinity), fc.double({ min: 100.001, max: 1e6, noNaN: true }), fc.double({ min: -1e6, max: -0.001, noNaN: true })),
(warning, junkCritical) => {
const r = resolveThresholds({
context_warning_threshold: warning,
context_critical_threshold: junkCritical,
});
return r.warning === warning && r.critical === CRITICAL_THRESHOLD;
},
),
);
});
test('per-key fallback, mirrored: one unusable WARNING does not discard a usable critical', () => {
// The mirror of the property above, and NOT redundant with it: without
// this, a resolver that reverts BOTH keys the moment warning is unusable
// passes all seven other properties. Verified against exactly that mutant
// — it answers 35/25 for {warning: 150, critical: 30} where the real
// resolver answers 35/30, and the suite stayed green at 125/0 until this
// row existed. (Found by Codex review of this round.)
//
// critical is generated strictly below the DEFAULT warning (35), so the
// resolved pair {35, critical} stays ordered and the honoured half is
// observable rather than swallowed by the pair check.
fc.assert(
fc.property(
fc.double({ min: 0, max: 34.999, noNaN: true }),
fc.oneof(fc.string(), fc.boolean(), fc.constant(null), fc.constant(undefined), fc.constant(NaN), fc.constant(Infinity), fc.double({ min: 100.001, max: 1e6, noNaN: true }), fc.double({ min: -1e6, max: -0.001, noNaN: true })),
(critical, junkWarning) => {
const r = resolveThresholds({
context_warning_threshold: junkWarning,
context_critical_threshold: critical,
});
return r.warning === WARNING_THRESHOLD && r.critical === critical;
},
),
);
});
test('the two in-range endpoints with no legal partner always revert', () => {
// `warning: 0` and `critical: 100` are inside the 0-100 domain and pass the
// per-key check, but `critical < warning` can never hold for either: nothing
// is below 0 and nothing is above 100. So each is discarded for EVERY value
// of the other key. config-set refuses them at write time (tests/config.test.cjs);
// this row pins the READ side, which must stay total and simply fall back.
//
// The 0.001 / 99.999 controls are what make it a claim about the endpoints
// rather than about small and large numbers generally.
const D = { warning: WARNING_THRESHOLD, critical: CRITICAL_THRESHOLD };
for (const partner of [undefined, 0, 50, 100]) {
assert.deepStrictEqual(
resolveThresholds({ context_warning_threshold: 0, context_critical_threshold: partner }), D,
`warning 0 must revert whatever critical is (tried ${partner})`);
assert.deepStrictEqual(
resolveThresholds({ context_critical_threshold: 100, context_warning_threshold: partner }), D,
`critical 100 must revert whatever warning is (tried ${partner})`);
}
assert.deepStrictEqual(
resolveThresholds({ context_warning_threshold: 0.001, context_critical_threshold: 0 }),
{ warning: 0.001, critical: 0 },
'control: just inside the dead endpoint still resolves, so the row above is about 0 itself');
assert.deepStrictEqual(
resolveThresholds({ context_warning_threshold: 100, context_critical_threshold: 99.999 }),
{ warning: 100, critical: 99.999 },
'control: just inside the other dead endpoint still resolves');
});
test('a non-object hooks argument of any shape yields the defaults', () => {
fc.assert(
fc.property(
fc.oneof(fc.constant(null), fc.constant(undefined), fc.string(), fc.double(), fc.boolean()),
(hooks) => isDefaults(resolveThresholds(hooks)),
),
);
});
});