UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

2,165 lines • 115 kB
import { projectCwdMismatch } from "./project-expected-cwd.js";
import { isHostOwnedToolRuntimeCapability, toolRuntimeCapabilities, } from "../../cli/tool-runtime.js";
import { messagePathProjectRoot, resolveMessagePath } from "../../core/message-path-resolution.js";
import { startOpenTimer } from "../../core/open-timing.js";
import { admitExternalMessage } from "./admit-external-message-command.js";
import { apiKeyLoginCredential } from "./api-key-login-credential.js";
import { clientFacingIdentity } from "./client-admission-record.js";
import { ExtensionOwnerLifetime } from "./extension-owner-lifetime.js";
/**
 * Per-connection RPC handler.
 *
 * This is the transport-independent core extracted from `runRpcMode`. It owns
 * exactly one AgentSession runtime and speaks the JSONL RPC protocol over an
 * injected output sink and a caller-driven line feed. It knows nothing about
 * `process.stdout`, `process.stdin`, or process signals — those belong to the
 * host (classic single-connection stdio in `rpc-mode.ts` or another transport
 * adapter).
 *
 * Behaviour is byte-for-byte identical to the original `runRpcMode` command
 * loop: the same responses, the same event stream, the same extension-UI
 * bridge, the same backpressure discipline. The only difference is that writes
 * go to `sink.writeRaw` instead of `writeRawStdout`, and the caller decides how
 * (and whether) to end the process.
 */
import * as crypto from "node:crypto";
import { basename, dirname, extname } from "node:path";
import { VERSION } from "../../config.js";
import { authMethodStatus, buildLoginProviderInfos } from "../../core/auth-providers.js";
import { ContinueFromLeafError } from "../../core/continue-from-leaf.js";
import { listCredentialAccounts, orderCredentialAccounts } from "../../core/credential-account-order.js";
import { CredentialAccountNotFoundError, resetCredentialAccount } from "../../core/credential-account-reset.js";
import { pinCredentialAccount, removeCredentialAccount, selectedOAuthAccount } from "../../core/credential-accounts.js";
import { AssistantEditError, SessionStreamingError } from "../../core/edited-assistant-message.js";
import { UserEditError } from "../../core/edited-user-message.js";
import { emitProviderAccountsChanged, subscribeProviderAccountEvents, } from "../../core/extensions/builtin/anthropic-subscription/account-events.js";
import { ProviderAccountTestError, testClaudeCliAccount, } from "../../core/extensions/builtin/anthropic-subscription/claude-cli-account-test.js";
import { takeClaudeCliLoginOutcome } from "../../core/extensions/builtin/anthropic-subscription/claude-cli-login-outcome.js";
import { isMcpControlInventoryChanged, MCP_CONTROL_INVENTORY_CHANGED_EVENT, MCP_CONTROL_INVENTORY_REQUEST_EVENT, } from "../../core/extensions/builtin/mcp/control-inventory.js";
import { applyFastMode, resolveServiceTierMemoryModel, } from "../../core/extensions/builtin/service-tier.js";
import { ExtensionRpcRequestNotFoundError } from "../../core/extensions/runner.js";
import { attachMcpGrantPresentation } from "../../core/mcp-grant-presentations.js";
import { describeUsageLimitEntry, describeUsageLimitMessage } from "../../core/retry-fallback/usage-limit.js";
import { setSessionArchived } from "../../core/session-archived.js";
import { parseSessionSettled, setSessionSettled, settledFields } from "../../core/session-settled.js";
import { carryRecovery } from "../../core/session-recovery-identity.js";
import { subscriptionLimits } from "../../core/subscription-limits/limit-store.js";
import { storedKeyHint, verifyApiKey } from "../../core/verify-api-key.js";
import { theme } from "../interactive/theme/theme.js";
import { automaticInputWouldBeHeld, holdAutomaticInput } from "./automatic-input-gate.js";
import { DURABLE_CLIENT_MESSAGE_ID_CAPABILITY } from "./client-admission-record.js";
import { ClientAdmissions } from "./client-admissions.js";
import { admitInputChain } from "./chain-admission.js";
import { handleClientInput } from "./client-input-handler.js";
import { ConnectionQuestionBridge, degradeQuestion, sessionQuestionBridges } from "./connection-question-bridge.js";
import { refusePromptAtMovedLeaf } from "./prompt-expected-leaf.js";
import { remoteOriginDeclaration, remoteOriginField } from "./remote-origin.js";
import { advertisedCompactCapabilities, sessionDeclaration } from "./compact-negotiation.js";
import { commandAuthority } from "./command-authority.js";
import { runInRecordScope } from "../../core/turn-origin.js";
import { recordTurnOrigin } from "./delegated-drive-origin.js";
import { ConnectionRoles, DELEGATION_INCARNATION_STALE, roleConnection } from "./connection-roles.js";
import { ACCOUNT_RESET_CAPABILITY, AUTH_KEY_VERIFY_CAPABILITY, AUTO_TITLE_SESSIONS_CAPABILITY, buildCustomUnsupportedRequest, CONTINUE_FROM_LEAF_CAPABILITY, DEFAULT_CUSTOM_EXTENSION_LABEL, EVAL_CELLS_CAPABILITY, EXTENSION_EVENTS_CAPABILITY, ENTRIES_TAIL_CAPABILITY, INTERACTIVE_USER_CAPABILITY, MEDIA_PLACEHOLDERS_CAPABILITY, PROVIDER_ACCOUNT_TEST_CAPABILITY, QUESTION_CAPABILITY, QUEUED_MESSAGE_REVISION_CAPABILITY, RESUME_SESSION_CAPABILITY, RETRY_FALLBACK_COMMAND_CAPABILITY, SESSION_HELD_CAPABILITY, TOOL_BARRIER_CAPABILITY, TASK_PARENT_CAPABILITY, } from "./custom-capability.js";
import { runAbort, runGetEffectStatus, runQueuedInputChange, runSetThinkingLevel, } from "./effect-commands.js";
import { EffectLedger } from "./effect-ledger.js";
import { EFFECT_LEDGER_CAPABILITY } from "./effect-record.js";
import { handleModelCommand } from "./handlers-model.js";
import { SET_PERMISSION_PRESET_CAPABILITY } from "./host-capabilities.js";
import { runSetPermissionPreset } from "./permission-preset-command.js";
import { createRpcEventOutputBuffer } from "./event-output-buffer.js";
import { extensionUiProgressReply, refusedExtensionUiResponse } from "./extension-ui-response.js";
import { answerExtensionUiResponse } from "./handlers-answer.js";
import { SecretInputRequests } from "./secret-input-requests.js";
import { answerGetEntries } from "./get-entries-command.js";
import { COMPACT_TRANSPORT_METHODS, compactTransportDisabled } from "./compact-transport-refusal.js";
import { runHoldInput } from "./held-input-commands.js";
import { HostSessionControl } from "./host-session-control.js";
import { createRpcLoginPromptCallbacks } from "./login-prompts.js";
import { answerMemoryReport } from "./memory-report-command.js";
import { readProjectSettings, readProjectTrust, writeProjectSetting, writeProjectTrust, } from "./project-workspace-commands.js";
import { readProjectFile, writeProjectFile } from "./project-file-commands.js";
import { getProjectMcpServers, setProjectMcpServers } from "./project-mcp-commands.js";
import { readProjectSkillFile, writeProjectSkillFile } from "./project-skill-commands.js";
import { readUserSkillFile, writeUserSkillFile } from "./user-skill-commands.js";
import { protocolIdentity } from "./protocol-identity.js";
import { buildRpcCommandsForSession, createCommandsChangedEvent, rpcCommandListDigest } from "./rpc-command-surface.js";
import { rpcCommandPayloadError, rpcCommandShapeError, rpcMessageLengthError, sessionRetryFallbackError, } from "./rpc-input-validation.js";
import { buildRpcSessionState } from "./rpc-session-state.js";
import { getSnapshot } from "./get-snapshot.js";
import { sessionUiAnswerLedger } from "./ui-answer-ledger.js";
import { RPC_ERROR_EVAL_CELL_NOT_FOUND, RPC_ERROR_EVAL_CELLS_UNAVAILABLE, RPC_ERROR_INVALID_ARGUMENT, RPC_ERROR_INVALID_EVAL_CELL_REQUEST, RPC_ERROR_MEDIA_NOT_FOUND, RPC_ERROR_MODEL_NOT_FOUND, STEER_WHEN_IDLE_CAPABILITY, USER_ACTIVITY_CAPABILITY, } from "./rpc-types.js";
import { SESSION_MODEL_SCOPE_CAPABILITY } from "./rpc-session-command-types.js";
import { runPauseForRun, runResumeForRun } from "./run-hold-commands.js";
import { interruptRunningTurn } from "./session-control-actions-data.js";
import { SessionExtensionUiRequests, sessionDialogRequests } from "./session-extension-ui-requests.js";
import { subscribeSessionRecords } from "./session-record-stream.js";
import { subscribeStructuredSessionRecords } from "./structured-session-records.js";
import { RpcSessionRelocation } from "./session-relocation.js";
import { answerHostResponse, isHostResponse, relocationChannel, } from "./session-relocation-channel.js";
export { buildRpcSessionState } from "./rpc-session-state.js";
/** The tool call a dialog is about, as wire fields (absent when the dialog is not a permission request). */
function dialogCall(opts) {
    const audience = opts?.audience === "session" ? { audience: "session" } : {};
    if (opts?.toolCallId === undefined)
        return audience;
    return {
        toolCallId: opts.toolCallId,
        ...(opts.parentToolCallId === undefined ? {} : { parentToolCallId: opts.parentToolCallId }),
        ...audience,
    };
}
function loadedExtensionName(path) {
    const synthetic = /^<(?:builtin|inline):([^>]+)>$/.exec(path);
    if (synthetic)
        return synthetic[1];
    const withoutExtension = basename(path, extname(path));
    if (withoutExtension !== "index")
        return withoutExtension;
    const parent = dirname(path);
    const parentName = basename(parent);
    return parentName === "src" || parentName === "dist" ? basename(dirname(parent)) : parentName;
}
function loadedExtensions(session) {
    return session.resourceLoader.getExtensions().extensions.map((extension) => ({
        name: loadedExtensionName(extension.path),
        path: extension.path,
        sourceInfo: extension.sourceInfo,
        enabled: true,
    }));
}
function loadedMcpStatus(server) {
    if (server.status !== undefined)
        return server.status;
    if (server.serverInfo !== null)
        return "connected";
    if (server.authStatus === "notLoggedIn")
        return "needs_auth";
    return "enabled";
}
function loadedMcpServers(snapshot) {
    return snapshot.servers.map((server) => ({
        name: server.name,
        toolCount: server.tools.length,
        status: loadedMcpStatus(server),
        authStatus: server.authStatus,
        ...(server.lastError === undefined ? {} : { lastError: server.lastError }),
    }));
}
/**
 * The prior digest a startup-settled announcement reports: the session leaves its MCP startup window, whatever digest
 * the connection last recorded, so a shared host's per-connection dedupe never drops it as a no-op transition.
 */
const MCP_STARTUP_PENDING_DIGEST = "mcp-startup-pending";
function loadedSurfacesFingerprint(session, extensions, mcpServers) {
    return JSON.stringify({
        skills: session.resourceLoader.getSkills().skills.map((skill) => ({
            name: skill.name,
            path: skill.filePath,
            sourceInfo: skill.sourceInfo,
            enabled: !skill.disableModelInvocation,
        })),
        extensions,
        mcpServers,
    });
}
/**
 * Locate one media block inside a tool result by `(toolCallId, contentIndex)`.
 *
 * Durable session entries are searched first: they survive the live message window
 * being compacted or trimmed away. The live `session.messages` cover the window
 * between `tool_execution_end` and persistence. Returns `undefined` unless the index
 * lands on an actual image block, so a text block or an out-of-range index is
 * reported as `media_not_found` rather than as a differently-shaped success.
 */
function findToolResultMedia(session, toolCallId, contentIndex) {
    const fromContent = (content) => {
        if (!Array.isArray(content))
            return undefined;
        const block = content[contentIndex];
        return block?.type === "image" ? block : undefined;
    };
    const matches = (message) => {
        const candidate = message;
        return candidate?.role === "toolResult" && candidate.toolCallId === toolCallId;
    };
    for (const entry of session.sessionManager.getEntries()) {
        if (entry.type !== "message")
            continue;
        if (!matches(entry.message))
            continue;
        const found = fromContent(entry.message.content);
        if (found)
            return found;
    }
    for (const message of session.messages) {
        if (!matches(message))
            continue;
        const found = fromContent(message.content);
        if (found)
            return found;
    }
    return undefined;
}
/**
 * Create a per-connection RPC handler bound to one runtime host and one sink.
 *
 * This performs the initial `rebindSession()` and returns synchronously with a
 * handler whose `handleInputLine` is ready to use. Signal handling, stdin
 * wiring, and process exit are intentionally NOT done here.
 */
export function createRpcConnectionHandler(runtimeHost, sink, options = {}) {
    let clientCapabilities = options.capabilities;
    let unsubscribeSecurityNotices;
    let detachGrantPresentation;
    let grantPresentationAbort = new AbortController();
    let securityBoundByHost = false;
    const connectionRoles = options.connectionRoles ?? new ConnectionRoles();
    const currentConnectionId = () => roleConnection(options.clientInfo?.connectionId());
    const hasRole = (role) => connectionRoles.has(currentConnectionId(), role);
    const connectionOrigin = () => connectionRoles.isDelegated(currentConnectionId()) ? "delegated" : "local";
    // senpi#6342: a turn follows this machine's own setting; only a device the forwarder did not state is the same account
    // counts as delegated for the permission rule.
    const turnOrigin = () => connectionOrigin() === "delegated" && !connectionRoles.sameAccount(currentConnectionId()) ? "delegated" : "local";
    const routingSessionId = options.sessionId;
    // True only while THIS connection's own command drives a replacement; the issuer
    // already learns the new identity from its command response.
    let replacementIssuedHere = false;
    let session = runtimeHost.session;
    const rpcOwners = new ExtensionOwnerLifetime(options.sessionId === undefined, options.rpcOwner, options.retireRpcBinding);
    let sessionControl;
    let relocation;
    const promptCalls = new Set();
    // Set by the first command that asks this session for a turn; `set_retry_fallback` is refused after it.
    let turnRequested = false;
    let unsubscribe;
    let unsubscribeStructured;
    let unsubscribeBackpressure;
    let unsubscribeLoadedSurfaces;
    let unsubscribeExtensionEvents;
    let mcpWireStatus = { servers: [] };
    /** Abort owner of the last settled turn; `session.currentAbortSource` clears on settle. */
    let lastAbortSource;
    let loadedSurfacesDigest;
    let rpcCommandsDigest;
    let suppressLoadedSurfaceEvents = false;
    let mcpStartupSettledPending = false;
    const eventOutput = sink.writeRecord
        ? undefined
        : createRpcEventOutputBuffer(sink.writeRaw, options.eventFlushScheduler);
    const effectContext = () => ({
        session,
        by: { connectionId: options.clientInfo?.connectionId() ?? "local" },
        ...(routingSessionId === undefined ? {} : { sessionId: routingSessionId }),
    });
    const tagSessionRecord = (value) => routingSessionId === undefined ? value : carryRecovery(value, { ...value, sessionId: routingSessionId });
    const output = (obj) => {
        const record = tagSessionRecord(obj);
        if (sink.writeRecord)
            sink.writeRecord(record);
        else
            eventOutput?.writeImmediate(record);
    };
    const outputEvent = (event) => {
        const record = tagSessionRecord(event);
        if (sink.writeRecord)
            sink.writeRecord(record);
        else
            eventOutput?.enqueueEvent(record);
    };
    const currentLoadedSurfaces = () => {
        const extensions = loadedExtensions(session);
        const mcpServers = loadedMcpServers(mcpWireStatus);
        return {
            data: { extensions, mcpServers },
            digest: loadedSurfacesFingerprint(session, extensions, mcpServers),
        };
    };
    const recordLoadedSurfaces = (emitChange) => {
        const current = currentLoadedSurfaces();
        const previous = loadedSurfacesDigest;
        const changed = previous !== undefined && previous !== current.digest;
        loadedSurfacesDigest = current.digest;
        if (emitChange && changed && !suppressLoadedSurfaceEvents)
            emitLoadedSurfacesChanged(previous, current.digest);
        return current;
    };
    const emitLoadedSurfacesChanged = (previous, digest) => {
        if (options.publishLoadedSurfacesChanged)
            options.publishLoadedSurfacesChanged(previous, digest);
        else
            outputEvent({ type: "loaded_surfaces_changed" });
    };
    /** The one event that ends the MCP startup window, emitted even when the digest is unchanged (senpi#4828). */
    const announceMcpStartupSettled = () => {
        if (suppressLoadedSurfaceEvents) {
            mcpStartupSettledPending = true;
            return;
        }
        mcpStartupSettledPending = false;
        loadedSurfacesDigest = currentLoadedSurfaces().digest;
        emitLoadedSurfacesChanged(MCP_STARTUP_PENDING_DIGEST, loadedSurfacesDigest);
    };
    const requestMcpWireStatus = async () => {
        let response;
        const request = {
            sessionId: session.sessionId,
            respond(snapshot) {
                response ??= snapshot;
            },
        };
        session.resourceLoader.emitExtensionEvent?.(MCP_CONTROL_INVENTORY_REQUEST_EVENT, request);
        mcpWireStatus = response === undefined ? { servers: [] } : await response;
    };
    const subscribeLoadedSurfaceEvents = () => {
        unsubscribeLoadedSurfaces?.();
        unsubscribeLoadedSurfaces = session.resourceLoader.onExtensionEvent?.(MCP_CONTROL_INVENTORY_CHANGED_EVENT, (data) => {
            if (!isMcpControlInventoryChanged(data) || data.sessionId !== session.sessionId)
                return;
            mcpWireStatus = data.snapshot;
            if (data.startupSettled === true)
                announceMcpStartupSettled();
            else if (!suppressLoadedSurfaceEvents)
                recordLoadedSurfaces(true);
        });
    };
    const unsubscribeProviderAccountEvents = subscribeProviderAccountEvents((event) => {
        if (event.type === "accounts_changed") {
            outputEvent({ type: "auth_accounts_changed", provider: event.provider });
            return;
        }
        outputEvent({
            type: "account_failover",
            provider: event.provider,
            from: event.from,
            to: event.to,
            reason: event.reason,
        });
    });
    const waitForRpcBackpressure = async () => {
        eventOutput?.flushEvents();
        await sink.waitForBackpressure();
    };
    const success = (id, command, data) => {
        if (data === undefined) {
            return { id, type: "response", command, success: true };
        }
        return { id, type: "response", command, success: true, data };
    };
    const error = (id, command, message, errorCode, errorData) => {
        const details = errorData === undefined && (command === "edit_user_message" || command === "navigate_tree")
            ? { leafId: session.sessionManager.getLeafId() }
            : errorData;
        return {
            id,
            type: "response",
            command,
            success: false,
            error: message,
            ...(errorCode ? { errorCode } : {}),
            ...(details === undefined ? {} : { errorData: details }),
        };
    };
    // Pending extension UI requests waiting for response
    const uiRequestChanged = (change) => {
        session.refreshLifecycle();
        if (change && routingSessionId !== undefined)
            output(change);
    };
    const pendingExtensionRequests = new SessionExtensionUiRequests(uiRequestChanged);
    const grantRequests = new Map();
    sessionDialogRequests.set(session, pendingExtensionRequests);
    const raiserOrigin = () => connectionRoles.originOf(options.clientInfo?.connectionId());
    const questions = new ConnectionQuestionBridge(output, uiRequestChanged, () => options.clientInfo?.connectionId(), raiserOrigin);
    let secretHandshake = false;
    const secretOwners = new Map();
    const secretCapable = (connection) => connectionRoles.has(connection, INTERACTIVE_USER_CAPABILITY) &&
        (connection === undefined
            ? secretHandshake && clientCapabilities?.includes("secret_input_v1") === true
            : options.clientInfo?.hasCapability?.(connection, "secret_input_v1") === true);
    const secretRoute = options.secretInputRoute ?? {
        presenter: (id) => secretOwners.get(id)?.connection,
        select: (id, cancel) => {
            const connection = options.clientInfo?.connectionId();
            if (!secretCapable(connection))
                return "client_unsupported";
            secretOwners.set(id, { connection, cancel });
            return undefined;
        },
        accepts: (id) => secretOwners.has(id) &&
            secretOwners.get(id)?.connection === options.clientInfo?.connectionId() &&
            secretCapable(secretOwners.get(id)?.connection),
        available: (id) => secretOwners.has(id) && secretCapable(secretOwners.get(id)?.connection),
        send: (_id, record) => output(record),
        settled: (id) => {
            const owner = secretOwners.get(id);
            if (owner)
                owner.cancel = undefined;
        },
        forget: (id) => {
            secretOwners.delete(id);
        },
    };
    const secretInputs = new SecretInputRequests(secretRoute, () => session.admitHumanAnswer());
    let shutdownRequested = false;
    // In-flight OAuth logins, keyed by provider. login_start registers one; the
    // login promise clears it on completion; login_cancel aborts it. The flow is
    // fire-command-then-subscribe: login_start responds immediately and the URL +
    // terminal result arrive as auth_login_url / auth_login_end events.
    const activeLogins = new Map();
    /** Helper for dialog methods with signal/timeout support */
    function createDialogPromise(opts, defaultValue, request, parseResponse) {
        if (opts?.signal?.aborted)
            return Promise.resolve(defaultValue);
        const id = crypto.randomUUID();
        const raised = { ...request, ...remoteOriginField(raiserOrigin()) };
        return new Promise((resolve, reject) => {
            let timeoutId;
            const cleanup = () => {
                if (timeoutId)
                    clearTimeout(timeoutId);
                opts?.signal?.removeEventListener("abort", onAbort);
                pendingExtensionRequests.delete(id);
                if (grantRequests.delete(id))
                    options.grantDialogSettled?.(id);
            };
            const onAbort = () => {
                cleanup();
                resolve(defaultValue);
            };
            opts?.signal?.addEventListener("abort", onAbort, { once: true });
            if (opts?.timeout) {
                timeoutId = setTimeout(() => {
                    cleanup();
                    resolve(defaultValue);
                }, opts.timeout);
            }
            pendingExtensionRequests.set(id, {
                frame: { type: "extension_ui_request", id, ...raised },
                resolve: (response) => {
                    cleanup();
                    resolve(parseResponse(response));
                },
                reject,
            });
            if (request.grantSelect === true)
                grantRequests.set(id, options.clientInfo?.connectionId());
            output({ type: "extension_ui_request", id, ...raised });
            if (request.grantSelect === true && typeof request.title === "string")
                options.grantDialogRaised?.(id, request.title);
        });
    }
    const selectGrantDialog = (title, choices, opts) => createDialogPromise(opts, undefined, {
        method: "select",
        title,
        options: choices,
        timeout: opts?.timeout,
        grantSelect: true,
    }, (response) => "cancelled" in response && response.cancelled
        ? undefined
        : "value" in response
            ? response.value
            : undefined);
    /**
     * Create an extension UI context that uses the RPC protocol.
     */
    const createExtensionUIContext = () => ({
        capabilities: { secretInput: true },
        question: (request, opts) => clientCapabilities?.includes(QUESTION_CAPABILITY)
            ? questions.ask(request, opts)
            : degradeQuestion(createExtensionUIContext(), request, opts),
        select: (title, options, opts) => createDialogPromise(opts, undefined, { method: "select", title, options, timeout: opts?.timeout, ...dialogCall(opts) }, (r) => ("cancelled" in r && r.cancelled ? undefined : "value" in r ? r.value : undefined)),
        confirm: (title, message, opts) => createDialogPromise(opts, false, { method: "confirm", title, message, timeout: opts?.timeout, ...dialogCall(opts) }, (r) => ("cancelled" in r && r.cancelled ? false : "confirmed" in r ? r.confirmed : false)),
        input: (title, placeholder, opts) => opts?.secret
            ? secretInputs.input(title, placeholder, opts)
            : createDialogPromise(opts, undefined, { method: "input", title, placeholder, timeout: opts?.timeout, ...dialogCall(opts) }, (r) => ("cancelled" in r && r.cancelled ? undefined : "value" in r ? r.value : undefined)),
        notify(message, type) {
            // Fire and forget - no response needed
            output({
                type: "extension_ui_request",
                id: crypto.randomUUID(),
                method: "notify",
                message,
                notifyType: type,
            });
        },
        onTerminalInput() {
            // Raw terminal input not supported in RPC mode
            return () => { };
        },
        setStatus(key, text) {
            // Fire and forget - no response needed
            output({
                type: "extension_ui_request",
                id: crypto.randomUUID(),
                method: "setStatus",
                statusKey: key,
                statusText: text,
            });
        },
        setSubagentCost(costUsd) {
            // Fire and forget, like setStatus: a client that renders the footer cost adds it there.
            output({
                type: "extension_ui_request",
                id: crypto.randomUUID(),
                method: "setSubagentCost",
                costUsd: costUsd !== undefined && Number.isFinite(costUsd) && costUsd >= 0 ? costUsd : undefined,
            });
        },
        setWorkingMessage(_message) {
            // Working message not supported in RPC mode - requires TUI loader access
        },
        setWorkingVisible(_visible) {
            // Working visibility not supported in RPC mode - requires TUI loader access
        },
        setWorkingIndicator(_options) {
            // Working indicator customization not supported in RPC mode - requires TUI loader access
        },
        setHiddenThinkingLabel(_label) {
            // Hidden thinking label not supported in RPC mode - requires TUI message rendering access
        },
        setWidget(key, content, options) {
            // Only string-array widgets cross the wire; a component factory needs a TUI to render into.
            if (content === undefined || Array.isArray(content)) {
                output({
                    type: "extension_ui_request",
                    id: crypto.randomUUID(),
                    method: "setWidget",
                    widgetKey: key,
                    widgetLines: content,
                    widgetPlacement: options?.placement,
                    ...(content !== undefined && options?.actions !== undefined && options.actions.length > 0
                        ? { widgetActions: options.actions.map(({ label, command }) => ({ label, command })) }
                        : {}),
                });
            }
        },
        setFooter(_factory) {
            // Custom footer not supported in RPC mode - requires TUI access
        },
        setHeader(_factory) {
            // Custom header not supported in RPC mode - requires TUI access
        },
        setTitle(title) {
            // Fire and forget - host can implement terminal title control
            output({
                type: "extension_ui_request",
                id: crypto.randomUUID(),
                method: "setTitle",
                title,
            });
        },
        setProgramStatus() {
            // OSC 7501 is a terminal report; an RPC session owns no terminal, so it sends no frame.
        },
        async custom() {
            // Custom UI cannot be rendered in RPC mode. By default this returns
            // undefined synchronously with NO wire message — byte-identical to the
            // original behavior. ONLY when the client advertised the
            // "custom_unsupported" capability do we emit an additive notice request
            // so an opt-in client can render a "requires the classic TUI" dialog before
            // returning undefined. The name is best-effort: ctx.ui.custom carries no
            // extension identity, so a generic label is used.
            const request = buildCustomUnsupportedRequest(clientCapabilities, DEFAULT_CUSTOM_EXTENSION_LABEL);
            if (request) {
                output(request);
            }
            return undefined;
        },
        pasteToEditor(text) {
            // Paste handling not supported in RPC mode - falls back to setEditorText
            this.setEditorText(text);
        },
        setEditorText(text) {
            // Fire and forget - host can implement editor control
            output({
                type: "extension_ui_request",
                id: crypto.randomUUID(),
                method: "set_editor_text",
                text,
            });
        },
        getEditorText() {
            // Synchronous method can't wait for RPC response
            // Host should track editor state locally if needed
            return "";
        },
        async editor(title, prefill, opts) {
            const id = crypto.randomUUID();
            const frame = {
                type: "extension_ui_request",
                id,
                method: "editor",
                title,
                prefill,
                ...dialogCall(opts),
                ...remoteOriginField(raiserOrigin()),
            };
            return new Promise((resolve, reject) => {
                pendingExtensionRequests.set(id, {
                    frame,
                    resolve: (response) => {
                        if ("cancelled" in response && response.cancelled) {
                            resolve(undefined);
                        }
                        else if ("value" in response) {
                            resolve(response.value);
                        }
                        else {
                            resolve(undefined);
                        }
                    },
                    reject,
                });
                output(frame);
            });
        },
        addAutocompleteProvider() {
            // Autocomplete provider composition is not supported in RPC mode
        },
        setEditorComponent() {
            // Custom editor components not supported in RPC mode
        },
        getEditorComponent() {
            // Custom editor components not supported in RPC mode
            return undefined;
        },
        get theme() {
            return theme;
        },
        getAllThemes() {
            return [];
        },
        getTheme(_name) {
            return undefined;
        },
        setTheme(_theme) {
            // Theme switching not supported in RPC mode
            return { success: false, error: "Theme switching not supported in RPC mode" };
        },
        getToolsExpanded() {
            // Tool expansion not supported in RPC mode - no TUI
            return false;
        },
        setToolsExpanded(_expanded) {
            // Tool expansion not supported in RPC mode - no TUI
        },
    });
    const refreshLoadedSurfacesAfter = async (operation, resetMcp) => {
        const previousDigest = loadedSurfacesDigest;
        const wasSuppressed = suppressLoadedSurfaceEvents;
        suppressLoadedSurfaceEvents = true;
        if (resetMcp)
            mcpWireStatus = { servers: [] };
        let commandsChanged;
        try {
            await operation();
            await requestMcpWireStatus();
            loadedSurfacesDigest = currentLoadedSurfaces().digest;
            const commands = buildRpcCommandsForSession(session);
            commandsChanged = createCommandsChangedEvent(rpcCommandsDigest, commands);
            rpcCommandsDigest = rpcCommandListDigest(commands);
        }
        finally {
            suppressLoadedSurfaceEvents = wasSuppressed;
        }
        if (!wasSuppressed && commandsChanged) {
            outputEvent(commandsChanged);
        }
        if (!wasSuppressed && mcpStartupSettledPending) {
            announceMcpStartupSettled();
        }
        else if (!wasSuppressed && previousDigest !== undefined && previousDigest !== loadedSurfacesDigest) {
            emitLoadedSurfacesChanged(previousDigest, loadedSurfacesDigest);
        }
    };
    runtimeHost.setRebindSession(async () => {
        // AgentSessionRuntime invokes this callback while completing a replacement.
        // Do not await extension binding here: extensions may wait for session work
        // that the replacement still owns. The command response must acknowledge the
        // committed replacement; derived surfaces refresh out of band afterwards.
        await rebindSession(true);
    });
    const rebindAfterLocalReplacement = async () => {
        replacementIssuedHere = true;
        try {
            await rebindSession();
        }
        finally {
            replacementIssuedHere = false;
        }
    };
    const refreshGrantPresentation = () => {
        grantPresentationAbort.abort();
        detachGrantPresentation?.();
        detachGrantPresentation = undefined;
        grantPresentationAbort = new AbortController();
        if (securityBoundByHost)
            return;
        if (!clientCapabilities?.includes("grant_select"))
            return;
        // A connection that speaks for another device never presents a grant (D11 R-1, senpi#6901 review H1).
        if (connectionRoles.originOf(currentConnectionId()) !== undefined)
            return;
        const signal = grantPresentationAbort.signal;
        detachGrantPresentation = attachMcpGrantPresentation(session.sessionId, (title, choices, opts) => selectGrantDialog(title, choices, {
            ...opts,
            signal: opts?.signal === undefined ? signal : AbortSignal.any([opts.signal, signal]),
        }));
    };
    const subscribeExtensionEvents = () => {
        unsubscribeExtensionEvents = clientCapabilities?.includes(EXTENSION_EVENTS_CAPABILITY)
            ? session.extensionRunner.onRpcEvent(({ name, data }) => {
                outputEvent({ type: "extension_event", name, data });
            })
            : undefined;
    };
    /** A classic connection's own declaration (senpi#4006); a shared host routes it through `clientInfo` instead. */
    const replaceClientCapabilities = (next) => {
        const hadEvents = clientCapabilities?.includes(EXTENSION_EVENTS_CAPABILITY) ?? false;
        const hadQuestion = clientCapabilities?.includes(QUESTION_CAPABILITY) ?? false;
        clientCapabilities = next;
        // A structured question the client can no longer answer settles once as cancelled.
        if (hadQuestion && !next.includes(QUESTION_CAPABILITY))
            questions.cancelAll();
        if (hadEvents === next.includes(EXTENSION_EVENTS_CAPABILITY))
            return;
        unsubscribeExtensionEvents?.();
        subscribeExtensionEvents();
    };
    const rebindSession = async (deferRefresh = false) => {
        secretInputs.close();
        rpcOwners.retireBinding();
        unbindMcpForms?.();
        unbindMcpForms = undefined;
        unsubscribeSecurityNotices?.();
        unsubscribeLoadedSurfaces?.();
        unsubscribeExtensionEvents?.();
        const replacedSession = session !== runtimeHost.session;
        session = runtimeHost.session;
        const hostedSecurity = options.bindSecurityPresentation?.(session, {
            ...createExtensionUIContext(),
            select: selectGrantDialog,
        });
        securityBoundByHost = hostedSecurity !== undefined;
        refreshGrantPresentation();
        unsubscribeSecurityNotices =
            hostedSecurity ??
                session.securityNotices?.subscribe((snapshot) => {
                    for (const notice of snapshot.notices)
                        output({
                            type: "extension_ui_request",
                            id: crypto.randomUUID(),
                            method: "notify",
                            notifyType: "warning",
                            message: notice.message,
                        });
                });
        sessionQuestionBridges.set(session, questions);
        sessionDialogRequests.set(session, pendingExtensionRequests);
        // Installed before the bind below: extensions register their control endpoint on session_start.
        sessionControl?.dispose();
        sessionControl = new HostSessionControl(session, runtimeHost.launchProfile?.sessionContext?.host_socket, (line) => void process.stderr.write(`senpi rpc session ${routingSessionId ?? "classic"}: ${line}\n`));
        session.setControlEndpointHost?.(sessionControl);
        // Only a host session can be released for a move, so only it offers relocation (docs/rpc.md).
        void relocation?.dispose();
        relocation =
            routingSessionId === undefined
                ? undefined
                : new RpcSessionRelocation({
                    session,
                    pendingPrompts: () => [...promptCalls],
                    channel: relocationChannel({
                        capabilities: () => clientCapabilities,
                        clientInfo: options.clientInfo,
                        output,
                        outputEvent,
                    }),
                });
        session.setRelocationHost?.(relocation);
        if (replacedSession) {
            lastAbortSource = undefined;
            if (routingSessionId !== undefined || !replacementIssuedHere) {
                // A replacement can be driven by ANY attached client, so every connection must be
                // told the live binding moved and given the new authoritative identity. Emitted
                // before the derived-surface refresh so a client cannot act on the old identity in
                // the window the refresh takes.
                outputEvent({
                    type: "session_replaced",
                    durableSessionId: session.sessionId,
                    sessionFile: session.sessionFile,
                    cwd: session.sessionManager.getCwd(),
                    sessionName: session.sessionName,
                });
            }
        }
        subscribeExtensionEvents();
        subscribeLoadedSurfaceEvents();
        // Subscribe to the replaced session before its bind runs, not after. The bind
        // is deferred by design - awaiting it would deadlock a client whose
        // session_start handler blocks on an extension_ui_request it cannot answer
        // while still awaiting the replacement response - and it still mutates the
        // session it owns: pi-rules appends a durable scan entry from session_start.
        // Those entries must reach every attached connection, and nothing else can
        // carry them, because the session file is not written until an assistant
        // message exists, so a client that misses the notification can never
        // reconstruct the session it is bound to. Installing here also drops the
        // previous session's subscription immediately instead of after the bind.
        installSessionSubscriptions();
        const extensionUi = createExtensionUIContext();
        unbindMcpForms = await options.bindMcpForms?.(session, extensionUi);
        const refresh = refreshLoadedSurfacesAfter(() => session.bindExtensions({
            uiContext: extensionUi,
            mode: "rpc",
            commandContextActions: {
                waitForIdle: () => session.agent.waitForIdle(),
                newSession: async (options) => runtimeHost.newSession(options),
                fork: async (entryId, forkOptions) => {
                    const result = await runtimeHost.fork(entryId, forkOptions);
                    return { cancelled: result.cancelled };
                },
                navigateTree: async (targetId, options) => {
                    const result = await session.navigateTree(targetId, {
                        summarize: options?.summarize,
                        customInstructions: options?.customInstructions,
                        replaceInstructions: options?.replaceInstructions,
                        label: options?.label,
                        expectedLeafId: options?.expectedLeafId,
                    });
                    return { cancelled: result.cancelled };
                },
                editAssistantMessage: async (entryId, text, options) => {
                    const result = await session.editAssistantMessage(entryId, text, {
                        summarize: options?.summarize,
                        customInstructions: options?.customInstructions,
                        expectedLeafId: options?.expectedLeafId,
                    });
                    return { cancelled: result.cancelled, unchanged: result.unchanged, entryId: result.entryId };
                },
                editUserMessage: async (entryId, text, options) => {
                    const result = await session.editUserMessage(entryId, text, {
                        summarize: options?.summarize,
                        customInstructions: options?.customInstructions,
                        expectedLeafId: options?.expectedLeafId,
                    });
                    return { cancelled: result.cancelled, unchanged: result.unchanged, entryId: result.entryId };
                },
                switchSession: async (sessionPath, options) => {
                    return runtimeHost.switchSession(sessionPath, options);
                },
                reload: async () => {
                    await refreshLoadedSurfacesAfter(async () => {
                        await session.reload();
                    }, false);
                },
            },
            shutdownHandler: () => {
                if (options.shutdownHandler) {
                    options.shutdownHandler();
                }
                else {
                    shutdownRequested = true;
                }
            },
            onError: (err) => {
                output({
                    type: "extension_error",
                    extensionPath: err.extensionPath,
                    event: err.event,
                    error: err.error,
                });
            },
        }), true);
        function installSessionSubscriptions() {
            unsubscribe?.();
            unsubscribeBackpressure?.();
            unsubscribeStructured?.();
            unsubscribe = unsubscribeBackpressure = undefined;
            // Structured first: an Agent built outside the session layer refuses it before anything is installed.
            unsubscribeStructured = sink.writeStructured
                ? subscribeStructuredSessionRecords(session, (record) => sink.writeStructured?.(tagSessionRecord(record)), (cause) => sink.structuredFailed?.(cause))
                : undefined;
            unsubscribe = subscribeSessionRecords(session, outputEvent, (abortSource) => {
                lastAbortSource = abortSource;
            });
            unsubscribeBackpressure = session.agent.subscribe(async () => {
                await waitForRpcBackpressure();
            });
        }
        // Seals what a previous load left nonterminal before any command can read or replay it.
        EffectLedger.forSession(session);
        if (deferRefresh) {
            // The subscription is already installed on the replaced session above, so
            // the deferred refresh only needs its failure reported. Re-installing here
            // would replay the settings-source selection a second time.
            void refresh.then(() => session.releaseSettledSessionMemory(), (cause) => {
                outputEvent({ type: "rpc_error", error: String(cause) });
            });
            return;
        }
        const timer = startOpenTimer("rebind");
        await refresh;
        timer.mark("bind_extensions");
        await ClientAdmissions.forSession(session).ready;
        timer.mark("client_admissions");
        // The open-time readers above (receipt seals, extension starts) read the full history; an idle
        // session lets it go now instead of holding it until its first turn settles (senpi#3519).
        session.releaseSettledSessionMemory();
    };
    /**
     * Drive an OAuth login for one provider as fire-command-then-subscribe.
     *
     * Reuses AuthStorage.login (the same callbacks the classic TUI uses). Only the
     * URL-based happy path is surfaced over RPC: onAuth emits an auth_login_url
     * event, onProgress (for example a `provider_login_start` handler's download
     * line) emits auth_login_progress, and success/failure/cancel emit a single
     * auth_login_end event. Callbacks
     * that need interactive mid-flow input (onPrompt/onSelect) are answered over
     * the extension UI dialog channel, so clients may either answer the dialog or
     * finish through the browser/callback-server path. An unanswered dialog never
     * blocks that completion path: it is released the moment the login settles.
     * Secrets are never emitted: only the provider id, the auth URL, the prompt
     * text/option labels, and a success flag (plus a non-secret error message)
     * cross the wire; a client's answer is consumed in-process.
     */
    const startLogin = async (provider, slotId) => {
        // A second login_start for the same provider aborts the prior attempt.
        activeLogins.get(provider)?.abort();
        const controller = new AbortController();
        const settled = new AbortController();
        const loginSignal = AbortSignal.any([controller.signal, settled.signal]);
        activeLogins.set(provider, controller);
        const authStorage = session.modelRegistry.authStorage;
        // A replacement left by an earlier login is not this login's; drop it so it is never reported here.
        authStorage.takeReplacement(provider);
        try {
            const callbacks = {
                onAuth: (info) => {
                    outputEvent({ type: "auth_login_url", provider, url: info.url });
                },
                onDeviceCode: (info) => {
                    outputEvent({ type: "auth_login_url", provider, url: info.verificationUri });
                },
                ...createRpcLoginPromptCallbacks(createExtensionUIContext(), loginSignal),
                onProgress: (message) => {
                    outputEvent({ type: "auth_login_progress", provider, text: message });
                },
                signal: controller.signal,
            };
            if (slotId !== undefined) {
                const reauthorize = selectedOAuthAccount(authStorage, provider, slotId);
                await session.modelRuntime.login(provider, "oauth", {
                    signal: controller.signal,
                    prompt: async (prompt) => {
                        const answer = prompt.type === "select"
                            ? await callbacks.onSelect?.(prompt)
                            : await callbacks.onPrompt?.(prompt);
                        if (answer === undefined)
                            throw new Error("Login cancelled");
                        return answer;
                    },
                    notify: (event) => {
                        switch (event.type) {
                            case "auth_url":
                                callbacks.onAuth(event);
                                break;
                            case "device_code":
                                callbacks.onDeviceCode(event);
                                break;
                            case "progress":
                                callbacks.onProgress?.(event.message);
                                break;
                            case "info":
                                callbacks.onProgress?.([event.message, ...(event.links ?? []).map((link) => link.url)].join("\n"));
                                break;
                        }
                    },
                }, { reauthorize, reauthorizeSlot: slotId });
                emitProviderAccountsChanged(provider);
            }
            else {
                await authStorage.login(provider, callbacks);
            }
            session.modelRegistry.refresh();
            emitProviderAccountsChanged(provider, "login");
            // The client shows what this login replaced and where the previous credential is (senpi#3486).
            const replacement = authStorage.takeReplacement(provider);
            // A Claude CLI sign-in names the account it signed in and its connection test (senpi#4436).
            const signedIn = takeClaudeCliLoginOutcome(controller.signal);
            outputEvent({ type: "auth_login_end", provider, success: true, ...replacement, ...signedIn });
        }
        catch (loginError) {
            const message = loginError instanceof Error ? loginError.message : String(loginError);
            outputEvent({ type: "auth_login_end", provider, success: false, error: message });
        }
        finally {
            // Release any dialog still pending so it can never outlive the login.
            settled.abort();
            if (activeLogins.get(provider) === controller) {
                activeLogins.delete(provider);
            }
        }
    };
    /**
     * Host capabilities for `applyFastMode`, mirroring what the `/fast` command passes from its
     * extension context. The notification is dropped: a command response carries the message
     * (`data` on success, `error` on refusal), so a duplicate `extension_ui_request` would be noise.
     */
    const fastModeContext = () => ({
        cwd: session.cwd,
        agentDir: session.agentDir,
        model: session.model,
        modelRegistry: session.modelRegistry,
        serviceTier: session.serviceTier,
        isProjectTrusted: () => session.settingsManager.isProjectTrusted(),
        notify: () => { },
        setSessionModel: async (model) => {
            if (!session.modelRuntime.hasConfiguredAuth(model.provider))
                return false;
            await session.setSessionModel(model, { source: "rpc", actor: "set_fast_mode" });
            return true;
        },
        setSessionFastMode: (enabled) => session.setSessionFastMode(enabled),
    });
    // Handle a single command
    const handleCommand = async (command, requestContext) => {
        const id = command.id;
        const cwdMismatch = projectCwdMismatch(command, session.sessionManager.getCwd());
        if (cwdMismatch !== undefined)
            return error(id, command.type, cwdMismatch.message, cwdMismatch.errorCode);
        switch (command.type) {
            case "get_protocol_info":
                return {
                    id,
                    type: "response",
                    command: "get_protocol_info",
                    success: true,
                    data: {
                        protocolVersion: 1,
                        serverVersion: VERSION,
                        capabilities: [
                            ...new Set([
                                "multi_session",
                                "snapshot_cursor",
                                "ui_answer_ledger",
                                "session_lifecycle",
                                AUTO_TITLE_SESSIONS_CAPABILITY,
                                MEDIA_PLACEHOLDERS_CAPABILITY,
                                "secret_input_v1",
                                DURABLE_CLIENT_MESSAGE_ID_CAPABILITY,
                                EFFECT_LEDGER_CAPABILITY,
                                STEER_WHEN_IDLE_CAPABILITY,
                                CONTINUE_FROM_LEAF_CAPABILITY,
                                EVAL_CELLS_CAPABILITY,
                                AUTH_KEY_VERIFY_CAPABILITY,
                                PROVIDER_ACCOUNT_TEST_CAPABILITY,
                                ACCOUNT_RESET_CAPABILITY,
                                QUEUED_MESSAGE_REVISION_CAPABILITY,
                                TOOL_BARRIER_CAPABILITY,
                                USER_ACTIVITY_CAPABILITY,
                                SESSION_MODEL_SCOPE_CAPABILITY,
                                RESUME_SESSION_CAPABILITY,
                                ENTRIES_TAIL_CAPABILITY,
                                SET_PERMISSION_PRESET_CAPABILITY,
                                ...(options.retryFallbackCommand ? [RETRY_FALLBACK_COMMAND_CAPABILITY] : []),
                                ...(options.toolRuntime ? toolRuntimeCapabilities(options.toolRuntime) : []),
                                // Client flags cannot advertise a host guard this classic binding does not run, nor a tool runtime.
                                ...advertisedCompactCapabilities(options.capabilities ?? [], false).filter((capability) => capability !== SESSION_HELD_CAPABILITY &&
                                    capability !== "park_claim" &&
                                    !isHostOwnedToolRuntimeCapability(capability)),
                            ]),
                        ],
                        mode: "classic",
                        ...protocolIdentity(),
                    },
                };
            case "open_session":
            case "park_session":
            case "claim_session":
                return error(id, command.type, "multi_session_disabled");
            // =================================================================
            // Prompting
            // =================================================================
            case "prompt": {
                // senpi#6922: a prompt decided at one leaf never runs from another; nothing is appended or sent.
                const leafRefusal = refusePromptAtMovedLeaf(session, command, automaticInputWouldBeHeld(session, hasRole(INTERACTIVE_USER_CAPABILITY)));
                if (leafRefusal !== undefined)
                    return error(id, "prompt", leafRefusal.message, leafRefusal.errorCode);
                const chain = admitInputChain(command.chainCount, session.settingsManager);
                if (chain.kind === "refused")
                    return error(id, "prompt", chain.refusal.message, chain.refusal.errorCode, chain.refusal.errorData);
                if (holdAutomaticInput(session, command, hasRole(INTERACTIVE_USER_CAPABILITY)))
                    return success(id, "prompt", { disposition: "handled", heldAsNotice: true });
                turnRequested = true;
                const admission = handleClientInput(session, command, { output, promptCalls }, { interactiveUser: hasRole(INTERACTIVE_USER_CAPABILITY), connectionOrigin: turnOrigin() });
                promptCalls.add(admission);
                void admission.finally(() => promptCalls.delete(admission));
                return undefined;
            }
            case "append_user_message": {
                const content = command.content;
                const message = typeof content === "string"
                    ? { role: "user", content, timestamp: Date.now() }
                    : { role: "user", content, timestamp: Date.now() };
                session.sessionManager.appendMessage(message);
                session.agent.state.messages = session.sessionManager.buildSessionContext().messages;
                return success(id, "append_user_message");
            }
            case "append_session_entry": {
                session.appendSessionEntry(command.entry);
                return success(id, "append_session_entry");
            }
            case "continue_from_leaf": {
                turnRequested = true;
                try {
                    await session.continueFromLeaf();
                    return success(id, "continue_from_leaf");
                }
                catch (err) {
                    if (err instanceof ContinueFromLeafError)
                        return error(id, command.type, err.message, err.code);
                    throw err;
                }
            }
            case "resume_session": {
                turnRequested = true;
                const { answer, settled } = await session.beginResume({
                    expectedSeq: command.expectedSeq,
                    mode: command.mode,
                    message: command.message,
                    effectId: command.effectId,
                    unlessStopped: command.unlessStopped,
                });
                promptCalls.add(settled);
                void settled.then(() => promptCalls.delete(settled), (err) => {
                    promptCalls.delete(settled);
                    output({
                        type: "extension_error",
                        extensionPath: "runtime",
                        event: "resume_session",
                        error: err instanceof Error ? err.message : String(err),
                    });
                });
                return success(id, "resume_session", answer);
            }
            case "send_custom_message": {
                // A context message that asks for no turn does not block a launch-time chain, like an extension's.
                if (command.triggerTurn === true)
                    turnRequested = true;
                await session.sendCustomMessage({
                    customType: command.customType,
                    content: command.content,
                    display: command.display,
                    details: command.details,
                }, { triggerTurn: command.triggerTurn, deliverAs: command.deliverAs });
                return success(id, "send_custom_message");
            }
            case "steer":
            case "follow_up": {
                const chain = admitInputChain(command.chainCount, session.settingsManager);
                if (chain.kind === "refused")
                    return error(id, command.type, chain.refusal.message, chain.refusal.errorCode, chain.refusal.errorData);
                if (holdAutomaticInput(session, command, hasRole(INTERACTIVE_USER_CAPABILITY)))
                    return success(id, command.type, { disposition: "handled", heldAsNotice: true });
                turnRequested = true;
                await handleClientInput(session, command, { output, promptCalls }, { interactiveUser: hasRole(INTERACTIVE_USER_CAPABILITY), connectionOrigin: turnOrigin() });
                return undefined;
            }
            case "abort":
                if (command.intent === "stop" && !hasRole(INTERACTIVE_USER_CAPABILITY))
                    return error(id, "abort", "stop_requires_interactive_client: declare interactive_user with set_client_info first");
                secretInputs.cancelAll();
                return runAbort(effectContext(), command, () => (command.intent === "stop" ? session.stop() : session.abort()).catch((cause) => {
                    // Report quiesce failures out of band; the answer never waits on them.
                    outputEvent({ type: "rpc_error", error: String(cause) });
                }));
            case "interrupt":
                if (command.intent === "stop" && !hasRole(INTERACTIVE_USER_CAPABILITY))
                    return error(id, "interrupt", "stop_requires_interactive_client: declare interactive_user with set_client_info first");
                if (command.intent === "stop") {
                    // A Stop names the user's intent, not one turn: an automatic turn that started after the named one stops too.
                    const stopped = await interruptRunningTurn(session, undefined, () => {
                        ClientAdmissions.forSession(session).noteAbort(effectContext().by);
                        return session.stop();
                    }, command.background);
                    if (!stopped.interrupted)
                        await session.stop();
                    return success(id, "interrupt", stopped);
                }
                return success(id, "interrupt", await interruptRunningTurn(session, command.turnId, () => {
                    ClientAdmissions.forSession(session).noteAbort(effectContext().by);
                    return session.abort();
                }, command.background));
            case "hold_input": {
                const answer = runHoldInput(session, command, {
                    interactiveUser: hasRole(INTERACTIVE_USER_CAPABILITY),
                    taskParent: hasRole(TASK_PARENT_CAPABILITY),
                });
                return answer.ok ? success(id, "hold_input", answer.data) : error(id, "hold_input", answer.error);
            }
            case "pause_for_run": {
                const answer = await runPauseForRun(session, command, hasRole(TASK_PARENT_CAPABILITY));
                return answer.ok ? success(id, "pause_for_run", answer.data) : error(id, "pause_for_run", answer.error);
            }
            case "resume_for_run": {
                const answer = await runResumeForRun(session, command, hasRole(TASK_PARENT_CAPABILITY));
                return answer.ok ? success(id, "resume_for_run", answer.data) : error(id, "resume_for_run", answer.error);
            }
            case "withdraw_held_input":
                if (!hasRole(INTERACTIVE_USER_CAPABILITY))
                    return error(id, "withdraw_held_input", "hold_input_requires_interactive_client");
                return success(id, "withdraw_held_input", { withdrawn: session.withdrawHeldInput(command.inputId) });
            case "abort_compaction": {
                session.abortCompaction();
                return success(id, "abort_compaction");
            }
            case "reload": {
                secretInputs.close();
                const result = await session.reload();
                return success(id, "reload", result);
            }
            case "check_reload_veto": {
                return success(id, "check_reload_veto", await session.checkReloadVeto());
            }
            case "dequeue_message":
            case "edit_queued_message":
                return runQueuedInputChange(effectContext(), command);
            case "clear_queue": {
                ClientAdmissions.clearQueuedFor(session);
                const cleared = session.clearQueue({ abortWillFollow: command.abortWillFollow ?? false });
                await session.whenQueueClearRecorded();
                return success(id, "clear_queue", {
                    steering: cleared.steering,
                    followUp: cleared.followUp,
                    ordered: [...cleared.ordered],
                });
            }
            case "get_steering_messages":
                return success(id, "get_steering_messages", {
                    messages: [...session.getSteeringMessages()],
                    ordered: session
                        .getQueuedInputs()
                        .filter((input) => input.mode === "steer")
                        .map(clientFacingIdentity),
                });
            case "get_follow_up_messages":
                return success(id, "get_follow_up_messages", {
                    messages: [...session.getFollowUpMessages()],
                    ordered: session
                        .getQueuedInputs()
                        .filter((input) => input.mode === "followUp")
                        .map(clientFacingIdentity),
                });
            case "abort_branch_summary":
                session.abortBranchSummary();
                return success(id, "abort_branch_summary");
            case "wake": {
                const deliveryIds = Array.isArray(command.delivery_ids)
                    ? command.delivery_ids.filter((entry) => typeof entry === "string")
                    : undefined;
                const result = (await sessionControl?.wake(deliveryIds)) ?? {};
                return success(id, "wake", { admitted: result.admitted ?? [] });
            }
            case "admit_external_message": {
                const peer = {
                    origin: connectionRoles.originOf(currentConnectionId()),
                    connectionOrigin: connectionOrigin(),
                    turnOrigin: turnOrigin(),
                };
                const answer = admitExternalMessage(session, command, peer);
                if (!answer.ok) {
                    const { refusal } = answer;
                    return error(id, command.type, refusal.message, refusal.errorCode, "errorData" in refusal ? refusal.errorData : undefined);
                }
                turnRequested = true;
                return success(id, command.type, answer.data);
            }
            case "new_session": {
                const options = command.parentSession ? { parentSession: command.parentSession } : undefined;
                const result = await runtimeHost.newSession(options);
                if (routingSessionId === undefined && !result.cancelled && session !== runtimeHost.session)
                    await rebindAfterLocalReplacement();
                return success(id, "new_session", result);
            }
            // =================================================================
            // State
            // =================================================================
            case "set_client_info": {
                const origin = remoteOriginDeclaration(command.remoteOrigin, (declared) => connectionRoles.acceptsOrigin(currentConnectionId(), declared));
                if ("refusal" in origin)
                    return error(id, "set_client_info", origin.refusal);
                const next = sessionDeclaration(options, command, clientCapabilities, (caps, incarnation) => connectionRoles.declare(currentConnectionId(), caps, incarnation), replaceClientCapabilities, commandAuthority.getStore()?.incarnation);
                if (next.refusal)
                    return error(id, "set_client_info", next.refusal, next.refusal === DELEGATION_INCARNATION_STALE ? "delegation_incarnation_stale" : undefined);
                if (origin.origin !== undefined)
                    connectionRoles.declareOrigin(currentConnectionId(), origin.origin);
                clientCapabilities = next.capabilities;
                if (command.capabilities !== undefined) {
                    secretHandshake = true;
                    secretRoute.handshake?.(command.capabilities);
                    secretInputs.recheck();
                }
                if (next.grantChanged || origin.origin !== undefined)
                    refreshGrantPresentation();
                return success(id, "set_client_info");
            }
            case "get_snapshot": {
                const snapshot = getSnapshot(session, command, {
                    lastAbortSource,
                    enqueue: (markerId) => {
                        if (routingSessionId !== undefined)
                            output({ type: "snapshot_marker", markerId });
                    },
                });
                return "error" in snapshot
                    ? error(id, "get_snapshot", snapshot.error, RPC_ERROR_INVALID_ARGUMENT)
                    : success(id, "get_snapshot", snapshot.data);
            }
            case "get_state":
                return success(id, "get_state", {
                    ...buildRpcSessionState(session, lastAbortSource),
                    answeredUiRequests: [...sessionUiAnswerLedger(session).snapshot(), ...secretInputs.answered()],
                });
            case "request_self_settle":
                return success(id, "request_self_settle", session.requestSelfSettle());
            case "get_effect_status":
                return runGetEffectStatus(effectContext(), command);
            case "get_pending_inputs":
                return success(id, "get_pending_inputs", await session.getPendingInputs(command.knownClientMessageIds));
            // =================================================================
            // Model
            // =================================================================
            case "set_model":
            case "set_favorite_models":
            case "set_scoped_models":
            case "cycle_model":
            case "get_available_models":
                return handleModelCommand(effectContext(), command);
            // =================================================================
            // Thinking
            // =================================================================
            case "set_thinking_level":
                return runSetThinkingLevel(effectContext(), command);
            case "set_permission_preset":
                return runSetPermissionPreset(runtimeHost, command);
            case "cycle_thinking_level": {
                const level = session.cycleThinkingLevel();
                if (!level) {
                    return success(id, "cycle_thinking_level", null);
                }
                return success(id, "cycle_thinking_level", { level });
            }
            case "get_available_thinking_levels":
                return success(id, "get_available_thinking_levels", {
                    levels: session.getAvailableThinkingLevels(),
                });
            case "get_model_thinking_level": {
                if (typeof command.provider !== "string" || typeof command.modelId !== "string") {
                    return error(id, "get_model_thinking_level", "get_model_thinking_level requires 'provider' and 'modelId'.", RPC_ERROR_INVALID_ARGUMENT);
                }
                const models = await session.modelRegistry.getAvailable();
                const model = models.find((m) => m.provider === command.provider && m.id === command.modelId);
                if (!model) {
                    return error(id, "get_model_thinking_level", `Model not found: ${command.provider}/${command.modelId}`, RPC_ERROR_MODEL_NOT_FOUND);
                }
                return success(id, "get_model_thinking_level", session.getThinkingLevelForModel(model));
            }
            // =================================================================
            // Fast mode
            // =================================================================
            case "set_fast_mode": {
                if (typeof command.enabled !== "boolean") {
                    return error(id, "set_fast_mode", "set_fast_mode requires a boolean 'enabled' field.");
                }
                const model = session.model;
                if (!model) {
                    return error(id, "set_fast_mode", "No active model.");
                }
                // Same entry point as the /fast command: persistence, `-fast` key normalization,
                // and pin precedence live in applyFastMode, never duplicated here.
                const result = await applyFastMode(fastModeContext(), command.enabled);
                if (!result.applied) {
                    // A refusal (non-Codex model, active `:priority` pin, failed model switch) has no
                    // notification channel for a command response, so it is reported as an error
                    // instead of a success that silently did nothing.
                    return error(id, "set_fast_mode", result.message);
                }
                const memoryModel = resolveServiceTierMemoryModel(session.modelRegistry, session.model ?? model);
                return success(id, "set_fast_mode", {
                    enabled: result.enabled,
                    serviceTier: result.recordedTier,
                    provider: memoryModel.provider,
                    modelId: memoryModel.id,
                });
            }
            case "get_fast_mode":
                return success(id, "get_fast_mode", {
                    enabled: session.isFastModeActive(),
                    serviceTier: session.effectiveServiceTier ?? null,
                });
            // =================================================================
            // Queue Modes
            // =================================================================
            case "set_steering_mode": {
                session.setSteeringMode(command.mode);
                return success(id, "set_steering_mode");
            }
            case "set_follow_up_mode": {
                session.setFollowUpMode(command.mode);
                return success(id, "set_follow_up_mode");
            }
            // =================================================================
            // Compaction
            // =================================================================
            case "compact": {
                const result = await session.compact(command.customInstructions);
                return success(id, "compact", result);
            }
            case "set_auto_compaction": {
                session.setAutoCompactionEnabled(command.enabled);
                return success(id, "set_auto_compaction");
            }
            // =================================================================
            // Retry
            // =================================================================
            case "set_auto_retry": {
                session.setAutoRetryEnabled(command.enabled);
                return success(id, "set_auto_retry");
            }
            case "abort_retry": {
                session.abortRetry();
                return success(id, "abort_retry");
            }
            case "set_retry_fallback": {
                if (!options.retryFallbackCommand) {
                    return error(id, "set_retry_fallback", "set_retry_fallback is for a single-session rpc process; a host session takes open_session.retryFallback.");
                }
                const profileError = sessionRetryFallbackError(command.retryFallback);
                if (profileError !== undefined || command.retryFallback === undefined) {
                    return error(id, "set_retry_fallback", profileError ?? "set_retry_fallback needs retryFallback.");
                }
                // A launch-time setting: refused once this connection asked for a turn (even one not started yet), while
                // any turn runs, or once the session holds turn history, so a chain never changes under a turn or a
                // retry already in flight. Extension context messages (`custom`, e.g. one a component adds on
                // session_start) are not a turn and do not count.
                if (turnRequested || session.isStreaming || session.messages.some((message) => message.role !== "custom")) {
                    return error(id, "set_retry_fallback", "set_retry_fallback must arrive before the session's first turn.");
                }
                runtimeHost.setRetryFallback(command.retryFallback);
                return success(id, "set_retry_fallback");
            }
            // =================================================================
            // Bash
            // =================================================================
            case "navigate_tree": {
                // Addressing refusals. The command type forbids both spellings at once and neither of
                // them, so TypeScript narrows these branches to `never` - they exist for the inbound
                // JSON no type can police, which is why the command name is written out here.
                if (command.entryId !== undefined && command.targetId !== undefined) {
                    return error(id, "navigate_tree", "navigate_tree takes either entryId or targetId, not both");
                }
                if (command.entryId === undefined && command.targetId === undefined) {
                    return error(id, "navigate_tree", "navigate_tree requires entryId or targetId");
                }
                const targetId = command.entryId ?? command.targetId;
                if (typeof targetId !== "string" || targetId.length === 0) {
                    return error(id, command.type, "navigate_tree requires a non-empty entryId or targetId");
                }
                if (command.intent !== undefined && command.intent !== "select" && command.intent !== "resume") {
                    return error(id, command.type, "navigate_tree intent must be select or resume");
                }
                try {
                    // Core owns selection/resumption, summaries, cancellation, and root reset.
                    // Pass the requested entry itself, not a client- or handler-computed parent.
                    const result = await session.navigateTree(targetId, {
                        ...(command.intent !== undefined ? { intent: command.intent } : {}),
                        summarize: command.summarize,
                        customInstructions: command.customInstructions,
                        replaceInstructions: command.replaceInstructions,
                        label: command.label,
                        expectedLeafId: command.expectedLeafId,
                    });
                    const leafId = session.sessionManager.getLeafId();
                    if (command.targetId !== undefined) {
                        return success(id, command.type, { ...result, leafId });
                    }
                    if (result.cancelled) {
                        return success(id, command.type, {
                            outcome: "cancelled",
                            leafId,
                            ...(result.aborted ? { aborted: true } : {}),
                        });
                    }
                    return success(id, command.type, {
                        outcome: "navigated",
                        leafId,
                        ...(result.editorText !== undefined ? { editorText: result.editorText } : {}),
                        ...(result.summaryEntry ? { summaryEntryId: result.summaryEntry.id } : {}),
                    });
                }
                catch (err) {
                    if (err instanceof AssistantEditError || err instanceof SessionStreamingError) {
                        return error(id, command.type, err.message, err.code);
                    }
                    throw err;
                }
            }
            case "record_bash_result":
                session.recordBashResult(command.command, command.result, {
                    excludeFromContext: command.excludeFromContext,
                });
                return success(id, "record_bash_result");
            case "release_tool_barrier":
                return success(id, "release_tool_barrier", { released: session.releaseToolBarrier(command.barrierId) });
            case "set_label":
                session.sessionManager.appendLabelChange(command.entryId, command.label);
                return success(id, "set_label");
            case "bash": {
                if (routingSessionId !== undefined)
                    outputEvent({ type: "bash_start" });
                try {
                    const eventResult = await session.extensionRunner.emitUserBash({
                        type: "user_bash",
                        command: command.command,
                        excludeFromContext: command.excludeFromContext ?? false,
                        cwd: session.sessionManager.getCwd(),
                    });
                    if (eventResult?.result) {
                        session.recordBashResult(command.command, eventResult.result, {
                            excludeFromContext: command.excludeFromContext,
                        });
                        return success(id, "bash", eventResult.result);
                    }
                    const result = await session.executeBash(command.command, undefined, {
                        excludeFromContext: command.excludeFromContext,
                        id: command.executionId,
                        // Functions cannot cross JSONL. Host extensions may still provide the
                        // executable operations object; client-supplied data is only a wire-safe hint.
                        operations: eventResult?.operations,
                    });
                    return success(id, "bash", result);
                }
                finally {
                    if (routingSessionId !== undefined)
                        outputEvent({ type: "bash_end" });
                }
            }
            case "abort_bash": {
                session.abortBash();
                return success(id, "abort_bash");
            }
            case "cleanup_bash_output":
                await session.cleanupBashOutput(command.path);
                return success(id, "cleanup_bash_output");
            // =================================================================
            // Session
            // =================================================================
            case "get_session_stats": {
                const stats = session.getSessionStats();
                return success(id, "get_session_stats", stats);
            }
            case "resolve_path":
                return success(id, "resolve_path", resolveMessagePath({
                    path: command.path,
                    cwd: session.cwd,
                    projectRoot: messagePathProjectRoot(session.cwd),
                }));
            case "memory_report": {
                const answer = await answerMemoryReport(session);
                return answer.ok ? success(id, "memory_report", answer.data) : error(id, "memory_report", answer.error);
            }
            case "export_html": {
                const path = await session.exportToHtml(command.outputPath, { themeName: command.themeName });
                return success(id, "export_html", { path });
            }
            case "export_jsonl": {
                return success(id, "export_jsonl", { path: session.exportToJsonl(command.outputPath) });
            }
            case "switch_session": {
                const result = await runtimeHost.switchSession(command.sessionPath, { cwdOverride: command.cwdOverride });
                if (routingSessionId === undefined && !result.cancelled && session !== runtimeHost.session)
                    await rebindAfterLocalReplacement();
                return success(id, "switch_session", result);
            }
            case "fork": {
                const result = await runtimeHost.fork(command.entryId, { position: command.position });
                if (routingSessionId === undefined && !result.cancelled && session !== runtimeHost.session)
                    await rebindAfterLocalReplacement();
                return success(id, "fork", { text: result.selectedText, cancelled: result.cancelled });
            }
            case "edit_assistant_message": {
                if (typeof command.entryId !== "string" ||
                    command.entryId.length === 0 ||
                    typeof command.text !== "string") {
                    return error(id, command.type, "edit_assistant_message requires a non-empty entryId and a text string");
                }
                try {
                    const result = await session.editAssistantMessage(command.entryId, command.text, {
                        summarize: command.summarize,
                        customInstructions: command.customInstructions,
                        expectedLeafId: command.expectedLeafId,
                    });
                    const leafId = session.sessionManager.getLeafId();
                    if (result.unchanged) {
                        return success(id, command.type, { outcome: "unchanged", leafId });
                    }
                    if (result.cancelled) {
                        return success(id, command.type, {
                            outcome: "cancelled",
                            leafId,
                            ...(result.aborted ? { aborted: true } : {}),
                        });
                    }
                    const entry = result.entryId ? session.sessionManager.getEntry(result.entryId) : undefined;
                    if (entry?.type !== "message" || leafId === null) {
                        return error(id, command.type, "Edited assistant entry was not persisted");
                    }
                    return success(id, command.type, {
                        outcome: "edited",
                        entry,
                        leafId,
                        ...(result.summaryEntry ? { summaryEntryId: result.summaryEntry.id } : {}),
                    });
                }
                catch (err) {
                    if (err instanceof AssistantEditError || err instanceof SessionStreamingError) {
                        return error(id, command.type, err.message, err.code);
                    }
                    throw err;
                }
            }
            case "edit_user_message": {
                if (typeof command.entryId !== "string" ||
                    command.entryId.length === 0 ||
                    typeof command.text !== "string") {
                    return error(id, command.type, "edit_user_message requires a non-empty entryId and a text string");
                }
                try {
                    const result = await session.editUserMessage(command.entryId, command.text, {
                        summarize: command.summarize,
                        customInstructions: command.customInstructions,
                        expectedLeafId: command.expectedLeafId,
                    });
                    const leafId = session.sessionManager.getLeafId();
                    if (result.unchanged) {
                        return success(id, command.type, { outcome: "unchanged", leafId });
                    }
                    if (result.cancelled) {
                        return success(id, command.type, {
                            outcome: "cancelled",
                            leafId,
                            ...(result.aborted ? { aborted: true } : {}),
                        });
                    }
                    const entry = result.entryId ? session.sessionManager.getEntry(result.entryId) : undefined;
                    if (entry?.type !== "message" || leafId === null) {
                        return error(id, command.type, "Edited user entry was not persisted");
                    }
                    return success(id, command.type, {
                        outcome: "edited",
                        entry,
                        leafId,
                        ...(result.summaryEntry ? { summaryEntryId: result.summaryEntry.id } : {}),
                    });
                }
                catch (err) {
                    if (err instanceof UserEditError ||
                        err instanceof AssistantEditError ||
                        err instanceof SessionStreamingError) {
                        return error(id, command.type, err.message, err.code);
                    }
                    throw err;
                }
            }
            case "clone": {
                const leafId = session.sessionManager.getLeafId();
                if (!leafId) {
                    return error(id, "clone", "Cannot clone session: no current entry selected");
                }
                const result = await runtimeHost.fork(leafId, { position: "at" });
                if (routingSessionId === undefined && !result.cancelled && session !== runtimeHost.session)
                    await rebindAfterLocalReplacement();
                return success(id, "clone", { cancelled: result.cancelled });
            }
            case "get_fork_messages": {
                const messages = session.getUserMessagesForForking();
                return success(id, "get_fork_messages", { messages });
            }
            case "get_entries":
                return answerGetEntries(session, command);
            case "get_tree": {
                const sessionManager = session.sessionManager;
                const tree = sessionManager.getTree();
                // getTree returns defensive nodes; keep the persisted entries diagnostic-bearing.
                const nodes = [...tree];
                for (const node of nodes) {
                    node.entry = describeUsageLimitEntry(node.entry);
                    for (const child of node.children)
                        nodes.push(child);
                }
                session.releaseFullHistoryView();
                return success(id, "get_tree", { tree, leafId: sessionManager.getLeafId() });
            }
            case "get_last_assistant_text": {
                const text = session.getLastAssistantText();
                return success(id, "get_last_assistant_text", { text });
            }
            case "import_jsonl": {
                const result = await runtimeHost.importFromJsonl(command.inputPath, command.cwdOverride);
                if (routingSessionId === undefined && !result.cancelled && session !== runtimeHost.session)
                    await rebindAfterLocalReplacement();
                return success(id, "import_jsonl", result);
            }
            case "set_session_name": {
                const name = command.name.trim();
                if (!name) {
                    return error(id, "set_session_name", "Session name cannot be empty");
                }
                session.setSessionName(name);
                return success(id, "set_session_name");
            }
            case "set_session_archived": {
                setSessionArchived(session.sessionManager, command.archived);
                outputEvent({ type: "session_archived_changed", archived: command.archived });
                return success(id, "set_session_archived");
            }
            case "set_session_settled": {
                // Read as unknown: a local client's `by` is unchecked by the inbound schema.
                const parsed = parseSessionSettled(command);
                if (!parsed.ok)
                    return error(id, "set_session_settled", parsed.error, RPC_ERROR_INVALID_ARGUMENT);
                setSessionSettled(session.sessionManager, parsed.value);
                outputEvent({ type: "session_settled_changed", ...settledFields(parsed.value) });
                return success(id, "set_session_settled");
            }
            case "record_user_activity": {
                // `prompt` activity is emitted by prompt admission itself; a client may only report a draft.
                // Read as unknown: the wire value is unchecked, whatever the declared command type says.
                const kind = command.kind;
                if (kind !== "draft") {
                    return error(id, "record_user_activity", 'record_user_activity: kind must be "draft"', RPC_ERROR_INVALID_ARGUMENT);
                }
                session.recordUserActivity("draft");
                return success(id, "record_user_activity");
            }
            // =================================================================
            // Messages
            // =================================================================
            case "get_subscription_limits": {
                return success(id, "get_subscription_limits", { accounts: subscriptionLimits.snapshot() });
            }
            case "get_messages": {
                return success(id, "get_messages", {
                    messages: session.messages.map((message) => message.role === "assistant" ? describeUsageLimitMessage(message) : message),
                });
            }
            // On-demand fetch of a media block a `media_placeholders` client received as
            // an `image_ref` stub. Durable session entries first (they survive the live
            // message window being compacted away), then the live messages for a result
            // that has not been persisted yet.
            case "get_media": {
                const { toolCallId, contentIndex } = command;
                const content = findToolResultMedia(session, toolCallId, contentIndex);
                if (!content) {
                    return error(id, "get_media", RPC_ERROR_MEDIA_NOT_FOUND, RPC_ERROR_MEDIA_NOT_FOUND);
                }
                return success(id, "get_media", { toolCallId, contentIndex, content });
            }
            // =================================================================
            // Commands (available for invocation via prompt)
            // =================================================================
            case "get_commands": {
                return success(id, "get_commands", { commands: buildRpcCommandsForSession(session) });
            }
            case "get_loaded_surfaces": {
                // A read answers with the inventory and never announces: changes are announced by their own notifications.
                await requestMcpWireStatus();
                return success(id, "get_loaded_surfaces", currentLoadedSurfaces().data);
            }
            case "get_project_trust":
                return success(id, "get_project_trust", await readProjectTrust(session));
            case "set_project_trust": {
                const result = await writeProjectTrust(session, command.choice, (task) => runtimeHost.runInProjectTrustChangeOrder(task));
                return result.ok
                    ? success(id, "set_project_trust", result.data)
                    : error(id, "set_project_trust", result.message, result.errorCode);
            }
            case "get_project_settings":
                return success(id, "get_project_settings", readProjectSettings(session));
            case "set_project_setting": {
                const result = await writeProjectSetting(session, command.key, command.value);
                return result.ok
                    ? success(id, "set_project_setting", result.data)
                    : error(id, "set_project_setting", result.message, result.errorCode);
            }
            case "read_project_file": {
                const result = await readProjectFile(session, command.path);
                return result.ok
                    ? success(id, "read_project_file", result.data)
                    : error(id, "read_project_file", result.message, result.errorCode, result.errorData);
            }
            case "write_project_file": {
                const result = await writeProjectFile(session, command.path, command.content, command.ifEtag);
                return result.ok
                    ? success(id, "write_project_file", result.data)
                    : error(id, "write_project_file", result.message, result.errorCode, result.errorData);
            }
            case "get_project_mcp_servers": {
                const result = await getProjectMcpServers(session);
                return result.ok
                    ? success(id, "get_project_mcp_servers", result.data)
                    : error(id, "get_project_mcp_servers", result.message, result.errorCode, result.errorData);
            }
            case "set_project_mcp_servers": {
                const result = await setProjectMcpServers(session, command.patch, command.ifEtag);
                return result.ok
                    ? success(id, "set_project_mcp_servers", result.data)
                    : error(id, "set_project_mcp_servers", result.message, result.errorCode, result.errorData);
            }
            case "read_project_skill_file": {
                const result = await readProjectSkillFile(session, command.skill);
                return result.ok
                    ? success(id, "read_project_skill_file", result.data)
                    : error(id, "read_project_skill_file", result.message, result.errorCode, result.errorData);
            }
            case "write_project_skill_file": {
                const result = await writeProjectSkillFile(session, command.skill, command.content, command.ifEtag);
                return result.ok
                    ? success(id, "write_project_skill_file", result.data)
                    : error(id, "write_project_skill_file", result.message, result.errorCode, result.errorData);
            }
            case "read_user_skill_file": {
                const result = await readUserSkillFile(session, command.skill);
                return result.ok
                    ? success(id, "read_user_skill_file", result.data)
                    : error(id, "read_user_skill_file", result.message, result.errorCode, result.errorData);
            }
            case "write_user_skill_file": {
                const origin = connectionRoles.originOf(currentConnectionId());
                const result = await writeUserSkillFile(session, command.skill, command.content, command.ifEtag, origin);
                return result.ok
                    ? success(id, "write_user_skill_file", result.data)
                    : error(id, "write_user_skill_file", result.message, result.errorCode, result.errorData);
            }
            case "extension_request":
            case "eval_cells.list":
            case "eval_cells.peek":
            case "eval_cells.stop": {
                const name = command.type === "extension_request" ? command.name.trim() : command.type;
                if (name.length === 0)
                    return error(id, command.type, "Extension RPC request name cannot be empty");
                try {
                    const data = await session.extensionRunner.requestRpc(name, command.type === "extension_request" ? command.data : command, requestContext);
                    return success(id, command.type, data);
                }
                catch (cause) {
                    if (cause instanceof ExtensionRpcRequestNotFoundError &&
                        (name === "eval_cells.list" || name === "eval_cells.peek" || name === "eval_cells.stop")) {
                        return error(id, command.type, "Eval cell controls are unavailable in this session", RPC_ERROR_EVAL_CELLS_UNAVAILABLE);
                    }
                    if (cause instanceof Error &&
                        "code" in cause &&
                        (cause.code === RPC_ERROR_EVAL_CELL_NOT_FOUND || cause.code === RPC_ERROR_INVALID_EVAL_CELL_REQUEST)) {
                        return error(id, command.type, cause.message, cause.code);
                    }
                    throw cause;
                }
            }
            // =================================================================
            // Auth (task 13)
            // =================================================================
            case "get_auth_providers": {
                const modelRegistry = session.modelRegistry;
                await modelRegistry.authStorage.readWhileBusy();
                const oauthInfos = buildLoginProviderInfos(modelRegistry, "oauth");
                const apiKeyInfos = buildLoginProviderInfos(modelRegistry, "api_key");
                const providers = [...oauthInfos, ...apiKeyInfos].map((info) => {
                    const origin = modelRegistry.getProviderOrigin(info.id);
                    return {
                        id: info.id,
                        name: info.name,
                        authType: info.authType,
                        status: authMethodStatus(modelRegistry, info),
                        ...storedKeyHint(modelRegistry, info),
                        ...(origin === undefined ? {} : { origin }),
                    };
                });
                // Read after the rows: building them re-reads a store that failed to load (#4413).
                const storeError = modelRegistry.authStorage.getLoadError();
                const storeBusy = modelRegistry.authStorage.isCredentialStoreBusy();
                return success(id, "get_auth_providers", {
                    providers,
                    ...(storeError ? { storeError } : {}),
                    ...(storeBusy ? { storeBusy: true } : {}),
                });
            }
            case "login_start": {
                // Respond IMMEDIATELY: success:true means the flow has started. The
                // URL and terminal result are delivered via auth_login_url /
                // auth_login_end events, because an interactive OAuth round-trip
                // cannot fit within the request timeout.
                void startLogin(command.provider, command.slotId);
                return success(id, "login_start");
            }
            case "login_cancel": {
                const controller = activeLogins.get(command.provider);
                controller?.abort();
                return success(id, "login_cancel");
            }
            case "login_api_key": {
                const credential = await apiKeyLoginCredential(session.modelRuntime.getProvider(command.provider), command.key, command.region);
                const replacement = session.modelRegistry.authStorage.set(command.provider, credential);
                // The response carries what this key replaced (senpi#3448). Consume the record before the
                // await below, so no record is pending across it and a concurrent OAuth login keeps its own (#3952).
                session.modelRegistry.authStorage.takeReplacement(command.provider);
                // Answer after the registry sees the key: a client re-reading auth status on this
                // response must not get the pre-login snapshot (#2384).
                await session.modelRegistry.refresh();
                return success(id, "login_api_key", replacement);
            }
            case "verify_api_key": {
                const verification = await verifyApiKey({
                    models: session.modelRegistry.getAll(),
                    provider: command.provider,
                    key: command.key,
                    signal: AbortSignal.timeout(15_000),
                });
                return success(id, "verify_api_key", verification);
            }
            case "logout": {
                // The runtime's logout signs every Claude CLI account out of the CLI before deleting the credential
                // (senpi#4436); a provider-wide logout drops the runtime key too, as `AuthStorage.logout` did. `slotId`
                // signs out one ChatGPT account and keeps its registration.
                if (command.slotId !== undefined)
                    selectedOAuthAccount(session.modelRegistry.authStorage, command.provider, command.slotId);
                const { notices, revocation } = await session.modelRuntime.logoutProvider(command.provider, {
                    slotId: command.slotId,
                });
                if (command.slotId === undefined)
                    session.modelRegistry.authStorage.removeRuntimeApiKey(command.provider);
                await session.modelRegistry.refresh();
                emitProviderAccountsChanged(command.provider);
                const data = {
                    ...(notices.length === 0 ? {} : { notices: [...notices] }),
                    ...(revocation === undefined ? {} : { status: revocation.status }),
                };
                return success(id, "logout", Object.keys(data).length === 0 ? undefined : data);
            }
            case "get_provider_accounts": {
                const activeName = session.modelRuntime.getActiveCredentialAccount(command.provider, session.sessionManager.getSessionId());
                const listing = await listCredentialAccounts(session.modelRegistry.authStorage, command.provider, process.env, undefined, activeName);
                return success(id, "get_provider_accounts", { accounts: listing.accounts, ordered: listing.ordered });
            }
            case "account_pin": {
                await pinCredentialAccount(session.modelRegistry.authStorage, command.provider, command.name);
                return success(id, "account_pin");
            }
            case "account_remove": {
                const { notice } = await removeCredentialAccount(session.modelRegistry.authStorage, command.provider, command.name);
                return success(id, "account_remove", notice === undefined ? undefined : { notice });
            }
            case "test_provider_account": {
                try {
                    const check = await testClaudeCliAccount({
                        provider: command.provider,
                        name: command.name,
                        cwd: session.cwd,
                        store: session.modelRegistry.authStorage,
                    });
                    return success(id, "test_provider_account", { check });
                }
                catch (testError) {
                    if (testError instanceof ProviderAccountTestError)
                        return error(id, "test_provider_account", testError.code);
                    throw testError;
                }
            }
            case "account_reset": {
                if (typeof command.provider !== "string" || typeof command.name !== "string") {
                    return error(id, "account_reset", "provider and name must be strings", "invalid_argument");
                }
                try {
                    await resetCredentialAccount(session.modelRegistry.authStorage, command.provider, command.name);
                }
                catch (resetError) {
                    if (resetError instanceof CredentialAccountNotFoundError) {
                        return error(id, "account_reset", "Provider account not found. Refresh the account list.", resetError.code);
                    }
                    return error(id, "account_reset", "Could not reset this account. Try again.", "account_reset_failed");
                }
                return success(id, "account_reset");
            }
            case "account_order": {
                const names = command.names;
                if (names !== null && !(Array.isArray(names) && names.every((name) => typeof name === "string"))) {
                    throw new Error("account_order names must be an array of account names, or null to clear the order");
                }
                await orderCredentialAccounts(session.modelRegistry.authStorage, command.provider, command.names);
                return success(id, "account_order");
            }
            default: {
                const unknownCommand = command;
                // senpi#5748: a connection that reaches this handler with one of these has no compact transport.
                if (COMPACT_TRANSPORT_METHODS.has(unknownCommand.type))
                    return compactTransportDisabled({ ...unknownCommand, id });
                return error(id, unknownCommand.type, `Unknown command: ${unknownCommand.type}`);
            }
        }
    };
    const handleInputLine = async (line, requestContext) => {
        let parsed;
        try {
            parsed = JSON.parse(line);
        }
        catch {
            output(error(undefined, "parse", "Failed to parse command: invalid JSON"));
            await waitForRpcBackpressure();
            return;
        }
        const shapeError = rpcCommandShapeError(parsed);
        if (shapeError) {
            output(error(undefined, "parse", shapeError));
            await waitForRpcBackpressure();
            return;
        }
        if (typeof parsed === "object" &&
            parsed !== null &&
            "type" in parsed &&
            parsed.type === "extension_ui_progress") {
            const progress = parsed;
            if (secretInputs.has(progress.id) || ("secret" in parsed && parsed.secret === true)) {
                output(refusedExtensionUiResponse(progress.id, "invalid_secret_response"));
                return;
            }
            // One correlated reply, after the question_updated the draft causes: a forwarder waits for a reply to every
            // record it passes on, and must not need a list of records that have none (senpi#5761).
            output(extensionUiProgressReply(progress.id, questions.progress(progress)));
            return;
        }
        if (isHostResponse(parsed)) {
            output(answerHostResponse(parsed, (frame) => relocation?.respond(frame) ?? "unknown"));
            return;
        }
        // Handle extension UI responses
        if (typeof parsed === "object" &&
            parsed !== null &&
            "type" in parsed &&
            parsed.type === "extension_ui_response") {
            const sender = options.clientInfo?.connectionId();
            const reply = answerExtensionUiResponse(parsed, {
                secretInputs,
                grantRequests,
                workerSecurityBridge: options.workerSecurityBridge === true,
                sender,
                grantCapable: () => sender === undefined
                    ? clientCapabilities?.includes("grant_select") === true
                    : options.clientInfo?.hasCapability?.(sender, "grant_select") === true,
                questions,
                dialogs: pendingExtensionRequests,
                routed: routingSessionId !== undefined,
                ledger: sessionUiAnswerLedger(session),
                // An answer is a person's input by its entry point, whatever the connection role.
                beforeAnswer: () => session.admitHumanAnswer(),
            });
            if (reply)
                output(reply);
            return;
        }
        const command = parsed;
        const payloadError = rpcCommandPayloadError(command);
        const messageLengthError = rpcMessageLengthError(command);
        if (payloadError || messageLengthError) {
            output(error(command.id, command.type, payloadError ?? messageLengthError));
            await waitForRpcBackpressure();
            return;
        }
        try {
            // senpi#6342: the record's own handling, and the first run it starts, run under the record's origin.
            const response = await runInRecordScope(recordTurnOrigin(command, turnOrigin()), () => handleCommand(command, requestContext));
            if (response) {
                output(response);
                await waitForRpcBackpressure();
            }
        }
        catch (commandError) {
            const missingCwd = commandError instanceof Error && commandError.name === "MissingSessionCwdError" && "issue" in commandError;
            output(error(command.id, command.type, commandError instanceof Error ? commandError.message : String(commandError), missingCwd ? "missing_session_cwd" : undefined, missingCwd ? commandError.issue : undefined));
            await waitForRpcBackpressure();
        }
    };
    // A single-session connection hears its account's new limit windows as they are reported. A host
    // session's handler (it has a routing `sessionId`) does not: the host broadcasts the record once.
    const stopLimitEvents = options.sessionId === undefined
        ? subscriptionLimits.subscribe((limits) => outputEvent({ type: "subscription_limits", ...limits }))
        : () => { };
    const dispose = async (disposeOptions = {}) => {
        unbindMcpForms?.();
        grantPresentationAbort.abort();
        detachGrantPresentation?.();
        unsubscribeSecurityNotices?.();
        stopLimitEvents();
        sessionControl?.dispose();
        sessionControl = undefined;
        const relocationSettled = relocation?.dispose();
        relocation = undefined;
        questions.cancelAll();
        secretInputs.dispose();
        pendingExtensionRequests.close();
        unsubscribeProviderAccountEvents();
        unsubscribe?.();
        unsubscribeStructured?.();
        unsubscribeBackpressure?.();
        unsubscribeLoadedSurfaces?.();
        unsubscribeExtensionEvents?.();
        unsubscribe = undefined;
        unsubscribeStructured = undefined;
        unsubscribeBackpressure = undefined;
        unsubscribeLoadedSurfaces = undefined;
        unsubscribeExtensionEvents = undefined;
        // The session teardown aborts the run after this dispose: a relocation answer already given reaches its tool first.
        await relocationSettled;
        if (options.disposeRuntime !== false) {
            await runtimeHost.dispose(disposeOptions);
        }
    };
    // Perform the initial bind synchronously-scheduled so the handler is ready
    // as soon as the caller awaits `ready`.
    let unbindMcpForms;
    const ready = rebindSession();
    return {
        ready,
        async handleInputLine(line) {
            const requestContext = rpcOwners.capture()?.context;
            await ready;
            await handleInputLine(line, requestContext);
        },
        pendingPrompts() {
            return [...promptCalls];
        },
        setClientCapabilities(capabilities) {
            replaceClientCapabilities([...capabilities]);
            refreshGrantPresentation();
        },
        isShutdownRequested() {
            return shutdownRequested;
        },
        cancelPendingExtensionUiRequests() {
            secretInputs.cancelAll();
            questions.cancelAll();
            pendingExtensionRequests.cancelAll();
        },
        connectionDetached(connectionId) {
            for (const [id, owner] of secretOwners)
                if (owner.connection === connectionId) {
                    owner.cancel?.();
                    secretOwners.delete(id);
                }
            relocation?.connectionDetached(connectionId);
        },
        async dispose(disposeOptions) {
            rpcOwners.dispose();
            await ready;
            await dispose(disposeOptions);
        },
    };
}
//# sourceMappingURL=connection-handler.js.map