Skip to content
Merged
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
39 changes: 15 additions & 24 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ import {
type ProjectionTimelinePageOptions,
type ProjectionRecordFilter,
type ProjectionRecords,
type ProjectionStoreV2Error,
type ProjectionCheckpointContext,
type OlderSettledThreadsListing,
type ActiveShellIndexEntry,
Expand Down Expand Up @@ -3802,18 +3803,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
cause: "The pull request watch ended or its thread settled while it was read.",
});
}
const projectionError = (cause: ProjectionStoreV2Error) =>
new OrchestratorProjectionError({ threadId: command.threadId, cause });
// A wake that reuses a queued wake's message folds into it rather than queuing another run.
const replacedRun =
const replacedMessage =
wake === undefined
? undefined
: (yield* projectionStore
.getThreadRecords(command.threadId, ["runs"])
.pipe(
Effect.mapError(
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
),
)).runs.find((run) => run.userMessageId === wake.messageId);
if (replacedRun !== undefined && replacedRun.status !== "queued") {
.getMessagesById(command.threadId, [wake.messageId])
.pipe(Effect.mapError(projectionError))).get(wake.messageId);
const replacedRun =
replacedMessage?.runId == null
? undefined
: yield* projectionStore
.getRun(command.threadId, replacedMessage.runId)
.pipe(Effect.mapError(projectionError));
if (replacedMessage !== undefined && replacedRun?.status !== "queued") {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
Expand Down Expand Up @@ -3847,21 +3852,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
: wake.notification.source,
};
if (replacedRun !== undefined) {
const message = yield* projectionStore
.getRunMessage(command.threadId, replacedRun.id)
.pipe(
Effect.mapError(
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
),
);
if (message === undefined) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause: `Queued run ${replacedRun.id} has no user message.`,
});
}
if (replacedMessage !== undefined && replacedRun !== undefined) {
const now = yield* DateTime.now;
yield* emit(
events,
Expand All @@ -3874,7 +3865,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
providerInstanceId: replacedRun.providerInstanceId,
occurredAt: now,
payload: {
...message,
...replacedMessage,
text: wakeMessage.text,
notification,
updatedAt: now,
Expand Down
6 changes: 4 additions & 2 deletions apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,10 @@ export const make = Effect.gen(function* () {
const key = lifeKey(target);
const queued = queuedWakes.get(key);
if (queued === undefined) return undefined;
const { runs } = yield* projections.getThreadRecords(target.thread.id, ["runs"]);
if (runs.some((run) => run.userMessageId === queued.messageId && run.status === "queued")) {
const { runs } = yield* projections.getThreadRecords(target.thread.id, ["runs"], {
runStatuses: ["queued"],
});
if (runs.some((run) => run.userMessageId === queued.messageId)) {
return queued;
}
queuedWakes.delete(key);
Expand Down