Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(desktop): close a codex web search tool call when it completes (#102100)

posthog[bot]57a63c7a1378
diff --git a/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.test.ts b/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.test.ts
index b1f2a2026365..35065a828b98 100644
--- a/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.test.ts
+++ b/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.test.ts
@@ -142,6 +142,23 @@ describe("mapAppServerNotification", () => {
expect(queryless?.update).toMatchObject({ title: "Web search" });
});
+ it("completes a statusless item, so a webSearch does not stay in progress", () => {
+ const result = mapAppServerNotification(
+ "s-1",
+ APP_SERVER_NOTIFICATIONS.ITEM_COMPLETED,
+ { item: { type: "webSearch", id: "w1", query: "posthog hogql docs" } },
+ );
+
+ expect(result).toEqual({
+ sessionId: "s-1",
+ update: {
+ sessionUpdate: "tool_call_update",
+ toolCallId: "w1",
+ status: "completed",
+ },
+ });
+ });
+
it("maps a declined completion to a failed tool_call_update", () => {
const result = mapAppServerNotification(
"s-1",
diff --git a/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.ts b/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.ts
index 5d76af301901..fe87e4adaf6e 100644
--- a/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.ts
+++ b/products/desktop/packages/agent/src/adapters/codex-app-server/mapping.ts
@@ -619,6 +619,11 @@ function mapStatus(
): "completed" | "failed" | "in_progress" {
if (status === "completed") return "completed";
if (status === "failed" || status === "declined") return "failed";
+ // Both callers describe a finished item, and item types such as `webSearch`
+ // carry no status field at all. Reporting `in_progress` there leaves the tool
+ // call open for good: the thread chip keeps reading as active and the APM
+ // span only ends when the turn does.
+ if (status === undefined) return "completed";
return "in_progress";
}
diff --git a/products/desktop/packages/agent/src/otel-telemetry.test.ts b/products/desktop/packages/agent/src/otel-telemetry.test.ts
index c863e5958e22..7ad8ef91d548 100644
--- a/products/desktop/packages/agent/src/otel-telemetry.test.ts
+++ b/products/desktop/packages/agent/src/otel-telemetry.test.ts
@@ -726,6 +726,33 @@ describe("OtelRunTelemetry", () => {
.map((span) => span.name)
.sort(),
).toEqual(["task_run", "tool_call:read", "turn"]);
+ expect(spanByName("tool_call:read").attributes).toMatchObject({
+ tool_status: "unterminated",
+ });
+ });
+
+ it("settles a tool call that arrives already terminal", async () => {
+ telemetry.append(RUN_ID, makeEntry("session/prompt", {}));
+ // A memory recall emits one tool_call and never updates it again.
+ telemetry.append(
+ RUN_ID,
+ sessionUpdate({
+ sessionUpdate: "tool_call",
+ toolCallId: "t1",
+ kind: "read",
+ status: "completed",
+ }),
+ );
+ telemetry.append(
+ RUN_ID,
+ makeEntry("_posthog/turn_complete", { stopReason: "end_turn" }),
+ );
+
+ await telemetry.shutdown();
+
+ const tool = spanByName("tool_call:read");
+ expect(tool.status.code).toBe(SpanStatusCode.OK);
+ expect(tool.attributes).toMatchObject({ tool_status: "completed" });
});
});
});
diff --git a/products/desktop/packages/agent/src/otel-trace-builder.ts b/products/desktop/packages/agent/src/otel-trace-builder.ts
index 012fc7d23ee6..23094c41ddae 100644
--- a/products/desktop/packages/agent/src/otel-trace-builder.ts
+++ b/products/desktop/packages/agent/src/otel-trace-builder.ts
@@ -247,6 +247,14 @@ export class RunTraceBuilder {
parentContext,
);
const context = trace.setSpan(parentContext, span);
+ // A tool call can arrive already terminal, as a memory recall does. No
+ // later update follows it, so settling it here keeps it out of the sweep,
+ // which would otherwise report a known outcome as unterminated.
+ const status = update.status;
+ if (status === "completed" || status === "failed") {
+ this.settleTool(span, status, time);
+ return context;
+ }
if (toolCallId) {
this.toolSpans.set(toolCallId, { span, context });
} else {
@@ -256,6 +264,19 @@ export class RunTraceBuilder {
return context;
}
+ /** Records a terminal tool outcome on its span and ends it. */
+ private settleTool(
+ span: Span,
+ status: "completed" | "failed",
+ time: Date,
+ ): void {
+ span.setAttribute("tool_status", status);
+ span.setStatus({
+ code: status === "failed" ? SpanStatusCode.ERROR : SpanStatusCode.OK,
+ });
+ span.end(time);
+ }
+
private handleToolUpdate(
update: Record<string, unknown>,
time: Date,
@@ -266,11 +287,7 @@ export class RunTraceBuilder {
const status = update.status;
if (status === "completed" || status === "failed") {
- open.span.setAttribute("tool_status", status);
- open.span.setStatus({
- code: status === "failed" ? SpanStatusCode.ERROR : SpanStatusCode.OK,
- });
- open.span.end(time);
+ this.settleTool(open.span, status, time);
this.toolSpans.delete(toolCallId);
}
return open.context;
@@ -298,12 +315,18 @@ export class RunTraceBuilder {
* mid-flight) marks them errored so APM doesn't show a healthy-looking
* active tool under a failed run; otherwise the outcome is unknown and the
* status stays unset.
+ *
+ * A swept span's duration runs to the end of the turn instead of measuring
+ * the tool, so `unterminated` names it. Otherwise the sweep is
+ * indistinguishable from a slow tool and inflates the kind's percentiles.
*/
private closeOpenTools(time: Date, opts?: { interrupted?: boolean }): void {
for (const { span } of this.toolSpans.values()) {
if (opts?.interrupted) {
span.setAttribute("tool_status", "interrupted");
span.setStatus({ code: SpanStatusCode.ERROR });
+ } else {
+ span.setAttribute("tool_status", "unterminated");
}
span.end(time);
}