Skip to content

feat(harness): normalize recoverable tool arguments#71

Open
senamakel wants to merge 12 commits into
tinyhumansai:mainfrom
senamakel:feat/recoverable-tool-validation
Open

feat(harness): normalize recoverable tool arguments#71
senamakel wants to merge 12 commits into
tinyhumansai:mainfrom
senamakel:feat/recoverable-tool-validation

Conversation

@senamakel

@senamakel senamakel commented Jul 22, 2026

Copy link
Copy Markdown
Member

Summary

  • add an opt-in NormalizeThenReturnToolError invalid-argument policy
  • decode JSON objects emitted as strings, including markdown-fenced payloads
  • coerce non-object arguments to {} only for schemas without required fields
  • preserve precise recoverable validation errors for required-field schemas

This moves provider-neutral argument recovery into the harness admission boundary, where it runs before schema validation and before tool execution.

Validation

  • cargo fmt --check
  • cargo test normalized_ --lib
  • cargo test normalization_preserves_required_field_validation_errors --lib

Summary by CodeRabbit

  • New Features
    • Added NormalizeThenReturnToolError for schema-invalid tool-argument recovery, repairing common malformed inputs such as JSON encoded-as-strings and markdown-fenced payloads.
    • Valid normalized arguments can now execute automatically when they match the tool schema.
  • Bug Fixes
    • Improved schema validation behavior for required/properties when inputs are non-objects, especially for schemas with explicit types or permissive object shapes.
  • Documentation
    • Updated the schema-invalid tool-argument recovery docs to describe the new normalization step and outcomes.
  • Tests
    • Expanded end-to-end coverage for normalization and validation-diagnostic behavior.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7ef4c620-5a2b-4a6f-ad38-5e01649f07b2

📥 Commits

Reviewing files that changed from the base of the PR and between 3c13ec1 and 65f56c0.

📒 Files selected for processing (4)
  • src/harness/agent_loop/test.rs
  • src/harness/agent_loop/tools.rs
  • src/harness/runtime/types.rs
  • src/harness/tool/mod.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/harness/tool/mod.rs
  • src/harness/runtime/types.rs
  • src/harness/agent_loop/tools.rs

📝 Walkthrough

Walkthrough

Adds NormalizeThenReturnToolError, normalizes common malformed object arguments before validation, preserves failures for unsatisfied required fields, updates typed-schema validation, and adds end-to-end tests and documentation.

Changes

Tool argument recovery

Layer / File(s) Summary
Recovery policy and schema validation
src/harness/runtime/types.rs, src/harness/tool/mod.rs
Adds the normalization policy and changes required and properties handling for explicitly typed non-object values.
Admission-time argument normalization
src/harness/agent_loop/tools.rs
Repairs JSON strings, fenced JSON, and eligible non-object arguments before validating tool calls, while preserving raw arguments for error events.
End-to-end coverage and documentation
src/harness/agent_loop/test.rs, docs/modules/harness/tool.md
Adds normalization fixtures, Tokio coverage for valid and invalid shapes, and documentation for schema-invalid recovery.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AgentLoop
  participant admit_tool_call
  participant normalize_tool_arguments
  participant SchemaValidation
  participant RegisteredTool
  AgentLoop->>admit_tool_call: submit tool call
  admit_tool_call->>normalize_tool_arguments: normalize malformed arguments
  normalize_tool_arguments-->>admit_tool_call: normalized or original arguments
  admit_tool_call->>SchemaValidation: validate against captured schema
  SchemaValidation-->>admit_tool_call: validation result
  admit_tool_call->>RegisteredTool: invoke valid arguments
Loading

Possibly related PRs

  • tinyhumansai/tinyagents#42: Extended the shared InvalidArgsPolicy and tool-argument validation recovery path used by this change.

Suggested reviewers: m3ga-mind, yh928

Poem

I’m a rabbit with arguments, neatly repaired,
Fenced JSON unwrapped and carefully squared.
Required fields stay honest when clues disappear,
Valid tools hop onward; errors stop here.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding normalization for recoverable tool arguments in the harness.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b2307b665f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs
Comment thread src/harness/runtime/types.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 105892bc0e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs
Comment thread src/harness/agent_loop/tools.rs Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

arguments: call.arguments.clone(),

P2 Badge Snapshot raw args before normalization

When normalization decodes a stringified object but the decoded object still fails validation, for example "{\"query\":42}" for a string query, call.arguments has already been overwritten before this event is emitted. AgentEvent::InvalidToolArgs documents that it preserves the raw model-supplied arguments for replay/analysis, but this now records the normalized object instead; keep a pre-normalization snapshot and emit that on validation failures.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/harness/agent_loop/test.rs`:
- Around line 1112-1113: Extend the assertions in the relevant test around
run.final_response and calls to verify that the emitted tool result preserves
the original JSON string "hello" unchanged. Keep the existing execution-count
assertion, and inspect the captured tool result using the test’s existing
result-access mechanism rather than relying only on schema validation.

In `@src/harness/agent_loop/tools.rs`:
- Around line 481-490: Update the object-capability detection in the schema
normalization flow around accepts_object so schemas whose enum contains object
values are recognized as object-capable. Inspect the enum array and treat it as
accepting objects when any enum entry is an object, preserving the existing
type, properties, and required checks and allowing validate_schema_value to
normalize matching stringified objects.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c6e12bd4-0496-4c11-9f6c-da32b75e25d2

📥 Commits

Reviewing files that changed from the base of the PR and between 2583fcc and 3c13ec1.

📒 Files selected for processing (5)
  • docs/modules/harness/tool.md
  • src/harness/agent_loop/test.rs
  • src/harness/agent_loop/tools.rs
  • src/harness/runtime/types.rs
  • src/harness/tool/mod.rs

Comment thread src/harness/agent_loop/test.rs
Comment thread src/harness/agent_loop/tools.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c13ec1035

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a4ccc1f95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5262493eb1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02d7425ff2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/harness/agent_loop/tools.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 65f56c076d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

self.policy.invalid_args,
InvalidArgsPolicy::NormalizeThenReturnToolError
) {
normalize_tool_arguments(call, &schema);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize arguments before tool middleware

When NormalizeThenReturnToolError is enabled and a registered tool receives a stringified or fenced JSON object, this normalization runs after run_before_tool has already inspected or approved the call. Argument-sensitive hooks such as a HumanApprovalMiddleware ApprovalFn or a custom policy middleware can therefore see only the raw string and allow it, while the tool later executes with the decoded object; normalize before the before_tool hooks, or otherwise make those hooks run on the final arguments that will be executed.

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant