Skip to content

Commit 4f971d9

Browse files
fix(ui): report peer-edited queue reorder conflicts
1 parent 9be931e commit 4f971d9

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎ui/src/pages/chat/chat-send-actions.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
import { listStoredChatOutboxes, storedChatOutboxScopeKey } from "./composer-persistence.ts";
3838
import { formatConnectError } from "./connect-error.ts";
3939
import {
40+
activeQueuedMessageEdit,
4041
isQueuedMessageBeingEdited,
4142
isQueuedMessageRetryBlocked,
4243
isQueuedMessageReorderBlocked,
@@ -168,11 +169,39 @@ export function moveQueuedChatMessage(
168169
}
169170
const sessionKey = item.sessionKey ?? host.sessionKey;
170171
const scope = readChatQueueForScope(host, sessionKey, item.agentId);
172+
// This pane already excludes its own edited row from rendered move indices,
173+
// but cannot see edits owned by peers. Rebuild that exact offered index space
174+
// so a peer-edit barrier is distinguishable from an ordinary no-op.
175+
const localEditId = activeQueuedMessageEdit(host)?.id;
176+
const offeredSegment = chatQueueMovableSegments(
177+
scope,
178+
(row) => isMovableChatQueueItem(row) && row.id !== localEditId,
179+
).find((rows) => rows.some((row) => row.id === id));
180+
const fromIndex = offeredSegment?.findIndex((row) => row.id === id) ?? -1;
181+
const requestedIndex = offeredSegment
182+
? Math.min(Math.max(toIndex, 0), offeredSegment.length - 1)
183+
: -1;
184+
if (fromIndex < 0 || fromIndex === requestedIndex) {
185+
return "noop";
186+
}
187+
const crossedPeerEdit = offeredSegment!.some(
188+
(row, index) =>
189+
index >= Math.min(fromIndex, requestedIndex) &&
190+
index <= Math.max(fromIndex, requestedIndex) &&
191+
row.id !== id &&
192+
isQueuedMessageBeingEdited(host, row.id),
193+
);
194+
if (crossedPeerEdit) {
195+
setChatError(host, QUEUED_MESSAGE_REORDER_CONFLICT_ERROR);
196+
return "rejected";
197+
}
171198
const segment = chatQueueMovableSegments(
172199
scope,
173200
(row) => isMovableChatQueueItem(row) && !isQueuedMessageBeingEdited(host, row.id),
174201
).find((rows) => rows.some((row) => row.id === id));
175-
const moves = reorderChatQueueItems(segment ?? [], id, toIndex);
202+
const targetId = offeredSegment![requestedIndex]?.id;
203+
const segmentTargetIndex = segment?.findIndex((row) => row.id === targetId) ?? -1;
204+
const moves = reorderChatQueueItems(segment ?? [], id, segmentTargetIndex);
176205
if (moves.length === 0) {
177206
return "noop";
178207
}

‎ui/src/pages/chat/queued-message-edit.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
isQueuedMessageRemovalBlocked,
2828
isQueuedMessageReorderBlocked,
2929
isQueuedMessageRetryBlocked,
30+
QUEUED_MESSAGE_REORDER_CONFLICT_ERROR,
3031
updateQueuedMessageEdit,
3132
} from "./queued-message-edit.ts";
3233
import { OFFLINE_QUEUE_STORAGE_ERROR } from "./steer-lifecycle.ts";
@@ -352,6 +353,52 @@ describe("queued message edit round-trip", () => {
352353
}
353354
});
354355

356+
it("reports when a peer reorder crosses the row another pane is editing", () => {
357+
const { host, unsubscribe } = queueHost([{}, {}, {}]);
358+
const peer = makeChatHost({ connected: false, sessionKey: SESSION_KEY });
359+
const stopPeer = subscribeChatOutboxProjection(peer as never);
360+
361+
try {
362+
beginQueuedMessageEdit(host as never, "queued-2");
363+
364+
expect(moveQueuedChatMessage(peer as never, "queued-3", 0)).toBe("rejected");
365+
expect(peer.chatError).toBe(QUEUED_MESSAGE_REORDER_CONFLICT_ERROR);
366+
expect(storedOrder(peer)).toEqual(["message 1", "message 2", "message 3"]);
367+
} finally {
368+
stopPeer();
369+
unsubscribe();
370+
}
371+
});
372+
373+
it("translates peer reorder indices within one side of an edited-row barrier", () => {
374+
const { host, unsubscribe } = queueHost([{}, {}, {}, {}]);
375+
const peer = makeChatHost({ connected: false, sessionKey: SESSION_KEY });
376+
const stopPeer = subscribeChatOutboxProjection(peer as never);
377+
378+
try {
379+
beginQueuedMessageEdit(host as never, "queued-2");
380+
381+
expect(moveQueuedChatMessage(peer as never, "queued-4", 2)).toBe("moved");
382+
expect(storedOrder(peer)).toEqual(["message 1", "message 2", "message 4", "message 3"]);
383+
} finally {
384+
stopPeer();
385+
unsubscribe();
386+
}
387+
});
388+
389+
it("keeps local reorder indices within one side of its own edited-row barrier", () => {
390+
const { host, unsubscribe } = queueHost([{}, {}, {}, {}]);
391+
392+
try {
393+
beginQueuedMessageEdit(host as never, "queued-2");
394+
395+
expect(moveQueuedChatMessage(host as never, "queued-4", 0)).toBe("moved");
396+
expect(storedOrder(host)).toEqual(["message 1", "message 2", "message 4", "message 3"]);
397+
} finally {
398+
unsubscribe();
399+
}
400+
});
401+
355402
it("does not collapse same-payload row and composer sends", async () => {
356403
const ack = createDeferred<{ status: "started" }>();
357404
const sendRequest = vi.fn(() => ack.promise);

0 commit comments

Comments
 (0)