Swap: lock journals to one session; record the base file in the header

- open_ctx takes a non-blocking flock on the journal. A journal locked
  by another kte process is left alone: this session reports it once
  and does not write (records from two sessions used to interleave in
  one file). Opening a file whose journal is live in another session
  skips recovery instead of replaying or removing that journal.
- The header records the size and mtime of the file the journal's
  records apply to (flags bit 0, bytes 24..39; previously reserved, so
  no version change). Before offering recovery, a journal whose base no
  longer matches the file on disk (changed after a crash, or the crash
  fell between a save's rename and the journal reset) is treated as
  stale and offered for deletion, instead of replaying its edits onto
  different text. Journals from older kte (flag clear) replay as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
Claude
2026-09-24 09:16:04 -07:00
committed by Kyle Isom
parent 7e16680a26
commit cb9b6bdf45
5 changed files with 202 additions and 9 deletions
+25
View File
@@ -512,6 +512,31 @@ Editor::ProcessPendingOpens()
} catch (...) {
swp_exists = false;
}
if (swp_exists && kte::SwapManager::JournalInUse(swp)) {
// Another running kte is editing this file: its journal is live.
// Open without recovery and leave the journal alone; this
// session will not journal the file.
std::string err;
if (!OpenFile(req.path, err)) {
SetStatus(err);
continue;
}
apply_pending_line(*this, req.line1);
SetStatus(req.path + " is open in another kte; this copy has no crash recovery");
return true;
}
if (swp_exists && !kte::SwapManager::JournalMatchesFile(swp, req.path)) {
// The file changed (or was removed) after the journal started, so
// its position-based edits no longer apply.
pending_recovery_prompt_ = RecoveryPromptKind::DeleteCorruptSwap;
pending_recovery_open_ = req;
pending_recovery_swap_path_ = swp;
pending_recovery_replay_err_ = "stale";
StartPrompt(PromptKind::Confirm, "Swap", "");
SetStatus("Swap file for " + req.path +
" predates changes to the file on disk and cannot be applied. Delete it? (y/N, C-g cancel)");
return opened_any;
}
if (swp_exists) {
Buffer tmp;
std::string oerr;