prosaic-core 1.0.1

General-purpose natural language generation from structured data
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
//! End-to-end integration tests for the retrospective refine pass.
//!
//! Verifies the iteration controller, constraint application, and
//! `DocumentPlan::render` dispatch operate correctly across realistic
//! corpora. Each test pins one observable property of the refine loop
//! so regressions in any layer surface here.

use prosaic_core::{
    Context, Diagnoser, Diagnostic, DocumentPlan, Engine, LengthDistribution,
    ParagraphOpenerMonotony, RefineConfig, RefineConstraint, RenderedDocument, Salience,
    SalienceBias, Session, Strictness, StyleProfile, Value, Variation,
};
use prosaic_grammar_en::English;
use std::sync::Arc;

fn engine(refine: bool) -> Engine {
    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Fixed);
    if refine {
        e = e.refine(RefineConfig::balanced().with_max_iterations(3));
    }
    e.register_template_at("evt.touched", "{name|refer} was touched", Salience::Medium)
        .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was modified",
        Salience::Medium,
    )
    .unwrap();
    e.register_template_at(
        "evt.renamed",
        "{name|refer} was renamed to {new_name}",
        Salience::Medium,
    )
    .unwrap();
    e
}

fn ctx_named(name: &str) -> Context {
    let mut c = Context::new();
    c.insert("name", Value::String(name.into()));
    c.insert("entity_type", Value::String("class".into()));
    c
}

fn ctx_renamed(old: &str, new: &str) -> Context {
    let mut c = Context::new();
    c.insert("name", Value::String(old.into()));
    c.insert("new_name", Value::String(new.into()));
    c.insert("entity_type", Value::String("class".into()));
    c
}

/// Build a plan that puts each event in its own paragraph (ByEntity
/// grouping splits on entity name change). Forces multiple paragraphs so
/// paragraph-opener and connective-family diagnosers have material.
fn build_multi_entity_plan(engine: &Engine) -> DocumentPlan {
    let events: Vec<(&str, Context)> = vec![
        ("evt.modified", ctx_named("Alpha")),
        ("evt.touched", ctx_named("Alpha")),
        ("evt.modified", ctx_named("Bravo")),
        ("evt.touched", ctx_named("Bravo")),
        ("evt.modified", ctx_named("Charlie")),
        ("evt.touched", ctx_named("Charlie")),
        ("evt.modified", ctx_named("Delta")),
        ("evt.renamed", ctx_renamed("Delta", "Echo")),
    ];
    DocumentPlan::from_events(&events, engine)
}

#[test]
fn refine_off_renders_byte_identical_to_no_refine_path() {
    let plan = build_multi_entity_plan(&engine(false));
    let off = plan.render(&engine(false), &mut Session::new()).unwrap();
    let off_again = plan.render(&engine(false), &mut Session::new()).unwrap();
    assert_eq!(off, off_again);
    assert!(!off.is_empty());
}

#[test]
fn refine_on_returns_outcome_with_iteration_metadata() {
    let plan = build_multi_entity_plan(&engine(true));
    let outcome = plan
        .render_refined(&engine(true), &mut Session::new())
        .unwrap();
    assert!(!outcome.text.is_empty());
    assert!(outcome.iterations_run <= 3);
}

#[test]
fn refine_on_terminates_when_diagnoses_clean_or_iter_cap() {
    let engine = engine(true);
    let plan = build_multi_entity_plan(&engine);
    let outcome = plan.render_refined(&engine, &mut Session::new()).unwrap();
    // Either the loop converged cleanly or it ran to its iteration cap.
    // Both are valid termination conditions; the property under test is
    // that the loop terminates rather than spinning.
    assert!(outcome.iterations_run <= 3);
}

#[test]
fn refine_on_renders_deterministically_across_repeats() {
    let engine = engine(true);
    let plan = build_multi_entity_plan(&engine);
    let first = plan.render_refined(&engine, &mut Session::new()).unwrap();
    for _ in 0..30 {
        let again = plan.render_refined(&engine, &mut Session::new()).unwrap();
        assert_eq!(first.text, again.text);
        assert_eq!(first.iterations_run, again.iterations_run);
    }
}

#[test]
fn refine_blacklist_is_honored_across_iteration_renders() {
    // Pin: when paragraph-opener monotony fires (≥3 paragraphs share an
    // opener), the constraint blacklists that opener. After refinement,
    // the dominant opener's count should drop or be redistributed.
    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Fixed)
        .refine(
            RefineConfig::off()
                // Force just the monotony diagnoser, with a low threshold
                // to make the test deterministic.
                .clone(),
        );
    let custom_diag = ParagraphOpenerMonotony {
        threshold: 2,
        min_paragraphs: 2,
    };
    let mut config = RefineConfig::balanced().with_max_iterations(3);
    config.diagnosers.clear();
    config.diagnosers.push(Arc::new(custom_diag));
    e = e.refine(config);
    e.register_template("evt.modified", "{name|refer} was modified")
        .unwrap();
    e.register_template("evt.touched", "{name|refer} was touched")
        .unwrap();

    // Force same-entity-different-action across many entities so each
    // paragraph emits an "Additionally," opener naturally.
    let events: Vec<(&str, Context)> = vec![
        ("evt.modified", ctx_named("Alpha")),
        ("evt.touched", ctx_named("Alpha")),
        ("evt.modified", ctx_named("Bravo")),
        ("evt.touched", ctx_named("Bravo")),
        ("evt.modified", ctx_named("Charlie")),
        ("evt.touched", ctx_named("Charlie")),
        ("evt.modified", ctx_named("Delta")),
        ("evt.touched", ctx_named("Delta")),
    ];
    let plan = DocumentPlan::from_events(&events, &e);

    // Apples-to-apples baseline: same plan, render_structured (no refine
    // loop). This isolates the contribution of the refine loop itself
    // from the structured-vs-flat sentence-emission difference.
    let baseline_engine = engine(false);
    let baseline_plan = DocumentPlan::from_events(&events, &baseline_engine);
    let baseline_doc = baseline_plan
        .render_structured(&baseline_engine, &mut Session::new())
        .unwrap();
    let baseline_additionally = baseline_doc.text.matches("Additionally,").count();

    let refined_outcome = plan.render_refined(&e, &mut Session::new()).unwrap();
    let refined_additionally = refined_outcome.text.matches("Additionally,").count();

    // The refine pass must not increase the count of the dominant opener,
    // and on documents long enough to trigger the diagnoser it should
    // strictly reduce it (often to zero, since the blacklist applies for
    // the whole iteration).
    assert!(
        refined_additionally <= baseline_additionally,
        "refine pass must not increase 'Additionally,' count. \
         baseline={baseline_additionally}, refined={refined_additionally}\n\
         baseline text:\n{}\n\nrefined text:\n{}",
        baseline_doc.text,
        refined_outcome.text
    );
}

#[test]
fn refine_outcome_score_is_non_negative_and_finite() {
    let engine = engine(true);
    let plan = build_multi_entity_plan(&engine);
    let outcome = plan.render_refined(&engine, &mut Session::new()).unwrap();
    assert!(outcome.final_score.is_finite());
    assert!(outcome.final_score >= 0.0);
}

// ── Custom-diagnoser scaffolding for v0.6.1 constraint tests ──────────

/// Single-shot diagnoser: emits the supplied constraints exactly once,
/// with high enough severity that the iteration controller applies them.
/// After the first call, the diagnoser returns nothing so the loop can
/// converge cleanly. This is the test scaffolding that lets each
/// constraint variant drive a real iteration without requiring a
/// real-world failure condition the built-in diagnosers would catch.
struct OneShotDiagnoser {
    name: &'static str,
    constraints: std::sync::Mutex<Option<Vec<RefineConstraint>>>,
}

impl OneShotDiagnoser {
    fn new(name: &'static str, constraints: Vec<RefineConstraint>) -> Self {
        Self {
            name,
            constraints: std::sync::Mutex::new(Some(constraints)),
        }
    }
}

impl Diagnoser for OneShotDiagnoser {
    fn name(&self) -> &'static str {
        self.name
    }

    fn diagnose(
        &self,
        _document: &RenderedDocument,
        _profile: Option<&StyleProfile>,
    ) -> Vec<Diagnostic> {
        let Some(c) = self.constraints.lock().unwrap().take() else {
            return Vec::new();
        };
        vec![Diagnostic {
            diagnoser: self.name,
            severity: 1.0,
            constraints: c,
        }]
    }
}

fn refine_with_only(diagnoser: Arc<dyn Diagnoser>) -> RefineConfig {
    // `with_min_improvement(-100.0)` forces the iteration controller to
    // accept any candidate regardless of score change, so the constraint
    // application path is exercised even when the rendered output's
    // composite score doesn't strictly improve. The tests assert the
    // candidate's output content directly, which is what's under test.
    let mut c = RefineConfig::balanced()
        .with_max_iterations(2)
        .with_min_improvement(-100.0);
    c.diagnosers.clear();
    c.diagnosers.push(diagnoser);
    c
}

// ── PrimeRecencyWindow ────────────────────────────────────────────────

#[test]
fn prime_recency_window_suppresses_primed_connective_on_next_iteration() {
    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Fixed)
        .refine(refine_with_only(Arc::new(OneShotDiagnoser::new(
            "prime_test",
            vec![RefineConstraint::PrimeRecencyWindow {
                connectives: vec![
                    "Additionally,".to_string(),
                    "Furthermore,".to_string(),
                    "It also".to_string(),
                ],
                list_styles: vec![],
            }],
        ))));
    e.register_template("evt.modified", "{name|refer} was modified")
        .unwrap();
    e.register_template("evt.touched", "{name|refer} was touched")
        .unwrap();

    let events: Vec<(&str, Context)> = vec![
        ("evt.modified", ctx_named("Alpha")),
        ("evt.touched", ctx_named("Alpha")),
        ("evt.modified", ctx_named("Bravo")),
        ("evt.touched", ctx_named("Bravo")),
    ];
    let plan = DocumentPlan::from_events(&events, &e);
    let outcome = plan.render_refined(&e, &mut Session::new()).unwrap();

    // The continuation pool ("Additionally,", "Furthermore,", "It also")
    // is fully primed, so the family-budget gate inside
    // `select_connective_filtered` immediately suppresses any
    // continuation connective on the next iteration's renders.
    assert!(
        !outcome.text.contains("Additionally,")
            && !outcome.text.contains("Furthermore,")
            && !outcome.text.contains("It also"),
        "primed continuation pool must not emit on the next iteration. \
         text:\n{}",
        outcome.text
    );
    assert!(outcome.iterations_run >= 1);
}

// ── OverrideSalienceBias ──────────────────────────────────────────────

#[test]
fn override_salience_bias_changes_tier_selection_in_next_iteration() {
    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Fixed)
        .refine(refine_with_only(Arc::new(OneShotDiagnoser::new(
            "salience_test",
            // SalienceBias::Lower shrinks the bands so the same
            // consumer_count lands in a *higher* tier — Medium → High in
            // typical mid-range cases.
            vec![RefineConstraint::OverrideSalienceBias(SalienceBias::Lower)],
        ))));
    e.register_template_at(
        "evt.modified",
        "{name|refer} was lightly tweaked",
        Salience::Low,
    )
    .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was modified",
        Salience::Medium,
    )
    .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was extensively overhauled across consumers",
        Salience::High,
    )
    .unwrap();

    let mut ctx = ctx_named("UserService");
    // consumer_count=18 lands in Medium under default thresholds
    // (low_max=2, high_min=20). With SalienceBias::Lower the bands
    // shrink (low_max=1, high_min=15) so the same value lands in High.
    ctx.insert("consumer_count", Value::Number(18));

    let events = vec![("evt.modified", ctx)];
    let plan = DocumentPlan::from_events(&events, &e);
    let outcome = plan.render_refined(&e, &mut Session::new()).unwrap();
    assert!(
        outcome.text.contains("extensively overhauled"),
        "SalienceBias::Lower should promote the variant tier from Medium \
         to High, picking the 'extensively overhauled' template. text:\n{}",
        outcome.text
    );
}

// ── ForceVariantTier ──────────────────────────────────────────────────

#[test]
fn force_variant_tier_short_circuits_to_specified_tier() {
    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Fixed)
        .refine(refine_with_only(Arc::new(OneShotDiagnoser::new(
            "force_tier_test",
            vec![RefineConstraint::ForceVariantTier {
                template_key: "evt.modified".to_string(),
                tier: Salience::High,
            }],
        ))));
    e.register_template_at(
        "evt.modified",
        "{name|refer} was lightly tweaked",
        Salience::Low,
    )
    .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was modified",
        Salience::Medium,
    )
    .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was extensively overhauled across consumers",
        Salience::High,
    )
    .unwrap();

    // Salience::Low context (no consumer_count, default 0). Under normal
    // resolution the engine picks the Low variant. ForceVariantTier
    // short-circuits the tier resolution to High regardless.
    let events = vec![("evt.modified", ctx_named("UserService"))];
    let plan = DocumentPlan::from_events(&events, &e);
    let outcome = plan.render_refined(&e, &mut Session::new()).unwrap();
    assert!(
        outcome.text.contains("extensively overhauled"),
        "ForceVariantTier{{tier=High}} must select the High variant \
         independent of context salience. text:\n{}",
        outcome.text
    );
}

// ── TightenLengthDistribution ─────────────────────────────────────────

#[test]
fn tighten_length_distribution_changes_candidate_scoring() {
    // Register two Medium variants so the choose-best path runs and the
    // candidate-discourse score actually informs the pick.
    let target = LengthDistribution {
        // Strongly biased toward long sentences — should make the longer
        // variant score better than the shorter.
        short: 0.0,
        medium: 0.0,
        long: 1.0,
        short_max_words: 6,
        medium_max_words: 12,
    };

    struct LengthDiagnoser {
        target: LengthDistribution,
        consumed: std::sync::Mutex<bool>,
    }
    impl Diagnoser for LengthDiagnoser {
        fn name(&self) -> &'static str {
            "length_test"
        }
        fn diagnose(
            &self,
            _document: &RenderedDocument,
            _profile: Option<&StyleProfile>,
        ) -> Vec<Diagnostic> {
            let mut c = self.consumed.lock().unwrap();
            if *c {
                return Vec::new();
            }
            *c = true;
            vec![Diagnostic {
                diagnoser: "length_test",
                severity: 1.0,
                constraints: vec![RefineConstraint::TightenLengthDistribution(
                    self.target.clone(),
                )],
            }]
        }
    }

    let mut e = Engine::new(English::new())
        .strictness(Strictness::Strict)
        .variation(Variation::Seeded(7))
        .refine(refine_with_only(Arc::new(LengthDiagnoser {
            target,
            consumed: std::sync::Mutex::new(false),
        })));
    e.register_template_at("evt.modified", "{name|refer} was tweaked", Salience::Medium)
        .unwrap();
    e.register_template_at(
        "evt.modified",
        "{name|refer} was extensively overhauled across many consumers and dependencies",
        Salience::Medium,
    )
    .unwrap();

    // Two events to trigger choose-best (first render is unconditional).
    let events = vec![
        ("evt.modified", ctx_named("Alpha")),
        ("evt.modified", ctx_named("Bravo")),
    ];
    let plan = DocumentPlan::from_events(&events, &e);
    let outcome = plan.render_refined(&e, &mut Session::new()).unwrap();
    // Under the long-leaning override, the second event's choose-best
    // pass should prefer the longer variant.
    assert!(
        outcome.text.contains("extensively overhauled"),
        "TightenLengthDistribution(long-leaning) should bias choose-best \
         toward the longer variant. text:\n{}",
        outcome.text
    );
}