Skip to content

fix(import): save messages and checkpoints atomically - #16420

Closed
hsusul wants to merge 2 commits into
unslothai:mainfrom
hsusul:fix/external-import-recovery
Closed

hsusul wants to merge 2 commits into
unslothai:mainfrom
hsusul:fix/external-import-recovery

Conversation

@hsusul

@hsusul hsusul commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Why

Retrying a failed external-chat import can permanently skip later source turns. Message rows committed before their progress checkpoint, leaving a state the importer cannot safely replay.

What changed

  • Pass a named import checkpoint into the existing message-sync transaction and write it before commit.
  • Reuse the ledger UPSERT in the checkpoint-only path, preserving revision and maximum-turn behavior.
  • Add real SQLite-failure regressions for initial and incremental imports, retries, Studio edits/deletions and parent links.

Scope

This prevents new partial message/checkpoint commits. It does not infer historical missing checkpoints from edited or deleted rows. Thread/project metadata keeps its existing separate write path.

Tradeoffs

An optional checkpoint on message sync avoids extracting its large transaction body. Imports with no message changes retain their existing checkpoint-only write.

Blast Radius

Other message-sync callers omit the optional checkpoint and keep their behavior. No schema migration or shared connection state is added.

Verification

  • test_external_import_atomic.py: 2 failed, 1 passed on current main; 3 passed with the fix. Reverting importer integration or splitting the storage transaction reproduces both failures.
  • The public importer recovery reproduction now retains both source turns, then imports zero on repeat.
  • Storage, import, archive, generation and research suites: 1,043 passed, with one existing deprecation warning. Formatter, Ruff and diff checks pass.

Closes #16419.

@danielhanchen

Copy link
Copy Markdown
Member

Review record for #16420, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#680.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror f551090458 (this PR's 1041e032f)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: f551090458

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror f551090458 (this PR's 1041e032f).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen

Copy link
Copy Markdown
Member

Further recovery testing found a regression after deleting and reimporting a chat. Import one turn, delete the last chat, then interrupt its reimport with a failing checkpoint write: the message rolls back, but the deleted chat's old checkpoint remains. After appending a second turn, retry stores only second; the same sequence on the base stores first and second.

The correction clears an obsolete checkpoint in the same transaction that recreates a missing thread. It preserves checkpoints and intentional message deletions in surviving threads. The new regression fails on this PR and passes with the correction; the broader storage/import/archive/research/generation selection passes all 590 tests without skips.

Direct maintainer pushes and a follow-up PR into this branch were both refused. I preserved your commits in #16458 and added the correction there. The correction alone is commit 2dd439b3f, which can also be cherry-picked into this branch.

rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Nov 2, 2026
…slothai#16458)

Preserve imported messages and their progress checkpoint in one SQLite transaction, and discard a deleted chat's stale checkpoint atomically when recreating its thread. Retries recover every source turn while retaining edits and message deletions in surviving threads.

Includes the original atomic-import work from unslothai#16420 and the deleted-chat recovery correction.

Fixes unslothai#16419.

Co-authored-by: Henry Su <[email protected]>
@danielhanchen

Copy link
Copy Markdown
Member

The atomic-import work is merged in #16458, with your original contribution credited and the additional deleted-chat checkpoint recovery fix included. Closing this PR as superseded so the incomplete version is not merged separately. Thank you for the original fix and reproducer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Failed external-chat imports can skip newly appended turns

2 participants