ECC/tests/hooks
JongHyeok Park 0071fa5c3c
refactor(hooks): consolidate PostToolUse hooks into sync/async dispatchers (#2494)
* refactor(hooks): consolidate PostToolUse hooks into sync/async dispatchers

Replace 10 individual PostToolUse entries in hooks.json with two
consolidated dispatcher entries (post:dispatcher:sync /
post:dispatcher:async). The dispatcher's internal registry preserves
every hook ID, matcher, and profile, so ECC_DISABLED_HOOKS and
ECC_HOOK_PROFILE gating behave exactly as before.

Performance (Edit event, actual hooks.json commands spawned in
parallel like the harness does, median of 7 runs):
- Blocking hook latency: 81ms -> 49ms (~40% faster; 7 blocking
  processes -> 1 sync dispatcher)
- Node processes per tool call: 10 -> 2 (7 blocking + 3 async
  -> 1 sync + 1 async)
- observe-runner now runs in-process (~370ms) inside the async
  dispatcher, which stays backgrounded (async: true, timeout 45s),
  so it adds no user-facing latency.

Also:
- dashboard-web lists dispatcher-managed child hooks so the hook
  inventory stays complete
- post-edit-console-warn refactored to export run() for in-process
  dispatch while keeping standalone stdin behavior
- dispatcher stdin reading is multi-byte safe (StringDecoder) and
  child hook exit codes propagate to the dispatcher exit code

* test(hooks): replace emoji literal with unicode escape for CI unicode safety check

* fix(hooks): adopt explicit cli() entrypoint and merge multi-hook stdout

Address Greptile review on #2494:

- Replace the non-standard 'require.main === undefined' guard with an
  explicit exported cli(). The hooks.json bootstraps now call
  require(s).cli(), so merely requiring the module (dashboard-web,
  test runners, Jest, worker threads) can never trigger dispatch,
  attach stdin listeners, or set process.exitCode.
- Replace last-writer-wins stdout with mergeHookStdout(): when several
  hooks emit additionalContext envelopes they merge into a single
  PostToolUse envelope; non-mergeable raw stdout keeps the last hook's
  output and emits a stderr warning naming the dropped hook IDs, so
  nothing is lost silently.

Also includes local formatter reformatting of the dispatcher and its
test file (no behavioral changes beyond the above).

* fix(hooks): keep post:bash:dispatcher phase reachable in minimal profile

The Greptile P1 premise was partially incorrect: sub-hooks without
explicit profiles default to standard,strict via parseProfiles()
(scripts/lib/hook-flags.js), so audit/cost logs never ran under the
minimal profile on main either — there is no user-visible regression.

However, main did spawn the bash dispatcher phase unconditionally and
let each sub-hook gate itself. Restore that semantic by opening the
outer registry gate to minimal,standard,strict so a future sub-hook
that opts into minimal is not silently blocked at the phase level.
Adds the previously missing minimal-profile async dry-run test.

* test(hooks): assert failing hook exit code propagates to real process status

Spawns the actual dispatcher subprocess with an injected failing hook
and asserts the OS-level exit status, stderr diagnostic, and suppressed
pass-through — closing the E2E gap CodeRabbit flagged on #2494.

* chore: retrigger CI (flaky windows powershell bootstrap test)
2026-07-19 15:47:10 -04:00
..
auto-tmux-dev.test.js feat: add C++ language support and hook tests (#539) 2026-03-16 14:31:49 -07:00
bash-hook-dispatcher.test.js fix(hooks): stop pre/post Bash dispatcher from echoing the input event (#2240) 2026-06-15 13:48:46 -04:00
block-no-verify.test.js fix: close block-no-verify bypass holes 2026-05-12 22:28:12 -04:00
check-hook-enabled.test.js feat: add C++ language support and hook tests (#539) 2026-03-16 14:31:49 -07:00
config-protection.test.js fix: integrate recent hook and docs PRs (#1905) 2026-05-14 21:37:28 -04:00
continuous-learning-observe-runner.test.js refactor(hooks): consolidate PostToolUse hooks into sync/async dispatchers (#2494) 2026-07-19 15:47:10 -04:00
continuous-learning-shebang-consistency.test.js chore(continuous-learning-v2): standardize shell shebangs to env bash (#2401) 2026-07-03 20:40:44 -07:00
cost-tracker.test.js fix(hooks): prefer fresh harness cost cache (#2054) 2026-05-25 14:08:11 -04:00
cursor-block-no-verify.test.js fix(.cursor/hooks): route block-no-verify through local hook to fix message-body false positives (#2107) (#2177) 2026-06-07 13:01:36 +08:00
design-quality-check.test.js feat: add web frontend rules and design quality hook 2026-04-02 17:33:17 -07:00
detect-project-worktree.test.js fix(learning): add project registry maintenance 2026-05-19 12:51:18 -04:00
doc-file-warning.test.js fix(hooks): guard doc-file-warning stdin listeners behind require.main (#2358) 2026-06-29 15:54:55 -07:00
ecc-context-monitor.test.js fix: add context monitor cost warning opt-out 2026-05-17 01:53:57 -04:00
ecc-metrics-bridge.test.js fix(hooks): stop false loop warnings and repeated identical context warnings (#2121) 2026-06-07 13:26:30 +08:00
ecc-statusline.test.js chore: update statusline ANSI color palette 2026-05-15 23:18:01 -04:00
evaluate-session.test.js fix: remove unused execFileSync import (no-unused-vars ESLint) 2026-02-18 07:15:53 +05:30
gateguard-fact-force.test.js feat(gateguard): add GATEGUARD_EXEMPT_GLOBS path exemptions (#2432) 2026-07-03 20:41:11 -07:00
governance-capture.test.js fix: resolve blocker PR validation regressions 2026-03-25 01:34:29 -04:00
hook-flags.test.js feat: add C++ language support and hook tests (#539) 2026-03-16 14:31:49 -07:00
hooks.test.js refactor(hooks): consolidate PostToolUse hooks into sync/async dispatchers (#2494) 2026-07-19 15:47:10 -04:00
insaits-security-monitor.test.js test: skip insaits monitor subprocesses without python 2026-05-18 00:47:05 -04:00
insaits-security-wrapper.test.js test: harden InsAIts wrapper fake Python shim 2026-05-12 01:13:01 -04:00
mcp-health-check.test.js fix(hooks): quote args when probing Windows .cmd MCP servers via shell (#2343) 2026-06-29 15:54:52 -07:00
migrate-homunculus-home-escape.test.js fix(clv2): escape $HOME before pgrep -f in migrate-homunculus.sh (#2339) 2026-06-25 16:47:32 -07:00
observe-entrypoint-allowlist.test.js fix(continuous-learning-v2): accept claude-vscode as valid entrypoint (#2134) 2026-06-07 13:01:24 +08:00
observe-signal-counter-race.test.js fix: resolve open-issue cluster (#2295, #2298, #2303–#2306, #2340) + createdTime fallback bug (#2408) 2026-07-03 21:10:45 -07:00
observe-signal-timeout.test.js fix: resolve open-issue cluster (#2295, #2298, #2303–#2306, #2340) + createdTime fallback bug (#2408) 2026-07-03 21:10:45 -07:00
observe-subdirectory-detection.test.js fix(learning): add project registry maintenance 2026-05-19 12:51:18 -04:00
observer-loop-archive.test.js fix(clv2): archive observations only after successful analysis in observer-loop (#2386) 2026-06-29 18:43:37 -07:00
observer-loop-mktemp.test.js fix: resolve open-issue cluster (#2295, #2298, #2303–#2306, #2340) + createdTime fallback bug (#2408) 2026-07-03 21:10:45 -07:00
observer-memory.test.js fix: resolve open-issue cluster (#2295, #2298, #2303–#2306, #2340) + createdTime fallback bug (#2408) 2026-07-03 21:10:45 -07:00
plan-canvas-sessions-hook.test.js feat: Plan Canvas, a browser review canvas for plans (#2467) 2026-07-08 17:12:48 -04:00
plugin-hook-bootstrap.test.js fix(windows): prefer PowerShell over bash to prevent zombie process accumulation (#2346) 2026-06-29 15:54:58 -07:00
post-bash-hooks.test.js feat: add C++ language support and hook tests (#539) 2026-03-16 14:31:49 -07:00
posttooluse-dispatcher.test.js refactor(hooks): consolidate PostToolUse hooks into sync/async dispatchers (#2494) 2026-07-19 15:47:10 -04:00
pre-bash-commit-quality.test.js fix: run pre-bash linters through windows wrappers 2026-04-29 18:59:10 -04:00
pre-bash-dev-server-block.test.js fix(dev-server-block): stop blocking dev-<suffix> scripts (#2179) 2026-06-07 13:25:39 +08:00
pre-bash-reminders.test.js fix: surface warn-only PreToolUse hooks (#2084) 2026-05-28 07:45:46 -04:00
quality-gate.test.js feat: add C++ language support and hook tests (#539) 2026-03-16 14:31:49 -07:00
run-with-flags-truncation.test.js fix: stability batch — hook stdin truncation, Codex exa TOML, Stop hook JSON, GateGuard repetition (#2227) 2026-06-11 00:31:33 -04:00
session-activity-tracker.test.js test: cover session activity tracker edge paths 2026-04-29 18:15:51 -04:00
session-end.test.js fix(session-end): preserve $-sequences in user messages when rewriting summary (#2180) 2026-06-07 13:25:36 +08:00
stop-format-typecheck.test.js perf(hooks): batch format+typecheck at Stop instead of per Edit (#746) 2026-03-31 14:12:12 -07:00
stop-hooks-stdout.test.js fix: stability batch — hook stdin truncation, Codex exa TOML, Stop hook JSON, GateGuard repetition (#2227) 2026-06-11 00:31:33 -04:00
suggest-compact.test.js fix: context-size /compact trigger, Codex marketplace plugin path, live README badges (#2237) 2026-06-11 16:21:53 -04:00
test_insaits_security_monitor.py test: cover hook bootstrap and InsAIts monitor 2026-04-29 17:45:22 -04:00