mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-05 07:54:38 +00:00
* M1: terminals survive daemon restarts: stop reasons, detached terminal worker, kill cleanup
restart_server_request.killTerminals / shutdown_server_request.keepTerminals and server_info.features.terminalsSurviveRestart; CLI daemon restart --kill-terminals and daemon stop --keep-terminals (capability-gated). The terminal worker runs detached over an owner-only, secret-authenticated socket and is adopted on daemon start. One kill cleanup contract (the reaper) by birth time, used by the daemon, the supervisor and stopDaemonInstance. Settings offers Restart and close terminals.
* M1: fix review findings: reaper session/table retry, short-command creation order, remote kill-restart cleanup report, stop test port
* M1: fix review findings: reaper-owned cleanup record, session member identity by terminal id, unreadable cleanup records, Settings restart error rendered in the card
* M2: worker protocol versions and draining, worker runs from its own copy, desktop upgrades keep terminals, out-of-date daemon notice, docs/terminal-worker.md
- Daemon classifies live workers by protocol: adopts current, drains one version old, shuts
down older, leaves a newer one running and answers list/create with
terminal_worker_incompatible (daemon status prints it). Never more than two workers.
- esbuild worker bundle (build:lib); a built daemon copies bundle, shell integration and
node-pty into $PASEO_HOME/terminal-worker/<version>-<hash>/ and launches from it; copies no
record names are pruned.
- Desktop app_update/version_mismatch stop with keep terminals on macOS/Linux; Windows and a
daemon without the capability close them.
- Out-of-date daemon sidebar notice with Update button, worded by terminalsSurviveRestart.
- PASEO-13: unreadable worker records fail status and kill-restart retry.
- PASEO-14: a session member without a terminal id is unknown, not unrelated.
- Smoke test 46 with A/B installation fixtures and per-fixture CLI shims (PASEO-9 fold).
* M2: track in-flight worker requests without leaving an unhandled rejection
detach({ afterPendingRequests }) tracked each request with request.finally(), which rejects
with the request; a fire-and-forget send that timed out surfaced as an unhandled rejection
(worker-terminal-manager.test.ts). Track settlement only.
* M2: CI round 1 fixes: worker terminal manager in the MCP key test, status cleanup field, shared socket dir
- mcp-server.test.ts (new upstream test) built the worker manager with the old fork API;
it now starts a real detached worker in a throwaway home.
- daemon-session.test.ts expected status without terminalCleanupUnconfirmed (added in M1).
- paseo-daemon.test.ts: a home under the scratch temp dir is too long for a unix socket, so
the worker listens in the per-user paseo-<uid> directory, which outlives workers by design;
the test now requires it to be empty instead of absent.
* M2: run Windows PowerShell by its install path for process identity
server-tests (windows-latest): bootstrap-provider-availability.test.ts starts a daemon with a
PATH that holds only git, and the terminal worker failed to start: 'cannot read its own process
identity: spawn powershell.exe ENOENT'. Process inspection (managed-processes.ts inspect and the
terminal worker's process table) now runs %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe.
* M2: lifecycle e2e plain stop uses the shutdown RPC on Windows
CI round 2, server-integration (windows-latest): 'stop and stop --force never signal processes
a stale or earlier-boot record names' called stopDaemonInstance without a shutdown channel,
which Windows refuses ('has no graceful shutdown channel'). Plain stops in the file now pass
one, as the CLI does.
* M2: native notice outlet, Windows desktop-update wording, worker cap counts unconfirmed cleanups, create recovers after a drain
* M2: phone sidebar mounts the callout slot (iOS notice verified), worker record reads retry transient errors
* M2: native notice preconditions use explicit working directories, one block per terminal
* M1: compact sidebar shows only native callouts on every platform; PTY round trips in the MCP key test get a 5 s window
* ci: pin Playwright browser jobs to ubuntu-24.04
The pinned Playwright (1.58) has no ubuntu-26.04 build, and forks whose
ubuntu-latest already resolves to 26.04 fail at browser install. Job names
are unchanged, so required-check contexts still match.
* ci: use ubuntu-latest for Playwright jobs, same as upstream
Reverts the ubuntu-24.04 pin; upstream main's Playwright shards pass on ubuntu-latest.
* fix(server): keep terminal worker record IO off the daemon event loop
* fix(server): restore terminal creation after worker failure
A disconnected terminal worker remained the current manager indefinitely, leaving dead sessions listed and every later create bound to a closed socket. Confirm process death before retiring the member and use the existing single launch path for replacement. Reconnect live workers and replay their screens while preserving terminal identity.
* test(server): verify orphan cleanup during worker recovery
Recovery reaps orphaned programs before a later daemon stop. The previous fixed-delay assertion raced that cleanup and failed when it completed promptly. Verify orphan removal, same-daemon replacement creation and cleanup on normal or forced stop instead.
* test(server): isolate Windows dependency fixture setup
Build the local package registry in setup so its npm packing processes do not consume the installation journey deadline. Install the platform package used by Windows runtime fixtures directly, avoiding an unused wrapper postinstall probe without a timeout.
* test(server): wait for pending watchers before disposal
Observation admission starts before asynchronous path setup reaches watcher subscription. Disposing at the admission counter races setup and never exercises late watcher cleanup. Wait for the subscriptions to be pending before disposing.
---------
Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
250 lines
9.0 KiB
TypeScript
250 lines
9.0 KiB
TypeScript
import { createHash, randomUUID } from "node:crypto";
|
|
import {
|
|
chmodSync,
|
|
existsSync,
|
|
mkdirSync,
|
|
readdirSync,
|
|
readFileSync,
|
|
renameSync,
|
|
rmSync,
|
|
statSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { access, readdir, rm } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { ensurePrivateDirectory } from "../server/private-files.js";
|
|
import { listTerminalWorkerRecords, terminalWorkerDirectory } from "./terminal-worker-record.js";
|
|
|
|
// A terminal worker outlives the installation that started it. An npm or desktop update
|
|
// replaces that installation's files, so a worker launched from them would fail its next
|
|
// terminal create (node-pty's spawn-helper, the zsh integration). A built daemon therefore
|
|
// copies the worker bundle, its shell integration and node-pty into
|
|
// $PASEO_HOME/terminal-worker/<paseoVersion>-<content hash>/ and launches the worker from there.
|
|
// The hash keeps two builds of one version (a local rebuild) from sharing a copy. A copy is
|
|
// removed once no worker record names it: after a worker drains, after a kill cleanup, and at
|
|
// daemon start. Dev runs (.ts source) launch from source and copy nothing.
|
|
|
|
export const TERMINAL_WORKER_BUNDLE_NAME = "terminal-worker.mjs";
|
|
// Written last into a finished copy; a directory without it is an interrupted copy.
|
|
const RUNTIME_MARKER = "paseo-terminal-worker-runtime.json";
|
|
const TEMP_PREFIX = ".tmp-";
|
|
|
|
export class TerminalWorkerRuntimeError extends Error {
|
|
constructor(message: string, options?: { cause?: unknown }) {
|
|
super(message, options);
|
|
this.name = "TerminalWorkerRuntimeError";
|
|
}
|
|
}
|
|
|
|
/** What the launcher runs: the worker script, the node flags it needs, and the copy it is in. */
|
|
export interface TerminalWorkerEntry {
|
|
scriptPath: string;
|
|
execArgv: string[];
|
|
/** The copy's directory name under terminal-worker/, or null when run from source. */
|
|
runtime: string | null;
|
|
}
|
|
|
|
function sourceExecArgv(): string[] {
|
|
const loaderUrl = new URL("./terminal-ts-loader.mjs", import.meta.url).href;
|
|
const importSource = [
|
|
'import { register } from "node:module";',
|
|
'import { pathToFileURL } from "node:url";',
|
|
`register(${JSON.stringify(loaderUrl)}, pathToFileURL("./"));`,
|
|
].join(" ");
|
|
return [
|
|
"--experimental-strip-types",
|
|
"--import",
|
|
`data:text/javascript,${encodeURIComponent(importSource)}`,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* The entry a daemon launches its worker from: the source file in dev, otherwise a copy of the
|
|
* built bundle in this home, prepared on demand.
|
|
*/
|
|
export function resolveTerminalWorkerEntry(input: {
|
|
paseoHome: string;
|
|
paseoVersion: string;
|
|
}): TerminalWorkerEntry {
|
|
if (import.meta.url.endsWith(".ts")) {
|
|
return {
|
|
scriptPath: fileURLToPath(new URL("./terminal-worker-process.ts", import.meta.url)),
|
|
execArgv: sourceExecArgv(),
|
|
runtime: null,
|
|
};
|
|
}
|
|
const bundleDirectory = fileURLToPath(new URL("../terminal-worker/", import.meta.url));
|
|
if (!existsSync(path.join(bundleDirectory, TERMINAL_WORKER_BUNDLE_NAME))) {
|
|
throw new TerminalWorkerRuntimeError(
|
|
`The terminal worker bundle is missing from ${bundleDirectory}; rebuild @getpaseo/server (npm run build).`,
|
|
);
|
|
}
|
|
const require = createRequire(import.meta.url);
|
|
const nodePtyDirectory = path.dirname(require.resolve("node-pty/package.json"));
|
|
return prepareTerminalWorkerRuntime({
|
|
paseoHome: input.paseoHome,
|
|
paseoVersion: input.paseoVersion,
|
|
bundleDirectory,
|
|
nodePtyDirectory,
|
|
});
|
|
}
|
|
|
|
function runtimeName(input: {
|
|
paseoVersion: string;
|
|
bundleDirectory: string;
|
|
nodePtyDirectory: string;
|
|
}): string {
|
|
const hash = createHash("sha256");
|
|
hash.update(readFileSync(path.join(input.bundleDirectory, TERMINAL_WORKER_BUNDLE_NAME)));
|
|
hash.update(readFileSync(path.join(input.nodePtyDirectory, "package.json")));
|
|
hash.update(`${process.platform}-${process.arch}`);
|
|
const version = input.paseoVersion.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
return `${version}-${hash.digest("hex").slice(0, 12)}`;
|
|
}
|
|
|
|
// Plain fs calls on purpose: inside the desktop app the sources live in app.asar, which
|
|
// Electron's fs reads transparently but newer helpers like cpSync do not.
|
|
function copyTree(source: string, destination: string): void {
|
|
const stat = statSync(source);
|
|
if (stat.isDirectory()) {
|
|
mkdirSync(destination, { recursive: true });
|
|
for (const name of readdirSync(source)) {
|
|
copyTree(path.join(source, name), path.join(destination, name));
|
|
}
|
|
return;
|
|
}
|
|
writeFileSync(destination, readFileSync(source));
|
|
// Keeps node-pty's spawn-helper executable.
|
|
chmodSync(destination, stat.mode & 0o777);
|
|
}
|
|
|
|
function copyNodePty(source: string, destination: string): void {
|
|
mkdirSync(destination, { recursive: true });
|
|
copyTree(path.join(source, "package.json"), path.join(destination, "package.json"));
|
|
copyTree(path.join(source, "lib"), path.join(destination, "lib"));
|
|
const native = [
|
|
path.join("build", "Release"),
|
|
path.join("prebuilds", `${process.platform}-${process.arch}`),
|
|
].filter((relative) => existsSync(path.join(source, relative)));
|
|
if (native.length === 0) {
|
|
throw new TerminalWorkerRuntimeError(
|
|
`node-pty at ${source} has no native build for ${process.platform}-${process.arch}`,
|
|
);
|
|
}
|
|
for (const relative of native) {
|
|
copyTree(path.join(source, relative), path.join(destination, relative));
|
|
}
|
|
}
|
|
|
|
function isCompleteRuntime(directory: string): boolean {
|
|
return existsSync(path.join(directory, RUNTIME_MARKER));
|
|
}
|
|
|
|
/**
|
|
* Copies the worker bundle, its shell integration and node-pty into this home, unless a finished
|
|
* copy of the same content is already there. Returns the entry to launch from the copy.
|
|
*/
|
|
export function prepareTerminalWorkerRuntime(input: {
|
|
paseoHome: string;
|
|
paseoVersion: string;
|
|
bundleDirectory: string;
|
|
nodePtyDirectory: string;
|
|
}): TerminalWorkerEntry {
|
|
const root = terminalWorkerDirectory(input.paseoHome);
|
|
ensurePrivateDirectory(root);
|
|
const name = runtimeName(input);
|
|
const directory = path.join(root, name);
|
|
const entry = {
|
|
scriptPath: path.join(directory, TERMINAL_WORKER_BUNDLE_NAME),
|
|
execArgv: [],
|
|
runtime: name,
|
|
};
|
|
if (isCompleteRuntime(directory)) return entry;
|
|
|
|
// An interrupted copy left behind: no worker can be running from it.
|
|
rmSync(directory, { recursive: true, force: true });
|
|
const temporary = path.join(root, `${TEMP_PREFIX}${name}-${randomUUID()}`);
|
|
try {
|
|
mkdirSync(temporary, { recursive: true });
|
|
copyTree(
|
|
path.join(input.bundleDirectory, TERMINAL_WORKER_BUNDLE_NAME),
|
|
path.join(temporary, TERMINAL_WORKER_BUNDLE_NAME),
|
|
);
|
|
copyTree(
|
|
path.join(input.bundleDirectory, "shell-integration"),
|
|
path.join(temporary, "shell-integration"),
|
|
);
|
|
copyNodePty(input.nodePtyDirectory, path.join(temporary, "node_modules", "node-pty"));
|
|
writeFileSync(
|
|
path.join(temporary, RUNTIME_MARKER),
|
|
`${JSON.stringify({ paseoVersion: input.paseoVersion, copiedAt: new Date().toISOString() })}\n`,
|
|
);
|
|
renameSync(temporary, directory);
|
|
} catch (error) {
|
|
rmSync(temporary, { recursive: true, force: true });
|
|
if (isCompleteRuntime(directory)) return entry;
|
|
throw new TerminalWorkerRuntimeError(
|
|
`Cannot copy the terminal worker into ${directory}: ${error instanceof Error ? error.message : String(error)}`,
|
|
{ cause: error },
|
|
);
|
|
}
|
|
return entry;
|
|
}
|
|
|
|
export interface PruneTerminalWorkerRuntimesResult {
|
|
removed: string[];
|
|
/** Copies that could not be deleted (a worker that just exited can still hold files on Windows). */
|
|
failed: Array<{ directory: string; error: string }>;
|
|
}
|
|
|
|
/**
|
|
* Deletes every copy no worker record names. With an unreadable record it deletes nothing: that
|
|
* record could name a copy a live worker runs from.
|
|
*/
|
|
export async function pruneTerminalWorkerRuntimes(
|
|
paseoHome: string,
|
|
): Promise<PruneTerminalWorkerRuntimesResult> {
|
|
const result: PruneTerminalWorkerRuntimesResult = { removed: [], failed: [] };
|
|
const root = terminalWorkerDirectory(paseoHome);
|
|
const files = await listTerminalWorkerRecords(paseoHome);
|
|
const inUse = new Set<string>();
|
|
for (const file of files) {
|
|
if ("error" in file) return result;
|
|
if (file.record.runtime) inUse.add(file.record.runtime);
|
|
}
|
|
let names: string[];
|
|
try {
|
|
names = await readdir(root);
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") return result;
|
|
throw error;
|
|
}
|
|
for (const name of names) {
|
|
if (inUse.has(name)) continue;
|
|
const directory = path.join(root, name);
|
|
if (
|
|
!name.startsWith(TEMP_PREFIX) &&
|
|
!(await access(path.join(directory, RUNTIME_MARKER)).then(
|
|
() => true,
|
|
(error: NodeJS.ErrnoException) => {
|
|
if (error.code === "ENOENT" || error.code === "ENOTDIR") return false;
|
|
throw error;
|
|
},
|
|
))
|
|
)
|
|
continue;
|
|
try {
|
|
await rm(directory, { recursive: true, force: true });
|
|
result.removed.push(directory);
|
|
} catch (error) {
|
|
result.failed.push({
|
|
directory,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
}
|
|
}
|
|
return result;
|
|
}
|