Skip to content
Closed
38 changes: 30 additions & 8 deletions apps/desktop/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ Gateway and runs Agents. The daemon exits with the app.
services on launch with `installer.js --resume`, which also restarts them
when the instance's `user.env` changed. A source folder removed since, or
one the installer no longer accepts as a Linger checkout (such as one the
earlier Simplex app unpacked), is unpacked again from the app's package
first and replaces the recorded one. The record also names the app
earlier Simplex app unpacked, or this package's own folder with files
missing since), is unpacked again from the app's package first and
replaces the recorded one. The record also names the app
version that unpacked the source; when this app brings another source
package (it was installed over an older one), that launch unpacks it and
resumes on it, with the "Updating Linger" page instead of the older
Expand All @@ -185,7 +186,12 @@ Gateway and runs Agents. The daemon exits with the app.
from their folder, the record keeps naming it, and the start failure page's
Retry moves them again. An app older than the recorded version
(a downgrade) leaves the services on their newer source and logs
`local_server.source_newer_than_app`. A Quick Install that stopped
`local_server.source_newer_than_app`. When the local model stops a resume,
the start failure names the stack's first known binding reason code
(`details.reasons`, such as `pi-extensions-unavailable`) in its own words
and, by `details.code`, the action that works: Try Again for
`LOCAL_MODELS_RECOVERY_REQUIRED` (the resume applies the approved plan
again), Set Up Again to review a new plan or choose another runtime. A Quick Install that stopped
before the computer enrolled is not resumed: setup shows again and Install
finishes it, reusing a finished job only while its origin answers (a stopped
server gets a new job, which starts the services again). The start failure
Expand Down Expand Up @@ -270,7 +276,10 @@ Gateway and runs Agents. The daemon exits with the app.
`desktop` section puts the daemon in Desktop mode: no self-update, it exits
when its stdin closes, and it reports `{"type":"status",...}` lines on
stdout, which drive the tray and the SPA status. Its log goes to
`<userData>/logs/computer-daemon.log`. Crashes restart with backoff; new
`<userData>/logs/computer-daemon.log`. Crashes restart with backoff; the
exit reason, logged with `computer_daemon.exited` and shown once starts
keep failing, is the last stderr line outside the daemon's log records (a
Go crash: its `panic:` line), or none; new
folders or a new name restart it once tasks finish.
On macOS it writes `{"type":"hello","features":["permissions.v1"]}` to
the daemon's stdin after spawn and answers `permission.request` lines with
Expand Down Expand Up @@ -326,12 +335,25 @@ Gateway and runs Agents. The daemon exits with the app.
on `--quit`, so setup skips `--quit` for it (CloseLinger asks the person to
quit a running app). Retry in that prompt runs the stop again. Nothing running: the stop finds nothing and setup goes on. The
stop also asks the pinned localmodels kit, when that kit is installed in the
home, to end its foundations controller under `runtime/model-services`
(`foundations stop`) and the stack's processes (`stack down`); it downloads
nothing, and Linger does not end the tool's processes itself.
home, to unload every model its model library loaded (`library status`,
then `library unload` for each `loaded` model), to end its foundations
controller under `runtime/model-services` (`foundations stop`), and to stop
the stack's processes and the gateway the tool started, the library's
Bifrost included (`stack down`); it downloads nothing, and Linger does not
end the tool's processes itself. Every step runs even when an earlier one
fails.
On Windows the tool cannot prove and end a controller from a kit that has
no stop command.
The tool refuses the chat stack on Windows, so no engine runs there.
A quit the person chose (the tray, the app menu, the Dock) does the same
for the local models, except the controller, which serves the services
that keep running: `LocalServer` starts `installer.js --home <default
home> --stop-local-models` under `ELECTRON_RUN_AS_NODE`, detached, with
its output in `<home>/installer/local-models-stop.log`, and does not wait
for it. It runs only when `desktop/local-server.json` names an
installation. The Computer exits with the app, so nothing uses the models
until the next launch, whose resume applies the approved plan again. A
quit for an update or a relaunch, and the `--quit` of setup or the
uninstaller (which stop the models themselves right after), leave them.
- `src/main/legacy-migration.ts`: on start, stops a bridge an older version
left running and moves its credential and the Desktop 0.16 daemon identity
into the daemon layout, so the computer keeps its id.
Expand Down
10 changes: 5 additions & 5 deletions apps/desktop/distribution/darwin-arm64.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"platform": "darwin",
"arch": "arm64",
"localmodelsTool": {
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev39/darwin-arm64.tgz",
"sha256": "e15c1d96c4db70cc834b42693228c6b6bbe7eb575e79d19e8aff2f0bf8ba6e48",
"bytes": 14937177,
"sourceCommit": "9a72159a9c114e323de703fcbf6e030fbf7d480a",
"manifestSha256": "b9f750c55834913394ebc459395f5331915d02d98ebeecee3586fb1721a7bc41"
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev40/darwin-arm64.tgz",
"sha256": "648e8909915d8da2b87ae4f4797cd22f2d46d8a7cd025287ab9e6b5436119687",
"bytes": 14953559,
"sourceCommit": "a4de3e359d2a4f9b654f9ae7bdf6887c55896b7d",
"manifestSha256": "15f7138847ef4d13f9c2df455ec04423dc35d30d759d5f952dcb0b69d81db13b"
},
"nativeSources": [
{
Expand Down
10 changes: 5 additions & 5 deletions apps/desktop/distribution/linux-x64.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"platform": "linux",
"arch": "x64",
"localmodelsTool": {
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev39/linux-x64.tgz",
"sha256": "f58f55f94e3559ffd959af9e9f44663a41ec34aef53a1c2d1ff7279ec207af3a",
"bytes": 20776267,
"sourceCommit": "9a72159a9c114e323de703fcbf6e030fbf7d480a",
"manifestSha256": "60664ab53e44b9967bf21e54a2e2fb263862296537ebd24691ab96e7ed268336"
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev40/linux-x64.tgz",
"sha256": "a8e176fa426360f489bf9c552064faf7e1dd88072b463a13185ff1193a223683",
"bytes": 20790291,
"sourceCommit": "a4de3e359d2a4f9b654f9ae7bdf6887c55896b7d",
"manifestSha256": "d36a909ddad7be0de0642766102a5478efd22d7d437c86d8b63ac7bddc24e082"
},
"nativeSources": [
{
Expand Down
10 changes: 5 additions & 5 deletions apps/desktop/distribution/win32-x64.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"platform": "win32",
"arch": "x64",
"localmodelsTool": {
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev39/win32-x64.tgz",
"sha256": "492ed40845a7d732f077b575c7ef4d75f06965c991e5e2c4c0e9f356ed0da37f",
"bytes": 23174114,
"sourceCommit": "9a72159a9c114e323de703fcbf6e030fbf7d480a",
"manifestSha256": "f17122b311b71a3b37e3207a2c0cdce6f84b3a14e09ef0bceed81be74e90e161"
"url": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev40/win32-x64.tgz",
"sha256": "de07306075b5efbf2158c41a7c87fe4939bac45b9b6f953270ed00017a6540be",
"bytes": 23191788,
"sourceCommit": "a4de3e359d2a4f9b654f9ae7bdf6887c55896b7d",
"manifestSha256": "c7e2fab80d7aa2bdf52975a0b6e180611830a5d3c013bb302d16ff5c3c0d42dd"
},
"nativeSources": [
{
Expand Down
53 changes: 53 additions & 0 deletions apps/desktop/src/main/computer-daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,57 @@ describe.skipIf(process.platform === "win32")("spawnDaemon", () => {
expect(reason).toBe("read Computer config: denied");
expect(readFileSync(log, "utf8")).toContain("starting\n");
});

const exitOf = async (script: string) => {
const directory = mkdtempSync(path.join(os.tmpdir(), "spawn-daemon-"));
const executable = path.join(directory, "daemon");
writeFileSync(executable, `#!/bin/sh\n${script}`);
chmodSync(executable, 0o755);
const log = path.join(directory, "logs", "computer-daemon.log");
const child = spawnDaemon(executable, log)("config.json", {});
return new Promise<[number | null, string?]>((resolve) =>
child.onExit((...exit) => resolve(exit)),
);
};

it("never takes a log record for the exit reason", async () => {
const [code, reason] = await exitOf(
[
'echo "2026/11/17 20:26:26 INFO local_models.job_finished action=download exit_code=0" >&2',
'echo "2026/11/17 20:29:38 WARN runtime.diagnostic type=pi status=ready" >&2',
"kill -9 $$",
].join("\n"),
);
expect(code).toBeNull();
expect(reason).toBeUndefined();
});

it("keeps the error the daemon printed when log records follow it", async () => {
const [, reason] = await exitOf(
[
'echo "2026/11/17 20:26:26 INFO daemon.started" >&2',
'echo "read Computer config: private path is not owned by the current user" >&2',
'echo "2026/11/17 20:26:27 INFO runner.shutdown phase=completed" >&2',
"exit 1",
].join("\n"),
);
expect(reason).toBe(
"read Computer config: private path is not owned by the current user",
);
});

it("reports a Go panic by its panic line, not its stack trace", async () => {
const [code, reason] = await exitOf(
[
'echo "panic: assignment to entry in nil map" >&2',
"echo >&2",
'echo "goroutine 1 [running]:" >&2',
'echo "main.main()" >&2',
'echo "\t/src/main.go:10 +0x1d" >&2',
"exit 2",
].join("\n"),
);
expect(code).toBe(2);
expect(reason).toBe("panic: assignment to entry in nil map");
});
});
14 changes: 13 additions & 1 deletion apps/desktop/src/main/computer-daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ export class ComputerDaemonModule implements Module {

// Starts the daemon executable with only the given environment. Its stderr
// (the daemon log) and any stdout that is not a status line go to logFile.
// A record of the daemon's log: Go's log package prefix, then the level.
const LOG_RECORD =
/^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? (?:DEBUG|INFO|WARN|ERROR)\b/;
// The first line of a Go runtime crash.
const GO_CRASH = /^(?:panic|fatal error): /;

export const spawnDaemon =
(executable: string, logFile: string): SpawnDaemon =>
(configFile, env) => {
Expand Down Expand Up @@ -576,9 +582,15 @@ export const spawnDaemon =
});
};
child.stderr.pipe(log, { end: false });
// The daemon prints why it stopped as its last line outside its log;
// its log records (a timestamp, then the level) may come from any time
// before. A Go runtime crash leads with its panic line, then a trace.
let crashed = false;
createInterface({ input: child.stderr }).on("line", (line) => {
const text = line.trim();
if (text) reason = text.slice(0, 500);
if (!text || crashed || LOG_RECORD.test(text)) return;
crashed = GO_CRASH.test(text);
reason = text.slice(0, 500);
});
createInterface({ input: child.stdout }).on("line", (line) => {
if (line.startsWith("{")) for (const listener of lines) listener(line);
Expand Down
20 changes: 14 additions & 6 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,14 @@ async function main(): Promise<void> {
const host = new ModuleHost(modules, { config, log, bus: new EventBus() });

const showApp = (): void => setup.showApp();
// The uninstaller, or setup replacing this release, asked to quit.
let replacing = false;
app.on("second-instance", (_event, argv) => {
// Without asking: the uninstaller is removing the app.
if (quitRequested(argv)) app.quit();
else showApp();
if (quitRequested(argv)) {
replacing = true;
app.quit();
} else showApp();
});
app.on("activate", showApp);
// Closing the window only hides it; the tray keeps the app running.
Expand All @@ -340,10 +344,14 @@ async function main(): Promise<void> {
if (quitting) return;
quitting = true;
event.preventDefault();
void host.stop(relaunch || updating ? "update" : "quit").finally(() => {
if (relaunch) app.relaunch();
app.exit(0);
});
// Setup and the uninstaller stop the services and the local models
// themselves right after --quit; a stop from here would race theirs.
void host
.stop(relaunch || updating || replacing ? "update" : "quit")
.finally(() => {
if (relaunch) app.relaunch();
app.exit(0);
});
});

// Tasks run in the app, so quitting takes the computer offline; ask first
Expand Down
Loading
Loading