Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crosshead/runtime",
"version": "0.5.0-alpha.7",
"version": "0.5.0-alpha.8",
"description": "Crosshead local runtime: persistent memory for Claude Code, Codex, Pi, Hermes and OpenCode. CLI, daemon, host adapters and hooks.",
"license": "Apache-2.0",
"private": true,
Expand Down
20 changes: 13 additions & 7 deletions src/runtime/daemon-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const runOnlyBotsDaemonCli = async (
let cleanup: Promise<void> | null = null;
const closeComposition = () => cleanup ??= composition?.close() ?? Promise.resolve();
let daemon: OnlyBotsLocalRuntimeDaemon;
let removeSignalHandlers = () => {};
try {
if (organizationalWorkerConfigPath && composeOrganizationalWorker) {
composition = await composeOrganizationalWorker({
Expand All @@ -96,13 +97,8 @@ export const runOnlyBotsDaemonCli = async (
? { organizationalWorkerConfigPath }
: {}),
});
await daemon.start(port, ownership);
} catch (error) {
await closeComposition().catch(() => undefined);
await ownership.close();
throw error;
}

// Readiness is published inside start(). Arm signals first; stop() waits
// for an in-progress start before quiescing writers and releasing ownership.
let shuttingDown = false;
const shutdown = async () => {
if (shuttingDown) return;
Expand All @@ -125,4 +121,14 @@ export const runOnlyBotsDaemonCli = async (
};
process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);
removeSignalHandlers = () => { process.off("SIGTERM", shutdown); process.off("SIGINT", shutdown); };
await daemon.start(port, ownership);
} catch (error) {
removeSignalHandlers();
await closeComposition().catch(() => undefined);
await ownership.close();
throw error;
}


};
26 changes: 24 additions & 2 deletions tests/runtime/daemon-ownership.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const fixture = async (run: (home: string) => Promise<void>) => {
ensureRuntimeHomeLayout(home);
try { await run(home); } finally { rmSync(home, { recursive: true, force: true }); }
};
const childDaemon = (home: string, extra: string[] = []) => {
const childDaemon = (home: string, extra: string[] = [], entry = path.resolve("dist/runtime/daemon.js")) => {
// Build before this suite: these checks exercise the shipped CLI in another
// OS process, including signal handlers and the parent-death kernel lock.
const child = spawn(process.execPath, [path.resolve("dist/runtime/daemon.js"), "serve", "--home", home, ...extra], {
const child = spawn(process.execPath, [entry, "serve", "--home", home, ...extra], {
stdio: ["ignore", "ignore", "pipe"], env: { PATH: process.env.PATH },
});
let error = "";
Expand Down Expand Up @@ -62,6 +62,28 @@ const ready = async (home: string, job: Job) => {
};

describe("single-home daemon ownership", () => {
it("handles SIGTERM while startup has published readiness but has not returned", async () => fixture(async home => {
const entry=path.join(home,"paused-start.cjs");
writeFileSync(entry,`
const {OnlyBotsLocalRuntimeDaemon}=require(${JSON.stringify(path.resolve("dist/runtime/local-runtime.js"))});
const original=OnlyBotsLocalRuntimeDaemon.prototype.start;
OnlyBotsLocalRuntimeDaemon.prototype.start=async function(...args){
await original.apply(this,args);
await new Promise(()=>{}); // Deterministically hold the readiness boundary.
};
require(${JSON.stringify(path.resolve("dist/runtime/daemon-host.js"))}).runOnlyBotsDaemonCli().catch(error=>{console.error(error.message);process.exit(1);});
`,{mode:0o600});
const job=childDaemon(home,[],entry);
try {
await ready(home,job);
await stopDetachedRuntimeDaemon(home);
expect(await waitTerminal(job)).toEqual({code:0,signal:null});
expect(loadDaemonState(ensureRuntimeHomeLayout(home)).status).toBe("stopped");
const successor=new OnlyBotsLocalRuntimeDaemon(home);
try {await successor.start();} finally {await successor.stop();}
} finally {await cleanup(job);}
}),15000);

it("excludes another instance and a failed contender stop cannot alter owner state", async () => fixture(async home => {
const owner = new OnlyBotsLocalRuntimeDaemon(home);
const contender = new OnlyBotsLocalRuntimeDaemon(home);
Expand Down
Loading