Skip to content

fix: support Codex grammar-formatted custom tools - #378

Draft
franciscojavierarceo wants to merge 3 commits into
mainfrom
codex/fix-codex-grammar-tools
Draft

franciscojavierarceo wants to merge 3 commits into
mainfrom
codex/fix-codex-grammar-tools

Conversation

@franciscojavierarceo

@franciscojavierarceo franciscojavierarceo commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Draft status — October 11, 2026: The proposed grammar adapter is not a contract-preserving fix. It copies a Lark or regex definition into a function description while the normalized input field still accepts any string. Following Maral’s review, grammar-formatted custom tools must continue to fail closed until an enforcing or validating path exists. The implementation and validation below describe an exploratory branch, not merge-ready support. Green CI does not resolve this design concern.

The dedicated Responses apply_patch tool uses per-file operations and apply_patch_call_output, which require a bidirectional adapter for Codex’s raw custom-tool calls and outputs. Current vLLM filters model-visible Responses tools to functions, so dedicated apply_patch also needs upstream support before this PR can provide an end-to-end fix. The next validation must cover request selection, calls, results, streaming, continuation, and failures with a real vLLM backend.

Summary

Investigates #358. This draft does not resolve it yet.

Codex declares native apply_patch as a custom tool with a Lark grammar. The gateway currently rejects that declaration before inference, even though its model catalog enables the tool.

  • Accept typed text, Lark, and regex custom-tool formats. Preserve grammar definitions in the upstream function-tool description and keep the existing string input adapter, custom-call streaming translation, and continuation path.
  • Continue rejecting malformed formats, empty grammar definitions, and unsupported deferred loading.
  • Enable native apply_patch in the launcher-generated catalog and remove the documented shell-edit workaround. Clarify that llm-d deployments must route the Codex model-catalog request to Agentic API.
  • Add regression coverage and a deterministic smoke test that runs actual Codex patch execution and tool call output continuation over HTTP and WebSocket. Require a completed file_change event as well as the expected file contents.
  • Merge current main and resolve conflicts while preserving its ToolDeclaration API and code interpreter support.
  • Move the harness CI job to Ubuntu 22.04 after Ubuntu 24.04 blocked Codex/bwrap loopback setup. Add a namespace preflight check and runner-specific Cargo cache keys; keep Codex sandboxing enabled and leave its sandbox and approval settings unchanged.

Grammar is model guidance, not upstream constrained decoding. Codex still parses and validates patches and applies its sandbox and approval policy. This adds no separate streaming or tool-execution path.

Test Plan

  • All checks on f5fbbbe pass, including Rust, Python, pre-commit, wheel builds, and the Claude Code/Codex harness job. Deployment and publication jobs are skipped as expected for this PR.
  • After merging main: 1,356 server/core tests passed (5 ignored); server/core Clippy passed with all targets and warnings denied. All repository pre-commit hooks passed, including rustfmt and the Rust file-size checks.
  • Deterministic native apply_patch smoke passed over HTTP and WebSocket with Codex 0.160.0 after the conflict resolution.
  • With the exact CI-pinned Codex 0.149.1, the full local scripts/codex-smoke.sh suite passed: text, image requests/capture checks, and native patch execution plus continuation over HTTP and WebSocket. The default sandbox remained enabled. The updated harness also passed on GitHub's Ubuntu 22.04 runner.
  • Earlier validation: deterministic actual-Codex smoke passed over HTTP and WebSocket with Codex 0.155.0-alpha.9.2.
  • Earlier live validation with Qwen/Qwen3.8-27B-FP8 on vLLM 0.29.0: HTTP and WebSocket edit/resume runs passed add, update, rename, delete, Unicode, backslash, and matching custom-call/output checks. These live runs were not repeated for the latest CI-only change.
  • Earlier validation with the actual saved Spark profile: five live turns passed native patches, shell verification, CLI restart/resume, Desktop app-server restart/resume, and same-task continuation after a gateway service restart. Sandbox and approval settings were retained. Desktop's backend was tested, not the native UI.

No replay cassettes were edited; existing captures are reused for normalization assertions, and the new end-to-end smoke uses a synthetic upstream. These checks validate the exploratory adapter's execution path, not grammar enforcement or a dedicated Responses apply_patch adapter.

@maralbahari

Copy link
Copy Markdown
Collaborator

Thanks for working on this. This brings back the concern raised in the review of #158: copying a Lark or regex grammar into the function description while accepting any string as input does not preserve the custom tool’s constrained-decoding contract. That review recommended enforcing or validating the grammar, or rejecting unsupported formats.

vLLM has grammar compilation and next-token masking, but this adapter does not connect the declared grammar to that machinery. Consequently, vLLM does not enforce this grammar through the proposed path. Prompt guidance may improve results, and Codex’s patch validation remains useful, but neither preserves the decoding guarantee. I understand the PR acknowledges this limitation; my concern is that it reintroduces the behavior previously rejected.

For apply_patch specifically, could we instead explore supporting the Responses API’s dedicated {"type": "apply_patch"} tool, with an adapter for Codex’s custom tool named apply_patch? This would require translating the raw patch format, calls, and results, and ensuring backend support, not just renaming the type. That seems worth investigating before treating prompt-only grammar handling as the complete fix.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The focused adaptation reuses existing execution paths, has targeted regression coverage, and has no identified blocking issues.

Review effort: Balanced
Findings: None

What changed in this PR

Enables Codex’s grammar-formatted custom tools through the existing function-tool adapter, preserving client execution, streaming, and continuation behavior.

Changes:

  • Accept typed text, Lark, and regex formats; carry grammars as model guidance, not constrained decoding.
  • Enable native apply_patch in launcher catalogs and update setup documentation.
  • Add regression coverage and actual-Codex HTTP/WebSocket smoke tests.
File Description
scripts/​codex-smoke.sh Runs the native patch smoke test.
scripts/​codex_apply_patch_smoke.py Checks actual Codex patch execution and continuation.
docs/​guides/​codex-desktop.md Replaces the workaround and clarifies catalog routing.
docs/​design/​codex-integration.md Documents grammar adaptation and verification.
crates/​agentic-server/​tests/​responses_websocket_test.rs Checks grammar preservation across continuations.
crates/​agentic-server/​src/​agentic_harness.rs Enables native patch tools in launcher catalogs.
crates/​agentic-server-core/​tests/​tool_normalization_test.rs Checks recorded grammar declarations normalize correctly.
crates/​agentic-server-core/​tests/​custom_tool_test.rs Verifies grammar-tool adaptation.
crates/​agentic-server-core/​src/​types/​tools/​params.rs Uses typed custom-tool formats.
crates/​agentic-server-core/​src/​types/​tools/​mod.rs Exports custom format types.
crates/​agentic-server-core/​src/​types/​tools/​custom.rs Defines formats and malformed-input tests.
crates/​agentic-server-core/​src/​types/​request_response.rs Updates normalization documentation and tests.
crates/​agentic-server-core/​src/​tool/​normalize.rs Clarifies custom-tool normalization documentation.
crates/​agentic-server-core/​src/​tool/​custom.rs Adds grammar instructions and empty-definition validation.
ARCHITECTURE.md Documents reuse of existing execution paths.
.rust-file-sizes.json Lowers the parameter file’s size allowance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@franciscojavierarceo
franciscojavierarceo marked this pull request as draft October 7, 2026 12:35
Merge main and preserve the grammar-formatted custom-tool adapter through the protocol-neutral ToolDeclaration path. Keep newer code-interpreter support and ratchet the merged file-size allowance.

Validation: 1,356 server/core unit and focused integration tests passed (5 ignored); Clippy all targets with warnings denied; all pre-commit hooks; actual Codex 0.160.0 HTTP and WebSocket native-patch smoke tests.
Signed-off-by: Francisco Javier Arceo <[email protected]>
Pin the harness job to Ubuntu 22.04 and verify user/network namespace loopback setup before building. Keep Codex's default workspace-write and network restrictions; isolate build caches by runner image. Require a completed native file-change event in addition to exact fixture contents.

Validation: actual Codex 0.149.1 HTTP and WebSocket patch tests passed with the default sandbox; all pre-commit hooks passed. The original Ubuntu 24.04 failure was bwrap RTM_NEWADDR EPERM, before patch execution.
Signed-off-by: Francisco Javier Arceo <[email protected]>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants