mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): record step cost and retries on traces, quiet two noisy logs (#62140)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
c33eaa170e
commit
cb79aef052
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -47,6 +47,8 @@ interface Input {
|
||||
readonly agent: Agent.ID
|
||||
readonly model: SessionRunnerModel.Resolved
|
||||
readonly prepared: Omit<SessionModelRequest.Prepared, "event">
|
||||
/** 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, {
|
||||
|
||||
@@ -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")),
|
||||
|
||||
@@ -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<Tool.Context>(),
|
||||
executions: new Array<string>(),
|
||||
closedTransports: new Array<Session.ID>(),
|
||||
spans: new Array<Tracer.NativeSpan>(),
|
||||
blockTools: (count = 1) =>
|
||||
Effect.acquireRelease(
|
||||
Effect.all({ started: Deferred.make<void>(), release: Deferred.make<void>() }).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([
|
||||
|
||||
@@ -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<void>()
|
||||
// Register before process release so the deadline remains active during termination.
|
||||
|
||||
Reference in New Issue
Block a user