Repository navigation
Constrain Gemma 4 tool-call argument keys and report malformed calls (#3268) - #3463
Conversation
Pragmatic Code Review — PR #3463 (issue #3268)Review Type: Initial Review (no prior reviews or comments on the PR) Scope read: SummaryThe change is a net improvement and nothing in it blocks a merge. The grammar rewrite targets the real cause: llama.cpp's rule is The findings below are five improvements and a few optional ones. Requirements Check
Code Review FindingsNo 🔴 Critical findings. 🟡 Important1. The engine-side behaviour is pinned only by 📁 let constrained = constrain_gemma4_argument_keys(grammar_str);
if constrained.is_none() && tmpl_result.chat_format == CHAT_FORMAT_PEG_GEMMA4 {
tracing::warn!( ... "argument keys are not constrained" ...llama-cpp-2 = { version = ">=0.1.146", optional = true }The rewrite matches one exact line of llama.cpp-generated text. Deleting the Recommendation: change the requirement to 2. 📁 let node_type = ["nodeType", "node_type", "type"]
.iter()
.find_map(|key| node.get(*key).and_then(|v| v.as_str()))Every node wire struct in 3. The reply-shaping clause in the 📁 ADR-064 rule 2 gives tool descriptions tool selection. "Call this with node_type='schema'" and "EXISTING SCHEMAS is not that list" are selection guidance, and the 0 of 3 → 3 of 3 measurement supports them. "The answer names each one it returned" is reply policy. The PR's own data says the reply still summarises on one database, so this clause has no measured effect. ADR-064 says content returns "only when a specific measurement demands it, one rule at a time". Recommendation: drop the clause, or keep it only with an A/B reading that shows it changes the reply. 4. A call refused as malformed is still replayed verbatim into the next prompt 📁 /// The offending text is deliberately not quoted back:
/// the model copies the shape of what it reads in its own history.
The tool result does not quote it, but the assistant turn pushed at line 3384 carries On the native path the grammar now makes the kwargs first call unreachable, so this mostly affects the OpenAI-compatible engine. Recommendation: when a call is classified as malformed, record its arguments in history as 5. The new scenario's score has never been observed 📁 The scorer is well tested, but criterion 5 asks for coverage on the default local model, and the one rep excluded the scenario's group. A scenario expected to fail from its first run reports nothing useful when it does. Recommendation: get one reading where the group runs before merging, or record in the issue that the criterion is open. If the expected result is a failure, say so where the fixture's other known-failing scenarios are tracked. 🟢 Suggestions6. 📁 None => (
malformed_call_result(malformed_message.unwrap_or_default()),
true,
),
7. The clarify pre-scan can now disagree with the per-call loop 📁 The comment says the pre-scan "cannot disagree with what that loop decides for the same call". A 8. 📁 A reply that samples the 9. Record the NodeSpace-side grammar rewrite in nodespace-docs ADR-056 names "a NodeSpace-side mitigation" for Gemma 4's argument grammar as a re-introduction path for 12B. This PR is the first such mitigation and brings a constraint (a field name with a space or other excluded character is not writable through a native tool call). That belongs in the docs repo, not only in a doc comment. Nitpicks
Checked and found sound
RecommendationAPPROVE — findings 1 to 5 are worth addressing; none blocks the merge. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.
Review addressed (commit cb1e940)Addressed
Not changed
Verification: Re-Review DecisionDecision: NO RE-REVIEW NEEDED Rationale: No critical findings were raised. The changes are a dependency pin, a deleted description clause, a removed dead lookup, comment rewording, and two small, test-covered changes on the malformed-call path (the result plumbing and one extra check in the pre-scan). None adds functionality or changes design. |
…loses #3268) "What schemas do we have here?" on gemma-4-E4B produced a call whose whole kwargs-style argument list arrived as one JSON key, then a call with the answer inside its arguments, then a reply naming only the custom types. nlp-engine - Narrow llama.cpp's Gemma 4 grammar rule for an argument key from "any text up to the next colon" to a name, so an argument list, prose, an over-quoted key or a leaked quote token can no longer be a key. - Stop generation at the call's close marker. A string closed with a plain quote never ends, and swallowed the marker and the model's answer up to the token cap. agent - Do not dispatch arguments that parse as JSON but are not an object of named parameters; report them to the model as a malformed call, counted by the same breaker as arguments that do not parse. - When a tool failure goes unmentioned in the reply, say which action failed and why instead of a generic retry prompt. - search_nodes' description says node_type='schema' lists every type, and that EXISTING SCHEMAS is not that list. - The fabricated-id guard read a bolded id with its closing asterisks and replaced a correct schema list. eval - decisions: an outcome scenario for "what schemas do we have", scored on the reply naming built-in and custom types off a successful search. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l error - search_nodes reported every node's type as empty (nodeType is the wire spelling) and a schema node's title as empty (its name is its content), so a type listing gave the model ids and nothing to call them. - The message that replaces a reply ignoring a tool failure quotes the tool's first sentence only; the rest is addressed to the model. - search_nodes' description says a type listing names each type returned. - Drop a timing figure from a live test's doc comment that was measured while another run shared the machine. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…se, tighten the malformed-call path
- Pin llama-cpp-2 to =0.1.146: the grammar rewrite matches one rule of the
text this version generates, and only Cargo.lock was holding it.
- search_result_summary reads nodeType only; the other two spellings never
occur on a typed node value.
- Drop "the answer names each one it returned" from search_nodes'
description: reply policy in a selection channel, with no reading that
isolates it (ADR-064).
- The dispatch match yields the malformed-call result itself, instead of a
message carried beside an absent result.
- The clarify pre-scan applies the same malformed-call check as the per-call
loop, so the two cannot disagree about a call.
- A cut-short tool error ends in its ellipsis, without a full stop after it.
- Comments: say what replaying a malformed call as {} was measured to do,
and describe behaviour rather than its history.
Skipped, with reasons in the PR comment: gating the close-marker stop on an
open call (needs a live-model run to verify), and a distinct tag for the
eval's two outcome expectations.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
a8b7431 to
128202f
Compare
Part of #3268. Acceptance criteria 1, 2 and 4 are met. Criterion 3 is partly met and criterion 5 has a scenario but no score yet (see "Not fixed"), so the issue stays open.
What went wrong
"What schemas do we have here?" on gemma-4-E4B failed three ways in one turn:
[^:}]+— any text up to the next colon. A kwargs-style call (query="",node_type="schema",limit=50,sorting=[{…) satisfies it and reaches the tool as one JSON key.<|"|>token. Closed with a plain", it never ends, and swallows the call's close marker and the answer the model writes next.Changes
nlp-engine
constrain_gemma4_argument_keysrewrites the grammar's key rule so a key can only be a name. A warning is logged if llama.cpp's rule is not in the expected shape, andllama-cpp-2is pinned to=0.1.146so the rule cannot change under acargo update.ends_gemma4_tool_callstops generation at<tool_call|>(a turn makes one call).agent
MAX_CONSECUTIVE_MALFORMED_CALLS, renamed).search_nodes' description saysnode_type='schema'lists every type and that EXISTING SCHEMAS is not that list. One line changes instage2_tool_surface.golden.search_nodesresults reported every node's type as empty (nodeTypeis the wire spelling) and a schema node's title as empty (its name is its content). Both are filled in now.**nodespace://ordered-list**with its closing asterisks, so a correct schema list was replaced by "Could you confirm…". Found while measuring; fixed here.eval
decisionsgainsoutcome-list-types: the reply must name built-in and custom types off a successfulsearch_nodes.Measured on gemma-4-e4b-q4km
search_nodeswithnode_type='schema'Not fixed
eval:decisionsits group was excluded, along with three others, because thesetup-venueturn declined to re-create a type that already existed. Of the 8 scenarios that were scored, 6 passed;skill-instance-requestanddeclarative-fact-report-resolvedfailed. There is no baseline rep on main to compare against.Tests
--run-ignored only, needs the E4B GGUF):toolcall_json_shape, 4 of 4 pass.a_retry_cannot_copy_over_quoted_keys_from_the_prior_callreplacesretry_copies_the_shape_of_the_prior_call, whose measured outcome this change flips.bun run test:changedandquality:fixon the branch merged with current main.🤖 Generated with Claude Code