use std::path::{Path, PathBuf};
use serde_json::Value;
use tokio_util::sync::CancellationToken;
use crate::{
cache,
config::{Eval, RepoConfig, read_workspace_config},
remote::Session,
scope,
store::{self, HumanClaim, Receipts, Request},
tools::human::{CommandLine, Registry, ToolResult},
};
pub const MAX_RESULT_BYTES: usize = 256_000;
pub const ISSUES_READ_BYTES: u64 = MAX_RESULT_BYTES as u64 + 1;
pub fn default_reviewer() -> Result<crate::types::ReviewerId, String> {
crate::platform::user_name()
.ok_or("Provide an explicit reviewer id; no default reviewer is available.")?
.parse()
}
pub async fn open(
state: &Path,
request: &crate::types::RequestId,
) -> Result<(Receipts, PathBuf), String> {
let repo = store::read_request_repo_by_id(state, request.clone()).await?;
Ok((Receipts::open(state, &repo).await?, repo))
}
async fn with_receipts<T>(
state: &Path,
request: &crate::types::RequestId,
action: impl AsyncFnOnce(&Receipts) -> Result<T, String>,
) -> Result<T, String> {
let (receipts, _) = open(state, request).await?;
let result = action(&receipts).await;
let closed = receipts.close().await;
result.and_then(|value| closed.map(|()| value))
}
pub async fn claim_request(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
) -> Result<HumanClaim, String> {
with_receipts(state, request, async |receipts| {
claim(receipts, request, reviewer).await
})
.await
}
pub async fn unclaim_request(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
) -> Result<HumanClaim, String> {
with_receipts(state, request, async |receipts| {
unclaim(receipts, request, reviewer).await
})
.await
}
pub async fn run_request_tool(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
tool: &crate::config::ToolName,
cancellation: CancellationToken,
) -> Result<ToolResult, String> {
with_receipts(state, request, async |receipts| {
run_human_tool(
receipts,
request,
reviewer,
tool,
cancellation,
crate::tools::human::log::Run::unwatched(),
)
.await
})
.await
}
pub async fn claim(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
) -> Result<HumanClaim, String> {
claim_acquisition(receipts, request, reviewer)
.await
.map(|(claim, _)| claim)
}
pub async fn claim_acquisition(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
) -> Result<(HumanClaim, bool), String> {
receipts.claim_human(request, reviewer).await
}
pub async fn release_exact_claim(state: &Path, claim: &HumanClaim) -> Result<bool, String> {
with_receipts(state, &claim.request_id, async |receipts| {
receipts.release_exact_claim(claim.clone()).await
})
.await
}
pub async fn unclaim(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
) -> Result<HumanClaim, String> {
receipts.release_human(request, reviewer).await
}
pub async fn run_human_tool(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
tool: &crate::config::ToolName,
cancellation: CancellationToken,
run: crate::tools::human::log::Run,
) -> Result<ToolResult, String> {
let ready = async {
let (mut request, _) = receipts.human_request(request, reviewer).await?;
let config = reconnect(receipts, &request).await?;
let registry = Registry::new(&config, &request.eval_id)?;
if !registry.list().any(|entry| &entry.name == tool) {
return Err::<_, String>("Unknown registered Human tool.".into());
}
recheck(
receipts,
&mut request,
reviewer,
&config,
cancellation.clone(),
)
.await?;
receipts.human_request(&request.id, reviewer).await?;
Ok((config, request.eval_id))
};
let (config, eval) = match ready.await {
Ok(ready) => ready,
Err(error) => {
run.end(crate::tools::human::log::End::Failed(error.clone()));
return Err(error);
}
};
let registry = Registry::new(&config, &eval).expect("the registry was just built");
Ok(registry.call_logged(tool, cancellation, run).await)
}
pub async fn tool_commands(
receipts: &Receipts,
request: &crate::types::RequestId,
) -> Result<std::collections::BTreeMap<crate::config::ToolName, Result<CommandLine, String>>, String>
{
let request = receipts.waiting_human(request).await?;
let config = reconnect(receipts, &request).await?;
commands_for(&config, &request.eval_id)
}
pub async fn preview_tool_commands(
request: std::sync::Arc<Request>,
) -> Result<std::collections::BTreeMap<crate::config::ToolName, Result<CommandLine, String>>, String>
{
crate::task::blocking(move || {
let config = declarations(&request).map_err(|mismatch| match mismatch {
Mismatch::Unreadable(error) => error,
Mismatch::Changed(reason) => reason.into(),
})?;
commands_for(&config, &request.eval_id)
})
.await?
}
fn commands_for(
config: &RepoConfig,
eval: &crate::types::EvalId,
) -> Result<std::collections::BTreeMap<crate::config::ToolName, Result<CommandLine, String>>, String>
{
let registry = Registry::new(config, eval)?;
Ok(registry
.list()
.filter(|tool| registry.is_command(&tool.name))
.map(|tool| (tool.name.clone(), registry.command(&tool.name)))
.collect())
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Submission {
verdict: crate::runtime::Verdict,
issues: Vec<String>,
}
pub const GREEN_WITH_ISSUES: &str = "GREEN takes no issues: remove them, or submit RED with them.";
pub const RED_WITHOUT_ISSUE: &str = "RED needs at least one issue.";
impl<'de> serde::Deserialize<'de> for Submission {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Wire {
verdict: crate::runtime::Verdict,
#[serde(default)]
issues: Vec<String>,
}
let wire = Wire::deserialize(deserializer)?;
Self::new(wire.verdict, wire.issues).map_err(serde::de::Error::custom)
}
}
impl TryFrom<Value> for Submission {
type Error = String;
fn try_from(value: Value) -> Result<Self, Self::Error> {
serde_json::from_value(value).map_err(|error| error.to_string())
}
}
impl Submission {
pub fn new(verdict: crate::runtime::Verdict, issues: Vec<String>) -> Result<Self, String> {
let issues: Vec<String> = issues
.into_iter()
.map(|issue| issue.trim().to_owned())
.collect();
if issues.iter().any(String::is_empty) {
return Err("An issue needs text.".into());
}
match verdict {
crate::runtime::Verdict::Green if !issues.is_empty() => {
return Err(GREEN_WITH_ISSUES.into());
}
crate::runtime::Verdict::Red if issues.is_empty() => {
return Err(RED_WITHOUT_ISSUE.into());
}
crate::runtime::Verdict::Green | crate::runtime::Verdict::Red => {}
}
let submission = Self { verdict, issues };
if serde_json::to_vec(&submission.to_json())
.expect("a submission is JSON")
.len()
> MAX_RESULT_BYTES
{
return Err(format!("Human result exceeds {MAX_RESULT_BYTES} bytes."));
}
Ok(submission)
}
pub fn verdict(&self) -> crate::runtime::Verdict {
self.verdict
}
pub fn issues(&self) -> &[String] {
&self.issues
}
pub fn to_json(&self) -> Value {
serde_json::to_value(self).expect("Human submission is JSON")
}
fn result(&self) -> store::ExecutionResult {
store::ExecutionResult::Human {
verdict: self.verdict,
issues: self
.issues
.iter()
.map(|reason| store::HumanIssue {
reason: reason.clone(),
})
.collect(),
}
}
}
pub async fn submit(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
submission: &Submission,
cancellation: CancellationToken,
) -> Result<Request, String> {
submit_answer(
receipts,
request,
reviewer,
SubmittedAnswer::Explicit(submission),
cancellation,
)
.await
}
enum SubmittedAnswer<'a> {
Explicit(&'a Submission),
PreparedExplicit(&'a Submission),
SavedDraft,
PreparedDraft(crate::review::Draft),
}
async fn submit_answer(
receipts: &Receipts,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
answer: SubmittedAnswer<'_>,
cancellation: CancellationToken,
) -> Result<Request, String> {
let (mut request, _) = receipts.human_request(request, reviewer).await?;
let config = reconnect(receipts, &request).await?;
let (answer, expected) = match answer {
SubmittedAnswer::Explicit(submission) => (submission.result(), None),
SubmittedAnswer::PreparedExplicit(submission) => {
(submission.result(), Some(crate::review::Draft::default()))
}
other => {
let draft = match other {
SubmittedAnswer::SavedDraft => receipts
.read_review_draft(&request.id, reviewer)
.await?
.unwrap_or_default(),
SubmittedAnswer::PreparedDraft(draft) => draft,
SubmittedAnswer::Explicit(_) | SubmittedAnswer::PreparedExplicit(_) => {
unreachable!()
}
};
(draft.submission()?.result(), Some(draft))
}
};
recheck(receipts, &mut request, reviewer, &config, cancellation).await?;
request.state = crate::store::RequestState::completed(answer, crate::broker::now());
receipts
.settle_human_if_draft(&request, reviewer, expected)
.await
}
pub async fn submit_and_publish(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
submission: &Submission,
cancellation: CancellationToken,
) -> Result<Request, String> {
publish_answer(
state,
request,
reviewer,
SubmittedAnswer::Explicit(submission),
cancellation,
)
.await
}
pub(crate) async fn publish_explicit_without_draft(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
submission: &Submission,
cancellation: CancellationToken,
) -> Result<Request, String> {
publish_answer(
state,
request,
reviewer,
SubmittedAnswer::PreparedExplicit(submission),
cancellation,
)
.await
}
pub(crate) async fn publish_saved_draft(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
draft: Option<crate::review::Draft>,
cancellation: CancellationToken,
) -> Result<Request, String> {
let answer = draft.map_or(SubmittedAnswer::SavedDraft, SubmittedAnswer::PreparedDraft);
publish_answer(state, request, reviewer, answer, cancellation).await
}
async fn publish_answer(
state: &Path,
request: &crate::types::RequestId,
reviewer: &crate::types::ReviewerId,
answer: SubmittedAnswer<'_>,
cancellation: CancellationToken,
) -> Result<Request, String> {
let (receipts, repo) = open(state, request).await?;
let result = async {
let remote = Session::open(Some(state), Some(&repo))?;
let request = submit_answer(&receipts, request, reviewer, answer, cancellation).await?;
if let Some(remote) = remote {
remote
.publish_request(&receipts, &request)
.await
.map_err(|error| {
format!("The Human result is saved locally, but publishing it failed: {error}")
})?;
}
Ok(request)
}
.await;
let closed = receipts.close().await;
result.and_then(|request| closed.map(|()| request))
}
pub(crate) fn definition(
config: &RepoConfig,
eval: &Eval,
) -> Result<crate::store::HumanDefinition, String> {
let scope = scope::eval_scope(config, eval).map_err(|e| e.to_string())?;
Ok(crate::store::HumanDefinition::authored(
&config.root,
eval,
&scope,
))
}
enum Mismatch {
Unreadable(String),
Changed(&'static str),
}
async fn reconnect(receipts: &Receipts, request: &Request) -> Result<RepoConfig, String> {
match declarations(request) {
Ok(config) => Ok(config),
Err(Mismatch::Unreadable(error)) => Err(error),
Err(Mismatch::Changed(error)) => {
receipts
.retire_human(&request.id, crate::types::FailureCode::InputChanged, error)
.await?;
Err(error.into())
}
}
}
fn declarations(request: &Request) -> Result<RepoConfig, Mismatch> {
let repo = &request
.provenance
.as_ref()
.ok_or_else(|| Mismatch::Unreadable("Human request has no provenance.".into()))?
.repo_path;
let sites = request
.human_definition
.as_ref()
.and_then(|definition| definition.artifacts.value())
.and_then(|artifacts| {
artifacts
.iter()
.map(|(name, artifact)| {
Some((
name.clone(),
crate::config::Site {
path: artifact.path.value()?.clone(),
definition: artifact.definition.value()?.clone(),
},
))
})
.collect::<Option<std::collections::BTreeMap<_, _>>>()
});
let scoped = sites.and_then(|sites| {
crate::config::read_eval_scope(repo, &request.eval_id, &sites)
.ok()
.flatten()
});
let config = match scoped {
Some(config) => config,
None => read_workspace_config(repo).map_err(|e| Mismatch::Unreadable(e.to_string()))?,
};
let eval = config
.evals
.iter()
.find(|eval| eval.id == request.eval_id)
.ok_or(Mismatch::Changed("Recorded Human eval no longer exists."))?;
if Some(definition(&config, eval).map_err(Mismatch::Unreadable)?) != request.human_definition {
return Err(Mismatch::Changed(
"Human Artifact scope or eval declarations changed since the request was recorded.",
));
}
Ok(config)
}
async fn recheck(
receipts: &Receipts,
request: &mut Request,
reviewer: &crate::types::ReviewerId,
config: &RepoConfig,
cancellation: CancellationToken,
) -> Result<(), String> {
if cancellation.is_cancelled() {
return Err("Human action was cancelled.".into());
}
let eval = config
.evals
.iter()
.find(|eval| eval.id == request.eval_id)
.ok_or("Recorded Human eval no longer exists.")?;
if request.key.is_none() && cache::live_input(config, eval).is_none() {
return Ok(());
}
let output = request
.run_dir
.as_deref()
.ok_or("Human request has no output directory.")?;
let parallelism = cache::Parallelism::new(cache::Parallelism::available())?;
let (code, error) = match if let Some(expected) = &request.key {
cache::recheck(config, eval, output, ¶llelism, cancellation.clone())
.await
.map(|actual| actual.as_ref() == Some(expected))
} else {
cache::recheck_observations(
config,
&request.fingerprints,
output,
¶llelism,
cancellation.clone(),
)
.await
} {
Ok(true) => return Ok(()),
Ok(false) => (
crate::types::FailureCode::InputChanged,
"Fingerprint changed during review.".into(),
),
Err(error) if cancellation.is_cancelled() => return Err(error),
Err(error) => (crate::types::FailureCode::FingerprintRecheckFailed, error),
};
request.state =
crate::store::RequestState::failed(error.clone(), Some(code), crate::broker::now());
receipts.settle_human(request, reviewer).await?;
Err(error)
}