[Bug] Async memory compression state is shared across concurrent file reviews
OpenCodeReview Version
main @ 4ee453f (built from source)
Operating System
macOS (Apple Silicon)
Installation Method
Built from source
LLM Provider
Other OpenAI-compatible endpoint
Bug Description
When multiple files are reviewed concurrently, llmloop.Runner shares one asynchronous memory-compression job across all RunPerFile conversations.
The runner is session-scoped, but pendingJob is conversation-scoped state:
type Runner struct {
// ...
compressionMu sync.Mutex
pendingJob *compressionJob
}
addNextMessage calls tryApplyPendingCompression(messages) and cancelPendingCompression() without associating the job with the current file or conversation. With --concurrency > 1, a compression job started for file A can therefore be observed, applied, replaced, or canceled by file B.
There is also a same-conversation cancellation path: async compression can start when the pre-append token count exceeds the 60% soft limit, then the newly appended assistant/tool-result messages can push the conversation beyond the 80% warning limit in the same addNextMessage call, immediately canceling the request that was just started.
This produces context canceled errors for memory-compression requests. Some OpenAI-compatible gateways surface that client-side cancellation as HTTP 500, even though ordinary review requests continue to succeed. More importantly, applying a summary created from another file's message history can affect review correctness.
Steps to Reproduce
- Configure a review template with
MEMORY_COMPRESSION_TASKand a sufficiently smallMAX_TOKENSso the thresholds are reached predictably. - Start a review containing at least two files with
--concurrency 2or higher. - Let file A cross the 60% soft limit and keep its compression request in flight.
- Continue file B until it calls
tryApplyPendingCompressionor crosses the 80% warning limit. - Observe that the single
Runner.pendingJobcan be applied or canceled by file B, and the compression client receivescontext canceled.
A deterministic regression test can use a blocking fake LLM client:
- start compression for
a.go; - enter
addNextMessageforb.go; - assert that B neither cancels nor applies A's compression result.
A separate same-file case can start compression just above the soft limit and append enough messages to cross the warning limit; the current ordering starts and then immediately cancels the job.
Expected Behavior
Each RunPerFile conversation should own its compression state.
- A compression summary must only be applied to the message history from which it was created.
- One file must not cancel, replace, or consume another file's compression job.
- The threshold handling should not launch an async request that is immediately canceled in the same message update.
- Aggregate token counters and warnings may remain shared at the runner/session level.
Logs / Error Output
memory_compression_task: context canceled
POST [redacted OpenAI-compatible endpoint]: context canceled
In one anonymized run with concurrency 4, ordinary LLM requests continued to succeed while 4 of 12 memory-compression requests were canceled. One cancellation happened within a few milliseconds, consistent with a soft-limit start followed by warning-limit cancellation in the same update path.
Additional Context
Relevant code on current main:
- Runner stores one shared pending job
- addNextMessage applies/cancels the shared job
- async compression lifecycle
Suggested fix: move pending compression ownership into per-RunPerFile state, or key the job by a stable conversation/file identifier and validate ownership before applying or canceling it. Also decide whether to start async compression only after appending the new messages, so a job is not started and canceled in the same update.
A conservative alternative is synchronous compression at the threshold, trading some latency for a simpler lifecycle.
This is not a request to retry context canceled. Once the parent context is canceled, retrying the same request context cannot succeed; the issue is the ownership and lifecycle of the compression request.
I searched the existing issues and did not find a report for this shared-state race.
Thanks for the thorough bug report — the analysis is spot-on. We've verified that the shared pendingJob state on Runner does introduce a cross-conversation race when --concurrency > 1, and the same-file self-cancellation path you described is also real.
In practice the bug is most likely to surface with larger PRs or lower MAX_TOKENS settings, but the code path is clearly incorrect regardless of how often it triggers today.
Would you be interested in submitting a PR to fix this? The core change is moving compression ownership into per-RunPerFile scope (or keying by file/conversation). Happy to discuss the approach further if that would be helpful.
Taking this up per your invitation — thanks for confirming the analysis. Approach: move compression-job ownership into per-RunPerFile conversation state (a small compressionState{mu, pendingJob} created inside RunPerFile and threaded through addNextMessage/trigger/apply/cancel), keep aggregate token counters and warnings on Runner, and evaluate the soft-limit async trigger after the new messages are appended so a job is never started and canceled in the same update. Will include deterministic regression tests with a channel-gated fake client (cross-file isolation, apply-only-to-own-history, no start-then-cancel) plus a -race concurrency test. PR coming shortly.
PR is up: #395 — it implements the per-RunPerFile compression ownership you suggested (per-conversation compressionState threaded through trigger/apply/cancel, async trigger moved after the appends, deferred cancel at conversation end). Regression tests cover cross-file isolation, owner-only apply, the same-update start-then-cancel case, and a 4-goroutine -race run; an E2E against a local OpenAI-compatible stub showed zero context canceled compression failures at concurrency 2.