Skip to content

fix(agent): enforce PreToolUse on schema-parsed args and UserPromptSubmit on structured messages - #47

Open
alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796091790-fix-lifecycle-hook-enforcement
Open

alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796091790-fix-lifecycle-hook-enforcement

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes two lifecycle-hook enforcement gaps in ModelResult:

  1. PreToolUse now receives schema-parsed arguments and re-validates mutations:

    • Previously, PreToolUse received raw call.arguments before input_schema defaults and coercions were applied by execute_tool. A blocking hook inspecting tool_input could miss defaulted or coerced fields (for example, dangerous: bool = True omitted from {}).
    • Added _emit_pre_tool_use, _tool_input_for_hooks, and _revalidate_mutated_arguments in src/openrouter_agent/model_result.py (used by both _run_tool_with_hooks and _prepare_tool_calls_for_approval), plus an optional normalize_payload callback on HooksManager.emit / execute_handler_chain so mutations piped between chained PreToolUse handlers are also re-parsed through input_schema.
    • PermissionRequest, PostToolUse, and PostToolUseFailure also receive schema-parsed tool_input.
  2. UserPromptSubmit now fires on SDK typed messages and structured content arrays:

    • Previously, _maybe_run_user_prompt_submit / _find_latest_user_string_index only matched plain dict items with isinstance(content, str), skipping typed SDK objects (such as EasyInputMessage) and messages whose content is a structured array (such as [{"type": "input_text", "text": "..."}]).
    • Added _extract_user_message_prompt, _find_latest_user_prompt_index, and _replace_user_message_prompt to extract text from both plain dicts and typed SDK objects (string content or structured content arrays, including single non-list user message inputs) and apply mutated_prompt back without mutating caller objects or dropping non-text parts (such as input_image).

Testing

  • Added unit tests in tests/unit/test_lifecycle_hook_enforcement.py covering both fixes (schema defaults, coercions, chained mutation re-validation, invalid mutation rejection, approval-path PreToolUse, and UserPromptSubmit rejection/mutation across EasyInputMessage, structured content arrays with non-text parts, and single-message inputs).
  • Verified with uv run --extra dev ruff check ., uv run --extra dev ruff format --check ., uv run --extra dev pytest tests/unit -q --cov --cov-fail-under=83 (189 passed, 85.71% coverage), and uv run --extra dev mypy src tests.
@devin-ai-integration

devin-ai-integration Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access that start with 'Devin'.

  • Disable automatic comment, CI, and merge conflict monitoring

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant