causa-runtime 0.0.1

Causa runtime — optional components: execution stack (turn loop, dispatch, streaming, pause/resume, session)
Documentation
//! `resume_turn` — the continuation half of a paused turn: both resume
//! entries consume the **complete paused outcome** plus a request that
//! carries only what is new.
//!
//! A pause suspends a turn between the tool-use gate and executor
//! dispatch (or, for steering, before the next model round). Everything
//! about *where* the turn paused — round, quota counts, the hook's
//! prepared work, queued inputs — is checkpointed in the outcome's
//! [`Continuation`]; the trace is observational and may be trimmed
//! freely. The decision vocabulary is deliberately not new: approve /
//! reject / rewrite are the three constructions of the existing
//! `HookOutcome`, so `decide_batch`'s pause and `resume_turn`'s release
//! are two halves of one gate.
//!
//! Validation runs before anything executes: a rejected request returns
//! the untouched paused material in [`ResumeRejection`] — no model call,
//! no tool execution, no fact appended or sealed. Input errors are never
//! turned into terminal outcomes of the original run.

use std::collections::HashSet;

use causa_kernel::{TextPayload, ToolCallId, TurnContext};

use crate::config::TurnRunOptions;
use crate::control::RunControl;
use crate::conversation::SealedResult;
use crate::driver::{
    Continuation, ConversationOutcome, PausePoint, ResumeState, TurnResult, TurnRunner,
    unanswered_tool_calls,
};
use crate::hook::HookOutcome;

/// The resume payload: only what is NEW at resume time. The host passes
/// the complete paused outcome — the single checkpoint — plus this
/// request.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ResumeRequest {
    /// The new decision for the calls still awaiting approval: approve =
    /// `HookOutcome::passthrough(awaiting)`, reject = all-rejected,
    /// rewrite = edited `to_execute`. It must cover the continuation's
    /// prepared `awaiting` calls exactly once — hook rejections saved in
    /// the continuation can never be re-decided. `None` only for steering
    /// resumes; a decision on a steering pause is rejected.
    pub decision: Option<HookOutcome>,
    /// New inputs appended before the next model round, after the
    /// continuation's queued inputs (empty for pure approvals). Identical
    /// texts are independent inputs and are never deduped.
    pub inject: Vec<TextPayload>,
}

/// A rejected resume request: the reason plus the complete paused
/// material, returned untouched for correction. Validation happens before
/// any side effect, so the outcome is exactly what the caller handed in.
#[derive(Debug)]
pub struct ResumeRejection<T> {
    /// Why the resume request was rejected.
    pub reason: String,
    /// The complete paused material, exactly as received.
    pub outcome: T,
}

impl<T> ResumeRejection<T> {
    /// The untouched paused material — correct the request and retry.
    pub fn into_outcome(self) -> T {
        self.outcome
    }
}

impl<T> std::fmt::Display for ResumeRejection<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "resume rejected: {}", self.reason)
    }
}

impl<T: std::fmt::Debug> std::error::Error for ResumeRejection<T> {}

/// Resume a paused conversation turn from its complete paused outcome.
///
/// - `outcome.result` must be [`TurnResult::Paused`], the state's active
///   turn open and stamped [`SealedResult::Paused`] — exactly what the
///   driver left behind when the turn paused.
/// - `request` carries the new decision (approval pauses) and the new
///   steering injection; see [`ResumeRequest`].
///
/// Counters continue from the continuation (`round`,
/// `accounted_tool_calls`) — never re-derived from the trace. Lower
/// limits stop the turn before external execution: a lowered
/// `max_tool_calls` refuses the withheld batch itself, a lowered
/// `max_model_rounds` refuses the paused round's batch.
///
/// Resumes continue in batch phase — the facts are complete, and a
/// streaming continuation is future work (the `TurnInteraction` still
/// receives batch decisions and steering pulls).
// Rejection returns the complete paused material by value so the caller can
// correct the request and retry, matching the Session construction contract.
#[allow(clippy::result_large_err)]
pub async fn resume_turn(
    runner: &TurnRunner,
    outcome: ConversationOutcome,
    request: ResumeRequest,
    options: TurnRunOptions,
    ctrl: RunControl,
) -> Result<ConversationOutcome, ResumeRejection<ConversationOutcome>> {
    let TurnResult::Paused { .. } = &outcome.result else {
        return Err(ResumeRejection {
            reason: "resume requires a paused outcome".into(),
            outcome,
        });
    };
    // State-shape checks (conversation-only; the bare-turn entry checks
    // the outcome's context instead). Every rejection returns the
    // untouched material.
    let Some(active) = outcome.state.active_turn() else {
        return Err(ResumeRejection {
            reason: "paused outcome has no active turn".into(),
            outcome,
        });
    };
    if active.is_sealed() {
        return Err(ResumeRejection {
            reason: "the paused active turn must be open".into(),
            outcome,
        });
    }
    if outcome.state.sealed_result() != Some(SealedResult::Paused) {
        return Err(ResumeRejection {
            reason: "the conversation state is not stamped Paused".into(),
            outcome,
        });
    }
    if let Err(reason) = validate_resume(
        &outcome.result,
        outcome.state.active_turn().expect("checked above"),
        &request,
    ) {
        return Err(ResumeRejection { reason, outcome });
    }
    let TurnResult::Paused { continuation } = outcome.result else {
        unreachable!("validated above");
    };
    let ConversationOutcome { state, trace, .. } = outcome;
    let resumed = runner
        .resume_conversation(
            state,
            options,
            ctrl,
            ResumeState {
                continuation,
                decision: request.decision,
                inject: request.inject,
                trace,
            },
        )
        .await
        // The entry gates (no active turn / sealed / fresh-run-on-paused)
        // are all covered by the validation above, so the runner cannot
        // reject here.
        .expect("resume validation covered the runner entry gates");
    Ok(resumed)
}

/// Entry-level validation shared by both resume entries: the outcome must
/// be paused, the fact record open, and the continuation consistent with
/// the request and the facts. Returns a clone of the continuation for the
/// driver's `ResumeState`.
pub(crate) fn validate_resume(
    result: &TurnResult,
    facts: &TurnContext,
    request: &ResumeRequest,
) -> Result<Continuation, String> {
    let TurnResult::Paused { continuation } = result else {
        return Err("resume requires a paused outcome".into());
    };
    validate_continuation(continuation, request, facts)?;
    Ok(continuation.clone())
}

/// Continuation-vs-request-vs-facts validation: the pause point pairs
/// with the decision vocabulary, the decision covers the prepared batch
/// exactly once (no foreign ids, no duplicates, no omissions), and the
/// continuation's prepared work is exactly the turn's unanswered tool
/// calls.
pub(crate) fn validate_continuation(
    continuation: &Continuation,
    request: &ResumeRequest,
    facts: &TurnContext,
) -> Result<(), String> {
    validate_continuation_facts(continuation, facts)?;
    match &continuation.pause_point {
        PausePoint::AwaitingApproval { prepared, .. } => {
            let decision = request
                .decision
                .as_ref()
                .ok_or_else(|| "an approval pause requires a decision (HookOutcome)".to_string())?;
            // The decision covers the awaiting calls exactly once.
            let awaiting: HashSet<&ToolCallId> =
                prepared.awaiting.iter().map(|p| &p.call_id).collect();
            let mut covered: HashSet<&ToolCallId> = HashSet::new();
            for id in decision
                .to_execute
                .iter()
                .map(|p| &p.call_id)
                .chain(decision.rejected.iter().map(|r| &r.call_id))
            {
                if !awaiting.contains(id) {
                    return Err(format!(
                        "decision covers call {:?} which is not awaiting approval",
                        id.0
                    ));
                }
                if !covered.insert(id) {
                    return Err(format!("decision covers call {:?} more than once", id.0));
                }
            }
            if covered.len() != awaiting.len() {
                let missing: Vec<String> = awaiting
                    .difference(&covered)
                    .map(|id| id.0.clone())
                    .collect();
                return Err(format!(
                    "decision does not cover every awaiting call: missing {missing:?}"
                ));
            }
            // The new decision's explicit unknown-outcome entries may only
            // pin calls among its own precomputed results whose status is
            // UnknownOutcome — no foreign ids, no duplicates, no actions on
            // decided results. Entries may be omitted; the driver resolves
            // those through its configuration.
            let rejected_unknown: HashSet<&ToolCallId> = decision
                .rejected
                .iter()
                .filter(|r| r.status == causa_kernel::ToolResultStatus::UnknownOutcome)
                .map(|r| &r.call_id)
                .collect();
            let mut pinned: HashSet<&ToolCallId> = HashSet::new();
            for d in &decision.unknown_decisions {
                if !rejected_unknown.contains(&d.call_id) {
                    return Err(format!(
                        "decision pins an unknown-outcome action for call {:?} which has no precomputed UnknownOutcome result",
                        d.call_id.0
                    ));
                }
                if !pinned.insert(&d.call_id) {
                    return Err(format!(
                        "decision pins call {:?} more than once",
                        d.call_id.0
                    ));
                }
            }
            Ok(())
        }
        PausePoint::PausedForSteering => {
            if request.decision.is_some() {
                return Err("a steering pause takes no decision".into());
            }
            Ok(())
        }
    }
}

/// Validate saved continuation material without asking for a future resume
/// decision. Both checkpoint registration and resume use this same check.
pub(crate) fn validate_continuation_facts(
    continuation: &Continuation,
    facts: &TurnContext,
) -> Result<(), String> {
    if facts.is_sealed() {
        return Err("the paused turn must be open to resume".into());
    }
    let unanswered = unanswered_tool_calls(facts);
    match &continuation.pause_point {
        PausePoint::AwaitingApproval { prepared, .. } => {
            let mut saved = HashSet::new();
            for id in prepared
                .awaiting
                .iter()
                .map(|call| &call.call_id)
                .chain(prepared.rejected.iter().map(|result| &result.call_id))
            {
                if !saved.insert(id) {
                    return Err(format!("continuation saves call {:?} more than once", id.0));
                }
            }
            let fact_ids: HashSet<_> = unanswered.iter().map(|call| &call.call_id).collect();
            if saved != fact_ids {
                return Err("continuation does not match the turn's unanswered tool calls".into());
            }
            let saved_unknown: HashSet<_> = prepared
                .rejected
                .iter()
                .filter(|result| result.status == causa_kernel::ToolResultStatus::UnknownOutcome)
                .map(|result| &result.call_id)
                .collect();
            let mut pinned = HashSet::new();
            for decision in &prepared.unknown_decisions {
                if !saved_unknown.contains(&decision.call_id) || !pinned.insert(&decision.call_id) {
                    return Err(format!(
                        "checkpoint decision for call {:?} does not match its saved UnknownOutcome results",
                        decision.call_id.0
                    ));
                }
            }
            if pinned.len() != saved_unknown.len() {
                return Err(
                    "checkpoint is missing the unknown-outcome decision for a saved result".into(),
                );
            }
        }
        PausePoint::PausedForSteering if !unanswered.is_empty() => {
            return Err("steering continuation would leave unanswered tool calls".into());
        }
        PausePoint::PausedForSteering => {}
    }
    Ok(())
}