Skip to content

Commit cbcd036

Browse files
committed
fix(ui): reject ambiguous permission restarts
1 parent 02ffbb0 commit cbcd036

6 files changed

Lines changed: 58 additions & 23 deletions

File tree

‎src/gateway/server-methods/chat-restart-recovery.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { isAgentHarnessSessionKey } from "../../sessions/agent-harness-session-k
2828
import { isAcpSessionKey, resolveSessionDispatchKind } from "../../sessions/session-key-utils.js";
2929
import { sessionDeliveryChannel } from "../../utils/delivery-context.shared.js";
3030
import { parseInlineDirectives } from "../../utils/directive-tags.js";
31+
import type { ChatAbortControllerEntry } from "../chat-abort.js";
3132
import { resolveChatRunOwnerAgentId } from "../chat-run-owner.js";
3233
import type { GatewayRecoveryRuntime } from "../server-instance-runtime.types.js";
3334
import { deriveGatewaySessionLifecycleSnapshot } from "../session-lifecycle-state.js";
@@ -94,6 +95,24 @@ export function resolvePermissionRestartChatAdmission(params: {
9495
};
9596
}
9697

98+
export function resolvePermissionRestartAbortEntry(params: {
99+
entries: ReadonlyMap<string, ChatAbortControllerEntry>;
100+
lifecycleGeneration: string;
101+
runId: string;
102+
sessionId?: string;
103+
sessionKey: string;
104+
}): ChatAbortControllerEntry | undefined {
105+
const entry = params.entries.get(params.runId);
106+
return entry &&
107+
entry.kind !== "agent" &&
108+
entry.turnKind !== "btw" &&
109+
entry.lifecycleGeneration === params.lifecycleGeneration &&
110+
entry.sessionKey === params.sessionKey &&
111+
entry.sessionId === params.sessionId
112+
? entry
113+
: undefined;
114+
}
115+
97116
export type RestartSafeChatTerminalState = {
98117
error?: string;
99118
retryable: boolean;

‎src/gateway/server-methods/chat-send-admission.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { resolveChatSendOriginatingRoute } from "./chat-origin-routing.js";
4242
import {
4343
hasRestartRecoveryTerminalRun,
4444
isRetryableUnadoptedChatClaim,
45+
resolvePermissionRestartAbortEntry,
4546
resolvePermissionRestartChatAdmission,
4647
resolveRestartSafeChatAdmission,
4748
} from "./chat-restart-recovery.js";
@@ -267,24 +268,19 @@ export async function admitChatSend(params: {
267268
? replyRunRegistry.resolveCurrentInterruptTarget(activeRunScopeKey)
268269
: undefined;
269270
const resolvedRunAbortEntry = expectedInterruptRunId
270-
? context.chatAbortControllers.get(expectedInterruptRunId)
271+
? resolvePermissionRestartAbortEntry({
272+
entries: context.chatAbortControllers,
273+
lifecycleGeneration,
274+
runId: expectedInterruptRunId,
275+
sessionId: latestEntry?.sessionId,
276+
sessionKey,
277+
})
271278
: undefined;
272-
const matchesExpectedRunAbort = Boolean(
273-
expectedInterruptRunId &&
274-
resolvedRunAbortEntry &&
275-
resolvedRunAbortEntry.kind !== "agent" &&
276-
resolvedRunAbortEntry.turnKind !== "btw" &&
277-
resolvedRunAbortEntry.lifecycleGeneration === lifecycleGeneration &&
278-
resolvedRunAbortEntry.sessionKey === sessionKey &&
279-
resolvedRunAbortEntry.sessionId === latestEntry?.sessionId,
280-
);
281-
if (!commitOutcome && expectedInterruptRunId && matchesExpectedRunAbort) {
279+
if (!commitOutcome && resolvedRunAbortEntry) {
282280
runAbortInterruptTarget = resolvedRunAbortEntry;
283281
}
284282
const capturedRunAbortStillMatches = Boolean(
285-
runAbortInterruptTarget &&
286-
context.chatAbortControllers.get(expectedInterruptRunId!) === runAbortInterruptTarget &&
287-
matchesExpectedRunAbort,
283+
runAbortInterruptTarget && resolvedRunAbortEntry === runAbortInterruptTarget,
288284
);
289285
if (
290286
expectedInterruptRunId &&

‎src/gateway/server.chat.gateway-server-chat.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ describe("gateway server chat", () => {
605605
?.abortSignal;
606606
dispatchStarted.resolve();
607607
if (!signal?.aborted) {
608-
await new Promise<void>((resolve) =>
609-
signal?.addEventListener("abort", () => resolve(), { once: true }),
610-
);
608+
await new Promise<void>((resolve) => {
609+
signal?.addEventListener("abort", () => resolve(), { once: true });
610+
});
611611
}
612612
return undefined;
613613
});

‎ui/src/i18n/locales/en.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5950,6 +5950,8 @@ export const en: TranslationMap = {
59505950
default: "Default",
59515951
defaultDescription: "Follow the agent's configured policy.",
59525952
fullRequiresAdmin: "Full access requires operator.admin access.",
5953+
activeRunUnavailable:
5954+
"The active turn cannot be restarted because its run identity is unavailable. Wait for it to finish, then change permissions.",
59535955
updateFailed: "Failed to update permissions: {error}",
59545956
modes: {
59555957
"read-only": {

‎ui/src/pages/chat/chat-pane-session-controls.test.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,31 +256,39 @@ describe("chat pane composer controls", () => {
256256
activeRunIds: ["run-active"],
257257
hasActiveRun: true,
258258
status: "running",
259-
restarts: true,
259+
outcome: "restart",
260260
},
261261
{
262262
label: "locally running with a stale idle session row",
263263
chatRunId: "run-active",
264264
activeRunIds: [],
265265
hasActiveRun: false,
266266
status: "done",
267-
restarts: true,
267+
outcome: "restart",
268268
},
269269
{
270270
label: "idle",
271271
chatRunId: null,
272272
activeRunIds: [],
273273
hasActiveRun: false,
274274
status: "done",
275-
restarts: false,
275+
outcome: "patch",
276+
},
277+
{
278+
label: "running session without active run IDs",
279+
chatRunId: null,
280+
activeRunIds: [],
281+
hasActiveRun: true,
282+
status: "running",
283+
outcome: "blocked",
276284
},
277285
{
278286
label: "running session with ambiguous active runs",
279287
chatRunId: null,
280288
activeRunIds: ["run-one", "run-two"],
281289
hasActiveRun: true,
282290
status: "running",
283-
restarts: false,
291+
outcome: "blocked",
284292
},
285293
] as const)("restarts only a $label session after changing permissions", async (sessionCase) => {
286294
const patch = vi.fn(async () => ({}));
@@ -319,7 +327,7 @@ describe("chat pane composer controls", () => {
319327

320328
await controls.permissionPicker.onSelect("guarded");
321329

322-
if (sessionCase.restarts) {
330+
if (sessionCase.outcome === "restart") {
323331
expect(restartTurn).toHaveBeenCalledWith(
324332
expect.objectContaining({
325333
key: state.sessionKey,
@@ -330,9 +338,13 @@ describe("chat pane composer controls", () => {
330338
}),
331339
);
332340
expect(patch).not.toHaveBeenCalled();
333-
} else {
341+
} else if (sessionCase.outcome === "patch") {
334342
expect(restartTurn).not.toHaveBeenCalled();
335343
expect(patch).toHaveBeenCalled();
344+
} else {
345+
expect(restartTurn).not.toHaveBeenCalled();
346+
expect(patch).not.toHaveBeenCalled();
347+
expect(state.chatError).toContain("Wait for it to finish");
336348
}
337349
});
338350

‎ui/src/pages/chat/chat-pane-session-controls.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
readSessionMethodAccess,
77
type SessionMethodAccess,
88
} from "../../lib/session-method-access.ts";
9+
import { isSessionRunActive } from "../../lib/session-run-state.ts";
910
import { scopedAgentParamsForSession } from "../../lib/sessions/index.ts";
1011
import { generateUUID } from "../../lib/uuid.ts";
1112
import { readChatSessionActionAccess } from "./chat-session-action-access.ts";
@@ -167,6 +168,8 @@ export function renderChatPaneComposerControls(params: {
167168
}
168169
const listedRunIds = selectedSession?.activeRunIds ?? [];
169170
const activeRunId = state.chatRunId ?? (listedRunIds.length === 1 ? listedRunIds[0] : null);
171+
const activeRunIdentityUnavailable =
172+
selectedSession && isSessionRunActive(selectedSession) && !activeRunId;
170173
const sessionKey = state.sessionKey;
171174
const client = state.client;
172175
const connectionEpoch = state.connectionEpoch;
@@ -179,6 +182,9 @@ export function renderChatPaneComposerControls(params: {
179182
scopedAgentParamsForSession(state, sessionKey).agentId === agentScope.agentId;
180183
try {
181184
state.chatError = state.lastError = null;
185+
if (activeRunIdentityUnavailable) {
186+
throw new Error(t("chat.permissionControls.activeRunUnavailable"));
187+
}
182188
const patched = activeRunId
183189
? await restartChatSessionTurn(state, {
184190
sessionKey,

0 commit comments

Comments
 (0)