feat(epic4): подтверждать durable Task после атомарного PG-commit (Story 4.2)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 19m34s
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
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 19m34s
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
Application entry выдаёт Task ID только после атомарного PostgreSQL commit Task+intent+capacity; replay/conflict, owner scope, bounded envelope, terminal CAS и fail-closed readiness проверены на реальном PG. Также включён принятый план перехода на rmcp (только документ, миграция не запускается).
This commit is contained in:
1 parent
cc612516a2
commit
2219a7217d
37 files changed
+4028
-136
No files matched your search
@@ -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,13 @@ 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_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 +118,7 @@ pub mod ports {
|
||||
PreparedRequest, ProtocolAdapter, ProtocolAdapterError, ResponseCacheScope,
|
||||
RuntimeRequestContext, SharedProtocolAdapter,
|
||||
};
|
||||
pub use crate::task::TaskStore;
|
||||
}
|
||||
|
||||
pub use access::{
|
||||
@@ -192,6 +201,13 @@ 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_MAX_RETENTION_MS, TASK_PURGE_BATCH_LIMIT,
|
||||
TASK_SCOPED_KEY_MAX_BYTES, TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskEnvelope,
|
||||
TaskError, TaskFinishRequest, TaskId, TaskOwnerKind, TaskPins, TaskPurgeReport, TaskRecord,
|
||||
TaskState, TaskStore, TaskSupportIds, 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,730 @@
|
||||
//! 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::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 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(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
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,
|
||||
}
|
||||
|
||||
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 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()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
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 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)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>;
|
||||
|
||||
/// Bounded retention: fails guaranteed-undispatched queued Tasks past their deadline and removes
|
||||
/// expired terminal records. Live and non-terminal work is never deleted.
|
||||
async fn purge_expired(&self, batch: u32) -> Result<TaskPurgeReport, 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(),
|
||||
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(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user