fix: resolve all three functional issues in trial execution

- WebSocket broadcasts: Next.js tRPC router now routes broadcasts via
  POST /internal/broadcast on the Bun ws-server process, which holds
  the actual client connections. Broadcasts were previously silently
  dropped due to the split singleton across processes.

- ws-server stubs: request_trial_status and request_trial_events now
  use the real async DB methods instead of the stub getTrialStatusSync/
  getTrialEventsSync that always returned null/[].

- Duplicate branch case: removed the unreachable second case "branch"
  block in executeAction switch; server-side branching is a pass-through
  since routing is client-orchestrated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-08 13:24:32 -04:00
parent 943c7bd963
commit 14182bf078
4 changed files with 43 additions and 16 deletions
+17 -4
View File
@@ -46,9 +46,22 @@ console.log(`Starting WebSocket server on port ${port}...`);
serve<WSData>({
port,
fetch(req, server) {
async fetch(req, server) {
const url = new URL(req.url);
// Internal broadcast endpoint — called by Next.js tRPC router
if (url.pathname === "/internal/broadcast") {
if (req.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
const { trialId, message } = (await req.json()) as {
trialId: string;
message: { type: string; data: Record<string, unknown> };
};
await wsManager.broadcast(trialId, message);
return new Response("OK", { status: 200 });
}
if (url.pathname === "/api/websocket") {
if (req.headers.get("upgrade") !== "websocket") {
return new Response("WebSocket upgrade required", { status: 426 });
@@ -114,7 +127,7 @@ serve<WSData>({
}),
);
},
message(ws: ServerWebSocket<WSData>, message) {
async message(ws: ServerWebSocket<WSData>, message) {
const { clientId, trialId } = ws.data;
try {
@@ -131,7 +144,7 @@ serve<WSData>({
break;
case "request_trial_status": {
const status = wsManager.getTrialStatusSync(trialId);
const status = await wsManager.getTrialStatus(trialId);
ws.send(
JSON.stringify({
type: "trial_status",
@@ -146,7 +159,7 @@ serve<WSData>({
}
case "request_trial_events": {
const events = wsManager.getTrialEventsSync(
const events = await wsManager.getTrialEvents(
trialId,
msg.data?.limit ?? 100,
);