#1632·jcode

Stop during a session checkpoint replays the journal twice; duplicated tool_use permanently 400s on Anthropic

Author: guyb1Created Oct 1, 2026Updated Oct 1, 2026

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:

  1. checkpoint_snapshot writes <id>.json with every message (L200), then deletes <id>.journal.jsonl (L202).
  2. 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.
  3. On the next start, load_from_path replays the leftover journal, and apply_journal_entry does self.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.
  4. The Anthropic formatter can't recover from this:
    • dedupe_tool_results keeps 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_use is sent with no result right after it.

Steps to reproduce

  1. Have a session whose journal holds tool_use/tool_result pairs.
  2. 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.
  3. Start jcode. The session loads with the journal's messages duplicated.
  4. 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:

rust
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 after

In 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:

  1. Idempotent replay: in apply_journal_entry, skip appended messages whose id is already in the session. Message ids are unique per session, so replaying a journal that is already in the snapshot does nothing.
  2. 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.
  3. Graceful SIGTERM: let an in-flight session save finish before process::exit(0).
  4. Formatter backstop: drop repeated tool_use ids the way dedupe_tool_results drops 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.