Files
multica/server/internal/handler/daemon_skill_resolve_scope_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

378 lines
13 KiB
Go

package handler
import (
"context"
"errors"
"net/http"
"strings"
"sync"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/analytics"
"github.com/multica-ai/multica/server/internal/service"
"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/skillbundle"
)
// The daemon resolves one skill bundle per request (GH #4505), so what the
// resolve endpoint loads per request is multiplied by the agent's skill count
// across a cold dispatch. These tests pin that it loads only the refs it was
// asked for, and that narrowing the read did not narrow what the agent is
// allowed to see -- the junction predicate is now doing the authorization the
// full-set lookup used to do.
// Query markers include the trailing kind so they cannot match a sibling query
// whose name shares a prefix (ListAgentSkills vs ListAgentSkillsByIDs).
const (
queryFullAgentSkills = "-- name: ListAgentSkills :many"
queryScopedAgentSkills = "-- name: ListAgentSkillsByIDs :many"
querySkillFilesByIDs = "-- name: ListSkillFilesBySkillIDs :many"
)
var errInjectedScopedSkillRead = errors.New("injected scoped skill read failure")
// skillQuerySpy passes every statement through to the real pool while
// recording which skill reads ran and how many IDs each was given, so a test
// can assert the request cost and not just the response body. failQuery fails
// one chosen query.
type skillQuerySpy struct {
inner db.DBTX
failQuery string
mu sync.Mutex
fullSkillCalls int
scopedSkillIDs []int
skillFileCallIDs []int
}
func (s *skillQuerySpy) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
return s.inner.Exec(ctx, sql, args...)
}
func (s *skillQuerySpy) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
return s.inner.QueryRow(ctx, sql, args...)
}
func (s *skillQuerySpy) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
switch {
case strings.Contains(sql, queryScopedAgentSkills):
s.record(&s.scopedSkillIDs, uuidArgLen(args, 1))
if s.failQuery == queryScopedAgentSkills {
return nil, errInjectedScopedSkillRead
}
case strings.Contains(sql, queryFullAgentSkills):
s.mu.Lock()
s.fullSkillCalls++
s.mu.Unlock()
case strings.Contains(sql, querySkillFilesByIDs):
s.record(&s.skillFileCallIDs, uuidArgLen(args, 0))
}
return s.inner.Query(ctx, sql, args...)
}
func (s *skillQuerySpy) record(into *[]int, n int) {
s.mu.Lock()
*into = append(*into, n)
s.mu.Unlock()
}
func (s *skillQuerySpy) snapshot() (fullCalls int, scoped, files []int) {
s.mu.Lock()
defer s.mu.Unlock()
return s.fullSkillCalls, append([]int(nil), s.scopedSkillIDs...), append([]int(nil), s.skillFileCallIDs...)
}
func uuidArgLen(args []any, i int) int {
if i >= len(args) {
return -1
}
ids, ok := args[i].([]pgtype.UUID)
if !ok {
return -1
}
return len(ids)
}
func newSpyHandler(t *testing.T, spy *skillQuerySpy) *Handler {
t.Helper()
return New(db.New(spy), testPool, testHandler.Hub, testHandler.Bus, testHandler.EmailService,
nil, nil, analytics.NoopClient{}, Config{})
}
// resolveScopeFixture creates a runtime, a dispatched task, and an agent
// carrying one skill per name in skillNames (each with one supporting file).
func resolveScopeFixture(t *testing.T, ctx context.Context, name string, skillNames ...string) (runtimeID, taskID string, skillIDs []string) {
t.Helper()
runtimeID = createClaimReclaimRuntime(t, ctx, name+" runtime")
agentID, issueID := createClaimReclaimAgentAndIssue(t, ctx, runtimeID, name+" agent")
for _, skillName := range skillNames {
skillID := dbfx.Insert(t, "skill", testutil.Cols{
"workspace_id": testWorkspaceID,
"name": skillName,
"description": "Resolve scope fixture",
"content": skillName + " content",
"config": testutil.Raw("'{}'::jsonb"),
"created_by": testUserID,
})
dbfx.InsertNoID(t, "skill_file", testutil.Cols{
"skill_id": skillID,
"path": "rules.md",
"content": skillName + " rules",
}, "skill_id = $1", skillID)
dbfx.InsertNoID(t, "agent_skill", testutil.Cols{
"agent_id": agentID,
"skill_id": skillID,
}, "skill_id = $1", skillID)
skillIDs = append(skillIDs, skillID)
}
taskID = dbfx.Task(t, agentID, testutil.Cols{
"runtime_id": runtimeID,
"issue_id": issueID,
"status": "dispatched",
"dispatched_at": testutil.Raw("now()"),
})
return runtimeID, taskID, skillIDs
}
func resolveBundles(t *testing.T, h *Handler, runtimeID, taskID string, refs ...resolveSkillBundleRef) *testutil.Response {
t.Helper()
req := newOwnerDaemonRequest("POST",
"/api/daemon/runtimes/"+runtimeID+"/tasks/"+taskID+"/skill-bundles/resolve",
resolveSkillBundlesRequest{Skills: refs})
req = withURLParams(req, "runtimeId", runtimeID, "taskId", taskID)
return testutil.Call(t, h.ResolveTaskSkillBundles, req)
}
func workspaceRef(skillID string) resolveSkillBundleRef {
return resolveSkillBundleRef{ID: skillID, Source: skillbundle.SourceWorkspace, Hash: "sha256:whatever"}
}
// TestResolveTaskSkillBundles_LoadsOnlyTheRequestedSkill is the point of the
// change: an agent with three skills asks for one, and the server must read
// and hash one. Before, every resolve request loaded the agent's entire skill
// set to pick a single bundle out of it, so a cold dispatch of N skills cost N
// full loads.
func TestResolveTaskSkillBundles_LoadsOnlyTheRequestedSkill(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
ctx := context.Background()
spy := &skillQuerySpy{inner: testPool}
runtimeID, taskID, skillIDs := resolveScopeFixture(t, ctx, "resolvescope",
"resolvescope-alpha", "resolvescope-beta", "resolvescope-gamma")
var resp struct {
Bundles []service.AgentSkillData `json:"bundles"`
}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID, workspaceRef(skillIDs[1])).
Want(http.StatusOK).JSON(&resp)
if len(resp.Bundles) != 1 || resp.Bundles[0].ID != skillIDs[1] {
t.Fatalf("resolved %+v, want exactly the requested skill %s", resp.Bundles, skillIDs[1])
}
if len(resp.Bundles[0].Files) != 1 || resp.Bundles[0].Files[0].Content != "resolvescope-beta rules" {
t.Fatalf("resolved bundle lost its files: %+v", resp.Bundles[0].Files)
}
if resp.Bundles[0].Hash == "" {
t.Fatal("resolved bundle has no hash")
}
fullCalls, scoped, files := spy.snapshot()
if fullCalls != 0 {
t.Fatalf("the full ListAgentSkills ran %d times; resolve must not load the agent's whole skill set", fullCalls)
}
if len(scoped) != 1 || scoped[0] != 1 {
t.Fatalf("scoped skill query calls = %v, want exactly one call for one id", scoped)
}
if len(files) != 1 || files[0] != 1 {
t.Fatalf("skill file query calls = %v, want exactly one call for one id", files)
}
}
// TestResolveTaskSkillBundles_BuiltinRefTouchesNoWorkspaceQuery pins the other
// half: built-ins live in the binary, so resolving one must not reach the
// database at all.
func TestResolveTaskSkillBundles_BuiltinRefTouchesNoWorkspaceQuery(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
ctx := context.Background()
spy := &skillQuerySpy{inner: testPool}
runtimeID, taskID, _ := resolveScopeFixture(t, ctx, "resolvebuiltin", "resolvebuiltin-alpha")
// The unscoped set: resolution deliberately serves any built-in by id.
builtins := testHandler.TaskService.AllBuiltinSkills()
if len(builtins) == 0 {
t.Skip("no builtin skills embedded in this build")
}
want := builtins[0]
var resp struct {
Bundles []service.AgentSkillData `json:"bundles"`
}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID, resolveSkillBundleRef{
ID: service.BuiltinSkillID(want.Name),
Source: skillbundle.SourceBuiltin,
Hash: "sha256:whatever",
}).Want(http.StatusOK).JSON(&resp)
if len(resp.Bundles) != 1 || resp.Bundles[0].Name != want.Name || resp.Bundles[0].Content != want.Content {
t.Fatalf("resolved %+v, want builtin %q", resp.Bundles, want.Name)
}
fullCalls, scoped, files := spy.snapshot()
if fullCalls != 0 || len(scoped) != 0 || len(files) != 0 {
t.Fatalf("builtin resolve hit the database: full=%d scoped=%v files=%v", fullCalls, scoped, files)
}
}
// TestResolveTaskSkillBundles_KeepsRequestOrderAndDeduplicates: the scoped
// query orders by name, so response order can no longer fall out of the load.
// It must follow the request, and a repeated ref must not be loaded twice.
func TestResolveTaskSkillBundles_KeepsRequestOrderAndDeduplicates(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
ctx := context.Background()
spy := &skillQuerySpy{inner: testPool}
// Named so the query's name ASC order is the reverse of the request order.
runtimeID, taskID, skillIDs := resolveScopeFixture(t, ctx, "resolveorder",
"resolveorder-zulu", "resolveorder-alpha")
zulu, alpha := skillIDs[0], skillIDs[1]
var resp struct {
Bundles []service.AgentSkillData `json:"bundles"`
}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID,
workspaceRef(zulu), workspaceRef(alpha), workspaceRef(zulu)).
Want(http.StatusOK).JSON(&resp)
got := []string{}
for _, bundle := range resp.Bundles {
got = append(got, bundle.ID)
}
want := []string{zulu, alpha, zulu}
if len(got) != len(want) {
t.Fatalf("resolved %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("resolved %v, want request order %v", got, want)
}
}
_, scoped, files := spy.snapshot()
if len(scoped) != 1 || scoped[0] != 2 {
t.Fatalf("scoped skill query calls = %v, want one call for 2 distinct ids (the repeated ref must not load twice)", scoped)
}
if len(files) != 1 || files[0] != 2 {
t.Fatalf("skill file query calls = %v, want one call for 2 ids", files)
}
}
// TestResolveTaskSkillBundles_RefusesSkillsTheAgentCannotSee is the reason the
// narrowed read is safe: the old code authorized by membership in the agent's
// full bundle set, and the new query has to reject exactly the same refs.
func TestResolveTaskSkillBundles_RefusesSkillsTheAgentCannotSee(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
ctx := context.Background()
runtimeID, taskID, skillIDs := resolveScopeFixture(t, ctx, "resolvedeny", "resolvedeny-owned")
agentID := ""
dbfx.QueryRow(t, `SELECT agent_id::text FROM agent_task_queue WHERE id = $1`, taskID).Scan(&agentID)
// A workspace skill nobody assigned to this agent.
unassigned := dbfx.Insert(t, "skill", testutil.Cols{
"workspace_id": testWorkspaceID,
"name": "resolvedeny-unassigned",
"description": "",
"content": "unassigned content",
"config": testutil.Raw("'{}'::jsonb"),
"created_by": testUserID,
})
// Assigned, but the assignment is disabled.
disabled := dbfx.Insert(t, "skill", testutil.Cols{
"workspace_id": testWorkspaceID,
"name": "resolvedeny-disabled",
"description": "",
"content": "disabled content",
"config": testutil.Raw("'{}'::jsonb"),
"created_by": testUserID,
})
dbfx.InsertNoID(t, "agent_skill", testutil.Cols{
"agent_id": agentID,
"skill_id": disabled,
"enabled": false,
}, "skill_id = $1", disabled)
// A skill owned by a different workspace.
foreignWorkspace := dbfx.Insert(t, "workspace", testutil.Cols{
"name": "Resolve deny foreign",
"slug": "resolve-deny-foreign",
"description": "",
"issue_prefix": "RDF",
})
foreign := dbfx.Insert(t, "skill", testutil.Cols{
"workspace_id": foreignWorkspace,
"name": "resolvedeny-foreign",
"description": "",
"content": "foreign content",
"config": testutil.Raw("'{}'::jsonb"),
"created_by": testUserID,
})
tests := []struct {
name string
ref resolveSkillBundleRef
}{
{name: "skill not assigned to the agent", ref: workspaceRef(unassigned)},
{name: "assignment disabled", ref: workspaceRef(disabled)},
{name: "skill owned by another workspace", ref: workspaceRef(foreign)},
{name: "unparseable skill id", ref: workspaceRef("not-a-uuid")},
{name: "source with no server-side producer", ref: resolveSkillBundleRef{
ID: skillIDs[0], Source: skillbundle.SourcePlugin, Hash: "sha256:whatever",
}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
spy := &skillQuerySpy{inner: testPool}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID, tc.ref).Want(http.StatusNotFound)
})
}
// The agent's own skill still resolves, so the cases above are denials and
// not a fixture that could never resolve anything.
spy := &skillQuerySpy{inner: testPool}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID, workspaceRef(skillIDs[0])).Want(http.StatusOK)
}
// TestResolveTaskSkillBundles_ScopedReadFailureReturns500 extends the
// fail-closed rule to the new query: a failed read must not answer 404, which
// the daemon would treat as a permanent "this skill is gone" rather than
// retrying.
func TestResolveTaskSkillBundles_ScopedReadFailureReturns500(t *testing.T) {
if testHandler == nil || testPool == nil {
t.Skip("database not available")
}
ctx := context.Background()
runtimeID, taskID, skillIDs := resolveScopeFixture(t, ctx, "resolvereadfail", "resolvereadfail-alpha")
spy := &skillQuerySpy{inner: testPool, failQuery: queryScopedAgentSkills}
resolveBundles(t, newSpyHandler(t, spy), runtimeID, taskID, workspaceRef(skillIDs[0])).
Want(http.StatusInternalServerError)
}