Stop during a session checkpoint replays the journal twice; duplicated tool_use permanently 400s on Anthropic
What happens
A session gets permanently stuck: every Anthropic request fails with
400 messages.N: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_...Retrying and restarting don't help. The history has every tool result. The problem is that a block of messages, tool_use/tool_result pairs included, appears twice, with the same message ids and identical content.
This happens when jcode is stopped (SIGTERM, for example a container stop) in the middle of a session checkpoint. We run jcode as a daemon in containers that are stopped when idle, and hit this in production. It reproduces on v0.90.0.
Cause
A checkpoint takes two steps, and nothing marks the journal as already saved into the snapshot:
checkpoint_snapshotwrites<id>.jsonwith every message (L200), then deletes<id>.journal.jsonl(L202).- The daemon's SIGTERM handler calls
std::process::exit(0)without waiting for an in-flight save, so the process can die between those two steps. - On the next start,
load_from_pathreplays the leftover journal, andapply_journal_entrydoesself.messages.extend(entry.append_messages)with no check for ids already loaded from the snapshot. Every journaled message is appended a second time. The next save writes the duplicates into the snapshot, so they survive every later restart. - The Anthropic formatter can't recover from this:
dedupe_tool_resultskeeps only the first copy of each repeated result.- The dangling-tool_use repair only looks for ids that have no result anywhere in the history. Here every id has one, earlier on.
- So the second copy of each
tool_useis sent with no result right after it.
Steps to reproduce
- Have a session whose journal holds
tool_use/tool_resultpairs. - Simulate the interrupted checkpoint: write the snapshot with the journal's messages already included, and leave the journal on disk. Or SIGTERM the daemon between the snapshot write and the journal delete.
- Start jcode. The session loads with the journal's messages duplicated.
- Send any message with an Anthropic model. You get the 400 above, every time.
Minimal formatter-level repro on v0.90.0. A history of [user text, A1(toolu_1), U1(result 1), A2(toolu_2), U2(result 2)] formats cleanly. The same history with its 4 tool messages repeated (what the journal replay produces) fails:
let stored: Vec<StoredMessage> = serde_json::from_value(session["messages"].clone())?;
let messages: Vec<Message> = stored.iter().map(StoredMessage::to_message).collect();
let api = jcode_provider_anthropic::format_messages(&messages, true);
// clean: 5 messages, every tool_use answered immediately
// duplicated: 9 stored -> 7 formatted; messages.5 has toolu_1 and toolu_2
// with no tool_result immediately afterIn our production case, 64 journaled messages were duplicated, which left 18 orphaned tool_use ids. Removing the repeated message ids (all byte-identical) made the session valid again.
Expected
An interrupted checkpoint should never duplicate history. Any one of these would prevent it, and they can be combined:
- Idempotent replay: in
apply_journal_entry, skip appended messages whoseidis already in the session. Message ids are unique per session, so replaying a journal that is already in the snapshot does nothing. - Mark the journal as saved: record in the snapshot how much of the journal it already contains (for example a checkpoint counter), and replay only newer entries.
- Graceful SIGTERM: let an in-flight session save finish before
process::exit(0). - Formatter backstop: drop repeated
tool_useids the waydedupe_tool_resultsdrops repeated results, so a duplicated history can't permanently brick a session.
Related, but a different trigger: #1571. Both have the same symptom: results exist in history, but not right after their tool_use.
Source: 1jehuang/jcode