fix(orchestration): emit task_failed on dispatch failure paths

Completes the lifecycle pair on the failure side. After commit 81f4dc3
closed task_started → task_completed on the happy path, the failure path
was still silent in orchestration_events:

  task_failed: 0 rows (lifetime)

…despite three real failure code paths each calling logDebug and either
returning 500 or marking orchestration_runs outcome:'error' — none wrote
to orchestration_events, so a single one-query dispatch-outcome timeline
could only see successes.

Three sites instrumented, each mirrors the existing task_started emit
shape (userId, projectId, projectKey, source, adapter, intent, detail)
with eventType:'task_failed' and detail carrying the truncated error
message:

  1. src/app/api/inject/route.ts !result.ok branch
     — covers the /api/inject failure path (the most-trafficked dispatch
       entry); previously every failed inject left an orphan task_started
  2. src/app/api/orchestration/run/route.ts codex/gemini catch
     — task_started was already emitted ~line 296 before injectIntoTab;
       if that throws, the pair closes here
  3. src/app/api/orchestration/run/route.ts openclaw worker catch
     — the orchestration_runs row was already marked outcome:'error'
       by commit 384cacc, but orchestration_events had nothing — now
       has the matching event row

All three writes are fire-and-forget (.catch(console.error)) so
telemetry failures don't break the user-facing error response. detail
field truncated to 400 chars (column is text but no need to dump full
stack into an events stream).

After this lands, the operator query becomes:

  SELECT date_trunc('hour', happened_at) hr,
         event_type, COUNT(*)
    FROM orchestration_events
   WHERE happened_at > NOW() - INTERVAL '24 hours'
     AND event_type IN ('task_started','task_completed','task_failed')
   GROUP BY 1, 2 ORDER BY 1 DESC, 2;

…which reads as a chronological dispatch-success-rate view. Previously
the same query showed only the 'task_started' column populated.

tsc + eslint + build clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-21 06:44:16 +02:00
1 parent 81f4dc397a
commit 9a0ac77ac3
2 files changed
+45

No files matched your search

+15
View File
@@ -210,6 +210,21 @@ export async function POST(req: NextRequest) {
runtimeAvailable: isRuntimeAvailable(),
},
});
// Mirror the task_started emit on the failure branch so the
// started/failed pair closes in orchestration_events — without this,
// every failed inject left an orphan task_started with no paired
// completion of any kind (task_completed OR task_failed).
createOrchestrationEvent({
userId,
projectId,
projectKey: canonical,
eventType: "task_failed",
source: "api-inject",
adapter: eventAdapter,
intent: eventIntent,
detail: `${promptLabel} — ${result.error}`.slice(0, 400),
happenedAt: new Date(nowS * 1000),
}).catch((err) => console.error("[inject] db write failed:", err));
return NextResponse.json({ error: `Injection failed: ${result.error}` }, { status: 500 });
}
+30
View File
@@ -338,6 +338,20 @@ export async function POST(req: NextRequest) {
message: `${request.adapter} inject failed: ${message}`,
meta: { userId, adapter: request.adapter, intent: request.intent, projectKey: request.projectKey, projectPath: request.projectPath },
});
// Close the started/failed pair — task_started was emitted at line ~296
// before injectIntoTab; if that throws, record the failed counterpart
// here so orchestration_events doesn't carry an orphan start.
createOrchestrationEvent({
userId,
projectId: request.projectId ?? null,
projectKey: request.projectKey,
eventType: "task_failed",
source: "api-orchestration",
adapter: request.adapter,
intent: request.intent,
detail: `${intent.name} — ${message}`.slice(0, 400),
happenedAt: new Date(),
}).catch((e) => console.error("[orchestration/run] task_failed write failed:", e));
return NextResponse.json({ error: `Inject failed: ${message}` }, { status: 500 });
}
}
@@ -387,6 +401,22 @@ export async function POST(req: NextRequest) {
message: `openclaw worker start failed: ${message}`,
meta: { userId, runId: run.id, adapter: request.adapter, intent: request.intent, projectKey: request.projectKey, projectPath: request.projectPath },
});
// Worker never started → the orchestration_runs row was just marked
// outcome:'error' above, but orchestration_events still had nothing
// for the openclaw failure path. Emit task_failed so the dispatch-
// outcome timeline shows the attempt regardless of which adapter
// failed.
createOrchestrationEvent({
userId,
projectId: request.projectId ?? null,
projectKey: request.projectKey,
eventType: "task_failed",
source: "api-orchestration",
adapter: request.adapter,
intent: request.intent,
detail: `openclaw worker start failed: ${message}`.slice(0, 400),
happenedAt: new Date(),
}).catch((e) => console.error("[orchestration/run] task_failed write failed:", e));
return NextResponse.json({ error: "Worker failed to start" }, { status: 500 });
}