malvin 0.2.7

Non-interactive research and coding agent
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
use crate::router_flow::router_flow_prompt::{
    RouterAPromptInput, RouterBPromptInput, RouterKpopCommonPromptInput,
    RouterSummarizePromptInput, build_router_a_prompt, build_router_b_prompt,
    build_router_header_prompt, build_router_kpop_common_prompt, build_router_mbc2_prompt,
    build_router_summarize_prompt, prepare_router_prompt_store, router_b_prompt_label,
};
use malvin::config::DEFAULT_CLI_MODEL;
use malvin::flow_prompt_join_test_helpers::flow_test_artifacts;
use malvin::prompts::PromptStore;

#[test]
fn build_router_a_prompt_expands_malvin_command_with_active_model() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let prompt_root = tmp.path().join("prompts");
    std::fs::create_dir_all(&prompt_root).expect("mkdir prompts");
    std::fs::write(
        prompt_root.join("router_a.md"),
        "Use {{ malvin_command }}\n{{ code_extra }}\nSee {{ user_request_path }}.\n",
    )
    .expect("write router_a");
    std::fs::write(
        prompt_root.join("router_code_extra.md"),
        "- Make sure the code checks all pass:\n```\n{{ code_checks }}\n```\n",
    )
    .expect("write code_extra");
    let store = PromptStore::with_root(prompt_root);
    let artifacts = flow_test_artifacts(&tmp);
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: "composer-2",
        gates: false,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(body.contains("malvin --model=composer-2"));
    assert!(!body.contains("{{ malvin_command }}"));
}

#[test]
fn build_router_a_prompt_renders_without_unresolved_braces() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: false,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(!body.contains("{{"));
    assert!(body.contains("__MALVIN_DONE__"));
}

#[test]
fn build_router_a_prompt_includes_code_checks_when_gates_enabled() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "echo ROUTER_CHECK_LINE\n");
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: true,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(body.contains("echo ROUTER_CHECK_LINE"));
    assert!(!body.contains("{{"));
}

#[test]
fn build_router_a_prompt_omits_code_checks_when_gates_disabled() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "echo ROUTER_CHECK_LINE\n");
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: false,
        gates_just_ran: true,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(!body.contains("echo ROUTER_CHECK_LINE"));
    assert!(!body.contains("quality gates were just run"));
    assert!(
        !body.contains("__begin_gates_output__"),
        "empty code_checks must yield empty code_extra: {body}"
    );
    assert!(!body.contains("{{"));
}

#[test]
fn build_router_a_prompt_omits_code_extra_when_gate_commands_empty() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "# no commands\n\n  \n");
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: true,
        gates_just_ran: true,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(
        !body.contains("__begin_gates_output__"),
        "whitespace-only code_checks must yield empty code_extra: {body}"
    );
    assert!(
        !body.contains("quality gates were just run"),
        "empty code_extra must not append the post-gates note: {body}"
    );
    assert!(!body.contains("{{"));
}

#[test]
fn router_code_extra_note_absent_when_gates_have_not_run() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "true\n");
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: true,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(
        !body.contains("quality gates were just run"),
        "note must be absent before any gate run: {body}"
    );
    assert!(!body.contains("{{"));
}

#[test]
fn router_a_prompt_ignores_process_global_gates_just_ran_flag() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "true\n");
    let store = prepare_router_prompt_store().expect("store");
    // Global may be stale from a prior request; prompt text follows the input only.
    malvin::gate_loop_session::set_quality_gates_just_ran(true);
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: true,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    malvin::gate_loop_session::set_quality_gates_just_ran(false);
    assert!(
        !body.contains("quality gates were just run"),
        "prompt builders must not read process-global just_ran: {body}"
    );
}

#[test]
fn router_code_extra_note_present_after_gates_just_ran() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    malvin::seed_malvin_checks(tmp.path(), "true\n");
    let store = prepare_router_prompt_store().expect("store");
    // Global false proves the note comes from the explicit input, not shared state.
    malvin::gate_loop_session::set_quality_gates_just_ran(false);
    let body = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: true,
        gates_just_ran: true,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(
        body.contains("The quality gates were just run, and their output is in `"),
        "note must be present right after a gate run: {body}"
    );
    assert!(
        body.contains("quality_gates.log"),
        "note must name the quality_gates.log path: {body}"
    );
    assert!(!body.contains("{{"));
    assert!(
        !body.contains("NB: The code checks may have already been run"),
        "old unconditional NB line must be gone: {body}"
    );
}

#[test]
fn build_router_summarize_prompt_renders_dm_body_without_unresolved_braces() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_summarize_prompt(RouterSummarizePromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
    })
    .expect("router_summarize");
    assert!(!body.contains("{{"));
    assert!(
        body.contains("Write a summary of this entire session"),
        "must render router_summarize.md: {body}"
    );
}

#[test]
fn build_router_b_prompt_selects_creative_template_when_flag_set() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let plain = build_router_b_prompt(RouterBPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        creative: false,
        no_kpop: false,
    })
    .expect("router_b");
    let creative = build_router_b_prompt(RouterBPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        creative: true,
        no_kpop: false,
    })
    .expect("router_b_creative");
    assert!(
        plain.contains("KPop: Satisfy the requirements."),
        "default router_b must keep KPop satisfy instruction: {plain}"
    );
    assert!(
        !plain.contains("MBC2"),
        "default router_b must not mention MBC2: {plain}"
    );
    assert!(creative.contains("MBC2"));
    assert!(
        creative.contains("KPop: Satisfy the requirements."),
        "creative router_b must keep KPop satisfy instruction: {creative}"
    );
    assert_eq!(
        router_b_prompt_label(malvin::prompts::RouterBPromptFlags {
            creative: false,
            no_kpop: false,
        }),
        "router_b.md"
    );
    assert_eq!(
        router_b_prompt_label(malvin::prompts::RouterBPromptFlags {
            creative: true,
            no_kpop: false,
        }),
        "router_b_creative.md"
    );
    assert_eq!(
        router_b_prompt_label(malvin::prompts::RouterBPromptFlags {
            creative: false,
            no_kpop: true,
        }),
        "router_b_no_kpop.md"
    );
    assert_eq!(
        router_b_prompt_label(malvin::prompts::RouterBPromptFlags {
            creative: true,
            no_kpop: true,
        }),
        "router_b_no_kpop.md"
    );
}

#[test]
fn build_router_mbc2_prompt_embeds_plan_text() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let body = build_router_mbc2_prompt(&store, &artifacts).expect("mbc2");
    assert!(!body.contains("{{"));
    assert!(body.contains("MBC2"), "must render mbc2.md: {body}");
    let plan = std::fs::read_to_string(&artifacts.plan_path).expect("plan");
    assert!(
        body.contains(plan.trim()),
        "mbc2 must embed plan text; body={body}"
    );
}

#[test]
fn build_router_prompts_select_no_kpop_templates_when_flag_set() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let kpop = build_router_kpop_common_prompt(RouterKpopCommonPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        max_hypotheses: 3,
        no_kpop: true,
        gate_iteration: 1,
    })
    .expect("kpop_common_no_kpop");
    assert!(
        kpop.is_empty(),
        "no_kpop kpop_common must be empty after trim: {kpop:?}"
    );
    let a = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: false,
        gates_just_ran: false,
        no_kpop: true,
    })
    .expect("router_a_no_kpop");
    assert!(
        !a.contains("KPop:"),
        "no_kpop router_a must omit KPop: prefix: {a}"
    );
    assert!(a.contains("Find unsatisfied requirements"));
    let b = build_router_b_prompt(RouterBPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        creative: true,
        no_kpop: true,
    })
    .expect("router_b_no_kpop");
    assert!(
        !b.contains("KPop:"),
        "no_kpop router_b must omit KPop: prefix: {b}"
    );
    assert!(
        !b.contains("MBC2"),
        "no_kpop wins over creative for router_b: {b}"
    );
    assert!(b.contains("Satisfy the requirements"));
}

#[test]
fn build_router_prompts_use_canonical_templates() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let artifacts = flow_test_artifacts(&tmp);
    let store = prepare_router_prompt_store().expect("store");
    let header = build_router_header_prompt(
        crate::router_flow::router_flow_prompt::RouterHeaderPromptInput {
            store: &store,
            artifacts: &artifacts,
            model: DEFAULT_CLI_MODEL,
            max_hypotheses: 5,
            no_kpop: false,
            gate_iteration: 1,
        },
    )
    .expect("header");
    assert!(
        header.to_ascii_lowercase().contains("falsifiable"),
        "header embeds kpop_common via kpop_insert"
    );
    let header_no = build_router_header_prompt(
        crate::router_flow::router_flow_prompt::RouterHeaderPromptInput {
            store: &store,
            artifacts: &artifacts,
            model: DEFAULT_CLI_MODEL,
            max_hypotheses: 5,
            no_kpop: true,
            gate_iteration: 1,
        },
    )
    .expect("header no_kpop");
    assert!(
        !header_no.to_ascii_lowercase().contains("falsifiable"),
        "no_kpop header must omit kpop method language"
    );
    let a = build_router_a_prompt(RouterAPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        gates: false,
        gates_just_ran: false,
        no_kpop: false,
    })
    .expect("router_a");
    assert!(!a.to_ascii_lowercase().contains("falsif"));
    let b = build_router_b_prompt(RouterBPromptInput {
        store: &store,
        artifacts: &artifacts,
        model: DEFAULT_CLI_MODEL,
        creative: false,
        no_kpop: false,
    })
    .expect("router_b");
    assert!(!b.to_ascii_lowercase().contains("falsif"));
}