sim_lib_forge/lib.rs
1//! Compiled intent records, BRIDGE lifts, and reusable packet-program lookup.
2//!
3//! `sim-lib-forge` is the FORGE intent compiler: it turns a plain-English task
4//! into a reusable, verified, cached BRIDGE packet-program. It sits on top of the
5//! BRIDGE envelope (`sim-codec-bridge` + `sim-lib-bridge`) and adds the
6//! compile / verify / cache / route layer around it. Nothing here is
7//! agent-specific -- a compiled intent is an ordinary SIM value any caller can
8//! run.
9//!
10//! # Pipeline
11//!
12//! ```text
13//! prose
14//! -> lift `forge_lift_once` | `forge_lift_frontier` (prose -> candidate BridgePacket)
15//! -> type check `assert_return_shape_parses` (a lift MUST type its own output)
16//! -> rx check (BRIDGE structural validation, from sim-lib-bridge)
17//! -> verify `verify_answer` over a `VerifyCatalog` (assertion | judge | evidence)
18//! -> promote `PromotePolicy` (Candidate -> Verified -> Golden)
19//! -> store `IntentLibrary` (named index over the content store)
20//! -> resolve `forge_resolve` (a Golden hit skips the model)
21//! -> route `run_intent_routed` (cheap-first, escalate on failure)
22//! ```
23//!
24//! # Key API
25//!
26//! - Artifact: `CompiledIntent` / `IntentStatus` (name, version, source-prose cid,
27//! packet cid, verifier ids, approval state) and `IntentLibrary`, the named
28//! index that makes reuse a lookup instead of a recompile.
29//! - Lift: `forge_lift_once` (one-shot) and `forge_lift_frontier` (built
30//! part-by-part through the shared BRIDGE `frontier`, each step
31//! flat-grammar-constrained and checked), with `normalize_prose` and
32//! `assert_return_shape_parses`.
33//! - Reuse: `forge_resolve` / `ForgeResolver` / `PromotePolicy` -- a matching
34//! Golden intent is fetched instead of re-lifted.
35//! - Verify: `verify_answer` / `Verifier` / `VerifyCatalog` -- assertion, judge
36//! (a BRIDGE COLLAB vote), and evidence checks, so the checker catches a
37//! well-formed but *wrong* answer, not only a malformed one.
38//! - Route: `run_intent_routed` / `RoutePolicy` -- run a cheap model first and
39//! escalate on verifier failure (the safe model downshift).
40//! - Measure: `run_eval` / `EvalCase` / `standard_eval_corpus` -- accuracy, token
41//! cost, and model-call count across raw / compiled / cached / downshifted arms.
42//! - Grow: `propose_frame` extends the BRIEF frame vocabulary from unmapped
43//! intent; `forge_verb` / `ForgeLib` is the `sim forge` Bootloader verb
44//! (lift -> review the inferred Shape -> promote -> run).
45//!
46//! # Why compile prose at all
47//!
48//! The compiler is itself a model, so a lift is a *candidate*, never trusted on
49//! sight: it is validated by the BRIDGE checker and only promoted to a reusable
50//! Golden once it passes. Precision comes from freezing and typing the contract
51//! (a checked return Shape, owned instruction text, no injection channel); speed
52//! comes from caching the Golden (a hit skips inference entirely) and from
53//! downshifting to a cheaper model behind a checker that actually catches wrong.
54//! The model never gets smarter -- the envelope gets reliable and the artifact
55//! gets reused.
56//!
57//! Named out of scope: semantic prose lookup. `normalize_prose` keys reuse on a
58//! byte-level normal form, not on meaning, so two differently-worded but
59//! equivalent prompts do not share a Golden.
60
61#![forbid(unsafe_code)]
62#![deny(missing_docs)]
63
64mod author;
65mod author_loop;
66mod contract_expr;
67mod contract_query;
68mod contracts;
69mod eval;
70mod eval_author;
71mod eval_author_corpus;
72mod frame_propose;
73mod intent;
74mod library;
75mod lift;
76mod lift_frontier;
77mod normalize;
78mod packet_artifact;
79mod resolve;
80mod route;
81mod shape_infer;
82mod tokens;
83mod verb;
84mod verify;
85
86#[cfg(test)]
87mod author_tests;
88#[cfg(test)]
89mod contract_tests;
90#[cfg(test)]
91mod eval_author_tests;
92#[cfg(test)]
93mod eval_tests;
94#[cfg(test)]
95mod frame_propose_tests;
96#[cfg(test)]
97mod resolve_tests;
98#[cfg(test)]
99mod route_tests;
100#[cfg(test)]
101mod tests;
102#[cfg(test)]
103mod verb_tests;
104#[cfg(test)]
105mod verify_tests;
106
107pub use author::{
108 AuthorTask, CONTRACT_PROJECTION_EXTRA, ContractProjection, ContractProjectionCaps,
109 OUTPUT_GRAMMAR_GRAPH_EXTRA, author_model_request, project_contracts,
110};
111pub use author_loop::{AuthorOutcome, authorized_capabilities, run_author_task};
112pub use contract_expr::{contract_card_from_expr, contract_card_shape};
113pub use contract_query::{
114 ContractDeckCache, ContractQueryReport, RankedContractCard, ShapeQuery, query_contract_deck,
115};
116pub use contracts::{ContractCard, ContractDeck, ContractGap, assemble_contract_deck};
117pub use eval::{
118 ArmMetrics, EvalArm, EvalCase, EvalCassette, EvalPlayback, EvalReport, run_eval,
119 standard_eval_arms, standard_eval_corpus,
120};
121pub use eval_author::{
122 AuthorArm, AuthorArmMetrics, AuthorBenchReport, AuthorCase, run_author_bench,
123 standard_author_arms, standard_author_cases,
124};
125pub use frame_propose::{
126 FrameSpecProposal, approve_frame_proposal, propose_frame, proposed_frame_part,
127};
128pub use intent::{CompiledIntent, IntentStatus};
129pub use library::IntentLibrary;
130pub use lift::{LiftOptions, forge_lift_once};
131pub use lift_frontier::forge_lift_frontier;
132pub use normalize::normalize_prose;
133pub use packet_artifact::store_packet_artifact;
134pub use resolve::{ForgeResolver, PromotePolicy, forge_resolve, forge_resolve_with_options};
135pub use route::{
136 RouteAttempt, RouteAttemptStatus, RoutePolicy, RouteProvenance, RouteTarget, RoutedAnswer,
137 run_intent_routed, run_intent_routed_report,
138};
139pub use shape_infer::assert_return_shape_parses;
140pub use tokens::{estimate_prompt_tokens, semantic_tokens};
141pub use verb::{ForgeLib, forge_entrypoint_symbol, forge_verb};
142pub use verify::{
143 ProbeOracle, Verifier, VerifyCatalog, VerifyFailure, VerifyProbe, VerifyReport, verify_answer,
144};
145
146/// Cookbook recipes embedded from this crate's `recipes/` directory.
147pub static RECIPES: sim_cookbook::EmbeddedDir =
148 include!(concat!(env!("OUT_DIR"), "/cookbook_recipes.rs"));