diff --git a/changelog.d/fixed-task-mcp-lease-token-renewal.md b/changelog.d/fixed-task-mcp-lease-token-renewal.md new file mode 100644 index 0000000000..27777458c1 --- /dev/null +++ b/changelog.d/fixed-task-mcp-lease-token-renewal.md @@ -0,0 +1 @@ +- Fix the task MCP going dead 30 minutes into a remote contributor's task on hives with `task_mcp.remote_enabled`: the per-task MCP token expired with the first task-lease window even though the hub kept renewing the lease, so every task that ran longer got HTTP 403 from its task MCP for the rest of the task. The token now works for as long as the hub's lease on the task is alive, is still refused as soon as the task is released, completed, failed, yanked or its lease lapses, and has a hard cap of 24 hours after assignment. diff --git a/src/docs/contributor-relay.md b/src/docs/contributor-relay.md index dd9a9cfaa9..81cc7a2e35 100644 --- a/src/docs/contributor-relay.md +++ b/src/docs/contributor-relay.md @@ -21,7 +21,7 @@ You do not need the operator or wire-protocol sections to start one relay. In th The hub grants task context only for the task it assigned. When `task_mcp.remote_enabled` is on, `task_assign` includes a lease-scoped MCP server (`mcp.url`, `mcp.token`, `mcp.expires_at`). The relay writes it as a standard `mcpServers.hive_task` config for the launched CLI and omits the token from the debug task file. -That MCP lease is read-only and bound to the task id, contributor session identity, repo, issue/PR number, and task-lease expiry. It can call `task_context`, `related_work`, `ci_health`, and `context_bundle` for that task's repo and related issue/PR set only. Asking for another repo or task returns a typed refusal in the tool's `data` field and is audited by the hub. Releasing, completing, failing, yanking, or expiring the task revokes the MCP token with the task lease. +That MCP lease is read-only and bound to the task id, contributor session identity, repo, issue/PR number, and the hub's task lease. It keeps working for as long as the hub keeps that lease alive, which every progress report from your relay does, up to `mcp.expires_at`, a hard cap 24 hours after assignment. It can call `task_context`, `related_work`, `ci_health`, and `context_bundle` for that task's repo and related issue/PR set only. Asking for another repo or task returns a typed refusal in the tool's `data` field and is audited by the hub. Releasing, completing, failing, yanking, or expiring the task revokes the MCP token with the task lease. ## Basic setup diff --git a/src/docs/design/task-mcp.md b/src/docs/design/task-mcp.md index 474264cf08..ac204f6368 100644 --- a/src/docs/design/task-mcp.md +++ b/src/docs/design/task-mcp.md @@ -25,7 +25,9 @@ Prompt-injection handling is structural: issue and PR text is only returned insi ## Phase 2: lease-scoped remote contributors -Remote contributor relays can opt in with `task_mcp.remote_enabled: true`. When a task is assigned, the hub mints an HMAC-signed lease bearer bound to the assignment tuple: task id, contributor session identity, repository, issue/PR number, the run stage present on the lease at mint time, and the same expiry as the hub's task lease. Unstaged leases leave the token stage empty. The lease token is stored by hash next to the task lease, along with the minted stage, so releasing, expiring, yanking, completing, or failing the task revokes MCP access with the same lifecycle as task ownership. +Remote contributor relays can opt in with `task_mcp.remote_enabled: true`. When a task is assigned, the hub mints an HMAC-signed lease bearer bound to the assignment tuple: task id, contributor session identity, repository, issue/PR number, and the run stage present on the lease at mint time. Unstaged leases leave the token stage empty. The lease token is stored by hash next to the task lease, along with the minted stage, so releasing, expiring, yanking, completing, or failing the task revokes MCP access with the same lifecycle as task ownership. + +The token is minted once and never re-sent, so its lifetime follows the task lease instead of a clock of its own. The lease starts with a 30-minute window and every accepted `task_progress` renews it; the token is accepted for as long as that lease record is unexpired and still holds this token's id and hash, which is what keeps the task MCP reachable for a task that runs longer than one window. The token's own `expires_at` is only a hard upper bound, 24 hours after assignment (`config.DefaultTaskMCPLaunchTokenTTL`, the value hub-launched tokens default to; `task_mcp.launch_token_ttl` does not change it for lease tokens). Past that bound the token is refused even while the lease is still being renewed; before it the token is refused as soon as the lease is released, expires unrenewed, is recorded again, or has a newer token minted for it. A lease token therefore never outlives its lease. A task still running at the bound keeps its lease but loses its task MCP; the reference relay ends a task after `HIVE_ABSOLUTE_TASK_DEADLINE_MS` (4 hours by default), so only a relay set to work one task for more than a day reaches it. The `mcp.expires_at` a relay receives in `task_assign` (below) is this upper bound, not the end of the current lease window: it does not move when the lease is renewed, and the token can stop working before it. `task_assign` now has an optional `mcp` block: diff --git a/src/pkg/dashboard/contribute_select.go b/src/pkg/dashboard/contribute_select.go index 00aeb4f38c..5b46bf9512 100644 --- a/src/pkg/dashboard/contribute_select.go +++ b/src/pkg/dashboard/contribute_select.go @@ -1423,7 +1423,7 @@ func (h *ContributeWSHub) selectTaskPass(c *ContributorConnection, skippedUnmint c.mu.Unlock() turnEnvelopeID := h.persistTurnEnvelopeForAssignment(c, assignment, gen, prompt, chosen.labels) - mcp := h.mintTaskMCPForAssignment(identityOf(c), assignment, assignedAt.Add(leaseTTL), c.profile.GitHubUsername) + mcp := h.mintTaskMCPForAssignment(identityOf(c), assignment, assignedAt.Add(taskMCPLeaseTokenMaxTTL), c.profile.GitHubUsername) // #8380: assert the issue claim for this lease — on the issue itself when // the tier may write comments, on the lease alone otherwise. Runs after the diff --git a/src/pkg/dashboard/contribute_standby_dispatch.go b/src/pkg/dashboard/contribute_standby_dispatch.go index 9b6d7ae0cc..d3c82a14ac 100644 --- a/src/pkg/dashboard/contribute_standby_dispatch.go +++ b/src/pkg/dashboard/contribute_standby_dispatch.go @@ -337,7 +337,7 @@ func (h *ContributeWSHub) assignStandbyTask(c *ContributorConnection, item Ready c.mu.Unlock() turnEnvelopeID := h.persistTurnEnvelopeForAssignment(c, assignment, gen, prompt, item.Labels) - mcp := h.mintTaskMCPForAssignment(identityOf(c), assignment, assignedAt.Add(leaseTTL), c.profile.GitHubUsername) + mcp := h.mintTaskMCPForAssignment(identityOf(c), assignment, assignedAt.Add(taskMCPLeaseTokenMaxTTL), c.profile.GitHubUsername) msg := &WSMessage{ Type: "task_assign", Seq: h.nextSeq(), diff --git a/src/pkg/dashboard/task_mcp_lease.go b/src/pkg/dashboard/task_mcp_lease.go index e2a57b734e..cec069c120 100644 --- a/src/pkg/dashboard/task_mcp_lease.go +++ b/src/pkg/dashboard/task_mcp_lease.go @@ -83,6 +83,22 @@ func (h *ContributeWSHub) allowTaskMCPCall(leaseID string, now time.Time) bool { return true } +// taskMCPLeaseTokenMaxTTL is the hard cap on a remote contributor's task MCP +// lease token, measured from assignment. It is deliberately NOT the task lease +// window: the lease is renewed on every accepted task_progress (renewLease) +// while the token is minted once and never re-sent, so a token that expired +// with the first window cut a healthy task off from its task MCP at minute 30. +// Whether the token is accepted before the cap is decided by the server-side +// lease record alone (authenticateTaskMCPLease), so the token never outlives +// its lease; the cap only bounds how long one minted token can be presented at +// all, however long the lease is kept alive. The value is the launch-token +// default; the launch-token TTL setting configures hub launches only. +const taskMCPLeaseTokenMaxTTL = config.DefaultTaskMCPLaunchTokenTTL + +// mintTaskMCPForAssignment mints the lease token for one assignment and binds +// it, by id and hash, to the lease recorded for that task. expiresAt is the +// token's own hard cap - assignment plus taskMCPLeaseTokenMaxTTL - not the end +// of the lease window. func (h *ContributeWSHub) mintTaskMCPForAssignment(identity string, assign *WSTaskAssign, expiresAt time.Time, contributor string) *WSTaskMCP { if h == nil || h.server == nil || assign == nil || !h.server.taskMCPRemoteEnabled() { return nil @@ -117,6 +133,12 @@ func (h *ContributeWSHub) mintTaskMCPForAssignment(identity string, assign *WSTa return &WSTaskMCP{URL: endpoint, Token: token, ExpiresAt: expiresAt.UTC().Format(time.RFC3339)} } +// authenticateTaskMCPLease accepts a remote contributor's lease token only +// while the server-side lease it was minted for still backs it: a record for +// the token's {identity, task} exists, has not expired, and still carries this +// token's id and hash (plus repo, number and stage). The token's own expiry is +// only the hard cap, so these checks are what revokes it - on release, lease +// expiry, a lease recorded again, or a newer token minted for the same lease. func (s *Server) authenticateTaskMCPLease(r *http.Request) (*http.Request, bool) { if !s.taskMCPRemoteEnabled() { return r, false diff --git a/src/pkg/dashboard/task_mcp_lease_lifetime_test.go b/src/pkg/dashboard/task_mcp_lease_lifetime_test.go new file mode 100644 index 0000000000..301f005050 --- /dev/null +++ b/src/pkg/dashboard/task_mcp_lease_lifetime_test.go @@ -0,0 +1,368 @@ +package dashboard + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/crux/taskmcp" + "github.com/hivecommons/hive/pkg/config" +) + +// task_mcp_lease_lifetime_test.go pins how long a remote contributor's task +// MCP lease token works. The hub mints it once, at assignment, and never +// re-sends it; the task lease it is bound to is renewed by every accepted +// task_progress. The token used to carry the FIRST lease window as its own +// expiry, so a healthy task lost its task MCP (HTTP 403) thirty minutes after +// assignment. Its own expiry is now only a hard cap; before the cap the +// server-side lease record decides, and these tests cover both directions. + +const leaseLifetimeSecret = "lease-lifetime-secret" + +// leaseLifetimeHardCap is the documented upper bound on a lease token's own +// expiry, measured from assignment. It is stated here rather than read from +// the hub's constant so that moving the bound is a deliberate edit. +const leaseLifetimeHardCap = 24 * time.Hour + +func leaseLifetimeConfig() *config.Config { + return &config.Config{ + TaskMCP: config.TaskMCPConfig{RemoteEnabled: true}, + Dashboard: config.DashboardConfig{PublicURL: "https://hive.example"}, + } +} + +func newLeaseLifetimeServer(t *testing.T) *Server { + t.Helper() + // #8287: a grant whose lease cannot be persisted is refused, so the + // registry lives in a temp dir rather than under /data. + dir := t.TempDir() + s := newTestServer() + s.authToken = leaseLifetimeSecret + s.deps = &Dependencies{Config: leaseLifetimeConfig()} + s.contributeHub = NewContributeWSHub(s.logger, s) + s.contributeHub.taskLeasesFile = filepath.Join(dir, "task-leases.json") + t.Cleanup(s.contributeHub.Close) + return s +} + +// leaseLifetimeCall asks the task MCP endpoint for task_context as bearer. +func leaseLifetimeCall(s *Server, bearer string) *httptest.ResponseRecorder { + const call = `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"task_context","arguments":{}}}` + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, taskmcp.EndpointPath, strings.NewReader(call)) + req.Header.Set("Authorization", "Bearer "+bearer) + s.handleContributeMCP(rec, req) + return rec +} + +// leaseLifetimeAssignedAt is when the hub recorded taskID as assigned. +func leaseLifetimeAssignedAt(t *testing.T, h *ContributeWSHub, taskID string) time.Time { + t.Helper() + h.mu.RLock() + defer h.mu.RUnlock() + for _, c := range h.connections { + c.mu.Lock() + held, at := c.currentTask != nil && c.currentTask.TaskID == taskID, c.taskAssignedAt + c.mu.Unlock() + if held { + return at + } + } + t.Fatalf("no connection holds task %s", taskID) + return time.Time{} +} + +// leaseLifetimeAssignOverRelay drives a real ready -> task_assign round trip +// on a hub with task_mcp.remote_enabled, and returns the assignment and when +// the hub recorded it. +func leaseLifetimeAssignOverRelay(t *testing.T) (WSMessage, time.Time) { + t.Helper() + s, ts := setupWSTest(t) + t.Cleanup(ts.Close) + s.authToken = leaseLifetimeSecret + s.deps = &Dependencies{Config: leaseLifetimeConfig()} + s.contributeHub.server = s + seedOneIssue(s, 4242, "task that runs longer than one lease window") + + conn, _ := registerAndAuth(t, s, ts, "long-task-relay") + t.Cleanup(func() { _ = conn.Close() }) + if err := conn.WriteJSON(WSMessage{Type: "ready", Seq: 1}); err != nil { + t.Fatalf("ready: %v", err) + } + assign := readMsg(t, conn) + if assign.Type != "task_assign" || assign.MCP == nil || assign.MCP.Token == "" { + t.Fatalf("expected task_assign with an mcp block, got type=%s reason=%q mcp=%v", assign.Type, assign.Reason, assign.MCP != nil) + } + return assign, leaseLifetimeAssignedAt(t, s.contributeHub, assign.TaskID) +} + +// leaseTokenLifetimeAtAssignment is how long after assignment the hub lets a +// lease token be presented. It is measured on a real assignment rather than +// restated here, so a test that ages a task follows whatever selectTask +// stamps into the token. +func leaseTokenLifetimeAtAssignment(t *testing.T) time.Duration { + t.Helper() + assign, assignedAt := leaseLifetimeAssignOverRelay(t) + claims, err := taskmcp.VerifyLeaseToken([]byte(leaseLifetimeSecret), assign.MCP.Token, assignedAt) + if err != nil { + t.Fatalf("the token in task_assign does not verify at assignment: %v", err) + } + return claims.ExpiresAt.Sub(assignedAt) +} + +// assertLeaseTokenOutlivesFirstLeaseWindow checks the token an assignment +// handed out against the clock it will be presented on: still accepted after +// the first lease window (a renewed lease is alive then), refused past the +// hard cap, and advertised with that same cap in the mcp block. +func assertLeaseTokenOutlivesFirstLeaseWindow(t *testing.T, mcp *WSTaskMCP, assignedAt time.Time) { + t.Helper() + if mcp == nil || mcp.Token == "" { + t.Fatal("the assignment carried no mcp block") + } + secret := []byte(leaseLifetimeSecret) + for _, after := range []time.Duration{leaseTTL + time.Minute, leaseLifetimeHardCap - time.Minute} { + if _, err := taskmcp.VerifyLeaseToken(secret, mcp.Token, assignedAt.Add(after)); err != nil { + t.Errorf("%s after assignment the token is rejected (%v); the lease is renewed past its first %s window and the token is never re-sent", after, err, leaseTTL) + } + } + if _, err := taskmcp.VerifyLeaseToken(secret, mcp.Token, assignedAt.Add(leaseLifetimeHardCap+time.Minute)); !errors.Is(err, taskmcp.ErrLeaseTokenExpired) { + t.Errorf("%s after assignment: err = %v, want %v", leaseLifetimeHardCap+time.Minute, err, taskmcp.ErrLeaseTokenExpired) + } + claims, err := taskmcp.VerifyLeaseToken(secret, mcp.Token, assignedAt) + if err != nil { + t.Fatalf("the token does not verify at assignment: %v", err) + } + if want := claims.ExpiresAt.Format(time.RFC3339); mcp.ExpiresAt != want { + t.Errorf("mcp.expires_at = %q, want the token's own expiry %q", mcp.ExpiresAt, want) + } +} + +func TestTaskAssignMCPTokenOutlivesFirstLeaseWindow(t *testing.T) { + assign, assignedAt := leaseLifetimeAssignOverRelay(t) + assertLeaseTokenOutlivesFirstLeaseWindow(t, assign.MCP, assignedAt) +} + +func TestStandbyDispatchMCPTokenOutlivesFirstLeaseWindow(t *testing.T) { + cfg := standbyAutoDispatchConfig(true, "T2", 1) + cfg.TaskMCP.RemoteEnabled = true + cfg.Dashboard.PublicURL = "https://hive.example" + s, _ := standbyAutoDispatchHarness(t, cfg) + s.authToken = leaseLifetimeSecret + + msg, err := s.contributeHub.DispatchStandby("quality", "") + if err != nil || msg == nil || msg.Type != "task_assign" { + t.Fatalf("DispatchStandby = %#v, %v; want a task_assign", msg, err) + } + assertLeaseTokenOutlivesFirstLeaseWindow(t, msg.MCP, leaseLifetimeAssignedAt(t, s.contributeHub, msg.TaskID)) +} + +// leaseLifetimeTask is one task as the hub holds it mid-flight: who holds it, +// the assignment, and the lease token minted for it. +type leaseLifetimeTask struct { + identity string + assign *WSTaskAssign + token string +} + +// startLeaseLifetimeTask puts owner/repo#42 on the hub the way an assignment +// at assignedAt does - a connection holding it, the server-side lease, and a +// lease token expiring at tokenExpiry - and then renews the lease as the +// relay's progress report thirty seconds ago did. The hub reads the wall +// clock, so the stamps are aged rather than the test slept. +func startLeaseLifetimeTask(t *testing.T, s *Server, username string, assignedAt, tokenExpiry time.Time) leaseLifetimeTask { + t.Helper() + h := s.contributeHub + task := leaseLifetimeTask{ + identity: username + "-id", + assign: &WSTaskAssign{TaskID: fmt.Sprintf("ct-owner/repo-42-%d", assignedAt.Unix()), Kind: "issue", Repo: "owner/repo", Number: 42, Title: "leased work"}, + } + h.mu.Lock() + h.connections[username] = &ContributorConnection{ + profile: &ContributorProfile{GitHubUsername: username, ContributorID: task.identity}, + currentTask: task.assign, + currentTaskGen: 2, + taskAssignedAt: assignedAt, + } + h.mu.Unlock() + if err := h.recordLeaseForKey(task.identity, task.assign.TaskID, task.assign.Repo, task.assign.Number, task.assign.identityKey(), "trusted", 2, assignedAt); err != nil { + t.Fatalf("recordLeaseForKey: %v", err) + } + mcp := h.mintTaskMCPForAssignment(task.identity, task.assign, tokenExpiry, username) + if mcp == nil || mcp.Token == "" { + t.Fatal("no mcp block minted") + } + task.token = mcp.Token + if err := h.renewLease(task.identity, task.assign.TaskID, time.Now().Add(-30*time.Second)); err != nil { + t.Fatalf("renewLease: %v", err) + } + if left := task.leaseLeft(h); left <= 0 { + t.Fatalf("setup: the renewed lease must be alive, has %s left", left) + } + return task +} + +// leaseLeft is how much of the task's lease window remains; zero when the hub +// holds no lease for it. +func (task leaseLifetimeTask) leaseLeft(h *ContributeWSHub) time.Duration { + h.leaseMu.Lock() + defer h.leaseMu.Unlock() + l := h.leaseForLocked(task.identity, task.assign.TaskID) + if l == nil { + return 0 + } + return time.Until(l.expiresAt).Round(time.Second) +} + +// ageLease moves the lease's expiry back by d, as if that much wall-clock had +// passed with no progress report. +func (task leaseLifetimeTask) ageLease(t *testing.T, h *ContributeWSHub, d time.Duration) { + t.Helper() + h.leaseMu.Lock() + defer h.leaseMu.Unlock() + l := h.leaseForLocked(task.identity, task.assign.TaskID) + if l == nil { + t.Fatalf("no lease for %s", task.assign.TaskID) + } + l.expiresAt = l.expiresAt.Add(-d) +} + +// The reported defect: a task assigned more than one lease window ago, whose +// relay is still reporting progress, was answered 403. +func TestContributeMCPRenewedLeaseKeepsItsTokenWorking(t *testing.T) { + lifetime := leaseTokenLifetimeAtAssignment(t) + for _, age := range []time.Duration{time.Minute, leaseTTL + time.Minute, 6 * time.Hour} { + t.Run("assigned "+age.String()+" ago", func(t *testing.T) { + s := newLeaseLifetimeServer(t) + assignedAt := time.Now().Add(-age) + task := startLeaseLifetimeTask(t, s, "alice", assignedAt, assignedAt.Add(lifetime)) + + rec := leaseLifetimeCall(s, task.token) + if rec.Code != http.StatusOK { + t.Fatalf("task assigned %s ago still holds its lease (%s left) but the task MCP answers HTTP %d %s", + age, task.leaseLeft(s.contributeHub), rec.Code, strings.TrimSpace(rec.Body.String())) + } + var env struct { + Data taskmcp.TaskContextData `json:"data"` + } + if err := json.Unmarshal([]byte(mcpResultText(t, rec.Body.Bytes())), &env); err != nil { + t.Fatal(err) + } + if env.Data.Assignment.TaskID != task.assign.TaskID { + t.Fatalf("assignment = %#v, want task %s", env.Data.Assignment, task.assign.TaskID) + } + }) + } +} + +// The other half of the rule: a token still inside its own hard cap stops +// working the moment the lease it is bound to stops backing it. +func TestContributeMCPLeaseTokenDiesWithItsLease(t *testing.T) { + cases := []struct { + name string + end func(t *testing.T, s *Server, task leaseLifetimeTask) + }{ + {"task released", func(t *testing.T, s *Server, task leaseLifetimeTask) { + s.contributeHub.revokeLease(task.identity, task.assign.TaskID) + }}, + {"lease expired without renewal", func(t *testing.T, s *Server, task leaseLifetimeTask) { + task.ageLease(t, s.contributeHub, leaseTTL) + }}, + {"expired lease pruned", func(t *testing.T, s *Server, task leaseLifetimeTask) { + task.ageLease(t, s.contributeHub, leaseTTL) + if dropped := s.contributeHub.pruneExpiredLeases(time.Now()); dropped != 1 { + t.Fatalf("pruneExpiredLeases dropped %d leases, want 1", dropped) + } + }}, + {"task reassigned to another contributor", func(t *testing.T, s *Server, task leaseLifetimeTask) { + s.contributeHub.revokeLease(task.identity, task.assign.TaskID) + now := time.Now() + next := startLeaseLifetimeTask(t, s, "bob", now, now.Add(leaseLifetimeHardCap)) + if rec := leaseLifetimeCall(s, next.token); rec.Code != http.StatusOK { + t.Fatalf("the new holder's own token: status = %d, want 200", rec.Code) + } + }}, + {"lease recorded again for the same task", func(t *testing.T, s *Server, task leaseLifetimeTask) { + if err := s.contributeHub.recordLeaseForKey(task.identity, task.assign.TaskID, task.assign.Repo, task.assign.Number, task.assign.identityKey(), "trusted", 3, time.Now()); err != nil { + t.Fatalf("recordLeaseForKey: %v", err) + } + }}, + {"token replaced by a newer one", func(t *testing.T, s *Server, task leaseLifetimeTask) { + newer := s.contributeHub.mintTaskMCPForAssignment(task.identity, task.assign, time.Now().Add(leaseLifetimeHardCap), "alice") + if newer == nil || newer.Token == "" || newer.Token == task.token { + t.Fatal("no replacement token minted") + } + if rec := leaseLifetimeCall(s, newer.Token); rec.Code != http.StatusOK { + t.Fatalf("the replacement token: status = %d, want 200", rec.Code) + } + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + s := newLeaseLifetimeServer(t) + // Two hours in: well past the first lease window, well inside the cap. + assignedAt := time.Now().Add(-2 * time.Hour) + task := startLeaseLifetimeTask(t, s, "alice", assignedAt, assignedAt.Add(leaseLifetimeHardCap)) + if rec := leaseLifetimeCall(s, task.token); rec.Code != http.StatusOK { + t.Fatalf("setup: the token must work while its lease is alive, status = %d", rec.Code) + } + + tc.end(t, s, task) + + if rec := leaseLifetimeCall(s, task.token); rec.Code != http.StatusForbidden { + t.Fatalf("status = %d, want 403: the token outlived its lease (%s left on the hub's lease for this task)", + rec.Code, task.leaseLeft(s.contributeHub)) + } + }) + } +} + +// A lease answers only for the exact token it recorded. A twin the hub's own +// secret signed for the same lease under the same id, but with a later +// expiry, is refused: the cap cannot be stretched by re-signing. +func TestContributeMCPLeaseTokenIsMatchedByHash(t *testing.T) { + s := newLeaseLifetimeServer(t) + assignedAt := time.Now().Add(-2 * time.Hour) + task := startLeaseLifetimeTask(t, s, "alice", assignedAt, assignedAt.Add(leaseLifetimeHardCap)) + secret := []byte(leaseLifetimeSecret) + claims, err := taskmcp.VerifyLeaseToken(secret, task.token, time.Now()) + if err != nil { + t.Fatalf("the leased token does not verify: %v", err) + } + claims.ExpiresAt = claims.ExpiresAt.Add(leaseLifetimeHardCap) + twin, _, err := taskmcp.MintLeaseToken(secret, claims, time.Now()) + if err != nil || twin == task.token { + t.Fatalf("mint twin: token changed = %v, err = %v", twin != task.token, err) + } + if rec := leaseLifetimeCall(s, twin); rec.Code != http.StatusForbidden { + t.Fatalf("twin token with the lease's token id: status = %d, want 403", rec.Code) + } + if rec := leaseLifetimeCall(s, task.token); rec.Code != http.StatusOK { + t.Fatalf("the recorded token: status = %d, want 200", rec.Code) + } +} + +// The cap holds even for a lease that is still being renewed. +func TestContributeMCPLeaseTokenRefusedPastItsHardCap(t *testing.T) { + for _, tc := range []struct { + age time.Duration + want int + }{ + {leaseLifetimeHardCap - time.Hour, http.StatusOK}, + {leaseLifetimeHardCap + time.Hour, http.StatusForbidden}, + } { + t.Run("assigned "+tc.age.String()+" ago", func(t *testing.T) { + s := newLeaseLifetimeServer(t) + assignedAt := time.Now().Add(-tc.age) + task := startLeaseLifetimeTask(t, s, "alice", assignedAt, assignedAt.Add(leaseLifetimeHardCap)) + if rec := leaseLifetimeCall(s, task.token); rec.Code != tc.want { + t.Fatalf("status = %d, want %d (%s left on the lease)", rec.Code, tc.want, task.leaseLeft(s.contributeHub)) + } + }) + } +}