feat(cli): claudemesh daemon — peer mesh runtime (v0.9.0)
Long-lived process that holds a persistent WS to the broker and exposes
a local IPC surface (UDS + bearer-auth TCP loopback). Implements the
v0.9.0 spec under .artifacts/specs/.
Core:
- daemon up | status | version | down | accept-host
- daemon outbox list [--failed|--pending|--inflight|--done|--aborted]
- daemon outbox requeue <id> [--new-client-id <id>]
- daemon install-service / uninstall-service (macOS launchd, Linux systemd)
IPC routes:
- /v1/version, /v1/health
- /v1/send (POST) — full §4.5.1 idempotency lookup table
- /v1/inbox (GET) — paged history
- /v1/events — SSE stream of message/peer_join/peer_leave/broker_status
- /v1/peers — broker passthrough
- /v1/profile — summary/status/visible/avatar/title/bio/capabilities
- /v1/outbox + /v1/outbox/requeue — operator recovery
Storage (SQLite via node:sqlite / bun:sqlite):
- outbox.db: pending/inflight/done/dead/aborted with audit columns
- inbox.db: dedupe by client_message_id, decrypts DMs via existing crypto
- BEGIN IMMEDIATE serialization for daemon-local accept races
Identity:
- host_fingerprint.json (machine-id || first-stable-mac)
- refuse-on-mismatch policy with `daemon accept-host` recovery
CLI integration:
- claudemesh send detects the daemon and routes through /v1/send when
present, falling back to bridge socket / cold path otherwise
Tests: 15-case coverage of the §4.5.1 IPC duplicate lookup table.
Spec arc preserved at .artifacts/specs/2026-05-03-daemon-{v1..v10}.md;
v0.9.0 implementation target locked at 2026-05-03-daemon-spec-v0.9.0.md;
deferred items at 2026-05-03-daemon-spec-broker-hardening-followups.md.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
76
apps/cli/src/daemon/db/sqlite.ts
Normal file
76
apps/cli/src/daemon/db/sqlite.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
// SQLite shim. The daemon runs under Node 22.5+ in production (node:sqlite).
|
||||
// During local dev (bun src/entrypoints/cli.ts daemon up) we fall back to
|
||||
// bun:sqlite, which has a near-identical API surface for what we use.
|
||||
|
||||
export type SqliteDb = {
|
||||
prepare(sql: string): {
|
||||
run(...params: unknown[]): { changes: number; lastInsertRowid: number | bigint };
|
||||
get<T = unknown>(...params: unknown[]): T | undefined;
|
||||
all<T = unknown>(...params: unknown[]): T[];
|
||||
};
|
||||
exec(sql: string): void;
|
||||
close(): void;
|
||||
};
|
||||
|
||||
interface DatabaseCtor {
|
||||
new (path: string): SqliteDb;
|
||||
}
|
||||
|
||||
let cached: DatabaseCtor | null = null;
|
||||
|
||||
async function loadSqlite(): Promise<DatabaseCtor> {
|
||||
if (cached) return cached;
|
||||
|
||||
// Prefer node:sqlite (production runtime).
|
||||
try {
|
||||
const mod = (await import("node:sqlite")) as { DatabaseSync: DatabaseCtor };
|
||||
cached = mod.DatabaseSync;
|
||||
return cached;
|
||||
} catch (nodeErr) {
|
||||
// Dev path: bun:sqlite. Bun's Database has prepare/exec/close already.
|
||||
try {
|
||||
const bunMod = (await import("bun:sqlite")) as { Database: DatabaseCtor };
|
||||
cached = bunMod.Database;
|
||||
return cached;
|
||||
} catch {
|
||||
const msg = `claudemesh daemon requires Node.js 22.5+ for the embedded SQLite store ` +
|
||||
`(node:sqlite), or Bun (bun:sqlite) for dev. ` +
|
||||
`Current: ${process.version}. Original error: ${String(nodeErr)}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function openSqlite(path: string): Promise<SqliteDb> {
|
||||
const Database = await loadSqlite();
|
||||
const db = new Database(path);
|
||||
// Default pragmas for daemon use:
|
||||
// journal_mode WAL — concurrent reads while one writer is in BEGIN IMMEDIATE.
|
||||
// synchronous NORMAL — balance durability/throughput; daemon is the only writer.
|
||||
// foreign_keys ON — enforce constraints if any are added later.
|
||||
// busy_timeout — let BEGIN IMMEDIATE wait briefly for a contending writer.
|
||||
db.exec(`
|
||||
PRAGMA journal_mode = WAL;
|
||||
PRAGMA synchronous = NORMAL;
|
||||
PRAGMA foreign_keys = ON;
|
||||
PRAGMA busy_timeout = 5000;
|
||||
`);
|
||||
return db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run `fn` inside a `BEGIN IMMEDIATE` transaction. Per spec §4.5.1, this is
|
||||
* what serializes IPC accept against concurrent same-id requests; SQLite has
|
||||
* no row-level lock and `SELECT FOR UPDATE` is not supported.
|
||||
*/
|
||||
export function inImmediateTx<T>(db: SqliteDb, fn: () => T): T {
|
||||
db.exec("BEGIN IMMEDIATE");
|
||||
try {
|
||||
const out = fn();
|
||||
db.exec("COMMIT");
|
||||
return out;
|
||||
} catch (err) {
|
||||
try { db.exec("ROLLBACK"); } catch { /* ignore */ }
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user