Repository navigation
feat(code): add Hooks v2 execution engine #4880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Johannes du Plessis (johannes117)
merged 7 commits into
main
from
johannes117/code/hooks-engine
Jul 21, 2026
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1db711d
feat(code): add typed hooks data models
johannes117 8400dd2
feat(code): add Hooks v2 execution engine
johannes117 1bd7f11
fix(code): isolate Hooks v2 imports and processes
johannes117 5372d53
chore(code): merge main into hooks-engine
johannes117 01f731e
cr
johannes117 73dafc6
cr
johannes117 5528f76
fix(code): preserve hook exit status after closed stdin
johannes117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Hook contracts, execution engine, and compatibility dispatch.""" | ||
|
|
||
| from deepagents_code.hooks.engine import HookEngine | ||
| from deepagents_code.hooks.legacy import ( | ||
| HOOK_SUBPROCESS_TIMEOUT, | ||
| HOOK_TOOL_OUTPUT_LIMIT, | ||
| _background_tasks, | ||
| _dispatch_hook_sync as _dispatch_hook_sync, | ||
| _load_hooks as _load_hooks, | ||
| dispatch_hook, | ||
| dispatch_hook_fire_and_forget, | ||
| drain_pending_hooks, | ||
| has_pending_hooks, | ||
| subprocess as subprocess, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "HOOK_SUBPROCESS_TIMEOUT", | ||
| "HOOK_TOOL_OUTPUT_LIMIT", | ||
| "HookEngine", | ||
| "_background_tasks", | ||
| "dispatch_hook", | ||
| "dispatch_hook_fire_and_forget", | ||
| "drain_pending_hooks", | ||
| "has_pending_hooks", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """Standalone orchestration for the Hooks v2 execution engine.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from deepagents_code.hooks.models.domain import HookDiagnostic | ||
| from deepagents_code.hooks.projection import serialize_hook_input | ||
| from deepagents_code.hooks.reducer import reduce_hook_results | ||
| from deepagents_code.hooks.runner import ( | ||
| DEFAULT_HOOK_TIMEOUT, | ||
| MAX_HOOK_OUTPUT_BYTES, | ||
| HandlerResult, | ||
| run_command_handler, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from deepagents_code.hooks.models.domain import HookDecision, HookInvocation | ||
| from deepagents_code.hooks.snapshot import HooksSnapshot | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class HookEngine: | ||
| """Execute Hooks v2 invocations against one immutable snapshot.""" | ||
|
|
||
| snapshot: HooksSnapshot | ||
| default_timeout: float = DEFAULT_HOOK_TIMEOUT | ||
| max_output_bytes: int = MAX_HOOK_OUTPUT_BYTES | ||
|
|
||
| async def run(self, invocation: HookInvocation) -> HookDecision: | ||
| """Execute matching handlers and return a normalized decision. | ||
|
|
||
| Args: | ||
| invocation: Native lifecycle invocation. | ||
|
|
||
| Returns: | ||
| The event-specific decision produced by ordered hook reduction. | ||
| """ | ||
| match = self.snapshot.match(invocation) | ||
| try: | ||
| payload = serialize_hook_input(invocation) | ||
| except (TypeError, ValueError) as exc: | ||
| diagnostic = HookDiagnostic( | ||
| code="projection_failed", | ||
| severity="warning", | ||
| message=f"Could not project hook invocation: {exc}", | ||
| ) | ||
| return reduce_hook_results( | ||
| invocation, | ||
| (), | ||
| diagnostics=(*match.diagnostics, diagnostic), | ||
| ) | ||
|
|
||
| results: list[HandlerResult] = [] | ||
| for handler in match.handlers: | ||
| result = await run_command_handler( | ||
|
johannes117 marked this conversation as resolved.
Resolved
|
||
| handler, | ||
| payload, | ||
| cwd=invocation.context.cwd, | ||
| default_timeout=self.default_timeout, | ||
| max_output_bytes=self.max_output_bytes, | ||
| ) | ||
| results.append(result) | ||
| if result.output is not None and not result.output.continue_: | ||
| break | ||
| return reduce_hook_results( | ||
| invocation, | ||
| results, | ||
| diagnostics=match.diagnostics, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Typed contracts for the hooks system.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Cached runtime validators for hook model boundaries.""" | ||
|
|
||
| from pydantic import TypeAdapter | ||
|
|
||
| from deepagents_code.hooks.models.config import HooksConfig | ||
| from deepagents_code.hooks.models.domain import ( | ||
| HookDecision, | ||
| HookDomainEvent, | ||
| HookInvocation, | ||
| ) | ||
| from deepagents_code.hooks.models.transport import ( | ||
| HookInvocationRequest, | ||
| HookInvocationResponse, | ||
| ) | ||
| from deepagents_code.hooks.models.wire import HookWireInput, HookWireOutput | ||
|
|
||
| HOOK_DOMAIN_EVENT_ADAPTER = TypeAdapter(HookDomainEvent) | ||
| HOOK_INVOCATION_ADAPTER = TypeAdapter(HookInvocation) | ||
| HOOK_DECISION_ADAPTER = TypeAdapter(HookDecision) | ||
| HOOK_WIRE_INPUT_ADAPTER = TypeAdapter(HookWireInput) | ||
| HOOK_WIRE_OUTPUT_ADAPTER = TypeAdapter(HookWireOutput) | ||
| HOOKS_CONFIG_ADAPTER = TypeAdapter(HooksConfig) | ||
| HOOK_INVOCATION_REQUEST_ADAPTER = TypeAdapter(HookInvocationRequest) | ||
| HOOK_INVOCATION_RESPONSE_ADAPTER = TypeAdapter(HookInvocationResponse) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Validated hook configuration models.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal, TypeAlias | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
|
|
||
| from deepagents_code.hooks.models.domain import ( # noqa: TC001 - Pydantic runtime annotation. | ||
| HookEvent, | ||
| ) | ||
|
|
||
|
|
||
| class _ConfigModel(BaseModel): | ||
| model_config = ConfigDict(extra="forbid") | ||
|
|
||
|
|
||
| class CommandHandlerSpec(_ConfigModel): | ||
| """Configuration for a synchronous command hook.""" | ||
|
|
||
| type: Literal["command"] | ||
| command: str | ||
| timeout: float | None = None | ||
| status_message: str | None = Field(default=None, alias="statusMessage") | ||
|
|
||
|
|
||
| HandlerSpec: TypeAlias = CommandHandlerSpec | ||
|
|
||
|
|
||
| class MatcherGroup(_ConfigModel): | ||
| """A matcher and its ordered hook handlers.""" | ||
|
|
||
| matcher: str | None = None | ||
| hooks: list[HandlerSpec] | ||
|
|
||
|
|
||
| class HooksConfig(_ConfigModel): | ||
| """Top-level configuration grouped by hook event.""" | ||
|
|
||
| hooks: dict[HookEvent, list[MatcherGroup]] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.