mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-09 01:46:46 +08:00
* 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]>
261 lines
10 KiB
Go
261 lines
10 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/multica-ai/multica/server/internal/service"
|
|
"github.com/multica-ai/multica/server/internal/testutil"
|
|
"github.com/multica-ai/multica/server/pkg/protocol"
|
|
)
|
|
|
|
// actAsRun makes a request come from an agent's run, as the CLI does inside a
|
|
// task.
|
|
func actAsRun(req *http.Request, agentID, taskID string) *http.Request {
|
|
req.Header.Set("X-Agent-ID", agentID)
|
|
req.Header.Set("X-Task-ID", taskID)
|
|
req.Header.Set("X-Actor-Source", "task_token")
|
|
return req
|
|
}
|
|
|
|
func setStatusAsRun(t *testing.T, issueID, status, agentID, taskID string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
req := withURLParam(newRequest("PUT", "/api/issues/"+issueID, map[string]any{"status": status}), "id", issueID)
|
|
if agentID != "" {
|
|
req = actAsRun(req, agentID, taskID)
|
|
}
|
|
testHandler.UpdateIssue(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("set status %q: %d %s", status, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func commentOn(t *testing.T, issueID, content, agentID, taskID string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
req := withURLParam(newRequest("POST", "/api/issues/"+issueID+"/comments", map[string]any{"content": content}), "id", issueID)
|
|
if agentID != "" {
|
|
req = actAsRun(req, agentID, taskID)
|
|
}
|
|
testHandler.CreateComment(w, req)
|
|
if w.Code != http.StatusCreated && w.Code != http.StatusOK {
|
|
t.Fatalf("comment: %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func wakeupRunsOf(t *testing.T, wakeupID string) int {
|
|
t.Helper()
|
|
return dbfx.Count(t, `SELECT count(*) FROM agent_task_queue WHERE context->>'wakeup_id' = $1`, wakeupID)
|
|
}
|
|
|
|
func lastWakeupOutcome(t *testing.T, issueID string) (outcome, taskID string) {
|
|
t.Helper()
|
|
var raw []byte
|
|
dbfx.QueryRow(t, `SELECT details FROM activity_log WHERE issue_id = $1 AND action = 'wakeup_triggered'
|
|
ORDER BY created_at DESC, id DESC LIMIT 1`, issueID).Scan(&raw)
|
|
var details struct {
|
|
Outcome string `json:"outcome"`
|
|
TaskID string `json:"task_id"`
|
|
}
|
|
_ = json.Unmarshal(raw, &details)
|
|
return details.Outcome, details.TaskID
|
|
}
|
|
|
|
// claimRun claims a queued run as a daemon does and returns what its prompt
|
|
// gets from wakeups that joined it. Only a daemon that advertises
|
|
// joined-wakeups-v1 takes them along.
|
|
func claimRun(t *testing.T, taskID string, rendersJoined bool) string {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
dbfx.Exec(t, `UPDATE agent_task_queue SET status = 'dispatched', dispatched_at = clock_timestamp() WHERE id = $1`, taskID)
|
|
task, err := testHandler.Queries.GetAgentTask(ctx, parseUUID(taskID))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runtime, err := testHandler.Queries.GetAgentRuntime(ctx, task.RuntimeID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := newOwnerDaemonRequest("POST", "/claim", nil)
|
|
if rendersJoined {
|
|
req.Header.Set("X-Client-Capabilities", protocol.DaemonCapabilityJoinedWakeupsV1)
|
|
}
|
|
resp, _, _, _, _, failure := testHandler.buildClaimedTaskResponse(req, &task, runtime, uuidToString(task.RuntimeID), testWorkspaceID)
|
|
if failure != nil {
|
|
t.Fatalf("claim: %+v", failure)
|
|
}
|
|
return resp.WakeupJoined
|
|
}
|
|
|
|
// startRun marks a claimed run started, as the daemon's start call does.
|
|
func startRun(t *testing.T, taskID string) {
|
|
t.Helper()
|
|
dbfx.Exec(t, `UPDATE agent_task_queue SET status = 'running', started_at = clock_timestamp() WHERE id = $1`, taskID)
|
|
}
|
|
|
|
func createRule(t *testing.T, issueID, agentID string, source pgtype.UUID, in service.WakeupInput) string {
|
|
t.Helper()
|
|
in.AgentID = agentID
|
|
w, err := (&service.IssueWakeupService{Tasks: testHandler.TaskService}).Create(context.Background(), parseUUID(issueID), parseUUID(testUserID), source, in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return uuidToString(w.ID)
|
|
}
|
|
|
|
// #8849: the parent's agent closes the last sub-issue of a stage from its own
|
|
// run on the parent. That run knows, so the rule records the stage without
|
|
// queuing a second run; once that run has ended, or when someone else closes
|
|
// the stage, the agent is woken as before.
|
|
func TestChildDoneDoesNotWakeTheRunThatClosedTheStage(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
runStatus string
|
|
byAgent bool
|
|
wantRuns int
|
|
wantEntry string
|
|
}{
|
|
{"own run still going", "running", true, 0, "acknowledged"},
|
|
{"own run already ended", "completed", true, 1, "woke"},
|
|
{"someone else closed it", "running", false, 1, "woke"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fx := newChildDoneFixture(t, "in_progress")
|
|
agentID := handlerTestAgentID(t)
|
|
setIssueAssigneeDirect(t, fx.parent.ID, "agent", agentID)
|
|
run := dbfx.Task(t, agentID, testutil.Cols{"issue_id": fx.parent.ID, "runtime_id": testRuntimeID, "status": tc.runStatus})
|
|
if tc.byAgent {
|
|
setStatusAsRun(t, fx.child.ID, "done", agentID, run)
|
|
} else {
|
|
updateChildStatus(t, fx.child.ID, "done")
|
|
}
|
|
if runs := childDoneRuns(t, fx.parent.ID); len(runs) != tc.wantRuns {
|
|
t.Fatalf("runs = %+v, want %d", runs, tc.wantRuns)
|
|
}
|
|
if entries := childDoneEntries(t, fx.parent.ID); len(entries) != 1 || entries[0].Outcome != tc.wantEntry {
|
|
t.Fatalf("entries = %+v, want %s", entries, tc.wantEntry)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A comment from the agent itself never wakes it; a member's comment does.
|
|
func TestWakeupSkipsTheAgentsOwnComment(t *testing.T) {
|
|
issue := dbfx.Issue(t, "own comment")
|
|
t.Cleanup(func() { cleanupChildDoneIssue(issue) })
|
|
agentID := handlerTestAgentID(t)
|
|
rule := createRule(t, issue, agentID, pgtype.UUID{}, service.WakeupInput{Instruction: "Reply to new comments", Kind: "event", Mode: "continuous", EventTypes: []string{"comment.created"}})
|
|
run := dbfx.Task(t, agentID, testutil.Cols{"issue_id": issue, "runtime_id": testRuntimeID, "status": "running"})
|
|
|
|
commentOn(t, issue, "Deployed the fix.", agentID, run)
|
|
runWakeupTick(t)
|
|
if n := wakeupRunsOf(t, rule); n != 0 {
|
|
t.Fatalf("the agent's own comment started %d runs", n)
|
|
}
|
|
if outcome, _ := lastWakeupOutcome(t, issue); outcome != "acknowledged" {
|
|
t.Fatalf("outcome = %q, want acknowledged", outcome)
|
|
}
|
|
|
|
commentOn(t, issue, "Can you double-check staging?", "", "")
|
|
runWakeupTick(t)
|
|
if n := wakeupRunsOf(t, rule); n != 1 {
|
|
t.Fatalf("a member's comment started %d runs, want 1", n)
|
|
}
|
|
}
|
|
|
|
// A rule that fires while its agent already has a run waiting on the issue
|
|
// keeps its inputs for that run: no second run, and the run carries the
|
|
// instruction and every fact once it is claimed.
|
|
func TestWakeupJoinsTheAgentsWaitingRun(t *testing.T) {
|
|
issue := dbfx.Issue(t, "join waiting run")
|
|
t.Cleanup(func() { cleanupChildDoneIssue(issue) })
|
|
agentID := handlerTestAgentID(t)
|
|
setIssueAssigneeDirect(t, issue, "agent", agentID)
|
|
waiting := dbfx.Task(t, agentID, testutil.Cols{"issue_id": issue, "runtime_id": testRuntimeID, "status": "queued", "originator_user_id": testUserID, "accountable_user_id": testUserID})
|
|
rule := createRule(t, issue, agentID, pgtype.UUID{}, service.WakeupInput{Instruction: "Summarize the discussion", Kind: "event", Mode: "continuous", EventTypes: []string{"comment.created"}})
|
|
|
|
commentOn(t, issue, "Here is more context.", "", "")
|
|
runWakeupTick(t)
|
|
commentOn(t, issue, "One more thing.", "", "")
|
|
runWakeupTick(t)
|
|
if n := wakeupRunsOf(t, rule); n != 0 {
|
|
t.Fatalf("the rule queued %d runs of its own", n)
|
|
}
|
|
|
|
notes := claimRun(t, waiting, true)
|
|
if strings.Count(notes, "Wakeup "+rule) != 1 || !strings.Contains(notes, "Summarize the discussion") || strings.Count(notes, "comment.created") != 2 {
|
|
t.Fatalf("joined notes = %q", notes)
|
|
}
|
|
startRun(t, waiting)
|
|
runWakeupTick(t)
|
|
outcome, taskID := lastWakeupOutcome(t, issue)
|
|
if outcome != "merged" || taskID != waiting {
|
|
t.Fatalf("outcome = %q task = %q, want merged into %s", outcome, taskID, waiting)
|
|
}
|
|
if n := wakeupRunsOf(t, rule); n != 0 {
|
|
t.Fatalf("the rule queued %d runs of its own", n)
|
|
}
|
|
}
|
|
|
|
// A daemon that cannot render joined wakeups claims the waiting run without
|
|
// them; the rule then starts its own run.
|
|
func TestWakeupRunsOnItsOwnAfterAnOlderDaemonsClaim(t *testing.T) {
|
|
issue := dbfx.Issue(t, "older daemon")
|
|
t.Cleanup(func() { cleanupChildDoneIssue(issue) })
|
|
agentID := handlerTestAgentID(t)
|
|
waiting := dbfx.Task(t, agentID, testutil.Cols{"issue_id": issue, "runtime_id": testRuntimeID, "status": "queued", "originator_user_id": testUserID, "accountable_user_id": testUserID})
|
|
rule := createRule(t, issue, agentID, pgtype.UUID{}, service.WakeupInput{Instruction: "Summarize the discussion", Kind: "event", EventTypes: []string{"comment.created"}})
|
|
|
|
commentOn(t, issue, "Here is more context.", "", "")
|
|
runWakeupTick(t)
|
|
if notes := claimRun(t, waiting, false); notes != "" {
|
|
t.Fatalf("an older daemon's claim got %q", notes)
|
|
}
|
|
runWakeupTick(t)
|
|
if n := wakeupRunsOf(t, rule); n != 1 {
|
|
t.Fatalf("runs of the rule = %d, want 1", n)
|
|
}
|
|
}
|
|
|
|
// A condition the agent's own unfinished run satisfied does not wake it when
|
|
// the agent set up the rule; a person's rule still does, because the running
|
|
// agent does not have that instruction.
|
|
func TestConditionRuleAndTheAgentsOwnChange(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
ownRule bool
|
|
wantRuns int
|
|
}{
|
|
{"rule the agent set up", true, 0},
|
|
{"rule a person set up", false, 1},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
issue := dbfx.Issue(t, "own condition", testutil.Cols{"status": "in_progress"})
|
|
t.Cleanup(func() { cleanupChildDoneIssue(issue) })
|
|
agentID := handlerTestAgentID(t)
|
|
var source pgtype.UUID
|
|
if tc.ownRule {
|
|
earlier := dbfx.Task(t, agentID, testutil.Cols{"issue_id": issue, "runtime_id": testRuntimeID, "status": "completed"})
|
|
source = parseUUID(earlier)
|
|
}
|
|
rule := createRule(t, issue, agentID, source, service.WakeupInput{Instruction: "Run the release checklist", Kind: "event",
|
|
Condition: json.RawMessage(`{"type":"issue_field","field":"status","value":"in_review"}`)})
|
|
runWakeupTick(t) // records the starting state
|
|
run := dbfx.Task(t, agentID, testutil.Cols{"issue_id": issue, "runtime_id": testRuntimeID, "status": "running"})
|
|
setStatusAsRun(t, issue, "in_review", agentID, run)
|
|
runWakeupTick(t)
|
|
if n := wakeupRunsOf(t, rule); n != tc.wantRuns {
|
|
t.Fatalf("runs = %d, want %d", n, tc.wantRuns)
|
|
}
|
|
})
|
|
}
|
|
}
|