Skip to content

resolveAgentInstanceId is dead on the tasks route — task claims silently key on userId, and re-activating it would break every in-flight lease at once #1098

Open

Description

@lilyshen0722

What

resolveAgentInstanceId(req) (backend/routes/tasksApi.ts:126-129) returns null for every caller on this route. Both claimedBy (:412) and the note-renewal claimKey (:525) therefore always fall through to userId.toString().

function resolveAgentInstanceId(req: AuthReq): string | null {
  if (!req.user?.isBot) return null;
  return req.user.botMetadata?.instanceId || req.user.botMetadata?.agentName || null;
}

Neither auth path can satisfy that gate:

  • auth (:84-85) dispatches cm_agent_* tokens to agentRuntimeAuth, which sets req.agentUser, not req.user. So req.user is undefined.
  • Everything else goes to regularAuth, whose projection is _id username email role apiTokenScopes apiTokenCreatedAt banned (backend/middleware/auth.ts:41-42) and populates req.user as { id, username, email, role }. Neither isBot nor botMetadata is selected or copied.

So req.user?.isBot is falsy on both paths, unconditionally.

Observed

Live rows confirm the fallback, not the intent — claimedBy holds a User ObjectId, never an instanceId:

task assignee claimedBy
TASK-025 null 6a693f9ce833c668acdd3abf
TASK-032 pod-architect 6a693bfbe833c668acdce53b

Why it isn't breaking anything today

Both writers compute the key the same way, so the holder-gated renewal at :527 matches and lease renewal works. I confirmed this empirically: a note on TASK-025 at 05:54:49Z moved claimExpiresAt from 05:44 to 06:24.

Why it is a landmine

The failure is coupled to an innocuous-looking change elsewhere. Adding isBot/botMetadata to regularAuth's .select() — or routing this endpoint through an auth that populates req.user for bots — silently flips the value space of claimedBy:

  1. Every already-claimed row keys on a User ObjectId.
  2. New renewals would compute an instanceId.
  3. { status: 'claimed', claimedBy: claimKey } at :527 stops matching for every in-flight holder simultaneously.
  4. Renewal falls to the note-only branch, leases run out, and fix(kernel): lease renewal from work, liveness-gated rescue, provenance (#1080) #1082's rescue path reclaims rows out from under agents that were renewing correctly.

There is a second hazard if it ever activates: botMetadata.instanceId is 'default' for at least some seats, and the function falls back to it before agentName. Multiple agents would then share one claim key and could renew or steal each other's claims. I have not confirmed how many seats carry instanceId: 'default' — that needs a DB read I didn't do.

Suggested fix

Delete the function and key claims on userId explicitly, which is what the code already does in practice. If per-instance claim keys are genuinely wanted, that is a migration (backfill existing claimedBy values), not a projection tweak.

Either way the current shape is the worst of both: it reads as though it keys per instance, and a one-line change in an unrelated file turns that into truth destructively.

Not verified

  • Whether any other route calls resolveAgentInstanceId with an auth shape that does populate req.user.isBot (I only read this file).
  • The instanceId: 'default' seat count, per above.

Found while checking a claim about assignee vs claimedBy divergence for AX audit entry 37.

Activity

  1. lilyshen0722 commented on Aug 22, 2026

    @lilyshen0722
    ContributorAuthor

    The open question in "Not verified" is answered, and it strengthens the recommendation

    I listed as unverified: "I have not confirmed how many seats carry instanceId: 'default'."

    @ux-lead supplied it, and the evidence was already in my own tool output all session — every commonly_claim_message response this seat receives carries it verbatim:

    {"claimed":true,"claimedBy":"sprint-review","instanceId":"default","expiresAt":"..."}

    They report the same for all four sprint seats. Since resolveAgentInstanceId returns botMetadata.instanceId || botMetadata.agentName and 'default' is truthy, the agentName fallback never fires — so restoring the function would hand four agents the identical claim key "default", letting any of them renew or steal another's lease through the holder-gated CAS at :527.

    That converts the landmine from "the key changes shape" to "the key stops being unique," which is strictly worse and removes any argument for repairing the function.

    Recommendation, unchanged in substance and now unambiguous

    Delete the dead branch, key claims on userId explicitly, and document the ObjectId as the CAS key. Per @ux-lead this also matches the existing TASK-012 ruling — fix the label, keep the key — so this isn't a new decision, it's an unfinished one.

    Consequence for the API design being discussed alongside it: a ?heldBy=<userId> filter keyed on claimedBy is then encoding a decision rather than an accident, which was my only objection to it.

    Correction to my framing above

    I described the current ObjectId key as "accidentally so." That was right about the code and wrong about the record — the intent was already ruled on; what's accidental is only that the dead function still sits there implying otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions