Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

9.9 KB
Raw Blame
use super::*;

#[test]
fn hermes_chat_plan_grant_installs_and_reopens_without_widening_its_scope() {
    let mut publication = snapshot(1, "renderer");
    publication.aliases[0].protocols = vec![IngressProtocol::ChatCompletions];
    publication.aliases[0].candidates = vec![exact_test_candidate(
        1,
        IngressProtocol::ChatCompletions,
        None,
    )];
    publication.grants[0].protocol = IngressProtocol::ChatCompletions;
    publication.grants[0].consumer = Some(hiroute_domain::AgentModelConsumerV1::Hermes);
    let publication = reseal(publication);
    publication.validate().unwrap();
    let directory = TestDirectory::new();
    let lkg = directory.path().join("publication.json");
    let installer = GatewayPublicationInstaller::open(&lkg).unwrap();
    publish(&installer, publication.clone());
    drop(installer);
    let reopened = GatewayPublicationInstaller::open(&lkg).unwrap();
    assert_eq!(reopened.active().unwrap().publication_revision(), 1);
    let mut widened = publication;
    let (name, route) = test_plan_route("beta", 20);
    widened.grants[0].routes.insert(name, route);
    assert!(reseal(widened).validate().is_err());
}

#[test]
fn publication_revision_gap_is_rejected_before_lkg_or_core_swap() {
    let directory = TestDirectory::new();
    let lkg = directory.path().join("publication.json");
    let installer = GatewayPublicationInstaller::open(&lkg).unwrap();
    publish(&installer, snapshot(1, "renderer-1"));
    let last_good = std::fs::read(&lkg).unwrap();

    assert!(matches!(
        installer.prepare(snapshot(3, "renderer-3")),
        Err(PublicationInstallError::Core(
            InstallError::ResyncRequired { .. }
        ))
    ));
    assert_eq!(installer.active().unwrap().publication_revision(), 1);
    assert_eq!(std::fs::read(&lkg).unwrap(), last_good);
    drop(installer);
    assert_eq!(
        GatewayPublicationInstaller::open(&lkg)
            .unwrap()
            .active()
            .unwrap()
            .publication_revision(),
        1
    );
}

#[test]
fn active_plan_revision_accepts_grant_reachable_protocol_expansion() {
    let directory = TestDirectory::new();
    let lkg = directory.path().join("publication.json");
    let installer = GatewayPublicationInstaller::open(&lkg).unwrap();
    let product = serde_json::from_slice::<hiroute_domain::GatewayPublicationV1>(include_bytes!(
        "../../../../../e2e/product/fixtures/routing/current-publication.v3.json"
    ))
    .unwrap();
    let mut initial: GatewayPublicationSnapshotV3 =
        serde_json::from_value(serde_json::to_value(product.gateway_snapshot().unwrap()).unwrap())
            .unwrap();
    initial.validate().unwrap();
    publish(&installer, initial.clone());

    let alias = initial
        .aliases
        .iter_mut()
        .find(|alias| {
            alias.protocols == [IngressProtocol::Responses]
                && alias.candidates.iter().all(|candidate| {
                    candidate.protocol_profiles.iter().any(|profile| {
                        profile.ingress_protocol == hiroute_domain::UpstreamProtocol::Messages
                    })
                })
        })
        .expect("fixture must contain an ungranted Messages-capable Plan");
    let alias_id = alias.served_model_id.clone();
    alias.protocols.push(IngressProtocol::Messages);
    let plan_route = initial
        .grants
        .iter()
        .find_map(|grant| grant.routes.get(&alias_id))
        .unwrap()
        .clone();
    let grant = initial
        .grants
        .iter_mut()
        .find(|grant| grant.protocol == IngressProtocol::Messages)
        .expect("fixture must contain a Messages grant");
    grant.routes.insert(alias_id.clone(), plan_route);
    initial.publication_revision += 1;
    let expanded = reseal(initial);

    publish(&installer, expanded.clone());
    assert!(
        installer.active().unwrap().aliases[&*alias_id]
            .execution
            .protocols
            .contains(&IngressProtocol::Messages)
    );
    drop(installer);
    assert_eq!(
        GatewayPublicationInstaller::open(&lkg)
            .unwrap()
            .active()
            .unwrap()
            .publication_revision(),
        expanded.publication_revision
    );
}

#[test]
fn restored_plan_can_serve_codex_and_claude_without_a_new_plan_revision() {
    let directory = TestDirectory::new();
    let lkg = directory.path().join("publication.json");
    let installer = GatewayPublicationInstaller::open(&lkg).unwrap();
    let product = serde_json::from_slice::<hiroute_domain::GatewayPublicationV1>(include_bytes!(
        "../../../../../e2e/product/fixtures/routing/current-publication.v3.json"
    ))
    .unwrap();
    let initial: GatewayPublicationSnapshotV3 =
        serde_json::from_value(serde_json::to_value(product.gateway_snapshot().unwrap()).unwrap())
            .unwrap();
    let alias = initial
        .aliases
        .iter()
        .find(|alias| {
            alias.protocols == [IngressProtocol::Responses]
                && alias.candidates.iter().all(|candidate| {
                    candidate.protocol_profiles.iter().any(|profile| {
                        profile.ingress_protocol == hiroute_domain::UpstreamProtocol::Messages
                    })
                })
        })
        .expect("fixture has a Plan usable by both clients");
    let alias_id = alias.served_model_id.clone();
    let original_revision = alias.agent_plan_revision;
    let plan_route = initial
        .grants
        .iter()
        .find_map(|grant| grant.routes.get(&alias_id))
        .unwrap()
        .clone();
    publish(&installer, initial.clone());

    let mut disconnected = initial.clone();
    disconnected.publication_revision += 1;
    disconnected
        .aliases
        .retain(|alias| alias.served_model_id != alias_id);
    for grant in &mut disconnected.grants {
        grant.routes.remove(&alias_id);
    }
    publish(&installer, reseal(disconnected.clone()));
    drop(installer);

    let restarted = GatewayPublicationInstaller::open(&lkg).unwrap();
    let mut both = disconnected;
    both.publication_revision += 1;
    let mut restored = alias.clone();
    restored.protocols = vec![IngressProtocol::Responses, IngressProtocol::Messages];
    both.aliases.push(restored);
    for grant in &mut both.grants {
        if matches!(
            grant.protocol,
            IngressProtocol::Responses | IngressProtocol::Messages
        ) {
            grant.routes.insert(alias_id.clone(), plan_route.clone());
        }
    }
    publish(&restarted, reseal(both));
    let active = restarted.active().unwrap();
    assert_eq!(
        active.agent_plan_revision(&alias_id),
        Some(original_revision)
    );
    let protocols = &active.aliases[&*alias_id].execution.protocols;
    assert!(protocols.contains(&IngressProtocol::Responses));
    assert!(protocols.contains(&IngressProtocol::Messages));
}

#[test]
fn mixed_plan_grant_authorizes_only_each_selected_protocol_after_restart() {
    let product: hiroute_domain::GatewayPublicationV1 = serde_json::from_slice(include_bytes!(
        "../../../../../e2e/product/fixtures/routing/current-publication.v3.json"
    ))
    .unwrap();
    let mut snapshot: GatewayPublicationSnapshotV3 =
        serde_json::from_value(serde_json::to_value(product.gateway_snapshot().unwrap()).unwrap())
            .unwrap();
    let messages = snapshot
        .aliases
        .iter()
        .find(|alias| {
            alias.candidates.iter().all(|c| {
                c.protocol_profiles
                    .iter()
                    .any(|p| p.ingress_protocol == hiroute_domain::UpstreamProtocol::Messages)
            })
        })
        .unwrap()
        .served_model_id
        .clone();
    let responses = snapshot
        .aliases
        .iter()
        .find(|alias| {
            alias.served_model_id != messages
                && alias.protocols.contains(&IngressProtocol::Responses)
        })
        .unwrap()
        .served_model_id
        .clone();
    let routes = [&messages, &responses]
        .into_iter()
        .map(|name| {
            (
                name.clone(),
                snapshot
                    .grants
                    .iter()
                    .find_map(|g| g.routes.get(name))
                    .unwrap()
                    .clone(),
            )
        })
        .collect();
    let alias = snapshot
        .aliases
        .iter_mut()
        .find(|a| a.served_model_id == messages)
        .unwrap();
    if !alias.protocols.contains(&IngressProtocol::Messages) {
        alias.protocols.push(IngressProtocol::Messages);
    }
    snapshot.grants = vec![GrantV1 {
        consumer: None,
        grant_id: "mixed-plan-protocols".into(),
        generation: 1,
        bearer_token_sha256: token_sha256("mixed-protocol-token"),
        protocol: IngressProtocol::Responses,
        routes,
        route_protocols: [(messages.clone(), IngressProtocol::Messages)].into(),
    }];
    let directory = TestDirectory::new();
    let path = directory.path().join("mixed.json");
    let installer = GatewayPublicationInstaller::open(&path).unwrap();
    publish(&installer, reseal(snapshot));
    drop(installer);
    let installer = std::sync::Arc::new(GatewayPublicationInstaller::open(&path).unwrap());
    let authority = crate::server::dispatch::GatewayRequestAuthority::new(installer);
    for (protocol, allowed, forbidden) in [
        (IngressProtocol::Messages, &messages, &responses),
        (IngressProtocol::Responses, &responses, &messages),
    ] {
        assert!(
            authority
                .begin(protocol, Some("Bearer mixed-protocol-token"))
                .unwrap()
                .authorize_alias(allowed, std::time::Instant::now())
                .is_ok()
        );
        assert!(
            authority
                .begin(protocol, Some("Bearer mixed-protocol-token"))
                .unwrap()
                .authorize_alias(forbidden, std::time::Instant::now())
                .is_err()
        );
    }
}