Skip to content

Commit aeb7045

Browse files
t3dotggclaude
andcommitted
fix(server): refresh only after a run that ran a merge or close command
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 447d6c2 commit aeb7045

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PullRequestOperationError,
88
RunId,
99
ThreadId,
10+
TurnItemId,
1011
type OrchestrationV2Command as OrchestrationCommand,
1112
type OrchestrationV2DomainEvent,
1213
type OrchestrationV2Run,
@@ -316,6 +317,36 @@ function runUpdated(
316317
};
317318
}
318319

320+
function commandRan(threadId: ThreadId, input: string): OrchestrationV2DomainEvent {
321+
const runId = RunId.make(`run:${threadId}:1`);
322+
const at = DateTime.makeUnsafe(NOW);
323+
return {
324+
type: "turn-item.updated",
325+
id: EventId.make(`event:${threadId}:command`),
326+
threadId,
327+
runId,
328+
occurredAt: at,
329+
payload: {
330+
id: TurnItemId.make(`item:${threadId}:command`),
331+
threadId,
332+
runId,
333+
nodeId: null,
334+
providerThreadId: null,
335+
providerTurnId: null,
336+
nativeItemRef: null,
337+
parentItemId: null,
338+
ordinal: 1,
339+
status: "completed",
340+
title: null,
341+
startedAt: at,
342+
completedAt: at,
343+
updatedAt: at,
344+
type: "command_execution",
345+
input,
346+
},
347+
};
348+
}
349+
319350
/** What the reactor would have persisted, so the next sweep sees its own writes. */
320351
function applySync(
321352
snapshot: TestShellSnapshot,
@@ -877,7 +908,7 @@ describe("PullRequestSyncReactor", () => {
877908
),
878909
);
879910

880-
it.effect("reads the open links of a thread fresh when its run ends", () =>
911+
it.effect("reads open links fresh only when a run that ran a merge command ends", () =>
881912
Effect.scoped(
882913
Effect.gen(function* () {
883914
yield* TestClock.setTime(Date.parse(NOW));
@@ -904,8 +935,14 @@ describe("PullRequestSyncReactor", () => {
904935
yield* Ref.set(state, "merged");
905936
yield* Ref.set(fixture.summaryCalls, []);
906937

907-
yield* Queue.offer(fixture.domainEvents, runUpdated(ThreadId.make("agent"), "completed"));
908-
// The ended run's thread lookup, then the requested sweep.
938+
yield* Queue.offerAll(fixture.domainEvents, [
939+
// A run that only reads its pull request costs no host read when it ends.
940+
commandRan(ThreadId.make("other"), "gh pr view 9"),
941+
runUpdated(ThreadId.make("other"), "completed"),
942+
commandRan(ThreadId.make("agent"), "gh pr merge 7 --squash 2>&1 | tail -3"),
943+
runUpdated(ThreadId.make("agent"), "completed"),
944+
]);
945+
// The agent thread's lookup, then the requested sweep.
909946
yield* Queue.take(fixture.snapshotReads);
910947
yield* Queue.take(fixture.snapshotReads);
911948
yield* reactor.drain;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import * as ProjectionStore from "./ProjectionStore.ts";
3333
import { isTerminalRunStatus } from "./ThreadManagementService.ts";
3434

3535
const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000;
36+
/** Shell commands that can merge or close a pull request without a merge notification. */
37+
const PULL_REQUEST_CLOSE_COMMAND = /\b(?:gh\s+pr|glab\s+mr)\s+(?:merge|close)\b/u;
3638

3739
type SnapshotFields = Omit<ThreadPullRequestSnapshot, "syncedAt">;
3840

@@ -332,6 +334,8 @@ export const make = Effect.gen(function* () {
332334
}).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))),
333335
);
334336

337+
// Threads whose current run ran a merge or close command, until that run ends.
338+
const closeCommandThreads = new Set<ThreadId>();
335339
const refreshOpenLinks = (threadId: ThreadId) =>
336340
projections.getThreadsWithPullRequests(threadId).pipe(
337341
Effect.flatMap((threads) =>
@@ -364,10 +368,20 @@ export const make = Effect.gen(function* () {
364368
{ discard: true },
365369
);
366370
// An agent can merge or close its pull request from a shell (`gh pr merge`), which
367-
// sends no merge notification. Read the thread's open links fresh when its run ends,
368-
// so settlement does not wait for the next sweep and the cached summary.
371+
// sends no merge notification. When a run that ran such a command ends, read the
372+
// thread's open links fresh, so settlement does not wait for the next sweep and the
373+
// cached summary. Other runs add no host reads.
374+
case "turn-item.updated":
375+
if (
376+
event.payload.type === "command_execution" &&
377+
PULL_REQUEST_CLOSE_COMMAND.test(event.payload.input)
378+
) {
379+
closeCommandThreads.add(event.threadId);
380+
}
381+
return Effect.void;
369382
case "run.updated":
370-
return isTerminalRunStatus(event.payload.status)
383+
return isTerminalRunStatus(event.payload.status) &&
384+
closeCommandThreads.delete(event.threadId)
371385
? refreshOpenLinks(event.threadId)
372386
: Effect.void;
373387
default:

‎docs/internals/overview.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Thread settlement is server-owned. The
8989
and inactivity settings without a connected client. Merge notifications invalidate cached PR state
9090
and trigger a check. A merge outside T3, such as an agent running `gh pr merge`, sends no
9191
notification, so the [PR sync reactor](../../apps/server/src/orchestration-v2/PullRequestSyncReactor.ts)
92-
also re-reads a thread's open links when its run ends. The guarded `thread.auto-settle` command rejects newer activity, explicit
92+
re-reads a thread's open links when a run that ran a merge or close command ends. The guarded `thread.auto-settle` command rejects newer activity, explicit
9393
settlement overrides, and live or blocked work. It records the activity timestamp for stable
9494
sorting and detaches idle provider sessions. Clients render the persisted result; they do not
9595
derive settlement from their own clocks or PR caches.

0 commit comments

Comments
 (0)