Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf(discovery): materialize contracts only after metadata selection
  • Loading branch information
yyjeqhc committed Sep 29, 2026
commit a4552c83e75687e127cc98f1729d93c4b3feab3e
7 changes: 7 additions & 0 deletions crates/webcodex-tool-contracts/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ pub use tool_specs::{
registered_tool_specs, skill_management_tool_specs, skill_runtime_tool_specs,
stateless_operator_extension_tool_specs, work_result_app_tool_specs,
};

#[cfg(any(test, feature = "root-test-support"))]
pub use tool_specs::take_tool_materialization_counts_for_test;
pub use tool_specs::{
exact_manifest_specialist_tool_descriptors, registered_tool_descriptors,
stateless_operator_extension_tool_descriptors, ToolDescriptor,
};
219 changes: 150 additions & 69 deletions crates/webcodex-tool-contracts/src/registry/tool_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,80 @@ use super::super::tool_spec::ToolSpec;
use super::{output_schema_for_tool, tool_annotations};
use std::collections::BTreeSet;

/// Schema-free catalog entry. Selection and counts need metadata, not eagerly
/// materialized input/output contracts. This record does not admit a tool.
#[derive(Debug, Clone)]
pub struct ToolDescriptor {
pub name: String,
pub description: String,
}

impl ToolDescriptor {
pub fn annotations(&self) -> serde_json::Value {
tool_annotations(&self.name)
}

pub fn input_schema(&self) -> serde_json::Value {
#[cfg(any(test, feature = "root-test-support"))]
MATERIALIZATIONS.with(|count| {
let (input, output) = count.get();
count.set((input + 1, output));
});
input_schema_for_tool(&self.name)
}

pub fn into_spec(self) -> ToolSpec {
let input_schema = self.input_schema();
#[cfg(any(test, feature = "root-test-support"))]
MATERIALIZATIONS.with(|count| {
let (input, output) = count.get();
count.set((input, output + 1));
});
ToolSpec {
input_schema,
output_schema: output_schema_for_tool(&self.name),
annotations: self.annotations(),
name: self.name,
description: self.description,
}
}
}

#[cfg(any(test, feature = "root-test-support"))]
thread_local! {
static MATERIALIZATIONS: std::cell::Cell<(usize, usize)> = const { std::cell::Cell::new((0, 0)) };
}

/// Test-thread-local contract projection counts, not cache internals or telemetry.
#[cfg(any(test, feature = "root-test-support"))]
pub fn take_tool_materialization_counts_for_test() -> (usize, usize) {
MATERIALIZATIONS.with(|count| count.replace((0, 0)))
}

pub fn registered_tool_descriptors() -> Vec<ToolDescriptor> {
resolve_tool_descriptors(model_visible_tool_definitions())
}

pub fn registered_tool_specs() -> Vec<ToolSpec> {
resolve_tool_specs(model_visible_tool_definitions())
registered_tool_descriptors()
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Specialist mutation contracts are intentionally absent from the ordinary
/// model surface but remain discoverable by exact name through tool_manifest.
pub fn exact_manifest_specialist_tool_specs() -> Vec<ToolSpec> {
resolve_tool_specs(EXACT_MANIFEST_SPECIALIST_TOOL_NAMES.iter().map(|name| {
pub fn exact_manifest_specialist_tool_descriptors() -> Vec<ToolDescriptor> {
resolve_tool_descriptors(EXACT_MANIFEST_SPECIALIST_TOOL_NAMES.iter().map(|name| {
let definition = lookup_tool_definition(name)
.unwrap_or_else(|| panic!("missing exact-manifest specialist definition: {name}"));
debug_assert!(!definition.visibility.is_model_visible());
definition
}))
}

fn operator_extension_specs(
specs: Vec<ToolSpec>,
family: ToolOperatorExtensionFamily,
) -> Vec<ToolSpec> {
specs
pub fn exact_manifest_specialist_tool_specs() -> Vec<ToolSpec> {
exact_manifest_specialist_tool_descriptors()
.into_iter()
.filter(|spec| runtime_tool_operator_extension_family(&spec.name) == Some(family))
.map(ToolDescriptor::into_spec)
.collect()
}

Expand Down Expand Up @@ -140,76 +192,80 @@ pub fn job_terminal_continuation_app_tool_specs() -> Vec<ToolSpec> {
]
}

/// Fixed admin-only forensic trace reader. It remains globally ModelHidden and
/// is projected only by capable Stateless MCP 2026 adapters.
fn operator_family_descriptors(family: ToolOperatorExtensionFamily) -> Vec<ToolDescriptor> {
let descriptors = match family {
ToolOperatorExtensionFamily::SkillRuntime | ToolOperatorExtensionFamily::SkillManagement => skills::tool_descriptors(),
ToolOperatorExtensionFamily::MemoryRuntime | ToolOperatorExtensionFamily::MemoryManagement => memory::tool_descriptors(),
ToolOperatorExtensionFamily::TraceDiagnostics => vec![tool_descriptor("read_tool_trace", "Admin-only bounded reader for Server-hosted full tool-request traces. Omit payload_index to list safe payload metadata first; then read one bounded JSON payload by index. Available only when full trace mode is enabled. Trace payloads may contain sensitive tool data and never grant execution authority.")],
};
descriptors
.into_iter()
.filter(|descriptor| {
runtime_tool_operator_extension_family(&descriptor.name) == Some(family)
})
.collect()
}

pub fn operator_diagnostic_tool_specs() -> Vec<ToolSpec> {
operator_extension_specs(
vec![tool_spec(
"read_tool_trace",
"Admin-only bounded reader for Server-hosted full tool-request traces. Omit payload_index to list safe payload metadata first; then read one bounded JSON payload by index. Available only when full trace mode is enabled. Trace payloads may contain sensitive tool data and never grant execution authority.",
)],
ToolOperatorExtensionFamily::TraceDiagnostics,
)
operator_family_descriptors(ToolOperatorExtensionFamily::TraceDiagnostics)
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Fixed read-only project Memory runtime contract. Definitions remain hidden
/// from generic/GPT Action registries and are projected only by capable
/// Stateless MCP 2026 adapters.
pub fn memory_runtime_tool_specs() -> Vec<ToolSpec> {
operator_extension_specs(
memory::tool_specs(),
ToolOperatorExtensionFamily::MemoryRuntime,
)
operator_family_descriptors(ToolOperatorExtensionFamily::MemoryRuntime)
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Fixed project Memory mutation plus global Memory lifecycle contract. Durable
/// Memory/scope cardinality never changes this schema set. Each tool's canonical
/// ToolDefinition authority distinguishes project-scoped memory:manage from
/// admin-only lifecycle inspection/purge; permission evaluation remains independent.
pub fn memory_management_tool_specs() -> Vec<ToolSpec> {
operator_extension_specs(
memory::tool_specs(),
ToolOperatorExtensionFamily::MemoryManagement,
)
operator_family_descriptors(ToolOperatorExtensionFamily::MemoryManagement)
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Fixed read-only Skill runtime contract. These definitions are deliberately
/// ModelHidden globally and are projected only by the capable Stateless MCP
/// 2026 adapter.
pub fn skill_runtime_tool_specs() -> Vec<ToolSpec> {
operator_extension_specs(
skills::tool_specs(),
ToolOperatorExtensionFamily::SkillRuntime,
)
operator_family_descriptors(ToolOperatorExtensionFamily::SkillRuntime)
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Fixed Runner-global Skill-management contract. Package/version cardinality
/// never changes this schema set; the MCP adapter additionally requires explicit
/// operator authority before projecting these tools.
pub fn skill_management_tool_specs() -> Vec<ToolSpec> {
operator_extension_specs(
skills::tool_specs(),
operator_family_descriptors(ToolOperatorExtensionFamily::SkillManagement)
.into_iter()
.map(ToolDescriptor::into_spec)
.collect()
}

/// Canonical ordered operator-extension universe. Capability/authority filtering
/// belongs to the adapter; the catalog itself never grants access.
pub fn stateless_operator_extension_tool_descriptors() -> Vec<ToolDescriptor> {
[
ToolOperatorExtensionFamily::SkillRuntime,
ToolOperatorExtensionFamily::SkillManagement,
)
ToolOperatorExtensionFamily::MemoryRuntime,
ToolOperatorExtensionFamily::MemoryManagement,
ToolOperatorExtensionFamily::TraceDiagnostics,
]
.into_iter()
.flat_map(operator_family_descriptors)
.collect()
}

/// Canonical fixed Stateless MCP 2026 operator-extension universe. These specs
/// remain globally ModelHidden and are projected only by protocol-capability-aware
/// adapters and discovery. Keeping the composition here prevents tools/list,
/// Adaptive gateway admission, and tool_manifest from maintaining separate name sets.
pub fn stateless_operator_extension_tool_specs() -> Vec<ToolSpec> {
skill_runtime_tool_specs()
stateless_operator_extension_tool_descriptors()
.into_iter()
.chain(skill_management_tool_specs())
.chain(memory_runtime_tool_specs())
.chain(memory_management_tool_specs())
.chain(operator_diagnostic_tool_specs())
.map(ToolDescriptor::into_spec)
.collect()
}

fn resolve_tool_specs<'a>(
fn resolve_tool_descriptors<'a>(
definitions: impl IntoIterator<Item = &'a ToolDefinition>,
) -> Vec<ToolSpec> {
) -> Vec<ToolDescriptor> {
let mut specs = Vec::new();
let mut seen_definition_names = BTreeSet::new();
for definition in definitions {
Expand All @@ -225,29 +281,54 @@ fn resolve_tool_specs<'a>(
definition.name
)
});
specs.push(tool_spec(definition.name, model_spec.description));
specs.push(tool_descriptor(definition.name, model_spec.description));
}
specs
}

pub(super) fn tool_spec(name: &'static str, description: impl Into<String>) -> ToolSpec {
fn tool_descriptor(name: &'static str, description: impl Into<String>) -> ToolDescriptor {
debug_assert!(
lookup_tool_definition(name).is_some(),
"{name} ToolSpec is missing a ToolDefinition"
"{name} descriptor lacks definition"
);
ToolSpec {
name: name.to_string(),
ToolDescriptor {
name: name.to_owned(),
description: description.into(),
input_schema: input_schema_for_tool(name),
output_schema: output_schema_for_tool(name),
annotations: tool_annotations(name),
}
}

pub(super) fn tool_spec(name: &'static str, description: impl Into<String>) -> ToolSpec {
tool_descriptor(name, description).into_spec()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn descriptors_preserve_full_contract_metadata_and_order_without_materialization() {
take_tool_materialization_counts_for_test();
let descriptors = registered_tool_descriptors()
.into_iter()
.chain(exact_manifest_specialist_tool_descriptors())
.chain(stateless_operator_extension_tool_descriptors())
.collect::<Vec<_>>();
assert_eq!(take_tool_materialization_counts_for_test(), (0, 0));
let specs = registered_tool_specs()
.into_iter()
.chain(exact_manifest_specialist_tool_specs())
.chain(stateless_operator_extension_tool_specs())
.collect::<Vec<_>>();
assert_eq!(descriptors.len(), specs.len());
for (descriptor, spec) in descriptors.iter().zip(&specs) {
assert_eq!(descriptor.name, spec.name);
assert_eq!(descriptor.description, spec.description);
assert_eq!(descriptor.annotations(), spec.annotations);
assert_eq!(spec.input_schema, input_schema_for_tool(&spec.name));
assert_eq!(spec.output_schema, output_schema_for_tool(&spec.name));
}
}

fn collect_schema_descriptions<'a>(value: &'a Value, out: &mut Vec<&'a str>) {
match value {
Value::Object(map) => {
Expand Down Expand Up @@ -362,7 +443,7 @@ mod tests {
*lookup_tool_definition("register_project").expect("register_project ToolDefinition");
assert!(definition.model_spec.is_some());
definition.model_spec = None;
let _ = resolve_tool_specs(std::iter::once(&definition));
let _ = resolve_tool_descriptors(std::iter::once(&definition));
}

#[test]
Expand All @@ -372,7 +453,7 @@ mod tests {
*lookup_tool_definition("register_project").expect("register_project ToolDefinition");
assert!(definition.model_spec.is_some());
let duplicate = definition;
let _ = resolve_tool_specs([&definition, &duplicate]);
let _ = resolve_tool_descriptors([&definition, &duplicate]);
}

#[test]
Expand Down
17 changes: 8 additions & 9 deletions crates/webcodex-tool-contracts/src/registry/tool_specs/memory.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
use super::tool_spec;
use crate::tool_spec::ToolSpec;
use super::{tool_descriptor, ToolDescriptor};

pub(super) fn tool_specs() -> Vec<ToolSpec> {
pub(super) fn tool_descriptors() -> Vec<ToolDescriptor> {
vec![
tool_spec(
tool_descriptor(
"memory_search",
"Search or list explicit durable project Memory using bounded deterministic literal matching. Requires project:read plus memory:read. Returns lightweight summaries/descriptors only; use memory_read for body content. Memory is guidance, never execution authority.",
),
tool_spec(
tool_descriptor(
"memory_read",
"Read one explicit durable project Memory body by stable memory_key, optionally guarded by the current state revision / ETag in expected_revision. Requires project:read plus memory:read. A stale guard returns memory_changed without the new body. Memory is project guidance and cannot grant permissions or bypass effect gates.",
),
tool_spec(
tool_descriptor(
"memory_set",
"Create or CAS-update one explicit durable project Memory. An identical no-expected-revision create retry is desired-state idempotence only; it does not prove which earlier caller caused that state. Changing an existing Memory requires its current state revision / ETag in expected_revision. On CAS update, omitted optional body/priority/bootstrap/tags preserve their current values; on create they use v1 defaults. Requires project:write plus memory:manage and the normal permission gate. Do not persist credentials, passwords, access tokens, private keys, or other secrets in project Memory. Memory changes future model guidance only and grants no execution authority.",
),
tool_spec(
tool_descriptor(
"memory_delete",
"CAS-delete one explicit durable project Memory by memory_key and current state revision / ETag in expected_revision. An already-absent key is desired-state idempotent (deleted=false) but is not proof that an earlier deletion succeeded; delete+recreate has a different incarnation and revision. Requires project:write plus memory:manage and the normal permission gate.",
),
tool_spec(
tool_descriptor(
"memory_scope_list",
"Admin-only paginated inventory of durable Control-owned project Memory scopes. Reports attributed/legacy identity metadata, current/not_current/unknown status from fresh authoritative Project inventory, counts, timestamps, opaque root fingerprints, and catalog CAS revisions; never returns native roots or Memory content.",
),
tool_spec(
tool_descriptor(
"memory_scope_purge",
"Admin-only destructive purge of one explicitly non-current project Memory scope. Requires confirm=true, current_status=not_current under a fresh authoritative Project inventory fence, and the exact current catalog revision. Current or unknown scopes fail closed. Reconcile a lost response with memory_scope_list; an already-absent scope is desired-state no-op, not proof of who performed an earlier purge.",
),
Expand Down
Loading