Shell Start and Stop¶
Note
The order in which the shell starts, what stops it from starting, how it runs with no windows, and how it stops. It is the runtime view of what Process Model and Threading and Data Flow sketch. Decided: one process and one tre loop (ADR-0003 Process Model), settings.tdl read first and a bad one stops the shell (Settings File; settings.tdl is the confirmed name, with the root block Settings { }, D20, ADR-0014 Shell File Language > Decisions on the Open Items (2026-10-30)), run(keep_alive=True) and LoopHandle.stop() (TRE-REQ-005 Keep-Alive and Loop Stop), stay idle with no windows. Everything else here is proposed, with a recommendation.
Purpose and Scope¶
- Define the start sequence, the failure rule at each step, the running state, the stop sequence, and the process rules that surround them: one instance, signals, exit codes.
- Not in this note: how files are found and the registry works (Config Discovery and Registry), how errors are worded (Validation and Errors), reloading a running shell (Hot Reload), the command line and the control socket (IPC and CLI, to write), and how the shell is launched at login (
70 Delivery, to write).
Behaviour¶
Process Rules¶
- One process, one loop. The shell is one Python process; tre's
App.run()owns the main thread; everything that touches a window or node runs there (Process Model and Threading > The rules). - One instance per session (decided 2026-10-28). Two shells would fight over the registry, the D-Bus names the shell owns and the same surfaces. The shell takes a lock in
$XDG_RUNTIME_DIR/tesserae-shell/on start. A second start finds the lock, prints that a shell is already running with its process id, and exits with code 4. The control commands go over the shell's D-Bus interface, not a socket (ADR-0013 Control Interface, accepted 2026-10-28; IPC and CLI). Letting a second start ask the first to reload was rejected. The runtime directory rules are in the next item. - The runtime directory is required (decided 2026-10-28). The shell needs
XDG_RUNTIME_DIR, for its lock file and because a Wayland client finds the compositor socket there whenWAYLAND_DISPLAYis a relative name (from libwayland, not read for this note). It does not fall back to another directory, as the XDG base directory spec allows: a fallback such as/tmpwould make the lock and the socket reachable by other users, and without the directory the shell has no Wayland connection anyway. In pre-flight the shell checks: - the variable is set, non-empty and an absolute path, and the directory exists: otherwise exit code 2 naming the variable and the problem;
- the directory is owned by the current user: otherwise exit code 2;
- the mode is 0700 (the spec requires it): otherwise a warning, and the shell continues.
The shell creates
$XDG_RUNTIME_DIR/tesserae-shell/with mode 0700 for the lock file. It disappears at logout, which suits a lock that must not outlive the session. - Cleanup. The spec says files not accessed for 6 hours may be cleaned unless their sticky bit is set, so the shell sets the sticky bit on the lock file instead of running a timer that would wake the idle loop. #needs-verification whether any cleaner runs on the target session, and that the bit is honoured on these files.
- Where it is unset. A systemd user service or a KDE autostart inherits the variable; a launch from cron or a bare SSH session with no login session does not, and gets the exit-2 message.
- Observed on this machine, 2026-10-28:
XDG_RUNTIME_DIR=/run/user/1000, owned by the user, modedrwx------, withWAYLAND_DISPLAY=wayland-1. - Wayland only. No Wayland compositor, or one without the layer shell, is an environment error: tre raises
UnsupportedPlatform(TRE-REQ-001 Layer Surface Window > Delivered (tre #195 closed)) and the shell exits with code 2 naming it. X11 is not supported (V1 Scope > Target environment).
Start Sequence¶
flowchart TD
a[1 Pre-flight: Python, tre, Wayland] --> b[2 Find the config directory]
b --> c[3 Read settings.tdl]
c --> d[4 Start logging]
d --> e[5 Take the instance lock]
e --> f[6 Open the registry]
f --> g[7 Discover, parse, validate files]
g --> h[8 Resolve references and compile TEL]
h --> i[9 Start the loop with keep_alive]
i --> j[10 Read outputs, build the model]
j --> k[11 Open surfaces]
k --> l[12 Start modules]
l --> m[Running]
| Step | What happens | Source | If it fails |
|---|---|---|---|
| 1. Pre-flight | Python 3.14 or later; tre 0.6.0 or later (tesserae-engine, import tess_re; the Frame needs hit_shape, from 0.5.9; the binder needs Window.build, from 0.5.10); a Wayland session; a valid XDG_RUNTIME_DIR (above) |
Frame > Failure modes and recovery, Surface Types | Stop, code 2, a message naming the missing version or the environment |
| 2. Config directory | $XDG_CONFIG_HOME/tesserae-shell/ if set, non-empty and absolute, else $HOME/.config/tesserae-shell/ |
Settings File > The config directory | If it does not exist, create it with the default folders (decided 2026-10-28) |
| 3. Settings | Read settings.tdl; expand ~ and variables; check paths |
Settings File | Stop, code 1, error on the console and in the default log directory |
| 4. Logging | Start Loguru with the Logging keys, or the defaults |
Settings File > Log files | Fall back to the console only and say so; do not stop |
| 5. Lock | Take the instance lock | This note | Another shell runs: stop, code 4 |
| 6. Registry | Open registry.json at the configured path; create an empty one if missing; rebuild records from file UIDs when needed |
Config Discovery and Registry | A registry that cannot be read or written: stop, code 1 |
| 7. Discover, parse, validate | Find files, parse TDL (the metadata block, then the root block, which must agree with the suffix), check the schema and the semantic rules, follow Import: |
Validation and Errors | Collect every error, then stop, code 1 |
| 8. Resolve and compile | Resolve name references through the registry; compile TEL expressions | ADR-0008 Tesserae Expression Language | Stop, code 1 |
| 9. Loop | App.run(keep_alive=True) starts the loop with zero windows; output events and call_soon work |
TRE-REQ-005 Keep-Alive and Loop Stop | tre older than 0.5.9: caught in step 1 |
| 10. Model | Read App.outputs(), match Monitor blocks by Connector, then Description, then Index (TDL Language > Monitors (Shell Schema)), build the shell model |
V1 Scope > Multi-monitor, Data Flow > Monitor hot-plug | A Monitor block that matches no monitor is ignored |
| 11. Surfaces | Create one window per surface per monitor, from a callback on the loop | Surfaces | A surface that fails (a bad layer, tre failed) is logged and skipped; the others open |
| 12. Modules | Create each module class once and call start() |
Module Contract > Lifecycle | A module whose start raises is marked failed and logged; the others continue |
- Steps 1 to 8 run before the loop and may block; the user sees an error and no surfaces. From step 9 on, the loop runs and nothing blocks it (Process Model and Threading > The rules).
- The loop starts before the windows exist because tre can now run with none, and a window is added from a callback (TRE-REQ-005 Keep-Alive and Loop Stop). Modules start after the model is built, so a value sent before its surface exists is kept in the store (Process Model and Threading > Lifecycle (proposed)).
- Threads start where they are first needed: the config watcher after step 8, the D-Bus service thread at step 10, when the shell exports its own interface (D-Bus Service Layer > The Service Thread), the signal watcher at step 9, the watchdog after step 12 (Process Model and Threading > Threads).
Running¶
- No windows: stay idle (decided 2026-10-26). If every monitor is set to none, or no output is present, the shell keeps running and waits for
output_added(Data Flow > Monitor hot-plug). - Monitors come and go: windows close with
output_removedand are created again when the output returns; a dead window is never reused. - An unhandled exception in a callback is logged by tre and does not stop the loop (Process Model and Threading > What tre guarantees).
- The shell reports a ready state once step 12 finishes (a log line, a
Stateproperty change on the shell's D-Bus interface, and a notice to systemd, below).
Telling Systemd (Decided 2026-10-28)¶
The shell notifies systemd. When the environment variable NOTIFY_SOCKET is set, the shell sends one datagram with the text READY=1 to that socket once step 12 finishes, and STOPPING=1 when the stop sequence starts. systemd acts on them only when the unit uses Type=notify (sd_notify, read 2026-10-28). The protocol is a single datagram on a Unix socket, so the shell needs no library for it. With no NOTIFY_SOCKET set, the shell sends nothing. The unit file that sets Type=notify is a delivery item (70 Delivery, to write).
Stop Sequence¶
Triggers: SIGTERM or SIGINT, a Quit call on the shell's D-Bus interface, the session ending. SIGHUP reloads (proposed, Hot Reload). Python signal handlers do not run on an idle loop, so a watcher thread reads a socketpair fed by signal.set_wakeup_fd and calls LoopHandle.stop() (TRE-REQ-005 Keep-Alive and Loop Stop, Process Model and Threading > Lifecycle (proposed)).
- Set the state to Stopping; send
STOPPING=1; ignore file changes and reload requests. LoopHandle.stop(): every window closes without a veto (each hearsclosed, reason"stopped"), andApp.run()returns.- Stop modules in reverse start order: call
stop(), join the module's threads. A module gets 3 seconds; the whole step gets 10 seconds (decided 2026-10-28; tuned in testing). - Set the shell's shortcut actions inactive and drop the Tray module's host name, on the D-Bus service thread, with a limit of 1 second (Global Shortcuts > Cleaning Up, Plasma Coexistence). Then stop the D-Bus service thread, the watcher and the watchdog.
- Write the registry once, atomically (Config Discovery and Registry > The registry file).
-
Flush logs, release the instance lock, exit with code 0.
-
A second SIGTERM or SIGINT during stopping ends the process at once with code 130 after flushing logs.
- A step that overruns its timeout is logged by name (the module and the thread) and skipped; the process still exits.
- Floating Panel positions are saved when the Panel's
movedevent fires, not only at stop, so a killed shell loses nothing (Panel > Position and attachment).
Exit Codes¶
| Code | Meaning |
|---|---|
| 0 | Normal stop |
| 1 | Configuration error: settings, a file, the registry |
| 2 | Environment error: no Wayland or layer shell, tre or Python too old |
| 4 | Another shell is already running |
| 70 | An internal error: an unhandled exception outside a callback |
| 130 | Forced stop by a second signal |
The codes are decided (2026-10-28). Codes 3 and 5 to 69 are left free for later use.
Configuration¶
Start and stop read settings.tdl keys only: Directories, Registry, Logging, Validation, Main_Shell (Settings File). Proposed additions, to settle while building: Stop_Timeout (default 10 seconds) and Module_Stop_Timeout (default 3 seconds). No other key.
Python Contract¶
None. A module sees start() after the model exists and stop() at shutdown (Module Contract > Lifecycle). The shell never calls a module before step 12.
Integration¶
- tre:
App.run(keep_alive=True),App.thread_handle(),LoopHandle.stop()andcall_soon,App.outputs()(TRE-REQ-005 Keep-Alive and Loop Stop). The shell requires 0.5.9 or later. - Signals:
signal.set_wakeup_fdon a socketpair and a watcher thread (Process Model and Threading > Threads). - systemd: the
sd_notifydatagram onNOTIFY_SOCKET(Telling systemd (decided 2026-10-28)). - Wayland and KWin: the session must provide
wlr-layer-shell; the surface specs list what else is used (Surfaces). - A supervisor (restart on a hard hang) is a later item (Process Model and Threading > Failure and recovery); the exit codes let one tell a crash from a configuration error.
Design¶
None. Start and stop have no visual design; a failed start prints to the console and the log, and a running shell shows its errors the way Validation and Errors says.
Accessibility¶
A failure that stops the shell must also be readable without a console: the shell may be launched from a session with none. Decided 2026-10-28: the same error is sent as a desktop notification through org.freedesktop.Notifications when one is available (the shell calls it as a client; it does not own the server, ADR-0004 Plasma Coexistence). Open question 3.
Performance Budget¶
- Start is dominated by file parsing, validation and window creation. Validation is in code and loads no JSON Schema library (ADR-0020 Validation in Code).
- Budgets (time to first surface, time to ready, stop time) are set in
60 Quality/and measured on this machine; none is measured yet. - Stop is bounded: 10 seconds in total (decided).
Failure Modes and Recovery¶
| Failure | Result |
|---|---|
| A step before the loop fails | The shell prints every error found at that step, exits with the code above, and opens no surface |
A module raises in start |
That module is marked failed and logged; surfaces keep default values; other modules run |
| A surface fails to open | Logged and skipped; the rest open |
| The Wayland connection is lost | tre's windows close, the loop ends, the process exits; a supervisor restarts it |
| The loop hangs | The watchdog logs the stall; only a supervisor can recover (Process Model and Threading > Failure and recovery) |
| The process is killed | Nothing is written at stop; the lock is released by the kernel when the process dies, because it is a file lock (decided with the lock) |
| A stale lock with no process | A file lock held by flock dies with the process, so there is none to clear (decided with the lock) |
Open Questions¶
All seven were answered on 2026-10-28.
- One instance: a lock; a second start is refused.
- Exit codes: the table above.
- Failure notice: a desktop notification when the shell stops at start or fails a reload, opt-out.
- Config directory creation: create it with the default folders when missing.
- Timeouts: 3 seconds per module and 10 seconds in total.
- Ready notice: the shell notifies systemd (
READY=1andSTOPPING=1onNOTIFY_SOCKET). - Lock path:
$XDG_RUNTIME_DIR/tesserae-shell/, not the config directory. -
XDG_RUNTIME_DIRis required, with no fallback; the shell checks it in pre-flight and sets the sticky bit on its lock file (maintainer, 2026-10-28, as recommended; the control socket was dropped by ADR-0013).
Questions After Testing¶
Answered by: a test on the target session that lists the lock directory after a cleaner pass and checks the sticky bit.
- Does any cleaner run on the target session, and is the sticky bit honoured on the lock files? Context: a cleaner that removes the lock file would allow a second instance. Recommendation: keep the sticky bit and verify on the real session. #needs-verification
Acceptance Criteria¶
- With no
settings.tdl, the shell starts from the default folders and the surfaces open. - With an invalid
settings.tdl, the shell exits with code 1, prints the error withfile:line:columnand writes it tologs/, and opens no surface. - With tre older than 0.5.9, the shell exits with code 2 and names the required version.
- With
XDG_RUNTIME_DIRunset, empty or relative, the shell exits with code 2 and names the variable; with a directory owned by another user it exits with code 2; with a mode other than 0700 it warns and starts. - The lock file sits in
$XDG_RUNTIME_DIR/tesserae-shell/(mode 0700) with the sticky bit set. - A second start while one runs exits with code 4 and names the first's process id.
- With every output removed, the shell keeps running and reopens surfaces when an output returns.
- SIGTERM on an idle shell stops it in under 10 seconds with code 0, after each module's
stopran and the registry was written. - A module whose
stophangs delays the stop by at most 3 seconds, is named in the log, and the shell still exits. - A second SIGINT during stop ends the process with code 130.
- A module whose
startraises does not prevent the others from starting. - With
NOTIFY_SOCKETset, the shell sendsREADY=1after the last module starts andSTOPPING=1when it begins to stop; with it unset, nothing is sent.