Repository navigation
fix(gemini): preserve thought_signature on stored tool calls; retry burst 429s; enable compaction - #1256
Conversation
…ilent When browser setup had already completed but Firefox was simply not running, the bridge stopped responding and every message pointed the agent at action='setup', which is a one-time install/repair flow. Agents then re-ran setup (reopening the extension installer) instead of just starting Firefox. - Detect whether Firefox is running (per-OS) and, when the bridge is installed but silent because Firefox is closed, launch Firefox detached and wait for the bridge to reconnect before failing. - ensure_firefox_ready() now auto-launches Firefox and proceeds with the requested action when the bridge reconnects, prefixing a note. - browser setup and 'jcode browser status'/tool status now distinguish 'Firefox is not running' from a genuinely broken install and say setup is one-time and not needed again. - JCODE_BROWSER_AUTOLAUNCH=0 disables the auto-launch (also used to keep tests hermetic).
Root cause of the recurring HTTP 400 on the *second* message of a Gemini session: Function call is missing a thought_signature in functionCall parts. ... function call `default_api:websearch`, position 10. The signature was ingested and streamed correctly (ToolUseSignature -> ToolCall.thought_signature), but every turn loop then hand-built the persisted ContentBlock::ToolUse, and three of the four copies hardcoded `thought_signature: None`: jcode-tui/src/tui/app/turn.rs:333,401 (interrupt paths) jcode-tui/src/tui/app/turn.rs:1087 (main TUI path - every turn) jcode-app-core/src/agent/turn_streaming_mpsc.rs:1081 Only turn_loops.rs:785 carried it through. So the signature was captured, then discarded the moment the assistant message was stored: turn 2 replayed a fully-unsigned history and the backend rejected it. Retrying could never help, because the stored history was already stripped. Rather than fix four copies and leave the fifth to rot, the construction moves to ToolCall::to_tool_use_block(), the single place a completed tool call becomes a stored block. Duplication is what let this diverge. This is the real fix for the failure that PR #1 (branch fix/gemini-missing-thought-signature-recovery) only mitigated: that downgrade-to-text retry salvages an already-stripped history, but with signatures preserved it should now rarely be reached. Tests: three regression tests in jcode-message-types covering signature preservation, session-JSON round-trip (resumed sessions hit the same 400 if the field is lost on disk), and clean omission for providers that do not use signatures. Falsified by restoring `None`: 2 of 3 fail. jcode-app-core has 9 pre-existing unrelated failures, identical with and without this change (verified via stash). fmt clean; jcode-tui clippy warning count unchanged at 48.
supports_compaction() gates the entire compaction block in Agent::messages_for_provider, including the emergency hard-compact and payload truncation that fire at the 95% critical threshold. These three runtimes returned a hardcoded false and implement no native_compact, so they had no compaction of any kind: a long session grew unchecked until the provider rejected the prompt, with nothing able to rescue it. Unlike OpenAI there was no config escape hatch either. None of the three has native server-side compaction to conflict with, and all three inherit the default complete_simple(), which is what generate_compaction_artifact falls back to for the text summary. So opting in gives them the full engine: proactive/semantic folding, hard compact and emergency truncation. Latent rather than observed today only because no Gemini session on the test machine had yet exceeded 50 messages; Gemini advertises a 1M window, so the failure arrives late and all at once.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a5489f9c89
ℹ️ 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".
| // disabled the entire compaction block in `Agent::messages_for_provider` | ||
| // — including the emergency hard-compact and payload truncation at the | ||
| // critical threshold — leaving these sessions with no safety net at all. | ||
| true |
There was a problem hiding this comment.
Keep Cursor compaction off until summaries bypass prompt truncation
When a Cursor session is large enough to compact, the generic compactor sends the selected history through complete_simple, which reaches build_cli_prompt; that function retains only the final 120,000 characters (MAX_PROMPT_CHARS, lines 28 and 96-105). The compaction manager nevertheless marks every selected message as covered by the resulting summary, so an oversized compaction prompt silently omits the earliest conversation while hiding all of those original messages from subsequent requests. Keep this disabled or provide a chunked/non-truncating summary path for Cursor.
Useful? React with 👍 / 👎.
| if status == reqwest::StatusCode::TOO_MANY_REQUESTS && attempt < MAX_429_RETRIES { | ||
| let delay = Duration::from_millis(1500u64.saturating_mul(1u64 << attempt)); |
There was a problem hiding this comment.
Restrict retries to transient 429 responses
When Code Assist returns a persistent 429 for exhausted quota or billing limits, this condition retries solely by status and ignores the error body and any server retry interval. It therefore issues five doomed requests and delays the actionable error by 46.5 seconds; conversely, a transient limit with a retry interval longer than that can exhaust all attempts prematurely. Classify the body or honor the server-provided retry delay before entering this burst-retry loop.
Useful? React with 👍 / 👎.
|
| let delay = Duration::from_millis(1500u64.saturating_mul(1u64 << attempt)); | ||
| jcode_base::logging::warn(&format!( | ||
| "Gemini {} hit burst 429 (attempt {}/{}); retrying in {:?}", | ||
| method, | ||
| attempt + 1, | ||
| MAX_429_RETRIES, | ||
| delay | ||
| )); | ||
| attempt += 1; | ||
| tokio::time::sleep(delay).await; |
There was a problem hiding this comment.
The new 429 handling retries only after fixed 1.5/3/6/12/24-second delays and never reads Retry-After. When the provider directs the client to wait longer, all six attempts can be consumed before requests are permitted again. A response with Retry-After: 60 exhausts the final attempt at about 46.6 seconds, causing the turn to fail even though waiting for the server-provided deadline would allow a later retry.
Artifacts
- Runs an injected in-module test against the parent and current revisions with a local 429 endpoint that sends Retry-After: 60, exercising the actual post_json retry path; it demonstrates the compared behavior.
- Captured command output for the parent revision shows one local HTTP 429 Too Many Requests response with Retry-After: 60 and one request at 30 ms; before the change there was no 429 retry.
- Captured command output for the current revision shows six local HTTP 429 Too Many Requests responses despite Retry-After: 60, with the final attempt at 46.568 seconds; the retry budget is exhausted before provider availability.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-provider-gemini-runtime/src/lib.rs
Line: 401-410
Comment:
**Honor Retry-After Timing**
The new 429 handling retries only after fixed 1.5/3/6/12/24-second delays and never reads `Retry-After`. When the provider directs the client to wait longer, all six attempts can be consumed before requests are permitted again. A response with `Retry-After: 60` exhausts the final attempt at about 46.6 seconds, causing the turn to fail even though waiting for the server-provided deadline would allow a later retry.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| // No native server-side compaction exists for this provider, so jcode's | ||
| // own summary compaction is the only thing standing between a long | ||
| // session and a hard context-limit rejection. Returning `false` here | ||
| // disabled the entire compaction block in `Agent::messages_for_provider` | ||
| // — including the emergency hard-compact and payload truncation at the | ||
| // critical threshold — leaving these sessions with no safety net at all. | ||
| true | ||
| } |
There was a problem hiding this comment.
Keep Switched-Provider Context
Enabling compaction for Gemini exposes OpenAI-native compaction state to a conversion path that cannot replay it. Gemini and Antigravity omit the encrypted compaction block entirely, while Cursor replaces it with a fixed marker. After switching providers, the request loses the compacted earlier conversation and contains only the active suffix, so the model can no longer use that prior context.
Artifacts
- The authored executable creates isolated before and after worktrees, injects focused runtime tests, and captures the provider conversion results; it demonstrates the exact exercised source path.
- The executed `HEAD^` run shows all three providers had compaction disabled while the same Gemini, Antigravity, and Cursor conversions respectively omit or reduce the OpenAI-native block; the unsafe conversion existed but was not enabled by this PR.
- The executed `HEAD` run shows PR fix(gemini): preserve thought_signature on stored tool calls; retry burst 429s; enable compaction #1256 enables compaction and the focused tests pass with `gemini contents=[]`, `antigravity contents=[]`, and Cursor's placeholder-only prompt; the enabled path loses the compacted context.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-provider-gemini-runtime/src/lib.rs
Line: 1096-1103
Comment:
**Keep Switched-Provider Context**
Enabling compaction for Gemini exposes OpenAI-native compaction state to a conversion path that cannot replay it. Gemini and Antigravity omit the encrypted compaction block entirely, while Cursor replaces it with a fixed marker. After switching providers, the request loses the compacted earlier conversation and contains only the active suffix, so the model can no longer use that prior context.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.…ty) like burst 429s
…onId Gemini/Claude-CLI/Grok emit StreamEvent::SessionId with the *provider* resume handle. The mpsc turn loop re-broadcast it as ServerEvent::SessionId, which the TUI treats as the jcode session id and stores in remote_session_id. Every later reload/reconnect then ran 'jcode --resume <provider-id>', the server found no such session and created a fresh empty one, and the user saw their entire conversation vanish while the transcript sat intact on disk.
| let transient = | ||
| status == reqwest::StatusCode::TOO_MANY_REQUESTS || status.is_server_error(); | ||
| if transient && attempt < MAX_429_RETRIES { |
There was a problem hiding this comment.
If Code Assist completes a generateContent request before responding with a 5xx, this loop sends the identical POST again without an idempotency or deduplication key. The provider can run and bill the generation twice while jcode discards the first result; onboardUser uses the same helper and has the same replay behavior. Retry only failures where no response was received, or use the provider-supported idempotency mechanism.
Artifacts
- The executed shell harness starts a local Code Assist-compatible server, returns a 500 then 200 for generateContent, and records each received request, demonstrating the precise retry contract.
- Combined output from executing the local harness shows HTTP 500 followed by HTTP 200, identical request body and user prompt ID, and no idempotency or deduplication headers, confirming an unkeyed replay.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-provider-gemini-runtime/src/lib.rs
Line: 403-405
Comment:
**Prevent Duplicate Generations**
If Code Assist completes a `generateContent` request before responding with a 5xx, this loop sends the identical POST again without an idempotency or deduplication key. The provider can run and bill the generation twice while jcode discards the first result; `onboardUser` uses the same helper and has the same replay behavior. Retry only failures where no response was received, or use the provider-supported idempotency mechanism.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.…at a bounded size Every turn re-sends the whole transcript. With Gemini's 1M window and the 0.80 trigger, a session could reach ~800k tokens per request before folding; one real session hit 371k/turn (1.3 MB JSON) with compaction never firing. [compaction] max_context_tokens bounds the budget in the single set_budget funnel so every provider path (agent, TUI, model switch) honors it. 0 = off.
| /// Update the token budget (e.g., when model changes) | ||
| pub fn set_budget(&mut self, budget: usize) { | ||
| self.token_budget = budget; | ||
| self.token_budget = Self::capped_budget(&self.compaction_config, budget); |
There was a problem hiding this comment.
When [compaction] max_context_tokens changes during a running session, the existing compaction manager continues to use the value captured when it was created. After the global configuration reloads from 50,000 to 10,000, calling set_budget(1_000_000) still permits 50,000 tokens for that session, while a newly created manager uses 10,000. This can leave a long-running session 40,000 tokens above an operator's newly lowered context and cost limit.
Artifacts
- The initial run shows that a manager constructed with a 50,000-token limit applies that limit.
- The reload run shows the existing manager remains at 50,000 while a new manager uses the reloaded 10,000 limit.
- The focused Rust harness keeps one manager alive across the configuration reload and compares it with a new manager.
Compaction reload harness manifest
- The manifest defines the isolated executable used to exercise the reload behavior.
- The runner executes and records both the initial and reloaded configuration cases.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-base/src/compaction.rs
Line: 245
Comment:
**Refresh Compaction Cap**
When `[compaction] max_context_tokens` changes during a running session, the existing compaction manager continues to use the value captured when it was created. After the global configuration reloads from 50,000 to 10,000, calling `set_budget(1_000_000)` still permits 50,000 tokens for that session, while a newly created manager uses 10,000. This can leave a long-running session 40,000 tokens above an operator's newly lowered context and cost limit.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.…f for Code Assist
Code Assist (cloudcode-pa) throttles per client identity: with the same OAuth
token, project and body, 'jcode/1.0 (gemini)' hit 429 RATE_LIMIT_EXCEEDED after
1-2 requests while the official GeminiCLI-tui UA ran unlimited (A/B, two rounds,
order swapped). Send the CLI UA and x-goog-api-client; JCODE_GEMINI_USER_AGENT
overrides.
Code Assist also rejects a root anyOf beside type/properties ('specified other
fields alongside any_of'), which the swarm tool's spawn/non-spawn branches use.
Enable flatten_top_level_combiners for the GEMINI dialect (nested anyOf is fine).
… the route gemini will take An API key in gemini.env silently won over the Code Assist subscription and there was no config knob to stop it, only JCODE_GEMINI_FORCE_OAUTH. Add gemini_force_oauth to [provider] and mirror it into that env var from apply_env_overrides (env wins), the same pattern as copilot_premium. auth::gemini::force_oauth()/uses_api_key() are the single source of truth; the runtime, auth status and auth-test credential_probe now agree, so 'jcode auth-test --provider gemini' reports the route actually used. Also: register JCODE_WAKE_MODE in CONFIG_ENV_KEYS (fingerprint test was already failing on this branch) and update the swarm anyOf test for the Code Assist root-combiner flattening from d143f3e.
…UD_PROJECT Workspace accounts fail every turn with 'requires setting GOOGLE_CLOUD_PROJECT' when the env var is missing, and env-only config (launchctl setenv, .zshrc) does not reach processes started from the menubar or shells older than the setenv. Add gemini_project to [provider] and mirror it into the env from apply_env_overrides (env wins), same pattern as gemini_force_oauth. Verified: env -u GOOGLE_CLOUD_PROJECT jcode auth-test --provider gemini PASS.
| if let Ok(v) = std::env::var("JCODE_GEMINI_FORCE_OAUTH") { | ||
| self.provider.gemini_force_oauth = parse_env_bool(&v).unwrap_or(false); | ||
| } else if self.provider.gemini_force_oauth { | ||
| crate::env::set_var("JCODE_GEMINI_FORCE_OAUTH", "1"); | ||
| } | ||
|
|
||
| // Gemini Code Assist project: same pattern. The runtime only reads | ||
| // GOOGLE_CLOUD_PROJECT, and env-only config (launchctl setenv, .zshrc) | ||
| // does not reach processes started from the menubar or older shells. | ||
| if let Ok(v) = std::env::var("GOOGLE_CLOUD_PROJECT") { | ||
| let v = v.trim(); | ||
| if !v.is_empty() { | ||
| self.provider.gemini_project = Some(v.to_string()); | ||
| } | ||
| } else if let Some(project) = self | ||
| .provider | ||
| .gemini_project | ||
| .as_deref() | ||
| .map(str::trim) | ||
| .filter(|p| !p.is_empty()) | ||
| { | ||
| crate::env::set_var("GOOGLE_CLOUD_PROJECT", project); |
There was a problem hiding this comment.
Loading gemini_force_oauth = true or a Gemini project writes those values into the process environment. Every later reload then treats the values written by the earlier load as external overrides: changing OAuth to false, changing the project, or removing it has no effect. Long-running processes therefore continue using stale authentication and billing-project settings until restarted.
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-base/src/config/env_overrides.rs
Line: 830-851
Comment:
**Avoid Sticky Gemini Overrides**
Loading `gemini_force_oauth = true` or a Gemini project writes those values into the process environment. Every later reload then treats the values written by the earlier load as external overrides: changing OAuth to `false`, changing the project, or removing it has no effect. Long-running processes therefore continue using stale authentication and billing-project settings until restarted.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| // Code Assist rejects a root `anyOf` when `type`/`properties` sit | ||
| // beside it ("specified other fields alongside any_of"), which is how | ||
| // the `swarm` tool expresses its spawn/non-spawn branches. Nested | ||
| // `anyOf` inside a property is fine, so only the root is widened. | ||
| flatten_top_level_combiners: true, | ||
| prune_dangling_required: true, | ||
| const_as_enum: true, | ||
| one_of_as_any_of: true, |
There was a problem hiding this comment.
Flattening Gemini's top-level anyOf removes the branch that requires label when action is spawn, while retaining spawn as an allowed action. Gemini can consequently emit a schema-valid {"action":"spawn","prompt":"review the API"} call that jcode rejects locally for its missing label. The requested worker is not created, and the user loses a tool round-trip.
Artifacts
- Executed the existing raw-schema test and captured its passing output, establishing that the unnormalized spawn branch requires a nonblank label.
- Executed the focused probe against the real Gemini dialect and swarm execution path; it shows the advertised requirement is action-only and the unlabeled spawn is rejected, confirming the mismatch.
- Captured the authored test source that generated the focused schema-normalization and runtime-execution evidence.
Ran code and verified through T-Rex
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-schema-dialect/src/registry.rs
Line: 93-100
Comment:
**Require Spawn Labels**
Flattening Gemini's top-level `anyOf` removes the branch that requires `label` when `action` is `spawn`, while retaining `spawn` as an allowed action. Gemini can consequently emit a schema-valid `{"action":"spawn","prompt":"review the API"}` call that jcode rejects locally for its missing label. The requested worker is not created, and the user loses a tool round-trip.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Comments Outside DiffThese findings sit on lines the diff does not cover, so they could not be posted inline. Each one leaves this list once its file changes.
|
|
Merged into master via reviewed integration 8567dfa. Preserved thought signatures and provider/session identity fixes, but corrected review findings: config no longer exports sticky process environment, auth state is keyed by route/project, compaction caps refresh safely, and HTTP retries classify transient versus persistent quota errors and respect bounded Retry-After. Cursor compaction remains disabled because its summary path truncates input. Combined config/compaction tests, 146 provider/message/schema tests, local HTTP 429->503->200 replay, actual cache replacement, and a two-turn agent signature/session-ID test passed. No live Google account acceptance was possible, so this does not certify upstream credentials/quota behavior. Integrated with original PR ancestry and contributor attribution preserved, applying only the actual contribution rather than the divergent old upstream tree. Validation is local targeted testing and isolated public-interface acceptance, not a claim that hosted CI is green. |
Three fixes on top of v0.84.0 for Gemini "works on turn 1, fails on turn 2":
turn_streaming_mpsc.rs,turn.rs). The signature was captured fromStreamEvent::ToolUseSignaturebut the persistedContentBlock::ToolUsewas hand-built withthought_signature: Nonein the mpsc/TUI paths (onlyturn_loops.rscarried it). Turn 2 replayed an unsigned history and Gemini 3.x returned HTTP 400Function call is missing a thought_signature. Construction now goes throughToolCall::to_tool_use_block(). Three regression tests injcode-message-types; falsified by restoringNone(2 of 3 fail).supports_compaction() == falseskipped even the emergency hard-compact).Verified on a daemon-hosted mpsc session: two sequential tool calls on
gemini-2.5-pro(server-upgraded togemini-3.1-pro-preview), persisted history carriesthought_signatureon every tool call, zerostream_error.Refs #518 (shared thought-signature persistence). This reference does not claim live-provider acceptance or close that issue.