open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

[Bug] Async memory compression state is shared across concurrent file reviews

Open peckfly opened this issue 4 months ago • 3 comments

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

  1. Configure a review template with MEMORY_COMPRESSION_TASK and a sufficiently small MAX_TOKENS so the thresholds are reached predictably.
  2. Start a review containing at least two files with --concurrency 2 or higher.
  3. Let file A cross the 60% soft limit and keep its compression request in flight.
  4. Continue file B until it calls tryApplyPendingCompression or crosses the 80% warning limit.
  5. Observe that the single Runner.pendingJob can be applied or canceled by file B, and the compression client receives context canceled.

A deterministic regression test can use a blocking fake LLM client:

  • start compression for a.go;
  • enter addNextMessage for b.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:

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.

peckfly avatar Jul 16 '26 13:07 peckfly

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.

lizhengfeng101 avatar Jul 17 '26 04:07 lizhengfeng101

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.

chethanuk avatar Jul 17 '26 09:07 chethanuk

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.

chethanuk avatar Jul 17 '26 10:07 chethanuk