Skip to content

Commit a6c9765

Browse files
saravmajesticclaude
andcommitted
fix(observability): carry the turn's trace onto loop-written messages
Compaction continue/replay, subtask summary and validator retry messages inherit the turn they continue. noReply prompts bind nothing; a prompt or command that fails before its loop releases its binding. Loop test through overflow + auto-compaction. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 1aa2489 commit a6c9765

6 files changed

Lines changed: 72 additions & 10 deletions

File tree

‎packages/opencode/src/server/routes/session.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -925,11 +925,15 @@ export const SessionRoutes = lazy(() =>
925925
return stream(c, async (stream) => {
926926
const sessionID = c.req.valid("param").sessionID
927927
const body = c.req.valid("json")
928-
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext)
928+
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext).
929+
// A noReply prompt runs no turn, so binds nothing; a prompt that fails before its loop is released.
929930
const messageID = body.messageID ?? MessageID.ascending()
930-
TraceContext.bind(messageID, c.req.header("traceparent"))
931+
TraceContext.bind(messageID, body.noReply ? undefined : c.req.header("traceparent"))
932+
const msg = await SessionPrompt.prompt({ ...body, sessionID, messageID }).catch((error) => {
933+
TraceContext.release(sessionID, [messageID])
934+
throw error
935+
})
931936
// altimate_change end
932-
const msg = await SessionPrompt.prompt({ ...body, sessionID, messageID })
933937
stream.write(JSON.stringify(msg))
934938
})
935939
},
@@ -961,11 +965,15 @@ export const SessionRoutes = lazy(() =>
961965
return stream(c, async () => {
962966
const sessionID = c.req.valid("param").sessionID
963967
const body = c.req.valid("json")
964-
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext)
968+
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext).
969+
// A noReply prompt runs no turn, so binds nothing.
965970
const messageID = body.messageID ?? MessageID.ascending()
966-
TraceContext.bind(messageID, c.req.header("traceparent"))
971+
TraceContext.bind(messageID, body.noReply ? undefined : c.req.header("traceparent"))
967972
// altimate_change end
968973
SessionPrompt.prompt({ ...body, sessionID, messageID }).catch((err) => {
974+
// altimate_change start — a prompt that failed before its loop is released here
975+
TraceContext.release(sessionID, [messageID])
976+
// altimate_change end
969977
log.error("prompt_async failed", { sessionID, error: err })
970978
Bus.publish(Session.Event.Error, {
971979
sessionID,
@@ -1008,11 +1016,15 @@ export const SessionRoutes = lazy(() =>
10081016
async (c) => {
10091017
const sessionID = c.req.valid("param").sessionID
10101018
const body = c.req.valid("json")
1011-
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext)
1019+
// altimate_change start — bind the client's trace to this turn's user message (see TraceContext);
1020+
// a command that fails before its loop is released
10121021
const messageID = body.messageID ?? MessageID.ascending()
10131022
TraceContext.bind(messageID, c.req.header("traceparent"))
1023+
const msg = await SessionPrompt.command({ ...body, sessionID, messageID }).catch((error) => {
1024+
TraceContext.release(sessionID, [messageID])
1025+
throw error
1026+
})
10141027
// altimate_change end
1015-
const msg = await SessionPrompt.command({ ...body, sessionID, messageID })
10161028
return c.json(msg)
10171029
},
10181030
)

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { Context, Effect, Layer } from "effect"
3232
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
3333
import { serviceUse } from "@opencode-ai/core/effect/service-use"
3434
import path from "node:path"
35+
import { TraceContext } from "@/altimate/observability/trace-context" // altimate_change — client trace per turn
3536
// altimate_change end
3637

3738
export namespace SessionCompaction {
@@ -1638,6 +1639,9 @@ When constructing the summary, try to stick to this template:
16381639
system: original.system,
16391640
variant: original.variant,
16401641
})
1642+
// altimate_change start — the replayed prompt continues the compacted turn (client trace)
1643+
TraceContext.inherit(replayMsg.id, input.parentID)
1644+
// altimate_change end
16411645
for (const part of replay.parts) {
16421646
if (part.type === "compaction") continue
16431647
const replayPart =
@@ -1683,6 +1687,8 @@ When constructing the summary, try to stick to this template:
16831687
system: latestField("system") ?? userMessage.system,
16841688
variant: latestField("variant") ?? userMessage.variant,
16851689
})
1690+
// The continue message carries the compacted turn on (client trace).
1691+
TraceContext.inherit(continueMsg.id, input.parentID)
16861692
// altimate_change end
16871693
// altimate_change start — deterministic corroborated-facts-only
16881694
// state ledger appended to the synthetic continue message (all-modes, compaction-gated).

‎packages/opencode/src/session/llm.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,14 @@ export namespace LLM {
321321
maxOutputTokens,
322322
// altimate_change end
323323
abortSignal: input.abort,
324-
// altimate_change start — send the canonical headers used by the budget estimator, bound to the current Altimate Base session
325-
// altimate_change start — forward the turn's trace to the Altimate gateways (see TraceContext)
324+
// altimate_change start — send the canonical headers used by the budget estimator, bound to the
325+
// current Altimate Base session, plus the turn's client trace for the Altimate gateways
326326
headers: withManagedSessionHeaders(
327327
input.model.providerID,
328328
input.sessionID,
329329
TraceContext.withHeaders(requestHeaders, trace, input.model.providerID, provider.options?.headers),
330330
),
331331
// altimate_change end
332-
// altimate_change end
333332
maxRetries: input.retries ?? 0,
334333
messages: [
335334
...system.map(

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,9 @@ export namespace SessionPrompt {
10641064
model: lastUser.model,
10651065
}
10661066
await Session.updateMessage(summaryUserMsg)
1067+
// altimate_change start — this loop-written message continues the step's turn (client trace)
1068+
TraceContext.inherit(summaryUserMsg.id, lastUser.id)
1069+
// altimate_change end
10671070
await Session.updatePart({
10681071
id: PartID.ascending(),
10691072
messageID: summaryUserMsg.id,
@@ -1884,6 +1887,8 @@ export namespace SessionPrompt {
18841887
agent: lastUser.agent,
18851888
model: lastUser.model,
18861889
} as MessageV2.Info)
1890+
// This loop-written message continues the step's turn (client trace).
1891+
TraceContext.inherit(syntheticMessageID, lastUser.id)
18871892

18881893
// Append the validator body as a text part on the new user message.
18891894
await Session.updatePart({

‎packages/opencode/test/altimate/trace-context.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ describe("TraceContext in telemetry envelopes", () => {
169169
Telemetry.track(sessionEnd("ses_untraced"))
170170
Telemetry.track({ type: "first_launch", timestamp: Date.now(), version: "x", is_upgrade: true } as any)
171171
})
172+
expect(envelopes.length).toBeGreaterThan(0)
172173
for (const envelope of envelopes) expect(envelope.tags["ai.operation.id"]).toBeUndefined()
173174
TraceContext.release("ses_traced", ["msg_tel_other"])
174175
})

‎packages/opencode/test/session/trace-context-loop.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,52 @@ it.live("each turn's gateway calls carry that turn's trace, released when the tu
152152

153153
// Turn C, traced differently: its own trace, not A's.
154154
const c = yield* turn("third traced turn", TRACE_B)
155+
expect(c.chat.length).toBeGreaterThan(0)
155156
for (const hit of c.chat) expect(traceOf(hit.headers)).toBe(TRACE_B)
156157
}),
157158
{ git: true, config: providerCfg },
158159
),
159160
{ timeout: 20_000 },
160161
)
161162

163+
it.live("a turn that overflows and auto-compacts keeps its trace on every call, before and after", () =>
164+
provideTmpdirServerLegacy(
165+
Effect.fnUntraced(function* ({ llm }) {
166+
const prompt = yield* SessionPrompt.Service
167+
const sessions = yield* Session.Service
168+
const session = yield* sessions.create({
169+
title: "trace compaction",
170+
permission: [{ permission: "*", pattern: "*", action: "allow" }],
171+
})
172+
const messageID = MessageID.ascending()
173+
TraceContext.bind(messageID, traceparent(TRACE_A))
174+
// Overflow -> compaction summary -> continuation: the loop writes the compaction marker and
175+
// the continue message itself; both must carry the turn on.
176+
yield* llm.error(413, { error: { message: "request entity too large" } })
177+
yield* llm.text("summary of the conversation so far")
178+
yield* llm.text("final answer after compaction")
179+
yield* prompt.prompt({
180+
sessionID: session.id,
181+
messageID,
182+
agent: "build",
183+
noReply: true,
184+
parts: [{ type: "text", text: "long turn that overflows" }],
185+
})
186+
yield* prompt.loop({ sessionID: session.id })
187+
188+
const messages = yield* sessions.messages({ sessionID: session.id })
189+
expect(messages.some((message) => message.parts.some((part) => part.type === "compaction"))).toBe(true)
190+
const hits = yield* llm.hits
191+
// The overflowing call, the summariser and the continuation (plus any title call).
192+
expect(hits.length).toBeGreaterThanOrEqual(3)
193+
for (const hit of hits) expect(traceOf(hit.headers)).toBe(TRACE_A)
194+
expect(TraceContext.activeTraceId(session.id)).toBeUndefined()
195+
}),
196+
{ git: true, config: providerCfg },
197+
),
198+
{ timeout: 30_000 },
199+
)
200+
162201
function hit<T>(hits: T[]): T {
163202
return hits[hits.length - 1]
164203
}

0 commit comments

Comments
 (0)