Files
multica/server/internal/handler/task_supplement_test.go
e2f4a22030 feat: steer a running agent from the normal composer, per recipient (MUL-7631) (#8760)
* feat(server): steer running turns from ordinary comments (MUL-7631)

A comment can now name recipients whose running turn should receive it
(`steer_agent_ids` on POST /issues/:id/comments). Each named recipient
with a running, steer-capable turn gets the comment bound to that turn
instead of a follow-up run; any other recipient keeps its normal
queued / coalesced / deferred trigger, so losing a race never drops input.

- One comment can steer several turns: receipts are keyed by
  (comment_id, task_id) and returned as `supplements` on comments and
  timeline entries. The single supplement_* fields mirror the first
  receipt for older clients.
- Completion reconciliation skips a steered comment only for the agent
  it steered, so the same comment still reaches recipients that were
  addressed without steering.
- Delivery claims match the (comment, run) pair.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(issues): choose per recipient how a message reaches a running agent (MUL-7631)

Steering moves from a separate "Add message" box on the run row into the
normal reply and comment composers. Recipients keep the existing trigger
rules; each recipient's current run on the issue decides what the message
can do:

- running (and steer-capable): add to the current run (default in that
  run's own thread), start after this run, stop and start over, or skip
- queued: include when it starts, or skip
- idle: start on send, or skip

Receipts follow the message ("Waiting for Lambda to read it" → "Read by
Lambda · after step N"), a failed delivery offers retry or sending it as
a new run, and the run's step list marks where it read the message.
A draft whose recipient finished meanwhile stays in place and says it
will start a new run; a message with files is handled after the run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(server): steer only the chosen turn, and make a retried steer idempotent (MUL-7631)

- Steering names the exact running turns the author chose
  (`steer_task_ids`). A chosen turn that ended before the send arrives is
  never swapped for a later turn of the same agent; that recipient keeps
  its normal trigger.
- A steering send carries `client_request_id`. A retry after a lost
  response returns the original comment (200) instead of creating a second
  comment and a second delivery; a concurrent twin binding the same request
  is treated as already steered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): guard steering sends against stale previews and broad resends (MUL-7631)

- "Stop and start over" never stops a run while the trigger preview is
  still catching up with an edited @mention: the stop is dropped unless the
  preview answers the submitted text.
- Steering sends the chosen turns' task ids and one request id per draft
  text, reused on retries of the same text.
- "Send as a new run" on a failed receipt reaches only that receipt's
  agent: every other current recipient is suppressed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): keep the reply box placeholder neutral (MUL-7631)

The empty reply box cannot know who a reply goes to: the author may
@ someone else or only talk to a teammate. The recipient chip already
says what Send will do once there is text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* test(issues): give steering composer tests room for the preview debounce (MUL-7631)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(issues): choose the default reply to a running agent (MUL-7631)

Preferences > Comments gains "When replying to a running agent":
"Add to current run" (default) or "Start after this run". It only
changes what a reply does without a per-message choice; the recipient
chip still offers both, and replies that never steered by default
(another thread, an idle or queued agent) are unaffected. Saved on this
device next to the sticky comment bar.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(server): save one comment per send and honor "don't start" on replay (MUL-7631)

Idempotency now lives on the comment, not the delivery receipt. A send's
client_request_id is stored on the comment behind a partial unique index
(migrations 550/551), so a retry, or a concurrent twin, returns the saved
comment (200) whether the send steered a running turn or fell back to a
normal trigger. Before, a twin could save a second comment, and a send whose
turn had ended kept no receipt to find.

The agents an author chose not to start are stored on the comment too, and
completion replay skips them. "Send as a new run" for one missed recipient,
and "Won't start this time", no longer wake the other agent once its current
run completes. An edit re-records the choice.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): repeat the original steering request on retry (MUL-7631)

Until a send is settled, retrying the same text sends its first steering
request unchanged, even after the chosen turn ended. The server then returns
the comment the first attempt saved instead of posting it again as new input.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(migrations): fail fast on locks in 549 and 550 (MUL-7631)

Both take an ACCESS EXCLUSIVE lock on a table the product touches
continuously: comment for every read and write, task_supplement for every
daemon claim. Bound lock acquisition and execution, as 483 does for issue,
so a long transaction makes the migration fail and retry on the next run
instead of queueing all traffic behind it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(server): steer at most one turn per comment during rollout (MUL-7631)

Steering is live, and servers from before per-run receipts claim every
receipt of a comment at once. While they still serve daemons during a
rollout, a comment bound to two turns could have the second copy marked in
flight and never delivered. Until every server claims per (comment, run),
a comment steers the first chosen turn only; later recipients keep their
normal trigger. A follow-up lifts the limit after this ships.

Also covers a steer_task_ids entry that names another issue's turn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(server): finish a saved send on retry; keep an edit's choices with its text (MUL-7631)

A send's comment was saved before its agents were started or steered, and
deduplication treated the saved comment as the whole send. A dropped
connection cancelled the request between the two, and the retry only
returned the comment, so no agent ever received it.

- Attempts of one logical send now run one at a time under a transaction
  advisory lock, held on its own connection: a twin waits for the attempt in
  progress (up to 30s, then 409) instead of racing it, and a process that
  dies releases it.
- After the comment is saved, dispatch runs on a non-cancelable context, and
  comment.client_request_dispatched_at (migration 552) records that it
  finished. A retry that finds a saved but undispatched comment finishes the
  dispatch with the comment's recorded choices.

An edit's "don't start" choices were written by a separate statement after
the text, so interleaved edits could leave an older edit's choice. They are
now part of UpdateComment, under the same revision check as the text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): offer one steered recipient per message during rollout (MUL-7631)

The server steers at most one turn per comment until #8800, but the
composer showed "Add to current run" for every running recipient and the
server quietly queued the rest. Now only one recipient steers: the one the
author picked, otherwise the first that would by default. The others show
"Start after this run", and picking "Add to current run" on one of them
moves the steer there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(server): claim a send before dispatching it; drop the request lock (MUL-7631)

The previous recovery marked a send dispatched only after reaching its
agents, so a failure after dispatch made a retry reach them all again. It
also held a pooled connection per send for an advisory lock while the send
needed more from the same pool, which could wait on itself until timeout.

A send's comment now holds a dispatch claim instead
(comment.client_request_dispatched_at, one conditional update): only the
attempt whose claim takes effect publishes the comment and reaches its
agents. A failed claim writes nothing else and returns 500 for the client
to retry; a retry that finds an unclaimed comment claims and dispatches
it; a claimed one is only returned. Concurrent attempts no longer wait on
each other, and post-save dispatch keeps a 30s bound of its own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(server): let one comment steer several running turns (MUL-7631)

Lifts the rollout limit from #8760. Every server now claims receipts per
(comment, run), so a comment can be bound to each running turn its author
chose, and each recipient receives it in its own run.

Merge only after #8760 is deployed everywhere.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* feat(issues): steer every running recipient a message addresses (MUL-7631)

The client side of lifting the rollout limit: each running recipient can
take the message in its current turn again, instead of only one per
message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* refactor(issues): drop send-retry idempotency and two display extras (MUL-7631)

A steering send now behaves like any other comment when its response is
lost: a retry posts it again. The machinery that tried to make that retry
return the saved comment grew with every edge it met, and ordinary comments
never had it:

- Remove comment.client_request_id, its unique index, and the dispatch
  claim (migrations 551/552, and 550 reduced to suppressed_agent_ids).
- Remove the replay/resume path in CreateComment, the post-save
  non-cancelable dispatch context, and the client's sticky request id.
  Steering sends only `steer_task_ids`; each binding uses the comment id
  as its receipt request id.

Two display extras go too, since the UI already shows the same thing:

- The "run ended while you were writing" notice: the recipient chip
  already switches from "Add to current run" to its new action.
- "after step N" on a read receipt: the run trace already places the
  message at the step where it was read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): space agent names in Chinese copy; neutral recipients header (MUL-7631)

- The steer badge joined names with Intl.ListFormat, which gives
  "Lambda和Orion" in Chinese. Latin names are now spaced from the Chinese
  joiner ("Lambda 和 Orion"), per the zh copy conventions; the enumeration
  comma stays unspaced.
- The multi-recipient popover was still titled "Will start when sent" and
  its tooltip said "choose who runs", though each row now picks how that
  agent handles the message. Title it "Recipients" and reword the tooltip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

* fix(issues): translate the recipient source labels in Chinese (MUL-7631)

"assignee" and "@mention" were left in English in zh-Hans; the recipient
menu header shows them. Use 负责人 (per the zh conventions) and @提及.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-09-25 04:42:26 +08:00

615 lines
27 KiB
Go

package handler
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/testutil"
db "github.com/multica-ai/multica/server/pkg/db/generated"
"github.com/multica-ai/multica/server/pkg/protocol"
)
type supplementFixture struct {
runtimeID string
agentID string
issueID string
taskID string
triggerID string
}
func TestStableTaskSupplementFailureReason(t *testing.T) {
for _, reason := range []string{
protocol.TaskSupplementFailureTurnNotStarted,
protocol.TaskSupplementFailureProviderRejected,
protocol.TaskSupplementFailureTimeout,
protocol.TaskSupplementFailureTurnEnded,
} {
if got := stableTaskSupplementFailureReason(reason); got != reason {
t.Fatalf("stableTaskSupplementFailureReason(%q) = %q", reason, got)
}
}
for _, raw := range []string{"", "供应商错误:无法发送", strings.Repeat("界", 600)} {
if got := stableTaskSupplementFailureReason(raw); got != protocol.TaskSupplementFailureProviderRejected {
t.Fatalf("raw reason %q escaped as %q", raw, got)
}
}
}
func TestTaskSupplementSettlementHasNoDatabaseTrigger(t *testing.T) {
if testPool == nil {
t.Skip("database not available")
}
var exists bool
if err := testPool.QueryRow(t.Context(), `
SELECT EXISTS (
SELECT 1
FROM pg_trigger
WHERE tgname = 'trg_settle_terminal_task_supplements'
AND NOT tgisinternal
)
`).Scan(&exists); err != nil {
t.Fatal(err)
}
if exists {
t.Fatal("task supplement settlement still depends on a database trigger")
}
}
func TestTaskSupplementClaimStartReplay(t *testing.T) {
f := newSupplementFixture(t, "codex", "dispatched", false)
dbfx.Cleanup(t, `DELETE FROM task_supplement_capability WHERE task_id=$1`, f.taskID)
generation := time.Date(2026, 9, 23, 0, 0, 0, 0, time.UTC)
dbfx.Exec(t, `UPDATE agent_task_queue SET dispatched_at=$2 WHERE id=$1`, f.taskID, generation)
for range 2 {
req := withURLParam(newDaemonTokenRequest(http.MethodPost, "/start", map[string]any{
"runtime_id": f.runtimeID, "dispatched_at": generation.Format(time.RFC3339Nano),
"capabilities": []string{protocol.DaemonCapabilityTaskSupplementV1},
}, testWorkspaceID, "start-claim-test"), "taskId", f.taskID)
var started AgentTaskResponse
testutil.Call(t, testHandler.StartTask, req).Want(http.StatusOK).JSON(&started)
if started.SupplementCapability != protocol.DaemonCapabilityTaskSupplementV1 {
t.Fatalf("lost negotiated capability on start replay: %+v", started)
}
}
if n := dbfx.Count(t, `SELECT count(*) FROM task_supplement_capability WHERE task_id=$1`, f.taskID); n != 1 {
t.Fatalf("start replay wrote %d capabilities", n)
}
}
func TestTaskSupplementLateDeliveryAndDeletion(t *testing.T) {
f := newSupplementFixture(t, "claude", "running", true)
var sent, pending CommentResponse
supplementRequest(t, f, "0199a4e8-22ce-7b01-bba5-111111111111", "already injected").Want(http.StatusCreated).JSON(&sent)
if _, err := testHandler.Queries.ClaimNextTaskSupplement(t.Context(), parseUUID(f.taskID)); err != nil {
t.Fatal(err)
}
supplementRequest(t, f, "0199a4e8-22ce-7b01-bba5-222222222222", "never injected").Want(http.StatusCreated).JSON(&pending)
deleteRequest := func() *http.Request {
return withURLParam(newRequest(http.MethodDelete, "/comments/"+sent.ID, nil), "commentId", sent.ID)
}
testutil.Call(t, testHandler.DeleteComment, deleteRequest()).Want(http.StatusConflict)
if w := completeTaskViaHandler(t, f.taskID, "done"); w.Code != http.StatusOK {
t.Fatalf("complete task: status %d: %s", w.Code, w.Body.String())
}
for _, c := range []CommentResponse{sent, pending} {
req := withURLParams(newDaemonTokenRequest(http.MethodPost, "/ack", map[string]any{"delivered": true}, testWorkspaceID, "legit-daemon"), "taskId", f.taskID, "commentId", c.ID)
want := http.StatusOK
if c.ID == pending.ID {
want = http.StatusConflict
}
testutil.Call(t, testHandler.AckTaskSupplement, req).Want(want)
}
testutil.Call(t, testHandler.DeleteComment, deleteRequest()).Want(http.StatusNoContent)
if n := dbfx.Count(t, `SELECT count(*) FROM task_supplement WHERE comment_id=$1`, sent.ID); n != 0 {
t.Fatal("deleted comment retained its receipt")
}
}
func TestTaskSupplementSubmissionBoundsAndConflicts(t *testing.T) {
f := newSupplementFixture(t, "codex", "running", true)
const requestID = "0199a4e8-22ce-7b01-bba5-111111111111"
supplementRequest(t, f, requestID, strings.Repeat("x", maxCommentContentBytes+1)).Want(http.StatusBadRequest)
if n := dbfx.Count(t, `SELECT count(*) FROM task_supplement WHERE task_id=$1`, f.taskID); n != 0 {
t.Fatal("oversized input was persisted")
}
supplementRequest(t, f, requestID, "original").Want(http.StatusCreated)
// Another author's request key is unique within the task but invisible to
// the current author's idempotency lookup. This is a conflict, not a 500.
dbfx.Exec(t, `UPDATE task_supplement SET author_id=$2 WHERE task_id=$1`, f.taskID, "0199a4e8-22ce-7b01-bba5-333333333333")
supplementRequest(t, f, requestID, "collision").Want(http.StatusConflict)
if n := dbfx.Count(t, `SELECT count(*) FROM comment WHERE issue_id=$1 AND content='collision'`, f.issueID); n != 0 {
t.Fatal("conflict left an orphan comment")
}
}
func newSupplementFixture(t *testing.T, provider, status string, negotiated bool) supplementFixture {
t.Helper()
if testHandler == nil {
t.Skip("database not available")
}
runtimeID := dbfx.Runtime(t, "supplement-"+provider, testutil.Cols{"provider": provider})
agentID := dbfx.Agent(t, "Supplement "+provider, runtimeID)
issueID := dbfx.Issue(t, "supplement "+provider)
triggerID := dbfx.Comment(t, issueID, "original objective")
taskID := dbfx.Task(t, agentID, testutil.Cols{
"issue_id": issueID,
"runtime_id": runtimeID,
"trigger_comment_id": triggerID,
"status": status,
"started_at": testutil.Raw("CASE WHEN '" + status + "' = 'running' THEN now() ELSE NULL END"),
})
if negotiated {
dbfx.Exec(t, `
INSERT INTO task_supplement_capability (task_id, workspace_id, issue_id, capability)
VALUES ($1, $2, $3, $4)
`, taskID, testWorkspaceID, issueID, protocol.DaemonCapabilityTaskSupplementV1)
dbfx.Cleanup(t, `DELETE FROM task_supplement_capability WHERE task_id = $1`, taskID)
}
dbfx.Cleanup(t, `DELETE FROM task_supplement WHERE task_id = $1`, taskID)
return supplementFixture{runtimeID: runtimeID, agentID: agentID, issueID: issueID, taskID: taskID, triggerID: triggerID}
}
func supplementRequest(t *testing.T, fixture supplementFixture, requestID, content string) *testutil.Response {
t.Helper()
req := newRequest(http.MethodPost, "/api/issues/"+fixture.issueID+"/tasks/"+fixture.taskID+"/supplements", map[string]any{
"client_request_id": requestID,
"content": content,
})
return testutil.Call(t, testHandler.CreateTaskSupplement,
withURLParams(req, "id", fixture.issueID, "taskId", fixture.taskID),
)
}
func TestTaskSupplementNegotiationFailsClosed(t *testing.T) {
for _, tc := range []struct {
name string
provider string
daemonAdvertises bool
wantCapabilityRow bool
}{
{name: "codex negotiated", provider: "codex", daemonAdvertises: true, wantCapabilityRow: true},
{name: "claude negotiated", provider: "claude", daemonAdvertises: true, wantCapabilityRow: true},
{name: "grok negotiated", provider: "grok", daemonAdvertises: true, wantCapabilityRow: true},
{name: "claude old daemon", provider: "claude", daemonAdvertises: false, wantCapabilityRow: false},
{name: "old daemon", provider: "codex", daemonAdvertises: false, wantCapabilityRow: false},
{name: "unsupported runtime", provider: "kimi", daemonAdvertises: true, wantCapabilityRow: false},
} {
t.Run(tc.name, func(t *testing.T) {
fixture := newSupplementFixture(t, tc.provider, "dispatched", false)
var capabilities []string
if tc.daemonAdvertises {
capabilities = []string{protocol.DaemonCapabilityTaskSupplementV1}
}
req := withURLParam(newRequest(http.MethodPost, "/start", map[string]any{"capabilities": capabilities}), "taskId", fixture.taskID)
var started AgentTaskResponse
testutil.Call(t, testHandler.StartTask, req).Want(http.StatusOK).JSON(&started)
if started.Status != "running" || started.StartedAt == nil {
t.Fatalf("StartTask returned stale row: %#v", started)
}
if got := started.SupplementCapability == protocol.DaemonCapabilityTaskSupplementV1; got != tc.wantCapabilityRow {
t.Fatalf("negotiated capability = %v, want %v", got, tc.wantCapabilityRow)
}
if !tc.wantCapabilityRow {
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-111111111111", "extra").Want(http.StatusPreconditionFailed)
} else {
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-111111111111", "extra").Want(http.StatusCreated)
}
})
}
}
func TestTaskSupplementCapabilityDoesNotBreakNonIssueStarts(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
for _, provider := range []string{"codex", "claude", "grok"} {
t.Run(provider, func(t *testing.T) {
runtimeID := dbfx.Runtime(t, "supplement-no-issue", testutil.Cols{"provider": provider})
agentID := dbfx.Agent(t, "Supplement no issue", runtimeID)
taskID := dbfx.Task(t, agentID, testutil.Cols{"runtime_id": runtimeID, "issue_id": nil, "status": "dispatched"})
started, err := testHandler.TaskService.StartTask(t.Context(), parseUUID(taskID), true)
if err != nil || started.Status != "running" {
t.Fatalf("non-issue start = %#v: %v", started, err)
}
if n := dbfx.Count(t, `SELECT count(*) FROM task_supplement_capability WHERE task_id = $1`, taskID); n != 0 {
t.Fatalf("capability rows = %d, want 0", n)
}
})
}
}
func TestTaskSupplementCompletionDoesNotReplayBoundComments(t *testing.T) {
for _, provider := range []string{"codex", "claude", "grok"} {
for _, status := range []string{"pending", "delivering", "delivered", "failed"} {
for _, ordinary := range []string{"none", "unhandled", "queued"} {
t.Run(provider+"/"+status+"/"+ordinary, func(t *testing.T) {
fixture := newSupplementFixture(t, provider, "running", true)
// A plain reply routes back to the agent owning the original thread.
dbfx.Exec(t, `UPDATE comment SET content = $2 WHERE id = $1`, fixture.triggerID,
fmt.Sprintf("[@Supplement](mention://agent/%s) original objective", fixture.agentID))
dbfx.Exec(t, `UPDATE agent_task_queue SET delivered_comment_ids = ARRAY[trigger_comment_id] WHERE id = $1`, fixture.taskID)
dbfx.Cleanup(t, `DELETE FROM agent_task_queue WHERE issue_id = $1`, fixture.issueID)
var ordinaryID, queuedID string
if ordinary != "none" {
ordinaryID = dbfx.Comment(t, fixture.issueID, "Handle this ordinary reply next.", testutil.Cols{
"parent_id": fixture.triggerID,
})
}
if ordinary == "queued" {
queuedID = dbfx.Task(t, fixture.agentID, testutil.Cols{
"issue_id": fixture.issueID, "runtime_id": fixture.runtimeID,
"trigger_comment_id": ordinaryID,
})
}
var supplement CommentResponse
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-999999999999", "Add this only to the current run.").
Want(http.StatusCreated).JSON(&supplement)
// Receipt transitions are covered by the retry test; vary only the
// persisted state consumed by completion reconciliation here.
dbfx.Exec(t, `UPDATE task_supplement SET status = $2 WHERE comment_id = $1`, supplement.ID, status)
// Drive the real completion handler: a direct SQL status update skips
// the reconciliation that previously replayed the supplement.
req := newDaemonTokenRequest(http.MethodPost, "/api/daemon/tasks/"+fixture.taskID+"/complete",
map[string]any{"output": "done"}, testWorkspaceID, "legit-daemon")
testutil.Call(t, testHandler.CompleteTask, withURLParam(req, "taskId", fixture.taskID)).Want(http.StatusOK)
if status == "pending" || status == "delivering" {
receipt, err := testHandler.Queries.GetTaskSupplementForRun(t.Context(), db.GetTaskSupplementForRunParams{
CommentID: parseUUID(supplement.ID), TaskID: parseUUID(fixture.taskID), WorkspaceID: parseUUID(testWorkspaceID),
})
if err != nil || receipt.Status != "failed" || receipt.FailureReason.String != protocol.TaskSupplementFailureTurnEnded {
t.Fatalf("completion did not settle pending delivery: %#v: %v", receipt, err)
}
}
wantTasks := 1
if ordinary != "none" {
wantTasks++
}
if n := dbfx.Count(t, `SELECT count(*) FROM agent_task_queue WHERE issue_id = $1`, fixture.issueID); n != wantTasks {
t.Fatalf("completion left %d runs, want %d; supplement must never create a follow-up", n, wantTasks)
}
if ordinary != "none" {
var followupID, triggerID string
var coalesced []string
dbfx.QueryRow(t, `SELECT id, trigger_comment_id, coalesced_comment_ids::text[]
FROM agent_task_queue WHERE issue_id = $1 AND id <> $2 AND status = 'queued'`,
fixture.issueID, fixture.taskID).Scan(&followupID, &triggerID, &coalesced)
if queuedID != "" && followupID != queuedID {
t.Fatalf("replaced existing queued run %s with %s", queuedID, followupID)
}
if triggerID != ordinaryID || slices.Contains(coalesced, supplement.ID) {
t.Fatalf("follow-up input = trigger %s, coalesced %v; want ordinary comment %s without supplement %s",
triggerID, coalesced, ordinaryID, supplement.ID)
}
}
})
}
}
}
}
func TestTaskSupplementConcurrentClaimsKeepUniqueReceipts(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
const additions = 20
for i := 0; i < additions; i++ {
supplementRequest(t, fixture, fmt.Sprintf("0199a4e8-22ce-7b01-bba5-%012x", i+1),
fmt.Sprintf("addition %02d", i)).Want(http.StatusCreated)
}
var wg sync.WaitGroup
type claimResult struct {
row db.ClaimNextTaskSupplementRow
err error
}
claims := make(chan claimResult, additions)
for i := 0; i < additions; i++ {
wg.Add(1)
go func() {
defer wg.Done()
row, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID))
claims <- claimResult{row, err}
}()
}
wg.Wait()
close(claims)
seen := make(map[string]bool, additions)
for result := range claims {
claimed := result.row
id := uuidToString(claimed.CommentID)
if result.err != nil || seen[id] || claimed.AttemptCount != 1 {
t.Fatalf("claim %q duplicated=%v attempts=%d: %v", id, seen[id], claimed.AttemptCount, result.err)
}
seen[id] = true
if _, err := testHandler.Queries.AckTaskSupplementDelivered(context.Background(), db.AckTaskSupplementDeliveredParams{
TaskID: parseUUID(fixture.taskID), CommentID: claimed.CommentID,
}); err != nil {
t.Fatalf("ack %s: %v", id, err)
}
}
if len(seen) != additions {
t.Fatalf("claimed %d unique comments, want %d", len(seen), additions)
}
if _, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID)); !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("extra claim = %v, want no rows", err)
}
if delivered := dbfx.Count(t, `SELECT count(*) FROM task_supplement WHERE task_id = $1 AND status = 'delivered'`, fixture.taskID); delivered != additions {
t.Fatalf("delivered receipts = %d, want %d", delivered, additions)
}
var taskCount int
dbfx.QueryRow(t, `SELECT count(*) FROM agent_task_queue WHERE issue_id = $1`, fixture.issueID).Scan(&taskCount)
if taskCount != 1 {
t.Fatalf("concurrent additions created %d runs, want one", taskCount)
}
}
func TestTaskSupplementConcurrentDuplicateSubmissionsAndRetries(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
const attempts = 10
responses := make(chan *testutil.Response, attempts)
var wg sync.WaitGroup
for i := 0; i < attempts; i++ {
wg.Add(1)
go func() {
defer wg.Done()
responses <- supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-888888888888", "duplicate addition")
}()
}
wg.Wait()
close(responses)
var commentID string
for response := range responses {
var comment CommentResponse
response.WantOneOf(http.StatusOK, http.StatusCreated).JSON(&comment)
if comment.SupplementTaskID != fixture.taskID || comment.SupplementStatus != "pending" {
t.Fatalf("submission receipt = %#v", comment)
}
if commentID != "" && commentID != comment.ID {
t.Fatalf("duplicate submission created %s, want %s", comment.ID, commentID)
}
commentID = comment.ID
}
if n := dbfx.Count(t, `SELECT count(*) FROM task_supplement WHERE task_id = $1`, fixture.taskID); n != 1 {
t.Fatalf("duplicate submissions created %d receipts", n)
}
if n := dbfx.Count(t, `SELECT count(*) FROM comment WHERE issue_id = $1 AND content = 'duplicate addition'`, fixture.issueID); n != 1 {
t.Fatalf("duplicate submissions left %d comments", n)
}
claim, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID))
if err != nil {
t.Fatal(err)
}
if _, err := testHandler.Queries.AckTaskSupplementFailed(context.Background(), db.AckTaskSupplementFailedParams{
TaskID: parseUUID(fixture.taskID), CommentID: claim.CommentID,
FailureReason: pgtype.Text{String: protocol.TaskSupplementFailureProviderRejected, Valid: true},
}); err != nil {
t.Fatal(err)
}
responses = make(chan *testutil.Response, attempts)
for i := 0; i < attempts; i++ {
wg.Add(1)
go func() {
defer wg.Done()
req := withURLParams(newRequest(http.MethodPost, "/retry", nil),
"id", fixture.issueID, "taskId", fixture.taskID, "commentId", commentID)
responses <- testutil.Call(t, testHandler.RetryTaskSupplement, req)
}()
}
wg.Wait()
close(responses)
for response := range responses {
response.Want(http.StatusOK)
}
retried, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID))
if err != nil || retried.CommentID != claim.CommentID || retried.AttemptCount != 2 {
t.Fatalf("retry claim = %#v: %v", retried, err)
}
if _, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID)); !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("duplicate retry left another claim: %v", err)
}
ack := db.AckTaskSupplementDeliveredParams{TaskID: parseUUID(fixture.taskID), CommentID: claim.CommentID}
firstAck, err := testHandler.Queries.AckTaskSupplementDelivered(context.Background(), ack)
if err != nil || firstAck.Status != "delivered" || !firstAck.DeliveredAt.Valid {
t.Fatalf("delivery receipt = %#v: %v", firstAck, err)
}
secondAck, err := testHandler.Queries.AckTaskSupplementDelivered(context.Background(), ack)
if err != nil || !secondAck.DeliveredAt.Valid || secondAck.DeliveredAt != firstAck.DeliveredAt {
t.Fatalf("duplicate acknowledgement changed receipt: %#v: %v", secondAck, err)
}
}
func TestTaskSupplementTimestampOrderAndCommentIDTieBreak(t *testing.T) {
for _, sameTime := range []bool{false, true} {
t.Run(fmt.Sprintf("same timestamp=%v", sameTime), func(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
var first, second CommentResponse
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-666666666666", "first").Want(http.StatusCreated).JSON(&first)
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-777777777777", "second").Want(http.StatusCreated).JSON(&second)
firstTime := time.Date(2026, 9, 22, 1, 0, 0, 0, time.UTC)
secondTime := firstTime.Add(time.Second)
want := []string{first.ID, second.ID}
if sameTime {
secondTime = firstTime
slices.Sort(want)
} else {
// Deliberately invert creation order: timestamps, not a hidden
// allocation sequence, must drive both metadata and claims.
firstTime, secondTime = secondTime, firstTime
want = []string{second.ID, first.ID}
}
dbfx.Exec(t, `UPDATE task_supplement SET created_at = $2 WHERE comment_id = $1`, first.ID, firstTime)
dbfx.Exec(t, `UPDATE task_supplement SET created_at = $2 WHERE comment_id = $1`, second.ID, secondTime)
rows, err := testHandler.Queries.ListTaskSupplementMetadata(context.Background(), db.ListTaskSupplementMetadataParams{
WorkspaceID: parseUUID(testWorkspaceID), TaskIds: []pgtype.UUID{parseUUID(fixture.taskID)},
})
if err != nil || len(rows) != 1 {
t.Fatalf("metadata rows=%v: %v", rows, err)
}
if got := uuidsToStrings(rows[0].CommentIds); !slices.Equal(got, want) {
t.Fatalf("metadata order=%v, want %v", got, want)
}
for _, id := range want {
claim, err := testHandler.Queries.ClaimNextTaskSupplement(context.Background(), parseUUID(fixture.taskID))
if err != nil || uuidToString(claim.CommentID) != id {
t.Fatalf("claim=%s, want %s: %v", uuidToString(claim.CommentID), id, err)
}
}
})
}
}
func TestTaskSupplementTerminalRaceCreatesNothing(t *testing.T) {
for _, status := range []string{"completed", "cancelled"} {
t.Run(status, func(t *testing.T) {
assertTaskSupplementTerminalRaceCreatesNothing(t, status)
})
}
}
func assertTaskSupplementTerminalRaceCreatesNothing(t *testing.T, terminalStatus string) {
t.Helper()
fixture := newSupplementFixture(t, "codex", "running", true)
tx, err := testPool.Begin(context.Background())
if err != nil {
t.Fatal(err)
}
defer tx.Rollback(context.Background())
if _, err := tx.Exec(context.Background(), `SELECT id FROM agent_task_queue WHERE id = $1 FOR UPDATE`, fixture.taskID); err != nil {
t.Fatal(err)
}
result := make(chan error, 1)
go func() {
_, createErr := testHandler.Queries.CreateTaskSupplement(context.Background(), db.CreateTaskSupplementParams{
TaskID: parseUUID(fixture.taskID), IssueID: parseUUID(fixture.issueID), WorkspaceID: parseUUID(testWorkspaceID),
AuthorID: parseUUID(testUserID), Content: "must not orphan",
ClientRequestID: parseUUID("0199a4e8-22ce-7b01-bba5-444444444444"),
})
result <- createErr
}()
if _, err := tx.Exec(context.Background(), `UPDATE agent_task_queue SET status = $2, completed_at = now() WHERE id = $1`, fixture.taskID, terminalStatus); err != nil {
t.Fatal(err)
}
if err := tx.Commit(context.Background()); err != nil {
t.Fatal(err)
}
select {
case err := <-result:
if !errors.Is(err, pgx.ErrNoRows) {
t.Fatalf("race create error = %v, want no rows", err)
}
case <-time.After(5 * time.Second):
t.Fatal("race create remained blocked")
}
var comments, supplements int
dbfx.QueryRow(t, `SELECT count(*) FROM comment WHERE issue_id = $1 AND content = 'must not orphan'`, fixture.issueID).Scan(&comments)
dbfx.QueryRow(t, `SELECT count(*) FROM task_supplement WHERE task_id = $1`, fixture.taskID).Scan(&supplements)
if comments != 0 || supplements != 0 {
t.Fatalf("terminal race left comments=%d supplements=%d", comments, supplements)
}
}
func TestTaskSupplementStopRemainsIndependent(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
var comment CommentResponse
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-777777777777", "pending when stopped").
Want(http.StatusCreated).JSON(&comment)
cancelReq := withURLParams(newRequest(http.MethodPost, "/cancel", nil),
"id", fixture.issueID, "taskId", fixture.taskID)
testutil.Call(t, testHandler.CancelTask, cancelReq).Want(http.StatusOK)
receipt, err := testHandler.Queries.GetTaskSupplementForRun(context.Background(), db.GetTaskSupplementForRunParams{
CommentID: parseUUID(comment.ID), TaskID: parseUUID(fixture.taskID), WorkspaceID: parseUUID(testWorkspaceID),
})
if err != nil || receipt.Status != "failed" || receipt.FailureReason.String != "turn_ended" {
t.Fatalf("stop receipt = %#v, err %v", receipt, err)
}
var taskCount int
dbfx.QueryRow(t, `SELECT count(*) FROM agent_task_queue WHERE issue_id = $1`, fixture.issueID).Scan(&taskCount)
if taskCount != 1 {
t.Fatalf("stop or supplement created %d runs, want one", taskCount)
}
}
func TestTaskSupplementFailureSettlesPendingReceipt(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
var comment CommentResponse
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-555555555555", "pending when failed").
Want(http.StatusCreated).JSON(&comment)
if w := failTaskViaHandler(t, fixture.taskID); w.Code != http.StatusOK {
t.Fatalf("fail task: status %d: %s", w.Code, w.Body.String())
}
receipt, err := testHandler.Queries.GetTaskSupplementForRun(t.Context(), db.GetTaskSupplementForRunParams{
CommentID: parseUUID(comment.ID), TaskID: parseUUID(fixture.taskID), WorkspaceID: parseUUID(testWorkspaceID),
})
if err != nil || receipt.Status != "failed" || receipt.FailureReason.String != protocol.TaskSupplementFailureTurnEnded {
t.Fatalf("failed task receipt = %#v, err %v", receipt, err)
}
}
func TestTaskSupplementTerminalWriteRollsBackWhenSettlementFails(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
var comment CommentResponse
supplementRequest(t, fixture, "0199a4e8-22ce-7b01-bba5-aaaaaaaaaaaa", "must settle atomically").
Want(http.StatusCreated).JSON(&comment)
dbfx.Exec(t, `
CREATE OR REPLACE FUNCTION reject_task_supplement_settlement() RETURNS trigger AS $$
BEGIN RAISE EXCEPTION 'settlement rejected'; END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER reject_task_supplement_settlement
BEFORE UPDATE OF status ON task_supplement
FOR EACH ROW EXECUTE FUNCTION reject_task_supplement_settlement()
`)
t.Cleanup(func() {
_, _ = testPool.Exec(context.Background(), `DROP TRIGGER IF EXISTS reject_task_supplement_settlement ON task_supplement`)
_, _ = testPool.Exec(context.Background(), `DROP FUNCTION IF EXISTS reject_task_supplement_settlement()`)
})
if w := completeTaskViaHandler(t, fixture.taskID, "done"); w.Code != http.StatusInternalServerError {
t.Fatalf("complete task with rejected settlement: status %d: %s", w.Code, w.Body.String())
}
var taskStatus, receiptStatus string
dbfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, fixture.taskID).Scan(&taskStatus)
dbfx.QueryRow(t, `SELECT status FROM task_supplement WHERE comment_id = $1`, comment.ID).Scan(&receiptStatus)
if taskStatus != "running" || receiptStatus != "pending" {
t.Fatalf("partial terminal commit: task=%s receipt=%s", taskStatus, receiptStatus)
}
}
func TestTaskSupplementPermissionAndTenantIsolation(t *testing.T) {
fixture := newSupplementFixture(t, "codex", "running", true)
otherUser := dbfx.Insert(t, "user", testutil.Cols{"name": "No Invoke", "email": "[email protected]"})
dbfx.Member(t, testWorkspaceID, otherUser, "member")
req := newRequestAs(otherUser, http.MethodPost, "/supplements", map[string]any{
"client_request_id": "0199a4e8-22ce-7b01-bba5-555555555555", "content": "not allowed",
})
testutil.Call(t, testHandler.CreateTaskSupplement,
withURLParams(req, "id", fixture.issueID, "taskId", fixture.taskID)).Want(http.StatusForbidden)
foreignIssueID, foreignTaskID := setupForeignWorkspaceFixture(t)
crossTenant := newRequest(http.MethodPost, "/api/issues/"+foreignIssueID+"/tasks/"+foreignTaskID+"/supplements", map[string]any{
"client_request_id": "0199a4e8-22ce-7b01-bba5-666666666666", "content": "cross tenant",
})
testutil.Call(t, testHandler.CreateTaskSupplement,
withURLParams(crossTenant, "id", foreignIssueID, "taskId", foreignTaskID)).Want(http.StatusNotFound)
var count int
dbfx.QueryRow(t, `SELECT count(*) FROM task_supplement WHERE task_id = $1`, fixture.taskID).Scan(&count)
if count != 0 {
t.Fatalf("denied requests created %d supplements", count)
}
}