use std::time::Duration;
use anyhow::{Context as _, Result, bail};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::agent::registry::ParentKey;
pub(crate) const ARG: &str = "extract";
pub(crate) const MAX_TEXT_BYTES: usize = 64 * 1024;
pub(crate) const MAX_DEPTH: usize = 8;
pub(crate) const DEADLINE: Duration = Duration::from_mins(5);
pub(crate) const PURPOSE: &str = "extract";
const ENFORCED: &[&str] = &[
"type",
"properties",
"required",
"items",
"enum",
"const",
"additionalProperties",
];
const TOLERATED: &[&str] = &[
"$schema",
"$id",
"title",
"description",
"default",
"examples",
"format",
"pattern",
"minimum",
"maximum",
"exclusiveMinimum",
"exclusiveMaximum",
"multipleOf",
"minLength",
"maxLength",
"minItems",
"maxItems",
"minProperties",
"maxProperties",
];
const REFERENCE_LIKE: &[&str] = &[
"$ref",
"$defs",
"$dynamicRef",
"$dynamicAnchor",
"$anchor",
"$recursiveRef",
"$recursiveAnchor",
];
const TYPES: &[&str] = &[
"object", "array", "string", "number", "integer", "boolean", "null",
];
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Extract(Value);
impl<'de> Deserialize<'de> for Extract {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> std::result::Result<Self, D::Error> {
let value = Value::deserialize(deserializer)?;
Self::parse(&value).map_err(serde::de::Error::custom)
}
}
impl Extract {
pub(crate) fn from_arg(value: &Value) -> Result<Self> {
match value {
Value::String(text) => Self::from_text(text),
other => Self::parse(other),
}
}
pub(crate) fn from_text(text: &str) -> Result<Self> {
if text.len() > MAX_TEXT_BYTES {
bail!(
"usage: an `{ARG}` schema is at most {} KiB of text",
MAX_TEXT_BYTES / 1024
);
}
let text = text.trim();
if text.is_empty() {
bail!("usage: `{ARG}` is empty — give the JSON schema the answer must match");
}
let value: Value = serde_json::from_str(text)
.with_context(|| format!("usage: `{ARG}` is not valid JSON"))?;
Self::parse(&value)
}
fn parse(value: &Value) -> Result<Self> {
let schema = Self(value.clone());
let canonical = schema.canonical();
if canonical.len() > MAX_TEXT_BYTES {
bail!(
"usage: an `{ARG}` schema is at most {} KiB of text",
MAX_TEXT_BYTES / 1024
);
}
validate_node(&schema.0, 1)?;
Ok(schema)
}
pub(crate) fn from_stored(raw: Option<String>) -> Option<Self> {
match serde_json::from_str::<Self>(&raw?) {
Ok(extract) => Some(extract),
Err(e) => {
tracing::warn!(
error = %e,
"stored `extract` schema is unreadable — delivering the answer whole",
);
None
}
}
}
pub(crate) fn canonical(&self) -> String {
render(&self.0)
}
fn check(&self, value: &Value) -> std::result::Result<(), String> {
check_node(&self.0, value, "the answer ")
}
}
pub(crate) struct StepContext<'a> {
pub role: crate::Role,
pub workspace: &'a str,
pub conversation: Option<&'a str>,
pub parent: Option<ParentKey>,
}
async fn transform(extract: &Extract, step: &StepContext<'_>, answer: &str) -> Option<String> {
let _visible = crate::agent::registry::NON_AGENT_CALLS.register(
PURPOSE,
step.workspace,
step.parent.clone(),
false,
None,
);
let prompt = crate::prompt::substitute(
&crate::prompt::load_prompt("extraction/schema.md"),
&[("{{schema}}", &extract.canonical()), ("{{answer}}", answer)],
);
let mut request = crate::agent::chat_request(step.role, None, Vec::new());
let recorded = match step.conversation {
Some(conversation) => crate::session::store().get_provider(conversation).await,
None => None,
};
match recorded {
Some(stored) => {
request.model = stored.model;
request.provider = Some(stored.provider);
}
None => request.provider = crate::providers::selection::pick(&request.model),
}
request.meta = Some(crate::ChatRequestMeta {
purpose: PURPOSE,
agent_id: format!("extract_{}", crate::generate_suffix()),
role: step.role.as_str().to_string(),
workspace: step.workspace.to_string(),
ticket_id: None,
});
let schema = extract.clone();
let validate = move |value: &Value| schema.check(value);
let rules = crate::agent::extraction::Rules {
feedback: true,
truncation_terminal: true,
deadline: Some(DEADLINE),
validator_class: crate::retry::FailureClass::SchemaMismatch,
};
match crate::agent::extraction::retry_extract_structured_with(
&[],
&prompt,
&request,
Some(&validate),
Some(&crate::retry::RetryPolicy::extract()),
rules,
)
.await
{
Ok(value) => match serde_json::to_string(&value) {
Ok(text) => Some(text),
Err(e) => {
tracing::warn!(error = %e, "the extracted answer could not be written as JSON");
None
}
},
Err(e) => {
tracing::warn!(
purpose = PURPOSE,
workspace = step.workspace,
error = %e,
"the answer could not be turned into JSON matching its schema — forwarding it whole",
);
None
}
}
}
pub(crate) async fn answer_as_json_reporting(
extract: Option<&Extract>,
step: &StepContext<'_>,
answer: &str,
) -> (String, bool) {
let Some(extract) = extract.filter(|_| !answer.trim().is_empty()) else {
return (answer.to_string(), false);
};
match transform(extract, step, answer).await {
Some(json) => (json, true),
None => (answer.to_string(), false),
}
}
fn validate_node(node: &Value, level: usize) -> Result<()> {
if level > MAX_DEPTH {
bail!(
"usage: an `{ARG}` schema nests at most {MAX_DEPTH} levels — the root is the first, and \
every `properties` value and `items` node adds one"
);
}
let Some(object) = node.as_object() else {
if node.is_boolean() {
bail!("usage: a boolean subschema is not supported in an `{ARG}` schema");
}
bail!("usage: an `{ARG}` schema node must be a JSON object");
};
for key in object.keys() {
if ENFORCED.contains(&key.as_str()) || TOLERATED.contains(&key.as_str()) {
continue;
}
if REFERENCE_LIKE.contains(&key.as_str()) {
bail!("usage: `{key}` is reference-like and is not supported in an `{ARG}` schema");
}
bail!("usage: `{key}` is not supported in an `{ARG}` schema");
}
if let Some(values) = object.get("enum") {
match values.as_array() {
Some(values) if !values.is_empty() => {}
Some(_) => bail!("usage: `enum` lists at least one allowed value"),
None => bail!("usage: `enum` is a list of allowed values"),
}
}
let types = declared_types(object)?;
if object.contains_key("properties") && !types.is_empty() && !types.contains(&"object") {
bail!("usage: a schema node declaring `properties` must accept `object`");
}
if object.contains_key("items") && !types.is_empty() && !types.contains(&"array") {
bail!("usage: a schema node declaring `items` must accept `array`");
}
if !constrains_something(object, &types) {
bail!(
"usage: this `{ARG}` schema node constrains nothing — give it a `type`, `properties`, \
`items`, `required`, `enum` or `const`"
);
}
if let Some(properties) = object.get("properties") {
let Some(properties) = properties.as_object() else {
bail!("usage: `properties` is an object of property schemas");
};
for schema in properties.values() {
validate_node(schema, level + 1)?;
}
}
if let Some(required) = object.get("required") {
let Some(required) = required.as_array() else {
bail!("usage: `required` is a list of property names");
};
let declared = object.get("properties").and_then(Value::as_object);
for name in required {
let Some(name) = name.as_str() else {
bail!("usage: `required` is a list of property names");
};
let known = declared.is_some_and(|properties| properties.contains_key(name));
if !known {
bail!("usage: `required` names `{name}`, which no `properties` entry declares");
}
}
}
if let Some(items) = object.get("items") {
if items.is_array() {
bail!("usage: array-form `items` is not supported — give one schema for every item");
}
validate_node(items, level + 1)?;
}
if let Some(additional) = object.get("additionalProperties")
&& !additional.is_boolean()
{
bail!("usage: `additionalProperties` is `true` or `false`, not a schema");
}
Ok(())
}
fn declared_types(object: &Map<String, Value>) -> Result<Vec<&'static str>> {
let Some(declared) = object.get("type") else {
return Ok(Vec::new());
};
let names = match declared {
Value::String(name) => vec![name.as_str()],
Value::Array(names) if !names.is_empty() => names
.iter()
.map(|name| {
name.as_str().ok_or_else(|| {
anyhow::anyhow!("usage: `type` is a type name or a list of them")
})
})
.collect::<Result<Vec<_>>>()?,
_ => bail!("usage: `type` is a type name or a non-empty list of them"),
};
names
.into_iter()
.map(|name| {
TYPES
.iter()
.copied()
.find(|known| *known == name)
.ok_or_else(|| {
anyhow::anyhow!(
"usage: `type` names `{name}`, which is not one of {}",
TYPES.join(", ")
)
})
})
.collect()
}
fn constrains_something(object: &Map<String, Value>, types: &[&str]) -> bool {
let declares = |key: &str| match object.get(key) {
Some(Value::Array(list)) => !list.is_empty(),
Some(Value::Object(map)) => !map.is_empty(),
Some(_) => true,
None => false,
};
let names_something = !types.is_empty()
|| object.contains_key("items")
|| object.contains_key("const")
|| ["properties", "required", "enum"]
.iter()
.any(|key| declares(key));
if !names_something {
return false;
}
let any_object = types.contains(&"object")
&& object.get("additionalProperties") == Some(&Value::Bool(true))
&& !["properties", "required", "enum"]
.iter()
.any(|key| declares(key))
&& !object.contains_key("const");
!any_object
}
fn check_node(node: &Value, value: &Value, path: &str) -> std::result::Result<(), String> {
let object = node
.as_object()
.ok_or_else(|| format!("{path}is not a schema"))?;
if let Some(expected) = object.get("const")
&& expected != value
{
return Err(format!(
"{path}must be {}, the only allowed value",
render(expected)
));
}
if let Some(values) = object.get("enum").and_then(Value::as_array)
&& !values.contains(value)
{
let allowed: Vec<String> = values.iter().map(render).collect();
return Err(format!("{path}must be one of {}", allowed.join(", ")));
}
let types = declared_types(object).expect("a validated schema's `type`");
if !types.is_empty() && !types.iter().any(|kind| matches_type(kind, value)) {
return Err(format!(
"{path}must be {}, not {}",
types.join(" or "),
kind(value)
));
}
if let Some(entries) = value.as_object() {
if let Some(required) = object.get("required").and_then(Value::as_array) {
for name in required.iter().filter_map(Value::as_str) {
if !entries.contains_key(name) {
return Err(format!("{path}is missing required property `{name}`"));
}
}
}
let declared = object.get("properties").and_then(Value::as_object);
if let Some(properties) = declared {
for (name, schema) in properties {
if let Some(entry) = entries.get(name) {
check_node(schema, entry, &format!("{path}`{name}` "))?;
}
}
}
if object.get("additionalProperties") == Some(&Value::Bool(false)) {
for name in entries.keys() {
if !declared.is_some_and(|properties| properties.contains_key(name)) {
return Err(format!(
"{path}has property `{name}`, which the schema does not allow"
));
}
}
}
}
if let (Some(items), Some(entries)) = (object.get("items"), value.as_array()) {
for (index, entry) in entries.iter().enumerate() {
check_node(items, entry, &format!("{path}item {index} "))?;
}
}
Ok(())
}
fn matches_type(kind: &str, value: &Value) -> bool {
match kind {
"object" => value.is_object(),
"array" => value.is_array(),
"string" => value.is_string(),
"number" => value.is_number(),
"integer" => value.is_i64() || value.is_u64(),
"boolean" => value.is_boolean(),
"null" => value.is_null(),
_ => false,
}
}
fn kind(value: &Value) -> &'static str {
match value {
Value::Null => "null",
Value::Bool(_) => "boolean",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Array(_) => "array",
Value::Object(_) => "object",
}
}
fn render(value: &Value) -> String {
serde_json::to_string(value).expect("a JSON value always serializes")
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[expect(clippy::needless_pass_by_value)] fn schema(value: Value) -> Extract {
Extract::parse(&value).expect("the schema is accepted")
}
#[expect(clippy::needless_pass_by_value)] fn refused(value: Value) -> String {
Extract::parse(&value)
.expect_err("the schema is refused")
.to_string()
}
#[test]
fn the_enforced_subset_is_accepted() {
for value in [
json!({ "type": "string" }),
json!({ "type": ["string", "null"] }),
json!({ "type": "integer" }),
json!({ "type": "number" }),
json!({ "type": "boolean" }),
json!({ "type": "array", "items": { "type": "string" } }),
json!({ "type": "object", "properties": { "a": { "type": "string" } }, "required": ["a"] }),
json!({ "type": "object", "properties": { "a": { "type": "string" } }, "additionalProperties": false }),
json!({ "enum": ["a", "b"] }),
json!({ "const": 3 }),
json!({ "type": "object", "properties": { "a": { "type": "string" } }, "additionalProperties": true }),
] {
let _ = schema(value);
}
}
#[test]
fn tolerated_keywords_are_ignored() {
let value = json!({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "x",
"title": "t",
"description": "d",
"default": 1,
"examples": [1],
"format": "date",
"pattern": "^a",
"minimum": 1,
"maxLength": 3,
"type": "integer"
});
let schema = schema(value);
assert!(schema.check(&json!(4)).is_ok(), "a bound is not enforced");
}
#[test]
fn a_stored_schema_reads_back_and_an_unreadable_one_is_absent() {
let schema = schema(json!({ "type": "string" }));
assert_eq!(
Extract::from_stored(Some(schema.canonical())),
Some(schema),
"a stored schema reads back as itself"
);
assert_eq!(Extract::from_stored(None), None);
assert_eq!(Extract::from_stored(Some("{not json".to_string())), None);
assert_eq!(
Extract::from_stored(Some(r#"{"type":"object","allOf":[]}"#.to_string())),
None,
"a stored schema this build would refuse is not applied as a looser one"
);
}
#[test]
fn unsupported_keywords_are_refused_by_name() {
for (value, named) in [
(json!({ "$ref": "#/a" }), "$ref"),
(json!({ "type": "object", "$comment": "x" }), "$comment"),
(json!({ "type": "object", "allOf": [] }), "allOf"),
(json!({ "type": "object", "anyOf": [] }), "anyOf"),
(json!({ "type": "object", "oneOf": [] }), "oneOf"),
(json!({ "type": "object", "not": {} }), "not"),
(
json!({ "type": "object", "patternProperties": {} }),
"patternProperties",
),
(
json!({ "type": "object", "propertyNames": {} }),
"propertyNames",
),
(json!({ "type": "array", "contains": {} }), "contains"),
(
json!({ "type": "object", "dependencies": {} }),
"dependencies",
),
(
json!({ "type": "object", "dependentRequired": {} }),
"dependentRequired",
),
(
json!({ "type": "object", "dependentSchemas": {} }),
"dependentSchemas",
),
(
json!({ "type": "object", "unevaluatedProperties": false }),
"unevaluatedProperties",
),
(
json!({ "type": "array", "unevaluatedItems": false }),
"unevaluatedItems",
),
(json!({ "type": "object", "$defs": {} }), "$defs"),
(
json!({ "type": "object", "definitions": {} }),
"definitions",
),
(json!({ "type": "array", "prefixItems": [] }), "prefixItems"),
(
json!({ "type": "array", "additionalItems": false }),
"additionalItems",
),
(json!({ "type": "object", "if": {} }), "if"),
(json!({ "type": "object", "then": {} }), "then"),
(json!({ "type": "object", "else": {} }), "else"),
] {
let err = refused(value);
assert!(err.contains(named), "the refusal names {named}: {err}");
}
}
#[test]
fn a_schema_that_constrains_nothing_is_refused() {
for value in [
json!({}),
json!({ "title": "only annotations" }),
json!({ "type": "object", "additionalProperties": true }),
json!({ "type": ["object", "null"], "additionalProperties": true }),
json!({ "required": [] }),
json!({ "type": "object", "properties": {}, "additionalProperties": true }),
] {
assert!(refused(value).contains("constrains nothing"));
}
assert!(
Extract::from_arg(&json!({
"type": "object",
"properties": {},
"additionalProperties": false,
}))
.is_ok()
);
assert!(
Extract::from_arg(&json!({
"type": "object",
"additionalProperties": true,
"enum": [{ "a": 1 }],
}))
.is_ok()
);
}
#[test]
fn a_node_must_accept_the_shape_it_declares() {
assert!(
refused(json!({ "type": "string", "properties": { "a": { "type": "string" } } }))
.contains("must accept `object`")
);
assert!(
refused(json!({ "type": "object", "items": { "type": "string" } }))
.contains("must accept `array`")
);
assert!(
refused(json!({ "type": "object", "required": ["a"] }))
.contains("no `properties` entry declares")
);
assert!(
refused(json!({ "type": "array", "items": [{ "type": "string" }] }))
.contains("array-form `items`")
);
assert!(
refused(json!({ "type": "object", "additionalProperties": { "type": "string" } }))
.contains("not a schema")
);
assert!(refused(json!(true)).contains("boolean subschema"));
assert!(refused(json!({ "type": "bogus" })).contains("not one of"));
assert!(refused(json!({ "enum": [] })).contains("`enum` lists at least one allowed value"));
}
#[test]
fn the_nesting_bound_counts_the_root() {
let mut node = json!({ "type": "string" });
for _ in 0..(MAX_DEPTH - 1) {
node = json!({ "type": "array", "items": node });
}
let _ = schema(node.clone());
let deeper = json!({ "type": "array", "items": node });
assert!(refused(deeper).contains("nests at most"));
}
#[test]
fn the_text_cap_applies_to_every_form() {
let oversized = json!({
"type": "string",
"description": "x".repeat(MAX_TEXT_BYTES),
});
assert!(Extract::from_text(&oversized.to_string()).is_err());
assert!(Extract::from_arg(&oversized).is_err());
assert!(serde_json::from_value::<Extract>(oversized).is_err());
assert!(Extract::from_text(" ").is_err(), "empty is refused");
assert!(Extract::from_text("not json").is_err());
}
#[test]
fn key_order_and_whitespace_do_not_change_a_schema() {
let first = Extract::from_text(r#"{"type":"object","properties":{"a":{"type":"string"}}}"#)
.expect("accepted");
let second = Extract::from_text(
"{ \"properties\": { \"a\": { \"type\": \"string\" } }, \"type\": \"object\" }",
)
.expect("accepted");
assert_eq!(first, second);
assert_eq!(first.canonical(), second.canonical());
}
#[test]
fn the_check_reports_what_is_wrong() {
let schema = schema(json!({
"type": "object",
"properties": {
"name": { "type": "string" },
"tags": { "type": "array", "items": { "type": "string" } },
"kind": { "enum": ["a", "b"] }
},
"required": ["name"],
"additionalProperties": false
}));
assert!(
schema
.check(&json!({ "name": "x", "tags": ["t"], "kind": "a" }))
.is_ok()
);
assert_eq!(
schema.check(&json!({})).unwrap_err(),
"the answer is missing required property `name`"
);
assert_eq!(
schema.check(&json!({ "name": 1 })).unwrap_err(),
"the answer `name` must be string, not number"
);
assert_eq!(
schema
.check(&json!({ "name": "x", "tags": [1] }))
.unwrap_err(),
"the answer `tags` item 0 must be string, not number"
);
assert_eq!(
schema
.check(&json!({ "name": "x", "kind": "c" }))
.unwrap_err(),
"the answer `kind` must be one of \"a\", \"b\""
);
assert_eq!(
schema
.check(&json!({ "name": "x", "extra": 1 }))
.unwrap_err(),
"the answer has property `extra`, which the schema does not allow"
);
assert_eq!(
schema.check(&json!([1])).unwrap_err(),
"the answer must be object, not array"
);
}
#[test]
fn a_union_type_accepts_every_listed_type() {
let schema = schema(json!({ "type": ["string", "null"] }));
assert!(schema.check(&json!(null)).is_ok());
assert!(schema.check(&json!("x")).is_ok());
assert!(schema.check(&json!(1)).is_err());
}
#[test]
fn the_argument_is_read_from_an_object_or_from_json_text() {
let object = json!({ "type": "object", "properties": { "a": { "type": "string" } } });
let from_object = Extract::from_arg(&object).expect("an object is accepted");
let from_text =
Extract::from_arg(&json!(object.to_string())).expect("its JSON text is accepted");
assert_eq!(from_object, from_text, "both forms are one schema");
for refused in [json!([{ "type": "string" }]), json!(true), json!(3)] {
assert!(
Extract::from_arg(&refused).is_err(),
"must refuse {refused}"
);
}
}
#[tokio::test]
#[serial_test::serial(provider, drain)]
async fn the_transformation_asks_in_one_user_turn() {
let _store = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_management_test_stores().await;
let _policy_guard =
crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
let fake = std::sync::Arc::new(crate::util::test::FakeProvider::new().ok(r#"{"a":"one"}"#));
let _provider_guard = crate::util::test::install_fake_provider(fake.clone());
let schema =
schema(json!({ "type": "object", "properties": { "a": { "type": "string" } } }));
let step = StepContext {
role: crate::Role::Worker,
workspace: "ws",
conversation: None,
parent: None,
};
assert_eq!(
answer_as_json_reporting(Some(&schema), &step, "the whole answer").await,
(r#"{"a":"one"}"#.to_string(), true)
);
let messages = fake.request_messages.lock().unwrap();
let sent = &messages[0];
assert!(
!sent.contains('\u{0}'),
"one turn, not a conversation: {sent}"
);
assert!(
sent.contains("role: User"),
"the turn is the user's: {sent}"
);
assert!(sent.contains("the whole answer"), "{sent}");
let canonical = format!("{:?}", schema.canonical());
assert!(
sent.contains(canonical.trim_matches('"')),
"the schema is rendered into the same turn: {sent}"
);
}
#[tokio::test]
#[serial_test::serial(provider, drain)]
async fn the_transformation_runs_on_the_steps_recorded_model_and_provider() {
let _store = crate::util::test::STORE_LOCK.lock().await;
crate::util::test::init_management_test_stores().await;
let _policy_guard =
crate::util::test::install_test_retry_policy(crate::retry::tiny_test_policy());
let fake = std::sync::Arc::new(crate::util::test::FakeProvider::new().ok(r#"{"a":"one"}"#));
let _provider_guard = crate::util::test::install_fake_provider(fake.clone());
let conversation = crate::session::worker_branch_agent_id("schema_model");
crate::session::store()
.set_branch(&conversation, None, None, None, None, None)
.await
.expect("the step's conversation exists");
let stored = crate::providers::selection::StoredProvider {
model: "step-model".to_string(),
provider: "step-provider".to_string(),
};
assert!(
crate::session::store()
.set_provider(&conversation, Some(&stored))
.await
.expect("the provider is recorded")
);
let schema =
schema(json!({ "type": "object", "properties": { "a": { "type": "string" } } }));
let step = StepContext {
role: crate::Role::Worker,
workspace: "ws",
conversation: Some(&conversation),
parent: None,
};
assert_eq!(
answer_as_json_reporting(Some(&schema), &step, "the answer").await,
(r#"{"a":"one"}"#.to_string(), true)
);
let sent = fake.sent.lock().unwrap();
assert_eq!(sent[0].model, "step-model");
assert_eq!(sent[0].provider.as_deref(), Some("step-provider"));
}
}