Skip to content

Commit 6daa0c4

Browse files
committed
refactor(providers): build account switching on the new usage limits
Upstream now normalizes subscription usage itself: adapters emit typed windows, ProviderUsageLimitsIngestion merges them onto ServerProvider.usageLimits, and the clients already render them. Drop our parallel rate-limit layer and read that state instead. Removed: the ServerProviderRateLimit contract, the per-driver normalizer, the registry mutator that carried volatile state, and the reactor half that tracked limits. What is left is the part upstream does not do: continue a thread on another account of the same provider when the bound one runs out of usage. The reactor is renamed to ProviderAccountSwitchReactor to say what it now does, and reset wording comes from the shared usage-limit helpers so the banner, the picker and the limits view agree.
1 parent d986ca5 commit 6daa0c4

30 files changed

Lines changed: 696 additions & 1139 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import * as ThreadPlanProgress from "../src/orchestration/ThreadPlanProgress.ts"
5858
import { RuntimeReceiptBusTest } from "../src/orchestration/Layers/RuntimeReceiptBus.ts";
5959
import { OrchestrationReactorLive } from "../src/orchestration/Layers/OrchestrationReactor.ts";
6060
import { ProviderCommandReactorLive } from "../src/orchestration/Layers/ProviderCommandReactor.ts";
61-
import * as ProviderRateLimitReactor from "../src/orchestration/ProviderRateLimitReactor.ts";
61+
import * as ProviderAccountSwitchReactor from "../src/orchestration/ProviderAccountSwitchReactor.ts";
6262
import { ProviderRuntimeIngestionLive } from "../src/orchestration/Layers/ProviderRuntimeIngestion.ts";
6363
import { CheckpointReactor } from "../src/orchestration/Services/CheckpointReactor.ts";
6464
import { ProviderRuntimeIngestionService } from "../src/orchestration/Services/ProviderRuntimeIngestion.ts";
@@ -348,7 +348,7 @@ export const makeOrchestrationIntegrationHarness = (
348348
Layer.provideMerge(textGenerationLayer),
349349
Layer.provideMerge(serverSettingsLayer),
350350
);
351-
const providerRateLimitReactorLayer = ProviderRateLimitReactor.layer.pipe(
351+
const providerAccountSwitchReactorLayer = ProviderAccountSwitchReactor.layer.pipe(
352352
Layer.provideMerge(runtimeServicesLayer),
353353
Layer.provideMerge(serverSettingsLayer),
354354
);
@@ -390,7 +390,7 @@ export const makeOrchestrationIntegrationHarness = (
390390
const orchestrationReactorLayer = OrchestrationReactorLive.pipe(
391391
Layer.provideMerge(runtimeIngestionLayer),
392392
Layer.provideMerge(providerCommandReactorLayer),
393-
Layer.provideMerge(providerRateLimitReactorLayer),
393+
Layer.provideMerge(providerAccountSwitchReactorLayer),
394394
Layer.provideMerge(checkpointReactorLayer),
395395
Layer.provideMerge(
396396
Layer.succeed(ThreadDeletionReactor, {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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 * as ProviderRateLimitReactor from "../ProviderRateLimitReactor.ts";
10+
import * as ProviderAccountSwitchReactor from "../ProviderAccountSwitchReactor.ts";
1111
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
1212
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
1313
import * as ThreadSettlementReactor from "../ThreadSettlementReactor.ts";
@@ -50,9 +50,9 @@ describe("OrchestrationReactor", () => {
5050
}),
5151
),
5252
Layer.provideMerge(
53-
Layer.succeed(ProviderRateLimitReactor.ProviderRateLimitReactor, {
53+
Layer.succeed(ProviderAccountSwitchReactor.ProviderAccountSwitchReactor, {
5454
start: () => {
55-
started.push("provider-rate-limit-reactor");
55+
started.push("provider-account-switch-reactor");
5656
return Effect.void;
5757
},
5858
drain: Effect.void,
@@ -113,7 +113,7 @@ describe("OrchestrationReactor", () => {
113113
expect(started).toEqual([
114114
"provider-runtime-ingestion",
115115
"provider-command-reactor",
116-
"provider-rate-limit-reactor",
116+
"provider-account-switch-reactor",
117117
"checkpoint-reactor",
118118
"thread-deletion-reactor",
119119
"thread-pull-request-reactor",

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "../Services/OrchestrationReactor.ts";
88
import { CheckpointReactor } from "../Services/CheckpointReactor.ts";
99
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
10-
import * as ProviderRateLimitReactor from "../ProviderRateLimitReactor.ts";
10+
import * as ProviderAccountSwitchReactor from "../ProviderAccountSwitchReactor.ts";
1111
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
1212
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
1313
import * as ThreadSettlementReactor from "../ThreadSettlementReactor.ts";
@@ -17,7 +17,8 @@ import * as AgentAwarenessRelay from "../../relay/AgentAwarenessRelay.ts";
1717
export const makeOrchestrationReactor = Effect.gen(function* () {
1818
const providerRuntimeIngestion = yield* ProviderRuntimeIngestionService;
1919
const providerCommandReactor = yield* ProviderCommandReactor;
20-
const providerRateLimitReactor = yield* ProviderRateLimitReactor.ProviderRateLimitReactor;
20+
const providerAccountSwitchReactor =
21+
yield* ProviderAccountSwitchReactor.ProviderAccountSwitchReactor;
2122
const checkpointReactor = yield* CheckpointReactor;
2223
const threadDeletionReactor = yield* ThreadDeletionReactor;
2324
const threadSettlementReactor = yield* ThreadSettlementReactor.ThreadSettlementReactor;
@@ -27,7 +28,7 @@ export const makeOrchestrationReactor = Effect.gen(function* () {
2728
const start: OrchestrationReactorShape["start"] = Effect.fn("start")(function* () {
2829
yield* providerRuntimeIngestion.start();
2930
yield* providerCommandReactor.start();
30-
yield* providerRateLimitReactor.start();
31+
yield* providerAccountSwitchReactor.start();
3132
yield* checkpointReactor.start();
3233
yield* threadDeletionReactor.start();
3334
yield* threadPullRequestReactor.start();

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {
1515
} from "@t3tools/contracts";
1616
import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
1717
import {
18-
isProviderRateLimitActive,
19-
selectRateLimitFallbackProvider,
20-
} from "@t3tools/shared/providerRateLimits";
18+
exhaustedUsageWindow,
19+
selectAccountSwitchTarget,
20+
} from "@t3tools/shared/providerAccountSwitching";
2121
import { isTemporaryWorktreeBranch, WORKTREE_BRANCH_PREFIX } from "@t3tools/shared/git";
2222
import * as Cache from "effect/Cache";
2323
import * as Cause from "effect/Cause";
@@ -48,9 +48,9 @@ import { ProviderService } from "../../provider/Services/ProviderService.ts";
4848
import { ProviderRegistry } from "../../provider/Services/ProviderRegistry.ts";
4949
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
5050
import {
51-
formatRateLimitSwitchSummary,
51+
formatAccountSwitchSummary,
5252
PROVIDER_INSTANCE_SWITCHED_ACTIVITY_KIND,
53-
} from "../ProviderRateLimitReactor.ts";
53+
} from "../ProviderAccountSwitchReactor.ts";
5454
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
5555
import {
5656
ProviderCommandReactor,
@@ -1195,8 +1195,8 @@ const make = Effect.gen(function* () {
11951195
* the same provider session. The switch is recorded as a thread activity so
11961196
* it is visible in the transcript.
11971197
*/
1198-
const resolveRateLimitedModelSelection = Effect.fnUntraced(function* (input: {
1199-
readonly thread: OrchestrationThread;
1198+
const resolveAccountSwitchModelSelection = Effect.fnUntraced(function* (input: {
1199+
readonly thread: Pick<OrchestrationThread, "id" | "modelSelection" | "session">;
12001200
readonly requestedModelSelection: ModelSelection | undefined;
12011201
readonly createdAt: string;
12021202
}) {
@@ -1211,7 +1211,8 @@ const make = Effect.gen(function* () {
12111211
const nowMs = Date.parse(input.createdAt);
12121212
const providers = yield* providerRegistry.getProviders;
12131213
const current = providers.find((provider) => provider.instanceId === currentInstanceId);
1214-
if (!current || !isProviderRateLimitActive(current.rateLimit, nowMs)) {
1214+
const spent = current ? exhaustedUsageWindow(current, nowMs) : null;
1215+
if (!current || !spent) {
12151216
return input.requestedModelSelection;
12161217
}
12171218
const autoSwitch = yield* serverSettingsService.getSettings.pipe(
@@ -1221,22 +1222,23 @@ const make = Effect.gen(function* () {
12211222
if (!autoSwitch) {
12221223
return input.requestedModelSelection;
12231224
}
1224-
const fallback = selectRateLimitFallbackProvider({
1225+
const fallback = selectAccountSwitchTarget({
12251226
providers,
12261227
instanceId: currentInstanceId,
12271228
nowMs,
12281229
});
12291230
if (!fallback) {
12301231
return input.requestedModelSelection;
12311232
}
1232-
yield* Effect.logInfo("provider command reactor routing turn to fallback account", {
1233+
yield* Effect.logInfo("provider command reactor routing turn to another account", {
12331234
threadId: input.thread.id,
12341235
fromInstanceId: currentInstanceId,
12351236
toInstanceId: fallback.instanceId,
1236-
resetsAt: current.rateLimit?.resetsAt,
1237+
window: spent.id,
1238+
resetsAt: spent.resetsAt,
12371239
});
12381240
yield* Effect.all({
1239-
commandId: serverCommandId("provider-rate-limit-switch"),
1241+
commandId: serverCommandId("provider-account-switch"),
12401242
eventId: serverEventId(),
12411243
}).pipe(
12421244
Effect.flatMap(({ commandId, eventId }) =>
@@ -1248,12 +1250,12 @@ const make = Effect.gen(function* () {
12481250
id: eventId,
12491251
tone: "info",
12501252
kind: PROVIDER_INSTANCE_SWITCHED_ACTIVITY_KIND,
1251-
summary: formatRateLimitSwitchSummary({ from: current, to: fallback }),
1253+
summary: formatAccountSwitchSummary({ from: current, to: fallback }),
12521254
payload: {
1253-
reason: "rate-limit",
1255+
reason: "usage-limit",
12541256
fromInstanceId: currentInstanceId,
12551257
toInstanceId: fallback.instanceId,
1256-
...(current.rateLimit?.resetsAt ? { resetsAt: current.rateLimit.resetsAt } : {}),
1258+
...(spent.resetsAt ? { resetsAt: spent.resetsAt } : {}),
12571259
},
12581260
turnId: null,
12591261
createdAt: input.createdAt,
@@ -1515,7 +1517,7 @@ const make = Effect.gen(function* () {
15151517
);
15161518
}
15171519

1518-
const modelSelection = yield* resolveRateLimitedModelSelection({
1520+
const modelSelection = yield* resolveAccountSwitchModelSelection({
15191521
thread,
15201522
requestedModelSelection: event.payload.modelSelection,
15211523
createdAt: event.payload.createdAt,

0 commit comments

Comments
 (0)