Skip to content

Commit 54a0bcc

Browse files
committed
feat(web): show provider plan limits in the sidebar
Providers meter subscription plans on rolling windows — Claude's 5-hour and weekly windows, Codex's primary/secondary pair, plus spend and credit ceilings — and T3 Code never surfaced any of it. You found out you were out of headroom by getting rate limited mid-turn. Adapters now normalize their native payload into one provider-agnostic shape, a queue-backed reactor records each observation onto the provider snapshot clients already subscribe to, and the per-instance status cache carries the last observation across restarts. The sidebar footer gets a gauge next to Settings, Pull Requests and Usage: the ring fills to the most constrained live window across every provider and environment, and its popover breaks down each provider, plan, window, and time left. A window whose reset instant has passed renders as reset instead of repeating a spent number. Providers only report while they run, so the meter is explicit about having no data yet rather than implying zero usage. Model: claude-opus-5[1m] · Harness: T3 Code (Claude Code)
1 parent b1b497d commit 54a0bcc

29 files changed

Lines changed: 1825 additions & 8 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Full docs live in [docs/](./docs). There's no docs site yet.
8282
- [Remote access from a phone or another machine](./docs/user/remote-access.md)
8383
- [Keeping app and server in sync](./docs/user/updating.md)
8484
- [Source control integrations](./docs/user/source-control.md)
85+
- [Track plan limits](./docs/user/plan-limits.md)
8586
- Multiple accounts: [Codex](./docs/user/providers-codex.md) · [Claude](./docs/user/providers-claude.md)
8687
- Linux: [run T3 Code as a background service](./docs/user/background-service.md)
8788

‎apps/server/integration/OrchestrationEngineHarness.integration.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import * as ThreadPlanProgress from "../src/orchestration/ThreadPlanProgress.ts"
5656
import { RuntimeReceiptBusTest } from "../src/orchestration/Layers/RuntimeReceiptBus.ts";
5757
import { OrchestrationReactorLive } from "../src/orchestration/Layers/OrchestrationReactor.ts";
5858
import { ProviderCommandReactorLive } from "../src/orchestration/Layers/ProviderCommandReactor.ts";
59+
import { ProviderRateLimitsReactor } from "../src/orchestration/Services/ProviderRateLimitsReactor.ts";
5960
import { ProviderRuntimeIngestionLive } from "../src/orchestration/Layers/ProviderRuntimeIngestion.ts";
6061
import { CheckpointReactor } from "../src/orchestration/Services/CheckpointReactor.ts";
6162
import { ProviderRuntimeIngestionService } from "../src/orchestration/Services/ProviderRuntimeIngestion.ts";
@@ -369,6 +370,12 @@ export const makeOrchestrationIntegrationHarness = (
369370
const orchestrationReactorLayer = OrchestrationReactorLive.pipe(
370371
Layer.provideMerge(runtimeIngestionLayer),
371372
Layer.provideMerge(providerCommandReactorLayer),
373+
Layer.provideMerge(
374+
Layer.succeed(ProviderRateLimitsReactor, {
375+
start: () => Effect.void,
376+
drain: Effect.void,
377+
}),
378+
),
372379
Layer.provideMerge(checkpointReactorLayer),
373380
Layer.provideMerge(
374381
Layer.succeed(ThreadDeletionReactor, {

‎apps/server/src/orchestration/Layers/OrchestrationReactor.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { afterEach, describe, expect, it } from "vite-plus/test";
77

88
import { CheckpointReactor } from "../Services/CheckpointReactor.ts";
99
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
10+
import { ProviderRateLimitsReactor } from "../Services/ProviderRateLimitsReactor.ts";
1011
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
1112
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
1213
import { OrchestrationReactor } from "../Services/OrchestrationReactor.ts";
@@ -23,7 +24,7 @@ describe("OrchestrationReactor", () => {
2324
runtime = null;
2425
});
2526

26-
it("starts provider ingestion, provider command, checkpoint, and thread deletion reactors", async () => {
27+
it("starts provider ingestion, provider command, rate limits, checkpoint, and thread deletion reactors", async () => {
2728
const started: string[] = [];
2829

2930
runtime = ManagedRuntime.make(
@@ -46,6 +47,15 @@ describe("OrchestrationReactor", () => {
4647
drain: Effect.void,
4748
}),
4849
),
50+
Layer.provideMerge(
51+
Layer.succeed(ProviderRateLimitsReactor, {
52+
start: () => {
53+
started.push("provider-rate-limits-reactor");
54+
return Effect.void;
55+
},
56+
drain: Effect.void,
57+
}),
58+
),
4959
Layer.provideMerge(
5060
Layer.succeed(CheckpointReactor, {
5161
start: () => {
@@ -83,6 +93,7 @@ describe("OrchestrationReactor", () => {
8393
expect(started).toEqual([
8494
"provider-runtime-ingestion",
8595
"provider-command-reactor",
96+
"provider-rate-limits-reactor",
8697
"checkpoint-reactor",
8798
"thread-deletion-reactor",
8899
"agent-awareness-relay",

‎apps/server/src/orchestration/Layers/OrchestrationReactor.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import {
77
} from "../Services/OrchestrationReactor.ts";
88
import { CheckpointReactor } from "../Services/CheckpointReactor.ts";
99
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
10+
import { ProviderRateLimitsReactor } from "../Services/ProviderRateLimitsReactor.ts";
1011
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
1112
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
1213
import * as AgentAwarenessRelay from "../../relay/AgentAwarenessRelay.ts";
1314

1415
export const makeOrchestrationReactor = Effect.gen(function* () {
1516
const providerRuntimeIngestion = yield* ProviderRuntimeIngestionService;
1617
const providerCommandReactor = yield* ProviderCommandReactor;
18+
const providerRateLimitsReactor = yield* ProviderRateLimitsReactor;
1719
const checkpointReactor = yield* CheckpointReactor;
1820
const threadDeletionReactor = yield* ThreadDeletionReactor;
1921
const agentAwarenessRelay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
2022

2123
const start: OrchestrationReactorShape["start"] = Effect.fn("start")(function* () {
2224
yield* providerRuntimeIngestion.start();
2325
yield* providerCommandReactor.start();
26+
yield* providerRateLimitsReactor.start();
2427
yield* checkpointReactor.start();
2528
yield* threadDeletionReactor.start();
2629
yield* agentAwarenessRelay.start();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* ProviderRateLimitsReactorLive - runtime stream -> provider snapshot.
3+
*
4+
* Adapters normalize their native plan-limit payload and attach it to
5+
* `account.rate-limits.updated`; this reactor is the only thing that knows
6+
* where that observation ends up. Events without a normalized snapshot (a
7+
* provider shape we cannot map yet) are ignored — they are still logged as raw
8+
* runtime events for diagnostics.
9+
*
10+
* @module ProviderRateLimitsReactorLive
11+
*/
12+
import type { ProviderRuntimeEvent } from "@t3tools/contracts";
13+
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
14+
import * as Cause from "effect/Cause";
15+
import * as Effect from "effect/Effect";
16+
import * as Layer from "effect/Layer";
17+
import * as Stream from "effect/Stream";
18+
19+
import { ProviderRegistry } from "../../provider/Services/ProviderRegistry.ts";
20+
import { ProviderService } from "../../provider/Services/ProviderService.ts";
21+
import { forkParked } from "../../serverActivation.ts";
22+
import {
23+
ProviderRateLimitsReactor,
24+
type ProviderRateLimitsReactorShape,
25+
} from "../Services/ProviderRateLimitsReactor.ts";
26+
27+
type AccountRateLimitsUpdatedEvent = Extract<
28+
ProviderRuntimeEvent,
29+
{ type: "account.rate-limits.updated" }
30+
>;
31+
32+
export const make = Effect.gen(function* () {
33+
const providerService = yield* ProviderService;
34+
const providerRegistry = yield* ProviderRegistry;
35+
36+
const processEvent = (event: AccountRateLimitsUpdatedEvent) => {
37+
const snapshot = event.payload.snapshot;
38+
const instanceId = event.providerInstanceId;
39+
// `ProviderService` stamps the instance id on every event it fans out; an
40+
// event without one predates that and cannot be attributed to a snapshot.
41+
if (!snapshot || instanceId === undefined) {
42+
return Effect.void;
43+
}
44+
return providerRegistry.setProviderRateLimits({ instanceId, rateLimits: snapshot }).pipe(
45+
Effect.asVoid,
46+
Effect.catchCause((cause) =>
47+
Cause.hasInterruptsOnly(cause)
48+
? Effect.failCause(cause)
49+
: Effect.logWarning("failed to record provider rate limits", {
50+
instanceId,
51+
provider: event.provider,
52+
cause: Cause.pretty(cause),
53+
}),
54+
),
55+
);
56+
};
57+
58+
const worker = yield* makeDrainableWorker(processEvent);
59+
60+
const start: ProviderRateLimitsReactorShape["start"] = () =>
61+
forkParked(
62+
Stream.runForEach(providerService.streamEvents, (event) =>
63+
event.type === "account.rate-limits.updated" ? worker.enqueue(event) : Effect.void,
64+
),
65+
).pipe(Effect.asVoid);
66+
67+
return {
68+
start,
69+
drain: worker.drain,
70+
} satisfies ProviderRateLimitsReactorShape;
71+
});
72+
73+
export const ProviderRateLimitsReactorLive = Layer.effect(ProviderRateLimitsReactor, make);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ProviderRateLimitsReactor - records plan rate-limit observations.
3+
*
4+
* Providers only report their plan headroom while a session is live, so the
5+
* observation has to be captured as it flies past on the runtime stream and
6+
* stored on the provider snapshot the clients already subscribe to.
7+
*
8+
* @module ProviderRateLimitsReactor
9+
*/
10+
import * as Context from "effect/Context";
11+
import type * as Effect from "effect/Effect";
12+
import type * as Scope from "effect/Scope";
13+
14+
export interface ProviderRateLimitsReactorShape {
15+
/**
16+
* Start forwarding `account.rate-limits.updated` observations into the
17+
* provider registry. Must be run in a scope so the worker is finalized on
18+
* shutdown.
19+
*/
20+
readonly start: () => Effect.Effect<void, never, Scope.Scope>;
21+
22+
/** Resolves once every observed event has been applied. Test seam. */
23+
readonly drain: Effect.Effect<void>;
24+
}
25+
26+
export class ProviderRateLimitsReactor extends Context.Service<
27+
ProviderRateLimitsReactor,
28+
ProviderRateLimitsReactorShape
29+
>()("t3/orchestration/Services/ProviderRateLimitsReactor") {}

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ import {
9595
} from "../Errors.ts";
9696
import { type ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts";
9797
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
98+
import {
99+
type ClaudeRateLimitEventPayload,
100+
normalizeClaudeRateLimitEvent,
101+
} from "../providerRateLimits.ts";
98102
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.fromJsonString(Schema.Unknown));
99103
const decodeUnknownJsonStringExit = Schema.decodeUnknownExit(Schema.fromJsonString(Schema.Unknown));
100104

@@ -3552,11 +3556,16 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
35523556
}
35533557

35543558
if (message.type === "rate_limit_event") {
3559+
const snapshot = normalizeClaudeRateLimitEvent({
3560+
event: message.rate_limit_info as ClaudeRateLimitEventPayload,
3561+
observedAt: base.createdAt,
3562+
});
35553563
yield* offerRuntimeEvent({
35563564
...base,
35573565
type: "account.rate-limits.updated",
35583566
payload: {
35593567
rateLimits: message,
3568+
...(snapshot ? { snapshot } : {}),
35603569
},
35613570
});
35623571
return;

‎apps/server/src/provider/Layers/CodexAdapter.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
} from "./CodexSessionRuntime.ts";
6666
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
6767
import { resolveCodexLaunchArgs } from "./codexLaunchArgs.ts";
68+
import { normalizeCodexRateLimits } from "../providerRateLimits.ts";
6869
const isCodexAppServerProcessExitedError = Schema.is(CodexErrors.CodexAppServerProcessExitedError);
6970
const isCodexAppServerTransportError = Schema.is(CodexErrors.CodexAppServerTransportError);
7071
const isCodexSessionRuntimeThreadIdMissingError = Schema.is(
@@ -1408,15 +1409,25 @@ function mapToRuntimeEvents(
14081409
}
14091410

14101411
if (event.method === "account/rateLimits/updated") {
1411-
if (!readPayload(EffectCodexSchema.V2AccountRateLimitsUpdatedNotification, event.payload)) {
1412+
const payload = readPayload(
1413+
EffectCodexSchema.V2AccountRateLimitsUpdatedNotification,
1414+
event.payload,
1415+
);
1416+
if (!payload) {
14121417
return [];
14131418
}
1419+
const base = runtimeEventBase(event, canonicalThreadId);
1420+
const snapshot = normalizeCodexRateLimits({
1421+
rateLimits: payload.rateLimits,
1422+
observedAt: base.createdAt,
1423+
});
14141424
return [
14151425
{
14161426
type: "account.rate-limits.updated",
1417-
...runtimeEventBase(event, canonicalThreadId),
1427+
...base,
14181428
payload: {
14191429
rateLimits: event.payload ?? {},
1430+
...(snapshot ? { snapshot } : {}),
14201431
},
14211432
},
14221433
];

0 commit comments

Comments
 (0)