Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/src/protocol/round.rs

Raw
//! Coherent provider-round publication (SMH-SPEC-SPEC0001, SMH-SPEC-PLUG0001).
use super::{
    ProtocolConnectionConfig, ProtocolCredential, plugin_fault, protocol_transport_with_loader,
    registered_protocol_transport, validate_effective_settings,
};
use crate::plugins::Capability;
use crate::wasm::PluginRegistry;
use crate::wasm::{PluginEngine, PluginGeneration};
use futures::StreamExt;
use smith::error::{Result, SmithError};
use smith::provider::StreamFn;
use smith::tool::CancelHandle;
use std::sync::Arc;
use std::sync::mpsc::{self, SyncSender};

const PROVIDER_COMMAND_ITEMS: usize = 1;

/// Cold runtime-owned configuration plus bounded route to the provider worker.
pub struct ProviderRoundOwner {
    connection: ProtocolConnectionConfig,
    selection: Option<[String; 3]>,
    sender: SyncSender<Command>,
}

enum Command {
    Publish {
        stream: StreamFn,
        reply: SyncSender<()>,
    },
    Request {
        request: smith::provider::ProviderRequest,
        cancel: CancelHandle,
        reply: SyncSender<smith::provider::ProviderStream>,
    },
}

pub fn provider_round_channel(
    connection: ProtocolConnectionConfig,
    selection: Option<[String; 3]>,
) -> (StreamFn, ProviderRoundOwner) {
    let (sender, receiver) = mpsc::sync_channel(PROVIDER_COMMAND_ITEMS);
    std::thread::spawn(move || {
        let mut published: Option<StreamFn> = None;
        for command in receiver {
            match command {
                Command::Publish { stream, reply } => {
                    published = Some(stream);
                    let _sent = reply.send(());
                }
                Command::Request {
                    request,
                    cancel,
                    reply,
                } => {
                    let stream = published.as_ref().map_or_else(
                        || failure(plugin_fault("provider round has no published candidate")),
                        |stream| stream(request, cancel),
                    );
                    // Stream construction queues every initial intent before
                    // replying. Agent drains those records before polling it.
                    let _sent = reply.send(stream);
                }
            }
        }
    });
    let requests = sender.clone();
    let stream: StreamFn = Arc::new(move |request, cancel| {
        let (reply, prepared) = mpsc::sync_channel(PROVIDER_COMMAND_ITEMS);
        if requests
            .send(Command::Request {
                request,
                cancel,
                reply,
            })
            .is_err()
        {
            return failure(plugin_fault("provider worker stopped"));
        }
        prepared
            .recv()
            .unwrap_or_else(|_| failure(plugin_fault("provider worker stopped")))
    });
    (
        stream,
        ProviderRoundOwner {
            connection,
            selection,
            sender,
        },
    )
}

fn failure(error: SmithError) -> smith::provider::ProviderStream {
    futures::stream::iter(std::iter::once(Err(error))).boxed()
}

impl ProviderRoundOwner {
    pub(crate) fn redaction_values(
        &self,
        store: &smith_ai::auth::AuthStore,
    ) -> Result<Vec<String>> {
        match &self.connection.credential {
            ProtocolCredential::ApiKey(reference) => Ok(vec![reference.resolve(store)?]),
            ProtocolCredential::Subscription(_) => Ok(Vec::new()),
        }
    }

    pub(crate) fn validate_model(&self, model: &str) -> Result<()> {
        if self
            .connection
            .catalog
            .models
            .iter()
            .any(|candidate| candidate.id == model)
            && self
                .selection
                .as_ref()
                .is_none_or(|selection| selection[1] == model)
        {
            return Ok(());
        }
        Err(plugin_fault("model not authorized by selected connection"))
    }

    pub(crate) fn validate_provider(&self, provider: &str) -> Result<()> {
        if self
            .selection
            .as_ref()
            .map_or(provider == self.connection.connection_id, |selection| {
                selection[0] == provider
            })
        {
            return Ok(());
        }
        Err(plugin_fault("provider connection unavailable"))
    }

    pub(crate) const fn set_include_usage(&mut self, value: bool) {
        self.connection.settings.include_usage = value;
        self.connection.settings.provenance =
            crate::wasm::bindings::exports::smith::plugin::provider::SettingsProvenance::Explicit;
    }

    pub(crate) const fn subscription(&self) -> bool {
        matches!(
            self.connection.credential,
            ProtocolCredential::Subscription(_)
        )
    }

    pub(crate) fn publish(
        &self,
        registry: &mut PluginRegistry,
        engine: &PluginEngine,
    ) -> Result<()> {
        let stream = prepare_transport(
            self.connection.clone(),
            self.selection.as_ref(),
            registry,
            engine,
        )?;
        let (reply, accepted) = mpsc::sync_channel(PROVIDER_COMMAND_ITEMS);
        self.sender
            .send(Command::Publish { stream, reply })
            .map_err(|_| plugin_fault("provider worker stopped"))?;
        accepted
            .recv()
            .map_err(|_| plugin_fault("provider worker stopped"))
    }
}

pub(super) fn prepare_transport(
    connection: ProtocolConnectionConfig,
    selection: Option<&[String; 3]>,
    registry: &mut PluginRegistry,
    engine: &PluginEngine,
) -> Result<StreamFn> {
    if let Some([provider, model, source]) = selection {
        return registered_protocol_transport(registry, provider, model, source, connection);
    }
    let mut generation = if let Some(artifact) = &connection.artifact {
        PluginGeneration::load(
            engine,
            connection.protocol.manifest()?,
            artifact,
            &[Capability::NetScope, Capability::SecretProxy],
        )
    } else if let Some(generation) = registry.protocol_generation(connection.protocol.binding())? {
        // Fresh scratch instance, exact validated code, no path rediscovery.
        generation.protocol_loader()()
    } else {
        PluginGeneration::load_embedded(
            engine,
            connection.protocol.manifest()?,
            connection.protocol.embedded(),
            &[Capability::NetScope, Capability::SecretProxy],
        )
    }?;
    match connection.protocol.binding() {
        Some(binding) => generation.require_protocol_binding(binding)?,
        None => generation.require_responses_binding()?,
    }
    validate_effective_settings(&mut generation, connection.settings)?;
    let load = generation.protocol_loader();
    let credential_load = match &connection.credential {
        ProtocolCredential::ApiKey(_) => None,
        ProtocolCredential::Subscription(credential) => {
            let generation = registry.bind_credential_source_with_override(
                credential.provider().consumer(),
                credential.provider().source(),
                credential.module_override(),
            )?;
            Some(Arc::new(generation.protocol_loader()) as crate::wasm::GenerationLoader)
        }
    };
    Ok(protocol_transport_with_loader(
        connection,
        load,
        None,
        credential_load,
    ))
}

#[cfg(all(test, target_arch = "x86_64"))]
mod tests {
    //! SMH-SPEC-SPEC0001: bounded round handoff holds owned request data.
    use super::*;

    #[test]
    fn round_handoff_layout_is_exact() {
        assert_eq!(std::mem::size_of::<Command>(), 168);
        assert_eq!(std::mem::size_of::<crate::wasm::GenerationLoader>(), 16);
    }
}