You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
safeSaveDocuments setting to atomic save files using a temporary file and rename syscall that atomic move the file on the same filesystem (either the old file or new file exist if the application/OS crashes)
Add unit tests
Testing
The following two comments contain the session RFC and high-level technical design (inspired by vscode workspaces).
Reacted by Ran Luo, Gurran, Stephen Karl Larroque, Alex Cicovic, brainchild0, Roman Kornev, Vladimir, CoreJa and bogosorter
A session contains a persistent state of the window layout, editor configuration and file information:
editor/window state
layout configuration
opened files/folders
unsaved tabs
unsaved changes
tab/file configuration like encoding and line ending
At a later stage we can also save the cursor position, file history and side bar states like opened folders in tree view etc.
MarkText runs as a single-instance and each window is associated with a unique session. Extensions and MarkText can save further files in the session or global, accessible for all sessions. MarkText allows to spawn multiple instances if the instance has a different application data directory than all started instances.
All active sessions are restored after a forced shutdown, crash or similar (hereinafter as "forced restart") but configuration/file changes may be lost up to a certain point. The last active session (= last active window with associated session) is loaded after closing MarkText gracefully load. Sessions are saved in the application data directory that can be changed with --user-data-dir <dir> and must be writable.
Unsaved changes
Unsaved files/tabs are automatically saved after some time/when closing the window and restored during application start when using a persistent session or workspace. A notification is display if a file has been changed externally after saving unsaved changes but the file is restored as usual.
Automatic restore
Sessions are loaded/restored during application start. Restore of unsaved changes or sessions can be disabled by setting restoreChangesOnStart and/or restoreSessionOnStart to false (= command-line flag --nosession). If restoreChangesOnStart is set to true but not restoreSessionOnStart a temporary session is used for each window until application shutdown. If both are set to false neither sessions nor backups are created (but may a in-memory session is used?).
Persistent session
Persistent sessions are used by MarkText and identified by a hashed normalized directory path using MD5-Hex. A lower-case directory path is used on macOS and Windows because the filesystem API is case-insensitive by default (normally we need to check the filesystem type and OS configuration). If the same directory is opened later on, the last state is restored without loading a particular session. A persistent session may be delete if it was not use for a long time and is not an active session or can be explicitly deleted by the user.
Temporary session
A temporary session can be created via -n or --new-window flag or via menu File -> New Window. Temporary sessions are persistent as long as MarkText is not closed and deleted on application start if it's not an active session. Temporary sessions are identified by a random ID with a different length than persistent sessions. Selecting a directory in a window will load or create a persistent session - the old session is closed or temporary session is discarded.
Workspace
A workspace is a user-defined persistent session that is created by the user with a user-defined name and never deleted by MarkText. There is no technical difference between persistent sessions and workspaces. A single workspace may contain multiple directories but persistent/temporary sessions allow at most one directory.
Active sessions
An active session is not another session type but rather a session that is currently opened. All active sessions are stored on disk and restored if MarkText is forced to restart. Even a temporary session that was opened in a window is restored in this case. After a graceful shutdown, only the last window (last active session) is restored.
Session Management
A session (not workspace) is created only by MarkText and may be deleted and loaded via command-line.
-s or --session:
Load a session by ID: marktext --session <id>
Load multiple sessions or workspaces by ID and create a window per session: marktext --session load <IDs>
Delete a given session by ID: marktext --session delete <id>
List all sessions: marktext --session list
Workspace Management
A workspace can be created, deleted and loaded via GUI.
Command Palette
A workspace command palette entry should be added to configure workspaces.
Option:
create: Create a workspace with a user-defined name. The name is used as workspace label and a unique session ID is generated.
rename: Rename the given workspace; only the session label is changed.
delete: Delete the given workspace.
list: List all workspaces but not sessions.
add: Add the directory(s) to the given workspace.
Settings panel
We may add a settings panel on demand for additional configuration. Not yet planned.
Command-line
When opening a file via command-line...
...the associated tab is selected and the window is focused. If the file is opened in multiple windows the last active window is focused and the associated tab is selected.
...the file is added to the window/session of the last focused window.
...the file is added to the specified session or workspace and opened in a new window if --session <id> is also passed. The windows is focused if a window with given session already exists.
Opening a directory via command-line or GUI load or create the associated session.
# The last active session is restored if MarkText is not started or the last active window is focused.
marktext
# Create a new temporary session and open a new window.
marktext --new-window <dir|file(s)>
# See above paragraph.
marktext <file(s)>
# Create or load the associated session in a new window.
marktext <dir>
# Open or focus the given session/workspace in a new window.
marktext --session <id> <files>
The recovery directory contains a full file backup of unsaved files/tabs identified by md5hex(path.normalize(isLinux ? filePath : filePath.toLowerCase())) or tab id (untitled-<number>) for untitled tabs.
Add a timestamp to the filename to prevent overwrite issues? Meaning, create the new backup and remove old backup afterwards.
Session structure
<session>/
├── storage/ (storage for current session)
├── session.json
├── settings.json (reserved for session settings to override global settings)
└── window-state.json
interfaceSessionFile{type: SessionFileType;// The file path or untitled tab id (untitled-<number> in lower-case).path: string;lf?: string;// Add more information at a later stage.}
enumSessionFileType{File=0,Untitled=1}
TODO:
Should we move window-state.json to preference root? Do we really need this per each session?
Process Architecture
Currently all I/O is handled by Electrons main process and send via IPC to the editor (renderer process). This can cause problems with high CPU utilization because the event loop runs on Chromiums UI thread. Blocking this thread will block/delay all renderer processes. Using a separated process for session and file recovery may be a little bit exaggerated as first considered. Which leads us to the new idea for a separate I/O background process.
New Process Architecture
Adding a detached background process allows us to move all I/O to this process and relieve the main process, but also to respawn the process on critical errors during I/O. The main process is still allowed to do I/O during startup and to save configurations in the application data directory. Editor windows are required to request all read/write requests from the background process over IPC (before from the main process over IPC which lead to a shutdown of all windows on a critical error). The main process keeps basis session information about opened files and directory to quickly open new files in the best editor window. Editor windows manage the associated session and periodically save the editor buffer to a backup directory. The background process also copy the old file before overwriting a markdown file to avoid loss of data if enabled.
IPC for I/O Background Process
The background process should be a hidden renderer process with node integration enabled that communicates directly with each editor via a MessagePort channel.
Unsaved file/tab changes are automatically saved after some time/when closing the window and restored during application start when using a persistent session or workspace.
Do you mean that if I didn't save the tab/file, I can still close window. the file content saved to session?
If a file has been changed externally after saving unsaved changes an notification
How do you know that the file has been changed by other editor ?
Do you mean that if I didn't save the tab/file, I can still close window. the file content saved to session?
Yes, as long as it's not a temporary session but unsaved changes are bound to the session. If you File -> Open Folder the same folder the same persistent sessions is loaded because a persistent session represent a directory on disk.
E.g. you are using session A and file-1 has unsaved changes, then you open another session and open file-1 too. The changes from session A are not restored but if you open session A again they are restored.
How do you know that the file has been changed by other editor ?
This only applies when Mark Text is closed. We can compare the last modification time from both files (fs.stat).
Can we save all setting, keybinding and session in electron-store? So we do not need the json files?
I think we should still separate settings and key bindings. electron-store save the data as JSON file, so we can create multiple stores but after reviewing the code we might have another problem. The library reads after each get and not just after the file has changed externally. I think we can start using electron-store for settings and pass changed to renderer (as currently) and later replace electron-store with an optimized fork that only reads the file after a change occurred.
For session I'll create a custom SessionStorage class or so that is especially for sessions. I have a rough idea how I'll implement that.
Thank you for this detailed RFC. This issue has been open since 2019 with no active implementation, and the core team indicated in September 2019 that it was not planned for the near term. The most critical user need from this RFC — recovering unsaved changes after a crash — is now partially addressed by the editorBufferStore mechanism introduced during the engine rewrite (per-window tab state is saved atomically to disk and restored on next launch). The full session system as designed here (persistent sessions tied to directory paths, temporary/workspace sessions, background I/O process, --session CLI management) has not been built and there is no current plan to do so. Given the age of this RFC and the significant engineering investment it would require, we are closing this issue as stale. If there is community interest in revisiting specific pieces (e.g. workspace support or crash recovery improvements), please open a focused new issue with a concrete scope.
Activity
fxha commentedon Apr 28, 2019
Session
Overview
A session contains a persistent state of the window layout, editor configuration and file information:
At a later stage we can also save the cursor position, file history and side bar states like opened folders in tree view etc.
MarkText runs as a single-instance and each window is associated with a unique session. Extensions and MarkText can save further files in the session or global, accessible for all sessions. MarkText allows to spawn multiple instances if the instance has a different application data directory than all started instances.
All active sessions are restored after a forced shutdown, crash or similar (hereinafter as "forced restart") but configuration/file changes may be lost up to a certain point. The last active session (= last active window with associated session) is loaded after closing MarkText gracefully load. Sessions are saved in the application data directory that can be changed with
--user-data-dir <dir>and must be writable.Unsaved changes
Unsaved files/tabs are automatically saved after some time/when closing the window and restored during application start when using a persistent session or workspace. A notification is display if a file has been changed externally after saving unsaved changes but the file is restored as usual.
Automatic restore
Sessions are loaded/restored during application start. Restore of unsaved changes or sessions can be disabled by setting
restoreChangesOnStartand/orrestoreSessionOnStarttofalse(= command-line flag--nosession). IfrestoreChangesOnStartis set totruebut notrestoreSessionOnStarta temporary session is used for each window until application shutdown. If both are set tofalseneither sessions nor backups are created (but may a in-memory session is used?).Persistent session
Persistent sessions are used by MarkText and identified by a hashed normalized directory path using MD5-Hex. A lower-case directory path is used on macOS and Windows because the filesystem API is case-insensitive by default (normally we need to check the filesystem type and OS configuration). If the same directory is opened later on, the last state is restored without loading a particular session. A persistent session may be delete if it was not use for a long time and is not an active session or can be explicitly deleted by the user.
Temporary session
A temporary session can be created via
-nor--new-windowflag or via menuFile -> New Window. Temporary sessions are persistent as long as MarkText is not closed and deleted on application start if it's not an active session. Temporary sessions are identified by a random ID with a different length than persistent sessions. Selecting a directory in a window will load or create a persistent session - the old session is closed or temporary session is discarded.Workspace
A workspace is a user-defined persistent session that is created by the user with a user-defined name and never deleted by MarkText. There is no technical difference between persistent sessions and workspaces. A single workspace may contain multiple directories but persistent/temporary sessions allow at most one directory.
Active sessions
An active session is not another session type but rather a session that is currently opened. All active sessions are stored on disk and restored if MarkText is forced to restart. Even a temporary session that was opened in a window is restored in this case. After a graceful shutdown, only the last window (last active session) is restored.
Session Management
A session (not workspace) is created only by MarkText and may be deleted and loaded via command-line.
-sor--session:marktext --session <id>marktext --session load <IDs>marktext --session delete <id>marktext --session listWorkspace Management
A workspace can be created, deleted and loaded via GUI.
Command Palette
A
workspacecommand palette entry should be added to configure workspaces.Option:
create: Create a workspace with a user-defined name. The name is used as workspace label and a unique session ID is generated.rename: Rename the given workspace; only the session label is changed.delete: Delete the given workspace.list: List all workspaces but not sessions.add: Add the directory(s) to the given workspace.Settings panel
We may add a settings panel on demand for additional configuration. Not yet planned.
Command-line
When opening a file via command-line...
--session <id>is also passed. The windows is focused if a window with given session already exists.Opening a directory via command-line or GUI load or create the associated session.
fxha commentedon Apr 28, 2019
Session (Technical Design)
Overview (application data directory)
sessions.json (= active sessions):
When Mark Text is closed gracefully only one session (last window) is saved but while Mark Text is running all active session IDs are stored.
File Recovery
The recovery directory contains a full file backup of unsaved files/tabs identified by
md5hex(path.normalize(isLinux ? filePath : filePath.toLowerCase()))or tab id (untitled-<number>) for untitled tabs.TODO:
Session structure
Session ID:
md5hex(path.normalize(isLinux ? rootFolder : rootFolder.toLowerCase()))(32 characters)'t' + uuidv4()(31characters)uuidv4()(30 characters)session.json:
{ "version": "number", "id": "string", "type": "SessionType", "label": "string", "folders": "string[]", "files": "SessionFile[]", "views": { "sideBar": { "visible": "boolean", "width": "number", "activeViewId": "string" }, "tabBar": { "selectedIndex": "number", "visible": "boolean" } } }Details:
TODO:
window-state.jsonto preference root? Do we really need this per each session?Process Architecture
Currently all I/O is handled by Electrons main process and send via IPC to the editor (renderer process). This can cause problems with high CPU utilization because the event loop runs on Chromiums UI thread. Blocking this thread will block/delay all renderer processes. Using a separated process for session and file recovery may be a little bit exaggerated as first considered. Which leads us to the new idea for a separate I/O background process.
New Process Architecture
Adding a detached background process allows us to move all I/O to this process and relieve the main process, but also to respawn the process on critical errors during I/O. The main process is still allowed to do I/O during startup and to save configurations in the application data directory. Editor windows are required to request all read/write requests from the background process over IPC (before from the main process over IPC which lead to a shutdown of all windows on a critical error). The main process keeps basis session information about opened files and directory to quickly open new files in the best editor window. Editor windows manage the associated session and periodically save the editor buffer to a backup directory. The background process also copy the old file before overwriting a markdown file to avoid loss of data if enabled.
IPC for I/O Background Process
The background process should be a hidden renderer process with node integration enabled that communicates directly with each editor via a
MessagePortchannel.Jocs commentedon May 1, 2019
Do you mean that if I didn't save the tab/file, I can still close window. the file content saved to session?
How do you know that the file has been changed by other editor ?
Jocs commentedon May 1, 2019
I agree to include tabs.json into session.json.
@fxha Can we save all
setting,keybindingandsessionin electron-store? So we do not need thejsonfiles?fxha commentedon May 1, 2019
Yes, as long as it's not a temporary session but unsaved changes are bound to the session. If you
File -> Open Folderthe same folder the same persistent sessions is loaded because a persistent session represent a directory on disk.E.g. you are using session
Aandfile-1has unsaved changes, then you open another session and openfile-1too. The changes from sessionAare not restored but if you open sessionAagain they are restored.This only applies when Mark Text is closed. We can compare the last modification time from both files (
fs.stat).I think we should still separate settings and key bindings.
electron-storesave the data as JSON file, so we can create multiple stores but after reviewing the code we might have another problem. The library reads after eachgetand not just after the file has changed externally. I think we can start usingelectron-storefor settings and pass changed to renderer (as currently) and later replaceelectron-storewith an optimized fork that only reads the file after a change occurred.For session I'll create a custom
SessionStorageclass or so that is especially for sessions. I have a rough idea how I'll implement that.Jocs commentedon May 2, 2019
Do you mean this maybe cause performance issue?
fxha commentedon May 2, 2019
Yes, I think so.
Jocs commentedon May 2, 2019
I think you can adjust the main folder structure first, then submit a PR, I can also take a look, then add some code of Mark Text Preference.
Jocs commentedon Sep 29, 2019
@fxha Do you have plan to do this in version 0.16.0?
fxha commentedon Sep 29, 2019
Currently not planned.
Jocs commentedon Jun 26, 2026
Thank you for this detailed RFC. This issue has been open since 2019 with no active implementation, and the core team indicated in September 2019 that it was not planned for the near term. The most critical user need from this RFC — recovering unsaved changes after a crash — is now partially addressed by the
editorBufferStoremechanism introduced during the engine rewrite (per-window tab state is saved atomically to disk and restored on next launch). The full session system as designed here (persistent sessions tied to directory paths, temporary/workspace sessions, background I/O process,--sessionCLI management) has not been built and there is no current plan to do so. Given the age of this RFC and the significant engineering investment it would require, we are closing this issue as stale. If there is community interest in revisiting specific pieces (e.g. workspace support or crash recovery improvements), please open a focused new issue with a concrete scope.