Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

test(daemon): cover cancelled classification in executeAndDrain (#1692)

Bohan Jiang95912243bbd3
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.go
index eb54f17e0e..9e8ce1093f 100644
--- a/server/internal/daemon/daemon_test.go
+++ b/server/internal/daemon/daemon_test.go
@@ -421,6 +421,34 @@ func TestExecuteAndDrain_NoRetryWhenSessionEstablished(t *testing.T) {
}
}
+// blockingBackend returns a Session whose Result channel is never written to,
+// so executeAndDrain can only exit via the drainCtx.Done() path.
+type blockingBackend struct{}
+
+func (blockingBackend) Execute(_ context.Context, _ string, _ agent.ExecOptions) (*agent.Session, error) {
+ msgCh := make(chan agent.Message)
+ resCh := make(chan agent.Result)
+ close(msgCh)
+ return &agent.Session{Messages: msgCh, Result: resCh}, nil
+}
+
+func TestExecuteAndDrain_ContextCancelled_ReportsCancelled(t *testing.T) {
+ t.Parallel()
+
+ d := newTestDaemon(t)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ result, _, err := d.executeAndDrain(ctx, blockingBackend{}, "p", agent.ExecOptions{}, slog.Default(), "t")
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if result.Status != "cancelled" {
+ t.Fatalf("expected status=cancelled when parent ctx is cancelled, got %q (err=%q)", result.Status, result.Error)
+ }
+}
+
func TestEnsureRepoReadyFastPathDoesNotRefresh(t *testing.T) {
t.Parallel()
diff --git a/server/pkg/agent/agent.go b/server/pkg/agent/agent.go
index f6f2dce835..842e616f22 100644
--- a/server/pkg/agent/agent.go
+++ b/server/pkg/agent/agent.go
@@ -77,7 +77,7 @@ type TokenUsage struct {
// Result is the final outcome after an agent session completes.
type Result struct {
- Status string // "completed", "failed", "aborted", "timeout"
+ Status string // "completed", "failed", "aborted", "timeout", "cancelled"
Output string // accumulated text output
Error string // error message if failed
DurationMs int64