Skip to content

Commit e4bfdc5

Browse files
committed
Preserve parent context in agent loop timeout
Use the caller-provided context when creating the agent loop inactivity timeout instead of oc.backgroundContext(ctx), so parent cancellations propagate to the derived context. Add TestWithAgentLoopInactivityTimeoutPreservesParentCancellation to verify the derived context cancels and reports context.Canceled when the parent is cancelled. Changes in bridges/ai/agent_loop_runtime.go and bridges/ai/agent_loop_test.go.
1 parent 9098d1b commit e4bfdc5

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

bridges/ai/agent_loop_runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func agentLoopInactivityCause(ctx context.Context) error {
8282
}
8383

8484
func (oc *AIClient) withAgentLoopInactivityTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
85-
runCtx, touch, cancel := withActivityTimeout(oc.backgroundContext(ctx), agentLoopInactivityTimeout, errAgentLoopInactivityTimeout)
85+
runCtx, touch, cancel := withActivityTimeout(ctx, agentLoopInactivityTimeout, errAgentLoopInactivityTimeout)
8686
return withAgentLoopActivity(runCtx, touch), cancel
8787
}
8888

bridges/ai/agent_loop_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,23 @@ func TestWithActivityTimeoutResetsOnTouchAndCancelsOnIdle(t *testing.T) {
157157
t.Fatalf("expected inactivity timeout cause, got %v", context.Cause(ctx))
158158
}
159159
}
160+
161+
func TestWithAgentLoopInactivityTimeoutPreservesParentCancellation(t *testing.T) {
162+
parent, parentCancel := context.WithCancel(context.Background())
163+
client := &AIClient{}
164+
165+
ctx, cancel := client.withAgentLoopInactivityTimeout(parent)
166+
defer cancel()
167+
168+
parentCancel()
169+
170+
select {
171+
case <-ctx.Done():
172+
case <-time.After(100 * time.Millisecond):
173+
t.Fatal("expected derived context to cancel when parent is cancelled")
174+
}
175+
176+
if !errors.Is(context.Cause(ctx), context.Canceled) {
177+
t.Fatalf("expected cancelled cause from parent, got %v", context.Cause(ctx))
178+
}
179+
}

0 commit comments

Comments
 (0)