Skip to content

Commit 32dbced

Browse files
committed
fix(server): drop stale queued pull-request watch wakes
1 parent 0d7d7a9 commit 32dbced

2 files changed

Lines changed: 125 additions & 4 deletions

File tree

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

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,37 @@ function nextQueuedRun(
599599
return queuedRunsInDeliveryOrder(projection)[0];
600600
}
601601

602+
function isStalePullRequestWatchWake(
603+
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "messages">,
604+
run: OrchestrationV2Run,
605+
): boolean {
606+
const source = projection.messages.find((message) => message.id === run.userMessageId)
607+
?.notification?.source;
608+
if (source?.kind !== "monitor" || source.pullRequest === undefined) {
609+
return false;
610+
}
611+
const stamp = source.pullRequest;
612+
const link = threadPullRequestsOf(projection.thread).find(
613+
(candidate) =>
614+
candidate.source !== "stack-dismissed" && threadPullRequestKeysEqual(candidate, stamp),
615+
);
616+
if (link === undefined) {
617+
return true;
618+
}
619+
if (stamp.watchEnded === true) {
620+
// The closed notice is the watch's final delivery, so it still goes out once
621+
// the watch is gone; only a newer watch generation supersedes it.
622+
return link.watch !== undefined && link.watch.startedAt !== stamp.watchStartedAt;
623+
}
624+
return (
625+
link.watch === undefined ||
626+
link.watch.startedAt !== stamp.watchStartedAt ||
627+
stamp.headSha !== link.watch.headSha ||
628+
link.snapshot?.state === "merged" ||
629+
link.snapshot?.state === "closed"
630+
);
631+
}
632+
602633
function latestStableRun(
603634
projection: Pick<OrchestrationV2ThreadProjection, "runs">,
604635
): OrchestrationV2Run | null {
@@ -1267,7 +1298,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12671298
) =>
12681299
Effect.gen(function* () {
12691300
if (!(yield* projectionStore.canStartQueuedRun(threadId))) return;
1270-
const projection = yield* readCommandProjection(threadId);
1301+
let projection = yield* readCommandProjection(threadId);
12711302
if (
12721303
projection.thread.archivedAt !== null ||
12731304
projection.thread.deletedAt !== null ||
@@ -1289,7 +1320,70 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12891320
if (usageLimitBlockedRun(projection.runs, projection.turnItems, sessionError) !== null) {
12901321
return;
12911322
}
1292-
const queuedRun = nextQueuedRun(projection);
1323+
let queuedRun = nextQueuedRun(projection);
1324+
while (queuedRun !== undefined && isStalePullRequestWatchWake(projection, queuedRun)) {
1325+
const cancelledAt = yield* DateTime.now;
1326+
const staleRootNode =
1327+
queuedRun.rootNodeId === null
1328+
? undefined
1329+
: projection.nodes.find((candidate) => candidate.id === queuedRun.rootNodeId);
1330+
const staleAttempt =
1331+
queuedRun.activeAttemptId === null
1332+
? undefined
1333+
: projection.attempts.find((candidate) => candidate.id === queuedRun.activeAttemptId);
1334+
yield* writeSystemEvents([
1335+
{
1336+
type: "run.updated" as const,
1337+
threadId,
1338+
runId: queuedRun.id,
1339+
...(queuedRun.rootNodeId === null ? {} : { nodeId: queuedRun.rootNodeId }),
1340+
providerInstanceId: queuedRun.providerInstanceId,
1341+
occurredAt: cancelledAt,
1342+
payload: {
1343+
...queuedRun,
1344+
status: "cancelled" as const,
1345+
queuePosition: null,
1346+
completedAt: cancelledAt,
1347+
},
1348+
},
1349+
...(staleAttempt === undefined || staleRootNode === undefined
1350+
? []
1351+
: [
1352+
{
1353+
type: "run-attempt.updated" as const,
1354+
threadId,
1355+
runId: queuedRun.id,
1356+
nodeId: staleRootNode.id,
1357+
providerInstanceId: queuedRun.providerInstanceId,
1358+
occurredAt: cancelledAt,
1359+
payload: {
1360+
...staleAttempt,
1361+
status: "cancelled" as const,
1362+
completedAt: cancelledAt,
1363+
},
1364+
},
1365+
]),
1366+
...(staleRootNode === undefined
1367+
? []
1368+
: [
1369+
{
1370+
type: "node.updated" as const,
1371+
threadId,
1372+
runId: queuedRun.id,
1373+
nodeId: staleRootNode.id,
1374+
providerInstanceId: queuedRun.providerInstanceId,
1375+
occurredAt: cancelledAt,
1376+
payload: {
1377+
...staleRootNode,
1378+
status: "cancelled" as const,
1379+
completedAt: cancelledAt,
1380+
},
1381+
},
1382+
]),
1383+
]);
1384+
projection = yield* readCommandProjection(threadId);
1385+
queuedRun = nextQueuedRun(projection);
1386+
}
12931387
if (queuedRun === undefined) {
12941388
return;
12951389
}
@@ -2369,7 +2463,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23692463
threadId: command.threadId,
23702464
messageId: command.wake.messageId,
23712465
text: command.wake.text,
2372-
notification: command.wake.notification,
2466+
notification: {
2467+
...command.wake.notification,
2468+
source:
2469+
command.wake.notification.source.kind === "monitor"
2470+
? {
2471+
kind: "monitor",
2472+
pullRequest: {
2473+
host: command.host,
2474+
repository: command.repository,
2475+
number: command.number,
2476+
watchStartedAt: command.startedAt,
2477+
headSha: command.watch?.headSha ?? null,
2478+
...(command.watch === null ? { watchEnded: true } : {}),
2479+
},
2480+
}
2481+
: command.wake.notification.source,
2482+
},
23732483
attachments: [],
23742484
dispatchMode: { type: "queue_after_active" },
23752485
createdBy: "agent",

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,18 @@ export const OrchestrationV2NotificationSource = kindUnionWithFallback(
10651065
Schema.Struct({ kind: Schema.Literal("background_command").transform("command") }),
10661066
SubagentNotificationSource,
10671067
CommandNotificationSource,
1068-
Schema.Struct({ kind: Schema.Literal("monitor") }),
1068+
Schema.Struct({
1069+
kind: Schema.Literal("monitor"),
1070+
// Optional so monitor sources persisted before the stamp existed still decode.
1071+
pullRequest: Schema.optional(
1072+
Schema.Struct({
1073+
...ThreadPullRequestKey.fields,
1074+
watchStartedAt: IsoDateTime,
1075+
headSha: Schema.NullOr(TrimmedNonEmptyString),
1076+
watchEnded: Schema.optional(Schema.Boolean),
1077+
}),
1078+
),
1079+
}),
10691080
Schema.Struct({ kind: Schema.Literal("background_task") }),
10701081
Schema.Struct({ kind: Schema.Literal("system") }),
10711082
],

0 commit comments

Comments
 (0)