mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-24 00:35:31 +00:00
Conflict management (the gate previously had none): - New scripts/lib/context-gate-state.js — single source of truth for gate thresholds plus isGateEnabled/isGateActive; context-gate.js now imports from it (public exports unchanged). - suggest-compact defers to the gate: both the context-size and tool-count /compact suggestions stay silent at/above the gate threshold. The gate orders checkpoint-and-restart there; suggesting the lossy /compact path in the same band was a contradictory instruction. The transcript-reading gate check on the count path only runs when a count message would fire. - ecc-context-monitor defers to the gate: the context warnings (including the critical 'ask the user / do NOT write handoff files' text, which directly contradicted the gate's checkpoint order) are suppressed inside the gate band; cost/scope/loop warnings unaffected. - Header + hooks/README.md now state the real auto-compact interaction: with auto-compact ON, compaction (~83.5% used per ecc-statusline's 16.5% buffer) precedes the 90% gate, so autoCompactEnabled:false (or a raised window) is required for the gate to be reachable — the previous 'safe with auto-compact left on' claim was wrong. Inferred-window softening (review feedback): - When the window size is inferred (unrecognized model), the gate fires as a strong recommendation instead of an order and flags the inference plus the ECC_CONTEXT_WINDOW_TOKENS override, so a possibly-wrong 200k denominator never forces a restart on a larger-window model. Known models keep the mandatory wording. Tests: inferred-softening case; gate-deference pairs for both advisory hooks; ecc-context-monitor test summary switched to the parseable 'Passed:/Failed:' tokens run-all.js requires.
108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
/**
|
|
* Context-gate state — shared threshold resolution and activity check.
|
|
*
|
|
* Single source of truth for the context-gate thresholds so the advisory
|
|
* hooks (suggest-compact, ecc-context-monitor) can defer to the gate
|
|
* instead of emitting contradictory guidance in the same context band.
|
|
* The gate orders a checkpoint-and-restart; the advisory hooks suggest
|
|
* /compact or "ask the user" — both must go silent once the gate owns
|
|
* the turn.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const { isHookEnabled } = require('./hook-flags');
|
|
|
|
const DEFAULT_GATE_PCT = 90;
|
|
const DEFAULT_EMERGENCY_PCT = 96;
|
|
const MIN_PCT = 1;
|
|
const MAX_PCT = 100;
|
|
|
|
/** Hook id and profiles exactly as registered in hooks/hooks.json. */
|
|
const GATE_HOOK_ID = 'user-prompt:context-gate';
|
|
const GATE_HOOK_PROFILES = 'standard,strict';
|
|
|
|
/**
|
|
* Resolve a percent setting from the environment.
|
|
* `0` disables the gate entirely; invalid values fall back to the default.
|
|
* A whole decimal integer is required — parseInt-style partial parses
|
|
* ('90abc' -> 90, '0x1' -> 0) would silently shift or disable the gate.
|
|
* @param {object} env
|
|
* @param {string} name
|
|
* @param {number} fallback
|
|
* @returns {number}
|
|
*/
|
|
function resolvePct(env, name, fallback) {
|
|
const raw = env && env[name];
|
|
if (raw !== undefined && raw !== null && raw !== '') {
|
|
if (!/^(?:0|[1-9]\d*)$/.test(String(raw).trim())) return fallback;
|
|
const parsed = Number(String(raw).trim());
|
|
if (parsed === 0) return 0;
|
|
if (Number.isInteger(parsed) && parsed >= MIN_PCT && parsed <= MAX_PCT) {
|
|
return parsed;
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
/**
|
|
* Gate threshold percent for the given environment (0 = gate disabled).
|
|
* @param {object} [env]
|
|
* @returns {number}
|
|
*/
|
|
function resolveGatePct(env = process.env) {
|
|
return resolvePct(env, 'ECC_CONTEXT_GATE_PCT', DEFAULT_GATE_PCT);
|
|
}
|
|
|
|
/**
|
|
* Emergency escalation percent for the given environment.
|
|
* @param {object} [env]
|
|
* @returns {number}
|
|
*/
|
|
function resolveEmergencyPct(env = process.env) {
|
|
return resolvePct(env, 'ECC_CONTEXT_GATE_EMERGENCY_PCT', DEFAULT_EMERGENCY_PCT);
|
|
}
|
|
|
|
/**
|
|
* True when the context-gate hook is installed-and-enabled for this
|
|
* environment (profile allows it, not in ECC_DISABLED_HOOKS, threshold
|
|
* not set to 0). Says nothing about current occupancy.
|
|
* @param {object} [env]
|
|
* @returns {boolean}
|
|
*/
|
|
function isGateEnabled(env = process.env) {
|
|
if (resolveGatePct(env) === 0) return false;
|
|
return isHookEnabled(GATE_HOOK_ID, { profiles: GATE_HOOK_PROFILES, env });
|
|
}
|
|
|
|
/**
|
|
* True when the gate is enabled AND the observed occupancy has reached
|
|
* its threshold — i.e. the gate is issuing (or about to issue) the
|
|
* checkpoint order and advisory hooks must stay silent.
|
|
* @param {object} params
|
|
* @param {number} params.tokens - Observed context tokens.
|
|
* @param {number} params.windowTokens - Resolved window size.
|
|
* @param {object} [params.env]
|
|
* @returns {boolean}
|
|
*/
|
|
function isGateActive({ tokens, windowTokens, env = process.env }) {
|
|
if (!Number.isFinite(tokens) || !Number.isFinite(windowTokens) || windowTokens <= 0) {
|
|
return false;
|
|
}
|
|
if (!isGateEnabled(env)) return false;
|
|
const pct = Math.floor((tokens / windowTokens) * 100);
|
|
return pct >= resolveGatePct(env);
|
|
}
|
|
|
|
module.exports = {
|
|
resolvePct,
|
|
resolveGatePct,
|
|
resolveEmergencyPct,
|
|
isGateEnabled,
|
|
isGateActive,
|
|
DEFAULT_GATE_PCT,
|
|
DEFAULT_EMERGENCY_PCT,
|
|
GATE_HOOK_ID,
|
|
GATE_HOOK_PROFILES
|
|
};
|