Files
chenhg5-cc-connect/agent/opencode
cg33 a82b01aa27 fix(opencode): eliminate TempDir cleanup race in TestAvailableModels under -cover (#1118) (#1254)
Root cause: AvailableModels() with a warm persistent cache calls
startPersistentModelRefresh(), which spawns a background goroutine that
writes the refreshed model list into the test's TempDir.  When the test
function returns, t.TempDir() runs RemoveAll before that goroutine
finishes its last write — manifesting as:

  TempDir RemoveAll cleanup: unlinkat /tmp/.../001: directory not empty

The race window is too small to hit reliably without -cover; coverage
instrumentation slows the goroutine enough to make it reproducible.

Fix (two-part):
1. Add refreshWg sync.WaitGroup to Agent and track every background
   refresh goroutine with Add(1)/Done() in startPersistentModelRefresh.
   This is a pure bookkeeping addition with zero production behaviour
   change.
2. In TestAvailableModels_PrefersPersistentCacheOverDiscoveredModels,
   register t.Cleanup(func() { a.refreshWg.Wait() }) immediately after
   creating the agent (before AvailableModels is called). Cleanup
   functions run LIFO: our Wait fires before t.TempDir's RemoveAll,
   guaranteeing the goroutine has finished writing before the directory
   is cleaned up.

Verified: go test -count=1 -cover ./agent/opencode/... run 10 times
consecutively — 10/10 PASS, 0 failures.

Co-authored-by: root <root@UYQQVGRAEKQKNQP>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-07 21:18:36 +08:00
..