Skip to content

Data-loss: session close path can wipe a large transcript (fix + tests available on fork) #814

Closed

Description

@dwillitzer

Summary

A close/clear path that swaps in a fresh 0-message session and saves with status=Closed triggers save()'s shrink detection (messages.len() < persist_state.messages_len) and checkpoints a full snapshot — overwriting the real transcript snapshot with an empty stub and deleting the journal. The later .bak rotation then captures the already-wiped state, so both copies end up empty and --resume shows an empty session.

Hit this in production 2026-08-05 on a 12-hour session (686 messages, 1.5MB snapshot). Daemon log forensics:

10:55:06  SESSION_PERSISTENCE save_mode=append    messages=686  snapshot_bytes=1,539,899
10:55:38  SESSION_PERSISTENCE save_mode=snapshot  messages=0    snapshot_bytes=600  status=Closed   <- wipe
10:55:41  SESSION_PERSISTENCE load_done           messages=0                                        <- resume sees nothing

Fix (ready on my fork — PR creation appears restricted on this repo)

Branch: https://github.com/dwillitzer/jcode/tree/fix/session-pre-wipe-guard (single commit, cherry-picks cleanly onto origin/master)

checkpoint_snapshot() gains a guard_snapshot_shrink() pre-step: when the in-memory transcript is empty but the on-disk snapshot exceeds 4KB, snapshot + journal are first copied to timestamped .pre-wipe-<ts>.json.bak / .jsonl.bak siblings.

  • Never blocks the save — legitimate compaction shrinks are unaffected (guard requires empty; metadata stubs are under threshold)
  • Backup names avoid *.json so session scanners don't surface phantoms
  • Emits SESSION_PERSISTENCE phase=pre_wipe_backup for observability; backup failure logs and proceeds (fail-open)
  • Recovery: cp session_X.pre-wipe-<ts>.json.bak session_X.json

Includes two regression tests (wipe path produces byte-identical restorable backup with full recovery round-trip; small/compaction checkpoints produce no backups).

Related fixes on the same fork, happy to PR any/all if you enable PRs or prefer patches:

Branch What
fix/swarm-schema-anyof Top-level anyOf in the swarm tool schema breaks strict providers (Anthropic API rejects it); flattened to a plain object schema
fix/swarm-worker-provider-pin Swarm worker spawns drop the openai-compatible:<profile> prefix, so workers lose the auth profile and fail; pins the full prefix through spawn (with tests)
feat/prompt-cache-override supports_prompt_cache config override so openai-compatible profiles fronting Anthropic models emit cache_control breakpoints (verified ~92% cached-token hit rate through a claude-family proxy)
fix/mcp-tool-name-sanitize MCP tool names with characters outside [a-zA-Z0-9_-] are rejected by provider APIs; sanitizes at registration. Also prefers configured judge models over hardcoded defaults

All branches are single-purpose, based on current master (02439b4), and cherry-pick cleanly. If you'd rather receive these differently (git bundles, mailed patches, enabling PRs), say the word.

Activity

  1. added
    bug
    triage: needs-decision
    autonomous: no
    priority: critical
    on Aug 6, 2026
  2. 1jehuang commented on Aug 6, 2026

    @1jehuang
    Owner

    Thanks for the detailed report. Because this can cause data loss, it needs a focused reproduction and an audit of snapshot/journal atomicity and close ordering before a fix is merged. Recovery and interrupted-close tests should be required as part of the fix.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  3. added a commit that references this issue on Aug 6, 2026
    a4972b3
  4. 1jehuang commented on Aug 6, 2026

    @1jehuang
    Owner

    The immediate safety fix is now in PR #820. Empty checkpoints can no longer replace an existing non-empty persisted transcript. Before refusing the save, snapshots larger than 4 KiB and their journals are copied to timestamped pre-wipe recovery files. Normal non-empty compaction remains allowed.

    Verification: three focused persistence tests pass, covering byte-identical snapshot/journal recovery, destructive-checkpoint refusal, normal non-empty shrink, and small metadata stubs.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  5. added
    triage: fixed-pending-release
    and removed
    triage: needs-decision
    autonomous: no
    on Aug 6, 2026
  6. 1jehuang commented on Aug 6, 2026

    @1jehuang
    Owner

    Additional end-to-end verification passed against the packaged binary and a dedicated isolated daemon. A real non-empty session was created and closed, then resumed by ID. The resumed model returned the exact marker from the original turn (TRANSCRIPT_GUARD_814), confirming that the transcript survived the close/resume boundary.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  7. added a commit that references this issue on Aug 8, 2026
    35e9e4b
  8. added a commit that references this issue on Aug 15, 2026
    a8e0afd
  9. added a commit that references this issue on Aug 30, 2026
    3c7d13f
  10. 15 remaining items

  11. added 11 commits that reference this issue on Sep 3, 2026
    417eb35
    0059ccd
    66f1ede
    c626f81
    7510c3c
    d8b6bab
    92e9998
    3a4169a
    8771590
    2b823fd
    9959e9b
  12. added 2 commits that reference this issue on Sep 18, 2026
    51460b6
    b7b5977
  13. added 2 commits that reference this issue on Oct 9, 2026
    587b086
    e2e161b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: criticaltriage: fixed-pending-release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions