Skip to content

feat(agents): stream the usage log instead of holding it whole - #290

Merged
artempartos merged 1 commit into
artempartos/container-side-log-uploadfrom
artempartos/stream-usage-logs
Sep 17, 2026
Merged

artempartos merged 1 commit into
artempartos/container-side-log-uploadfrom
artempartos/stream-usage-logs

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Stacked on #289. Base is that branch, so the diff here is only this change; it retargets to develop automatically when #289 merges.

Why, corrected

I said the fix for cursor_cli/grok/codex was to move #collect_usage out of the cleanup phase into a job. That was the wrong diagnosis, and measuring it says so plainly. Over the 30 days to 2026-09-17, for those three runtimes:

sessions                                       281
cleanup ran (some log collected)               277   (98.6%)
http.log collected                              52   (18.5%)
→ cleanup ran but http.log did not arrive      225
sessions that failed IN the cleanup phase        1   (30 days, every agent)

And the sizes of the ones that did arrive: p50 818 KB, largest ever 2.04 MB. Nothing bigger has ever survived.

So the phase was never the problem. The transfer was — a tar copy of the whole file through the API server, with a practical ceiling around 2 MB against a log that reaches 90 MB. A job would have moved work that was not failing for the reason it was moved.

What this does instead

#collect_usage took its log as a String, and that is the only reason the bytes had to be here at all.

  • Those logs now take the same direct upload as every other (feat(sessions): let the container upload its own logs, and drop the size cap #289), so #usage_log_paths — added one commit earlier to hold them back — goes away with the reason for it.
  • They come back as Sessions::LogSource, which reads the stored object a chunk at a time and yields lines.
  • Every parser was already written against each_line (GrokAdapter#extract_events_from_mitm, CursorCliAdapter#build_rpc_windows, CodexAdapter twice over). A String is still an each_line, so the small-log path is untouched and no adapter can tell which it got.

Memory, the transport ceiling and the phase budget all drop out together. A job stays available if wall-clock later argues for one — but that would be a different reason, measured.

Codex counted the log's lines in order to log that count, which is a third full pass over something that no longer fits. It reports the size alone.

Care taken in the streaming

  • Re-readable, because Codex makes two passes (OTLP metrics, then raw exchanges) and each reopens rather than holding the log between them.
  • A line split across two reads is reassembled, tested at exactly CHUNK_BYTES + 100.
  • Encoding is fixed per line, not per chunk, so a multi-byte character landing on a read boundary stays whole — tested with a Cyrillic character straddling it.
  • An empty source opens nothing.

Verification

test/services/agents, test/services/sessions, test/services/container_strategies, test/temporal: 838 runs, 2346 assertions, 0 failures, 0 errors. rubocop clean on the changed files, brakeman clean (0 warnings).

New coverage: LogSource against the cases above, plus the contract itself — GrokAdapter#collect_usage produces the same token breakdown from a LogSource as from the String the existing test feeds it. In the collector, an uploaded log now comes back as a stream, and the log an adapter parses is uploaded like any other.

🤖 Generated with Claude Code

`#collect_usage` took its log as a String, which is why `/var/log/mitm/http.log` had to
be copied out of the container and materialised here — and copying it is what failed.
Over the 30 days to 2026-09-17, cleanup ran for 277 of the 281 cursor_cli/grok/codex
sessions while only 52 produced an `http.log` row, the largest 2.04 MB; exactly one
session in that window failed inside the cleanup phase. The ceiling was the transfer,
not the time budget, so moving the work to a job would have fixed the wrong thing.

Those logs now take the same direct upload as every other, and come back as a
Sessions::LogSource that reads the stored object in chunks. Every parser was already
written as `log_content.each_line`, and a String is still one, so the small-log path is
untouched and no adapter can tell the difference. `#usage_log_paths`, added one commit
ago to keep these on the bounded read, is gone with the reason for it.

Codex counted the log's lines to log that count, which is a third full pass over
something that no longer fits; it reports the size alone now.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit 990474d into artempartos/container-side-log-upload Sep 17, 2026
7 checks passed
aixle-com Bot pushed a commit that referenced this pull request Nov 26, 2026
aixle-com Bot pushed a commit that referenced this pull request Nov 27, 2026
NOX73 added a commit that referenced this pull request Nov 27, 2026
* docs(tools): tech design for attached-only session tools

Signed-off-by: Alex <[email protected]>

* docs(tools): update tool attachment design with plumbing, migration and flag decisions

Signed-off-by: Alex <[email protected]>

* docs(tools): drop feature flag, one migration, templates pull request in tool-attachment design

Signed-off-by: Alex <[email protected]>

* docs(tools): update tool-attachment index entry

Signed-off-by: Alex <[email protected]>

* docs(tools): tool attachment design round 4 (#290)

Signed-off-by: Alex <[email protected]>

* docs(tools): tool attachment design round 5, tracker and repository reachability, no production ids

Signed-off-by: Alex <[email protected]>

---------

Signed-off-by: Alex <[email protected]>
Co-authored-by: Alex <[email protected]>
NOX73 added a commit that referenced this pull request Nov 27, 2026
…295) (#904)

* docs(integrations): tech design for integrations chosen per session (#295)

Signed-off-by: Alex <[email protected]>

* docs(integrations): update tech design for #295 after review round 1

Signed-off-by: Alex <[email protected]>

* Design: address review findings on session integrations (restore, Coder identity, tenant checks, backfill)

Signed-off-by: Alex <[email protected]>

* docs(integrations): design round 4 for #295: no Coder identity rules, #294 inside, #290 gates, suspended backfill

Signed-off-by: Alex <[email protected]>

* docs(integrations): fix models test line in session integrations design

Signed-off-by: Alex <[email protected]>

---------

Signed-off-by: Alex <[email protected]>
Co-authored-by: Alex <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant