Skip to main content

Module rule_render

Module rule_render 

Source
Expand description

Render a mined/remembered rule as a concrete code-spec instead of a free-prose blob (roadmap item ⑥).

The output is a single slot-based Markdown template with progressive disclosure: a section is emitted only when its source data exists, and a missing slot is silently dropped rather than rendered as “N/A” (an empty placeholder reads as fabricated emptiness). Every rendered slot traces to a field we already store or a stored rule_example — nothing here invents a contract, a validation condition, a trigger, or a metric. The mapping is pure, deterministic string reshaping with no LLM call and no new persisted data.

This module is intentionally public and DB-free so item ① (rule packs) can import the same render_* helpers and serialize a published pack in the exact format a locally-recalled rule renders in — see the roadmap’s “missing-field decision table” which both surfaces honor.

The MCP get_rules detail path drives this via [crate::mcp_server::tools::util::render_full_rule_with_examples], which builds a RuleRenderInput from its private SkillDetailRow and calls render_code_spec. The PostToolUse hook keeps its own compact render (serve_render::render_rule_block) by design — it renders from the indexed body string, not a row, and enforces a tight per-injection token budget.

Structs§

RuleRenderInput
Borrowed, DB-free view of the fields a renderer needs. Built from the MCP layer’s private SkillDetailRow (which can’t leak out of mcp_server) and — for item ① — from a pack row, so both surfaces render identically.

Enums§

Polarity
Whether a directive states a positive obligation (MUST) or a prohibition (AVOID). Cheap keyword classify; defaults to Must when ambiguous.

Functions§

directive_polarity
Classify a directive statement as a positive obligation or a prohibition by scanning for negative-polarity keywords. Pure and unit-testable; ambiguous statements default to Polarity::Must.
render_cases_block
### Cases block, or None when there are no examples. Framed as a conformance test (❌ Counter-example / ✅ Conforming) rather than decoration so the agent reads the pair as the acceptance criterion.
render_code_spec
Render a full rule as the §3 code-spec template. Pure and DB-free: the caller supplies a RuleRenderInput; every section is derived deterministically from those fields with progressive disclosure.
render_contract_block
### Contract block. Always present (the verbatim-description fallback guarantees body text is never dropped). For mined rules the Rule: statement becomes a single MUST:/AVOID: obligation; for verb-led conversation rules the first sentence becomes the obligation and the rest becomes a Rationale: sub-line (the WHY is the value, so it is kept).
render_provenance_block
### Provenance block. Reuses the candidate source-proof parser to pull the PR Source:, the Comment: URL and a short reviewer excerpt out of a mined description. Returns None only when there is neither parseable source proof nor a source_repo — the header’s ← learned from segment already carries the top-level attribution, so a conversation rule with no proof omits this section rather than repeating the header.
render_validation_matrix
### Validation / Error matrix block, or None when no row is derivable.