feat(epic4): публиковать execution mode и применять общий admission (Story 4.1, FR-21, FR-24)
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 12m45s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 40m35s
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 12m45s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 40m35s
Tool Draft фиксирует synchronous/background/adaptive, deadline и fallback; runtime применяет единый снимок политики (concurrency, очередь, approval, deadline) до внешнего действия. Насыщение отклоняется typed overload с retry guidance, mandatory safety store fail closed, background/adaptive используют опубликованный synchronous/unsupported fallback. Refs: #29
This commit is contained in:
1 parent
44774883a0
commit
e80d30ddb0
72 files changed
+4157
-121
No files matched your search
@@ -0,0 +1,496 @@
|
||||
//! ATDD Story 4.1 (FR-21/FR-24): authoritative PostgreSQL execution admission.
|
||||
//!
|
||||
//! These oracles drive the real store against a real PostgreSQL schema: scoped permits, bounded
|
||||
//! queue, typed overload, durable enqueue decisions, deadline propagation, quota keys that ignore
|
||||
//! the operation version, fairness order and fail-closed health.
|
||||
|
||||
use super::common;
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crank_core::{
|
||||
ADMIN_ORIGIN_KEY, AdmissionMode, AdmissionOutcome, AdmissionPermit, AdmissionRequest,
|
||||
AdmissionSchedulerConfig, AdmissionScope, ApprovalEvidence, ExecutionAdmission,
|
||||
NoTasksFallback, ToolExecutionMode, current_unix_ms,
|
||||
};
|
||||
use crank_registry::{AdmissionLimits, PostgresExecutionAdmission};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
fn limits(global: usize, agent: usize, tool: usize, queue: usize) -> AdmissionLimits {
|
||||
AdmissionLimits {
|
||||
global_limit: global,
|
||||
agent_limit: agent,
|
||||
tool_limit: tool,
|
||||
queue_limit: queue,
|
||||
}
|
||||
}
|
||||
|
||||
fn store(
|
||||
pool: sqlx::PgPool,
|
||||
limits: AdmissionLimits,
|
||||
scheduler: AdmissionSchedulerConfig,
|
||||
) -> PostgresExecutionAdmission {
|
||||
PostgresExecutionAdmission::new(pool, limits, scheduler).expect("valid admission store")
|
||||
}
|
||||
|
||||
fn scope(agent: &str, operation: &str, version: u32) -> AdmissionScope {
|
||||
AdmissionScope {
|
||||
workspace_id: "ws_default".to_owned(),
|
||||
agent_key: agent.to_owned(),
|
||||
operation_id: operation.to_owned(),
|
||||
operation_version: version,
|
||||
}
|
||||
}
|
||||
|
||||
fn request(scope: AdmissionScope, deadline_in_ms: i64) -> AdmissionRequest {
|
||||
AdmissionRequest {
|
||||
scope,
|
||||
mode: AdmissionMode::Synchronous,
|
||||
policy_mode: ToolExecutionMode::Synchronous,
|
||||
no_tasks_fallback: NoTasksFallback::Synchronous,
|
||||
approval: ApprovalEvidence::NotRequired,
|
||||
fingerprint: format!("fp_{}", current_unix_ms()),
|
||||
expires_at_unix_ms: current_unix_ms() + deadline_in_ms,
|
||||
}
|
||||
}
|
||||
|
||||
fn background(scope: AdmissionScope, fallback: NoTasksFallback) -> AdmissionRequest {
|
||||
AdmissionRequest {
|
||||
mode: AdmissionMode::Background,
|
||||
no_tasks_fallback: fallback,
|
||||
..request(scope, 30_000)
|
||||
}
|
||||
}
|
||||
|
||||
fn permit(outcome: AdmissionOutcome) -> AdmissionPermit {
|
||||
match outcome {
|
||||
AdmissionOutcome::Permitted(permit) => permit,
|
||||
other => panic!("expected a scoped permit, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_metric_poll() {
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn permits_are_scoped_released_once_and_respect_the_agent_budget() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(4, 1, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let first = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 2_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
assert_eq!(first.scope.agent_key, "agent_a");
|
||||
|
||||
// The same Agent is already at its scoped budget: the second request waits and expires instead of
|
||||
// over-admitting, and it never dispatches.
|
||||
let waiting = admission
|
||||
.admit(request(scope("agent_a", "op_leads", 2), 120))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
assert_eq!(waiting, AdmissionOutcome::DeadlineElapsed);
|
||||
|
||||
// Another Agent still has the global capacity it is entitled to.
|
||||
let other = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_b", "op_leads", 1), 2_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
assert_eq!(other.scope.agent_key, "agent_b");
|
||||
assert!(admission.release(&other).await.unwrap());
|
||||
// Repeated release is idempotent and never frees a foreign or already released permit.
|
||||
assert!(!admission.release(&other).await.unwrap());
|
||||
|
||||
assert!(admission.release(&first).await.unwrap());
|
||||
let snapshot = admission.snapshot().await.unwrap();
|
||||
assert_eq!(snapshot.executing, 0);
|
||||
assert_eq!(snapshot.waiting, 0);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn full_queue_returns_a_typed_overload_with_retry_guidance() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(1, 1, 1, 2),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let held = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 5_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
|
||||
// Two bounded waiting entries fill the queue.
|
||||
let mut waiters = Vec::new();
|
||||
for version in 1..=2 {
|
||||
let admission = admission.clone();
|
||||
waiters.push(tokio::spawn(async move {
|
||||
admission
|
||||
.admit(request(scope("agent_b", "op_leads", version), 5_000))
|
||||
.await
|
||||
}));
|
||||
}
|
||||
while admission.snapshot().await.unwrap().waiting < 2 {
|
||||
wait_for_metric_poll().await;
|
||||
}
|
||||
|
||||
let rejected = admission
|
||||
.admit(request(scope("agent_c", "op_leads", 1), 5_000))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
match rejected {
|
||||
AdmissionOutcome::Overloaded { retry_after_ms } => {
|
||||
assert!((1..=crank_core::MAX_RETRY_AFTER_MS).contains(&retry_after_ms));
|
||||
}
|
||||
other => panic!("expected typed overload, got {other:?}"),
|
||||
}
|
||||
|
||||
// Cancelling the held permit lets the queued waiters make progress instead of leaking capacity.
|
||||
assert!(admission.release(&held).await.unwrap());
|
||||
for waiter in waiters.drain(..) {
|
||||
let outcome = waiter.await.expect("waiter task");
|
||||
if let Ok(AdmissionOutcome::Permitted(permit)) = outcome {
|
||||
let _ = admission.release(&permit).await;
|
||||
}
|
||||
}
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn background_decisions_are_durable_pinned_and_released_once() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(4, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let decision = match admission
|
||||
.admit(background(
|
||||
scope("agent_a", "op_report", 3),
|
||||
NoTasksFallback::Synchronous,
|
||||
))
|
||||
.await
|
||||
.expect("admission store is reachable")
|
||||
{
|
||||
AdmissionOutcome::Enqueued(decision) => decision,
|
||||
other => panic!("expected a durable enqueue decision, got {other:?}"),
|
||||
};
|
||||
assert_eq!(decision.scope.operation_version, 3);
|
||||
assert_eq!(decision.policy_mode, ToolExecutionMode::Synchronous);
|
||||
|
||||
// A second process (fresh pool and store over the same schema) must read the committed decision:
|
||||
// it occupies bounded queue capacity and is not a runnable execution.
|
||||
let reconnected = store(
|
||||
database.concurrent_registry(2).await.pool().clone(),
|
||||
limits(4, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
let snapshot = reconnected.snapshot().await.unwrap();
|
||||
assert_eq!(snapshot.waiting, 1);
|
||||
assert_eq!(snapshot.executing, 0);
|
||||
|
||||
assert!(
|
||||
reconnected
|
||||
.cancel(decision.ticket_id, "not_the_owner")
|
||||
.await
|
||||
.unwrap()
|
||||
.eq(&false),
|
||||
"a foreign cancel must never release the decision"
|
||||
);
|
||||
assert!(
|
||||
reconnected.snapshot().await.unwrap().waiting.eq(&1),
|
||||
"the foreign cancel must leave the decision intact"
|
||||
);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn background_without_a_task_transport_is_typed_unsupported() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(4, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let outcome = admission
|
||||
.admit(background(
|
||||
scope("agent_a", "op_report", 1),
|
||||
NoTasksFallback::Unsupported,
|
||||
))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
assert_eq!(outcome, AdmissionOutcome::Unsupported);
|
||||
assert_eq!(admission.snapshot().await.unwrap().waiting, 0);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unverified_approval_never_produces_a_decision() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(4, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let mut denied = background(
|
||||
scope("agent_a", "op_report", 1),
|
||||
NoTasksFallback::Synchronous,
|
||||
);
|
||||
denied.approval = ApprovalEvidence::NotGranted;
|
||||
assert_eq!(
|
||||
admission.admit(denied).await.unwrap(),
|
||||
AdmissionOutcome::ApprovalRequired
|
||||
);
|
||||
|
||||
let mut denied_sync = request(scope("agent_a", "op_report", 1), 1_000);
|
||||
denied_sync.approval = ApprovalEvidence::NotGranted;
|
||||
assert_eq!(
|
||||
admission.admit(denied_sync).await.unwrap(),
|
||||
AdmissionOutcome::ApprovalRequired
|
||||
);
|
||||
assert_eq!(admission.snapshot().await.unwrap().waiting, 0);
|
||||
assert_eq!(admission.snapshot().await.unwrap().executing, 0);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn quota_keys_ignore_the_operation_version() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(4, 4, 1, 8),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let held = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 2_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
// A different Published Version of the same Tool shares the same per-Tool budget.
|
||||
let outcome = admission
|
||||
.admit(request(scope("agent_a", "op_leads", 2), 120))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
assert_eq!(outcome, AdmissionOutcome::DeadlineElapsed);
|
||||
assert!(admission.release(&held).await.unwrap());
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn weighted_agents_are_served_their_quantum_in_order() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(1, 4, 4, 8),
|
||||
AdmissionSchedulerConfig {
|
||||
agent_weight: 2,
|
||||
tool_weight: 1,
|
||||
},
|
||||
);
|
||||
let held = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_z", "op_leads", 1), 5_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
|
||||
let (sender, mut receiver) = mpsc::channel(3);
|
||||
let mut tasks = Vec::new();
|
||||
for (agent, version) in [("agent_a", 1), ("agent_a", 2), ("agent_b", 1)] {
|
||||
let admission = admission.clone();
|
||||
let sender = sender.clone();
|
||||
tasks.push(tokio::spawn(async move {
|
||||
let outcome = admission
|
||||
.admit(request(scope(agent, "op_leads", version), 5_000))
|
||||
.await;
|
||||
if let Ok(AdmissionOutcome::Permitted(permit)) = outcome {
|
||||
let _ = sender.send(permit.scope.agent_key.clone()).await;
|
||||
// Hold briefly so the grant order, not task scheduling, decides the sequence.
|
||||
tokio::time::sleep(Duration::from_millis(30)).await;
|
||||
let _ = admission.release(&permit).await;
|
||||
}
|
||||
}));
|
||||
}
|
||||
drop(sender);
|
||||
assert!(admission.release(&held).await.unwrap());
|
||||
|
||||
let mut order = Vec::new();
|
||||
while let Some(agent) = receiver.recv().await {
|
||||
order.push(agent);
|
||||
if order.len() == 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for task in tasks {
|
||||
let _ = tokio::time::timeout(Duration::from_secs(5), task).await;
|
||||
}
|
||||
// agent_a carries weight 2 and therefore receives two consecutive grants before agent_b.
|
||||
assert_eq!(order, vec!["agent_a", "agent_a", "agent_b"]);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn mandatory_state_failure_fails_closed_and_reports_not_ready() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
// No migration on this schema: the mandatory admission state does not exist yet.
|
||||
let unmigrated = database.raw_pool().await;
|
||||
let admission = store(unmigrated, limits(4, 4, 4, 8), Default::default());
|
||||
assert!(admission.health().await.is_err());
|
||||
assert!(
|
||||
admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 1_000))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn held_permits_survive_a_new_process_view() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(1, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
let held = permit(
|
||||
admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 2_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
|
||||
let reconnected = store(
|
||||
database.concurrent_registry(2).await.pool().clone(),
|
||||
limits(1, 4, 4, 8),
|
||||
Default::default(),
|
||||
);
|
||||
assert_eq!(reconnected.snapshot().await.unwrap().executing, 1);
|
||||
let outcome = reconnected
|
||||
.admit(request(scope("agent_b", "op_leads", 1), 120))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
assert_eq!(
|
||||
outcome,
|
||||
AdmissionOutcome::DeadlineElapsed,
|
||||
"a held permit is not silently reused by another process"
|
||||
);
|
||||
assert!(reconnected.release(&held).await.unwrap());
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn published_execution_policy_is_pinned_across_a_new_draft_version() {
|
||||
use crank_core::{NoTasksFallback, OperationStatus, ToolExecutionMode, ToolExecutionPolicy};
|
||||
use crank_registry::{CreateVersionRequest, PublishRequest};
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let workspace = common::test_workspace_id();
|
||||
let policy = ToolExecutionPolicy {
|
||||
mode: ToolExecutionMode::Background,
|
||||
deadline_ms: 45_000,
|
||||
no_tasks_fallback: NoTasksFallback::Unsupported,
|
||||
};
|
||||
let mut version_one = common::test_operation("op_policy", 1, OperationStatus::Draft);
|
||||
version_one.execution_config.execution_policy = Some(policy);
|
||||
registry
|
||||
.create_operation(&workspace, &version_one, None)
|
||||
.await
|
||||
.unwrap();
|
||||
registry
|
||||
.publish_operation(PublishRequest {
|
||||
workspace_id: &workspace,
|
||||
operation_id: &version_one.id,
|
||||
version: 1,
|
||||
published_at: &OffsetDateTime::parse("2026-03-25T12:10:00Z", &Rfc3339).unwrap(),
|
||||
published_by: Some("alice"),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = registry
|
||||
.get_published_operation(&version_one.id)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("published operation");
|
||||
assert_eq!(published.execution_config.execution_policy, Some(policy));
|
||||
|
||||
// A later Draft with another policy never rewrites the already accepted publication.
|
||||
let mut version_two = common::test_operation("op_policy", 2, OperationStatus::Draft);
|
||||
version_two.execution_config.execution_policy = Some(ToolExecutionPolicy {
|
||||
mode: ToolExecutionMode::Synchronous,
|
||||
deadline_ms: 1_000,
|
||||
no_tasks_fallback: NoTasksFallback::Synchronous,
|
||||
});
|
||||
registry
|
||||
.create_version(CreateVersionRequest {
|
||||
workspace_id: &workspace,
|
||||
snapshot: &version_two,
|
||||
change_note: Some("next draft"),
|
||||
created_by: Some("alice"),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
let unchanged = registry
|
||||
.get_published_operation(&version_one.id)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("published operation");
|
||||
assert_eq!(unchanged.version, 1);
|
||||
assert_eq!(unchanged.execution_config.execution_policy, Some(policy));
|
||||
database.cleanup().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_origin_bucket_is_bounded_together_with_agents() {
|
||||
let database = common::TestDatabase::new().await;
|
||||
let registry = database.registry().await;
|
||||
let admission = store(
|
||||
registry.pool().clone(),
|
||||
limits(1, 1, 1, 4),
|
||||
Default::default(),
|
||||
);
|
||||
let held = permit(
|
||||
admission
|
||||
.admit(request(scope(ADMIN_ORIGIN_KEY, "op_leads", 1), 2_000))
|
||||
.await
|
||||
.expect("admission store is reachable"),
|
||||
);
|
||||
let started = Instant::now();
|
||||
let outcome = admission
|
||||
.admit(request(scope("agent_a", "op_leads", 1), 120))
|
||||
.await
|
||||
.expect("admission store is reachable");
|
||||
assert_eq!(outcome, AdmissionOutcome::DeadlineElapsed);
|
||||
assert!(started.elapsed() >= Duration::from_millis(100));
|
||||
assert!(admission.release(&held).await.unwrap());
|
||||
database.cleanup().await;
|
||||
}
|
||||
Reference in new issue
Block a user