Bug 508506 - Improve autosave/stashing for unsaved documents to prevent data loss
Summary: Improve autosave/stashing for unsaved documents to prevent data loss
Status: REPORTED
Alias: None
Product: kate
Classification: Applications
Component: sessions (other bugs)
Version First Reported In: 25.08.0
Platform: Ubuntu Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-08-20 08:24 UTC by Jaypers
Modified: 2025-08-30 17:24 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jaypers 2025-08-20 08:24:12 UTC
SUMMARY
Currently, stashed and swap files for unsaved documents in Kate are only updated under specific conditions:
  -  When a new document tab is opened
  -  When a document tab is closed
  -  When the session is manually saved

This behaviour leads to potential data loss if Kate or the system crashes, because unsaved files do not have continuously updated swap/stash data like normal saved files do.

STEPS TO REPRODUCE
 1. In Settings -> Open/Save -> Advanced:
     Enable "Swap file mode" and set the "Save swap files every:" to 5s
 2. In Settings -> Session:
     Under "Automatically save and restore:" make sure "Newly-created unsaved files" checkbox is set.
 3. In a terminal:
     watch 'cat ~/.local/share/kate/stash/Default.katesession/Document*'
 4. Create a new file and start typing.
 5. Observe in terminal that "Document 1" has been created.
 6.  Make further updates and observe no change to stash or swap.
 7.  Open new tab and start typing. 
 8.  Observe Document 1, Document 1 swap and Document 2 updated.
 9.  Make changes to Document 1 and observe no change.
10. Close Document 2 and observe Document 1 is updated.
11. Make change to Document 1 and save the session (Sessions -> Save session)
12. Observe Document 1 is updated.

EXPECTED RESULT
One or more of the following could mitigate the issue:
- Periodic session saving: 
  Implement a configurable autosave timer that periodically saves the session state, including stashed documents.
- CLI argument:
  Add a flag such as "kate --save-session" to allow external tools (cron/systemd/timers) to trigger session saving.
- Improve stash/swap handling: 
  Make unsaved stashed documents behave like normally saved files, i.e. continuously updated in the background, so that crashes do not cause data loss.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Kubuntu 24.04.3
KDE Plasma Version: 5.27.12
KDE Frameworks Version: 5.115.0
Qt Version: 5.15.13
Kate Version: 25.08.0 (Snap install)

ADDITIONAL INFORMATION
Stashing of  unsaved documents was fixed by:
https://invent.kde.org/utilities/kate/-/merge_requests/1578

and is related to these bugs (amongst others):
https://bugs.kde.org/show_bug.cgi?id=274102 (REOPENED)
https://bugs.kde.org/show_bug.cgi?id=353654 (RESOLVED)
https://bugs.kde.org/show_bug.cgi?id=375594 (RESOLVED)
https://bugs.kde.org/show_bug.cgi?id=475210 (RESOLVED)
https://bugs.kde.org/show_bug.cgi?id=504691 (RESOLVED)
Comment 1 Jaypers 2025-08-20 08:33:05 UTC
I forgot to mention the initial step of saving a session with the name Default. 
Without this none of the session settings will apply. 

As a side note, in settings it's not obvious that a session first needs to be saved/created for it's features to become available. Perhaps a note in settings or popup at first startup could help with this.
Comment 2 Jaypers 2025-08-20 15:12:47 UTC
Note: I was confusing backup files for swap files in my original post. The behaviour for stash files still applies.
Comment 3 Waqar Ahmed 2025-08-30 17:15:37 UTC
Just auto saving every N seconds might be a cheaper and simpler solution. Tracking document edits might be More complex and costly
Comment 4 Christoph Cullmann 2025-08-30 17:22:38 UTC
The question is if we not could use the swap files for that.
Comment 5 Waqar Ahmed 2025-08-30 17:24:10 UTC
That would be the ideal solution.

The current solution is very flaky