Wymcp.Tool.Actions (Wymcp v0.10.0)

View Source

Answers every question about the actions a tool module declares: the validator chain its Wymcp.Tool.actions/0 declaration runs through, and the action-schema invariant that chain enforces.

The chain runs once per registration. Wymcp.Tool.Registration reads Wymcp.Tool.actions/0 and hands the map it read to check!/2, so the map a registration holds is the map the chain accepted, and every reader — dispatch, Wymcp.Help, Wymcp.Tool.Schema — takes it from the registration and reads its mandatory keys with no check of its own.

What a consumer may write into Wymcp.Tool.actions/0 is not stated here. The format catalogue belongs with the behaviour a consumer implements, in Wymcp.Tool's moduledoc under "Action schema format", and the two moments this chain runs at are listed there under "When schemas are validated". This module states the rule those moments enforce and holds the code enforcing it.

The vocabulary itself also stays with the behaviour: Wymcp.Tool is the one code home for the key list, read through Wymcp.Tool.action_schema_keys/0, and the chain here reads it through that accessor at each use rather than snapshotting it into an attribute of its own — one owner, read at run time, so the two modules carry no compile-time dependency in this direction.

The callback-surface check and the callback-shape check are different layers: the first is Wymcp.CallbackSurface's, and the second stays in Wymcp.Tool, under its invariant there; this chain is the callback-shape check's first part.

The action-schema invariant

Every key in the action-schema vocabulary is validated: a key outside Wymcp.Tool.action_schema_keys/0 is rejected wherever the chain runs (unknown implies rejected), and every key inside it is checked by a validator in that chain which rejects at least a wrong-type value (known implies validated). That rule is the action-schema invariant.

Three clauses, each with its own enforcement:

  • unknown implies rejected — validate_known_keys!/3 subtracts the vocabulary from an action schema's keys and raises on whatever is left, so a misspelling fails at one of the moments in Wymcp.Tool's "When schemas are validated" instead of vanishing silently from help and tools/list.
  • known implies validated — Wymcp.ActionSchemaInvariantTest derives one cell per key from Wymcp.Tool.action_schema_keys/0 and asserts each one rejects a wrong-type value, so a key joining the vocabulary without a validator fails that test rather than shipping unchecked.
  • restated implies pinned — every other statement of the vocabulary is derived from Wymcp.Tool.action_schema_keys/0 or compared against it by a cell in that same test: Wymcp.Tool.action_schema/0's key set, its mandatory/optional split against Wymcp.Tool.mandatory_action_schema_keys/0, and Wymcp.Tool's "Action schema format" catalogue, which must carry a bullet for every key. The published type has no other enforcement — nothing writes @spec — so without that cell the contract Wymcp.Tool publishes and the chain this module runs can disagree while the build stays green.

The invariant is about coverage: a validator satisfies it by rejecting a wrong type. How deep validation reaches is a separate rule — wymcp validates exactly what it interprets. Below an action schema's own keys, the keywords Wymcp.Tool.KeySet walks — "properties", an "items" that is a single schema, and an "additionalProperties" schema — are the surface this chain checks, and the rest of a property schema is JSV's to read: the value gate's root build (Wymcp.Tool.ValueGate), at the same two moments, refuses a schema JSV cannot build. Key type is part of the type: :properties is a map from property name to property schema, so a non-string key is a wrong type there, as a non-binary entry is in :required, and every map the walk visits below it is held to the shape the closure is read from, because that shape decides the closure dispatch enforces and help publishes. What a nested schema may hold is catalogued in Wymcp.Tool's "Action schema format", and the offences the walk reports are listed at Wymcp.Tool.KeySet.schema_offences/1. :defaults is checked against that closure as the unknown-key gate checks a call's data: every key in it, and every key inside a default wherever the walk descends, is a string the object it sits in declares (Wymcp.Tool.KeySet.default_offences/2), so no default merged into data carries a key the gate would refuse from a caller.

A schema that breaks either rule raises one Wymcp.ConfigurationError naming every offence — the first ten, sorted by location, then a count of the rest — one per line, each with its fix; shape offences come first, and :defaults is checked only once there are none, because that check reads a well-formed schema. A location is the map holding the offence, spelled in Elixir access syntax from the action-schema key with every segment inspected — :properties["o"]["items"], or :defaults["opts"] for a key inside a default — so it names the map literal the author edits. A data path names a place in data instead, and has no spelling for a schema under "items", which no array index reaches.

Summary

Functions

Validate every action schema in module and return :ok. Raises Wymcp.ConfigurationError with a descriptive message on the first malformed action — and first of all when Wymcp.Tool.actions/0, or a schema in it, is not a map or is a struct. The configuration refusal names module and actions/0 as the callback at fault.

Functions

validate!(module)

Validate every action schema in module and return :ok. Raises Wymcp.ConfigurationError with a descriptive message on the first malformed action — and first of all when Wymcp.Tool.actions/0, or a schema in it, is not a map or is a struct. The configuration refusal names module and actions/0 as the callback at fault.

The chain the registration moment runs on a tool's actions, reached through Wymcp.Tool.Registration, which makes the one call to Wymcp.Tool.actions/0.