Runtime hooks, proxies, and shims
Guard applies policy at the boundary between a harness and the tool or package action it requests. Hook facts become trusted inputs, policy results stay authoritative, and the result is rendered for the harness, answered by a bounded resident path, or sent to the daemon. Launchers and package shims keep managed activity on this path. Containment routes move an allowed action to a stronger execution boundary when policy requires it. See Guard decision and enforcement, approval gates and scope resolution, secret read risk controls, evaluation and assurance workflows, and contained execution and runtime boundaries.
Interception surfaces
Hook facts
Hook facts give each decision path the same view of an incoming event. The resident names the event, tool, command, and launch target from accepted payload shapes before policy and connector checks use them. It keeps command text as sent while using cleaned text for identity and presence checks. rust/…/hook_payload_facts_op.rs• L139-180 and rust/…/hook_payload_facts_op.rs• L198-245.
The facts path also answers small admission questions. It reports whether an event can pause and whether it is a permission request. It turns bounded time hints into a safe hook budget instead of letting a caller provide an unlimited deadline. rust/…/hook_payload_facts_op.rs• L41-80 and rust/…/hook_payload_facts_op.rs• L93-128.
References: rust/…/hook_payload_facts_op.rs• L41-80 rust/…/hook_payload_facts_op.rs• L139-180 rust/…/hook_payload_facts_op.rs• L198-245
Policy composition
Policy composition combines configured action, trusted binding data, caller hints, native floors, and daemon state into one result. The strongest applicable action remains in force. A payload hint cannot turn a review or block into an allow. A daemon failure tightens the result in strict mode and keeps the current action in permissive mode. rust/…/hook_decision_compose.rs• L61-141 and rust/…/hook_decision_compose.rs• L266-354.
The composition step requests only the facts it needs. This keeps costly checks lazy and leaves the native runtime as the authority that selects the observations. The result carries the reason for a relaxation or fallback so response and audit paths keep the same meaning. rust/…/hook_decision_compose.rs• L152-225.
References: rust/…/hook_decision_compose.rs• L61-141 rust/…/hook_decision_compose.rs• L152-225 rust/…/hook_decision_compose.rs• L266-354
Harness responses
Native rendering turns one Guard result into the response a harness expects. It covers direct hook answers, review pauses, recording-only responses, bridge output, approval text, artifact identity, and event status. It also answers availability questions before a full decision exists. rust/…/hook_render_op.rs• L97-170 and rust/…/hook_render_op.rs• L418-455.
The renderer reads event and verdict fields again when a response can change authority. This stops a forwarding caller from relabeling a block as an allow or treating a before-event as an after-event. Invalid, unreadable, or oversized input produces an error or a fail-closed result. rust/…/hook_render_op.rs• L335-398 and rust/…/hook_cli_emit.rs• L203-239.
Bridge-specific output keeps the daemon's action intact while matching each harness contract. A harness that reads a JSON document gets its expected exit status. A bounded bridge gets the rendered stdout, stderr, and status. rust/…/hook_cli_emit.rs• L70-153 and rust/…/hook_cli_emit.rs• L203-239.
References: rust/…/hook_render_op.rs• L97-170 rust/…/hook_render_op.rs• L335-398 rust/…/hook_cli_emit.rs• L70-153 rust/…/hook_cli_emit.rs• L203-239
Direct hook execution
Direct answer lane
The direct lane is a bounded fast path for requests whose result the daemon would not change. It accepts a supported and managed harness, a current resident, a settled home and workspace, and an acknowledged policy binding. It can answer common pre-tool, post-tool, prompt, lifecycle, and emergency-safe events without a daemon round trip. rust/…/hook_direct.rs• L205-247 and rust/…/hook_direct.rs• L337-407.
The lane remains an optimization of the daemon path, not a second policy authority. Unsupported events, incomplete or changed bindings, unsafe payloads, unmanaged harnesses, uncertain local rules, review work, and missing evidence capacity defer. A superseded resident also defers so an old runtime cannot continue to answer hooks after an update. rust/…/hook_direct.rs• L188-203, rust/…/hook_direct.rs• L357-407, and rust/…/hook_direct_defer.rs• L7-80.
The lane can reuse a saved exact-action decision or an approve-once result only when the current request matches the stored identity and integrity state. A saved block wins. A saved allow can answer only an eligible review. Package and Guard-control actions are not lifted by a broad local grant. rust/…/hook_direct_saved_decision.rs• L91-210 and rust/…/hook_direct_saved_decision.rs• L275-385.
References: rust/…/hook_direct.rs• L169-203 rust/…/hook_direct.rs• L205-247 rust/…/hook_direct.rs• L337-407 rust/…/hook_direct_defer.rs• L7-80 rust/…/hook_direct_saved_decision.rs• L91-210
Direct lifecycle paths
Lifecycle hooks cover events that the edge does not review. The resident returns the daemon's fixed harness response for those events without inventing a deny. Claude permission requests and permission prompts are different: they can use a signed notice only when the resident has the bound integrity key and can mark the notice seen in the same store transaction. rust/…/hook_direct_lifecycle.rs• L1-32 and rust/…/hook_direct_lifecycle.rs• L133-216.
Claude question settlement keeps the answer tied to the pending permission that the hook found. It settles the question, updates policy-integrity state, saves the result, and consumes the pending item as one guarded sequence. If any step cannot prove the same state, the hook defers to the daemon. rust/…/hook_direct_lifecycle.rs• L219-405 and rust/…/local_question.rs• L24-89.
References: rust/…/hook_direct_lifecycle.rs• L133-216 rust/…/hook_direct_lifecycle.rs• L219-405 rust/…/local_question.rs• L24-89
Direct evidence
Every direct answer leaves evidence for the daemon. The resident keeps hook records in a bounded memory queue. The daemon drains pages and acknowledges them by sequence and resident epoch. A page stays queued until its acknowledgement, and a queue that cannot hold the record makes the hook defer instead of answering without an audit record. rust/…/hook_direct_evidence.rs• L1-31 and rust/…/hook_direct_evidence.rs• L245-413.
The drain validates receipts, records work that the resident can complete, and returns the rest to the daemon. It also carries diagnostic counts for answers and defer reasons without retaining raw commands in those counts. The writer records the final evidence under the Guard home that owns it. rust/…/hook_direct_evidence_drain.rs• L36-73, rust/…/hook_direct_evidence_drain.rs• L189-226, and rust/…/hook_evidence_writer_op.rs• L322-399.
The ordinary evidence writer is one long-lived writer per Guard home. It queues activity, receipts, package discovery, and post-tool records, then drains them to the store. Store failures and policy mutations stay pending until the caller receives and handles their signal. rust/…/hook_evidence_writer_registry.rs• L38-50 and rust/…/hook_evidence_writer_op.rs• L186-199.
References: rust/…/hook_direct_evidence.rs• L1-31 rust/…/hook_direct_evidence.rs• L245-413 rust/…/hook_direct_evidence_drain.rs• L36-73 rust/…/hook_evidence_writer_op.rs• L322-399 rust/…/hook_evidence_writer_registry.rs• L120-170
Cursor routes
Cursor shell hooks have three high-level routes: ordinary review, a bounded probe, or a contained sink call. A probe asks for a policy binding only when it has a workspace. A sink route requires the bound sink command. If the edge does not prove a valid routed allow, the probe returns a plain allow and leaves the real shell call to its normal review path. rust/…/hook_direct_cursor_route.rs• L14-56 and rust/…/hook_direct_cursor_route.rs• L95-135.
This split prevents a route marker from granting execution authority. Only the expected Cursor payload shape can select a probe or sink. Windows, mismatched markers, receipt-only calls, and failed sink checks remain ordinary reviews. rust/…/hook_direct_cursor_route_ops_tests.rs• L113-193 and rust/…/hook_direct_cursor_route_ops_tests.rs• L204-280.
References: rust/…/hook_direct_cursor_route.rs• L14-56 rust/…/hook_direct_cursor_route.rs• L95-135 rust/…/hook_direct_cursor_route_ops_tests.rs• L113-193
Structured output mediation
Oh My Pi hooks can require a second Guard check on model-visible output after a tool runs. The resident uses the managed-policy view bound to the direct lane, prepares the content check, and forwards only when a second read still names the same binding. A missing or changed view withholds the content instead of forwarding it. rust/…/hook_direct_pi.rs• L62-99 and rust/…/hook_direct_pi.rs• L192-246.
The same rule applies during an edge outage. Only a view that explicitly says no mediation is required stays open. All other covered Pi and OMP output is withheld until the managed authority is available. rust/…/hook_direct_pi.rs• L101-123.
References: rust/…/hook_direct_pi.rs• L62-99 rust/…/hook_direct_pi.rs• L101-123 rust/…/hook_direct_pi.rs• L192-246
Hook authority state
Signed lifecycle state
Hook authority state carries short-lived facts between related hook events. It covers Claude permission notices and pending questions as well as Cursor pending shell state and native allowances. Each item is tied to its purpose, owner, session or conversation, artifact, command, and integrity key. Invalid, foreign, or expired state is removed and never returned to a hook. rust/…/hook_authority_state_op.rs• L1-18 and rust/…/hook_authority_state_op.rs• L43-105.
The state operation derives names from the business identity when the request supplies one. A request may instead use a key that the resident issued earlier. Reads and updates run under one immediate store transaction, so a notice cannot be answered unseen or marked with a foreign key. rust/…/hook_authority_state_op.rs• L199-248, rust/…/hook_authority_state_op.rs• L286-439, and rust/…/transaction.rs• L3-37.
Claude question settlement checks the stored pending permission against the hook's projected question. An unseen or unanswered question remains pending. A matching answer creates a save plan only when the integrity generation can move from its current value to the next value and then finalize. rust/…/dispatch.rs• L95-117 and rust/…/local_question.rs• L24-89.
References: rust/…/hook_authority_state_op.rs• L1-18 rust/…/hook_authority_state_op.rs• L199-248 rust/…/hook_authority_state_op.rs• L286-439 rust/…/dispatch.rs• L95-117 rust/…/transaction.rs• L3-37
Review request binding
A paused native review becomes an approval request only after Guard binds it to the exact hook action. The binding carries the harness, action identity, policy scope, reason, approval command, and response envelope. Exact-action tokens, package approval keys, origin receipts, and native policy facts remain attached to the request that created them. rust/…/hook_adapter_review_request.rs• L40-165.
The review binding operation projects a policy token only from a verified receipt and a committed marker. A mid-update marker is rejected. An unbound review stays unbound. This prevents a hook from using policy state while that state is changing. rust/…/hook_review_binding_op.rs• L34-62 and rust/…/hook_review_binding_op.rs• L65-112.
References: rust/…/hook_adapter_review_request.rs• L57-165 rust/…/hook_review_binding_op.rs• L34-62 rust/…/hook_review_binding_op.rs• L65-112
Launch and containment paths
Managed hook launchers
Managed hook launchers give installed harnesses a stable entry point into the Guard runtime. Management recognition must identify both the current harness and the installed runtime, so an update or repair can replace stale Guard entries without removing a user hook or another Guard installation. This applies to module bridges, scripts, native runtime prefixes, and Unix or Windows launcher forms. rust/…/hook_launch_scan_tests.rs• L55-164.
Runtime scans distinguish a stale pinned runtime from an old path mentioned only in notes. They also inspect manifest-listed files within bounded limits. A malformed or incomplete install is reported as needing repair rather than treated as a healthy managed path. rust/…/hook_launch_scan_tests.rs• L228-353 and rust/…/hook_launch_scan_tests.rs• L366-448.
References: rust/…/hook_launch_scan_tests.rs• L55-164 rust/…/hook_launch_scan_tests.rs• L228-353 rust/…/hook_launch_scan_tests.rs• L366-448
Package manager shims
Package manager shims intercept supported package commands before they reach the real manager. They provide explicit install, repair, activation, removal, status, probing, and shell-path behavior. Probe and status modes describe the managed shim. They do not grant package execution authority. rust/…/mod.rs• L1-12, rust/…/mod.rs• L35-69, and rust/…/mod.rs• L79-120.
Package reviews remain tied to the native plan and evaluator result. A registry block wins over a saved allow. An evaluator allow or guide applies only to a proven install shape. A missing registry result returns a retry response rather than allowing the package action. rust/…/hook_package_review_settlement.rs• L21-64 and rust/…/hook_package_review_settlement.rs• L78-125.
References: rust/…/mod.rs• L1-12 rust/…/mod.rs• L35-69 rust/…/hook_package_review_settlement.rs• L21-64
Contained execution routes
Contained execution changes where an allowed action runs when direct execution would cross a required boundary. The direct lane accepts a sink only from the current binding, then replaces the harness response with the routed result. A missing or invalid sink defers instead of widening the action. rust/…/hook_direct.rs• L430-487 and rust/…/hook_direct.rs• L511-567.
This route changes execution location, not the policy decision. See contained execution and runtime boundaries for the boundary model.
References: rust/…/hook_direct.rs• L430-487 rust/…/hook_direct.rs• L511-567 rust/…/hook_direct_cursor_route.rs• L44-51
Policy transition controls
Admission recovery
Policy admission recovery turns bounded native failures into a safe user-facing posture. Temporary failures ask for a retry. A persistent installed-file mismatch asks for repair. Repeated resident failures ask for one more retry and then repair. A policy-source update has its own retry window and does not run the action during that window. rust/…/hook_policy_admission_op.rs• L41-60.
The recovery reason is produced from bounded, privacy-safe error state. The caller can act on the reason without sending the failed hook through another resident request. rust/…/hook_policy_admission_op.rs• L63-115.
References: rust/…/hook_policy_admission_op.rs• L41-60 rust/…/hook_policy_admission_op.rs• L63-115
Policy fence windows
Policy fence windows group hooks that wait while the policy source changes. The resident tracks active waiters and counts admitted and exhausted calls. It emits a window event when the last waiter leaves, while direct waits and late leaves remain in the same accounting path. rust/…/hook_policy_fence_window_op.rs• L68-80 and rust/…/hook_policy_fence_window_op.rs• L309-387.
The window protects the transition without allowing a wait to last forever. Poll plans reserve time for delivery and the next attempt. Completed events return to the pending queue when a store write fails, and overflow is reported rather than hidden. Review fences separately protect eligible enforcing pre-tool and prompt reviews for a short lease. rust/…/hook_policy_fence_window_op.rs• L174-184, rust/…/hook_policy_fence_window_op.rs• L243-307, and rust/…/hook_policy_fence_window_persistence.rs• L18-47.
References: rust/…/hook_policy_fence_window_op.rs• L174-184 rust/…/hook_policy_fence_window_op.rs• L243-307 rust/…/hook_policy_fence_window_op.rs• L309-433 rust/…/hook_policy_fence_window_persistence.rs• L18-47
Change checklist
When these files or sections are changed, remember to consider these:
- Keep payload fact extraction aligned with every caller that composes event, command, tool, launch-target, and time-budget policy.
- Keep untrusted payload hints below configured actions, native edge floors, verified-benign relaxations, and strict daemon failure handling.
- Keep direct-lane eligibility tied to the current runtime, managed harness, home, workspace, binding, policy view, containment sink, and native evidence writer.
- Keep saved decisions and approve-once reuse tied to the exact action, workspace identity, integrity state, receipt, and store gate.
- Keep direct evidence acknowledgements bound to the resident epoch. Do not let a restart acknowledge records from an older queue.
- Keep lifecycle state reads, question settlement, integrity-generation changes, and pending-row consumption on one authority path.
- Keep review tokens and package approval keys tied to the committed binding, exact action, and authenticated receipt.
- Keep launcher scans specific to managed command shapes and runtime pins. Do not treat user notes or another installation as a stale Guard entry.
- Keep policy-fence admission, recovery advice, review leases, persistence, and emitted window events consistent when the policy source changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A direct request has an incomplete or changed binding | The resident could answer with policy or workspace facts that are no longer current | Defer until the home, workspace, snapshot, ACK, and writer contract match |
| A saved decision does not match the receipt, exact action, workspace, or integrity state | A different action could inherit an allow or block | Verify the full action identity and use the daemon when any lookup is uncertain |
| A direct evidence queue is full or a page is not acknowledged | An allowed hook could lose its audit record | Defer on capacity failure and retain pages by sequence and resident epoch |
| A lifecycle row is foreign, expired, unkeyed, or mid-transaction | One session or conversation could inherit another action's authority | Verify purpose, owner, key, age, and committed state in one transaction |
| A Cursor probe or contained sink has an invalid shape | A route marker could bypass normal review or run outside containment | Accept only the expected route and require the bound sink and edge proof |
| Structured output mediation has no stable managed-policy view | Model-visible output could pass without its required check | Withhold the output or defer to the daemon |
| A package evaluator is not yet proven or the registry blocks the package | A package action could run before its review is complete | Keep the review or return the fixed retry or block response |
| A hook launcher is stale or a config scan is unverified | Repair could remove a user hook or leave Guard inactive | Match the full managed form and report repair or partial removal |
| A policy fence event cannot be stored | Transition telemetry could be lost and later calls could misread the window | Restore the batch, retain overflow counts, and retry the write |
Key files
| File | Why to read it |
|---|---|
| rust/…/hook_payload_facts_op.rs | Defines the shared event, command, tool, launch-target, and admission facts. |
| rust/…/hook_decision_compose.rs | Shows how policy inputs become one authoritative action. |
| rust/…/hook_render_op.rs | Defines native rendering for harness responses, review reuse, bridges, and fail-closed paths. |
| rust/…/hook_cli_emit.rs | Shows how a daemon answer becomes harness output and an exit status. |
| rust/…/hook_direct.rs | Defines when the resident answers a hook and when it defers to the daemon. |
| rust/…/hook_direct_saved_decision.rs | Defines integrity-bound exact-action reuse and approve-once handling. |
| rust/…/hook_direct_evidence.rs | Defines the bounded queue and epoch acknowledgement for direct-hook evidence. |
| rust/…/hook_direct_evidence_drain.rs | Defines page draining, receipt validation, and handoff to the daemon writer. |
| rust/…/hook_evidence_writer_op.rs | Defines the per-home evidence writer and its recovery signals. |
| rust/…/hook_authority_state_op.rs | Defines signed lifecycle state for Claude and Cursor hook paths. |
| rust/…/local_question.rs | Defines guarded Claude question settlement and integrity finalization. |
| rust/…/hook_review_binding_op.rs | Defines the committed policy binding used by review paths. |
| rust/…/hook_direct_cursor_route.rs | Defines Cursor probe, sink, and ordinary review routing. |
| rust/…/hook_direct_pi.rs | Defines Oh My Pi structured-output mediation. |
| rust/…/hook_package_review_settlement.rs | Defines package review outcomes after plan and evaluator results. |
| rust/…/hook_policy_fence_window_op.rs | Defines policy transition wait accounting and review fences. |
| rust/…/hook_removal_sweep.rs | Defines safe discovery and removal of Guard-owned hook handlers. |
| rust/…/mod.rs | Defines the package-manager interception boundary. |
| docs/guard-decision-and-enforcement.md | Parent page for decisions applied at runtime boundaries. |
| docs/approval-gates-and-scope-resolution.md | Explains approval binding for paused hook actions. |
| docs/contained-execution-and-runtime-boundaries.md | Explains the stronger execution boundary used by containment routes. |