From cb79aef05200e47a26500658b9c0c4adfaa3d683 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sat, 7 Nov 2026 13:48:23 -0800 Subject: [PATCH] fix(core): record step cost and retries on traces, quiet two noisy logs (#62140) Co-authored-by: Aiden Cline --- packages/core/src/config.ts | 11 ++-- packages/core/src/session/runner/llm.ts | 12 +++- packages/core/src/session/runner/step.ts | 7 +++ .../core/test/session-runner-recorded.test.ts | 1 + packages/core/test/session-runner.test.ts | 58 ++++++++++++++++++- packages/util/src/cross-spawn-spawner.ts | 2 +- 6 files changed, 81 insertions(+), 10 deletions(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index a629da0e53c..f4a3de1f5e9 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -148,14 +148,17 @@ export const layer = (options?: Options) => return } const result = ConfigNormalize.normalize(input) - yield* Effect.forEach(result.diagnostics, (diagnostic) => - Effect.logWarning("configuration normalization diagnostic", { + // Every reload repeats the routine notes on a config that loads. A rejected config or a dropped malformed + // setting is the user's to fix, so those stay warnings. + yield* Effect.forEach(result.diagnostics, (diagnostic) => { + const log = result.type === "rejected" || diagnostic.kind === "invalid" ? Effect.logWarning : Effect.logDebug + return log("configuration normalization diagnostic", { source, path: diagnostic.path[0] === "$" ? "$" : `$.${diagnostic.path.join(".")}`, kind: diagnostic.kind, action: diagnostic.message, - }), - ) + }) + }) if (result.type === "rejected") return const info = Option.getOrUndefined(decodeInfo(result.encoded)) if (info) return info diff --git a/packages/core/src/session/runner/llm.ts b/packages/core/src/session/runner/llm.ts index 857347d1d46..b52b20e42f6 100644 --- a/packages/core/src/session/runner/llm.ts +++ b/packages/core/src/session/runner/llm.ts @@ -237,6 +237,7 @@ const layer = Layer.effect( let initial: SessionContext.Loaded | undefined = first let recoverOverflow = true let recoverContinuation = true + let retryAttempt: number | undefined while (true) { // Reuse boundary preparation once; retries refresh context without delivering more input. const loaded = initial ?? (yield* prepareContext(sessionID).pipe(Effect.flatMap(context.load))) @@ -294,6 +295,7 @@ const layer = Layer.effect( // A tool call can arrive during a reload; its permission check reads the session's agent. executeTool: (call) => plugins.awaitActivation.pipe(Effect.andThen(prepared.executeTool(call))), }, + retryAttempt, retry: (cause, error, proposed) => retry.decide({ cause, @@ -312,6 +314,7 @@ const layer = Layer.effect( : Effect.succeed(false), ), }) + retryAttempt = undefined const completed = yield* SessionStep.Outcome.$match(outcome, { Completed: Effect.fnUntraced(function* (outcome) { // The Session is about to go idle, which leaves the background summary time to finish. @@ -321,13 +324,16 @@ const layer = Layer.effect( ) return outcome.needsContinuation }), - Retry: (outcome) => - retry.wait({ + Retry: Effect.fnUntraced(function* (outcome) { + retryAttempt = outcome.decision.attempt + yield* retry.wait({ decision: outcome.decision, error: outcome.error, assistantMessageID, - }), + }) + }), Continue: Effect.fnUntraced(function* (outcome) { + retryAttempt = outcome.decision.attempt yield* retry.wait({ decision: outcome.decision, error: outcome.error, diff --git a/packages/core/src/session/runner/step.ts b/packages/core/src/session/runner/step.ts index 9243c6702b5..4fa3178277e 100644 --- a/packages/core/src/session/runner/step.ts +++ b/packages/core/src/session/runner/step.ts @@ -47,6 +47,8 @@ interface Input { readonly agent: Agent.ID readonly model: SessionRunnerModel.Resolved readonly prepared: Omit + /** Set when this attempt runs because an earlier one in the step scheduled a retry. */ + readonly retryAttempt?: number readonly retry: ( cause: AIError, error: SessionError.Error, @@ -77,6 +79,7 @@ export const make = Effect.gen(function* () { yield* Effect.annotateCurrentSpan({ "gen_ai.provider.name": input.model.ref.providerID, "gen_ai.request.model": input.model.ref.id, + ...(input.retryAttempt === undefined ? {} : { "opencode.retry.attempt": input.retryAttempt }), }) const publisher = createLLMEventPublisher(bus, { sessionID: input.sessionID, @@ -188,6 +191,9 @@ export const make = Effect.gen(function* () { ...(tokens?.cacheReadInputTokens === undefined ? {} : { "gen_ai.usage.cache_read.input_tokens": tokens.cacheReadInputTokens }), + ...(tokens?.cacheWriteInputTokens === undefined + ? {} + : { "gen_ai.usage.cache_creation.input_tokens": tokens.cacheWriteInputTokens }), ...(llmFailure === undefined ? {} : { "error.type": llmFailure.reason._tag }), }) if ( @@ -255,6 +261,7 @@ export const make = Effect.gen(function* () { .pipe(Effect.orElseSucceed(() => undefined)) : undefined const usage = record.finish ? SessionUsage.record(record.finish.usage, input.model.cost) : undefined + if (usage) yield* Effect.annotateCurrentSpan("opencode.usage.cost_usd", usage.cost) if (record.failure) yield* publisher.publishStepFailure({ ...usage, snapshot, files }) if (record.finish && usage && !record.failure) yield* bus.publish(SessionEvent.Step.Ended, { diff --git a/packages/core/test/session-runner-recorded.test.ts b/packages/core/test/session-runner-recorded.test.ts index baa729d6652..b1f09b0224c 100644 --- a/packages/core/test/session-runner-recorded.test.ts +++ b/packages/core/test/session-runner-recorded.test.ts @@ -268,6 +268,7 @@ describe("SessionRunnerLLM recorded", () => { "gen_ai.response.finish_reasons": ["stop"], "gen_ai.usage.input_tokens": 465, "gen_ai.usage.output_tokens": 2, + "opencode.usage.cost_usd": 0, }) expect(attempt?.attributes.get("opencode.llm.first_output_ms")).toBeNumber() }).pipe(Effect.withSpan("fixture.request")), diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index b777003a52b..e3499f5dfda 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -77,7 +77,7 @@ import { SessionSystemPrompt } from "@opencode/core/session/system-prompt" import { ID, Model } from "@opencode/core/model" import { Location } from "@opencode/core/location" import { Provider } from "@opencode/core/provider" -import { Cause, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream } from "effect" +import { Cause, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Queue, Schema, Scope, Stream, Tracer } from "effect" import { TestClock } from "effect/testing" import { and, asc, desc, eq, sql } from "drizzle-orm" import { testEffect } from "./lib/effect" @@ -231,6 +231,7 @@ const makeRunnerState = (compaction?: SessionRunnerModel.Resolved["compaction"]) authorizations: new Array(), executions: new Array(), closedTransports: new Array(), + spans: new Array(), blockTools: (count = 1) => Effect.acquireRelease( Effect.all({ started: Deferred.make(), release: Deferred.make() }).pipe( @@ -470,7 +471,22 @@ const layer = Layer.unwrap( awaitIdle: coordinator.awaitIdle, }) }), - ).pipe(Layer.provide(runnerLayer), Layer.orDie) + ).pipe( + Layer.provide(runnerLayer), + Layer.provide( + Layer.succeed( + Tracer.Tracer, + Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options) + state.spans.push(span) + return span + }, + }), + ), + ), + Layer.orDie, + ) return AppNodeBuilder.build( LayerNode.group([ Database.node, @@ -6029,6 +6045,44 @@ describe("SessionRunnerLLM", () => { ]) }) + for (const failed of [ + { name: "before output", stream: () => Stream.fail(providerUnavailable()) }, + { + name: "after output", + stream: () => + TestLLM.failAfter( + providerUnavailable(), + LLMEvent.stepStart({ index: 0 }), + LLMEvent.textStart({ id: "traced-partial" }), + LLMEvent.textDelta({ id: "traced-partial", text: "Partial" }), + ), + }, + ]) + scenario(`marks only the retried attempt span with its attempt number ${failed.name}`, function* (s) { + yield* s.admit("Trace retried attempt") + yield* s.llm.push(failed.stream()) + yield* s.llm.push(TestLLM.text("Recovered", "traced-retry-success")) + + const scheduled = yield* subscribeRetries(s) + const run = yield* s.resume.pipe(Effect.forkChild) + yield* Queue.take(scheduled) + yield* TestClock.adjust("2400 millis") + yield* Fiber.join(run) + + expect(s.requests).toHaveLength(2) + expect( + s.spans + .filter((span) => span.name === "SessionStep.attempt") + .map((span) => ({ + error: span.attributes.get("error.type"), + attempt: span.attributes.get("opencode.retry.attempt"), + })), + ).toEqual([ + { error: "Transport", attempt: undefined }, + { error: undefined, attempt: 2 }, + ]) + }) + scenario("retries an unknown finish before output", function* (s) { yield* s.admit("Retry unknown finish") yield* s.llm.push([ diff --git a/packages/util/src/cross-spawn-spawner.ts b/packages/util/src/cross-spawn-spawner.ts index 5e67a4acaa9..723d246000a 100644 --- a/packages/util/src/cross-spawn-spawner.ts +++ b/packages/util/src/cross-spawn-spawner.ts @@ -431,7 +431,7 @@ const makeCrossSpawnSpawner = Effect.gen(function* () { command: ChildProcess.StandardCommand, opts: NodeChildProcess.SpawnOptions, ) { - yield* Effect.logInfo("spawning process", { command: command.command, args: command.args, cwd: opts.cwd }) + yield* Effect.logDebug("spawning process", { command: command.command, args: command.args, cwd: opts.cwd }) const [proc, closed, exited, settle] = yield* launchProcess(command, yield* executable(command.command), opts) const stopOutput = yield* Deferred.make() // Register before process release so the deadline remains active during termination.