Skip to content

feat(site): open queued edits before the server confirms - #29556

Closed
mafredri wants to merge 4 commits into
mathias/codagt-971-fe-3-edit-queuedfrom
mathias/codagt-971-fe-4-eager-edit
Closed

mafredri wants to merge 4 commits into
mathias/codagt-971-fe-3-edit-queuedfrom
mathias/codagt-971-fe-4-eager-edit

Conversation

@mafredri

Copy link
Copy Markdown
Member

Edit now opens the composer at once and sends the begin marker behind it, so the click feels like editing a history message. Rows show the local edit state through queuedEditOverride until the server's marker catches up. Marker requests never mark the composer pending and run in order, so an end or save cannot overtake its begin. If the begin fails, the composer follows whichever row the server has under edit, or leaves edit mode.

Review notes: the lost-edit tracker distinguishes a row sent before its edit began (never_began, cancel) from one whose edit another client ended (ended, keep the typed text). Earlier marker-based list tests become override-based, since the list now renders from the override.

Part of the CODAGT-971 frontend stack: 4 of 4.

🤖 This PR was created with the help of Coder Agents on behalf of @mafredri, and will be reviewed by a human. 🏂🏻

@linear-code

linear-code Bot commented Sep 18, 2026

Copy link
Copy Markdown

CODAGT-971

@mafredri

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-18T12:11:18.599184Z 4e936f3 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e936f379a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const request = queuedMessageRequestsRef.current.then(() =>
mutate({ queuedMessageId: id, req }),
);
queuedMessageRequestsRef.current = request.catch(() => undefined);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Abort saves when the begin marker fails

If a user submits the optimistic editor before {editing: true} settles and that begin request is rejected, this recovery converts the failed queue tail into a fulfilled promise, so the queued save still executes afterward. This is unsafe when a stale client tries to edit a row after another client has paused the chat: the begin is rejected by coderd/x/chatd/chatstate/transitions.go:905-907, but the subsequent request can still overwrite the row because content is updated independently at lines 908-927. Preserve the begin failure for dependent save/end operations instead of allowing them to proceed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially valid. The mechanics are as described, but the trace is not: Enter leaves edit mode synchronously (clearInputForEdit) before the begin settles, so no resync happens; the save then applies the text the user typed, which is the intended content. Making dependents abort would either poison the chain for the rest of the session or double-toast and re-enter edit mode, so ordering stays as is. The ordering itself is now expressed with a React Query mutation scope (3cf5df7), see below.$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

],
{ queuedEditOverride: { id: 9, editing: true } },
);
expect(screen.getByText("Editing")).toBeInTheDocument();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move visual-state assertions out of Vitest

The new override test asserts only rendered presence and absence with toBeInTheDocument, rather than an interaction's callback, request, or state outcome. These visual branches should be captured by a Storybook state, while Vitest should exercise observable behavior such as invoking Edit or Cancel with the expected row; the frontend contract explicitly prohibits these DOM-presence assertions.

AGENTS.md reference: site/AGENTS.md:L7-L7

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Replaced with one behavioral test (override marks row 9 editing; click Cancel edit; onEndEdit(9)), the hint test is gone, and two stories show the override states (3cf5df7).$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

Comment on lines +794 to +798
patchQueuedMessage(
id,
{ editing: true },
"Failed to edit queued message.",
).catch(() => setFailedQueuedEditBeginID(id));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Associate begin failures with an edit attempt

When requests are slow, a user can select row A, row B, and then row A again before A's first begin settles because onEdit now returns immediately. If the first A request fails, this callback records only A's row ID, so the render logic treats the newer A attempt as failed and cancels its composer even though its own begin is still queued and may succeed, leaving the server editing A while the local UI has exited edit mode. Track an attempt token or request identity so an obsolete failure cannot cancel a later edit of the same row.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid: A, B, A with A's first begin failing would cancel the live A edit. A per-attempt counter now ignores failures of superseded attempts; the render-time row check stays (3cf5df7).$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

Comment on lines +232 to +266
it("lets the local edit state override the row's marker", () => {
renderList(
[
{ ...MockChatQueuedMessage, id: 9 },
{ ...MockChatQueuedMessage, id: 10 },
],
{ queuedEditOverride: { id: 9, editing: true } },
);
expect(screen.getByText("Editing")).toBeInTheDocument();
expect(screen.getByText("Waiting")).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Cancel edit" }),
).toBeInTheDocument();
cleanup();

renderList([{ ...MockEditingChatQueuedMessage, id: 9 }], {
queuedEditOverride: { id: 9, editing: false },
});
expect(screen.queryByText("Editing")).not.toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "Cancel edit" }),
).not.toBeInTheDocument();
});

it("hides the Enter hint on the head while Enter saves an edit", () => {
renderList([{ ...MockChatQueuedMessage, id: 9 }]);
expect(screen.getByText("to send")).toBeInTheDocument();
cleanup();

renderList([{ ...MockChatQueuedMessage, id: 9 }], {
enterSendsHead: false,
});
expect(screen.queryByText("to send")).not.toBeInTheDocument();
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

violations of frontend rules

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed (3cf5df7).$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

// begin it follows. A 404 means the row was sent or removed. Drop it
// locally; a queue_update for it may never arrive. Every failure is
// reported here, so callers only decide whether to continue.
const queuedMessageRequestsRef = useRef<Promise<unknown>>(Promise.resolve());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what the heck

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gone. The intent was ordering: Edit opens the composer before the begin PATCH settles, so a quick Escape or Enter must not reach the server before the begin. React Query already has this: mutations sharing a scope.id run serially and a failed one does not block the next (mutationCache.canRun/runNext). editChatQueuedMessage now sets scope: { id: \chat-queued-messages-${chatId}` }andpatchQueuedMessage` is plain async/await again (3cf5df7).$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

const request = queuedMessageRequestsRef.current.then(() =>
mutate({ queuedMessageId: id, req }),
);
queuedMessageRequestsRef.current = request.catch(() => undefined);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the heck

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thread as above: replaced by the mutation scope (3cf5df7).$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

Comment on lines +698 to +763
// The begin request settles after the click's render, so its failure
// goes through state. The composer then follows the row the server
// has under edit, if any.
const [failedQueuedEditBeginID, setFailedQueuedEditBeginID] = useState<
number | null
>(null);
const queuedEditBeginFailed =
failedQueuedEditBeginID !== null &&
failedQueuedEditBeginID === queuedEditTargetID;
if (failedQueuedEditBeginID !== null) {
setFailedQueuedEditBeginID(null);
}
const serverEditRow = useChatSelector(store, (s) =>
s.queuedMessages.find((row) => row.editing_since),
);
if (queuedEdit.lost === "ended") {
editing.leaveEdit();
} else if (queuedEditBeginFailed && serverEditRow) {
const { text, fileBlocks } = getEditableContentPayload(
serverEditRow.content,
);
editing.handleBeginEdit(
{ kind: "queued", id: serverEditRow.id },
text,
fileBlocks,
);
} else if (queuedEdit.lost === "never_began" || queuedEditBeginFailed) {
editing.handleCancelEdit();
}

const endQueuedEditBeforeLeaving = async (): Promise<boolean> => {
if (queuedEditTargetID === null) {
return true;
}
try {
await handleEndQueuedMessageEdit(queuedEditTargetID);
return true;
} catch {
return false;
// Rows show the local edit state before the server confirms it. The
// composer target covers a begin; an end is tracked until the row's
// marker clears or the request fails.
const [endingQueuedEditID, setEndingQueuedEditID] = useState<number | null>(
null,
);
const endingQueuedEditRow = useChatSelector(store, (s) =>
s.queuedMessages.find((row) => row.id === endingQueuedEditID),
);
if (endingQueuedEditID !== null && !endingQueuedEditRow?.editing_since) {
setEndingQueuedEditID(null);
}
let queuedEditOverride: QueuedEditOverride | undefined;
if (queuedEditTargetID !== null) {
queuedEditOverride = { id: queuedEditTargetID, editing: true };
} else if (endingQueuedEditID !== null) {
queuedEditOverride = { id: endingQueuedEditID, editing: false };
}

const endQueuedMessageEdit = (id: number) => {
setEndingQueuedEditID(id);
return patchQueuedMessage(
id,
{ editing: false },
"Failed to stop editing the queued message.",
).catch((error: unknown) => {
setEndingQueuedEditID((current) => (current === id ? null : current));
throw error;
});
};

// Failures are reported by patchQueuedMessage; the row keeps offering
// Cancel edit while its marker remains.
const handleEndQueuedMessageEdit = (id: number) => {
if (id === queuedEditTargetID) {
editing.handleCancelEdit();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this component has far too much junk in it. far too much logic is living in it. we really need to organise this code better

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The queued-edit orchestration (begin/end/save, lost-edit tracking, restore, override) is a self-contained unit that belongs in its own hook next to useConversationEditingState, with the page only wiring props. I'd rather do that as one move after this stack lands than mid-review; say if you want it in this PR instead.$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and earlier than promised: the queued-edit orchestration now lives in hooks/useQueuedMessageEdit.ts with its own tests (12), and the page only wires requests and props. This PR is folded into #29555 so the eager design is reviewed once rather than as a rewrite of an intermediate one; closing this PR.$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch from 60bb342 to 4c2cc30 Compare September 18, 2026 13:04
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-4-eager-edit branch from 4e936f3 to af52e29 Compare September 18, 2026 13:04
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch from 4c2cc30 to 1b8fceb Compare September 18, 2026 13:12
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-4-eager-edit branch from af52e29 to 95d193a Compare September 18, 2026 13:12
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch from 1b8fceb to d4de206 Compare September 18, 2026 13:27
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-4-eager-edit branch from 95d193a to 3cf5df7 Compare September 18, 2026 13:27
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch from d4de206 to 17cd798 Compare September 18, 2026 14:05
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-4-eager-edit branch from 3cf5df7 to 4a9c0e5 Compare September 18, 2026 14:05
Edit state carries an EditingTarget with a kind instead of a bare
history message ID, so a second target kind can be added without
touching every consumer again. The edit model override and the
editable payload extraction move into reusable helpers, and the
queued row actions share one button and one action runner.

No behavior changes.
The backend now pauses a chat when its queue head is under edit and
marks the row with editing_since. The frontend treats paused as a
finished turn (chime, buffered part discard, archive hint), queues
instead of sending while paused, and never promotes a head that is
under edit. Queued rows show Editing and Waiting badges from the
marker; the row actions to begin or end an edit come next.

Queued rows are compared structurally because a marker or content
change no longer changes the row's identity.
A queued row gets Edit and Cancel edit actions. Edit marks the row on
the server and loads it into the composer; Save patches the content
and clears the marker; Cancel clears the marker, which while paused
sends the row unchanged. Each step waits for the server before the
composer changes mode, so a failed request leaves the user where they
were.

The page owns the server calls. A 404 means the row was already sent
or removed, so it is dropped locally. If another client ends the edit,
the composer leaves edit mode and keeps the typed text as the draft.
A chat that loads with a row under edit reopens that edit.
Edit opens the composer at once and sends the begin marker behind it,
so the click feels the same as editing a history message. The rows
show the local edit state through queuedEditOverride until the
server's marker catches up. Marker requests run on their own mutation
and in order, so an end or save cannot overtake the begin it follows.

If the begin fails, the composer follows whichever row the server has
under edit, or leaves edit mode. The lost-edit tracker now tells a row
that was sent before its edit began apart from one whose edit was
ended by another client, because only the latter should keep the
typed text as the draft.
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch from 17cd798 to b608b0b Compare September 18, 2026 15:48
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-4-eager-edit branch from 4a9c0e5 to eb7138b Compare September 18, 2026 15:48
@mafredri
mafredri force-pushed the mathias/codagt-971-fe-3-edit-queued branch 2 times, most recently from f938d82 to c14a860 Compare September 18, 2026 18:36
@mafredri

Copy link
Copy Markdown
Member Author

Folded into #29555 (c14a860): the eager design ships there directly, with the orchestration extracted into useQueuedMessageEdit. Closing.$\n\n> 🤖 Reply by Coder Agents on behalf of @mafredri.

@mafredri mafredri closed this Sep 18, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 18, 2026
@mafredri
mafredri removed this pull request from stack #29557 October 30, 2026 11:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants