Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: daemon WS push channel with poll fallback (#89)

Generbb6ca538a583
diff --git a/src/cli/daemon/client.test.ts b/src/cli/daemon/client.test.ts
index dd7f7598d..4f0e37500 100644
--- a/src/cli/daemon/client.test.ts
+++ b/src/cli/daemon/client.test.ts
@@ -418,15 +418,72 @@ describe("DaemonClient retries on TypeError (network failure)", () => {
});
// ---------------------------------------------------------------------------
-// DaemonClient has no heartbeat() or claimTask() methods
+// DaemonClient.heartbeat() tests
// ---------------------------------------------------------------------------
-describe("DaemonClient removed methods", () => {
- it("does not have heartbeat method", () => {
+describe("DaemonClient.heartbeat() with mocked fetch", () => {
+ const originalFetch = globalThis.fetch;
+
+ afterEach(() => {
+ globalThis.fetch = originalFetch;
+ });
+
+ it("sends daemon_id in body to /api/daemon/heartbeat", async () => {
+ globalThis.fetch = vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ json: () => Promise.resolve({ ok: true }),
+ });
+
const client = new DaemonClient("http://localhost:8080");
- expect((client as any).heartbeat).toBeUndefined();
+ await client.heartbeat("tok", "d1");
+
+ expect(globalThis.fetch).toHaveBeenCalledWith(
+ "http://localhost:8080/api/daemon/heartbeat",
+ expect.objectContaining({
+ method: "POST",
+ body: JSON.stringify({ daemon_id: "d1" }),
+ }),
+ );
});
+});
+
+// ---------------------------------------------------------------------------
+// DaemonClient.sweep() tests
+// ---------------------------------------------------------------------------
+
+describe("DaemonClient.sweep() with mocked fetch", () => {
+ const originalFetch = globalThis.fetch;
+ afterEach(() => {
+ globalThis.fetch = originalFetch;
+ });
+
+ it("sends daemon_id in body to /api/daemon/sweep", async () => {
+ globalThis.fetch = vi.fn().mockResolvedValue({
+ ok: true,
+ status: 200,
+ json: () => Promise.resolve({ ok: true }),
+ });
+
+ const client = new DaemonClient("http://localhost:8080");
+ await client.sweep("tok", "d1");
+
+ expect(globalThis.fetch).toHaveBeenCalledWith(
+ "http://localhost:8080/api/daemon/sweep",
+ expect.objectContaining({
+ method: "POST",
+ body: JSON.stringify({ daemon_id: "d1" }),
+ }),
+ );
+ });
+});
+
+// ---------------------------------------------------------------------------
+// DaemonClient removed methods
+// ---------------------------------------------------------------------------
+
+describe("DaemonClient removed methods", () => {
it("does not have claimTask method", () => {
const client = new DaemonClient("http://localhost:8080");
expect((client as any).claimTask).toBeUndefined();
diff --git a/src/cli/daemon/client.ts b/src/cli/daemon/client.ts
index f0aa1375f..6491b1228 100644
--- a/src/cli/daemon/client.ts
+++ b/src/cli/daemon/client.ts
@@ -65,6 +65,18 @@ export class DaemonClient {
return RegisterResponseSchema.parse(raw);
}
+ heartbeat(token: string, daemonId: string): Promise<unknown> {
+ return this.request("POST", "/api/daemon/heartbeat", token, {
+ daemon_id: daemonId,
+ });
+ }
+
+ sweep(token: string, daemonId: string): Promise<unknown> {
+ return this.request("POST", "/api/daemon/sweep", token, {
+ daemon_id: daemonId,
+ });
+ }
+
deregister(token: string, daemonId: string) {
return this.request("POST", "/api/daemon/deregister", token, {
daemon_id: daemonId,
diff --git a/src/cli/daemon/config.ts b/src/cli/daemon/config.ts
index a31ba6573..14d8d072d 100644
--- a/src/cli/daemon/config.ts
+++ b/src/cli/daemon/config.ts
@@ -69,6 +69,9 @@ export interface DaemonConfig {
codexModel: string;
opencodeModel: string;
pollInterval: number;
+ wsPollInterval: number;
+ heartbeatInterval: number;
+ sweepInterval: number;
agentTimeout: number;
messageInactivityTimeout: number;
maxConcurrentTasks: number;
@@ -104,6 +107,9 @@ export function loadDaemonConfig(profile?: string): DaemonConfig {
pollInterval: parseDuration(
process.env.ALOOK_DAEMON_POLL_INTERVAL || "3s",
),
+ wsPollInterval: parseDuration(process.env.ALOOK_DAEMON_WS_POLL_INTERVAL || "60s"),
+ heartbeatInterval: parseDuration(process.env.ALOOK_DAEMON_HEARTBEAT_INTERVAL || "5s"),
+ sweepInterval: parseDuration(process.env.ALOOK_DAEMON_SWEEP_INTERVAL || "60s"),
agentTimeout: parseDuration(process.env.ALOOK_AGENT_TIMEOUT || "12h"),
messageInactivityTimeout: parseDuration(process.env.ALOOK_MESSAGE_INACTIVITY_TIMEOUT || "20m"),
maxConcurrentTasks: parseInt(
diff --git a/src/cli/daemon/daemon.test.ts b/src/cli/daemon/daemon.test.ts
index f11fe5daa..1612354fc 100644
--- a/src/cli/daemon/daemon.test.ts
+++ b/src/cli/daemon/daemon.test.ts
@@ -14,6 +14,7 @@ const mockClientInstance = {
failTask: vi.fn(async () => ({})),
supersedeTask: vi.fn(async () => ({})),
reportMessages: vi.fn(async () => ({})),
+ sweep: vi.fn(async () => ({})),
};
vi.mock("./client.js", () => {
function MockDaemonClient() { return mockClientInstance; }
@@ -37,6 +38,8 @@ vi.mock("./config.js", () => ({
deviceName: "test-host",
workspacesRoot: path.join("/tmp", "ws"),
cliVersion: "0.1.0",
+ wsPollInterval: 30000,
+ sweepInterval: 60000,
})),
sessionRunnerLogDir: vi.fn(() => path.join("/tmp", "alook", "daemon", "session-runners")),
daemonLogFilePath: vi.fn(() => path.join("/tmp", "alook", "daemon", "logs", "2026-01-01.log")),
@@ -52,6 +55,15 @@ vi.mock("./health.js", () => ({
})),
}));
+vi.mock("./ws-client.js", () => {
+ class MockDaemonWsClient {
+ connect = vi.fn();
+ close = vi.fn();
+ isConnected = vi.fn(() => false);
+ }
+ return { DaemonWsClient: MockDaemonWsClient };
+});
+
vi.mock("./agent/index.js", () => ({
detectVersion: vi.fn(async () => "1.0.0"),
}));
@@ -574,7 +586,7 @@ describe("daemon shutdown", () => {
it("clears poll interval before calling deregister", async () => {
await startDaemon();
- expect(intervalTimers.length).toBe(1);
+ expect(intervalTimers.length).toBe(3); // pollTimer + heartbeatTimer + sweepTimer
const pollTimer = intervalTimers[0];
const deregisterMock = mockClientInstance.deregister;
@@ -597,7 +609,7 @@ describe("daemon shutdown", () => {
await shutdownHandler!();
expect(clearedTimers).toContain(pollTimer);
- expect(deregisterCalledAt).toBe(1);
+ expect(deregisterCalledAt).toBe(3); // After clearing all 3 timers (poll + heartbeat + sweep)
});
it("deregisters each workspace with correct token on shutdown", async () => {
diff --git a/src/cli/daemon/daemon.ts b/src/cli/daemon/daemon.ts
index d0e0fe9db..e0029d7c0 100644
--- a/src/cli/daemon/daemon.ts
+++ b/src/cli/daemon/daemon.ts
@@ -6,6 +6,8 @@ import { type Task, type SessionRunnerInput, fromApiTask } from "./types.js";
import { type MarkerData, writeMarkerFile } from "./session-runner.js";
import { loadCLIConfigForProfile, saveCLIConfigForProfile } from "../lib/config.js";
import { createLogger } from "../lib/logger.js";
+import { DaemonWsClient } from "./ws-client.js";
+import type { DaemonPushMessage } from "@alook/shared";
const log = createLogger({ module: "daemon" });
import { cmdPrefix } from "../lib/env.js";
@@ -482,19 +484,162 @@ export async function startDaemon(
evictWorkspace(id);
}
- try {
- await reconcilePendingCompletions(config.workspacesRoot);
- } catch (e) {
- log.debug("reconciliation error", e);
- }
-
if (workspaceStates.length === 0) {
log.info("All workspaces evicted — shutting down");
shutdown();
}
};
- const pollTimer = setInterval(pollCycle, config.pollInterval);
+ let pollTimer = setInterval(pollCycle, config.pollInterval);
+
+ // --- Heartbeat timer (independent of poll and WS state) ---
+ const heartbeatPing = () => {
+ for (const ws of workspaceStates) {
+ client.heartbeat(ws.token, config.daemonId).catch((e) => {
+ log.debug("heartbeat failed", { workspaceId: ws.workspaceId, err: String(e) });
+ });
+ }
+ };
+ const heartbeatTimer = setInterval(heartbeatPing, config.heartbeatInterval);
+
+ // --- WS Push Channel (primary) + Poll fallback ---
+ // Any active machine token suffices — WS auth validates daemon-level access,
+ // not per-workspace. Tasks are routed by workspaceId within handleWsPush.
+ const firstToken = workspaceStates[0]?.token;
+
+ function updatePollInterval(newInterval: number) {
+ clearInterval(pollTimer);
+ pollTimer = setInterval(pollCycle, newInterval);
+ }
+
+ function handleWsPush(msg: DaemonPushMessage) {
+ switch (msg.type) {
+ case "daemon.tasks":
+ for (const apiTask of msg.tasks) {
+ if (activeTasks.size >= config.maxConcurrentTasks) break;
+ const task = fromApiTask(apiTask);
+ if (activeTasks.has(task.id)) continue;
+ const ws = workspaceStates.find((w) => w.workspaceId === task.workspaceId);
+ if (!ws) continue;
+ syncAgentId(task.agentId, ws.workspaceId);
+ activeTasks.add(task.id);
+ handleTask(client, config, runtimeIndex, task, ws.token, activeTasks)
+ .catch((e) => { log.error("WS task error", e); activeTasks.delete(task.id); });
+ }
+ break;
+
+ case "daemon.file_requests": {
+ const ws = workspaceStates.find((w) => w.workspaceId === msg.workspaceId);
+ if (ws) {
+ for (const req of msg.requests) {
+ handleFileRequest(client, config, ws.workspaceId, req, ws.token)
+ .catch((e) => log.debug("WS file request error", e));
+ }
+ }
+ break;
+ }
+
+ case "daemon.meetings":
+ for (const m of msg.meetings) {
+ const ws = workspaceStates.find((w) => w.workspaceId === m.workspace_id);
+ if (!ws) continue;
+ const agentBaseDir = join(config.workspacesRoot, m.workspace_id, m.agent_id, "workdir");
+ const timelineDir = join(agentBaseDir, ".context_timeline");
+ spawnMeetingRunner({
+ meetingId: m.id,
+ meetingUrl: m.meeting_url,
+ participants: m.participants,
+ workspaceId: m.workspace_id,
+ callbackUrl: config.serverURL,
+ authToken: ws.token,
+ agentName: m.agent_name,
+ agentId: m.agent_id,
+ timelineDir,
+ title: m.title,
+ });
+ }
+ break;
+
+ case "daemon.evict":
+ evictWorkspace(msg.workspaceId);
+ break;
+
+ case "daemon.update":
+ if (!isUpdating() && msg.version !== config.cliVersion) {
+ handleCliUpdate(msg.version, () => requestRestart(), profile);
+ }
+ break;
+
+ case "daemon.rescan":
+ log.info("WS rescan requested — restarting daemon");
+ requestRestart();
+ break;
+
+ case "daemon.kill": {
+ const ws = workspaceStates.find((w) => w.workspaceId === msg.workspaceId);
+ if (ws) {
+ const killTask = fromApiTask({
+ id: msg.taskId,
+ agent_id: "",
+ runtime_id: "",
+ conversation_id: "",
+ workspace_id: ws.workspaceId,
+ prompt: "",
+ status: "queued",
+ priority: 0,
+ dispatched_at: null,
+ started_at: null,
+ completed_at: null,
+ result: null,
+ error: null,
+ created_at: new Date().toISOString(),
+ type: "kill_task",
+ context: { target_task_id: msg.targetTaskId },
+ agent: null,
+ sender: null,
+ });
+ activeTasks.add(killTask.id);
+ handleTask(client, config, runtimeIndex, killTask, ws.token, activeTasks)
+ .catch((e) => { log.error("WS kill task error", e); activeTasks.delete(killTask.id); });
+ }
+ break;
+ }
+ }
+ }
+
+ const wsClient = firstToken
+ ? new DaemonWsClient({
+ serverURL: config.serverURL,
+ daemonId: config.daemonId,
+ machineToken: firstToken,
+ onMessage: handleWsPush,
+ onConnected: () => {
+ log.info("WS connected — switching to low-frequency poll");
+ updatePollInterval(config.wsPollInterval);
+ },
+ onDisconnected: () => {
+ log.info("WS disconnected — reverting to high-frequency poll");
+ updatePollInterval(config.pollInterval);
+ },
+ })
+ : null;
+
+ wsClient?.connect();
+
+ // --- Sweep timer: triggers server-side sweep + local reconciliation ---
+ const sweepTick = async () => {
+ for (const ws of workspaceStates) {
+ client.sweep(ws.token, config.daemonId).catch((e) => {
+ log.debug("sweep ping failed", { workspaceId: ws.workspaceId, err: String(e) });
+ });
+ }
+ try {
+ await reconcilePendingCompletions(config.workspacesRoot);
+ } catch (e) {
+ log.debug("reconciliation error", e);
+ }
+ };
+ const sweepTimer = setInterval(sweepTick, config.sweepInterval);
let shuttingDown = false;
let restartRequested = false;
@@ -509,6 +654,9 @@ export async function startDaemon(
shuttingDown = true;
log.info(restartRequested ? "Restarting..." : "Shutting down...");
clearInterval(pollTimer);
+ clearInterval(heartbeatTimer);
+ clearInterval(sweepTimer);
+ wsClient?.close();
const shutdownMs = restartRequested ? 30000 : (Number(process.env.ALOOK_SHUTDOWN_TIMEOUT_MS) || 5000);
const timeout = setTimeout(() => process.exit(1), shutdownMs);
diff --git a/src/cli/daemon/ws-client.test.ts b/src/cli/daemon/ws-client.test.ts
new file mode 100644
index 000000000..3725dcd47
--- /dev/null
+++ b/src/cli/daemon/ws-client.test.ts
@@ -0,0 +1,183 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+import { DaemonWsClient } from "./ws-client.js";
+
+class MockWebSocket extends EventTarget {
+ static OPEN = 1;
+ static CLOSED = 3;
+
+ readyState = MockWebSocket.OPEN;
+ url: string;
+ send = vi.fn();
+
+ constructor(url: string) {
+ super();
+ this.url = url;
+ }
+
+ close() {
+ this.readyState = MockWebSocket.CLOSED;
+ this.dispatchEvent(new Event("close"));
+ }
+
+ simulateOpen() {
+ this.dispatchEvent(new Event("open"));
+ }
+
+ simulateMessage(data: string) {
+ this.dispatchEvent(new MessageEvent("message", { data }));
+ }
+
+ simulateClose() {
+ this.readyState = MockWebSocket.CLOSED;
+ this.dispatchEvent(new Event("close"));
+ }
+}
+
+function makeClient(overrides: Partial<ConstructorParameters<typeof DaemonWsClient>[0]> = {}) {
+ return new DaemonWsClient({
+ serverURL: "https://alook.ai",
+ daemonId: "my-host",
+ machineToken: "al_test123",
+ onMessage: vi.fn(),
+ onConnected: vi.fn(),
+ onDisconnected: vi.fn(),
+ ...overrides,
+ });
+}
+
+describe("DaemonWsClient", () => {
+ let originalWebSocket: typeof globalThis.WebSocket;
+
+ beforeEach(() => {
+ vi.useFakeTimers();
+ originalWebSocket = globalThis.WebSocket;
+ (globalThis as any).WebSocket = MockWebSocket;
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ globalThis.WebSocket = originalWebSocket;
+ });
+
+ it("constructs production URL correctly", () => {
+ const client = makeClient();
+ expect(client.getUrl()).toBe("wss://alook.ai/api/ws/daemon?daemonId=my-host");
+ });
+
+ it("constructs local development URL correctly", () => {
+ const client = makeClient({ serverURL: "http://localhost:3000" });
+ expect(client.getUrl()).toBe("ws://localhost:8789/?daemonId=my-host");
+ });
+
+ it("reports disconnected initially", () => {
+ const client = makeClient();
+ expect(client.isConnected()).toBe(false);
+ });
+
+ it("sends auth message on open", () => {
+ const client = makeClient();
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+
+ expect(ws.send).toHaveBeenCalledWith(JSON.stringify({
+ type: "auth",
+ machineToken: "al_test123",
+ daemonId: "my-host",
+ }));
+ });
+
+ it("sets connected=true and calls onConnected after auth.ok", () => {
+ const onConnected = vi.fn();
+ const client = makeClient({ onConnected });
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+ ws.simulateMessage(JSON.stringify({ type: "auth.ok" }));
+
+ expect(client.isConnected()).toBe(true);
+ expect(onConnected).toHaveBeenCalledTimes(1);
+ });
+
+ it("validates messages with DaemonPushMessageSchema — valid message calls onMessage", () => {
+ const onMessage = vi.fn();
+ const client = makeClient({ onMessage });
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+ ws.simulateMessage(JSON.stringify({ type: "auth.ok" }));
+ ws.simulateMessage(JSON.stringify({ type: "daemon.rescan" }));
+
+ expect(onMessage).toHaveBeenCalledTimes(1);
+ expect(onMessage).toHaveBeenCalledWith({ type: "daemon.rescan" });
+ });
+
+ it("invalid message (bad schema) does not call onMessage", () => {
+ const onMessage = vi.fn();
+ const client = makeClient({ onMessage });
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+ ws.simulateMessage(JSON.stringify({ type: "auth.ok" }));
+ ws.simulateMessage(JSON.stringify({ type: "unknown.garbage", foo: "bar" }));
+
+ expect(onMessage).not.toHaveBeenCalled();
+ });
+
+ it("schedules reconnect on close (verify reconnectTimer is set)", () => {
+ const client = makeClient();
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+ ws.simulateClose();
+
+ expect((client as any).reconnectTimer).not.toBeNull();
+ });
+
+ it("does not reconnect after close() is called", () => {
+ const client = makeClient();
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+
+ client.close();
+
+ expect((client as any).reconnectTimer).toBeNull();
+ expect((client as any).closed).toBe(true);
+ });
+
+ it("liveness timeout triggers ws.close() if no messages received", () => {
+ const client = makeClient();
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+
+ const closeSpy = vi.spyOn(ws, "close");
+ vi.advanceTimersByTime(55_000);
+
+ expect(closeSpy).toHaveBeenCalled();
+ });
+
+ it("calls onDisconnected when connection drops after being authenticated", () => {
+ const onDisconnected = vi.fn();
+ const client = makeClient({ onDisconnected });
+ client.connect();
+
+ const ws = (client as any).ws as MockWebSocket;
+ ws.simulateOpen();
+ ws.simulateMessage(JSON.stringify({ type: "auth.ok" }));
+
+ expect(client.isConnected()).toBe(true);
+ ws.simulateClose();
+
+ expect(onDisconnected).toHaveBeenCalledTimes(1);
+ expect(client.isConnected()).toBe(false);
+ });
+});
diff --git a/src/cli/daemon/ws-client.ts b/src/cli/daemon/ws-client.ts
new file mode 100644
index 000000000..5a2340e59
--- /dev/null
+++ b/src/cli/daemon/ws-client.ts
@@ -0,0 +1,164 @@
+import { createLogger } from "../lib/logger.js";
+import { DaemonPushMessageSchema } from "@alook/shared";
+import type { DaemonPushMessage } from "@alook/shared";
+
+const log = createLogger({ module: "ws-client" });
+
+const WS_RECONNECT_INIT = 1000;
+const WS_RECONNECT_MAX = 30_000;
+const WS_PING_INTERVAL = 25_000;
+const WS_LIVENESS_TIMEOUT = 50_000;
+const WS_DO_DEV_PORT = 8789;
+
+export interface DaemonWsClientOptions {
+ serverURL: string;
+ daemonId: string;
+ machineToken: string;
+ onMessage: (msg: DaemonPushMessage) => void;
+ onConnected: () => void;
+ onDisconnected: () => void;
+}
+
+export class DaemonWsClient {
+ private ws: WebSocket | null = null;
+ private reconnectDelay = WS_RECONNECT_INIT;
+ private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
+ private pingInterval: ReturnType<typeof setInterval> | null = null;
+ private livenessInterval: ReturnType<typeof setInterval> | null = null;
+ private lastMessageAt = 0;
+ private connected = false;
+ private closed = false;
+
+ constructor(private opts: DaemonWsClientOptions) {}
+
+ getUrl(): string {
+ const url = new URL(this.opts.serverURL);
+ const isLocal = url.hostname === "localhost" || url.hostname === "127.0.0.1";
+ if (isLocal) {
+ return `ws://localhost:${WS_DO_DEV_PORT}/?daemonId=${this.opts.daemonId}`;
+ }
+ const protocol = url.protocol === "https:" ? "wss:" : "ws:";
+ return `${protocol}//${url.host}/api/ws/daemon?daemonId=${this.opts.daemonId}`;
+ }
+
+ isConnected(): boolean {
+ return this.connected;
+ }
+
+ connect(): void {
+ if (this.closed) return;
+ this.cleanup();
+
+ const wsUrl = this.getUrl();
+ log.info("connecting", { url: wsUrl });
+
+ try {
+ this.ws = new WebSocket(wsUrl);
+ } catch (err) {
+ log.warn("ws creation failed", { err: String(err) });
+ this.scheduleReconnect();
+ return;
+ }
+
+ this.ws.addEventListener("open", () => {
+ this.reconnectDelay = WS_RECONNECT_INIT;
+ this.ws!.send(JSON.stringify({
+ type: "auth",
+ machineToken: this.opts.machineToken,
+ daemonId: this.opts.daemonId,
+ }));
+ this.lastMessageAt = Date.now();
+ this.startHeartbeat();
+ });
+
+ this.ws.addEventListener("message", (event) => {
+ this.lastMessageAt = Date.now();
+ const str = typeof event.data === "string" ? event.data : "";
+ if (str === "pong") return;
+
+ try {
+ const msg = JSON.parse(str);
+ if (msg.type === "auth.ok") {
+ log.info("authenticated");
+ this.connected = true;
+ this.opts.onConnected();
+ return;
+ }
+ const parsed = DaemonPushMessageSchema.safeParse(msg);
+ if (!parsed.success) {
+ log.warn("invalid push message", { err: parsed.error.message });
+ return;
+ }
+ this.opts.onMessage(parsed.data);
+ } catch (err) {
+ log.debug("message parse error", { err: String(err) });
+ }
+ });
+
+ this.ws.addEventListener("error", () => {
+ log.debug("ws error");
+ });
+
+ this.ws.addEventListener("close", () => {
+ const wasConnected = this.connected;
+ this.connected = false;
+ this.stopHeartbeat();
+ if (wasConnected) {
+ this.opts.onDisconnected();
+ }
+ if (!this.closed) {
+ this.scheduleReconnect();
+ }
+ });
+ }
+
+ close(): void {
+ this.closed = true;
+ this.cleanup();
+ if (this.reconnectTimer) {
+ clearTimeout(this.reconnectTimer);
+ this.reconnectTimer = null;
+ }
+ }
+
+ private cleanup(): void {
+ this.stopHeartbeat();
+ if (this.ws) {
+ try { this.ws.close(); } catch {}
+ this.ws = null;
+ }
+ this.connected = false;
+ }
+
+ private scheduleReconnect(): void {
+ if (this.closed) return;
+ const delay = Math.min(this.reconnectDelay, WS_RECONNECT_MAX);
+ this.reconnectDelay = Math.min(delay * 2, WS_RECONNECT_MAX);
+ const jitter = Math.random() * 500;
+ log.debug("reconnecting", { delayMs: Math.round(delay + jitter) });
+ this.reconnectTimer = setTimeout(() => {
+ this.reconnectTimer = null;
+ this.connect();
+ }, delay + jitter);
+ }
+
+ private startHeartbeat(): void {
+ this.pingInterval = setInterval(() => {
+ if (this.ws?.readyState === WebSocket.OPEN) {
+ this.ws.send("ping");
+ }
+ }, WS_PING_INTERVAL);
+
+ this.livenessInterval = setInterval(() => {
+ if (Date.now() - this.lastMessageAt > WS_LIVENESS_TIMEOUT) {
+ log.warn("liveness timeout, closing");
+ this.ws?.close();
+ }
+ }, 5_000);
+ }
+
+ private stopHeartbeat(): void {
+ if (this.pingInterval) { clearInterval(this.pingInterval); this.pingInterval = null; }
+ if (this.livenessInterval) { clearInterval(this.livenessInterval); this.livenessInterval = null; }
+ }
+}
diff --git a/src/shared/src/db/queries/task.ts b/src/shared/src/db/queries/task.ts
index deaa8be43..59bf8e544 100644
--- a/src/shared/src/db/queries/task.ts
+++ b/src/shared/src/db/queries/task.ts
@@ -258,6 +258,26 @@ export async function claimTask(db: Database, agentId: string, workspaceId: stri
return null;
}
+export async function dispatchTaskById(db: Database, id: string, workspaceId: string) {
+ const rows = await db
+ .update(agentTaskQueue)
+ .set({ status: "dispatched", dispatchedAt: new Date().toISOString() })
+ .where(
+ and(eq(agentTaskQueue.id, id), eq(agentTaskQueue.workspaceId, workspaceId), eq(agentTaskQueue.status, "queued"))
+ )
+ .returning();
+ return rows[0] ? ClaimedTaskRowSchema.parse(rows[0]) : null;
+}
+
+export async function revertDispatchedToQueued(db: Database, id: string, workspaceId: string) {
+ await db
+ .update(agentTaskQueue)
+ .set({ status: "queued", dispatchedAt: null })
+ .where(
+ and(eq(agentTaskQueue.id, id), eq(agentTaskQueue.workspaceId, workspaceId), eq(agentTaskQueue.status, "dispatched"))
+ );
+}
+
export async function startTask(db: Database, id: string, workspaceId: string) {
const rows = await db
.update(agentTaskQueue)
diff --git a/src/shared/src/index.ts b/src/shared/src/index.ts
index 7a75dc5cc..99b252996 100644
--- a/src/shared/src/index.ts
+++ b/src/shared/src/index.ts
@@ -25,6 +25,7 @@ export type {
Channel,
WsMessage,
WorkspaceFileResult,
+ DaemonPushMessage,
} from "./types";
// API types
@@ -108,6 +109,8 @@ export {
TaskAgentDataApiSchema,
TaskApiBaseSchema,
TaskApiSchema,
+ HeartbeatRequestSchema,
+ SweepRequestSchema,
PollRequestSchema,
PollResponseSchema,
PollMeetingItemSchema,
@@ -158,6 +161,7 @@ export {
WorkspaceFileEntrySchema,
WorkspaceFileReportSchema,
CreateStudioRequestSchema,
+ DaemonPushMessageSchema,
} from "./schemas";
export type {
@@ -165,6 +169,8 @@ export type {
TaskAgentDataApi,
TaskApiBase,
TaskApi,
+ HeartbeatRequest,
+ SweepRequest,
PollRequest,
PollResponse,
PollMeetingItem,
@@ -200,6 +206,7 @@ export type {
WorkspaceFileBrowseRequest,
WorkspaceFileReport,
CreateStudioRequest,
+ DaemonPushMessageType,
} from "./schemas";
// Database
diff --git a/src/shared/src/schemas.ts b/src/shared/src/schemas.ts
index f9ec0ac36..c290266a2 100644
--- a/src/shared/src/schemas.ts
+++ b/src/shared/src/schemas.ts
@@ -111,6 +111,18 @@ export const TaskApiSchema = TaskApiBaseSchema.extend({
});
export type TaskApi = z.infer<typeof TaskApiSchema>;
+// ---------------------------------------------------------------------------
+// Heartbeat (lightweight liveness ping, independent of poll)
+// ---------------------------------------------------------------------------
+
+export const HeartbeatRequestSchema = z.object({
+ daemon_id: z.string().min(1),
+});
+export type HeartbeatRequest = z.infer<typeof HeartbeatRequestSchema>;
+
+export const SweepRequestSchema = HeartbeatRequestSchema;
+export type SweepRequest = HeartbeatRequest;
+
// ---------------------------------------------------------------------------
// Poll request/response (replaces heartbeat + per-runtime claim)
// ---------------------------------------------------------------------------
@@ -151,6 +163,21 @@ export const PollResponseSchema = z.object({
});
export type PollResponse = z.infer<typeof PollResponseSchema>;
+// ---------------------------------------------------------------------------
+// Daemon push messages (server -> daemon WebSocket)
+// ---------------------------------------------------------------------------
+
+export const DaemonPushMessageSchema = z.discriminatedUnion("type", [
+ z.object({ type: z.literal("daemon.tasks"), tasks: z.array(TaskApiSchema) }),
+ z.object({ type: z.literal("daemon.file_requests"), workspaceId: z.string(), requests: z.array(FileRequestItemSchema) }),
+ z.object({ type: z.literal("daemon.meetings"), meetings: z.array(PollMeetingItemSchema) }),
+ z.object({ type: z.literal("daemon.evict"), workspaceId: z.string() }),
+ z.object({ type: z.literal("daemon.update"), version: z.string() }),
+ z.object({ type: z.literal("daemon.rescan") }),
+ z.object({ type: z.literal("daemon.kill"), workspaceId: z.string(), taskId: z.string(), targetTaskId: z.string() }),
+]);
+export type DaemonPushMessageType = z.infer<typeof DaemonPushMessageSchema>;
+
// ---------------------------------------------------------------------------
// Register response
// ---------------------------------------------------------------------------
diff --git a/src/shared/src/types.ts b/src/shared/src/types.ts
index 07bde4aa6..b20f45bf4 100644
--- a/src/shared/src/types.ts
+++ b/src/shared/src/types.ts
@@ -1,4 +1,4 @@
-import type { TaskApi, WorkspaceFileEntry } from "./schemas";
+import type { TaskApi, WorkspaceFileEntry, FileRequestItem, PollMeetingItem } from "./schemas";
export type EmailDirection = "inbound" | "outbound";
@@ -295,3 +295,13 @@ export interface WorkspaceFileResult {
error?: string;
path: string;
}
+
+/** Messages pushed from server to daemon via WebSocket. */
+export type DaemonPushMessage =
+ | { type: "daemon.tasks"; tasks: TaskApi[] }
+ | { type: "daemon.file_requests"; workspaceId: string; requests: FileRequestItem[] }
+ | { type: "daemon.meetings"; meetings: PollMeetingItem[] }
+ | { type: "daemon.evict"; workspaceId: string }
+ | { type: "daemon.update"; version: string }
+ | { type: "daemon.rescan" }
+ | { type: "daemon.kill"; workspaceId: string; taskId: string; targetTaskId: string }
diff --git a/src/web/src/app/api/agents/[id]/workspace/browse/route.ts b/src/web/src/app/api/agents/[id]/workspace/browse/route.ts
index d372b2130..3795debe8 100644
--- a/src/web/src/app/api/agents/[id]/workspace/browse/route.ts
+++ b/src/web/src/app/api/agents/[id]/workspace/browse/route.ts
@@ -5,6 +5,7 @@ import { withAuth } from "@/lib/middleware/auth";
import { parseBody, writeJSON, writeError } from "@/lib/middleware/helpers";
import { getDb } from "@/lib/db";
import { cacheKeys } from "@/lib/cache";
+import { broadcastToDaemon } from "@/lib/broadcast";
export const POST = withAuth(async (req: NextRequest, ctx) => {
const { env } = getCloudflareContext();
@@ -34,5 +35,17 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
kv.put(cacheKeys.hasPendingFileRequest(workspaceId), "1", { expirationTtl: 60 }).catch(() => {});
}
+ // Push file request to daemon (best-effort)
+ if (agent.runtimeId) {
+ const runtime = await queries.runtime.getAgentRuntime(db, agent.runtimeId);
+ if (runtime) {
+ broadcastToDaemon(runtime.daemonId, {
+ type: "daemon.file_requests",
+ workspaceId,
+ requests: [{ id: row.id, agent_id: agentId, request_type: body.request_type, path: body.path }],
+ }).catch(() => {});
+ }
+ }
+
return writeJSON({ request_id: row.id });
});
diff --git a/src/web/src/app/api/daemon/heartbeat/route.test.ts b/src/web/src/app/api/daemon/heartbeat/route.test.ts
new file mode 100644
index 000000000..b6cc7a867
--- /dev/null
+++ b/src/web/src/app/api/daemon/heartbeat/route.test.ts
@@ -0,0 +1,179 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+import { NextRequest } from "next/server";
+
+const mockUpsertMachine = vi.fn();
+const mockBroadcastToUser = vi.fn();
+const mockKvPut = vi.fn().mockResolvedValue(undefined);
+const mockKvGet = vi.fn().mockResolvedValue(null);
+
+vi.mock("@opennextjs/cloudflare", () => ({
+ getCloudflareContext: vi.fn(() => ({
+ env: {
+ DB: {},
+ CACHE_KV: {
+ put: (...args: unknown[]) => mockKvPut(...args),
+ get: (...args: unknown[]) => mockKvGet(...args),
+ delete: vi.fn().mockResolvedValue(undefined),
+ },
+ },
+ })),
+}));
+
+vi.mock("@/lib/db", () => ({
+ getDb: vi.fn(() => ({})),
+}));
+
+vi.mock("@alook/shared", async () => {
+ const real = await vi.importActual<typeof import("@alook/shared")>("@alook/shared");
+ return {
+ ...real,
+ queries: {
+ machine: {
+ upsertMachine: (...args: unknown[]) => mockUpsertMachine(...args),
+ },
+ },
+ };
+});
+
+vi.mock("@/lib/middleware/auth", () => ({
+ withAuth: vi.fn((handler: any) => async (req: any) => {
+ return handler(req, { userId: "u1", email: "[email protected]", workspaceId: "w1" });
+ }),
+}));
+
+vi.mock("@/lib/middleware/helpers", async () =>
+ await vi.importActual<typeof import("@/lib/middleware/helpers")>("@/lib/middleware/helpers")
+);
+
+vi.mock("@/lib/broadcast", () => ({
+ broadcastToUser: (...args: unknown[]) => mockBroadcastToUser(...args),
+}));
+
+vi.mock("@/lib/logger", () => ({
+ log: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), debug: vi.fn() },
+}));
+
+vi.mock("@/lib/cache", () => ({
+ cacheKeys: {
+ heartbeat: (wsId: string, daemonId: string) => `hb:${wsId}:${daemonId}`,
+ },
+ throttled: vi.fn((_key: string, _interval: number, fn: () => Promise<void>) => fn().then(() => true)),
+}));
+
+import { POST } from "./route";
+
+function postReq(body: unknown) {
+ return new NextRequest("http://localhost/api/daemon/heartbeat", {
+ method: "POST",
+ body: JSON.stringify(body),
+ headers: { "Content-Type": "application/json" },
+ });
+}
+
+describe("POST /api/daemon/heartbeat", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockUpsertMachine.mockResolvedValue({});
+ mockBroadcastToUser.mockResolvedValue(undefined);
+ });
+
+ it("returns 400 when daemon_id is missing", async () => {
+ const res = await POST(postReq({}));
+ expect(res.status).toBe(400);
+ });
+
+ it("returns 400 when daemon_id is empty", async () => {
+ const res = await POST(postReq({ daemon_id: "" }));
+ expect(res.status).toBe(400);
+ });
+
+ it("returns ok: true on valid request", async () => {
+ const res = await POST(postReq({ daemon_id: "d1" }));
+ const body = await res.json();
+
+ expect(res.status).toBe(200);
+ expect(body).toEqual({ ok: true });
+ });
+
+ it("writes KV heartbeat with 120s TTL", async () => {
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockKvPut).toHaveBeenCalledWith(
+ "hb:w1:d1",
+ expect.any(String),
+ { expirationTtl: 120 },
+ );
+ });
+
+ it("upserts machine via throttled D1 write", async () => {
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockUpsertMachine).toHaveBeenCalledWith({}, {
+ daemonId: "d1",
+ workspaceId: "w1",
+ deviceInfo: "d1",
+ });
+ });
+
+ it("broadcasts runtime.status when daemon transitions from offline to online", async () => {
+ mockKvGet.mockResolvedValue(null);
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockBroadcastToUser).toHaveBeenCalledWith("u1", {
+ type: "runtime.status",
+ daemonId: "d1",
+ workspaceId: "w1",
+ status: "online",
+ });
+ });
+
+ it("does not broadcast when daemon was already online", async () => {
+ mockKvGet.mockResolvedValue("2026-05-20T10:00:00.000Z");
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockBroadcastToUser).not.toHaveBeenCalled();
+ });
+
+ it("does not fail when upsertMachine throws", async () => {
+ mockUpsertMachine.mockRejectedValue(new Error("D1 timeout"));
+
+ const res = await POST(postReq({ daemon_id: "d1" }));
+ const body = await res.json();
+
+ expect(res.status).toBe(200);
+ expect(body).toEqual({ ok: true });
+ });
+
+ it("requires machine token (workspaceId must be present)", async () => {
+ vi.resetModules();
+
+ vi.doMock("@opennextjs/cloudflare", () => ({
+ getCloudflareContext: vi.fn(() => ({
+ env: { DB: {}, CACHE_KV: { put: vi.fn(), get: vi.fn().mockResolvedValue(null) } },
+ })),
+ }));
+ vi.doMock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+ vi.doMock("@alook/shared", async () => {
+ const real = await vi.importActual<typeof import("@alook/shared")>("@alook/shared");
+ return { ...real, queries: { machine: { upsertMachine: vi.fn() } } };
+ });
+ vi.doMock("@/lib/middleware/auth", () => ({
+ withAuth: vi.fn((handler: any) => async (req: any) => {
+ return handler(req, { userId: "u1", email: "[email protected]" });
+ }),
+ }));
+ vi.doMock("@/lib/middleware/helpers", async () =>
+ await vi.importActual<typeof import("@/lib/middleware/helpers")>("@/lib/middleware/helpers")
+ );
+ vi.doMock("@/lib/broadcast", () => ({ broadcastToUser: vi.fn() }));
+ vi.doMock("@/lib/logger", () => ({ log: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), debug: vi.fn() } }));
+ vi.doMock("@/lib/cache", () => ({
+ cacheKeys: { heartbeat: (wsId: string, daemonId: string) => `hb:${wsId}:${daemonId}` },
+ throttled: vi.fn((_k: string, _i: number, fn: () => Promise<void>) => fn().then(() => true)),
+ }));
+
+ const { POST: POST2 } = await import("./route");
+ const res = await POST2(postReq({ daemon_id: "d1" }));
+ expect(res.status).toBe(403);
+ });
+});
diff --git a/src/web/src/app/api/daemon/heartbeat/route.ts b/src/web/src/app/api/daemon/heartbeat/route.ts
new file mode 100644
index 000000000..5463195cf
--- /dev/null
+++ b/src/web/src/app/api/daemon/heartbeat/route.ts
@@ -0,0 +1,61 @@
+import { NextRequest } from "next/server";
+import { getCloudflareContext } from "@opennextjs/cloudflare";
+import { queries, HeartbeatRequestSchema } from "@alook/shared";
+import { getDb } from "@/lib/db";
+import { withAuth } from "@/lib/middleware/auth";
+import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";
+import { broadcastToUser } from "@/lib/broadcast";
+import { log } from "@/lib/logger";
+
+export const POST = withAuth(async (req: NextRequest, ctx) => {
+ const { env } = getCloudflareContext();
+ const { cacheKeys, throttled } = await import("@/lib/cache");
+
+ const [body, err] = await parseBody(req, HeartbeatRequestSchema);
+ if (err) return err;
+
+ if (!ctx.workspaceId) {
+ return writeError("Forbidden: machine token required", 403);
+ }
+
+ const D1_HEARTBEAT_THROTTLE_S = 15;
+ const kv = (env as Env).CACHE_KV ?? null;
+ let wasOffline = false;
+ if (kv) {
+ const prev = await kv.get(cacheKeys.heartbeat(ctx.workspaceId, body.daemon_id));
+ wasOffline = !prev;
+ kv.put(
+ cacheKeys.heartbeat(ctx.workspaceId, body.daemon_id),
+ new Date().toISOString(),
+ { expirationTtl: 120 },
+ ).catch(() => {});
+ }
+
+ try {
+ const db = getDb((env as Env).DB);
+ await throttled(
+ `hb_d1:${ctx.workspaceId}:${body.daemon_id}`,
+ D1_HEARTBEAT_THROTTLE_S,
+ async () => {
+ await queries.machine.upsertMachine(db, {
+ daemonId: body.daemon_id,
+ workspaceId: ctx.workspaceId!,
+ deviceInfo: body.daemon_id,
+ });
+ },
+ );
+ } catch (e) {
+ log.warn("heartbeat: machine upsert failed", { daemonId: body.daemon_id, err: String(e) });
+ }
+
+ if (wasOffline) {
+ broadcastToUser(ctx.userId, {
+ type: "runtime.status",
+ daemonId: body.daemon_id,
+ workspaceId: ctx.workspaceId,
+ status: "online",
+ }).catch(() => {});
+ }
+
+ return writeJSON({ ok: true });
+});
diff --git a/src/web/src/app/api/daemon/sweep/route.test.ts b/src/web/src/app/api/daemon/sweep/route.test.ts
new file mode 100644
index 000000000..9eaac737a
--- /dev/null
+++ b/src/web/src/app/api/daemon/sweep/route.test.ts
@@ -0,0 +1,142 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+import { NextRequest } from "next/server";
+
+const mockSweepStaleState = vi.fn();
+const mockPromoteDue = vi.fn(async () => 0);
+
+vi.mock("@opennextjs/cloudflare", () => ({
+ getCloudflareContext: vi.fn(() => ({
+ env: {
+ DB: {},
+ CACHE_KV: {
+ put: vi.fn().mockResolvedValue(undefined),
+ get: vi.fn().mockResolvedValue(null),
+ delete: vi.fn().mockResolvedValue(undefined),
+ },
+ },
+ })),
+}));
+
+vi.mock("@/lib/db", () => ({
+ getDb: vi.fn(() => ({})),
+}));
+
+vi.mock("@alook/shared", async () => {
+ const real = await vi.importActual<typeof import("@alook/shared")>("@alook/shared");
+ return { ...real };
+});
+
+vi.mock("@/lib/middleware/auth", () => ({
+ withAuth: vi.fn((handler: any) => async (req: any) => {
+ return handler(req, { userId: "u1", email: "[email protected]", workspaceId: "w1" });
+ }),
+}));
+
+vi.mock("@/lib/middleware/helpers", async () =>
+ await vi.importActual<typeof import("@/lib/middleware/helpers")>("@/lib/middleware/helpers")
+);
+
+vi.mock("@/lib/services/sweep", () => ({
+ sweepStaleState: (...args: unknown[]) => mockSweepStaleState(...args),
+}));
+
+vi.mock("@/lib/services/calendar", () => ({
+ promoteDueCalendarEventsForWorkspace: (...args: unknown[]) => mockPromoteDue(...args),
+}));
+
+vi.mock("@/lib/logger", () => ({
+ log: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), debug: vi.fn() },
+}));
+
+vi.mock("@/lib/cache", () => ({
+ throttled: vi.fn((_key: string, _interval: number, fn: () => Promise<any>) => fn()),
+}));
+
+import { POST } from "./route";
+
+function postReq(body: unknown) {
+ return new NextRequest("http://localhost/api/daemon/sweep", {
+ method: "POST",
+ body: JSON.stringify(body),
+ headers: { "Content-Type": "application/json" },
+ });
+}
+
+describe("POST /api/daemon/sweep", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockSweepStaleState.mockResolvedValue(undefined);
+ mockPromoteDue.mockResolvedValue(0);
+ });
+
+ it("returns 403 without machine token auth", async () => {
+ vi.resetModules();
+
+ vi.doMock("@opennextjs/cloudflare", () => ({
+ getCloudflareContext: vi.fn(() => ({
+ env: { DB: {}, CACHE_KV: { put: vi.fn(), get: vi.fn().mockResolvedValue(null), delete: vi.fn() } },
+ })),
+ }));
+ vi.doMock("@/lib/db", () => ({ getDb: vi.fn(() => ({})) }));
+ vi.doMock("@alook/shared", async () => {
+ const real = await vi.importActual<typeof import("@alook/shared")>("@alook/shared");
+ return { ...real };
+ });
+ vi.doMock("@/lib/middleware/auth", () => ({
+ withAuth: vi.fn((handler: any) => async (req: any) => {
+ return handler(req, { userId: "u1", email: "[email protected]" });
+ }),
+ }));
+ vi.doMock("@/lib/middleware/helpers", async () =>
+ await vi.importActual<typeof import("@/lib/middleware/helpers")>("@/lib/middleware/helpers")
+ );
+ vi.doMock("@/lib/services/sweep", () => ({ sweepStaleState: vi.fn() }));
+ vi.doMock("@/lib/services/calendar", () => ({ promoteDueCalendarEventsForWorkspace: vi.fn() }));
+ vi.doMock("@/lib/logger", () => ({ log: { warn: vi.fn(), info: vi.fn(), error: vi.fn(), debug: vi.fn() } }));
+ vi.doMock("@/lib/cache", () => ({ throttled: vi.fn((_k: string, _i: number, fn: () => Promise<any>) => fn()) }));
+
+ const { POST: POST2 } = await import("./route");
+ const res = await POST2(postReq({ daemon_id: "d1" }));
+ expect(res.status).toBe(403);
+ });
+
+ it("calls sweepStaleState with correct db and workspaceId", async () => {
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockSweepStaleState).toHaveBeenCalledWith({}, "w1");
+ });
+
+ it("calls promoteDueCalendarEventsForWorkspace (throttled)", async () => {
+ await POST(postReq({ daemon_id: "d1" }));
+
+ expect(mockPromoteDue).toHaveBeenCalledWith({}, "w1");
+ });
+
+ it("returns { ok: true } on success", async () => {
+ const res = await POST(postReq({ daemon_id: "d1" }));
+ const body = await res.json();
+
+ expect(res.status).toBe(200);
+ expect(body).toEqual({ ok: true });
+ });
+
+ it("survives sweepStaleState throwing (still returns 200)", async () => {
+ mockSweepStaleState.mockRejectedValue(new Error("D1 timeout"));
+
+ const res = await POST(postReq({ daemon_id: "d1" }));
+ const body = await res.json();
+
+ expect(res.status).toBe(200);
+ expect(body).toEqual({ ok: true });
+ });
+
+ it("survives calendar promotion throwing (still returns 200)", async () => {
+ mockPromoteDue.mockRejectedValue(new Error("calendar error"));
+
+ const res = await POST(postReq({ daemon_id: "d1" }));
+ const body = await res.json();
+
+ expect(res.status).toBe(200);
+ expect(body).toEqual({ ok: true });
+ });
+});
diff --git a/src/web/src/app/api/daemon/sweep/route.ts b/src/web/src/app/api/daemon/sweep/route.ts
new file mode 100644
index 000000000..503355a8e
--- /dev/null
+++ b/src/web/src/app/api/daemon/sweep/route.ts
@@ -0,0 +1,45 @@
+import { NextRequest } from "next/server";
+import { getCloudflareContext } from "@opennextjs/cloudflare";
+import { SweepRequestSchema } from "@alook/shared";
+import { getDb } from "@/lib/db";
+import { withAuth } from "@/lib/middleware/auth";
+import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";
+import { sweepStaleState } from "@/lib/services/sweep";
+import { promoteDueCalendarEventsForWorkspace } from "@/lib/services/calendar";
+import { log } from "@/lib/logger";
+
+export const POST = withAuth(async (req: NextRequest, ctx) => {
+ const { env } = getCloudflareContext();
+ const { throttled } = await import("@/lib/cache");
+
+ const [, err] = await parseBody(req, SweepRequestSchema);
+ if (err) return err;
+
+ if (!ctx.workspaceId) {
+ return writeError("Forbidden: machine token required", 403);
+ }
+
+ const db = getDb((env as Env).DB);
+
+ try {
+ await sweepStaleState(db, ctx.workspaceId);
+ } catch (e) {
+ log.warn("sweep failed", { workspaceId: ctx.workspaceId, err: String(e) });
+ }
+
+ try {
+ await throttled(`cal:${ctx.workspaceId}`, 30, async () => {
+ const enqueued = await promoteDueCalendarEventsForWorkspace(
+ db,
+ ctx.workspaceId!,
+ );
+ if (enqueued > 0) {
+ log.info("calendar: enqueued", { workspaceId: ctx.workspaceId, enqueued });
+ }
+ });
+ } catch (e) {
+ log.warn("calendar: promote failed", { workspaceId: ctx.workspaceId, err: String(e) });
+ }
+
+ return writeJSON({ ok: true });
+});
diff --git a/src/web/src/app/api/daemon/tasks/poll/route.test.ts b/src/web/src/app/api/daemon/tasks/poll/route.test.ts
index dab105fda..858e37750 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.test.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.test.ts
@@ -170,7 +170,7 @@ describe("POST /api/daemon/tasks/poll", () => {
mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
});
- it("returns evicted: true and skips heartbeat when daemon has no runtimes", async () => {
+ it("returns evicted: true when daemon has no runtimes", async () => {
mockGetRuntimeIdsByDaemon.mockResolvedValue([]);
const res = await POST(postReq({ daemon_id: "d1" }));
@@ -179,12 +179,9 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(res.status).toBe(200);
expect(body.tasks).toEqual([]);
expect(body.evicted).toBe(true);
- expect(mockUpsertMachine).not.toHaveBeenCalled();
- expect(mockBroadcastToUser).not.toHaveBeenCalled();
});
it("omits evicted field for normal polls with active runtimes", async () => {
- mockUpsertMachine.mockResolvedValue({});
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
mockSweepStaleState.mockResolvedValue(undefined);
mockBroadcastToUser.mockResolvedValue(undefined);
@@ -195,12 +192,9 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(res.status).toBe(200);
expect(body.evicted).toBeUndefined();
- expect(mockUpsertMachine).toHaveBeenCalled();
- expect(mockBroadcastToUser).toHaveBeenCalled();
});
- it("resolves runtime IDs from daemon_id and upserts machine liveness", async () => {
- mockUpsertMachine.mockResolvedValue({});
+ it("resolves runtime IDs from daemon_id", async () => {
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1", "r2"]);
mockSweepStaleState.mockResolvedValue(undefined);
mockBroadcastToUser.mockResolvedValue(undefined);
@@ -209,12 +203,6 @@ describe("POST /api/daemon/tasks/poll", () => {
await POST(postReq({ daemon_id: "d1" }));
expect(mockGetRuntimeIdsByDaemon).toHaveBeenCalledWith({}, "d1", "w1");
- expect(mockUpsertMachine).toHaveBeenCalledTimes(1);
- expect(mockUpsertMachine).toHaveBeenCalledWith({}, {
- daemonId: "d1",
- workspaceId: "w1",
- deviceInfo: "d1",
- });
});
it("returns tasks with agent data for claimed tasks", async () => {
@@ -250,8 +238,7 @@ describe("POST /api/daemon/tasks/poll", () => {
});
});
- it("broadcasts single runtime.status with daemonId and workspaceId", async () => {
- mockUpsertMachine.mockResolvedValue({});
+ it("does not broadcast runtime.status (heartbeat handles that)", async () => {
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1", "r2", "r3"]);
mockSweepStaleState.mockResolvedValue(undefined);
mockBroadcastToUser.mockResolvedValue(undefined);
@@ -259,26 +246,10 @@ describe("POST /api/daemon/tasks/poll", () => {
await POST(postReq({ daemon_id: "d1" }));
- // Single broadcast, not per-runtime
- expect(mockBroadcastToUser).toHaveBeenCalledTimes(1);
- expect(mockBroadcastToUser).toHaveBeenCalledWith("u1", {
+ expect(mockBroadcastToUser).not.toHaveBeenCalledWith("u1", expect.objectContaining({
type: "runtime.status",
- daemonId: "d1",
- workspaceId: "w1",
status: "online",
- });
- });
-
- it("calls sweepStaleState", async () => {
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockResolvedValue(undefined);
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
-
- await POST(postReq({ daemon_id: "d1" }));
-
- expect(mockSweepStaleState).toHaveBeenCalledWith({}, "w1");
+ }));
});
it("respects max_tasks parameter", async () => {
@@ -305,38 +276,6 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(mockClaimTasksForRuntimes).toHaveBeenCalledWith(["r1"], 1, "w1");
});
- it("invokes calendar promotion between sweep and task claim", async () => {
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockResolvedValue(undefined);
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
- mockPromoteDue.mockResolvedValue(2);
-
- await POST(postReq({ daemon_id: "d1" }));
-
- expect(mockPromoteDue).toHaveBeenCalledWith({}, "w1");
- // Calendar promotion is scoped per-call to the authenticated workspace.
- const promoteOrder = mockPromoteDue.mock.invocationCallOrder[0]!;
- const sweepOrder = mockSweepStaleState.mock.invocationCallOrder[0]!;
- const claimOrder = mockClaimTasksForRuntimes.mock.invocationCallOrder[0]!;
- expect(sweepOrder).toBeLessThan(promoteOrder);
- expect(promoteOrder).toBeLessThan(claimOrder);
- });
-
- it("does not fail the poll when calendar promotion throws", async () => {
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockResolvedValue(undefined);
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
- mockPromoteDue.mockRejectedValue(new Error("D1 write failed"));
-
- const res = await POST(postReq({ daemon_id: "d1" }));
- expect(res.status).toBe(200);
- expect(mockClaimTasksForRuntimes).toHaveBeenCalled();
- });
-
it("returns pending_update when pendingUpdateVersion is set and cli_version is older", async () => {
mockUpsertMachine.mockResolvedValue({});
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
@@ -794,8 +733,7 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(body.meetings).toBeUndefined();
});
- it("still returns tasks when upsertMachine fails (D1 transient error)", async () => {
- mockUpsertMachine.mockRejectedValue(new Error("D1 timeout"));
+ it("poll works without heartbeat logic (upsertMachine no longer called)", async () => {
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
mockSweepStaleState.mockResolvedValue(undefined);
mockBroadcastToUser.mockResolvedValue(undefined);
@@ -806,20 +744,7 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(res.status).toBe(200);
expect(body.tasks).toEqual([]);
- });
-
- it("still returns tasks when sweepStaleState fails (D1 transient error)", async () => {
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockRejectedValue(new Error("D1 timeout"));
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
-
- const res = await POST(postReq({ daemon_id: "d1" }));
- const body = await res.json();
-
- expect(res.status).toBe(200);
- expect(body.tasks).toEqual([]);
+ expect(mockUpsertMachine).not.toHaveBeenCalled();
});
it("still returns tasks when pending check fails (D1 transient error)", async () => {
@@ -838,22 +763,9 @@ describe("POST /api/daemon/tasks/poll", () => {
expect(body.pending_update).toBeUndefined();
});
- // --- Heartbeat cache invalidation ---
-
- it("does NOT invalidate allRuntimes cache on poll heartbeat (prevents cache thrashing)", async () => {
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockResolvedValue(undefined);
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
+ // --- Heartbeat is now a separate endpoint ---
- await POST(postReq({ daemon_id: "d1" }));
-
- expect(mockInvalidate).not.toHaveBeenCalledWith("runtimes:w1");
- });
-
- it("writes KV heartbeat with 120s TTL", async () => {
- mockUpsertMachine.mockResolvedValue({});
+ it("poll does not write KV heartbeat (moved to /api/daemon/heartbeat)", async () => {
mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
mockSweepStaleState.mockResolvedValue(undefined);
mockBroadcastToUser.mockResolvedValue(undefined);
@@ -861,33 +773,10 @@ describe("POST /api/daemon/tasks/poll", () => {
await POST(postReq({ daemon_id: "d1" }));
- expect(mockKvPut).toHaveBeenCalledWith(
+ expect(mockKvPut).not.toHaveBeenCalledWith(
"hb:w1:d1",
expect.any(String),
- { expirationTtl: 120 },
+ expect.anything(),
);
});
-
- it("poll does not crash when invalidate throws", async () => {
- mockInvalidate.mockRejectedValue(new Error("KV delete failed"));
- mockUpsertMachine.mockResolvedValue({});
- mockGetRuntimeIdsByDaemon.mockResolvedValue(["r1"]);
- mockSweepStaleState.mockResolvedValue(undefined);
- mockBroadcastToUser.mockResolvedValue(undefined);
- mockClaimTasksForRuntimes.mockResolvedValue([]);
-
- const res = await POST(postReq({ daemon_id: "d1" }));
- const body = await res.json();
-
- expect(res.status).toBe(200);
- expect(body.tasks).toEqual([]);
- });
-
- it("does not invalidate cache when daemon has no runtimes (evicted early)", async () => {
- mockGetRuntimeIdsByDaemon.mockResolvedValue([]);
-
- await POST(postReq({ daemon_id: "d1" }));
-
- expect(mockInvalidate).not.toHaveBeenCalled();
- });
});
diff --git a/src/web/src/app/api/daemon/tasks/poll/route.ts b/src/web/src/app/api/daemon/tasks/poll/route.ts
index 034f3356c..f9be0cd2b 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts
@@ -1,18 +1,16 @@
import { NextRequest } from "next/server";
import { getCloudflareContext } from "@opennextjs/cloudflare";
-import { queries, PollRequestSchema, semverGte, toAlookAddress, type FileRequestItem, type PollMeetingItem } from "@alook/shared";
+import { queries, PollRequestSchema, semverGte, type FileRequestItem, type PollMeetingItem } from "@alook/shared";
import { getDb, withD1Retry } from "@/lib/db"
import { withAuth } from "@/lib/middleware/auth";
import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";
-import { taskToResponse } from "@/lib/api/responses";
import { TaskService } from "@/lib/services/task";
-import { sweepStaleState } from "@/lib/services/sweep";
-import { promoteDueCalendarEventsForWorkspace } from "@/lib/services/calendar";
+import { TaskPayloadBuilder } from "@/lib/services/task-payload-builder";
import { broadcastToUser } from "@/lib/broadcast";
import { log } from "@/lib/logger";
export const POST = withAuth(async (req: NextRequest, ctx) => {
- const { env, ctx: cfCtx } = getCloudflareContext();
+ const { env } = getCloudflareContext();
const db = getDb((env as Env).DB);
const { cached, cacheKeys, throttled } = await import("@/lib/cache");
@@ -34,68 +32,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
return writeJSON({ tasks: [], evicted: true });
}
- // 2. Liveness: write heartbeat to KV (fast) + D1 upsert throttled via timestamp.
- // KV heartbeat is the primary source; D1 is the cross-colo fallback.
- // 15s throttle is safe: KV heartbeat (120s TTL) handles realtime liveness,
- // D1 only needs to be fresh enough for cross-colo failover detection.
- const D1_HEARTBEAT_THROTTLE_S = 15;
- const kv = (env as Env).CACHE_KV ?? null;
- if (kv) {
- kv.put(
- cacheKeys.heartbeat(ctx.workspaceId, body.daemon_id),
- new Date().toISOString(),
- { expirationTtl: 120 },
- ).catch(() => {});
- }
- try {
- await throttled(
- `hb_d1:${ctx.workspaceId}:${body.daemon_id}`,
- D1_HEARTBEAT_THROTTLE_S,
- async () => {
- await queries.machine.upsertMachine(db, {
- daemonId: body.daemon_id,
- workspaceId: ctx.workspaceId!,
- deviceInfo: body.daemon_id,
- });
- },
- );
- } catch (e) {
- log.warn("machine upsert failed", { daemonId: body.daemon_id, err: String(e) });
- }
-
- broadcastToUser(ctx.userId, {
- type: "runtime.status",
- daemonId: body.daemon_id,
- workspaceId: ctx.workspaceId,
- status: "online",
- }).catch(() => {});
-
- // 3. Housekeeping: sweep stale state — non-blocking (runs after response is sent)
- cfCtx.waitUntil(
- sweepStaleState(db, ctx.workspaceId).catch((e) => {
- log.warn("sweep failed", { workspaceId: ctx.workspaceId, err: String(e) });
- })
- );
-
- // 3b. Promote due calendar events — non-blocking, throttled to once per 30s per workspace
- cfCtx.waitUntil(
- throttled(`cal:${ctx.workspaceId}`, 30, async () => {
- const enqueued = await promoteDueCalendarEventsForWorkspace(
- db,
- ctx.workspaceId!,
- );
- if (enqueued > 0) {
- log.info("calendar: enqueued", { workspaceId: ctx.workspaceId, enqueued });
- }
- }).catch((err) => {
- log.warn("calendar: promote failed", {
- workspaceId: ctx.workspaceId,
- err: String(err),
- });
- })
- );
-
- // 4. Task claiming
+ // 2. Task claiming
const taskService = new TaskService(db);
const claimed = await withD1Retry(() => taskService.claimTasksForRuntimes(
runtimeIds,
@@ -103,142 +40,15 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
ctx.workspaceId!,
));
- // Batch-fetch shared data before the task loop to avoid N+1 queries
- // Uses KV cache for stable data (agents, emails, colleagues)
- const nonKillTasks = claimed.filter((t) => t.type !== "kill_task");
- const agentIds = [...new Set(nonKillTasks.map((t) => t.agentId))];
-
- const [allAgents, allEmailAccounts, allColleagues] = agentIds.length > 0
- ? await Promise.all([
- cached(cacheKeys.allAgents(ctx.workspaceId!), 300, () => queries.agent.getAllAgentsForWorkspace(db, ctx.workspaceId!)),
- cached(cacheKeys.allEmailAccounts(ctx.workspaceId!), 600, () => queries.emailAccount.getAllEmailAccountsForWorkspace(db, ctx.workspaceId!)),
- cached(cacheKeys.allColleagues(ctx.workspaceId!), 600, () => queries.agentLink.getAllColleaguesForWorkspace(db, ctx.workspaceId!)).catch(() => [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>),
- ]).then(([agents, emails, colleagues]) => {
- const agentIdSet = new Set(agentIds);
- return [
- agents.filter((a) => agentIdSet.has(a.id)),
- emails.filter((a) => agentIdSet.has(a.agentId)),
- colleagues.filter((c) => agentIdSet.has(c.agentId)),
- ] as const;
- })
- : [[], [], [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>];
-
- const agentMap = new Map(allAgents.map((a) => [a.id, a]));
- const emailAccountsByAgent = new Map<string, string[]>();
- for (const acc of allEmailAccounts) {
- const list = emailAccountsByAgent.get(acc.agentId) ?? [];
- list.push(acc.emailAddress);
- emailAccountsByAgent.set(acc.agentId, list);
- }
- const colleaguesByAgent = new Map<string, typeof allColleagues>();
- for (const c of allColleagues) {
- const list = colleaguesByAgent.get(c.agentId) ?? [];
- list.push(c);
- colleaguesByAgent.set(c.agentId, list);
- }
+ const payloadBuilder = new TaskPayloadBuilder(db);
+ const tasks = await payloadBuilder.buildFullPayloads(claimed, ctx.workspaceId!);
- const tasks = [];
- const memberCache = new Map<string, { globalInstruction: string } | null>();
- const userCache = new Map<string, { name: string; email: string } | null>();
- const convoCache = new Map<string, Awaited<ReturnType<typeof queries.conversation.getConversation>> | null>();
- for (const task of claimed) {
- if (task.type === "kill_task") {
- tasks.push({ ...taskToResponse(task), agent: null, sender: null });
- continue;
- }
-
- const agent = agentMap.get(task.agentId) ?? null;
- const emailAddresses: string[] = [];
- if (agent) {
- if (agent.emailHandle) emailAddresses.push(`${agent.emailHandle}@alook.ai`);
- const customAccounts = emailAccountsByAgent.get(agent.id) ?? [];
- emailAddresses.push(...customAccounts);
- }
-
- let instructions = agent?.instructions ?? "";
- if (agent?.ownerId) {
- if (!memberCache.has(agent.ownerId)) {
- const m = await cached(
- cacheKeys.member(task.workspaceId, agent.ownerId),
- 600,
- () => queries.member.getMemberByUserAndWorkspace(db, agent.ownerId!, task.workspaceId),
- );
- memberCache.set(agent.ownerId, m ? { globalInstruction: m.globalInstruction } : null);
- }
- const cachedMember = memberCache.get(agent.ownerId);
- if (cachedMember?.globalInstruction) {
- instructions = [cachedMember.globalInstruction, instructions].filter(Boolean).join("\n\n");
- }
- }
-
- let ownerName: string | null = null;
- if (agent?.ownerId) {
- if (!userCache.has(agent.ownerId)) {
- const u = await cached(
- cacheKeys.user(agent.ownerId),
- 1800,
- () => queries.user.getUser(db, agent.ownerId!),
- );
- userCache.set(agent.ownerId, u ? { name: u.name, email: u.email } : null);
- }
- ownerName = userCache.get(agent.ownerId)?.name ?? null;
- }
-
- let convo = convoCache.get(task.conversationId) ?? null;
- if (task.conversationId && !convoCache.has(task.conversationId)) {
- convo = await queries.conversation.getConversation(db, task.conversationId, task.workspaceId);
- convoCache.set(task.conversationId, convo);
- }
- const taskChannel = convo?.channel ?? "default";
-
- let sender: { name: string; email: string; is_owner: boolean } | null = null;
- if (task.type === "user_dm_message" && convo?.userId) {
- if (!userCache.has(convo.userId)) {
- const u = await cached(
- cacheKeys.user(convo.userId),
- 1800,
- () => queries.user.getUser(db, convo!.userId!),
- );
- userCache.set(convo.userId, u ? { name: u.name, email: u.email } : null);
- }
- const cachedUser = userCache.get(convo.userId);
- if (cachedUser) {
- sender = {
- name: cachedUser.name,
- email: cachedUser.email,
- is_owner: convo.userId === agent?.ownerId,
- };
- }
- }
-
- const rawColleagues = colleaguesByAgent.get(task.agentId) ?? [];
- const colleagues = rawColleagues.map((c) => ({
- name: c.name,
- email: c.emailHandle ? toAlookAddress(c.emailHandle) : "",
- description: c.description,
- instruction: c.instruction,
- }));
-
- tasks.push({
- ...taskToResponse(task),
- channel: taskChannel,
- sender,
- agent: agent
- ? {
- instructions,
- name: agent.name,
- runtime_config: agent.runtimeConfig || {},
- email_handle: agent.emailHandle || null,
- email_addresses: emailAddresses,
- user_email: ctx.email || null,
- user_name: ownerName,
- colleagues,
- }
- : null,
- });
+ // Patch user_email (only available in poll context via auth)
+ for (const t of tasks) {
+ if (t.agent) t.agent.user_email = ctx.email || null;
}
- // 5. Pending update & rescan check + meeting claim — throttled to once per 30s
+ // 3. Pending update & rescan check + meeting claim — throttled to once per 30s
let pendingUpdate: { version: string } | undefined;
let pendingRescan: boolean | undefined;
let meetings: PollMeetingItem[] | undefined;
diff --git a/src/web/src/contexts/agent-context.tsx b/src/web/src/contexts/agent-context.tsx
index d49c35933..37ddee2b5 100644
--- a/src/web/src/contexts/agent-context.tsx
+++ b/src/web/src/contexts/agent-context.tsx
@@ -208,22 +208,6 @@ export function AgentProvider({
};
}, [reloadRuntimes]);
- // Debounced reload for runtime.status events
- const statusDebounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
- const debouncedReload = useCallback(() => {
- if (statusDebounceRef.current) clearTimeout(statusDebounceRef.current);
- statusDebounceRef.current = setTimeout(() => {
- statusDebounceRef.current = null;
- reload();
- }, 300);
- }, [reload]);
-
- // Cleanup debounce timer on unmount
- useEffect(() => {
- return () => {
- if (statusDebounceRef.current) clearTimeout(statusDebounceRef.current);
- };
- }, []);
// Listen for real-time WS events
const handleWsMessage = useCallback(
@@ -244,14 +228,14 @@ export function AgentProvider({
case "runtime.status":
// Filter by workspaceId — ignore messages from other workspaces
if (msg.workspaceId !== workspaceId) break;
- debouncedReload();
+ reloadRuntimes();
break;
case "task.updated":
fetchTaskCounts();
break;
}
},
- [reload, debouncedReload, fetchTaskCounts, workspaceId]
+ [reload, reloadRuntimes, fetchTaskCounts, workspaceId]
);
useUserWs(handleWsMessage, { onReconnect: reloadRuntimes });
diff --git a/src/web/src/lib/broadcast.ts b/src/web/src/lib/broadcast.ts
index 5e86e39d5..cd9bdeb2b 100644
--- a/src/web/src/lib/broadcast.ts
+++ b/src/web/src/lib/broadcast.ts
@@ -1,5 +1,5 @@
import { getCloudflareContext } from "@opennextjs/cloudflare"
-import type { WsMessage } from "@alook/shared"
+import type { WsMessage, DaemonPushMessage } from "@alook/shared"
import { DEV_WS_DO_URL, createLogger } from "@alook/shared"
const log = createLogger({ service: "broadcast" })
@@ -17,23 +17,18 @@ async function doSend(url: string, body: string, label: Record<string, string>)
})
if (res.ok) return
log.warn("broadcast service-binding non-ok", { ...label, status: res.status })
- return
} catch {
// Service binding unavailable — fall through to HTTP
}
const fallbackUrl = wsDoUrl || DEV_WS_DO_URL
- try {
- const res = await fetch(`${fallbackUrl}${url}`, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body,
- })
- if (!res.ok) {
- log.warn("broadcast failed", { ...label, status: res.status })
- }
- } catch (err) {
- log.warn("broadcast error", { ...label, err: String(err) })
+ const res = await fetch(`${fallbackUrl}${url}`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body,
+ })
+ if (!res.ok) {
+ throw new Error(`broadcast failed: ${res.status}`)
}
}
@@ -63,3 +58,11 @@ export function broadcastToAgent(agentId: string, message: WsMessage): Promise<v
{ agentId, type: message.type },
)
}
+
+export function broadcastToDaemon(daemonId: string, message: DaemonPushMessage): Promise<void> {
+ return sendBroadcast(
+ `/broadcast/daemon/${daemonId}`,
+ JSON.stringify(message),
+ { daemonId, type: message.type },
+ )
+}
diff --git a/src/web/src/lib/services/task-payload-builder.test.ts b/src/web/src/lib/services/task-payload-builder.test.ts
new file mode 100644
index 000000000..323bfe281
--- /dev/null
+++ b/src/web/src/lib/services/task-payload-builder.test.ts
@@ -0,0 +1,201 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+
+const mockGetAllAgentsForWorkspace = vi.fn();
+const mockGetAllEmailAccountsForWorkspace = vi.fn();
+const mockGetAllColleaguesForWorkspace = vi.fn();
+const mockGetMemberByUserAndWorkspace = vi.fn();
+const mockGetUser = vi.fn();
+const mockGetConversation = vi.fn();
+
+vi.mock("@alook/shared", async () => {
+ const real = await vi.importActual<typeof import("@alook/shared")>("@alook/shared");
+ return {
+ ...real,
+ queries: {
+ agent: {
+ getAllAgentsForWorkspace: (...args: unknown[]) => mockGetAllAgentsForWorkspace(...args),
+ },
+ emailAccount: {
+ getAllEmailAccountsForWorkspace: (...args: unknown[]) => mockGetAllEmailAccountsForWorkspace(...args),
+ },
+ agentLink: {
+ getAllColleaguesForWorkspace: (...args: unknown[]) => mockGetAllColleaguesForWorkspace(...args),
+ },
+ member: {
+ getMemberByUserAndWorkspace: (...args: unknown[]) => mockGetMemberByUserAndWorkspace(...args),
+ },
+ user: {
+ getUser: (...args: unknown[]) => mockGetUser(...args),
+ },
+ conversation: {
+ getConversation: (...args: unknown[]) => mockGetConversation(...args),
+ },
+ },
+ };
+});
+
+vi.mock("@/lib/cache", () => ({
+ cached: vi.fn((_key: string, _ttl: number, fn: () => Promise<any>) => fn()),
+ cacheKeys: {
+ allAgents: (wsId: string) => `agents:${wsId}`,
+ allEmailAccounts: (wsId: string) => `ea:${wsId}`,
+ allColleagues: (wsId: string) => `col:${wsId}`,
+ member: (wsId: string, userId: string) => `mem:${wsId}:${userId}`,
+ user: (userId: string) => `usr:${userId}`,
+ },
+}));
+
+vi.mock("@/lib/api/responses", () => ({
+ taskToResponse: (t: any) => ({
+ id: t.id,
+ agent_id: t.agentId,
+ runtime_id: t.runtimeId,
+ workspace_id: t.workspaceId,
+ prompt: t.prompt,
+ status: t.status,
+ type: t.type,
+ }),
+}));
+
+import { TaskPayloadBuilder } from "./task-payload-builder";
+
+function makeTask(overrides: Record<string, unknown> = {}) {
+ return {
+ id: "t1",
+ agentId: "a1",
+ runtimeId: "r1",
+ workspaceId: "w1",
+ conversationId: "c1",
+ prompt: "do something",
+ status: "dispatched",
+ priority: 1,
+ result: null,
+ context: null,
+ type: "user_dm_message",
+ contextKey: null,
+ sessionId: null,
+ createdAt: new Date("2026-01-01"),
+ dispatchedAt: new Date("2026-01-01"),
+ startedAt: null,
+ completedAt: null,
+ error: null,
+ traceId: null,
+ parentTaskId: null,
+ ...overrides,
+ };
+}
+
+describe("TaskPayloadBuilder", () => {
+ const db = {} as any;
+ let builder: TaskPayloadBuilder;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ builder = new TaskPayloadBuilder(db);
+ mockGetAllAgentsForWorkspace.mockResolvedValue([]);
+ mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
+ mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
+ });
+
+ it("returns empty array for empty input", async () => {
+ const result = await builder.buildFullPayloads([], "w1");
+ expect(result).toEqual([]);
+ expect(mockGetAllAgentsForWorkspace).not.toHaveBeenCalled();
+ });
+
+ it("handles kill_task type (returns taskToResponse with agent: null, sender: null)", async () => {
+ const task = makeTask({ type: "kill_task" });
+ const result = await builder.buildFullPayloads([task], "w1");
+
+ expect(result).toHaveLength(1);
+ expect(result[0].agent).toBeNull();
+ expect(result[0].sender).toBeNull();
+ expect(result[0].id).toBe("t1");
+ expect(mockGetAllAgentsForWorkspace).not.toHaveBeenCalled();
+ });
+
+ it("builds full payload for a normal task", async () => {
+ mockGetAllAgentsForWorkspace.mockResolvedValue([
+ { id: "a1", ownerId: "owner1", instructions: "be helpful", name: "Bot", runtimeConfig: { model: "gpt-4" }, emailHandle: "bot" },
+ ]);
+ mockGetAllEmailAccountsForWorkspace.mockResolvedValue([
+ { agentId: "a1", emailAddress: "[email protected]" },
+ ]);
+ mockGetAllColleaguesForWorkspace.mockResolvedValue([
+ { agentId: "a1", name: "Helper", emailHandle: "helper", description: "helps", instruction: "be nice" },
+ ]);
+ mockGetUser.mockResolvedValue({ name: "Owner", email: "[email protected]" });
+ mockGetMemberByUserAndWorkspace.mockResolvedValue({ globalInstruction: "" });
+ mockGetConversation.mockResolvedValue({ userId: "sender1", channel: "slack" });
+ mockGetUser
+ .mockResolvedValueOnce({ name: "Owner", email: "[email protected]" })
+ .mockResolvedValueOnce({ name: "Sender", email: "[email protected]" });
+
+ const task = makeTask({ type: "user_dm_message" });
+ const result = await builder.buildFullPayloads([task], "w1");
+
+ expect(result).toHaveLength(1);
+ expect(result[0].agent).toMatchObject({
+ instructions: "be helpful",
+ name: "Bot",
+ runtime_config: { model: "gpt-4" },
+ email_handle: "bot",
+ email_addresses: ["[email protected]", "[email protected]"],
+ colleagues: [{ name: "Helper", email: "[email protected]", description: "helps", instruction: "be nice" }],
+ });
+ expect(result[0].channel).toBe("slack");
+ expect(result[0].sender).toMatchObject({ name: "Sender", email: "[email protected]" });
+ });
+
+ it("merges globalInstruction from member into agent instructions", async () => {
+ mockGetAllAgentsForWorkspace.mockResolvedValue([
+ { id: "a1", ownerId: "owner1", instructions: "agent rules", name: "Bot", runtimeConfig: {} },
+ ]);
+ mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
+ mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
+ mockGetMemberByUserAndWorkspace.mockResolvedValue({ globalInstruction: "speak english" });
+ mockGetUser.mockResolvedValue({ name: "Owner", email: "[email protected]" });
+ mockGetConversation.mockResolvedValue(null);
+
+ const task = makeTask();
+ const result = await builder.buildFullPayloads([task], "w1");
+
+ expect(result[0].agent!.instructions).toBe("speak english\n\nagent rules");
+ });
+
+ it("handles task where agent is not found (agent: null)", async () => {
+ mockGetAllAgentsForWorkspace.mockResolvedValue([]);
+ mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
+ mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
+ mockGetConversation.mockResolvedValue(null);
+
+ const task = makeTask({ agentId: "nonexistent" });
+ const result = await builder.buildFullPayloads([task], "w1");
+
+ expect(result).toHaveLength(1);
+ expect(result[0].agent).toBeNull();
+ });
+
+ it("dedupes DB queries when multiple tasks share the same agent", async () => {
+ mockGetAllAgentsForWorkspace.mockResolvedValue([
+ { id: "a1", ownerId: "owner1", instructions: "shared", name: "Bot", runtimeConfig: {} },
+ ]);
+ mockGetAllEmailAccountsForWorkspace.mockResolvedValue([]);
+ mockGetAllColleaguesForWorkspace.mockResolvedValue([]);
+ mockGetMemberByUserAndWorkspace.mockResolvedValue({ globalInstruction: "" });
+ mockGetUser.mockResolvedValue({ name: "Owner", email: "[email protected]" });
+ mockGetConversation.mockResolvedValue(null);
+
+ const task1 = makeTask({ id: "t1", agentId: "a1" });
+ const task2 = makeTask({ id: "t2", agentId: "a1" });
+ const result = await builder.buildFullPayloads([task1, task2], "w1");
+
+ expect(result).toHaveLength(2);
+ expect(result[0].agent!.name).toBe("Bot");
+ expect(result[1].agent!.name).toBe("Bot");
+ expect(mockGetAllAgentsForWorkspace).toHaveBeenCalledTimes(1);
+ expect(mockGetAllEmailAccountsForWorkspace).toHaveBeenCalledTimes(1);
+ expect(mockGetAllColleaguesForWorkspace).toHaveBeenCalledTimes(1);
+ expect(mockGetMemberByUserAndWorkspace).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/src/web/src/lib/services/task-payload-builder.ts b/src/web/src/lib/services/task-payload-builder.ts
new file mode 100644
index 000000000..6ec239bcf
--- /dev/null
+++ b/src/web/src/lib/services/task-payload-builder.ts
@@ -0,0 +1,146 @@
+import type { Database, ClaimedTaskRow } from "@alook/shared";
+import { queries, TASK_TYPES, toAlookAddress } from "@alook/shared";
+import { taskToResponse } from "@/lib/api/responses";
+import { cached, cacheKeys } from "@/lib/cache";
+
+export class TaskPayloadBuilder {
+ constructor(private db: Database) {}
+
+ async buildFullPayloads(tasks: ClaimedTaskRow[], workspaceId: string) {
+ const nonKillTasks = tasks.filter((t) => t.type !== TASK_TYPES.KILL_TASK);
+ const agentIds = [...new Set(nonKillTasks.map((t) => t.agentId))];
+
+ const [allAgents, allEmailAccounts, allColleagues] = agentIds.length > 0
+ ? await Promise.all([
+ cached(cacheKeys.allAgents(workspaceId), 300, () => queries.agent.getAllAgentsForWorkspace(this.db, workspaceId)),
+ cached(cacheKeys.allEmailAccounts(workspaceId), 600, () => queries.emailAccount.getAllEmailAccountsForWorkspace(this.db, workspaceId)),
+ cached(cacheKeys.allColleagues(workspaceId), 600, () => queries.agentLink.getAllColleaguesForWorkspace(this.db, workspaceId)).catch(() => [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>),
+ ]).then(([agents, emails, colleagues]) => {
+ const agentIdSet = new Set(agentIds);
+ return [
+ agents.filter((a) => agentIdSet.has(a.id)),
+ emails.filter((a) => agentIdSet.has(a.agentId)),
+ colleagues.filter((c) => agentIdSet.has(c.agentId)),
+ ] as const;
+ })
+ : [[], [], [] as Awaited<ReturnType<typeof queries.agentLink.getAllColleaguesForWorkspace>>];
+
+ const agentMap = new Map(allAgents.map((a) => [a.id, a]));
+ const emailAccountsByAgent = new Map<string, string[]>();
+ for (const acc of allEmailAccounts) {
+ const list = emailAccountsByAgent.get(acc.agentId) ?? [];
+ list.push(acc.emailAddress);
+ emailAccountsByAgent.set(acc.agentId, list);
+ }
+ const colleaguesByAgent = new Map<string, typeof allColleagues>();
+ for (const c of allColleagues) {
+ const list = colleaguesByAgent.get(c.agentId) ?? [];
+ list.push(c);
+ colleaguesByAgent.set(c.agentId, list);
+ }
+
+ const memberCache = new Map<string, { globalInstruction: string } | null>();
+ const userCache = new Map<string, { name: string; email: string } | null>();
+ const convoCache = new Map<string, Awaited<ReturnType<typeof queries.conversation.getConversation>> | null>();
+
+ const results = [];
+ for (const task of tasks) {
+ if (task.type === TASK_TYPES.KILL_TASK) {
+ results.push({ ...taskToResponse(task), agent: null, sender: null });
+ continue;
+ }
+
+ const agent = agentMap.get(task.agentId) ?? null;
+ const emailAddresses: string[] = [];
+ if (agent) {
+ if (agent.emailHandle) emailAddresses.push(`${agent.emailHandle}@alook.ai`);
+ const customAccounts = emailAccountsByAgent.get(agent.id) ?? [];
+ emailAddresses.push(...customAccounts);
+ }
+
+ let instructions = agent?.instructions ?? "";
+ if (agent?.ownerId) {
+ if (!memberCache.has(agent.ownerId)) {
+ const m = await cached(
+ cacheKeys.member(workspaceId, agent.ownerId),
+ 600,
+ () => queries.member.getMemberByUserAndWorkspace(this.db, agent.ownerId!, workspaceId),
+ );
+ memberCache.set(agent.ownerId, m ? { globalInstruction: m.globalInstruction } : null);
+ }
+ const cachedMember = memberCache.get(agent.ownerId);
+ if (cachedMember?.globalInstruction) {
+ instructions = [cachedMember.globalInstruction, instructions].filter(Boolean).join("\n\n");
+ }
+ }
+
+ let ownerName: string | null = null;
+ if (agent?.ownerId) {
+ if (!userCache.has(agent.ownerId)) {
+ const u = await cached(
+ cacheKeys.user(agent.ownerId),
+ 1800,
+ () => queries.user.getUser(this.db, agent.ownerId!),
+ );
+ userCache.set(agent.ownerId, u ? { name: u.name, email: u.email } : null);
+ }
+ ownerName = userCache.get(agent.ownerId)?.name ?? null;
+ }
+
+ let convo = convoCache.get(task.conversationId) ?? null;
+ if (task.conversationId && !convoCache.has(task.conversationId)) {
+ convo = await queries.conversation.getConversation(this.db, task.conversationId, workspaceId);
+ convoCache.set(task.conversationId, convo);
+ }
+ const taskChannel = convo?.channel ?? "default";
+
+ let sender: { name: string; email: string; is_owner: boolean } | null = null;
+ if (task.type === TASK_TYPES.USER_DM_MESSAGE && convo?.userId) {
+ if (!userCache.has(convo.userId)) {
+ const u = await cached(
+ cacheKeys.user(convo.userId),
+ 1800,
+ () => queries.user.getUser(this.db, convo!.userId!),
+ );
+ userCache.set(convo.userId, u ? { name: u.name, email: u.email } : null);
+ }
+ const cachedUser = userCache.get(convo.userId);
+ if (cachedUser) {
+ sender = {
+ name: cachedUser.name,
+ email: cachedUser.email,
+ is_owner: convo.userId === agent?.ownerId,
+ };
+ }
+ }
+
+ const rawColleagues = colleaguesByAgent.get(task.agentId) ?? [];
+ const colleagues = rawColleagues.map((c) => ({
+ name: c.name,
+ email: c.emailHandle ? toAlookAddress(c.emailHandle) : "",
+ description: c.description,
+ instruction: c.instruction,
+ }));
+
+ results.push({
+ ...taskToResponse(task),
+ channel: taskChannel,
+ sender,
+ agent: agent
+ ? {
+ instructions,
+ name: agent.name,
+ runtime_config: (agent.runtimeConfig || {}) as Record<string, unknown>,
+ email_handle: agent.emailHandle || null,
+ email_addresses: emailAddresses,
+ user_email: null as string | null,
+ user_name: ownerName,
+ colleagues,
+ }
+ : null,
+ });
+ }
+
+ return results;
+ }
+}
diff --git a/src/web/src/lib/services/task.ts b/src/web/src/lib/services/task.ts
index 5c274f97f..caad355f9 100644
--- a/src/web/src/lib/services/task.ts
+++ b/src/web/src/lib/services/task.ts
@@ -1,9 +1,10 @@
import type { Database } from "@alook/shared";
import { queries, TASK_TYPES, MAX_TASKS_PER_TRACE } from "@alook/shared";
import { log } from "@/lib/logger";
-import { broadcastToUser } from "@/lib/broadcast";
+import { broadcastToUser, broadcastToDaemon } from "@/lib/broadcast";
import { messageToResponse, taskToResponse } from "@/lib/api/responses";
import { invalidate, cacheKeys } from "@/lib/cache";
+import { TaskPayloadBuilder } from "@/lib/services/task-payload-builder";
const taskQueries = queries.task;
const agentQueries = queries.agent;
@@ -51,6 +52,10 @@ export class TaskService {
parentTaskId: opts?.parentTaskId ?? null,
});
invalidate(cacheKeys.activeTaskCounts(workspaceId)).catch(() => {});
+ // Push task to daemon via WS (best-effort). Awaited to ensure task state
+ // settles (dispatched on success, reverted to queued on failure) before
+ // the HTTP response returns, preventing races with subsequent poll calls.
+ await this.pushTaskToDaemon(task, workspaceId).catch(() => {});
return task;
}
@@ -407,4 +412,31 @@ export class TaskService {
}
}
}
+
+ private async pushTaskToDaemon(
+ task: Awaited<ReturnType<typeof taskQueries.createTask>>,
+ workspaceId: string,
+ ) {
+ const runtime = await queries.runtime.getAgentRuntime(this.db, task.runtimeId);
+ if (!runtime) return;
+
+ const dispatched = await taskQueries.dispatchTaskById(this.db, task.id, workspaceId);
+ if (!dispatched) return;
+
+ const builder = new TaskPayloadBuilder(this.db);
+ const payloads = await builder.buildFullPayloads([dispatched], workspaceId);
+ if (payloads.length === 0) {
+ await taskQueries.revertDispatchedToQueued(this.db, task.id, workspaceId);
+ return;
+ }
+
+ try {
+ await broadcastToDaemon(runtime.daemonId, {
+ type: "daemon.tasks",
+ tasks: payloads,
+ });
+ } catch {
+ await taskQueries.revertDispatchedToQueued(this.db, task.id, workspaceId);
+ }
+ }
}
diff --git a/src/ws-do/src/index.ts b/src/ws-do/src/index.ts
index 2643b03ab..357f53979 100644
--- a/src/ws-do/src/index.ts
+++ b/src/ws-do/src/index.ts
@@ -9,6 +9,17 @@ export default {
const url = new URL(request.url)
const traceId = request.headers.get("X-Trace-Id") ?? undefined
+ const daemonBroadcast = url.pathname.match(/^\/broadcast\/daemon\/(.+)$/)
+ if (daemonBroadcast && request.method === "POST") {
+ const daemonId = daemonBroadcast[1]
+ const reqLog = log.child({ traceId, daemonId })
+ reqLog.debug("broadcasting to daemon")
+
+ const doId = env.WS_DO.idFromName("daemon:" + daemonId)
+ const stub = env.WS_DO.get(doId)
+ return stub.fetch(new Request("http://internal/broadcast", { method: "POST", body: request.body, duplex: "half" } as RequestInit))
+ }
+
const userBroadcast = url.pathname.match(/^\/broadcast\/user\/(.+)$/)
if (userBroadcast && request.method === "POST") {
const userId = userBroadcast[1]
@@ -20,6 +31,16 @@ export default {
return stub.fetch(new Request("http://internal/broadcast", { method: "POST", body: request.body, duplex: "half" } as RequestInit))
}
+ const daemonId = url.searchParams.get("daemonId")
+ if (daemonId) {
+ const reqLog = log.child({ traceId, daemonId })
+ reqLog.info("daemon websocket upgrade")
+
+ const doId = env.WS_DO.idFromName("daemon:" + daemonId)
+ const stub = env.WS_DO.get(doId)
+ return stub.fetch(request)
+ }
+
const userId = url.searchParams.get("userId")
if (!userId) return new Response("userId required", { status: 400 })
diff --git a/src/ws-do/src/ws-durable.test.ts b/src/ws-do/src/ws-durable.test.ts
index 72de6bf49..f07ebd632 100644
--- a/src/ws-do/src/ws-durable.test.ts
+++ b/src/ws-do/src/ws-durable.test.ts
@@ -129,7 +129,7 @@ describe("WebSocketDurableObject", () => {
const acceptCall = (ctx.acceptWebSocket as ReturnType<typeof vi.fn>).mock.calls[0]
const serverWs = acceptCall[0]
- expect(serverWs.deserializeAttachment()).toEqual({ userId: "", authenticated: false })
+ expect(serverWs.deserializeAttachment()).toEqual({ type: "user", userId: "", authenticated: false })
})
})
@@ -139,9 +139,9 @@ describe("WebSocketDurableObject", () => {
// Set up two WebSockets: one authenticated, one not
const wsAuth = createMockWebSocket()
- wsAuth.serializeAttachment({ userId: "u1", authenticated: true })
+ wsAuth.serializeAttachment({ type: "user", userId: "u1", authenticated: true })
const wsUnauth = createMockWebSocket()
- wsUnauth.serializeAttachment({ userId: "", authenticated: false })
+ wsUnauth.serializeAttachment({ type: "user", userId: "", authenticated: false })
;(ctx.getWebSockets as ReturnType<typeof vi.fn>).mockReturnValue([wsAuth, wsUnauth])
const req = new Request("http://internal/broadcast", {
@@ -162,9 +162,9 @@ describe("WebSocketDurableObject", () => {
const { durable, ctx } = createDO()
const wsOpen = createMockWebSocket(WebSocket.OPEN)
- wsOpen.serializeAttachment({ userId: "u1", authenticated: true })
+ wsOpen.serializeAttachment({ type: "user", userId: "u1", authenticated: true })
const wsClosed = createMockWebSocket(WebSocket.CLOSED)
- wsClosed.serializeAttachment({ userId: "u1", authenticated: true })
+ wsClosed.serializeAttachment({ type: "user", userId: "u1", authenticated: true })
;(ctx.getWebSockets as ReturnType<typeof vi.fn>).mockReturnValue([wsOpen, wsClosed])
const req = new Request("http://internal/broadcast", {
@@ -185,13 +185,13 @@ describe("WebSocketDurableObject", () => {
mockGetValidSession.mockResolvedValue("user-42")
const ws = createMockWebSocket()
- ws.serializeAttachment({ userId: "", authenticated: false })
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
await durable.webSocketMessage(ws as any, JSON.stringify({ type: "auth", token: "valid-token" }))
expect(mockGetValidSession).toHaveBeenCalledWith({}, "valid-token")
expect(ws.send).toHaveBeenCalledWith(JSON.stringify({ type: "auth.ok" }))
- expect(ws.deserializeAttachment()).toEqual({ userId: "user-42", authenticated: true })
+ expect(ws.deserializeAttachment()).toEqual({ type: "user", userId: "user-42", authenticated: true })
})
it("closes with 1008 on invalid token", async () => {
@@ -199,7 +199,7 @@ describe("WebSocketDurableObject", () => {
mockGetValidSession.mockResolvedValue(null)
const ws = createMockWebSocket()
- ws.serializeAttachment({ userId: "", authenticated: false })
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
await durable.webSocketMessage(ws as any, JSON.stringify({ type: "auth", token: "bad" }))
@@ -211,7 +211,7 @@ describe("WebSocketDurableObject", () => {
const { durable } = createDO()
const ws = createMockWebSocket()
- ws.serializeAttachment({ userId: "", authenticated: false })
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
await durable.webSocketMessage(ws as any, JSON.stringify({ type: "some-event" }))
@@ -223,13 +223,13 @@ describe("WebSocketDurableObject", () => {
mockGetValidSession.mockResolvedValue(null)
const ws = createMockWebSocket()
- ws.serializeAttachment({ userId: "", authenticated: false })
+ ws.serializeAttachment({ type: "user", userId: "", authenticated: false })
await durable.webSocketMessage(ws as any, JSON.stringify({ type: "auth", token: "expired-token" }))
expect(ws.close).toHaveBeenCalledWith(1008, "Unauthorized")
expect(ws.send).not.toHaveBeenCalled()
- expect(ws.deserializeAttachment()).toEqual({ userId: "", authenticated: false })
+ expect(ws.deserializeAttachment()).toEqual({ type: "user", userId: "", authenticated: false })
})
it("closes on invalid JSON", async () => {
diff --git a/src/ws-do/src/ws-durable.ts b/src/ws-do/src/ws-durable.ts
index f639b2d86..786c64ccc 100644
--- a/src/ws-do/src/ws-durable.ts
+++ b/src/ws-do/src/ws-durable.ts
@@ -3,10 +3,9 @@ import { createDb, queries, createLogger } from "@alook/shared"
const log = createLogger({ service: "ws-do" })
-interface ConnectionState {
- userId: string
- authenticated: boolean
-}
+type ConnectionState =
+ | { type: "user"; userId: string; authenticated: boolean }
+ | { type: "daemon"; daemonId: string; authenticated: boolean }
export class WebSocketDurableObject extends DurableObject<Env> {
async fetch(request: Request): Promise<Response> {
@@ -29,7 +28,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
// Connections start unauthenticated. Client must send {type:"auth",token}
// before any other message. See webSocketMessage below.
- server.serializeAttachment({ userId: "", authenticated: false } as ConnectionState)
+ server.serializeAttachment({ type: "user", userId: "", authenticated: false } as ConnectionState)
this.ctx.setWebSocketAutoResponse(
new WebSocketRequestResponsePair("ping", "pong")
@@ -46,15 +45,29 @@ export class WebSocketDurableObject extends DurableObject<Env> {
const state = ws.deserializeAttachment() as ConnectionState
- const msg = parsed as { type: string; token?: string }
+ const msg = parsed as { type: string; token?: string; machineToken?: string; daemonId?: string }
+
if (msg.type === "auth") {
+ if (msg.machineToken && msg.daemonId) {
+ const valid = await this.validateMachineToken(msg.machineToken, msg.daemonId)
+ if (!valid) {
+ log.warn("daemon websocket auth failed", { daemonId: msg.daemonId })
+ ws.close(1008, "Unauthorized")
+ return
+ }
+ ws.serializeAttachment({ type: "daemon", daemonId: msg.daemonId, authenticated: true } as ConnectionState)
+ log.info("daemon websocket authenticated", { daemonId: msg.daemonId })
+ ws.send(JSON.stringify({ type: "auth.ok" }))
+ return
+ }
+
const userId = await this.validateToken(msg.token!)
if (!userId) {
log.warn("websocket auth failed")
ws.close(1008, "Unauthorized")
return
}
- ws.serializeAttachment({ userId, authenticated: true } as ConnectionState)
+ ws.serializeAttachment({ type: "user", userId, authenticated: true } as ConnectionState)
log.info("websocket authenticated", { userId })
ws.send(JSON.stringify({ type: "auth.ok" }))
return
@@ -66,7 +79,12 @@ export class WebSocketDurableObject extends DurableObject<Env> {
}
}
- async webSocketClose(): Promise<void> {}
+ async webSocketClose(ws: WebSocket): Promise<void> {
+ const state = ws.deserializeAttachment() as ConnectionState
+ if (state?.type === "daemon" && state.authenticated) {
+ log.info("daemon websocket closed", { daemonId: state.daemonId })
+ }
+ }
async webSocketError(ws: WebSocket, error: unknown): Promise<void> {
log.error("websocket error", { err: error instanceof Error ? error : String(error) })
@@ -86,4 +104,13 @@ export class WebSocketDurableObject extends DurableObject<Env> {
const db = createDb(this.env.DB)
return queries.session.getValidSession(db, token)
}
+
+ private async validateMachineToken(token: string, daemonId: string): Promise<boolean> {
+ if (!token.startsWith("al_")) return false
+ const db = createDb(this.env.DB)
+ const mt = await queries.machineToken.getMachineTokenByToken(db, token)
+ if (!mt || mt.status !== "active" || !mt.workspaceId) return false
+ const runtimes = await queries.runtime.getRuntimeIdsByDaemon(db, daemonId, mt.workspaceId)
+ return runtimes.length > 0
+ }
}