agentic examples

The Schema Export

The Schema Export: a capability contract emitted as draft-07 JSON Schema (new this round), then PROVEN faithful - the same payloads are judged by Agentic's validator and by an independent interpreter reading only the exported document. Any disagreement means the projection lies. 200 seeded payloads: zero disagreements.

Data & Pipelines Round 7 Piotr Solnica exit 0

source on github

bundle exec ruby examples/json_schema_export.rb

a real captured run

THE EXPORTED SCHEMA
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "book_shipment inputs",
    "type": "object",
    "required": [
      "mode",
      "weight_kg",
      "reference"
    ],
    "properties": {
      "mode": {
        "type": "string",
        "enum": [
          "air",
          "sea",
          "road"
        ]
      },
      "weight_kg": {
        "type": "number",
        "minimum": 1,
        "maximum": 30000
      },
      "reference": {
        "type": "string",
        "minLength": 1
      },
      "fragile": {
        "type": "boolean"
      },
      "tags": {
        "type": "array",
        "minItems": 1
      }
    },
    "additionalProperties": true
  }

THE AGREEMENT PROOF (200 seeded payloads, 44 valid)
  the exported schema and the live validator agreed on every payload.
  the projection is faithful: what the JSON document promises is
  exactly what the boundary enforces.

source

# frozen_string_literal: true

# The Schema Export: a capability contract emitted as draft-07 JSON
# Schema (new this round), then PROVEN faithful - the same payloads
# are judged by Agentic's validator and by an independent interpreter
# reading only the exported document. Any disagreement means the
# projection lies. 200 seeded payloads: zero disagreements.
#
#   bundle exec ruby examples/json_schema_export.rb [seed]
#
# Runs offline; prints the schema and the agreement score.

require class="s">"bundler/setup"
require class="s">"agentic"
require class="s">"json"

seed = (ARGV.first || 20260707).to_i
rng = Random.new(seed)

spec = Agentic:class="y">:CapabilitySpecification.new(
  name: class="s">"book_shipment",
  description: class="s">"Book a shipment",
  version: class="s">"1.0.0",
  inputs: {
    mode: {type: class="s">"string", required: true, enum: %w[air sea road]},
    weight_kg: {type: class="s">"number", required: true, min: 1, max: 30_000},
    reference: {type: class="s">"string", required: true, non_empty: true},
    fragile: {type: class="s">"boolean"},
    tags: {type: class="s">"array", non_empty: true}
  }
)

schema = spec.to_json_schema

puts class="s">"THE EXPORTED SCHEMA"
puts JSON.pretty_generate(schema).gsub(/^/, class="s">"  ")
puts

# --- an independent interpreter that knows ONLY the JSON document ------------
def schema_accepts?(schema, payload)
  data = payload.transform_keys(&class="y">:to_s)

  return false unless schema[class="s">"required"].all? { |key| data.key?(key) }

  schema[class="s">"properties"].all? do |key, rules|
    next true unless data.key?(key)

    value = data[key]
    type_ok = case rules[class="s">"type"]
    when class="s">"string" then value.is_a?(String)
    when class="s">"number" then value.is_a?(Numeric)
    when class="s">"boolean" then value == true || value == false
    when class="s">"array" then value.is_a?(Array)
    else true
    end

    type_ok &&
      (rules[class="s">"enum"].nil? || rules[class="s">"enum"].include?(value)) &&
      (rules[class="s">"minimum"].nil? || (value.is_a?(Numeric) && value >= rules[class="s">"minimum"])) &&
      (rules[class="s">"maximum"].nil? || (value.is_a?(Numeric) && value <= rules[class="s">"maximum"])) &&
      (rules[class="s">"minLength"].nil? || (value.respond_to?(class="y">:length) && value.length >= rules[class="s">"minLength"])) &&
      (rules[class="s">"minItems"].nil? || (value.is_a?(Array) && value.size >= rules[class="s">"minItems"]))
  end
end

# --- generate payloads that wander in and out of validity --------------------
def valid_payload(rng)
  {
    mode: %w[air sea road].sample(random: rng),
    weight_kg: rng.rand(1..30_000),
    reference: class="s">"REF-#{rng.rand(1000)}",
    fragile: [true, false].sample(random: rng)
  }
end

def corrupted_payload(rng)
  payload = valid_payload(rng)
  if rng.rand < 0.6
    corruptions = {
      mode: [class="s">"teleport", 7], weight_kg: [0, class="s">"heavy", 50_000],
      reference: [class="s">""], fragile: [class="s">"yes"], tags: [[]]
    }
    field = corruptions.keys.sample(random: rng)
    payload[field] = corruptions[field].sample(random: rng)
  end
  payload
end

def chaos_payload(rng)
  payload = {}
  payload[class="y">:mode] = [class="s">"air", class="s">"sea", class="s">"teleport", 7].sample(random: rng) if rng.rand < 0.8
  payload[class="y">:weight_kg] = [rng.rand(1..30_000), -4, class="s">"heavy"].sample(random: rng) if rng.rand < 0.8
  payload[class="y">:reference] = [class="s">"REF-#{rng.rand(1000)}", class="s">""].sample(random: rng) if rng.rand < 0.8
  payload[class="y">:tags] = [[class="s">"a"], []].sample(random: rng) if rng.rand < 0.4
  payload
end

def random_payload(rng)
  # Half start valid and get one field corrupted (or not); half are chaos
  (rng.rand < 0.5) ? corrupted_payload(rng) : chaos_payload(rng)
end

validator = Agentic:class="y">:CapabilityValidator.new(spec)
trials = 200
disagreements = []
accepted = 0

trials.times do
  payload = random_payload(rng)

  agentic_verdict = begin
    validator.validate_inputs!(payload)
    true
  rescue Agentic:class="y">:Errors:class="y">:ValidationError
    false
  end

  schema_verdict = schema_accepts?(schema, payload)
  accepted += 1 if agentic_verdict
  disagreements << payload if agentic_verdict != schema_verdict
end

puts class="s">"THE AGREEMENT PROOF (#{trials} seeded payloads, #{accepted} valid)"
if disagreements.empty?
  puts class="s">"  the exported schema and the live validator agreed on every payload."
  puts class="s">"  the projection is faithful: what the JSON document promises is"
  puts class="s">"  exactly what the boundary enforces."
else
  puts class="s">"  DISAGREEMENTS (the export lies about the contract):"
  disagreements.first(5).each { |payload| puts class="s">"  - #{payload.inspect}" }
  exit 1
end