Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

96.0 KB
Raw Blame
use crate::automation::{
    AutomationCommand, DevToolsAuthChallengeAction, DevToolsContinueInterceptedRequestCommand,
    DevToolsContinueInterceptedResponseCommand, DevToolsContinueWithAuthCommand,
    DevToolsFailInterceptedRequestCommand, DevToolsFulfillInterceptedRequestCommand,
    DevToolsRequestFailure, DevToolsRequestId, FrontendProtocol,
};
use crate::conn::{
    BackgroundNavigationBodyCompletionSink, BackgroundNavigationClaimError, CapturedBody,
    CdpConnection, Cmd, CommandOwnerScope, DEFAULT_LOADER_ID,
    PendingStreamingDocumentResponseNavigation, monotonic_timestamp_seconds,
};
use crate::domains::command_output::CommandOutputPlan;
use crate::domains::{activity, network, page};
use moli_core::page::{
    CompletedPageCommand, RendererSyntheticResponseBody, SubresourceResourceType,
};
use moli_url_policy::BrowserUrlScheme;
use url::Url;

use super::helpers::{
    decode_base64_bytes, response_headers_from_params, response_headers_with_presence_from_params,
};
use super::navigation::{
    complete_tokened_materialized_navigation_as_background_events_async,
    load_or_pause_navigation_for_auth_as_background_events_async,
};
use super::params::{
    CloseWebSocketParams, ContinueRequestParams, ContinueResponseParams,
    DispatchWebSocketMessageParams, ErrorReason, FailRequestParams, FulfillRequestParams,
    WebSocketMessageOpcode,
};
use super::state::{
    PreparedSubresourceCorrelation, action_session_id_for_devtools_context,
    pending_request_action_result_with_id_validation, take_pending_navigation,
    take_pending_subresource_fetch_request_for_action_session,
    take_pending_subresource_response_request_for_action_session,
};
use super::{
    FetchCommandOutput, FetchCommandTaskStep, PendingFetchCommandDispatch, PendingFetchCommandKind,
    PendingFetchCommandOperation,
};

const BLOCKED_BY_CLIENT_ERROR_TEXT: &str = "net::ERR_BLOCKED_BY_CLIENT";
const CONTINUE_RESPONSE_AFTER_BODY_TAKEN_ERROR: &str =
    "Unable to continue request as is after body is taken";
const CONTINUE_RESPONSE_PARTIAL_OVERRIDE_ERROR: &str =
    "Cannot override only status or headers, both should be provided";

fn emit_devtools_empty_success(out: &mut FetchCommandOutput) {
    out.push_success();
}

pub(super) fn start_devtools_fetch_command(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    command_session_id: Option<&str>,
    command: AutomationCommand,
) -> FetchCommandTaskStep {
    let owner = CommandOwnerScope::capture(conn, command_session_id);
    start_devtools_fetch_command_for_owner(conn, command_id, &owner, command)
}

pub(super) fn start_devtools_fetch_command_for_owner(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    owner: &CommandOwnerScope,
    command: AutomationCommand,
) -> FetchCommandTaskStep {
    match command {
        AutomationCommand::ContinueInterceptedRequest(command) => {
            start_devtools_continue_intercepted_request_command(conn, command_id, owner, &command)
        }
        AutomationCommand::ContinueInterceptedResponse(command) => {
            start_devtools_continue_intercepted_response_command(conn, command_id, owner, &command)
        }
        AutomationCommand::ContinueWithAuth(command) => {
            super::auth::start_devtools_continue_with_auth_command(
                conn, command_id, owner, &command,
            )
        }
        AutomationCommand::FailInterceptedRequest(command) => {
            start_devtools_fail_intercepted_request_command(conn, command_id, owner, command)
        }
        AutomationCommand::FulfillInterceptedRequest(command) => {
            start_devtools_fulfill_intercepted_request_command(conn, command_id, owner, command)
        }
        _ => FetchCommandTaskStep::Complete(CommandOutputPlan::error(
            -32000,
            "UnsupportedDevToolsCommand",
        )),
    }
}

fn navigation_fail_request_failure(error_reason: Option<ErrorReason>) -> DevToolsRequestFailure {
    match error_reason {
        Some(ErrorReason::Aborted) => DevToolsRequestFailure::Aborted,
        Some(ErrorReason::BlockedByClient) => {
            DevToolsRequestFailure::Failed(BLOCKED_BY_CLIENT_ERROR_TEXT.to_owned())
        }
        Some(reason) => DevToolsRequestFailure::Failed(reason.as_ref().to_owned()),
        None => DevToolsRequestFailure::Failed("Fetch request failed".to_owned()),
    }
}

fn optional_response_code(code: Option<i64>) -> Result<Option<u16>, ()> {
    code.map(u16::try_from).transpose().map_err(|_| ())
}

fn parse_continue_request_url(raw_url: &str) -> Result<Url, ()> {
    let url = Url::parse(raw_url).map_err(|_| ())?;
    match BrowserUrlScheme::from_url(&url) {
        BrowserUrlScheme::Http
        | BrowserUrlScheme::Https
        | BrowserUrlScheme::WebSocket
        | BrowserUrlScheme::SecureWebSocket => Ok(url),
        _ => Err(()),
    }
}

pub(super) enum PendingContinueRequestState {
    SubresourceFetch {
        correlation: PreparedSubresourceCorrelation,
    },
    Navigation {
        pending: Box<crate::conn::PendingFetchNavigation>,
    },
}

pub(super) fn start_continue_request_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: ContinueRequestParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let command = match build_cdp_continue_intercepted_request_command(conn, cmd, params) {
        Ok(command) => command,
        Err(()) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    start_devtools_fetch_command(
        conn,
        cmd.id,
        cmd.session_id,
        AutomationCommand::ContinueInterceptedRequest(command),
    )
}

fn build_cdp_continue_intercepted_request_command(
    conn: &CdpConnection,
    cmd: &Cmd<'_>,
    params: ContinueRequestParams,
) -> Result<DevToolsContinueInterceptedRequestCommand, ()> {
    if let Some(url) = params.url.as_deref() {
        parse_continue_request_url(url)?;
    }
    let decoded_post_data = match params.post_data.as_ref().map(AsRef::as_ref) {
        Some(post_data) => match decode_base64_bytes(post_data) {
            Ok(body) => Some(bytes::Bytes::from(body)),
            Err(()) => return Err(()),
        },
        None => None,
    };
    let headers = params.headers.map(|headers| {
        let mut fields = moli_fetch::RequestHeaders::default();
        for header in headers {
            if let Some((_, value)) = fields
                .iter_mut()
                .find(|(name, _)| name.eq_ignore_ascii_case(&header.name))
            {
                *value = header.value.into_bytes();
            } else {
                fields.push((header.name, header.value.into_bytes()));
            }
        }
        fields
    });
    let (browser_context_id, target_id) =
        devtools_fetch_owner_identity_for_session(conn, cmd.session_id);
    Ok(DevToolsContinueInterceptedRequestCommand {
        context: cmd.automation_context(target_id.as_deref(), browser_context_id.as_deref()),
        request_id: DevToolsRequestId::from(params.request_id.as_ref().to_owned()),
        url: params.url,
        method: params.method,
        post_data: decoded_post_data,
        headers,
        intercept_response: params.intercept_response.unwrap_or(false),
    })
}

fn parsed_continue_request_url(
    command: &DevToolsContinueInterceptedRequestCommand,
) -> Result<Option<Url>, ()> {
    match command.url.as_deref() {
        Some(url) => parse_continue_request_url(url).map(Some),
        None => Ok(None),
    }
}

fn start_devtools_continue_intercepted_request_command(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    owner: &CommandOwnerScope,
    command: &DevToolsContinueInterceptedRequestCommand,
) -> FetchCommandTaskStep {
    let command_session_id = owner.session_id();
    let parsed_url = match parsed_continue_request_url(command) {
        Ok(parsed_url) => parsed_url,
        Err(()) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let request_id = command.request_id.as_str().to_owned();
    let action_session_id = action_session_id_for_devtools_context(
        command_session_id,
        command.context.protocol,
        command.context.session_id.as_ref(),
    );
    if let Some(pending) = take_pending_subresource_fetch_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        let mut pending = pending;
        let command_headers =
            command
                .headers
                .clone()
                .map(|headers| match command.context.protocol {
                    FrontendProtocol::Cdp => moli_fetch::RequestHeaderOverride::current_request(
                        headers,
                        pending
                            .request_stage_pause_state()
                            .and_then(|chain| chain.header_override.as_ref()),
                    ),
                    FrontendProtocol::WebDriverClassic | FrontendProtocol::WebDriverBidi => {
                        moli_fetch::RequestHeaderOverride::RedirectChain(headers)
                    }
                });
        if pending
            .request_stage_pause_state()
            .is_some_and(|chain| !chain.remaining_sessions.is_empty())
        {
            pending.apply_request_stage_continue_modifications(
                parsed_url,
                command.method.clone(),
                command.post_data.clone(),
                command_headers,
            );
            let Some(event) =
                super::subresource::next_chained_subresource_request_pause_event(conn, pending)
            else {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "RequestNotFound",
                ));
            };
            let mut plan = CommandOutputPlan::default();
            plan.push_success();
            plan.push_background_event(event);
            return FetchCommandTaskStep::Complete(plan);
        }
        let (chain_url, chain_method, chain_body, chain_headers) =
            pending.accumulated_request_stage_continue_modifications();
        let parsed_url = parsed_url.or(chain_url);
        let method = command.method.clone().or(chain_method);
        let post_data = if command.post_data.is_some() {
            command.post_data.clone().map(Some)
        } else {
            chain_body
        };
        let headers = command_headers.or(chain_headers);
        pending.request_stage_chain = None;
        let configured_response_stage = conn
            .target_fetch_subresource_interception_snapshot_for_owner(owner)
            .is_some_and(|snapshot| {
                snapshot.has_response_stage_candidate(pending.resource_type.into())
            });
        let intercept_response = command.intercept_response || configured_response_stage;
        if let Some(continuation) = pending.detached_parser_script_fetch_continuation() {
            if !continuation.continue_request(parsed_url) {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "RequestNotFound",
                ));
            }
            return FetchCommandTaskStep::Complete(CommandOutputPlan::success());
        }
        let handle_auth_requests = conn.target_fetch_handle_auth_requests_for_owner(owner);
        let should_register =
            pending.resource_type != SubresourceResourceType::WebSocket || intercept_response;
        let correlation = match if configured_response_stage && !command.intercept_response {
            PreparedSubresourceCorrelation::prepare_deferred_response_stage(
                conn,
                owner,
                &request_id,
                &pending,
            )
        } else {
            PreparedSubresourceCorrelation::prepare(
                conn,
                owner,
                &request_id,
                &pending,
                should_register,
            )
        } {
            Some(correlation) => correlation,
            None => {
                conn.register_pending_subresource_fetch_request_for_owner(
                    owner, request_id, pending,
                );
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "RequestNotFound",
                ));
            }
        };
        let pending_page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page
                .start_continue_pending_subresource_fetch(
                    pending.internal_id,
                    parsed_url,
                    method,
                    post_data,
                    headers,
                    intercept_response,
                    handle_auth_requests,
                )
                .map_err(|error| format!("subresource fetch continue failed: {error}")),
            Err(message) => Err(message.to_owned()),
        };
        let pending_page = match pending_page {
            Ok(pending_page) => pending_page,
            Err(message) => {
                correlation.rollback(conn);
                conn.register_pending_subresource_fetch_request_for_owner(
                    owner, request_id, pending,
                );
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::ContinueRequest {
                state: Box::new(PendingContinueRequestState::SubresourceFetch { correlation }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }
    if let Some(mut pending) = take_pending_navigation(conn, owner, action_session_id, &request_id)
    {
        if command.intercept_response {
            pending.intercept_response = true;
            pending.response_stage_url_match_policy =
                crate::conn::ResponseStageUrlMatchPolicy::AlreadyMatched;
        }
        if let Some(parsed) = parsed_url {
            pending.navigation.requested_url = parsed;
        }
        if let Some(method) = command.method.clone() {
            pending.navigation.request_method = method;
        }
        if let Some(body) = command.post_data.clone() {
            pending.navigation.set_request_body_bytes(body);
        }
        if let Some(headers) = command.headers.clone() {
            if command.context.protocol == FrontendProtocol::Cdp {
                pending
                    .navigation
                    .redirect_headers
                    .get_or_insert_with(|| pending.navigation.request_headers.clone());
            } else {
                pending.navigation.redirect_headers = None;
            }
            pending.navigation.request_headers = headers;
        }
        pending.request_cookie_report = page::navigation_cookie_access_report(
            conn,
            command_session_id,
            &pending.navigation.requested_url,
            &pending.navigation.request_method,
            None,
            pending.navigation.request_load_policy,
            None,
        );
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::ContinueRequest {
                state: Box::new(PendingContinueRequestState::Navigation {
                    pending: Box::new(pending),
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }
    pending_request_action_output_plan(
        conn,
        owner,
        &request_id,
        command.context.protocol == FrontendProtocol::Cdp,
    )
    .map_or_else(FetchCommandTaskStep::Complete, |()| {
        FetchCommandTaskStep::Complete(CommandOutputPlan::success())
    })
}

pub(super) async fn complete_continue_request_command_async(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
    state: PendingContinueRequestState,
    out: &mut FetchCommandOutput,
) {
    match state {
        PendingContinueRequestState::SubresourceFetch { correlation } => {
            if let Err(error) = finish_continue_subresource_request(conn, owner, completed) {
                correlation.rollback(conn);
                out.push_error(-32000, error);
                return;
            }
            emit_devtools_empty_success(out);
        }
        PendingContinueRequestState::Navigation { pending } => {
            emit_devtools_empty_success(out);
            load_or_pause_navigation_for_auth_as_background_events_async(
                conn, out, *pending, None, None,
            )
            .await;
        }
    }
}

fn finish_continue_subresource_request(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
) -> Result<(), String> {
    let completion = completed.ok_or_else(|| "Missing renderer completion".to_owned())??;
    let page = conn.loaded_page_mut_for_protocol_access_for_owner(owner)?;
    page.finish_continue_pending_subresource_fetch(completion)
        .map(|_| ())
        .map_err(|error| format!("subresource fetch continue failed: {error}"))
}

pub(super) enum PendingFailRequestState {
    Navigation {
        pending: Box<crate::conn::PendingFetchNavigation>,
        failure: DevToolsRequestFailure,
    },
    SubresourceFetch {
        pending: Box<crate::conn::PendingSubresourceFetchRequest>,
    },
    SubresourceResponse {
        pending: Box<crate::conn::PendingSubresourceFetchResponseRequest>,
    },
    ResponseTransfer {
        transfer: Box<crate::conn::PausedDocumentTransfer>,
        failure: DevToolsRequestFailure,
    },
}

pub(super) fn start_fail_request_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: FailRequestParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let failure = navigation_fail_request_failure(params.error_reason);
    let command = build_cdp_fail_intercepted_request_command(conn, cmd, params.request_id, failure);
    start_devtools_fetch_command(
        conn,
        cmd.id,
        cmd.session_id,
        AutomationCommand::FailInterceptedRequest(command),
    )
}

fn build_cdp_fail_intercepted_request_command(
    conn: &CdpConnection,
    cmd: &Cmd<'_>,
    request_id: String,
    failure: DevToolsRequestFailure,
) -> DevToolsFailInterceptedRequestCommand {
    let (browser_context_id, target_id) =
        devtools_fetch_owner_identity_for_session(conn, cmd.session_id);
    DevToolsFailInterceptedRequestCommand {
        context: cmd.automation_context(target_id.as_deref(), browser_context_id.as_deref()),
        request_id: DevToolsRequestId::from(request_id),
        failure,
    }
}

fn start_devtools_fail_intercepted_request_command(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    owner: &CommandOwnerScope,
    command: DevToolsFailInterceptedRequestCommand,
) -> FetchCommandTaskStep {
    let command_session_id = owner.session_id();
    let validate_request_id = command.context.protocol == FrontendProtocol::Cdp;
    let request_id = command.request_id.into_string();
    let failure = command.failure;
    let error_text = failure.to_string();
    let action_session_id = action_session_id_for_devtools_context(
        command_session_id,
        command.context.protocol,
        command.context.session_id.as_ref(),
    );

    if let Some(pending) = take_pending_navigation(conn, owner, action_session_id, &request_id) {
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FailRequest {
                state: Box::new(PendingFailRequestState::Navigation {
                    pending: Box::new(pending),
                    failure,
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }

    if let Some(pending) = take_pending_subresource_fetch_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        let pending = pending;
        if let Some(continuation) = pending.detached_parser_script_fetch_continuation() {
            if !continuation.fail(error_text.clone()) {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "RequestNotFound",
                ));
            }
            let mut plan = CommandOutputPlan::success();
            let mut events = Vec::new();
            let loader_id = conn
                .current_document_loader_id_for_owner(owner)
                .unwrap_or_else(|| DEFAULT_LOADER_ID.to_owned());
            network::emit_loading_failed(
                &mut events,
                command_session_id,
                &pending.network_request_id,
                &pending.frame_id,
                &loader_id,
                monotonic_timestamp_seconds(),
                &error_text,
                pending.resource_type.into(),
            );
            plan.extend_background_events(events);
            return FetchCommandTaskStep::Complete(plan);
        }
        let page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page,
            Err(message) if message == "NoDocumentLoaded" => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "NoDocumentLoaded",
                ));
            }
            Err(message) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        let pending_page = match page
            .start_fail_pending_subresource_fetch(pending.internal_id, error_text.clone())
        {
            Ok(pending_page) => pending_page,
            Err(error) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    format!("subresource fetch fail failed: {error}"),
                ));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FailRequest {
                state: Box::new(PendingFailRequestState::SubresourceFetch {
                    pending: Box::new(pending),
                }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }

    if let Some(pending) = take_pending_subresource_response_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        let page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page,
            Err(message) if message == "NoDocumentLoaded" => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "NoDocumentLoaded",
                ));
            }
            Err(message) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        let pending_page = match page
            .start_fail_pending_subresource_response(pending.internal_id, error_text.clone())
        {
            Ok(pending_page) => pending_page,
            Err(error) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    format!("subresource response fail failed: {error}"),
                ));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FailRequest {
                state: Box::new(PendingFailRequestState::SubresourceResponse {
                    pending: Box::new(pending),
                }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }

    if let Some(transfer) =
        conn.take_fetch_response_transfer_for_cancellation_for_owner(owner, &request_id)
    {
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FailRequest {
                state: Box::new(PendingFailRequestState::ResponseTransfer {
                    transfer: Box::new(transfer),
                    failure,
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }

    pending_request_action_output_plan(conn, owner, &request_id, validate_request_id)
        .map_or_else(FetchCommandTaskStep::Complete, |()| {
            FetchCommandTaskStep::Complete(CommandOutputPlan::success())
        })
}

pub(super) async fn complete_fail_request_command_async(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
    state: PendingFailRequestState,
    out: &mut FetchCommandOutput,
) {
    match state {
        PendingFailRequestState::Navigation { pending, failure } => {
            let pending = *pending;
            emit_devtools_empty_success(out);
            let token = pending.document_navigation_token;
            let navigation_state = pending.navigation;
            let navigation = network::materialize_navigation_load_result(
                conn,
                &navigation_state,
                Err(failure.into()),
            );
            complete_tokened_materialized_navigation_as_background_events_async(
                conn,
                out,
                token,
                navigation_state,
                navigation,
            )
            .await;
        }
        PendingFailRequestState::SubresourceFetch { pending } => {
            let Some(completed) = completed else {
                out.push_error(-32000, "Missing renderer completion");
                return;
            };
            let completion = match completed {
                Ok(completion) => completion,
                Err(error) => {
                    out.push_error(-32000, error);
                    return;
                }
            };
            match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
                Ok(page) => {
                    if let Err(error) = page.finish_fail_pending_subresource_fetch(completion) {
                        out.push_error(-32000, format!("subresource fetch fail failed: {error}"));
                        return;
                    }
                }
                Err(message) if message == "NoDocumentLoaded" => {
                    out.push_error(-32000, "NoDocumentLoaded");
                    return;
                }
                Err(message) => {
                    out.push_error(-32000, message);
                    return;
                }
            }
            emit_devtools_empty_success(out);
            let mut events = Vec::new();
            activity::flush_post_subresource_fetch_request_activity_background_events_async(
                conn,
                &mut events,
                owner.session_id(),
                &pending,
            )
            .await;
            out.extend_background_events(events);
        }
        PendingFailRequestState::SubresourceResponse { pending } => {
            let Some(completed) = completed else {
                out.push_error(-32000, "Missing renderer completion");
                return;
            };
            let completion = match completed {
                Ok(completion) => completion,
                Err(error) => {
                    out.push_error(-32000, error);
                    return;
                }
            };
            match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
                Ok(page) => {
                    if let Err(error) = page.finish_fail_pending_subresource_response(completion) {
                        out.push_error(
                            -32000,
                            format!("subresource response fail failed: {error}"),
                        );
                        return;
                    }
                }
                Err(message) if message == "NoDocumentLoaded" => {
                    out.push_error(-32000, "NoDocumentLoaded");
                    return;
                }
                Err(message) => {
                    out.push_error(-32000, message);
                    return;
                }
            }
            emit_devtools_empty_success(out);
            let mut events = Vec::new();
            activity::flush_post_subresource_response_activity_background_events_async(
                conn,
                &mut events,
                owner.session_id(),
                &pending,
            )
            .await;
            out.extend_background_events(events);
        }
        PendingFailRequestState::ResponseTransfer { transfer, failure } => {
            let transfer = *transfer;
            let (token, navigation_state, navigation) = transfer.fail(failure.into());
            let navigation =
                network::materialize_navigation_load_result(conn, &navigation_state, navigation);
            emit_devtools_empty_success(out);
            complete_tokened_materialized_navigation_as_background_events_async(
                conn,
                out,
                token,
                navigation_state,
                navigation,
            )
            .await;
        }
    }
}

pub(super) enum PendingFulfillRequestState {
    SubresourceFetch {
        pending: Box<crate::conn::PendingSubresourceFetchRequest>,
        request_id: String,
        network_request_id: String,
        websocket_socket_id: Option<u64>,
        register_synthetic_websocket: bool,
    },
    SubresourceResponse {
        pending: Box<crate::conn::PendingSubresourceFetchResponseRequest>,
    },
    Navigation {
        pending: Box<crate::conn::PendingFetchNavigation>,
        response_code: u16,
        response_headers: Vec<(String, Vec<u8>)>,
        decoded_body: Option<RendererSyntheticResponseBody>,
    },
    ResponseTransfer {
        transfer: Box<crate::conn::PausedDocumentTransfer>,
        response_code: u16,
        response_headers: Vec<(String, Vec<u8>)>,
        decoded_body: Option<RendererSyntheticResponseBody>,
    },
}

pub(super) enum PendingWebSocketCommandOperation {
    DispatchText,
    DispatchBinary,
    Close,
}

pub(super) fn start_fulfill_request_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: FulfillRequestParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let body = match params.body {
        Some(body) => match decode_base64_bytes(&body) {
            Ok(bytes) => Some(bytes),
            Err(()) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32602,
                    "InvalidParams",
                ));
            }
        },
        None => None,
    };
    let response_headers = match response_headers_from_params(
        params.response_headers,
        params.binary_response_headers.as_deref(),
    ) {
        Ok(headers) => headers,
        Err(()) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let response_code = params.response_code.unwrap_or(200);
    let command = build_cdp_fulfill_intercepted_request_command(
        conn,
        cmd,
        params.request_id,
        response_code,
        response_headers,
        body,
        params.response_phrase,
    );
    start_devtools_fetch_command(
        conn,
        cmd.id,
        cmd.session_id,
        AutomationCommand::FulfillInterceptedRequest(command),
    )
}

fn build_cdp_fulfill_intercepted_request_command(
    conn: &CdpConnection,
    cmd: &Cmd<'_>,
    request_id: String,
    response_code: u16,
    response_headers: Vec<(String, Vec<u8>)>,
    body: Option<Vec<u8>>,
    response_phrase: Option<String>,
) -> DevToolsFulfillInterceptedRequestCommand {
    let (browser_context_id, target_id) =
        devtools_fetch_owner_identity_for_session(conn, cmd.session_id);
    DevToolsFulfillInterceptedRequestCommand {
        context: cmd.automation_context(target_id.as_deref(), browser_context_id.as_deref()),
        request_id: DevToolsRequestId::from(request_id),
        response_code,
        response_headers,
        body,
        response_phrase,
    }
}

fn start_devtools_fulfill_intercepted_request_command(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    owner: &CommandOwnerScope,
    command: DevToolsFulfillInterceptedRequestCommand,
) -> FetchCommandTaskStep {
    let command_session_id = owner.session_id();
    let validate_request_id = command.context.protocol == FrontendProtocol::Cdp;
    let request_id = command.request_id.into_string();
    let response_code = command.response_code;
    let response_headers = command.response_headers;
    let decoded_body = command.body.map(RendererSyntheticResponseBody::from_bytes);
    let _ = command.response_phrase;

    let action_session_id = action_session_id_for_devtools_context(
        command_session_id,
        command.context.protocol,
        command.context.session_id.as_ref(),
    );
    if let Some(pending) = take_pending_subresource_fetch_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        let pending = pending;
        if let Some(continuation) = pending.detached_parser_script_fetch_continuation() {
            let body = decoded_body
                .unwrap_or_else(RendererSyntheticResponseBody::empty)
                .into_body_bytes();
            if !continuation.fulfill(response_code, response_headers, body) {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "RequestNotFound",
                ));
            }
            return FetchCommandTaskStep::Complete(CommandOutputPlan::success());
        }
        let network_request_id = pending.network_request_id.clone();
        let websocket_socket_id = pending.websocket_socket_id;
        let register_synthetic_websocket =
            pending.resource_type == SubresourceResourceType::WebSocket && response_code == 101;
        let page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page,
            Err(message) if message == "NoDocumentLoaded" => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "NoDocumentLoaded",
                ));
            }
            Err(message) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        let pending_page = match page.start_fulfill_pending_subresource_fetch(
            pending.internal_id,
            response_code,
            response_headers.clone(),
            decoded_body.unwrap_or_else(RendererSyntheticResponseBody::empty),
        ) {
            Ok(pending_page) => pending_page,
            Err(error) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    format!("subresource fetch fulfill failed: {error}"),
                ));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FulfillRequest {
                state: Box::new(PendingFulfillRequestState::SubresourceFetch {
                    pending: Box::new(pending),
                    request_id,
                    network_request_id,
                    websocket_socket_id,
                    register_synthetic_websocket,
                }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }

    if let Some(pending) = take_pending_subresource_response_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        let page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page,
            Err(message) if message == "NoDocumentLoaded" => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "NoDocumentLoaded",
                ));
            }
            Err(message) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        let pending_page = match page.start_fulfill_pending_subresource_response(
            pending.internal_id,
            response_code,
            response_headers.clone(),
            decoded_body.unwrap_or_else(RendererSyntheticResponseBody::empty),
        ) {
            Ok(pending_page) => pending_page,
            Err(error) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    format!("subresource response fulfill failed: {error}"),
                ));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FulfillRequest {
                state: Box::new(PendingFulfillRequestState::SubresourceResponse {
                    pending: Box::new(pending),
                }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }

    if let Some(pending) = take_pending_navigation(conn, owner, action_session_id, &request_id) {
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FulfillRequest {
                state: Box::new(PendingFulfillRequestState::Navigation {
                    pending: Box::new(pending),
                    response_code,
                    response_headers: response_headers.clone(),
                    decoded_body,
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }

    if let Some(transfer) =
        conn.take_available_fetch_response_transfer_for_owner(owner, &request_id)
    {
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::FulfillRequest {
                state: Box::new(PendingFulfillRequestState::ResponseTransfer {
                    transfer: Box::new(transfer),
                    response_code,
                    response_headers,
                    decoded_body,
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }

    pending_request_action_output_plan(conn, owner, &request_id, validate_request_id)
        .map_or_else(FetchCommandTaskStep::Complete, |()| {
            FetchCommandTaskStep::Complete(CommandOutputPlan::success())
        })
}

pub(super) async fn complete_fulfill_request_command_async(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
    state: PendingFulfillRequestState,
    out: &mut FetchCommandOutput,
) {
    match state {
        PendingFulfillRequestState::SubresourceFetch {
            pending,
            request_id,
            network_request_id,
            websocket_socket_id,
            register_synthetic_websocket,
        } => {
            let Some(completed) = completed else {
                out.push_error(-32000, "Missing renderer completion");
                return;
            };
            let completion = match completed {
                Ok(completion) => completion,
                Err(error) => {
                    out.push_error(-32000, error);
                    return;
                }
            };
            match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
                Ok(page) => {
                    if let Err(error) = page.finish_fulfill_pending_subresource_fetch(completion) {
                        out.push_error(
                            -32000,
                            format!("subresource fetch fulfill failed: {error}"),
                        );
                        return;
                    }
                }
                Err(message) if message == "NoDocumentLoaded" => {
                    out.push_error(-32000, "NoDocumentLoaded");
                    return;
                }
                Err(message) => {
                    out.push_error(-32000, message);
                    return;
                }
            }
            if register_synthetic_websocket && let Some(socket_id) = websocket_socket_id {
                conn.register_synthetic_websocket_request_for_owner(
                    owner,
                    request_id,
                    network_request_id,
                    socket_id,
                );
            }
            emit_devtools_empty_success(out);
            let mut events = Vec::new();
            activity::flush_post_subresource_fetch_request_activity_background_events_async(
                conn,
                &mut events,
                owner.session_id(),
                &pending,
            )
            .await;
            out.extend_background_events(events);
        }
        PendingFulfillRequestState::SubresourceResponse { pending } => {
            let Some(completed) = completed else {
                out.push_error(-32000, "Missing renderer completion");
                return;
            };
            let completion = match completed {
                Ok(completion) => completion,
                Err(error) => {
                    out.push_error(-32000, error);
                    return;
                }
            };
            match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
                Ok(page) => {
                    if let Err(error) = page.finish_fulfill_pending_subresource_response(completion)
                    {
                        out.push_error(
                            -32000,
                            format!("subresource response fulfill failed: {error}"),
                        );
                        return;
                    }
                }
                Err(message) if message == "NoDocumentLoaded" => {
                    out.push_error(-32000, "NoDocumentLoaded");
                    return;
                }
                Err(message) => {
                    out.push_error(-32000, message);
                    return;
                }
            }
            emit_devtools_empty_success(out);
            let mut events = Vec::new();
            activity::flush_post_subresource_response_activity_background_events_async(
                conn,
                &mut events,
                owner.session_id(),
                &pending,
            )
            .await;
            out.extend_background_events(events);
        }
        PendingFulfillRequestState::Navigation {
            pending,
            response_code,
            response_headers,
            decoded_body,
        } => {
            let pending = *pending;
            emit_devtools_empty_success(out);
            let token = pending.document_navigation_token;
            let body = CapturedBody::from_optional_renderer_synthetic_response_body(decoded_body);
            let navigation_state = pending.navigation;
            let navigation = conn
                .build_navigation_from_buffered_body_source_for_navigation_async(
                    token.as_ref(),
                    &navigation_state,
                    navigation_state.requested_url.clone(),
                    response_code,
                    response_headers,
                    body,
                    pending.request_cookie_report,
                    Default::default(),
                    network::MainDocumentBodyProgressSource::default(),
                )
                .await;
            let navigation =
                network::materialize_navigation_load_result(conn, &navigation_state, navigation);
            complete_tokened_materialized_navigation_as_background_events_async(
                conn,
                out,
                token,
                navigation_state,
                navigation,
            )
            .await;
        }
        PendingFulfillRequestState::ResponseTransfer {
            transfer,
            response_code,
            response_headers,
            decoded_body,
        } => {
            let transfer = *transfer;
            emit_devtools_empty_success(out);
            let (token, navigation_state, navigation) = transfer
                .fulfill_synthetic_async(
                    conn,
                    response_code,
                    response_headers,
                    CapturedBody::from_optional_renderer_synthetic_response_body(decoded_body),
                )
                .await;
            let navigation =
                network::materialize_navigation_load_result(conn, &navigation_state, navigation);
            complete_tokened_materialized_navigation_as_background_events_async(
                conn,
                out,
                token,
                navigation_state,
                navigation,
            )
            .await;
        }
    }
}

fn pending_request_action_output_plan(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    request_id: &str,
    validate_request_id: bool,
) -> Result<(), CommandOutputPlan> {
    pending_request_action_result_with_id_validation(conn, owner, request_id, validate_request_id)
}

pub(super) fn start_dispatch_websocket_message_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: DispatchWebSocketMessageParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let Some(socket_id) =
        conn.synthetic_websocket_socket_id_for_session_owner(cmd.session_id, &params.request_id)
    else {
        return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, "RequestNotFound"));
    };

    let page = match conn.loaded_page_mut_for_protocol_access(cmd.session_id) {
        Ok(page) => page,
        Err(message) if message == "NoDocumentLoaded" => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32000,
                "NoDocumentLoaded",
            ));
        }
        Err(message) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
        }
    };
    let (operation, pending_page) = match WebSocketMessageOpcode::parse(&params.opcode) {
        Some(WebSocketMessageOpcode::Text) => (
            PendingWebSocketCommandOperation::DispatchText,
            page.start_receive_synthetic_websocket_text(socket_id, params.data),
        ),
        Some(WebSocketMessageOpcode::Binary) => {
            let bytes = match decode_base64_bytes(&params.data) {
                Ok(bytes) => bytes,
                Err(()) => {
                    return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                        -32602,
                        "InvalidParams",
                    ));
                }
            };
            (
                PendingWebSocketCommandOperation::DispatchBinary,
                page.start_receive_synthetic_websocket_binary(socket_id, bytes),
            )
        }
        None => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let pending = match pending_page {
        Ok(pending) => pending,
        Err(error) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32000,
                error.to_string(),
            ));
        }
    };
    FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new(
        conn,
        cmd.id,
        cmd.session_id,
        PendingFetchCommandKind::DispatchWebSocketMessage { operation },
        PendingFetchCommandOperation::Page(pending),
    ))
}

pub(super) fn start_close_websocket_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: CloseWebSocketParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let Some(socket_id) =
        conn.synthetic_websocket_socket_id_for_session_owner(cmd.session_id, &params.request_id)
    else {
        return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, "RequestNotFound"));
    };

    let page = match conn.loaded_page_mut_for_protocol_access(cmd.session_id) {
        Ok(page) => page,
        Err(message) if message == "NoDocumentLoaded" => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32000,
                "NoDocumentLoaded",
            ));
        }
        Err(message) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
        }
    };
    let pending = match page.start_close_synthetic_websocket_from_server(
        socket_id,
        params.code,
        params.reason,
    ) {
        Ok(pending) => pending,
        Err(error) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32000,
                error.to_string(),
            ));
        }
    };
    FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new(
        conn,
        cmd.id,
        cmd.session_id,
        PendingFetchCommandKind::CloseWebSocket,
        PendingFetchCommandOperation::Page(pending),
    ))
}

pub(super) fn complete_websocket_page_command(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
    operation: PendingWebSocketCommandOperation,
    out: &mut FetchCommandOutput,
) {
    let Some(completed) = completed else {
        out.push_error(-32000, "Missing renderer completion");
        return;
    };
    let completion = match completed {
        Ok(completion) => completion,
        Err(error) => {
            out.push_error(-32000, error);
            return;
        }
    };
    let result = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
        Ok(page) => match operation {
            PendingWebSocketCommandOperation::DispatchText => {
                page.finish_receive_synthetic_websocket_text(completion)
            }
            PendingWebSocketCommandOperation::DispatchBinary => {
                page.finish_receive_synthetic_websocket_binary(completion)
            }
            PendingWebSocketCommandOperation::Close => {
                page.finish_close_synthetic_websocket_from_server(completion)
            }
        },
        Err(message) if message == "NoDocumentLoaded" => {
            out.push_error(-32000, "NoDocumentLoaded");
            return;
        }
        Err(message) => {
            out.push_error(-32000, message);
            return;
        }
    };
    match result {
        Ok(()) => out.push_success(),
        Err(error) => out.push_error(-32000, error.to_string()),
    }
}

pub(super) enum PendingContinueResponseState {
    ResponseTransfer {
        request_id: String,
        transfer: Box<crate::conn::PausedDocumentTransfer>,
        response_code: Option<u16>,
        response_headers: Vec<(String, Vec<u8>)>,
    },
    SubresourceResponse {
        pending: Box<crate::conn::PendingSubresourceFetchResponseRequest>,
    },
}

pub(super) fn start_continue_response_command(
    conn: &mut CdpConnection,
    cmd: &Cmd<'_>,
) -> FetchCommandTaskStep {
    let params: ContinueResponseParams = match cmd.get_params() {
        Ok(Some(params)) => params,
        _ => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let response_code = match optional_response_code(params.response_code) {
        Ok(code) => code,
        Err(()) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let response_headers = match response_headers_with_presence_from_params(
        params.response_headers,
        params
            .binary_response_headers
            .as_ref()
            .map(|headers| headers.as_ref()),
    ) {
        Ok(headers) => headers,
        Err(()) => {
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                "InvalidParams",
            ));
        }
    };
    let command = build_cdp_continue_intercepted_response_command(
        conn,
        cmd,
        params.request_id.as_ref(),
        response_code,
        response_headers,
        params.response_phrase,
    );
    start_devtools_fetch_command(
        conn,
        cmd.id,
        cmd.session_id,
        AutomationCommand::ContinueInterceptedResponse(command),
    )
}

fn build_cdp_continue_intercepted_response_command(
    conn: &CdpConnection,
    cmd: &Cmd<'_>,
    request_id: &str,
    response_code: Option<u16>,
    response_headers: Option<Vec<(String, Vec<u8>)>>,
    response_phrase: Option<String>,
) -> DevToolsContinueInterceptedResponseCommand {
    let (browser_context_id, target_id) =
        devtools_fetch_owner_identity_for_session(conn, cmd.session_id);
    DevToolsContinueInterceptedResponseCommand {
        context: cmd.automation_context(target_id.as_deref(), browser_context_id.as_deref()),
        request_id: DevToolsRequestId::from(request_id),
        response_code,
        response_headers,
        response_phrase,
        auth_credentials: None,
    }
}

pub(super) fn devtools_fetch_owner_identity_for_session(
    conn: &CdpConnection,
    session_id: Option<&str>,
) -> (Option<String>, Option<String>) {
    conn.target_owner_identity_for_session(session_id)
        .map(|(browser_context_id, target_id)| (Some(browser_context_id), target_id))
        .unwrap_or((None, None))
}

fn start_devtools_continue_intercepted_response_command(
    conn: &mut CdpConnection,
    command_id: Option<u64>,
    owner: &CommandOwnerScope,
    command: &DevToolsContinueInterceptedResponseCommand,
) -> FetchCommandTaskStep {
    let command_session_id = owner.session_id();
    let request_id = command.request_id.as_str().to_owned();
    let response_code = command.response_code;
    let response_headers = command.response_headers.clone();
    let action_session_id = action_session_id_for_devtools_context(
        command_session_id,
        command.context.protocol,
        command.context.session_id.as_ref(),
    );
    if let Some(transfer) =
        conn.take_available_fetch_response_transfer_for_owner(owner, &request_id)
    {
        let _ = &command.response_phrase;
        let transfer_response_headers = response_headers.clone().unwrap_or_default();
        if let Some(sender) = conn.background_navigation_completion_sender_for_owner(owner) {
            match transfer.into_pending_streaming_document_response_navigation() {
                Ok(pending) => {
                    // Chromium ACKs Fetch.continueResponse when the response-stage
                    // pause is released; parser/DCL/body completion continues
                    // asynchronously. Keep streaming main-document bodies on the
                    // same background completion path as Page.navigate so the
                    // command result never waits for body EOF.
                    continue_streaming_document_response_in_background(
                        conn,
                        sender,
                        pending,
                        response_code,
                        transfer_response_headers,
                    );
                    return FetchCommandTaskStep::Complete(CommandOutputPlan::success());
                }
                Err(transfer) => {
                    return FetchCommandTaskStep::Pending(
                        PendingFetchCommandDispatch::new_for_owner(
                            command_id,
                            owner.clone(),
                            PendingFetchCommandKind::ContinueResponse {
                                state: Box::new(PendingContinueResponseState::ResponseTransfer {
                                    request_id,
                                    transfer,
                                    response_code,
                                    response_headers: transfer_response_headers,
                                }),
                            },
                            PendingFetchCommandOperation::Ready,
                        ),
                    );
                }
            }
        }
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::ContinueResponse {
                state: Box::new(PendingContinueResponseState::ResponseTransfer {
                    request_id,
                    transfer: Box::new(transfer),
                    response_code,
                    response_headers: transfer_response_headers,
                }),
            },
            PendingFetchCommandOperation::Ready,
        ));
    }
    if let Some(pending) = take_pending_subresource_response_request_for_action_session(
        conn,
        owner,
        action_session_id,
        &request_id,
    ) {
        if pending.response_body_taken_as_stream {
            conn.register_pending_subresource_fetch_response_request_for_owner(
                owner, request_id, pending,
            );
            return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                -32602,
                CONTINUE_RESPONSE_AFTER_BODY_TAKEN_ERROR,
            ));
        }
        let has_remaining_response_stage_chain = pending
            .response_stage_pause_state()
            .is_some_and(|chain| !chain.remaining_sessions.is_empty());
        if has_remaining_response_stage_chain && command.auth_credentials.is_none() {
            if response_code.is_none()
                && response_headers.is_none()
                && command.response_phrase.is_none()
            {
                let Some(event) = super::subresource::next_chained_subresource_response_pause_event(
                    conn, pending,
                ) else {
                    return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                        -32000,
                        "RequestNotFound",
                    ));
                };
                let mut plan = CommandOutputPlan::default();
                plan.push_success();
                plan.push_background_event(event);
                return FetchCommandTaskStep::Complete(plan);
            }
            if let (Some(override_response_code), Some(response_headers)) =
                (response_code, response_headers.clone())
            {
                let mut pending = pending;
                pending.apply_response_head_override(override_response_code, response_headers);
                let Some(event) = super::subresource::next_chained_subresource_response_pause_event(
                    conn, pending,
                ) else {
                    return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                        -32000,
                        "RequestNotFound",
                    ));
                };
                let mut plan = CommandOutputPlan::default();
                plan.push_success();
                plan.push_background_event(event);
                return FetchCommandTaskStep::Complete(plan);
            }
            if response_code.is_some()
                || response_headers.is_some()
                || command.response_phrase.is_some()
            {
                conn.register_pending_subresource_fetch_response_request_for_owner(
                    owner, request_id, pending,
                );
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32602,
                    CONTINUE_RESPONSE_PARTIAL_OVERRIDE_ERROR,
                ));
            }
        }
        let accumulated_response_head_override = pending.response_head_overridden
            && response_code.is_none()
            && response_headers.is_none()
            && command.auth_credentials.is_none();
        let continue_response_code = response_code
            .or_else(|| accumulated_response_head_override.then_some(pending.response_status));
        let continue_response_headers = if accumulated_response_head_override {
            Some(pending.response_headers.clone())
        } else {
            response_headers
        };
        let pending_internal_id = pending.internal_id;
        let page = match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
            Ok(page) => page,
            Err(message) if message == "NoDocumentLoaded" => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    "NoDocumentLoaded",
                ));
            }
            Err(message) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(-32000, message));
            }
        };
        let pending_page = match page.start_continue_pending_subresource_response(
            pending_internal_id,
            continue_response_code,
            continue_response_headers,
        ) {
            Ok(pending_page) => pending_page,
            Err(error) => {
                return FetchCommandTaskStep::Complete(CommandOutputPlan::error(
                    -32000,
                    format!("subresource response continue failed: {error}"),
                ));
            }
        };
        return FetchCommandTaskStep::Pending(PendingFetchCommandDispatch::new_for_owner(
            command_id,
            owner.clone(),
            PendingFetchCommandKind::ContinueResponse {
                state: Box::new(PendingContinueResponseState::SubresourceResponse {
                    pending: Box::new(pending),
                }),
            },
            PendingFetchCommandOperation::Page(pending_page),
        ));
    }
    if let Some(step) = super::auth::start_devtools_continue_with_auth_command_for_pending(
        conn,
        command_id,
        owner,
        &continue_response_auth_command(command),
    ) {
        return step;
    }
    pending_request_action_output_plan(
        conn,
        owner,
        &request_id,
        command.context.protocol == FrontendProtocol::Cdp,
    )
    .map_or_else(FetchCommandTaskStep::Complete, |()| {
        FetchCommandTaskStep::Complete(CommandOutputPlan::success())
    })
}

fn continue_response_auth_command(
    command: &DevToolsContinueInterceptedResponseCommand,
) -> DevToolsContinueWithAuthCommand {
    let (action, username, password) = command.auth_credentials.as_ref().map_or(
        (DevToolsAuthChallengeAction::Default, None, None),
        |credentials| {
            (
                DevToolsAuthChallengeAction::ProvideCredentials,
                Some(credentials.username.clone()),
                Some(credentials.password.clone()),
            )
        },
    );
    DevToolsContinueWithAuthCommand {
        context: command.context.clone(),
        request_id: command.request_id.clone(),
        action,
        username,
        password,
    }
}

async fn continue_response_transfer_inline(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    out: &mut FetchCommandOutput,
    request_id: String,
    transfer: crate::conn::PausedDocumentTransfer,
    response_code: Option<u16>,
    response_headers: Vec<(String, Vec<u8>)>,
) {
    match transfer
        .continue_response_async(conn, response_code, response_headers)
        .await
    {
        Ok((document_navigation_token, navigation_state, navigation)) => {
            let navigation =
                network::materialize_navigation_load_result(conn, &navigation_state, navigation);
            emit_devtools_empty_success(out);
            complete_tokened_materialized_navigation_as_background_events_async(
                conn,
                out,
                document_navigation_token,
                navigation_state,
                navigation,
            )
            .await;
        }
        Err(transfer) => {
            conn.register_pending_fetch_response_transfer_for_owner(owner, request_id, transfer);
            out.push_error(-32000, "ResponseBodyStreamActive");
        }
    }
}

pub(super) async fn complete_continue_response_command_async(
    conn: &mut CdpConnection,
    owner: &CommandOwnerScope,
    completed: Option<Result<CompletedPageCommand, String>>,
    state: PendingContinueResponseState,
    out: &mut FetchCommandOutput,
) {
    match state {
        PendingContinueResponseState::ResponseTransfer {
            request_id,
            transfer,
            response_code,
            response_headers,
        } => {
            continue_response_transfer_inline(
                conn,
                owner,
                out,
                request_id,
                *transfer,
                response_code,
                response_headers,
            )
            .await;
        }
        PendingContinueResponseState::SubresourceResponse { pending } => {
            let pending = *pending;
            let Some(completed) = completed else {
                out.push_error(-32000, "Missing renderer completion");
                return;
            };
            let completion = match completed {
                Ok(completion) => completion,
                Err(error) => {
                    out.push_error(-32000, error);
                    return;
                }
            };
            match conn.loaded_page_mut_for_protocol_access_for_owner(owner) {
                Ok(page) => {
                    if let Err(error) =
                        page.finish_continue_pending_subresource_response(completion)
                    {
                        out.push_error(
                            -32000,
                            format!("subresource response continue failed: {error}"),
                        );
                        return;
                    }
                }
                Err(message) if message == "NoDocumentLoaded" => {
                    out.push_error(-32000, "NoDocumentLoaded");
                    return;
                }
                Err(message) => {
                    out.push_error(-32000, message);
                    return;
                }
            }
            emit_devtools_empty_success(out);
            let mut events = Vec::new();
            activity::flush_post_subresource_response_activity_background_events_async(
                conn,
                &mut events,
                owner.session_id(),
                &pending,
            )
            .await;
            out.extend_background_events(events);
        }
    }
}

fn continue_streaming_document_response_in_background(
    conn: &mut CdpConnection,
    sender: tokio::sync::mpsc::UnboundedSender<page::BackgroundNavigationCompletion>,
    pending: PendingStreamingDocumentResponseNavigation,
    response_code: Option<u16>,
    response_headers: Vec<(String, Vec<u8>)>,
) {
    let PendingStreamingDocumentResponseNavigation {
        document_navigation_token,
        navigation,
        response,
        network_observation_journal,
        body_progress_source,
    } = pending;
    let job = match conn.background_streaming_response_navigation_load_job_for_navigation(
        &document_navigation_token,
        &navigation,
        response,
        network_observation_journal,
        response_code,
        response_headers,
        body_progress_source,
    ) {
        Ok(job) => job,
        Err(BackgroundNavigationClaimError::AlreadyClaimed) => return,
        Err(BackgroundNavigationClaimError::Stale) => {
            let _ = sender.send(page::BackgroundNavigationCompletion::new(
                document_navigation_token,
                navigation,
                Err(moli_fetch::FetchCancelled.into()),
            ));
            return;
        }
    };
    tokio::task::spawn_local(async move {
        let body_completion_sink = BackgroundNavigationBodyCompletionSink::new(
            sender.clone(),
            document_navigation_token.clone(),
            navigation.clone(),
        );
        let navigation_result = job.run(Some(body_completion_sink)).await;
        let _ = sender.send(page::BackgroundNavigationCompletion::new(
            document_navigation_token,
            navigation,
            navigation_result,
        ));
    });
}

#[cfg(test)]
mod protocol_neutral_tests {
    use crate::automation::DevToolsRequestFailure;

    #[test]
    fn cdp_abort_reason_is_preserved_as_a_typed_cause() {
        use super::{ErrorReason, navigation_fail_request_failure};
        let failure = navigation_fail_request_failure(Some(ErrorReason::Aborted));
        let error = anyhow::Error::new(failure).context("interception continuation");
        assert!(matches!(
            error.downcast_ref::<DevToolsRequestFailure>(),
            Some(DevToolsRequestFailure::Aborted)
        ));
        assert_eq!(
            navigation_fail_request_failure(Some(ErrorReason::ConnectionAborted)),
            DevToolsRequestFailure::Failed("ConnectionAborted".to_owned()),
        );
    }

    use crate::automation::{AutomationCommand, AutomationEvent, FrontendProtocol};
    use moli_core::page::SubresourceResourceType;
    use serde_json::{Value, json};
    use url::Url;

    use crate::conn::{
        BrowserContext, CapturedBody, CdpConnection, Cmd, PendingSubresourceFetchOwnerKind,
        PendingSubresourceFetchRequest, PendingSubresourceFetchRequestStage,
        PendingSubresourceFetchRequestStageChain, PendingSubresourceFetchResponseRequest,
        PendingSubresourceFetchResponseStage, PendingSubresourceFetchResponseStageChain,
    };

    use super::{
        ContinueResponseParams, build_cdp_continue_intercepted_request_command,
        build_cdp_continue_intercepted_response_command,
        build_cdp_fail_intercepted_request_command, build_cdp_fulfill_intercepted_request_command,
        parsed_continue_request_url, start_devtools_fetch_command,
    };

    #[test]
    fn cdp_continue_request_builds_protocol_neutral_intercepted_request_command() {
        let conn = CdpConnection::new();
        let params = json!({
            "requestId": "interception-job-0",
            "url": "https://example.test/next",
            "method": "POST",
            "postData": "aGVsbG8=",
            "headers": [{"name": "x-test", "value": "1"}],
            "interceptResponse": true,
        });
        let cmd = Cmd::for_test(
            Some(20),
            "Fetch.continueRequest",
            &params,
            Some("SID-0"),
            r#"{"id":20,"method":"Fetch.continueRequest"}"#,
        );
        let params = cmd
            .get_params()
            .expect("continueRequest params should parse")
            .expect("continueRequest params should be present");

        let command = build_cdp_continue_intercepted_request_command(&conn, &cmd, params)
            .expect("continueRequest command should normalize");
        let parsed_url =
            parsed_continue_request_url(&command).expect("continueRequest URL should parse");

        assert_eq!(command.context.protocol, FrontendProtocol::Cdp);
        assert_eq!(
            command.context.session_id.as_ref().map(|id| id.as_str()),
            Some("SID-0")
        );
        assert_eq!(command.context.target_id, None);
        assert_eq!(command.context.browser_context_id, None);
        assert_eq!(command.request_id.as_str(), "interception-job-0");
        assert_eq!(command.url.as_deref(), Some("https://example.test/next"));
        assert_eq!(command.method.as_deref(), Some("POST"));
        assert_eq!(command.post_data.as_deref(), Some(b"hello".as_slice()));
        assert_eq!(
            command.headers.map(|headers| headers.to_byte_strings()),
            Some(vec![("x-test".to_owned(), "1".to_owned())])
        );
        assert!(command.intercept_response);
        assert_eq!(
            parsed_url.as_ref().map(|url| url.as_str()),
            Some("https://example.test/next")
        );
    }

    #[test]
    fn cdp_continue_response_builds_protocol_neutral_intercepted_response_command() {
        let conn = CdpConnection::new();
        let params = Value::Null;
        let cmd = Cmd::for_test(
            Some(21),
            "Fetch.continueResponse",
            &params,
            Some("SID-1"),
            r#"{"id":21,"method":"Fetch.continueResponse"}"#,
        );

        let command = build_cdp_continue_intercepted_response_command(
            &conn,
            &cmd,
            "interception-job-1",
            Some(204),
            Some(vec![("x-test".to_owned(), b"1".to_vec())]),
            Some("No Content".to_owned()),
        );

        assert_eq!(command.context.protocol, FrontendProtocol::Cdp);
        assert_eq!(
            command.context.session_id.as_ref().map(|id| id.as_str()),
            Some("SID-1")
        );
        assert_eq!(command.context.target_id, None);
        assert_eq!(command.context.browser_context_id, None);
        assert_eq!(command.request_id.as_str(), "interception-job-1");
        assert_eq!(command.response_code, Some(204));
        assert_eq!(
            command.response_headers,
            Some(vec![("x-test".to_owned(), b"1".to_vec())])
        );
        assert_eq!(command.response_phrase.as_deref(), Some("No Content"));
    }

    #[test]
    fn devtools_fetch_entry_routes_continue_request_command() {
        let mut conn = CdpConnection::new();
        let params = json!({"requestId": "missing-request"});
        let cmd = Cmd::for_test(
            Some(24),
            "Fetch.continueRequest",
            &params,
            Some("SID-4"),
            r#"{"id":24,"method":"Fetch.continueRequest"}"#,
        );
        let params = cmd
            .get_params()
            .expect("continueRequest params should parse")
            .expect("continueRequest params should be present");
        let command = build_cdp_continue_intercepted_request_command(&conn, &cmd, params)
            .expect("continueRequest command should normalize");

        let step = start_devtools_fetch_command(
            &mut conn,
            cmd.id,
            cmd.session_id,
            AutomationCommand::ContinueInterceptedRequest(command),
        );

        let super::super::FetchCommandTaskStep::Complete(plan) = step else {
            panic!("missing continueRequest should complete with a terminal protocol error");
        };
        let mut out = Vec::new();
        plan.emit_into(&mut out, cmd.id, cmd.session_id);
        assert_eq!(out[0]["id"], json!(24));
        assert!(out[0]["error"].is_object());
    }

    #[test]
    fn cdp_continue_request_repauses_next_matching_fetch_session() {
        let mut conn = CdpConnection::new();
        let mut browser_context = BrowserContext::new("BID-chain".to_owned());
        browser_context.set_active_target_id("TID-chain".to_owned());
        browser_context.attach_active_session("SID-primary".to_owned());
        assert!(
            browser_context
                .assign_attached_session_to_target("TID-chain", "SID-attached".to_owned())
        );
        browser_context
            .active_page_target_mut()
            .runtime_slot
            .set_page_attachment_id_for_test(1);
        conn.install_browser_context_fixture_for_test(browser_context);

        let page_owner = conn
            .target_page_residence_identity_for_session(Some("SID-primary"))
            .expect("test target should expose a Page residence identity");
        let pending = PendingSubresourceFetchRequest {
            residence: crate::conn::PendingSubresourceFetchResidence::InstalledPage(page_owner),
            owner_session_id: Some("SID-primary".to_owned()),
            action_session_id: Some("SID-primary".to_owned()),
            owner_kind: PendingSubresourceFetchOwnerKind::Fetch,
            internal_id: 77,
            network_request_id: "NETWORK-77".to_owned(),
            network_request_handle: None,
            frame_id: "FRAME-77".to_owned(),
            document_url: Url::parse("https://example.test/page").unwrap(),
            resource_type: SubresourceResourceType::Fetch,
            websocket_socket_id: None,
            request_stage_chain: Some(Box::new(PendingSubresourceFetchRequestStageChain {
                header_override: None,
                url: Url::parse("https://example.test/api").unwrap(),
                method: "GET".to_owned(),
                headers: vec![("x-old".to_owned(), "1".to_owned())].into(),
                body: None,
                body_override: None,
                request_cookie_report: None,
                remaining_sessions: vec![PendingSubresourceFetchRequestStage {
                    session_id: Some("SID-attached".to_owned()),
                    owner_kind: PendingSubresourceFetchOwnerKind::Fetch,
                    request_id: "FETCH-aux".to_owned(),
                    blocked_intercepts: Vec::new(),
                }],
            })),
        };
        assert!(
            conn.register_pending_subresource_fetch_request_for_session_owner(
                Some("SID-primary"),
                "FETCH-primary".to_owned(),
                pending,
            )
        );

        let params = json!({
            "requestId": "FETCH-primary",
            "url": "https://example.test/rewritten",
            "method": "POST",
            "postData": "Ym9keQ==",
            "headers": [{"name": "x-new", "value": "2"}],
        });
        let cmd = Cmd::for_test(
            Some(31),
            "Fetch.continueRequest",
            &params,
            Some("SID-primary"),
            r#"{"id":31,"method":"Fetch.continueRequest"}"#,
        );
        let params = cmd
            .get_params()
            .expect("continueRequest params should parse")
            .expect("continueRequest params should be present");
        let command = build_cdp_continue_intercepted_request_command(&conn, &cmd, params)
            .expect("continueRequest command should normalize");

        let upload_storage = command.post_data.as_ref().unwrap().as_ptr();

        let step = start_devtools_fetch_command(
            &mut conn,
            cmd.id,
            cmd.session_id,
            AutomationCommand::ContinueInterceptedRequest(command),
        );

        let super::super::FetchCommandTaskStep::Complete(plan) = step else {
            panic!("chained continueRequest should complete without a page command");
        };
        let events = plan.into_background_events(cmd.id, cmd.session_id);
        assert_eq!(events.len(), 2);
        let (paused_message, paused_sidecar) = events[1].clone().into_parts();
        let Some(AutomationEvent::RequestPaused(paused_sidecar)) = paused_sidecar else {
            panic!("chained request pause should carry a typed automation sidecar");
        };
        assert_eq!(paused_sidecar.request_id.as_str(), "FETCH-aux");
        assert_eq!(
            paused_sidecar.blocked_intercepts,
            Vec::<crate::automation::DevToolsNetworkInterceptId>::new()
        );
        assert_eq!(
            paused_sidecar.network_id.as_ref().map(|id| id.as_str()),
            Some("NETWORK-77")
        );
        let out = events
            .into_iter()
            .map(crate::conn::BackgroundProtocolEvent::into_protocol_message)
            .collect::<Vec<_>>();
        assert_eq!(out.len(), 2);
        assert_eq!(out[0]["id"], json!(31));
        assert_eq!(out[0]["sessionId"], json!("SID-primary"));
        assert_eq!(out[1]["method"], json!("Fetch.requestPaused"));
        assert_eq!(paused_message, out[1]);
        assert_eq!(out[1]["sessionId"], json!("SID-attached"));
        assert_eq!(out[1]["params"]["requestId"], json!("FETCH-aux"));
        assert_eq!(out[1]["params"]["networkId"], json!("NETWORK-77"));
        assert_eq!(
            out[1]["params"]["request"]["url"],
            json!("https://example.test/rewritten")
        );
        assert_eq!(out[1]["params"]["request"]["method"], json!("POST"));
        assert_eq!(out[1]["params"]["request"]["postData"], json!("body"));
        assert_eq!(out[1]["params"]["request"]["headers"]["x-new"], json!("2"));

        assert!(
            conn.take_pending_subresource_fetch_request_for_session_owner(
                Some("SID-primary"),
                "FETCH-aux",
            )
            .is_none(),
            "old owner must not be able to consume the chained request"
        );
        let chained = conn
            .take_pending_subresource_fetch_request_for_session_owner(
                Some("SID-attached"),
                "FETCH-aux",
            )
            .expect("next Fetch session should own the chained request");
        let (url, method, body, headers) =
            chained.accumulated_request_stage_continue_modifications();
        assert_eq!(
            url.as_ref().map(|url| url.as_str()),
            Some("https://example.test/rewritten")
        );
        assert_eq!(method.as_deref(), Some("POST"));
        assert_eq!(body, Some(Some(bytes::Bytes::from_static(b"body"))));
        assert_eq!(
            body.as_ref().unwrap().as_ref().unwrap().as_ptr(),
            upload_storage
        );
        assert_eq!(
            headers.map(|headers| headers.headers().to_byte_strings()),
            Some(vec![("x-new".to_owned(), "2".to_owned())])
        );
    }

    #[test]
    fn cdp_continue_response_repauses_next_matching_fetch_session() {
        let mut conn = CdpConnection::new();
        let mut browser_context = BrowserContext::new("BID-response-chain".to_owned());
        browser_context.set_active_target_id("TID-response-chain".to_owned());
        browser_context.attach_active_session("SID-primary".to_owned());
        assert!(
            browser_context
                .assign_attached_session_to_target("TID-response-chain", "SID-attached".to_owned())
        );
        browser_context
            .active_page_target_mut()
            .runtime_slot
            .set_page_attachment_id_for_test(1);
        conn.install_browser_context_fixture_for_test(browser_context);

        let page_owner = conn
            .target_page_residence_identity_for_session(Some("SID-primary"))
            .expect("test target should expose a Page residence identity");
        let pending = PendingSubresourceFetchResponseRequest {
            page_owner,
            owner_session_id: Some("SID-primary".to_owned()),
            action_session_id: Some("SID-primary".to_owned()),
            owner_kind: PendingSubresourceFetchOwnerKind::Fetch,
            internal_id: 79,
            network_request_id: "NETWORK-79".to_owned(),
            network_request_handle: None,
            frame_id: "FRAME-79".to_owned(),
            document_url: Url::parse("https://example.test/page").unwrap(),
            resource_type: SubresourceResourceType::Fetch,
            websocket_socket_id: None,
            url: Url::parse("https://example.test/api").unwrap(),
            method: "GET".to_owned(),
            request_headers: vec![("accept".to_owned(), "application/json".to_owned())].into(),
            request_body: None,
            request_cookie_report: None,
            response_status: 200,
            response_headers: vec![("content-type".to_owned(), b"application/json".to_vec())],
            response_head_overridden: false,
            response_body_taken_as_stream: false,
            response_body: CapturedBody::from_bytes(br#"{"ok":true}"#.to_vec()),
            response_stage_chain: Some(Box::new(PendingSubresourceFetchResponseStageChain {
                remaining_sessions: vec![PendingSubresourceFetchResponseStage {
                    session_id: Some("SID-attached".to_owned()),
                    owner_kind: PendingSubresourceFetchOwnerKind::Fetch,
                    request_id: "FETCH-response-aux".to_owned(),
                    blocked_intercepts: Vec::new(),
                }],
            })),
        };
        assert!(
            conn.register_pending_subresource_fetch_response_request_for_session_owner(
                Some("SID-primary"),
                "FETCH-response-primary".to_owned(),
                pending,
            )
        );

        let params = json!({"requestId": "FETCH-response-primary"});
        let cmd = Cmd::for_test(
            Some(33),
            "Fetch.continueResponse",
            &params,
            Some("SID-primary"),
            r#"{"id":33,"method":"Fetch.continueResponse"}"#,
        );
        let params: ContinueResponseParams = cmd
            .get_params()
            .expect("continueResponse params should parse")
            .expect("continueResponse params should be present");
        let command = build_cdp_continue_intercepted_response_command(
            &conn,
            &cmd,
            params.request_id.as_ref(),
            None,
            None,
            None,
        );

        let step = start_devtools_fetch_command(
            &mut conn,
            cmd.id,
            cmd.session_id,
            AutomationCommand::ContinueInterceptedResponse(command),
        );

        let super::super::FetchCommandTaskStep::Complete(plan) = step else {
            panic!("chained continueResponse should complete without a page command");
        };
        let events = plan.into_background_events(cmd.id, cmd.session_id);
        assert_eq!(events.len(), 2);
        let (paused_message, paused_sidecar) = events[1].clone().into_parts();
        let Some(AutomationEvent::RequestPaused(paused_sidecar)) = paused_sidecar else {
            panic!("chained response pause should carry a typed automation sidecar");
        };
        assert_eq!(paused_sidecar.request_id.as_str(), "FETCH-response-aux");
        assert_eq!(paused_sidecar.status, Some(200));
        assert_eq!(
            paused_sidecar.network_id.as_ref().map(|id| id.as_str()),
            Some("NETWORK-79")
        );
        let out = events
            .into_iter()
            .map(crate::conn::BackgroundProtocolEvent::into_protocol_message)
            .collect::<Vec<_>>();
        assert_eq!(out.len(), 2);
        assert_eq!(out[0]["id"], json!(33));
        assert_eq!(out[0]["sessionId"], json!("SID-primary"));
        assert_eq!(out[1]["method"], json!("Fetch.requestPaused"));
        assert_eq!(paused_message, out[1]);
        assert_eq!(out[1]["sessionId"], json!("SID-attached"));
        assert_eq!(out[1]["params"]["requestId"], json!("FETCH-response-aux"));
        assert_eq!(out[1]["params"]["networkId"], json!("NETWORK-79"));
        assert_eq!(out[1]["params"]["responseStatusCode"], json!(200));
        assert_eq!(
            out[1]["params"]["responseHeaders"][0],
            json!({"name": "content-type", "value": "application/json"})
        );

        assert!(
            conn.take_pending_subresource_fetch_response_request_for_owner(
                &crate::conn::CommandOwnerScope::for_session("SID-primary"),
                Some("SID-primary"),
                "FETCH-response-aux",
            )
            .is_none(),
            "old owner action session must not be able to resolve the chained response"
        );
        let chained = conn
            .take_pending_subresource_fetch_response_request_for_owner(
                &crate::conn::CommandOwnerScope::for_session("SID-attached"),
                Some("SID-attached"),
                "FETCH-response-aux",
            )
            .expect("next Fetch session should own the chained response");
        assert_eq!(chained.owner_session_id.as_deref(), Some("SID-attached"));
        assert_eq!(chained.action_session_id.as_deref(), Some("SID-attached"));
        assert_eq!(chained.response_status, 200);
    }

    #[test]
    fn cdp_continue_request_repauses_network_or_bidi_stage_on_target_owner_route() {
        let mut conn = CdpConnection::new();
        let mut browser_context = BrowserContext::new("BID-chain-bidi".to_owned());
        browser_context.set_active_target_id("TID-chain-bidi".to_owned());
        browser_context.attach_active_session("SID-primary".to_owned());
        browser_context
            .active_page_target_mut()
            .runtime_slot
            .set_page_attachment_id_for_test(1);
        conn.install_browser_context_fixture_for_test(browser_context);

        let page_owner = conn
            .target_page_residence_identity_for_session(Some("SID-primary"))
            .expect("test target should expose a Page residence identity");
        let pending = PendingSubresourceFetchRequest {
            residence: crate::conn::PendingSubresourceFetchResidence::InstalledPage(page_owner),
            owner_session_id: Some("SID-primary".to_owned()),
            action_session_id: Some("SID-primary".to_owned()),
            owner_kind: PendingSubresourceFetchOwnerKind::Fetch,
            internal_id: 88,
            network_request_id: "NETWORK-88".to_owned(),
            network_request_handle: None,
            frame_id: "FRAME-88".to_owned(),
            document_url: Url::parse("https://example.test/page").unwrap(),
            resource_type: SubresourceResourceType::Fetch,
            websocket_socket_id: None,
            request_stage_chain: Some(Box::new(PendingSubresourceFetchRequestStageChain {
                header_override: None,
                url: Url::parse("https://example.test/api").unwrap(),
                method: "GET".to_owned(),
                headers: Vec::new().into(),
                body: None,
                body_override: None,
                request_cookie_report: None,
                remaining_sessions: vec![PendingSubresourceFetchRequestStage {
                    session_id: Some("BIDI-SID".to_owned()),
                    owner_kind: PendingSubresourceFetchOwnerKind::NetworkOrBidi,
                    request_id: "FETCH-bidi".to_owned(),
                    blocked_intercepts: Vec::new(),
                }],
            })),
        };
        assert!(
            conn.register_pending_subresource_fetch_request_for_session_owner(
                Some("SID-primary"),
                "FETCH-primary".to_owned(),
                pending,
            )
        );

        let params = json!({
            "requestId": "FETCH-primary",
            "url": "https://example.test/rewritten",
        });
        let cmd = Cmd::for_test(
            Some(32),
            "Fetch.continueRequest",
            &params,
            Some("SID-primary"),
            r#"{"id":32,"method":"Fetch.continueRequest"}"#,
        );
        let params = cmd
            .get_params()
            .expect("continueRequest params should parse")
            .expect("continueRequest params should be present");
        let command = build_cdp_continue_intercepted_request_command(&conn, &cmd, params)
            .expect("continueRequest command should normalize");

        let step = start_devtools_fetch_command(
            &mut conn,
            cmd.id,
            cmd.session_id,
            AutomationCommand::ContinueInterceptedRequest(command),
        );

        let super::super::FetchCommandTaskStep::Complete(plan) = step else {
            panic!("chained Network/BiDi continueRequest should complete without a page command");
        };
        let mut out = Vec::new();
        plan.emit_into(&mut out, cmd.id, cmd.session_id);
        assert_eq!(out.len(), 2);
        assert_eq!(out[1]["method"], json!("Fetch.requestPaused"));
        assert_eq!(out[1]["sessionId"], json!("BIDI-SID"));
        assert_eq!(out[1]["params"]["requestId"], json!("FETCH-bidi"));
        assert_eq!(out[1]["params"]["networkId"], json!("NETWORK-88"));

        assert!(
            conn.take_pending_subresource_fetch_request_for_session_owner(
                Some("BIDI-SID"),
                "FETCH-bidi",
            )
            .is_none(),
            "BiDi-only session id has no CDP route and must not own the stored pending request"
        );
        let chained = conn
            .take_pending_subresource_fetch_request_for_owner(
                &crate::conn::CommandOwnerScope::for_session("SID-primary"),
                Some("BIDI-SID"),
                "FETCH-bidi",
            )
            .expect("target owner route should retain the chained Network/BiDi pending request");
        assert_eq!(
            chained.owner_session_id.as_deref(),
            Some("SID-primary"),
            "stored owner remains routable even though the protocol event targets BIDI-SID"
        );
        assert_eq!(
            chained.action_session_id.as_deref(),
            Some("BIDI-SID"),
            "BiDi event session is the session allowed to resolve the chained request"
        );
        assert_eq!(
            chained.owner_kind,
            PendingSubresourceFetchOwnerKind::NetworkOrBidi
        );
    }

    #[test]
    fn cdp_fail_request_builds_protocol_neutral_intercepted_request_command() {
        let conn = CdpConnection::new();
        let params = Value::Null;
        let cmd = Cmd::for_test(
            Some(22),
            "Fetch.failRequest",
            &params,
            Some("SID-2"),
            r#"{"id":22,"method":"Fetch.failRequest"}"#,
        );

        let command = build_cdp_fail_intercepted_request_command(
            &conn,
            &cmd,
            "interception-job-2".to_owned(),
            DevToolsRequestFailure::Failed("net::ERR_BLOCKED_BY_CLIENT".to_owned()),
        );

        assert_eq!(command.context.protocol, FrontendProtocol::Cdp);
        assert_eq!(
            command.context.session_id.as_ref().map(|id| id.as_str()),
            Some("SID-2")
        );
        assert_eq!(command.context.target_id, None);
        assert_eq!(command.context.browser_context_id, None);
        assert_eq!(command.request_id.as_str(), "interception-job-2");
        assert_eq!(
            command.failure,
            DevToolsRequestFailure::Failed("net::ERR_BLOCKED_BY_CLIENT".to_owned())
        );
    }

    #[test]
    fn cdp_fulfill_request_builds_protocol_neutral_intercepted_request_command() {
        let conn = CdpConnection::new();
        let params = Value::Null;
        let cmd = Cmd::for_test(
            Some(23),
            "Fetch.fulfillRequest",
            &params,
            Some("SID-3"),
            r#"{"id":23,"method":"Fetch.fulfillRequest"}"#,
        );

        let command = build_cdp_fulfill_intercepted_request_command(
            &conn,
            &cmd,
            "interception-job-3".to_owned(),
            204,
            vec![("x-test".to_owned(), b"yes".to_vec())],
            Some(vec![1, 2, 3]),
            Some("No Content".to_owned()),
        );

        assert_eq!(command.context.protocol, FrontendProtocol::Cdp);
        assert_eq!(
            command.context.session_id.as_ref().map(|id| id.as_str()),
            Some("SID-3")
        );
        assert_eq!(command.context.target_id, None);
        assert_eq!(command.context.browser_context_id, None);
        assert_eq!(command.request_id.as_str(), "interception-job-3");
        assert_eq!(command.response_code, 204);
        assert_eq!(
            command.response_headers,
            vec![("x-test".to_owned(), b"yes".to_vec())]
        );
        assert_eq!(command.body, Some(vec![1, 2, 3]));
        assert_eq!(command.response_phrase.as_deref(), Some("No Content"));
    }
}