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.

6.4 KB
Raw Blame
//! Whitelisted non-text evidence. Full wire fields never enter this projection.
use crate::writer::ObservationStoreError as Error;
use hiroute_domain::{
    CanonicalDigest, ExecutionFactEnvelopeV1, ExecutionFactV1, ObservationRoutingContextV1,
    ObservationSafeFactV2, UsageProvenanceV1,
};
use rusqlite::{Transaction, params};
pub(crate) fn project(
    transaction: &Transaction<'_>,
    envelope: &ExecutionFactEnvelopeV1,
    digest: &CanonicalDigest,
) -> Result<usize, Error> {
    let json = serde_json::to_string(&projection(envelope, digest)?).map_err(|_| Error::Corrupt)?;
    transaction.execute("INSERT OR IGNORE INTO observation_safe_facts_v2(workspace_id,request_id,original_digest,body_json) VALUES(?1,?2,?3,?4)",params![envelope.correlation.workspace_id.as_str(),envelope.correlation.request_id.as_str(),digest.as_str(),json]).map_err(|_|Error::ActivityUnavailable)
}

/// Rebuild only an existing projection proven to come from a supported pre-applicability
/// envelope. Callers authenticate the carrier and original digest before reaching here.
pub(super) fn refresh_pre_applicability(
    transaction: &Transaction<'_>,
    envelope: &ExecutionFactEnvelopeV1,
    digest: &CanonicalDigest,
) -> Result<usize, Error> {
    let fact = projection(envelope, digest)?;
    if fact.pre_applicability_schema_digest.is_none() {
        return Ok(0);
    }
    let json = serde_json::to_string(&fact).map_err(|_| Error::Corrupt)?;
    transaction.execute("UPDATE observation_safe_facts_v2 SET body_json=?4 WHERE workspace_id=?1 AND request_id=?2 AND original_digest=?3 AND body_json<>?4", params![envelope.correlation.workspace_id.as_str(),envelope.correlation.request_id.as_str(),digest.as_str(),json]).map_err(|_|Error::ActivityUnavailable)
}

fn projection(
    envelope: &ExecutionFactEnvelopeV1,
    digest: &CanonicalDigest,
) -> Result<ObservationSafeFactV2, Error> {
    let fact_json = serde_json::to_value(&envelope.fact).map_err(|_| Error::Corrupt)?;
    let mut fact = ObservationSafeFactV2 {
        pre_applicability_schema_digest: None,
        decision_protocol: None,
        routing_observation: None,
        event_id: envelope.event_id.to_string(),
        sequence: envelope.sequence,
        original_digest: digest.to_string(),
        routing_context: Some(ObservationRoutingContextV1::from_execution_trust(
            &envelope.trust,
        )),
        occurred_at_ms: Some(envelope.occurred_at_ms().map_err(|_| Error::Corrupt)?),
        event_kind: fact_json
            .get("kind")
            .and_then(|value| value.as_str())
            .ok_or(Error::Corrupt)?
            .into(),
        attempt_ordinal: None,
        native_model: None,
        input_tokens: None,
        output_tokens: None,
        cache_read_tokens: None,
        cache_write_tokens: None,
        reasoning_tokens: None,
        outcome: None,
        sensitive_fields_deleted: false,
    };
    match &envelope.fact {
        ExecutionFactV1::RouteDecision(decision) => {
            if hiroute_domain::is_pre_applicability_execution_digest(
                envelope.schema_digest.as_str(),
            ) && envelope.validate_persisted_contract().is_ok()
                && decision
                    .complexity
                    .as_ref()
                    .is_none_or(|c| c.decision_protocol.is_none())
            {
                fact.pre_applicability_schema_digest = Some(envelope.schema_digest.to_string());
            }
            fact.decision_protocol = decision
                .complexity
                .as_ref()
                .and_then(|c| c.decision_protocol);
            if let Some(complexity) = &decision.complexity
                && let Some(observation) = &complexity.routing_observation
            {
                let (Some(plan_id), hiroute_domain::ModelRequestRouteV2::Plan { revision, .. }) =
                    (&envelope.trust.agent_plan_id, &envelope.trust.route)
                else {
                    return Err(Error::Corrupt);
                };
                if !observation.validate() {
                    return Err(Error::Corrupt);
                }
                fact.routing_observation = Some(hiroute_domain::ObservationRoutingDecisionV1 {
                    plan_id: plan_id.as_str().to_owned(),
                    plan_revision: *revision,
                    inherited: complexity.decision_source
                        == hiroute_domain::ComplexityDecisionSourceV1::Inherited,
                    selected_branch_id: complexity.branch_id.clone(),
                    execution_group: complexity.execution_group,
                    selection_reason: complexity.selection_reason,
                    observation: observation.clone(),
                });
            }
        }
        ExecutionFactV1::AttemptStarted {
            ordinal,
            request_model,
            ..
        } => {
            fact.attempt_ordinal = Some(*ordinal);
            fact.native_model = Some(request_model.clone());
        }
        ExecutionFactV1::SemanticCommit { ordinal, .. } => fact.attempt_ordinal = Some(*ordinal),
        ExecutionFactV1::UsageAndCache {
            ordinal,
            input_tokens,
            output_tokens,
            cache_read_tokens,
            cache_write_tokens,
            reasoning_tokens,
            input_provenance,
            output_provenance,
            cache_read_provenance,
            cache_write_provenance,
            reasoning_provenance,
            ..
        } => {
            let reported = |value: Option<u64>, provenance: &UsageProvenanceV1| {
                if *provenance == UsageProvenanceV1::Reported {
                    value
                } else {
                    None
                }
            };
            fact.attempt_ordinal = Some(*ordinal);
            fact.input_tokens = reported(*input_tokens, input_provenance);
            fact.output_tokens = reported(*output_tokens, output_provenance);
            fact.cache_read_tokens = reported(*cache_read_tokens, cache_read_provenance);
            fact.cache_write_tokens = reported(*cache_write_tokens, cache_write_provenance);
            fact.reasoning_tokens = reported(*reasoning_tokens, reasoning_provenance);
        }
        ExecutionFactV1::RequestFinished { .. } => {
            fact.outcome = fact_json
                .get("outcome")
                .and_then(|value| value.as_str())
                .map(str::to_owned)
        }
        _ => {}
    }
    Ok(fact)
}