fix(control): sync DB prompt_queue → /tmp on local cockpit-app reads

Adjacent gap caught by direct probe: Neon project_states.prompt_queue
for 'cockpit' had 1 item, /tmp/agent-queue-cockpit on the same
machine showed empty array. The drift exists because /api/beacon/
queue/[tab] PUT writes to BOTH DB and /tmp, but on Vercel that /tmp
is ephemeral; the user's local machine never receives that write.
Net: home/'s /control queue badge (3c78208) reads /tmp → reports 0
queued, even though DB has the truth.

Fix is small and stays in cockpit-app: every /api/control GET (which
the UI polls every few seconds while open) now also mirrors each
project's DB prompt_queue out to /tmp/agent-queue-<tab>, gated on
isRuntimeAvailable() so it ONLY runs on the user's local box (Vercel
serverless never touches /tmp pointlessly). Atomic write via
.tmp+rename matches the existing pattern in /api/beacon/queue.

Net effect:
  • home/'s queue badge reflects DB reality while the UI is open
  • bash hook's two /tmp readers (agent-hook-bridge.sh:413,607) get
    fresh data without my touching the file the user has denied
    edits to 3x
  • cockpit-app's own queue UI keeps working as before (DB is still
    the source of truth)

Doesn't address the gap when the UI is CLOSED — /tmp goes stale
then. Tolerable for now since both readers fire when someone is
using cockpit, and the UI is open then. Long-term: periodic
background sync job in cockpit-daemon, or home/ reads DB directly.

tsc clean, npm run test:home 89/89.
This commit is contained in:
g-but committed 2026-05-20 12:15:48 +02:00
1 parent 3c782088b3
commit 58e4366ab1
1 file changed
+18
+18
View File
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { exec } from "child_process";
import { promisify } from "util";
import path from "path";
import fs from "fs";
import { getZellijTabs, shellEscape } from "@/lib/zellij";
import { getProjects, type ProjectRow } from "@/db/queries/projects";
import { createOrchestrationEvent, getLatestEventsByProjectKeys } from "@/db/queries/orchestration-events";
@@ -263,6 +264,23 @@ export async function GET() {
const liveTab = resolveEffectiveTab(tab, zellijTabs);
const session = parseSession(liveTab);
// Mirror DB-backed prompt queue → /tmp/agent-queue-<tab> on local
// cockpit-app boxes ONLY (runtime present means we're on the user's
// machine, not Vercel). Closes the drift home/'s /control queue
// badge surfaces: when the queue is PUT to Vercel, the /tmp mirror
// on Vercel goes nowhere, so the local /tmp stays stale even though
// Neon has the truth. Refreshing on every /api/control GET keeps the
// local file in sync while the UI is open — long enough that home/
// and the bash hook (agent-hook-bridge.sh:413,607) see fresh queue
// data.
if (!readonly && isRuntimeAvailable() && dbState?.promptQueue) {
try {
const p = stateFile.queue(tab.toLowerCase());
fs.writeFileSync(p + ".tmp", JSON.stringify(dbState.promptQueue));
fs.renameSync(p + ".tmp", p);
} catch { /* mirror failure is non-fatal — DB is source of truth */ }
}
// Only the project's owner writes to project_states. A viewer reading a team
// (readonly) project's session would otherwise create a row under their own
// userId, which would never be queried again and would drift from the owner's.