From 19755e1d47aa52cd7ebe868ac5c765250389860e Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Tue, 17 Nov 2026 12:54:34 +0000 Subject: [PATCH] Skip model progress after caller cancellation --- docs/roadmap/remediation.md | 4 +++ internal/framework/llm/adapter.go | 15 +++++------ internal/framework/llm/adapter_test.go | 35 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 5edb5ab..811f254 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -206,6 +206,10 @@ including coordinated repair turns. An integration regression delays stderr beyond the model deadline and verifies the same provider calls and correction. Live progress, reporting modes, and presentation failure tests also passed. +The follow-up checks the parent context before each progress notification. +Adapter tests cover calls canceled before progress starts and calls canceled +during the start notification. The CLI progress and cancellation tests passed. + **Affected plan:** Stages 12 and 19. **Evidence:** [Pipeline wiring](../../internal/cli/process_pipeline.go#L30) diff --git a/internal/framework/llm/adapter.go b/internal/framework/llm/adapter.go index b86fe3e..6a9b702 100644 --- a/internal/framework/llm/adapter.go +++ b/internal/framework/llm/adapter.go @@ -22,8 +22,8 @@ type CallHooks struct { OnCall func(structuredoutput.CallRecord) OnEvent pk.ExecutionObserver OnEventWithCall func(uint64, structuredoutput.DiagnosticContext, pk.ExecutionEvent) - // OnProgress runs before and after the engine's timed operation. A nil - // count marks the start. A completion may have unknown execution counts. + // OnProgress runs outside the engine's timed operation while the caller + // context is active. A nil count marks the start. OnProgress func(structuredoutput.DiagnosticContext, *outcomes.ExecutionCounts, error) } @@ -71,9 +71,12 @@ func (c *RoleClient) prepareCall(parent context.Context, req structuredoutput.Re var record structuredoutput.CallRecord progressContext := req.Context progressContext.Role = c.role - if progress := c.hooks.OnProgress; progress != nil { - progress(progressContext, nil, nil) + emitProgress := func(counts *outcomes.ExecutionCounts, err error) { + if progress := c.hooks.OnProgress; progress != nil && parent.Err() == nil { + progress(progressContext, counts, err) + } } + emitProgress(nil, nil) if c.hooks.OnCall != nil || c.hooks.OnEventWithCall != nil { record.CallID = c.runtime.nextCallID.Add(1) record.RequestedPromptID = req.PromptID @@ -123,9 +126,7 @@ func (c *RoleClient) prepareCall(parent context.Context, req structuredoutput.Re } onCall(record) } - if progress := c.hooks.OnProgress; progress != nil { - progress(progressContext, &response.Execution, err) - } + emitProgress(&response.Execution, err) return response, err }} fail := func(err error) (*pendingCall, error) { diff --git a/internal/framework/llm/adapter_test.go b/internal/framework/llm/adapter_test.go index a5b5609..b13d39c 100644 --- a/internal/framework/llm/adapter_test.go +++ b/internal/framework/llm/adapter_test.go @@ -29,6 +29,41 @@ type reply struct { content string } +func TestCanceledCallSkipsProgress(t *testing.T) { + for _, tc := range []struct { + name string + cancelBefore bool + wantStarts int + }{ + {name: "canceled before start", cancelBefore: true}, + {name: "canceled during start", wantStarts: 1}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if tc.cancelBefore { + cancel() + } + starts, finishes := 0, 0 + client := RoleClient{hooks: CallHooks{ + OnProgress: func(_ structuredoutput.DiagnosticContext, counts *outcomes.ExecutionCounts, _ error) { + if counts == nil { + starts++ + cancel() + } else { + finishes++ + } + }, + }} + var result map[string]any + _, err := client.CompleteStructured(ctx, structuredoutput.Request{}, &result) + if !errors.Is(err, context.Canceled) || starts != tc.wantStarts || finishes != 0 { + t.Fatalf("progress starts = %d, finishes = %d, error = %v", starts, finishes, err) + } + }) + } +} + func adapterFixture(t *testing.T, replies []reply, retries, repairs int, hook func(*http.Request)) (*Runtime, *atomic.Int32, string) { return adapterFixtureWithPrompt(t, replies, retries, repairs, hook, "") }