Repository navigation
Retry mcp_get_delegation when a lagging replica answers NoSuchDelegation; document the 5-minute delegation cap - #220
Merged
Conversation
`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
There was a problem hiding this comment.
🟢 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_delegationrequest onNoSuchDelegationafter 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
marked this pull request as ready for review
October 29, 2026 13:29
sea-snake
approved these changes
Oct 29, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Identities::derive_app_delegationcalls II'smcp_prepare_delegation(an update,call_and_wait) and then immediatelymcp_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 answersErr(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 threadprepared.account_numberandprepared.expiration— and pushed the retry onto the model, which minted a brand-new per-app key and paid for anotherprepareupdate each time.This PR makes the server absorb the transient itself, the way II's own frontend does (
retryGetDelegationinfetchDelegation.ts: up to fiveget_delegationtries with linear backoff onno_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_NSinsrc/internet_identity/src/mcp.rs), so with the server's 30 sREDERIVE_MARGIN_NSa 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
NoSuchDelegationtool errors seen against beta II.Changes
identities.rs: retry only themcp_get_delegationquery onNoSuchDelegation. Newget_delegation_with_retryre-sends the identical request (same per-app key, same prepared account and expiration — nothing is re-prepared) after the waits inGET_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:Unauthorizedstill maps to the reconnect message unretried, anInternalCanisterErroris reported as before, and a transport or decode failure propagates as is. Each retry logs atinfo, exhaustion atwarn.map_delegation_error'sNoSuchDelegationarm 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.REDERIVE_MARGIN_NS, and themax_ttlcomment inidentities.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) thatgetis retried onNoSuchDelegation.get_delegation_with_retryover scripted Candid-encodedmcp_get_delegationreplies on a paused tokio clock (tokio/test-utiladded as a dev-dependency ofimcp2-core; a feature on a dependency the crate already has, soCargo.lockis unchanged and--lockedstill 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, andInternalCanisterErroreach end after one attempt with no wait, even with a good reply scripted behind them.IiDelegation/IiSignedDelegationnow deriveDebugso the tests canexpect_erron the result.Not changed:
docs/icp-mcp-privacy-policy-draft.mdstill 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-targetscargo test --locked --all-targets(workspace: all suites green;imcp2-core206 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_orsimplifications intools.rs/calls.rs/discover.rs, a redundant closure inmanagement.rs, andrepeat().take()/clonenits in tests); they are the same onmainand left for a separate cleanup.npm test --prefix monitoring/mcp-status(dashboard untouched)Checklist
Generated by Claude Code