Skip to content

Commit c09a486

Browse files
anandgupta42claude
andcommitted
fix: address cubic review on serve trace lifecycle (#886)
Follow-up to the consensus-review fixes, addressing cubic-dev-ai comments: - serve.ts: SIGINT/SIGTERM shutdown now exits with signal-conventional codes (130 / 143) instead of 0, so a signalled stop isn't masked as a successful run. beforeExit still exits 0. Matches cli/cmd/run.ts. - trace-consumer.ts (loadConfig): a config-load error no longer disables tracing — it keeps `enabled` true and falls back to Trace.create()'s default exporter (the original "must not prevent the host from working" intent), but now logs a warning so the fallback isn't silent. - trace-consumer.ts (getOrCreateTrace): drop the dead `Trace.setActive(trace)` call. It sets a process-global active trace that nothing reads and that is a footgun in multi-session serve (last-event-wins); per-session routing is via the sessionTraces map. Verified: marker guard, typecheck, and trace-consumer / tracing-followups / worker-trace-clearing / bridge-merge-invariants suites all green. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent a701fdf commit c09a486

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎packages/opencode/src/altimate/observability/trace-consumer.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ export class TraceConsumer {
9191
this.exporters = exporters
9292
this.maxFiles = tc?.maxFiles
9393
} catch (error) {
94-
// Config failure should not prevent the host (TUI/serve) from working
95-
this.enabled = false
96-
Log.Default.debug("[tracing] failed to load config, disabling", {
94+
// Config failure must not prevent the host (TUI/serve) from tracing:
95+
// leave `enabled` true and `exporters` undefined so getOrCreateTrace
96+
// falls back to Trace.create()'s default FileExporter. Warn so the
97+
// fallback isn't silent (the original concern was the lack of any signal,
98+
// not the fallback itself).
99+
Log.Default.warn("[tracing] failed to load config, using default tracer", {
97100
error: error instanceof Error ? error.message : String(error),
98101
})
99102
}
@@ -139,7 +142,11 @@ export class TraceConsumer {
139142
void trace.endTrace().catch(() => {})
140143
return this.sessionTraces.get(sessionID) ?? null
141144
}
142-
Trace.setActive(trace)
145+
// Intentionally NOT calling Trace.setActive() here: it sets a single
146+
// process-global active trace, which is meaningless (and a footgun) for a
147+
// multi-session consumer where serve runs many sessions concurrently —
148+
// whichever session's event arrived last would win. Per-session routing
149+
// is via the sessionTraces map; nothing reads Trace.active on this path.
143150
this.sessionTraces.set(sessionID, trace)
144151
return trace
145152
} catch (error) {

‎packages/opencode/src/cli/cmd/serve.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ export const ServeCommand = cmd({
3737
// left un-finalized (status never "completed", no summary/narrative).
3838
// Mirrors the SIGINT/SIGTERM/beforeExit pattern in cli/cmd/run.ts.
3939
let isShuttingDown = false
40-
const shutdown = async () => {
40+
const shutdown = async (code: number) => {
4141
if (isShuttingDown) return
4242
isShuttingDown = true
4343
await traceSub.stop()
4444
await server.stop()
45-
process.exit(0)
45+
process.exit(code)
4646
}
47-
process.once("SIGINT", () => void shutdown())
48-
process.once("SIGTERM", () => void shutdown())
49-
process.once("beforeExit", () => void shutdown())
47+
// Exit with signal-conventional codes (128 + signal number) so a
48+
// SIGINT/SIGTERM isn't masked as a successful (0) run. beforeExit is a
49+
// normal drain, so it exits 0. Matches cli/cmd/run.ts.
50+
process.once("SIGINT", () => void shutdown(130))
51+
process.once("SIGTERM", () => void shutdown(143))
52+
process.once("beforeExit", () => void shutdown(0))
5053
// altimate_change end
5154

5255
await new Promise(() => {})

0 commit comments

Comments
 (0)