@devries-cameron20 / t3code Mirror · read-only / commits / 02f8a3280b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

fix(server): PR watches wake when CI passes before the first sweep (#37741)

Co-authored-by: Usama Khan <usama@Usamas-MacBook-Pro.local> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Usama Khan committed Nov 16, 2026 at 14:07 UTC 02f8a3280b8635d75a64b21587aef678b4391b51
2 files changed +147 -2
apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
+17 -2
@@ -137,8 +137,9 @@ interface WatchLife {
137 readonly reads: number;
138 }
139
140 -const lifeKey = ({ thread, link, watch }: WatchTarget) =>
141 - `${thread.id} ${threadPullRequestKeyOf(link)} ${watch.startedAt}`;
140 +const lifeKeyOf = (threadId: string, link: ThreadPullRequestLink, watch: ThreadPullRequestWatch) =>
141 + `${threadId} ${threadPullRequestKeyOf(link)} ${watch.startedAt}`;
142 +const lifeKey = ({ thread, link, watch }: WatchTarget) => lifeKeyOf(thread.id, link, watch);
143
144 const minutes = (ms: number) => Math.max(0, Math.round(ms / 60_000));
145
@@ -863,6 +864,20 @@ export const make = Effect.gen(function* () {
864 );
865 }
866 });
867 + // A watch's first read is its baseline, so it reads now: CI that finishes before the next
868 + // sweep would otherwise be taken as old news and never wake the thread.
869 + yield* forkParked(
870 + Stream.runForEach(engine.streamDomainEvents, (event) => {
871 + if (event.type !== "thread.pull-request-synced") return Effect.void;
872 + const unread = visibleThreadPullRequests(event.payload.pullRequests ?? []).flatMap(
873 + (link) =>
874 + link.watch === undefined || lives.has(lifeKeyOf(event.threadId, link, link.watch))
875 + ? []
876 + : [threadPullRequestKeyOf(link)],
877 + );
878 + return Effect.forEach(new Set(unread), readOnChange, { discard: true });
879 + }).pipe(Effect.catchCause(logFailure("pull request watch event stream failed", {}))),
880 + );
881 yield* forkParked(
882 sweep.pipe(
883 Effect.andThen(follow),
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+130
@@ -21,6 +21,7 @@ import {
21 RuntimeRequestId,
22 TurnItemId,
23 type ModelSelection,
24 + type OrchestrationV2DomainEvent,
25 type OrchestrationV2Run,
26 type OrchestrationV2ShellSnapshot,
27 type OrchestrationV2RuntimeRequest,
@@ -5450,6 +5451,135 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
5451 }),
5452 );
5453
5454 + it.effect(
5455 + "reads a pull request when its watch starts, so checks that pass before a sweep wake",
5456 + () =>
5457 + Effect.gen(function* () {
5458 + const orchestrator = yield* Orchestrator.OrchestratorV2;
5459 + const projections = yield* ProjectionStore.ProjectionStoreV2;
5460 + const threadId = ThreadId.make("runtime-pull-request-watch-start-read");
5461 + const projectId = ProjectId.make("pr-watch-start-read-project");
5462 + yield* seedProject({
5463 + projectId,
5464 + title: "Watch start read",
5465 + workspaceRoot: "/workspace/watch-start-read",
5466 + defaultModelSelection: null,
5467 + createdAt: "2026-10-01T00:00:00.000Z",
5468 + });
5469 + yield* orchestrator.dispatch({
5470 + type: "thread.create",
5471 + createdBy: "user",
5472 + creationSource: "web",
5473 + commandId: CommandId.make("pr-watch-start-read-create"),
5474 + threadId,
5475 + projectId,
5476 + title: "Watch start read",
5477 + modelSelection,
5478 + runtimeMode: "full-access",
5479 + interactionMode: "default",
5480 + branch: null,
5481 + worktreePath: null,
5482 + });
5483 +
5484 + let checks: PullRequestDetail["checks"] = [
5485 + { name: "lint", status: "pending", description: null, url: null, required: true },
5486 + ];
5487 + const detailReads = yield* Queue.unbounded<void>();
5488 + const watchListings = yield* Queue.unbounded<void>();
5489 + const domainEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
5490 + const host = Layer.mergeAll(
5491 + NodeServices.layer,
5492 + NodeCrypto.layer,
5493 + Layer.mock(PullRequestService.PullRequestService)({
5494 + // Earlier tests can leave watches in other projects; only this one's reads count.
5495 + detail: (input) =>
5496 + (input.projectId === projectId
5497 + ? Queue.offer(detailReads, undefined)
5498 + : Effect.void
5499 + ).pipe(
5500 + Effect.as({
5501 + ...watchedPullRequestDetail({
5502 + projectId,
5503 + number: 16,
5504 + at: "2026-10-02T12:00:00.000Z",
5505 + }),
5506 + headSha: "bbbbbbbbbb",
5507 + checks,
5508 + }),
5509 + ),
5510 + activity: () =>
5511 + Effect.succeed({
5512 + comments: [],
5513 + commentCount: 0,
5514 + commentsTruncated: false,
5515 + reviewThreads: [],
5516 + commits: [],
5517 + }),
5518 + invalidate: () => Effect.void,
5519 + changes: () => Stream.never,
5520 + }),
5521 + // Events come from a queue that exists before the reactor subscribes, so none is missed.
5522 + Layer.mock(Orchestrator.OrchestratorV2)({
5523 + dispatch: orchestrator.dispatch,
5524 + streamDomainEvents: Stream.fromQueue(domainEvents),
5525 + }),
5526 + Layer.mock(ProjectionStore.ProjectionStoreV2)({
5527 + getThreadsWithPullRequests: (id) =>
5528 + projections
5529 + .getThreadsWithPullRequests(id)
5530 + .pipe(Effect.tap(() => Queue.offer(watchListings, undefined))),
5531 + }),
5532 + );
5533 + const wakes = Effect.map(
5534 + orchestrator.getThreadRecords(threadId, ["messages"]),
5535 + ({ messages }) =>
5536 + messages.filter(
5537 + (message) => message.notification !== undefined && message.notification !== null,
5538 + ),
5539 + );
5540 + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
5541 + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
5542 + yield* reactor.start();
5543 + // The first sweep listed watches before this one existed, so it cannot be the read below.
5544 + yield* Queue.take(watchListings);
5545 +
5546 + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 16 };
5547 + const started = yield* orchestrator.dispatch({
5548 + type: "thread.pull-request.watch",
5549 + commandId: CommandId.make("pr-watch-start-read-start"),
5550 + threadId,
5551 + ...key,
5552 + watching: true,
5553 + link: { url: "https://github.com/pingdotgg/t3code/pull/16", source: "agent" },
5554 + });
5555 + yield* Queue.offerAll(
5556 + domainEvents,
5557 + started.storedEvents.map((stored) => stored.event),
5558 + );
5559 + // The clock never moves, so no sweep can be the read that sees the checks running.
5560 + yield* Queue.take(detailReads);
5561 +
5562 + checks = [
5563 + { name: "lint", status: "success", description: null, url: null, required: true },
5564 + ];
5565 + // A sweep waits for the watch-start read in flight.
5566 + yield* reactor.sweep;
5567 + yield* worker.drain();
5568 + const woken = yield* wakes;
5569 + assert.lengthOf(woken, 1);
5570 + assert.equal(woken[0]!.notification?.summary, "#16: checks passed");
5571 +
5572 + yield* orchestrator.dispatch({
5573 + type: "thread.pull-request.watch",
5574 + commandId: CommandId.make("pr-watch-start-read-end"),
5575 + threadId,
5576 + ...key,
5577 + watching: false,
5578 + });
5579 + yield* worker.drain();
5580 + }),
5581 + );
5582 +
5583 it.effect("wakes for checks that complete after the no-checks notice on the same head", () =>
5584 Effect.gen(function* () {
5585 const orchestrator = yield* Orchestrator.OrchestratorV2;