fix(runtime): RunSync returns context.Canceled on aborted run

This commit is contained in:
sausheong committed 2026-06-13 15:33:02 +08:00
1 parent 3eb9596ee6
commit 04f02c56d6
2 files changed
+33

No files matched your search

+31
View File
@@ -896,6 +896,37 @@ func TestRun_AbortMidDispatchProducesPairedSession(t *testing.T) {
}
}
// TestRunSync_ReturnsErrorOnAbort verifies that RunSync surfaces an aborted run
// as a non-nil error (context.Canceled) rather than returning (partialText,
// nil). Cron/sync callers must be able to distinguish a completed run from a
// cancelled one. Reuses the same abort scaffolding as
// TestRun_AbortMidDispatchProducesPairedSession (a tool batch + cancelOnNth).
func TestRunSync_ReturnsErrorOnAbort(t *testing.T) {
threeToolCalls := []llm.ToolCall{
{ID: "tc_0", Name: "noop", Input: json.RawMessage(`{}`)},
{ID: "tc_1", Name: "noop", Input: json.RawMessage(`{}`)},
{ID: "tc_2", Name: "noop", Input: json.RawMessage(`{}`)},
}
llmFake := &threeToolCallLLM{toolCalls: threeToolCalls}
ctx, cancel := context.WithCancel(context.Background())
count := 0
exec := &cancelOnNthExecutor{n: 1, cancel: cancel, count: &count}
r := &Runtime{
LLM: llmFake,
Tools: exec,
Session: session.NewSession("a", "k"),
AgentID: "a",
Model: "test-model",
MaxTurns: 5,
}
_, err := r.RunSync(ctx, "go", nil)
require.Error(t, err)
require.ErrorIs(t, err, context.Canceled)
}
// threeToolCallLLM emits the configured tool_calls as one assistant turn,
// then EventDone. On the next call (after tool results, if the runtime
// loops back), emits only EventDone with no tool_calls so Run terminates.
+2
View File
@@ -849,6 +849,8 @@ func (r *Runtime) RunSync(ctx context.Context, userMsg string, images []llm.Imag
switch event.Type {
case EventTextDelta:
response.WriteString(event.Text)
case EventAborted:
return response.String(), context.Canceled
case EventError:
return response.String(), event.Error
}