Skip to content

Commit 2cff4c3

Browse files
committed
feat(server): make the pull request lookup interval a background activity setting
1 parent dfbb11b commit 2cff4c3

7 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as NodeChildProcess from "node:child_process";
66
import * as NodeServices from "@effect/platform-node/NodeServices";
77
import { it } from "@effect/vitest";
88
import * as Duration from "effect/Duration";
9+
import * as TestClock from "effect/testing/TestClock";
910
import * as Effect from "effect/Effect";
1011
import * as FileSystem from "effect/FileSystem";
1112
import * as Layer from "effect/Layer";
@@ -1066,6 +1067,44 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
10661067
}),
10671068
);
10681069

1070+
it.effect.each([
1071+
["balanced", 5],
1072+
["performance", 1],
1073+
] as const)(
1074+
"asks the host for an unchanged branch once per %s lookup interval",
1075+
([profile, minutes]) =>
1076+
Effect.gen(function* () {
1077+
const repoDir = yield* makeTempDir("t3code-git-manager-");
1078+
yield* initRepo(repoDir);
1079+
const remoteDir = yield* createBareRemote();
1080+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
1081+
yield* runGit(repoDir, ["checkout", "-b", "feature/lookup-interval"]);
1082+
yield* runGit(repoDir, ["push", "-u", "origin", "feature/lookup-interval"]);
1083+
const { manager, ghCalls } = yield* makeManager({
1084+
ghScenario: { prListSequence: ["[]"] },
1085+
serverSettings: { backgroundActivity: { profile } },
1086+
});
1087+
const lookups = () => ghCalls.filter((call) => call.startsWith("pr list ")).length;
1088+
const ask = manager.branchPullRequest({ cwd: repoDir, branch: "feature/lookup-interval" });
1089+
1090+
yield* ask;
1091+
yield* TestClock.adjust(Duration.seconds(minutes * 60 - 1));
1092+
yield* ask;
1093+
expect(lookups()).toBe(1);
1094+
1095+
yield* TestClock.adjust("2 seconds");
1096+
yield* ask;
1097+
expect(lookups()).toBe(2);
1098+
1099+
// Turn ends and explicit refreshes do not wait for the interval.
1100+
yield* manager.branchPullRequest(
1101+
{ cwd: repoDir, branch: "feature/lookup-interval" },
1102+
{ refresh: true },
1103+
);
1104+
expect(lookups()).toBe(3);
1105+
}),
1106+
);
1107+
10691108
it.effect("turn-end refresh preserves failed PR lookup backoff", () =>
10701109
Effect.gen(function* () {
10711110
const repoDir = yield* makeTempDir("t3code-git-manager-");

‎apps/server/src/git/GitManager.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
type SourceControlWritingStyleSettings,
3636
type ThreadId,
3737
} from "@t3tools/contracts";
38+
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
3839
import {
3940
hasProjectSettingsOverrides,
4041
resolveProjectSettings,
@@ -137,9 +138,10 @@ const SHORT_SHA_LENGTH = 7;
137138
const TOAST_DESCRIPTION_MAX = 72;
138139
const STATUS_RESULT_CACHE_TTL = Duration.seconds(1);
139140
const STATUS_RESULT_CACHE_CAPACITY = 2_048;
140-
// Matches the automatic settlement sweep cadence so every background sweep
141-
// reads fresh branch state: an external merge settles within about a minute
142-
// instead of waiting out a longer cache. Unpublished branches never reach the
141+
// The shortest lifetime of a pull request lookup, and the cadence of the
142+
// sweeps that read it. The Background Activity `pullRequestLookupInterval`
143+
// can only lengthen it: at this floor an external merge settles within about a
144+
// minute, at a longer interval within that interval. Unpublished branches never reach the
143145
// host (a local probe answers first), and failed lookups still back off
144146
// exponentially via prLookupFailureTtl, so throttling pressure still drops
145147
// under 429s instead of amplifying it.
@@ -1065,6 +1067,20 @@ export const make = Effect.gen(function* () {
10651067
prLookupFailureStreakByKey.set(key, streak);
10661068
return prLookupFailureTtl(streak);
10671069
};
1070+
// Every background reader of a branch's pull request (thread discovery,
1071+
// settlement, status) goes through this cache, so its lifetime is the one
1072+
// place that bounds how often the hosting CLI runs for an unchanged branch.
1073+
// Set by each lookup, read by `timeToLive` right after it.
1074+
let prLookupTtl = PR_LOOKUP_CACHE_TTL;
1075+
const readPrLookupTtl = serverSettingsService.getSettings.pipe(
1076+
Effect.map((settings) =>
1077+
Duration.max(
1078+
resolveServerBackgroundActivitySettings(settings).pullRequestLookupInterval,
1079+
PR_LOOKUP_CACHE_TTL,
1080+
),
1081+
),
1082+
Effect.orElseSucceed(() => PR_LOOKUP_CACHE_TTL),
1083+
);
10681084
const prLookupCache = yield* Cache.makeWith(
10691085
(key: string) => {
10701086
const [
@@ -1083,6 +1099,7 @@ export const make = Effect.gen(function* () {
10831099
...(remoteName.length > 0 ? { remoteName } : {}),
10841100
};
10851101
return Effect.gen(function* () {
1102+
prLookupTtl = yield* readPrLookupTtl;
10861103
const { headContext, lookup } = yield* resolveLookupHeadContext(cwd, details);
10871104
if (!lookup) {
10881105
return { latest: null, headContext };
@@ -1105,7 +1122,7 @@ export const make = Effect.gen(function* () {
11051122
timeToLive: (exit, key) => {
11061123
if (Exit.isSuccess(exit)) {
11071124
prLookupFailureStreakByKey.delete(key);
1108-
return PR_LOOKUP_CACHE_TTL;
1125+
return prLookupTtl;
11091126
}
11101127
return nextPrLookupFailureTtl(key);
11111128
},

‎apps/web/src/components/settings/SettingsPanels.logic.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export function resolveBackgroundActivityProfileOption(
177177
overrides: {
178178
automaticGitFetchInterval: resolved.automaticGitFetchInterval,
179179
providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
180+
pullRequestLookupInterval: resolved.pullRequestLookupInterval,
180181
hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
181182
hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
182183
idleClientTtl: resolved.idleClientTtl,
@@ -316,6 +317,7 @@ export function backgroundActivityOverrideSettings(
316317
const nextOverrides: BackgroundActivityOverridePatch = {
317318
automaticGitFetchInterval: resolved.automaticGitFetchInterval,
318319
providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
320+
pullRequestLookupInterval: resolved.pullRequestLookupInterval,
319321
hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
320322
hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
321323
idleClientTtl: resolved.idleClientTtl,

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ function BackgroundActivityAdvancedDialog({
879879
const providerHealthRefreshIntervalSeconds = durationToSeconds(
880880
resolvedBackgroundActivity.providerHealthRefreshInterval,
881881
);
882+
const pullRequestLookupIntervalMinutes = Math.round(
883+
durationToSeconds(resolvedBackgroundActivity.pullRequestLookupInterval) / 60,
884+
);
882885
const hostPowerMonitorActiveIntervalSeconds = durationToSeconds(
883886
resolvedBackgroundActivity.hostPowerMonitorActiveInterval,
884887
);
@@ -979,6 +982,45 @@ function BackgroundActivityAdvancedDialog({
979982
</div>
980983
</div>
981984

985+
<div className="flex flex-col gap-3 border-b px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
986+
<div className="min-w-0 space-y-1">
987+
<div className="text-sm font-medium">Pull request lookup interval</div>
988+
<p className="text-xs leading-relaxed text-muted-foreground">
989+
Ask the Git host again for a branch's pull request. Finished turns and refreshes
990+
ask right away.
991+
</p>
992+
</div>
993+
<div className="flex shrink-0 items-center gap-2">
994+
<NumberField
995+
value={pullRequestLookupIntervalMinutes}
996+
min={1}
997+
step={1}
998+
size="sm"
999+
className="w-32"
1000+
onValueChange={(value) =>
1001+
updateSettings(
1002+
backgroundActivityOverrideSettings(
1003+
settings.backgroundActivity,
1004+
resolvedBackgroundActivity,
1005+
{
1006+
pullRequestLookupInterval: Duration.minutes(
1007+
normalizeIntervalSeconds(value, 1),
1008+
),
1009+
},
1010+
),
1011+
)
1012+
}
1013+
>
1014+
<NumberFieldGroup>
1015+
<NumberFieldDecrement aria-label="Decrease pull request lookup interval" />
1016+
<NumberFieldInput aria-label="Pull request lookup interval in minutes" />
1017+
<NumberFieldIncrement aria-label="Increase pull request lookup interval" />
1018+
</NumberFieldGroup>
1019+
</NumberField>
1020+
<span className="text-xs text-muted-foreground">minutes</span>
1021+
</div>
1022+
</div>
1023+
9821024
<div className="flex flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
9831025
<div className="min-w-0 space-y-1">
9841026
<div className="text-sm font-medium">Provider health interval</div>

‎apps/web/src/components/settings/settingsSearch.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export const SETTINGS_SEARCH_ITEMS = [
391391
to: "/settings/general",
392392
scope: "environment-defaults",
393393
searchTerms: [
394-
"balanced performance battery saver advanced git fetch provider health refresh host power monitor idle policy",
394+
"balanced performance battery saver advanced git fetch pull request lookup provider health refresh host power monitor idle policy",
395395
],
396396
},
397397
{

‎packages/contracts/src/settings.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ export type SourceControlWritingStyleSettings = typeof SourceControlWritingStyle
913913

914914
export const DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL = Duration.seconds(30);
915915
export const DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL = Duration.minutes(5);
916+
export const DEFAULT_PULL_REQUEST_LOOKUP_INTERVAL = Duration.minutes(5);
916917

917918
export const BackgroundActivityProfile = Schema.Literals([
918919
"balanced",
@@ -933,6 +934,7 @@ export type BackgroundActivityProfileSelection = typeof BackgroundActivityProfil
933934
export const BackgroundActivityOverrides = Schema.Struct({
934935
automaticGitFetchInterval: Schema.optionalKey(Schema.DurationFromMillis),
935936
providerHealthRefreshInterval: Schema.optionalKey(Schema.DurationFromMillis),
937+
pullRequestLookupInterval: Schema.optionalKey(Schema.DurationFromMillis),
936938
hostPowerMonitorActiveInterval: Schema.optionalKey(Schema.DurationFromMillis),
937939
hostPowerMonitorIdleInterval: Schema.optionalKey(Schema.DurationFromMillis),
938940
idleClientTtl: Schema.optionalKey(Schema.DurationFromMillis),

‎packages/shared/src/backgroundActivitySettings.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DEFAULT_BACKGROUND_ACTIVITY_PROFILE,
55
DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL,
66
DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL,
7+
DEFAULT_PULL_REQUEST_LOOKUP_INTERVAL,
78
type ServerSettings,
89
} from "@t3tools/contracts";
910
import * as Duration from "effect/Duration";
@@ -12,6 +13,8 @@ export interface ResolvedBackgroundActivitySettings {
1213
readonly profile: BackgroundActivityProfile;
1314
readonly automaticGitFetchInterval: Duration.Duration;
1415
readonly providerHealthRefreshInterval: Duration.Duration;
16+
/** How long a branch keeps its last pull request lookup before the hosting CLI is asked again. */
17+
readonly pullRequestLookupInterval: Duration.Duration;
1518
readonly hostPowerMonitorActiveInterval: Duration.Duration;
1619
readonly hostPowerMonitorIdleInterval: Duration.Duration;
1720
readonly idleClientTtl: Duration.Duration;
@@ -26,6 +29,7 @@ const PRESET_SETTINGS: Record<BackgroundActivityProfile, ResolvedBackgroundActiv
2629
profile: "performance",
2730
automaticGitFetchInterval: Duration.seconds(15),
2831
providerHealthRefreshInterval: Duration.minutes(1),
32+
pullRequestLookupInterval: Duration.minutes(1),
2933
hostPowerMonitorActiveInterval: Duration.seconds(30),
3034
hostPowerMonitorIdleInterval: Duration.minutes(2),
3135
idleClientTtl: Duration.seconds(45),
@@ -38,6 +42,7 @@ const PRESET_SETTINGS: Record<BackgroundActivityProfile, ResolvedBackgroundActiv
3842
profile: "balanced",
3943
automaticGitFetchInterval: DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL,
4044
providerHealthRefreshInterval: DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL,
45+
pullRequestLookupInterval: DEFAULT_PULL_REQUEST_LOOKUP_INTERVAL,
4146
hostPowerMonitorActiveInterval: Duration.seconds(30),
4247
hostPowerMonitorIdleInterval: Duration.minutes(5),
4348
idleClientTtl: Duration.seconds(45),
@@ -50,6 +55,7 @@ const PRESET_SETTINGS: Record<BackgroundActivityProfile, ResolvedBackgroundActiv
5055
profile: "battery-saver",
5156
automaticGitFetchInterval: Duration.seconds(0),
5257
providerHealthRefreshInterval: Duration.minutes(15),
58+
pullRequestLookupInterval: Duration.minutes(15),
5359
hostPowerMonitorActiveInterval: Duration.minutes(1),
5460
hostPowerMonitorIdleInterval: Duration.minutes(10),
5561
idleClientTtl: Duration.seconds(45),
@@ -87,6 +93,8 @@ export function resolveBackgroundActivitySettings(
8793
overrides.automaticGitFetchInterval ?? preset.automaticGitFetchInterval,
8894
providerHealthRefreshInterval:
8995
overrides.providerHealthRefreshInterval ?? preset.providerHealthRefreshInterval,
96+
pullRequestLookupInterval:
97+
overrides.pullRequestLookupInterval ?? preset.pullRequestLookupInterval,
9098
hostPowerMonitorActiveInterval:
9199
overrides.hostPowerMonitorActiveInterval ?? preset.hostPowerMonitorActiveInterval,
92100
hostPowerMonitorIdleInterval:
@@ -110,6 +118,7 @@ function resolvedSettingsEqual(
110118
return (
111119
durationsEqual(a.automaticGitFetchInterval, b.automaticGitFetchInterval) &&
112120
durationsEqual(a.providerHealthRefreshInterval, b.providerHealthRefreshInterval) &&
121+
durationsEqual(a.pullRequestLookupInterval, b.pullRequestLookupInterval) &&
113122
durationsEqual(a.hostPowerMonitorActiveInterval, b.hostPowerMonitorActiveInterval) &&
114123
durationsEqual(a.hostPowerMonitorIdleInterval, b.hostPowerMonitorIdleInterval) &&
115124
durationsEqual(a.idleClientTtl, b.idleClientTtl) &&
@@ -160,6 +169,9 @@ export function normalizeBackgroundActivitySettings(
160169
)
161170
? { providerHealthRefreshInterval: resolved.providerHealthRefreshInterval }
162171
: {}),
172+
...(!durationsEqual(resolved.pullRequestLookupInterval, preset.pullRequestLookupInterval)
173+
? { pullRequestLookupInterval: resolved.pullRequestLookupInterval }
174+
: {}),
163175
...(!durationsEqual(
164176
resolved.hostPowerMonitorActiveInterval,
165177
preset.hostPowerMonitorActiveInterval,
@@ -258,6 +270,7 @@ export function normalizeServerBackgroundActivitySettings(
258270
overrides: {
259271
automaticGitFetchInterval: resolved.automaticGitFetchInterval,
260272
providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
273+
pullRequestLookupInterval: resolved.pullRequestLookupInterval,
261274
hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
262275
hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
263276
idleClientTtl: resolved.idleClientTtl,

0 commit comments

Comments
 (0)