diff --git a/packages/cli/src/fastPath.ts b/packages/cli/src/fastPath.ts index 0111f3cb25..9ae409cf44 100644 --- a/packages/cli/src/fastPath.ts +++ b/packages/cli/src/fastPath.ts @@ -55,6 +55,10 @@ export function messagingOnly(config: { messaging_only?: boolean } | null | unde return config?.messaging_only === true; } +/** Why `cast start` (and the installer, which runs it) starts nothing here. */ +export const MESSAGING_ONLY_NO_DAEMON_MESSAGE = + "This machine is messaging-only, so the full daemon does not run here: cast relay delivers its messages. To run the daemon, set cast config messaging_only false."; + export function isTaskContextFastPath(argv: string[]): boolean { return isSessionStartVerb(argv, "_task-context"); } diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 7eb48b8565..3d0cde9227 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -148,7 +148,7 @@ import { isManagedInstall } from "./managedInstall.js"; import { checkForUpdates, performUpdate, UPDATE_IN_PROGRESS, showUpdateNotice, getVersion, getMemoryVersion, getTaskVersion, getWorkVersion, getWorkflowVersion, getMessagingVersion, getVisualVersion, getForksVersion, getPublishVersion, getStateVersion, getBrowserVersion, getChatVersion, ensureCastAlias, isDevMode, updateRecentlyFailed, recordUpdateFailure, getDecideVersion, getCallsVersion, getLimitsVersion, getComputerVersion, getCheckVersion, getSkillsVersion, getPrVersion, getModsVersion, getSimVersion, getVaultVersion} from "./update.js"; import { type SectionSpec, getSnippetTargets, installSectionToTargets, cutOwnedSections, REFERENCES_SECTION, installMessagingSnippet, installReferencesSnippet, installPublishSnippet, installBrowserSnippet, installChatSnippet, snippetStale, stampSnippet } from "./snippets.js"; import { installAllStableHooks, parseStableHookClient, removeAllStableHooks, runStableContextHook } from "./stableContext.js"; -import { isCredentialHelperFastPath, isRelayHookArgv, isStableContextFastPath as isStableContextFastPathArgv, isTaskContextFastPath, messagingOnly, runFastPath } from "./fastPath.js"; +import { isCredentialHelperFastPath, isRelayHookArgv, isStableContextFastPath as isStableContextFastPathArgv, isTaskContextFastPath, MESSAGING_ONLY_NO_DAEMON_MESSAGE, messagingOnly, runFastPath } from "./fastPath.js"; import { DAEMON_BUILD_ID } from "./daemonBuildId.js"; import { BUILD_ID_VALUE_RE, daemonBuildUnchanged } from "./daemonBuildGate.js"; import { DAEMON_STOP_SIGKILL_MS } from "./shutdownBudget.js"; @@ -1619,6 +1619,13 @@ function startDaemon(opts: { afterStop?: boolean } = {}): void { console.error(WINDOWS_DAEMON_UNSUPPORTED_MESSAGE); return; } + // A messaging-only machine runs cast relay instead, and the daemon and the + // relay must never both deliver one message. Every start path comes through + // here: cast start, cast restart, and the installer re-run on a signed-in machine. + if (messagingOnly(readConfig())) { + console.log(MESSAGING_ONLY_NO_DAEMON_MESSAGE); + return; + } ensureConfigDir(); if (!ensureTmux()) { @@ -4957,8 +4964,8 @@ program const startedAt = Date.now(); startDaemon(); // startDaemon only explains the refusal; a script asking for the daemon - // on native Windows must see it did not start. - if (!daemonSupportedOnPlatform()) process.exit(1); + // on native Windows or a messaging-only machine must see it did not start. + if (!daemonSupportedOnPlatform() || messagingOnly(readConfig())) process.exit(1); // Ensure autostart is configured so daemon restarts on reboot/crash ensureAutostart(); if (options.wait) { @@ -9630,6 +9637,8 @@ function ensureAutostart(): boolean { const platform = process.platform; // Something else supervises the daemon: a second, user-level one would race it. if (autostartDisabledByEnv()) return false; + // Nothing may restart a daemon a messaging-only machine never runs. + if (messagingOnly(readConfig())) return false; if (findSystemDaemonUnit()) { retireUserDaemonService(); return false; diff --git a/packages/cli/src/messagingOnlyStart.cli.test.ts b/packages/cli/src/messagingOnlyStart.cli.test.ts new file mode 100644 index 0000000000..8e9ee7f47a --- /dev/null +++ b/packages/cli/src/messagingOnlyStart.cli.test.ts @@ -0,0 +1,37 @@ +// A messaging-only machine runs cast relay, never the full daemon (#469): +// re-running the installer on a signed-in machine runs `cast start`, which +// must neither start the daemon nor write the unit that restarts it. +import { afterEach, expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { existsSync, mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const roots: string[] = []; +const cli = fileURLToPath(new URL("./main.ts", import.meta.url)); + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +test("cast start on a messaging-only machine starts no daemon and writes no autostart", () => { + const home = mkdtempSync(join(tmpdir(), "codecast-messaging-only-")); + roots.push(home); + mkdirSync(join(home, ".codecast")); + writeFileSync(join(home, ".codecast", "config.json"), JSON.stringify({ + auth_token: "test-only-token", + convex_url: "https://codecast.invalid", + messaging_only: true, + })); + const run = spawnSync(process.execPath, ["--no-env-file", cli, "start"], { + env: { PATH: process.env.PATH!, HOME: home, XDG_CONFIG_HOME: join(home, ".config"), NODE_ENV: "test" }, + encoding: "utf-8", + timeout: 20_000, + }); + expect(run.stdout).toContain("This machine is messaging-only, so the full daemon does not run here"); + expect(run.status).toBe(1); + expect(existsSync(join(home, ".codecast", "daemon.pid"))).toBe(false); + expect(existsSync(join(home, ".config", "systemd", "user", "codecast.service"))).toBe(false); + expect(existsSync(join(home, "Library", "LaunchAgents", "sh.codecast.daemon.plist"))).toBe(false); +});