Merge ghostlight-verse/civic-name-schema: tool schemas state the kernel's civic-name rule from one bound (cut-civic-name-schema r1, report h1)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
c78d7e58ea
3 changed files with 178 additions and 30 deletions
|
|
@ -2574,7 +2574,17 @@ pub(super) const ACTOR_ROLE: &str = "actor";
|
|||
/// alphabet: world-declared, kernel-opaque, and safe to surface in a generated
|
||||
/// tool description.
|
||||
pub(crate) fn is_civic_name(value: &str) -> bool {
|
||||
is_tool_name(value, 48)
|
||||
is_tool_name(value, CIVIC_NAME_MAX)
|
||||
}
|
||||
|
||||
/// The longest civic name, in characters. The kernel check above and the
|
||||
/// schema every civic-name field carries both read this one number.
|
||||
const CIVIC_NAME_MAX: usize = 48;
|
||||
|
||||
/// The tool-schema property for a value the kernel will hold to
|
||||
/// [`is_civic_name`]: the model reads the rule where it fills the field.
|
||||
fn civic_name_schema(description: &str) -> Value {
|
||||
tool_schema::civic_name(description, CIVIC_NAME_MAX)
|
||||
}
|
||||
|
||||
/// `[a-z][a-z0-9_]{0,max-1}`. Affordance kinds and roles become generated tool
|
||||
|
|
@ -2701,7 +2711,7 @@ fn validate_affordance(
|
|||
mismatches: &mut Vec<Mismatch>,
|
||||
) {
|
||||
let site = || Site::Declaration(handle.clone());
|
||||
if !is_tool_name(&kind.0, 48) || roles.iter().any(|spec| !is_tool_name(&spec.role.0, 32)) {
|
||||
if !is_tool_name(&kind.0, CIVIC_NAME_MAX) || roles.iter().any(|spec| !is_tool_name(&spec.role.0, 32)) {
|
||||
mismatches.push(Mismatch::InvalidAffordanceName {
|
||||
handle: handle.clone(),
|
||||
});
|
||||
|
|
@ -7095,15 +7105,12 @@ fn field_schema(kind: PatchFieldKind) -> Value {
|
|||
tool_schema::canonical_string("an exact evidence receipt retrieved for this answer")
|
||||
}
|
||||
PatchFieldKind::Flag => json!({"type": "boolean"}),
|
||||
PatchFieldKind::Text(description) | PatchFieldKind::Name(description) => {
|
||||
tool_schema::canonical_string(description)
|
||||
}
|
||||
PatchFieldKind::Text(description) => tool_schema::canonical_string(description),
|
||||
PatchFieldKind::Name(description) => civic_name_schema(description),
|
||||
PatchFieldKind::TextList(description) => {
|
||||
tool_schema::list(tool_schema::canonical_string(description))
|
||||
}
|
||||
PatchFieldKind::NameSet(description) => {
|
||||
tool_schema::list(tool_schema::canonical_string(description))
|
||||
}
|
||||
PatchFieldKind::NameSet(description) => tool_schema::list(civic_name_schema(description)),
|
||||
PatchFieldKind::Choice(values) => tool_schema::name_enum(values),
|
||||
PatchFieldKind::Composite(shape) => composite_schema(shape),
|
||||
PatchFieldKind::CompositeList(shape) => tool_schema::list(composite_schema(shape)),
|
||||
|
|
@ -7133,7 +7140,7 @@ fn composite_schema(shape: CompositeShape) -> Value {
|
|||
"restricted",
|
||||
vec![(
|
||||
"requires".to_owned(),
|
||||
tool_schema::canonical_string("the authority kind that opens this route"),
|
||||
civic_name_schema("the authority kind that opens this route"),
|
||||
)],
|
||||
),
|
||||
],
|
||||
|
|
@ -7158,7 +7165,7 @@ fn composite_schema(shape: CompositeShape) -> Value {
|
|||
CompositeShape::AuthorityGrantRef => tool_schema::object(vec![
|
||||
(
|
||||
"kind".to_owned(),
|
||||
tool_schema::canonical_string("the authority kind"),
|
||||
civic_name_schema("the authority kind"),
|
||||
),
|
||||
(
|
||||
"over".to_owned(),
|
||||
|
|
@ -7222,7 +7229,7 @@ fn composite_schema(shape: CompositeShape) -> Value {
|
|||
("subject", Some(subject_ref_schema())),
|
||||
(
|
||||
"trait",
|
||||
Some(tool_schema::canonical_string("the trait's name")),
|
||||
Some(civic_name_schema("the trait's name")),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
@ -7357,7 +7364,7 @@ fn precondition_ref_schema() -> Value {
|
|||
),
|
||||
(
|
||||
"kind".to_owned(),
|
||||
tool_schema::canonical_string("the authority kind"),
|
||||
civic_name_schema("the authority kind"),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
@ -7365,7 +7372,7 @@ fn precondition_ref_schema() -> Value {
|
|||
"has_standing",
|
||||
vec![(
|
||||
"grievance".to_owned(),
|
||||
tool_schema::canonical_string("the grievance kind"),
|
||||
civic_name_schema("the grievance kind"),
|
||||
)],
|
||||
),
|
||||
(
|
||||
|
|
@ -7439,7 +7446,7 @@ fn precondition_schema() -> Value {
|
|||
("over".to_owned(), role()),
|
||||
(
|
||||
"kind".to_owned(),
|
||||
tool_schema::canonical_string("the authority kind"),
|
||||
civic_name_schema("the authority kind"),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
@ -7447,7 +7454,7 @@ fn precondition_schema() -> Value {
|
|||
"has_standing",
|
||||
vec![(
|
||||
"grievance".to_owned(),
|
||||
tool_schema::canonical_string("the grievance kind"),
|
||||
civic_name_schema("the grievance kind"),
|
||||
)],
|
||||
),
|
||||
(
|
||||
|
|
@ -7482,13 +7489,13 @@ fn component_op_kind_schema() -> Value {
|
|||
let authority_kind = || {
|
||||
vec![(
|
||||
"kind".to_owned(),
|
||||
tool_schema::canonical_string("the authority kind"),
|
||||
civic_name_schema("the authority kind"),
|
||||
)]
|
||||
};
|
||||
let office = || {
|
||||
vec![(
|
||||
"office".to_owned(),
|
||||
tool_schema::canonical_string("the office's name"),
|
||||
civic_name_schema("the office's name"),
|
||||
)]
|
||||
};
|
||||
tool_schema::variant(
|
||||
|
|
@ -8276,6 +8283,133 @@ mod tests {
|
|||
CallerId, CommandBody, CommandId, KernelError, SubmitReceipt, WorldKernel, WorldPhase,
|
||||
};
|
||||
|
||||
/// Every leaf string schema in `value`.
|
||||
fn string_leaves<'a>(value: &'a Value, out: &mut Vec<&'a serde_json::Map<String, Value>>) {
|
||||
match value {
|
||||
Value::Object(fields) => {
|
||||
if fields.get("type") == Some(&json!("string")) {
|
||||
out.push(fields);
|
||||
}
|
||||
fields.values().for_each(|inner| string_leaves(inner, out));
|
||||
}
|
||||
Value::Array(items) => items.iter().for_each(|inner| string_leaves(inner, out)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn civic_rule_text() -> String {
|
||||
format!("lowercase snake_case, starting with a letter, at most {CIVIC_NAME_MAX} characters")
|
||||
}
|
||||
|
||||
/// Owner: `is_civic_name` and `CIVIC_NAME_MAX`. The model reads the rule
|
||||
/// where it fills the field; an unstated rule refused 17 of 17 patches in
|
||||
/// the Zyphos atlas run. Each description below names a value that reaches
|
||||
/// `is_civic_name` (affordance kind, grant and precondition kinds, offices,
|
||||
/// delegated kinds, grievances, trait names).
|
||||
#[test]
|
||||
fn every_civic_field_carries_the_rule() {
|
||||
let civic_phrases = [
|
||||
"the affordance's tool name",
|
||||
"the authority kind",
|
||||
"the grievance kind",
|
||||
"the office's name",
|
||||
"the trait's name",
|
||||
"an authority kind the office lends",
|
||||
];
|
||||
let pattern = format!("^[a-z][a-z0-9_]{{0,{}}}$", CIVIC_NAME_MAX - 1);
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for tool in patch_tools() {
|
||||
let schema: Value = serde_json::from_str(&tool.parameters_json).unwrap();
|
||||
let mut leaves = Vec::new();
|
||||
string_leaves(&schema, &mut leaves);
|
||||
for leaf in leaves {
|
||||
let description = leaf
|
||||
.get("description")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or_default();
|
||||
let Some(phrase) = civic_phrases
|
||||
.iter()
|
||||
.find(|phrase| description.starts_with(**phrase))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
seen.insert(*phrase);
|
||||
assert_eq!(
|
||||
leaf.get("pattern"),
|
||||
Some(&json!(pattern)),
|
||||
"{}: {description}",
|
||||
tool.name
|
||||
);
|
||||
assert!(
|
||||
description.contains(&civic_rule_text()),
|
||||
"{}: {description}",
|
||||
tool.name
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
seen.len(),
|
||||
civic_phrases.len(),
|
||||
"the walk missed a civic field: {seen:?}"
|
||||
);
|
||||
// Every Name and NameSet field resolves into a civic schema, whatever its words.
|
||||
for entry in PATCH_TOOLS {
|
||||
for field in entry.fields {
|
||||
let leaf = match field.kind {
|
||||
PatchFieldKind::Name(_) => field_schema(field.kind),
|
||||
PatchFieldKind::NameSet(_) => field_schema(field.kind)["items"].clone(),
|
||||
_ => continue,
|
||||
};
|
||||
assert_eq!(leaf["pattern"], json!(pattern), "{}", entry.name);
|
||||
assert!(
|
||||
leaf["description"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains(&civic_rule_text())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The exemplar the schema ships is itself a civic name, and the boundary
|
||||
/// the text states is the boundary `is_civic_name` enforces.
|
||||
#[test]
|
||||
fn the_exemplar_and_the_stated_rule_agree_with_the_kernel() {
|
||||
for kind in [PatchFieldKind::Name("x"), PatchFieldKind::NameSet("x")] {
|
||||
let example = field_example(kind);
|
||||
let names = match &example {
|
||||
Value::Array(items) => items.clone(),
|
||||
single => vec![single.clone()],
|
||||
};
|
||||
for name in names {
|
||||
assert!(is_civic_name(name.as_str().unwrap()), "{name}");
|
||||
}
|
||||
}
|
||||
assert!(is_civic_name("toll_command"));
|
||||
assert!(is_civic_name(&"a".repeat(CIVIC_NAME_MAX)));
|
||||
assert!(!is_civic_name(&"a".repeat(CIVIC_NAME_MAX + 1)));
|
||||
for bad in ["", "Toll", "1toll", "_toll", "toll-command", "toll command", "t\u{f6}ll"] {
|
||||
assert!(!is_civic_name(bad), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Free-text fields are untouched: no pattern, same words.
|
||||
#[test]
|
||||
fn text_fields_are_unchanged() {
|
||||
assert_eq!(
|
||||
field_schema(PatchFieldKind::Label),
|
||||
json!({"type": "string", "description": "a display label"})
|
||||
);
|
||||
assert_eq!(
|
||||
field_schema(PatchFieldKind::Statement),
|
||||
json!({"type": "string", "description": "the committed world text this fact carries"})
|
||||
);
|
||||
assert_eq!(
|
||||
field_schema(PatchFieldKind::Text("some prose")),
|
||||
json!({"type": "string", "description": "some prose"})
|
||||
);
|
||||
}
|
||||
|
||||
/// Owner: `WorldPatch::sets_persona_of_existing_subject`. Consumer:
|
||||
/// `ghostlight-dungeon`'s play agent (PA.f61, parked at
|
||||
/// `parked/ghostlight-eve-surface`), which refuses a
|
||||
|
|
|
|||
|
|
@ -43,6 +43,20 @@ pub(super) fn canonical_string(description: &str) -> Value {
|
|||
json!({"type": "string", "description": description})
|
||||
}
|
||||
|
||||
/// A string the kernel will hold to its civic-name rule, `[a-z][a-z0-9_]` up to
|
||||
/// `max` characters. The rule is stated twice on purpose: as a `pattern` a
|
||||
/// strict decoder can enforce, and in the description the model reads. `max` is
|
||||
/// the kernel's own bound, passed in so the rule has one owner.
|
||||
pub(super) fn civic_name(description: &str, max: usize) -> Value {
|
||||
json!({
|
||||
"type": "string",
|
||||
"pattern": format!("^[a-z][a-z0-9_]{{0,{}}}$", max - 1),
|
||||
"description": format!(
|
||||
"{description}: lowercase snake_case, starting with a letter, at most {max} characters (e.g. toll_command)"
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn bounded_integer(min: u64, max: u64) -> Value {
|
||||
json!({"type": "integer", "minimum": min, "maximum": max})
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue