GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(server): refresh usage limits while no client watches (usage pac… · scuttletoby/t3code@3779d13

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • background
      • BackgroundPolicy.ts
    • server.ts
    • serverSettings.ts
    • usage
      • HeadlessUsageRefresher.settings.test.ts
      • HeadlessUsageRefresher.test.ts
      • HeadlessUsageRefresher.ts
  • packages
    • contracts/src
      • settings.ts
    • shared/src
      • serverSettings.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/background/BackgroundPolicy.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/background/BackgroundPolicy.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function hasThermalPressure(hostPower: HostPowerSnapshot): boolean {
122122
return hostPower.thermalState === "serious" || hostPower.thermalState === "critical";
123123
}
124124

125-
function isHostConstrained(
125+
export function isHostConstrained(
126126
hostPower: HostPowerSnapshot,
127127
settings: ResolvedBackgroundActivitySettings,
128128
): boolean {
Collapse file

‎apps/server/src/server.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/server.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import * as ProviderAdapterRegistry from "./orchestration-v2/ProviderAdapterRegi
116116
import * as ProviderRegistry from "./provider/ProviderRegistry.ts";
117117
import * as ProviderUsageLimitsIngestion from "./provider/ProviderUsageLimitsIngestion.ts";
118118
import * as UsageReadingsHistory from "./usage/UsageReadingsHistory.ts";
119+
import * as HeadlessUsageRefresher from "./usage/HeadlessUsageRefresher.ts";
119120
import * as UsageLimitSources from "./usage/UsageLimitSources.ts";
120121
import * as ProjectFaviconResolver from "./project/ProjectFaviconResolver.ts";
121122
import * as T3ProjectFileLoader from "./project/T3ProjectFileLoader.ts";
@@ -683,6 +684,7 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
683684
).pipe(Layer.provide(DelegatedTaskQuestionReactor.layer), Layer.provide(ProjectionStoreV2.layer)),
684685
ProviderUsageLimitsIngestion.layer,
685686
UsageReadingsHistory.layer,
687+
HeadlessUsageRefresher.layer,
686688
layerProviderInstallationRefresh,
687689
ProviderInstallation.layer,
688690
ReplayMarkers.layer,
Collapse file

‎apps/server/src/serverSettings.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/serverSettings.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ const ATOMIC_SETTINGS_KEYS: ReadonlySet<string> = new Set([
543543
"backgroundActivity",
544544
"automaticGitFetchInterval",
545545
"providerHealthRefreshInterval",
546+
"headlessUsageRefreshInterval",
546547
"sourceControlWriterModelSelection",
547548
"reviewGuideModelSelection",
548549
"textGenerationModelSelection",
Collapse file

‎apps/server/src/usage/HeadlessUsageRefresher.settings.test.ts‎

Copy file name to clipboard
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Fork: `headlessUsageRefreshInterval` through the real settings service: old files, patches,
3+
* the sparse settings file and restarts.
4+
*/
5+
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
6+
import * as NodeServices from "@effect/platform-node/NodeServices";
7+
import { DEFAULT_SERVER_SETTINGS, ServerSettings, ServerSettingsPatch } from "@t3tools/contracts";
8+
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
9+
import { assert, it } from "@effect/vitest";
10+
import * as Duration from "effect/Duration";
11+
import * as Effect from "effect/Effect";
12+
import * as FileSystem from "effect/FileSystem";
13+
import * as Layer from "effect/Layer";
14+
import * as Schema from "effect/Schema";
15+
16+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
17+
import * as ServerConfig from "../config.ts";
18+
import * as SqlitePersistence from "../persistence/Sqlite.ts";
19+
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
20+
import * as ServerSettingsModule from "../serverSettings.ts";
21+
22+
const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);
23+
const decodeSettingsJson = Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings));
24+
25+
/** One settings service; each provide is one server run over the same settings file. */
26+
const serverRun = () =>
27+
ServerSettingsModule.layer.pipe(
28+
Layer.provide(ServerSecretStore.layer),
29+
Layer.provide(ManagedPolicy.layerTest()),
30+
Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
31+
);
32+
33+
const minutesOf = (value: Duration.Duration) => Duration.toMillis(value) / 60_000;
34+
35+
it("a patch keeps the interval a whole Duration and leaves it alone when unrelated", () => {
36+
const set = applyServerSettingsPatch(
37+
DEFAULT_SERVER_SETTINGS,
38+
decodePatch({ headlessUsageRefreshInterval: 15 * 60_000 }),
39+
);
40+
assert.isTrue(Duration.isDuration(set.headlessUsageRefreshInterval));
41+
assert.equal(minutesOf(set.headlessUsageRefreshInterval), 15);
42+
const unrelated = applyServerSettingsPatch(set, decodePatch({ enableAgentBrowserAccess: false }));
43+
assert.equal(minutesOf(unrelated.headlessUsageRefreshInterval), 15);
44+
const off = applyServerSettingsPatch(set, decodePatch({ headlessUsageRefreshInterval: 0 }));
45+
assert.equal(Duration.toMillis(off.headlessUsageRefreshInterval), 0);
46+
});
47+
48+
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("settings file", (it) => {
49+
it.effect("an old file gets the default; a saved interval survives a restart, unchanged", () =>
50+
Effect.gen(function* () {
51+
const { settingsPath } = yield* ServerConfig.ServerConfig;
52+
const fs = yield* FileSystem.FileSystem;
53+
// Old state: a settings file written before the setting existed.
54+
yield* fs.writeFileString(settingsPath, `{ "enableAgentBrowserAccess": false }`);
55+
const first = yield* Effect.gen(function* () {
56+
const service = yield* ServerSettingsModule.ServerSettingsService;
57+
assert.equal(minutesOf((yield* service.getSettings).headlessUsageRefreshInterval), 10);
58+
const saved = yield* service.updateSettings(
59+
decodePatch({ headlessUsageRefreshInterval: 30 * 60_000 }),
60+
);
61+
assert.isTrue(Duration.isDuration(saved.headlessUsageRefreshInterval));
62+
assert.equal(minutesOf(saved.headlessUsageRefreshInterval), 30);
63+
// A second, unrelated save keeps it.
64+
yield* service.updateSettings(decodePatch({ enableAgentBrowserAccess: true }));
65+
return JSON.parse(yield* fs.readFileString(settingsPath));
66+
}).pipe(Effect.provide(serverRun()));
67+
assert.equal(first.headlessUsageRefreshInterval, 30 * 60_000);
68+
assert.equal(
69+
minutesOf((yield* decodeSettingsJson(JSON.stringify(first))).headlessUsageRefreshInterval),
70+
30,
71+
);
72+
73+
// A restart reads it back; turning it off and back to the default keeps the file sparse.
74+
const second = yield* Effect.gen(function* () {
75+
const service = yield* ServerSettingsModule.ServerSettingsService;
76+
assert.equal(minutesOf((yield* service.getSettings).headlessUsageRefreshInterval), 30);
77+
yield* service.updateSettings(decodePatch({ headlessUsageRefreshInterval: 0 }));
78+
const off = JSON.parse(yield* fs.readFileString(settingsPath));
79+
yield* service.updateSettings(decodePatch({ headlessUsageRefreshInterval: 10 * 60_000 }));
80+
return { off, back: JSON.parse(yield* fs.readFileString(settingsPath)) };
81+
}).pipe(Effect.provide(serverRun()));
82+
assert.equal(second.off.headlessUsageRefreshInterval, 0);
83+
assert.notProperty(second.back, "headlessUsageRefreshInterval");
84+
}).pipe(
85+
Effect.provide(
86+
Layer.fresh(
87+
ServerConfig.layerTest(process.cwd(), { prefix: "t3code-headless-usage-settings-test-" }),
88+
),
89+
),
90+
),
91+
);
92+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.