Commit ed1eecd
fix: write session traces in headless serve mode (#886)
* fix: write session traces in headless serve mode
Session tracing was implemented only at the terminal entrypoints — the TUI
worker and `run` construct the tracer and feed it events, but `serve` had no
trace wiring at all. Sessions driven over HTTP (e.g. the VS Code chat panel,
which spawns `altimate serve` and POSTs to `/session/{id}/prompt_async`)
never wrote `ses_*.json` files to `~/.local/share/altimate-code/traces/`:
the only hook in the shared session pipeline is `Tracer.active?.logSpan`,
and `Tracer.active` was never set in serve mode.
- Extract the TUI worker's inline event-stream→trace logic into a shared
`TraceConsumer` (`altimate/observability/trace-consumer.ts`): config
loading, per-session trace map, MAX_TRACES eviction, event feeding,
reset/flush
- Rewire `cli/cmd/tui/worker.ts` to use the shared consumer (behavior
unchanged)
- Add `subscribeTraceConsumer()` — an in-process `/event` subscription
mirroring the worker's loop — and start it in `ServeCommand`, so serve
sessions produce the same trace files as the terminal
- Tests: full event-sequence → completed trace file, interleaved sessions,
malformed events, disabled config, flush/reset semantics
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix: don't clobber trace files with post-idle straggler events
Real serve sessions emit trailing events after `session.status: idle` —
the user message is re-published with its generated `summary` once title
generation completes. Finalizing immediately on idle deleted the trace
from the map, and the straggler then lazily re-created an EMPTY trace
for the same sessionID whose finalization overwrote the rich
`<sessionId>.json` file (observed end-to-end in docker code-server:
42KB trace clobbered down to 749 bytes).
Fix: schedule finalization 2s after idle instead of finalizing inline;
any event for the session during the grace window pushes the deadline
back and is absorbed into the live trace. As a bonus the generated
session title now lands in trace metadata. flush()/reset()/eviction
clear pending timers.
Verified: chat session in docker code-server (glob tool call + 2
generations) produces a complete 42KB trace with title, model, tool
span, and token totals.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* test: update event-literal invariant for trace-consumer extraction
The worker's inline event handling moved into the shared
altimate/observability/trace-consumer.ts — point the cross-module
event-type literal invariant at the new consumer (and cover the full
set it consumes) instead of cli/cmd/tui/worker.ts.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* chore: wrap startEventStream trace reset in altimate_change markers
worker.ts is an upstream-shared file; the traceConsumer.reset() call (which
replaced main's inline sessionTraces clear) must be wrapped so the upstream
merge tooling protects it. Caught by script/upstream/analyze.ts --markers
--strict (the Marker Guard CI check).
* chore: wrap traceConsumer.loadConfig call in altimate_change markers
* fix: finalize serve traces on shutdown + harden trace consumer (#886)
Addresses the multi-model consensus review on PR #886. The `TraceConsumer`
extraction was sound, but the `serve` integration had lifecycle gaps.
`cli/cmd/serve.ts`:
- Capture the `subscribeTraceConsumer` handle and wire `SIGINT`/`SIGTERM`/
`beforeExit` to `stop()` so traces are finalized on shutdown instead of left
un-`completed`. Mirrors the existing pattern in `cli/cmd/run.ts`.
`altimate/observability/trace-consumer.ts`:
- `stop()` now drains the event loop (bounded by a 1s timeout) before
`flush()`, so finalization can't race an in-flight `handleEvent`.
- The `for await` stream loop catches mid-stream errors and reconnects with
exponential backoff (250ms -> 30s) instead of dying permanently; backoff
sleeps are abortable so shutdown isn't delayed.
- Finalize and evict per-session state on `session.deleted` (a real
end-of-session signal) so long-lived serve processes don't accumulate
`sessionUserMsgIds` / cached traces.
- `loadConfig` failure now fails safe (`enabled = false`) instead of silently
tracing to the default dir, and logs at debug.
- `getOrCreateTrace` and `handleEvent` catch blocks log at debug (still never
throw) for headless diagnosability.
- Auth header uses `Buffer.from(...).toString("base64")` (UTF-8 safe) instead
of `btoa`.
- Added a test seam: `subscribeTraceConsumer` accepts an optional injected
`consumer` and `subscribe` source (production behaviour unchanged via
defaults).
Tests:
- New `subscribeTraceConsumer` coverage: `stop()` drains + finalizes; a
mid-stream throw reconnects rather than killing the loop.
- New `TraceConsumer` coverage: incremental snapshots land on disk BEFORE any
flush (the real serve path), and `session.deleted` finalizes + releases
state.
- De-flaked the `reset()` test (poll instead of fixed `setTimeout`).
- Registered the `session.deleted` literal in `bridge-merge-invariants`.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* 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) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: anandgupta42 <anand@altimate.ai>1 parent 5ffeadb commit ed1eecd
7 files changed
Lines changed: 946 additions & 282 deletions
File tree
- packages/opencode
- src
- altimate/observability
- cli/cmd
- tui
- test
- altimate
- cli/tui
- upstream
Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
| |||
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
23 | 55 | | |
24 | | - | |
25 | 56 | | |
26 | 57 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
143 | 56 | | |
144 | 57 | | |
145 | 58 | | |
146 | 59 | | |
147 | | - | |
148 | | - | |
149 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
150 | 65 | | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | 66 | | |
158 | 67 | | |
159 | 68 | | |
| |||
175 | 84 | | |
176 | 85 | | |
177 | 86 | | |
178 | | - | |
179 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
180 | 90 | | |
181 | 91 | | |
182 | 92 | | |
| |||
194 | 104 | | |
195 | 105 | | |
196 | 106 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
| 107 | + | |
326 | 108 | | |
327 | 109 | | |
328 | 110 | | |
| |||
404 | 186 | | |
405 | 187 | | |
406 | 188 | | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
| 189 | + | |
412 | 190 | | |
413 | 191 | | |
414 | 192 | | |
| |||
0 commit comments