[PR #5398] [Engine] Integrate tool and JSON constraints on Metal, with Mac regression evidence #5413

Open
opened 2026-11-07 16:35:02 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5398
Author: @tuhochi
Created: 11/6/2026
Status: 🔄 Open

Base: main ← Head: contribution/tool-json-metal


📝 Commits (10+)

  • 1903bb4 [Engine] integrate tool and JSON token constraints with Metal verification
  • 58e44df [Engine] preserve exact tool-name masks for Responses namespaces
  • f72a483 [Engine] preserve parallel request IDs when adding token masks
  • 0cb8b78 [Tests] exercise minimal and optional JSON validation modes independently
  • 01ed660 [Tests] remove platform and stale-version assumptions from app fixtures
  • 3030aa3 [Tests] isolate launch pilot terminal capabilities from the runner
  • b7dbc59 [Tests] identify the full server regression gate explicitly
  • a3e29a8 [Tests] simulate the destination disk in the low-space warning test
  • 2881d47 [Tests] isolate TUI screens from installed network helpers
  • 6fe85c2 [Tests] verify legacy download guard before the explicit override

📊 Changes

38 files changed (+2998 additions, -61 deletions)

View changed files

📝 .github/workflows/terminal-minimal-server.yml (+24 -4)
📝 apps/host/tests/component-updates.test.mjs (+1 -1)
📝 docs/DETAILS.md (+37 -2)
➕ include/strata/core/token_mask.hpp (+162 -0)
📝 include/strata/core/verify.hpp (+18 -0)
📝 metal/src/engine/window.cpp (+40 -1)
📝 requirements.txt (+1 -0)
➕ serve/constrain.py (+751 -0)
➕ serve/fake_mask_engine.py (+89 -0)
📝 serve/responses.py (+23 -2)
📝 serve/server.py (+112 -4)
📝 serve/structured.py (+8 -0)
➕ serve/test_constrain.py (+971 -0)
📝 serve/test_ds_parallel.py (+9 -1)
📝 serve/test_server.py (+2 -2)
📝 serve/test_structured.py (+44 -0)
➕ serve/test_tool_name_constraint.py (+349 -0)
📝 setup.py (+2 -1)
📝 src/core/verify.cpp (+70 -0)
📝 src/program/generate.cpp (+91 -6)

...and 18 more files

📄 Description

Description

Problem and approach

Requests could return undeclared tool names or schema-invalid JSON despite
providing tools or a response format. Combining tools and JSON constraints
also exposed a mask-width mismatch on Metal.

This builds on ncphri’s #3800 and #4222, which provide the original
constraint implementation. This PR adds:

  • Metal integration for token masks and speculative verification.
  • Persistent declared-name protection across prose and multiple tool calls.
  • Fixes for mask-width composition and the thinking-to-JSON boundary.
  • Request-driven activation without additional enabling ENV variables.

The architectural reference is llama.cpp: constrain tokens before emission,
then stream accepted output. Identical behavior in every case is not claimed.

Apple Silicon results

Same model and frozen requests, comparing unpatched Strata with our integrated fork:

Check Unpatched Patched
Undeclared tool names — targeted stress 10/10 0/10
Undeclared tool names — additional variants 3/5 0/5
JSON stress cases passing 3/10 9/10
Tools + JSON integration cases passing 0/3 3/3

Across 113 requests, ordinary control outputs were unchanged. Warm median latency changed by +0.4–0.7% for tool workloads and −0.7–+2.5% for JSON workloads. These measurements cover this integrated fork, not either original PR in isolation.

Reproduction, raw results and measurement limits (https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md)

Validation and remaining limits

  • 85 focused tests passed on this PR branch. Legacy fake-engine tests use the explicit tool-name-guard opt-out.
  • The earlier full server-suite run had five failures/errors; a fully passing server suite is not claimed.
  • CUDA, HIP and SYCL builds and hardware execution remain outstanding.
  • Benchmark responses were independently validated against the full schema. One array case still fails: unsupported uniqueItems makes llguidance compilation fail, and the existing fallback drops the mask.
  • No additional strict server-side rejection was introduced. llama.cpp also lacks uniqueItems enforcement, but retains supported constraints when skipping it; fallback parity remains incomplete.

Keeping this PR in draft while the outstanding checks are addressed.

## Apple Silicon results

Same model and frozen requests, comparing unpatched Strata with our integrated fork:

Check Unpatched Patched
Undeclared tool names — targeted stress 10/10 0/10
Undeclared tool names — additional variants 3/5 0/5
JSON stress cases passing 3/10 9/10
Tools + JSON integration cases passing 0/3 3/3

Across 113 requests, ordinary control outputs were unchanged. Warm median
latency changed by +0.4–0.7% for tool workloads and −0.7–+2.5% for JSON
workloads. These measurements cover this integrated fork, not either
original PR in isolation.

Reproduction, raw results and measurement limits (https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md)

Validation and remaining limits

  • 85 focused tests passed on this PR branch. Legacy fake-engine tests use
    the explicit tool-name-guard opt-out.
  • The earlier full server-suite run had five failures/errors; a fully
    passing server suite is not claimed.
  • CUDA, HIP and SYCL builds and hardware execution remain outstanding.
  • Benchmark responses were independently validated against the full schema.
    One array case still fails: unsupported uniqueItems makes llguidance
    compilation fail, and the existing fallback drops the mask.
  • No additional strict server-side rejection was introduced. llama.cpp also
    lacks uniqueItems enforcement, but retains supported constraints when
    skipping it; fallback parity remains incomplete.

Keeping this PR in draft while the outstanding checks are addressed.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5398 **Author:** [@tuhochi](https://github.com/tuhochi) **Created:** 11/6/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `contribution/tool-json-metal` --- ### 📝 Commits (10+) - [`1903bb4`](https://github.com/Niko1221/Strata/commit/1903bb44128245151db76f6fe9bbe0f1bd218bdf) [Engine] integrate tool and JSON token constraints with Metal verification - [`58e44df`](https://github.com/Niko1221/Strata/commit/58e44dfee7bc692e5b2cbac34ccf6a439f770720) [Engine] preserve exact tool-name masks for Responses namespaces - [`f72a483`](https://github.com/Niko1221/Strata/commit/f72a483b18a62f9a91c18002b3ce2a72f316e5e6) [Engine] preserve parallel request IDs when adding token masks - [`0cb8b78`](https://github.com/Niko1221/Strata/commit/0cb8b7802684905c13972c39ef389b7d499a3815) [Tests] exercise minimal and optional JSON validation modes independently - [`01ed660`](https://github.com/Niko1221/Strata/commit/01ed660364c4ec8bedd41dd0cfd248ea2f4a3bda) [Tests] remove platform and stale-version assumptions from app fixtures - [`3030aa3`](https://github.com/Niko1221/Strata/commit/3030aa3cd46db0fdcdb493aded9f427042ee1334) [Tests] isolate launch pilot terminal capabilities from the runner - [`b7dbc59`](https://github.com/Niko1221/Strata/commit/b7dbc591e2fdf9093908ce1d5850b93f511a26a1) [Tests] identify the full server regression gate explicitly - [`a3e29a8`](https://github.com/Niko1221/Strata/commit/a3e29a8f7186242fdbab1ef73898b9f0aa566a88) [Tests] simulate the destination disk in the low-space warning test - [`2881d47`](https://github.com/Niko1221/Strata/commit/2881d47e108913a169a948940fc9ddd856b56b0a) [Tests] isolate TUI screens from installed network helpers - [`6fe85c2`](https://github.com/Niko1221/Strata/commit/6fe85c21a649017a06d6769782295a343ac75532) [Tests] verify legacy download guard before the explicit override ### 📊 Changes **38 files changed** (+2998 additions, -61 deletions) <details> <summary>View changed files</summary> 📝 `.github/workflows/terminal-minimal-server.yml` (+24 -4) 📝 `apps/host/tests/component-updates.test.mjs` (+1 -1) 📝 `docs/DETAILS.md` (+37 -2) ➕ `include/strata/core/token_mask.hpp` (+162 -0) 📝 `include/strata/core/verify.hpp` (+18 -0) 📝 `metal/src/engine/window.cpp` (+40 -1) 📝 `requirements.txt` (+1 -0) ➕ `serve/constrain.py` (+751 -0) ➕ `serve/fake_mask_engine.py` (+89 -0) 📝 `serve/responses.py` (+23 -2) 📝 `serve/server.py` (+112 -4) 📝 `serve/structured.py` (+8 -0) ➕ `serve/test_constrain.py` (+971 -0) 📝 `serve/test_ds_parallel.py` (+9 -1) 📝 `serve/test_server.py` (+2 -2) 📝 `serve/test_structured.py` (+44 -0) ➕ `serve/test_tool_name_constraint.py` (+349 -0) 📝 `setup.py` (+2 -1) 📝 `src/core/verify.cpp` (+70 -0) 📝 `src/program/generate.cpp` (+91 -6) _...and 18 more files_ </details> ### 📄 Description Description ## Problem and approach Requests could return undeclared tool names or schema-invalid JSON despite providing tools or a response format. Combining tools and JSON constraints also exposed a mask-width mismatch on Metal. This builds on ncphri’s #3800 and #4222, which provide the original constraint implementation. This PR adds: - Metal integration for token masks and speculative verification. - Persistent declared-name protection across prose and multiple tool calls. - Fixes for mask-width composition and the thinking-to-JSON boundary. - Request-driven activation without additional enabling ENV variables. The architectural reference is llama.cpp: constrain tokens before emission, then stream accepted output. Identical behavior in every case is not claimed. <h2>Apple Silicon results</h2> <p>Same model and frozen requests, comparing unpatched Strata with our integrated fork:</p> Check | Unpatched | Patched -- | -- | -- Undeclared tool names — targeted stress | 10/10 | 0/10 Undeclared tool names — additional variants | 3/5 | 0/5 JSON stress cases passing | 3/10 | 9/10 Tools + JSON integration cases passing | 0/3 | 3/3 <p>Across 113 requests, ordinary control outputs were unchanged. Warm median latency changed by +0.4–0.7% for tool workloads and −0.7–+2.5% for JSON workloads. These measurements cover this integrated fork, not either original PR in isolation.</p> <p><a href="https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md">Reproduction, raw results and measurement limits (https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md)</a></p> <h2>Validation and remaining limits</h2> <ul> <li>85 focused tests passed on this PR branch. Legacy fake-engine tests use the explicit tool-name-guard opt-out.</li> <li>The earlier full server-suite run had five failures/errors; a fully passing server suite is not claimed.</li> <li>CUDA, HIP and SYCL builds and hardware execution remain outstanding.</li> <li>Benchmark responses were independently validated against the full schema. One array case still fails: unsupported <code>uniqueItems</code> makes llguidance compilation fail, and the existing fallback drops the mask.</li> <li>No additional strict server-side rejection was introduced. llama.cpp also lacks <code>uniqueItems</code> enforcement, but retains supported constraints when skipping it; fallback parity remains incomplete.</li> </ul> <p>Keeping this PR in draft while the outstanding checks are addressed.</p> ## Apple Silicon results Same model and frozen requests, comparing unpatched Strata with our integrated fork: | Check | Unpatched | Patched | |---|---:|---:| | Undeclared tool names — targeted stress | 10/10 | 0/10 | | Undeclared tool names — additional variants | 3/5 | 0/5 | | JSON stress cases passing | 3/10 | 9/10 | | Tools + JSON integration cases passing | 0/3 | 3/3 | Across 113 requests, ordinary control outputs were unchanged. Warm median latency changed by +0.4–0.7% for tool workloads and −0.7–+2.5% for JSON workloads. These measurements cover this integrated fork, not either original PR in isolation. [Reproduction, raw results and measurement limits (https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md)](https://github.com/tuhochi/Strata/blob/07544207d16993c31f5dda2067b2c0671b33be40/fork/evaluation/P3-HARDWARE-20261106.md) ## Validation and remaining limits - 85 focused tests passed on this PR branch. Legacy fake-engine tests use the explicit tool-name-guard opt-out. - The earlier full server-suite run had five failures/errors; a fully passing server suite is not claimed. - CUDA, HIP and SYCL builds and hardware execution remain outstanding. - Benchmark responses were independently validated against the full schema. One array case still fails: unsupported uniqueItems makes llguidance compilation fail, and the existing fallback drops the mask. - No additional strict server-side rejection was introduced. llama.cpp also lacks uniqueItems enforcement, but retains supported constraints when skipping it; fallback parity remains incomplete. Keeping this PR in draft while the outstanding checks are addressed. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5413
No description provided.