MCPcopy Create free account
hub / github.com/alibaba/open-code-review / triggerAsyncCompression

Method triggerAsyncCompression

internal/llmloop/compression.go:381–419  ·  view source on GitHub ↗

triggerAsyncCompression kicks off a background compression job for the conversation owning st. A no-op when a job is already pending — the check-and-set happens under st.mu so concurrent callers cannot replace (and thereby leak) an in-flight job.

(ctx context.Context, st *compressionState, messages []llm.Message, taskKey string)

Source from the content-addressed store, hash-verified

379// check-and-set happens under st.mu so concurrent callers cannot replace
380// (and thereby leak) an in-flight job.
381func (r *Runner) triggerAsyncCompression(ctx context.Context, st *compressionState, messages []llm.Message, taskKey string) {
382 st.mu.Lock()
383 if st.pendingJob != nil {
384 st.mu.Unlock()
385 return
386 }
387 msgSnapshot := copyMessages(messages)
388 asyncCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
389 job := &compressionJob{done: make(chan struct{}), cancel: cancel, snapshotLen: len(messages)}
390 st.pendingJob = job
391 st.mu.Unlock()
392
393 // Registered before the goroutine starts so WaitBackground can never miss
394 // a job that was launched but has not run yet.
395 r.bg.Go(func() {
396 defer cancel()
397 rebuilt, err := r.runCompression(asyncCtx, msgSnapshot, taskKey)
398
399 st.mu.Lock()
400 defer st.mu.Unlock()
401
402 if st.pendingJob != job {
403 return // cancelled or superseded
404 }
405 if err != nil {
406 // Still the owner, so this is a genuine failure rather than a
407 // deliberate cancel (cancelPendingCompression cancels and clears
408 // pendingJob under the lock, so cancelled jobs fail the ownership
409 // check above and die silently). Abandon the job rather than
410 // applying a truncated/unmodified snapshot over live messages.
411 fmt.Fprintf(stdout.Writer(), "[ocr] Memory compression failed: %v\n", r.progressError(err))
412 st.pendingJob = nil
413 close(job.done)
414 return
415 }
416 job.rebuilt = rebuilt
417 close(job.done)
418 })
419}
420
421// tryApplyPendingCompression checks whether a background compression has
422// completed and swaps the rebuilt messages into place. Returns true if

Calls 5

runCompressionMethod · 0.95
progressErrorMethod · 0.95
WriterFunction · 0.92
WithTimeoutMethod · 0.80
copyMessagesFunction · 0.70