Skip to content

Commit c3045c1

Browse files
perf(server): limit startup PR discovery to recent settlements
Port the seven-day startup window from #13757 to the v2 service. Co-authored-by: Theo Browne <[email protected]>
1 parent 1451e7c commit c3045c1

2 files changed

Lines changed: 119 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,117 @@ describe("ThreadPullRequestServiceV2 reads", () => {
377377
),
378378
);
379379

380+
it.effect(
381+
"backfills only the last seven days of settled threads and keeps discovering unsettled threads",
382+
() =>
383+
Effect.scoped(
384+
Effect.gen(function* () {
385+
yield* TestClock.setTime(DateTime.toEpochMillis(NOW));
386+
const boundary = DateTime.subtract(NOW, { days: 7 });
387+
const old = DateTime.subtract(boundary, { milliseconds: 1 });
388+
const threads = [
389+
{ id: "recent", settledAt: NOW },
390+
{ id: "boundary", settledAt: boundary },
391+
{ id: "old", settledAt: old, updatedAt: old },
392+
// Auto-settle records old activity in settledAt but updates updatedAt now.
393+
{ id: "auto-settled", settledAt: old, updatedAt: NOW },
394+
{ id: "old-unsettled", updatedAt: old },
395+
{ id: "recent-override", settledOverride: "settled" as const, updatedAt: NOW },
396+
{ id: "old-override", settledOverride: "settled" as const, updatedAt: old },
397+
].map(({ id, ...overrides }) => ({
398+
...threadShell(id),
399+
...overrides,
400+
branch: id,
401+
})) satisfies ReadonlyArray<OrchestrationV2ThreadShell>;
402+
const project: OrchestrationProjectShell = {
403+
id: ProjectId.make("project-1"),
404+
title: "Project",
405+
workspaceRoot: "/workspace/project",
406+
defaultModelSelection: null,
407+
scripts: [],
408+
createdAt: "2026-08-01T00:00:00.000Z",
409+
updatedAt: "2026-08-01T00:00:00.000Z",
410+
};
411+
const activation = yield* Deferred.make<void>();
412+
const sweeps = yield* Queue.unbounded<void>();
413+
const branches: Array<string> = [];
414+
const layerDependencies = Layer.mergeAll(
415+
Layer.mock(Orchestrator.OrchestratorV2)({
416+
streamDomainEvents: Stream.never,
417+
getShellSnapshot: (options) =>
418+
Queue.offer(sweeps, undefined).pipe(
419+
Effect.as({
420+
schemaVersion: 2,
421+
snapshotSequence: 1,
422+
threads: options?.unsettledOnly
423+
? threads.filter(
424+
(thread) =>
425+
thread.settledAt === null && thread.settledOverride !== "settled",
426+
)
427+
: threads,
428+
archivedThreads: [],
429+
}),
430+
),
431+
}),
432+
Layer.mock(ProjectStore.ProjectStoreV2)({
433+
listShells: () => Effect.succeed([project]),
434+
}),
435+
Layer.mock(GitManager.GitManager)({
436+
branchPullRequest: ({ branch }) =>
437+
Effect.sync(() => {
438+
branches.push(branch);
439+
return null;
440+
}),
441+
}),
442+
Layer.mock(PullRequestService.PullRequestService)({}),
443+
Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
444+
resolve: () =>
445+
Effect.succeed({
446+
canonicalKey: "github.com/acme/project",
447+
locator: {
448+
source: "git-remote" as const,
449+
remoteName: "origin",
450+
remoteUrl: "[email protected]:acme/project.git",
451+
},
452+
provider: "github" as const,
453+
owner: "acme",
454+
name: "project",
455+
}),
456+
}),
457+
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
458+
Layer.succeed(
459+
Crypto.Crypto,
460+
Crypto.make({
461+
randomBytes: (size) => new Uint8Array(size).fill(1),
462+
digest: (_algorithm, data) => Effect.succeed(data),
463+
}),
464+
),
465+
FileSystem.layerNoop({}),
466+
);
467+
468+
yield* Effect.gen(function* () {
469+
const service = yield* ThreadPullRequestService.make;
470+
yield* service.start();
471+
yield* Deferred.succeed(activation, undefined);
472+
yield* Queue.take(sweeps);
473+
yield* service.drain;
474+
const startupBranches = branches.splice(0).sort();
475+
476+
yield* TestClock.adjust("1 minute");
477+
yield* Queue.take(sweeps);
478+
yield* service.drain;
479+
expect(branches).toEqual(["old-unsettled"]);
480+
expect(startupBranches).toEqual([
481+
"boundary",
482+
"old-unsettled",
483+
"recent",
484+
"recent-override",
485+
]);
486+
}).pipe(Effect.provide(layerDependencies));
487+
}),
488+
),
489+
);
490+
380491
it.effect("periodic sweeps read only unsettled threads once backfill is done", () =>
381492
Effect.scoped(
382493
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/ThreadPullRequestService.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import {
1313
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
1414
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
1515
import * as Cause from "effect/Cause";
16+
import * as Clock from "effect/Clock";
1617
import * as Context from "effect/Context";
1718
import * as Crypto from "effect/Crypto";
19+
import * as DateTime from "effect/DateTime";
20+
import * as Duration from "effect/Duration";
1821
import * as Effect from "effect/Effect";
1922
import * as FileSystem from "effect/FileSystem";
2023
import * as Layer from "effect/Layer";
@@ -91,6 +94,7 @@ export function projectWorkspaceMatchesSnapshot(
9194
}
9295

9396
const BACKFILL_ATTEMPTS = 5;
97+
const BACKFILL_WINDOW = Duration.days(7);
9498

9599
interface RefreshRequest {
96100
readonly threadId: ThreadId | null;
@@ -145,10 +149,13 @@ export const make = Effect.gen(function* () {
145149
]);
146150
const projects = new Map(projectShells.map((project) => [project.id, project]));
147151
if (request.backfill) {
152+
const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(BACKFILL_WINDOW);
148153
for (const thread of threadSnapshot.threads) {
154+
// Auto-settle updates updatedAt, so prefer the last activity in settledAt.
149155
if (
150156
(thread.settledOverride === "settled" || thread.settledAt !== null) &&
151-
thread.branchPullRequest == null
157+
thread.branchPullRequest == null &&
158+
DateTime.toEpochMillis(thread.settledAt ?? thread.updatedAt) >= cutoff
152159
) {
153160
pendingBackfill.set(thread.id, BACKFILL_ATTEMPTS);
154161
}

0 commit comments

Comments
 (0)