Skip to content

Comments

fix: repair unpaired tool calls when loading sessions#3707

Closed
bheemreddy181 wants to merge 2 commits intoopenclaw:mainfrom
bheemreddy181:fix/repair-sessions-on-load
Closed

fix: repair unpaired tool calls when loading sessions#3707
bheemreddy181 wants to merge 2 commits intoopenclaw:mainfrom
bheemreddy181:fix/repair-sessions-on-load

Conversation

@bheemreddy181
Copy link

@bheemreddy181 bheemreddy181 commented Jan 29, 2026

When a session is loaded with assistant tool calls that never received results (e.g., due to exec timeout, crash, or interruption), the session would become stuck and unresponsive.

This fix adds repair logic during session initialization that:

  • Detects unpaired tool calls in the loaded session history
  • Injects synthetic error tool results for missing results
  • Marks the session as unflushed so repairs are persisted

This ensures sessions can always recover from incomplete tool executions instead of remaining permanently stuck.

Fixes group chat sessions getting stuck when exec commands time out.

Greptile Overview

Greptile Summary

This PR makes session loading more resilient by repairing incomplete tool execution transcripts during SessionManager initialization. It detects assistant tool calls that never received matching tool results and injects synthetic error toolResult messages so strict providers don’t reject the transcript; it also marks the session as unflushed so the repaired transcript is persisted. Additionally, it includes SwiftFormat-driven formatting updates across several macOS CLI/app files.

Confidence Score: 4/5

  • This PR appears safe to merge; changes are localized and include tests for the new transcript repair behavior.
  • The core repair logic delegates to existing repairToolUseResultPairing and is exercised by a new Vitest suite. The main remaining concern is type-safety around the any cast when passing session messages into the repair function, which could hide unexpected message shapes at runtime, but it’s unlikely to break existing behavior.
  • src/agents/pi-embedded-runner/session-manager-init.ts (type-safety around message casting); macOS CLI help text alignment if output formatting matters.

(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!

@openclaw-barnacle openclaw-barnacle bot added channel: discord Channel integration: discord agents Agent runtime and tooling labels Jan 29, 2026
@bheemreddy181 bheemreddy181 force-pushed the fix/repair-sessions-on-load branch from beb5a5b to 5d3b90d Compare January 29, 2026 02:44
@openclaw-barnacle openclaw-barnacle bot removed the channel: discord Channel integration: discord label Jan 29, 2026
@bheemreddy181 bheemreddy181 force-pushed the fix/repair-sessions-on-load branch from 5d3b90d to 912909b Compare January 29, 2026 05:23
When a session is loaded with assistant tool calls that never received
results (e.g., due to exec timeout, crash, or interruption), the session
would become stuck and unresponsive.

This fix adds repair logic during session initialization that:
- Detects unpaired tool calls in the loaded session history
- Injects synthetic error tool results for missing results
- Marks the session as unflushed so repairs are persisted

This ensures sessions can always recover from incomplete tool executions
instead of remaining permanently stuck.

Fixes group chat sessions getting stuck when exec commands time out.
@bheemreddy181 bheemreddy181 force-pushed the fix/repair-sessions-on-load branch from 912909b to 0e4ae1b Compare January 29, 2026 21:14
@openclaw-barnacle openclaw-barnacle bot added the app: macos App: macos label Jan 31, 2026
- Sort imports alphabetically (Foundation before framework imports)
- Fix indentation and remove trailing spaces in help text
- Simplify redundant property definitions
- Wrap single-line property bodies onto multiple lines
- Use if expressions for conditional assignments
- Convert comment to doc comment for API declarations
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile


if (messages.length === 0) return;

const report = repairToolUseResultPairing(messages as any);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Avoid any cast when repairing messages

repairToolUseResultPairing expects AgentMessage[], but messages is currently inferred as NonNullable<SessionMessageEntry["message"]>[] and is cast to any (session-manager-init.ts:86). This loses type safety right at the boundary where we’re mutating transcripts, and can hide shape mismatches until runtime.

Consider narrowing SessionMessageEntry["message"] to the concrete AgentMessage type (or validating/casting each message to AgentMessage) so this call can be typed without any.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/session-manager-init.ts
Line: 86:86

Comment:
[P2] Avoid `any` cast when repairing messages

`repairToolUseResultPairing` expects `AgentMessage[]`, but `messages` is currently inferred as `NonNullable<SessionMessageEntry["message"]>[]` and is cast to `any` (`session-manager-init.ts:86`). This loses type safety right at the boundary where we’re mutating transcripts, and can hide shape mismatches until runtime.

Consider narrowing `SessionMessageEntry["message"]` to the concrete `AgentMessage` type (or validating/casting each message to `AgentMessage`) so this call can be typed without `any`.

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 3, 2026

Additional Comments (1)

apps/macos/Sources/MoltbotMacCLI/ConnectCommand.swift
[P3] Help output indentation is inconsistent

The help text indentation was increased for the header/usage/options lines, but the option lines remain less indented, so the output is visually misaligned (e.g. ConnectCommand.swift:101-113). Consider making the --url/--token/... lines match the indentation level used for Options: (and similarly in the other CLI commands). Also appears in apps/macos/Sources/MoltbotMacCLI/DiscoverCommand.swift:58-68 and apps/macos/Sources/MoltbotMacCLI/WizardCommand.swift:71-81.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/macos/Sources/MoltbotMacCLI/ConnectCommand.swift
Line: 101:103

Comment:
[P3] Help output indentation is inconsistent

The help text indentation was increased for the header/usage/options lines, but the option lines remain less indented, so the output is visually misaligned (e.g. `ConnectCommand.swift:101-113`). Consider making the `--url/--token/...` lines match the indentation level used for `Options:` (and similarly in the other CLI commands). Also appears in `apps/macos/Sources/MoltbotMacCLI/DiscoverCommand.swift:58-68` and `apps/macos/Sources/MoltbotMacCLI/WizardCommand.swift:71-81`.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@bheemreddy181 bheemreddy181 deleted the fix/repair-sessions-on-load branch February 7, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling app: macos App: macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant