# Hook Router Consolidation

**Date:** 2026-07-19
**Register:** `surface`
**Status:** Applied and documented in pack

## Why This Pass Happened

The hook layer had reached the point where correctness alone was not enough. It
was catching real issues, but it was doing so with too many separate reads,
repeated scans, and unnecessary writes into `KAE/state/`. That mattered because
the system is now large enough that bad hook design becomes an infrastructure
problem, not just a style problem.

The question for this pass was simple:

> How do we keep roughly the same enforcement coverage while asking the
> filesystem to do less work?

## Findings

### 1. The main cost was structural fan-out

The worst overhead was not one catastrophic file. It was the pattern of many
small hooks each spawning separately on the same write lane. That meant one user
action could trigger multiple near-identical file reads and state checks.

The conclusion was that the biggest gain would not come from micro-optimising
one guard in isolation. It would come from collapsing repeated write-lane work
into one router path.

### 2. State churn was part of the real cost

Several gates were rewriting violation-state files even when nothing meaningful
had changed. That inflates disk activity and makes the KAE state folder noisier
than it needs to be.

The correct principle is:

- a gate should write state when truth changed
- a gate should not write state just because it ran

### 3. Some gates were paying disk cost before earning it

Several checks were still doing deeper reads on paths that should have been
rejected immediately. That is backwards. Cheap path filters should eliminate
obvious non-targets first, before any heavier read or parse work happens.

## What Was Done

### Low-risk efficiency pass

The first pass tightened obvious waste without changing system behaviour:

- redundant no-op state writes were suppressed
- early path rejection was strengthened so hooks skipped non-target files sooner
- directory noise was filtered earlier in brand and brief prerequisite checks

This reduced unnecessary work without weakening enforcement.

### Structural router pass

The larger change was the addition of a shared post-write router:

- `post-write-router.js` became the single write-lane dispatcher
- the write-lane hooks were refactored to run through one router process rather
  than many separate Node spawns
- hook registration in `.claude/settings.json` was updated accordingly

This was the meaningful systems change: one coordinated execution path instead
of a pile of parallel, mostly-overlapping hook invocations.

## What This Means Operationally

### Good news

- enforcement coverage stayed in place
- the system became less wasteful on normal write activity
- the filesystem does less repetitive work for the same control result

### Still true

This was not a full elimination of hook cost.

- `PreToolUse` still has fan-out and remains a future optimisation target
- some gates are inherently heavier because they audit real structure, not
  string fragments
- `tasks-data-gate.js` in particular is allowed to cost more because task
  integrity is genuinely structural

So the platform is safer now, but the doctrine remains:

> expensive checks must justify themselves

## Design Rule Going Forward

Future hooks should be judged on four questions before they are accepted:

1. Can the path be rejected earlier?
2. Can the read be shared with another gate?
3. Can the state write be skipped when nothing changed?
4. Does this belong as a separate hook at all, or should it sit behind a router
   or registry?

If a proposed hook fails those tests, it is probably not ready to ship.

## Relationship To The Rest Of The System

This pass matters beyond the hook folder.

- It supports GAIA's operating-cost discipline.
- It strengthens DEMETER's process integrity by making enforcement sustainable.
- It helps HEPHAESTUS because build-session rules now sit on a more stable
  control layer.
- It supports the wider registry-first direction: fewer ad-hoc execution paths,
  more shared lanes.

## Read Next

- `methodology-enforcement-pass-2026-07-19.md`
- `ghn-audit-and-rebuild-2026-07-19.md`
- `live-system-changes.html`

## Bottom Line

The lesson from this pass is simple:

> Hook quality is now a system-health concern.

Good enforcement is not just about catching mistakes. It is also about how much
infrastructure pressure the enforcement layer creates while it is doing its
job.
