@@ -1601,7 +1601,7 @@ func TestRun_PrepareMessagesInjectsSystemContextMidLoop(t *testing.T) {
16011601
16021602 // Simulate: after the tool executes (step 0), instruction
16031603 // becomes available. PrepareMessages injects it before step 1.
1604- var instructionInjected atomic. Bool
1604+ instructionInjected := make ( chan struct {})
16051605 var instructionAvailable atomic.Value
16061606 // The tool sets instruction after execution.
16071607 tool := fantasy .NewAgentTool (
@@ -1624,14 +1624,16 @@ func TestRun_PrepareMessagesInjectsSystemContextMidLoop(t *testing.T) {
16241624 return nil
16251625 },
16261626 PrepareMessages : func (msgs []fantasy.Message ) []fantasy.Message {
1627- if instructionInjected .Load () {
1627+ select {
1628+ case <- instructionInjected :
16281629 return nil
1630+ default :
16291631 }
16301632 instr , ok := instructionAvailable .Load ().(string )
16311633 if ! ok || instr == "" {
16321634 return nil
16331635 }
1634- instructionInjected . Store ( true )
1636+ close ( instructionInjected )
16351637 // Insert a system message after existing system messages.
16361638 result := make ([]fantasy.Message , 0 , len (msgs )+ 1 )
16371639 inserted := false
@@ -1748,7 +1750,7 @@ func TestRun_PrepareMessagesOnlyFiresOnce(t *testing.T) {
17481750 },
17491751 })
17501752 require .NoError (t , err )
1751- require .Equal (t , 3 , int ( streamCalls ) )
1753+ require .Equal (t , 3 , streamCalls )
17521754 // PrepareMessages is called before each of the 3 steps.
17531755 require .Equal (t , 3 , int (prepareCalls .Load ()))
17541756}
0 commit comments