Skip to content

Commit 7040139

Browse files
Yuruzuuclaude
andcommitted
perf(server): PR watches wake 14× sooner and stop waiting on unrelated reads (935 ms → 65 ms median)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3d369d5 commit 7040139

9 files changed

Lines changed: 436 additions & 226 deletions

File tree

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

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Layer from "effect/Layer";
2424
import * as Option from "effect/Option";
2525
import * as Schedule from "effect/Schedule";
2626
import * as Schema from "effect/Schema";
27+
import * as Semaphore from "effect/Semaphore";
2728
import type * as Scope from "effect/Scope";
2829

2930
import {
@@ -43,6 +44,9 @@ import {
4344
/** Checks take minutes; faster sweeps waste the account's shared host quota. */
4445
const SWEEP_MINUTES = 2;
4546
const READ_FAILURE_LIMIT = 8;
47+
/** Group reads and thread tails in flight at once, inside the host's own request limit. */
48+
const GROUP_READS = 4;
49+
const TAIL_READS = 4;
4650
/** Without fingerprints, reread quiet PRs on snapshot changes or this interval to catch edits. */
4751
const QUIET_REREAD_MS = 10 * 60_000;
4852
/** Reread despite unchanged fingerprints; they miss review-thread comment edits. */
@@ -335,39 +339,48 @@ export const make = Effect.gen(function* () {
335339
paginated.every(
336340
(thread) => cached.tails.get(thread.id)?.count === (thread.commentCount ?? 0),
337341
);
342+
// Each thread's cursor chain stays serial; separate threads page side by side.
343+
const read = yield* Effect.forEach(
344+
paginated,
345+
(thread) =>
346+
Effect.gen(function* () {
347+
const count = thread.commentCount ?? 0;
348+
const known = reuse ? cached.tails.get(thread.id) : undefined;
349+
if (known !== undefined) return known;
350+
let cursor = thread.nextCommentsCursor ?? null;
351+
const comments = new Map<string, PullRequestComment>();
352+
const cursors = new Set<string>();
353+
while (cursor !== null) {
354+
if (cursors.has(cursor)) return null;
355+
cursors.add(cursor);
356+
const page: PullRequestThreadCommentsResult = yield* pullRequests.threadComments({
357+
...reference,
358+
threadId: thread.id,
359+
cursor,
360+
});
361+
for (const comment of page.comments) {
362+
comments.set(comment.id, {
363+
...comment,
364+
kind: "review-comment",
365+
path: thread.path,
366+
reviewState: null,
367+
});
368+
}
369+
cursor = page.nextCursor;
370+
}
371+
if (thread.comments.length + comments.size < count) return null;
372+
return { count, comments: [...comments.values()] };
373+
}),
374+
{ concurrency: TAIL_READS },
375+
);
338376
const tails = new Map<
339377
string,
340378
{ count: number; comments: ReadonlyArray<PullRequestComment> }
341379
>();
342380
const remarks = [...activity.comments];
343-
for (const thread of paginated) {
344-
let cursor = thread.nextCommentsCursor ?? null;
345-
const count = thread.commentCount ?? 0;
346-
let tail = reuse ? cached.tails.get(thread.id) : undefined;
347-
if (tail === undefined) {
348-
const comments = new Map<string, PullRequestComment>();
349-
const cursors = new Set<string>();
350-
while (cursor !== null) {
351-
if (cursors.has(cursor)) return null;
352-
cursors.add(cursor);
353-
const page: PullRequestThreadCommentsResult = yield* pullRequests.threadComments({
354-
...reference,
355-
threadId: thread.id,
356-
cursor,
357-
});
358-
for (const comment of page.comments) {
359-
comments.set(comment.id, {
360-
...comment,
361-
kind: "review-comment",
362-
path: thread.path,
363-
reviewState: null,
364-
});
365-
}
366-
cursor = page.nextCursor;
367-
}
368-
if (thread.comments.length + comments.size < count) return null;
369-
tail = { count, comments: [...comments.values()] };
370-
}
381+
for (const [index, thread] of paginated.entries()) {
382+
const tail = read[index];
383+
if (tail == null) return null;
371384
tails.set(thread.id, tail);
372385
remarks.push(...tail.comments);
373386
}
@@ -626,22 +639,26 @@ export const make = Effect.gen(function* () {
626639
),
627640
{ discard: true },
628641
);
629-
const fingerprints = yield* Effect.forEach(groups, fingerprintOf, {
630-
concurrency: "unbounded",
631-
});
642+
// Every fingerprint is asked for at once so the host can batch them, and each group reads
643+
// as soon as its own answer lands rather than waiting on the slowest host.
644+
const reads = yield* Semaphore.make(GROUP_READS);
632645
yield* Effect.forEach(
633646
groups,
634-
(group, index) => {
635-
const fingerprint = fingerprints[index]!;
636-
return fingerprint === "paused"
637-
? Effect.void
638-
: readGroup(group, fingerprint).pipe(
639-
Effect.catchCause(
640-
logFailure("pull request watch check failed", { pullRequest: group.key }),
641-
),
642-
);
643-
},
644-
{ concurrency: 4, discard: true },
647+
(group) =>
648+
fingerprintOf(group).pipe(
649+
Effect.flatMap((fingerprint) =>
650+
fingerprint === "paused"
651+
? Effect.void
652+
: reads.withPermit(
653+
readGroup(group, fingerprint).pipe(
654+
Effect.catchCause(
655+
logFailure("pull request watch check failed", { pullRequest: group.key }),
656+
),
657+
),
658+
),
659+
),
660+
),
661+
{ concurrency: "unbounded", discard: true },
645662
);
646663
}).pipe(
647664
Effect.catchCause(logFailure("pull request watch sweep failed", {})),

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,6 +4257,116 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
42574257
}),
42584258
);
42594259

4260+
it.effect(
4261+
"reads each watched PR as its fingerprint lands and pages review threads side by side",
4262+
() =>
4263+
Effect.gen(function* () {
4264+
const orchestrator = yield* Orchestrator.OrchestratorV2;
4265+
const projectId = ProjectId.make("pr-watch-pipeline-project");
4266+
yield* seedProject({
4267+
projectId,
4268+
title: "Watch pipeline",
4269+
workspaceRoot: "/workspace/watch-pipeline",
4270+
defaultModelSelection: null,
4271+
createdAt: "2026-10-01T00:00:00.000Z",
4272+
});
4273+
for (const number of [21, 22]) {
4274+
const threadId = ThreadId.make(`runtime-pull-request-watch-pipeline-${number}`);
4275+
yield* orchestrator.dispatch({
4276+
type: "thread.create",
4277+
createdBy: "user",
4278+
creationSource: "web",
4279+
commandId: CommandId.make(`pr-watch-pipeline-create-${number}`),
4280+
threadId,
4281+
projectId,
4282+
title: "Watch pipeline",
4283+
modelSelection,
4284+
runtimeMode: "full-access",
4285+
interactionMode: "default",
4286+
branch: null,
4287+
worktreePath: null,
4288+
});
4289+
yield* orchestrator.dispatch({
4290+
type: "thread.pull-request.watch",
4291+
commandId: CommandId.make(`pr-watch-pipeline-start-${number}`),
4292+
threadId,
4293+
host: "github.com",
4294+
repository: "pingdotgg/t3code",
4295+
number,
4296+
watching: true,
4297+
link: { url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent" },
4298+
});
4299+
}
4300+
const at = "2026-10-02T12:00:00.000Z";
4301+
const reply = (thread: string): PullRequestComment => ({
4302+
id: `${thread}-11`,
4303+
kind: "review-comment",
4304+
author: { login: "reviewer", name: null, avatarUrl: null },
4305+
body: `Reply on ${thread}.`,
4306+
createdAt: "2999-01-01T00:00:00.000Z",
4307+
url: null,
4308+
path: "src/index.ts",
4309+
reviewState: null,
4310+
});
4311+
const reviewThread = (id: string) => ({
4312+
id,
4313+
path: "src/index.ts",
4314+
line: 1,
4315+
side: "right" as const,
4316+
isResolved: false,
4317+
isOutdated: false,
4318+
comments: [],
4319+
commentCount: 1,
4320+
nextCommentsCursor: `${id}-start`,
4321+
});
4322+
// #21's host answers only after #22 is read, and thread A's tail only once B's is asked
4323+
// for, so either wait on unrelated work never finishes.
4324+
const read22 = yield* Deferred.make<void>();
4325+
const tailB = yield* Deferred.make<void>();
4326+
const host = Layer.mergeAll(
4327+
NodeServices.layer,
4328+
NodeCrypto.layer,
4329+
Layer.mock(PullRequestService.PullRequestService)({
4330+
watchFingerprint: (input) =>
4331+
input.number === 21
4332+
? Deferred.await(read22).pipe(Effect.as(null))
4333+
: Effect.succeed(null),
4334+
detail: (input) =>
4335+
(input.number === 22 ? Deferred.succeed(read22, undefined) : Effect.void).pipe(
4336+
Effect.as(watchedPullRequestDetail({ projectId, number: input.number, at })),
4337+
),
4338+
activity: (input) =>
4339+
Effect.succeed({
4340+
comments: [],
4341+
commentCount: input.number === 22 ? 2 : 0,
4342+
commentsTruncated: input.number === 22,
4343+
reviewThreadsTruncated: false,
4344+
reviewThreads: input.number === 22 ? [reviewThread("A"), reviewThread("B")] : [],
4345+
commits: [],
4346+
}),
4347+
threadComments: (input) =>
4348+
(input.threadId === "A"
4349+
? Deferred.await(tailB)
4350+
: Deferred.succeed(tailB, undefined)
4351+
).pipe(Effect.as({ comments: [reply(input.threadId)], nextCursor: null })),
4352+
}),
4353+
);
4354+
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
4355+
yield* reactor.sweep;
4356+
4357+
const texts = (number: number) =>
4358+
orchestrator
4359+
.getThreadRecords(ThreadId.make(`runtime-pull-request-watch-pipeline-${number}`), [
4360+
"messages",
4361+
])
4362+
.pipe(Effect.map(({ messages }) => messages.map((message) => message.text).join("\n")));
4363+
assert.include(yield* texts(21), "#21");
4364+
const told = yield* texts(22);
4365+
assert.include(told, "Reply on A.");
4366+
assert.include(told, "Reply on B.");
4367+
}),
4368+
);
4369+
42604370
it.effect("persists rejected command receipts across retries", () =>
42614371
Effect.gen(function* () {
42624372
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎apps/server/src/pullRequest/PullRequestReadCache.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PR filesystem ca
4444
const invalidate = yield* cache
4545
.invalidate("pr")
4646
.pipe(Effect.forkChild({ startImmediately: true }));
47+
// Another PR's invalidation does not wait on this read; this one's does.
48+
yield* cache.invalidate("other-pr");
49+
assert.isUndefined(invalidate.pollUnsafe());
4750
yield* Deferred.succeed(release, undefined);
4851
yield* Fiber.join(read);
4952
yield* Fiber.join(invalidate);

‎apps/server/src/pullRequest/PullRequestReadCache.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { detachStackFrame } from "../git/detachStackFrame.ts";
2626
import { forkParked } from "../serverActivation.ts";
2727

2828
const CONCURRENT_READS = 512;
29+
/** Invalidation excludes only reads whose scopes share its stripe, not every PR read. */
30+
const SCOPE_STRIPES = 64;
2931
// Entry filenames are the URI-encoded `<storeId>:<key>`; the scope revisions file has
3032
// no store id. Effect before 4.1 wrote `<storeId><key>`, still pruned here.
3133
const STORE_ID = "pr-v2";
@@ -87,6 +89,11 @@ export const make = Effect.gen(function* () {
8789
const clock = yield* Clock.Clock;
8890
let enabled = true;
8991
const lock = yield* Semaphore.make(CONCURRENT_READS);
92+
const stripes = yield* Effect.forEach(Array.from({ length: SCOPE_STRIPES }), () =>
93+
Semaphore.make(CONCURRENT_READS),
94+
);
95+
const stripeOf = (scope: string) => (Hash.string(scope) >>> 0) % SCOPE_STRIPES;
96+
const revisionWrites = yield* Semaphore.make(1);
9097
const digest = (key: string) =>
9198
digester.digest(Digest.sha256, new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode));
9299
const revisions = yield* Cache.makeWith(
@@ -162,6 +169,12 @@ export const make = Effect.gen(function* () {
162169
PersistenceError: () => read,
163170
SchemaError: () => read,
164171
}),
172+
// A read stores its result under the revisions it started with, so an invalidation of
173+
// one of its scopes must wait for it; reads of other scopes run on.
174+
(effect) =>
175+
[...new Set(scopes.map(stripeOf))]
176+
.toSorted((left, right) => left - right)
177+
.reduceRight((inner, stripe) => stripes[stripe]!.withPermits(1)(inner), effect),
165178
lock.withPermits(1),
166179
);
167180
}, detachStackFrame),
@@ -182,7 +195,8 @@ export const make = Effect.gen(function* () {
182195
return Effect.logWarning("PR cache disabled after clearing failed");
183196
}),
184197
Effect.uninterruptible,
185-
lock.withPermits(CONCURRENT_READS),
198+
revisionWrites.withPermits(1),
199+
(effect, scope) => stripes[stripeOf(scope)]!.withPermits(CONCURRENT_READS)(effect),
186200
detachStackFrame,
187201
),
188202
});

0 commit comments

Comments
 (0)