Module Contract¶
Note
A module has three parts: a layout TDL file (<Name>_Module.tdl: a metadata block with Name: (the call name), then a root Module { } block holding widgets, expressions and Params; the root block names the file type and must agree with the suffix; TDL Language, ADR-0014 Shell File Language), a style TDL file, and a Python file (<Name>_Module.py) next to them, in a Modules/<module_name>/ folder (ADR-0007 Shell UI Layer > What a module is (2026-10-26)). This note is about the Python part: the logic. The Python file holds one class that extends the shell's Module base. The shell creates it once, gives it a context, and calls start, stop and, for a changed configuration, reload. The module owns its data and its threads; it sends finished values to the shell, which renders them. Decided parts come from ADR-0007 Shell UI Layer and ADR-0009 D-Bus Client Library; names and signatures below are proposed. Short snippets are illustrations, not code to build.
What is Decided¶
| Decision | Source |
|---|---|
A module is <Name>_Module.tdl plus a required matching <Name>_Module.py, next to each other, in Modules/<module_name>/ with all supporting files |
ADR-0007 Shell UI Layer > Modules (2026-10-25) |
The shell imports the file, finds the class that extends a shell Module base, creates it once, and calls start, stop and an optional reload |
ADR-0007 Shell UI Layer > Module Python (2026-10-25) |
The module receives a context with its TDL values, its name (the Name: in its metadata block) and UID, a logger and the send entry point |
same |
One instance per module file, shared by every use; per-use differences are the module's job. The Python reaches each use by the key <Module>@<uid>, which it receives from ctx.uses and passes back to name a use in an action (D23, ADR-0014 Shell File Language > Decisions on the Open Items (2026-10-30); Module Context > Uses) |
ADR-0007 Shell UI Layer > Modules (2026-10-25) |
| The module's Python reaches its TDL file by file name, unique name or UID | same |
| Modules send values to shell entry points; the shell converts what it can | ADR-0007 Shell UI Layer > Module data reaches the view through shell entry points (2026-10-25) |
| A module's Python can be reloaded at runtime | ADR-0007 Shell UI Layer > Events and module reload (2026-10-26) |
Blocking work runs off the loop; D-Bus goes through the shared dbus-fast service thread |
Process Model and Threading, ADR-0009 D-Bus Client Library |
The Class (Proposed)¶
# Modules/Clock/Clock_Module.py (illustration; the import path is not decided)
from tess_shell import Module
class Clock(Module):
def start(self):
self.ctx.every(1.0, self.tick) # a timer the shell runs
self.tick()
def tick(self):
self.ctx.send("time", "14:05") # a plain value
def stop(self):
pass # timers are cancelled by the shell
- One class. The file holds exactly one class extending
Module. None or more than one is a load error naming the file (ADR-0007 Shell UI Layer > Monitors, names and errors (2026-10-25)). - No constructor arguments. The base class sets
self.ctxbeforestart()is called. - Methods.
start(),stop(), an optionalreload()and an optionalvalidate_use(use); the base class provides empty versions.validate_useis called when a use is loaded, before it is shown; it raisesValueErrorwith a message to reject a param, and the shell reports it as an error naming the file, the use and the param (Clock Module, decided 2026-10-27 that the Clock validates its format on use loaded; the hook and its name are proposed). - Service hooks. An optional
on_service_lost(name)and an optionalon_service_found(name); the base class provides empty versions. The D-Bus layer calls them on the loop thread (handed over withcall_soon) when a well-known name the module uses loses or gains an owner: lost gives the module its empty state, found makes it re-read its properties (D-Bus Service Layer > Listening and Name Owners). Decided: they belong to the contract, so every module handles a service the same way (maintainer, 2026-11-12). - Actions. A shell file can call a module method from an event (the shell, not TEL, maps events: ADR-0007 Shell UI Layer > Events and module reload (2026-10-26)). Decided (maintainer, 2026-10-26): only methods marked with an
@actiondecorator can be called from a shell file, and a shell file cannot call anything else. Slow work in an action goes to a thread and the method returns at once.
Lifecycle¶
stateDiagram-v2
[*] --> Created: shell imports the file and creates the class
Created --> Running: start() returns
Created --> Failed: start() raises
Running --> Running: reload() for a changed TDL file
Running --> Stopping: stop() on quit or code reload
Stopping --> Created: new code imported, new instance made
Stopping --> [*]: threads joined, instance dropped
Running --> Failed: a callback or thread raises past the helpers
Failed --> Created: code reload
| Method | Runs on | Called when | Must |
|---|---|---|---|
start() |
Loop thread | After the shell model is built (Process Model and Threading > Lifecycle (proposed)) | Return quickly. Start threads or timers for anything slow. Values sent before a surface exists are kept |
stop() |
Loop thread | On quit, or before a code reload | Stop its own threads and close connections, within a timeout. The shell joins threads started through ctx.thread |
validate_use(use) |
Loop thread | A use of the module is loaded or its params change | Check use.params and raise ValueError on a bad value. Quick, no I/O (proposed) |
reload() |
Loop thread | The module's TDL file changed and the Python did not | Re-read self.ctx.config. No restart |
Code reload (ADR-0007 Shell UI Layer > Events and module reload (2026-10-26), mechanics proposed): the shell imports the new code first and checks it for the class; only then does it call stop() on the old instance, join its threads, purge the module's own modules from sys.modules, create the new instance and call start(). If the new code fails to import, the old instance keeps running and the error is logged with file:line:column. The value store keeps the last values until the new instance sends. #needs-verification: importlib with live threads, and supporting files in the module folder.
Rules for the Module Author¶
- Never touch tre nodes. Send values; the shell renders (Layers and Components > Dependency rules).
- Never block the loop.
start,stop,reloadand actions return fast; blocking work usesctx.thread,ctx.dbusor its own threads (Process Model and Threading > The rules). - Own your data. Read from your sources, keep your own files, JSON or database. The shell stores only the values you send, not your data.
- Catch your own thread errors, or start threads through
ctx.thread, which catches and logs them with the module's name. - No heavy computation on the shared service thread. D-Bus callbacks are short and never block, or they delay every D-Bus module (ADR-0009 D-Bus Client Library).
- Heavy CPU work leaves the process (a subprocess or native code); the GIL is shared with the loop (Process Model and Threading > Performance notes).
Custom Blocks (Decided)¶
A module author can add a block, in Python, shipped with the module (maintainer, 2026-11-01). The block is a Block class in the module's .py with declared properties, parts, states and events; it is built from tre's node kinds. The API sketch and the rules are written once in Block Reference > Custom Blocks (R12); this note does not repeat them. A file holds exactly one Module class (above) and any number of Block classes.
Integration¶
- Threads. Methods on the loop; module work on
ctx.threads; D-Bus callbacks on the shared service thread; values cross to the loop withcall_soonthroughctx.send. - Names. The expressions that place values into widgets are written in the module's own layout TDL file, not in the surface. Which names they read (the use's
params, the values the Python sends) is open (ADR-0007 Shell UI Layer > Open items). #needs-verification - D-Bus.
ctx.dbus(Module Context).
Failure Modes and Recovery¶
| Failure | Result |
|---|---|
| No class, or more than one | Load error naming the file |
<Name>_Module.py missing |
Load error naming both files |
start() raises |
Module marked failed; logged with name and UID; other modules continue; surfaces keep last values or defaults |
An exception in a ctx.thread or ctx.dbus callback |
Caught, logged with the module name; the module keeps running |
| A bad code reload | Old instance keeps running; error logged |
stop() does not return within the timeout |
Logged; the shell continues shutdown |
Open Questions¶
-
self.ctxis set by the base class beforestart(); there is no constructor argument (maintainer, 2026-11-04, M2 decisions, G3). - Only
@action-marked methods are callable from a shell file (maintainer, 2026-10-26). - Is
actionthe final decorator name, and may an action return a value? Context: the value form of an event key (a bare action name, or a block withAction:and named arguments) was decided on 2026-11-01 in Block Reference > Events; Events and Actions usesactionand ignores a returned value in v1. Recommendation: keepactionand ignore return values in v1. - What are the
Blockbase class andProphelper of a custom block? Context: Block Reference > Custom Block API (Decided) describes the custom block API. Recommendation: define both exactly as that section states and add nothing else in v1. - How a module used several times is named: a use's
Name:is for the file, references and expressions, and Python uses the<Module>@<uid>key (D23, ADR-0014 Shell File Language > Decisions on the Open Items (2026-10-30); the head word is the module name, ADR-0014 Shell File Language > Layout Rules, items 6 and 7). Which names an expression reads (params, sent values) stays open under Integration. - The package is
tess_shell; a module writesfrom tess_shell import Module, action(maintainer, 2026-11-04, G3). - M2 offers only
ctx.on_tick("second" or "minute"), driven by the shared wall-clock source;ctx.everyandctx.afterwait until a module needs them (G4). -
reload()takes no arguments; the module re-readsctx.config(G3).
Acceptance Criteria¶
- A module with one
Modulesubclass and its matching TDL file loads, starts and sends a value that a view shows. - A missing
.py, no class, or two classes each fail with a message naming the files. - A module whose
start()raises is marked failed and the other modules keep running. - Editing the module's
.pyreloads it without restarting the shell; a bad edit keeps the old instance. - Editing only its TDL file calls
reload(). -
stop()is called on quit, and its threads are joined within the timeout. - A callback exception on
ctx.threadorctx.dbusis logged with the module name and does not stop the shell.