Skip to content

fix(agent): validate tool context against its schema before storing it - #44

Open
alexb-openrouter wants to merge 2 commits into
mainfrom
devin/1796054657-validate-tool-context
Open

alexb-openrouter wants to merge 2 commits into
mainfrom
devin/1796054657-validate-tool-context

Conversation

@alexb-openrouter

@alexb-openrouter alexb-openrouter commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

set_context / set_shared_context only dropped keys not in the schema; values were never validated. A tool could store {"capability": "admin"} under a schema declaring Literal["read"], and the next tool sharing the same ToolContextStore read it. Initial context was never validated either, and extract_tool_context was unused.

This matches @openrouter/agent's tool-context.ts:

# set_context / set_shared_context
merged = {**store.get_tool_context(key), **filter_schema_keys(schema, partial)}
store.set_tool_context(key, schema.model_validate(merged).model_dump())  # raises; store unchanged, no notify
  • Values are stored as the parsed model_dump() output, so validators, coercions and defaults apply.
  • build_tool_execute_context now calls extract_tool_context(store, key, schema) (new signature, internal and not exported) to validate the tool's stored context and the shared context when a schema is set. Invalid initial context raises before the tool body runs.
  • A failed write raises inside the tool body, so execute_tool returns it as the call's error.
  • With no schema, writes are stored as before.

Tests in tests/unit/test_tool_context_validation.py: Literal rejection across two execute_tool calls sharing one store (local and shared), store and subscribers unchanged on failure, validators/coercion/defaults applied, merge with existing values, and initial-context rejection. 6 of 7 fail on main.

@devin-ai-integration

devin-ai-integration Bot commented Nov 30, 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
@alexb-openrouter
alexb-openrouter marked this pull request as ready for review November 30, 2026 16:48

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