On this page

0320. Automatic Purity & Authority Badges and --assert-pure / --assert-total Enforcement

Context

Tenon already models direct external effects and transitive call-chain witnesses in crates/tenon/src/authority/ (--report=authority) and statically proves eliminated runtime value checks in tenon_compiler::checks (--report=checks and checkfree fn). However, three gaps remained for developers and coding agents:

  1. checkfree fn callee loophole: checkfree fn only verifies that no unproved runtime checks remain inside the annotated function's own body; calling a helper with an unproved divide, bounds, overflow, slice, or shift check silently bypassed total-function intent.
  2. No structured authority report or automatic API purity badges: --report=authority emitted text only, tenon doc --card reported capability names without a clean pure/effectful + total classification, and tenon outline --json omitted function purity and capability parameter metadata.
  3. No CI/agent enforcement flags: There was no CLI mechanism on tenon check to fail closed when a designated function accidentally gains an external effect or a transitive runtime trap check.

Decision

  1. Rich Static Purity, Authority & Transitive Totality Summary (crates/tenon/src/authority/):

    • Added authority::Summary and FnAuthority over authority::collect + authority::reach + tenon_compiler::checks.
    • For each materialized function, computes:
      • effects: BTreeSet<String> (all reachable modeled effect categories plus unknown.extern),
      • is_pure: bool (effects.is_empty()),
      • local_checks: usize (unproved check sites in the function body),
      • is_total: bool (transitively zero remaining runtime checks across the function and every transitive callee, computed via deterministic reverse-BFS shortest-path reachability),
      • deterministic call-chain witnesses for reachable effects and check_witness for remaining runtime checks.
    • Added structured JSON output (schema: "tenon-authority-1") when --report=authority is combined with --json or --diagnostics=json, while keeping existing human --report=authority output byte-for-byte backward-compatible.
  2. Automatic Purity/Effect Badges in tenon doc --card and tenon outline:

    • Extended doc_card::ApiItem with purity ("pure" | "effectful"), total: bool, and effects: Vec<String>.
    • Lowered entry files use authority::summarize for exact transitive effects and totality; non-entry modules infer effects from capability parameters, local capability-bearing types, and transitive extern calls, and infer conservative local totality from transitive checkfree declarations.
    • Text cards render compact badges ([pure], [pure, total], [effects: fs, clock], [total, effects: ...]) within existing token budgets; JSON cards include "purity", "total", and "effects" on every fn item.
    • tenon outline --json emits "purity" ("pure" | "effectful") and "capabilities" (sorted capability parameter type names, when non-empty) on fn entries while preserving human outline output unchanged.
  3. CLI Purity & Totality Enforcement (crates/tenon/src/purity_check.rs):

    • Added repeatable --assert-pure <fn> and --assert-total <fn> flags to tenon check (supporting both --flag <fn> and --flag=<fn>).
    • Emits deterministic diagnostics in human or --json format and exits with status 1 on failure:
      • E1250: Function asserted pure reaches an external effect, with exact function span and shortest call-chain witness naming the reachable effect and terminal operation.
      • E1251: Function asserted total has a remaining runtime check (in self or a transitive callee), with exact function span and shortest call-chain witness naming the check kind and terminal location.
    • Emits a clear usage error (exit 2) if the named function is not found in the checked program.

Consequences