Commit Graph
11 Commits
Author SHA1 Message Date
haelyra ce5b17fb45 test(hooks): keep context-gate cwd fixtures inside private sandboxes
Replace the literal /tmp/... cwd values in the context-gate suites with
paths under each test's realpath'd mkdtemp sandbox. CodeQL treats /tmp
string literals as shared temp sources and traced them through the hook
input into readFileTail. The hook only hashes cwd, so assertions are
unchanged.
2026-11-17 00:46:54 -05:00
haelyra 8a5cbb7e14 test(hooks): resolve context-gate fixture roots with realpath
Wrap #3106's mkdtemp fixture roots in realpathSync, matching the existing
atomic-write tests, so CodeQL no longer traces shared-temp paths from
these fixtures into read handles. Assertions are unchanged.
2026-11-17 00:45:14 -05:00
haelyra e413166c50 test(hooks): make context-gate tests portable to Windows (#3106)
The hook proposes a correct absolute checkpoint path on Windows, but the
tests located that line with /^\s+[/\\].*\.md$/, which only matches paths
starting with a slash. Windows paths start with a drive letter (C:\...),
so every case that read the proposed path saw undefined and failed.

- Extract the proposed checkpoint as the indented line that ends in .md
  and satisfies path.isAbsolute, so POSIX and Windows paths both match.
- Include the hook output in the "both fires should propose" assertion
  so a genuine decline is diagnosable from CI logs.
- Write transcript fixtures into private fs.mkdtempSync directories
  instead of predictable names directly under os.tmpdir() (CodeQL
  js/insecure-temporary-file) in context-gate, context-gate-state and
  ecc-context-monitor tests.
2026-11-16 22:07:01 -05:00
haelyra ae9ce433cd fix(hooks): harden context-gate checkpoint ownership (#3106)
Maintainer carry-forward of the four edge cases reproduced on 5a565cf:

- Same-named project roots: bind the proposal to a SHA-256 owner key of
  the full real project root plus the raw session ID, and put that key in
  both the file name and the ownership marker.
- Sanitized session-ID aliasing: hash the raw session ID before filename
  sanitization so IDs such as owner/a and owner?a no longer share a path.
- Linked or special destinations: probe with lstat, open read-only with
  O_NOFOLLOW and O_NONBLOCK, require a regular single-link file whose
  complete first-line marker matches, and decline linked handoff
  directories. A FIFO at the destination previously hung the prompt.
- Literal resume arguments: emit the resume prompt as one single-quoted
  POSIX (or PowerShell on Windows) argument.

Also merge the UserPromptSubmit registration into the existing event
array. The main merge left a duplicate UserPromptSubmit key in
hooks.json and hooks.metadata.json, and JSON.parse kept only the last,
which silently dropped prompt:hookify-runtime.

Adds tests/hooks/context-gate-ownership.test.js, which fires the hook
through the registered run-with-flags.js entry: 12/12 pass here, 1/12
pass on 5a565cf.
2026-11-16 21:11:56 -05:00
CarbonPyramid 5a565cf1d1 fix(hooks): bind context-gate checkpoint to real identity; frame handoff as a proposal
Round-6 ownership revision for the enforce-mode checkpoint (PR #3106,
@haelyra requested-changes on fffda19).

1. Require real handoff identity, preserve an occupied destination.
   - resolveCheckpointPath -> resolveCheckpointIdentity: drops the literal
     'project'/'session' fallbacks. A session id (input.session_id or a
     session env override) AND a project are now required; when neither
     resolves it returns null and the hook declines enforcement, so two
     same-named projects with no session id can no longer collide on one
     shared <sessions>/resume/project-session.md path.
   - New pickOwnedDestination + readOwnerSession: before targeting an
     existing destination the gate checks an ecc-handoff ownership marker.
     A file owned by this session is reused (idempotent); an unmarked,
     unreadable, or foreign-owned file is never overwritten — the gate
     disambiguates to a sibling (-2, -3, ...) or declines.
   - Missing-identity fire now emits a degraded advisory (buildDeclineText):
     it names no checkpoint path and orders no write, but still warns and
     tells the operator to set ECC_SESSION_ID — not a silent drop.

2. Report a proposed handoff until a write is verified.
   - buildOrderText no longer calls the resume command a completed "receipt".
     The checkpoint is a PROPOSAL; the resume command is gated on an explicit
     "verify the file exists and is non-empty" step. The model is instructed
     to stamp the exact ownership marker (session+project) and record the
     source revision (git rev-parse HEAD), binding the handoff to a revision.

Tests: existing buildOrderText case extended for proposal framing; three new
disposable, sandboxed (ECC_AGENT_DATA_HOME) ownership cases — missing-identity
same-named projects with a pre-existing destination that survives byte-for-byte,
foreign-owned destination disambiguated, same-owner destination reused. Full
suite 4765/4765 serial.
2026-11-05 22:51:16 -06:00
CarbonPyramid fffda19ce6 fix(hooks): make context-gate enforcement opt-in; route checkpoint through handoff dir
Round 5 review (haelyra): enforcement must be opt-in and the enabled
checkpoint must preserve ownership.

- Add ECC_CONTEXT_GATE_MODE (advisory default | enforce | off) in
  context-gate-state.js; isGateEnabled is true only in enforce mode, so a
  default install is inert and adds no advisory suppression (suggest-compact
  and ecc-context-monitor keep their normal behavior). A mistyped mode falls
  back to advisory with a one-time stderr note.
- context-gate.js returns '' before any transcript work unless enforce is
  set (no per-prompt checkpoint cost by default).
- In enforce mode the order now targets a collision-safe, identity-bearing
  path under the session-handoff dir
  (getSessionsDir()/resume/<project>-<sessionId>.md) instead of a fixed
  project-root RESUME.md: never collides with an operator file, repeated
  fires in one session are idempotent, the resume command is the receipt,
  and the order states that observed usage is not itself permission to stop.
- Tests: default-inert, mode resolution, handoff-path/preserve/idempotent,
  explicit disable; advisory-hook suppression tests set enforce and add a
  default-does-not-suppress case. Bounded reader + threshold tests retained.
- Docs: hooks/README.md, README.md, hooks.metadata.json, header comment.

Full suite 4762/4762 (serial). validate-hooks: 25 matchers.
2026-11-05 12:30:26 -06:00
CarbonPyramid d7030abab7 fix(hooks): harden context-gate threshold resolution boundaries
Two boundary cases in threshold resolution raised in review:

- Clamp emergencyPct to at least gatePct+1 in run(). A gate threshold set
  at/above the emergency default (e.g. ECC_CONTEXT_GATE_PCT=98 vs the 96
  default) previously made every fire count as EMERGENCY, permanently
  skipping step 1 (bring in-flight work to a clean stopping point). The
  clamp preserves the two-tier order.

- resolvePct emits a one-time stderr note when an env override is present
  but rejected (e.g. "50.5", "abc", "150"). The return stays the
  deterministic fallback, so the contract is unchanged, but a typo'd
  override no longer silently re-arms the default gate unnoticed.

Regression tests added for both. Full suite 4751/4751.
2026-10-06 10:46:46 -05:00
CarbonPyramid bdc8ac3bff fix(hooks): confirm gate activity before advisory suppression; thread injected env through window resolution
Round-2 review fixes:

- ecc-context-monitor no longer suppresses its context warnings on
  configuration + bridge percentage alone. Deference now requires
  CONFIRMED gate activity via the shared gateOwnsTranscript (moved from
  suggest-compact into lib/context-gate-state.js): transcript readable,
  usage resolved, occupancy at/above threshold. When the gate cannot
  evaluate usage (missing/unreadable transcript) it cannot fire, so the
  monitor's CONTEXT CRITICAL warning survives as the fallback — an
  8%-remaining session is never left with no guidance. The transcript
  read only happens when a context warning is possible (<=35% remaining).
  run()-level tests encode both the fallback and suppression paths.

- resolveContextWindow accepts an env argument; context-gate and
  suggest-compact pass their (possibly injected) env through, so an
  options.env ECC_CONTEXT_WINDOW_TOKENS override now reaches window
  resolution instead of silently falling back to process.env. Regression
  tests cover the injected form and the registered-path process.env form.

- hooks/README.md: 'At or above the gate threshold' (deferral is
  inclusive at 90%), plus the fallback behavior.

- New tests/lib/context-gate-state.test.js covering threshold resolution,
  enabled/active checks, and gateOwnsTranscript semantics.

Full suite: 4748/4748.
2026-09-13 19:10:48 -05:00
CarbonPyramid 03b1a80838 feat(hooks): context-gate conflict management + inferred-window softening
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.
2026-09-13 16:13:27 -05:00
CarbonPyramid 6813b4a9ea fix(hooks): context-gate review fixes (env plumbing, strict pct parsing, test summary tokens)
- run() second arg is runner metadata from run-with-flags.js, not the
  environment; read controls from options.env || process.env so
  ECC_CONTEXT_GATE_PCT / ECC_CONTEXT_GATE_EMERGENCY_PCT work in the
  registered hook path (they were silently ignored before). Regression
  test exercises the metadata-second-arg shape against process.env.
- resolvePct: require a whole decimal integer before conversion;
  parseInt accepted '90abc' (-> 90) and '0x1' (-> 0, silently disabling
  the gate) instead of falling back.
- Test summary now emits the 'Passed: N' / 'Failed: N' tokens that
  tests/run-all.js parses; the lowercase format contributed zero to
  repo totals.
2026-09-13 15:42:19 -05:00
CarbonPyramid e9809d75ac feat(hooks): add context-gate checkpoint-and-restart hook
Deterministic UserPromptSubmit gate: at >=90% context occupancy, inject a
mandatory checkpoint protocol (clean stopping point, structured RESUME.md,
one-line resume command, close session) instead of per-turn advisory
stop suggestions. Escalates to immediate checkpoint at >=96%. Silent below
threshold; fails open silent. Reuses transcript-context.js for measurement.
2026-09-12 23:52:14 -05:00