Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(in-app-agent): mark loop errors on the APM span after ACK (#16404)

Lysander Kieselb79e5426227f
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
index d9a884de58..c10176a732 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
@@ -546,6 +546,10 @@ export async function executeInAppAgentRun(params: {
});
},
onError: async (error) => {
+ // The loop may close the stream after this callback instead of
+ // erroring it. Mark the job span now so Datadog APM status:error
+ // still has @error.message after we ACK the BullMQ job.
+ traceException(error);
await flushPersistedRunEvents({
status: InAppAgentRunStatus.FAILED,
...(failureCode() ?? {
diff --git a/worker/src/features/in-app-agent/runtime/agent.ts b/worker/src/features/in-app-agent/runtime/agent.ts
index f7f7a3c87f..95489b9daf 100644
--- a/worker/src/features/in-app-agent/runtime/agent.ts
+++ b/worker/src/features/in-app-agent/runtime/agent.ts
@@ -409,7 +409,7 @@ export async function createAgUiStream(params: {
subscription?.unsubscribe();
logger.error("Failed to persist in-app agent event", {
- error,
+ error: toLoggableError(error),
runId: params.input.runId,
threadId: params.input.threadId,
eventType,
@@ -745,7 +745,7 @@ export async function createAgUiStream(params: {
}
logger.error("Error in agent execution", {
- error,
+ error: toLoggableError(error),
runId: params.input.runId,
threadId: params.input.threadId,
});
@@ -819,7 +819,7 @@ export async function createAgUiStream(params: {
}
logger.error("Error initializing agent", {
- error,
+ error: toLoggableError(error),
runId: params.input.runId,
threadId: params.input.threadId,
});
@@ -1540,6 +1540,22 @@ function createRunErrorEvent(
};
}
+function toLoggableError(error: unknown): {
+ name?: string;
+ message: string;
+ stack?: string;
+} {
+ if (error instanceof Error) {
+ return {
+ name: error.name,
+ message: error.message,
+ stack: error.stack,
+ };
+ }
+
+ return { message: String(error) };
+}
+
function getRunErrorMessage(event: AgUiEvent) {
return typeof event.message === "string" && event.message.trim()
? event.message