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

Method triggerAsyncCompression

internal/llmloop/compression.go:277–313  ·  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, filePath string)

Source from the content-addressed store, hash-verified

275// check-and-set happens under st.mu so concurrent callers cannot replace
276// (and thereby leak) an in-flight job.
277func (r *Runner) triggerAsyncCompression(ctx context.Context, st *compressionState, messages []llm.Message, filePath string) {
278 st.mu.Lock()
279 if st.pendingJob != nil {
280 st.mu.Unlock()
281 return
282 }
283 msgSnapshot := copyMessages(messages)
284 asyncCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Minute)
285 job := &compressionJob{done: make(chan struct{}), cancel: cancel, snapshotLen: len(messages)}
286 st.pendingJob = job
287 st.mu.Unlock()
288
289 go func() {
290 defer cancel()
291 rebuilt, err := r.runCompression(asyncCtx, msgSnapshot, filePath)
292
293 st.mu.Lock()
294 defer st.mu.Unlock()
295
296 if st.pendingJob != job {
297 return // cancelled or superseded
298 }
299 if err != nil {
300 // Still the owner, so this is a genuine failure rather than a
301 // deliberate cancel (cancelPendingCompression cancels and clears
302 // pendingJob under the lock, so cancelled jobs fail the ownership
303 // check above and die silently). Abandon the job rather than
304 // applying a truncated/unmodified snapshot over live messages.
305 fmt.Fprintf(stdout.Writer(), "[ocr] Memory compression failed: %v\n", err)
306 st.pendingJob = nil
307 close(job.done)
308 return
309 }
310 job.rebuilt = rebuilt
311 close(job.done)
312 }()
313}
314
315// tryApplyPendingCompression checks whether a background compression has
316// completed and swaps the rebuilt messages into place. Returns true if

Callers 3

addNextMessageMethod · 0.95

Calls 3

runCompressionMethod · 0.95
WriterFunction · 0.92
copyMessagesFunction · 0.70

Tested by 2