Skip to content

Commit 5e624cc

Browse files
Fizzngthuydiemclaude
committed
fix(identity): report an issuer that keeps rate-limiting an agent as a limit
Co-authored-by: Diem Nguyen <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Fizz <6be7be23ec5bba868f3e1db5ec86013d76bd1a293dce5b435a192158fec13641@buzz.block.builderlab.xyz>
1 parent 063fccd commit 5e624cc

5 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎docs/identity.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ issuance cannot silently start another enrollment. A new sign-in revision or
890890
explicit agent deletion permits a reset; ordinary restore of the same session does
891891
not. Failure to persist the reservation prevents dispatch. A known transient
892892
outcome releases it only if that storage write succeeds.
893+
An issuer still answering 429 when retries or the deadline run out is reported
894+
to the owner as a limit rather than an unreachable service; it stays transient.
893895

894896
The `/v1/nip-fi/agent-grant` and `/v1/nip-fi/agent-assertion` routes and
895897
`Nostr-Agent-Proof` header are this configured adapter's wire contract, not

‎src-tauri/src/federated_identity/agent/mod.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ impl AgentTokens {
505505
.grants += 1;
506506
return Err(STOPPED.into());
507507
}
508-
Err(failure) => return Err(refused(failure.code)),
508+
Err(failure) => return Err(refused(failure)),
509509
}
510510
}
511511
Ok(cached) if grant.replacement => {
512512
self.settle(agent, grant, retirements).await?;
513513
cached
514514
}
515-
minted => minted.map_err(|failure| refused(failure.code))?,
515+
minted => minted.map_err(refused)?,
516516
};
517517
if !self.live(&agent.pubkey, incarnation) {
518518
return Err(DELETED.into());
@@ -1088,7 +1088,7 @@ impl From<&str> for Error {
10881088
impl From<Failure> for Error {
10891089
fn from(failure: Failure) -> Self {
10901090
Self {
1091-
message: refused(failure.code),
1091+
message: refused(failure),
10921092
refused: matches!(
10931093
failure.code,
10941094
"adapter-refused"
@@ -1102,8 +1102,13 @@ impl From<Failure> for Error {
11021102
}
11031103

11041104
/// What the agent's owner sees when the identity issuer turns it away.
1105-
fn refused(code: &'static str) -> String {
1106-
match code {
1105+
fn refused(failure: Failure) -> String {
1106+
if failure.rate_limited {
1107+
return "Your organization is limiting this agent's requests; try again later. \
1108+
If it keeps failing, you may have reached your organization's agent limit"
1109+
.into();
1110+
}
1111+
match failure.code {
11071112
"network-unavailable" | "network-timeout" => {
11081113
"Your organization's identity service is unreachable".into()
11091114
}

‎src-tauri/src/federated_identity/agent/tests.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,3 +1837,34 @@ async fn removing_one_row_keeps_another_rows_admission_and_the_shared_durable_gr
18371837
.unwrap();
18381838
assert!(survivor.commit(false, || Ok(())).is_err());
18391839
}
1840+
1841+
#[tokio::test]
1842+
async fn an_issuer_that_keeps_rate_limiting_is_not_reported_as_unreachable() {
1843+
let limited = || (429, serde_json::json!({ "error": "rate_limited" }));
1844+
let unavailable = || (503, serde_json::json!({ "error": "issuance_unavailable" }));
1845+
let (base, served) = issuer(vec![
1846+
limited(),
1847+
limited(),
1848+
limited(),
1849+
limited(),
1850+
limited(),
1851+
unavailable(),
1852+
(200, enrolled(&"b".repeat(64))),
1853+
limited(),
1854+
limited(),
1855+
limited(),
1856+
])
1857+
.await;
1858+
let f = fixture(base);
1859+
let limit = |error: String| assert!(error.contains("limiting this agent"), "{error}");
1860+
// Enrollment: throttling is transient, so the next request enrolls again.
1861+
limit(f.tokens.evidence(&f.agent, &f.key, 0).await.unwrap_err());
1862+
// Only the last attempt's answer decides.
1863+
assert_eq!(
1864+
f.tokens.evidence(&f.agent, &f.key, 0).await.unwrap_err(),
1865+
"Your organization's identity service is unreachable"
1866+
);
1867+
// Assertion minting.
1868+
limit(f.tokens.evidence(&f.agent, &f.key, 0).await.unwrap_err());
1869+
assert_eq!(settled(served).await.len(), 10);
1870+
}

‎src-tauri/src/federated_identity/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ impl FederatedIdentity {
415415
return Err(Failure {
416416
code: "reauthentication-required",
417417
session_denied: true,
418+
rate_limited: false,
418419
});
419420
}
420421
#[derive(Deserialize)]

‎src-tauri/src/federated_identity/protocol.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ pub(crate) fn client() -> Result<Client, &'static str> {
3737
pub(crate) struct Failure {
3838
pub(crate) code: &'static str,
3939
pub(crate) session_denied: bool,
40+
/// The last answer before giving up, by retries or the deadline, was a 429.
41+
pub(crate) rate_limited: bool,
4042
}
4143
impl From<&'static str> for Failure {
4244
fn from(code: &'static str) -> Self {
4345
Self {
4446
code,
4547
session_denied: false,
48+
rate_limited: false,
4649
}
4750
}
4851
}
@@ -81,9 +84,16 @@ pub(crate) async fn json<T: DeserializeOwned>(
8184
// One total deadline includes bounded retry and Retry-After. Refusals and
8285
// invalid success bodies do not retry; no browser is launched here.
8386
let terminal_headers = std::sync::atomic::AtomicBool::new(false);
87+
let rate_limited = std::sync::atomic::AtomicBool::new(false);
8488
tokio::time::timeout(DEADLINE, async {
8589
for attempt in 0..3u64 {
8690
let response = request().send().await;
91+
rate_limited.store(
92+
response
93+
.as_ref()
94+
.is_ok_and(|response| response.status().as_u16() == 429),
95+
std::sync::atomic::Ordering::Relaxed,
96+
);
8797
let retry = match response {
8898
Err(_) => Some(None),
8999
Ok(response) => {
@@ -143,6 +153,7 @@ pub(crate) async fn json<T: DeserializeOwned>(
143153
return Err(Failure {
144154
code,
145155
session_denied,
156+
rate_limited: false,
146157
});
147158
}
148159
}
@@ -169,6 +180,10 @@ pub(crate) async fn json<T: DeserializeOwned>(
169180
},
170181
)
171182
})?
183+
.map_err(|failure| Failure {
184+
rate_limited: rate_limited.load(std::sync::atomic::Ordering::Relaxed),
185+
..failure
186+
})
172187
}
173188

174189
fn retry_after(value: &str) -> Option<Duration> {

0 commit comments

Comments
 (0)