Skip to content

Commit f9583ad

Browse files
committed
fix(gateway): report pre-launch worker errors
1 parent f1ca5e2 commit f9583ad

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎src/gateway/server-methods/chat-send-agent-dispatch.ts‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,19 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
395395
await measureDiagnosticsTimelineSpan(
396396
"gateway.chat_send.post_dispatch",
397397
async () => {
398-
const returnedAgentErrorPayloads = agentRunStarted
399-
? replyDispatch.deliveredReplies
400-
.map((entryInner) => entryInner.payload)
401-
.filter((payload) => payload.isError)
402-
: [];
398+
const returnedAgentErrorPayloads = replyDispatch.deliveredReplies
399+
.map((entryInner) => entryInner.payload)
400+
.filter((payload) => payload.isError);
401+
const hasReturnedAgentError =
402+
returnedAgentErrorPayloads.length > 0 &&
403+
(agentRunStarted || !isInternalTextSlashCommandTurn);
403404
const returnedAgentErrorMessage =
404405
returnedAgentErrorPayloads
405406
.map((payload) => payload.text?.trim())
406407
.filter((text): text is string => Boolean(text))
407408
.join(" | ") || undefined;
408409
if (
409-
agentRunStarted &&
410-
returnedAgentErrorPayloads.length > 0 &&
410+
hasReturnedAgentError &&
411411
!userTurnRecorder.hasPersisted() &&
412412
!userTurnRecorder.isBlocked()
413413
) {
@@ -426,7 +426,7 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
426426
// Agent runs persist model-visible turns through SessionManager; this dispatcher owns
427427
// live delivery. Mirroring agent finals would duplicate normal assistant turns. The
428428
// non-agent branch has no runtime-owned turn, so it appends one before broadcasting.
429-
if (!agentRunStarted && !queuedFollowup.isEnqueued()) {
429+
if (!agentRunStarted && !queuedFollowup.isEnqueued() && !hasReturnedAgentError) {
430430
await finalizeChatSendNonAgentReplies({
431431
accountId,
432432
context,
@@ -443,12 +443,11 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
443443
context,
444444
deliveredReplies: replyDispatch.deliveredReplies,
445445
emitFirstAssistantServerTiming,
446-
hasReturnedAgentErrorPayloads: returnedAgentErrorPayloads.length > 0,
446+
hasReturnedAgentErrorPayloads: hasReturnedAgentError,
447447
session,
448448
});
449449
}
450-
const shouldBroadcastAgentError =
451-
returnedAgentErrorPayloads.length > 0 && !broadcastedSourceReplyFinal;
450+
const shouldBroadcastAgentError = hasReturnedAgentError && !broadcastedSourceReplyFinal;
452451
if (shouldBroadcastAgentError) {
453452
broadcastChatError({
454453
context,

‎src/gateway/server-methods/chat.directive-tags.test.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,10 +4521,15 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45214521
expect(finalBroadcasts).toStrictEqual([]);
45224522
});
45234523

4524-
it("broadcasts returned agent-run error payloads after an agent starts", async () => {
4524+
it.each([
4525+
["after start", true],
4526+
["before launch", false],
4527+
] as const)("broadcasts returned agent-run error payloads $0", async (_name, agentStarted) => {
45254528
await createGatewayUserTurnSqliteFixture("openclaw-chat-send-agent-returned-error-");
4526-
const errorMessage = "LLM idle timeout (120s): no response from model";
4527-
mockState.triggerAgentRunStart = true;
4529+
const errorMessage = agentStarted
4530+
? "LLM idle timeout (120s): no response from model"
4531+
: "Worker must bootstrap the current build before continuing";
4532+
mockState.triggerAgentRunStart = agentStarted;
45284533
mockState.dispatchedReplies = [
45294534
{
45304535
kind: "final",
@@ -4535,23 +4540,24 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
45354540
},
45364541
];
45374542
const { context, send } = createChatRequestFixture();
4543+
const runId = agentStarted ? "idem-agent-returned-error" : "idem-agent-rejected-before-launch";
45384544

45394545
const broadcast = await send({
4540-
idempotencyKey: "idem-agent-returned-error",
4546+
idempotencyKey: runId,
45414547
message: "please keep working",
45424548
});
45434549

45444550
expect(broadcast).toMatchObject({
4545-
runId: "idem-agent-returned-error",
4551+
runId,
45464552
sessionKey: "agent:main:main",
45474553
state: "error",
45484554
errorMessage,
45494555
});
45504556
expect(broadcast).not.toHaveProperty("message");
4551-
const dedupe = context.dedupe.get("chat:idem-agent-returned-error");
4557+
const dedupe = context.dedupe.get(`chat:${runId}`);
45524558
expect(dedupe?.ok).toBe(false);
45534559
expect(dedupe?.payload).toMatchObject({
4554-
runId: "idem-agent-returned-error",
4560+
runId,
45554561
status: "error",
45564562
summary: errorMessage,
45574563
});

0 commit comments

Comments
 (0)