astrocook.follow module#

A session followed by the GUI (phase 6-ter, PR 5-bis; decision 7 of the phase, option b).

A program that changes a session (the MCP server, a script) saves it to an archive after every change; the GUI, launched on that archive, reloads it each time it is replaced, and shows the session read only. This module is the part shared by the two sides, with no import of Qt or of mcp: writing the archive, the marker that says it is followed, whether the GUI can run here, and the command that launches it.

The marker is a small JSON file next to the archive, <archive>.follow.json, with the identifier of the session, the process that writes the archive and the version of Astrocook (the banner of the GUI says them; a marker without them is still read). The GUI looks for it whenever it opens an archive, whichever way the file arrives (the command line, the macOS open event, drag and drop): a flag of the command line would not reach an application already running on macOS. When the writer stops following the session it removes the marker, and the GUI says that the session is no longer followed.

The archive is never read half written: write_archive() saves it as session_save does, to a temporary file in the same folder that replaces the archive only when complete (os.replace, atomic on the same file system; _write_replacing of astrocook.io.adapter, F94). The marker is written in the same way.

astrocook.follow.MARKER_SUFFIX = '.follow.json'#

Appended to the path of a followed archive to make the path of its marker

astrocook.follow.MACOS_APPS = (PosixPath('/Applications/Astrocook.app'), PosixPath('/home/runner/Applications/Astrocook.app'))#

Where the macOS application is looked for, in this order

astrocook.follow.marker_path(archive: str | PathLike) → Path[source]#

The path of the marker of archive.

class astrocook.follow.FollowMarker(session_id: str, pid: int | None = None, version: str = '')[source]#

Bases: object

What the marker of a followed archive says.

session_id: str#

The identifier of the session in the program that writes the archive

pid: int | None = None#

The process that writes the archive (None in the marker of a program that did not write it)

version: str = ''#

The version of Astrocook of that program (’’ if the marker does not say)

writer_alive() → bool[source]#

Whether the process that writes the archive is still running.

Checked on macOS and Linux only (a signal 0 to the process); on Windows it is taken as running. A marker left behind by a writer that did not stop cleanly (killed) belongs to a dead process. A marker without a pid is taken as running.

__init__(session_id: str, pid: int | None = None, version: str = '') → None#
astrocook.follow.write_marker(archive: str | PathLike, session_id: str) → Path[source]#

Write the marker of archive (atomically) and return its path.

astrocook.follow.read_marker(archive: str | PathLike) → FollowMarker | None[source]#

The marker of archive, or None if there is none (or it cannot be read: the archive then opens as any other).

astrocook.follow.write_archive(history, archive: str | PathLike) → None[source]#

Save the current state of a session history to archive, with its first state as the original data and its log, as session_save does (atomically).

astrocook.follow.remove(archive: str | PathLike) → None[source]#

Stop following archive: remove its marker, then the archive.

astrocook.follow.gui_unavailable_reason() → str | None[source]#

Why the GUI cannot be shown here, or None if it can.

It cannot without PySide6 (the Docker image of the server installs the package without it), or on Linux without a display (DISPLAY or WAYLAND_DISPLAY).

astrocook.follow.gui_command(archive: str | PathLike) → List[str][source]#

The command that opens archive in the GUI.

On macOS the application (open -a Astrocook), if it is installed in /Applications or ~/Applications; else, and on the other systems, the astrocook command, or this Python with -m astrocook if the command is not on the path.

astrocook.follow.launch_gui(archive: str | PathLike) → List[str][source]#

Launch the GUI on archive, detached from this process, and return the command used.

The GUI reads nothing from this process and writes nothing to it: its standard streams go to the null device (the standard output of the MCP server is its channel to the client), and it keeps running when this process ends.

Raises:

OSError – If the command cannot be started.