-
-
Notifications
You must be signed in to change notification settings - Fork 150
fix(agent-runner): drain the parallelism queue when an ACP conversation ends #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package acpbridge | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "log/slog" | ||
| "testing" | ||
|
|
||
| "github.com/alicebob/miniredis/v2" | ||
| "github.com/google/uuid" | ||
| "github.com/redis/go-redis/v9" | ||
|
|
||
| "github.com/Paca-AI/agent-runner/internal/messaging" | ||
| ) | ||
|
|
||
| // An ACP conversation's terminal status has to land on | ||
| // StreamAgentConversationStatus — services/api's AgentQueueConsumer runs | ||
| // AdvanceQueue only on that stream, so without the entry a conversation queued | ||
| // behind the same agent is never started. | ||
| func TestPublishQueueStatus_AppendsToConversationStatusStream(t *testing.T) { | ||
| mr, err := miniredis.Run() | ||
| if err != nil { | ||
| t.Fatalf("miniredis.Run: %v", err) | ||
| } | ||
| t.Cleanup(mr.Close) | ||
| client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) | ||
| t.Cleanup(func() { client.Close() }) | ||
| d := &Dispatcher{ | ||
| Publisher: messaging.NewPublisher(client), | ||
| Log: slog.New(slog.NewTextHandler(io.Discard, nil)), | ||
| } | ||
|
|
||
| convID := uuid.New() | ||
| d.publishQueueStatus(context.Background(), convID, "failed") | ||
|
|
||
| entries, err := client.XRange(context.Background(), messaging.StreamAgentConversationStatus, "-", "+").Result() | ||
| if err != nil { | ||
| t.Fatalf("XRange: %v", err) | ||
| } | ||
| if len(entries) != 1 { | ||
| t.Fatalf("stream has %d entries, want 1", len(entries)) | ||
| } | ||
| if got := entries[0].Values["conversation_id"]; got != convID.String() { | ||
| t.Errorf("conversation_id = %v, want %s", got, convID) | ||
| } | ||
| if got := entries[0].Values["status"]; got != "failed" { | ||
| t.Errorf("status = %v, want failed", got) | ||
| } | ||
| } | ||
|
|
||
| // A publish failure is logged, never panics or blocks the caller: the DB | ||
| // status is already written and stays the source of truth. | ||
| func TestPublishQueueStatus_ToleratesUnreachableValkey(t *testing.T) { | ||
| mr, err := miniredis.Run() | ||
| if err != nil { | ||
| t.Fatalf("miniredis.Run: %v", err) | ||
| } | ||
| client := redis.NewClient(&redis.Options{Addr: mr.Addr(), MaxRetries: -1}) | ||
| t.Cleanup(func() { client.Close() }) | ||
| mr.Close() | ||
| d := &Dispatcher{ | ||
| Publisher: messaging.NewPublisher(client), | ||
| Log: slog.New(slog.NewTextHandler(io.Discard, nil)), | ||
| } | ||
| d.publishQueueStatus(context.Background(), uuid.New(), "finished") | ||
| } | ||
|
|
||
| func TestIsTerminalStatus(t *testing.T) { | ||
| for status, want := range map[string]bool{ | ||
| "finished": true, | ||
| "failed": true, | ||
| "stopped": true, | ||
| "paused": false, | ||
| "running": false, | ||
| "queued": false, | ||
| "": false, | ||
| } { | ||
| if got := isTerminalStatus(status); got != want { | ||
| t.Errorf("isTerminalStatus(%q) = %v, want %v", status, got, want) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,6 +353,18 @@ func (s *Server) handleTurnStatusMessage(ctx context.Context, agentID uuid.UUID, | |
| s.Log.Warn("acpbridge: failed to record turn_status", "conversation_id", convID, "error", err) | ||
| return | ||
| } | ||
| // A terminal turn_status must also reach StreamAgentConversationStatus, the | ||
| // same as handler.publishTerminalStatus does for sandboxed agents: it is the | ||
| // only event services/api's AgentQueueConsumer runs AdvanceQueue on. Without | ||
| // it, a conversation queued behind this agent stays "queued" forever once the | ||
| // running turn ends — only a StopConversation (which publishes on its own) | ||
| // would ever free it. Published before the realtime lookup below so a failure | ||
| // there can't swallow it. | ||
| if isTerminalStatus(statusStr) { | ||
| if err := s.Publisher.PublishConversationStatus(ctx, convID, statusStr); err != nil { | ||
| s.Log.Warn("acpbridge: failed to publish conversation status", "conversation_id", convID, "status", statusStr, "error", err) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests cover the shared helper ( |
||
|
|
||
| projectID, ownerUserID, err := s.ConvRepo.GetConversationRealtimeContext(ctx, convID) | ||
| if err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isTerminalStatusduplicatesservices/api'sagentdom.ConversationStatus.IsTerminalas a hard-coded string list (the same way the stream key strings are hand-synced between the services). Fine as-is, but the daemon drifts-closed coupling means a new terminal status anywhere in the pipeline needs this constant updated in lockstep — worth a one-line comment pointing atapps/acp-bridge'sreportStatuscall sites as the producer of these strings, or ideally a test listing the exact statuses the daemon emits. The currentTestIsTerminalStatustable already documents intent, so this is a nit.