Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions packages/control-plane/src/session/message-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,15 @@ describe("SessionMessageQueue", () => {
});

expect(h.repository.cancelPendingMessage).toHaveBeenCalledWith("msg-1");
expect(h.broadcast).toHaveBeenCalledWith({ type: "prompt_queue_updated", promptQueue: [] });
expect(h.wsManager.send).toHaveBeenCalledWith(ws, {
type: "prompt_cancelled",
clientRequestId: "request-1",
messageId: "msg-1",
});
expect(h.broadcast).toHaveBeenCalledWith({ type: "prompt_queue_updated", promptQueue: [] });
expect(h.broadcast.mock.invocationCallOrder[0]).toBeLessThan(
h.wsManager.send.mock.invocationCallOrder[0]
);
expect(h.sessionStatus.reconcileAfterQueueRemoval).toHaveBeenCalledOnce();
});

Expand All @@ -451,7 +454,10 @@ describe("SessionMessageQueue", () => {
message: "This prompt is no longer pending and cannot be removed",
clientRequestId: "request-1",
});
expect(h.broadcast).not.toHaveBeenCalled();
expect(h.broadcast).toHaveBeenCalledWith({ type: "prompt_queue_updated", promptQueue: [] });
expect(h.broadcast.mock.invocationCallOrder[0]).toBeLessThan(
h.wsManager.send.mock.invocationCallOrder[0]
);
});

it("reconciles session status after removing a prompt", async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/control-plane/src/session/message-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ export class SessionMessageQueue {
ws: WebSocket,
data: { messageId: string; clientRequestId: string }
): Promise<void> {
if (!this.messageRepository.cancelPendingMessage(data.messageId)) {
const cancelled = this.messageRepository.cancelPendingMessage(data.messageId);
this.broadcastPromptQueue();
if (!cancelled) {
this.wsManager.send(ws, {
type: "error",
code: "PROMPT_NOT_CANCELLABLE",
Expand All @@ -343,7 +345,6 @@ export class SessionMessageQueue {
clientRequestId: data.clientRequestId,
messageId: data.messageId,
});
this.broadcastPromptQueue();
this.log.info("prompt.cancelled", {
event: "prompt.cancelled",
message_id: data.messageId,
Expand Down
23 changes: 21 additions & 2 deletions packages/control-plane/src/session/message-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,29 @@ describe("MessageRepository", () => {

it("projects unfinished messages into the prompt queue", () => {
vi.spyOn(repository, "listUnfinishedMessages").mockReturnValue([
{ id: "msg-1", content: "Continue", status: "pending" } as never,
{
id: "msg-1",
content: "Continue",
status: "pending",
source: "web",
callback_context: null,
} as never,
{
id: "msg-2",
content: "Reply in Linear",
status: "pending",
source: "linear",
callback_context: null,
} as never,
]);
expect(repository.listPromptQueue()).toEqual([
{ messageId: "msg-1", content: "Continue", status: "pending" },
{ messageId: "msg-1", content: "Continue", status: "pending", cancellable: true },
{
messageId: "msg-2",
content: "Reply in Linear",
status: "pending",
cancellable: false,
},
]);
});

Expand Down
4 changes: 4 additions & 0 deletions packages/control-plane/src/session/message-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export class MessageRepository {
messageId: message.id,
content: message.content,
status: message.status as "pending" | "processing",
cancellable:
Comment thread
open-inspect[bot] marked this conversation as resolved.
Outdated
message.status === "pending" &&
message.source === "web" &&
message.callback_context === null,
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ describe("Client WebSocket (via SELF.fetch)", () => {
promptQueue: Array<Record<string, unknown>>;
};
expect(subscribed.promptQueue).toEqual([
expect.objectContaining({ content: "Only once", status: "pending" }),
expect.objectContaining({ content: "Only once", status: "pending", cancellable: true }),
]);
expect(subscribed.promptQueue[0]).not.toHaveProperty("model");
expect(subscribed.promptQueue[0]).not.toHaveProperty("reasoningEffort");
Expand Down Expand Up @@ -931,7 +931,7 @@ describe("Client WebSocket (via SELF.fetch)", () => {
promptQueue: Array<{ messageId: string }>;
};
expect(subscribed.promptQueue).toContainEqual(
expect.objectContaining({ messageId: "message-linear" })
expect.objectContaining({ messageId: "message-linear", cancellable: false })
Comment thread
open-inspect[bot] marked this conversation as resolved.
);
const clientRequestId = crypto.randomUUID();
const rejected = collectMessages(ws, {
Expand Down
14 changes: 14 additions & 0 deletions packages/shared/src/types/server-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ describe("session view contracts", () => {
messageId: "message-running",
content: "Run this",
status: "processing",
cancellable: false,
},
{
messageId: "message-pending",
content: "Then this",
status: "pending",
cancellable: true,
},
];

Expand All @@ -188,6 +190,18 @@ describe("session view contracts", () => {
).toEqual(promptQueue);
});

it("treats queue items from older servers as non-cancellable", () => {
const message = serverMessageSchema.parse({
type: "prompt_queue_updated",
promptQueue: [{ messageId: "message-1", content: "Wait", status: "pending" }],
});

expect(message).toMatchObject({
type: "prompt_queue_updated",
promptQueue: [{ cancellable: false }],
});
});

it("echoes prompt request correlation", () => {
expect(
serverMessageSchema.parse({
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/types/server-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const promptQueueItemSchema = z.object({
messageId: z.string(),
content: z.string(),
status: z.enum(["pending", "processing"]),
cancellable: z.boolean().default(false),
});
export type PromptQueueItem = z.infer<typeof promptQueueItemSchema>;

Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/app/(app)/(sidebar)/session/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default function SessionPage() {
if (!capabilities.lifecycle) return;
if (cancellingPromptIdsRef.current.has(messageId)) return;
const queuedPrompt = promptQueue.find((item) => item.messageId === messageId);
if (!queuedPrompt || queuedPrompt.status !== "pending") return;
if (!queuedPrompt?.cancellable) return;

cancellingPromptIdsRef.current.add(messageId);
setCancellingPromptIds(new Set(cancellingPromptIdsRef.current));
Expand Down
42 changes: 32 additions & 10 deletions packages/web/src/components/queued-prompt-stack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe("QueuedPromptStack", () => {
it("shows queued prompts without removal controls in read-only mode", () => {
render(
<QueuedPromptStack
promptQueue={[{ messageId: "queued-1", content: "Review this", status: "pending" }]}
promptQueue={[
{ messageId: "queued-1", content: "Review this", status: "pending", cancellable: true },
]}
cancellingPromptIds={new Set()}
onRemove={vi.fn()}
capabilities={{ ...FULL_CAPABILITIES, lifecycle: false }}
Expand All @@ -39,9 +41,14 @@ describe("QueuedPromptStack", () => {
onRemove={vi.fn()}
capabilities={FULL_CAPABILITIES}
promptQueue={[
{ messageId: "running", content: "Already running", status: "processing" },
{ messageId: "next", content: "Run next", status: "pending" },
{ messageId: "later", content: "Run after that", status: "pending" },
{
messageId: "running",
content: "Already running",
status: "processing",
cancellable: false,
},
{ messageId: "next", content: "Run next", status: "pending", cancellable: true },
{ messageId: "later", content: "Run after that", status: "pending", cancellable: true },
]}
/>
);
Expand All @@ -60,7 +67,14 @@ describe("QueuedPromptStack", () => {
cancellingPromptIds={new Set()}
onRemove={vi.fn()}
capabilities={FULL_CAPABILITIES}
promptQueue={[{ messageId: "running", content: "Already running", status: "processing" }]}
promptQueue={[
{
messageId: "running",
content: "Already running",
status: "processing",
cancellable: false,
},
]}
/>
);

Expand All @@ -71,7 +85,9 @@ describe("QueuedPromptStack", () => {
const onRemove = vi.fn();
render(
<QueuedPromptStack
promptQueue={[{ messageId: "next", content: "Run next", status: "pending" }]}
promptQueue={[
{ messageId: "next", content: "Run next", status: "pending", cancellable: true },
]}
cancellingPromptIds={new Set(["next"])}
onRemove={onRemove}
capabilities={FULL_CAPABILITIES}
Expand All @@ -88,7 +104,9 @@ describe("QueuedPromptStack", () => {
const onRemove = vi.fn();
render(
<QueuedPromptStack
promptQueue={[{ messageId: "next", content: "Run next", status: "pending" }]}
promptQueue={[
{ messageId: "next", content: "Run next", status: "pending", cancellable: true },
]}
cancellingPromptIds={new Set()}
onRemove={onRemove}
capabilities={FULL_CAPABILITIES}
Expand All @@ -99,7 +117,7 @@ describe("QueuedPromptStack", () => {
expect(onRemove).toHaveBeenCalledWith("next");
});

it("offers removal for pending prompts whose eligibility is server-owned", () => {
it("does not offer removal for a server-owned prompt", () => {
const onRemove = vi.fn();
render(
<QueuedPromptStack
Expand All @@ -108,6 +126,7 @@ describe("QueuedPromptStack", () => {
messageId: "linear-prompt",
content: "Reply in Linear",
status: "pending",
cancellable: false,
},
]}
cancellingPromptIds={new Set()}
Expand All @@ -116,7 +135,10 @@ describe("QueuedPromptStack", () => {
/>
);

fireEvent.click(screen.getByRole("button", { name: "Remove queued prompt: Reply in Linear" }));
expect(onRemove).toHaveBeenCalledWith("linear-prompt");
expect(screen.getByText("Reply in Linear")).toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "Remove queued prompt: Reply in Linear" })
).not.toBeInTheDocument();
expect(onRemove).not.toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion packages/web/src/components/queued-prompt-stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function QueuedPromptStack({
<p className="min-w-0 flex-1 whitespace-pre-wrap break-words text-sm text-secondary-foreground">
{prompt.content}
</p>
{capabilities.lifecycle && (
{capabilities.lifecycle && prompt.cancellable && (
<button
type="button"
onClick={() => onRemove(prompt.messageId)}
Expand Down
9 changes: 8 additions & 1 deletion packages/web/src/components/session-timeline-scroll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,14 @@ describe("timeline auto-scrolling", () => {
<SessionTimeline
{...baseTimelineProps}
events={events}
promptQueue={[{ messageId: "queued", content: "Next prompt", status: "pending" }]}
promptQueue={[
{
messageId: "queued",
content: "Next prompt",
status: "pending",
cancellable: true,
},
]}
/>
);

Expand Down
8 changes: 5 additions & 3 deletions packages/web/src/components/session-timeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ describe("prompt queue status", () => {
{...baseTimelineProps}
events={events}
promptQueue={[
{ messageId: "running", content: "First", status: "processing" },
{ messageId: "next", content: "Second", status: "pending" },
{ messageId: "later", content: "Third", status: "pending" },
{ messageId: "running", content: "First", status: "processing", cancellable: false },
{ messageId: "next", content: "Second", status: "pending", cancellable: true },
{ messageId: "later", content: "Third", status: "pending", cancellable: true },
]}
/>
);
Expand All @@ -268,11 +268,13 @@ describe("prompt queue status", () => {
messageId: "canonical",
content: "Canonical prompt",
status: "processing",
cancellable: false,
},
{
messageId: "outside-replay",
content: "Queued outside replay",
status: "pending",
cancellable: true,
},
]}
/>
Expand Down
6 changes: 5 additions & 1 deletion packages/web/src/lib/session-socket/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe("sessionSocketReducer", () => {
messageId: "message-1",
content: "Running",
status: "processing" as const,
cancellable: false,
},
];
const state = createSessionSocketState({
Expand Down Expand Up @@ -166,6 +167,7 @@ describe("sessionSocketReducer", () => {
messageId: "message-2",
content: "Next",
status: "pending" as const,
cancellable: true,
},
];
const state = reduce(
Expand All @@ -177,7 +179,9 @@ describe("sessionSocketReducer", () => {

it("waits for the authoritative queue update after cancellation acknowledgement", () => {
const initial = subscribedState({
promptQueue: [{ messageId: "message-2", content: "Next", status: "pending" }],
promptQueue: [
{ messageId: "message-2", content: "Next", status: "pending", cancellable: true },
],
});
const state = reduce(
initial,
Expand Down
Loading