Skip to content

Commit e811133

Browse files
github-actions[bot]juliusmarminge
authored andcommitted
fix(web): preserve file attachments when editing queued runs
1 parent 233063a commit e811133

6 files changed

Lines changed: 112 additions & 8 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ describe("orchestrator MCP toolkit", () => {
898898
text: "Rewrite the automatic delivery.",
899899
})
900900
.pipe(Effect.flip);
901-
expect(completionEditError._tag).toBe("OrchestratorDispatchError");
901+
expect(completionEditError._tag).toBe("OrchestratorCommandRejectedError");
902902
const completionReorderError = yield* orchestrator
903903
.dispatch({
904904
type: "queued-run.reorder",

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ export class OrchestratorDispatchError extends Schema.TaggedError<OrchestratorDi
9696
}
9797
}
9898

99+
export class OrchestratorCommandRejectedError extends Schema.TaggedError<OrchestratorCommandRejectedError>()(
100+
"OrchestratorCommandRejectedError",
101+
{ commandId: CommandId, commandType: Schema.String, cause: Schema.optional(Schema.Defect()) },
102+
) {
103+
override get message(): string {
104+
return `Orchestration command ${this.commandType} (${this.commandId}) was rejected before commit.`;
105+
}
106+
}
107+
99108
export class OrchestratorProjectionError extends Schema.TaggedError<OrchestratorProjectionError>()(
100109
"OrchestratorProjectionError",
101110
{
@@ -174,6 +183,7 @@ export function canReplayCommandReceipt(
174183

175184
export const OrchestratorV2Error = Schema.Union([
176185
OrchestratorDispatchError,
186+
OrchestratorCommandRejectedError,
177187
OrchestratorProjectionError,
178188
OrchestratorDomainEventStreamError,
179189
OrchestratorProviderAdapterError,
@@ -7825,7 +7835,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
78257835
Effect.catch((cause) =>
78267836
Effect.gen(function* () {
78277837
const rejectedAt = yield* DateTime.now;
7828-
yield* eventSink
7838+
const receipt = yield* eventSink
78297839
.commitRejectedCommand({
78307840
commandId: command.commandId,
78317841
threadId: commandThreadId(command),
@@ -7843,6 +7853,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
78437853
}),
78447854
),
78457855
);
7856+
if (
7857+
command.type === "queued-run.edit" &&
7858+
receipt.status === "rejected" &&
7859+
cause._tag === "OrchestratorDispatchError"
7860+
) {
7861+
return yield* new OrchestratorCommandRejectedError({
7862+
commandId: cause.commandId,
7863+
commandType: cause.commandType,
7864+
cause: cause.cause,
7865+
});
7866+
}
78467867
return yield* cause;
78477868
}),
78487869
),

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,66 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
15491549
});
15501550
yield* dispatch;
15511551
yield* dispatch;
1552+
const queuedProjection = yield* threads.getThreadProjection(input.threadId);
1553+
const queuedRun = queuedProjection.runs.find(
1554+
(run) => run.userMessageId === MessageId.make("intake-second"),
1555+
);
1556+
assert.isDefined(queuedRun);
1557+
assert.equal(queuedRun.status, "queued");
1558+
const queuedMessage = queuedProjection.messages.find(
1559+
(message) => message.id === queuedRun.userMessageId,
1560+
);
1561+
assert.isDefined(queuedMessage);
1562+
const file: ChatAttachment = {
1563+
type: "file",
1564+
id: ChatAttachmentId.make(createPendingAttachmentId("pdf")),
1565+
name: "queued.pdf",
1566+
mimeType: "application/pdf",
1567+
sizeBytes: 4,
1568+
};
1569+
const filePath = resolveAttachmentPath({
1570+
attachmentsDir: config.attachmentsDir,
1571+
attachment: file,
1572+
});
1573+
assert.isNotNull(filePath);
1574+
yield* fs.writeFile(filePath, new Uint8Array([5, 6, 7, 8]));
1575+
const edit = ThreadMessageIntake.dispatchCommand({
1576+
type: "queued-run.edit",
1577+
commandId: CommandId.make("intake-edit"),
1578+
threadId: input.threadId,
1579+
runId: queuedRun.id,
1580+
text: "Edited with a file",
1581+
attachments: [...queuedMessage.attachments, file],
1582+
});
1583+
yield* edit;
1584+
yield* edit;
1585+
const editedProjection = yield* threads.getThreadProjection(input.threadId);
1586+
const editedMessage = editedProjection.messages.find(
1587+
(message) => message.id === queuedRun.userMessageId,
1588+
);
1589+
assert.isDefined(editedMessage);
1590+
assert.equal(editedMessage.attachments.length, 2);
1591+
assert.deepEqual(editedMessage.attachments[0], queuedMessage.attachments[0]);
1592+
assert.notEqual(editedMessage.attachments[1]?.id, file.id);
1593+
const durableFilePath = resolveAttachmentPath({
1594+
attachmentsDir: config.attachmentsDir,
1595+
attachment: editedMessage.attachments[1]!,
1596+
});
1597+
assert.isNotNull(durableFilePath);
1598+
assert.deepEqual(yield* fs.readFile(durableFilePath), new Uint8Array([5, 6, 7, 8]));
1599+
assert.deepEqual(yield* fs.readFile(filePath), new Uint8Array([5, 6, 7, 8]));
1600+
const beforeRejectedEdit = (yield* claimedFiles).length;
1601+
const rejectedEdit = yield* ThreadMessageIntake.dispatchCommand({
1602+
type: "queued-run.edit",
1603+
commandId: CommandId.make("intake-edit-rejected"),
1604+
threadId: input.threadId,
1605+
runId: queuedRun.id,
1606+
text: "",
1607+
attachments: [file],
1608+
}).pipe(Effect.flip);
1609+
assert.equal(rejectedEdit._tag, "OrchestratorCommandRejectedError");
1610+
assert.equal((yield* claimedFiles).length, beforeRejectedEdit);
1611+
assert.deepEqual(yield* fs.readFile(filePath), new Uint8Array([5, 6, 7, 8]));
15521612
const send = ThreadMessageIntake.sendToThread({
15531613
commandId: CommandId.make("intake-send"),
15541614
projectId,
@@ -1562,7 +1622,7 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
15621622
});
15631623
yield* send;
15641624
yield* send;
1565-
assert.equal((yield* claimedFiles).length, 3);
1625+
assert.equal((yield* claimedFiles).length, 4);
15661626
const final = yield* threads.getThreadProjection(input.threadId);
15671627
assert.equal(final.messages.length, 3);
15681628
for (const message of final.messages) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function dispatchWasNotAccepted(
1616
error: Orchestrator.OrchestratorV2Error | ThreadManagement.ThreadManagementError,
1717
) {
1818
switch (error._tag) {
19+
case "OrchestratorCommandRejectedError":
1920
case "OrchestratorProjectionError":
2021
case "OrchestratorProviderAdapterError":
2122
case "OrchestratorCommandPreviouslyRejectedError":
@@ -74,10 +75,14 @@ export const dispatchCommand = Effect.fn("ThreadMessageIntake.dispatchCommand")(
7475
);
7576
return yield* threads.dispatch({ ...command, answers, attachmentsByQuestionId });
7677
}
77-
if (command.type !== "message.dispatch") return yield* threads.dispatch(command);
78+
if (
79+
command.type !== "message.dispatch" &&
80+
(command.type !== "queued-run.edit" || command.attachments === undefined)
81+
)
82+
return yield* threads.dispatch(command);
7883
const claimed = yield* AttachmentClaims.claimPendingAttachments({
7984
threadId: command.threadId,
80-
attachments: command.attachments,
85+
attachments: command.attachments ?? [],
8186
});
8287
return yield* threads.dispatch({ ...command, attachments: claimed.attachments }).pipe(
8388
Effect.tap((result) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ it.layer(TestLayer)("OrchestrationV2LayerLive lifecycle", (it) => {
23752375
text: " ",
23762376
})
23772377
.pipe(Effect.flip);
2378-
assert.equal(emptyEditError._tag, "OrchestratorDispatchError");
2378+
assert.equal(emptyEditError._tag, "OrchestratorCommandRejectedError");
23792379

23802380
yield* orchestrator.dispatch({
23812381
type: "queued-run.cancel",

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,6 +3890,7 @@ export default function ChatView(props: ChatViewProps) {
38903890
reportFailure: false,
38913891
});
38923892
const queuedEditSaveInFlightRef = useRef(false);
3893+
const [isSavingQueuedEdit, setIsSavingQueuedEdit] = useState(false);
38933894
const queuedEditImageResources = useMemo(
38943895
() =>
38953896
(editingQueuedRun?.existingAttachments ?? [])
@@ -7259,6 +7260,17 @@ export default function ChatView(props: ChatViewProps) {
72597260
const editText = promptForSend.trim();
72607261
const newEditImages = [...composerImages];
72617262
const newEditFiles = [...composerFiles];
7263+
const newEditAttachments = [...newEditImages, ...newEditFiles];
7264+
if (
7265+
editingQueuedRun.existingAttachments.length + newEditAttachments.length >
7266+
PROVIDER_SEND_TURN_MAX_ATTACHMENTS
7267+
) {
7268+
setThreadError(
7269+
editingQueuedRun.threadId,
7270+
`A message can have at most ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} attachments.`,
7271+
);
7272+
return;
7273+
}
72627274
if (
72637275
editText.length === 0 &&
72647276
editingQueuedRun.existingAttachments.length === 0 &&
@@ -7268,6 +7280,7 @@ export default function ChatView(props: ChatViewProps) {
72687280
return;
72697281
}
72707282
queuedEditSaveInFlightRef.current = true;
7283+
setIsSavingQueuedEdit(true);
72717284
try {
72727285
const uploads = await prepareQueuedEditAttachments({
72737286
existingAttachments: editingQueuedRun.existingAttachments,
@@ -7319,6 +7332,7 @@ export default function ChatView(props: ChatViewProps) {
73197332
return;
73207333
}
73217334
setThreadError(editingQueuedRun.threadId, null);
7335+
releaseDraftAttachments(newEditAttachments);
73227336
promptRef.current = "";
73237337
clearComposerDraftContent(queuedEditDraftTargetFor(editingQueuedRun.runId));
73247338
composerRef.current?.resetCursorState();
@@ -7331,6 +7345,7 @@ export default function ChatView(props: ChatViewProps) {
73317345
);
73327346
} finally {
73337347
queuedEditSaveInFlightRef.current = false;
7348+
setIsSavingQueuedEdit(false);
73347349
}
73357350
return;
73367351
}
@@ -9145,7 +9160,10 @@ export default function ChatView(props: ChatViewProps) {
91459160
<ComposerSurface.Shell
91469161
contextStrip={showComposerContextStrip || showComposerModelStrip}
91479162
>
9148-
<ComposerSurface.Host>
9163+
<ComposerSurface.Host
9164+
inert={isSavingQueuedEdit}
9165+
aria-busy={isSavingQueuedEdit}
9166+
>
91499167
<div className="relative z-10">
91509168
<ChatComposer
91519169
composerRef={composerRef}
@@ -9169,7 +9187,7 @@ export default function ChatView(props: ChatViewProps) {
91699187
projectSelectionRequired={isLocalDraftThread && activeProject === null}
91709188
phase={phase}
91719189
isConnecting={isConnecting}
9172-
isSendBusy={isSendBusy}
9190+
isSendBusy={isSendBusy || isSavingQueuedEdit}
91739191
isPreparingWorktree={isPreparingWorktree}
91749192
queuedRunsControl={
91759193
isServerThread && activeThread ? (

0 commit comments

Comments
 (0)