Skip to content

Commit dabab45

Browse files
authored
perf(server): skip no-op settlement decisions (pingdotgg#27897)
Co-authored-by: Omar <[email protected]>
1 parent bc448b8 commit dabab45

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
571571
const snapshotReads = yield* Queue.unbounded<number>();
572572
const candidateReads = yield* Ref.make<ReadonlyArray<string | undefined>>([]);
573573
const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS);
574+
const settingsReadCount = yield* Ref.make(0);
574575
const settingsChanges = yield* PubSub.unbounded<ContractServerSettings>();
575576
const mergedPullRequests = yield* PubSub.unbounded<PullRequestService.PullRequestMergeEvent>();
576577
const commands = yield* Ref.make<ReadonlyArray<AutoSettleCommand>>([]);
@@ -640,7 +641,9 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
640641
const serverSettings = ServerSettings.ServerSettingsService.of({
641642
start: Effect.void,
642643
ready: Effect.void,
643-
getSettings: Ref.get(settings),
644+
getSettings: Ref.update(settingsReadCount, (count) => count + 1).pipe(
645+
Effect.andThen(Ref.get(settings)),
646+
),
644647
updateSettings,
645648
updateProviderInstance: () => Effect.die("Unexpected provider mutation"),
646649
withSettingsSnapshot: (use) => Ref.get(settings).pipe(Effect.flatMap(use)),
@@ -710,6 +713,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
710713
activation,
711714
snapshots,
712715
snapshotReadCount,
716+
settingsReadCount,
713717
snapshotReads,
714718
candidateReads,
715719
commands,
@@ -746,6 +750,37 @@ const startHarness = Effect.fn("startThreadSettlementHarness")(function* (
746750
});
747751

748752
describe("ThreadSettlementServiceV2 worker", () => {
753+
it.effect("skips settings and PR decisions for recent branchless threads", () =>
754+
Effect.scoped(
755+
Effect.gen(function* () {
756+
yield* TestClock.setTime(Date.parse(NOW));
757+
const threads = Array.from({ length: 32 }, (_, index) =>
758+
makeThread(`recent-${index}`, {
759+
updatedAt: DateTime.makeUnsafe(NOW),
760+
latestUserMessageAt: DateTime.makeUnsafe(Date.parse(NOW) - 2 * DAY_MS),
761+
}),
762+
);
763+
const fixture = yield* makeHarness({
764+
snapshot: makeSnapshot(threads),
765+
settings: { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 3 },
766+
});
767+
yield* Effect.gen(function* () {
768+
const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
769+
yield* startHarness(service, fixture.activation, fixture.snapshotReads);
770+
assert.deepEqual(yield* Ref.get(fixture.commands), []);
771+
assert.deepEqual(yield* Ref.get(fixture.branchCalls), []);
772+
assert.equal(yield* Ref.get(fixture.settingsReadCount), 2);
773+
yield* fixture.updateSettings({ sidebarAutoSettleAfterDays: 1 });
774+
yield* Queue.take(fixture.snapshotReads);
775+
yield* service.drain;
776+
assert.deepEqual(
777+
new Set((yield* Ref.get(fixture.commands)).map((command) => command.threadId)),
778+
new Set(threads.map((thread) => thread.id)),
779+
);
780+
}).pipe(Effect.provide(fixture.layer));
781+
}),
782+
),
783+
);
749784
it.effect("settles a merged pull request stored only in the thread links", () =>
750785
Effect.scoped(
751786
Effect.gen(function* () {

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,29 @@ export const make = Effect.gen(function* () {
364364

365365
const lookupCandidates = (yield* Effect.forEach(
366366
candidates,
367-
(thread) => settleThread(thread, null),
367+
(thread) => {
368+
const projectSettings = resolveProjectSettings(settings, thread.projectId).settings;
369+
// Skip no-op decisions; actual settlement still reads fresh settings before dispatch.
370+
const settledAt = resolveAutoSettlementAt({
371+
thread,
372+
pullRequest: null,
373+
nowMs,
374+
autoSettleAfterDays: projectSettings.sidebarAutoSettleAfterDays,
375+
autoSettleOnMerge: projectSettings.sidebarAutoSettleOnMerge,
376+
autoSettleKeepPullRequests: projectSettings.sidebarAutoSettleKeepPullRequests,
377+
});
378+
return settledAt === null ? Effect.succeed(thread) : settleThread(thread, null);
379+
},
368380
{ concurrency: 8 },
369381
))
370382
.filter((thread) => thread !== null)
371-
.filter((thread) => visibleThreadPullRequests(thread.pullRequests ?? []).length === 0);
383+
.filter((thread) => visibleThreadPullRequests(thread.pullRequests ?? []).length === 0)
384+
.filter(
385+
(thread) =>
386+
thread.branch !== null ||
387+
thread.linkedPullRequest != null ||
388+
thread.branchPullRequest != null,
389+
);
372390
const lookupCwdByThreadId = new Map<string, string>();
373391
yield* Effect.forEach(
374392
lookupCandidates,

0 commit comments

Comments
 (0)