Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/bcode-laminar/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// module, not here.

import type { Plugin } from "@opencode-ai/plugin"
import { SpanStatusCode } from "@opentelemetry/api"
import { NodeSDK } from "@opentelemetry/sdk-node"

import { createSpanExporter } from "./exporter"
Expand Down Expand Up @@ -108,6 +109,27 @@ export const LaminarPlugin: Plugin = ({ client }) => {
},
event: async ({ event }) => {
switch (event.type) {
case "session.error": {
// The turn span is the only place a failed run can be recorded.
// Nothing upstream does it: the AI SDK ends `ai.streamText` and
// `ai.streamText.doStream` inside a transform `flush` that never
// runs when the consumer aborts, so a provider error arriving
// mid-stream drops those spans entirely rather than marking them —
// the trace then showed a clean, shorter run. `session.error` is
// published just before the session goes idle (processor.ts `halt`),
// so the span is still open here; `session.idle` ends it below.
const sessionId = event.properties.sessionID
const span = sessionId ? sessionCurrentTurnSpan[sessionId] : undefined
const error = event.properties.error
// An abort is the user stopping the run, not a failure.
if (!span || !error || error.name === "MessageAbortedError") break
const detail = "message" in error.data ? error.data.message : ""
span.setStatus({
code: SpanStatusCode.ERROR,
message: detail ? `${error.name}: ${detail}` : error.name,
})
break
}
case "session.idle": {
const sessionId = event.properties.sessionID
const span = sessionCurrentTurnSpan[sessionId]
Expand Down
18 changes: 17 additions & 1 deletion packages/bcode-laminar/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// - `pino` logger — opencode plugins log via `client.app.log`; the plugin passes
// in a logger callback.

import { type Context, type Span, trace } from "@opentelemetry/api"
import { type Context, type Span, SpanStatusCode, trace } from "@opentelemetry/api"
import {
BatchSpanProcessor,
type ReadableSpan,
Expand All @@ -39,6 +39,7 @@ import { otelSpanIdToUUID, type StringUUID } from "./utils"

const SDK_VERSION = "bcode-laminar-0.1"
const SPAWNING_TOOL_NAMES = ["task"]
const TOOL_ERROR = "bcode.tool.error"
type LogFn = (level: "debug" | "info" | "warn" | "error", message: string) => void

export class OpenCodeLaminarSpanProcessor implements SpanProcessor {
Expand Down Expand Up @@ -153,6 +154,21 @@ export class OpenCodeLaminarSpanProcessor implements SpanProcessor {
}

onEnd(span: ReadableSpan): void {
// A tool that throws is normal agent flow, not a failed run: the model
// reads the `tool-error` result and adapts. The AI SDK still stamps ERROR
// on the `ai.toolCall` span (`recordErrorOnSpan`), and Laminar reds a
// whole trace if ANY of its spans is ERROR — so every run where the model
// wrote one buggy `browser_execute` snippet was reported as a failure.
// Demote to UNSET, keeping the message as an attribute and the recorded
// `exception` event untouched. Only the `turn` span decides run outcome.
//
// Mutated in place because `setStatus`/`setAttribute` are no-ops once the
// span has ended, which it has by the time onEnd runs.
if (span.attributes["ai.toolCall.id"] && span.status.code === SpanStatusCode.ERROR) {
Object.assign(span.attributes, { [TOOL_ERROR]: span.status.message ?? "" })
Object.assign(span, { status: { code: SpanStatusCode.UNSET } })
}

const spanId = span.spanContext().spanId
this.spanIdLists.delete(spanId)
this.spanIdToPath.delete(spanId)
Expand Down
Loading