* fix(server): stop a steered prompt from deadlocking on a permission Claude parks its turn inside the blocking canUseTool callback while a plan approval or a tool prompt waits for an answer. The SDK stops to read its input stream there, so a message sent at the open card is queued and never read. The user must reject the card or interrupt the agent by hand, and then send the message a second time. Deny the pending permissions after the steer is admitted. The prompt is already in the SDK input queue when the permission callback returns, so Claude reads it in the same turn. Plan mode stays on and no turn restarts. One pass is not enough. A turn that made several tool calls opens its next request 10 to 300 milliseconds after the previous one is answered, so a single snapshot advances the batch by one and parks the turn again. The Claude adapter already tracks the steers the SDK has not read, so expose that as an optional AgentSession.hasUnreadSteer and answer each new request while that method reports the prompt as unread. The release stops the moment the prompt is read, so a permission the agent asks for after it acts on the message still reaches the user. Only the two entrypoints that carry a message from a person set the flag. A scheduled prompt, a child-finished notification, and an agent-to-agent prompt must not answer a card the user is looking at. Denial left no timeline row, so a denied plan disappeared from the transcript. Claude and Codex now push a plan_approval row with approved: false. For Codex the row comes from resolvePlanPermission, which is the path that dismissPendingPlanApprovals also uses. In the app, queue mode held the message until the turn ended, and a parked turn never ends. The composer now sends while a permission is pending, and the button label agrees with the submit path. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(server): keep a post-prompt permission out of the release The release loop answered the pending requests before it asked whether the agent had read the prompt. The agent can read the prompt during a poll interval and then open a permission request because of it. The next pass denied that request, so a decision that belongs to the user was made for them. The loop now asks first and answers second. The first pass stays unconditional, because a provider that does not report unread steers must still get one release. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(server): make steer permission clearing provider-owned Keep the daemon policy explicit while letting each provider queue the steer and release permissions that block it. This removes timing-based polling and preserves rejected plans as canonical timeline rows. * fix(server): preserve omitted steer options Only construct provider steer options when permission clearing is requested. System prompts otherwise keep the existing undefined options contract. --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
195 lines
5.5 KiB
TypeScript
195 lines
5.5 KiB
TypeScript
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
|
|
import type { ActiveTurnBehavior } from "@getpaseo/protocol/messages";
|
|
import type { MessagePayload } from "@/composer/types";
|
|
import type { MessageInputKeyboardActionKind } from "@/keyboard/actions";
|
|
|
|
export type SendBehavior = ActiveTurnBehavior | "queue";
|
|
|
|
export function resolveActiveSendBehavior(
|
|
sendBehavior: SendBehavior,
|
|
hasPendingPermission: boolean,
|
|
): SendBehavior {
|
|
return sendBehavior === "queue" && hasPendingPermission ? "interrupt" : sendBehavior;
|
|
}
|
|
|
|
interface ComposerSurfaceState {
|
|
opacity: 0 | 1;
|
|
pointerEvents: "auto" | "none";
|
|
}
|
|
|
|
export interface ComposerSurfacePresentation {
|
|
input: ComposerSurfaceState;
|
|
overlay: ComposerSurfaceState;
|
|
}
|
|
|
|
const INPUT_PRESENTATION: ComposerSurfacePresentation = {
|
|
input: { opacity: 1, pointerEvents: "auto" },
|
|
overlay: { opacity: 0, pointerEvents: "none" },
|
|
};
|
|
|
|
const OVERLAY_PRESENTATION: ComposerSurfacePresentation = {
|
|
input: { opacity: 0, pointerEvents: "none" },
|
|
overlay: { opacity: 1, pointerEvents: "auto" },
|
|
};
|
|
|
|
export function resolveComposerSurfacePresentation(
|
|
showOverlay: boolean,
|
|
): ComposerSurfacePresentation {
|
|
return showOverlay ? OVERLAY_PRESENTATION : INPUT_PRESENTATION;
|
|
}
|
|
|
|
interface StopRealtimeVoiceContext {
|
|
voice: { stopVoice: () => Promise<unknown> } | null | undefined;
|
|
isRealtimeVoiceForCurrentAgent: boolean;
|
|
isAgentRunning: boolean;
|
|
client: { cancelAgent: (agentId: string) => Promise<unknown> } | null;
|
|
voiceAgentId: string | undefined;
|
|
}
|
|
|
|
interface SendActionContext {
|
|
defaultSendBehavior: SendBehavior;
|
|
isAgentRunning: boolean;
|
|
onQueue: ((payload: MessagePayload) => void) | undefined;
|
|
handleSendMessage: () => void;
|
|
handleQueueMessage: () => void;
|
|
}
|
|
|
|
interface DictationTranscriptContext {
|
|
value: string;
|
|
defaultSendBehavior: SendBehavior;
|
|
isAgentRunning: boolean;
|
|
onQueue: ((payload: MessagePayload) => void) | undefined;
|
|
onSubmit: (payload: MessagePayload) => void;
|
|
replaceText: (text: string) => void;
|
|
attachments: MessagePayload["attachments"];
|
|
cwd: string;
|
|
autoSend: boolean;
|
|
}
|
|
|
|
export function applyDictationTranscript(text: string, ctx: DictationTranscriptContext): void {
|
|
if (!text) return;
|
|
const shouldPad = ctx.value.length > 0 && !/\s$/.test(ctx.value);
|
|
const nextValue = `${ctx.value}${shouldPad ? " " : ""}${text}`;
|
|
|
|
if (!ctx.autoSend) {
|
|
ctx.replaceText(nextValue);
|
|
return;
|
|
}
|
|
|
|
ctx.replaceText(nextValue);
|
|
|
|
if (ctx.defaultSendBehavior === "queue" && ctx.isAgentRunning && ctx.onQueue) {
|
|
ctx.onQueue({ text: nextValue, attachments: ctx.attachments, cwd: ctx.cwd });
|
|
ctx.replaceText("");
|
|
return;
|
|
}
|
|
|
|
ctx.onSubmit({
|
|
text: nextValue,
|
|
attachments: ctx.attachments,
|
|
cwd: ctx.cwd,
|
|
forceSend: ctx.isAgentRunning || undefined,
|
|
});
|
|
}
|
|
|
|
interface MessageInputKeyboardActions {
|
|
focusInput: () => void;
|
|
isDictationRecording: () => boolean;
|
|
markTranscriptForSend: () => void;
|
|
confirmDictation: () => void | Promise<void>;
|
|
cancelDictation: () => void | Promise<void>;
|
|
startDictation: () => void | Promise<void>;
|
|
toggleRealtimeVoice: () => void;
|
|
isRealtimeVoiceActive: boolean;
|
|
toggleRealtimeVoiceMute: () => void;
|
|
}
|
|
|
|
export function computeCanStartDictation(input: {
|
|
client: DaemonClient | null;
|
|
isReadyForDictation: boolean | undefined;
|
|
disabled: boolean;
|
|
dictationUnavailableMessage: string | null | undefined;
|
|
}): boolean {
|
|
const socketConnected = input.client?.isConnected ?? false;
|
|
const readyForDictation = input.isReadyForDictation ?? socketConnected;
|
|
return (
|
|
socketConnected && readyForDictation && !input.disabled && !input.dictationUnavailableMessage
|
|
);
|
|
}
|
|
|
|
export function runDefaultSendAction(ctx: SendActionContext): void {
|
|
if (ctx.defaultSendBehavior === "queue" && ctx.isAgentRunning && ctx.onQueue) {
|
|
ctx.handleQueueMessage();
|
|
return;
|
|
}
|
|
ctx.handleSendMessage();
|
|
}
|
|
|
|
export function runAlternateSendAction(ctx: SendActionContext): void {
|
|
if (ctx.defaultSendBehavior === "queue") {
|
|
ctx.handleSendMessage();
|
|
return;
|
|
}
|
|
if (ctx.isAgentRunning && ctx.onQueue) {
|
|
ctx.handleQueueMessage();
|
|
}
|
|
}
|
|
|
|
export function runMessageInputKeyboardAction(
|
|
action: MessageInputKeyboardActionKind,
|
|
actions: MessageInputKeyboardActions,
|
|
): boolean {
|
|
if (action === "focus") {
|
|
actions.focusInput();
|
|
return true;
|
|
}
|
|
if (action === "send" || action === "dictation-confirm") {
|
|
if (actions.isDictationRecording()) {
|
|
actions.markTranscriptForSend();
|
|
void actions.confirmDictation();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
if (action === "voice-toggle") {
|
|
actions.toggleRealtimeVoice();
|
|
return true;
|
|
}
|
|
if (action === "voice-mute-toggle") {
|
|
if (actions.isRealtimeVoiceActive) {
|
|
actions.toggleRealtimeVoiceMute();
|
|
}
|
|
return true;
|
|
}
|
|
if (action === "dictation-cancel") {
|
|
if (actions.isDictationRecording()) {
|
|
void actions.cancelDictation();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
if (action === "dictation-toggle") {
|
|
if (actions.isDictationRecording()) {
|
|
actions.markTranscriptForSend();
|
|
void actions.confirmDictation();
|
|
} else {
|
|
void actions.startDictation();
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export async function stopRealtimeVoice(ctx: StopRealtimeVoiceContext): Promise<void> {
|
|
if (!ctx.voice || !ctx.isRealtimeVoiceForCurrentAgent) return;
|
|
|
|
if (ctx.isAgentRunning) {
|
|
if (!ctx.client || !ctx.voiceAgentId) {
|
|
throw new Error("Cannot stop the running voice agent while the host is unavailable");
|
|
}
|
|
await ctx.client.cancelAgent(ctx.voiceAgentId);
|
|
}
|
|
|
|
await ctx.voice.stopVoice();
|
|
}
|