Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-task-mcp-lease-token-renewal.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion src/docs/contributor-relay.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/docs/design/task-mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/dashboard/contribute_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/dashboard/contribute_standby_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
22 changes: 22 additions & 0 deletions src/pkg/dashboard/task_mcp_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading