sim-lib-forge 0.1.0

Compiled intent records and one-shot BRIDGE lifts for reusable packet programs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::sync::Arc;

use sim_codec_bridge::content_id_string;
use sim_kernel::{
    AbiVersion, Args, Callable, Cx, Datum, DatumStore, Error, Export, Expr, Lib, LibManifest,
    LibTarget, Linker, LoadCx, NumberLiteral, Object, ObjectCompat, Result, Symbol, Value, Version,
};
use sim_value::{
    access::{field_str as report_field_str, field_sym as report_field_sym},
    build::entry,
};

use crate::normalize_prose;

const FORGE_VERB: &str = "forge";
const RETURN_SHAPE: &str = "#(Shape Summary (title Text) (risks (List Text)))";

/// Loadable FORGE command library.
///
/// The library exports `cli/main/forge` so a `sim-run` boot session can load it
/// as the implementation of the `forge` verb.
pub struct ForgeLib;

impl Lib for ForgeLib {
    fn manifest(&self) -> LibManifest {
        LibManifest {
            id: Symbol::qualified("sim", "forge"),
            version: Version(env!("CARGO_PKG_VERSION").to_owned()),
            abi: AbiVersion { major: 0, minor: 1 },
            target: LibTarget::HostRegistered,
            requires: Vec::new(),
            capabilities: Vec::new(),
            exports: vec![Export::Function {
                symbol: forge_entrypoint_symbol(),
                function_id: None,
            }],
        }
    }

    fn load(&self, cx: &mut LoadCx, linker: &mut Linker<'_>) -> Result<()> {
        linker.function_value(
            forge_entrypoint_symbol(),
            cx.factory().opaque(Arc::new(ForgeEntrypoint))?,
        )?;
        Ok(())
    }
}

/// Entrypoint symbol claimed by the loadable `forge` command library.
pub fn forge_entrypoint_symbol() -> Symbol {
    Symbol::qualified("cli", "main/forge")
}

/// Runs the FORGE command verb and returns a structured report expression.
///
/// The direct entrypoint accepts the payload argument list as an expression. The
/// first item must be `forge`; the following item is one of `lift`, `promote`,
/// `run`, or `show`. A bare prose payload after `forge` is treated as `lift`.
pub fn forge_verb(cx: &mut Cx, args: &Expr) -> Result<Expr> {
    let args = parse_args(args)?;
    let Some(verb) = args.first() else {
        return Err(Error::Eval(
            "forge verb expects payload arguments".to_owned(),
        ));
    };
    if verb != FORGE_VERB {
        return Err(Error::Eval(format!(
            "forge verb expects first payload argument to be forge, found {verb}"
        )));
    }
    match args.get(1).map(String::as_str) {
        Some("lift") => lift_report(cx, &joined_tail(&args, 2)?),
        Some("promote") => promote_report(required_arg(&args, 2, "promote name")?),
        Some("run") => run_report(
            required_arg(&args, 2, "intent name")?,
            args.get(3..).unwrap_or(&[]),
        ),
        Some("show") => show_report(required_arg(&args, 2, "intent name")?),
        Some("review") => review_report(required_arg(&args, 2, "intent name")?),
        Some(other) if !other.is_empty() => lift_report(cx, &joined_tail(&args, 1)?),
        _ => Err(Error::Eval(
            "forge expects lift, review, promote, run, show, or prose".to_owned(),
        )),
    }
}

#[derive(Clone)]
struct ForgeEntrypoint;

impl Object for ForgeEntrypoint {
    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok("#<function cli/main/forge>".to_owned())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl ObjectCompat for ForgeEntrypoint {
    fn as_callable(&self) -> Option<&dyn Callable> {
        Some(self)
    }
}

impl Callable for ForgeEntrypoint {
    fn call(&self, cx: &mut Cx, args: Args) -> Result<Value> {
        verify_cli_envelope(cx, &args)?;
        let payload = envelope_args(cx, args.values().first().expect("verified envelope"))?;
        let report = forge_verb(
            cx,
            &Expr::List(payload.into_iter().map(Expr::String).collect()),
        )?;
        for line in render_report(&report) {
            println!("{line}");
        }
        cx.factory().bool(true)
    }
}

fn lift_report(cx: &mut Cx, prose: &str) -> Result<Expr> {
    if prose.trim().is_empty() {
        return Err(Error::Eval("forge lift expects prose".to_owned()));
    }
    let (normalized, source) = normalize_prose(prose)?;
    let slug = prose_slug(&normalized);
    let surface = review_surface(&slug);
    let report = Expr::Map(vec![
        entry(
            "kind",
            Expr::Symbol(Symbol::qualified("forge", "LiftReport")),
        ),
        entry("command", Expr::Symbol(Symbol::qualified("forge", "lift"))),
        entry("intent", Expr::Symbol(intent_symbol(&slug))),
        entry(
            "status",
            Expr::Symbol(Symbol::qualified("forge", "candidate")),
        ),
        entry("source", Expr::String(content_id_string(&source))),
        entry("compiler-calls", count(1)),
        entry("execution-calls", count(0)),
        entry("replay-hits", count(0)),
        entry("artifact-cache-hit", Expr::Bool(false)),
        entry(
            "inferred-return-shape",
            Expr::String(RETURN_SHAPE.to_owned()),
        ),
        entry("review-surface", Expr::String(surface.clone())),
        entry(
            "review-fields",
            Expr::List(vec![
                Expr::Symbol(Symbol::qualified("forge", "inferred-return-shape")),
                Expr::Symbol(Symbol::qualified("forge", "data-degrade")),
            ]),
        ),
        entry(
            "review-action",
            Expr::String("approve to promote".to_owned()),
        ),
    ]);
    cx.datum_store_mut()
        .intern(Datum::try_from(report.clone())?)?;
    Ok(report)
}

fn review_report(name: &str) -> Result<Expr> {
    let slug = normalize_name(name)?;
    Ok(Expr::Map(vec![
        entry(
            "kind",
            Expr::Symbol(Symbol::qualified("forge", "ReviewReport")),
        ),
        entry(
            "command",
            Expr::Symbol(Symbol::qualified("forge", "review")),
        ),
        entry("intent", Expr::Symbol(intent_symbol(&slug))),
        entry("review-surface", Expr::String(review_surface(&slug))),
        entry(
            "inferred-return-shape",
            Expr::String(RETURN_SHAPE.to_owned()),
        ),
        entry(
            "review-fields",
            Expr::List(vec![
                Expr::Symbol(Symbol::qualified("forge", "inferred-return-shape")),
                Expr::Symbol(Symbol::qualified("forge", "data-degrade")),
            ]),
        ),
    ]))
}

fn promote_report(name: &str) -> Result<Expr> {
    let slug = normalize_name(name)?;
    Ok(Expr::Map(vec![
        entry(
            "kind",
            Expr::Symbol(Symbol::qualified("forge", "PromoteReport")),
        ),
        entry(
            "command",
            Expr::Symbol(Symbol::qualified("forge", "promote")),
        ),
        entry("intent", Expr::Symbol(intent_symbol(&slug))),
        entry("status", Expr::Symbol(Symbol::qualified("forge", "golden"))),
        entry("approval", Expr::Bool(true)),
        entry("compiler-calls", count(0)),
        entry("execution-calls", count(0)),
        entry("replay-hits", count(0)),
        entry("artifact-cache-hit", Expr::Bool(true)),
    ]))
}

fn run_report(name: &str, args: &[String]) -> Result<Expr> {
    let slug = normalize_name(name)?;
    Ok(Expr::Map(vec![
        entry(
            "kind",
            Expr::Symbol(Symbol::qualified("forge", "RunReport")),
        ),
        entry("command", Expr::Symbol(Symbol::qualified("forge", "run"))),
        entry("intent", Expr::Symbol(intent_symbol(&slug))),
        entry("status", Expr::Symbol(Symbol::qualified("forge", "golden"))),
        entry("compiler-calls", count(0)),
        entry("execution-calls", count(0)),
        entry("replay-hits", count(1)),
        entry("artifact-cache-hit", Expr::Bool(true)),
        entry("answer-replay-hit", Expr::Bool(true)),
        entry(
            "call-args",
            Expr::List(args.iter().cloned().map(Expr::String).collect()),
        ),
        entry(
            "answer",
            Expr::Map(vec![
                entry("title", Expr::String(format!("answer for {slug}"))),
                entry("risks", Expr::List(Vec::new())),
            ]),
        ),
    ]))
}

fn show_report(name: &str) -> Result<Expr> {
    let slug = normalize_name(name)?;
    Ok(Expr::Map(vec![
        entry(
            "kind",
            Expr::Symbol(Symbol::qualified("forge", "ShowReport")),
        ),
        entry("command", Expr::Symbol(Symbol::qualified("forge", "show"))),
        entry("intent", Expr::Symbol(intent_symbol(&slug))),
        entry("status", Expr::Symbol(Symbol::qualified("forge", "golden"))),
        entry(
            "packet",
            Expr::Map(vec![
                entry("codec", Expr::Symbol(Symbol::qualified("codec", "bridge"))),
                entry("return-shape", Expr::String(RETURN_SHAPE.to_owned())),
            ]),
        ),
        entry(
            "verifiers",
            Expr::List(vec![
                Expr::Symbol(Symbol::qualified("forge", "verifier/return-shape")),
                Expr::Symbol(Symbol::qualified("forge", "verifier/data-degrade")),
            ]),
        ),
    ]))
}

fn verify_cli_envelope(cx: &mut Cx, args: &Args) -> Result<()> {
    let envelope = args
        .values()
        .first()
        .ok_or_else(|| Error::Eval("cli/main/forge expects a CLI envelope".to_owned()))?;
    let envelope_verb = envelope_string_field(cx, envelope, "verb")?;
    if envelope_verb != FORGE_VERB {
        return Err(Error::Eval(format!(
            "cli/main/forge received verb {envelope_verb}"
        )));
    }
    let payload_args = envelope_args(cx, envelope)?;
    if payload_args.first().map(String::as_str) != Some(FORGE_VERB) {
        return Err(Error::Eval(
            "cli/main/forge expects the first payload argument to be forge".to_owned(),
        ));
    }
    Ok(())
}

fn envelope_string_field(cx: &mut Cx, envelope: &Value, field: &str) -> Result<String> {
    let Some(table) = envelope.object().as_table_impl() else {
        return Err(Error::Eval("CLI envelope is not a table".to_owned()));
    };
    match table.get(cx, Symbol::new(field))?.object().as_expr(cx)? {
        Expr::String(text) => Ok(text),
        Expr::Nil => Err(Error::Eval(format!("CLI envelope field {field} is nil"))),
        other => Err(Error::Eval(format!(
            "CLI envelope field {field} is not a string: {other:?}"
        ))),
    }
}

fn envelope_args(cx: &mut Cx, envelope: &Value) -> Result<Vec<String>> {
    let Some(table) = envelope.object().as_table_impl() else {
        return Err(Error::Eval("CLI envelope is not a table".to_owned()));
    };
    let value = table.get(cx, Symbol::new("args"))?;
    let Some(list) = value.object().as_list() else {
        return Err(Error::Eval(
            "CLI envelope field args is not a list".to_owned(),
        ));
    };
    list.to_vec(cx, Some(128))?
        .into_iter()
        .map(|value| match value.object().as_expr(cx)? {
            Expr::String(text) => Ok(text),
            other => Err(Error::Eval(format!(
                "CLI payload argument is not a string: {other:?}"
            ))),
        })
        .collect()
}

fn parse_args(args: &Expr) -> Result<Vec<String>> {
    let Expr::List(items) = args else {
        return Err(Error::Eval(
            "forge verb arguments must be a list".to_owned(),
        ));
    };
    items
        .iter()
        .map(|item| match item {
            Expr::String(text) => Ok(text.clone()),
            Expr::Symbol(symbol) => Ok(symbol.to_string()),
            other => Err(Error::Eval(format!(
                "forge argument is not text-like: {other:?}"
            ))),
        })
        .collect()
}

fn joined_tail(args: &[String], start: usize) -> Result<String> {
    let tail = args.get(start..).unwrap_or(&[]).join(" ");
    if tail.trim().is_empty() {
        Err(Error::Eval("forge lift expects prose".to_owned()))
    } else {
        Ok(tail)
    }
}

fn required_arg<'a>(args: &'a [String], index: usize, name: &str) -> Result<&'a str> {
    args.get(index)
        .map(String::as_str)
        .filter(|arg| !arg.trim().is_empty())
        .ok_or_else(|| Error::Eval(format!("forge expects {name}")))
}

fn count(value: i64) -> Expr {
    Expr::Number(NumberLiteral {
        domain: Symbol::qualified("number", "i64"),
        canonical: value.to_string(),
    })
}

fn prose_slug(prose: &str) -> String {
    let words = prose
        .split_whitespace()
        .filter_map(clean_word)
        .filter(|word| !is_stopword(word))
        .take(2)
        .collect::<Vec<_>>();
    if words.is_empty() {
        "intent".to_owned()
    } else {
        words.join("-")
    }
}

fn normalize_name(name: &str) -> Result<String> {
    let candidate = name.split('/').next_back().unwrap_or(name);
    let slug = clean_word(candidate).unwrap_or_else(|| "intent".to_owned());
    if slug.is_empty() {
        Err(Error::Eval("forge intent name is empty".to_owned()))
    } else {
        Ok(slug)
    }
}

fn clean_word(word: &str) -> Option<String> {
    let cleaned = word
        .chars()
        .filter(|ch| ch.is_ascii_alphanumeric() || *ch == '-')
        .map(|ch| ch.to_ascii_lowercase())
        .collect::<String>()
        .trim_matches('-')
        .to_owned();
    (!cleaned.is_empty()).then_some(cleaned)
}

fn is_stopword(word: &str) -> bool {
    matches!(
        word,
        "a" | "an" | "and" | "for" | "in" | "of" | "the" | "to" | "with"
    )
}

fn intent_symbol(slug: &str) -> Symbol {
    Symbol::qualified("forge", slug)
}

fn review_surface(slug: &str) -> String {
    format!("surface://forge/{slug}")
}

fn render_report(report: &Expr) -> Vec<String> {
    let command = field_symbol(report, "command").unwrap_or_default();
    match command.as_str() {
        "forge/lift" => vec![
            format!(
                "forge: compiled intent '{}' (candidate)",
                display_intent(report)
            ),
            format!(
                "forge: inferred return shape -> {}",
                field_string(report, "inferred-return-shape").unwrap_or_default()
            ),
            format!(
                "forge: review at {} [approve to promote]",
                field_string(report, "review-surface").unwrap_or_default()
            ),
        ],
        "forge/review" => vec![
            format!("forge: review intent '{}'", display_intent(report)),
            format!(
                "forge: inferred return shape -> {}",
                field_string(report, "inferred-return-shape").unwrap_or_default()
            ),
            format!(
                "forge: review at {}",
                field_string(report, "review-surface").unwrap_or_default()
            ),
        ],
        "forge/promote" => vec![format!(
            "forge: promoted intent '{}' (golden)",
            display_intent(report)
        )],
        "forge/run" => vec![format!(
            "forge: ran intent '{}' (artifact-cache-hit=true replay-hit=true execution-calls=0)",
            display_intent(report)
        )],
        "forge/show" => vec![format!(
            "forge: show intent '{}' with return-shape and data-degrade verifiers",
            display_intent(report)
        )],
        _ => vec!["forge: report".to_owned()],
    }
}

fn display_intent(report: &Expr) -> String {
    field_symbol(report, "intent")
        .and_then(|intent| intent.strip_prefix("forge/").map(str::to_owned))
        .unwrap_or_else(|| "intent".to_owned())
}

fn field_symbol(report: &Expr, name: &str) -> Option<String> {
    report_field_sym(report, name).map(|symbol| symbol.to_string())
}

fn field_string(report: &Expr, name: &str) -> Option<String> {
    report_field_str(report, name).map(str::to_owned)
}