Repository navigation
fix(agent): recover Gemma 4 tools, images, and restart cache - #106
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_d181bb7f-0d04-4e81-a171-9f9e0bb75a6e) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f52e8ea10a
ℹ️ 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".
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_26fa73c6-827f-42b5-a3d1-cd3d88862b0c) |
32f8f09 to
2f12a7b
Compare
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_eb198af5-38b9-4042-8ab6-1627149aefd6) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2f12a7b719
ℹ️ 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".
2f12a7b to
cb99ab8
Compare
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_efdedc3f-6aaa-4f84-abf9-ffe1b24eb8d3) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cb99ab8079
ℹ️ 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".
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_4352c852-ba5b-41ed-9c07-9144e9029d89) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9084e14942
ℹ️ 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".
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_1a74e5d1-9fb7-43a2-8615-587c7fb453f6) |
…oad, mlx-node#106 Gemma4 tools/images/restart-cache Merge-forward of mirror/upstream (91f232b) into our patched main. Upstream delta since mlx-node#103: 91f232b feat(cli): revision-aware mlx download model (mlx-node#104) e6598c6 fix(agent): recover Gemma 4 tools, images, and restart cache (mlx-node#106) Conflict resolutions: - packages/agent (3 TS files): kept our genmlx-provider extension + our genmlx-only test, added upstream's mlx-local-image-input extension (order: mlx-provider, genmlx-provider, mlx-local-image-input, ...); took upstream's richer gemma4 FamilyTraits (reasoning + thinkingLevelMap). - crates/mlx-core auto-merged textually but broke the build: mlx-node#106 migrated the VLM image hash u64 -> engine::ImageCacheDigest, while our fork-only vlm_prepare_vision_continuation kept &[u64]. Migrated its signature to &[engine::ImageCacheDigest] (pure pass-through to vlm_prepare_vision_features; sole caller qwen3_5_moe already passes the digest type). Rebuild clean. - nested mlx-sys/mlx gitlink kept ours (b55ff6e6). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Summary
ImageContentRoot cause
Gemma 4 could sample a malformed outer tool block followed by a valid nested
call:...restart. The parser treated every byte before the first argument brace as the tool name, causing dispatch of the malformed prefix instead of the intended tool.Pi's clipboard flow stores a pasted image in a temporary file and inserts only its path into the editor. That path reached Gemma as plain text because no input transform upgraded it to an image attachment.
The cold tier could persist image-aware global KV blocks, but Gemma 4 skipped sliding-window sidecar capture for media turns. After restart, global layers could hit SSD while the out-of-pool sliding layers had no faithful state. Replaying expanded image placeholder token IDs cannot reconstruct vision embeddings, so image prefixes previously had to fall back cold.
Agent discovery also hard-coded local models as text-only and marked Gemma as non-reasoning. Template-less E2B checkpoints lacked selectable-thinking and tool-schema rendering. After a tool result, the canonical Gemma prompt continues inside an already-open thought channel; initializing the parser in ordinary message state exposed that reasoning as visible text.
Impact
Malformed Gemma restarts now dispatch the intended tool, including markers split across streamed chunks. Standalone absolute image paths from the interactive TUI become image content only after extension, magic-byte, size, and duplication guards pass.
A repeated Gemma 4 image prompt can reopen native SSD KV state after process restart. The caller still supplies the raw image so mlx-node can verify its digest, preprocessing identity, placeholder expansion, and positions before accepting the cache entry. Missing or malformed vision metadata, changed image bytes/order, incomplete sidecars, and corruption remain cache misses.
mlx agent --list-modelsnow reports thinking and image support for capable Gemma checkpoints before weight loading. Template-less sessions can select thinking, call tools with real schemas, consume canonical tool responses, and keep pre-tool and post-tool reasoning separate from the final answer.Cache accounting
A cache hit is capped at
prompt_tokens - 1because the final prompt token must be evaluated to produce next-token logits, then rounded down to the 16-token block boundary. Therefore a 786-token prompt reports 784 cached tokens. The real restart trace reported 960 cached out of 961 prompt tokens.Validation
Combined PR #106 branch after cherry-picking the two PR #107 commits:
cargo test -p mlx-core --lib gemma4— 420 passed, 9 ignoredcargo fmt --all -- --checkgit diff --checkyarn typecheckyarn lint— passed with pre-existing warnings onlySource-patch validation before transfer:
mlx agent --traceimage + Bash task across separate writer and restarted reader processes:cacheRead: 960for a 961-token promptcold_restore_walkfor 60 blocks,state=cold_sidecar, 960 primed tokens, zero replay tokens, andvlm_vision_tower_skipNote
High Risk
Touches durable KV/sliding cache identity, multimodal prefix restore, Gemma tool/wire-format rendering, and session replay semantics; regressions could cause wrong cache hits, broken image sessions, or mis-routed tools/reasoning.
Overview
This PR hardens multimodal cold-cache identity and Gemma 4 session behavior so image prefixes, tools, and thinking replay correctly across restarts and agent flows.
Paged / image cache (Rust): Image-bearing block keys move from
u64hashes to versioned SHA-256 payload digests, with all four digest words plus placeholder position wired into per-blockextra_keys, plus layout and preprocessing semantics markers. Hot lineage still uses a compact combined key; durable keys bind the full identity. Qwen vision cache keys follow the same digest type.Gemma 4 model: Pure-image turns can capture and restore sliding-window cold sidecars only after global K/V finalize succeeds and only at checkpoints at or after the full image span; restored sidecars must exactly match the effective prefix (no partial install inside an image run). When the cached prefix covers the image span, the vision tower is skipped. Template-less rendering gains
<|think|>, canonical tool declarations, in-turn tool responses, and tool-result deltas that resolve the function name from committed history (not opaque call ids). Prompt rendering records whether output starts inside an open thought channel so sync/stream parsers and promotion treat truncated post-tool reasoning as reasoning, not visible text.Gemma 4 output parser: Supports prompt-seeded open channels and recovers nested
call:restarts inside malformed outer tool bodies (stream + offline).Agent / TS: Tests document media-held
sendToolResultreplay carryingreasoningContent/thinkingEnabledper historical turn vs currentreasoningEffort, plus a local standalone image path input extension (guards, races, size caps). New integration tests assert image-aware KV + sliding objects survive cold-cache reopen and optional OS process restart with trace gates.Docs:
paged-cache.mddescribes the new image key ABI and stricter image sidecar semantics vs text-only optimization.Reviewed by Cursor Bugbot for commit da91abc. Bugbot is set up for automated code reviews on this repo. Configure here.