connector-contract-v3: delete the Responses fields and Refusal::Policy; type the request, outcome and refusals
ProviderRequest loses reasoning_summary, service_tier, output_format_name, previous_response_id and prompt_cache_key; reasoning_effort is a ReasoningEffort, max_output_tokens is required and cache_retention is added. Completed carries a StopReason and Usage, Failed a FailureKind, and the receipt names its provider. Refusal::Policy becomes ModelNotAllowed, OutputBound and PayloadBound, each its own branch in begin. cultcache-rs moves to the one CultLib pin a4eae90. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
685f449ddb
commit
5a4d8bd477
7 changed files with 543 additions and 104 deletions
|
|
@ -154,7 +154,7 @@ mod fixture_tests {
|
|||
let envelope: TransportEnvelope = rmp_serde::from_slice(&request).unwrap();
|
||||
assert_eq!(decrypt_invocation(&envelope, &security).unwrap(), expected);
|
||||
let response = encrypt_result(
|
||||
&TransportResult::refused(&expected, Refusal::Policy),
|
||||
&TransportResult::refused(&expected, Refusal::ModelNotAllowed),
|
||||
&security,
|
||||
)
|
||||
.unwrap();
|
||||
|
|
@ -175,7 +175,7 @@ mod fixture_tests {
|
|||
.unwrap();
|
||||
assert_eq!(
|
||||
client.execute(&invocation).unwrap().disposition,
|
||||
TransportDisposition::Refused(Refusal::Policy)
|
||||
TransportDisposition::Refused(Refusal::ModelNotAllowed)
|
||||
);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,17 +39,13 @@ pub struct ProviderRequest {
|
|||
pub model: String,
|
||||
pub instructions: String,
|
||||
pub input: Vec<InputItem>,
|
||||
pub reasoning_effort: Option<String>,
|
||||
pub reasoning_summary: Option<String>,
|
||||
pub service_tier: Option<String>,
|
||||
pub output_format_name: Option<String>,
|
||||
pub previous_response_id: Option<String>,
|
||||
pub reasoning_effort: Option<ReasoningEffort>,
|
||||
pub cache_retention: Option<CacheRetention>,
|
||||
pub tools: Vec<ToolDefinition>,
|
||||
pub tool_choice: ToolChoice,
|
||||
pub parallel_tool_calls: bool,
|
||||
pub output_schema_json: Option<String>,
|
||||
pub max_output_tokens: Option<u32>,
|
||||
pub prompt_cache_key: Option<String>,
|
||||
pub max_output_tokens: u32,
|
||||
}
|
||||
|
||||
impl ProviderRequest {
|
||||
|
|
@ -58,6 +54,7 @@ impl ProviderRequest {
|
|||
conversation_id: impl Into<String>,
|
||||
model: impl Into<String>,
|
||||
instructions: impl Into<String>,
|
||||
max_output_tokens: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
schema_id: PROVIDER_REQUEST_SCHEMA_ID.to_string(),
|
||||
|
|
@ -67,16 +64,12 @@ impl ProviderRequest {
|
|||
instructions: instructions.into(),
|
||||
input: Vec::new(),
|
||||
reasoning_effort: None,
|
||||
reasoning_summary: None,
|
||||
service_tier: None,
|
||||
output_format_name: None,
|
||||
previous_response_id: None,
|
||||
cache_retention: None,
|
||||
tools: Vec::new(),
|
||||
tool_choice: ToolChoice::Auto,
|
||||
parallel_tool_calls: false,
|
||||
output_schema_json: None,
|
||||
max_output_tokens: None,
|
||||
prompt_cache_key: None,
|
||||
max_output_tokens,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,38 +81,12 @@ impl ProviderRequest {
|
|||
require_id(&self.conversation_id, "conversation_id")?;
|
||||
require_id(&self.model, "model")?;
|
||||
require_content(&self.instructions, "instructions")?;
|
||||
if matches!(self.max_output_tokens, Some(0)) {
|
||||
if self.max_output_tokens == 0 {
|
||||
return Err(ContractError::Invalid("max_output_tokens"));
|
||||
}
|
||||
if self
|
||||
.prompt_cache_key
|
||||
.as_deref()
|
||||
.is_some_and(|value| value.trim().is_empty())
|
||||
{
|
||||
return Err(ContractError::Invalid("prompt_cache_key"));
|
||||
}
|
||||
for (value, field) in [
|
||||
(self.reasoning_effort.as_deref(), "reasoning_effort"),
|
||||
(self.reasoning_summary.as_deref(), "reasoning_summary"),
|
||||
(self.service_tier.as_deref(), "service_tier"),
|
||||
(self.output_format_name.as_deref(), "output_format_name"),
|
||||
(self.previous_response_id.as_deref(), "previous_response_id"),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
require_id(value, field)?;
|
||||
}
|
||||
}
|
||||
if let Some(schema) = &self.output_schema_json {
|
||||
require_content(schema, "output_schema_json")?;
|
||||
require_json_object(schema, "output_schema_json")?;
|
||||
require_provider_name(
|
||||
self.output_format_name
|
||||
.as_deref()
|
||||
.ok_or(ContractError::Invalid("output_format_name"))?,
|
||||
"output_format_name",
|
||||
)?;
|
||||
} else if self.output_format_name.is_some() {
|
||||
return Err(ContractError::Invalid("output_schema_json"));
|
||||
}
|
||||
|
||||
if self.tools.is_empty() && self.tool_choice == ToolChoice::Required {
|
||||
|
|
@ -167,6 +134,60 @@ pub enum ToolChoice {
|
|||
Required,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ReasoningEffort {
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
Max,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum CacheRetention {
|
||||
Short,
|
||||
Long,
|
||||
}
|
||||
|
||||
/// Why a provider stopped producing output.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum StopReason {
|
||||
EndTurn,
|
||||
ToolUse,
|
||||
MaxOutputTokens,
|
||||
StopSequence,
|
||||
Refusal,
|
||||
ContextWindow,
|
||||
}
|
||||
|
||||
/// What kind of failure a provider reported. `ResultSize` is the connector's own
|
||||
/// stand-in for a result whose sealed frame cannot be carried.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum FailureKind {
|
||||
ResultSize,
|
||||
InvalidRequest,
|
||||
Authentication,
|
||||
RateLimited,
|
||||
Unavailable,
|
||||
Timeout,
|
||||
Provider,
|
||||
}
|
||||
|
||||
/// Token accounting for one completed request. Cache reads and writes are
|
||||
/// counted apart from plain input; `reasoning_tokens` is absent when the
|
||||
/// provider does not report it.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Usage {
|
||||
pub input_tokens: u64,
|
||||
pub cache_read_tokens: u64,
|
||||
pub cache_write_tokens: u64,
|
||||
pub output_tokens: u64,
|
||||
pub reasoning_tokens: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum InputItem {
|
||||
UserText {
|
||||
|
|
@ -355,7 +376,7 @@ pub struct TransportReceipt {
|
|||
pub native_request_sha256: [u8; 32],
|
||||
pub provider_request_sha256: [u8; 32],
|
||||
pub model: String,
|
||||
pub transport: String,
|
||||
pub provider: String,
|
||||
pub outcome: TransportOutcome,
|
||||
}
|
||||
|
||||
|
|
@ -376,17 +397,13 @@ impl TransportReceipt {
|
|||
if self.native_request_sha256 != invocation.native_request_sha256 {
|
||||
return Err(ContractError::NativeDigest);
|
||||
}
|
||||
require_id(&self.transport, "receipt.transport")?;
|
||||
require_id(&self.provider, "receipt.provider")?;
|
||||
match &self.outcome {
|
||||
TransportOutcome::Completed {
|
||||
provider_response_id: Some(value),
|
||||
..
|
||||
} => require_id(value, "receipt.provider_response_id")?,
|
||||
TransportOutcome::Failed {
|
||||
failure_kind,
|
||||
message,
|
||||
} => {
|
||||
require_id(failure_kind, "receipt.failure_kind")?;
|
||||
TransportOutcome::Failed { message, .. } => {
|
||||
require_content(message, "receipt.failure_message")?;
|
||||
}
|
||||
TransportOutcome::Completed {
|
||||
|
|
@ -402,13 +419,11 @@ impl TransportReceipt {
|
|||
pub enum TransportOutcome {
|
||||
Completed {
|
||||
provider_response_id: Option<String>,
|
||||
input_tokens: Option<u64>,
|
||||
output_tokens: Option<u64>,
|
||||
reasoning_output_tokens: Option<u64>,
|
||||
cached_input_tokens: Option<u64>,
|
||||
stop_reason: StopReason,
|
||||
usage: Usage,
|
||||
},
|
||||
Failed {
|
||||
failure_kind: String,
|
||||
kind: FailureKind,
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
|
@ -418,7 +433,9 @@ pub enum Refusal {
|
|||
Expired,
|
||||
IdentitySubstitution,
|
||||
ProviderDigestSubstitution,
|
||||
Policy,
|
||||
ModelNotAllowed,
|
||||
OutputBound,
|
||||
PayloadBound,
|
||||
Capacity,
|
||||
InFlight,
|
||||
Indeterminate,
|
||||
|
|
@ -537,14 +554,14 @@ mod tests {
|
|||
#[test]
|
||||
fn provider_contract_refuses_transport_side_fixup_pressure() {
|
||||
let mut request = request();
|
||||
request.output_format_name = Some("epiphany.role.result".to_string());
|
||||
request.output_schema_json = Some(r#"{"type":"object"}"#.to_string());
|
||||
assert_eq!(request.validate(), Ok(()));
|
||||
request.output_schema_json = Some("[]".to_string());
|
||||
assert_eq!(
|
||||
request.validate(),
|
||||
Err(ContractError::Invalid("output_format_name"))
|
||||
Err(ContractError::Invalid("output_schema_json"))
|
||||
);
|
||||
|
||||
request.output_format_name = None;
|
||||
request.output_schema_json = None;
|
||||
request.input.push(InputItem::ToolResult {
|
||||
call_id: "x".repeat(65),
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ pub(crate) fn request() -> ProviderRequest {
|
|||
"conversation-1",
|
||||
"gpt-5.4",
|
||||
"Return the typed result.",
|
||||
4_096,
|
||||
);
|
||||
request.input.push(InputItem::UserText {
|
||||
text: "Projected state".to_string(),
|
||||
|
|
@ -67,13 +68,17 @@ pub(crate) fn receipt(invocation: &TransportInvocation) -> TransportReceipt {
|
|||
native_request_sha256: invocation.native_request_sha256,
|
||||
provider_request_sha256: invocation.provider_request_sha256,
|
||||
model: invocation.request.model.clone(),
|
||||
transport: "fixture-sse".to_string(),
|
||||
provider: "fixture-sse".to_string(),
|
||||
outcome: TransportOutcome::Completed {
|
||||
provider_response_id: Some("response-1".to_string()),
|
||||
input_tokens: Some(10),
|
||||
output_tokens: Some(3),
|
||||
reasoning_output_tokens: Some(1),
|
||||
cached_input_tokens: None,
|
||||
stop_reason: StopReason::EndTurn,
|
||||
usage: Usage {
|
||||
input_tokens: 10,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
output_tokens: 3,
|
||||
reasoning_tokens: Some(1),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -328,8 +328,8 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::client::ConnectorClient;
|
||||
use crate::contract::{
|
||||
Refusal, TransportDisposition, TransportEvent, TransportEventPayload, TransportOutcome,
|
||||
TransportResult,
|
||||
FailureKind, Refusal, TransportDisposition, TransportEvent, TransportEventPayload,
|
||||
TransportOutcome, TransportResult,
|
||||
};
|
||||
use crate::fixtures::{TestStore, caller, invocation_for, receipt, request};
|
||||
|
||||
|
|
@ -906,7 +906,7 @@ mod tests {
|
|||
let reply = client.execute(&invocation).unwrap();
|
||||
assert_eq!(
|
||||
reply.disposition,
|
||||
TransportDisposition::Refused(Refusal::Policy)
|
||||
TransportDisposition::Refused(Refusal::PayloadBound)
|
||||
);
|
||||
assert_eq!(backend.executions.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
|
@ -949,7 +949,7 @@ mod tests {
|
|||
transport: &str,
|
||||
) -> TransportResult {
|
||||
let mut receipt = receipt(invocation);
|
||||
receipt.transport = transport.to_string();
|
||||
receipt.provider = transport.to_string();
|
||||
TransportResult::transported(
|
||||
invocation,
|
||||
vec![TransportEvent {
|
||||
|
|
@ -984,9 +984,9 @@ mod tests {
|
|||
&result.disposition,
|
||||
TransportDisposition::Transported { events, receipt }
|
||||
if events.is_empty()
|
||||
&& receipt.transport == "model-connector"
|
||||
&& receipt.provider == "model-connector"
|
||||
&& receipt.outcome == TransportOutcome::Failed {
|
||||
failure_kind: "result_size".to_string(),
|
||||
kind: FailureKind::ResultSize,
|
||||
message: "provider result exceeded its byte bound".to_string(),
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use sha2::{Digest, Sha256};
|
|||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::contract::{
|
||||
ContractError, RECEIPT_SCHEMA_ID, RESULT_SCHEMA_ID, Refusal, TransportDisposition,
|
||||
ContractError, FailureKind, RECEIPT_SCHEMA_ID, RESULT_SCHEMA_ID, Refusal, TransportDisposition,
|
||||
TransportInvocation, TransportKey, TransportOutcome, TransportReceipt, TransportResult,
|
||||
canonical_invocation_bytes, require_id,
|
||||
};
|
||||
|
|
@ -228,18 +228,16 @@ impl TransportService {
|
|||
};
|
||||
}
|
||||
if envelope.ciphertext.len() > caller.max_payload_bytes {
|
||||
return self.reply_refusal(&invocation, Refusal::Policy, &security);
|
||||
return self.reply_refusal(&invocation, Refusal::PayloadBound, &security);
|
||||
}
|
||||
if let Err(error) = invocation.validate(now_unix_ms, self.max_expiry_skew_ms) {
|
||||
return self.reply_refusal(&invocation, contract_refusal(&error), &security);
|
||||
}
|
||||
if !caller.allowed_models.contains(&invocation.request.model)
|
||||
|| invocation
|
||||
.request
|
||||
.max_output_tokens
|
||||
.is_some_and(|limit| limit > caller.max_output_tokens)
|
||||
{
|
||||
return self.reply_refusal(&invocation, Refusal::Policy, &security);
|
||||
if !caller.allowed_models.contains(&invocation.request.model) {
|
||||
return self.reply_refusal(&invocation, Refusal::ModelNotAllowed, &security);
|
||||
}
|
||||
if invocation.request.max_output_tokens > caller.max_output_tokens {
|
||||
return self.reply_refusal(&invocation, Refusal::OutputBound, &security);
|
||||
}
|
||||
|
||||
let identity = RequestIdentity {
|
||||
|
|
@ -428,8 +426,8 @@ fn fits_frame(envelope: &TransportEnvelope, max_frame_bytes: usize) -> Result<bo
|
|||
.map_err(|_| ServiceError::Encoding)
|
||||
}
|
||||
|
||||
/// The transport named on the stand-in failure, fixed so its size is bounded.
|
||||
const STAND_IN_TRANSPORT: &str = "model-connector";
|
||||
/// The provider named on the stand-in failure, fixed so its size is bounded.
|
||||
const STAND_IN_PROVIDER: &str = "model-connector";
|
||||
|
||||
/// The failed result that stands in for one whose sealed frame cannot be
|
||||
/// carried: the same shape a provider failure takes, with nothing a provider
|
||||
|
|
@ -454,9 +452,9 @@ pub(crate) fn result_size_failure(
|
|||
native_request_sha256,
|
||||
provider_request_sha256,
|
||||
model: model.to_string(),
|
||||
transport: STAND_IN_TRANSPORT.to_string(),
|
||||
provider: STAND_IN_PROVIDER.to_string(),
|
||||
outcome: TransportOutcome::Failed {
|
||||
failure_kind: "result_size".to_string(),
|
||||
kind: FailureKind::ResultSize,
|
||||
message: "provider result exceeded its byte bound".to_string(),
|
||||
},
|
||||
}),
|
||||
|
|
@ -775,25 +773,26 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn policy_refuses_unlisted_models_and_output_tokens_over_the_caller_cap() {
|
||||
fn an_unlisted_model_is_refused_model_not_allowed() {
|
||||
let store = TestStore::new();
|
||||
let mut service = open_service(&store, 8);
|
||||
|
||||
let (admission, invocation) = run(&mut service, "unlisted-model", |request| {
|
||||
request.model = "other-model".to_string();
|
||||
});
|
||||
assert_eq!(refusal_of(admission, &invocation), Refusal::Policy);
|
||||
assert_eq!(refusal_of(admission, &invocation), Refusal::ModelNotAllowed);
|
||||
let (admission, _) = run(&mut service, "listed-model", |_| {});
|
||||
assert!(matches!(admission, TransportAdmission::Execute(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_tokens_over_the_caller_cap_are_refused_output_bound_inclusively() {
|
||||
let store = TestStore::new();
|
||||
let mut service = open_service(&store, 8);
|
||||
let (admission, invocation) = run(&mut service, "over-cap", |request| {
|
||||
request.max_output_tokens = Some(32_769);
|
||||
request.max_output_tokens = 32_769;
|
||||
});
|
||||
assert_eq!(refusal_of(admission, &invocation), Refusal::Policy);
|
||||
|
||||
for (id, tokens) in [
|
||||
("at-cap", Some(32_768)),
|
||||
("under-cap", Some(100)),
|
||||
("unset", None),
|
||||
] {
|
||||
assert_eq!(refusal_of(admission, &invocation), Refusal::OutputBound);
|
||||
for (id, tokens) in [("at-cap", 32_768), ("under-cap", 100)] {
|
||||
let (admission, _) = run(&mut service, id, |request| {
|
||||
request.max_output_tokens = tokens;
|
||||
});
|
||||
|
|
@ -824,7 +823,7 @@ mod tests {
|
|||
};
|
||||
assert!(matches!(admit(size), Ok(TransportAdmission::Execute(_))));
|
||||
let refused = admit(size - 1).unwrap();
|
||||
assert_eq!(refusal_of(refused, &invocation), Refusal::Policy);
|
||||
assert_eq!(refusal_of(refused, &invocation), Refusal::PayloadBound);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1074,7 +1073,7 @@ mod tests {
|
|||
transport: &str,
|
||||
) -> TransportResult {
|
||||
let mut receipt = receipt(invocation);
|
||||
receipt.transport = transport.to_string();
|
||||
receipt.provider = transport.to_string();
|
||||
TransportResult::transported(
|
||||
invocation,
|
||||
vec![TransportEvent {
|
||||
|
|
|
|||
Loading…
Reference in a new issue