Skip to content

Retry mcp_get_delegation when a lagging replica answers NoSuchDelegation; document the 5-minute delegation cap - #220

Merged
aterga merged 1 commit into
mainfrom
arshavir/ecstatic-ritchie-9zreec
Oct 29, 2026
Merged

aterga merged 1 commit into
mainfrom
arshavir/ecstatic-ritchie-9zreec

Conversation

@aterga

@aterga aterga commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Identities::derive_app_delegation calls II's mcp_prepare_delegation (an update, call_and_wait) and then immediately mcp_get_delegation (a query). The query is answered by a single replica, which may not yet have caught up with the certified state the update produced, so the signature is not in that replica's copy of II's signature map yet and II answers Err(NoSuchDelegation) for a delegation that exists. The server surfaced that as a tool error whose text blamed un-threaded parameters ("the prepared account/expiration were not threaded through") — a stale diagnosis, since the code does thread prepared.account_number and prepared.expiration — and pushed the retry onto the model, which minted a brand-new per-app key and paid for another prepare update each time.

This PR makes the server absorb the transient itself, the way II's own frontend does (retryGetDelegation in fetchDelegation.ts: up to five get_delegation tries with linear backoff on no_such_delegation), corrects the error text for the persistent case, and fixes the docs that still said II's default delegation lifetime is "≤ 1 hour" — II caps MCP delegations at 5 minutes (MCP_MAX_EXPIRATION_PERIOD_NS in src/internet_identity/src/mcp.rs), so with the server's 30 s REDERIVE_MARGIN_NS a cached delegation is reused for at most ~4.5 minutes and an app in continuous use re-derives every few minutes.

Related issues

None filed; this follows up the NoSuchDelegation tool errors seen against beta II.

Changes

  • identities.rs: retry only the mcp_get_delegation query on NoSuchDelegation. New get_delegation_with_retry re-sends the identical request (same per-app key, same prepared account and expiration — nothing is re-prepared) after the waits in GET_DELEGATION_BACKOFF (1 s, 2 s, 3 s: one immediate try plus three retries, six seconds of waiting in all). Any other outcome ends the loop at once: Unauthorized still maps to the reconnect message unretried, an InternalCanisterError is reported as before, and a transport or decode failure propagates as is. Each retry logs at info, exhaustion at warn.
  • map_delegation_error's NoSuchDelegation arm rewritten to describe what actually happened: the delegation prepared a moment ago was not yet visible to the replica that answered, and the server already retried the lookup N times over S seconds (both formatted from the schedule, so the text cannot drift from it). It tells the caller to retry once (that prepares a fresh delegation) and, if that fails the same way, to tell the user and stop.
  • Docs corrected to the 5-minute cap: the module doc, REDERIVE_MARGIN_NS, and the max_ttl comment in identities.rs, and the README's "Domain identities (on demand)" section, now state that II caps MCP delegations at 5 minutes, that the cache therefore re-derives every few minutes, and (README) that get is retried on NoSuchDelegation.
  • Tests: three unit tests drive get_delegation_with_retry over scripted Candid-encoded mcp_get_delegation replies on a paused tokio clock (tokio/test-util added as a dev-dependency of imcp2-core; a feature on a dependency the crate already has, so Cargo.lock is unchanged and --locked still passes): a lag that clears on the third reply returns the delegation after the scheduled waits (no wait before the first try); a lag that never clears stops after one try per schedule slot with the new message (and without the old "threaded" text); Unauthorized, a transport failure, and InternalCanisterError each end after one attempt with no wait, even with a good reply scripted behind them. IiDelegation/IiSignedDelegation now derive Debug so the tests can expect_err on the result.

Not changed: docs/icp-mcp-privacy-policy-draft.md still says per-application authorizations expire "at most one hour" after issue (lines ~170, 278, 354–356). It is a legal draft, so I left it for its author; with the 5-minute cap, the window a revoked grant's already-issued delegations stay usable is ~5 minutes, not an hour.

Testing

  • cargo build --locked --all-targets
  • cargo test --locked --all-targets (workspace: all suites green; imcp2-core 206 passed including the three new retry tests)
  • cargo fmt --all / cargo clippy --all-targets — the changed files are warning-free. Clippy (stable 1.97) reports 9 pre-existing warnings in files this PR does not touch (map_or simplifications in tools.rs/calls.rs/discover.rs, a redundant closure in management.rs, and repeat().take()/clone nits in tests); they are the same on main and left for a separate cleanup.
  • npm test --prefix monitoring/mcp-status (dashboard untouched)

Checklist

  • I have read the Contributing guidelines.
  • Docs (README / comments) updated for any user-visible change.
  • No secrets, credentials, or internal-only information are included.

Generated by Claude Code

`derive_app_delegation` follows the `mcp_prepare_delegation` update with an
immediate `mcp_get_delegation` query. The query is served by a single replica,
which may not have caught up with the certified state the update produced, so
II answers `NoSuchDelegation` for a delegation that exists. The server turned
that into a tool error blaming un-threaded parameters (a stale diagnosis: the
prepared account and expiration are threaded) and left the retry to the model,
which minted a new per-app key and paid for another prepare each time.

Now `get_delegation_with_retry` re-sends the identical `get` — same per-app
key, same prepared values — after 1, 2 and 3 seconds while II keeps answering
`NoSuchDelegation`, as II's own frontend does (`retryGetDelegation`). Nothing
else is retried: `Unauthorized` still means reconnect, and transport, decode
and internal errors propagate at once. The error for a delegation that never
appears now says so, including how often the server already retried.

Also correct the delegation lifetime in the module docs and the README: II caps
MCP delegations at 5 minutes (`MCP_MAX_EXPIRATION_PERIOD_NS`), not "<= 1 hour",
so with the 30 s re-derive margin the cache re-derives every few minutes.

Tests drive the retry loop over scripted Candid replies on a paused tokio clock
(`tokio/test-util` as a dev-dependency; the lockfile is unchanged).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VP2vTJT82F2CZg2cKGAmt6

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the delegation-retry and documentation changes.

No open issues 🎉

What changed in this PR

This PR makes app-delegation retrieval tolerate temporary Internet Identity replica lag without generating a new key or repeating the prepare update. It also corrects documentation to reflect the five-minute delegation cap.

Changes:

  • Retry the same mcp_get_delegation request on NoSuchDelegation after waits of 1, 2, and 3 seconds; return other errors immediately.
  • Update retry-exhaustion guidance and document delegation lifetime and cache reuse.
  • Add paused-clock tests for retry success, exhaustion, and non-retryable errors.
File Description
README.md Documents the five-minute cap, cache lifetime, and query retries.
crates/​imcp2-core/​src/​identities.rs Implements bounded retries, updates error text and comments, and adds tests.
crates/​imcp2-core/​Cargo.toml Enables Tokio’s test-util feature for paused-clock tests.

Review effort: Balanced

@aterga
aterga marked this pull request as ready for review October 29, 2026 13:29
@aterga
aterga requested a review from a team October 29, 2026 13:30
@aterga
aterga merged commit 7c9964f into main Oct 29, 2026
17 checks passed
@aterga aterga mentioned this pull request Nov 3, 2026
7 of 9 tasks
aterga added a commit that referenced this pull request Nov 3, 2026
All three crates share one version. A patch bump: nothing since 0.6.0
changes the crates' public API or refuses anything 0.6.0 allowed. It
carries the mcp_get_delegation retry on a lagging replica (#220), the
bound on what one /oauth/authorize request stores (#221), the reworded
open_app and call tool descriptions and server instructions (#226), and
clippy fixes (#224).

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants