Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {

/** Checks take minutes; faster sweeps waste the account's shared host quota. */
const SWEEP_MINUTES = 2;
/** Between sweeps, pending checks get a fingerprint probe this often; a full read only follows a change. */
const PENDING_PROBE_MS = 30_000;
const READ_FAILURE_LIMIT = 8;
/** Group reads in flight at once, inside the host's own request limit. */
const GROUP_READS = 4;
Expand Down Expand Up @@ -88,6 +90,8 @@ interface LastRead {
readonly at: number;
readonly fingerprint: string;
readonly inFlight: boolean;
/** Some watch still waits on checks, including a head whose checks have not registered yet. */
readonly checksPending: boolean;
readonly remarksComplete: boolean;
readonly watches: ReadonlySet<string>;
readonly status: string | null;
Expand Down Expand Up @@ -681,6 +685,12 @@ export const make = Effect.gen(function* () {
fingerprint === null ? QUIET_REREAD_MS : FINGERPRINT_REREAD_MS,
)
: null;
const reports = new Map(
group.targets.map((target) => [
target,
evaluatePullRequestWatch(target.watch, detail, remarks, now),
]),
);
lastReads.set(group.key, {
at:
plan.activity || last === undefined
Expand All @@ -690,13 +700,14 @@ export const make = Effect.gen(function* () {
inFlight:
detail.mergeability === "unknown" ||
detail.checks.some((check) => check.status === "pending"),
checksPending: [...reports.values()].some((report) => report.next.checksPending === true),
remarksComplete: plan.activity ? remarks !== null : (last?.remarksComplete ?? false),
watches: new Set(group.targets.map(watchKey)),
status: fingerprint?.status ?? null,
remarks: fingerprint?.remarks ?? null,
});
yield* eachTarget(group, (target) => {
const report = evaluatePullRequestWatch(target.watch, detail, remarks, now);
const report = reports.get(target)!;
if (report.changes.length > 0) {
const wake = {
...pullRequestWatchMessage({
Expand Down Expand Up @@ -839,6 +850,34 @@ export const make = Effect.gen(function* () {
Effect.withSpan("PullRequestWatchReactor.readOnChange"),
);

// Hosts without fingerprints are left to the sweep, since every probe there would be a full read.
// One pass per moved pull request covers every project watching it.
const probePending = Effect.gen(function* () {
const moved = new Set<string>();
yield* Effect.forEach(
currentGroups.filter((group) => {
const last = lastReads.get(group.key);
return last !== undefined && (last.inFlight || last.checksPending) && last.status !== null;
}),
(group) =>
fingerprintOf(group).pipe(
Effect.map((fingerprint) => {
const last = lastReads.get(group.key);
if (
fingerprint !== null &&
fingerprint !== "paused" &&
last !== undefined &&
(last.status !== fingerprint.status || last.remarks !== fingerprint.remarks)
) {
moved.add(pullRequestOf(group));
}
}),
),
{ concurrency: "unbounded", discard: true },
);
yield* Effect.forEach(moved, readOnChange, { concurrency: "unbounded", discard: true });
});

const start: PullRequestWatchReactor["Service"]["start"] = Effect.fn(
"PullRequestWatchReactor.start",
)(function* () {
Expand Down Expand Up @@ -870,6 +909,9 @@ export const make = Effect.gen(function* () {
Effect.asVoid,
),
);
// A host without a live feed leaves the sweep as the only reader, so a pending watch
// would wait the whole sweep for its checks; the probes catch the conclusion sooner.
yield* forkParked(probePending.pipe(Effect.delay(PENDING_PROBE_MS), Effect.forever));
});

return { start, sweep } satisfies PullRequestWatchReactor["Service"];
Expand Down
131 changes: 131 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5779,6 +5779,137 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);

// A fresh head has no checks yet; the watch waits for them all the same.
it.effect.each(["pending", "not yet registered"] as const)(
"wakes %s checks from the interim probe when the host has no live feed",
(start) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const threadId = ThreadId.make(`runtime-pull-request-watch-probe-${start}`);
const projectId = ProjectId.make(`pr-watch-probe-project-${start}`);
yield* seedProject({
projectId,
title: "Watch probe",
workspaceRoot: "/workspace/watch-probe",
defaultModelSelection: null,
createdAt: "2026-10-01T00:00:00.000Z",
});
yield* orchestrator.dispatch({
type: "thread.create",
createdBy: "user",
creationSource: "web",
commandId: CommandId.make(`pr-watch-probe-create-${start}`),
threadId,
projectId,
title: "Watch probe",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
});
const key = { host: "github.com", repository: "pingdotgg/t3code", number: 14 };
yield* orchestrator.dispatch({
type: "thread.pull-request.watch",
commandId: CommandId.make(`pr-watch-probe-start-${start}`),
threadId,
...key,
watching: true,
link: { url: "https://github.com/pingdotgg/t3code/pull/14", source: "agent" },
});

let checks: PullRequestDetail["checks"] =
start === "pending"
? [{ name: "lint", status: "pending", description: null, url: null, required: true }]
: [];
let status: string = start;
const detailReads = yield* Queue.unbounded<void>();
const fingerprintReads = yield* Queue.unbounded<void>();
const host = Layer.mergeAll(
NodeServices.layer,
NodeCrypto.layer,
Layer.mock(PullRequestService.PullRequestService)({
detail: (input) =>
// Watches left by earlier cases in this layer are swept too.
(input.projectId === projectId
? Queue.offer(detailReads, undefined)
: Effect.void
).pipe(
Effect.as({
...watchedPullRequestDetail({
projectId,
number: 14,
at: "2026-10-02T12:00:00.000Z",
}),
headSha: "6074dd48ff",
checks,
}),
),
activity: () =>
Effect.succeed({
comments: [],
commentCount: 0,
commentsTruncated: false,
reviewThreads: [],
commits: [],
}),
watchFingerprint: (input) =>
(input.projectId === projectId
? Queue.offer(fingerprintReads, undefined)
: Effect.void
).pipe(Effect.andThen(Effect.sync(() => ({ status, remarks: "none" })))),
invalidate: () => Effect.void,
// Without the GitHub app sign-in no feed is on, so only the sweep hears checks.
changes: () => Stream.empty,
}),
);
const wakes = Effect.map(
orchestrator.getThreadRecords(threadId, ["messages"]),
({ messages }) =>
messages.filter(
(message) => message.notification !== undefined && message.notification !== null,
),
);
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
yield* reactor.start();
yield* Queue.take(fingerprintReads);
yield* Queue.take(detailReads);
yield* worker.drain();
assert.lengthOf(yield* wakes, 0);

// A probe whose fingerprint has not moved reads nothing more.
yield* TestClock.adjust("30 seconds");
yield* Queue.take(fingerprintReads);
status = "passed";
checks = [
{ name: "lint", status: "success", description: null, url: null, required: true },
];
// The probe must hear the concluded checks long before the 2-minute sweep could.
yield* TestClock.adjust("30 seconds");
yield* Queue.take(detailReads);
yield* worker.drain();
const woken = yield* wakes;
assert.lengthOf(woken, 1);
assert.equal(woken[0]!.notification?.summary, "#14: checks passed");
assert.equal(yield* Queue.size(detailReads), 0);

// The settled pull request stays quiet until the sweep.
yield* TestClock.adjust("30 seconds");
yield* worker.drain();
assert.lengthOf(yield* wakes, 1);

yield* orchestrator.dispatch({
type: "thread.pull-request.watch",
commandId: CommandId.make(`pr-watch-probe-end-${start}`),
threadId,
...key,
watching: false,
});
yield* worker.drain();
}),
);

// regression: #21155 watching a pull request again repeated news the thread already got
it.effect("watching a dismissed stack layer again keeps what its last watch told", () =>
Effect.gen(function* () {
Expand Down
Loading