Skip to main content
Browse the documentation

observedrev 34c5c29docs/contracts/hooks.md

Contract: hooks

Label: specified (rules), implemented (records, pack, and dispatch in src/hooks.mjs).

Shape

hook { id, event, summary, check }
id      kebab-case string, unique within a pack
event   one of: run-start | before-tool | after-tool | run-end | skill-promoted | skill-deprecated
summary non-empty; what the check guarantees
check   a function: (payload) → { ok, message? } — deterministic code, never prose

The event set

The four events are the minimal neutral points a run model can always provide:

Event Fires Payload carries
run-start a run begins the objective
before-tool before a tool call dispatches the face ID and arguments
after-tool after a tool call returns the face ID and result
run-end a run completes the outcomes

A harness with richer event points may extend the set in its own registers; this contract holds the four every run model can honor.

Two lifecycle events sit beside the run events. They fire when a caller hands runHooks the notice a skill lifecycle move left (lifecycleNotice in src/skills.mjs, see the skills contract):

Event Fires Payload carries
skill-promoted a skill moved from review to promoted { name, from, to, approver }
skill-deprecated a skill moved to deprecated { name, from, to, reason }

Nothing fires them on its own. transitionSkill is pure and calls neither lifecycleNotice nor runHooks; the caller that made the move builds the notice and dispatches it. Dispatch is the same as for the run events: pack order, and a check that throws is a failed check.

Hook rules

  1. The check is a function. A hook record without a function is not a hook. This is the shape rule that keeps hooks deterministic by construction: checks are code the author wrote, not prose a model might interpret, and never a model call.
  2. Fail closed. A check that throws is a failed check, recorded with its error, and the failure stands. Dispatch never converts an error into a pass and never skips a hook because it threw.
  3. Hooks never widen scope. A hook observes a payload and returns a verdict on it. A hook cannot admit effects, grant gates, modify memory, or release the run to do things it was not already admitted to do. A check that “fails” a run toward a stop is enforcing; no check can fail toward permission.
  4. Determinism is the contract. Same payload, same result, every time. A check that reads a clock, a random source, or a network is not deterministic and does not belong in a hook.
  5. Summaries state the guarantee. “Tool arguments are well-formed before dispatch” is a summary; “makes things better” is not.

The pack

hookPack(hooks)            → { hook(id), hooksFor(event), ids(), size }
runHooks(pack, event, payload) → results[]  — { id, passed, detail } in pack order
  • Every entry is checked. The pack runs each entry through hook. A frozen record whose check is not a function is not a hook, and the pack refuses it. The packed record references the same check function it was given.
  • Unique IDs. A duplicate hook ID is a pack error.
  • Pack order dispatch. Hooks run in pack order for the event; results are reported in the same order, each marked passed: true|false.
  • Frozen records. A hook freezes on creation; its behavior is its check’s code, referenced, not copied.

What a hook never is

  • Not a specialist. A check that needs judgment belongs to the harness’s reviewers; a hook that wants a model is mislabeled and rejected at the boundary of this contract’s doctrine even though its shape may validate.
  • Not a verdict. Review verdicts are the admission loop’s records. A hook passes or fails a check; it never records an approve and never requests revisions.

Changes

  • 2.1.0 — adds the skill-promoted and skill-deprecated events. The four run events stay valid, and every hook and pack valid before still validates and dispatches as before. A caller that compared EVENTS to the four-event set sees two more members.

  • 0.2.0 — hookPack checks every entry with the hook rules. Before 0.2.0 a frozen record with a known event entered the pack unchecked, so a record whose check was prose could be packed; dispatch then failed it closed, but the pack held something that was not a hook. The change refuses entries that never met this contract; every valid entry packs as before.