@devries-cameron20 / t3code Mirror · read-only / commits / 9fae0e82c9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 00:30 UTC

fix(server): honor disabled usage hub polling (#10310)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Aditya Garud committed Oct 20, 2026 at 05:57 UTC 9fae0e82c973f645e8c617f349e6db2871062c5c
2 files changed +116 -5
apps/server/src/usage/UsageLimitSources.test.ts
+110 -2
@@ -1,10 +1,19 @@
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import { expect, it } from "@effect/vitest";
3 -import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
3 +import {
4 + ServerConfigStreamEvent,
5 + type ServerSettings as ServerSettingsValue,
6 + UsageLimitSourceId,
7 +} from "@t3tools/contracts";
8 +import * as Duration from "effect/Duration";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 +import * as PubSub from "effect/PubSub";
12 +import * as Ref from "effect/Ref";
13 import * as Schema from "effect/Schema";
7 -import { HttpClient } from "effect/http";
14 +import * as Stream from "effect/Stream";
15 +import * as TestClock from "effect/testing/TestClock";
16 +import { HttpClient, HttpClientResponse } from "effect/http";
17
18 import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
19 import * as ServerSettings from "../serverSettings.ts";
@@ -83,3 +92,102 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
92 Effect.scoped,
93 ),
94 );
95 +
96 +it.effect.each([
97 + { interval: Duration.seconds(0), expectedReads: 1, mode: "disabled" },
98 + { interval: Duration.minutes(1), expectedReads: 2, mode: "enabled" },
99 +])(
100 + "honors $mode polling while preserving explicit and settings refreshes",
101 + ({ interval, expectedReads }) =>
102 + Effect.gen(function* () {
103 + const settings = yield* ServerSettings.ServerSettingsService;
104 + const settingsChanges = yield* PubSub.unbounded<ServerSettingsValue>();
105 + const reads = yield* Ref.make(0);
106 + const allowBackgroundWork = yield* Ref.make(true);
107 + const sources = yield* UsageLimitSources.make.pipe(
108 + Effect.provide([
109 + Layer.succeed(ServerSettings.ServerSettingsService, {
110 + ...settings,
111 + streamChanges: Stream.fromPubSub(settingsChanges),
112 + }),
113 + Layer.mock(BackgroundPolicy.BackgroundPolicy)({
114 + shouldRunScopeWork: (scope) =>
115 + scope.type === "provider-status"
116 + ? Ref.get(allowBackgroundWork)
117 + : Effect.succeed(false),
118 + }),
119 + Layer.succeed(
120 + HttpClient.HttpClient,
121 + HttpClient.make((request) =>
122 + Ref.update(reads, (count) => count + 1).pipe(
123 + Effect.as(HttpClientResponse.fromWeb(request, Response.json({ files: [] }))),
124 + ),
125 + ),
126 + ),
127 + NodeCrypto.layer,
128 + ]),
129 + );
130 + yield* sources.streamChanges.pipe(
131 + Stream.filter((snapshots) => snapshots.length === 1),
132 + Stream.runHead,
133 + );
134 + expect((yield* sources.current)[0]?.error).toBeUndefined();
135 + expect(yield* Ref.get(reads)).toBe(1);
136 +
137 + yield* TestClock.adjust("1 minute");
138 + expect(yield* Ref.get(reads)).toBe(expectedReads);
139 +
140 + yield* sources.refresh;
141 + expect(yield* Ref.get(reads)).toBe(expectedReads + 1);
142 +
143 + const current = yield* settings.getSettings;
144 + const updated = yield* settings.updateSettings({
145 + usageLimitSources: {
146 + [UsageLimitSourceId.make("hub")]: {
147 + ...current.usageLimitSources[UsageLimitSourceId.make("hub")]!,
148 + label: "Renamed hub",
149 + },
150 + },
151 + });
152 + yield* PubSub.publish(settingsChanges, updated);
153 + yield* sources.streamChanges.pipe(
154 + Stream.filter((snapshots) => snapshots[0]?.label === "Renamed hub"),
155 + Stream.runHead,
156 + );
157 + expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
158 +
159 + yield* Ref.set(allowBackgroundWork, false);
160 + yield* TestClock.adjust("1 minute");
161 + expect(yield* Ref.get(reads)).toBe(expectedReads + 2);
162 +
163 + const beforeResuming = (yield* sources.current)[0]?.checkedAt;
164 + yield* Ref.set(allowBackgroundWork, true);
165 + yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.minutes(1) });
166 + yield* TestClock.adjust("2 minutes");
167 + expect(yield* Ref.get(reads)).toBeGreaterThan(expectedReads + 2);
168 + yield* sources.streamChanges.pipe(
169 + Stream.filter((snapshots) => snapshots[0]?.checkedAt !== beforeResuming),
170 + Stream.runHead,
171 + );
172 + yield* TestClock.adjust("30 seconds");
173 + const readsBeforeDisabling = yield* Ref.get(reads);
174 + yield* settings.updateSettings({ providerHealthRefreshInterval: Duration.seconds(0) });
175 + yield* TestClock.adjust("30 seconds");
176 + expect(yield* Ref.get(reads)).toBe(readsBeforeDisabling);
177 + }).pipe(
178 + Effect.provide(
179 + ServerSettings.layerTest({
180 + providerHealthRefreshInterval: interval,
181 + usageLimitSources: {
182 + [UsageLimitSourceId.make("hub")]: {
183 + kind: "cliproxy",
184 + url: "https://hub.test",
185 + managementKey: "test-key",
186 + enabled: true,
187 + },
188 + },
189 + }),
190 + ),
191 + Effect.scoped,
192 + ),
193 +);
apps/server/src/usage/UsageLimitSources.ts
+6 -3
@@ -137,10 +137,13 @@ export const make = Effect.gen(function* () {
137 );
138 yield* Effect.forever(
139 interval.pipe(
140 - Effect.flatMap((wait) =>
141 - Effect.sleep(Duration.toMillis(Duration.fromInputUnsafe(wait)) <= 0 ? "60 seconds" : wait),
140 + Effect.flatMap((wait) => Effect.sleep(Duration.isPositive(wait) ? wait : "60 seconds")),
141 + Effect.andThen(interval),
142 + Effect.flatMap((current) =>
143 + Duration.isPositive(current)
144 + ? backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })
145 + : Effect.succeed(false),
146 ),
143 - Effect.andThen(backgroundPolicy.shouldRunScopeWork({ type: "provider-status" })),
147 Effect.flatMap((shouldRun) => (shouldRun ? refresh : Effect.void)),
148 Effect.ignoreCause({ log: true }),
149 ),