Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b5fcc51cc | ||
|
|
433987413e | ||
|
|
5488eb73ec | ||
|
|
f9f8c625d1 | ||
|
|
6df6ffac4e | ||
|
|
2219a7217d | ||
|
|
cc612516a2 | ||
|
|
599dc1867b | ||
|
|
4e7440f25e |
No files matched your search
@@ -11,6 +11,12 @@ on:
|
||||
options: [story, feature]
|
||||
default: story
|
||||
required: true
|
||||
deploy_target:
|
||||
description: Deployment target for this run
|
||||
type: choice
|
||||
options: [none, production]
|
||||
default: none
|
||||
required: true
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
@@ -499,7 +505,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- required-checks
|
||||
if: ${{ gitea.event_name == 'push' && gitea.ref == 'refs/heads/main' }}
|
||||
if: ${{ gitea.event_name == 'workflow_dispatch' && gitea.ref == 'refs/heads/main' && inputs.validation_level == 'feature' && inputs.deploy_target == 'production' }}
|
||||
env:
|
||||
REGISTRY: git.itexp.me
|
||||
IMAGE_TAG: ${{ gitea.sha }}
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
mod admin_auth_command;
|
||||
#[path = "crank_migrate/db_connect.rs"]
|
||||
mod db_connect;
|
||||
#[path = "crank_migrate/reporting.rs"]
|
||||
mod reporting;
|
||||
|
||||
use reporting::{active_rotation, ensure_no_options, read_required_key_file, rotation_json};
|
||||
|
||||
use crank_config::{ConfigSource, parse_migrator};
|
||||
use crank_registry::{
|
||||
@@ -11,7 +15,7 @@ use crank_registry::{
|
||||
};
|
||||
use crank_runtime::SecretCrypto;
|
||||
use serde_json::json;
|
||||
use std::{path::Path, process::ExitCode};
|
||||
use std::process::ExitCode;
|
||||
use time::OffsetDateTime;
|
||||
const MASTER_KEY_ROTATION_PAGE_SIZE: i64 = 1_000;
|
||||
#[tokio::main]
|
||||
@@ -765,6 +769,59 @@ async fn process_rotation_batch(
|
||||
processed += 1;
|
||||
}
|
||||
}
|
||||
let mut after_task_id: Option<String> = None;
|
||||
loop {
|
||||
let tasks = registry
|
||||
.list_tasks_for_master_key_epoch_page(
|
||||
rotation.source_epoch,
|
||||
after_task_id.as_deref(),
|
||||
MASTER_KEY_ROTATION_PAGE_SIZE,
|
||||
)
|
||||
.await
|
||||
.map_err(CliError::from_registry)?;
|
||||
if tasks.is_empty() {
|
||||
break;
|
||||
}
|
||||
for task in tasks {
|
||||
after_task_id = Some(task.task_id.clone());
|
||||
if task.target_epoch == Some(rotation.target_epoch) {
|
||||
continue;
|
||||
}
|
||||
if max_versions.is_some_and(|limit| processed as usize >= limit) {
|
||||
return Ok(processed);
|
||||
}
|
||||
let plaintext = current_crypto
|
||||
.decrypt_for_epoch(&task.key_version, task.epoch, &task.ciphertext)
|
||||
.map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
"master_key.rotation",
|
||||
"rerun_rotation_or_abort",
|
||||
)
|
||||
})?;
|
||||
let target_ciphertext = target_crypto.encrypt(&plaintext).map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
"master_key.rotation",
|
||||
"rerun_rotation_or_abort",
|
||||
)
|
||||
})?;
|
||||
let now = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.stage_master_key_rotation_task_ciphertext(
|
||||
&rotation.id,
|
||||
&task.task_id,
|
||||
rotation.source_epoch,
|
||||
&target_ciphertext,
|
||||
target_crypto.key_version(),
|
||||
rotation.target_epoch,
|
||||
&now,
|
||||
)
|
||||
.await
|
||||
.map_err(CliError::from_registry)?;
|
||||
processed += 1;
|
||||
}
|
||||
}
|
||||
Ok(processed)
|
||||
}
|
||||
|
||||
@@ -818,6 +875,47 @@ async fn verify_staged_targets(
|
||||
verified += 1;
|
||||
}
|
||||
}
|
||||
let mut after_task_id: Option<String> = None;
|
||||
loop {
|
||||
let tasks = registry
|
||||
.list_target_tasks_for_master_key_rotation_page(
|
||||
rotation.target_epoch,
|
||||
after_task_id.as_deref(),
|
||||
MASTER_KEY_ROTATION_PAGE_SIZE,
|
||||
)
|
||||
.await
|
||||
.map_err(CliError::from_registry)?;
|
||||
if tasks.is_empty() {
|
||||
break;
|
||||
}
|
||||
for task in tasks {
|
||||
after_task_id = Some(task.task_id.clone());
|
||||
let ciphertext = task.target_ciphertext.as_deref().ok_or_else(|| {
|
||||
CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
"master_key.rotation",
|
||||
"rerun_rotation_or_abort",
|
||||
)
|
||||
})?;
|
||||
let key_version = task.target_key_version.as_deref().ok_or_else(|| {
|
||||
CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
"master_key.rotation",
|
||||
"rerun_rotation_or_abort",
|
||||
)
|
||||
})?;
|
||||
target_crypto
|
||||
.decrypt_for_epoch(key_version, rotation.target_epoch, ciphertext)
|
||||
.map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
"master_key.rotation",
|
||||
"rerun_rotation_or_abort",
|
||||
)
|
||||
})?;
|
||||
verified += 1;
|
||||
}
|
||||
}
|
||||
if verified != rotation.total_secret_versions {
|
||||
return Err(CliError::new(
|
||||
"master_key_rotation_verification_failed",
|
||||
@@ -877,89 +975,3 @@ fn decrypt_current(
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async fn active_rotation(
|
||||
registry: &PostgresRegistry,
|
||||
allowed_states: &[&str],
|
||||
) -> Result<Option<MasterKeyRotationRecord>, CliError> {
|
||||
let status = registry
|
||||
.master_key_rotation_status()
|
||||
.await
|
||||
.map_err(CliError::from_registry)?;
|
||||
Ok(status
|
||||
.rotations
|
||||
.into_iter()
|
||||
.find(|rotation| allowed_states.contains(&rotation.state.as_str())))
|
||||
}
|
||||
|
||||
fn rotation_json(rotation: &MasterKeyRotationRecord) -> serde_json::Value {
|
||||
json!({
|
||||
"id": rotation.id,
|
||||
"source_epoch": rotation.source_epoch,
|
||||
"target_epoch": rotation.target_epoch,
|
||||
"state": rotation.state,
|
||||
"backup_ref": rotation.backup_ref.as_ref().map(|_| "configured"),
|
||||
"checkpoint_secret_id": rotation.checkpoint_secret_id,
|
||||
"total_secret_versions": rotation.total_secret_versions,
|
||||
"processed_secret_versions": rotation.processed_secret_versions,
|
||||
"verified_secret_versions": rotation.verified_secret_versions,
|
||||
"failure_code": rotation.failure_code,
|
||||
})
|
||||
}
|
||||
|
||||
fn read_required_key_file(path: Option<&str>) -> Result<String, CliError> {
|
||||
let path = path.ok_or_else(|| {
|
||||
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
|
||||
})?;
|
||||
if path.len() > 512 || path.bytes().any(|byte| byte.is_ascii_control()) {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
let metadata = std::fs::metadata(Path::new(path)).map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
)
|
||||
})?;
|
||||
if !metadata.is_file() || metadata.len() > 16_384 {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
let value = std::fs::read_to_string(Path::new(path)).map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
)
|
||||
})?;
|
||||
if value.trim().is_empty() {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
fn ensure_no_options(options: &MasterKeyOptions) -> Result<(), CliError> {
|
||||
if options.current_key_file.is_some()
|
||||
|| options.target_key_file.is_some()
|
||||
|| options.backup_ref.is_some()
|
||||
|| options.rotation_id.is_some()
|
||||
|| options.max_versions.is_some()
|
||||
{
|
||||
return Err(CliError::new(
|
||||
"invalid_command",
|
||||
"cli.arguments",
|
||||
"run_master_key_status",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
//! Leaf/reporting helpers of the migrator CLI.
|
||||
//!
|
||||
//! Extracted from `crank-migrate.rs` without behaviour changes so the binary entry stays within
|
||||
//! the code-health line limit.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use crank_registry::{MasterKeyRotationRecord, PostgresRegistry};
|
||||
use serde_json::json;
|
||||
|
||||
use super::{CliError, MasterKeyOptions};
|
||||
|
||||
pub(super) async fn active_rotation(
|
||||
registry: &PostgresRegistry,
|
||||
allowed_states: &[&str],
|
||||
) -> Result<Option<MasterKeyRotationRecord>, CliError> {
|
||||
let status = registry
|
||||
.master_key_rotation_status()
|
||||
.await
|
||||
.map_err(CliError::from_registry)?;
|
||||
Ok(status
|
||||
.rotations
|
||||
.into_iter()
|
||||
.find(|rotation| allowed_states.contains(&rotation.state.as_str())))
|
||||
}
|
||||
|
||||
pub(super) fn rotation_json(rotation: &MasterKeyRotationRecord) -> serde_json::Value {
|
||||
json!({
|
||||
"id": rotation.id,
|
||||
"source_epoch": rotation.source_epoch,
|
||||
"target_epoch": rotation.target_epoch,
|
||||
"state": rotation.state,
|
||||
"backup_ref": rotation.backup_ref.as_ref().map(|_| "configured"),
|
||||
"checkpoint_secret_id": rotation.checkpoint_secret_id,
|
||||
"total_secret_versions": rotation.total_secret_versions,
|
||||
"processed_secret_versions": rotation.processed_secret_versions,
|
||||
"verified_secret_versions": rotation.verified_secret_versions,
|
||||
"failure_code": rotation.failure_code,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn read_required_key_file(path: Option<&str>) -> Result<String, CliError> {
|
||||
let path = path.ok_or_else(|| {
|
||||
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
|
||||
})?;
|
||||
if path.len() > 512 || path.bytes().any(|byte| byte.is_ascii_control()) {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
let metadata = std::fs::metadata(Path::new(path)).map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
)
|
||||
})?;
|
||||
if !metadata.is_file() || metadata.len() > 16_384 {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
let value = std::fs::read_to_string(Path::new(path)).map_err(|_| {
|
||||
CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
)
|
||||
})?;
|
||||
if value.trim().is_empty() {
|
||||
return Err(CliError::new(
|
||||
"master_key_input_invalid",
|
||||
"master_key.input",
|
||||
"verify_operator_input",
|
||||
));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub(super) fn ensure_no_options(options: &MasterKeyOptions) -> Result<(), CliError> {
|
||||
if options.current_key_file.is_some()
|
||||
|| options.target_key_file.is_some()
|
||||
|| options.backup_ref.is_some()
|
||||
|| options.rotation_id.is_some()
|
||||
|| options.max_versions.is_some()
|
||||
{
|
||||
return Err(CliError::new(
|
||||
"invalid_command",
|
||||
"cli.arguments",
|
||||
"run_master_key_status",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -29,7 +29,7 @@ sha2.workspace = true
|
||||
sqlx.workspace = true
|
||||
thiserror.workspace = true
|
||||
time.workspace = true
|
||||
tokio = { workspace = true, features = ["sync"] }
|
||||
tokio = { workspace = true, features = ["sync", "signal"] }
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
uuid.workspace = true
|
||||
@@ -45,5 +45,6 @@ opentelemetry_sdk.workspace = true
|
||||
prost.workspace = true
|
||||
reqwest.workspace = true
|
||||
tower.workspace = true
|
||||
tokio = { workspace = true, features = ["net", "time"] }
|
||||
tracing-opentelemetry.workspace = true
|
||||
metrics-util = "0.20.4"
|
||||
+169
-14
@@ -1,7 +1,7 @@
|
||||
use std::{io, process::ExitCode, time::Duration};
|
||||
|
||||
use crank_community_mcp::{
|
||||
auth::CommunityMachineCredentialVerifier, build_app_with_artifacts,
|
||||
auth::CommunityMachineCredentialVerifier, build_app_with_artifacts_and_tasks,
|
||||
session::PostgresTransportSessionStore,
|
||||
};
|
||||
use crank_config::{
|
||||
@@ -15,7 +15,7 @@ use crank_observability::{
|
||||
};
|
||||
use crank_registry::{
|
||||
AdmissionLimits, MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate,
|
||||
PostgresExecutionAdmission, PostgresPoolConfig, PostgresRegistry,
|
||||
PostgresExecutionAdmission, PostgresPoolConfig, PostgresRegistry, PostgresTaskStore,
|
||||
};
|
||||
use crank_runtime::{
|
||||
RequestRateLimitConfig, RequestRateLimiter, RuntimeCacheConfig, RuntimeCacheStores,
|
||||
@@ -201,7 +201,46 @@ async fn run(
|
||||
// admin-api publishes into. The compatibility constructor keeps an unprovisioned/unsafe root
|
||||
// fail-closed per read instead of preventing startup; nothing is ever written from this process.
|
||||
let artifact_store = crank_artifacts::ArtifactStore::new(config.storage_root.clone());
|
||||
let app = build_app_with_artifacts(
|
||||
// Story 4.2/4.3: the durable Task application entry shares the registry pool, the published
|
||||
// catalog refresh interval and the exact admission limits of the runtime; the public wire stays
|
||||
// synchronous/unsupported while a fenced in-process worker executes accepted Tasks.
|
||||
let task_store = std::sync::Arc::new(PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
admission_limits(runtime_limits),
|
||||
crank_core::AdmissionSchedulerConfig::default(),
|
||||
)?);
|
||||
let tasks = std::sync::Arc::new(crank_community_mcp::TaskApplication::new(
|
||||
registry.clone(),
|
||||
Duration::from_millis(config.refresh_ms),
|
||||
cache_stores.coordination.clone(),
|
||||
crank_runtime::TaskService::with_secret_crypto(
|
||||
task_store.clone(),
|
||||
std::sync::Arc::new(secret_crypto.clone()),
|
||||
),
|
||||
));
|
||||
// Story 4.3: the durable Task worker runs in this process over the same PostgreSQL store,
|
||||
// published catalog, executor and encrypted-input authority as the accepting application.
|
||||
let task_handler =
|
||||
std::sync::Arc::new(crank_community_mcp::TaskWorkerHandler::from_application(
|
||||
&tasks,
|
||||
registry.clone(),
|
||||
runtime.clone(),
|
||||
secret_crypto.clone(),
|
||||
));
|
||||
let task_worker = std::sync::Arc::new(crank_runtime::TaskWorker::new(
|
||||
task_store,
|
||||
task_handler,
|
||||
crank_runtime::TaskWorkerConfig {
|
||||
owner: format!("mcp-server-{}", std::process::id()),
|
||||
..crank_runtime::TaskWorkerConfig::default()
|
||||
},
|
||||
)?);
|
||||
let (task_shutdown_tx, task_shutdown_rx) = tokio::sync::watch::channel(false);
|
||||
let task_worker_handle = tokio::spawn({
|
||||
let worker = task_worker.clone();
|
||||
async move { worker.run_until_shutdown(task_shutdown_rx).await }
|
||||
});
|
||||
let app = build_app_with_artifacts_and_tasks(
|
||||
registry,
|
||||
Duration::from_millis(config.refresh_ms),
|
||||
config.runtime.base_url.clone(),
|
||||
@@ -217,6 +256,7 @@ async fn run(
|
||||
std::sync::Arc::new(CommunityMachineCredentialVerifier),
|
||||
runtime_limits.max_concurrent_sessions,
|
||||
artifact_store,
|
||||
tasks,
|
||||
);
|
||||
let listener = TcpListener::bind(config.bind_addr).await?;
|
||||
|
||||
@@ -237,15 +277,77 @@ async fn run(
|
||||
info!(name: "mcp.server.listening", bind_address = %config.bind_addr, "mcp-server listening");
|
||||
*startup_completed = true;
|
||||
|
||||
if let Some(metrics_server) = metrics_server {
|
||||
tokio::select! {
|
||||
result = axum::serve(listener, app) => result?,
|
||||
result = metrics_server.serve() => result?,
|
||||
let serve_result: Result<(), Box<dyn std::error::Error>> = async {
|
||||
if let Some(metrics_server) = metrics_server {
|
||||
tokio::select! {
|
||||
result = serve_with_worker_shutdown(app, listener, shutdown_signal(), task_shutdown_tx.clone()) => result?,
|
||||
result = metrics_server.serve() => result?,
|
||||
}
|
||||
} else {
|
||||
serve_with_worker_shutdown(app, listener, shutdown_signal(), task_shutdown_tx.clone())
|
||||
.await?;
|
||||
}
|
||||
} else {
|
||||
axum::serve(listener, app).await?;
|
||||
Ok(())
|
||||
}
|
||||
.await;
|
||||
|
||||
// Stop new claims; active attempts checkpoint/drain or are cancelled inside the bounded worker
|
||||
// shutdown timeout (<=30s), and queued PostgreSQL work is left for recovery.
|
||||
task_shutdown_tx.send(true).ok();
|
||||
if tokio::time::timeout(Duration::from_secs(30), task_worker_handle)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
tracing::warn!(
|
||||
name: "mcp.task_worker.shutdown_timeout",
|
||||
"durable Task worker did not stop within the story shutdown budget"
|
||||
);
|
||||
}
|
||||
serve_result
|
||||
}
|
||||
|
||||
/// Gracefully stop the durable Task worker as soon as shutdown is requested, while still
|
||||
/// draining in-flight HTTP connections before returning.
|
||||
async fn serve_with_worker_shutdown<S>(
|
||||
app: axum::Router,
|
||||
listener: tokio::net::TcpListener,
|
||||
signal: S,
|
||||
worker_stop: tokio::sync::watch::Sender<bool>,
|
||||
) -> std::io::Result<()>
|
||||
where
|
||||
S: std::future::Future<Output = ()> + Send + 'static,
|
||||
{
|
||||
let worker_stop_graceful = worker_stop.clone();
|
||||
let graceful = async move {
|
||||
signal.await;
|
||||
let _ = worker_stop_graceful.send(true);
|
||||
};
|
||||
let result = axum::serve(listener, app)
|
||||
.with_graceful_shutdown(graceful)
|
||||
.await;
|
||||
let _ = worker_stop.send(true);
|
||||
result
|
||||
}
|
||||
|
||||
/// Process shutdown trigger for the HTTP surface and the durable Task worker.
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
};
|
||||
#[cfg(unix)]
|
||||
let terminate = async {
|
||||
if let Ok(mut signal) =
|
||||
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
|
||||
{
|
||||
signal.recv().await;
|
||||
}
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
tokio::select! {
|
||||
_ = ctrl_c => {}
|
||||
_ = terminate => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_observability(
|
||||
@@ -293,16 +395,20 @@ fn init_observability(
|
||||
)?)
|
||||
}
|
||||
|
||||
fn admission_limits(runtime_limits: RuntimeLimits) -> AdmissionLimits {
|
||||
AdmissionLimits {
|
||||
global_limit: runtime_limits.max_concurrent_unary,
|
||||
..AdmissionLimits::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn execution_admission(
|
||||
pool: sqlx::PgPool,
|
||||
runtime_limits: RuntimeLimits,
|
||||
) -> Result<std::sync::Arc<dyn crank_core::ExecutionAdmission>, Box<dyn std::error::Error>> {
|
||||
let store = PostgresExecutionAdmission::new(
|
||||
pool,
|
||||
AdmissionLimits {
|
||||
global_limit: runtime_limits.max_concurrent_unary,
|
||||
..AdmissionLimits::default()
|
||||
},
|
||||
admission_limits(runtime_limits),
|
||||
crank_core::AdmissionSchedulerConfig::default(),
|
||||
)?;
|
||||
Ok(std::sync::Arc::new(store))
|
||||
@@ -487,3 +593,52 @@ fn runtime_cache_config(
|
||||
.map(|value| value.expose_secret().to_owned()),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::{Router, routing::get};
|
||||
use std::time::Duration;
|
||||
use tokio::sync::{oneshot, watch};
|
||||
|
||||
async fn slow_handler() -> &'static str {
|
||||
tokio::time::sleep(Duration::from_millis(600)).await;
|
||||
"ok"
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn worker_stops_before_http_drain_completes() {
|
||||
let app = Router::new().route("/slow", get(slow_handler));
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let (stop_tx, mut stop_rx) = watch::channel(false);
|
||||
let (signal_tx, signal_rx) = oneshot::channel::<()>();
|
||||
|
||||
let handle = tokio::spawn(serve_with_worker_shutdown(
|
||||
app,
|
||||
listener,
|
||||
async {
|
||||
let _ = signal_rx.await;
|
||||
},
|
||||
stop_tx,
|
||||
));
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let request = client.get(format!("http://{}/slow", addr));
|
||||
let in_flight = tokio::spawn(async move { request.send().await });
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(150)).await;
|
||||
let _ = signal_tx.send(());
|
||||
|
||||
tokio::time::timeout(Duration::from_millis(250), stop_rx.changed())
|
||||
.await
|
||||
.expect("worker stop signal should arrive before HTTP drain")
|
||||
.expect("watch changed");
|
||||
assert!(*stop_rx.borrow());
|
||||
|
||||
let response = in_flight.await.unwrap().unwrap();
|
||||
assert!(response.status().is_success());
|
||||
let serve_result = handle.await.unwrap();
|
||||
assert!(serve_result.is_ok());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod integration {
|
||||
mod catalog_access;
|
||||
mod common;
|
||||
mod durable_tasks;
|
||||
mod era_isolation;
|
||||
mod execution_certainty;
|
||||
mod execution_stages;
|
||||
|
||||
@@ -0,0 +1,413 @@
|
||||
//! Story 4.3 ATDD: durable Attempt ID reaches the adapter attempt span (PF-1).
|
||||
//!
|
||||
//! End-to-end: a real durable Task worker dispatches a retrying GET through a real local
|
||||
//! upstream, and the `adapter_attempt` spans carry the same durable Attempt ID that is
|
||||
//! persisted in `tasks.current_attempt_id` on both attempts.
|
||||
|
||||
use super::super::common;
|
||||
|
||||
use axum::Router;
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::get;
|
||||
use crank_community_mcp::TaskWorkerHandler;
|
||||
use crank_core::{
|
||||
AgentOperationBinding, CorrelationContext, RetryPolicy, Target, ToolExecutionPolicy,
|
||||
};
|
||||
use crank_runtime::{
|
||||
OutboundHttpPolicy, SecretCrypto, TaskWorker, TaskWorkerConfig, community_with_outbound_policy,
|
||||
};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use time::OffsetDateTime;
|
||||
use tracing::{Id, Subscriber, field::Visit};
|
||||
use tracing_subscriber::{Layer, layer::SubscriberExt, registry::LookupSpan};
|
||||
|
||||
const AGENT_SLUG: &str = "reports";
|
||||
const TOOL_NAME: &str = "attempt_trace";
|
||||
const KEY: &str = "attempt-trace-key-1";
|
||||
|
||||
async fn publish(
|
||||
registry: &crank_registry::PostgresRegistry,
|
||||
operation_id: &crank_core::OperationId,
|
||||
version: u32,
|
||||
) {
|
||||
registry
|
||||
.publish_operation(crank_registry::PublishRequest {
|
||||
workspace_id: &common::test_workspace_id(),
|
||||
operation_id,
|
||||
version,
|
||||
published_at: &OffsetDateTime::parse(
|
||||
"2026-03-26T10:00:00Z",
|
||||
&time::format_description::well_known::Rfc3339,
|
||||
)
|
||||
.unwrap(),
|
||||
published_by: Some("alice"),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn application_with_store(
|
||||
registry: &crank_registry::PostgresRegistry,
|
||||
) -> (
|
||||
crank_community_mcp::TaskApplication,
|
||||
std::sync::Arc<crank_registry::PostgresTaskStore>,
|
||||
SecretCrypto,
|
||||
) {
|
||||
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
|
||||
let observed_at = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
|
||||
epoch: crypto.master_key_epoch(),
|
||||
fingerprint: crypto.master_key_fingerprint(),
|
||||
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
|
||||
observed_at: &observed_at,
|
||||
})
|
||||
.await
|
||||
.expect("test master-key identity registered");
|
||||
let store = std::sync::Arc::new(
|
||||
crank_registry::PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
crank_registry::AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let tasks = crank_community_mcp::TaskApplication::new(
|
||||
registry.clone(),
|
||||
std::time::Duration::ZERO,
|
||||
std::sync::Arc::new(crank_runtime::InMemoryCoordinationStateStore::default()),
|
||||
crank_runtime::TaskService::with_secret_crypto(
|
||||
store.clone(),
|
||||
std::sync::Arc::new(crypto.clone()),
|
||||
),
|
||||
);
|
||||
(tasks, store, crypto)
|
||||
}
|
||||
|
||||
async fn principal(
|
||||
registry: &crank_registry::PostgresRegistry,
|
||||
name: &str,
|
||||
scopes: &[crank_core::PlatformApiKeyScope],
|
||||
) -> crank_community_mcp::TaskPrincipal {
|
||||
let token = common::create_platform_api_key(registry, AGENT_SLUG, name, scopes).await;
|
||||
let secret_hash = common::hash_access_secret(&token);
|
||||
let api_key = registry
|
||||
.get_platform_api_key_by_secret_for_agent_slug(
|
||||
common::test_workspace_slug(),
|
||||
AGENT_SLUG,
|
||||
&secret_hash,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("an active platform key resolves");
|
||||
registry
|
||||
.touch_platform_api_key(
|
||||
&api_key.api_key.workspace_id,
|
||||
&api_key.api_key.id,
|
||||
&OffsetDateTime::now_utc(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
crank_community_mcp::TaskPrincipal::Agent {
|
||||
workspace_slug: common::test_workspace_slug().to_owned(),
|
||||
agent_slug: AGENT_SLUG.to_owned(),
|
||||
credential: crank_core::VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: api_key.api_key.scopes.clone(),
|
||||
platform_api_key_id: Some(api_key.api_key.id.clone()),
|
||||
},
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn adapter_attempt_span_carries_durable_attempt_id() {
|
||||
let registry = common::test_registry().await;
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let upstream_app = Router::new().route(
|
||||
"/crm/leads",
|
||||
get({
|
||||
let calls = Arc::clone(&calls);
|
||||
move || {
|
||||
let calls = Arc::clone(&calls);
|
||||
async move {
|
||||
let n = calls.fetch_add(1, Ordering::SeqCst);
|
||||
if n == 0 {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(serde_json::json!({"error":"boom"})),
|
||||
);
|
||||
}
|
||||
(
|
||||
StatusCode::OK,
|
||||
axum::Json(serde_json::json!({"id":"lead-1"})),
|
||||
)
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
|
||||
let upstream = format!("http://{address}");
|
||||
|
||||
let mut operation = common::test_operation(&upstream, TOOL_NAME);
|
||||
let Target::Rest(target) = &mut operation.target;
|
||||
target.method = crank_core::HttpMethod::Get;
|
||||
operation.execution_config.execution_policy = Some(ToolExecutionPolicy {
|
||||
mode: crank_core::ToolExecutionMode::Background,
|
||||
deadline_ms: 60_000,
|
||||
no_tasks_fallback: crank_core::NoTasksFallback::Unsupported,
|
||||
});
|
||||
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
|
||||
registry
|
||||
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
|
||||
.await
|
||||
.unwrap();
|
||||
publish(®istry, &operation.id, 1).await;
|
||||
common::publish_agent_with_bindings(
|
||||
®istry,
|
||||
AGENT_SLUG,
|
||||
vec![AgentOperationBinding {
|
||||
operation_id: operation.id.clone(),
|
||||
operation_version: 1,
|
||||
tool_name: operation.name.clone(),
|
||||
tool_title: operation.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"attempt-trace",
|
||||
&[
|
||||
crank_core::PlatformApiKeyScope::Read,
|
||||
crank_core::PlatformApiKeyScope::Write,
|
||||
],
|
||||
)
|
||||
.await;
|
||||
|
||||
let (tasks, store, crypto) = application_with_store(®istry).await;
|
||||
let acceptance = tasks
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&serde_json::json!({"email": "user@example.com"}),
|
||||
&CorrelationContext::generate(),
|
||||
)
|
||||
.await
|
||||
.expect("durable acceptance");
|
||||
|
||||
let runtime =
|
||||
community_with_outbound_policy(OutboundHttpPolicy::allowing_hosts(["127.0.0.1"])).build();
|
||||
let handler = Arc::new(TaskWorkerHandler::from_application(
|
||||
&tasks,
|
||||
registry.clone(),
|
||||
runtime,
|
||||
crypto,
|
||||
));
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-attempt-trace".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = std::time::Duration::from_millis(50);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = std::time::Duration::from_secs(5);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
|
||||
let capture = TraceCapture::default();
|
||||
let subscriber = tracing_subscriber::registry().with(capture.clone());
|
||||
let _guard = tracing::subscriber::set_default(subscriber);
|
||||
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
let mut state = None;
|
||||
for _ in 0..200 {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
let current: Option<String> =
|
||||
sqlx::query_scalar("select state from tasks where task_id = $1")
|
||||
.bind(acceptance.task_id.as_uuid())
|
||||
.fetch_optional(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
state = current;
|
||||
if state.as_deref() == Some("completed") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), worker_task)
|
||||
.await
|
||||
.expect("worker stops inside the budget")
|
||||
.expect("worker exits cleanly")
|
||||
.expect("worker reports no error");
|
||||
server.abort();
|
||||
|
||||
let durable_attempt_id: Option<String> = sqlx::query_scalar::<_, Option<String>>(
|
||||
"select current_attempt_id from tasks where task_id = $1",
|
||||
)
|
||||
.bind(acceptance.task_id.as_uuid())
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(state.as_deref(), Some("completed"));
|
||||
|
||||
let spans = capture.snapshot();
|
||||
let attempt_spans: Vec<_> = spans
|
||||
.iter()
|
||||
.filter(|s| s.name == "adapter_attempt")
|
||||
.collect();
|
||||
assert_eq!(attempt_spans.len(), 2, "two adapter_attempt spans");
|
||||
for span in &attempt_spans {
|
||||
let aid = span.fields.get("attempt_id").cloned().unwrap_or_default();
|
||||
assert!(!aid.is_empty(), "attempt_id must not be empty");
|
||||
assert!(
|
||||
!aid.contains("traceparent"),
|
||||
"attempt_id must not contain traceparent"
|
||||
);
|
||||
assert_eq!(
|
||||
Some(aid.as_str()),
|
||||
durable_attempt_id.as_deref(),
|
||||
"span attempt_id must equal durable attempt id"
|
||||
);
|
||||
}
|
||||
let ids: Vec<String> = attempt_spans
|
||||
.iter()
|
||||
.map(|s| s.fields.get("attempt_id").cloned().unwrap_or_default())
|
||||
.collect();
|
||||
assert_eq!(ids[0], ids[1], "same durable id on both retries");
|
||||
assert_eq!(
|
||||
spans
|
||||
.iter()
|
||||
.filter(|span| span.name == "durable_task_attempt")
|
||||
.count(),
|
||||
1,
|
||||
"one durable attempt span per dispatch"
|
||||
);
|
||||
let runtime_span = spans
|
||||
.iter()
|
||||
.find(|span| span.name == "runtime.execute")
|
||||
.expect("runtime.execute span");
|
||||
assert_eq!(
|
||||
runtime_span.parent_name,
|
||||
Some("durable_task_attempt"),
|
||||
"the execution continues the durable attempt trace"
|
||||
);
|
||||
for span in &attempt_spans {
|
||||
assert_eq!(
|
||||
span.parent_name,
|
||||
Some("runtime.execute"),
|
||||
"each retry is a child attempt span under the durable execution"
|
||||
);
|
||||
}
|
||||
|
||||
let attempts: Vec<String> = attempt_spans
|
||||
.iter()
|
||||
.map(|s| s.fields.get("attempt").cloned().unwrap_or_default())
|
||||
.collect();
|
||||
assert!(attempts.contains(&"1".to_string()));
|
||||
assert!(attempts.contains(&"2".to_string()));
|
||||
for span in &attempt_spans {
|
||||
assert_eq!(
|
||||
span.fields.get("max_attempts").cloned().unwrap_or_default(),
|
||||
"2"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// TraceCapture minimal port from crates/crank-runtime/tests/stages.rs
|
||||
#[derive(Clone, Default)]
|
||||
struct TraceCapture {
|
||||
spans: Arc<Mutex<Vec<CapturedSpan>>>,
|
||||
}
|
||||
|
||||
impl TraceCapture {
|
||||
fn snapshot(&self) -> Vec<CapturedSpan> {
|
||||
self.spans.lock().expect("span lock").clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct CapturedSpan {
|
||||
name: &'static str,
|
||||
parent_name: Option<&'static str>,
|
||||
fields: std::collections::BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for TraceCapture
|
||||
where
|
||||
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
|
||||
{
|
||||
fn on_new_span(
|
||||
&self,
|
||||
attributes: &tracing::span::Attributes<'_>,
|
||||
id: &Id,
|
||||
context: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let parent = attributes
|
||||
.parent()
|
||||
.and_then(|parent| context.span(parent))
|
||||
.or_else(|| {
|
||||
attributes
|
||||
.is_contextual()
|
||||
.then(|| context.lookup_current())
|
||||
.flatten()
|
||||
});
|
||||
let mut visitor = FieldVisitor::default();
|
||||
attributes.record(&mut visitor);
|
||||
let mut spans = self.spans.lock().expect("span lock");
|
||||
let index = spans.len();
|
||||
spans.push(CapturedSpan {
|
||||
name: attributes.metadata().name(),
|
||||
parent_name: parent.map(|span| span.metadata().name()),
|
||||
fields: visitor.fields,
|
||||
});
|
||||
context
|
||||
.span(id)
|
||||
.expect("span exists")
|
||||
.extensions_mut()
|
||||
.insert(index);
|
||||
}
|
||||
|
||||
fn on_record(
|
||||
&self,
|
||||
id: &Id,
|
||||
values: &tracing::span::Record<'_>,
|
||||
context: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let mut visitor = FieldVisitor::default();
|
||||
values.record(&mut visitor);
|
||||
let span = context.span(id).expect("span exists");
|
||||
let index = *span.extensions().get::<usize>().expect("capture index");
|
||||
self.spans.lock().expect("span lock")[index]
|
||||
.fields
|
||||
.extend(visitor.fields);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FieldVisitor {
|
||||
fields: std::collections::BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
impl Visit for FieldVisitor {
|
||||
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
|
||||
self.fields
|
||||
.insert(field.name().to_owned(), value.to_owned());
|
||||
}
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
self.fields
|
||||
.insert(field.name().to_owned(), format!("{value:?}"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,610 @@
|
||||
//! ATDD Story 4.2 (FR-22/FR-23): the durable Task application entry over a real published catalog
|
||||
//! and a real PostgreSQL schema.
|
||||
//!
|
||||
//! These oracles drive the production application entry (`TaskApplication`) that the mcp-server
|
||||
//! composition root constructs: a verified machine credential is checked against the published Tool,
|
||||
//! the Task is accepted durably, a freshly constructed application instance reads the same identity
|
||||
//! and pins, replay/version conflicts never rebind, foreign callers receive no metadata, and a missing
|
||||
//! Task store fails closed and reports readiness as not ready. The public `tools/call` wire stays
|
||||
//! synchronous/unsupported: this suite never calls it.
|
||||
|
||||
use super::common;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crank_community_mcp::{TaskApplication, TaskApplicationError, TaskPrincipal};
|
||||
use crank_core::{
|
||||
AgentOperationBinding, CorrelationContext, NoTasksFallback, PlatformApiKeyScope, RequestId,
|
||||
TaskAcceptRequest, TaskEnvelope, TaskId, TaskOwnerKind, TaskPins, TaskState, TaskStore,
|
||||
TaskSupportIds, ToolExecutionMode, ToolExecutionPolicy,
|
||||
};
|
||||
use crank_registry::{
|
||||
AdmissionLimits, CreateVersionRequest, PostgresRegistry, PostgresTaskStore, PublishRequest,
|
||||
};
|
||||
use crank_runtime::{InMemoryCoordinationStateStore, SecretCrypto, TaskService};
|
||||
use serde_json::json;
|
||||
use sqlx::{PgPool, Row};
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
|
||||
const AGENT_SLUG: &str = "reports";
|
||||
const TOOL_NAME: &str = "report_gen";
|
||||
const KEY: &str = "retry-key-1";
|
||||
|
||||
fn operation(policy: ToolExecutionPolicy) -> crank_registry::RegistryOperation {
|
||||
let mut operation = common::test_operation("https://api.example.com", TOOL_NAME);
|
||||
operation.execution_config.execution_policy = Some(policy);
|
||||
operation
|
||||
}
|
||||
|
||||
fn background_policy() -> ToolExecutionPolicy {
|
||||
ToolExecutionPolicy {
|
||||
mode: ToolExecutionMode::Background,
|
||||
deadline_ms: 60_000,
|
||||
no_tasks_fallback: NoTasksFallback::Unsupported,
|
||||
}
|
||||
}
|
||||
|
||||
async fn seed_tool(registry: &PostgresRegistry, policy: ToolExecutionPolicy) {
|
||||
let operation = operation(policy);
|
||||
registry
|
||||
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
|
||||
.await
|
||||
.unwrap();
|
||||
publish(registry, &operation.id, 1).await;
|
||||
// A second, explicitly synchronous Tool of the same Agent: the durable entry must refuse it.
|
||||
let synchronous = common::test_operation("https://api.example.com", "report_sync");
|
||||
registry
|
||||
.create_operation(&common::test_workspace_id(), &synchronous, Some("alice"))
|
||||
.await
|
||||
.unwrap();
|
||||
publish(registry, &synchronous.id, 1).await;
|
||||
let binding = AgentOperationBinding {
|
||||
operation_id: operation.id.clone(),
|
||||
operation_version: 1,
|
||||
tool_name: operation.name.clone(),
|
||||
tool_title: operation.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
};
|
||||
let synchronous_binding = AgentOperationBinding {
|
||||
operation_id: synchronous.id.clone(),
|
||||
operation_version: 1,
|
||||
tool_name: synchronous.name.clone(),
|
||||
tool_title: synchronous.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
};
|
||||
common::publish_agent_with_bindings(registry, AGENT_SLUG, vec![binding, synchronous_binding])
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn publish(
|
||||
registry: &PostgresRegistry,
|
||||
operation_id: &crank_core::OperationId,
|
||||
version: u32,
|
||||
) {
|
||||
registry
|
||||
.publish_operation(PublishRequest {
|
||||
workspace_id: &common::test_workspace_id(),
|
||||
operation_id,
|
||||
version,
|
||||
published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(),
|
||||
published_by: Some("alice"),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn publish_tool_version(registry: &PostgresRegistry, version: u32) {
|
||||
let mut next = operation(background_policy());
|
||||
next.version = version;
|
||||
next.status = crank_core::OperationStatus::Draft;
|
||||
next.published_at = None;
|
||||
registry
|
||||
.create_version(CreateVersionRequest {
|
||||
workspace_id: &common::test_workspace_id(),
|
||||
snapshot: &next,
|
||||
change_note: Some("next publication"),
|
||||
created_by: Some("alice"),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
publish(registry, &next.id, version).await;
|
||||
// The agent catalog must bind the published Tool version for the new publication to be addressed.
|
||||
let agent_id = common::test_agent_id(AGENT_SLUG);
|
||||
let draft = crank_core::AgentVersion {
|
||||
agent_id: agent_id.clone(),
|
||||
version: 2,
|
||||
status: crank_core::AgentStatus::Draft,
|
||||
instructions: json!({}),
|
||||
tool_selection_policy: Default::default(),
|
||||
created_at: OffsetDateTime::parse("2026-03-26T10:01:00Z", &Rfc3339).unwrap(),
|
||||
};
|
||||
registry
|
||||
.create_agent_draft_version(crank_registry::CreateAgentDraftVersionRequest {
|
||||
workspace_id: &common::test_workspace_id(),
|
||||
agent_id: &agent_id,
|
||||
version: &draft,
|
||||
bindings: &[AgentOperationBinding {
|
||||
operation_id: next.id.clone(),
|
||||
operation_version: version,
|
||||
tool_name: next.name.clone(),
|
||||
tool_title: next.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
}],
|
||||
updated_at: &OffsetDateTime::parse("2026-03-26T10:01:00Z", &Rfc3339).unwrap(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
registry
|
||||
.publish_agent(crank_registry::PublishAgentRequest {
|
||||
workspace_id: &common::test_workspace_id(),
|
||||
agent_id: &agent_id,
|
||||
version: 2,
|
||||
published_at: &OffsetDateTime::parse("2026-03-26T10:02:00Z", &Rfc3339).unwrap(),
|
||||
published_by: Some("alice"),
|
||||
expected_state: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
const TEST_MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
|
||||
|
||||
fn task_service(pool: PgPool, crypto: SecretCrypto) -> TaskService {
|
||||
let store = PostgresTaskStore::new(
|
||||
pool,
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration");
|
||||
TaskService::with_secret_crypto(std::sync::Arc::new(store), std::sync::Arc::new(crypto))
|
||||
}
|
||||
|
||||
/// The application entry under the same encrypted-input authority the production process uses.
|
||||
async fn application(registry: &PostgresRegistry) -> TaskApplication {
|
||||
let crypto = SecretCrypto::new(TEST_MASTER_KEY).expect("test master key");
|
||||
let observed_at = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
|
||||
epoch: crypto.master_key_epoch(),
|
||||
fingerprint: crypto.master_key_fingerprint(),
|
||||
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
|
||||
observed_at: &observed_at,
|
||||
})
|
||||
.await
|
||||
.expect("test master-key identity registered");
|
||||
TaskApplication::new(
|
||||
registry.clone(),
|
||||
Duration::from_millis(0),
|
||||
std::sync::Arc::new(InMemoryCoordinationStateStore::default()),
|
||||
task_service(registry.pool().clone(), crypto),
|
||||
)
|
||||
}
|
||||
|
||||
async fn principal(
|
||||
registry: &PostgresRegistry,
|
||||
name: &str,
|
||||
scopes: &[PlatformApiKeyScope],
|
||||
) -> TaskPrincipal {
|
||||
let token = common::create_platform_api_key(registry, AGENT_SLUG, name, scopes).await;
|
||||
// The production machine-access path resolves the presented secret against the active platform
|
||||
// key of the addressed Agent and derives the scope identity from that verified record.
|
||||
let secret_hash = common::hash_access_secret(&token);
|
||||
let api_key = registry
|
||||
.get_platform_api_key_by_secret_for_agent_slug(
|
||||
common::test_workspace_slug(),
|
||||
AGENT_SLUG,
|
||||
&secret_hash,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("an active platform key resolves");
|
||||
registry
|
||||
.touch_platform_api_key(
|
||||
&api_key.api_key.workspace_id,
|
||||
&api_key.api_key.id,
|
||||
&OffsetDateTime::now_utc(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
TaskPrincipal::Agent {
|
||||
workspace_slug: common::test_workspace_slug().to_owned(),
|
||||
agent_slug: AGENT_SLUG.to_owned(),
|
||||
credential: crank_core::VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: api_key.api_key.scopes.clone(),
|
||||
platform_api_key_id: Some(api_key.api_key.id.clone()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn unsampled_correlation() -> CorrelationContext {
|
||||
CorrelationContext::new(
|
||||
crank_core::RequestId::generate(),
|
||||
crank_core::TraceContext::parse("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00")
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
async fn counts(pool: &PgPool) -> (i64, i64) {
|
||||
let row = sqlx::query(
|
||||
"select
|
||||
(select count(*)::bigint from tasks) as tasks,
|
||||
(select count(*)::bigint from execution_admission_entries where task_id is not null)
|
||||
as intents",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
(row.get("tasks"), row.get("intents"))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_verified_caller_receives_a_durable_task_that_a_new_instance_reads() {
|
||||
let registry = common::test_registry().await;
|
||||
seed_tool(®istry, background_policy()).await;
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"task-accept",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let correlation = unsampled_correlation();
|
||||
let app = application(®istry).await;
|
||||
|
||||
let acceptance = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&correlation,
|
||||
)
|
||||
.await
|
||||
.expect("acceptance over the real publication");
|
||||
assert_eq!(acceptance.state, TaskState::Queued);
|
||||
assert!(!acceptance.replayed);
|
||||
assert_eq!(acceptance.operation_version, 1);
|
||||
assert_eq!(acceptance.support.request_id, *correlation.request_id());
|
||||
assert_eq!(acceptance.support.trace_id, *correlation.trace_id());
|
||||
assert!(!acceptance.support.trace_sampled);
|
||||
assert_eq!(
|
||||
uuid::Uuid::parse_str(&acceptance.task_id.to_string())
|
||||
.unwrap()
|
||||
.get_version_num(),
|
||||
7,
|
||||
"the durable Task id is UUIDv7"
|
||||
);
|
||||
// Task, dispatch intent and queue slot committed together.
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
|
||||
// A freshly constructed application + service reads the same durable identity and pins.
|
||||
let reader = application(®istry).await;
|
||||
let record = reader
|
||||
.status(&caller, TOOL_NAME, acceptance.task_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(record.state, TaskState::Queued);
|
||||
assert_eq!(record.pins.operation_version, 1);
|
||||
assert_eq!(record.pins.policy_mode, ToolExecutionMode::Background);
|
||||
assert_eq!(record.support.request_id, acceptance.support.request_id);
|
||||
assert_eq!(record.support.traceparent, acceptance.support.traceparent);
|
||||
assert!(!record.support.trace_sampled);
|
||||
let outcome = reader
|
||||
.result(&caller, TOOL_NAME, acceptance.task_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(outcome.result.is_none() && outcome.error.is_none());
|
||||
|
||||
// Replay of the same scoped key and input returns the accepted Task instead of a new one.
|
||||
let replay = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(replay.task_id, acceptance.task_id);
|
||||
assert!(replay.replayed);
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_new_publication_never_rebinds_an_accepted_task() {
|
||||
let registry = common::test_registry().await;
|
||||
seed_tool(®istry, background_policy()).await;
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"task-rebind",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let app = application(®istry).await;
|
||||
let accepted = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let before = app
|
||||
.status(&caller, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
publish_tool_version(®istry, 2).await;
|
||||
|
||||
// The same retry keeps the accepted Task and its v1 pin; it never becomes a v2 Task.
|
||||
let replay = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(replay.task_id, accepted.task_id);
|
||||
assert_eq!(replay.operation_version, 1);
|
||||
let after = app
|
||||
.status(&caller, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(after.pins.operation_version, 1);
|
||||
assert_eq!(after.support.request_id, before.support.request_id);
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
|
||||
// An explicit other exact version is a typed conflict, not a rebind.
|
||||
let explicit = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
Some(2),
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.expect_err("a different explicit pin cannot rebind the accepted Task");
|
||||
assert_eq!(explicit, TaskApplicationError::VersionUnavailable);
|
||||
// The original explicit pin still resolves to the accepted Task.
|
||||
let pinned = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
Some(1),
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(pinned.task_id, accepted.task_id);
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn foreign_and_insufficient_callers_receive_no_task_metadata() {
|
||||
let registry = common::test_registry().await;
|
||||
seed_tool(®istry, background_policy()).await;
|
||||
let owner = principal(
|
||||
®istry,
|
||||
"task-owner",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let app = application(®istry).await;
|
||||
let accepted = app
|
||||
.accept_background(
|
||||
&owner,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// Positive control: the owner reads its Task.
|
||||
assert!(
|
||||
app.status(&owner, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
// A second verified key of the same Agent is another authorization scope: no payload, no metadata.
|
||||
let foreign_scope =
|
||||
principal(®istry, "task-other-scope", &[PlatformApiKeyScope::Read]).await;
|
||||
match app
|
||||
.status(&foreign_scope, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
{
|
||||
Err(TaskApplicationError::NotFound) => {}
|
||||
other => panic!("foreign scope must be not-found without metadata, got {other:?}"),
|
||||
}
|
||||
assert!(matches!(
|
||||
app.result(&foreign_scope, TOOL_NAME, accepted.task_id)
|
||||
.await,
|
||||
Err(TaskApplicationError::NotFound)
|
||||
));
|
||||
|
||||
// A synchronous publication is not accepted as background work at all.
|
||||
let synchronous_app = application(®istry).await;
|
||||
let refused = synchronous_app
|
||||
.accept_background(
|
||||
&owner,
|
||||
"report_sync",
|
||||
None,
|
||||
"sync-key",
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.expect_err("a synchronous policy has no durable acceptance");
|
||||
assert_eq!(refused, TaskApplicationError::NotBackground);
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_owned_tasks_are_scoped_to_that_admin_and_workspace() {
|
||||
let registry = common::test_registry().await;
|
||||
seed_tool(®istry, background_policy()).await;
|
||||
let app = application(®istry).await;
|
||||
let admin = TaskPrincipal::Admin {
|
||||
workspace_slug: common::test_workspace_slug().to_owned(),
|
||||
agent_slug: AGENT_SLUG.to_owned(),
|
||||
user_id: "user_admin_1".to_owned(),
|
||||
};
|
||||
let accepted = app
|
||||
.accept_background(
|
||||
&admin,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.expect("an authenticated administrator accepts durable work");
|
||||
assert_eq!(accepted.state, TaskState::Queued);
|
||||
// Positive control: the same administrator reads its own Task.
|
||||
assert!(
|
||||
app.status(&admin, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.is_ok()
|
||||
);
|
||||
|
||||
// An administrator is never a wildcard owner: another admin user and the owning Agent of the
|
||||
// same workspace both see nothing.
|
||||
let other_admin = TaskPrincipal::Admin {
|
||||
workspace_slug: common::test_workspace_slug().to_owned(),
|
||||
agent_slug: AGENT_SLUG.to_owned(),
|
||||
user_id: "user_admin_2".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
app.status(&other_admin, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskApplicationError::NotFound
|
||||
);
|
||||
assert_eq!(
|
||||
app.result(&other_admin, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskApplicationError::NotFound
|
||||
);
|
||||
let agent = principal(®istry, "task-admin-cross", &[PlatformApiKeyScope::Read]).await;
|
||||
assert_eq!(
|
||||
app.status(&agent, TOOL_NAME, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskApplicationError::NotFound
|
||||
);
|
||||
|
||||
// An admin identity addressed to another workspace cannot resolve the publication at all.
|
||||
let foreign_workspace = TaskPrincipal::Admin {
|
||||
workspace_slug: "other-workspace".to_owned(),
|
||||
agent_slug: AGENT_SLUG.to_owned(),
|
||||
user_id: "user_admin_1".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
app.accept_background(
|
||||
&foreign_workspace,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
"foreign-key",
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskApplicationError::NotFound
|
||||
);
|
||||
assert_eq!(counts(registry.pool()).await, (1, 1));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_missing_task_store_fails_closed_and_readiness_reports_not_ready() {
|
||||
let database_url = crank_test_support::postgres_schema_url("durable_tasks_outage").await;
|
||||
let pool = PgPool::connect(&database_url).await.unwrap();
|
||||
// No migration: the durable Task state does not exist.
|
||||
let outage_registry = PostgresRegistry::connect(&database_url).await;
|
||||
assert!(
|
||||
outage_registry.is_err(),
|
||||
"an unmigrated registry cannot be used"
|
||||
);
|
||||
|
||||
crank_registry::MigrationAuthority::apply(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
|
||||
seed_tool(®istry, background_policy()).await;
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"task-outage",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let app = application(®istry).await;
|
||||
assert!(app.health().await.is_ok());
|
||||
|
||||
// Fail closed: without the Task schema the entry accepts nothing and reports dependency failure.
|
||||
sqlx::query("drop table tasks cascade")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(app.health().await.is_err());
|
||||
let error = app
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
KEY,
|
||||
&json!({"report": "q3"}),
|
||||
&unsampled_correlation(),
|
||||
)
|
||||
.await
|
||||
.expect_err("no durable Task store, no acceptance");
|
||||
assert!(matches!(
|
||||
error,
|
||||
TaskApplicationError::Task(crank_core::TaskError::Unavailable { .. })
|
||||
));
|
||||
|
||||
// Readiness of the app that wires the same store reports the durable Task dependency as not ready.
|
||||
let router = common::build_test_app(registry.clone(), Duration::from_millis(0), None);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move {
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
});
|
||||
let response = reqwest::get(format!("http://{address}/ready"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), reqwest::StatusCode::SERVICE_UNAVAILABLE);
|
||||
let body: serde_json::Value = response.json().await.unwrap();
|
||||
assert_eq!(body["checks"]["durable_tasks"], json!("not_ready"));
|
||||
server.abort();
|
||||
}
|
||||
|
||||
mod attempt_trace;
|
||||
mod shutdown_window;
|
||||
mod task_worker;
|
||||
@@ -0,0 +1,190 @@
|
||||
//! Story 4.3 QA caveat proof: the narrow post-checkpoint / pre-permit stop window.
|
||||
//!
|
||||
//! Split from `task_worker.rs` to keep both integration files inside the code-health limit.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A stop that lands after the durable dispatch checkpoint but before the sealed dispatch permit
|
||||
/// exists finalizes the Task as failed(`shutdown_before_dispatch`) without any outbound call, and
|
||||
/// does not consume work that is still queued.
|
||||
///
|
||||
/// The window is made deterministic instead of raced: a `BEFORE UPDATE` trigger delays only the
|
||||
/// durable dispatch checkpoint, so the stop flag is set while that checkpoint statement is still in
|
||||
/// flight (before `TaskDispatchPermit`/`TaskHandler::execute` can exist).
|
||||
#[tokio::test]
|
||||
async fn task_worker_stop_between_checkpoint_and_dispatch_never_reaches_upstream() {
|
||||
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
|
||||
use crank_runtime::{
|
||||
TaskAttemptOutcome, TaskDispatchPermit, TaskHandler, TaskWorker, TaskWorkerConfig,
|
||||
TaskWorkerError,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
// The test registry exposes a single pooled connection: delaying the checkpoint inside its own
|
||||
// statement keeps the window deterministic without competing for a second connection.
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(
|
||||
"create function delay_task_dispatch_checkpoint() returns trigger language plpgsql as $$
|
||||
begin
|
||||
if new.current_attempt_id is not null and old.current_attempt_id is null then
|
||||
perform pg_sleep(1.5);
|
||||
end if;
|
||||
return new;
|
||||
end $$;
|
||||
create trigger delay_task_dispatch_checkpoint before update on tasks
|
||||
for each row execute function delay_task_dispatch_checkpoint();",
|
||||
))
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.expect("checkpoint delay trigger");
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let mut accepted = Vec::new();
|
||||
for (seed, scoped_key) in [("q", "a"), ("r", "c")] {
|
||||
let request = TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_stop_window".to_owned(),
|
||||
platform_api_key_id: None,
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 1,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_900_000_000_000,
|
||||
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
|
||||
approval_reference: None,
|
||||
},
|
||||
scoped_key: scoped_key.repeat(64),
|
||||
input_fingerprint: scoped_key.repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({ "report": seed })).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&crank_core::TraceContext::parse(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
};
|
||||
accepted.push(store.accept(request).await.expect("PG accepts"));
|
||||
}
|
||||
|
||||
// The first revalidation waits for the test; the second Task is never claimed (batch_limit = 1).
|
||||
let revalidate_entered = Arc::new(tokio::sync::Semaphore::new(0));
|
||||
let revalidate_release = Arc::new(tokio::sync::Semaphore::new(0));
|
||||
let executed = Arc::new(AtomicUsize::new(0));
|
||||
struct GatedHandler {
|
||||
entered: Arc<tokio::sync::Semaphore>,
|
||||
release: Arc<tokio::sync::Semaphore>,
|
||||
executed: Arc<AtomicUsize>,
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl TaskHandler for GatedHandler {
|
||||
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
|
||||
self.entered.add_permits(1);
|
||||
let _ = self.release.acquire().await;
|
||||
Ok(())
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
_lease: &TaskLease,
|
||||
_attempt_id: &str,
|
||||
_cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
_permit: TaskDispatchPermit,
|
||||
) -> TaskAttemptOutcome {
|
||||
self.executed.fetch_add(1, Ordering::SeqCst);
|
||||
TaskAttemptOutcome::Completed(json!({ "ok": true }))
|
||||
}
|
||||
}
|
||||
let handler: Arc<dyn TaskHandler> = Arc::new(GatedHandler {
|
||||
entered: revalidate_entered.clone(),
|
||||
release: revalidate_release.clone(),
|
||||
executed: executed.clone(),
|
||||
});
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-stop-window".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 30_000;
|
||||
config.poll_interval = Duration::from_millis(50);
|
||||
config.renewal_interval = Duration::from_secs(20);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(20);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
tokio::time::timeout(Duration::from_secs(5), revalidate_entered.acquire())
|
||||
.await
|
||||
.expect("worker must claim the first Task and enter revalidation")
|
||||
.expect("revalidation gate is open")
|
||||
.forget();
|
||||
|
||||
// Let revalidation continue; the checkpoint now runs while the trigger holds it for 1.5s.
|
||||
revalidate_release.add_permits(1);
|
||||
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||
|
||||
// The stop now lands while the durable checkpoint statement is still in flight: the worker may
|
||||
// finalize the Task, but no permit exists yet, so no outbound call can start.
|
||||
let stop_requested = std::time::Instant::now();
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
|
||||
tokio::time::timeout(Duration::from_secs(30), worker_task)
|
||||
.await
|
||||
.expect("worker stops inside the 30s budget")
|
||||
.expect("worker exits cleanly")
|
||||
.expect("worker reports no error");
|
||||
assert!(
|
||||
stop_requested.elapsed() >= Duration::from_millis(500),
|
||||
"the checkpoint must have still been in flight when the stop arrived"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
executed.load(Ordering::SeqCst),
|
||||
0,
|
||||
"a stop between checkpoint and permit must never reach the upstream adapter"
|
||||
);
|
||||
let access = TaskAccess {
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_stop_window".to_owned(),
|
||||
};
|
||||
let record = store.read(&access, accepted[0].task_id).await.unwrap();
|
||||
assert_eq!(record.state, TaskState::Failed);
|
||||
assert_eq!(record.attempts, 1, "the durable checkpoint did commit");
|
||||
assert!(record.current_attempt_id.is_some());
|
||||
assert_eq!(
|
||||
record
|
||||
.error
|
||||
.as_ref()
|
||||
.and_then(|error| error.value().get("code"))
|
||||
.and_then(|code| code.as_str()),
|
||||
Some("shutdown_before_dispatch")
|
||||
);
|
||||
|
||||
// Work that was still queued is not consumed by the stopped worker.
|
||||
let queued = store.read(&access, accepted[1].task_id).await.unwrap();
|
||||
assert_eq!(queued.state, TaskState::Queued);
|
||||
let recovered = store
|
||||
.claim_batch("worker_after_stop", 4, 10_000)
|
||||
.await
|
||||
.expect("queued work stays claimable");
|
||||
assert_eq!(recovered.len(), 1);
|
||||
assert_eq!(recovered[0].record.task_id, accepted[1].task_id);
|
||||
}
|
||||
@@ -0,0 +1,822 @@
|
||||
//! Story 4.3 ATDD: fenced durable Task worker leases, recovery and real upstream dispatch.
|
||||
//!
|
||||
//! Split from `durable_tasks.rs` to keep both integration files inside the code-health limit.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Store-level positive control only: PostgreSQL leases/fencing/recovery work without Valkey or a
|
||||
/// task-worker process. This does not exercise TaskWorker startup, a bounded authoritative scan,
|
||||
/// graceful shutdown, outbound dispatch, or runtime recovery timing, which remain unimplemented.
|
||||
#[tokio::test]
|
||||
async fn task_worker_store_lease_recovery_contract() {
|
||||
let registry = common::test_registry().await;
|
||||
let store = PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration");
|
||||
|
||||
// Positive control: no Valkey or worker process is involved; PostgreSQL alone accepts and
|
||||
// leases durable work, preserving the same admission-backed intent.
|
||||
let request = TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_1".to_owned(),
|
||||
platform_api_key_id: None,
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 1,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_900_000_000_000,
|
||||
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
|
||||
approval_reference: None,
|
||||
},
|
||||
scoped_key: "a".repeat(64),
|
||||
input_fingerprint: "b".repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&crank_core::TraceContext::parse(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
// This store-level control exercises lease/fence behavior, not the encrypted acceptance path.
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
};
|
||||
let accepted = store.accept(request).await.expect("PG accepts");
|
||||
let leases = store
|
||||
.claim_batch("worker_1", 4, 10_000)
|
||||
.await
|
||||
.expect("claim");
|
||||
assert_eq!(leases.len(), 1);
|
||||
assert_eq!(leases[0].record.task_id, accepted.task_id);
|
||||
let lease = leases[0].clone();
|
||||
|
||||
// A stale or competing owner cannot win CAS with the issued fencing token.
|
||||
assert!(
|
||||
store
|
||||
.renew_lease("worker_2", lease.record.task_id, lease.fence, 10_000)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.write_progress(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence + 1,
|
||||
TaskEnvelope::try_redacted(&json!({"progress": 1})).unwrap(),
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
// A stale fencing token can never checkpoint dispatch, even under the issuing owner.
|
||||
assert!(
|
||||
store
|
||||
.checkpoint_dispatch(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence + 1,
|
||||
"attempt-stale".to_owned()
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
// The valid owner still commits the dispatch checkpoint before any outbound work.
|
||||
assert!(
|
||||
store
|
||||
.checkpoint_dispatch(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence,
|
||||
"attempt-1".to_owned(),
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.current_attempt_id
|
||||
.as_deref()
|
||||
== Some("attempt-1")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn task_worker_runtime_dispatch_and_recovery_contract() {
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
|
||||
use crank_runtime::{
|
||||
TaskAttemptOutcome, TaskDispatchPermit, TaskHandler, TaskWorker, TaskWorkerConfig,
|
||||
TaskWorkerError,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
let pool = registry.pool().clone();
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
pool.clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let observed = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
// Positive control: PostgreSQL alone accepts the durable Task.
|
||||
let request = TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_runtime".to_owned(),
|
||||
platform_api_key_id: None,
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 1,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_900_000_000_000,
|
||||
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
|
||||
approval_reference: None,
|
||||
},
|
||||
scoped_key: "r".repeat(64),
|
||||
input_fingerprint: "b".repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({"email": "q3"})).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&crank_core::TraceContext::parse(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
};
|
||||
let accepted = store.accept(request).await.expect("PG accepts");
|
||||
let task_id = accepted.task_id;
|
||||
|
||||
struct RuntimeHandler {
|
||||
observed: Arc<AtomicUsize>,
|
||||
}
|
||||
#[async_trait]
|
||||
impl TaskHandler for RuntimeHandler {
|
||||
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
|
||||
Ok(())
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
_lease: &TaskLease,
|
||||
_attempt_id: &str,
|
||||
_cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
_permit: TaskDispatchPermit,
|
||||
) -> TaskAttemptOutcome {
|
||||
self.observed.fetch_add(1, Ordering::SeqCst);
|
||||
TaskAttemptOutcome::Completed(serde_json::json!({"ok": true}))
|
||||
}
|
||||
}
|
||||
let handler: Arc<dyn TaskHandler> = Arc::new(RuntimeHandler {
|
||||
observed: observed.clone(),
|
||||
});
|
||||
|
||||
// PG-only authoritative dispatch: no Valkey or upstream process is involved.
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-1".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = Duration::from_millis(100);
|
||||
config.renewal_interval = Duration::from_millis(1_000);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(5);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
let access = TaskAccess {
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_runtime".to_owned(),
|
||||
};
|
||||
let mut state = None;
|
||||
for _ in 0..100 {
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
if let Ok(record) = store.read(&access, task_id).await {
|
||||
state = Some(record.state);
|
||||
if record.state == TaskState::Completed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
|
||||
|
||||
assert_eq!(state, Some(TaskState::Completed));
|
||||
assert_eq!(
|
||||
observed.load(Ordering::SeqCst),
|
||||
1,
|
||||
"exactly one outbound attempt"
|
||||
);
|
||||
// Terminal state is not duplicated.
|
||||
let record = store.read(&access, task_id).await.expect("terminal read");
|
||||
assert_eq!(record.state, TaskState::Completed);
|
||||
|
||||
// Bounded shutdown with no fresh work returns promptly.
|
||||
let mut idle_config = TaskWorkerConfig::default();
|
||||
idle_config.owner = "runtime-worker-idle".to_owned();
|
||||
idle_config.poll_interval = Duration::from_millis(50);
|
||||
idle_config.shutdown_timeout = Duration::from_secs(2);
|
||||
let idle_worker = TaskWorker::new(
|
||||
store.clone(),
|
||||
handler_for_no_work(store.clone()),
|
||||
idle_config,
|
||||
)
|
||||
.expect("valid idle worker");
|
||||
idle_worker.stop();
|
||||
let started = std::time::Instant::now();
|
||||
idle_worker.run().await.expect("idle worker stops");
|
||||
assert!(started.elapsed() <= Duration::from_secs(30));
|
||||
}
|
||||
|
||||
/// Story 4.3 ATDD: failed pre-dispatch revalidation finishes the attempt without any outbound call.
|
||||
#[tokio::test]
|
||||
async fn task_worker_revalidation_denial_never_dispatches() {
|
||||
use crank_core::{TaskAccess, TaskError, TaskLease, TaskOwnerKind};
|
||||
use crank_runtime::{
|
||||
TaskAttemptOutcome, TaskDispatchPermit, TaskHandler, TaskWorker, TaskWorkerConfig,
|
||||
TaskWorkerError,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let request = TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_denied".to_owned(),
|
||||
platform_api_key_id: None,
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 1,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_900_000_000_000,
|
||||
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
|
||||
approval_reference: None,
|
||||
},
|
||||
scoped_key: "d".repeat(64),
|
||||
input_fingerprint: "b".repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&crank_core::TraceContext::parse(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
};
|
||||
let accepted = store.accept(request).await.expect("PG accepts");
|
||||
|
||||
struct DeniedHandler {
|
||||
observed: Arc<AtomicUsize>,
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl TaskHandler for DeniedHandler {
|
||||
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
|
||||
Err(TaskWorkerError::Store(TaskError::invalid(
|
||||
"publication or credential revoked",
|
||||
)))
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
_lease: &TaskLease,
|
||||
_attempt_id: &str,
|
||||
_cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
_permit: TaskDispatchPermit,
|
||||
) -> TaskAttemptOutcome {
|
||||
self.observed.fetch_add(1, Ordering::SeqCst);
|
||||
TaskAttemptOutcome::Completed(json!({"ok": true}))
|
||||
}
|
||||
}
|
||||
let observed = Arc::new(AtomicUsize::new(0));
|
||||
let handler: Arc<dyn TaskHandler> = Arc::new(DeniedHandler {
|
||||
observed: observed.clone(),
|
||||
});
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-denied".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = Duration::from_millis(50);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(5);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
let access = TaskAccess {
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_denied".to_owned(),
|
||||
};
|
||||
let mut state = None;
|
||||
for _ in 0..100 {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
if let Ok(record) = store.read(&access, accepted.task_id).await {
|
||||
state = Some(record.state);
|
||||
if record.state == TaskState::Failed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
|
||||
|
||||
assert_eq!(state, Some(TaskState::Failed));
|
||||
assert_eq!(
|
||||
observed.load(Ordering::SeqCst),
|
||||
0,
|
||||
"invalidated work never reaches the upstream"
|
||||
);
|
||||
}
|
||||
|
||||
fn handler_for_no_work(
|
||||
_store: std::sync::Arc<PostgresTaskStore>,
|
||||
) -> std::sync::Arc<dyn crank_runtime::TaskHandler> {
|
||||
struct NoWorkHandler;
|
||||
#[async_trait::async_trait]
|
||||
impl crank_runtime::TaskHandler for NoWorkHandler {
|
||||
async fn revalidate(
|
||||
&self,
|
||||
_lease: &crank_core::TaskLease,
|
||||
) -> Result<(), crank_runtime::TaskWorkerError> {
|
||||
Ok(())
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
_lease: &crank_core::TaskLease,
|
||||
_attempt_id: &str,
|
||||
_cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
|
||||
_permit: crank_runtime::TaskDispatchPermit,
|
||||
) -> crank_runtime::TaskAttemptOutcome {
|
||||
crank_runtime::TaskAttemptOutcome::Completed(serde_json::json!({"ok": true}))
|
||||
}
|
||||
}
|
||||
std::sync::Arc::new(NoWorkHandler)
|
||||
}
|
||||
|
||||
/// Story 4.3 end-to-end ATDD: the production durable-Task handler, the real runtime executor and a
|
||||
/// real local HTTP upstream observe exactly one dispatch after the fenced checkpoint, while the
|
||||
/// accepted parent trace is continued on the wire.
|
||||
#[tokio::test]
|
||||
async fn task_worker_dispatches_one_real_upstream_attempt_with_parent_trace() {
|
||||
use axum::Router;
|
||||
use axum::http::{HeaderMap, StatusCode};
|
||||
use axum::routing::post;
|
||||
use crank_community_mcp::TaskWorkerHandler;
|
||||
use crank_core::{AgentOperationBinding, PlatformApiKeyScope};
|
||||
use crank_runtime::{SecretCrypto, TaskWorker, TaskWorkerConfig};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let seen_traceparent = Arc::new(Mutex::new(None::<String>));
|
||||
|
||||
let app = Router::new().route(
|
||||
"/crm/leads",
|
||||
post({
|
||||
let calls = Arc::clone(&calls);
|
||||
let seen = Arc::clone(&seen_traceparent);
|
||||
move |headers: HeaderMap| {
|
||||
let calls = Arc::clone(&calls);
|
||||
let seen = Arc::clone(&seen);
|
||||
async move {
|
||||
calls.fetch_add(1, Ordering::SeqCst);
|
||||
if let Some(value) = headers
|
||||
.get("traceparent")
|
||||
.and_then(|value| value.to_str().ok())
|
||||
{
|
||||
*seen.lock().unwrap() = Some(value.to_owned());
|
||||
}
|
||||
(StatusCode::OK, axum::Json(json!({"id": "lead-1"})))
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
let upstream = format!("http://{address}");
|
||||
|
||||
let mut operation = common::test_operation(&upstream, TOOL_NAME);
|
||||
operation.execution_config.execution_policy = Some(background_policy());
|
||||
registry
|
||||
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
|
||||
.await
|
||||
.unwrap();
|
||||
publish(®istry, &operation.id, 1).await;
|
||||
common::publish_agent_with_bindings(
|
||||
®istry,
|
||||
AGENT_SLUG,
|
||||
vec![AgentOperationBinding {
|
||||
operation_id: operation.id.clone(),
|
||||
operation_version: 1,
|
||||
tool_name: operation.name.clone(),
|
||||
tool_title: operation.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"task-worker-e2e",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let correlation = unsampled_correlation();
|
||||
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
|
||||
let observed_at = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
|
||||
epoch: crypto.master_key_epoch(),
|
||||
fingerprint: crypto.master_key_fingerprint(),
|
||||
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
|
||||
observed_at: &observed_at,
|
||||
})
|
||||
.await
|
||||
.expect("test master-key identity registered");
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let tasks = TaskApplication::new(
|
||||
registry.clone(),
|
||||
Duration::ZERO,
|
||||
Arc::new(InMemoryCoordinationStateStore::default()),
|
||||
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
|
||||
);
|
||||
let acceptance = tasks
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
"worker-e2e-key",
|
||||
&json!({"email": "user@example.com"}),
|
||||
&correlation,
|
||||
)
|
||||
.await
|
||||
.expect("durable acceptance with executable input");
|
||||
|
||||
let runtime = crank_runtime::community_with_outbound_policy(
|
||||
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
|
||||
)
|
||||
.build();
|
||||
let handler = Arc::new(TaskWorkerHandler::from_application(
|
||||
&tasks,
|
||||
registry.clone(),
|
||||
runtime,
|
||||
crypto,
|
||||
));
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-e2e".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = Duration::from_millis(50);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(5);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
let mut state = None;
|
||||
for _ in 0..200 {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
let current: Option<String> =
|
||||
sqlx::query_scalar("select state from tasks where task_id = $1")
|
||||
.bind(acceptance.task_id.as_uuid())
|
||||
.fetch_optional(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
state = current;
|
||||
if state.as_deref() == Some("completed") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
|
||||
server.abort();
|
||||
|
||||
assert_eq!(state.as_deref(), Some("completed"));
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1, "exactly one upstream call");
|
||||
assert_eq!(
|
||||
seen_traceparent.lock().unwrap().as_deref(),
|
||||
Some(correlation.trace_context().traceparent()),
|
||||
"the durable attempt continues the accepted parent trace"
|
||||
);
|
||||
}
|
||||
|
||||
/// Story 4.3 end-to-end ATDD: an ambiguous post-checkpoint upstream failure is persisted as
|
||||
/// `outcome_unknown` (not a retryable failure) and is not redispatched automatically.
|
||||
#[tokio::test]
|
||||
async fn task_worker_ambiguous_upstream_error_becomes_outcome_unknown() {
|
||||
use axum::Router;
|
||||
use axum::http::StatusCode;
|
||||
use axum::routing::post;
|
||||
use crank_community_mcp::TaskWorkerHandler;
|
||||
use crank_core::{AgentOperationBinding, PlatformApiKeyScope};
|
||||
use crank_runtime::{SecretCrypto, TaskWorker, TaskWorkerConfig};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
let upstream_app = Router::new().route(
|
||||
"/crm/leads",
|
||||
post(|| async { (StatusCode::SERVICE_UNAVAILABLE, "upstream unavailable") }),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
|
||||
let upstream = format!("http://{address}");
|
||||
|
||||
let mut operation = common::test_operation(&upstream, TOOL_NAME);
|
||||
operation.execution_config.execution_policy = Some(background_policy());
|
||||
registry
|
||||
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
|
||||
.await
|
||||
.unwrap();
|
||||
publish(®istry, &operation.id, 1).await;
|
||||
common::publish_agent_with_bindings(
|
||||
®istry,
|
||||
AGENT_SLUG,
|
||||
vec![AgentOperationBinding {
|
||||
operation_id: operation.id.clone(),
|
||||
operation_version: 1,
|
||||
tool_name: operation.name.clone(),
|
||||
tool_title: operation.tool_description.title.clone(),
|
||||
tool_description_override: None,
|
||||
enabled: true,
|
||||
}],
|
||||
)
|
||||
.await;
|
||||
|
||||
let caller = principal(
|
||||
®istry,
|
||||
"task-worker-uncertain",
|
||||
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
|
||||
)
|
||||
.await;
|
||||
let correlation = unsampled_correlation();
|
||||
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
|
||||
let observed_at = OffsetDateTime::now_utc();
|
||||
registry
|
||||
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
|
||||
epoch: crypto.master_key_epoch(),
|
||||
fingerprint: crypto.master_key_fingerprint(),
|
||||
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
|
||||
observed_at: &observed_at,
|
||||
})
|
||||
.await
|
||||
.expect("test master-key identity registered");
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let tasks = TaskApplication::new(
|
||||
registry.clone(),
|
||||
Duration::ZERO,
|
||||
Arc::new(InMemoryCoordinationStateStore::default()),
|
||||
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
|
||||
);
|
||||
let acceptance = tasks
|
||||
.accept_background(
|
||||
&caller,
|
||||
TOOL_NAME,
|
||||
None,
|
||||
"worker-uncertain-key",
|
||||
&json!({"email": "user@example.com"}),
|
||||
&correlation,
|
||||
)
|
||||
.await
|
||||
.expect("durable acceptance with executable input");
|
||||
|
||||
let runtime = crank_runtime::community_with_outbound_policy(
|
||||
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
|
||||
)
|
||||
.build();
|
||||
let handler = Arc::new(TaskWorkerHandler::from_application(
|
||||
&tasks,
|
||||
registry.clone(),
|
||||
runtime,
|
||||
crypto,
|
||||
));
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-uncertain".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = Duration::from_millis(50);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(5);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
let mut state = None;
|
||||
for _ in 0..200 {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
let current: Option<String> =
|
||||
sqlx::query_scalar("select state from tasks where task_id = $1")
|
||||
.bind(acceptance.task_id.as_uuid())
|
||||
.fetch_optional(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
state = current;
|
||||
if state.as_deref() == Some("outcome_unknown") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
|
||||
server.abort();
|
||||
|
||||
assert_eq!(state.as_deref(), Some("outcome_unknown"));
|
||||
// No automatic retry: another authoritative scan never redispaches the uncertain attempt.
|
||||
assert!(
|
||||
store
|
||||
.claim_batch("worker_after", 4, 10_000)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
/// Story 4.3 ATDD: graceful stop stops new claims but lets an active attempt reach its fenced
|
||||
/// terminal commit within the 30s budget.
|
||||
#[tokio::test]
|
||||
async fn task_worker_graceful_stop_drains_active_attempt() {
|
||||
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
|
||||
use crank_runtime::{
|
||||
TaskAttemptOutcome, TaskDispatchPermit, TaskHandler, TaskWorker, TaskWorkerConfig,
|
||||
TaskWorkerError,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
let registry = common::test_registry().await;
|
||||
let store = Arc::new(
|
||||
PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: 8,
|
||||
},
|
||||
Default::default(),
|
||||
)
|
||||
.expect("valid task store configuration"),
|
||||
);
|
||||
let request = TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_drain".to_owned(),
|
||||
platform_api_key_id: None,
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 1,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_900_000_000_000,
|
||||
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
|
||||
approval_reference: None,
|
||||
},
|
||||
scoped_key: "s".repeat(64),
|
||||
input_fingerprint: "d".repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&crank_core::TraceContext::parse(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
|
||||
)
|
||||
.unwrap(),
|
||||
),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
};
|
||||
let accepted = store.accept(request).await.expect("PG accepts");
|
||||
|
||||
struct SlowHandler {
|
||||
started: Arc<AtomicUsize>,
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl TaskHandler for SlowHandler {
|
||||
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
|
||||
Ok(())
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
_lease: &TaskLease,
|
||||
_attempt_id: &str,
|
||||
_cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
_permit: TaskDispatchPermit,
|
||||
) -> TaskAttemptOutcome {
|
||||
self.started.fetch_add(1, Ordering::SeqCst);
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
TaskAttemptOutcome::Completed(json!({"ok": true}))
|
||||
}
|
||||
}
|
||||
let started = Arc::new(AtomicUsize::new(0));
|
||||
let handler: Arc<dyn TaskHandler> = Arc::new(SlowHandler {
|
||||
started: started.clone(),
|
||||
});
|
||||
let mut config = TaskWorkerConfig::default();
|
||||
config.owner = "runtime-worker-drain".to_owned();
|
||||
config.batch_limit = 1;
|
||||
config.lease_ms = 10_000;
|
||||
config.poll_interval = Duration::from_millis(50);
|
||||
config.max_inflight = 1;
|
||||
config.shutdown_timeout = Duration::from_secs(20);
|
||||
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
|
||||
let stop_flag = worker.stop_flag();
|
||||
let worker_task = tokio::spawn(async move { worker.run().await });
|
||||
|
||||
for _ in 0..100 {
|
||||
if started.load(Ordering::SeqCst) > 0 {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
assert_eq!(started.load(Ordering::SeqCst), 1, "attempt started");
|
||||
let stop_requested = std::time::Instant::now();
|
||||
stop_flag.store(true, Ordering::SeqCst);
|
||||
tokio::time::timeout(Duration::from_secs(30), worker_task)
|
||||
.await
|
||||
.expect("worker stops inside the 30s budget")
|
||||
.expect("worker exits cleanly")
|
||||
.expect("worker reports no error");
|
||||
assert!(stop_requested.elapsed() <= Duration::from_secs(30));
|
||||
|
||||
let access = TaskAccess {
|
||||
workspace_id: common::test_workspace_id().to_string(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_drain".to_owned(),
|
||||
};
|
||||
let record = store.read(&access, accepted.task_id).await.unwrap();
|
||||
assert_eq!(record.state, TaskState::Completed);
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
FROM node:22-alpine AS build
|
||||
|
||||
# The CI host uplink drops TLS 1.3 ClientHello carrying the hybrid
|
||||
# X25519MLKEM768 key share; pin the npm stage to TLS 1.2 until that network
|
||||
# defect is fixed. Certificates, SRI and the lockfile are unaffected.
|
||||
ENV NODE_OPTIONS=--tls-max-v1.2
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY apps/ui/package.json apps/ui/package-lock.json ./
|
||||
|
||||
@@ -272,7 +272,10 @@ fn record_unknown_approval_access_denial(reason: &'static str) {
|
||||
);
|
||||
}
|
||||
|
||||
fn allows_scope(scopes: &[PlatformApiKeyScope], required_scope: PlatformApiKeyScope) -> bool {
|
||||
pub(super) fn allows_scope(
|
||||
scopes: &[PlatformApiKeyScope],
|
||||
required_scope: PlatformApiKeyScope,
|
||||
) -> bool {
|
||||
match required_scope {
|
||||
PlatformApiKeyScope::Read => scopes.iter().any(|scope| {
|
||||
matches!(
|
||||
|
||||
@@ -51,13 +51,15 @@ mod auth_resolution;
|
||||
mod dispatch;
|
||||
mod endpoint;
|
||||
mod legacy_dispatch;
|
||||
|
||||
mod task_worker;
|
||||
mod tasks;
|
||||
use self::approvals::{
|
||||
approve_request, deny_request, get_approval_request, list_pending_approvals,
|
||||
};
|
||||
pub(super) use self::auth_resolution::resolve_operation_auth;
|
||||
use self::endpoint::{mcp_delete, mcp_get, mcp_post};
|
||||
|
||||
pub use self::task_worker::TaskWorkerHandler;
|
||||
pub use self::tasks::{TaskApplication, TaskApplicationError, TaskPrincipal};
|
||||
// Test-only import surface: the BUILD-internal `app/tests.rs` module reaches these two
|
||||
// through `super`, and the plain lib build does not use them (same pattern as
|
||||
// `#[cfg(test)] use invocation_history::observe_invocation_history_outcome;` above).
|
||||
@@ -92,6 +94,9 @@ pub(super) struct AppState {
|
||||
pub(super) signal_streams: Arc<crate::protocol::StreamRegistry>,
|
||||
/// Story 3.4: the bounded table of the `tools/call` requests a legacy client can still cancel.
|
||||
pub(super) in_flight: Arc<self::lifecycle::InFlightCalls>,
|
||||
/// Story 4.2: durable background Tasks over the same registry pool. The public wire stays
|
||||
/// synchronous/unsupported; readiness depends on this mandatory store.
|
||||
tasks: Arc<TaskApplication>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -137,6 +142,7 @@ pub fn build_app(
|
||||
DEFAULT_MAX_CONCURRENT_SESSIONS,
|
||||
false,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -192,6 +198,7 @@ pub fn build_app_with_background_workers_and_limits(
|
||||
max_concurrent_sessions,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -227,6 +234,41 @@ pub fn build_app_with_artifacts(
|
||||
max_concurrent_sessions,
|
||||
true,
|
||||
Some(artifact_store),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
/// Production launcher entry point with the durable Task application entry constructed by the
|
||||
/// process composition root from the same registry pool and admission limits as the runtime.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn build_app_with_artifacts_and_tasks(
|
||||
registry: PostgresRegistry,
|
||||
refresh_interval: Duration,
|
||||
public_base_url: Option<String>,
|
||||
secret_crypto: SecretCrypto,
|
||||
runtime: RuntimeExecutor,
|
||||
api_rate_limiter: RequestRateLimiter,
|
||||
coordination_store: Arc<dyn CoordinationStateStore>,
|
||||
sessions: SharedSessionStore,
|
||||
credential_verifier: SharedMachineCredentialVerifier,
|
||||
max_concurrent_sessions: usize,
|
||||
artifact_store: crank_artifacts::ArtifactStore,
|
||||
tasks: Arc<TaskApplication>,
|
||||
) -> Router {
|
||||
build_app_inner(
|
||||
registry,
|
||||
refresh_interval,
|
||||
public_base_url,
|
||||
secret_crypto,
|
||||
runtime,
|
||||
api_rate_limiter,
|
||||
coordination_store,
|
||||
sessions,
|
||||
credential_verifier,
|
||||
max_concurrent_sessions,
|
||||
true,
|
||||
Some(artifact_store),
|
||||
Some(tasks),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -244,8 +286,23 @@ fn build_app_inner(
|
||||
max_concurrent_sessions: usize,
|
||||
start_background_workers: bool,
|
||||
artifact_store: Option<crank_artifacts::ArtifactStore>,
|
||||
tasks: Option<Arc<TaskApplication>>,
|
||||
) -> Router {
|
||||
let session_metrics = ActiveSessionMetrics::start(Arc::clone(&sessions));
|
||||
let tasks = tasks.unwrap_or_else(|| {
|
||||
let store = crank_registry::PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
crank_registry::AdmissionLimits::default(),
|
||||
crank_core::AdmissionSchedulerConfig::default(),
|
||||
)
|
||||
.expect("default admission limits and scheduler weights are valid");
|
||||
Arc::new(TaskApplication::new(
|
||||
registry.clone(),
|
||||
refresh_interval,
|
||||
Arc::clone(&coordination_store),
|
||||
crank_runtime::TaskService::new(Arc::new(store)),
|
||||
))
|
||||
});
|
||||
let state = Arc::new(AppState {
|
||||
registry: registry.clone(),
|
||||
catalog: PublishedToolCatalog::new(registry, refresh_interval, coordination_store),
|
||||
@@ -263,6 +320,7 @@ fn build_app_inner(
|
||||
in_flight: Arc::new(lifecycle::InFlightCalls::new(
|
||||
lifecycle::MAX_IN_FLIGHT_CALLS,
|
||||
)),
|
||||
tasks,
|
||||
});
|
||||
if start_background_workers {
|
||||
spawn_approval_recovery(Arc::clone(&state));
|
||||
@@ -315,11 +373,17 @@ async fn readiness(State(state): State<Arc<AppState>>) -> Response {
|
||||
// Story 4.1: admission state is a mandatory dependency of every execution, so readiness must
|
||||
// reflect it rather than reporting ready on a bare `select 1`.
|
||||
let admission_ready = state.registry.execution_admission_health().await.is_ok();
|
||||
if postgres_ready && admission_ready {
|
||||
// Story 4.2: the durable Task store is mandatory for a durable acceptance acknowledgement.
|
||||
let durable_tasks_ready = state.tasks.health().await.is_ok();
|
||||
if postgres_ready && admission_ready && durable_tasks_ready {
|
||||
return Json(json!({
|
||||
"service": "mcp-server",
|
||||
"status": "ready",
|
||||
"checks": { "postgres": "ready", "execution_admission": "ready" }
|
||||
"checks": {
|
||||
"postgres": "ready",
|
||||
"execution_admission": "ready",
|
||||
"durable_tasks": "ready"
|
||||
}
|
||||
}))
|
||||
.into_response();
|
||||
}
|
||||
@@ -336,6 +400,14 @@ async fn readiness(State(state): State<Arc<AppState>>) -> Response {
|
||||
"not_ready"
|
||||
}),
|
||||
);
|
||||
checks.insert(
|
||||
"durable_tasks".to_owned(),
|
||||
json!(if durable_tasks_ready {
|
||||
"ready"
|
||||
} else {
|
||||
"not_ready"
|
||||
}),
|
||||
);
|
||||
(
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
Json(json!({
|
||||
|
||||
@@ -19,18 +19,31 @@ pub(crate) async fn resolve_operation_auth(
|
||||
workspace_id: &crank_core::WorkspaceId,
|
||||
execution_config: &crank_core::ExecutionConfig,
|
||||
) -> Result<Option<ResolvedAuth>, RuntimeError> {
|
||||
if execution_config.auth_profile_ref.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
let span = Stage::AuthResolve.span();
|
||||
let result = resolve_runtime_auth_for_task(
|
||||
resolve_operation_auth_with(
|
||||
&state.registry,
|
||||
&state.secret_crypto,
|
||||
workspace_id,
|
||||
execution_config,
|
||||
)
|
||||
.instrument(span.clone())
|
||||
.await;
|
||||
.await
|
||||
}
|
||||
|
||||
/// Same resolution for callers that hold the authoritative registry/secret-crypto pair without the
|
||||
/// HTTP application state (the durable Task worker).
|
||||
pub(crate) async fn resolve_operation_auth_with(
|
||||
registry: &PostgresRegistry,
|
||||
secret_crypto: &SecretCrypto,
|
||||
workspace_id: &crank_core::WorkspaceId,
|
||||
execution_config: &crank_core::ExecutionConfig,
|
||||
) -> Result<Option<ResolvedAuth>, RuntimeError> {
|
||||
if execution_config.auth_profile_ref.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
let span = Stage::AuthResolve.span();
|
||||
let result =
|
||||
resolve_runtime_auth_for_task(registry, secret_crypto, workspace_id, execution_config)
|
||||
.instrument(span.clone())
|
||||
.await;
|
||||
match &result {
|
||||
Ok(_) => StageOutcome::Success.record(&span),
|
||||
Err(_) => {
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
//! Application-level durable Task handler for Story 4.3.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
AgentId, ExecutionOrigin, MachineAccessMode, OperationSecurityLevel, PlatformApiKeyScope,
|
||||
TaskError, TaskLease, TaskOwnerKind, TraceContext, VerifiedMachineCredential, WorkspaceId,
|
||||
current_unix_ms,
|
||||
};
|
||||
use crank_registry::PostgresRegistry;
|
||||
use crank_registry::PublishedAgentTool;
|
||||
use crank_runtime::{
|
||||
ExecutionAuthorization, ExecutionRequestError, RuntimeExecutionRequest, RuntimeExecutor,
|
||||
SecretCrypto, TaskDispatchPermit, TaskHandler, TaskService, TaskWorkerError,
|
||||
};
|
||||
|
||||
use super::TaskApplication;
|
||||
use super::auth_resolution::resolve_operation_auth_with;
|
||||
use super::tool_resolution::runtime_operation;
|
||||
use crate::access::{allows_scope, credential_allows_security_level};
|
||||
use crate::catalog::PublishedToolCatalog;
|
||||
|
||||
pub struct TaskWorkerHandler {
|
||||
registry: PostgresRegistry,
|
||||
catalog: PublishedToolCatalog,
|
||||
runtime: RuntimeExecutor,
|
||||
secret_crypto: SecretCrypto,
|
||||
service: TaskService,
|
||||
}
|
||||
|
||||
impl TaskWorkerHandler {
|
||||
/// Production construction from the same application entry, registry and runtime the MCP
|
||||
/// process serves requests with. The worker needs no HTTP state, only these authoritative
|
||||
/// dependencies.
|
||||
pub fn from_application(
|
||||
tasks: &TaskApplication,
|
||||
registry: PostgresRegistry,
|
||||
runtime: RuntimeExecutor,
|
||||
secret_crypto: SecretCrypto,
|
||||
) -> Self {
|
||||
Self {
|
||||
registry,
|
||||
catalog: tasks.catalog(),
|
||||
runtime,
|
||||
secret_crypto,
|
||||
service: tasks.task_service(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Authoritative pre-dispatch authorization of one leased Task (Story 4.3).
|
||||
///
|
||||
/// Resolves the exact pinned Published Version in the current publication, repeats the approval
|
||||
/// policy and revalidates the typed persisted Platform API key identity against the registry:
|
||||
/// the key must still be active, unexpired and bound to this workspace/agent, and its exact
|
||||
/// scopes/security level must still authorize the operation. Any denial is a typed error, so the
|
||||
/// worker finishes the attempt without dispatch (fail closed before any network use).
|
||||
///
|
||||
/// An administrator Task has no authoritative Task revalidation API in the registry, so it stays
|
||||
/// fail-closed here instead of inventing authority.
|
||||
async fn authorize(&self, lease: &TaskLease) -> Result<PublishedAgentTool, TaskError> {
|
||||
if lease.record.owner_kind != TaskOwnerKind::Agent {
|
||||
return Err(TaskError::invalid(
|
||||
"admin task authority revalidation is not implemented",
|
||||
));
|
||||
}
|
||||
let Some(key_id) = lease.record.platform_api_key_id.as_ref() else {
|
||||
return Err(TaskError::invalid("task authority reference is missing"));
|
||||
};
|
||||
|
||||
let workspace_id = WorkspaceId::new(lease.record.workspace_id.as_str());
|
||||
let agent_id = AgentId::new(lease.record.owner_id.as_str());
|
||||
let workspace = self
|
||||
.registry
|
||||
.get_workspace(&workspace_id)
|
||||
.await
|
||||
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?
|
||||
.ok_or_else(|| TaskError::invalid("task workspace no longer exists"))?;
|
||||
let agents = self
|
||||
.registry
|
||||
.list_agents(&workspace_id)
|
||||
.await
|
||||
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?;
|
||||
let agent = agents
|
||||
.into_iter()
|
||||
.find(|agent| agent.id.as_str() == lease.record.owner_id.as_str())
|
||||
.ok_or_else(|| TaskError::invalid("task agent no longer exists"))?;
|
||||
|
||||
let catalog = self
|
||||
.catalog
|
||||
.get_catalog(&workspace.workspace.slug, &agent.slug)
|
||||
.await
|
||||
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?;
|
||||
let tool = catalog
|
||||
.tools
|
||||
.into_iter()
|
||||
.find(|tool| {
|
||||
tool.operation.id.as_str() == lease.record.operation_id.as_str()
|
||||
&& tool.operation.version == lease.record.pins.operation_version
|
||||
})
|
||||
.ok_or_else(|| TaskError::invalid("task published operation pin no longer matches"))?;
|
||||
|
||||
if tool.workspace_id.as_str() != lease.record.workspace_id.as_str() {
|
||||
return Err(TaskError::invalid("task workspace binding mismatch"));
|
||||
}
|
||||
if tool.agent_id.as_str() != lease.record.owner_id.as_str() {
|
||||
return Err(TaskError::invalid("task agent binding mismatch"));
|
||||
}
|
||||
if tool
|
||||
.operation
|
||||
.execution_config
|
||||
.approval_policy
|
||||
.as_ref()
|
||||
.is_some_and(|policy| policy.required)
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"missing approval proof for task operation",
|
||||
));
|
||||
}
|
||||
|
||||
// Authoritative revalidation of the exact immutable key identity. A revoked, expired,
|
||||
// foreign-workspace or foreign-agent key is absent here and never reaches the network.
|
||||
let key = self
|
||||
.registry
|
||||
.revalidate_platform_api_key(&workspace_id, &agent_id, key_id)
|
||||
.await
|
||||
.map_err(|_| TaskError::unavailable("task authority store unavailable"))?
|
||||
.ok_or_else(|| TaskError::invalid("task authority is no longer active"))?;
|
||||
let credential = VerifiedMachineCredential {
|
||||
machine_access_mode: MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: OperationSecurityLevel::Standard,
|
||||
scopes: key.api_key.scopes.clone(),
|
||||
platform_api_key_id: Some(key.api_key.id.clone()),
|
||||
};
|
||||
if !allows_scope(&credential.scopes, PlatformApiKeyScope::Write) {
|
||||
return Err(TaskError::invalid("task authority scope is insufficient"));
|
||||
}
|
||||
if !credential_allows_security_level(&credential, tool.operation.security_level) {
|
||||
return Err(TaskError::invalid(
|
||||
"task authority security level is insufficient",
|
||||
));
|
||||
}
|
||||
Ok(tool)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TaskHandler for TaskWorkerHandler {
|
||||
async fn revalidate(&self, lease: &TaskLease) -> Result<(), TaskWorkerError> {
|
||||
self.authorize(lease)
|
||||
.await
|
||||
.map_err(TaskWorkerError::Store)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
lease: &TaskLease,
|
||||
_attempt_id: &str,
|
||||
cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
permit: TaskDispatchPermit,
|
||||
) -> crank_runtime::TaskAttemptOutcome {
|
||||
use crank_runtime::TaskAttemptOutcome;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
// The lease may have been lost between the checkpoint and the outbound call; never dispatch
|
||||
// on behalf of a worker that no longer owns the Task.
|
||||
if cancelled.load(Ordering::Acquire) {
|
||||
return TaskAttemptOutcome::OutcomeUnknown;
|
||||
}
|
||||
|
||||
let tool = match self.authorize(lease).await {
|
||||
Ok(tool) => tool,
|
||||
Err(error) => {
|
||||
return TaskAttemptOutcome::Failed(serde_json::json!({"code": error.to_string()}));
|
||||
}
|
||||
};
|
||||
|
||||
let input = match self.service.executable_input(lease).await {
|
||||
Ok(input) => input,
|
||||
Err(error) => {
|
||||
return TaskAttemptOutcome::Failed(serde_json::json!({"code": error.to_string()}));
|
||||
}
|
||||
};
|
||||
|
||||
let operation = runtime_operation(&tool);
|
||||
let now_ms = current_unix_ms();
|
||||
let deadline_ms = lease.record.pins.execution_deadline_unix_ms;
|
||||
if deadline_ms <= now_ms {
|
||||
return TaskAttemptOutcome::Failed(
|
||||
serde_json::json!({"code": "task deadline elapsed"}),
|
||||
);
|
||||
}
|
||||
let deadline = Instant::now()
|
||||
+ Duration::from_millis((deadline_ms - now_ms).min(i64::MAX as i64) as u64);
|
||||
let workspace_id = WorkspaceId::new(lease.record.workspace_id.as_str());
|
||||
let agent_id = AgentId::new(lease.record.owner_id.as_str());
|
||||
let resolved_auth = match resolve_operation_auth_with(
|
||||
&self.registry,
|
||||
&self.secret_crypto,
|
||||
&workspace_id,
|
||||
&operation.execution_config,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(auth) => auth,
|
||||
Err(error) => {
|
||||
return TaskAttemptOutcome::Failed(
|
||||
serde_json::json!({ "code": error.to_string() }),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// Continue the parent trace persisted with the accepted Task; the worker's attempt span is a
|
||||
// child of it, and the executor records the attempt identity. Trace data stays out of labels.
|
||||
let trace_context = match TraceContext::parse(&lease.record.support.traceparent) {
|
||||
Ok(context) => context,
|
||||
Err(_) => {
|
||||
return TaskAttemptOutcome::Failed(
|
||||
serde_json::json!({ "code": "task trace parent is invalid" }),
|
||||
);
|
||||
}
|
||||
};
|
||||
let request_context = crank_runtime::RuntimeRequestContext::new(
|
||||
lease.record.support.request_id.clone(),
|
||||
trace_context,
|
||||
)
|
||||
.with_metering_context(
|
||||
workspace_id.clone(),
|
||||
Some(agent_id.clone()),
|
||||
crank_core::InvocationSource::AgentToolCall,
|
||||
);
|
||||
|
||||
let request = match RuntimeExecutionRequest::try_new(
|
||||
&workspace_id,
|
||||
ExecutionOrigin::AgentSnapshot,
|
||||
Some(&agent_id),
|
||||
&operation,
|
||||
&input,
|
||||
ExecutionAuthorization::Authorized,
|
||||
resolved_auth.as_ref(),
|
||||
&request_context,
|
||||
deadline,
|
||||
) {
|
||||
Ok(request) => request,
|
||||
Err(error) => {
|
||||
return TaskAttemptOutcome::Failed(match error {
|
||||
ExecutionRequestError::InvalidOrigin => {
|
||||
serde_json::json!({"code": "invalid execution origin"})
|
||||
}
|
||||
ExecutionRequestError::InvalidOperationVersion => {
|
||||
serde_json::json!({"code": "invalid operation version"})
|
||||
}
|
||||
ExecutionRequestError::DeadlineElapsed => {
|
||||
serde_json::json!({"code": "task deadline elapsed"})
|
||||
}
|
||||
ExecutionRequestError::InvalidMeteringScope => {
|
||||
serde_json::json!({"code": "invalid metering scope"})
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if cancelled.load(Ordering::Acquire) {
|
||||
return TaskAttemptOutcome::OutcomeUnknown;
|
||||
}
|
||||
match self.runtime.execute_task_outcome(request, permit).await {
|
||||
Ok(success) => TaskAttemptOutcome::Completed(success.output),
|
||||
Err(failure) => {
|
||||
// An ambiguous outbound failure is recorded as uncertainty, never as a retryable
|
||||
// failure: the durable checkpoint already exists, so Story 4.4 may reconcile it.
|
||||
if failure.outcome_certainty() == crank_core::OutcomeCertainty::OutcomeUnknown {
|
||||
return TaskAttemptOutcome::OutcomeUnknown;
|
||||
}
|
||||
TaskAttemptOutcome::Failed(
|
||||
serde_json::json!({ "code": format!("{:?}", failure.error_code()) }),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
//! Application entry of durable background Tasks (Story 4.2, FR-22/FR-23).
|
||||
//!
|
||||
//! The entry takes an already authenticated principal (a verified machine credential, or an
|
||||
//! administrator identity verified by the calling admin surface), resolves the exact published Tool,
|
||||
//! repeats the caller's authorization, pins the Published Version/policy/deadline and delegates the
|
||||
//! durable acceptance to the runtime Task service. The repository owns the transaction, so the
|
||||
//! returned Task ID exists only after Task + dispatch intent + queue slot committed.
|
||||
//!
|
||||
//! Deliberately not wired: the public MCP `tools/call`/Tasks wire (Story 4.7) and the worker/leases
|
||||
//! (Story 4.3). The existing synchronous/unsupported `tools/call` path is unchanged, and this entry
|
||||
//! never spawns an ephemeral execution future.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crank_core::{
|
||||
CorrelationContext, PlatformApiKeyId, TaskAcceptance, TaskAccess, TaskError, TaskId,
|
||||
TaskOwnerKind, TaskRecord, ToolExecutionMode,
|
||||
};
|
||||
use crank_registry::{PostgresRegistry, PublishedAgentTool};
|
||||
use crank_runtime::{TaskAcceptDraft, TaskOutcome, TaskService};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::access::credential_allows_security_level;
|
||||
use crate::catalog::PublishedToolCatalog;
|
||||
|
||||
/// Authenticated principal of one Task operation. The authorization scope identity is derived here
|
||||
/// from the verified credential/admin identity, never from a caller-supplied request string.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TaskPrincipal {
|
||||
Agent {
|
||||
workspace_slug: String,
|
||||
agent_slug: String,
|
||||
credential: crank_core::VerifiedMachineCredential,
|
||||
},
|
||||
/// Administrator identity that the admin surface already authenticated. It is never a wildcard:
|
||||
/// owner, workspace and authorization scope all participate in the stored predicate.
|
||||
Admin {
|
||||
workspace_slug: String,
|
||||
agent_slug: String,
|
||||
user_id: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl TaskPrincipal {
|
||||
fn workspace_slug(&self) -> &str {
|
||||
match self {
|
||||
Self::Agent { workspace_slug, .. } | Self::Admin { workspace_slug, .. } => {
|
||||
workspace_slug
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn agent_slug(&self) -> &str {
|
||||
match self {
|
||||
Self::Agent { agent_slug, .. } | Self::Admin { agent_slug, .. } => agent_slug,
|
||||
}
|
||||
}
|
||||
|
||||
fn owner(&self, tool: &PublishedAgentTool) -> (TaskOwnerKind, String) {
|
||||
match self {
|
||||
Self::Agent { .. } => (TaskOwnerKind::Agent, tool.agent_id.as_str().to_owned()),
|
||||
Self::Admin { user_id, .. } => (TaskOwnerKind::Admin, user_id.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed immutable Platform API key identity of this principal. A verified agent credential
|
||||
/// without a key ID cannot be revalidated by a worker, so it is refused instead of accepted;
|
||||
/// an administrator has no machine key and is stored as `None` (the worker fails closed).
|
||||
fn platform_api_key_id(&self) -> Result<Option<PlatformApiKeyId>, TaskApplicationError> {
|
||||
match self {
|
||||
Self::Agent { credential, .. } => credential
|
||||
.platform_api_key_id
|
||||
.clone()
|
||||
.map(Some)
|
||||
.ok_or(TaskApplicationError::Denied),
|
||||
Self::Admin { .. } => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded server-derived authorization scope identity of this principal.
|
||||
fn authorization_scope(&self) -> String {
|
||||
let identity = match self {
|
||||
Self::Agent { credential, .. } => {
|
||||
let mut scopes = credential
|
||||
.scopes
|
||||
.iter()
|
||||
.map(|scope| format!("{scope:?}"))
|
||||
.collect::<Vec<_>>();
|
||||
scopes.sort();
|
||||
format!(
|
||||
"key:{}|mode:{:?}|level:{:?}|scopes:{}",
|
||||
credential
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(|id| id.as_str().to_owned())
|
||||
.unwrap_or_else(|| "-".to_owned()),
|
||||
credential.machine_access_mode,
|
||||
credential.max_security_level,
|
||||
scopes.join(",")
|
||||
)
|
||||
}
|
||||
Self::Admin { user_id, .. } => format!("admin:{user_id}"),
|
||||
};
|
||||
identity.chars().take(160).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed refusal of the application entry. `NotFound`/`Denied` never reveal whether a foreign Task
|
||||
/// exists, and no variant carries Task payload or fingerprint.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum TaskApplicationError {
|
||||
#[error("task request was not found")]
|
||||
NotFound,
|
||||
#[error("task request is not authorized")]
|
||||
Denied,
|
||||
#[error("the published Tool policy does not accept durable background work")]
|
||||
NotBackground,
|
||||
#[error("the requested published Tool version is not available")]
|
||||
VersionUnavailable,
|
||||
#[error("this Tool requires a verified approval before acceptance")]
|
||||
ApprovalRequired,
|
||||
#[error(transparent)]
|
||||
Task(#[from] TaskError),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TaskApplication {
|
||||
catalog: PublishedToolCatalog,
|
||||
service: TaskService,
|
||||
}
|
||||
|
||||
impl TaskApplication {
|
||||
/// Production construction of the application entry over the process registry, the published
|
||||
/// catalog and the durable Task service.
|
||||
pub fn new(
|
||||
registry: PostgresRegistry,
|
||||
refresh_interval: Duration,
|
||||
coordination_store: Arc<dyn crank_core::CoordinationStateStore>,
|
||||
service: TaskService,
|
||||
) -> Self {
|
||||
Self {
|
||||
catalog: PublishedToolCatalog::new(registry, refresh_interval, coordination_store),
|
||||
service,
|
||||
}
|
||||
}
|
||||
|
||||
/// Runtime task service carrying encrypted executable input.
|
||||
pub fn task_service(&self) -> TaskService {
|
||||
self.service.clone()
|
||||
}
|
||||
|
||||
/// Published catalog of the same application entry, for the durable Task worker's pre-dispatch
|
||||
/// revalidation.
|
||||
pub(super) fn catalog(&self) -> PublishedToolCatalog {
|
||||
self.catalog.clone()
|
||||
}
|
||||
|
||||
/// Readiness of the durable Task store this entry depends on.
|
||||
pub async fn health(&self) -> Result<(), TaskError> {
|
||||
self.service.health().await
|
||||
}
|
||||
|
||||
/// Accepts one background request for an already authenticated principal.
|
||||
///
|
||||
/// `expected_version` is the caller's explicit Published Version pin. Without it the current
|
||||
/// publication is pinned for new work and a retry of the same scoped key returns the stored Task
|
||||
/// even after a newer publication. An explicit pin may only retry the Task accepted with that
|
||||
/// exact version: a different pin is a typed conflict, never a rebind.
|
||||
pub async fn accept_background(
|
||||
&self,
|
||||
principal: &TaskPrincipal,
|
||||
tool_name: &str,
|
||||
expected_version: Option<u32>,
|
||||
scoped_key: &str,
|
||||
input: &Value,
|
||||
correlation: &CorrelationContext,
|
||||
) -> Result<TaskAcceptance, TaskApplicationError> {
|
||||
let tool = self.resolve_authorized_tool(principal, tool_name).await?;
|
||||
let current_version = tool.operation.version;
|
||||
let Some(policy) = tool.operation.execution_config.execution_policy else {
|
||||
return Err(TaskApplicationError::NotBackground);
|
||||
};
|
||||
if policy.mode == ToolExecutionMode::Synchronous {
|
||||
return Err(TaskApplicationError::NotBackground);
|
||||
}
|
||||
if tool
|
||||
.operation
|
||||
.execution_config
|
||||
.approval_policy
|
||||
.as_ref()
|
||||
.is_some_and(|approval| approval.required)
|
||||
{
|
||||
// Without a verified, non-consuming approval reference this entry fails closed instead
|
||||
// of accepting work a worker could never safely execute.
|
||||
return Err(TaskApplicationError::ApprovalRequired);
|
||||
}
|
||||
let platform_api_key_id = principal.platform_api_key_id()?;
|
||||
let draft = |operation_version: u32| {
|
||||
let (owner_kind, owner_id) = principal.owner(&tool);
|
||||
TaskAcceptDraft {
|
||||
workspace_id: tool.workspace_id.as_str().to_owned(),
|
||||
owner_kind,
|
||||
owner_id,
|
||||
authorization_scope: principal.authorization_scope(),
|
||||
platform_api_key_id: platform_api_key_id.clone(),
|
||||
operation_id: tool.operation.id.as_str().to_owned(),
|
||||
operation_version,
|
||||
policy_mode: policy.mode,
|
||||
scoped_key: scoped_key.to_owned(),
|
||||
input: input.clone(),
|
||||
correlation: correlation.clone(),
|
||||
deadline: Instant::now() + Duration::from_millis(policy.deadline_ms.max(1)),
|
||||
retention_ms: TaskService::default_retention_ms(),
|
||||
approval_reference: None,
|
||||
}
|
||||
};
|
||||
|
||||
let Some(requested) = expected_version else {
|
||||
return Ok(self.service.accept(draft(current_version)).await?);
|
||||
};
|
||||
let draft = draft(requested);
|
||||
match self.service.lookup(&draft).await? {
|
||||
Some(record) => {
|
||||
if record.input_fingerprint != TaskService::input_fingerprint(input)? {
|
||||
return Err(TaskApplicationError::Task(TaskError::FingerprintConflict));
|
||||
}
|
||||
if record.pins.operation_version != requested {
|
||||
return Err(TaskApplicationError::VersionUnavailable);
|
||||
}
|
||||
Ok(TaskAcceptance {
|
||||
task_id: record.task_id,
|
||||
state: record.state,
|
||||
operation_version: record.pins.operation_version,
|
||||
support: record.support,
|
||||
created_at_unix_ms: record.created_at_unix_ms,
|
||||
retain_until_unix_ms: record.pins.retain_until_unix_ms,
|
||||
replayed: true,
|
||||
})
|
||||
}
|
||||
None if requested == current_version => Ok(self.service.accept(draft).await?),
|
||||
// A new Task can only be accepted against the currently published version.
|
||||
None => Err(TaskApplicationError::VersionUnavailable),
|
||||
}
|
||||
}
|
||||
|
||||
/// Owner-scoped status read of one Task of the addressed published Tool.
|
||||
pub async fn status(
|
||||
&self,
|
||||
principal: &TaskPrincipal,
|
||||
tool_name: &str,
|
||||
task_id: TaskId,
|
||||
) -> Result<TaskRecord, TaskApplicationError> {
|
||||
let access = self.task_access(principal, tool_name).await?;
|
||||
match self.service.status(&access, task_id).await {
|
||||
Ok(record) => Ok(record),
|
||||
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
|
||||
Err(error) => Err(TaskApplicationError::Task(error)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Owner-scoped result projection of one Task of the addressed published Tool.
|
||||
pub async fn result(
|
||||
&self,
|
||||
principal: &TaskPrincipal,
|
||||
tool_name: &str,
|
||||
task_id: TaskId,
|
||||
) -> Result<TaskOutcome, TaskApplicationError> {
|
||||
let access = self.task_access(principal, tool_name).await?;
|
||||
match self.service.result(&access, task_id).await {
|
||||
Ok(outcome) => Ok(outcome),
|
||||
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
|
||||
Err(error) => Err(TaskApplicationError::Task(error)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn task_access(
|
||||
&self,
|
||||
principal: &TaskPrincipal,
|
||||
tool_name: &str,
|
||||
) -> Result<TaskAccess, TaskApplicationError> {
|
||||
let tool = self.resolve_authorized_tool(principal, tool_name).await?;
|
||||
let (owner_kind, owner_id) = principal.owner(&tool);
|
||||
Ok(TaskAccess {
|
||||
workspace_id: tool.workspace_id.as_str().to_owned(),
|
||||
owner_kind,
|
||||
owner_id,
|
||||
authorization_scope: principal.authorization_scope(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolves the addressed Tool of the principal's published catalog and repeats its authorization
|
||||
/// check. A foreign workspace, an unknown Tool and an insufficient security level are typed
|
||||
/// refusals without metadata.
|
||||
async fn resolve_authorized_tool(
|
||||
&self,
|
||||
principal: &TaskPrincipal,
|
||||
tool_name: &str,
|
||||
) -> Result<PublishedAgentTool, TaskApplicationError> {
|
||||
let catalog = self
|
||||
.catalog
|
||||
.get_catalog(principal.workspace_slug(), principal.agent_slug())
|
||||
.await
|
||||
.map_err(|_| TaskApplicationError::NotFound)?;
|
||||
let tool = catalog
|
||||
.tools
|
||||
.into_iter()
|
||||
.find(|tool| tool.tool_name == tool_name)
|
||||
.ok_or(TaskApplicationError::NotFound)?;
|
||||
if let TaskPrincipal::Admin { workspace_slug, .. } = principal
|
||||
&& tool.workspace_slug != *workspace_slug
|
||||
{
|
||||
return Err(TaskApplicationError::Denied);
|
||||
}
|
||||
if let TaskPrincipal::Agent { credential, .. } = principal
|
||||
&& !credential_allows_security_level(credential, tool.operation.security_level)
|
||||
{
|
||||
return Err(TaskApplicationError::Denied);
|
||||
}
|
||||
Ok(tool)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ mod tool_search;
|
||||
mod transport;
|
||||
|
||||
pub use app::{
|
||||
build_app, build_app_with_artifacts, build_app_with_background_workers,
|
||||
build_app_with_background_workers_and_limits,
|
||||
TaskApplication, TaskApplicationError, TaskPrincipal, TaskWorkerHandler, build_app,
|
||||
build_app_with_artifacts, build_app_with_artifacts_and_tasks,
|
||||
build_app_with_background_workers, build_app_with_background_workers_and_limits,
|
||||
};
|
||||
@@ -18,6 +18,7 @@ pub mod prompt_composition;
|
||||
pub mod protocol;
|
||||
pub mod resource;
|
||||
pub mod secret;
|
||||
pub mod task;
|
||||
pub mod tool_catalog;
|
||||
pub mod tool_quality;
|
||||
pub mod tool_search;
|
||||
@@ -73,6 +74,15 @@ pub mod domain {
|
||||
};
|
||||
pub use crate::protocol::{AuthKind, ExportMode, HttpMethod, Protocol};
|
||||
pub use crate::secret::{Secret, SecretKind, SecretStatus, SecretVersion};
|
||||
pub use crate::task::{
|
||||
TASK_APPROVAL_REFERENCE_MAX_BYTES, TASK_DEFAULT_RETENTION_MS, TASK_ENVELOPE_MAX_BYTES,
|
||||
TASK_ENVELOPE_MAX_DEPTH, TASK_ENVELOPE_MAX_NODES,
|
||||
TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES, TASK_EXECUTABLE_INPUT_MAX_BYTES,
|
||||
TASK_IDENTITY_MAX_BYTES, TASK_MAX_RETENTION_MS, TASK_PURGE_BATCH_LIMIT,
|
||||
TASK_SCOPED_KEY_MAX_BYTES, TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskEnvelope,
|
||||
TaskError, TaskFinishRequest, TaskId, TaskOwnerKind, TaskPins, TaskPurgeReport, TaskRecord,
|
||||
TaskState, TaskSupportIds,
|
||||
};
|
||||
pub use crate::tool_catalog::{ToolCatalogAnalysis, ToolCatalogBudget};
|
||||
pub use crate::tool_quality::{
|
||||
ToolQualityCatalogTool, ToolQualityFinding, ToolQualityMappingRule, ToolQualityMappingSet,
|
||||
@@ -110,6 +120,7 @@ pub mod ports {
|
||||
PreparedRequest, ProtocolAdapter, ProtocolAdapterError, ResponseCacheScope,
|
||||
RuntimeRequestContext, SharedProtocolAdapter,
|
||||
};
|
||||
pub use crate::task::TaskStore;
|
||||
}
|
||||
|
||||
pub use access::{
|
||||
@@ -192,6 +203,14 @@ pub use product_event::{
|
||||
};
|
||||
pub use protocol::{AuthKind, ExportMode, HttpMethod, Protocol};
|
||||
pub use secret::{Secret, SecretKind, SecretStatus, SecretVersion};
|
||||
pub use task::{
|
||||
TASK_DEFAULT_RETENTION_MS, TASK_ENVELOPE_MAX_BYTES, TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES,
|
||||
TASK_EXECUTABLE_INPUT_MAX_BYTES, TASK_MAX_RETENTION_MS, TASK_PURGE_BATCH_LIMIT,
|
||||
TASK_SCOPED_KEY_MAX_BYTES, TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskEnvelope,
|
||||
TaskError, TaskFinishRequest, TaskId, TaskLease, TaskOwnerKind, TaskPins, TaskPurgeReport,
|
||||
TaskRecord, TaskState, TaskStore, TaskSupportIds, contains_sensitive_task_keys,
|
||||
redact_task_envelope, sensitive_task_key, validate_task_envelope,
|
||||
};
|
||||
pub use tool_catalog::{
|
||||
RECOMMENDED_TOOL_CATALOG_CONTEXT_TOKENS, ToolCatalogAnalysis, ToolCatalogAnalysisError,
|
||||
ToolCatalogBudget, analyze_tool_catalog,
|
||||
|
||||
@@ -0,0 +1,874 @@
|
||||
//! Durable background Task seam (Story 4.2, FR-22/FR-23).
|
||||
//!
|
||||
//! A Task is the internal, protocol-independent record of one accepted background request. This
|
||||
//! module owns the domain vocabulary only: the lifecycle states, the accepted identity/pins, the
|
||||
//! bounded persisted envelopes and the `TaskStore` port. The protocol adapter (Story 4.7) and the
|
||||
//! worker (Story 4.3) do not define these semantics; the authoritative store lives in PostgreSQL
|
||||
//! (`crank-registry`), so an accepted Task survives a crash or restart.
|
||||
//!
|
||||
//! Deliberately absent: credentials, prepared headers, approval bearer tokens and any claim that a
|
||||
//! Task was executed. A terminal state is written by a conditional transition only.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::correlation::{RequestId, TraceContext, TraceId};
|
||||
use crate::ids::PlatformApiKeyId;
|
||||
use crate::operation::ToolExecutionMode;
|
||||
|
||||
/// Byte budget of every persisted Task envelope (input, progress, result, error, schema metadata).
|
||||
pub const TASK_ENVELOPE_MAX_BYTES: usize = 32_768;
|
||||
/// Maximum byte budget of the decrypted executable Task input.
|
||||
pub const TASK_EXECUTABLE_INPUT_MAX_BYTES: usize = TASK_ENVELOPE_MAX_BYTES;
|
||||
/// Maximum byte budget of the stored encrypted executable Task input ciphertext.
|
||||
pub const TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES: usize = 98_304;
|
||||
/// Maximum nesting depth of a persisted Task envelope.
|
||||
pub const TASK_ENVELOPE_MAX_DEPTH: usize = 8;
|
||||
/// Maximum number of JSON members/nodes of a persisted Task envelope.
|
||||
pub const TASK_ENVELOPE_MAX_NODES: usize = 256;
|
||||
/// Bound of every scope/owner/operation identity column.
|
||||
pub const TASK_IDENTITY_MAX_BYTES: usize = 160;
|
||||
/// Bound of the caller-supplied idempotency key before it becomes a scoped digest.
|
||||
pub const TASK_SCOPED_KEY_MAX_BYTES: usize = 128;
|
||||
/// Bound of a saved approval reference (an identifier, never a bearer token).
|
||||
pub const TASK_APPROVAL_REFERENCE_MAX_BYTES: usize = 160;
|
||||
/// Default retention of a terminal Task after its execution deadline (internal bounded default).
|
||||
pub const TASK_DEFAULT_RETENTION_MS: i64 = 86_400_000;
|
||||
/// Upper bound on the retention an accepted Task may pin.
|
||||
pub const TASK_MAX_RETENTION_MS: i64 = 30 * 86_400_000;
|
||||
/// Upper bound of one retention/housekeeping batch.
|
||||
pub const TASK_PURGE_BATCH_LIMIT: u32 = 128;
|
||||
|
||||
/// Stable identity of one accepted Task. Generated as UUIDv7 and returned only after the accepting
|
||||
/// transaction committed, so the ID never exists without its durable record.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct TaskId(Uuid);
|
||||
|
||||
impl TaskId {
|
||||
pub fn generate() -> Self {
|
||||
Self(Uuid::now_v7())
|
||||
}
|
||||
|
||||
pub fn parse(value: &str) -> Result<Self, TaskError> {
|
||||
Uuid::parse_str(value)
|
||||
.map(Self)
|
||||
.map_err(|_| TaskError::invalid("task identity is not a UUID"))
|
||||
}
|
||||
|
||||
pub fn as_uuid(self) -> Uuid {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for TaskId {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
/// Closed Task lifecycle vocabulary. `outcome_unknown` is a non-terminal state: a value commit that
|
||||
/// failed to confirm its dispatch is never collapsed into a plain failure.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TaskState {
|
||||
Queued,
|
||||
Working,
|
||||
InputRequired,
|
||||
OutcomeUnknown,
|
||||
Completed,
|
||||
Failed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
impl TaskState {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Queued => "queued",
|
||||
Self::Working => "working",
|
||||
Self::InputRequired => "input_required",
|
||||
Self::OutcomeUnknown => "outcome_unknown",
|
||||
Self::Completed => "completed",
|
||||
Self::Failed => "failed",
|
||||
Self::Cancelled => "cancelled",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(value: &str) -> Result<Self, TaskError> {
|
||||
match value {
|
||||
"queued" => Ok(Self::Queued),
|
||||
"working" => Ok(Self::Working),
|
||||
"input_required" => Ok(Self::InputRequired),
|
||||
"outcome_unknown" => Ok(Self::OutcomeUnknown),
|
||||
"completed" => Ok(Self::Completed),
|
||||
"failed" => Ok(Self::Failed),
|
||||
"cancelled" => Ok(Self::Cancelled),
|
||||
_ => Err(TaskError::invalid("unknown task state")),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn is_terminal(self) -> bool {
|
||||
matches!(self, Self::Completed | Self::Failed | Self::Cancelled)
|
||||
}
|
||||
|
||||
/// One conditional transition may only move along this table. A terminal state has no successor,
|
||||
/// so a repeated terminal write can never rewrite an accepted result.
|
||||
pub const fn allows_transition_to(self, next: Self) -> bool {
|
||||
match self {
|
||||
Self::Queued => matches!(next, Self::Working | Self::Failed | Self::Cancelled),
|
||||
Self::Working => matches!(
|
||||
next,
|
||||
Self::InputRequired
|
||||
| Self::OutcomeUnknown
|
||||
| Self::Completed
|
||||
| Self::Failed
|
||||
| Self::Cancelled
|
||||
),
|
||||
Self::InputRequired => matches!(next, Self::Working | Self::Failed | Self::Cancelled),
|
||||
Self::OutcomeUnknown => {
|
||||
matches!(next, Self::Completed | Self::Failed | Self::Cancelled)
|
||||
}
|
||||
Self::Completed | Self::Failed | Self::Cancelled => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Who owns the accepted Task. An administrator is never a wildcard owner of every Task: the owner
|
||||
/// identity and the authorization scope are separate, both-pinned columns.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TaskOwnerKind {
|
||||
Agent,
|
||||
Admin,
|
||||
}
|
||||
|
||||
impl TaskOwnerKind {
|
||||
pub const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Agent => "agent",
|
||||
Self::Admin => "admin",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(value: &str) -> Result<Self, TaskError> {
|
||||
match value {
|
||||
"agent" => Ok(Self::Agent),
|
||||
"admin" => Ok(Self::Admin),
|
||||
_ => Err(TaskError::invalid("unknown task owner kind")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Support identities captured at acceptance. They are stored and returned independently of
|
||||
/// sampling, and a replay never rewrites them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskSupportIds {
|
||||
pub request_id: RequestId,
|
||||
pub trace_id: TraceId,
|
||||
pub traceparent: String,
|
||||
pub trace_sampled: bool,
|
||||
}
|
||||
|
||||
impl TaskSupportIds {
|
||||
pub fn new(request_id: RequestId, trace_context: &TraceContext) -> Self {
|
||||
Self {
|
||||
request_id,
|
||||
trace_id: trace_context.trace_id().clone(),
|
||||
traceparent: trace_context.traceparent().to_owned(),
|
||||
trace_sampled: trace_context.is_sampled(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-validates both the trace identity and the parent context: a Task must never persist an
|
||||
/// inconsistent or malformed support pair, and an unsampled context is still a valid parent.
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
if self.trace_id.as_str().is_empty() {
|
||||
return Err(TaskError::invalid("task trace identity is missing"));
|
||||
}
|
||||
let context = TraceContext::parse(&self.traceparent)
|
||||
.map_err(|_| TaskError::invalid("task trace parent is invalid"))?;
|
||||
if context.trace_id() != &self.trace_id || context.is_sampled() != self.trace_sampled {
|
||||
return Err(TaskError::invalid("task trace parent is inconsistent"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Execution semantics pinned at acceptance: a later publication never rewrites them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskPins {
|
||||
pub operation_version: u32,
|
||||
pub policy_mode: ToolExecutionMode,
|
||||
pub execution_deadline_unix_ms: i64,
|
||||
/// Retention deadline of the record; never the same thing as the execution deadline.
|
||||
pub retain_until_unix_ms: i64,
|
||||
/// Verified approval evidence reference (identifier only, never a bearer token).
|
||||
pub approval_reference: Option<String>,
|
||||
}
|
||||
|
||||
impl TaskPins {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
if self.operation_version == 0 {
|
||||
return Err(TaskError::invalid(
|
||||
"task operation version must be positive",
|
||||
));
|
||||
}
|
||||
if self.execution_deadline_unix_ms <= 0 {
|
||||
return Err(TaskError::invalid("task execution deadline is invalid"));
|
||||
}
|
||||
if self.retain_until_unix_ms < self.execution_deadline_unix_ms {
|
||||
return Err(TaskError::invalid(
|
||||
"task retention deadline precedes execution",
|
||||
));
|
||||
}
|
||||
if self.retain_until_unix_ms - self.execution_deadline_unix_ms > TASK_MAX_RETENTION_MS {
|
||||
return Err(TaskError::invalid("task retention exceeds its bound"));
|
||||
}
|
||||
if let Some(reference) = self.approval_reference.as_deref()
|
||||
&& (reference.is_empty()
|
||||
|| reference.len() > TASK_APPROVAL_REFERENCE_MAX_BYTES
|
||||
|| reference.bytes().any(|byte| byte.is_ascii_control()))
|
||||
{
|
||||
return Err(TaskError::invalid("task approval reference is invalid"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded, classified payload of a Task. Construction validates the byte/depth/node budget and
|
||||
/// redacts secret-bearing fields before anything is hashed or persisted.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskEnvelope {
|
||||
value: Value,
|
||||
}
|
||||
|
||||
impl TaskEnvelope {
|
||||
/// Validates the caller-provided value, then stores a redacted copy of it. The caller hashes the
|
||||
/// original value separately, so redaction never collapses two different requests into one.
|
||||
pub fn try_redacted(input: &Value) -> Result<Self, TaskError> {
|
||||
validate_task_envelope(input)?;
|
||||
Ok(Self {
|
||||
value: redact_task_envelope(input),
|
||||
})
|
||||
}
|
||||
|
||||
/// Accepts a value that was already validated and redacted at a trust boundary.
|
||||
pub fn from_persisted(value: Value) -> Result<Self, TaskError> {
|
||||
validate_task_envelope(&value)?;
|
||||
Ok(Self { value })
|
||||
}
|
||||
|
||||
pub fn value(&self) -> &Value {
|
||||
&self.value
|
||||
}
|
||||
|
||||
pub fn into_value(self) -> Value {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates the size, nesting depth and node count of one Task envelope. Every byte, collection and
|
||||
/// node is counted before the value is cloned, hashed or persisted.
|
||||
pub fn validate_task_envelope(value: &Value) -> Result<(), TaskError> {
|
||||
let encoded = serde_json::to_vec(value)
|
||||
.map_err(|_| TaskError::invalid("task envelope is not serializable"))?;
|
||||
if encoded.len() > TASK_ENVELOPE_MAX_BYTES {
|
||||
return Err(TaskError::invalid("task envelope exceeds its byte budget"));
|
||||
}
|
||||
let mut nodes = 0usize;
|
||||
check_budget(value, 1, &mut nodes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_budget(value: &Value, depth: usize, nodes: &mut usize) -> Result<(), TaskError> {
|
||||
if depth > TASK_ENVELOPE_MAX_DEPTH {
|
||||
return Err(TaskError::invalid("task envelope exceeds its depth budget"));
|
||||
}
|
||||
*nodes += 1;
|
||||
if *nodes > TASK_ENVELOPE_MAX_NODES {
|
||||
return Err(TaskError::invalid("task envelope exceeds its node budget"));
|
||||
}
|
||||
match value {
|
||||
Value::Object(object) => {
|
||||
for (key, child) in object {
|
||||
check_budget_name(key)?;
|
||||
check_budget(child, depth + 1, nodes)?;
|
||||
}
|
||||
}
|
||||
Value::Array(values) => {
|
||||
for child in values {
|
||||
check_budget(child, depth + 1, nodes)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_budget_name(key: &str) -> Result<(), TaskError> {
|
||||
if key.len() > TASK_IDENTITY_MAX_BYTES {
|
||||
return Err(TaskError::invalid("task envelope key exceeds its budget"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Field-name classification of secret-bearing values. A matching field keeps its position but never
|
||||
/// its plaintext in the persisted envelope.
|
||||
pub fn sensitive_task_key(name: &str) -> bool {
|
||||
let lowered = name.to_ascii_lowercase();
|
||||
[
|
||||
"password",
|
||||
"passwd",
|
||||
"secret",
|
||||
"token",
|
||||
"api_key",
|
||||
"apikey",
|
||||
"authorization",
|
||||
"credential",
|
||||
"private_key",
|
||||
"cookie",
|
||||
"signature",
|
||||
]
|
||||
.iter()
|
||||
.any(|marker| lowered.contains(marker))
|
||||
}
|
||||
|
||||
/// Redacted projection of an already validated envelope.
|
||||
pub fn redact_task_envelope(value: &Value) -> Value {
|
||||
match value {
|
||||
Value::Object(object) => Value::Object(
|
||||
object
|
||||
.iter()
|
||||
.map(|(key, child)| {
|
||||
let value = if sensitive_task_key(key) {
|
||||
Value::String("[redacted]".to_owned())
|
||||
} else {
|
||||
redact_task_envelope(child)
|
||||
};
|
||||
(key.clone(), value)
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
Value::Array(values) => Value::Array(values.iter().map(redact_task_envelope).collect()),
|
||||
other => other.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursive check used to fail closed on sensitive fields before encryption.
|
||||
pub fn contains_sensitive_task_keys(value: &Value) -> bool {
|
||||
match value {
|
||||
Value::Object(object) => object
|
||||
.iter()
|
||||
.any(|(key, child)| sensitive_task_key(key) || contains_sensitive_task_keys(child)),
|
||||
Value::Array(values) => values.iter().any(contains_sensitive_task_keys),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
/// Write request of a new durable Task. The store owns the transaction: Task, dispatch intent and
|
||||
/// queue capacity commit together, and no acknowledgement exists before that commit.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskAcceptRequest {
|
||||
pub task_id: TaskId,
|
||||
pub workspace_id: String,
|
||||
pub owner_kind: TaskOwnerKind,
|
||||
pub owner_id: String,
|
||||
pub authorization_scope: String,
|
||||
/// Typed identity of the Platform API key that authorized the acceptance (Story 4.3). Only the
|
||||
/// immutable key ID is stored, never the bearer secret; a worker revalidates it authoritatively.
|
||||
/// `None` means the accepting principal has no machine key (administrator) and fail-closes.
|
||||
pub platform_api_key_id: Option<PlatformApiKeyId>,
|
||||
pub operation_id: String,
|
||||
pub pins: TaskPins,
|
||||
/// Digest of the caller key over the full owner/authorization/Tool scope.
|
||||
pub scoped_key: String,
|
||||
/// Digest of the canonical validated executable input.
|
||||
pub input_fingerprint: String,
|
||||
pub input: TaskEnvelope,
|
||||
pub support: TaskSupportIds,
|
||||
/// Bounded encrypted executable input, persisted alongside the redacted input projection.
|
||||
pub executable_input_ciphertext: Option<String>,
|
||||
pub executable_input_key_version: Option<String>,
|
||||
pub executable_input_master_key_epoch: Option<i64>,
|
||||
}
|
||||
|
||||
impl TaskAcceptRequest {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
for (field, value) in [
|
||||
("workspace", self.workspace_id.as_str()),
|
||||
("owner", self.owner_id.as_str()),
|
||||
("authorization scope", self.authorization_scope.as_str()),
|
||||
("operation", self.operation_id.as_str()),
|
||||
] {
|
||||
if value.is_empty() || value.len() > TASK_IDENTITY_MAX_BYTES {
|
||||
return Err(TaskError::invalid(format!("task {field} is invalid")));
|
||||
}
|
||||
}
|
||||
if let Some(key_id) = self.platform_api_key_id.as_ref()
|
||||
&& (key_id.as_str().is_empty() || key_id.as_str().len() > TASK_IDENTITY_MAX_BYTES)
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task platform api key reference is invalid",
|
||||
));
|
||||
}
|
||||
if self.scoped_key.len() < 16 || self.scoped_key.len() > TASK_SCOPED_KEY_MAX_BYTES {
|
||||
return Err(TaskError::invalid("task scoped key digest is invalid"));
|
||||
}
|
||||
if self.input_fingerprint.len() != 64
|
||||
|| !self
|
||||
.input_fingerprint
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
|
||||
{
|
||||
return Err(TaskError::invalid("task input fingerprint is invalid"));
|
||||
}
|
||||
self.pins.validate()?;
|
||||
self.support.validate()?;
|
||||
let has_ciphertext = self.executable_input_ciphertext.is_some();
|
||||
let has_key = self.executable_input_key_version.is_some();
|
||||
let has_epoch = self.executable_input_master_key_epoch.is_some();
|
||||
match (has_ciphertext, has_key, has_epoch) {
|
||||
(false, false, false) => {}
|
||||
(true, true, true) => {
|
||||
let ciphertext = self.executable_input_ciphertext.as_deref().unwrap();
|
||||
if ciphertext.is_empty()
|
||||
|| ciphertext.len() > TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
let key_version = self.executable_input_key_version.as_deref().unwrap();
|
||||
if key_version.is_empty() || key_version.len() > 64 {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
if self.executable_input_master_key_epoch.unwrap_or_default() < 1 {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Successful acceptance, returned only after the accepting transaction committed. A replay returns
|
||||
/// the stored identity and support IDs of the original acceptance.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskAcceptance {
|
||||
pub task_id: TaskId,
|
||||
pub state: TaskState,
|
||||
/// Exact Published Version pinned by this acceptance; a replay returns the stored pin.
|
||||
pub operation_version: u32,
|
||||
pub support: TaskSupportIds,
|
||||
pub created_at_unix_ms: i64,
|
||||
pub retain_until_unix_ms: i64,
|
||||
pub replayed: bool,
|
||||
}
|
||||
|
||||
/// Owner-side access proof of a read or a terminal write. It is built by the application layer from
|
||||
/// the verified principal, never from a caller-supplied marker, and every store query carries the
|
||||
/// whole predicate so a foreign row is never fetched.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskAccess {
|
||||
pub workspace_id: String,
|
||||
pub owner_kind: TaskOwnerKind,
|
||||
pub owner_id: String,
|
||||
pub authorization_scope: String,
|
||||
}
|
||||
|
||||
impl TaskAccess {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
for (field, value) in [
|
||||
("workspace", self.workspace_id.as_str()),
|
||||
("owner", self.owner_id.as_str()),
|
||||
("authorization scope", self.authorization_scope.as_str()),
|
||||
] {
|
||||
if value.is_empty() || value.len() > TASK_IDENTITY_MAX_BYTES {
|
||||
return Err(TaskError::invalid(format!("task {field} is invalid")));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Read model of one Task. Lifecycle fields, pins and support IDs stay readable together; the
|
||||
/// protocol adapter only maps them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskRecord {
|
||||
pub task_id: TaskId,
|
||||
pub state: TaskState,
|
||||
pub workspace_id: String,
|
||||
pub owner_kind: TaskOwnerKind,
|
||||
pub owner_id: String,
|
||||
pub authorization_scope: String,
|
||||
/// Typed immutable identity of the Platform API key that authorized the acceptance; the worker
|
||||
/// revalidates this exact key against the registry before any outbound dispatch.
|
||||
pub platform_api_key_id: Option<PlatformApiKeyId>,
|
||||
pub operation_id: String,
|
||||
/// Canonical digest of the accepted executable input (compare-and-conflict identity).
|
||||
pub input_fingerprint: String,
|
||||
pub pins: TaskPins,
|
||||
pub support: TaskSupportIds,
|
||||
pub input: TaskEnvelope,
|
||||
pub progress: Option<TaskEnvelope>,
|
||||
pub result: Option<TaskEnvelope>,
|
||||
pub error: Option<TaskEnvelope>,
|
||||
pub attempts: u32,
|
||||
/// Monotonic lease/fencing token. It never resets and protects Task state mutations.
|
||||
pub fence: u64,
|
||||
/// Random process-instance lease owner, if a worker currently owns the Task.
|
||||
pub lease_owner: Option<String>,
|
||||
/// Lease expiry epoch milliseconds, if the Task is claimed.
|
||||
pub lease_until_unix_ms: Option<i64>,
|
||||
/// Current dispatch attempt identity, committed before any outbound adapter call.
|
||||
pub current_attempt_id: Option<String>,
|
||||
/// Dispatch checkpoint epoch milliseconds, if an outbound attempt was committed.
|
||||
pub dispatch_started_at_unix_ms: Option<i64>,
|
||||
/// Monotonic lifecycle version used by the terminal CAS.
|
||||
pub version: u32,
|
||||
pub created_at_unix_ms: i64,
|
||||
pub updated_at_unix_ms: i64,
|
||||
}
|
||||
|
||||
/// One conditional terminal transition. Exactly one concurrent writer wins; the losers observe the
|
||||
/// accepted state and never rewrite the result.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskFinishRequest {
|
||||
pub access: TaskAccess,
|
||||
pub task_id: TaskId,
|
||||
pub expected_state: TaskState,
|
||||
pub expected_version: u32,
|
||||
pub target_state: TaskState,
|
||||
pub result: Option<TaskEnvelope>,
|
||||
pub error: Option<TaskEnvelope>,
|
||||
}
|
||||
|
||||
impl TaskFinishRequest {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
self.access.validate()?;
|
||||
if !self.target_state.is_terminal() {
|
||||
return Err(TaskError::invalid("task finish target is not terminal"));
|
||||
}
|
||||
if self.expected_state.is_terminal() {
|
||||
return Err(TaskError::invalid("task finish source is already terminal"));
|
||||
}
|
||||
if !self.expected_state.allows_transition_to(self.target_state) {
|
||||
return Err(TaskError::invalid("task state transition is not allowed"));
|
||||
}
|
||||
if self.target_state == TaskState::Completed && self.result.is_none() {
|
||||
return Err(TaskError::invalid("a completed task requires a result"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded retention outcome.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct TaskPurgeReport {
|
||||
/// Queued Tasks that passed their execution deadline and were failed with their intent removed.
|
||||
pub expired_queued: u64,
|
||||
/// Terminal Tasks that passed their retention deadline.
|
||||
pub expired_terminal: u64,
|
||||
}
|
||||
|
||||
/// Typed Task failure. A denial and a missing Task share one outward shape, so a foreign caller can
|
||||
/// never distinguish "not yours" from "does not exist".
|
||||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum TaskError {
|
||||
#[error("task request is invalid: {message}")]
|
||||
Invalid { message: String },
|
||||
#[error("task store is unavailable: {message}")]
|
||||
Unavailable { message: String },
|
||||
#[error("task was not found")]
|
||||
NotFound,
|
||||
#[error("task request conflicts with the accepted request")]
|
||||
FingerprintConflict,
|
||||
#[error("task lifecycle transition was rejected")]
|
||||
StateConflict,
|
||||
#[error("background task capacity is exhausted")]
|
||||
Overloaded { retry_after_ms: u64 },
|
||||
}
|
||||
|
||||
impl TaskError {
|
||||
pub fn invalid(message: impl Into<String>) -> Self {
|
||||
Self::Invalid {
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unavailable(message: impl Into<String>) -> Self {
|
||||
Self::Unavailable {
|
||||
message: message.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the caller may tell this failure apart from an absent Task.
|
||||
pub fn is_not_found(&self) -> bool {
|
||||
matches!(self, Self::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
/// Leased view of a claimed Task: its durable record plus lease fencing identity.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TaskLease {
|
||||
pub record: TaskRecord,
|
||||
pub owner: String,
|
||||
pub fence: u64,
|
||||
pub lease_until: i64,
|
||||
}
|
||||
|
||||
/// Authoritative durable Task port.
|
||||
#[async_trait]
|
||||
pub trait TaskStore: Send + Sync {
|
||||
/// Proves the durable Task state is usable (schema and operations reachable).
|
||||
async fn health(&self) -> Result<(), TaskError>;
|
||||
|
||||
/// Atomically accepts a Task: Task record, dispatch intent and one queue slot commit together.
|
||||
async fn accept(&self, request: TaskAcceptRequest) -> Result<TaskAcceptance, TaskError>;
|
||||
|
||||
/// Reads one Task inside the caller's owner scope.
|
||||
async fn read(&self, access: &TaskAccess, task_id: TaskId) -> Result<TaskRecord, TaskError>;
|
||||
|
||||
/// Scoped lookup of an already accepted Task by its full-scope idempotency digest. Used to keep a
|
||||
/// retry of an explicitly pinned version from becoming new work; `None` means "not accepted yet".
|
||||
async fn find(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
operation_id: &str,
|
||||
scoped_key: &str,
|
||||
) -> Result<Option<TaskRecord>, TaskError>;
|
||||
|
||||
/// Applies exactly one conditional terminal transition and releases its intent atomically.
|
||||
async fn finish(&self, request: TaskFinishRequest) -> Result<TaskRecord, TaskError>;
|
||||
|
||||
/// Reads encrypted executable input only under a valid, unexpired current lease.
|
||||
async fn read_executable_input(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
) -> Result<(String, String, i64), TaskError>;
|
||||
|
||||
/// Bounded retention...
|
||||
async fn purge_expired(&self, batch: u32) -> Result<TaskPurgeReport, TaskError>;
|
||||
|
||||
/// Claims a bounded batch of queued or safely recoverable Tasks under leases.
|
||||
async fn claim_batch(
|
||||
&self,
|
||||
owner: &str,
|
||||
batch_limit: u32,
|
||||
lease_ms: i64,
|
||||
) -> Result<Vec<TaskLease>, TaskError>;
|
||||
|
||||
/// Renews a lease owned by `owner` when fence and expiry still match.
|
||||
async fn renew_lease(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
lease_ms: i64,
|
||||
) -> Result<TaskLease, TaskError>;
|
||||
|
||||
/// Persists redacted progress under a valid lease.
|
||||
async fn write_progress(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
progress: TaskEnvelope,
|
||||
) -> Result<TaskRecord, TaskError>;
|
||||
|
||||
/// Commits an outbound attempt identity and dispatch timestamp before network use.
|
||||
async fn checkpoint_dispatch(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
attempt_id: String,
|
||||
) -> Result<TaskRecord, TaskError>;
|
||||
|
||||
/// Applies a terminal transition only under a valid lease and releases intent atomically.
|
||||
async fn finish_leased(
|
||||
&self,
|
||||
request: TaskFinishRequest,
|
||||
owner: &str,
|
||||
fence: u64,
|
||||
) -> Result<TaskRecord, TaskError>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn request() -> TaskAcceptRequest {
|
||||
TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: "ws_default".to_owned(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_1".to_owned(),
|
||||
platform_api_key_id: Some(PlatformApiKeyId::new("pk_1")),
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 3,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: 1_790_000_000_000,
|
||||
retain_until_unix_ms: 1_790_000_000_000 + 1_000,
|
||||
approval_reference: Some("appr_1".to_owned()),
|
||||
},
|
||||
scoped_key: "a".repeat(64),
|
||||
input_fingerprint: "b".repeat(64),
|
||||
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
|
||||
support: TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&TraceContext::parse("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00")
|
||||
.unwrap(),
|
||||
),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uuid_v7_task_ids_are_unique_and_round_trip() {
|
||||
let first = TaskId::generate();
|
||||
let second = TaskId::generate();
|
||||
assert_ne!(first, second);
|
||||
assert_eq!(TaskId::parse(&first.to_string()).unwrap(), first);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn terminal_states_have_no_successor() {
|
||||
for terminal in [
|
||||
TaskState::Completed,
|
||||
TaskState::Failed,
|
||||
TaskState::Cancelled,
|
||||
] {
|
||||
assert!(terminal.is_terminal());
|
||||
for next in [
|
||||
TaskState::Queued,
|
||||
TaskState::Working,
|
||||
TaskState::Completed,
|
||||
TaskState::Failed,
|
||||
] {
|
||||
assert!(!terminal.allows_transition_to(next));
|
||||
}
|
||||
}
|
||||
assert!(TaskState::Queued.allows_transition_to(TaskState::Working));
|
||||
assert!(TaskState::Working.allows_transition_to(TaskState::OutcomeUnknown));
|
||||
assert!(!TaskState::Queued.allows_transition_to(TaskState::Completed));
|
||||
assert!(!TaskState::InputRequired.allows_transition_to(TaskState::Completed));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn envelope_bounds_reject_bytes_depth_and_nodes() {
|
||||
assert!(TaskEnvelope::try_redacted(&json!({"a": 1})).is_ok());
|
||||
assert!(
|
||||
TaskEnvelope::try_redacted(&json!({"a": "x".repeat(TASK_ENVELOPE_MAX_BYTES)})).is_err()
|
||||
);
|
||||
|
||||
let mut deep = json!(1);
|
||||
for _ in 0..=TASK_ENVELOPE_MAX_DEPTH {
|
||||
deep = json!({"n": deep});
|
||||
}
|
||||
assert!(TaskEnvelope::try_redacted(&deep).is_err());
|
||||
|
||||
let wide = Value::Array((0..=TASK_ENVELOPE_MAX_NODES).map(|_| json!(1)).collect());
|
||||
assert!(TaskEnvelope::try_redacted(&wide).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secret_bearing_fields_are_redacted_and_keep_their_shape() {
|
||||
let envelope = TaskEnvelope::try_redacted(&json!({
|
||||
"query": "leads",
|
||||
"auth": {"api_key": "sk_live_1", "nested": [{"password": "p"}]}
|
||||
}))
|
||||
.unwrap();
|
||||
let value = envelope.value();
|
||||
assert_eq!(value["query"], json!("leads"));
|
||||
assert_eq!(value["auth"]["api_key"], json!("[redacted]"));
|
||||
assert_eq!(value["auth"]["nested"][0]["password"], json!("[redacted]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsampled_support_ids_are_valid_and_consistent() {
|
||||
let support = TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&TraceContext::parse("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00")
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(!support.trace_sampled);
|
||||
assert!(support.traceparent.ends_with("-00"));
|
||||
support.validate().unwrap();
|
||||
|
||||
let mut inconsistent = support.clone();
|
||||
inconsistent.traceparent = inconsistent.traceparent.replace("-00", "-01");
|
||||
assert!(inconsistent.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_request_validation_holds_the_identity_and_pin_bounds() {
|
||||
assert!(request().validate().is_ok());
|
||||
|
||||
let mut oversized = request();
|
||||
oversized.workspace_id = "w".repeat(TASK_IDENTITY_MAX_BYTES + 1);
|
||||
assert!(oversized.validate().is_err());
|
||||
|
||||
let mut bad_fingerprint = request();
|
||||
bad_fingerprint.input_fingerprint = "XYZ".to_owned();
|
||||
assert!(bad_fingerprint.validate().is_err());
|
||||
|
||||
let mut bad_retention = request();
|
||||
bad_retention.pins.retain_until_unix_ms = bad_retention.pins.execution_deadline_unix_ms - 1;
|
||||
assert!(bad_retention.validate().is_err());
|
||||
|
||||
// The persisted authority reference is bounded and never empty.
|
||||
let mut empty_key_reference = request();
|
||||
empty_key_reference.platform_api_key_id = Some(PlatformApiKeyId::new(""));
|
||||
assert!(empty_key_reference.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finish_requires_a_terminal_reachable_transition() {
|
||||
let access = TaskAccess {
|
||||
workspace_id: "ws_default".to_owned(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_1".to_owned(),
|
||||
};
|
||||
let base = TaskFinishRequest {
|
||||
access,
|
||||
task_id: TaskId::generate(),
|
||||
expected_state: TaskState::Working,
|
||||
expected_version: 1,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(TaskEnvelope::try_redacted(&json!({"ok": true})).unwrap()),
|
||||
error: None,
|
||||
};
|
||||
assert!(base.validate().is_ok());
|
||||
|
||||
let mut non_terminal = base.clone();
|
||||
non_terminal.target_state = TaskState::Working;
|
||||
assert!(non_terminal.validate().is_err());
|
||||
|
||||
let mut terminal_source = base.clone();
|
||||
terminal_source.expected_state = TaskState::Completed;
|
||||
assert!(terminal_source.validate().is_err());
|
||||
|
||||
let mut completed_without_result = base;
|
||||
completed_without_result.result = None;
|
||||
assert!(completed_without_result.validate().is_err());
|
||||
}
|
||||
}
|
||||
@@ -117,6 +117,6 @@ pub use model::{
|
||||
};
|
||||
pub use postgres::{
|
||||
AdmissionLimits, AuthorizedResourceRead, BoundResourceAdmission, PostgresExecutionAdmission,
|
||||
PostgresPoolConfig, PostgresPoolConfigError, PostgresRegistry, admission_health,
|
||||
admission_snapshot,
|
||||
PostgresPoolConfig, PostgresPoolConfigError, PostgresRegistry, PostgresTaskStore,
|
||||
admission_health, admission_snapshot, tasks_health,
|
||||
};
|
||||
@@ -6,6 +6,10 @@ mod artifact_cleanup_indexes_v13;
|
||||
mod artifact_metadata_v12;
|
||||
mod authority;
|
||||
mod baseline_v1;
|
||||
mod durable_tasks_v25;
|
||||
mod durable_tasks_v26;
|
||||
mod durable_tasks_v27;
|
||||
mod durable_tasks_v28;
|
||||
mod execution_admission_v24;
|
||||
mod execution_outcome_v5;
|
||||
mod import_plan_v14;
|
||||
@@ -37,6 +41,10 @@ mod schema_guard_v20;
|
||||
mod schema_guard_v22;
|
||||
mod schema_guard_v23;
|
||||
mod schema_guard_v24;
|
||||
mod schema_guard_v25;
|
||||
mod schema_guard_v26;
|
||||
mod schema_guard_v27;
|
||||
mod schema_guard_v28;
|
||||
mod schema_guard_v7;
|
||||
mod schema_guard_v8;
|
||||
mod schema_guard_v9;
|
||||
|
||||
@@ -4,6 +4,10 @@ use super::agent_resource_bindings_v18;
|
||||
use super::approval_side_effects_v10;
|
||||
use super::artifact_cleanup_indexes_v13;
|
||||
use super::artifact_metadata_v12;
|
||||
use super::durable_tasks_v25;
|
||||
use super::durable_tasks_v26;
|
||||
use super::durable_tasks_v27;
|
||||
use super::durable_tasks_v28;
|
||||
use super::execution_admission_v24;
|
||||
use super::execution_outcome_v5;
|
||||
use super::import_plan_v14;
|
||||
@@ -40,9 +44,10 @@ use preflight::*;
|
||||
use contract::baseline_source_digest;
|
||||
use contract::validate_descriptors;
|
||||
const MIGRATION_LOCK_ID: i64 = 0x4352_414E_4B4D_4947;
|
||||
const CURRENT_VERSION: i64 = 24;
|
||||
const CURRENT_VERSION: i64 = 28;
|
||||
const IMPLEMENTED_VERSIONS: &[i64] = &[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
||||
27, 28,
|
||||
];
|
||||
const BASELINE_SOURCE_SHA256: &str =
|
||||
"eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675";
|
||||
@@ -337,6 +342,30 @@ impl MigrationAuthority {
|
||||
execution_admission_v24::SOURCE_SHA256,
|
||||
23,
|
||||
),
|
||||
expand_descriptor(
|
||||
25,
|
||||
durable_tasks_v25::NAME,
|
||||
durable_tasks_v25::SOURCE_SHA256,
|
||||
24,
|
||||
),
|
||||
expand_descriptor(
|
||||
26,
|
||||
durable_tasks_v26::NAME,
|
||||
durable_tasks_v26::SOURCE_SHA256,
|
||||
25,
|
||||
),
|
||||
expand_descriptor(
|
||||
27,
|
||||
durable_tasks_v27::NAME,
|
||||
durable_tasks_v27::SOURCE_SHA256,
|
||||
26,
|
||||
),
|
||||
expand_descriptor(
|
||||
28,
|
||||
durable_tasks_v28::NAME,
|
||||
durable_tasks_v28::SOURCE_SHA256,
|
||||
27,
|
||||
),
|
||||
]
|
||||
}
|
||||
pub fn validate_sequence() -> Result<(), MigrationError> {
|
||||
@@ -499,6 +528,18 @@ impl MigrationAuthority {
|
||||
if from < 24 {
|
||||
execution_admission_v24::apply(&mut transaction, &Self::sequence()[23]).await?;
|
||||
}
|
||||
if from < 25 {
|
||||
durable_tasks_v25::apply(&mut transaction, &Self::sequence()[24]).await?;
|
||||
}
|
||||
if from < 26 {
|
||||
durable_tasks_v26::apply(&mut transaction, &Self::sequence()[25]).await?;
|
||||
}
|
||||
if from < 27 {
|
||||
durable_tasks_v27::apply(&mut transaction, &Self::sequence()[26]).await?;
|
||||
}
|
||||
if from < 28 {
|
||||
durable_tasks_v28::apply(&mut transaction, &Self::sequence()[27]).await?;
|
||||
}
|
||||
match inspect(&mut transaction).await? {
|
||||
MigrationPreflight::Current {
|
||||
version: CURRENT_VERSION,
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const NAME: &str = "durable-tasks-v25";
|
||||
pub(super) const SOURCE: &str = include_str!("durable_tasks_v25.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"ada48bfb7d7130c84ab38217795608cd368ccb28af66faafbdfc4fc7609f67cd";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v25",
|
||||
Some(25),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.canonical_ledger",
|
||||
Some(25),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
-- V25: durable background Tasks (Story 4.2, FR-22, FR-23).
|
||||
--
|
||||
-- Одна строка `tasks` — принятая durable работа. Идентификатор Task (UUIDv7), owner scope,
|
||||
-- authorization scope, запрошенный Tool и точная published-версия, canonical fingerprint входа,
|
||||
-- support IDs (Request/Trace/parent TraceContext независимо от sampling) и bounded envelopes
|
||||
-- закрепляются в той же PostgreSQL transaction, что и dispatch intent
|
||||
-- (`execution_admission_entries.task_id`) и его queue slot. До commit наружу не выдаётся ни ID, ни ack.
|
||||
--
|
||||
-- `scoped_key` — digest caller-ключа по полному (workspace, owner kind/id, authorization scope,
|
||||
-- operation) scope; UNIQUE по этому набору и есть защита идемпотентности. Повтор того же ключа с тем
|
||||
-- же canonical fingerprint возвращает сохранённую Task, другой fingerprint — typed conflict.
|
||||
--
|
||||
-- `retain_until` — срок хранения записи, а не execution deadline: пока запись в retention, replay
|
||||
-- находит тот же Task. Удаление terminal-записей возможно только после retain_until; queued/working/
|
||||
-- input_required/outcome_unknown не удаляются никогда ради лимита.
|
||||
--
|
||||
-- Строка не хранит credentials, bearer/prepared headers и confirmation tokens: секретные поля входа
|
||||
-- редактируются до записи, а approval хранится безопасной ссылкой.
|
||||
|
||||
create table tasks (
|
||||
task_id uuid not null,
|
||||
workspace_id text not null,
|
||||
owner_kind text not null,
|
||||
owner_id text not null,
|
||||
authorization_scope text not null,
|
||||
operation_id text not null,
|
||||
operation_version integer not null,
|
||||
scoped_key text not null,
|
||||
input_fingerprint text not null,
|
||||
request_id text not null,
|
||||
trace_id text not null,
|
||||
traceparent text not null,
|
||||
trace_sampled boolean not null,
|
||||
policy_mode text not null,
|
||||
approval_reference text,
|
||||
state text not null,
|
||||
version integer not null default 1,
|
||||
attempts integer not null default 0,
|
||||
input_envelope jsonb not null,
|
||||
progress_envelope jsonb,
|
||||
result_envelope jsonb,
|
||||
error_envelope jsonb,
|
||||
execution_deadline timestamptz not null,
|
||||
retain_until timestamptz not null,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now(),
|
||||
constraint tasks_pkey primary key (task_id),
|
||||
constraint tasks_workspace_check check (octet_length(workspace_id) between 1 and 160),
|
||||
constraint tasks_owner_kind_check check (owner_kind in ('agent', 'admin')),
|
||||
constraint tasks_owner_check check (octet_length(owner_id) between 1 and 160),
|
||||
constraint tasks_authorization_scope_check check (
|
||||
octet_length(authorization_scope) between 1 and 160
|
||||
),
|
||||
constraint tasks_operation_check check (octet_length(operation_id) between 1 and 160),
|
||||
constraint tasks_operation_version_check check (operation_version >= 1),
|
||||
constraint tasks_scoped_key_check check (octet_length(scoped_key) between 16 and 128),
|
||||
constraint tasks_fingerprint_check check (octet_length(input_fingerprint) = 64),
|
||||
constraint tasks_request_check check (octet_length(request_id) between 1 and 128),
|
||||
constraint tasks_trace_check check (octet_length(trace_id) = 32),
|
||||
constraint tasks_traceparent_check check (octet_length(traceparent) = 55),
|
||||
constraint tasks_policy_mode_check check (
|
||||
policy_mode in ('synchronous', 'background', 'adaptive')
|
||||
),
|
||||
constraint tasks_state_check check (
|
||||
state in ('queued', 'working', 'input_required', 'outcome_unknown',
|
||||
'completed', 'failed', 'cancelled')
|
||||
),
|
||||
constraint tasks_lifecycle_check check (version >= 1 and attempts >= 0),
|
||||
constraint tasks_retention_check check (retain_until >= execution_deadline),
|
||||
constraint tasks_approval_check check (
|
||||
approval_reference is null or octet_length(approval_reference) between 1 and 160
|
||||
)
|
||||
);
|
||||
|
||||
create unique index tasks_scoped_key_uidx
|
||||
on tasks (workspace_id, owner_kind, owner_id, authorization_scope, operation_id, scoped_key);
|
||||
|
||||
create index tasks_retention_idx on tasks (state, retain_until, task_id);
|
||||
|
||||
create index tasks_deadline_idx on tasks (state, execution_deadline, task_id);
|
||||
|
||||
-- Dispatch intent: прежняя reserved-строка admission становится точкой связи Task и capacity.
|
||||
-- FK без cascade: удаление принятой Task не маскируется housekeeping-удалением intent.
|
||||
alter table execution_admission_entries
|
||||
add column task_id uuid,
|
||||
add constraint execution_admission_entries_task_fkey
|
||||
foreign key (task_id) references tasks (task_id);
|
||||
|
||||
create unique index execution_admission_entries_task_uidx
|
||||
on execution_admission_entries (task_id) where task_id is not null;
|
||||
@@ -0,0 +1,45 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const NAME: &str = "durable-tasks-v26";
|
||||
pub(super) const SOURCE: &str = include_str!("durable_tasks_v26.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"6dcbf9990deabb3984a333acbfc3cb490cfc886c2ec30093e3b46d9113616089";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v26",
|
||||
Some(26),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v26",
|
||||
Some(26),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V26: PostgreSQL lease/fencing/CAS additions for durable background Tasks (Story 4.3).
|
||||
alter table tasks add column if not exists fence bigint not null default 0;
|
||||
alter table tasks add column if not exists lease_owner text;
|
||||
alter table tasks add column if not exists lease_until timestamptz;
|
||||
alter table tasks add column if not exists current_attempt_id text;
|
||||
alter table tasks add column if not exists dispatch_started_at timestamptz;
|
||||
@@ -0,0 +1,45 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const NAME: &str = "durable-tasks-v27";
|
||||
pub(super) const SOURCE: &str = include_str!("durable_tasks_v27.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"a3c5335cbf81c244359faaee993487ae132d8b3be6b9c63fa322788021273dc8";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v27",
|
||||
Some(27),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v27",
|
||||
Some(27),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
-- V27: encrypted executable Task input (Story 4.3).
|
||||
alter table tasks add column if not exists executable_input_ciphertext text;
|
||||
alter table tasks add column if not exists executable_input_key_version text;
|
||||
alter table tasks add column if not exists executable_input_master_key_epoch bigint;
|
||||
|
||||
alter table tasks add constraint tasks_executable_input_key_version_check check (
|
||||
executable_input_ciphertext is null
|
||||
or (executable_input_key_version is not null and octet_length(executable_input_key_version) between 1 and 64)
|
||||
);
|
||||
|
||||
alter table tasks add column if not exists target_executable_input_ciphertext text;
|
||||
alter table tasks add column if not exists target_executable_input_key_version text;
|
||||
alter table tasks add column if not exists target_executable_input_master_key_epoch bigint;
|
||||
|
||||
alter table tasks add constraint tasks_executable_input_epoch_check check (
|
||||
executable_input_ciphertext is null
|
||||
or (executable_input_master_key_epoch is not null
|
||||
and executable_input_master_key_epoch >= 1)
|
||||
);
|
||||
@@ -0,0 +1,45 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const NAME: &str = "durable-tasks-v28";
|
||||
pub(super) const SOURCE: &str = include_str!("durable_tasks_v28.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"4dc77739c2ea1e32a4b00abd70df0f00fd82b7cf7933372f869f2c1e997a3676";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v28",
|
||||
Some(28),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_tasks_v28",
|
||||
Some(28),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
-- V28: typed Platform API key authority reference of a durable Task (Story 4.3).
|
||||
-- Only the immutable key identifier is stored, never the bearer secret.
|
||||
alter table tasks add column if not exists platform_api_key_id text;
|
||||
|
||||
alter table tasks add constraint tasks_platform_api_key_id_check check (
|
||||
platform_api_key_id is null
|
||||
or octet_length(platform_api_key_id) between 1 and 160
|
||||
);
|
||||
@@ -473,7 +473,47 @@ pub(super) async fn validate_schema_fingerprint(
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else {
|
||||
super::schema_guard_v24::validate_v24_execution_admission(connection).await?;
|
||||
super::schema_guard_v24::validate_v24_execution_admission(connection, current_version)
|
||||
.await?;
|
||||
}
|
||||
if current_version < 25 {
|
||||
if !super::schema_guard_v25::validate_v25_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
if !super::schema_guard_v26::validate_v26_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
if !super::schema_guard_v27::validate_v27_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
if !super::schema_guard_v28::validate_v28_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else if current_version < 26 {
|
||||
super::schema_guard_v25::validate_v25_durable_tasks(connection).await?;
|
||||
if !super::schema_guard_v26::validate_v26_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
if !super::schema_guard_v28::validate_v28_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else if current_version < 27 {
|
||||
super::schema_guard_v26::validate_v26_durable_tasks(connection).await?;
|
||||
if !super::schema_guard_v27::validate_v27_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
if !super::schema_guard_v28::validate_v28_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else if current_version < 28 {
|
||||
super::schema_guard_v26::validate_v26_base_columns(connection).await?;
|
||||
super::schema_guard_v27::validate_v27_durable_tasks(connection).await?;
|
||||
if !super::schema_guard_v28::validate_v28_absent(connection).await? {
|
||||
return Err(schema_error(current_version));
|
||||
}
|
||||
} else {
|
||||
super::schema_guard_v26::validate_v26_base_columns(connection).await?;
|
||||
super::schema_guard_v28::validate_v28_durable_tasks(connection).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -39,7 +39,24 @@ const ENTRY_COLUMNS: &[(&str, &str, bool)] = &[
|
||||
("granted_at", "timestamp with time zone", true),
|
||||
];
|
||||
|
||||
const CONSTRAINT_COUNT: &[(&str, usize)] = &[(CONTROLLERS, 4), (ENTRIES, 11)];
|
||||
/// V25 adds the nullable Task dispatch-intent link to the same reserved row. At version 24 the
|
||||
/// column must not exist yet, so the guard validates the shape of the version it describes.
|
||||
fn entry_columns(current_version: i64) -> Vec<(&'static str, &'static str, bool)> {
|
||||
let mut columns = ENTRY_COLUMNS.to_vec();
|
||||
if current_version >= 25 {
|
||||
columns.push(("task_id", "uuid", true));
|
||||
}
|
||||
columns
|
||||
}
|
||||
|
||||
fn constraint_count(table: &str, current_version: i64) -> Option<usize> {
|
||||
match table {
|
||||
CONTROLLERS => Some(4),
|
||||
ENTRIES if current_version >= 25 => Some(12),
|
||||
ENTRIES => Some(11),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
const UNIQUE_INDEXES: &[&str] = &["execution_admission_entries_owner_uidx"];
|
||||
|
||||
@@ -72,16 +89,22 @@ pub(super) async fn validate_v24_absent(
|
||||
/// Схема V24 присутствует в ожидаемой форме (для current_version >= 24).
|
||||
pub(super) async fn validate_v24_execution_admission(
|
||||
connection: &mut PgConnection,
|
||||
current_version: i64,
|
||||
) -> Result<(), MigrationError> {
|
||||
for relation in OWNED_RELATIONS {
|
||||
if !relation_exists(connection, relation).await? {
|
||||
return Err(v24_error("v24.relations"));
|
||||
}
|
||||
}
|
||||
for (table, columns) in [(CONTROLLERS, CONTROLLER_COLUMNS), (ENTRIES, ENTRY_COLUMNS)] {
|
||||
validate_columns(connection, table, columns).await?;
|
||||
let tables = [
|
||||
(CONTROLLERS, CONTROLLER_COLUMNS.to_vec()),
|
||||
(ENTRIES, entry_columns(current_version)),
|
||||
];
|
||||
for (table, columns) in tables {
|
||||
validate_columns(connection, table, &columns).await?;
|
||||
}
|
||||
for (table, expected) in CONSTRAINT_COUNT {
|
||||
for table in [CONTROLLERS, ENTRIES] {
|
||||
let expected = constraint_count(table, current_version).unwrap_or_default();
|
||||
let count: i64 = query(
|
||||
"select count(*)::bigint as count
|
||||
from pg_catalog.pg_constraint c
|
||||
@@ -95,7 +118,7 @@ pub(super) async fn validate_v24_execution_admission(
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get("count")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if count != i64::try_from(*expected).unwrap_or_default() {
|
||||
if count != i64::try_from(expected).unwrap_or_default() {
|
||||
return Err(v24_error("v24.constraints"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
//! V25 schema guard: durable background Tasks (Story 4.2, FR-22/FR-23).
|
||||
//!
|
||||
//! Схема V25 либо полностью отсутствует (миграция ещё не применена), либо присутствует ровно в
|
||||
//! ожидаемой форме: таблица `tasks` с закрытым словарём state/policy_mode и scoped uniqueness, плюс
|
||||
//! nullable `task_id` на существующей reserved-строке admission с FK и UNIQUE-индексом.
|
||||
|
||||
use sqlx::{PgConnection, Row, query};
|
||||
|
||||
use super::authority::MigrationError;
|
||||
use super::schema_guard::relation_exists;
|
||||
|
||||
const TASKS: &str = "tasks";
|
||||
const ENTRIES: &str = "execution_admission_entries";
|
||||
const SCOPED_INDEX: &str = "tasks_scoped_key_uidx";
|
||||
const TASK_INDEX: &str = "execution_admission_entries_task_uidx";
|
||||
const TASK_FK: &str = "execution_admission_entries_task_fkey";
|
||||
|
||||
const TASK_COLUMNS: &[(&str, &str, bool)] = &[
|
||||
("task_id", "uuid", false),
|
||||
("workspace_id", "text", false),
|
||||
("owner_kind", "text", false),
|
||||
("owner_id", "text", false),
|
||||
("authorization_scope", "text", false),
|
||||
("operation_id", "text", false),
|
||||
("operation_version", "integer", false),
|
||||
("scoped_key", "text", false),
|
||||
("input_fingerprint", "text", false),
|
||||
("request_id", "text", false),
|
||||
("trace_id", "text", false),
|
||||
("traceparent", "text", false),
|
||||
("trace_sampled", "boolean", false),
|
||||
("policy_mode", "text", false),
|
||||
("approval_reference", "text", true),
|
||||
("state", "text", false),
|
||||
("version", "integer", false),
|
||||
("attempts", "integer", false),
|
||||
("input_envelope", "jsonb", false),
|
||||
("progress_envelope", "jsonb", true),
|
||||
("result_envelope", "jsonb", true),
|
||||
("error_envelope", "jsonb", true),
|
||||
("execution_deadline", "timestamp with time zone", false),
|
||||
("retain_until", "timestamp with time zone", false),
|
||||
("created_at", "timestamp with time zone", false),
|
||||
("updated_at", "timestamp with time zone", false),
|
||||
];
|
||||
|
||||
fn v25_error(stage: &'static str) -> MigrationError {
|
||||
MigrationError::new(
|
||||
"partial_sequence",
|
||||
stage,
|
||||
Some(25),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
}
|
||||
|
||||
/// Схема V25 отсутствует целиком (для current_version < 25).
|
||||
pub(super) async fn validate_v25_absent(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<bool, MigrationError> {
|
||||
if relation_exists(connection, TASKS).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
if super::schema_guard::column_exists(connection, ENTRIES, "task_id").await? {
|
||||
return Ok(false);
|
||||
}
|
||||
for index in [SCOPED_INDEX, TASK_INDEX] {
|
||||
if index_exists(connection, index).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Схема V25 присутствует в ожидаемой форме (для current_version >= 25).
|
||||
pub(super) async fn validate_v25_durable_tasks(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
if !relation_exists(connection, TASKS).await? {
|
||||
return Err(v25_error("v25.relations"));
|
||||
}
|
||||
let count: i64 = query(
|
||||
"select count(*)::bigint as count from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get("count")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if count != i64::try_from(TASK_COLUMNS.len()).unwrap_or_default() {
|
||||
return Err(v25_error("v25.columns"));
|
||||
}
|
||||
for (column, data_type, nullable) in TASK_COLUMNS {
|
||||
let row = query(
|
||||
"select data_type, is_nullable from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.bind(column)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let valid = row.is_some_and(|row| {
|
||||
row.try_get::<String, _>("data_type").ok().as_deref() == Some(*data_type)
|
||||
&& row.try_get::<String, _>("is_nullable").ok().as_deref()
|
||||
== Some(if *nullable { "YES" } else { "NO" })
|
||||
});
|
||||
if !valid {
|
||||
return Err(v25_error("v25.columns"));
|
||||
}
|
||||
}
|
||||
if !super::schema_guard::column_exists(connection, ENTRIES, "task_id").await? {
|
||||
return Err(v25_error("v25.intent_column"));
|
||||
}
|
||||
for (constraint, vocabulary) in [
|
||||
(
|
||||
"tasks_state_check",
|
||||
[
|
||||
"queued",
|
||||
"working",
|
||||
"input_required",
|
||||
"outcome_unknown",
|
||||
"completed",
|
||||
"failed",
|
||||
"cancelled",
|
||||
]
|
||||
.as_slice(),
|
||||
),
|
||||
(
|
||||
"tasks_policy_mode_check",
|
||||
["synchronous", "background", "adaptive"].as_slice(),
|
||||
),
|
||||
] {
|
||||
let expression = normalized_expression(connection, TASKS, constraint).await?;
|
||||
if !expression.is_some_and(|expression| {
|
||||
super::schema_guard::enum_constraint_matches(
|
||||
&expression,
|
||||
column_of(constraint),
|
||||
vocabulary,
|
||||
)
|
||||
}) {
|
||||
return Err(v25_error("v25.vocabulary"));
|
||||
}
|
||||
}
|
||||
for (index, table) in [(SCOPED_INDEX, TASKS), (TASK_INDEX, ENTRIES)] {
|
||||
if index_unique(connection, table, index).await? != Some(true) {
|
||||
return Err(v25_error("v25.indexes"));
|
||||
}
|
||||
}
|
||||
if !constraint_exists(connection, ENTRIES, TASK_FK).await? {
|
||||
return Err(v25_error("v25.intent_fk"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn column_of(constraint: &str) -> &'static str {
|
||||
if constraint.contains("policy_mode") {
|
||||
"policy_mode"
|
||||
} else {
|
||||
"state"
|
||||
}
|
||||
}
|
||||
|
||||
async fn constraint_exists(
|
||||
connection: &mut PgConnection,
|
||||
table: &str,
|
||||
constraint: &str,
|
||||
) -> Result<bool, MigrationError> {
|
||||
query(
|
||||
"select exists (
|
||||
select 1 from pg_catalog.pg_constraint c
|
||||
join pg_catalog.pg_class t on t.oid = c.conrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema() and t.relname = $1 and c.conname = $2
|
||||
) as present",
|
||||
)
|
||||
.bind(table)
|
||||
.bind(constraint)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get::<bool, _>("present")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))
|
||||
}
|
||||
|
||||
async fn normalized_expression(
|
||||
connection: &mut PgConnection,
|
||||
table: &str,
|
||||
constraint: &str,
|
||||
) -> Result<Option<String>, MigrationError> {
|
||||
let row = query(
|
||||
"select pg_get_expr(c.conbin, c.conrelid) as expression
|
||||
from pg_catalog.pg_constraint c
|
||||
join pg_catalog.pg_class t on t.oid = c.conrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = t.relnamespace
|
||||
where n.nspname = current_schema() and t.relname = $1 and c.conname = $2
|
||||
and c.contype = 'c'",
|
||||
)
|
||||
.bind(table)
|
||||
.bind(constraint)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
Ok(row.and_then(|row| row.try_get::<String, _>("expression").ok()))
|
||||
}
|
||||
|
||||
async fn index_exists(connection: &mut PgConnection, index: &str) -> Result<bool, MigrationError> {
|
||||
query(
|
||||
"select exists (
|
||||
select 1 from pg_catalog.pg_class idx
|
||||
join pg_catalog.pg_namespace n on n.oid = idx.relnamespace
|
||||
where n.nspname = current_schema() and idx.relname = $1
|
||||
) as present",
|
||||
)
|
||||
.bind(index)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get::<bool, _>("present")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))
|
||||
}
|
||||
|
||||
async fn index_unique(
|
||||
connection: &mut PgConnection,
|
||||
table: &str,
|
||||
index: &str,
|
||||
) -> Result<Option<bool>, MigrationError> {
|
||||
let row = query(
|
||||
"select i.indisunique, i.indisvalid
|
||||
from pg_catalog.pg_index i
|
||||
join pg_catalog.pg_class idx on idx.oid = i.indexrelid
|
||||
join pg_catalog.pg_class t on t.oid = i.indrelid
|
||||
join pg_catalog.pg_namespace n on n.oid = idx.relnamespace
|
||||
where n.nspname = current_schema() and idx.relname = $1 and t.relname = $2",
|
||||
)
|
||||
.bind(index)
|
||||
.bind(table)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
Ok(row.and_then(|row| {
|
||||
let unique = row.try_get::<bool, _>("indisunique").ok()?;
|
||||
let valid = row.try_get::<bool, _>("indisvalid").ok()?;
|
||||
Some(unique && valid)
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
//! V26 schema guard: lease/fencing columns for durable background Tasks (Story 4.3).
|
||||
|
||||
use sqlx::{PgConnection, Row, query};
|
||||
|
||||
use super::authority::MigrationError;
|
||||
use super::schema_guard::relation_exists;
|
||||
|
||||
const TASKS: &str = "tasks";
|
||||
|
||||
const TASK_COLUMNS: &[(&str, &str, bool)] = &[
|
||||
("task_id", "uuid", false),
|
||||
("workspace_id", "text", false),
|
||||
("owner_kind", "text", false),
|
||||
("owner_id", "text", false),
|
||||
("authorization_scope", "text", false),
|
||||
("operation_id", "text", false),
|
||||
("operation_version", "integer", false),
|
||||
("scoped_key", "text", false),
|
||||
("input_fingerprint", "text", false),
|
||||
("request_id", "text", false),
|
||||
("trace_id", "text", false),
|
||||
("traceparent", "text", false),
|
||||
("trace_sampled", "boolean", false),
|
||||
("policy_mode", "text", false),
|
||||
("approval_reference", "text", true),
|
||||
("state", "text", false),
|
||||
("version", "integer", false),
|
||||
("attempts", "integer", false),
|
||||
("input_envelope", "jsonb", false),
|
||||
("progress_envelope", "jsonb", true),
|
||||
("result_envelope", "jsonb", true),
|
||||
("error_envelope", "jsonb", true),
|
||||
("execution_deadline", "timestamp with time zone", false),
|
||||
("retain_until", "timestamp with time zone", false),
|
||||
("created_at", "timestamp with time zone", false),
|
||||
("updated_at", "timestamp with time zone", false),
|
||||
("fence", "bigint", false),
|
||||
("lease_owner", "text", true),
|
||||
("lease_until", "timestamp with time zone", true),
|
||||
("current_attempt_id", "text", true),
|
||||
("dispatch_started_at", "timestamp with time zone", true),
|
||||
];
|
||||
|
||||
fn v26_error(stage: &'static str) -> MigrationError {
|
||||
MigrationError::new(
|
||||
"partial_sequence",
|
||||
stage,
|
||||
Some(26),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v26_absent(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<bool, MigrationError> {
|
||||
if relation_exists(connection, TASKS).await? {
|
||||
// delegate to v25 absent for relation mostly; V26 columns must not exist.
|
||||
for column in [
|
||||
"fence",
|
||||
"lease_owner",
|
||||
"lease_until",
|
||||
"current_attempt_id",
|
||||
"dispatch_started_at",
|
||||
] {
|
||||
if super::schema_guard::column_exists(connection, TASKS, column).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v26_base_columns(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
if !relation_exists(connection, TASKS).await? {
|
||||
return Err(v26_error("v26.relations"));
|
||||
}
|
||||
for (column, data_type, nullable) in TASK_COLUMNS {
|
||||
let row = query(
|
||||
"select data_type, is_nullable from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.bind(column)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let valid = row.is_some_and(|row| {
|
||||
row.try_get::<String, _>("data_type").ok().as_deref() == Some(*data_type)
|
||||
&& row.try_get::<String, _>("is_nullable").ok().as_deref()
|
||||
== Some(if *nullable { "YES" } else { "NO" })
|
||||
});
|
||||
if !valid {
|
||||
return Err(v26_error("v26.columns"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v26_durable_tasks(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
validate_v26_base_columns(connection).await?;
|
||||
let count: i64 = query(
|
||||
"select count(*)::bigint as count from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get("count")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if count != i64::try_from(TASK_COLUMNS.len()).unwrap_or_default() {
|
||||
return Err(v26_error("v26.columns"));
|
||||
}
|
||||
// Constraints/index checks inherited conceptually from V25; V26 adds columns only.
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
//! V27 schema guard: encrypted executable Task input columns (Story 4.3).
|
||||
|
||||
use sqlx::{PgConnection, Row, query};
|
||||
|
||||
use super::authority::MigrationError;
|
||||
use super::schema_guard::relation_exists;
|
||||
use super::schema_guard_v26::validate_v26_base_columns;
|
||||
|
||||
const TASKS: &str = "tasks";
|
||||
|
||||
pub(super) async fn validate_v27_absent(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<bool, MigrationError> {
|
||||
if relation_exists(connection, TASKS).await? {
|
||||
for column in [
|
||||
"executable_input_ciphertext",
|
||||
"executable_input_key_version",
|
||||
"executable_input_master_key_epoch",
|
||||
] {
|
||||
if super::schema_guard::column_exists(connection, TASKS, column).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v27_durable_tasks(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
validate_v26_base_columns(connection).await?;
|
||||
// Additional V27 columns.
|
||||
let extra = [
|
||||
("executable_input_ciphertext", "text", true),
|
||||
("executable_input_key_version", "text", true),
|
||||
("executable_input_master_key_epoch", "bigint", true),
|
||||
("target_executable_input_ciphertext", "text", true),
|
||||
("target_executable_input_key_version", "text", true),
|
||||
("target_executable_input_master_key_epoch", "bigint", true),
|
||||
];
|
||||
for (column, data_type, nullable) in extra {
|
||||
let row = query(
|
||||
"select data_type, is_nullable from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.bind(column)
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let valid = row.is_some_and(|row| {
|
||||
row.try_get::<String, _>("data_type").ok().as_deref() == Some(data_type)
|
||||
&& row.try_get::<String, _>("is_nullable").ok().as_deref()
|
||||
== Some(if nullable { "YES" } else { "NO" })
|
||||
});
|
||||
if !valid {
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"v27.columns",
|
||||
Some(27),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
}
|
||||
// Exact total column count for version 27: 43 V26 columns + 3 V27 columns.
|
||||
let count: i64 = query(
|
||||
"select count(*)::bigint as count from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get("count")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if count != i64::try_from(37).unwrap_or_default() {
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"v27.columns",
|
||||
Some(27),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//! V28 schema guard: typed Platform API key authority reference of a durable Task (Story 4.3).
|
||||
|
||||
use sqlx::{PgConnection, Row, query};
|
||||
|
||||
use super::authority::MigrationError;
|
||||
use super::schema_guard::relation_exists;
|
||||
|
||||
const TASKS: &str = "tasks";
|
||||
|
||||
pub(super) async fn validate_v28_absent(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<bool, MigrationError> {
|
||||
if relation_exists(connection, TASKS).await? {
|
||||
if super::schema_guard::column_exists(connection, TASKS, "platform_api_key_id").await? {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub(super) async fn validate_v28_durable_tasks(
|
||||
connection: &mut PgConnection,
|
||||
) -> Result<(), MigrationError> {
|
||||
// The V27 column set is still required and unchanged; V28 only appends one column.
|
||||
validate_v27_base_columns(connection).await?;
|
||||
let row = query(
|
||||
"select data_type, is_nullable from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1 and column_name = $2",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.bind("platform_api_key_id")
|
||||
.fetch_optional(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
let valid = row.is_some_and(|row| {
|
||||
row.try_get::<String, _>("data_type").ok().as_deref() == Some("text")
|
||||
&& row.try_get::<String, _>("is_nullable").ok().as_deref() == Some("YES")
|
||||
});
|
||||
if !valid {
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"v28.columns",
|
||||
Some(28),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
// Exact total column count for version 28: the 37 V27 columns + 1 V28 column.
|
||||
let count: i64 = query(
|
||||
"select count(*)::bigint as count from information_schema.columns
|
||||
where table_schema = current_schema() and table_name = $1",
|
||||
)
|
||||
.bind(TASKS)
|
||||
.fetch_one(&mut *connection)
|
||||
.await
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?
|
||||
.try_get("count")
|
||||
.map_err(|_| MigrationError::storage("preflight.schema"))?;
|
||||
if count != i64::try_from(38).unwrap_or_default() {
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"v28.columns",
|
||||
Some(28),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// V27 columns must still be exactly present before V28 is considered applied.
|
||||
async fn validate_v27_base_columns(connection: &mut PgConnection) -> Result<(), MigrationError> {
|
||||
for column in [
|
||||
"executable_input_ciphertext",
|
||||
"executable_input_key_version",
|
||||
"executable_input_master_key_epoch",
|
||||
"target_executable_input_ciphertext",
|
||||
"target_executable_input_key_version",
|
||||
"target_executable_input_master_key_epoch",
|
||||
] {
|
||||
if !super::schema_guard::column_exists(connection, TASKS, column).await? {
|
||||
return Err(MigrationError::new(
|
||||
"partial_sequence",
|
||||
"v28.columns",
|
||||
Some(28),
|
||||
"restore_known_good_backup",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -13,7 +13,8 @@ use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
AdmissionCursor, AdmissionDecision, AdmissionMode, AdmissionOutcome, AdmissionPermit,
|
||||
AdmissionQueueEntry, AdmissionQueueSnapshot, AdmissionRequest, AdmissionSchedulerConfig,
|
||||
AdmissionStoreError, ApprovalEvidence, ExecutionAdmission, NoTasksFallback, select_next_grant,
|
||||
AdmissionStoreError, ApprovalEvidence, ExecutionAdmission, NoTasksFallback, TaskError,
|
||||
select_next_grant,
|
||||
};
|
||||
use sqlx::{PgPool, Postgres, Row, Transaction};
|
||||
use uuid::Uuid;
|
||||
@@ -194,7 +195,8 @@ impl ExecutionAdmission for PostgresExecutionAdmission {
|
||||
async fn release(&self, permit: &AdmissionPermit) -> Result<bool, AdmissionStoreError> {
|
||||
let result = sqlx::query(
|
||||
"delete from execution_admission_entries
|
||||
where ticket_id = $1 and owner_token = $2 and state = 'executing'",
|
||||
where ticket_id = $1 and owner_token = $2 and state = 'executing'
|
||||
and task_id is null",
|
||||
)
|
||||
.bind(i64::try_from(permit.ticket_id).unwrap_or(i64::MAX))
|
||||
.bind(&permit.owner_token)
|
||||
@@ -207,7 +209,8 @@ impl ExecutionAdmission for PostgresExecutionAdmission {
|
||||
async fn cancel(&self, ticket_id: u64, owner_token: &str) -> Result<bool, AdmissionStoreError> {
|
||||
let result = sqlx::query(
|
||||
"delete from execution_admission_entries
|
||||
where ticket_id = $1 and owner_token = $2 and state in ('waiting', 'reserved')",
|
||||
where ticket_id = $1 and owner_token = $2 and state in ('waiting', 'reserved')
|
||||
and task_id is null",
|
||||
)
|
||||
.bind(i64::try_from(ticket_id).unwrap_or(i64::MAX))
|
||||
.bind(owner_token)
|
||||
@@ -301,9 +304,19 @@ impl PostgresExecutionAdmission {
|
||||
Ok(Attempt::Overloaded)
|
||||
}
|
||||
Err(AttemptError::Failure(error)) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(AdmissionStoreError::unavailable(error.to_string()))
|
||||
}
|
||||
Err(AttemptError::AdmissionFailure(error)) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(error)
|
||||
}
|
||||
Err(AttemptError::UniqueViolation) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(AdmissionStoreError::unavailable(
|
||||
"execution admission hit an unexpected unique conflict",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,82 +327,32 @@ impl PostgresExecutionAdmission {
|
||||
ticket: &mut Option<u64>,
|
||||
) -> Result<Attempt, AttemptError> {
|
||||
let workspace_id = context.request.scope.workspace_id.as_str();
|
||||
sqlx::query(
|
||||
"insert into execution_admission_controllers (workspace_id)
|
||||
values ($1), ($2)
|
||||
on conflict (workspace_id) do nothing",
|
||||
)
|
||||
.bind(DEPLOYMENT_CONTROLLER)
|
||||
.bind(workspace_id)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let cursor = lock_admission_in_tx(transaction, workspace_id).await?;
|
||||
|
||||
// Fixed lock order: deployment controller first, then the workspace controller.
|
||||
lock_controller(transaction, DEPLOYMENT_CONTROLLER).await?;
|
||||
let cursor = lock_controller(transaction, workspace_id).await?;
|
||||
|
||||
// Expired queue decisions and waiting tickets leave the bounded queue; held permits never do.
|
||||
sqlx::query(
|
||||
"delete from execution_admission_entries
|
||||
where ticket_id in (
|
||||
select ticket_id from execution_admission_entries
|
||||
where state in ('waiting', 'reserved') and expires_at <= now()
|
||||
order by ticket_id
|
||||
limit $1
|
||||
)",
|
||||
)
|
||||
.bind(EXPIRED_CLEANUP_BATCH)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
// Expired queue decisions and waiting tickets leave the bounded queue; held permits and
|
||||
// Task-linked dispatch intents never do (Story 4.2 owns the durable Task lifetime).
|
||||
cleanup_expired_unlinked(transaction).await?;
|
||||
|
||||
if ticket.is_none() {
|
||||
let queued: i64 = sqlx::query(
|
||||
"select count(*)::bigint as count from execution_admission_entries
|
||||
where state in ('waiting', 'reserved')",
|
||||
)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?
|
||||
.try_get("count")
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
if u64::try_from(queued).unwrap_or(u64::MAX)
|
||||
>= u64::try_from(self.limits.queue_limit).unwrap_or(u64::MAX)
|
||||
{
|
||||
let queued = bounded_queue_len(transaction).await?;
|
||||
if queued >= u64::try_from(self.limits.queue_limit).unwrap_or(u64::MAX) {
|
||||
return Err(AttemptError::Overloaded);
|
||||
}
|
||||
let state = match context.request.mode {
|
||||
AdmissionMode::Synchronous => "waiting",
|
||||
AdmissionMode::Background => "reserved",
|
||||
};
|
||||
let row = sqlx::query(
|
||||
"insert into execution_admission_entries (
|
||||
workspace_id, agent_key, operation_id, operation_version, mode, policy_mode,
|
||||
state, owner_token, fingerprint, expires_at
|
||||
) values ($1, $2, $3, $4, $5, $6, $7, $8, $9,
|
||||
to_timestamp($10::double precision / 1000.0))
|
||||
returning ticket_id",
|
||||
let ticket_id = insert_admission_entry(
|
||||
transaction,
|
||||
context.request,
|
||||
context.owner_token,
|
||||
state,
|
||||
None,
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(&context.request.scope.agent_key)
|
||||
.bind(&context.request.scope.operation_id)
|
||||
.bind(i32::try_from(context.request.scope.operation_version).unwrap_or(i32::MAX))
|
||||
.bind(context.request.mode.as_str())
|
||||
.bind(policy_mode_str(context.request))
|
||||
.bind(state)
|
||||
.bind(context.owner_token)
|
||||
.bind(&context.request.fingerprint)
|
||||
.bind(context.request.expires_at_unix_ms)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let ticket_id: i64 = row.try_get("ticket_id").map_err(AttemptError::from_sql)?;
|
||||
*ticket = Some(u64::try_from(ticket_id).unwrap_or_default());
|
||||
.await?;
|
||||
*ticket = Some(ticket_id);
|
||||
if context.request.mode == AdmissionMode::Background {
|
||||
return Ok(Attempt::Enqueued(
|
||||
u64::try_from(ticket_id).unwrap_or_default(),
|
||||
));
|
||||
return Ok(Attempt::Enqueued(ticket_id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,22 +483,260 @@ impl PostgresExecutionAdmission {
|
||||
}
|
||||
}
|
||||
|
||||
enum AttemptError {
|
||||
/// Takes both controller locks in the fixed `'*'` -> workspace order. Shared by the synchronous
|
||||
/// admission attempt and the durable Task repository, so both serialize on the same controller rows
|
||||
/// and one task/queue invariant holds. Callers own the transaction; nothing here commits or sleeps.
|
||||
pub(super) async fn lock_admission_in_tx(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
workspace_id: &str,
|
||||
) -> Result<AdmissionCursor, AttemptError> {
|
||||
sqlx::query(
|
||||
"insert into execution_admission_controllers (workspace_id)
|
||||
values ($1), ($2)
|
||||
on conflict (workspace_id) do nothing",
|
||||
)
|
||||
.bind(DEPLOYMENT_CONTROLLER)
|
||||
.bind(workspace_id)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
|
||||
lock_controller(transaction, DEPLOYMENT_CONTROLLER).await?;
|
||||
lock_controller(transaction, workspace_id).await
|
||||
}
|
||||
|
||||
/// Bounded cleanup of expired waiting/reserved rows that are not a durable Task dispatch intent.
|
||||
pub(super) async fn cleanup_expired_unlinked(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
) -> Result<(), AttemptError> {
|
||||
sqlx::query(
|
||||
"delete from execution_admission_entries
|
||||
where ticket_id in (
|
||||
select ticket_id from execution_admission_entries
|
||||
where state in ('waiting', 'reserved') and expires_at <= now()
|
||||
and task_id is null
|
||||
order by ticket_id
|
||||
limit $1
|
||||
)",
|
||||
)
|
||||
.bind(EXPIRED_CLEANUP_BATCH)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Authoritative bounded queue length: waiting and reserved rows share one budget.
|
||||
pub(super) async fn bounded_queue_len(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
) -> Result<u64, AttemptError> {
|
||||
let queued: i64 = sqlx::query(
|
||||
"select count(*)::bigint as count from execution_admission_entries
|
||||
where state in ('waiting', 'reserved')",
|
||||
)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?
|
||||
.try_get("count")
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
Ok(u64::try_from(queued).unwrap_or(u64::MAX))
|
||||
}
|
||||
|
||||
/// Inserts one bounded admission row. A Task-linked row is the durable dispatch intent of the Task
|
||||
/// accepted in the same transaction, so its capacity slot can never be silently reclaimed.
|
||||
pub(super) async fn insert_admission_entry(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
request: &AdmissionRequest,
|
||||
owner_token: &str,
|
||||
state: &str,
|
||||
task_id: Option<Uuid>,
|
||||
) -> Result<u64, AttemptError> {
|
||||
let row = sqlx::query(
|
||||
"insert into execution_admission_entries (
|
||||
workspace_id, agent_key, operation_id, operation_version, mode, policy_mode,
|
||||
state, owner_token, fingerprint, expires_at, task_id
|
||||
) values ($1, $2, $3, $4, $5, $6, $7, $8, $9,
|
||||
to_timestamp($10::double precision / 1000.0), $11)
|
||||
returning ticket_id",
|
||||
)
|
||||
.bind(&request.scope.workspace_id)
|
||||
.bind(&request.scope.agent_key)
|
||||
.bind(&request.scope.operation_id)
|
||||
.bind(i32::try_from(request.scope.operation_version).unwrap_or(i32::MAX))
|
||||
.bind(request.mode.as_str())
|
||||
.bind(policy_mode_str(request))
|
||||
.bind(state)
|
||||
.bind(owner_token)
|
||||
.bind(&request.fingerprint)
|
||||
.bind(request.expires_at_unix_ms)
|
||||
.bind(task_id)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let ticket_id: i64 = row.try_get("ticket_id").map_err(AttemptError::from_sql)?;
|
||||
Ok(u64::try_from(ticket_id).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// Shared grant step used by the durable Task store: grants one reserved Task intent if capacity allows,
|
||||
/// marks it `executing`, persists the weighted round-robin cursor, and returns its Task id. It reuses
|
||||
/// the same selector/capacity logic as synchronous admission so no second policy exists.
|
||||
pub(super) async fn grant_next_task(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
workspace_id: &str,
|
||||
cursor: &AdmissionCursor,
|
||||
limits: &AdmissionLimits,
|
||||
scheduler: AdmissionSchedulerConfig,
|
||||
) -> Result<(Option<Uuid>, AdmissionCursor), AttemptError> {
|
||||
let rows = sqlx::query(
|
||||
"select ticket_id, agent_key, operation_id, task_id
|
||||
from execution_admission_entries
|
||||
where state = 'reserved' and task_id is not null
|
||||
order by ticket_id
|
||||
limit $1
|
||||
for update skip locked",
|
||||
)
|
||||
.bind(QUEUE_READ_LIMIT)
|
||||
.fetch_all(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
if rows.is_empty() {
|
||||
return Ok((None, cursor.clone()));
|
||||
}
|
||||
struct Candidate {
|
||||
entry: AdmissionQueueEntry,
|
||||
task_id: Uuid,
|
||||
}
|
||||
let mut candidates = Vec::with_capacity(rows.len());
|
||||
for row in rows {
|
||||
let ticket_id: i64 = row.try_get("ticket_id").map_err(AttemptError::from_sql)?;
|
||||
let task_id: Uuid = row.try_get("task_id").map_err(AttemptError::from_sql)?;
|
||||
candidates.push(Candidate {
|
||||
entry: AdmissionQueueEntry {
|
||||
ticket_id: u64::try_from(ticket_id).unwrap_or_default(),
|
||||
sequence: 0,
|
||||
agent_key: row.try_get("agent_key").map_err(AttemptError::from_sql)?,
|
||||
operation_id: row
|
||||
.try_get("operation_id")
|
||||
.map_err(AttemptError::from_sql)?,
|
||||
},
|
||||
task_id,
|
||||
});
|
||||
}
|
||||
let queue: Vec<AdmissionQueueEntry> = candidates.iter().map(|c| c.entry.clone()).collect();
|
||||
let Some(selection) = select_next_grant(&queue, cursor, scheduler) else {
|
||||
return Ok((None, cursor.clone()));
|
||||
};
|
||||
let head_index = selection.index;
|
||||
let head = &candidates[head_index];
|
||||
|
||||
let global_executing: i64 = sqlx::query(
|
||||
"select count(*)::bigint as count from execution_admission_entries where state = 'executing'",
|
||||
)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?
|
||||
.try_get("count")
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let agent_counts = sqlx::query(
|
||||
"select agent_key as key, count(*)::bigint as count
|
||||
from execution_admission_entries where state = 'executing' group by agent_key",
|
||||
)
|
||||
.fetch_all(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let mut agent_map = std::collections::BTreeMap::new();
|
||||
for row in agent_counts {
|
||||
let key: String = row.try_get("key").map_err(AttemptError::from_sql)?;
|
||||
let count: i64 = row.try_get("count").map_err(AttemptError::from_sql)?;
|
||||
agent_map.insert(key, u64::try_from(count).unwrap_or_default());
|
||||
}
|
||||
let tool_counts = sqlx::query(
|
||||
"select operation_id as key, count(*)::bigint as count
|
||||
from execution_admission_entries where state = 'executing' group by operation_id",
|
||||
)
|
||||
.fetch_all(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let mut tool_map = std::collections::BTreeMap::new();
|
||||
for row in tool_counts {
|
||||
let key: String = row.try_get("key").map_err(AttemptError::from_sql)?;
|
||||
let count: i64 = row.try_get("count").map_err(AttemptError::from_sql)?;
|
||||
tool_map.insert(key, u64::try_from(count).unwrap_or_default());
|
||||
}
|
||||
|
||||
if u64::try_from(global_executing).unwrap_or(u64::MAX)
|
||||
>= u64::try_from(limits.global_limit).unwrap_or(u64::MAX)
|
||||
{
|
||||
return Ok((None, cursor.clone()));
|
||||
}
|
||||
let agent_executing = agent_map
|
||||
.get(head.entry.agent_key.as_str())
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
if agent_executing >= u64::try_from(limits.agent_limit).unwrap_or(u64::MAX) {
|
||||
return Ok((None, cursor.clone()));
|
||||
}
|
||||
let tool_executing = tool_map
|
||||
.get(head.entry.operation_id.as_str())
|
||||
.copied()
|
||||
.unwrap_or_default();
|
||||
if tool_executing >= u64::try_from(limits.tool_limit).unwrap_or(u64::MAX) {
|
||||
return Ok((None, cursor.clone()));
|
||||
}
|
||||
|
||||
sqlx::query(
|
||||
"update execution_admission_entries
|
||||
set state = 'executing', granted_at = clock_timestamp()
|
||||
where ticket_id = $1 and state = 'reserved'",
|
||||
)
|
||||
.bind(i64::try_from(head.entry.ticket_id).unwrap_or(i64::MAX))
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
|
||||
sqlx::query(
|
||||
"update execution_admission_controllers
|
||||
set cursor_agent = $2,
|
||||
cursor_agent_credit = $3,
|
||||
cursor_tool = $4,
|
||||
cursor_tool_credit = $5,
|
||||
updated_at = now()
|
||||
where workspace_id = $1",
|
||||
)
|
||||
.bind(workspace_id)
|
||||
.bind(selection.cursor.agent.as_deref())
|
||||
.bind(i32::try_from(selection.cursor.agent_credit).unwrap_or_default())
|
||||
.bind(selection.cursor.tool.as_deref())
|
||||
.bind(i32::try_from(selection.cursor.tool_credit).unwrap_or_default())
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
|
||||
Ok((Some(head.task_id), selection.cursor))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) enum AttemptError {
|
||||
/// Lock or statement timeout: the transaction made no decision and the waiter retries.
|
||||
Contended,
|
||||
/// Bounded queue capacity is exhausted: a typed overload outcome, never an unbounded wait.
|
||||
Overloaded,
|
||||
Failure(AdmissionStoreError),
|
||||
/// A foreign unique constraint aborted the transaction; the caller rolls back and rereads.
|
||||
UniqueViolation,
|
||||
/// Durable-Task side failure (the Task repository maps it to its own typed error).
|
||||
Failure(TaskError),
|
||||
/// Admission side failure.
|
||||
AdmissionFailure(AdmissionStoreError),
|
||||
}
|
||||
|
||||
impl AttemptError {
|
||||
fn from_sql(error: sqlx::Error) -> Self {
|
||||
pub(super) fn from_sql(error: sqlx::Error) -> Self {
|
||||
if let Some(database) = error.as_database_error()
|
||||
&& matches!(database.code().as_deref(), Some("55P03" | "57014"))
|
||||
{
|
||||
return Self::Contended;
|
||||
}
|
||||
Self::Failure(PostgresExecutionAdmission::unavailable(error))
|
||||
Self::AdmissionFailure(PostgresExecutionAdmission::unavailable(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -170,6 +170,69 @@ impl PostgresRegistry {
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// Authoritative revalidation of the Platform API key that authorized a durable Task (Story 4.3).
|
||||
///
|
||||
/// The key is returned only when the exact immutable ID is still active, unexpired and bound to
|
||||
/// the same workspace and agent; otherwise `None`, so a worker fails closed. Only the immutable
|
||||
/// identity is supplied here — the bearer secret is never stored or re-read.
|
||||
pub async fn revalidate_platform_api_key(
|
||||
&self,
|
||||
workspace_id: &WorkspaceId,
|
||||
agent_id: &AgentId,
|
||||
key_id: &PlatformApiKeyId,
|
||||
) -> Result<Option<PlatformApiKeyRecord>, RegistryError> {
|
||||
let row = sqlx::query(
|
||||
"select
|
||||
k.id,
|
||||
k.workspace_id,
|
||||
k.agent_id,
|
||||
k.key_kind,
|
||||
k.name,
|
||||
k.prefix,
|
||||
k.scopes_json,
|
||||
k.status,
|
||||
k.created_at,
|
||||
k.last_used_at,
|
||||
k.expires_at,
|
||||
k.allowed_origins_json
|
||||
from platform_api_keys k
|
||||
where k.id = $1
|
||||
and k.workspace_id = $2
|
||||
and k.agent_id = $3
|
||||
and k.key_kind = 'mcp_client'
|
||||
and k.status = 'active'
|
||||
and (k.expires_at is null or k.expires_at > now())
|
||||
limit 1",
|
||||
)
|
||||
.bind(key_id.as_str())
|
||||
.bind(workspace_id.as_str())
|
||||
.bind(agent_id.as_str())
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
row.map(|row| {
|
||||
Ok(PlatformApiKeyRecord {
|
||||
api_key: PlatformApiKey {
|
||||
id: PlatformApiKeyId::new(row.get::<String, _>("id")),
|
||||
workspace_id: WorkspaceId::new(row.get::<String, _>("workspace_id")),
|
||||
agent_id: row.get::<Option<String>, _>("agent_id").map(AgentId::new),
|
||||
key_kind: deserialize_enum_text(&row.get::<String, _>("key_kind"), "key_kind")?,
|
||||
name: row.get("name"),
|
||||
prefix: row.get("prefix"),
|
||||
scopes: deserialize_json_value(row.get::<Value, _>("scopes_json"))?,
|
||||
status: deserialize_enum_text(&row.get::<String, _>("status"), "status")?,
|
||||
created_at: row.get("created_at"),
|
||||
last_used_at: row.get("last_used_at"),
|
||||
expires_at: row.get("expires_at"),
|
||||
allowed_origins: deserialize_json_value(
|
||||
row.get::<Value, _>("allowed_origins_json"),
|
||||
)?,
|
||||
},
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
pub async fn get_approval_api_key_by_secret_for_agent_slug(
|
||||
&self,
|
||||
workspace_slug: &str,
|
||||
|
||||
@@ -52,6 +52,11 @@ impl PostgresRegistry {
|
||||
super::admission::admission_health(&self.pool).await
|
||||
}
|
||||
|
||||
/// Readiness probe of the durable Task state (Story 4.2).
|
||||
pub async fn durable_tasks_health(&self) -> Result<(), crank_core::TaskError> {
|
||||
super::tasks::tasks_health(&self.pool).await
|
||||
}
|
||||
|
||||
/// Authoritative admission queue readings for low-cardinality metrics.
|
||||
pub async fn execution_admission_snapshot(
|
||||
&self,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
const MASTER_KEY_PAGE_LIMIT_MAX: i64 = 10_000;
|
||||
pub(super) const MASTER_KEY_PAGE_LIMIT_MAX: i64 = 10_000;
|
||||
|
||||
impl PostgresRegistry {
|
||||
pub async fn verify_or_register_master_key_identity(
|
||||
@@ -356,12 +356,19 @@ impl PostgresRegistry {
|
||||
return Err(RegistryError::MasterKeyRotationConflict);
|
||||
}
|
||||
|
||||
let total = sqlx::query_scalar::<_, i64>(
|
||||
let secret_total = sqlx::query_scalar::<_, i64>(
|
||||
"select count(*) from secret_versions where master_key_epoch = $1",
|
||||
)
|
||||
.bind(source_epoch)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
let task_total = sqlx::query_scalar::<_, i64>(
|
||||
"select count(*) from tasks where executable_input_master_key_epoch = $1",
|
||||
)
|
||||
.bind(source_epoch)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
let total = secret_total + task_total;
|
||||
let rotation_id = format!("master-key-e{source_epoch}-to-e{target_epoch}");
|
||||
let aborted = sqlx::query(
|
||||
"select
|
||||
@@ -403,6 +410,16 @@ impl PostgresRegistry {
|
||||
.bind(target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update tasks
|
||||
set target_executable_input_ciphertext = null,
|
||||
target_executable_input_key_version = null,
|
||||
target_executable_input_master_key_epoch = null
|
||||
where target_executable_input_master_key_epoch = $1",
|
||||
)
|
||||
.bind(target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update master_key_rotations
|
||||
set state = 'running',
|
||||
@@ -662,9 +679,24 @@ impl PostgresRegistry {
|
||||
.bind(rotation.target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update tasks
|
||||
set executable_input_ciphertext = target_executable_input_ciphertext,
|
||||
executable_input_key_version = target_executable_input_key_version,
|
||||
executable_input_master_key_epoch = target_executable_input_master_key_epoch,
|
||||
target_executable_input_ciphertext = null,
|
||||
target_executable_input_key_version = null,
|
||||
target_executable_input_master_key_epoch = null
|
||||
where executable_input_master_key_epoch = $1
|
||||
and target_executable_input_master_key_epoch = $2",
|
||||
)
|
||||
.bind(rotation.source_epoch)
|
||||
.bind(rotation.target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update master_key_rotations
|
||||
set state = 'promoted',
|
||||
set state = 'promoted',
|
||||
updated_at = $2::timestamptz
|
||||
where id = $1",
|
||||
)
|
||||
@@ -697,6 +729,16 @@ impl PostgresRegistry {
|
||||
.bind(rotation.target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update tasks
|
||||
set target_executable_input_ciphertext = null,
|
||||
target_executable_input_key_version = null,
|
||||
target_executable_input_master_key_epoch = null
|
||||
where target_executable_input_master_key_epoch = $1",
|
||||
)
|
||||
.bind(rotation.target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"update master_key_rotations
|
||||
set state = 'aborted',
|
||||
@@ -883,7 +925,7 @@ async fn select_active_master_key_identity_for_update(
|
||||
Ok(row.map(map_master_key_identity))
|
||||
}
|
||||
|
||||
async fn select_master_key_rotation_for_update(
|
||||
pub(super) async fn select_master_key_rotation_for_update(
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
rotation_id: &str,
|
||||
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
||||
@@ -915,16 +957,19 @@ async fn select_master_key_rotation_for_update(
|
||||
})
|
||||
}
|
||||
|
||||
async fn count_staged_rotation_targets(
|
||||
pub(super) async fn count_staged_rotation_targets(
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
source_epoch: i64,
|
||||
target_epoch: i64,
|
||||
) -> Result<i64, RegistryError> {
|
||||
let count = sqlx::query_scalar::<_, i64>(
|
||||
"select count(*)
|
||||
from secret_versions
|
||||
where master_key_epoch = $1
|
||||
and target_master_key_epoch = $2",
|
||||
"select
|
||||
(select count(*) from secret_versions
|
||||
where master_key_epoch = $1
|
||||
and target_master_key_epoch = $2) +
|
||||
(select count(*) from tasks
|
||||
where executable_input_master_key_epoch = $1
|
||||
and target_executable_input_master_key_epoch = $2)",
|
||||
)
|
||||
.bind(source_epoch)
|
||||
.bind(target_epoch)
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
//! Task ciphertext paging/staging of the master-key rotation (Story 4.3).
|
||||
//!
|
||||
//! Extracted from `master_key.rs` without logic changes so that module stays within the
|
||||
//! code-health line limit; the operations and their SQL are unchanged.
|
||||
|
||||
use super::master_key::{
|
||||
MASTER_KEY_PAGE_LIMIT_MAX, count_staged_rotation_targets, select_master_key_rotation_for_update,
|
||||
};
|
||||
use super::*;
|
||||
|
||||
fn map_master_key_task_version(
|
||||
row: sqlx::postgres::PgRow,
|
||||
) -> Result<MasterKeyTaskVersion, RegistryError> {
|
||||
Ok(MasterKeyTaskVersion {
|
||||
task_id: row.get::<String, _>("task_id"),
|
||||
ciphertext: row
|
||||
.get::<Option<String>, _>("executable_input_ciphertext")
|
||||
.unwrap_or_default(),
|
||||
key_version: row
|
||||
.get::<Option<String>, _>("executable_input_key_version")
|
||||
.unwrap_or_default(),
|
||||
epoch: row
|
||||
.get::<Option<i64>, _>("executable_input_master_key_epoch")
|
||||
.unwrap_or_default(),
|
||||
target_ciphertext: row.get::<Option<String>, _>("target_executable_input_ciphertext"),
|
||||
target_key_version: row.get::<Option<String>, _>("target_executable_input_key_version"),
|
||||
target_epoch: row.get::<Option<i64>, _>("target_executable_input_master_key_epoch"),
|
||||
})
|
||||
}
|
||||
|
||||
impl PostgresRegistry {
|
||||
pub async fn list_tasks_for_master_key_epoch_page(
|
||||
&self,
|
||||
epoch: i64,
|
||||
after_task_id: Option<&str>,
|
||||
limit: i64,
|
||||
) -> Result<Vec<MasterKeyTaskVersion>, RegistryError> {
|
||||
if epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) {
|
||||
return Err(RegistryError::InvalidMasterKeyIdentity);
|
||||
}
|
||||
let rows = sqlx::query(
|
||||
"select task_id::text as task_id, executable_input_ciphertext, executable_input_key_version, executable_input_master_key_epoch, target_executable_input_ciphertext, target_executable_input_key_version, target_executable_input_master_key_epoch from tasks where executable_input_master_key_epoch = $1 and ($2::text is null or task_id::text > $2::text) order by task_id asc limit $3",
|
||||
)
|
||||
.bind(epoch)
|
||||
.bind(after_task_id)
|
||||
.bind(limit)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
rows.into_iter().map(map_master_key_task_version).collect()
|
||||
}
|
||||
|
||||
pub async fn list_target_tasks_for_master_key_rotation_page(
|
||||
&self,
|
||||
target_epoch: i64,
|
||||
after_task_id: Option<&str>,
|
||||
limit: i64,
|
||||
) -> Result<Vec<MasterKeyTaskVersion>, RegistryError> {
|
||||
if target_epoch < 1 || !(1..=MASTER_KEY_PAGE_LIMIT_MAX).contains(&limit) {
|
||||
return Err(RegistryError::InvalidMasterKeyIdentity);
|
||||
}
|
||||
let rows = sqlx::query(
|
||||
"select task_id::text as task_id, executable_input_ciphertext, executable_input_key_version, executable_input_master_key_epoch, target_executable_input_ciphertext, target_executable_input_key_version, target_executable_input_master_key_epoch from tasks where target_executable_input_master_key_epoch = $1 and ($2::text is null or task_id::text > $2::text) order by task_id asc limit $3",
|
||||
)
|
||||
.bind(target_epoch)
|
||||
.bind(after_task_id)
|
||||
.bind(limit)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
rows.into_iter().map(map_master_key_task_version).collect()
|
||||
}
|
||||
|
||||
pub async fn stage_master_key_rotation_task_ciphertext(
|
||||
&self,
|
||||
rotation_id: &str,
|
||||
task_id: &str,
|
||||
source_epoch: i64,
|
||||
target_ciphertext: &str,
|
||||
target_key_version: &str,
|
||||
target_epoch: i64,
|
||||
now: &OffsetDateTime,
|
||||
) -> Result<MasterKeyRotationRecord, RegistryError> {
|
||||
let mut tx = self.pool.begin().await?;
|
||||
let rotation = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
||||
if rotation.state != "running"
|
||||
|| rotation.source_epoch != source_epoch
|
||||
|| rotation.target_epoch != target_epoch
|
||||
{
|
||||
return Err(RegistryError::MasterKeyRotationConflict);
|
||||
}
|
||||
let result = sqlx::query(
|
||||
"update tasks
|
||||
set target_executable_input_ciphertext = $3,
|
||||
target_executable_input_key_version = $4,
|
||||
target_executable_input_master_key_epoch = $5
|
||||
where task_id::text = $1
|
||||
and executable_input_master_key_epoch = $2
|
||||
and (
|
||||
target_executable_input_master_key_epoch is null
|
||||
or target_executable_input_master_key_epoch = $5
|
||||
)
|
||||
and (
|
||||
target_executable_input_ciphertext is null
|
||||
or target_executable_input_ciphertext = $3
|
||||
)",
|
||||
)
|
||||
.bind(task_id)
|
||||
.bind(source_epoch)
|
||||
.bind(target_ciphertext)
|
||||
.bind(target_key_version)
|
||||
.bind(target_epoch)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
if result.rows_affected() != 1 {
|
||||
return Err(RegistryError::MasterKeyRotationConflict);
|
||||
}
|
||||
let processed = count_staged_rotation_targets(&mut tx, source_epoch, target_epoch).await?;
|
||||
sqlx::query(
|
||||
"update master_key_rotations
|
||||
set checkpoint_secret_id = checkpoint_secret_id,
|
||||
processed_secret_versions = $2,
|
||||
updated_at = $3::timestamptz
|
||||
where id = $1",
|
||||
)
|
||||
.bind(rotation_id)
|
||||
.bind(processed)
|
||||
.bind(now)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
let updated = select_master_key_rotation_for_update(&mut tx, rotation_id).await?;
|
||||
tx.commit().await?;
|
||||
Ok(updated)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MasterKeyTaskVersion {
|
||||
pub task_id: String,
|
||||
pub ciphertext: String,
|
||||
pub key_version: String,
|
||||
pub epoch: i64,
|
||||
pub target_ciphertext: Option<String>,
|
||||
pub target_key_version: Option<String>,
|
||||
pub target_epoch: Option<i64>,
|
||||
}
|
||||
@@ -10,6 +10,7 @@ mod auth;
|
||||
mod connection;
|
||||
mod import_job;
|
||||
mod master_key;
|
||||
mod master_key_tasks;
|
||||
mod observability;
|
||||
mod onboarding;
|
||||
mod onboarding_import;
|
||||
@@ -21,6 +22,7 @@ mod product_event;
|
||||
mod prompt;
|
||||
mod resource;
|
||||
mod secret;
|
||||
mod tasks;
|
||||
mod upstream;
|
||||
mod workspace;
|
||||
mod yaml_import;
|
||||
@@ -42,6 +44,8 @@ pub use admission::{
|
||||
};
|
||||
pub use pool_config::{PostgresPoolConfig, PostgresPoolConfigError};
|
||||
pub use resource::{AuthorizedResourceRead, BoundResourceAdmission};
|
||||
pub use tasks::PostgresTaskStore;
|
||||
pub use tasks::tasks_health;
|
||||
|
||||
use crate::{
|
||||
error::RegistryError,
|
||||
|
||||
@@ -0,0 +1,984 @@
|
||||
//! PostgreSQL implementation of the durable Task store (Story 4.2, FR-22/FR-23).
|
||||
//!
|
||||
//! One PostgreSQL transaction owns the whole acceptance: Task record, dispatch intent
|
||||
//! (`execution_admission_entries.task_id`) and its queue slot commit together, so no Task ID and no
|
||||
//! acknowledgement can exist without the durable record. The transaction reuses the transaction-aware
|
||||
//! primitives of the execution-admission store (`lock_admission_in_tx`, `bounded_queue_len`,
|
||||
//! `insert_admission_entry`) instead of copying capacity SQL or starting a nested transaction.
|
||||
//!
|
||||
//! Replay is resolved before any capacity mutation and never touches the accepted row. An ambiguous
|
||||
//! COMMIT is reported as an unavailable store: it is never treated as a proven rollback, so no
|
||||
//! compensating delete and no synchronous fallback can happen after an uncertain acceptance.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{
|
||||
AdmissionMode, AdmissionRequest, AdmissionScope, ApprovalEvidence, NoTasksFallback,
|
||||
PlatformApiKeyId, TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskEnvelope, TaskError,
|
||||
TaskFinishRequest, TaskId, TaskLease, TaskOwnerKind, TaskPins, TaskPurgeReport, TaskRecord,
|
||||
TaskState, TaskStore, TaskSupportIds, ToolExecutionMode,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use sqlx::{PgPool, Postgres, Row, Transaction};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::admission::{
|
||||
AdmissionLimits, AttemptError, bounded_queue_len, cleanup_expired_unlinked,
|
||||
insert_admission_entry, lock_admission_in_tx,
|
||||
};
|
||||
|
||||
const TASK_PROJECTION: &str = "task_id, state, workspace_id, owner_kind, owner_id,
|
||||
authorization_scope, platform_api_key_id, operation_id, input_fingerprint, operation_version,
|
||||
policy_mode,
|
||||
approval_reference,
|
||||
request_id, trace_id, traceparent, trace_sampled, input_envelope, progress_envelope,
|
||||
result_envelope, error_envelope, attempts, fence, lease_owner, lease_until,
|
||||
(extract(epoch from lease_until) * 1000)::bigint as lease_until_ms,
|
||||
current_attempt_id, dispatch_started_at,
|
||||
(extract(epoch from dispatch_started_at) * 1000)::bigint as dispatch_started_at_ms, version,
|
||||
(extract(epoch from execution_deadline) * 1000)::bigint as execution_deadline_ms,
|
||||
(extract(epoch from retain_until) * 1000)::bigint as retain_until_ms,
|
||||
(extract(epoch from created_at) * 1000)::bigint as created_ms,
|
||||
(extract(epoch from updated_at) * 1000)::bigint as updated_ms";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresTaskStore {
|
||||
pool: PgPool,
|
||||
limits: AdmissionLimits,
|
||||
scheduler: crank_core::AdmissionSchedulerConfig,
|
||||
}
|
||||
|
||||
impl PostgresTaskStore {
|
||||
pub fn new(
|
||||
pool: PgPool,
|
||||
limits: AdmissionLimits,
|
||||
scheduler: crank_core::AdmissionSchedulerConfig,
|
||||
) -> Result<Self, TaskError> {
|
||||
limits
|
||||
.validate()
|
||||
.map_err(|error| TaskError::invalid(error.to_string()))?;
|
||||
scheduler
|
||||
.validate()
|
||||
.map_err(|error| TaskError::invalid(error.to_string()))?;
|
||||
Ok(Self {
|
||||
pool,
|
||||
limits,
|
||||
scheduler,
|
||||
})
|
||||
}
|
||||
|
||||
fn unavailable(error: impl std::fmt::Display) -> TaskError {
|
||||
TaskError::unavailable(format!("durable task store is unavailable: {error}"))
|
||||
}
|
||||
|
||||
async fn start(&self) -> Result<Transaction<'_, Postgres>, TaskError> {
|
||||
let mut transaction = self.pool.begin().await.map_err(Self::unavailable)?;
|
||||
sqlx::query("set local lock_timeout = '3000ms'")
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
sqlx::query("set local statement_timeout = '5000ms'")
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
/// Scoped lookup of an accepted Task. The full owner/authorization/Tool predicate is part of the
|
||||
/// query, so a foreign row is never fetched.
|
||||
async fn stored_acceptance(
|
||||
&self,
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
request: &TaskAcceptRequest,
|
||||
) -> Result<Option<(TaskAcceptance, String)>, AttemptError> {
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"select {TASK_PROJECTION} from tasks
|
||||
where workspace_id = $1 and owner_kind = $2 and owner_id = $3
|
||||
and authorization_scope = $4 and operation_id = $5 and scoped_key = $6"
|
||||
)))
|
||||
.bind(&request.workspace_id)
|
||||
.bind(request.owner_kind.as_str())
|
||||
.bind(&request.owner_id)
|
||||
.bind(&request.authorization_scope)
|
||||
.bind(&request.operation_id)
|
||||
.bind(&request.scoped_key)
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
let Some(row) = row else {
|
||||
return Ok(None);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(AttemptError::Failure)?;
|
||||
Ok(Some((
|
||||
acceptance_from_record(&record),
|
||||
record.input_fingerprint,
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
fn acceptance_from_record(record: &TaskRecord) -> TaskAcceptance {
|
||||
TaskAcceptance {
|
||||
task_id: record.task_id,
|
||||
state: record.state,
|
||||
operation_version: record.pins.operation_version,
|
||||
support: record.support.clone(),
|
||||
created_at_unix_ms: record.created_at_unix_ms,
|
||||
retain_until_unix_ms: record.pins.retain_until_unix_ms,
|
||||
replayed: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Readiness probe of the durable Task state: the table must be reachable and readable. A missing or
|
||||
/// unreadable schema is a dependency failure, never a degraded-but-ready state.
|
||||
pub async fn tasks_health(pool: &PgPool) -> Result<(), TaskError> {
|
||||
sqlx::query("select count(*)::bigint from tasks where false")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.map_err(PostgresTaskStore::unavailable)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// One transaction's decision of the acceptance path.
|
||||
enum Accepted {
|
||||
Created(TaskAcceptance),
|
||||
Replayed(TaskAcceptance),
|
||||
Conflict,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TaskStore for PostgresTaskStore {
|
||||
async fn health(&self) -> Result<(), TaskError> {
|
||||
sqlx::query("select count(*)::bigint from tasks where false")
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
super::admission::admission_health(&self.pool)
|
||||
.await
|
||||
.map_err(|error| TaskError::unavailable(error.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn accept(&self, request: TaskAcceptRequest) -> Result<TaskAcceptance, TaskError> {
|
||||
request.validate()?;
|
||||
let mut transaction = self.start().await?;
|
||||
super::master_key::ensure_no_active_master_key_rotation(&mut transaction)
|
||||
.await
|
||||
.map_err(|error| match error {
|
||||
crate::RegistryError::Storage(_) => TaskError::unavailable(error.to_string()),
|
||||
_ => TaskError::StateConflict,
|
||||
})?;
|
||||
if let Some(epoch) = request.executable_input_master_key_epoch {
|
||||
super::master_key::ensure_active_master_key_epoch(&mut transaction, epoch)
|
||||
.await
|
||||
.map_err(|error| match error {
|
||||
crate::RegistryError::Storage(_) => TaskError::unavailable(error.to_string()),
|
||||
_ => TaskError::StateConflict,
|
||||
})?;
|
||||
}
|
||||
let outcome = self.accept_locked(&mut transaction, &request).await;
|
||||
match outcome {
|
||||
Ok(Accepted::Created(acceptance) | Accepted::Replayed(acceptance)) => {
|
||||
// The acknowledgement of a created Task is only possible after a real COMMIT.
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(TaskAcceptance {
|
||||
replayed: acceptance.replayed,
|
||||
..acceptance
|
||||
})
|
||||
}
|
||||
Ok(Accepted::Conflict) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(TaskError::FingerprintConflict)
|
||||
}
|
||||
Err(AttemptError::Overloaded) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(TaskError::Overloaded {
|
||||
retry_after_ms: crank_core::ADMISSION_OVERLOAD_RETRY_AFTER_MS,
|
||||
})
|
||||
}
|
||||
Err(AttemptError::Contended) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(TaskError::unavailable(
|
||||
"durable task acceptance could not take the admission locks",
|
||||
))
|
||||
}
|
||||
Err(AttemptError::UniqueViolation) => {
|
||||
// Roll back the aborted transaction, then reread the scoped key once outside it.
|
||||
transaction.rollback().await.ok();
|
||||
self.reread_after_unique(&request).await
|
||||
}
|
||||
Err(AttemptError::Failure(error)) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(error)
|
||||
}
|
||||
Err(AttemptError::AdmissionFailure(error)) => {
|
||||
transaction.rollback().await.ok();
|
||||
Err(TaskError::unavailable(error.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn read(&self, access: &TaskAccess, task_id: TaskId) -> Result<TaskRecord, TaskError> {
|
||||
access.validate()?;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"select {TASK_PROJECTION} from tasks
|
||||
where task_id = $1 and workspace_id = $2 and owner_kind = $3
|
||||
and owner_id = $4 and authorization_scope = $5"
|
||||
)))
|
||||
.bind(task_id.as_uuid())
|
||||
.bind(&access.workspace_id)
|
||||
.bind(access.owner_kind.as_str())
|
||||
.bind(&access.owner_id)
|
||||
.bind(&access.authorization_scope)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
return Err(TaskError::NotFound);
|
||||
};
|
||||
record_from_row(&row).map_err(Self::unavailable)
|
||||
}
|
||||
|
||||
async fn find(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
operation_id: &str,
|
||||
scoped_key: &str,
|
||||
) -> Result<Option<TaskRecord>, TaskError> {
|
||||
access.validate()?;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"select {TASK_PROJECTION} from tasks
|
||||
where workspace_id = $1 and owner_kind = $2 and owner_id = $3
|
||||
and authorization_scope = $4 and operation_id = $5 and scoped_key = $6"
|
||||
)))
|
||||
.bind(&access.workspace_id)
|
||||
.bind(access.owner_kind.as_str())
|
||||
.bind(&access.owner_id)
|
||||
.bind(&access.authorization_scope)
|
||||
.bind(operation_id)
|
||||
.bind(scoped_key)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
row.map(|row| record_from_row(&row)).transpose()
|
||||
}
|
||||
|
||||
async fn finish(&self, request: TaskFinishRequest) -> Result<TaskRecord, TaskError> {
|
||||
request.validate()?;
|
||||
let mut transaction = self.start().await?;
|
||||
let access = &request.access;
|
||||
let update = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set state = $1,
|
||||
result_envelope = $2,
|
||||
error_envelope = $3,
|
||||
version = version + 1,
|
||||
updated_at = now()
|
||||
where task_id = $4 and workspace_id = $5 and owner_kind = $6
|
||||
and owner_id = $7 and authorization_scope = $8
|
||||
and state = $9 and version = $10
|
||||
and (lease_owner is null or fence = 0)
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(request.target_state.as_str())
|
||||
.bind(request.result.as_ref().map(|value| value.value().clone()))
|
||||
.bind(request.error.as_ref().map(|value| value.value().clone()))
|
||||
.bind(request.task_id.as_uuid())
|
||||
.bind(&access.workspace_id)
|
||||
.bind(access.owner_kind.as_str())
|
||||
.bind(&access.owner_id)
|
||||
.bind(&access.authorization_scope)
|
||||
.bind(request.expected_state.as_str())
|
||||
.bind(i64::from(request.expected_version))
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = update else {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(self.classify_missed_finish(&request).await);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
// Terminal Task and its dispatch intent leave together: a half-done release is rolled back.
|
||||
sqlx::query("delete from execution_admission_entries where task_id = $1")
|
||||
.bind(request.task_id.as_uuid())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
async fn purge_expired(&self, batch: u32) -> Result<TaskPurgeReport, TaskError> {
|
||||
let batch = i64::from(batch.clamp(1, crank_core::TASK_PURGE_BATCH_LIMIT));
|
||||
let mut transaction = self.start().await?;
|
||||
super::master_key::ensure_no_active_master_key_rotation(&mut transaction)
|
||||
.await
|
||||
.map_err(|error| match error {
|
||||
crate::RegistryError::Storage(_) => TaskError::unavailable(error.to_string()),
|
||||
_ => TaskError::StateConflict,
|
||||
})?;
|
||||
let expired = sqlx::query(
|
||||
"with expired as (
|
||||
update tasks
|
||||
set state = 'failed',
|
||||
error_envelope = jsonb_build_object('reason', 'execution_deadline_elapsed'),
|
||||
version = version + 1,
|
||||
updated_at = now()
|
||||
where task_id in (
|
||||
select task_id from tasks
|
||||
where state = 'queued' and execution_deadline <= now()
|
||||
and lease_owner is null
|
||||
order by task_id
|
||||
limit $1
|
||||
)
|
||||
returning task_id
|
||||
), released as (
|
||||
delete from execution_admission_entries
|
||||
where task_id in (select task_id from expired)
|
||||
returning task_id
|
||||
)
|
||||
select (select count(*) from expired)::bigint as expired_queued",
|
||||
)
|
||||
.bind(batch)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let expired_queued: i64 = expired
|
||||
.try_get("expired_queued")
|
||||
.map_err(Self::unavailable)?;
|
||||
let purged = sqlx::query(
|
||||
"delete from tasks
|
||||
where task_id in (
|
||||
select task_id from tasks
|
||||
where state in ('completed', 'failed', 'cancelled') and retain_until <= now()
|
||||
order by task_id
|
||||
limit $1
|
||||
)",
|
||||
)
|
||||
.bind(batch)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(TaskPurgeReport {
|
||||
expired_queued: u64::try_from(expired_queued).unwrap_or_default(),
|
||||
expired_terminal: purged.rows_affected(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn claim_batch(
|
||||
&self,
|
||||
owner: &str,
|
||||
batch_limit: u32,
|
||||
lease_ms: i64,
|
||||
) -> Result<Vec<TaskLease>, TaskError> {
|
||||
let batch = (batch_limit.clamp(1, 64)) as usize;
|
||||
// A checkpointed attempt is never redispatched: once its lease expires the outbound outcome
|
||||
// is uncertain, so it resolves to outcome_unknown and releases its admission slot. Story 4.4
|
||||
// owns any later retry/cancellation of that uncertain outcome.
|
||||
let mut transaction = self.start().await?;
|
||||
sqlx::query(
|
||||
"with candidates as (
|
||||
select task_id from tasks
|
||||
where state = 'working' and lease_owner is not null
|
||||
and current_attempt_id is not null
|
||||
and lease_until < clock_timestamp()
|
||||
order by task_id
|
||||
limit 64
|
||||
), resolved as (
|
||||
update tasks
|
||||
set state = 'outcome_unknown',
|
||||
lease_owner = null,
|
||||
lease_until = null,
|
||||
version = version + 1,
|
||||
updated_at = now()
|
||||
where task_id in (select task_id from candidates)
|
||||
and state = 'working'
|
||||
returning task_id
|
||||
)
|
||||
delete from execution_admission_entries
|
||||
where task_id in (select task_id from resolved)",
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
|
||||
// A crash before the durable dispatch checkpoint left the Task queued for execution: its
|
||||
// admission intent is re-armed as reserved and the Task becomes claimable again. Recovery is
|
||||
// bounded by the <=30s lease expiry, inside the 60s story budget.
|
||||
sqlx::query(
|
||||
"with candidates as (
|
||||
select task_id from tasks
|
||||
where state = 'working' and lease_owner is not null
|
||||
and current_attempt_id is null
|
||||
and lease_until < clock_timestamp()
|
||||
order by task_id
|
||||
limit 64
|
||||
), requeued as (
|
||||
update tasks
|
||||
set state = 'queued',
|
||||
lease_owner = null,
|
||||
lease_until = null,
|
||||
version = version + 1,
|
||||
updated_at = now()
|
||||
where task_id in (select task_id from candidates)
|
||||
and state = 'working'
|
||||
returning task_id
|
||||
)
|
||||
update execution_admission_entries
|
||||
set state = 'reserved', granted_at = null
|
||||
where task_id in (select task_id from requeued) and state = 'executing'",
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
|
||||
let mut leases = Vec::new();
|
||||
for _ in 0..batch {
|
||||
let mut transaction = self.start().await?;
|
||||
let workspace_cursor =
|
||||
match super::admission::lock_admission_in_tx(&mut transaction, "*").await {
|
||||
Ok(cursor) => cursor,
|
||||
Err(error) => {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::unavailable(format!("{error:?}")));
|
||||
}
|
||||
};
|
||||
let (task_id, _updated_cursor) = match super::admission::grant_next_task(
|
||||
&mut transaction,
|
||||
"*",
|
||||
&workspace_cursor,
|
||||
&self.limits,
|
||||
self.scheduler,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::unavailable(format!("{error:?}")));
|
||||
}
|
||||
};
|
||||
// Persist the updated controller cursor even when no task is granted.
|
||||
let Some(task_id) = task_id else {
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
break;
|
||||
};
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set state = 'working',
|
||||
lease_owner = $1,
|
||||
lease_until = clock_timestamp() + ($2::bigint || ' milliseconds')::interval,
|
||||
fence = fence + 1,
|
||||
updated_at = now()
|
||||
where task_id = $3
|
||||
and (
|
||||
(state = 'queued' and lease_owner is null)
|
||||
or (state = 'working' and lease_owner is not null
|
||||
and lease_until < clock_timestamp() and current_attempt_id is null)
|
||||
)
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(owner)
|
||||
.bind(lease_ms)
|
||||
.bind(task_id)
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
// Persist cursor update (grant_next_task already did it) regardless.
|
||||
let Some(row) = row else {
|
||||
transaction.rollback().await.ok();
|
||||
break;
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
leases.push(TaskLease {
|
||||
owner: owner.to_owned(),
|
||||
fence: record.fence,
|
||||
lease_until: record.lease_until_unix_ms.unwrap_or_default(),
|
||||
record,
|
||||
});
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
}
|
||||
Ok(leases)
|
||||
}
|
||||
|
||||
async fn renew_lease(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
lease_ms: i64,
|
||||
) -> Result<TaskLease, TaskError> {
|
||||
let mut transaction = self.start().await?;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set lease_until = clock_timestamp() + ($1::bigint || ' milliseconds')::interval,
|
||||
updated_at = now()
|
||||
where task_id = $2 and lease_owner = $3 and fence = $4
|
||||
and lease_until > clock_timestamp()
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(lease_ms)
|
||||
.bind(task_id.as_uuid())
|
||||
.bind(owner)
|
||||
.bind(i64::try_from(fence).unwrap_or_default())
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::StateConflict);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
let lease = TaskLease {
|
||||
owner: owner.to_owned(),
|
||||
fence: record.fence,
|
||||
lease_until: record.lease_until_unix_ms.unwrap_or_default(),
|
||||
record,
|
||||
};
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(lease)
|
||||
}
|
||||
|
||||
async fn write_progress(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
progress: TaskEnvelope,
|
||||
) -> Result<TaskRecord, TaskError> {
|
||||
let mut transaction = self.start().await?;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set progress_envelope = $1, updated_at = now()
|
||||
where task_id = $2 and lease_owner = $3 and fence = $4
|
||||
and lease_until > clock_timestamp()
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(progress.value().clone())
|
||||
.bind(task_id.as_uuid())
|
||||
.bind(owner)
|
||||
.bind(i64::try_from(fence).unwrap_or_default())
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::StateConflict);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
async fn read_executable_input(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
) -> Result<(String, String, i64), TaskError> {
|
||||
let row = sqlx::query(
|
||||
"select executable_input_ciphertext, executable_input_key_version, executable_input_master_key_epoch
|
||||
from tasks
|
||||
where task_id = $1 and state = 'working' and lease_owner = $2 and fence = $3
|
||||
and lease_until > clock_timestamp()",
|
||||
)
|
||||
.bind(task_id.as_uuid())
|
||||
.bind(owner)
|
||||
.bind(i64::try_from(fence).unwrap_or_default())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
return Err(TaskError::StateConflict);
|
||||
};
|
||||
let ciphertext: Option<String> = row
|
||||
.try_get("executable_input_ciphertext")
|
||||
.map_err(Self::unavailable)?;
|
||||
let key_version: Option<String> = row
|
||||
.try_get("executable_input_key_version")
|
||||
.map_err(Self::unavailable)?;
|
||||
let epoch: Option<i64> = row
|
||||
.try_get("executable_input_master_key_epoch")
|
||||
.map_err(Self::unavailable)?;
|
||||
if let Some(epoch) = epoch {
|
||||
let active = sqlx::query_scalar::<_, i64>(
|
||||
"select epoch from master_key_identities where status = 'active' order by epoch desc limit 1",
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
if let Some(active) = active {
|
||||
if active != epoch {
|
||||
return Err(TaskError::StateConflict);
|
||||
}
|
||||
}
|
||||
}
|
||||
match (ciphertext, key_version, epoch) {
|
||||
(Some(ciphertext), Some(key_version), Some(epoch)) if !ciphertext.is_empty() => {
|
||||
Ok((ciphertext, key_version, epoch))
|
||||
}
|
||||
_ => Err(TaskError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
async fn checkpoint_dispatch(
|
||||
&self,
|
||||
owner: &str,
|
||||
task_id: TaskId,
|
||||
fence: u64,
|
||||
attempt_id: String,
|
||||
) -> Result<TaskRecord, TaskError> {
|
||||
let mut transaction = self.start().await?;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set current_attempt_id = $1,
|
||||
dispatch_started_at = now(),
|
||||
attempts = attempts + 1,
|
||||
updated_at = now()
|
||||
where task_id = $2 and lease_owner = $3 and fence = $4
|
||||
and lease_until > clock_timestamp() and current_attempt_id is null
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(&attempt_id)
|
||||
.bind(task_id.as_uuid())
|
||||
.bind(owner)
|
||||
.bind(i64::try_from(fence).unwrap_or_default())
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::StateConflict);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(record)
|
||||
}
|
||||
|
||||
async fn finish_leased(
|
||||
&self,
|
||||
request: TaskFinishRequest,
|
||||
owner: &str,
|
||||
fence: u64,
|
||||
) -> Result<TaskRecord, TaskError> {
|
||||
request.validate()?;
|
||||
let mut transaction = self.start().await?;
|
||||
let access = &request.access;
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"update tasks
|
||||
set state = $1,
|
||||
result_envelope = $2,
|
||||
error_envelope = $3,
|
||||
version = version + 1,
|
||||
updated_at = now(),
|
||||
lease_owner = null,
|
||||
lease_until = null
|
||||
where task_id = $4 and workspace_id = $5 and owner_kind = $6
|
||||
and owner_id = $7 and authorization_scope = $8
|
||||
and state = $9 and version = $10
|
||||
and lease_owner = $11 and fence = $12
|
||||
and lease_until > clock_timestamp()
|
||||
returning {TASK_PROJECTION}"
|
||||
)))
|
||||
.bind(request.target_state.as_str())
|
||||
.bind(request.result.as_ref().map(|value| value.value().clone()))
|
||||
.bind(request.error.as_ref().map(|value| value.value().clone()))
|
||||
.bind(request.task_id.as_uuid())
|
||||
.bind(&access.workspace_id)
|
||||
.bind(access.owner_kind.as_str())
|
||||
.bind(&access.owner_id)
|
||||
.bind(&access.authorization_scope)
|
||||
.bind(request.expected_state.as_str())
|
||||
.bind(i64::from(request.expected_version))
|
||||
.bind(owner)
|
||||
.bind(i64::try_from(fence).unwrap_or_default())
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
transaction.rollback().await.ok();
|
||||
return Err(TaskError::StateConflict);
|
||||
};
|
||||
let record = record_from_row(&row).map_err(Self::unavailable)?;
|
||||
sqlx::query("delete from execution_admission_entries where task_id = $1")
|
||||
.bind(request.task_id.as_uuid())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
transaction.commit().await.map_err(Self::unavailable)?;
|
||||
Ok(record)
|
||||
}
|
||||
}
|
||||
|
||||
impl PostgresTaskStore {
|
||||
async fn accept_locked(
|
||||
&self,
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
request: &TaskAcceptRequest,
|
||||
) -> Result<Accepted, AttemptError> {
|
||||
// Replay first: an already accepted Task is returned without capacity mutation, so it works
|
||||
// with a full queue and after the original execution deadline while the record is retained.
|
||||
if let Some((acceptance, fingerprint)) =
|
||||
self.stored_acceptance(transaction, request).await?
|
||||
{
|
||||
return Ok(if fingerprint == request.input_fingerprint {
|
||||
Accepted::Replayed(acceptance)
|
||||
} else {
|
||||
Accepted::Conflict
|
||||
});
|
||||
}
|
||||
|
||||
// Serialize with synchronous admission and any concurrent acceptance of the same scope.
|
||||
lock_admission_in_tx(transaction, &request.workspace_id).await?;
|
||||
|
||||
// The competitor may have committed while this transaction waited for the controller lock.
|
||||
if let Some((acceptance, fingerprint)) =
|
||||
self.stored_acceptance(transaction, request).await?
|
||||
{
|
||||
return Ok(if fingerprint == request.input_fingerprint {
|
||||
Accepted::Replayed(acceptance)
|
||||
} else {
|
||||
Accepted::Conflict
|
||||
});
|
||||
}
|
||||
|
||||
cleanup_expired_unlinked(transaction).await?;
|
||||
let queued = bounded_queue_len(transaction).await?;
|
||||
if queued >= u64::try_from(self.limits.queue_limit).unwrap_or(u64::MAX) {
|
||||
return Err(AttemptError::Overloaded);
|
||||
}
|
||||
|
||||
let created_ms: i64 = sqlx::query(
|
||||
"insert into tasks (
|
||||
task_id, workspace_id, owner_kind, owner_id, authorization_scope, operation_id,
|
||||
operation_version, scoped_key, input_fingerprint, request_id, trace_id, traceparent,
|
||||
trace_sampled, policy_mode, approval_reference, state, input_envelope,
|
||||
execution_deadline, retain_until, executable_input_ciphertext, executable_input_key_version,
|
||||
executable_input_master_key_epoch, platform_api_key_id
|
||||
) values (
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, 'queued', $16,
|
||||
to_timestamp($17::double precision / 1000.0),
|
||||
to_timestamp($18::double precision / 1000.0), $19, $20, $21, $22
|
||||
)
|
||||
returning (extract(epoch from created_at) * 1000)::bigint as created_ms",
|
||||
)
|
||||
.bind(request.task_id.as_uuid())
|
||||
.bind(&request.workspace_id)
|
||||
.bind(request.owner_kind.as_str())
|
||||
.bind(&request.owner_id)
|
||||
.bind(&request.authorization_scope)
|
||||
.bind(&request.operation_id)
|
||||
.bind(i32::try_from(request.pins.operation_version).unwrap_or(i32::MAX))
|
||||
.bind(&request.scoped_key)
|
||||
.bind(&request.input_fingerprint)
|
||||
.bind(request.support.request_id.as_str())
|
||||
.bind(request.support.trace_id.as_str())
|
||||
.bind(&request.support.traceparent)
|
||||
.bind(request.support.trace_sampled)
|
||||
.bind(policy_mode_str(request.pins.policy_mode))
|
||||
.bind(request.pins.approval_reference.as_deref())
|
||||
.bind(request.input.value().clone())
|
||||
.bind(request.pins.execution_deadline_unix_ms)
|
||||
.bind(request.pins.retain_until_unix_ms)
|
||||
.bind(request.executable_input_ciphertext.as_deref())
|
||||
.bind(request.executable_input_key_version.as_deref())
|
||||
.bind(request.executable_input_master_key_epoch)
|
||||
.bind(
|
||||
request
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(PlatformApiKeyId::as_str),
|
||||
)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await
|
||||
.map_err(task_sql_error)?
|
||||
.try_get("created_ms")
|
||||
.map_err(AttemptError::from_sql)?;
|
||||
|
||||
let admission_request = AdmissionRequest {
|
||||
scope: AdmissionScope {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
agent_key: request.owner_id.clone(),
|
||||
operation_id: request.operation_id.clone(),
|
||||
operation_version: request.pins.operation_version,
|
||||
},
|
||||
mode: AdmissionMode::Background,
|
||||
policy_mode: request.pins.policy_mode,
|
||||
no_tasks_fallback: NoTasksFallback::Unsupported,
|
||||
// Never reached: the intent row is inserted by this transaction, not by `admit`.
|
||||
approval: ApprovalEvidence::NotRequired,
|
||||
fingerprint: request.input_fingerprint.clone(),
|
||||
expires_at_unix_ms: request.pins.execution_deadline_unix_ms,
|
||||
};
|
||||
let owner_token = format!("task_{}", request.task_id);
|
||||
insert_admission_entry(
|
||||
transaction,
|
||||
&admission_request,
|
||||
&owner_token,
|
||||
"reserved",
|
||||
Some(request.task_id.as_uuid()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Accepted::Created(TaskAcceptance {
|
||||
task_id: request.task_id,
|
||||
state: TaskState::Queued,
|
||||
operation_version: request.pins.operation_version,
|
||||
support: request.support.clone(),
|
||||
created_at_unix_ms: created_ms,
|
||||
retain_until_unix_ms: request.pins.retain_until_unix_ms,
|
||||
replayed: false,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Bounded scoped reread after a foreign unique conflict aborted the transaction.
|
||||
async fn reread_after_unique(
|
||||
&self,
|
||||
request: &TaskAcceptRequest,
|
||||
) -> Result<TaskAcceptance, TaskError> {
|
||||
let row = sqlx::query(sqlx::AssertSqlSafe(format!(
|
||||
"select {TASK_PROJECTION} from tasks
|
||||
where workspace_id = $1 and owner_kind = $2 and owner_id = $3
|
||||
and authorization_scope = $4 and operation_id = $5 and scoped_key = $6"
|
||||
)))
|
||||
.bind(&request.workspace_id)
|
||||
.bind(request.owner_kind.as_str())
|
||||
.bind(&request.owner_id)
|
||||
.bind(&request.authorization_scope)
|
||||
.bind(&request.operation_id)
|
||||
.bind(&request.scoped_key)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::unavailable)?;
|
||||
let Some(row) = row else {
|
||||
return Err(TaskError::unavailable(
|
||||
"durable task acceptance ended with an unresolved unique conflict",
|
||||
));
|
||||
};
|
||||
let record = record_from_row(&row)?;
|
||||
if record.input_fingerprint != request.input_fingerprint {
|
||||
return Err(TaskError::FingerprintConflict);
|
||||
}
|
||||
Ok(acceptance_from_record(&record))
|
||||
}
|
||||
|
||||
/// A rejected conditional update is either a lost CAS race or a foreign/absent Task. Both are
|
||||
/// reported without payload, and a foreign Task is indistinguishable from an absent one.
|
||||
async fn classify_missed_finish(&self, request: &TaskFinishRequest) -> TaskError {
|
||||
let access = &request.access;
|
||||
match sqlx::query(
|
||||
"select state, version from tasks
|
||||
where task_id = $1 and workspace_id = $2 and owner_kind = $3
|
||||
and owner_id = $4 and authorization_scope = $5",
|
||||
)
|
||||
.bind(request.task_id.as_uuid())
|
||||
.bind(&access.workspace_id)
|
||||
.bind(access.owner_kind.as_str())
|
||||
.bind(&access.owner_id)
|
||||
.bind(&access.authorization_scope)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
{
|
||||
Ok(Some(_)) => TaskError::StateConflict,
|
||||
Ok(None) => TaskError::NotFound,
|
||||
Err(error) => Self::unavailable(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn policy_mode_str(mode: ToolExecutionMode) -> &'static str {
|
||||
match mode {
|
||||
ToolExecutionMode::Synchronous => "synchronous",
|
||||
ToolExecutionMode::Background => "background",
|
||||
ToolExecutionMode::Adaptive => "adaptive",
|
||||
}
|
||||
}
|
||||
|
||||
fn task_sql_error(error: sqlx::Error) -> AttemptError {
|
||||
if error
|
||||
.as_database_error()
|
||||
.and_then(|database| database.code())
|
||||
.as_deref()
|
||||
== Some("23505")
|
||||
{
|
||||
return AttemptError::UniqueViolation;
|
||||
}
|
||||
AttemptError::from_sql(error)
|
||||
}
|
||||
|
||||
fn record_from_row(row: &sqlx::postgres::PgRow) -> Result<TaskRecord, TaskError> {
|
||||
let task_id: Uuid = row.try_get("task_id").map_err(unavailable_row)?;
|
||||
let state: String = row.try_get("state").map_err(unavailable_row)?;
|
||||
let owner_kind: String = row.try_get("owner_kind").map_err(unavailable_row)?;
|
||||
let policy_mode: String = row.try_get("policy_mode").map_err(unavailable_row)?;
|
||||
let trace_id: String = row.try_get("trace_id").map_err(unavailable_row)?;
|
||||
let request_id: String = row.try_get("request_id").map_err(unavailable_row)?;
|
||||
let envelope = |column: &str| -> Result<Option<TaskEnvelope>, TaskError> {
|
||||
let value: Option<Value> = row.try_get(column).map_err(unavailable_row)?;
|
||||
value.map(TaskEnvelope::from_persisted).transpose()
|
||||
};
|
||||
Ok(TaskRecord {
|
||||
task_id: TaskId::parse(&task_id.to_string())?,
|
||||
state: TaskState::parse(&state)?,
|
||||
workspace_id: row.try_get("workspace_id").map_err(unavailable_row)?,
|
||||
owner_kind: TaskOwnerKind::parse(&owner_kind)?,
|
||||
owner_id: row.try_get("owner_id").map_err(unavailable_row)?,
|
||||
authorization_scope: row
|
||||
.try_get("authorization_scope")
|
||||
.map_err(unavailable_row)?,
|
||||
platform_api_key_id: row
|
||||
.try_get::<Option<String>, _>("platform_api_key_id")
|
||||
.map_err(unavailable_row)?
|
||||
.map(PlatformApiKeyId::new),
|
||||
operation_id: row.try_get("operation_id").map_err(unavailable_row)?,
|
||||
input_fingerprint: row.try_get("input_fingerprint").map_err(unavailable_row)?,
|
||||
pins: TaskPins {
|
||||
operation_version: u32::try_from(
|
||||
row.try_get::<i32, _>("operation_version")
|
||||
.map_err(unavailable_row)?,
|
||||
)
|
||||
.unwrap_or_default(),
|
||||
policy_mode: match policy_mode.as_str() {
|
||||
"background" => ToolExecutionMode::Background,
|
||||
"adaptive" => ToolExecutionMode::Adaptive,
|
||||
_ => ToolExecutionMode::Synchronous,
|
||||
},
|
||||
execution_deadline_unix_ms: row
|
||||
.try_get("execution_deadline_ms")
|
||||
.map_err(unavailable_row)?,
|
||||
retain_until_unix_ms: row.try_get("retain_until_ms").map_err(unavailable_row)?,
|
||||
approval_reference: row.try_get("approval_reference").map_err(unavailable_row)?,
|
||||
},
|
||||
support: TaskSupportIds {
|
||||
request_id: crank_core::RequestId::parse(&request_id)
|
||||
.map_err(|_| TaskError::unavailable("stored request id is invalid"))?,
|
||||
trace_id: crank_core::TraceId::parse(&trace_id)
|
||||
.map_err(|_| TaskError::unavailable("stored trace id is invalid"))?,
|
||||
traceparent: row.try_get("traceparent").map_err(unavailable_row)?,
|
||||
trace_sampled: row.try_get("trace_sampled").map_err(unavailable_row)?,
|
||||
},
|
||||
input: envelope("input_envelope")?
|
||||
.ok_or_else(|| TaskError::unavailable("stored task input envelope is missing"))?,
|
||||
progress: envelope("progress_envelope")?,
|
||||
result: envelope("result_envelope")?,
|
||||
error: envelope("error_envelope")?,
|
||||
attempts: u32::try_from(row.try_get::<i32, _>("attempts").map_err(unavailable_row)?)
|
||||
.unwrap_or_default(),
|
||||
fence: u64::try_from(row.try_get::<i64, _>("fence").map_err(unavailable_row)?)
|
||||
.unwrap_or_default(),
|
||||
lease_owner: row.try_get("lease_owner").map_err(unavailable_row)?,
|
||||
lease_until_unix_ms: row.try_get("lease_until_ms").map_err(unavailable_row)?,
|
||||
current_attempt_id: row.try_get("current_attempt_id").map_err(unavailable_row)?,
|
||||
dispatch_started_at_unix_ms: row
|
||||
.try_get("dispatch_started_at_ms")
|
||||
.map_err(unavailable_row)?,
|
||||
version: u32::try_from(row.try_get::<i32, _>("version").map_err(unavailable_row)?)
|
||||
.unwrap_or_default(),
|
||||
created_at_unix_ms: row.try_get("created_ms").map_err(unavailable_row)?,
|
||||
updated_at_unix_ms: row.try_get("updated_ms").map_err(unavailable_row)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn unavailable_row(error: sqlx::Error) -> TaskError {
|
||||
PostgresTaskStore::unavailable(error)
|
||||
}
|
||||
@@ -17,5 +17,6 @@ mod integration {
|
||||
mod prompt_drafts;
|
||||
mod prompt_publication;
|
||||
mod resource_drafts;
|
||||
mod tasks;
|
||||
mod workspace_access;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use crank_registry::{MigrationAuthority, MigrationPreflight, PostgresRegistry};
|
||||
use sqlx::Row;
|
||||
|
||||
mod artifact_metadata;
|
||||
mod durable_tasks_guard;
|
||||
mod import_plan;
|
||||
mod legacy_adoption;
|
||||
mod preflight;
|
||||
@@ -70,6 +71,11 @@ async fn controlled_authority_is_versioned_and_safe_under_concurrent_apply() {
|
||||
(21, "prompts-v21"),
|
||||
(22, "prompt-publications-v22"),
|
||||
(23, "prompt-dependencies-v23"),
|
||||
(24, "execution-admission-v24"),
|
||||
(25, "durable-tasks-v25"),
|
||||
(26, "durable-tasks-v26"),
|
||||
(27, "durable-tasks-v27"),
|
||||
(28, "durable-tasks-v28"),
|
||||
];
|
||||
assert_eq!(rows.len(), expected.len());
|
||||
for (row, (version, name)) in rows.iter().zip(expected) {
|
||||
@@ -180,7 +186,7 @@ async fn controlled_authority_is_versioned_and_safe_under_concurrent_apply() {
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(first.pool()).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
@@ -228,7 +234,7 @@ async fn v18_guard_rejects_missing_agent_resource_binding_trigger() {
|
||||
.expect("the V18-local guard must be restorable");
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"restoring the exact V18 contract must return the current verdict"
|
||||
);
|
||||
}
|
||||
@@ -354,7 +360,7 @@ async fn ledgerless_legacy_baseline_is_consolidated_without_data_loss() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -467,13 +473,13 @@ async fn healthy_v2_is_reported_as_migration_required_and_upgrades_to_v3() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 2,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
let trace_column: bool = sqlx::query_scalar(
|
||||
"select exists (
|
||||
@@ -516,7 +522,7 @@ async fn healthy_v3_upgrades_to_v4_with_honest_legacy_snapshot_provenance() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 3,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -576,7 +582,7 @@ async fn healthy_v4_upgrades_to_v5_without_fabricating_legacy_outcomes() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 4,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -830,6 +836,6 @@ async fn v3_upgrade_ignores_oversized_legacy_request_ids_in_partial_index() {
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
@@ -32,7 +32,7 @@ async fn healthy_v11_upgrades_to_v13_without_rewriting_prior_ledger() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 11,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
|
||||
@@ -177,7 +177,7 @@ async fn v12_guard_still_rejects_column_constraint_index_and_relation_drift() {
|
||||
sqlx::raw_sql(restore).execute(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"restore: {restore}"
|
||||
);
|
||||
}
|
||||
@@ -230,7 +230,7 @@ async fn v13_guard_rejects_wrong_cleanup_index_order_and_predicate() {
|
||||
sqlx::raw_sql(restore).execute(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"restore: {restore}"
|
||||
);
|
||||
}
|
||||
@@ -507,7 +507,7 @@ async fn assert_v12_drift(pool: &sqlx::PgPool, drift: &str, restore: &str) {
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"restore: {restore}"
|
||||
);
|
||||
}
|
||||
@@ -845,7 +845,7 @@ async fn v17_guard_rejects_publication_schema_drift() {
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
|
||||
for (drift, restore) in [
|
||||
@@ -879,7 +879,7 @@ async fn v17_guard_rejects_publication_schema_drift() {
|
||||
sqlx::raw_sql(restore).execute(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"restore: {restore}"
|
||||
);
|
||||
}
|
||||
|
||||
+8
-8
@@ -289,7 +289,7 @@ async fn assert_primary_key_nullability(pool: &sqlx::PgPool, table: &str, column
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"a rejected primary-key nullability change must leave the schema current"
|
||||
);
|
||||
|
||||
@@ -473,7 +473,7 @@ async fn assert_definition_only_drift(
|
||||
assert_eq!(super::catalog_constraint_definitions(pool).await, before);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -554,7 +554,7 @@ async fn story36_v16_guard_rejects_constraint_and_created_index_definition_drift
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"the baseline before the drift matrix must be the current V16 schema"
|
||||
);
|
||||
let mut observations: Vec<String> = Vec::new();
|
||||
@@ -773,7 +773,7 @@ async fn story36_v16_guard_rejects_constraint_and_created_index_definition_drift
|
||||
.await
|
||||
.expect("restoring the exact definition must apply");
|
||||
let restored = MigrationAuthority::preflight(&pool).await;
|
||||
let restored_current = matches!(restored, Ok(MigrationPreflight::Current { version: 23 }));
|
||||
let restored_current = matches!(restored, Ok(MigrationPreflight::Current { version: 28 }));
|
||||
// Точное восстановление доказывается сравнением снятого снимка с baseline, а не
|
||||
// мнением того же guard-а, который принимает неправильные определения (F4).
|
||||
let restored_exact = catalog_definition_snapshot(&pool).await == before;
|
||||
@@ -804,7 +804,7 @@ async fn story36_v16_guard_resolves_dependent_source_fk_in_the_current_schema()
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"baseline before inserting the shadow schema"
|
||||
);
|
||||
|
||||
@@ -875,7 +875,7 @@ async fn story36_v16_guard_resolves_dependent_source_fk_in_the_current_schema()
|
||||
// Тень не меняет вердикт guard-а по текущей схеме.
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"a same-named object in another schema must not change the verdict for the current schema"
|
||||
);
|
||||
|
||||
@@ -947,7 +947,7 @@ async fn story36_v16_guard_resolves_dependent_source_fk_in_the_current_schema()
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 },
|
||||
MigrationPreflight::Current { version: 28 },
|
||||
"the exact restoration must return the current V16 schema"
|
||||
);
|
||||
|
||||
@@ -958,7 +958,7 @@ async fn story36_v16_guard_resolves_dependent_source_fk_in_the_current_schema()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
|
||||
assert!(
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
use super::*;
|
||||
use crank_registry::MigrationApplyResult;
|
||||
|
||||
#[tokio::test]
|
||||
async fn clean_legacy_ledger_upgrades_to_v28_after_v25_removal() {
|
||||
let database_url = crank_test_support::postgres_schema_url("test_v43_clean_legacy").await;
|
||||
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
super::remove_v25_schema(&pool).await;
|
||||
// remove_v25_schema drops `tasks` and ledger >= 25, but V25 also added the
|
||||
// nullable dispatch-intent column/index/FK to execution_admission_entries;
|
||||
// restore those to the V24 shape so the legacy ledger is self-consistent.
|
||||
sqlx::raw_sql(
|
||||
"alter table execution_admission_entries drop constraint if exists execution_admission_entries_task_fkey;
|
||||
drop index if exists execution_admission_entries_task_uidx;
|
||||
alter table execution_admission_entries drop column if exists task_id;",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 24,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::apply(&pool).await.unwrap(),
|
||||
MigrationApplyResult::Applied { from: 24, to: 28 }
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn partial_v27_on_legacy_ledger_is_rejected_by_preflight() {
|
||||
let database_url = crank_test_support::postgres_schema_url("test_v43_partial_v27").await;
|
||||
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
super::remove_v25_schema(&pool).await;
|
||||
sqlx::raw_sql(
|
||||
"alter table execution_admission_entries drop constraint if exists execution_admission_entries_task_fkey;
|
||||
drop index if exists execution_admission_entries_task_uidx;
|
||||
alter table execution_admission_entries drop column if exists task_id;",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
// Reintroduce a partial V27-shaped `tasks` table on top of the legacy (v24) ledger.
|
||||
sqlx::raw_sql(sqlx::AssertSqlSafe(
|
||||
"create table tasks (
|
||||
task_id uuid primary key,
|
||||
executable_input_ciphertext text,
|
||||
executable_input_key_version text,
|
||||
executable_input_master_key_epoch bigint
|
||||
);",
|
||||
))
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let error = MigrationAuthority::preflight(&pool).await.unwrap_err();
|
||||
assert_eq!(error.code(), "partial_sequence");
|
||||
// The rejection today is attributed to V25's absent-schema guard because the
|
||||
// `tasks` relation already exists, so a behavioral RED pin for the missing
|
||||
// V27 guard line cannot be reached; this test fixes the rejection contract
|
||||
// instead of claiming new behavior.
|
||||
assert_eq!(error.version(), Some(24));
|
||||
assert_eq!(error.stage(), "preflight.schema");
|
||||
|
||||
let count: i64 =
|
||||
sqlx::query_scalar("select count(*)::bigint from __crank_migrations where version >= 25")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
count, 0,
|
||||
"nothing at or above V25 should have been applied over the partial fixture"
|
||||
);
|
||||
}
|
||||
@@ -71,7 +71,7 @@ async fn fresh_install_exposes_import_plan_objects_with_cascading_job_fk() {
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
let tables = sqlx::query(
|
||||
"select table_name
|
||||
@@ -224,7 +224,7 @@ async fn healthy_v13_upgrades_to_v14_without_changing_prior_ledger() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 13,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
|
||||
@@ -88,7 +88,7 @@ async fn published_ledgerless_baseline_upgrades_without_data_loss() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -101,7 +101,7 @@ async fn published_ledgerless_baseline_upgrades_without_data_loss() {
|
||||
assert_eq!(operation_count, 1);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ async fn published_ledgerless_historical_column_layout_upgrades_without_data_los
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -230,7 +230,7 @@ async fn published_ledgerless_historical_column_layout_upgrades_without_data_los
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ async fn published_initial_mcp_layout_is_accepted() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
@@ -280,7 +280,7 @@ async fn published_initial_mcp_layout_is_accepted() {
|
||||
assert!(supports_elicitation);
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -314,13 +314,13 @@ async fn published_in_place_mcp_upgrade_layout_is_accepted() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 0,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+15
-9
@@ -14,7 +14,7 @@ async fn fresh_install_creates_the_empty_revocation_authority() {
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
let rows: i64 = sqlx::query_scalar("select count(*) from resource_security_revocations")
|
||||
.fetch_one(&pool)
|
||||
@@ -42,12 +42,12 @@ async fn v18_upgrades_to_v19_without_rewriting_the_prior_ledger() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 18,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
MigrationAuthority::apply(&pool).await.unwrap(),
|
||||
MigrationApplyResult::Applied { from: 18, to: 23 }
|
||||
MigrationApplyResult::Applied { from: 18, to: 28 }
|
||||
);
|
||||
let after: Vec<(i64, String, String)> =
|
||||
sqlx::query_as("select version, name, checksum from __crank_migrations where version < 19 order by version")
|
||||
@@ -57,7 +57,7 @@ async fn v18_upgrades_to_v19_without_rewriting_the_prior_ledger() {
|
||||
assert_eq!(before, after, "V16–V18 ledger rows must not be rewritten");
|
||||
assert_eq!(
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::Current { version: 23 }
|
||||
MigrationPreflight::Current { version: 28 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -84,18 +84,24 @@ async fn a_database_newer_than_the_binary_fails_closed_at_startup() {
|
||||
let database_url = crank_test_support::postgres_schema_url("test_v19_future_version").await;
|
||||
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
// The same `version > CURRENT_VERSION` branch an old V23 binary takes when it
|
||||
// meets a V24 database; a synthetic V24 row exercises it without a second binary.
|
||||
// The same `version > CURRENT_VERSION` branch an older binary takes when it meets a newer
|
||||
// database; a synthetic version above the current one exercises it without a second binary.
|
||||
let current: i64 = sqlx::query_scalar("select max(version) from __crank_migrations")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let future = current + 1;
|
||||
sqlx::query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values (24, 'future-v24', repeat('a', 64), 'expand', 'n-minus-one-readable')",
|
||||
values ($1, 'future-synthetic', repeat('a', 64), 'expand', 'n-minus-one-readable')",
|
||||
)
|
||||
.bind(future)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let error = MigrationAuthority::preflight(&pool).await.unwrap_err();
|
||||
assert_eq!(error.code(), "future_version");
|
||||
assert_eq!(error.version(), Some(24));
|
||||
assert_eq!(error.version(), Some(future));
|
||||
assert_eq!(error.recovery(), "install_matching_application");
|
||||
assert!(
|
||||
MigrationAuthority::require_current(&pool).await.is_err(),
|
||||
@@ -155,7 +161,7 @@ async fn failed_v19_migration_rolls_back_schema_and_ledger() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 18,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -63,7 +63,7 @@ async fn failed_artifact_metadata_migration_rolls_back_schema_and_ledger() {
|
||||
MigrationAuthority::preflight(&pool).await.unwrap(),
|
||||
MigrationPreflight::MigrationRequired {
|
||||
current: 11,
|
||||
target: 23,
|
||||
target: 28,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ pub(crate) async fn remove_v4_schema(pool: &sqlx::PgPool) {
|
||||
.unwrap();
|
||||
}
|
||||
pub(crate) async fn remove_v5_schema(pool: &sqlx::PgPool) {
|
||||
remove_v24_schema(pool).await;
|
||||
remove_v6_schema(pool).await;
|
||||
sqlx::raw_sql(
|
||||
"drop index if exists invocation_logs_workspace_operation_version_idx;
|
||||
@@ -154,6 +155,7 @@ pub(crate) async fn remove_v11_schema(pool: &sqlx::PgPool) {
|
||||
.unwrap();
|
||||
}
|
||||
pub(crate) async fn remove_v14_schema(pool: &sqlx::PgPool) {
|
||||
remove_v24_schema(pool).await;
|
||||
remove_v15_schema(pool).await;
|
||||
sqlx::raw_sql(
|
||||
"drop table if exists import_plan_applications;
|
||||
@@ -224,6 +226,7 @@ pub(crate) async fn remove_v18_schema(pool: &sqlx::PgPool) {
|
||||
.unwrap();
|
||||
}
|
||||
pub(crate) async fn remove_v19_schema(pool: &sqlx::PgPool) {
|
||||
remove_v24_schema(pool).await;
|
||||
remove_v20_schema(pool).await;
|
||||
sqlx::raw_sql(
|
||||
"drop table if exists resource_security_revocations;
|
||||
@@ -307,6 +310,7 @@ pub(crate) async fn remove_v20_schema(pool: &sqlx::PgPool) {
|
||||
.unwrap();
|
||||
}
|
||||
pub(crate) async fn remove_v12_schema(pool: &sqlx::PgPool) {
|
||||
remove_v24_schema(pool).await;
|
||||
remove_v14_schema(pool).await;
|
||||
sqlx::raw_sql(
|
||||
"drop index if exists import_jobs_openapi_source_idx;
|
||||
@@ -323,3 +327,28 @@ pub(crate) async fn remove_v12_schema(pool: &sqlx::PgPool) {
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Story 4.2: снять V25 (durable Tasks и Task-linked dispatch intent) перед возвратом к < 25.
|
||||
pub(crate) async fn remove_v25_schema(pool: &sqlx::PgPool) {
|
||||
// V25 created `tasks`; V26–V28 only altered it, so dropping the table removes their schema too.
|
||||
sqlx::raw_sql(
|
||||
"drop table if exists tasks cascade;
|
||||
delete from __crank_migrations where version >= 25;",
|
||||
)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Story 4.1: снять V24 (authoritative execution admission) перед возвратом к < 24.
|
||||
pub(crate) async fn remove_v24_schema(pool: &sqlx::PgPool) {
|
||||
remove_v25_schema(pool).await;
|
||||
sqlx::raw_sql(
|
||||
"drop table if exists execution_admission_entries;
|
||||
drop table if exists execution_admission_controllers;
|
||||
delete from __crank_migrations where version = 24;",
|
||||
)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
@@ -0,0 +1,905 @@
|
||||
//! ATDD Story 4.2 (FR-22/FR-23): durable Task acceptance on a real PostgreSQL schema.
|
||||
//!
|
||||
//! These oracles drive the real store: Task + dispatch intent + queue slot in one transaction, replay
|
||||
//! and typed conflict before any capacity mutation, bounded shared capacity, the Task-linked intent
|
||||
//! guard of the legacy admission paths, one-winner terminal CAS with an immutable result, bounded
|
||||
//! retention, owner/authorization-scope isolation and fail-closed health.
|
||||
|
||||
use super::common;
|
||||
|
||||
use crank_core::{
|
||||
AdmissionMode, AdmissionOutcome, AdmissionRequest, AdmissionScope, AgentId, ApprovalEvidence,
|
||||
ExecutionAdmission, NoTasksFallback, PlatformApiKey, PlatformApiKeyId, PlatformApiKeyKind,
|
||||
PlatformApiKeyScope, PlatformApiKeyStatus, RequestId, TaskAcceptRequest, TaskAccess,
|
||||
TaskEnvelope, TaskError, TaskFinishRequest, TaskId, TaskOwnerKind, TaskPins, TaskState,
|
||||
TaskStore, TaskSupportIds, ToolExecutionMode, TraceContext, WorkspaceId, current_unix_ms,
|
||||
};
|
||||
use crank_registry::{
|
||||
AdmissionLimits, CreatePlatformApiKeyRequest, PostgresExecutionAdmission, PostgresTaskStore,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sqlx::{PgPool, Row};
|
||||
use time::{Duration as TimeDuration, OffsetDateTime};
|
||||
|
||||
const WORKSPACE: &str = "ws_default";
|
||||
const AGENT: &str = "agent_a";
|
||||
|
||||
fn limits(queue: usize) -> AdmissionLimits {
|
||||
AdmissionLimits {
|
||||
global_limit: 4,
|
||||
agent_limit: 4,
|
||||
tool_limit: 4,
|
||||
queue_limit: queue,
|
||||
}
|
||||
}
|
||||
|
||||
fn store_with(pool: PgPool, queue: usize) -> PostgresTaskStore {
|
||||
PostgresTaskStore::new(pool, limits(queue), Default::default()).expect("valid store config")
|
||||
}
|
||||
|
||||
fn unsampled_support() -> TaskSupportIds {
|
||||
TaskSupportIds::new(
|
||||
RequestId::generate(),
|
||||
&TraceContext::parse("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00").unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
fn request(key: &str, fingerprint: &str, agent: &str, deadline_in_ms: i64) -> TaskAcceptRequest {
|
||||
let deadline = current_unix_ms() + deadline_in_ms;
|
||||
TaskAcceptRequest {
|
||||
task_id: TaskId::generate(),
|
||||
workspace_id: WORKSPACE.to_owned(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: agent.to_owned(),
|
||||
authorization_scope: format!("credential:pk_{agent}"),
|
||||
platform_api_key_id: Some(PlatformApiKeyId::new(format!("pk_{agent}"))),
|
||||
operation_id: "op_report".to_owned(),
|
||||
pins: TaskPins {
|
||||
operation_version: 3,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
execution_deadline_unix_ms: deadline,
|
||||
retain_until_unix_ms: deadline + 3_600_000,
|
||||
approval_reference: Some("appr_1".to_owned()),
|
||||
},
|
||||
scoped_key: key.to_owned(),
|
||||
input_fingerprint: fingerprint.to_owned(),
|
||||
input: TaskEnvelope::try_redacted(&json!({"report": "q3", "api_key": "sk_live"})).unwrap(),
|
||||
support: unsampled_support(),
|
||||
executable_input_ciphertext: None,
|
||||
executable_input_key_version: None,
|
||||
executable_input_master_key_epoch: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn access(agent: &str) -> TaskAccess {
|
||||
TaskAccess {
|
||||
workspace_id: WORKSPACE.to_owned(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: agent.to_owned(),
|
||||
authorization_scope: format!("credential:pk_{agent}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn key(seed: &str) -> String {
|
||||
format!("{seed:0<64}").chars().take(64).collect()
|
||||
}
|
||||
|
||||
fn digest(seed: &str) -> String {
|
||||
format!("{seed:0<64}").chars().take(64).collect()
|
||||
}
|
||||
|
||||
async fn row_counts(pool: &PgPool) -> (i64, i64) {
|
||||
let row = sqlx::query(
|
||||
"select
|
||||
(select count(*)::bigint from tasks) as tasks,
|
||||
(select count(*)::bigint from execution_admission_entries where task_id is not null)
|
||||
as intents",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
(row.get("tasks"), row.get("intents"))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn acceptance_commits_task_intent_and_slot_together() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let request = request(&key("k1"), &digest("f1"), AGENT, 30_000);
|
||||
let task_id = request.task_id;
|
||||
let support = request.support.clone();
|
||||
|
||||
// Another connection holds both controller rows: the acceptance cannot commit yet.
|
||||
let mut blocker = registry.pool().begin().await.unwrap();
|
||||
sqlx::query(
|
||||
"insert into execution_admission_controllers (workspace_id) values ('*'), ($1)
|
||||
on conflict (workspace_id) do nothing",
|
||||
)
|
||||
.bind(WORKSPACE)
|
||||
.execute(&mut *blocker)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("update execution_admission_controllers set updated_at = updated_at")
|
||||
.execute(&mut *blocker)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let pool = registry.pool().clone();
|
||||
let acceptance = tokio::spawn(async move { store.accept(request).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(150)).await;
|
||||
|
||||
// No partial Task, no intent and no accepted response exist before COMMIT.
|
||||
assert!(!acceptance.is_finished());
|
||||
assert_eq!(row_counts(&pool).await, (0, 0));
|
||||
|
||||
blocker.commit().await.unwrap();
|
||||
let acceptance = acceptance.await.unwrap().expect("acceptance commits");
|
||||
assert_eq!(acceptance.state, TaskState::Queued);
|
||||
assert!(!acceptance.replayed);
|
||||
assert_eq!(acceptance.task_id, task_id);
|
||||
assert_eq!(acceptance.support, support);
|
||||
assert_eq!(row_counts(registry.pool()).await, (1, 1));
|
||||
|
||||
// A fresh connection and store read the committed pins/identities back.
|
||||
let reader = store_with(database.concurrent_registry(2).await.pool().clone(), 8);
|
||||
let record = reader.read(&access(AGENT), task_id).await.unwrap();
|
||||
assert_eq!(record.state, TaskState::Queued);
|
||||
assert_eq!(record.pins.operation_version, 3);
|
||||
assert_eq!(record.pins.policy_mode, ToolExecutionMode::Background);
|
||||
assert_eq!(record.pins.approval_reference.as_deref(), Some("appr_1"));
|
||||
assert_eq!(record.support.request_id, support.request_id);
|
||||
assert_eq!(record.support.trace_id, support.trace_id);
|
||||
assert_eq!(record.support.traceparent, support.traceparent);
|
||||
assert!(!record.support.trace_sampled);
|
||||
// Secret-bearing input is classified before persistence.
|
||||
assert_eq!(record.input.value()["api_key"], json!("[redacted]"));
|
||||
assert_eq!(record.input.value()["report"], json!("q3"));
|
||||
assert!(record.result.is_none());
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replay_returns_the_same_task_and_one_slot() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let first = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.expect("first acceptance");
|
||||
let before = store.read(&access(AGENT), first.task_id).await.unwrap();
|
||||
|
||||
let replay = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.expect("replay returns the accepted Task");
|
||||
assert_eq!(replay.task_id, first.task_id);
|
||||
assert!(replay.replayed);
|
||||
assert_eq!(replay.support.request_id, first.support.request_id);
|
||||
assert_eq!(row_counts(registry.pool()).await, (1, 1));
|
||||
|
||||
// Replay never rewrites the accepted row or its audit evidence.
|
||||
let after = store.read(&access(AGENT), first.task_id).await.unwrap();
|
||||
assert_eq!(after.version, before.version);
|
||||
assert_eq!(after.updated_at_unix_ms, before.updated_at_unix_ms);
|
||||
assert_eq!(after.created_at_unix_ms, before.created_at_unix_ms);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replay_stays_available_with_a_full_queue_and_past_execution_deadline() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 1);
|
||||
|
||||
// A short deadline: the Task is still retained after its execution deadline elapsed.
|
||||
let first = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 1))
|
||||
.await
|
||||
.expect("first acceptance");
|
||||
sqlx::query(
|
||||
"update tasks set execution_deadline = now() - interval '1 hour' where task_id = $1",
|
||||
)
|
||||
.bind(first.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
|
||||
|
||||
let replayed = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 1))
|
||||
.await
|
||||
.expect("replay works with a full queue and an elapsed execution deadline");
|
||||
assert_eq!(replayed.task_id, first.task_id);
|
||||
assert!(replayed.replayed);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_different_fingerprint_on_the_same_key_is_a_typed_conflict() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let first = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.expect("first acceptance");
|
||||
let before = store.read(&access(AGENT), first.task_id).await.unwrap();
|
||||
|
||||
let conflict = store
|
||||
.accept(request(&key("k1"), &digest("f2"), AGENT, 30_000))
|
||||
.await
|
||||
.expect_err("different input is a conflict");
|
||||
assert_eq!(conflict, TaskError::FingerprintConflict);
|
||||
assert_eq!(row_counts(registry.pool()).await, (1, 1));
|
||||
|
||||
let after = store.read(&access(AGENT), first.task_id).await.unwrap();
|
||||
assert_eq!(after.version, before.version);
|
||||
assert_eq!(after.updated_at_unix_ms, before.updated_at_unix_ms);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn bounded_capacity_is_shared_and_overload_creates_nothing() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 1);
|
||||
let admission =
|
||||
PostgresExecutionAdmission::new(registry.pool().clone(), limits(1), Default::default())
|
||||
.unwrap();
|
||||
|
||||
// A durable enqueue decision and a durable Task share the same bounded queue budget.
|
||||
let waiting = admission
|
||||
.admit(AdmissionRequest {
|
||||
scope: AdmissionScope {
|
||||
workspace_id: WORKSPACE.to_owned(),
|
||||
agent_key: AGENT.to_owned(),
|
||||
operation_id: "op_sync".to_owned(),
|
||||
operation_version: 1,
|
||||
},
|
||||
mode: AdmissionMode::Background,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
no_tasks_fallback: NoTasksFallback::Synchronous,
|
||||
approval: ApprovalEvidence::NotRequired,
|
||||
fingerprint: "fp_sync".to_owned(),
|
||||
expires_at_unix_ms: current_unix_ms() + 30_000,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(matches!(waiting, AdmissionOutcome::Enqueued(_)));
|
||||
|
||||
let overloaded = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.expect_err("queue is full");
|
||||
assert!(matches!(overloaded, TaskError::Overloaded { .. }));
|
||||
assert_eq!(row_counts(registry.pool()).await, (0, 0));
|
||||
assert_eq!(
|
||||
admission.snapshot().await.unwrap().waiting,
|
||||
1,
|
||||
"the durable refusal changed no capacity"
|
||||
);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn legacy_cleanup_and_cancel_never_remove_a_task_linked_intent() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
let admission =
|
||||
PostgresExecutionAdmission::new(registry.pool().clone(), limits(8), Default::default())
|
||||
.unwrap();
|
||||
|
||||
let accepted = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 1))
|
||||
.await
|
||||
.expect("acceptance");
|
||||
// The linked reserved row looks expired; the legacy TTL cleanup must ignore it.
|
||||
sqlx::query("update execution_admission_entries set expires_at = now() - interval '1 hour'")
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let ticket: i64 =
|
||||
sqlx::query("select ticket_id from execution_admission_entries where task_id = $1")
|
||||
.bind(accepted.task_id.as_uuid())
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap()
|
||||
.get("ticket_id");
|
||||
let owner_token: String =
|
||||
sqlx::query("select owner_token from execution_admission_entries where ticket_id = $1")
|
||||
.bind(ticket)
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap()
|
||||
.get("owner_token");
|
||||
|
||||
// A legacy attempt runs the bounded expiry cleanup; a foreign/own legacy cancel is refused.
|
||||
let _ = admission
|
||||
.admit(AdmissionRequest {
|
||||
scope: AdmissionScope {
|
||||
workspace_id: WORKSPACE.to_owned(),
|
||||
agent_key: "agent_b".to_owned(),
|
||||
operation_id: "op_sync".to_owned(),
|
||||
operation_version: 1,
|
||||
},
|
||||
mode: AdmissionMode::Synchronous,
|
||||
policy_mode: ToolExecutionMode::Synchronous,
|
||||
no_tasks_fallback: NoTasksFallback::Synchronous,
|
||||
approval: ApprovalEvidence::NotRequired,
|
||||
fingerprint: "fp_sync".to_owned(),
|
||||
expires_at_unix_ms: current_unix_ms() + 50,
|
||||
})
|
||||
.await;
|
||||
assert!(!admission.cancel(ticket as u64, &owner_token).await.unwrap());
|
||||
assert_eq!(row_counts(registry.pool()).await, (1, 1));
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&access(AGENT), accepted.task_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.state,
|
||||
TaskState::Queued
|
||||
);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminal_transition_has_one_winner_and_an_immutable_result() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let accepted = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let queued = store.read(&access(AGENT), accepted.task_id).await.unwrap();
|
||||
assert_eq!(queued.version, 1);
|
||||
|
||||
// Moving a queued Task straight to completed is not an allowed transition.
|
||||
let skipped = TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: accepted.task_id,
|
||||
expected_state: TaskState::Queued,
|
||||
expected_version: queued.version,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(TaskEnvelope::try_redacted(&json!({"ok": true})).unwrap()),
|
||||
error: None,
|
||||
};
|
||||
assert_eq!(
|
||||
store
|
||||
.finish(skipped)
|
||||
.await
|
||||
.expect_err("transition rejected"),
|
||||
TaskError::Invalid {
|
||||
message: "task state transition is not allowed".to_owned()
|
||||
}
|
||||
);
|
||||
|
||||
let started = TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: accepted.task_id,
|
||||
expected_state: TaskState::Queued,
|
||||
expected_version: queued.version,
|
||||
target_state: TaskState::Working,
|
||||
result: None,
|
||||
error: None,
|
||||
};
|
||||
assert_eq!(
|
||||
store
|
||||
.finish(started)
|
||||
.await
|
||||
.expect_err("working is not terminal"),
|
||||
TaskError::Invalid {
|
||||
message: "task finish target is not terminal".to_owned()
|
||||
}
|
||||
);
|
||||
sqlx::query("update tasks set state = 'working' where task_id = $1")
|
||||
.bind(accepted.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
let working = store.read(&access(AGENT), accepted.task_id).await.unwrap();
|
||||
|
||||
let mut finishers = Vec::new();
|
||||
for marker in ["first", "second"] {
|
||||
let store = store.clone();
|
||||
let mut finish = TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: accepted.task_id,
|
||||
expected_state: TaskState::Working,
|
||||
expected_version: working.version,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(TaskEnvelope::try_redacted(&json!({"marker": marker})).unwrap()),
|
||||
error: None,
|
||||
};
|
||||
finish.target_state = TaskState::Completed;
|
||||
finishers.push(tokio::spawn(async move { store.finish(finish).await }));
|
||||
}
|
||||
let first = finishers.remove(0).await.unwrap();
|
||||
let second = finishers.remove(0).await.unwrap();
|
||||
let winners = [&first, &second]
|
||||
.iter()
|
||||
.filter(|result| result.is_ok())
|
||||
.count();
|
||||
assert_eq!(winners, 1, "exactly one concurrent CAS wins");
|
||||
let loser = if first.is_err() { &first } else { &second };
|
||||
assert_eq!(
|
||||
loser.as_ref().err().unwrap(),
|
||||
&TaskError::StateConflict,
|
||||
"the loser observes a rejected transition, not a rewrite"
|
||||
);
|
||||
|
||||
let terminal = store.read(&access(AGENT), accepted.task_id).await.unwrap();
|
||||
assert_eq!(terminal.state, TaskState::Completed);
|
||||
assert_eq!(terminal.version, working.version + 1);
|
||||
let marker = terminal.result.as_ref().unwrap().value()["marker"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
assert!(marker == "first" || marker == "second");
|
||||
|
||||
// A repeated terminal write and a terminal->terminal move are rejected.
|
||||
let repeated = TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: accepted.task_id,
|
||||
expected_state: TaskState::Completed,
|
||||
expected_version: terminal.version,
|
||||
target_state: TaskState::Failed,
|
||||
result: None,
|
||||
error: Some(TaskEnvelope::try_redacted(&json!({"reason": "x"})).unwrap()),
|
||||
};
|
||||
assert!(repeated.validate().is_err());
|
||||
|
||||
// Terminal Task and its dispatch intent left together.
|
||||
assert_eq!(row_counts(registry.pool()).await, (1, 0));
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn retention_is_bounded_and_never_deletes_live_work() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let queued = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let expired_queued = store
|
||||
.accept(request(&key("k2"), &digest("f2"), AGENT, 1))
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"update tasks set execution_deadline = now() - interval '1 hour' where task_id = $1",
|
||||
)
|
||||
.bind(expired_queued.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
// A retained terminal Task whose retention has not expired yet and one that has.
|
||||
let terminal = store
|
||||
.accept(request(&key("k3"), &digest("f3"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"update tasks set state = 'completed', result_envelope = '{\"ok\":true}'::jsonb,
|
||||
retain_until = now() + interval '1 hour' where task_id = $1",
|
||||
)
|
||||
.bind(terminal.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("delete from execution_admission_entries where task_id = $1")
|
||||
.bind(terminal.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
let expired_terminal = store
|
||||
.accept(request(&key("k4"), &digest("f4"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query(
|
||||
"update tasks set state = 'cancelled', execution_deadline = now() - interval '2 hours',
|
||||
retain_until = now() - interval '1 hour'
|
||||
where task_id = $1",
|
||||
)
|
||||
.bind(expired_terminal.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("delete from execution_admission_entries where task_id = $1")
|
||||
.bind(expired_terminal.task_id.as_uuid())
|
||||
.execute(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let report = store.purge_expired(64).await.unwrap();
|
||||
assert_eq!(report.expired_queued, 1);
|
||||
assert_eq!(report.expired_terminal, 1);
|
||||
|
||||
// Live queued work is never deleted or failed; the expired queued Task released its slot.
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&access(AGENT), queued.task_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.state,
|
||||
TaskState::Queued
|
||||
);
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&access(AGENT), terminal.task_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.state,
|
||||
TaskState::Completed
|
||||
);
|
||||
assert!(matches!(
|
||||
store
|
||||
.read(&access(AGENT), expired_queued.task_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.state,
|
||||
TaskState::Failed
|
||||
));
|
||||
assert!(
|
||||
store
|
||||
.read(&access(AGENT), expired_terminal.task_id)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
let remaining: i64 = sqlx::query("select count(*)::bigint as count from execution_admission_entries where task_id is not null")
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap()
|
||||
.get("count");
|
||||
assert_eq!(remaining, 1, "only the live queued Task keeps its intent");
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn foreign_scope_reads_are_not_found_and_never_leak_payload() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let accepted = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Positive control: the owner reads its Task.
|
||||
assert!(store.read(&access(AGENT), accepted.task_id).await.is_ok());
|
||||
|
||||
// Foreign owner, foreign authorization scope and foreign workspace are all not-found.
|
||||
let mut foreign_owner = access(AGENT);
|
||||
foreign_owner.owner_id = "agent_b".to_owned();
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&foreign_owner, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskError::NotFound
|
||||
);
|
||||
let mut foreign_scope = access(AGENT);
|
||||
foreign_scope.authorization_scope = "credential:pk_other".to_owned();
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&foreign_scope, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskError::NotFound
|
||||
);
|
||||
let mut foreign_workspace = access(AGENT);
|
||||
foreign_workspace.workspace_id = "ws_other".to_owned();
|
||||
assert_eq!(
|
||||
store
|
||||
.read(&foreign_workspace, accepted.task_id)
|
||||
.await
|
||||
.unwrap_err(),
|
||||
TaskError::NotFound
|
||||
);
|
||||
|
||||
// The same caller key under a foreign owner creates its own Task, never a visible collision.
|
||||
let other = store
|
||||
.accept(request(&key("k1"), &digest("f1"), "agent_b", 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_ne!(other.task_id, accepted.task_id);
|
||||
assert_eq!(row_counts(registry.pool()).await, (2, 2));
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_bounded_envelope_is_rejected_before_persistence() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// Fresh schema: the first connection applies the migration, later pools reuse the same schema.
|
||||
let _migrated = database.registry().await;
|
||||
let registry = database.concurrent_registry(4).await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
assert!(
|
||||
TaskEnvelope::try_redacted(&json!({"blob": "x".repeat(64 * 1024)})).is_err(),
|
||||
"an oversized envelope is rejected before any accept"
|
||||
);
|
||||
|
||||
let mut invalid = request(&key("k1"), &digest("f1"), AGENT, 30_000);
|
||||
invalid.scoped_key = "short".to_owned();
|
||||
assert!(matches!(invalid.validate(), Err(TaskError::Invalid { .. })));
|
||||
assert_eq!(
|
||||
store.accept(invalid).await.unwrap_err(),
|
||||
TaskError::Invalid {
|
||||
message: "task scoped key digest is invalid".to_owned()
|
||||
}
|
||||
);
|
||||
assert_eq!(row_counts(registry.pool()).await, (0, 0));
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn task_worker_lease_and_fencing_contract() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let created = store
|
||||
.accept(request(&key("lease1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let leases = store.claim_batch("worker_1", 4, 10_000).await.unwrap();
|
||||
assert_eq!(leases.len(), 1);
|
||||
assert_eq!(leases[0].record.task_id, created.task_id);
|
||||
assert_eq!(leases[0].record.state, TaskState::Working);
|
||||
assert_eq!(leases[0].owner, "worker_1");
|
||||
|
||||
let lease = &leases[0];
|
||||
let fenced_progress = store
|
||||
.write_progress(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence,
|
||||
TaskEnvelope::try_redacted(&json!({"progress": 1})).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(fenced_progress.progress.is_some());
|
||||
|
||||
// stale fence rejected
|
||||
let bad = store
|
||||
.renew_lease("worker_1", lease.record.task_id, lease.fence + 1, 10_000)
|
||||
.await;
|
||||
assert!(bad.is_err());
|
||||
|
||||
// checkpoint dispatch once
|
||||
let checkpointed = store
|
||||
.checkpoint_dispatch(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence,
|
||||
"attempt-1".to_owned(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
checkpointed.current_attempt_id.as_deref(),
|
||||
Some("attempt-1")
|
||||
);
|
||||
assert!(checkpointed.dispatch_started_at_unix_ms.is_some());
|
||||
|
||||
// second checkpoint rejected
|
||||
let second = store
|
||||
.checkpoint_dispatch(
|
||||
"worker_1",
|
||||
lease.record.task_id,
|
||||
lease.fence,
|
||||
"attempt-2".to_owned(),
|
||||
)
|
||||
.await;
|
||||
assert!(second.is_err());
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn store_failure_fails_closed_without_records() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// No migration on this schema: the durable Task state does not exist yet.
|
||||
let unmigrated = database.raw_pool().await;
|
||||
let store = store_with(unmigrated, 8);
|
||||
|
||||
assert!(store.health().await.is_err());
|
||||
let error = store
|
||||
.accept(request(&key("k1"), &digest("f1"), AGENT, 30_000))
|
||||
.await
|
||||
.expect_err("acceptance fails closed");
|
||||
assert!(matches!(error, TaskError::Unavailable { .. }));
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
/// Story 4.3 ATDD: the typed immutable Platform API key reference of an accepted Task is persisted,
|
||||
/// read back through the lease, and only an active, unexpired, exactly bound key is authoritatively
|
||||
/// revalidated. Revocation, expiry and a foreign workspace/agent binding all close; an administrator
|
||||
/// Task stores no machine key identity at all, so a worker fails closed on it.
|
||||
#[tokio::test]
|
||||
async fn accepted_task_authority_reference_is_persisted_and_revalidated() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let workspace = WorkspaceId::new(WORKSPACE);
|
||||
let now = OffsetDateTime::now_utc();
|
||||
// `ws_default` is seeded by the baseline migration; the keys only need that workspace row.
|
||||
let agent = AgentId::new(AGENT);
|
||||
|
||||
let active_id = PlatformApiKeyId::new("pk_task_active");
|
||||
for key in [
|
||||
authority_key(
|
||||
"pk_task_active",
|
||||
&workspace,
|
||||
AGENT,
|
||||
PlatformApiKeyStatus::Active,
|
||||
None,
|
||||
),
|
||||
authority_key(
|
||||
"pk_task_revoked",
|
||||
&workspace,
|
||||
AGENT,
|
||||
PlatformApiKeyStatus::Revoked,
|
||||
None,
|
||||
),
|
||||
authority_key(
|
||||
"pk_task_expired",
|
||||
&workspace,
|
||||
AGENT,
|
||||
PlatformApiKeyStatus::Active,
|
||||
Some(now - TimeDuration::hours(1)),
|
||||
),
|
||||
] {
|
||||
let secret_hash = format!("secret-hash-{}", key.id.as_str());
|
||||
registry
|
||||
.create_platform_api_key(CreatePlatformApiKeyRequest {
|
||||
secret_hash: &secret_hash,
|
||||
api_key: &key,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Acceptance persists only the immutable typed reference, never the bearer secret.
|
||||
let mut accepted = request(&key("k_authority"), &digest("fa"), AGENT, 300_000);
|
||||
accepted.platform_api_key_id = Some(active_id.clone());
|
||||
let task_id = accepted.task_id;
|
||||
store.accept(accepted).await.expect("acceptance commits");
|
||||
|
||||
let leases = store
|
||||
.claim_batch("worker_authority", 8, 30_000)
|
||||
.await
|
||||
.expect("claim");
|
||||
let lease = leases
|
||||
.into_iter()
|
||||
.find(|lease| lease.record.task_id == task_id)
|
||||
.expect("accepted task is leased");
|
||||
assert_eq!(lease.record.platform_api_key_id.as_ref(), Some(&active_id));
|
||||
|
||||
// Positive: the exact active key of this workspace/agent is revalidated authoritatively.
|
||||
let revalidated = registry
|
||||
.revalidate_platform_api_key(&workspace, &agent, &active_id)
|
||||
.await
|
||||
.expect("authoritative read")
|
||||
.expect("active key is authoritative");
|
||||
assert!(
|
||||
revalidated
|
||||
.api_key
|
||||
.scopes
|
||||
.contains(&PlatformApiKeyScope::Write)
|
||||
);
|
||||
|
||||
// Negatives: revocation, expiry and a foreign workspace/agent binding all fail closed.
|
||||
let foreign_agent = AgentId::new("agent_b");
|
||||
let foreign_workspace = WorkspaceId::new("ws_elsewhere");
|
||||
for (other_workspace, other_agent, other_key) in [
|
||||
(&workspace, &agent, PlatformApiKeyId::new("pk_task_revoked")),
|
||||
(&workspace, &agent, PlatformApiKeyId::new("pk_task_expired")),
|
||||
(&workspace, &foreign_agent, active_id.clone()),
|
||||
(&foreign_workspace, &agent, active_id.clone()),
|
||||
] {
|
||||
let found = registry
|
||||
.revalidate_platform_api_key(other_workspace, other_agent, &other_key)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
found.is_none(),
|
||||
"authority must fail closed for {}",
|
||||
other_key.as_str()
|
||||
);
|
||||
}
|
||||
|
||||
// An administrator Task carries no machine key identity: the worker has nothing to revalidate.
|
||||
let mut admin_request = request(&key("k_admin"), &digest("fb"), AGENT, 300_000);
|
||||
admin_request.owner_kind = TaskOwnerKind::Admin;
|
||||
admin_request.owner_id = "user_admin".to_owned();
|
||||
admin_request.authorization_scope = "admin:user_admin".to_owned();
|
||||
admin_request.platform_api_key_id = None;
|
||||
let admin_task = admin_request.task_id;
|
||||
store
|
||||
.accept(admin_request)
|
||||
.await
|
||||
.expect("administrator acceptance commits");
|
||||
let admin_record = store
|
||||
.read(
|
||||
&TaskAccess {
|
||||
workspace_id: WORKSPACE.to_owned(),
|
||||
owner_kind: TaskOwnerKind::Admin,
|
||||
owner_id: "user_admin".to_owned(),
|
||||
authorization_scope: "admin:user_admin".to_owned(),
|
||||
},
|
||||
admin_task,
|
||||
)
|
||||
.await
|
||||
.expect("owner read");
|
||||
assert_eq!(admin_record.platform_api_key_id, None);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
fn authority_key(
|
||||
id: &str,
|
||||
workspace_id: &WorkspaceId,
|
||||
agent: &str,
|
||||
status: PlatformApiKeyStatus,
|
||||
expires_at: Option<OffsetDateTime>,
|
||||
) -> PlatformApiKey {
|
||||
PlatformApiKey {
|
||||
id: PlatformApiKeyId::new(id),
|
||||
workspace_id: workspace_id.clone(),
|
||||
agent_id: Some(AgentId::new(agent)),
|
||||
key_kind: PlatformApiKeyKind::McpClient,
|
||||
name: id.to_owned(),
|
||||
prefix: "crk_live".to_owned(),
|
||||
scopes: vec![PlatformApiKeyScope::Write],
|
||||
status,
|
||||
created_at: OffsetDateTime::now_utc(),
|
||||
last_used_at: None,
|
||||
expires_at,
|
||||
allowed_origins: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
mod task_worker;
|
||||
@@ -0,0 +1,163 @@
|
||||
//! Story 4.3 ATDD: durable Task leases, fencing, crash recovery and bounded claim behavior.
|
||||
//!
|
||||
//! Split from `tasks.rs` to keep both integration files inside the code-health limit.
|
||||
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
/// Story 4.3 ATDD: a worker crash before the durable dispatch checkpoint leaves only an expired
|
||||
/// lease. PostgreSQL is authoritative, so another worker recovers the Task and the dead owner can
|
||||
/// never commit a terminal outcome with its old fence.
|
||||
#[tokio::test]
|
||||
async fn task_worker_expired_pre_checkpoint_lease_is_recovered_and_stale_owner_cannot_commit() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let created = store
|
||||
.accept(request(&key("recover1"), &digest("f2"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let first = store.claim_batch("worker_dead", 1, 150).await.unwrap();
|
||||
assert_eq!(first.len(), 1);
|
||||
assert_eq!(first[0].record.task_id, created.task_id);
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
|
||||
|
||||
// Recovery on the next bounded authoritative scan, with no Valkey wake-up.
|
||||
let recovered = store.claim_batch("worker_live", 4, 10_000).await.unwrap();
|
||||
let lease = recovered
|
||||
.into_iter()
|
||||
.find(|lease| lease.record.task_id == created.task_id)
|
||||
.expect("expired pre-checkpoint Task is recovered");
|
||||
assert!(lease.fence > first[0].fence, "recovery bumps the fence");
|
||||
|
||||
// The previous owner's terminal CAS is rejected; the live owner wins exactly once.
|
||||
let stale = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: created.task_id,
|
||||
expected_state: first[0].record.state,
|
||||
expected_version: first[0].record.version,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(TaskEnvelope::try_redacted(&json!({"stale": true})).unwrap()),
|
||||
error: None,
|
||||
},
|
||||
"worker_dead",
|
||||
first[0].fence,
|
||||
)
|
||||
.await;
|
||||
assert!(matches!(stale, Err(TaskError::StateConflict)));
|
||||
|
||||
let finished = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access: access(AGENT),
|
||||
task_id: created.task_id,
|
||||
expected_state: lease.record.state,
|
||||
expected_version: lease.record.version,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(TaskEnvelope::try_redacted(&json!({"ok": true})).unwrap()),
|
||||
error: None,
|
||||
},
|
||||
"worker_live",
|
||||
lease.fence,
|
||||
)
|
||||
.await
|
||||
.expect("current lease wins the terminal CAS");
|
||||
assert_eq!(finished.state, TaskState::Completed);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
/// Story 4.3 ATDD: after the dispatch checkpoint the outbound outcome is uncertain. An expired
|
||||
/// attempt resolves to `outcome_unknown`, is never redispatched automatically and releases the
|
||||
/// admission slot it held.
|
||||
#[tokio::test]
|
||||
async fn task_worker_expired_checkpointed_attempt_becomes_outcome_unknown_without_redispatch() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let store = store_with(registry.pool().clone(), 8);
|
||||
|
||||
let created = store
|
||||
.accept(request(&key("uncertain1"), &digest("f3"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let leases = store.claim_batch("worker_dead", 1, 150).await.unwrap();
|
||||
let lease = leases.into_iter().next().expect("claim");
|
||||
store
|
||||
.checkpoint_dispatch(
|
||||
"worker_dead",
|
||||
lease.record.task_id,
|
||||
lease.fence,
|
||||
"attempt-uncertain".to_owned(),
|
||||
)
|
||||
.await
|
||||
.expect("durable dispatch checkpoint");
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
|
||||
|
||||
// The authority scan resolves uncertainty instead of handing the attempt to another worker.
|
||||
let after = store.claim_batch("worker_live", 4, 10_000).await.unwrap();
|
||||
assert!(
|
||||
after.is_empty(),
|
||||
"a checkpointed attempt must never be redispatched"
|
||||
);
|
||||
let record = store.read(&access(AGENT), created.task_id).await.unwrap();
|
||||
assert_eq!(record.state, TaskState::OutcomeUnknown);
|
||||
assert_eq!(
|
||||
record.current_attempt_id.as_deref(),
|
||||
Some("attempt-uncertain")
|
||||
);
|
||||
let intents: i64 = sqlx::query_scalar(
|
||||
"select count(*)::bigint from execution_admission_entries where task_id = $1",
|
||||
)
|
||||
.bind(created.task_id.as_uuid())
|
||||
.fetch_one(registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(intents, 0, "uncertain terminal outcome releases capacity");
|
||||
|
||||
// Still no automatic retry on the following scans.
|
||||
assert!(
|
||||
store
|
||||
.claim_batch("worker_live", 4, 10_000)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
/// Story 4.3 ATDD: concurrent claimers never hold the same Task at once.
|
||||
#[tokio::test]
|
||||
async fn task_worker_concurrent_claimers_never_double_lease_one_task() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let store = std::sync::Arc::new(store_with(registry.pool().clone(), 8));
|
||||
|
||||
let created = store
|
||||
.accept(request(&key("concurrent1"), &digest("f4"), AGENT, 30_000))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut handles = Vec::new();
|
||||
for index in 0..4 {
|
||||
let store = store.clone();
|
||||
handles.push(tokio::spawn(async move {
|
||||
store
|
||||
.claim_batch(&format!("worker_{index}"), 1, 10_000)
|
||||
.await
|
||||
.unwrap()
|
||||
}));
|
||||
}
|
||||
let mut leased = Vec::new();
|
||||
for handle in handles {
|
||||
leased.extend(handle.await.unwrap());
|
||||
}
|
||||
assert_eq!(leased.len(), 1, "exactly one owner wins the claim");
|
||||
assert_eq!(leased[0].record.task_id, created.task_id);
|
||||
|
||||
database.cleanup().await;
|
||||
}
|
||||
@@ -22,7 +22,9 @@ use serde_json::{Map, Value, json};
|
||||
use time::OffsetDateTime;
|
||||
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
||||
use tracing::{Instrument, Span, debug, warn};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::task_worker::TaskDispatchPermit;
|
||||
use crate::{
|
||||
AdapterResponse, ExecutionAuthorization, PreparedRequest, RuntimeError,
|
||||
RuntimeExecutionRequest, RuntimeLimits, RuntimeOperation, RuntimeRequestContext,
|
||||
@@ -178,8 +180,72 @@ impl RuntimeExecutor {
|
||||
pub(crate) async fn execute_request(
|
||||
&self,
|
||||
request: RuntimeExecutionRequest<'_>,
|
||||
) -> Result<(Value, Value), RuntimeError> {
|
||||
self.execute_request_impl(request, None).await
|
||||
}
|
||||
|
||||
pub async fn execute_task_outcome(
|
||||
&self,
|
||||
request: RuntimeExecutionRequest<'_>,
|
||||
permit: TaskDispatchPermit,
|
||||
) -> Result<crank_core::ExecutionSuccess, crank_core::ExecutionFailure> {
|
||||
let operation = request.operation().clone();
|
||||
let operation_id = operation.operation_id.clone();
|
||||
let operation_version = operation.operation_version;
|
||||
let origin = request.origin();
|
||||
let correlation = crank_core::CorrelationContext::new(
|
||||
request.request_context().request_id.clone(),
|
||||
request.request_context().trace_context.clone(),
|
||||
);
|
||||
|
||||
// Validate permit binding before any adapter dispatch.
|
||||
if permit.operation_id() != operation_id.as_str()
|
||||
|| permit.operation_version() != operation_version
|
||||
|| permit.workspace_id() != request.workspace_id().as_str()
|
||||
|| permit.agent_id() != request.agent_id().map(|value| value.as_str())
|
||||
{
|
||||
return Err(crate::normalize_runtime_error_for_operation(
|
||||
&RuntimeError::InvalidExecutionRequest,
|
||||
&correlation,
|
||||
&operation,
|
||||
));
|
||||
}
|
||||
if !permit.consume() {
|
||||
return Err(crate::normalize_runtime_error_for_operation(
|
||||
&RuntimeError::InvalidExecutionRequest,
|
||||
&correlation,
|
||||
&operation,
|
||||
));
|
||||
}
|
||||
|
||||
match self.execute_request_impl(request, Some(permit)).await {
|
||||
Ok((output, request_preview)) => Ok(crank_core::ExecutionSuccess {
|
||||
operation_id: operation_id.clone(),
|
||||
operation_version,
|
||||
origin,
|
||||
correlation,
|
||||
request_preview,
|
||||
output,
|
||||
}),
|
||||
Err(error) => Err(crate::normalize_runtime_error_for_operation(
|
||||
&error,
|
||||
&correlation,
|
||||
&operation,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn execute_request_impl(
|
||||
&self,
|
||||
request: RuntimeExecutionRequest<'_>,
|
||||
permit: Option<TaskDispatchPermit>,
|
||||
) -> Result<(Value, Value), RuntimeError> {
|
||||
let operation = request.operation();
|
||||
// Derive trusted dispatch from a present permit; a missing permit always uses ordinary admission.
|
||||
let trusted_task_dispatch = permit.is_some();
|
||||
// The durable Attempt ID written by the dispatch checkpoint travels with the permit, so the
|
||||
// trace names the real persisted attempt instead of inventing one (Story 4.3 AC).
|
||||
let durable_attempt_id = permit.as_ref().map(|permit| permit.attempt_id().to_owned());
|
||||
let input = request.input();
|
||||
let request_context = request.request_context();
|
||||
debug_assert_eq!(request.authorization(), ExecutionAuthorization::Authorized);
|
||||
@@ -205,7 +271,9 @@ impl RuntimeExecutor {
|
||||
let dispatch_started = Arc::new(AtomicBool::new(false));
|
||||
// True until the admission gate answered. A deadline that fires while the request is still
|
||||
// waiting for a permit is an admission refusal with zero dispatch, never an upstream timeout.
|
||||
let admission_pending = Arc::new(AtomicBool::new(self.admission.is_some()));
|
||||
let admission_pending = Arc::new(AtomicBool::new(
|
||||
!trusted_task_dispatch && self.admission.is_some(),
|
||||
));
|
||||
let invocation_metrics = ToolInvocationMetrics::start_with_exemplar(
|
||||
metric_invocation_source(Some(request_context)),
|
||||
request_context
|
||||
@@ -222,14 +290,19 @@ impl RuntimeExecutor {
|
||||
// One admission gate for every executable path (Story 4.1): the published policy decides
|
||||
// the effective mode, the absolute deadline includes queue waiting and the scoped permit
|
||||
// is held for the whole local execution future, including retries.
|
||||
let mut lease = crate::admission::acquire(
|
||||
self.admission.as_ref(),
|
||||
self.coordination_store.as_deref(),
|
||||
&request,
|
||||
execution_deadline,
|
||||
)
|
||||
.await?;
|
||||
admission_pending.store(false, Ordering::Release);
|
||||
let mut lease = if trusted_task_dispatch {
|
||||
None
|
||||
} else {
|
||||
let result = crate::admission::acquire(
|
||||
self.admission.as_ref(),
|
||||
self.coordination_store.as_deref(),
|
||||
&request,
|
||||
execution_deadline,
|
||||
)
|
||||
.await?;
|
||||
admission_pending.store(false, Ordering::Release);
|
||||
result
|
||||
};
|
||||
let _permit = self.acquire_unary_permit(operation)?;
|
||||
let _inflight = InFlightGuard::runtime();
|
||||
let mapping_span = Stage::RuntimeArgumentsMap.span();
|
||||
@@ -246,6 +319,7 @@ impl RuntimeExecutor {
|
||||
prepared_request,
|
||||
Some(request_context),
|
||||
Arc::clone(&dispatch_started),
|
||||
durable_attempt_id.as_deref(),
|
||||
)
|
||||
.await;
|
||||
if let Some(lease) = lease.as_mut() {
|
||||
@@ -348,6 +422,7 @@ impl RuntimeExecutor {
|
||||
prepared_request: PreparedRequest,
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
dispatch_started: Arc<AtomicBool>,
|
||||
durable_attempt_id: Option<&str>,
|
||||
) -> Result<Value, RuntimeError> {
|
||||
let mut prepared_request = prepared_request;
|
||||
let idempotency_applicable = crate::idempotency::policy(operation).is_some();
|
||||
@@ -464,6 +539,7 @@ impl RuntimeExecutor {
|
||||
operation,
|
||||
idempotency_key.as_deref(),
|
||||
),
|
||||
durable_attempt_id,
|
||||
)
|
||||
.await;
|
||||
if let Ok(response) = &adapter_response {
|
||||
@@ -604,10 +680,23 @@ impl RuntimeExecutor {
|
||||
request_context: Option<&RuntimeRequestContext>,
|
||||
dispatch_started: Arc<AtomicBool>,
|
||||
verified_idempotency: bool,
|
||||
durable_attempt_id: Option<&str>,
|
||||
) -> Result<AdapterResponse, RuntimeError> {
|
||||
let max_attempts = retry_attempts(operation);
|
||||
let mut attempt = 1;
|
||||
loop {
|
||||
// Task dispatches reuse the durable Attempt ID committed by the dispatch checkpoint; the
|
||||
// retry index discriminates the attempts inside it. Only paths without a durable attempt
|
||||
// (synchronous tools/call) mint a synthetic per-attempt id.
|
||||
let attempt_id = durable_attempt_id
|
||||
.map(str::to_owned)
|
||||
.unwrap_or_else(|| format!("attempt-{}", Uuid::now_v7()));
|
||||
let attempt_span = tracing::info_span!(
|
||||
"adapter_attempt",
|
||||
attempt,
|
||||
max_attempts,
|
||||
attempt_id = %attempt_id
|
||||
);
|
||||
let result = self
|
||||
.execute_adapter(
|
||||
operation,
|
||||
@@ -615,6 +704,7 @@ impl RuntimeExecutor {
|
||||
request_context,
|
||||
Arc::clone(&dispatch_started),
|
||||
)
|
||||
.instrument(attempt_span)
|
||||
.await;
|
||||
match result {
|
||||
Ok(response) => return Ok(response),
|
||||
|
||||
@@ -314,7 +314,7 @@ fn request_fingerprint(input: &Value) -> Result<String, RuntimeError> {
|
||||
Ok(URL_SAFE_NO_PAD.encode(Sha256::digest(encoded)))
|
||||
}
|
||||
|
||||
fn canonical_json(value: &Value) -> Value {
|
||||
pub(crate) fn canonical_json(value: &Value) -> Value {
|
||||
match value {
|
||||
Value::Object(object) => {
|
||||
let mut entries = object.iter().collect::<Vec<_>>();
|
||||
|
||||
@@ -15,6 +15,8 @@ mod rate_limit;
|
||||
mod request_context;
|
||||
mod request_preparation;
|
||||
mod response_cache;
|
||||
mod task_worker;
|
||||
mod tasks;
|
||||
pub fn validate_resource_template_parameter(
|
||||
schema: &crank_schema::Schema,
|
||||
value: &serde_json::Value,
|
||||
@@ -114,3 +116,8 @@ pub use rate_limit::{
|
||||
};
|
||||
pub use request_context::{MeteringContext, ResponseCacheScope, RuntimeRequestContext};
|
||||
pub use secret_crypto::SecretCrypto;
|
||||
pub use task_worker::{
|
||||
TaskAttemptOutcome, TaskDispatchPermit, TaskHandler, TaskWorker, TaskWorkerConfig,
|
||||
TaskWorkerError,
|
||||
};
|
||||
pub use tasks::{TaskAcceptDraft, TaskOutcome, TaskService};
|
||||
@@ -0,0 +1,573 @@
|
||||
//! Fenced durable Task worker loop for Story 4.3.
|
||||
//!
|
||||
//! The worker is intentionally scheduler-ish: PostgreSQL is authoritative, polling is bounded,
|
||||
//! Valkey may only wake it up (never represented here as correctness), and every lease-protected
|
||||
//! write goes through CAS using owner + fencing token. The actual executable input and revalidation
|
||||
//! policy arrive via [`TaskHandler`] implemented by the application layer; `TaskRecord.input` is a
|
||||
//! redacted projection and is never dispatched.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use crank_core::{TaskEnvelope, TaskError, TaskFinishRequest, TaskLease, TaskState, TaskStore};
|
||||
use tokio::sync::watch;
|
||||
use tokio::task::JoinSet;
|
||||
use tracing::{Instrument, debug, warn};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Configuration for [`TaskWorker`]. Durations are capped by the caller to story timing budgets.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TaskWorkerConfig {
|
||||
/// Stable random-ish owner identity for this process instance. Required lease owner.
|
||||
pub owner: String,
|
||||
/// Maximum number of Tasks claimed in one bounded PG scan.
|
||||
pub batch_limit: u32,
|
||||
/// Lease length in milliseconds. Recovery stays within 60s when <= 30s is used.
|
||||
pub lease_ms: i64,
|
||||
/// Authoritative PG polling interval when no wake-up hint is available.
|
||||
pub poll_interval: Duration,
|
||||
/// Renewal cadence while processing a lease.
|
||||
pub renewal_interval: Duration,
|
||||
/// Concurrent in-flight Tasks for this worker.
|
||||
pub max_inflight: usize,
|
||||
/// Maximum time to wait for active work during graceful shutdown.
|
||||
pub shutdown_timeout: Duration,
|
||||
}
|
||||
|
||||
impl Default for TaskWorkerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
owner: format!("task-worker-{}", Uuid::now_v7()),
|
||||
batch_limit: 8,
|
||||
lease_ms: 30_000,
|
||||
poll_interval: Duration::from_millis(250),
|
||||
renewal_interval: Duration::from_millis(5_000),
|
||||
max_inflight: 4,
|
||||
shutdown_timeout: Duration::from_secs(30),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of one durable Task attempt produced by the application layer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum TaskAttemptOutcome {
|
||||
Completed(serde_json::Value),
|
||||
Failed(serde_json::Value),
|
||||
OutcomeUnknown,
|
||||
}
|
||||
|
||||
/// Application-owned seam for pre-dispatch revalidation and execution.
|
||||
///
|
||||
/// The worker owns lease renewal/CAS/fencing and the dispatch checkpoint. The handler only
|
||||
/// receives the leased record and attempt identity; it must not trust `TaskRecord.input` as
|
||||
/// executable payload.
|
||||
#[async_trait]
|
||||
pub trait TaskHandler: Send + Sync {
|
||||
async fn revalidate(&self, lease: &TaskLease) -> Result<(), TaskWorkerError>;
|
||||
async fn execute(
|
||||
&self,
|
||||
lease: &TaskLease,
|
||||
attempt_id: &str,
|
||||
cancelled: Arc<AtomicBool>,
|
||||
permit: TaskDispatchPermit,
|
||||
) -> TaskAttemptOutcome;
|
||||
}
|
||||
|
||||
/// Sealed, single-use trusted dispatch permit created by [`TaskWorker`] after a durable lease and
|
||||
/// successful `checkpoint_dispatch`. It is neither `Clone` nor constructible outside this crate, and
|
||||
/// it binds one durable Task attempt to its execution request.
|
||||
pub struct TaskDispatchPermit {
|
||||
task_id: crank_core::TaskId,
|
||||
operation_id: String,
|
||||
operation_version: u32,
|
||||
workspace_id: String,
|
||||
agent_id: Option<String>,
|
||||
owner: String,
|
||||
fence: u64,
|
||||
attempt_id: String,
|
||||
consumed: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
impl TaskDispatchPermit {
|
||||
pub(crate) fn new(
|
||||
task_id: crank_core::TaskId,
|
||||
operation_id: String,
|
||||
operation_version: u32,
|
||||
workspace_id: String,
|
||||
agent_id: Option<String>,
|
||||
owner: String,
|
||||
fence: u64,
|
||||
attempt_id: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
task_id,
|
||||
operation_id,
|
||||
operation_version,
|
||||
workspace_id,
|
||||
agent_id,
|
||||
owner,
|
||||
fence,
|
||||
attempt_id,
|
||||
consumed: std::sync::atomic::AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn task_id(&self) -> crank_core::TaskId {
|
||||
self.task_id
|
||||
}
|
||||
|
||||
pub fn operation_id(&self) -> &str {
|
||||
&self.operation_id
|
||||
}
|
||||
|
||||
pub fn operation_version(&self) -> u32 {
|
||||
self.operation_version
|
||||
}
|
||||
|
||||
pub fn workspace_id(&self) -> &str {
|
||||
&self.workspace_id
|
||||
}
|
||||
|
||||
pub fn agent_id(&self) -> Option<&str> {
|
||||
self.agent_id.as_deref()
|
||||
}
|
||||
|
||||
pub fn owner(&self) -> &str {
|
||||
&self.owner
|
||||
}
|
||||
|
||||
pub fn fence(&self) -> u64 {
|
||||
self.fence
|
||||
}
|
||||
|
||||
pub fn attempt_id(&self) -> &str {
|
||||
&self.attempt_id
|
||||
}
|
||||
|
||||
pub fn is_consumed(&self) -> bool {
|
||||
self.consumed.load(std::sync::atomic::Ordering::Acquire)
|
||||
}
|
||||
|
||||
pub fn consume(&self) -> bool {
|
||||
!self
|
||||
.consumed
|
||||
.swap(true, std::sync::atomic::Ordering::AcqRel)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum TaskWorkerError {
|
||||
#[error("task store error: {0}")]
|
||||
Store(#[from] TaskError),
|
||||
#[error("invalid worker configuration: {0}")]
|
||||
Config(String),
|
||||
#[error("task terminal transition CAS failed")]
|
||||
CasRejected,
|
||||
}
|
||||
|
||||
pub struct TaskWorker {
|
||||
store: Arc<dyn TaskStore>,
|
||||
handler: Arc<dyn TaskHandler>,
|
||||
config: TaskWorkerConfig,
|
||||
stopping: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl TaskWorker {
|
||||
pub fn new(
|
||||
store: Arc<dyn TaskStore>,
|
||||
handler: Arc<dyn TaskHandler>,
|
||||
config: TaskWorkerConfig,
|
||||
) -> Result<Self, TaskWorkerError> {
|
||||
if config.owner.is_empty() {
|
||||
return Err(TaskWorkerError::Config(
|
||||
"owner must not be empty".to_owned(),
|
||||
));
|
||||
}
|
||||
if config.batch_limit == 0 || config.batch_limit > 64 || config.lease_ms <= 0 {
|
||||
return Err(TaskWorkerError::Config(
|
||||
"batch_limit must be 1..=64 and lease_ms must be positive".to_owned(),
|
||||
));
|
||||
}
|
||||
if config.lease_ms > 30_000
|
||||
|| config.poll_interval.is_zero()
|
||||
|| config.poll_interval > Duration::from_secs(30)
|
||||
|| config.renewal_interval.is_zero()
|
||||
|| config.renewal_interval >= Duration::from_millis(config.lease_ms as u64)
|
||||
|| config.max_inflight == 0
|
||||
|| config.shutdown_timeout.is_zero()
|
||||
|| config.shutdown_timeout > Duration::from_secs(30)
|
||||
{
|
||||
return Err(TaskWorkerError::Config(
|
||||
"lease, polling, renewal, concurrency, or shutdown exceeds story timing bounds"
|
||||
.to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
store,
|
||||
handler,
|
||||
config,
|
||||
stopping: Arc::new(AtomicBool::new(false)),
|
||||
})
|
||||
}
|
||||
|
||||
/// Signal graceful shutdown. New claims stop; active handlers are cooperatively cancelled.
|
||||
pub fn stop(&self) {
|
||||
self.stopping.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
/// Returns a handle whose drop does not stop the worker; call `stop()` on the worker.
|
||||
pub fn stop_flag(&self) -> Arc<AtomicBool> {
|
||||
self.stopping.clone()
|
||||
}
|
||||
|
||||
/// Run until `stop()` is called. Uses PostgreSQL as the authoritative bounded scan.
|
||||
pub async fn run(&self) -> Result<(), TaskWorkerError> {
|
||||
self.run_inner(None).await
|
||||
}
|
||||
|
||||
/// Run until either `stop()` or the provided shutdown receiver becomes true.
|
||||
pub async fn run_until_shutdown(
|
||||
&self,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) -> Result<(), TaskWorkerError> {
|
||||
self.run_inner(Some(&mut shutdown)).await
|
||||
}
|
||||
|
||||
async fn run_inner(
|
||||
&self,
|
||||
mut shutdown: Option<&mut watch::Receiver<bool>>,
|
||||
) -> Result<(), TaskWorkerError> {
|
||||
let mut active: JoinSet<()> = JoinSet::new();
|
||||
loop {
|
||||
if self.stopping.load(Ordering::Acquire) {
|
||||
break;
|
||||
}
|
||||
if let Some(rx) = shutdown.as_mut() {
|
||||
if *rx.borrow() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Reap finished attempts before claiming more.
|
||||
while active.try_join_next().is_some() {}
|
||||
|
||||
let available = self.config.max_inflight.saturating_sub(active.len());
|
||||
if available > 0 {
|
||||
let batch_limit = self
|
||||
.config
|
||||
.batch_limit
|
||||
.min(u32::try_from(available).unwrap_or(u32::MAX));
|
||||
match self
|
||||
.store
|
||||
.claim_batch(&self.config.owner, batch_limit, self.config.lease_ms)
|
||||
.await
|
||||
{
|
||||
Ok(leases) => {
|
||||
for lease in leases {
|
||||
let store = self.store.clone();
|
||||
let handler = self.handler.clone();
|
||||
let owner = self.config.owner.clone();
|
||||
let lease_ms = self.config.lease_ms;
|
||||
let renewal_interval = self.config.renewal_interval;
|
||||
let stopping = self.stopping.clone();
|
||||
active.spawn(async move {
|
||||
process_lease(
|
||||
store,
|
||||
handler,
|
||||
owner,
|
||||
lease,
|
||||
lease_ms,
|
||||
renewal_interval,
|
||||
stopping,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
warn!(error = %error, "bounded task claim failed; retrying on next scan");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(self.config.poll_interval) => {}
|
||||
_ = async {
|
||||
if let Some(rx) = shutdown.as_mut() { let _ = rx.changed().await; } else { std::future::pending::<()>().await }
|
||||
} => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Graceful shutdown: stop claims (already) and let active work finish/checkpoint, bounded.
|
||||
// The same stop flag reaches cooperative cancellation and lease-renewal loops.
|
||||
self.stopping.store(true, Ordering::Release);
|
||||
let deadline = Instant::now() + self.config.shutdown_timeout;
|
||||
while !active.is_empty() {
|
||||
if Instant::now() >= deadline {
|
||||
warn!("graceful shutdown timeout reached; aborting remaining task attempts");
|
||||
active.abort_all();
|
||||
break;
|
||||
}
|
||||
tokio::select! {
|
||||
_ = active.join_next() => {}
|
||||
_ = tokio::time::sleep(Duration::from_millis(50)) => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_lease(
|
||||
store: Arc<dyn TaskStore>,
|
||||
handler: Arc<dyn TaskHandler>,
|
||||
owner: String,
|
||||
lease: TaskLease,
|
||||
lease_ms: i64,
|
||||
renewal_interval: Duration,
|
||||
stopping: Arc<AtomicBool>,
|
||||
) {
|
||||
let task_id = lease.record.task_id;
|
||||
let fence = lease.fence;
|
||||
let cancelled = Arc::new(AtomicBool::new(false));
|
||||
|
||||
// Pre-dispatch revalidation under the fresh lease.
|
||||
if let Err(error) = handler.revalidate(&lease).await {
|
||||
// A transient authority-store outage is not evidence that the work is invalid: leave the
|
||||
// lease to expire so the bounded recovery scan can retry it without dispatching now.
|
||||
if matches!(
|
||||
&error,
|
||||
TaskWorkerError::Store(TaskError::Unavailable { .. })
|
||||
) {
|
||||
debug!(task_id = %task_id, error = %error, "transient revalidation failure; leaving work for recovery");
|
||||
return;
|
||||
}
|
||||
debug!(task_id = %task_id, error = %error, "task revalidation failed; finishing without dispatch");
|
||||
finish_failed(
|
||||
&store,
|
||||
&owner,
|
||||
task_id,
|
||||
fence,
|
||||
&lease,
|
||||
"revalidation_failed",
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
if stopping.load(Ordering::Acquire) {
|
||||
// Graceful shutdown before the durable dispatch checkpoint: no outbound call can have
|
||||
// happened, so the Task is left leased. The bounded recovery scan requeues it after the
|
||||
// lease expires instead of turning a never-started attempt into a user-visible failure.
|
||||
return;
|
||||
}
|
||||
|
||||
let attempt_id = format!("attempt-{}", Uuid::now_v7());
|
||||
|
||||
// Durable checkpoint before any network use.
|
||||
if store
|
||||
.checkpoint_dispatch(&owner, task_id, fence, attempt_id.clone())
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
// Lease lost or checkpoint rejected: do not dispatch.
|
||||
cancelled.store(true, Ordering::Release);
|
||||
return;
|
||||
}
|
||||
if stopping.load(Ordering::Acquire) {
|
||||
// The checkpoint is durable, but we know the outbound callback has not started.
|
||||
finish_failed(
|
||||
&store,
|
||||
&owner,
|
||||
task_id,
|
||||
fence,
|
||||
&lease,
|
||||
"shutdown_before_dispatch",
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
// Sealed single-use permit for the trusted runtime dispatch bridge. It is created only after
|
||||
// the current lease and durable checkpoint above.
|
||||
let agent_id = if lease.record.owner_kind == crank_core::TaskOwnerKind::Agent {
|
||||
Some(lease.record.owner_id.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let permit = TaskDispatchPermit::new(
|
||||
lease.record.task_id,
|
||||
lease.record.operation_id.clone(),
|
||||
lease.record.pins.operation_version,
|
||||
lease.record.workspace_id.clone(),
|
||||
agent_id,
|
||||
owner.clone(),
|
||||
fence,
|
||||
attempt_id.clone(),
|
||||
);
|
||||
|
||||
// Background lease renewal while the attempt runs.
|
||||
let renewal_store = store.clone();
|
||||
let renewal_owner = owner.clone();
|
||||
let renewal_cancelled = cancelled.clone();
|
||||
let renewal_stopping = stopping.clone();
|
||||
let renewal_task_id = task_id;
|
||||
let renewal_handle = tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(renewal_interval);
|
||||
interval.tick().await;
|
||||
loop {
|
||||
interval.tick().await;
|
||||
if renewal_cancelled.load(Ordering::Acquire) {
|
||||
break;
|
||||
}
|
||||
if renewal_stopping.load(Ordering::Acquire) {
|
||||
renewal_cancelled.store(true, Ordering::Release);
|
||||
break;
|
||||
}
|
||||
if renewal_store
|
||||
.renew_lease(&renewal_owner, renewal_task_id, fence, lease_ms)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
renewal_cancelled.store(true, Ordering::Release);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Each dispatch gets a child span carrying the durable attempt identity, rooted in the parent
|
||||
// trace persisted with the accepted Task (never a mutable queue/cache projection).
|
||||
let span = tracing::info_span!("durable_task_attempt", task_id = %task_id, attempt_id = %attempt_id, fence);
|
||||
if let Ok(parent) = crank_core::TraceContext::parse(&lease.record.support.traceparent) {
|
||||
crank_trace::set_parent_from_trace_context(&span, &parent);
|
||||
}
|
||||
let outcome = handler
|
||||
.execute(&lease, &attempt_id, cancelled.clone(), permit)
|
||||
.instrument(span)
|
||||
.await;
|
||||
|
||||
cancelled.store(true, Ordering::Release);
|
||||
renewal_handle.abort();
|
||||
|
||||
// Terminal CAS under valid lease. Only one writer wins; stale token is rejected by store.
|
||||
let access = crank_core::TaskAccess {
|
||||
workspace_id: lease.record.workspace_id.clone(),
|
||||
owner_kind: lease.record.owner_kind,
|
||||
owner_id: lease.record.owner_id.clone(),
|
||||
authorization_scope: lease.record.authorization_scope.clone(),
|
||||
};
|
||||
|
||||
match outcome {
|
||||
TaskAttemptOutcome::Completed(value) => {
|
||||
let Ok(result) = TaskEnvelope::try_redacted(&value) else {
|
||||
finish_failed(
|
||||
&store,
|
||||
&owner,
|
||||
task_id,
|
||||
fence,
|
||||
&lease,
|
||||
"result_not_redactable",
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
};
|
||||
let _ = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access,
|
||||
task_id,
|
||||
expected_state: leased_expected_state(&lease),
|
||||
expected_version: lease.record.version,
|
||||
target_state: TaskState::Completed,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
},
|
||||
&owner,
|
||||
fence,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
TaskAttemptOutcome::Failed(value) => {
|
||||
let error = TaskEnvelope::try_redacted(&value).or_else(|_| {
|
||||
TaskEnvelope::try_redacted(&serde_json::json!({
|
||||
"code": "task_error_not_redactable"
|
||||
}))
|
||||
});
|
||||
let Ok(error) = error else {
|
||||
return;
|
||||
};
|
||||
let _ = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access,
|
||||
task_id,
|
||||
expected_state: leased_expected_state(&lease),
|
||||
expected_version: lease.record.version,
|
||||
target_state: TaskState::Failed,
|
||||
result: None,
|
||||
error: Some(error),
|
||||
},
|
||||
&owner,
|
||||
fence,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
TaskAttemptOutcome::OutcomeUnknown => {
|
||||
let _ = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access,
|
||||
task_id,
|
||||
expected_state: leased_expected_state(&lease),
|
||||
expected_version: lease.record.version,
|
||||
target_state: TaskState::OutcomeUnknown,
|
||||
result: None,
|
||||
error: None,
|
||||
},
|
||||
&owner,
|
||||
fence,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn leased_expected_state(lease: &TaskLease) -> TaskState {
|
||||
// Claimed tasks are expected to be in a non-terminal processing state.
|
||||
if lease.record.state == TaskState::Queued {
|
||||
TaskState::Queued
|
||||
} else {
|
||||
TaskState::Working
|
||||
}
|
||||
}
|
||||
|
||||
async fn finish_failed(
|
||||
store: &Arc<dyn TaskStore>,
|
||||
owner: &str,
|
||||
task_id: crank_core::TaskId,
|
||||
fence: u64,
|
||||
lease: &TaskLease,
|
||||
code: &str,
|
||||
) {
|
||||
let access = crank_core::TaskAccess {
|
||||
workspace_id: lease.record.workspace_id.clone(),
|
||||
owner_kind: lease.record.owner_kind,
|
||||
owner_id: lease.record.owner_id.clone(),
|
||||
authorization_scope: lease.record.authorization_scope.clone(),
|
||||
};
|
||||
let _ = store
|
||||
.finish_leased(
|
||||
TaskFinishRequest {
|
||||
access,
|
||||
task_id,
|
||||
expected_state: leased_expected_state(lease),
|
||||
expected_version: lease.record.version,
|
||||
target_state: TaskState::Failed,
|
||||
result: None,
|
||||
error: TaskEnvelope::try_redacted(&serde_json::json!({ "code": code })).ok(),
|
||||
},
|
||||
owner,
|
||||
fence,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
//! Runtime application service of durable Tasks (Story 4.2, FR-22/FR-23).
|
||||
//!
|
||||
//! The service is the only place that turns an approved background request into the durable domain
|
||||
//! request: bounded validated input, canonical input fingerprint, full-scope idempotency digest,
|
||||
//! pinned Published Version/deadline/retention and the support identities of the request. It holds an
|
||||
//! owned request, never `ResolvedAuth` or raw secrets, and it never dispatches: the durable store
|
||||
//! commits Task + dispatch intent + queue slot in one transaction and only then produces an
|
||||
//! acknowledgement.
|
||||
//!
|
||||
//! Reads go through the same owner-scoped access proof, so the durable row is only fetched for the
|
||||
//! caller that owns it. There is no in-memory fallback: a store failure is a typed refusal.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use crank_core::PlatformApiKeyId;
|
||||
use crank_core::{
|
||||
CorrelationContext, TASK_DEFAULT_RETENTION_MS, TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES,
|
||||
TASK_EXECUTABLE_INPUT_MAX_BYTES, TASK_MAX_RETENTION_MS, TASK_SCOPED_KEY_MAX_BYTES,
|
||||
TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskEnvelope, TaskError, TaskId, TaskLease,
|
||||
TaskOwnerKind, TaskPins, TaskRecord, TaskStore, TaskSupportIds, ToolExecutionMode,
|
||||
contains_sensitive_task_keys, current_unix_ms, validate_task_envelope,
|
||||
};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
/// One owned background request, already authorized by the application layer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TaskAcceptDraft {
|
||||
pub workspace_id: String,
|
||||
pub owner_kind: TaskOwnerKind,
|
||||
pub owner_id: String,
|
||||
/// Server-derived authorization scope identity of the verified principal.
|
||||
pub authorization_scope: String,
|
||||
/// Typed immutable Platform API key identity of the verified machine credential. It is sealed
|
||||
/// into the executable-input binding and revalidated authoritatively by the worker.
|
||||
pub platform_api_key_id: Option<PlatformApiKeyId>,
|
||||
pub operation_id: String,
|
||||
pub operation_version: u32,
|
||||
pub policy_mode: ToolExecutionMode,
|
||||
/// Caller key, bounded before it becomes a full-scope digest.
|
||||
pub scoped_key: String,
|
||||
/// Canonical executable input; it is validated, fingerprinted, then persisted redacted.
|
||||
pub input: serde_json::Value,
|
||||
pub correlation: CorrelationContext,
|
||||
/// Absolute execution deadline of the accepted work.
|
||||
pub deadline: Instant,
|
||||
/// Retention after the execution deadline.
|
||||
pub retention_ms: i64,
|
||||
pub approval_reference: Option<String>,
|
||||
}
|
||||
|
||||
/// Projection of the durable status/result read. `result`/`error` are only present once a terminal
|
||||
/// transition committed; a non-terminal Task reports its state and inputs without inventing output.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskOutcome {
|
||||
pub task_id: TaskId,
|
||||
pub state: crank_core::TaskState,
|
||||
pub result: Option<serde_json::Value>,
|
||||
pub error: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl TaskOutcome {
|
||||
fn from_record(record: TaskRecord) -> Self {
|
||||
Self {
|
||||
task_id: record.task_id,
|
||||
state: record.state,
|
||||
result: record.result.map(|envelope| envelope.value().clone()),
|
||||
error: record.error.map(|envelope| envelope.value().clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TaskService {
|
||||
store: Arc<dyn TaskStore>,
|
||||
crypto: Option<Arc<crate::SecretCrypto>>,
|
||||
}
|
||||
|
||||
impl TaskService {
|
||||
pub fn new(store: Arc<dyn TaskStore>) -> Self {
|
||||
Self {
|
||||
store,
|
||||
crypto: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_secret_crypto(store: Arc<dyn TaskStore>, crypto: Arc<crate::SecretCrypto>) -> Self {
|
||||
Self {
|
||||
store,
|
||||
crypto: Some(crypto),
|
||||
}
|
||||
}
|
||||
|
||||
/// Readiness of the mandatory durable Task state.
|
||||
pub async fn health(&self) -> Result<(), TaskError> {
|
||||
self.store.health().await
|
||||
}
|
||||
|
||||
/// Durable acceptance: the returned identity exists only after the store committed it.
|
||||
pub async fn accept(&self, draft: TaskAcceptDraft) -> Result<TaskAcceptance, TaskError> {
|
||||
validate_task_envelope(&draft.input)?;
|
||||
if contains_sensitive_task_keys(&draft.input) {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input contains sensitive fields",
|
||||
));
|
||||
}
|
||||
let (scoped_key, input_fingerprint) = (
|
||||
scoped_key_digest(&draft)?,
|
||||
canonical_input_fingerprint(&draft.input)?,
|
||||
);
|
||||
let execution_deadline_unix_ms = deadline_unix_ms(draft.deadline);
|
||||
let retention_ms = draft.retention_ms.clamp(0, TASK_MAX_RETENTION_MS);
|
||||
let task_id = TaskId::generate();
|
||||
let crypto = self.crypto.as_ref().ok_or_else(|| {
|
||||
TaskError::invalid("secret crypto is required for durable Task acceptance")
|
||||
})?;
|
||||
let binding = serde_json::json!({
|
||||
"task_id": task_id.to_string(),
|
||||
"workspace_id": draft.workspace_id,
|
||||
"owner_kind": draft.owner_kind.as_str(),
|
||||
"owner_id": draft.owner_id,
|
||||
"authorization_scope": draft.authorization_scope,
|
||||
"platform_api_key_id": draft
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(PlatformApiKeyId::as_str),
|
||||
"operation_id": draft.operation_id,
|
||||
"operation_version": draft.operation_version,
|
||||
"policy_mode": draft.policy_mode,
|
||||
"input_fingerprint": input_fingerprint,
|
||||
"input": draft.input,
|
||||
});
|
||||
let ciphertext = crypto
|
||||
.encrypt(&binding)
|
||||
.map_err(|error| TaskError::invalid(error.to_string()))?;
|
||||
if ciphertext.len() > TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES {
|
||||
return Err(TaskError::invalid(
|
||||
"encrypted task input exceeds its byte budget",
|
||||
));
|
||||
}
|
||||
let (
|
||||
executable_input_ciphertext,
|
||||
executable_input_key_version,
|
||||
executable_input_master_key_epoch,
|
||||
) = (
|
||||
Some(ciphertext),
|
||||
Some(crypto.key_version().to_owned()),
|
||||
Some(crypto.master_key_epoch()),
|
||||
);
|
||||
let request = TaskAcceptRequest {
|
||||
task_id,
|
||||
workspace_id: draft.workspace_id,
|
||||
owner_kind: draft.owner_kind,
|
||||
owner_id: draft.owner_id,
|
||||
authorization_scope: draft.authorization_scope,
|
||||
platform_api_key_id: draft.platform_api_key_id,
|
||||
operation_id: draft.operation_id,
|
||||
pins: TaskPins {
|
||||
operation_version: draft.operation_version,
|
||||
policy_mode: draft.policy_mode,
|
||||
execution_deadline_unix_ms,
|
||||
retain_until_unix_ms: execution_deadline_unix_ms.saturating_add(retention_ms),
|
||||
approval_reference: draft.approval_reference,
|
||||
},
|
||||
scoped_key,
|
||||
input_fingerprint,
|
||||
input: TaskEnvelope::try_redacted(&draft.input)?,
|
||||
support: TaskSupportIds::new(
|
||||
draft.correlation.request_id().clone(),
|
||||
draft.correlation.trace_context(),
|
||||
),
|
||||
executable_input_ciphertext,
|
||||
executable_input_key_version,
|
||||
executable_input_master_key_epoch,
|
||||
};
|
||||
self.store.accept(request).await
|
||||
}
|
||||
|
||||
/// Owner-scoped status read. A foreign or absent Task is one typed not-found.
|
||||
pub async fn status(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
task_id: TaskId,
|
||||
) -> Result<TaskRecord, TaskError> {
|
||||
self.store.read(access, task_id).await
|
||||
}
|
||||
|
||||
/// Owner-scoped result read, projected from the same authorized row.
|
||||
pub async fn result(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
task_id: TaskId,
|
||||
) -> Result<TaskOutcome, TaskError> {
|
||||
Ok(TaskOutcome::from_record(
|
||||
self.store.read(access, task_id).await?,
|
||||
))
|
||||
}
|
||||
|
||||
/// Decrypts and validates the executable input bound to a valid current lease.
|
||||
pub async fn executable_input(
|
||||
&self,
|
||||
lease: &TaskLease,
|
||||
) -> Result<serde_json::Value, TaskError> {
|
||||
let crypto = self.crypto.as_ref().ok_or_else(|| {
|
||||
TaskError::invalid("secret crypto is required for durable Task execution")
|
||||
})?;
|
||||
let (ciphertext, key_version, epoch) = self
|
||||
.store
|
||||
.read_executable_input(&lease.owner, lease.record.task_id, lease.fence)
|
||||
.await?;
|
||||
if ciphertext.len() > TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
let binding = crypto
|
||||
.decrypt_for_epoch(&key_version, epoch, &ciphertext)
|
||||
.map_err(|error| TaskError::invalid(error.to_string()))?;
|
||||
let object = binding
|
||||
.as_object()
|
||||
.ok_or_else(|| TaskError::invalid("task executable input binding is invalid"))?;
|
||||
let check_string = |field: &str, expected: &str| -> Result<(), TaskError> {
|
||||
match object.get(field).and_then(|value| value.as_str()) {
|
||||
Some(actual) if actual == expected => Ok(()),
|
||||
_ => Err(TaskError::invalid("task executable input binding mismatch")),
|
||||
}
|
||||
};
|
||||
check_string("task_id", &lease.record.task_id.to_string())?;
|
||||
check_string("workspace_id", &lease.record.workspace_id)?;
|
||||
check_string("owner_id", &lease.record.owner_id)?;
|
||||
check_string("authorization_scope", &lease.record.authorization_scope)?;
|
||||
let expected_key = lease
|
||||
.record
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(PlatformApiKeyId::as_str);
|
||||
match (object.get("platform_api_key_id"), expected_key) {
|
||||
(None | Some(serde_json::Value::Null), None) => {}
|
||||
(Some(serde_json::Value::String(actual)), Some(expected)) if actual == expected => {}
|
||||
_ => {
|
||||
return Err(TaskError::invalid("task executable input binding mismatch"));
|
||||
}
|
||||
}
|
||||
check_string("operation_id", &lease.record.operation_id)?;
|
||||
match object.get("owner_kind").and_then(|value| value.as_str()) {
|
||||
Some(actual) if actual == lease.record.owner_kind.as_str() => {}
|
||||
_ => return Err(TaskError::invalid("task executable input binding mismatch")),
|
||||
}
|
||||
match object
|
||||
.get("operation_version")
|
||||
.and_then(|value| value.as_u64())
|
||||
{
|
||||
Some(actual) if actual == u64::from(lease.record.pins.operation_version) => {}
|
||||
_ => return Err(TaskError::invalid("task executable input binding mismatch")),
|
||||
}
|
||||
let expected_policy = match lease.record.pins.policy_mode {
|
||||
ToolExecutionMode::Synchronous => "synchronous",
|
||||
ToolExecutionMode::Background => "background",
|
||||
ToolExecutionMode::Adaptive => "adaptive",
|
||||
};
|
||||
match object.get("policy_mode").and_then(|value| value.as_str()) {
|
||||
Some(actual) if actual == expected_policy => {}
|
||||
_ => return Err(TaskError::invalid("task executable input binding mismatch")),
|
||||
}
|
||||
check_string("input_fingerprint", &lease.record.input_fingerprint)?;
|
||||
let input = object
|
||||
.get("input")
|
||||
.cloned()
|
||||
.ok_or_else(|| TaskError::invalid("task executable input binding is missing input"))?;
|
||||
let canonical = canonical_input_fingerprint(&input)?;
|
||||
if canonical != lease.record.input_fingerprint {
|
||||
return Err(TaskError::invalid("task executable input binding mismatch"));
|
||||
}
|
||||
validate_task_envelope(&input)?;
|
||||
let encoded = serde_json::to_vec(&input)
|
||||
.map_err(|_| TaskError::invalid("task executable input is not serializable"))?;
|
||||
if encoded.len() > TASK_EXECUTABLE_INPUT_MAX_BYTES {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input exceeds its byte budget",
|
||||
));
|
||||
}
|
||||
if contains_sensitive_task_keys(&input) {
|
||||
return Err(TaskError::invalid(
|
||||
"task executable input contains sensitive fields",
|
||||
));
|
||||
}
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
/// Default retention pinned by the application layer when it does not choose one explicitly.
|
||||
pub fn default_retention_ms() -> i64 {
|
||||
TASK_DEFAULT_RETENTION_MS
|
||||
}
|
||||
|
||||
/// Scoped lookup of an already accepted Task by the same full-scope identity, used to keep a
|
||||
/// retry of an explicitly pinned version from being accepted as new work.
|
||||
pub async fn lookup(&self, draft: &TaskAcceptDraft) -> Result<Option<TaskRecord>, TaskError> {
|
||||
let scoped_key = scoped_key_digest(draft)?;
|
||||
let access = TaskAccess {
|
||||
workspace_id: draft.workspace_id.clone(),
|
||||
owner_kind: draft.owner_kind,
|
||||
owner_id: draft.owner_id.clone(),
|
||||
authorization_scope: draft.authorization_scope.clone(),
|
||||
};
|
||||
self.store
|
||||
.find(&access, &draft.operation_id, &scoped_key)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Canonical digest of a validated input, exposed so the application layer can compare a stored
|
||||
/// Task against the request it is retrying without duplicating the canonicalization.
|
||||
pub fn input_fingerprint(input: &serde_json::Value) -> Result<String, TaskError> {
|
||||
canonical_input_fingerprint(input)
|
||||
}
|
||||
}
|
||||
|
||||
fn deadline_unix_ms(deadline: Instant) -> i64 {
|
||||
let remaining = u64::try_from(
|
||||
deadline
|
||||
.saturating_duration_since(Instant::now())
|
||||
.as_millis(),
|
||||
)
|
||||
.unwrap_or_default();
|
||||
current_unix_ms().saturating_add(i64::try_from(remaining.max(1)).unwrap_or(i64::MAX))
|
||||
}
|
||||
|
||||
/// Digest of the caller key over the full Task identity. The scope is never abbreviated to a
|
||||
/// connection, protocol revision or raw credential, so two callers cannot collide on one key.
|
||||
fn scoped_key_digest(draft: &TaskAcceptDraft) -> Result<String, TaskError> {
|
||||
let key = draft.scoped_key.as_str();
|
||||
if key.is_empty()
|
||||
|| key.len() > TASK_SCOPED_KEY_MAX_BYTES
|
||||
|| key.bytes().any(|byte| byte.is_ascii_control())
|
||||
{
|
||||
return Err(TaskError::invalid("task idempotency key is invalid"));
|
||||
}
|
||||
let material = format!(
|
||||
"{}\n{}\n{}\n{}\n{}\n{}",
|
||||
draft.workspace_id,
|
||||
draft.owner_kind.as_str(),
|
||||
draft.owner_id,
|
||||
draft.authorization_scope,
|
||||
draft.operation_id,
|
||||
key
|
||||
);
|
||||
Ok(hex_digest(material.as_bytes()))
|
||||
}
|
||||
|
||||
/// Canonical digest of the validated executable input. Object key order never changes the digest,
|
||||
/// while any material business or secret value does.
|
||||
fn canonical_input_fingerprint(input: &serde_json::Value) -> Result<String, TaskError> {
|
||||
let canonical = crate::idempotency::canonical_json(input);
|
||||
let encoded = serde_json::to_vec(&canonical)
|
||||
.map_err(|_| TaskError::invalid("task input is not canonicalizable"))?;
|
||||
Ok(hex_digest(&encoded))
|
||||
}
|
||||
|
||||
fn hex_digest(bytes: &[u8]) -> String {
|
||||
Sha256::digest(bytes)
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn draft(key: &str, input: serde_json::Value) -> TaskAcceptDraft {
|
||||
TaskAcceptDraft {
|
||||
workspace_id: "ws_default".to_owned(),
|
||||
owner_kind: TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_1".to_owned(),
|
||||
platform_api_key_id: Some(PlatformApiKeyId::new("pk_1")),
|
||||
operation_id: "op_report".to_owned(),
|
||||
operation_version: 2,
|
||||
policy_mode: ToolExecutionMode::Background,
|
||||
scoped_key: key.to_owned(),
|
||||
input,
|
||||
correlation: CorrelationContext::generate(),
|
||||
deadline: Instant::now() + std::time::Duration::from_secs(30),
|
||||
retention_ms: TASK_DEFAULT_RETENTION_MS,
|
||||
approval_reference: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canonical_fingerprint_ignores_key_order_and_tracks_values() {
|
||||
let first = canonical_input_fingerprint(&json!({"a": 1, "b": {"c": 2}})).unwrap();
|
||||
let reordered = canonical_input_fingerprint(&json!({"b": {"c": 2}, "a": 1})).unwrap();
|
||||
assert_eq!(first, reordered);
|
||||
assert_eq!(first.len(), 64);
|
||||
|
||||
let changed = canonical_input_fingerprint(&json!({"a": 1, "b": {"c": 3}})).unwrap();
|
||||
assert_ne!(first, changed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_key_digest_covers_the_whole_identity() {
|
||||
let base = scoped_key_digest(&draft("key_1", json!({}))).unwrap();
|
||||
let mut foreign = draft("key_1", json!({}));
|
||||
foreign.owner_id = "agent_b".to_owned();
|
||||
assert_ne!(base, scoped_key_digest(&foreign).unwrap());
|
||||
|
||||
let mut other_scope = draft("key_1", json!({}));
|
||||
other_scope.authorization_scope = "credential:pk_2".to_owned();
|
||||
assert_ne!(base, scoped_key_digest(&other_scope).unwrap());
|
||||
|
||||
assert!(scoped_key_digest(&draft("", json!({}))).is_err());
|
||||
assert!(
|
||||
scoped_key_digest(&draft(
|
||||
&"k".repeat(TASK_SCOPED_KEY_MAX_BYTES + 1),
|
||||
json!({})
|
||||
))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,83 @@ async fn deadline_after_read_only_dispatch_remains_safe_to_retry() {
|
||||
assert_eq!(failure.stage(), ExecutionStage::Upstream);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn adapter_retry_child_spans_are_distinct_without_a_durable_attempt() {
|
||||
let _tracing_test_guard = TRACING_TEST_LOCK.lock().await;
|
||||
let capture = TraceCapture::default();
|
||||
let subscriber = tracing_subscriber::registry().with(capture.clone());
|
||||
|
||||
let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||
let executor = RuntimeExecutorBuilder::new()
|
||||
.register_adapter(Arc::new(FlakyReadAdapter {
|
||||
call_count: Arc::clone(&call_count),
|
||||
}))
|
||||
.build();
|
||||
let mut operation: crank_runtime::RuntimeOperation = operation().into();
|
||||
let Target::Rest(target) = &mut operation.target;
|
||||
target.method = HttpMethod::Get;
|
||||
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
|
||||
|
||||
// The synchronous tools/call path has no durable Attempt: every adapter attempt gets its own
|
||||
// synthetic id. A Task dispatch instead reuses the durable id (mcp-server `attempt_trace`).
|
||||
let result = async {
|
||||
let request_root = tracing::info_span!("request_root");
|
||||
executor
|
||||
.execute(&operation, &json!({"name": "retry-read"}))
|
||||
.instrument(request_root)
|
||||
.await
|
||||
}
|
||||
.with_subscriber(subscriber)
|
||||
.await
|
||||
.expect("read retry should recover");
|
||||
|
||||
assert_eq!(result, json!({"accepted": true}));
|
||||
|
||||
let spans = capture.snapshot();
|
||||
let runtime_span = spans
|
||||
.iter()
|
||||
.find(|span| span.name == "runtime.execute")
|
||||
.expect("runtime.execute span");
|
||||
assert_eq!(
|
||||
runtime_span.parent_name,
|
||||
Some("request_root"),
|
||||
"the execution continues the caller's trace root"
|
||||
);
|
||||
let attempt_spans: Vec<_> = spans
|
||||
.iter()
|
||||
.filter(|span| span.name == "adapter_attempt")
|
||||
.collect();
|
||||
assert_eq!(attempt_spans.len(), 2);
|
||||
for span in &attempt_spans {
|
||||
assert_eq!(span.parent_name, Some("runtime.execute"));
|
||||
assert!(
|
||||
!span
|
||||
.fields
|
||||
.get("attempt_id")
|
||||
.unwrap_or(&"".to_string())
|
||||
.is_empty()
|
||||
);
|
||||
assert!(
|
||||
!span
|
||||
.fields
|
||||
.get("attempt_id")
|
||||
.unwrap_or(&"".to_string())
|
||||
.contains("traceparent")
|
||||
);
|
||||
}
|
||||
let attempts: Vec<String> = attempt_spans
|
||||
.iter()
|
||||
.map(|span| span.fields.get("attempt").cloned().unwrap_or_default())
|
||||
.collect();
|
||||
assert!(attempts.contains(&"1".to_string()));
|
||||
assert!(attempts.contains(&"2".to_string()));
|
||||
let ids: Vec<String> = attempt_spans
|
||||
.iter()
|
||||
.map(|span| span.fields.get("attempt_id").cloned().unwrap_or_default())
|
||||
.collect();
|
||||
assert_ne!(ids[0], ids[1]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_retry_revalidates_adapter_attempt_and_keeps_one_success_outcome() {
|
||||
let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||
@@ -236,11 +313,7 @@ async fn successful_execution_has_real_stages_and_omits_inapplicable_ones() {
|
||||
.find(|span| span.name == "runtime.execute")
|
||||
.expect("runtime span");
|
||||
assert_eq!(runtime.parent_name, Some("mcp.request"));
|
||||
for child in [
|
||||
"runtime.arguments.map",
|
||||
"upstream.http",
|
||||
"runtime.response.transform",
|
||||
] {
|
||||
for child in ["runtime.arguments.map", "runtime.response.transform"] {
|
||||
assert_eq!(
|
||||
spans
|
||||
.iter()
|
||||
@@ -250,6 +323,14 @@ async fn successful_execution_has_real_stages_and_omits_inapplicable_ones() {
|
||||
"{child} must be a runtime child"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
spans
|
||||
.iter()
|
||||
.find(|span| span.name == "upstream.http")
|
||||
.and_then(|span| span.parent_name),
|
||||
Some("adapter_attempt"),
|
||||
"upstream.http must be an adapter child"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -18,8 +18,9 @@ feature jobs должны быть `skipped` для story и `success` для fe
|
||||
У Crank и FreeHub полный Rust clippy/workspace test и тяжёлые feature/security
|
||||
проверки выполняются только на feature. UI build и быстрые UI unit tests остаются
|
||||
на story, Playwright E2E — на feature. Crank image build/scan/compose smoke — на
|
||||
feature; фактический Deploy остаётся отдельным push-main job со своими build,
|
||||
scan, health и требованием успешного SHA. Тег релиза по умолчанию запускает story
|
||||
feature; фактический Deploy запускается только явным workflow_dispatch на main
|
||||
с validation_level=feature и deploy_target=production после успешных feature gates.
|
||||
Обычный merge/push не выкатывает production. Порядок: delivery-and-deploy.md. Тег релиза по умолчанию запускает story
|
||||
ATDD, сборку бинарных/UI артефактов, security audit и image scan. Полный release
|
||||
clippy/workspace/E2E доступен через ручной dispatch с `validation_level: feature`;
|
||||
отчёт evidence прикладывается только если такой E2E был выполнен. FreeHub
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
# Delivery и production deploy
|
||||
|
||||
## Разделение merge и выкатки
|
||||
|
||||
Push в `main` запускает только source CI: story-проверки, UI build и
|
||||
`Required Checks`. Он не запускает production deploy и не загружает секреты
|
||||
OpenBao. PR, метка `feature-ready`, story dispatch и dispatch без явно выбранного
|
||||
production также не выкатывают production.
|
||||
|
||||
Production deploy выполняет только job `Deploy` workflow `CI` и только при
|
||||
одновременном выполнении условий:
|
||||
|
||||
- событие `workflow_dispatch`;
|
||||
- ref `refs/heads/main`;
|
||||
- `validation_level: feature`;
|
||||
- `deploy_target: production`;
|
||||
- успешный `Required Checks`, включая feature jobs `Rust Feature Validation`,
|
||||
`Frontend E2E` и `Community Image Smoke`.
|
||||
|
||||
Любая другая комбинация оставляет deploy пропущенным, поэтому принятый merge в
|
||||
`main` больше не блокируется ожиданием production-выкатки.
|
||||
|
||||
## Ручной запуск
|
||||
|
||||
В Gitea UI: `Actions` -> workflow `CI` -> `Run workflow`.
|
||||
|
||||
Указать точные входные значения:
|
||||
|
||||
| Input | Значение | Обязателен |
|
||||
| --- | --- | --- |
|
||||
| Branch / ref | `main` | да |
|
||||
| `validation_level` | `feature` | да |
|
||||
| `deploy_target` | `production` | да |
|
||||
|
||||
`validation_level: story`, `deploy_target: none` или отсутствие `deploy_target`
|
||||
дают только source CI либо feature-проверки без deploy. Нельзя запускать
|
||||
production deploy из PR, с другой ветки или через push в `main`.
|
||||
|
||||
## Approval и runbook
|
||||
|
||||
Сам по себе ручной dispatch не является автоматическим разрешением на
|
||||
production. Перед запуском release owner или DevOps явно подтверждает:
|
||||
|
||||
1. SHA совпадает с принятым и проверенным коммитом в `main`.
|
||||
2. `Required Checks` для этого SHA успешны, включая feature jobs.
|
||||
3. Пройдены [production checklist](../production-checklist.md) и актуальный
|
||||
[deploy and staging smoke](../deploy-and-staging-smoke.md); известен порядок
|
||||
rollback и проверено наличие backup.
|
||||
4. Нет незапланированных изменений секретов, миграций или инфраструктуры.
|
||||
|
||||
После deploy проверить health endpoints и authenticated product smoke, затем
|
||||
зафиксировать результат в staging/production regression notes. Секреты OpenBao
|
||||
загружаются только внутри уже разрешённого job `Deploy`; не выводить их в логи и
|
||||
не передавать в PR или dispatch story.
|
||||
|
||||
## Ограничения
|
||||
|
||||
Workflow `Community Release` для тегов остаётся отдельным процессом публикации
|
||||
release-образов и не является заменой этому ручному production deploy. Изменение
|
||||
`ci.yml` не даёт постоянного разрешения на выкатку и не меняет приложение.
|
||||
@@ -0,0 +1,78 @@
|
||||
# Переход Crank на официальный MCP SDK rmcp
|
||||
|
||||
Статус: планирование одобрено владельцем 27.10.2026. Полная миграция и production-переключение этим планом не запускаются. Владелец планирования и очереди — hermes_tl; реализация — hermes_dev; независимая проверка — hermes_qa; Git/CI/релизы — hermes_devops. ARC подключается к конкретному техническому затруднению, не как обязательный новый этап.
|
||||
|
||||
## Результат
|
||||
|
||||
Стандартный протокольный слой MCP обслуживает официальный SDK. Crank сохраняет авторизацию, права Agent/workspace, опубликованные версии, REST mapping, admission и исполнение команд. Обе действующие версии протокола и функции Tools/Resources/Prompts остаются доступны. Снижается объём собственного протокольного кода; ускорение и производительность не объявляются измеренными заранее.
|
||||
|
||||
Исходная точка: принятый main после 4.1, известный merge cc612516a29d48f2831ba6bdf58c1dc777aa6abf. Перед исполнением получить актуальный main и убедиться, что в него вошла проверенная поставка. Плановый кандидат SDK — опубликованный rmcp 3.7.0; окончательный pin и lock проверяются на первом этапе, автоматического обновления на другую версию нет.
|
||||
|
||||
## 1. Проверить тонкий адаптер на реальном MCP-пути
|
||||
|
||||
Исполнитель: DEV, независимая QA. Риск: высокий — ошибка может нарушить доступ к инструментам.
|
||||
|
||||
Сделать изолированный исполняемый прототип SDK на существующей границе типизированных команд, сохранив авторизацию и динамический каталог. Публичный production endpoint пока обслуживает прежний путь.
|
||||
|
||||
Проверяемый результат:
|
||||
|
||||
- Динамические tools/list и tools/call работают через SDK с действующими сервисами Crank, включая запрет чужого Agent/неверных credentials и сохранение ошибок инструмента.
|
||||
- Через настоящий HTTP-путь проверены обе revisions: legacy initialize/session и modern discover/stateless, JSON/SSE, headers, progress и cancellation.
|
||||
- Отдельно проверены пригодность SessionManager для существующего хранения сессий, переподключение и владение отменой; наличие SDK-типа само по себе не считается доказательством.
|
||||
- Установлены существенные gaps для Resources/Prompts/notifications/subscriptions и текущих execution modes. Наличие интерфейса не выдаётся за полную совместимость.
|
||||
- На небольшом smoke-наборе проверено, что adapter не обходит действующие ограничения; замеченный риск памяти/очередей записан. Нагрузочная квалификация относится к завершённой функции, не превращается в условие этого прототипа. Существующие ceilings не меняются.
|
||||
- Результат — работающий код и краткое решение TL: готов перенос либо конкретная несовместимость с минимальным вариантом решения. Если потребуется ослабить контракт, заменить хранилище или создать большой дополнительный framework — вопрос владельцу до продолжения.
|
||||
|
||||
Это один достаточный опыт, а не серия исследований SDK. Использовать существующие wire fixtures и точечные проверки, сохранять непроверенные пункты явно.
|
||||
|
||||
## 2. Перенести действующие MCP-функции на SDK
|
||||
|
||||
Зависит от положительного результата этапа 1 и решения TL о достаточности совместимости. Исполнитель: DEV, независимая QA. Риск: высокий — затрагивается общий клиентский интерфейс.
|
||||
|
||||
Подключить SDK к действующему пути и перенести стандартные envelopes, dispatch, lifecycle и transport. Порядок переноса устанавливается по результату первого опыта, без параллельных writers одних MCP-файлов.
|
||||
|
||||
Проверяемый результат:
|
||||
|
||||
- Через SDK проходят действующие Tools, Resources, Prompts, completion, уведомления и подписки в применимых revisions, а также progress/cancel и существующая logging policy.
|
||||
- Сохранены авторизация/отзыв доступа, agent scope, advertised capabilities, admission, deadlines и семантика ошибок. SDK defaults не расширяют объявленные capabilities или список разрешённых revisions.
|
||||
- Сессии, reconnect и ограничения памяти/очередей сохраняют действующий контракт; in-memory хранилище не заменяет существующее долговременное хранение без решения.
|
||||
- Перенесённые участки собственного протокольного кода удаляются по мере принятия. Временный способ безопасного сравнения/отката не превращается во второй постоянный MCP runtime.
|
||||
- Существующие affected ATDD подтверждают старое поведение; тесты меняются только там, где проверяли приватное устройство старого адаптера, а не нужный клиенту контракт.
|
||||
|
||||
Новые бизнес-возможности, переработка REST runtime, базы, модели или UI в эту работу не входят.
|
||||
|
||||
## 3. Завершить замену и поставить результат
|
||||
|
||||
Зависит от принятого этапа 2. Исполнители: DEV/QA, затем DevOps. Риск: высокий — финальный переход общего MCP-пути. Внешняя проверка относительно реализации: независимая QA на существующем клиентском/wire-наборе обеих revisions.
|
||||
|
||||
Проверяемый результат:
|
||||
|
||||
- Удалены ставшие ненужными собственные parser/dispatch/transport участки, временные переключатели и неиспользуемые зависимости. Поддержка legacy protocol при этом сохраняется средствами SDK.
|
||||
- Один необходимый финальный feature-прогон подтверждает реальный клиентский путь обеих revisions, изоляцию доступа, streaming/cancel/session и применимые ограничения ресурсов. Полный набор не повторяется отдельно каждой ролью; обязательные CI-гейты сохраняются.
|
||||
- DevOps выполнил commit/push/PR, protected merge и честный release с проверенными артефактами. Документация описывает активный SDK, поддерживаемые версии и реальные ограничения.
|
||||
- План отката указывает предыдущую принятую версию и проверяет совместимость состояния. Production-переключение выполняется только отдельным разрешённым manual deployment по существующему runbook; эта карточка не даёт разрешения на выкатку.
|
||||
|
||||
## Порядок относительно текущей разработки
|
||||
|
||||
1. Закончить уже выполняемую проверку поставки 4.1, не открывать её разработку заново.
|
||||
2. Планировать SDK-адаптер перед дальнейшим расширением заменяемого transport/protocol слоя. Этап 1 не ждёт завершения будущих 4.2–4.7 или всей нагрузочной кампании.
|
||||
3. Доменные части будущих задач могут идти параллельно при отсутствии общего writer и явных интерфейсных зависимостях. Новую протокольную реализацию и миграцию одних файлов параллельно не вести.
|
||||
4. TL сопоставляет существующие 4.2–4.7 с SDK, чтобы не реализовать вручную уже предоставленный механизм. Действующие продуктовые AC не сокращаются. Неизвестная совместимость — проверка этапа 1, а не повод считать будущие функции готовыми.
|
||||
|
||||
## Постановка и контроль
|
||||
|
||||
Достаточно трёх последовательных исполнений по этапам выше, с same-card QA и действующей приёмкой TL; не создавать отдельный новый бизнес-эпик, PRD, новый тестовый framework или цепочку архитектурных согласований.
|
||||
|
||||
На текущем шаге TL фиксирует эти этапы в существующем backlog/Gitea, зависимости и приоритет относительно текущей работы. Будущие исполнительские карточки оставить в triage/backlog; не запускать полную миграцию автоматически из задачи планирования. Сроки не выдумывать до первого опыта. Подтверждённая непригодность SDK эскалируется владельцу с фактами и вариантами, а не молчаливым возвращением к самописному MCP.
|
||||
|
||||
## Основания
|
||||
|
||||
Канонические пути ниже находятся на Hermes `192.168.1.127` (SSH-пользователь `codex`); относительные пути считаются от `/home/codex/code/crank`. Это доступный TL источник R, а не текущая локальная папка оператора. Локальная копия аудита у оператора: `/home/bsodfather/code/hermes/deploy/mcp-sdk-audit-20261027/REPORT.md`.
|
||||
|
||||
- Решение владельца 27.10.2026: запланировать переход на rmcp.
|
||||
- Аудит: `/home/codex/.hermes/codex-operator/handoffs/crank-rmcp-audit-20261027.md`.
|
||||
- Действующие AC: `_bmad-output/initiative-crank/epic-mcp-capabilities/story-mcp-revision-capabilities.md`, в том числе оценка SDK AT-3.1-14; остальные MCP AC остаются действующими.
|
||||
- Официальный SDK: https://github.com/modelcontextprotocol/rust-sdk ; документация ServerHandler: https://docs.rs/rmcp/latest/rmcp/handler/server/trait.ServerHandler.html .
|
||||
- Общий процесс: `/home/codex/.hermes/team/development-delivery.md`, `validation-policy.md`, `delivery-efficiency.md`.
|
||||
|
||||
Предыдущее default-правило «сохранять собственный transport» не является техническим доказательством отказа от SDK. Цель нового плана — проверить и принять SDK-путь; продуктовые и security-контракты сохраняются.
|
||||
@@ -35,3 +35,56 @@ version, stage, stable error code, retryability и certainty. Для legacy v4
|
||||
Пути Admin API, MCP methods и успешные response fields не изменены. Поля ошибки
|
||||
добавлены обратно совместимо. Outbound DNS/redirect/retry/SSRF policy принадлежит
|
||||
Story 1.9 и этой границей не считается завершённой.
|
||||
|
||||
## Durable background Task (Story 4.2)
|
||||
|
||||
Принятие фоновой работы идёт через `TaskApplication → crank-runtime::TaskService →
|
||||
TaskStore`. PostgreSQL-реализация выполняет одну транзакцию: Task, dispatch intent
|
||||
(`execution_admission_entries.task_id`) и queue slot коммитятся вместе, и только
|
||||
после commit выдаётся ack с Task ID/поддержкой Request/Trace/parent TraceContext.
|
||||
Повтор того же полного scoped key возвращает сохранённую Task, другой canonical
|
||||
input fingerprint — типизированный конфликт; явная другая published-версия при том
|
||||
же ключе не перезакрепляет уже принятую работу. Owner-скоуп (workspace, Agent/admin,
|
||||
authorization scope) входит в каждый read; чужой Task неотличим от отсутствующего.
|
||||
|
||||
Границы этой story: публичный MCP wire Tasks/capabilities/activation остаётся
|
||||
Story 4.7, worker/leases/recovery реализованы в Story 4.3 (ниже). Существующий
|
||||
`tools/call` сохраняет synchronous/unsupported поведение; durable acceptance не
|
||||
запускает ephemeral execution и не рекламирует клиенту background capability.
|
||||
|
||||
## Исполнение durable Task (Story 4.3)
|
||||
|
||||
`mcp-server` запускает fenced worker в том же процессе: bounded claim через
|
||||
PostgreSQL `FOR UPDATE SKIP LOCKED` над reserved dispatch intent, lease owner/expiry
|
||||
и monotonic fence; Valkey не является источником истины и может только ускорять
|
||||
wake-up. Claim использует уже принятый shared admission из Story 4.1
|
||||
(`lock_admission_in_tx` по controller-строке `*` + `grant_next_task` с его
|
||||
scoped-лимитами, bounded queue и weighted fairness cursor) и не добавляет второго
|
||||
scheduler'а: 4.3-дельта лишь fencing/lease-поля и CAS-предикаты поверх того же
|
||||
grant, поэтому принятые fairness-инварианты 4.1 сохраняются. Progress, renewal,
|
||||
dispatch checkpoint и terminal outcome пишутся только через CAS по текущему
|
||||
owner/fence/expiry. До сетевого вызова worker повторно проверяет публикацию и
|
||||
точную версию capability, активный Platform API key/scope, approval policy и
|
||||
deadline; невалидная работа завершается без dispatch. Attempt ID и dispatch
|
||||
checkpoint коммитятся атомарно до outbound; после checkpoint потеря lease означает
|
||||
uncertain outcome и Task переходит в `outcome_unknown` без автоматического retry.
|
||||
Исполняемый input берётся только из зашифрованного Task payload через
|
||||
`TaskService::executable_input` (redacted projection никогда не исполняется).
|
||||
Parent TraceContext сохраняется с acceptance; durable attempt span продолжает его,
|
||||
а каждая adapter-попытка (включая retry существующих GET/verified-idempotent write)
|
||||
получает собственный child span `adapter_attempt` с тем же durable Attempt ID и
|
||||
номером попытки, поэтому retry не сливается с первым attempt в трассе, а любой
|
||||
такой span сопоставим с `tasks.current_attempt_id`; синтетический attempt id
|
||||
остаётся только у синхронного tools/call, где durable попытки нет. Скан PostgreSQL авторитетен:
|
||||
crash до checkpoint возвращает Task в queue по истечении lease (recovery ≤60s).
|
||||
|
||||
Graceful shutdown является процессным, а не только worker-локальным: сигнал
|
||||
SIGTERM/ctrl-c одновременно прекращает новые claims и запускает HTTP graceful
|
||||
drain (worker не ждёт завершения долгого соединения), активные attempts
|
||||
checkpoint-ятся/дренируются в пределах 30s, а ошибочное завершение HTTP-сервера
|
||||
также останавливает worker. Уже поставленные в queue Tasks не теряются. Остаётся
|
||||
узкое окно между durable checkpoint и созданием sealed dispatch permit: если stop
|
||||
приходит именно туда, Task финализируется как failed(`shutdown_before_dispatch`),
|
||||
не отправляя запрос; это доказано регрессией, которая детерминированно
|
||||
задерживает checkpoint в его собственной транзакции, проверяет отсутствие вызова
|
||||
adapter и сохранность ещё queued работы.
|
||||
@@ -1 +1 @@
|
||||
{"schema_version":1,"sequence":[{"backfill":{"kind":"none"},"checksum":"crank-community-baseline-v1","compatibility":"legacy-baseline","contract_evidence":null,"name":"community-baseline-v1","owner":"crank-registry","phase":"expand","readable_schema_max":1,"readable_schema_min":1,"source_digest":"eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675","transactional":true,"version":1},{"backfill":{"kind":"none"},"checksum":"1908b97ca7fe8a85d146b0eebf007d5018a9ff406cb099614270f3428ec1db48","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"legacy-consolidation-v2","owner":"crank-registry","phase":"expand","readable_schema_max":2,"readable_schema_min":1,"source_digest":"1908b97ca7fe8a85d146b0eebf007d5018a9ff406cb099614270f3428ec1db48","transactional":true,"version":2},{"backfill":{"kind":"none"},"checksum":"36487625503a8d4d8f18d5771c3c9b6705f845e267acbfcb7244c330f640cd94","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"request-trace-identity-v3","owner":"crank-registry","phase":"expand","readable_schema_max":3,"readable_schema_min":2,"source_digest":"36487625503a8d4d8f18d5771c3c9b6705f845e267acbfcb7244c330f640cd94","transactional":true,"version":3},{"backfill":{"kind":"none"},"checksum":"45723712a1ea49cd8bbf59d77148c225f3ec376df7433983d982cc6f9d7fb39c","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"operation-lifecycle-v4","owner":"crank-registry","phase":"expand","readable_schema_max":4,"readable_schema_min":3,"source_digest":"45723712a1ea49cd8bbf59d77148c225f3ec376df7433983d982cc6f9d7fb39c","transactional":true,"version":4},{"backfill":{"kind":"none"},"checksum":"bd6703249cc407789586327eb7fbbd5776ba27923c5dc7eb85b05d332585bf42","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"execution-outcome-v5","owner":"crank-registry","phase":"expand","readable_schema_max":5,"readable_schema_min":4,"source_digest":"bd6703249cc407789586327eb7fbbd5776ba27923c5dc7eb85b05d332585bf42","transactional":true,"version":5},{"backfill":{"kind":"none"},"checksum":"94dba9b9dd3364607bc37e7698478ba6644607a7f268a1621cd3acea6bc96c2d","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"platform-key-name-reuse-v6","owner":"crank-registry","phase":"expand","readable_schema_max":6,"readable_schema_min":5,"source_digest":"94dba9b9dd3364607bc37e7698478ba6644607a7f268a1621cd3acea6bc96c2d","transactional":true,"version":6},{"backfill":{"kind":"none"},"checksum":"06e5d4667d9a7346474e9dcb176b8b2b9680c4b89089bea5a18eb5a88d5ac60e","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"master-key-identity-v7","owner":"crank-registry","phase":"expand","readable_schema_max":7,"readable_schema_min":6,"source_digest":"06e5d4667d9a7346474e9dcb176b8b2b9680c4b89089bea5a18eb5a88d5ac60e","transactional":true,"version":7},{"backfill":{"kind":"none"},"checksum":"6361f3321a1442a77c695cad9b30c4702de1aa3e565bfbf1a7415d653302611f","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"admin-auth-lifecycle-v8","owner":"crank-registry","phase":"expand","readable_schema_max":8,"readable_schema_min":7,"source_digest":"6361f3321a1442a77c695cad9b30c4702de1aa3e565bfbf1a7415d653302611f","transactional":true,"version":8},{"backfill":{"kind":"none"},"checksum":"b49bf4b53691407ec27e3810b0531c0d440da7dddc62ea71eb219933c0a30211","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"agent-catalog-lifecycle-v9","owner":"crank-registry","phase":"expand","readable_schema_max":9,"readable_schema_min":8,"source_digest":"b49bf4b53691407ec27e3810b0531c0d440da7dddc62ea71eb219933c0a30211","transactional":true,"version":9},{"backfill":{"kind":"none"},"checksum":"6c87f680efdbc275fa2b920826b3e3e390aa34d6f2f1db48fe074a5d7691ba5b","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"approval-side-effects-v10","owner":"crank-registry","phase":"expand","readable_schema_max":10,"readable_schema_min":9,"source_digest":"6c87f680efdbc275fa2b920826b3e3e390aa34d6f2f1db48fe074a5d7691ba5b","transactional":true,"version":10},{"backfill":{"kind":"none"},"checksum":"a439bbcdc9cc909d717ed5a868c7a51bd1ad3c49c4e85fd20933743ee3f31166","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"onboarding-product-events-v11","owner":"crank-registry","phase":"expand","readable_schema_max":11,"readable_schema_min":10,"source_digest":"a439bbcdc9cc909d717ed5a868c7a51bd1ad3c49c4e85fd20933743ee3f31166","transactional":true,"version":11},{"backfill":{"kind":"none"},"checksum":"052058da53cd861b2a1af81243b34cc35204d665a9276f8ece563e061f8ebbfe","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"artifact-metadata-v12","owner":"crank-registry","phase":"expand","readable_schema_max":12,"readable_schema_min":11,"source_digest":"052058da53cd861b2a1af81243b34cc35204d665a9276f8ece563e061f8ebbfe","transactional":true,"version":12},{"backfill":{"kind":"none"},"checksum":"abe822c967e1b2cc3966ede988e05eb2d9Line truncated
|
||||
{"schema_version":1,"sequence":[{"backfill":{"kind":"none"},"checksum":"crank-community-baseline-v1","compatibility":"legacy-baseline","contract_evidence":null,"name":"community-baseline-v1","owner":"crank-registry","phase":"expand","readable_schema_max":1,"readable_schema_min":1,"source_digest":"eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675","transactional":true,"version":1},{"backfill":{"kind":"none"},"checksum":"1908b97ca7fe8a85d146b0eebf007d5018a9ff406cb099614270f3428ec1db48","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"legacy-consolidation-v2","owner":"crank-registry","phase":"expand","readable_schema_max":2,"readable_schema_min":1,"source_digest":"1908b97ca7fe8a85d146b0eebf007d5018a9ff406cb099614270f3428ec1db48","transactional":true,"version":2},{"backfill":{"kind":"none"},"checksum":"36487625503a8d4d8f18d5771c3c9b6705f845e267acbfcb7244c330f640cd94","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"request-trace-identity-v3","owner":"crank-registry","phase":"expand","readable_schema_max":3,"readable_schema_min":2,"source_digest":"36487625503a8d4d8f18d5771c3c9b6705f845e267acbfcb7244c330f640cd94","transactional":true,"version":3},{"backfill":{"kind":"none"},"checksum":"45723712a1ea49cd8bbf59d77148c225f3ec376df7433983d982cc6f9d7fb39c","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"operation-lifecycle-v4","owner":"crank-registry","phase":"expand","readable_schema_max":4,"readable_schema_min":3,"source_digest":"45723712a1ea49cd8bbf59d77148c225f3ec376df7433983d982cc6f9d7fb39c","transactional":true,"version":4},{"backfill":{"kind":"none"},"checksum":"bd6703249cc407789586327eb7fbbd5776ba27923c5dc7eb85b05d332585bf42","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"execution-outcome-v5","owner":"crank-registry","phase":"expand","readable_schema_max":5,"readable_schema_min":4,"source_digest":"bd6703249cc407789586327eb7fbbd5776ba27923c5dc7eb85b05d332585bf42","transactional":true,"version":5},{"backfill":{"kind":"none"},"checksum":"94dba9b9dd3364607bc37e7698478ba6644607a7f268a1621cd3acea6bc96c2d","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"platform-key-name-reuse-v6","owner":"crank-registry","phase":"expand","readable_schema_max":6,"readable_schema_min":5,"source_digest":"94dba9b9dd3364607bc37e7698478ba6644607a7f268a1621cd3acea6bc96c2d","transactional":true,"version":6},{"backfill":{"kind":"none"},"checksum":"06e5d4667d9a7346474e9dcb176b8b2b9680c4b89089bea5a18eb5a88d5ac60e","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"master-key-identity-v7","owner":"crank-registry","phase":"expand","readable_schema_max":7,"readable_schema_min":6,"source_digest":"06e5d4667d9a7346474e9dcb176b8b2b9680c4b89089bea5a18eb5a88d5ac60e","transactional":true,"version":7},{"backfill":{"kind":"none"},"checksum":"6361f3321a1442a77c695cad9b30c4702de1aa3e565bfbf1a7415d653302611f","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"admin-auth-lifecycle-v8","owner":"crank-registry","phase":"expand","readable_schema_max":8,"readable_schema_min":7,"source_digest":"6361f3321a1442a77c695cad9b30c4702de1aa3e565bfbf1a7415d653302611f","transactional":true,"version":8},{"backfill":{"kind":"none"},"checksum":"b49bf4b53691407ec27e3810b0531c0d440da7dddc62ea71eb219933c0a30211","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"agent-catalog-lifecycle-v9","owner":"crank-registry","phase":"expand","readable_schema_max":9,"readable_schema_min":8,"source_digest":"b49bf4b53691407ec27e3810b0531c0d440da7dddc62ea71eb219933c0a30211","transactional":true,"version":9},{"backfill":{"kind":"none"},"checksum":"6c87f680efdbc275fa2b920826b3e3e390aa34d6f2f1db48fe074a5d7691ba5b","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"approval-side-effects-v10","owner":"crank-registry","phase":"expand","readable_schema_max":10,"readable_schema_min":9,"source_digest":"6c87f680efdbc275fa2b920826b3e3e390aa34d6f2f1db48fe074a5d7691ba5b","transactional":true,"version":10},{"backfill":{"kind":"none"},"checksum":"a439bbcdc9cc909d717ed5a868c7a51bd1ad3c49c4e85fd20933743ee3f31166","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"onboarding-product-events-v11","owner":"crank-registry","phase":"expand","readable_schema_max":11,"readable_schema_min":10,"source_digest":"a439bbcdc9cc909d717ed5a868c7a51bd1ad3c49c4e85fd20933743ee3f31166","transactional":true,"version":11},{"backfill":{"kind":"none"},"checksum":"052058da53cd861b2a1af81243b34cc35204d665a9276f8ece563e061f8ebbfe","compatibility":"n-minus-one-readable","contract_evidence":null,"name":"artifact-metadata-v12","owner":"crank-registry","phase":"expand","readable_schema_max":12,"readable_schema_min":11,"source_digest":"052058da53cd861b2a1af81243b34cc35204d665a9276f8ece563e061f8ebbfe","transactional":true,"version":12},{"backfill":{"kind":"none"},"checksum":"abe822c967e1b2cc3966ede988e05eb2d9Line truncated
|
||||
@@ -67,3 +67,21 @@ run_atdd "execution policy validation" -p admin-api --lib operation_validation::
|
||||
run_atdd "execution admission store" -p crank-registry --test integration admission:: -- --test-threads=1
|
||||
run_atdd "runtime admission budget" -p mcp-server --test admission_budget -- --test-threads=1
|
||||
run_atdd "admission readiness" -p admin-api --lib routes::tests::readiness_checks -- --test-threads=1
|
||||
|
||||
# Story 4.2 (FR-22, FR-23): durable background Tasks. The lifecycle/envelope bounds are unit-level; the
|
||||
# authoritative store (one-transaction acceptance, replay/typed conflict, shared bounded capacity,
|
||||
# Task-linked intent guard, terminal CAS, retention, scope isolation, fail-closed health) and the
|
||||
# application entry (verified principal, exact publication pin, foreign-scope denial, readiness) run
|
||||
# against a real PostgreSQL schema. The migration suite proves a fresh and an already-migrated v25.
|
||||
run_atdd "durable task domain bounds" -p crank-core --lib task:: -- --test-threads=1
|
||||
run_atdd "runtime task identity" -p crank-runtime --lib tasks:: -- --test-threads=1
|
||||
run_atdd "durable task store" -p crank-registry --test integration tasks:: -- --test-threads=1
|
||||
run_atdd "durable task application entry" -p mcp-server --test integration durable_tasks:: -- --test-threads=1
|
||||
run_atdd "durable task migration" -p crank-registry --test integration migrations:: -- --test-threads=1
|
||||
|
||||
# Story 4.3 (FR-22, FR-23): fenced durable Task execution. The Story 4.2 selectors above already run
|
||||
# the lease/fencing/recovery store, the Task application entry and the migration guards; these two
|
||||
# oracles bound the rest of this story's AC: the process-level shutdown ordering (signal stops new
|
||||
# claims before a slow HTTP drain, not after it) and one child attempt span per adapter retry.
|
||||
run_atdd "task worker process shutdown" -p mcp-server --bin mcp-server -- --test-threads=1
|
||||
run_atdd "durable task retry attempt spans" -p crank-runtime --test stages retry -- --test-threads=1
|
||||
@@ -0,0 +1,201 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
WORKFLOW = ROOT / ".gitea" / "workflows" / "ci.yml"
|
||||
|
||||
|
||||
def workflow_text() -> str:
|
||||
return WORKFLOW.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def job_block(name: str) -> str:
|
||||
lines = workflow_text().splitlines()
|
||||
start = None
|
||||
for index, line in enumerate(lines):
|
||||
if line == f" {name}:":
|
||||
start = index
|
||||
break
|
||||
if start is None:
|
||||
raise AssertionError(f"job {name!r} not found")
|
||||
end = len(lines)
|
||||
for index in range(start + 1, len(lines)):
|
||||
if re.match(r"^ [A-Za-z0-9_-]+:\s*$", lines[index]):
|
||||
end = index
|
||||
break
|
||||
return "\n".join(lines[start:end])
|
||||
|
||||
|
||||
def deploy_guard() -> str:
|
||||
match = re.search(r"(?m)^ if:\s*(.+)$", job_block("deploy"))
|
||||
if match is None:
|
||||
raise AssertionError("deploy job has no guard")
|
||||
return match.group(1).strip()
|
||||
|
||||
|
||||
def guard_conditions() -> dict[str, str]:
|
||||
expression = deploy_guard()
|
||||
if expression.startswith("${{") and expression.endswith("}}"):
|
||||
expression = expression[3:-2].strip()
|
||||
conditions: dict[str, str] = {}
|
||||
for part in expression.split("&&"):
|
||||
match = re.fullmatch(
|
||||
r"([A-Za-z0-9_.]+)\s*==\s*['\"]([^'\"]+)['\"]", part.strip()
|
||||
)
|
||||
if match is None:
|
||||
raise AssertionError(f"unsupported deploy guard condition: {part!r}")
|
||||
conditions[match.group(1)] = match.group(2)
|
||||
return conditions
|
||||
|
||||
|
||||
def deploys(context: dict[str, str | None]) -> bool:
|
||||
return all(
|
||||
context.get(condition) == expected
|
||||
for condition, expected in guard_conditions().items()
|
||||
)
|
||||
|
||||
|
||||
def dispatch_input(name: str) -> str:
|
||||
match = re.search(
|
||||
rf"(?m)^ {re.escape(name)}:\n((?: .*\n)+)", workflow_text()
|
||||
)
|
||||
if match is None:
|
||||
raise AssertionError(f"workflow_dispatch input {name!r} not found")
|
||||
return match.group(0)
|
||||
|
||||
|
||||
class DeployPolicyTests(unittest.TestCase):
|
||||
def test_guard_allows_only_manual_production_feature_on_main(self):
|
||||
self.assertEqual(
|
||||
guard_conditions(),
|
||||
{
|
||||
"gitea.event_name": "workflow_dispatch",
|
||||
"gitea.ref": "refs/heads/main",
|
||||
"inputs.validation_level": "feature",
|
||||
"inputs.deploy_target": "production",
|
||||
},
|
||||
)
|
||||
|
||||
def test_manual_production_feature_on_main_deploys(self):
|
||||
self.assertTrue(
|
||||
deploys(
|
||||
{
|
||||
"gitea.event_name": "workflow_dispatch",
|
||||
"gitea.ref": "refs/heads/main",
|
||||
"inputs.validation_level": "feature",
|
||||
"inputs.deploy_target": "production",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_push_to_main_does_not_deploy(self):
|
||||
self.assertFalse(
|
||||
deploys(
|
||||
{
|
||||
"gitea.event_name": "push",
|
||||
"gitea.ref": "refs/heads/main",
|
||||
"inputs.validation_level": "feature",
|
||||
"inputs.deploy_target": "production",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_pull_request_with_feature_label_does_not_deploy(self):
|
||||
self.assertFalse(
|
||||
deploys(
|
||||
{
|
||||
"gitea.event_name": "pull_request",
|
||||
"gitea.ref": "refs/pull/42/head",
|
||||
"inputs.validation_level": "feature",
|
||||
"inputs.deploy_target": "production",
|
||||
"gitea.event.pull_request.labels.*.name": "feature-ready",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_missing_or_none_deploy_target_does_not_deploy(self):
|
||||
base = {
|
||||
"gitea.event_name": "workflow_dispatch",
|
||||
"gitea.ref": "refs/heads/main",
|
||||
"inputs.validation_level": "feature",
|
||||
}
|
||||
self.assertFalse(deploys(base))
|
||||
self.assertFalse(deploys({**base, "inputs.deploy_target": "none"}))
|
||||
|
||||
def test_story_dispatch_does_not_deploy(self):
|
||||
self.assertFalse(
|
||||
deploys(
|
||||
{
|
||||
"gitea.event_name": "workflow_dispatch",
|
||||
"gitea.ref": "refs/heads/main",
|
||||
"inputs.validation_level": "story",
|
||||
"inputs.deploy_target": "production",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_dispatch_on_other_ref_does_not_deploy(self):
|
||||
self.assertFalse(
|
||||
deploys(
|
||||
{
|
||||
"gitea.event_name": "workflow_dispatch",
|
||||
"gitea.ref": "refs/heads/release",
|
||||
"inputs.validation_level": "feature",
|
||||
"inputs.deploy_target": "production",
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_deploy_target_is_explicit_choice_defaulting_to_none(self):
|
||||
block = dispatch_input("deploy_target")
|
||||
self.assertIn("type: choice", block)
|
||||
self.assertIn("options: [none, production]", block)
|
||||
self.assertIn("default: none", block)
|
||||
self.assertIn("required: true", block)
|
||||
|
||||
def test_push_main_source_ci_stays_enabled(self):
|
||||
self.assertRegex(
|
||||
workflow_text(),
|
||||
r"(?m)^ push:\n branches:\n - main$",
|
||||
)
|
||||
|
||||
def test_deploy_requires_successful_required_checks(self):
|
||||
deploy = job_block("deploy")
|
||||
self.assertRegex(deploy, r"(?ms)^ needs:\n - required-checks$")
|
||||
|
||||
def test_required_checks_gate_feature_jobs(self):
|
||||
block = job_block("required-checks")
|
||||
for job in ("rust-feature", "frontend-e2e", "deployment"):
|
||||
self.assertIn(f" - {job}", block)
|
||||
for variable in ("RUST_FEATURE_RESULT", "E2E_RESULT", "IMAGE_RESULT"):
|
||||
self.assertIn(variable, block)
|
||||
|
||||
def test_only_deploy_loads_production_secrets(self):
|
||||
text = workflow_text()
|
||||
self.assertEqual(text.count("scripts/load-openbao-env.sh"), 1)
|
||||
self.assertIn("scripts/load-openbao-env.sh", job_block("deploy"))
|
||||
self.assertNotIn("openbao", job_block("deployment").lower())
|
||||
|
||||
def test_deploy_keeps_build_scan_backup_health_and_smoke_steps(self):
|
||||
deploy = job_block("deploy")
|
||||
for step in (
|
||||
"Load deployment secrets from OpenBao",
|
||||
"Build and push images",
|
||||
"Save deploy image scan evidence",
|
||||
"Sync deployment files to server",
|
||||
"Write environment file",
|
||||
"Validate required environment variables",
|
||||
"Deploy with Docker Compose",
|
||||
"Verify health endpoints",
|
||||
"Run authenticated product smoke",
|
||||
):
|
||||
self.assertIn(f"name: {step}", deploy)
|
||||
self.assertIn("scripts/scan-images.sh", deploy)
|
||||
self.assertIn("deploy-community.sh", deploy)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user