Skip to content

fix(agent): enforce PreToolUse on parsed inputs and UserPromptSubmit on structured user messages - #179

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

alexb-openrouter wants to merge 1 commit into
mainfrom
devin/1796091716-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 (packages/agent/src/lib/model-result.ts):

  1. PreToolUse (and PermissionRequest / PostToolUse / PostToolUseFailure) now inspect inputSchema-parsed tool arguments:

    • Previously, runToolWithHooks and prepareToolCallForApproval emitted PreToolUse with raw toolCall.arguments (e.g. {}), while executeTool parsed arguments through tool.function.inputSchema (which applies .default(), .coerce, and .transform()). A PreToolUse deny hook checking a defaulted or transformed field missed the effective value that execute received.
    • Added resolveHookToolInput(tool, rawArguments) which runs z4.safeParse(tool.function.inputSchema, rawArguments) and passes parsed.data when it succeeds as a record, falling back to rawArguments ?? {} when validation fails so hooks can still inspect or repair invalid input.
    • Unmutated pass-through still preserves original effectiveToolCall.arguments (including null), and mutated inputs continue to be re-parsed through inputSchema by executeTool and validatePreparedMutation.
  2. UserPromptSubmit now supports structured content-array user messages:

    • Previously, extractPromptAndApplier only matched { role: 'user', content: string }. Structured user messages such as { role: 'user', content: [{ type: 'input_text', text: '...' }] } were skipped, allowing blocking UserPromptSubmit hooks to be bypassed (or evaluated against an earlier string-content user message instead of the latest structured user message).
    • Updated findLatestUserMessageIndex and extractPromptAndApplier to target the latest user message regardless of whether content is a string or a content array, extracting all input_text / text blocks joined with '\n' (or '' for non-text-only arrays so blocking hooks still fire).
    • Mutations replace the first text block (preserving extra fields such as promptCacheBreakpoint), drop subsequent text blocks so unmutated text does not remain, preserve non-text blocks (input_image, input_file, etc.), or append an input_text block when none existed.

Verification

  • Added unit tests in packages/agent/tests/unit/model-result-hooks.test.ts covering inputSchema defaults/transforms/mutations on both direct and approval-gate paths, and structured content-array rejection, latest-message targeting, multimodal mutation, and non-text-only arrays for UserPromptSubmit.
  • Ran pnpm lint, pnpm typecheck, and pnpm test across all packages.
@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