System architecture

Internal package layout, wire routing rules, sensor enrichment math, and transport modes in what-if.

what-if is organized into pure, testable Go packages under internal/:

Package Responsibility
internal/config Precedence loader (defaults, TOML, env, flags), unified tier/model resolver, secret redaction
internal/auth OAuth 2.1 Resource Server verifier (RFC 8414/OIDC discovery, asymmetric JWKS/JWT, RFC 7662 introspection, RFC 9728 metadata)
internal/media Root-contained file resolution, magic-byte sniffer, bounded ffmpeg PCM WAV audio transcoder
internal/wire HTTP client with 429/529 retry backoff, 16 MiB cap, and adapters for System One, OpenAI Decisions, OpenRouter, Liquid, Rerank, and Models
internal/decide Pre-flight validation, bounded parallel items and what_if fan-out, JSON merge patches, top2_margin, and min_confidence abstention
internal/mcpserver stdio and Streamable HTTP (/_/mcp) MCP server, Skills over MCP (io.modelcontextprotocol/skills), embedded instructions, and dynamic locked-tier schemas
internal/cli Cobra commands (mcp, serve, ask, compare, rerank, models, doctor, version)
skills Embedded what-if Agent Skill (skill://what-if/SKILL.md) compiled into the binary via Go embed.FS

Wire selection and portability

When wire is set to auto, what-if selects the standard upstream endpoint based on payload features:

flowchart TD
  Req["Normalized Ask"] --> Dialect{"Configured dialect?"}
  Dialect -->|"openrouter"| OR["OpenRouter (/api/v1/systemone or /api/alpha/decisions)"]
  Dialect -->|"liquid"| LQ["Liquid (/decisions/v1/systemone)"]
  Dialect -->|"openai"| OAI["OpenAI (/v1/decisions)"]
  Dialect -->|"auto or systemone"| Check{"cache_key set
and text/image only?"} Check -->|"Yes and auto"| OAI Check -->|"No"| S1["System One (POST /v1/systemone)"]

All requests send only standard Authorization: Bearer <key> and Content-Type: application/json. Roundhouse-specific diagnostics (x-roundhouse-request-id, x-roundhouse-warning, x-roundhouse-dropped-fields) are read opportunistically from response headers without requiring custom request headers.

Sensor enrichment math

Primitive Reported fields Formula
noul noul, confidence, uncertain confidence = |2 * noul - 1|
choice choice, confidence, top2_margin, uncertain, probabilities top2_margin = p1 - p2; fallback confidence = (N * p1 - 1) / (N - 1)
score score, confidence, legend, uncertain, probabilities Weighted index sum(i * p_i) over ordered levels 0..L-1
span spans[] (label, text, start, end, score) Opt-in via enable_spans; Unicode code point offsets

Abstention precedence: question min_confidence overrides call min_confidence, which overrides the resolved tier min_confidence. If x-roundhouse-warning contains uncalibrated, calibrated is set to false in the tool response.

Skills over MCP

During initialize, internal/mcpserver declares both resources and extensions["io.modelcontextprotocol/skills"] = {"directoryRead": true} (SEP-2640). Clients inspect frontmatter and SHA-256 content digests via skills/list or skills/get before lazily fetching skill://what-if/SKILL.md via resources/read (or browsing skill://what-if via resources/directory/read), avoiding context bloat unless an agent invokes the skill.

HTTP authentication and caller key pass-through

When what-if serve runs over HTTP, inbound authentication and outbound caller key pass-through compose independently:

  1. Static bearer (server_token): Constant-time bearer comparison for headless automation and reverse-proxy deployments.
  2. OAuth 2.1 Resource Server ([auth]): Publishes RFC 9728 metadata at /.well-known/oauth-protected-resource and /.well-known/oauth-protected-resource/_/mcp, verifies asymmetric JWS tokens (RS*, PS*, ES*, EdDSA) via JWKS (rejecting alg:none and HS*), introspects opaque tokens via RFC 7662 without falling back from invalid JWS signatures, and binds Streamable HTTP sessions to TokenInfo.UserID. Static server_token remains accepted alongside OAuth when both are configured.
  3. Caller key pass-through (auth.passthrough): Reads the caller's upstream key from req.Extra.Header inside MCP tool handlers (X-Upstream-API-Key when combined with static or OAuth inbound auth, Authorization: Bearer when standalone) and forwards it only to requests targeting the default base_url, never to cross-origin tier backends, while scrubbing passed-through secrets from returned errors.

Connections

Local graph

Linked from