Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
139 lines
4.8 KiB
Rust
139 lines
4.8 KiB
Rust
//! Typed Guardian operations through the existing isolated-turn executor.
|
|
|
|
use akode_guardian_reviewer::GuardianReviewDetails;
|
|
use akode_guardian_reviewer::GuardianReviewSessionOutcome;
|
|
use akode_guardian_reviewer::ReviewDeadline;
|
|
use akode_guardian_reviewer::ReviewerRuntime;
|
|
use akode_guardian_reviewer::SessionDisposition;
|
|
use akode_guardian_reviewer::start_review_turn;
|
|
use akode_guardian_reviewer::wait_for_guardian_review;
|
|
use akode_protocol::models::FunctionCallOutputPayload;
|
|
use akode_protocol::models::ResponseItem;
|
|
use akode_protocol::protocol::TruncationPolicy;
|
|
use akode_protocol::turn_input::TurnInput;
|
|
use akode_protocol::turn_input::TurnInputRequest;
|
|
use akode_protocol::turn_input::TurnStartOptions;
|
|
use akode_utils_string::to_json_string_bounded;
|
|
use serde::Serialize;
|
|
use serde::de::DeserializeOwned;
|
|
use serde_json::Value;
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
/// The host must honor the runtime disposition, including when an operation fails.
|
|
#[must_use = "honor the disposition before reusing the isolated runtime"]
|
|
pub struct OperationResult<T> {
|
|
pub result: anyhow::Result<T>,
|
|
pub disposition: SessionDisposition,
|
|
}
|
|
|
|
/// Existing housekeeper callers share the same disposition contract.
|
|
pub type HousekeeperResult<T> = OperationResult<T>;
|
|
|
|
pub(crate) enum GuardianOperation {
|
|
HousekeeperRouting,
|
|
ChannelSelection,
|
|
}
|
|
|
|
impl GuardianOperation {
|
|
fn input_name(&self) -> &'static str {
|
|
match self {
|
|
Self::HousekeeperRouting => "guardian_housekeeper_input",
|
|
Self::ChannelSelection => "guardian_channel_selection_input",
|
|
}
|
|
}
|
|
|
|
fn label(&self) -> &'static str {
|
|
match self {
|
|
Self::HousekeeperRouting => "Guardian housekeeper routing",
|
|
Self::ChannelSelection => "Guardian channel selection",
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn context_input(
|
|
name: &str,
|
|
input: &impl Serialize,
|
|
policy: TruncationPolicy,
|
|
) -> anyhow::Result<ResponseItem> {
|
|
Ok(ResponseItem::FunctionCallOutput {
|
|
id: None,
|
|
call_id: None,
|
|
name: Some(name.into()),
|
|
namespace: Some("guardian".into()),
|
|
output: FunctionCallOutputPayload::from_text(to_json_string_bounded(
|
|
input,
|
|
policy.byte_budget(),
|
|
)?),
|
|
internal_chat_message_metadata_passthrough: None,
|
|
})
|
|
}
|
|
|
|
pub(crate) async fn sample<T: DeserializeOwned>(
|
|
runtime: &impl ReviewerRuntime,
|
|
operation: GuardianOperation,
|
|
input: &impl Serialize,
|
|
policy: TruncationPolicy,
|
|
schema: Value,
|
|
deadline: ReviewDeadline,
|
|
cancellation: &CancellationToken,
|
|
) -> OperationResult<T> {
|
|
let label = operation.label();
|
|
let input = match context_input(operation.input_name(), input, policy) {
|
|
Ok(input) => input,
|
|
Err(error) => {
|
|
return OperationResult {
|
|
result: Err(error),
|
|
disposition: SessionDisposition::Reusable,
|
|
};
|
|
}
|
|
};
|
|
let request =
|
|
TurnInputRequest::new(TurnInput::ResponseItem(input)).on_start(TurnStartOptions {
|
|
final_output_json_schema: Some(schema),
|
|
..Default::default()
|
|
});
|
|
let turn_id = match start_review_turn(runtime, request, deadline, Some(cancellation)).await {
|
|
Ok(turn_id) => turn_id,
|
|
Err(_) => {
|
|
return OperationResult {
|
|
result: Err(anyhow::anyhow!("{label} turn did not start")),
|
|
disposition: SessionDisposition::Discard,
|
|
};
|
|
}
|
|
};
|
|
let turn = wait_for_guardian_review(
|
|
runtime,
|
|
&turn_id,
|
|
deadline,
|
|
Some(cancellation),
|
|
&mut GuardianReviewDetails::without_session(),
|
|
)
|
|
.await;
|
|
let mut disposition = turn.disposition;
|
|
let result = match turn.outcome {
|
|
GuardianReviewSessionOutcome::Completed(Ok(Some(response))) => {
|
|
serde_json::from_str(response.raw()).map_err(anyhow::Error::from)
|
|
}
|
|
GuardianReviewSessionOutcome::Completed(_) => {
|
|
Err(anyhow::anyhow!("{label} returned an invalid response"))
|
|
}
|
|
GuardianReviewSessionOutcome::PromptBuildFailed(error)
|
|
| GuardianReviewSessionOutcome::SessionFailed { error, .. } => Err(error),
|
|
GuardianReviewSessionOutcome::InputBudgetExceeded => {
|
|
Err(anyhow::anyhow!("{label} input exceeded budget"))
|
|
}
|
|
GuardianReviewSessionOutcome::TimedOut => Err(anyhow::anyhow!("{label} timed out")),
|
|
GuardianReviewSessionOutcome::Aborted => Err(anyhow::anyhow!("{label} aborted")),
|
|
GuardianReviewSessionOutcome::StaleAuthorization => {
|
|
Err(anyhow::anyhow!("{label} input became stale"))
|
|
}
|
|
};
|
|
if result.is_err() && matches!(disposition, SessionDisposition::Reusable) {
|
|
disposition = SessionDisposition::Rollback;
|
|
}
|
|
OperationResult {
|
|
result,
|
|
disposition,
|
|
}
|
|
}
|