Skip to content
Back to skills

465 Context Loss Bug 5e2e50c1

ASecurity

Messages are only saved after successful completion of the agent loop. Any error, abort, or cancellation causes ALL messages to be lost, even if the conversation ran for many iterations.

  • 9 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 1, 2026
ai-agentspythonreactnode

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned June 1, 2026

npx -y skills add tools-only/X-Skills --skill 465-context-loss-bug_5e2e50c1 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 465 Context Loss Bug 5e2e50c1?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 465 Context Loss Bug 5e2e50c1
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-465-context-loss-bug-5e2e50c1/badge)](https://www.skillsdirectory.com/skills/tools-only-465-context-loss-bug-5e2e50c1)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Bug: Messages Not Persisted on Error/Abort

## Summary

Messages are only saved after successful completion of the agent loop. Any error, abort, or cancellation causes ALL messages to be lost, even if the conversation ran for many iterations.

## Root Cause

**Location:** `src/tunacode/core/agents/main.py:417-430`

`_persist_run_messages()` is called at line 417 ONLY after the iteration loop completes successfully. The `except` block at lines 422-430 does NOT persist messages - it only cleans up dangling tool calls.

```python
try:
    async with agent.iter(...) as agent_run:
        for node in agent_run:
            # iterations happen, usage/react updated

        # ONLY RUNS IF NO EXCEPTION
        self._persist_run_messages(agent_run, baseline_message_count)

except (UserAbortError, asyncio.CancelledError):
    # Messages NOT persisted here!
    _remove_dangling_tool_calls(...)
    raise
```

## Impact

- User can have a 20-minute conversation
- Error happens on iteration 10
- All 10 iterations of messages = lost
- Session shows activity (tokens, react entries, cost) but empty messages

## Evidence

Found 5+ sessions on disk with:
- `messages: []` (empty)
- `prompt_tokens: 415,708` (hundreds of thousands)
- `react_scratchpad.timeline: 8 entries` (iterations ran)
- `cost: $0.06` (money charged)

Example session: `951a0700590b351a_f62729e2-57d5-42b2-bcc2-864a2155a85b.json`

## Secondary Issue

**Location:** `src/tunacode/core/state.py:271-273`

```python
with suppress(TypeError, ValueError, AttributeError):
    result.append(msg_adapter.dump_python(msg, mode="json"))
```

Messages that fail to serialize are silently dropped. Violates "fail fast, fail loud" principle.

## Proposed Fix

Persist messages in the `except` block before re-raising:

```python
except (UserAbortError, asyncio.CancelledError):
    # Persist whatever messages we have so far
    try:
        run_messages = list(agent_run.all_messages())
        self.state_manager.session.messages = run_messages
    except Exception:
        pass  # best effort

    # Clean up dangling tool calls
    cleanup_applied = _remove_dangling_tool_calls(...)
    raise
```

## Files to Modify

1. `src/tunacode/core/agents/main.py` - Add message persistence in except block
2. `src/tunacode/core/state.py` - Consider logging serialization failures instead of silent suppress

## Related

- Gate 6: Exception Paths Are First-Class (CLAUDE.md)
- PR #246: Dangling tool calls bug (similar exception-path issue)

Files in this skill

  • README.md1004 B
  • skill.md2.4 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.