fix(usage): stop 429s with an activity-dependent poll cadence + manual refresh

The usage monitor polled the undocumented OAuth usage endpoint every 60s and
earned 429s. It now polls every 5 min while any task is Running and every
15 min while idle (usage_poll_interval_active_seconds / _idle_seconds, both
clamped to >= 60; the old single usage_poll_interval_seconds key is gone).

A 429 comes back as UsageRateLimitedException carrying Retry-After and adds
exponential backoff on top, capped at 30 min and never shorter than the normal
cadence; the strike count resets on the first success. The schedule arithmetic
is the pure static UsagePollSchedule.NextDelay.

Since the idle cadence is slow on purpose, WorkerHub.RefreshUsage drives
UsageMonitorService.RefreshNowAsync behind a Refresh now button in the Usage
Monitor modal: an out-of-band poll that pushes the loop's next-due time out so
no double poll follows, with a 10s cooldown so click-spam can't earn a 429.

Staleness now measures against the slower (idle) interval so an idle worker
isn't flagged stale just for not polling.
This commit is contained in:
mika kuns
2026-08-05 16:40:34 +02:00
parent f6cb8250bb
commit 2700c3d817
24 changed files with 615 additions and 50 deletions
@@ -44,6 +44,7 @@ public sealed class ClaudeOAuthUsageClientTests : IDisposable
public List<HttpRequestMessage> Requests { get; } = new();
public HttpStatusCode ResponseStatus { get; set; } = HttpStatusCode.OK;
public string ResponseBody { get; set; } = "{}";
public RetryConditionHeaderValue? RetryAfter { get; set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
{
@@ -52,6 +53,7 @@ public sealed class ClaudeOAuthUsageClientTests : IDisposable
{
Content = new StringContent(ResponseBody, Encoding.UTF8, "application/json"),
};
if (RetryAfter is not null) resp.Headers.RetryAfter = RetryAfter;
return Task.FromResult(resp);
}
}
@@ -179,4 +181,43 @@ public sealed class ClaudeOAuthUsageClientTests : IDisposable
await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetUsageAsync());
}
[Fact]
public async Task GetUsageAsync_429_ThrowsRateLimitedWithRetryAfterDelta()
{
WriteCredentials();
var (client, handler) = Build();
handler.ResponseStatus = HttpStatusCode.TooManyRequests;
handler.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromSeconds(45));
var ex = await Assert.ThrowsAsync<UsageRateLimitedException>(() => client.GetUsageAsync());
Assert.Equal(TimeSpan.FromSeconds(45), ex.RetryAfter);
}
[Fact]
public async Task GetUsageAsync_429_WithoutRetryAfter_HasNullRetryAfter()
{
WriteCredentials();
var (client, handler) = Build();
handler.ResponseStatus = HttpStatusCode.TooManyRequests;
var ex = await Assert.ThrowsAsync<UsageRateLimitedException>(() => client.GetUsageAsync());
Assert.Null(ex.RetryAfter);
Assert.Contains("429", ex.Message);
}
[Fact]
public async Task GetUsageAsync_429_WithPastRetryAfterDate_IgnoresIt()
{
WriteCredentials();
var (client, handler) = Build();
handler.ResponseStatus = HttpStatusCode.TooManyRequests;
handler.RetryAfter = new RetryConditionHeaderValue(DateTimeOffset.UtcNow.AddMinutes(-5));
var ex = await Assert.ThrowsAsync<UsageRateLimitedException>(() => client.GetUsageAsync());
Assert.Null(ex.RetryAfter);
}
}