Repository navigation
fix: prevent phantom toolResult from corrupting sessions - #39353
alvinttang wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 223e032e62
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (stopReason === "toolUse") { | ||
| const corrected = { ...assistant, stopReason: "stop" }; | ||
| out.push(corrected as AgentMessage); |
There was a problem hiding this comment.
Mark stopReason correction as a transcript change
When this branch rewrites an assistant message from stopReason: "toolUse" to "stop", changed is never set, so repairToolUseResultPairing can return the original messages array at the end (changedOrMoved stays false). In the exact scenario this commit targets (assistant has toolUse stop reason but no tool-call blocks), the correction is dropped and downstream code still sees the uncorrected transcript, so phantom tool-result generation is not actually prevented.
Useful? React with 👍 / 👎.
Greptile SummaryThis PR bundles two independent fixes: (1) correcting a phantom
Confidence Score: 2/5
Last reviewed commit: 223e032 |
| if (stopReason === "toolUse") { | ||
| const corrected = { ...assistant, stopReason: "stop" }; | ||
| out.push(corrected as AgentMessage); |
There was a problem hiding this comment.
changed flag not set — correction is silently discarded
When stopReason === "toolUse" with no tool calls, the code builds a corrected message and pushes it to out, but never sets changed = true. The function's return path at the end checks changedOrMoved ? out : messages — so if this stopReason correction is the only mutation in the entire repair pass, changed remains false, and the function returns the original messages array rather than out. The correction is silently dropped.
| if (stopReason === "toolUse") { | |
| const corrected = { ...assistant, stopReason: "stop" }; | |
| out.push(corrected as AgentMessage); | |
| if (stopReason === "toolUse") { | |
| const corrected = { ...assistant, stopReason: "stop" }; | |
| changed = true; | |
| out.push(corrected as AgentMessage); |
This means sessions where the phantom toolUse stopReason is the sole problem would continue to be corrupted even after the repair is applied, defeating the purpose of the fix.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/session-transcript-repair.ts
Line: 412-414
Comment:
**`changed` flag not set — correction is silently discarded**
When `stopReason === "toolUse"` with no tool calls, the code builds a `corrected` message and pushes it to `out`, but never sets `changed = true`. The function's return path at the end checks `changedOrMoved ? out : messages` — so if this stopReason correction is the **only** mutation in the entire repair pass, `changed` remains `false`, and the function returns the original `messages` array rather than `out`. The correction is silently dropped.
```suggestion
if (stopReason === "toolUse") {
const corrected = { ...assistant, stopReason: "stop" };
changed = true;
out.push(corrected as AgentMessage);
```
This means sessions where the phantom `toolUse` stopReason is the sole problem would continue to be corrupted even after the repair is applied, defeating the purpose of the fix.
How can I resolve this? If you propose a fix, please make it concise.| if ( | ||
| typeof currentApiKey === "string" && | ||
| currentApiKey.trim() && | ||
| !ENV_VAR_NAME_RE.test(currentApiKey.trim()) | ||
| ) { | ||
| const envVarName = resolveEnvApiKeyVarName(normalizedKey); | ||
| if (envVarName && process.env[envVarName] === currentApiKey) { | ||
| mutated = true; | ||
| normalizedProvider = { ...normalizedProvider, apiKey: envVarName }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Consider collision risk for short/common API key values
The reverse-lookup guard process.env[envVarName] === currentApiKey is a strict equality check against the resolved env var value. If the env var's value is a short or common string (e.g., a test key like "test" or "none"), this could produce a false positive match, silently replacing a legitimate literal API key with the env var name in the persisted config.
A comment noting this assumption — i.e., that real secret values are long enough to make accidental collision negligible — would help future maintainers understand why a length or entropy guard was deliberately omitted.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/models-config.providers.ts
Line: 528-538
Comment:
**Consider collision risk for short/common API key values**
The reverse-lookup guard `process.env[envVarName] === currentApiKey` is a strict equality check against the resolved env var value. If the env var's value is a short or common string (e.g., a test key like `"test"` or `"none"`), this could produce a false positive match, silently replacing a legitimate literal API key with the env var name in the persisted config.
A comment noting this assumption — i.e., that real secret values are long enough to make accidental collision negligible — would help future maintainers understand why a length or entropy guard was deliberately omitted.
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Acknowledged. The collision risk is negligible for real API keys (which are typically 40+ character random strings), but adding a brief comment noting this assumption is a good idea for future maintainers. Will add that alongside the fix.
|
Fixed — added the missing |
…claw#38757) When apiKey values are resolved from env var substitution by loadConfig(), the plaintext secret was being persisted to agents/*/agent/models.json. Add a reverse-lookup in normalizeProviders() that detects when an apiKey matches a known provider env var's value and replaces it with the env var name, preventing secrets from being written to disk.
When a fallback model returns stopReason 'toolUse' but the response content contains no actual toolCall blocks (only thinking + text), the uncorrected stopReason causes downstream repair code to create phantom toolResult entries with empty toolCallId and toolName. These phantom entries permanently corrupt the session, causing all subsequent requests to fail with tool pairing errors. Detect the mismatch between stopReason='toolUse' and empty toolCalls in repairToolUseResultPairing, and correct stopReason to 'stop' so no synthetic toolResults are generated. Fixes openclaw#21985 Co-Authored-By: Claude Opus 4.6 <[email protected]>
7236491 to
5828919
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 58289194e2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // var name), check whether it matches the canonical env var for this provider. | ||
| // This prevents resolveConfigEnvVars()-resolved secrets from being persisted | ||
| // to models.json as plaintext. (Fixes #38757) | ||
| const currentApiKey = normalizedProvider.apiKey; |
There was a problem hiding this comment.
Remove duplicate apiKey reverse-lookup block
normalizeProviders now contains two consecutive copies of the reverse-lookup snippet, and this second copy redeclares const currentApiKey in the same scope. That causes a duplicate identifier parse/type-check error, so the agents config module cannot compile/load until one copy is removed.
Useful? React with 👍 / 👎.
|
Thanks for catching the duplicate |
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing this as implemented after Codex review. Current What I checked:
So I’m closing this as already implemented rather than keeping a duplicate issue open. Review notes: reviewed against cb2c36b04975; fix evidence: commit 7d4718373698. |
Summary
toolResultentries with emptytoolCallIdstopReason: "toolUse"but the response content has NO actualtoolCallblocks (only thinking + text), downstream repair creates phantomtoolResultentries that permanently corrupt the sessionrepairToolUseResultPairingand correctstopReasonto"stop"so no synthetictoolResultentries are generatedFixes #21985
Test plan
stopReason: "toolUse"and no toolCall blocks no longer create phantom toolResults🤖 Generated with Claude Code