Skip to content

Commit 68101f9

Browse files
fix(server): an agent's merge read waits for any watch read in flight
Reading a thread's watches when its agent's merge or close ends cleared the cached read before waiting for the pass in flight, which could then record its read from before the merge, so the agent's read skipped the host and the watch later woke the agent on its own merge. The cached read is now cleared once that pass is done. Azure's `az repos pr update --id 19 --status completed` named no pull request, because its close pattern runs through `--id`, so it silenced every watch on the thread. Numbers are now read from the start of the command, `--id=19` too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f53e40c commit 68101f9

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,10 @@ export const make = Effect.gen(function* () {
855855
.map((link) => [thread.projectId, threadPullRequestKeyOf(link)] as const),
856856
),
857857
([projectId, pullRequest]) =>
858-
Effect.suspend(() => {
859-
lastReads.delete(`${projectId} ${pullRequest}`);
860-
return readOnChange(pullRequest);
861-
}),
858+
// After any pass in flight, which could record a read from before the merge.
859+
passes
860+
.withPermit(Effect.sync(() => lastReads.delete(`${projectId} ${pullRequest}`)))
861+
.pipe(Effect.andThen(readOnChange(pullRequest))),
862862
{ discard: true },
863863
),
864864
),

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,21 +4090,25 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
40904090
[20, "open"],
40914091
]);
40924092
const detailReads = yield* Queue.unbounded<number>();
4093+
const agentRead = yield* Deferred.make<void>();
40934094
const agentEvents = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
40944095
const host = Layer.mergeAll(
40954096
NodeServices.layer,
40964097
NodeCrypto.layer,
40974098
Layer.mock(PullRequestService.PullRequestService)({
40984099
detail: (input) =>
4099-
Queue.offer(detailReads, input.number).pipe(
4100-
Effect.as({
4100+
Effect.sync(() => states.get(input.number)!).pipe(
4101+
Effect.tap(() => Queue.offer(detailReads, input.number)),
4102+
// Reads of an open pull request are slow, so the first sweep outlasts the merge.
4103+
Effect.tap((state) => (state === "open" ? Effect.sleep("1 second") : Effect.void)),
4104+
Effect.map((state) => ({
41014105
...watchedPullRequestDetail({
41024106
projectId,
41034107
number: input.number,
41044108
at: "2026-10-02T12:00:00.000Z",
41054109
}),
4106-
state: states.get(input.number)!,
4107-
}),
4110+
state,
4111+
})),
41084112
),
41094113
activity: () =>
41104114
Effect.succeed({
@@ -4123,6 +4127,21 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
41234127
...orchestrator,
41244128
streamDomainEvents: Stream.fromQueue(agentEvents),
41254129
}),
4130+
// Only the read at a command's end asks for one thread's watches.
4131+
Layer.effect(
4132+
ProjectionStore.ProjectionStoreV2,
4133+
Effect.map(ProjectionStore.ProjectionStoreV2, (store) => ({
4134+
...store,
4135+
getThreadsWithPullRequests: (thread?: ThreadId) =>
4136+
store
4137+
.getThreadsWithPullRequests(thread)
4138+
.pipe(
4139+
Effect.tap(() =>
4140+
thread === undefined ? Effect.void : Deferred.succeed(agentRead, undefined),
4141+
),
4142+
),
4143+
})),
4144+
),
41264145
);
41274146
const runId = RunId.make(`run:${threadId}:1`);
41284147
const command = (status: "running" | typeof ended) =>
@@ -4154,16 +4173,18 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
41544173
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
41554174
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
41564175
yield* reactor.start();
4157-
// The first sweep finds both open.
4176+
// The first sweep has read both as open, and has not recorded that yet.
41584177
yield* Queue.take(detailReads);
41594178
yield* Queue.take(detailReads);
41604179

41614180
// While the agent merges #19, someone else closes #20.
41624181
yield* Queue.offer(agentEvents, yield* command("running"));
41634182
states.set(19, "merged");
41644183
states.set(20, "closed");
4165-
// The command's end reads the thread's watches in full, before any sweep.
4184+
// The command's end reads the thread's watches in full, after that sweep.
41664185
yield* Queue.offer(agentEvents, yield* command(ended));
4186+
yield* Deferred.await(agentRead);
4187+
yield* TestClock.adjust("1 second");
41674188
yield* Queue.take(detailReads);
41684189
yield* Queue.take(detailReads);
41694190
// A sweep waits for that read to finish.

‎apps/server/src/sourceControl/sourceControlDefinitions.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ it.each([
3030
["gh pr close https://github.com/o/r/pull/19 && gh pr merge 20", [19, 20]],
3131
["glab mr merge https://gitlab.com/o/r/-/merge_requests/7", [7]],
3232
["sleep 30; cafe pr merge 26271", [26271]],
33+
["az repos pr update --id 19 --status completed", [19]],
34+
["az repos pr update --id=19 --status=abandoned", [19]],
3335
["gh pr merge --squash", "any"],
3436
["gh stack merge", "any"],
3537
["gh pr merge 19 && gh pr merge --auto", "any"],

‎apps/server/src/sourceControl/sourceControlDefinitions.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const isChangeRequestCloseCommand = (command: string) =>
2626
(definition) => definition.changeRequestCloseCommand?.test(command) === true,
2727
);
2828

29-
const NAMED_CHANGE_REQUEST = /(?:^|\s)#?(\d+)(?=\s|$)|\/(?:pulls?|merge_requests)\/(\d+)/gu;
29+
const NAMED_CHANGE_REQUEST = /(?:^|[\s=])#?(\d+)(?=\s|$)|\/(?:pulls?|merge_requests)\/(\d+)/gu;
3030

3131
/**
3232
* The change request numbers a merge or close command names, or "any" when one names none, as a
@@ -37,7 +37,8 @@ export function changeRequestsClosedBy(command: string): ReadonlySet<number> | "
3737
for (const { changeRequestCloseCommand: close } of sourceControlDefinitions.definitions) {
3838
if (close === undefined) continue;
3939
for (const match of command.matchAll(new RegExp(close.source, `${close.flags}g`))) {
40-
const args = command.slice(match.index + match[0].length).split(/[;&|\n]/u, 1)[0] ?? "";
40+
// From the match's start, since Azure's match runs through `--id` to `--status`.
41+
const args = command.slice(match.index).split(/[;&|\n]/u, 1)[0] ?? "";
4142
const numbers = [...args.matchAll(NAMED_CHANGE_REQUEST)].map(([, bare, url]) =>
4243
Number(bare ?? url),
4344
);

0 commit comments

Comments
 (0)