Skip to content

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

Merged
artempartos merged 1 commit into
developfrom
artempartos/stream-usage-logs-on-develop
Sep 17, 2026
Merged

artempartos merged 1 commit into
developfrom
artempartos/stream-usage-logs-on-develop

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Why this exists twice

#290 was opened with artempartos/container-side-log-upload as its base, so that is where it merged — into #289's branch, after #289 had already gone into develop. It reports MERGED and its content never reached develop:

$ git cat-file -e origin/develop:app/services/sessions/log_source.rb   → ABSENT
$ git grep -c usage_log_paths origin/develop -- app/services/agents/base_adapter.rb   → 1

Same commit, cherry-picked onto develop, no changes. My mistake in stacking it.

What it does

#collect_usage took its log as a String, and that is the only reason the bytes had to be in this process at all. Measuring the failure said the phase was never the problem — over the 30 days to 2026-09-17, for cursor_cli/grok/codex:

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)
sizes that survived: p50 818 KB, largest 2.04 MB

The ceiling was the transfer — a tar copy of the whole file through the API server — against a log that reaches 90 MB.

So those logs take the same direct upload as every other (#289), #usage_log_paths goes away with the reason for it, and they come back as Sessions::LogSource, which reads the stored object a chunk at a time. Every parser was already written against each_line, and a String is still one, so the small-log path is untouched and no adapter can tell which it got.

Codex counted the log's lines in order to log that count — 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).
  • A line split across two reads is reassembled, tested at exactly CHUNK_BYTES + 100.
  • Encoding fixed per line, not per chunk, so a multi-byte character on a read boundary stays whole — tested with a Cyrillic character straddling it.
  • An empty source opens nothing.

Verification

Re-run against current develop after the cherry-pick, not just on the original branch: test/services/agents, test/services/sessions, test/services/container_strategies, test/temporal — 843 runs, 2360 assertions, 0 failures, 0 errors. rubocop and brakeman were clean on the same tree before the replant and nothing changed in it.

Coverage includes the contract itself: GrokAdapter#collect_usage produces the same token breakdown from a LogSource as from the String its existing test feeds it.

🤖 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 e548a3b into develop Sep 17, 2026
7 checks passed
aixle-com Bot pushed a commit that referenced this pull request Nov 30, 2026
…nregisters itself (#291) (#918)

* Runners: remove a machine's workspace and cache volumes when it unregisters itself

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

* Runners: build the keyless environment of the test without copying a lock

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

* fix(runner): say when unregister cannot reach Docker and leaves the volumes

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

* test(runner): use a DOCKER_HOST the Docker client refuses

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

---------

Signed-off-by: Alex <[email protected]>
Signed-off-by: Aixle Flow <[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