Repository navigation
Conversation
Greptile SummaryThis PR fixes a real bug in Key issue found:
Confidence Score: 2/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/turns.ts
Line: 68-69
Comment:
**Test fixtures out of sync with the fix**
The existing test suite in `src/agents/pi-embedded-helpers.validate-turns.test.ts` builds all its assistant-message fixtures using `type: "toolUse"` (e.g. lines 15–16, 371, 392, 421, 469, 471). After this change, the filter predicate only strips blocks whose `type === "toolCall"`, so a block typed `"toolUse"` satisfies `block.type !== "toolCall"` → `true` and is therefore **kept** rather than stripped.
This means several tests that rely on the stripping behaviour will now produce the wrong result:
- `"should strip tool_use blocks without matching tool_result"` — expects the `toolUse` block to be removed; it will be kept instead.
- `"should insert fallback text when all content would be removed"` — same issue; the fallback text path will never be reached.
- `"should handle multiple dangling tool_use blocks"` and `"should handle mixed tool_use with some having matching tool_result"` — both rely on `makeDualToolUseAssistantContent()` which emits `type: "toolUse"` blocks.
The test helpers and fixtures should be updated to use `type: "toolCall"` to match the runtime data produced by pi-agent-core, so the tests actually exercise the fixed code path.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 9d03fca |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9d03fca5df
ℹ️ 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".
| return false; | ||
| } | ||
| if (block.type !== "toolUse") { | ||
| if (block.type !== "toolCall") { |
There was a problem hiding this comment.
Handle Anthropic toolUse blocks when stripping calls
Filtering only toolCall regresses the existing Anthropic toolUse shape that this repo still handles elsewhere (for example src/agents/session-transcript-repair.ts explicitly preserves toolUse.input for Anthropic, and src/agents/pi-embedded-helpers.validate-turns.test.ts covers toolUse assistant blocks). In sessions where assistant content uses toolUse, dangling tool calls are now left in place, so truncated histories can still trigger Anthropic's tool_use ids found without tool_result blocks rejection; the stripping logic should remain compatible with both toolCall and toolUse.
Useful? React with 👍 / 👎.
| if (block.type !== "toolCall") { | ||
| return true; |
There was a problem hiding this comment.
Test fixtures out of sync with the fix
The existing test suite in src/agents/pi-embedded-helpers.validate-turns.test.ts builds all its assistant-message fixtures using type: "toolUse" (e.g. lines 15–16, 371, 392, 421, 469, 471). After this change, the filter predicate only strips blocks whose type === "toolCall", so a block typed "toolUse" satisfies block.type !== "toolCall" → true and is therefore kept rather than stripped.
This means several tests that rely on the stripping behaviour will now produce the wrong result:
"should strip tool_use blocks without matching tool_result"— expects thetoolUseblock to be removed; it will be kept instead."should insert fallback text when all content would be removed"— same issue; the fallback text path will never be reached."should handle multiple dangling tool_use blocks"and"should handle mixed tool_use with some having matching tool_result"— both rely onmakeDualToolUseAssistantContent()which emitstype: "toolUse"blocks.
The test helpers and fixtures should be updated to use type: "toolCall" to match the runtime data produced by pi-agent-core, so the tests actually exercise the fixed code path.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-helpers/turns.ts
Line: 68-69
Comment:
**Test fixtures out of sync with the fix**
The existing test suite in `src/agents/pi-embedded-helpers.validate-turns.test.ts` builds all its assistant-message fixtures using `type: "toolUse"` (e.g. lines 15–16, 371, 392, 421, 469, 471). After this change, the filter predicate only strips blocks whose `type === "toolCall"`, so a block typed `"toolUse"` satisfies `block.type !== "toolCall"` → `true` and is therefore **kept** rather than stripped.
This means several tests that rely on the stripping behaviour will now produce the wrong result:
- `"should strip tool_use blocks without matching tool_result"` — expects the `toolUse` block to be removed; it will be kept instead.
- `"should insert fallback text when all content would be removed"` — same issue; the fallback text path will never be reached.
- `"should handle multiple dangling tool_use blocks"` and `"should handle mixed tool_use with some having matching tool_result"` — both rely on `makeDualToolUseAssistantContent()` which emits `type: "toolUse"` blocks.
The test helpers and fixtures should be updated to use `type: "toolCall"` to match the runtime data produced by pi-agent-core, so the tests actually exercise the fixed code path.
How can I resolve this? If you propose a fix, please make it concise.9d03fca to
d6752e7
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d6752e72c1
ℹ️ 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".
| id === defaultSlotIdForKey("contextEngine") && | ||
| normalizedOwner !== CORE_CONTEXT_ENGINE_OWNER | ||
| ) { | ||
| return { ok: false, existingOwner: CORE_CONTEXT_ENGINE_OWNER }; |
There was a problem hiding this comment.
Allow core to register the default context engine
This new owner gate blocks the default legacy context engine from ever being registered in normal startup: ensureContextEnginesInitialized() still calls registerLegacyContextEngine() (in src/context-engine/legacy.ts), which uses registerContextEngine() and therefore the hardcoded public-sdk owner, so this check returns { ok: false } for the default slot id and the result is ignored. In environments that keep the default plugins.slots.contextEngine unset, resolveContextEngine() then throws that legacy is not registered, breaking agent/compaction flows that rely on the built-in fallback.
Useful? React with 👍 / 👎.
|
We've verified this fix in production. The type mismatch ( After patching this locally (adding This is a critical fix — without it, a single orphaned tool_use from a timeout permanently corrupts the session, causing an infinite retry loop that burns tokens until manual Would appreciate this getting merged. Happy to help with review if needed. |
…oolUses (closes openclaw#41571) pi-agent-core stores tool calls as type "toolCall" internally, but stripDanglingAnthropicToolUses was checking for "toolUse". Since "toolCall" !== "toolUse" is always true, the filter kept all blocks, making the function a no-op. Dangling tool call blocks were never removed, causing "tool_use ids found without tool_result blocks" errors from the Anthropic API after context truncation. Co-authored-by: Copilot <[email protected]>
Align test fixtures with the runtime type produced by pi-agent-core. The existing tests used "toolUse" for assistant content blocks, but pi-agent-core stores them as "toolCall". This ensures tests actually exercise the fixed stripping code path.
|
Closing this PR because the author has more than 10 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit. |
|
Thanks for the production validation, @mike2024-dev — the 164 failures / 10 corruption events data is very helpful in confirming the severity. No worries about the closure — it was just the bot enforcing the active PR limit. I'm cleaning up my queue now and will reopen this PR shortly with the updated test fixtures (as flagged by the Greptile review). Should be back up soon. Saw your #48888 as well — looks like a nice complementary fix for the error/aborted path. Once this one is reopened and merged, yours should be unblocked too. 🦞 |
…d of skipping When repairToolUseResultPairing encounters an assistant message with stopReason === 'error' or 'aborted', it previously skipped the message entirely to avoid creating synthetic tool_results for incomplete tool calls (see openclaw#4597). However, skipping these messages preserves any orphaned tool_use blocks inside them. Anthropic then rejects every subsequent API request with: 'tool_use ids were found without tool_result blocks immediately after: <ID>' This causes permanent session corruption — the same error fires on every retry until a human manually resets the session (see openclaw#48354). The fix: instead of skipping, strip the tool_use/toolCall blocks from error/aborted messages. Text content (if any) is preserved. If the message becomes empty after stripping, a placeholder text block is inserted so the message remains structurally valid. This avoids both failure modes: - Does NOT create synthetic tool_results (preserves openclaw#4597 fix) - Does NOT preserve orphaned tool_use blocks (fixes openclaw#48354) Fixes openclaw#48354 Related: openclaw#4597 openclaw#46866 openclaw#45393
Summary
stripDanglingAnthropicToolUseschecksblock.type !== "toolUse"but pi-agent-core stores tool calls astype: "toolCall". Since"toolCall" !== "toolUse"is always true, the filter keeps all blocks, making the function a no-op."tool_use ids found without tool_result blocks"errors from the Anthropic API. This breaks long-running sessions that hit context limits.stripDanglingAnthropicToolUsesfrom"toolUse"to"toolCall"insrc/agents/pi-embedded-helpers/turns.ts. Also corrected the localAnthropicContentBlocktype union.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
Long-running sessions that hit context truncation will now correctly strip dangling tool call blocks, preventing Anthropic API rejection errors.
Security Impact (required)
Repro + Verification
Environment
Steps
Expected
stripDanglingAnthropicToolUsesremoves orphaned tool_call blocks before sending to APIActual
Evidence
Type check (
pnpm tsgo) passes with no new errors. Format and lint clean. No existing test file forturns.ts— the fix is a one-character type string correction with clear correctness.Human Verification (required)
type: "toolCall"(not"toolUse"); verified openai.ts line 82 checks both types confirming both exist in codebaseAnthropicContentBlocktype union was also updated to matchReview Conversations
Compatibility / Migration
Failure Recovery (if this breaks)
src/agents/pi-embedded-helpers/turns.tsRisks and Mitigations
None