feat: model operation outcome guarantees

This commit is contained in:
2026-09-30 05:52:38 +09:00
parent 027555eb4e
commit 6de58da17d
8 changed files with 1105 additions and 150 deletions
+54 -3
View File
@@ -1,7 +1,10 @@
use std::collections::BTreeMap;
use wip_interface::wip_protocol::{INTERFACE_FORMAT_V1, TypeExpr, Value};
use wip_interface::{DispatchError, HostError, OperationResult, WipType, interface};
use wip_interface::{
DispatchError, DispatchPhase, EffectGuarantee, HostError, HostErrorKind, OperationResult,
WipType, interface,
};
/// Publication request.
#[derive(Debug, Clone, PartialEq, WipType)]
@@ -66,7 +69,10 @@ impl Catalog for AcceptingCatalog {
}
fn host_value(&self) -> OperationResult<String> {
Err(HostError::new("backend unavailable"))
Err(HostError::internal(
"backend unavailable",
EffectGuarantee::Unknown,
))
}
}
@@ -90,6 +96,26 @@ impl Catalog for RejectingCatalog {
}
}
struct PanickingCatalog;
impl Catalog for PanickingCatalog {
fn publish(
&self,
_request: PublishRequest,
_note: Option<String>,
) -> OperationResult<PublishOutcome> {
panic!("private panic payload")
}
fn ping(&self) -> OperationResult<()> {
panic!("private panic payload")
}
fn host_value(&self) -> OperationResult<String> {
panic!("private panic payload")
}
}
fn publish_arguments(with_note: bool) -> BTreeMap<String, Value> {
let mut arguments = BTreeMap::from([(
"request".into(),
@@ -203,16 +229,41 @@ fn typed_dispatch_validates_decodes_invokes_encodes_and_validates() {
let invalid =
CatalogDefinition::dispatch(&AcceptingCatalog, "publish", BTreeMap::new()).unwrap_err();
assert!(matches!(invalid, DispatchError::InvalidArguments(_)));
assert_eq!(invalid.phase(), DispatchPhase::BeforeInvocation);
assert_eq!(invalid.effect_guarantee(), None);
let host =
CatalogDefinition::dispatch(&AcceptingCatalog, "host_value", BTreeMap::new()).unwrap_err();
assert!(matches!(host, DispatchError::Host(error) if error.message() == "backend unavailable"));
assert!(
matches!(&host, DispatchError::Host(error) if error.message() == "backend unavailable")
);
assert_eq!(host.phase(), DispatchPhase::AfterInvocation);
assert_eq!(host.effect_guarantee(), Some(EffectGuarantee::Unknown));
let panic =
CatalogDefinition::dispatch(&PanickingCatalog, "host_value", BTreeMap::new()).unwrap_err();
assert!(matches!(panic, DispatchError::Panic));
assert_eq!(panic.phase(), DispatchPhase::AfterInvocation);
assert_eq!(panic.effect_guarantee(), Some(EffectGuarantee::Unknown));
assert!(!panic.to_string().contains("private panic payload"));
let unknown =
CatalogDefinition::dispatch(&AcceptingCatalog, "missing", BTreeMap::new()).unwrap_err();
assert!(matches!(unknown, DispatchError::UnknownOperation(name) if name == "missing"));
}
#[test]
fn host_errors_require_a_typed_kind_and_effect_guarantee() {
let internal = HostError::internal("x".repeat(4_096), EffectGuarantee::NotCommitted);
assert_eq!(internal.kind(), HostErrorKind::Internal);
assert_eq!(internal.effect_guarantee(), EffectGuarantee::NotCommitted);
assert!(internal.message().len() <= 512);
let resource = HostError::resource_limit("deadline", EffectGuarantee::Unknown);
assert_eq!(resource.kind(), HostErrorKind::ResourceLimitExceeded);
assert_eq!(resource.effect_guarantee(), EffectGuarantee::Unknown);
}
/// Operations whose distinct source names normalize to the same PascalCase.
#[interface]
pub trait DistinctOperations {