Files
multica/server/internal/handler/claim_project_context_test.go
13eb4bda93 MUL-8154 fix(daemon): enforce runtime ownership on daemon routes (#9423)
* fix(daemon): enforce runtime ownership on daemon routes (MUL-8154)

/api/daemon/* accepts any workspace member's PAT/JWT, and authorization only
checked workspace membership. Any member could claim another member's tasks
(receiving the agent's custom_env, MCP config and an owner-scoped task token),
write task results/messages, and take a runtime over by re-registering its
daemon_id.

- Runtime-level check on every runtime/task daemon route, HTTP heartbeat,
  deregister, batch claim (and so the WS claim RPC) and the WS handshake:
  user credentials must own the runtime (NULL owner fails closed); daemon
  tokens must match a non-empty runtime daemon_id.
- Register: pre-check that every existing row on the daemon_id belongs to the
  caller, plus a required_owner_id fence in both upserts (incl. the
  failed-profile path) so a concurrent registration cannot win the row.
  Daemon-token registration keeps the existing owner.
- Legacy merge: only for user-credential registrations, and only when the
  caller owns both rows, verified on the FOR UPDATE-locked rows.
- Tasks without a runtime are not writable through daemon routes; status
  stays readable.
- GetTaskStatus / WS lease queries carry runtime owner/daemon so the hot
  paths keep one round trip.

Co-authored-by: multica-agent <[email protected]>

* test(daemon): authenticate fixtures as the runtime owner (MUL-8154)

Most handler tests drove daemon routes with a daemon token whose daemon_id
did not match the fixture runtime, relying on the workspace-wide token scope
this fix removes. They now authenticate the way real daemons do, with the
runtime owner's user credential (newOwnerDaemonRequest). Tests about daemon
token scoping keep mdt_ and get fixtures with a matching daemon_id; the
ownerless-runtime tests also assert that a user credential is refused.

Co-authored-by: multica-agent <[email protected]>

* fix(daemon): serialize registration per daemon and keep Qoder WS claims working (MUL-8154)

Review of #9423:

- P1: the owner pre-check and the per-provider/profile upserts hit different
  conflict keys, so two members registering different providers of the same
  daemon could both pass the pre-check and both insert, leaving one daemon
  with two owners (and a daemon token for either runtime reaching the
  other's). Registration now runs the pre-check and every runtime write in one
  transaction under a pg_advisory_xact_lock on (workspace_id, daemon_id), on a
  single connection so queued registrations cannot starve the holder. Legacy
  merges, analytics and the register event run after commit; failed-profile
  records use savepoints so they stay best-effort.
- P2: the Qoder revocation check read the Authorization header, which the WS
  RPC bridge's in-process request does not carry, so valid Qoder WS claims
  silently returned nothing. DaemonAuth now records the mdt_ token hash in the
  request context, the WS identity keeps it, the RPC bridge re-attaches it,
  and the check validates that hash on every call (revocation still applies
  to an open connection).
- Regression tests for cross-provider and provider+profile concurrent
  registration, and for valid/revoked Qoder credentials over WS RPC.

Co-authored-by: multica-agent <[email protected]>

* fix(daemon): bind daemon tokens to the runtime owner they were issued for (MUL-8154)

Second review of #9423: a daemon_id can be re-registered by another member
after its runtimes are deleted, and a daemon token was bound only to
(workspace, daemon_id), so the previous owner's Remote MCP token reached the
new owner's runtime (heartbeat, claim, owner-scoped task token).

- daemon_token.owner_id (migration 588). Remote MCP tokens are bound to the
  locked runtime owner inside FinalizeTaskClaim; live tokens are backfilled
  only where their daemon has exactly one owner.
- DaemonAuth carries the owner binding (DB and cache; cache key bumped to v2
  so pre-upgrade entries are never read), the WS identity keeps it and the
  RPC bridge replays it.
- Daemon-token authorization now also requires token owner == runtime owner.
  Qoder-managed tokens stay unbound (governed by cloud_agent_connection), and
  user credentials can no longer register the reserved qoder-managed-* ids.
- Tests: the reuse lifecycle over HTTP, a new WS handshake and an already
  open WS connection; the reserved daemon_id; the concurrency barrier now
  hooks the registration transaction (TxStarter) so the interleaving is real.

Co-authored-by: multica-agent <[email protected]>

* fix(daemon): owner-bind daemon-token registration; drop unsafe token backfill (MUL-8154)

Third review of #9423:

- Migration 588 no longer backfills daemon_token.owner_id from the daemon's
  current owner: that owner may be a later re-registrant, which re-bound the
  previous owner's live token to them. Existing non-Qoder tokens stay NULL and
  are refused (24h Remote MCP credentials, re-issued on the next claim).
- owner_id is a plain UUID column, no foreign key / cascade (AGENTS.md).
- Register via a non-Qoder daemon token now uses the token's owner for the
  in-transaction pre-check and the SQL owner fence, and refuses unbound
  tokens. Qoder keeps its connection-based check; daemon tokens still never
  trigger legacy merges.
- Tests: an old owner's bound token and an unbound legacy token can neither
  register over nor heartbeat the replacement owner's runtime.

Co-authored-by: multica-agent <[email protected]>

* fix(daemon): grant the Qoder registration exemption only with a live connection (MUL-8154)

Fourth review of #9423: register skipped the token owner binding for any
qoder-managed-* daemon token, but checked the Qoder connection only when the
body declared a qoder_cloud runtime. A pre-upgrade Remote MCP token under that
prefix (no owner, no connection) could register type=claude and rewrite
another member's runtime.

Register now verifies the token against a live cloud_agent_connection before
anything else whenever the token is in the Qoder namespace, regardless of the
declared runtimes, and restricts that namespace to the bridge's own shape: a
built-in qoder_cloud runtime, no custom profiles, no failed_profiles.

Test covers built-in, custom-profile, failed-profiles-only and qoder_cloud
bodies with an unconnected token, plus the connected token's allowed and
refused shapes.

Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: multica-eve <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-10-22 16:47:06 +08:00

525 lines
19 KiB
Go

package handler
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/multica-ai/multica/server/internal/testutil"
db "github.com/multica-ai/multica/server/pkg/db/generated"
)
// The claim path resolves project context from a SOFT reference: issue.project_id,
// chat_session.project_id and the quick-create context JSONB are all plain
// columns with no foreign key behind them (see the repo's no-FK rule). These
// tests pin the tenant boundary that resolveClaimProjectContext is responsible
// for, using the only states that can actually produce a cross-tenant read:
// a dangling reference to a project in another workspace, and a project_resource
// row whose own workspace_id disagrees with its project's.
//
// Both were reachable before MUL-6547: the issue and quick-create branches read
// the project with GetProject (no workspace predicate) and every branch listed
// resources by project_id alone.
// foreignWorkspaceWithProject builds a complete project in a DIFFERENT workspace
// and returns (workspaceID, projectID). The caller points a same-workspace task
// at projectID to simulate the corrupt reference.
func foreignWorkspaceWithProject(t *testing.T, slug, prefix string) (string, string) {
t.Helper()
foreignWorkspaceID := dbfx.Insert(t, "workspace", testutil.Cols{
"name": "Foreign " + slug,
"slug": slug,
"description": "",
"issue_prefix": prefix,
})
foreignProjectID := dbfx.Project(t, foreignProjectTitle, testutil.Cols{
"workspace_id": foreignWorkspaceID,
"description": foreignProjectDescription,
})
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": foreignProjectID,
"workspace_id": foreignWorkspaceID,
"resource_type": "github_repo",
"resource_ref": `{"url":"` + foreignRepoURL + `"}`,
"position": 0,
})
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": foreignProjectID,
"workspace_id": foreignWorkspaceID,
"resource_type": "local_directory",
"resource_ref": `{"daemon_id":"foreign-daemon","local_path":"` + foreignLocalPath + `","execution_mode":"in_place"}`,
"position": 1,
})
return foreignWorkspaceID, foreignProjectID
}
const (
foreignProjectTitle = "Foreign project must not leak"
foreignProjectDescription = "Foreign project instructions must not leak"
foreignRepoURL = "https://github.com/example/foreign-tenant-repo"
foreignLocalPath = "/srv/foreign-tenant-project"
localFallbackRepoURL = "https://github.com/example/local-workspace-fallback"
)
// assertNoForeignContext fails when any part of the foreign tenant's project
// reached the claim response, on any field the daemon forwards to the agent.
func assertNoForeignContext(t *testing.T, body string, extra ...string) {
t.Helper()
for _, leaked := range append([]string{
foreignProjectTitle,
foreignProjectDescription,
foreignRepoURL,
foreignLocalPath,
}, extra...) {
if leaked == "" {
continue
}
if strings.Contains(body, leaked) {
t.Errorf("claim response leaked foreign tenant context %q: %s", leaked, body)
}
}
}
type claimProjectFields struct {
WorkspaceID string `json:"workspace_id"`
Repos []RepoData `json:"repos"`
ProjectID string `json:"project_id"`
ProjectTitle string `json:"project_title"`
ProjectDescription string `json:"project_description"`
ProjectResources []ProjectResourceData `json:"project_resources"`
}
// An issue whose project_id points at another workspace's project must degrade
// to workspace context. Before MUL-6547 the issue branch used GetProject, which
// has no workspace predicate, so the foreign project's title, description,
// repository URL and local path were all serialized into the claim.
func TestClaimTask_IssueProjectInForeignWorkspace_DegradesToWorkspaceRepos(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
setHandlerTestWorkspaceRepos(t, []map[string]string{
{"url": localFallbackRepoURL, "description": "local"},
})
_, foreignProjectID := foreignWorkspaceWithProject(t, "foreign-issue-project-ws", "FIP")
var agentID, runtimeID string
dbfx.QueryRow(t,
`SELECT id, runtime_id FROM agent WHERE workspace_id = $1 LIMIT 1`,
testWorkspaceID,
).Scan(&agentID, &runtimeID)
issueID := dbfx.Issue(t, "issue pointing at a foreign project", testutil.Cols{
"project_id": foreignProjectID,
"priority": "medium",
"number": 88101,
})
dbfx.Task(t, agentID, testutil.Cols{
"runtime_id": runtimeID,
"issue_id": issueID,
})
req := newOwnerDaemonRequest("POST", "/api/daemon/runtimes/"+runtimeID+"/claim", nil)
req = withURLParam(req, "runtimeId", runtimeID)
w := testutil.Call(t, testHandler.ClaimTaskByRuntime, req).Want(http.StatusOK)
var resp struct {
Task *claimProjectFields `json:"task"`
}
w.JSON(&resp)
if resp.Task == nil {
t.Fatal("expected task in response")
}
assertNoForeignContext(t, w.Text(), foreignProjectID)
if resp.Task.ProjectID != "" {
t.Errorf("project_id = %q, want empty for an out-of-workspace project reference", resp.Task.ProjectID)
}
if len(resp.Task.ProjectResources) != 0 {
t.Errorf("project_resources = %+v, want none", resp.Task.ProjectResources)
}
if len(resp.Task.Repos) != 1 || resp.Task.Repos[0].URL != localFallbackRepoURL {
t.Fatalf("repos = %+v, want only the local workspace fallback", resp.Task.Repos)
}
}
// The quick-create branch resolves its project from the task context JSONB
// rather than an issue row, and used the same unscoped GetProject. It needs its
// own assertion: a requester who edits the stored project_id must not be able to
// pull another tenant's resources into the claim.
func TestClaimTask_QuickCreateProjectInForeignWorkspace_DegradesToWorkspaceRepos(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
ctx := context.Background()
setHandlerTestWorkspaceRepos(t, []map[string]string{
{"url": localFallbackRepoURL, "description": "local"},
})
_, foreignProjectID := foreignWorkspaceWithProject(t, "foreign-qc-project-ws", "FQP")
agentID, runtimeID, daemonID := createRuntimeGuardAgent(t, ctx)
quickContext, _ := json.Marshal(map[string]any{
"type": "quick_create",
"prompt": "create a follow-up issue",
"requester_id": testUserID,
"workspace_id": testWorkspaceID,
"project_id": foreignProjectID,
})
dbfx.Exec(t, `
INSERT INTO agent_task_queue (agent_id, runtime_id, status, priority, context)
VALUES ($1, $2, 'queued', 2, $3)
`, agentID, runtimeID, quickContext)
req := newDaemonTokenRequest("POST", "/api/daemon/runtimes/"+runtimeID+"/claim", nil,
testWorkspaceID, daemonID)
req = withURLParam(req, "runtimeId", runtimeID)
w := testutil.Call(t, testHandler.ClaimTaskByRuntime, req).Want(http.StatusOK)
var resp struct {
Task *claimProjectFields `json:"task"`
}
w.JSON(&resp)
if resp.Task == nil {
t.Fatal("expected task in response")
}
assertNoForeignContext(t, w.Text(), foreignProjectID)
if resp.Task.ProjectID != "" {
t.Errorf("quick-create project_id = %q, want empty for an out-of-workspace project", resp.Task.ProjectID)
}
if len(resp.Task.ProjectResources) != 0 {
t.Errorf("quick-create project_resources = %+v, want none", resp.Task.ProjectResources)
}
if len(resp.Task.Repos) != 1 || resp.Task.Repos[0].URL != localFallbackRepoURL {
t.Fatalf("quick-create repos = %+v, want only the local workspace fallback", resp.Task.Repos)
}
}
// A project_resource row carries its own workspace_id, so a resource can
// disagree with the project it hangs off. Listing by project_id alone — which
// every claim branch did before MUL-6547 — serialized that row anyway. The
// in-workspace resource on the same project must still come through, proving the
// new predicate filters rather than just emptying the list.
func TestClaimTask_ProjectResourceFromForeignWorkspace_IsFilteredOut(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
foreignWorkspaceID, _ := foreignWorkspaceWithProject(t, "foreign-resource-ws", "FRW")
const localRepoURL = "https://github.com/example/local-project-repo"
projectID := dbfx.Project(t, "Project with a mismatched resource row")
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": projectID,
"workspace_id": testWorkspaceID,
"resource_type": "github_repo",
"resource_ref": `{"url":"` + localRepoURL + `"}`,
"position": 0,
})
// Same project, foreign workspace_id: only reachable through corrupt data,
// which is exactly the case the predicate exists for.
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": projectID,
"workspace_id": foreignWorkspaceID,
"resource_type": "github_repo",
"resource_ref": `{"url":"` + foreignRepoURL + `"}`,
"position": 1,
})
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": projectID,
"workspace_id": foreignWorkspaceID,
"resource_type": "local_directory",
"resource_ref": `{"daemon_id":"foreign-daemon","local_path":"` + foreignLocalPath + `","execution_mode":"in_place"}`,
"position": 2,
})
var agentID, runtimeID string
dbfx.QueryRow(t,
`SELECT id, runtime_id FROM agent WHERE workspace_id = $1 LIMIT 1`,
testWorkspaceID,
).Scan(&agentID, &runtimeID)
issueID := dbfx.Issue(t, "issue with a mismatched project resource", testutil.Cols{
"project_id": projectID,
"priority": "medium",
"number": 88102,
})
dbfx.Task(t, agentID, testutil.Cols{
"runtime_id": runtimeID,
"issue_id": issueID,
})
req := newOwnerDaemonRequest("POST", "/api/daemon/runtimes/"+runtimeID+"/claim", nil)
req = withURLParam(req, "runtimeId", runtimeID)
w := testutil.Call(t, testHandler.ClaimTaskByRuntime, req).Want(http.StatusOK)
var resp struct {
Task *claimProjectFields `json:"task"`
}
w.JSON(&resp)
if resp.Task == nil {
t.Fatal("expected task in response")
}
assertNoForeignContext(t, w.Text())
if resp.Task.ProjectID != projectID {
t.Errorf("project_id = %q, want %q", resp.Task.ProjectID, projectID)
}
if len(resp.Task.ProjectResources) != 1 {
t.Fatalf("project_resources count = %d, want only the in-workspace row: %+v",
len(resp.Task.ProjectResources), resp.Task.ProjectResources)
}
if len(resp.Task.Repos) != 1 || resp.Task.Repos[0].URL != localRepoURL {
t.Fatalf("repos = %+v, want only the in-workspace project repo", resp.Task.Repos)
}
}
// A run_only autopilot owns its task's workspace. taskToResponse seeds
// resp.WorkspaceID with the RUNTIME's workspace, and the branch used to widen it
// only `if resp.WorkspaceID == ""` — a condition that never held, so the
// isolation check compared the runtime workspace against itself and a
// cross-workspace autopilot_run_id was dispatched instead of rejected.
func TestClaimTask_AutopilotRunOnlyForeignWorkspace_CancelsTask(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
var localAgentID, localRuntimeID string
dbfx.QueryRow(t,
`SELECT id, runtime_id FROM agent WHERE workspace_id = $1 LIMIT 1`,
testWorkspaceID,
).Scan(&localAgentID, &localRuntimeID)
foreignWorkspaceID, foreignProjectID := foreignWorkspaceWithProject(t, "foreign-autopilot-ws", "FAW")
const foreignAutopilotTitle = "Foreign autopilot must not leak"
foreignAutopilotID := dbfx.Insert(t, "autopilot", testutil.Cols{
"workspace_id": foreignWorkspaceID,
"project_id": foreignProjectID,
"title": foreignAutopilotTitle,
"assignee_id": localAgentID,
"execution_mode": "run_only",
"created_by_type": "member",
"created_by_id": testUserID,
})
foreignRunID := dbfx.Insert(t, "autopilot_run", testutil.Cols{
"autopilot_id": foreignAutopilotID,
"source": "manual",
"status": "running",
})
taskID := dbfx.Task(t, localAgentID, testutil.Cols{
"runtime_id": localRuntimeID,
"issue_id": nil,
"autopilot_run_id": foreignRunID,
})
req := newOwnerDaemonRequest("POST", "/api/daemon/runtimes/"+localRuntimeID+"/claim", nil)
req = withURLParam(req, "runtimeId", localRuntimeID)
w := testutil.Call(t, testHandler.ClaimTaskByRuntime, req).Want(http.StatusInternalServerError)
if !strings.Contains(w.Text(), "task workspace isolation check failed") {
t.Fatalf("claim error = %q, want the workspace isolation failure", w.Text())
}
assertNoForeignContext(t, w.Text(), foreignProjectID)
if strings.Contains(w.Text(), foreignAutopilotTitle) {
t.Errorf("claim error leaked the foreign autopilot title: %s", w.Text())
}
var status string
dbfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, taskID).Scan(&status)
if status != "cancelled" {
t.Fatalf("cross-workspace autopilot task status = %q, want cancelled", status)
}
}
// failQueryDBTX delegates every query to the real pool except the ones whose
// SQL contains failOn, which fail with a transient-looking error. db.Queries is
// built on the DBTX interface, so this is the seam that lets a single source
// read fail while the rest of the claim proceeds normally — the shape of a real
// DB blip, which a globally broken pool cannot reproduce (it trips the earlier
// agent-load guard instead).
type failQueryDBTX struct {
db.DBTX
failOn string
err error
}
type errRow struct{ err error }
func (r errRow) Scan(...any) error { return r.err }
func (f failQueryDBTX) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
if strings.Contains(sql, f.failOn) {
return errRow{err: f.err}
}
return f.DBTX.QueryRow(ctx, sql, args...)
}
func (f failQueryDBTX) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
if strings.Contains(sql, f.failOn) {
return nil, f.err
}
return f.DBTX.Query(ctx, sql, args...)
}
// A claim resolves its SOURCE row — issue, chat session, autopilot_run,
// autopilot — before anything else. Those reads used to be written as
// `if x, err := ...; err == nil {`, with no else: a failed read silently
// skipped the whole branch. taskToResponse has already seeded resp.WorkspaceID
// with the RUNTIME's workspace by then, so the skipped claim sailed through the
// builder's backstop isolation check (which compared that seed against itself)
// and dispatched an agent with no issue, no chat input, or no autopilot
// instructions at all.
//
// A transient read failure must instead produce NO claim payload and preserve
// the task for the stale-dispatched reclaim — cancelling would destroy a valid
// task over a momentary DB error.
func TestBuildClaimedTaskResponse_SourceLoadFailure_PreservesTaskAndEmitsNoContext(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
var agentID, runtimeID string
dbfx.QueryRow(t,
`SELECT id, runtime_id FROM agent WHERE workspace_id = $1 LIMIT 1`,
testWorkspaceID,
).Scan(&agentID, &runtimeID)
projectID := dbfx.Project(t, "Source load failure project")
dbfx.Insert(t, "project_resource", testutil.Cols{
"project_id": projectID,
"workspace_id": testWorkspaceID,
"resource_type": "github_repo",
"resource_ref": `{"url":"https://github.com/example/must-not-appear"}`,
"position": 0,
})
issueID := dbfx.Issue(t, "source load failure issue", testutil.Cols{
"project_id": projectID,
"priority": "medium",
"number": 88201,
})
chatSessionID := dbfx.ChatSession(t, agentID, testutil.Cols{"project_id": projectID})
for _, tc := range []struct {
name string
failOn string
cols testutil.Cols
}{
{
name: "issue",
failOn: "FROM issue\nWHERE id = $1",
cols: testutil.Cols{"runtime_id": runtimeID, "issue_id": issueID},
},
{
name: "chat session",
failOn: "FROM chat_session\nWHERE id = $1",
cols: testutil.Cols{"runtime_id": runtimeID, "issue_id": nil, "chat_session_id": chatSessionID},
},
} {
t.Run(tc.name, func(t *testing.T) {
taskID := dbfx.Task(t, agentID, tc.cols)
dbfx.Exec(t, `UPDATE agent_task_queue SET status = 'dispatched' WHERE id = $1`, taskID)
task := db.AgentTaskQueue{
ID: parseUUID(taskID),
AgentID: parseUUID(agentID),
RuntimeID: parseUUID(runtimeID),
Status: "dispatched",
}
if issueRef, ok := tc.cols["issue_id"].(string); ok {
task.IssueID = parseUUID(issueRef)
}
if chatRef, ok := tc.cols["chat_session_id"].(string); ok {
task.ChatSessionID = parseUUID(chatRef)
}
brokenHandler := *testHandler
brokenHandler.Queries = db.New(failQueryDBTX{
DBTX: testPool,
failOn: tc.failOn,
err: errors.New("simulated transient read failure"),
})
runtime := db.AgentRuntime{
ID: parseUUID(runtimeID),
WorkspaceID: parseUUID(testWorkspaceID),
}
req := httptest.NewRequest(http.MethodPost, "/api/daemon/runtimes/"+runtimeID+"/claim", nil)
resp, _, _, _, _, failure := brokenHandler.buildClaimedTaskResponse(
req, &task, runtime, runtimeID, testWorkspaceID)
if failure == nil {
t.Fatal("expected the claim to be rejected when its source row cannot be read")
}
if failure.outcome != "error_source_load" {
t.Errorf("outcome = %q, want error_source_load", failure.outcome)
}
if failure.status != http.StatusInternalServerError {
t.Errorf("status = %d, want 500", failure.status)
}
// No context may be assembled from a source the handler could not read.
if resp.ThreadName != "" {
t.Errorf("thread_name = %q, want empty on a failed source read", resp.ThreadName)
}
if resp.ProjectID != "" || len(resp.ProjectResources) != 0 || len(resp.Repos) != 0 {
t.Errorf("project context leaked into a failed claim: project_id=%q resources=%d repos=%d",
resp.ProjectID, len(resp.ProjectResources), len(resp.Repos))
}
// A transient failure preserves the task for the reclaim path.
var status string
dbfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, taskID).Scan(&status)
if status != "dispatched" {
t.Errorf("task status = %q, want it preserved as dispatched for reclaim", status)
}
})
}
}
// The other half of the split: a source row that is GONE cannot be retried into
// existence, so it settles terminally instead of spinning in the reclaim loop.
// Legacy FKs keep this state from arising today (agent_task_queue.issue_id ON
// DELETE CASCADE and friends), so the branch is exercised directly rather than
// through a claim.
func TestRejectClaimSourceLoad_MissingRow_CancelsTask(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")
}
var agentID, runtimeID string
dbfx.QueryRow(t,
`SELECT id, runtime_id FROM agent WHERE workspace_id = $1 LIMIT 1`,
testWorkspaceID,
).Scan(&agentID, &runtimeID)
taskID := dbfx.Task(t, agentID, testutil.Cols{"runtime_id": runtimeID, "issue_id": nil})
dbfx.Exec(t, `UPDATE agent_task_queue SET status = 'dispatched' WHERE id = $1`, taskID)
task := db.AgentTaskQueue{ID: parseUUID(taskID), AgentID: parseUUID(agentID), Status: "dispatched"}
failure := testHandler.rejectClaimSourceLoad(
context.Background(), &task, pgx.ErrNoRows, "issue", uuidToString(task.IssueID))
if failure == nil {
t.Fatal("expected a failure for a missing source row")
}
if failure.outcome != "error_source_missing" {
t.Errorf("outcome = %q, want error_source_missing", failure.outcome)
}
if failure.message != "task is missing its issue" {
t.Errorf("message = %q, want it to name the missing source", failure.message)
}
var status string
dbfx.QueryRow(t, `SELECT status FROM agent_task_queue WHERE id = $1`, taskID).Scan(&status)
if status != "cancelled" {
t.Errorf("task status = %q, want cancelled for an unrecoverable source reference", status)
}
}