speccoder-core 0.7.1

Pure domain of speccoder: project snapshots, the task graph, verdict rules and command results. No IO, no async.
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Explore's pure parts: the report a session's run ends with (a claim,
//! checked here), the brief the person's choice composes and its digest,
//! the session's states, and the record a resumed run carries. No IO: the
//! runtime stores sessions and runs the skills.

use serde::{Deserialize, Serialize};

use crate::actions::Via;

/// The most a brief may hold.
pub const BRIEF_MAX: usize = 16 * 1024;

/// What a resumed run carries of the session, at most.
pub const CARRIED_MAX: usize = 24_000;

/// The skill `spec explore` falls back to when the format has none.
pub const FALLBACK: &str = "speccoder.explore";

/// One option the model proposes: a claim, never a decision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Approach {
    /// A short name.
    pub title: String,
    /// What it is.
    pub summary: String,
    /// What it gives up.
    #[serde(default)]
    pub tradeoffs: Vec<String>,
    /// What could go wrong.
    #[serde(default)]
    pub risks: Vec<String>,
}

/// How a run of a session ended: the model's report, schema-checked.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ExploreReport {
    /// The model's summary of the idea.
    pub summary: String,
    /// `done` or `incomplete`.
    pub status: String,
    /// The options.
    #[serde(default)]
    pub approaches: Vec<Approach>,
    /// What any approach must respect.
    #[serde(default)]
    pub constraints: Vec<String>,
    /// What is still open.
    #[serde(default)]
    pub open_questions: Vec<String>,
}

/// A constraint or open question, by its source.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Item {
    /// Its words.
    pub text: String,
    /// `model`, `person` or `nobody` (a question nobody could answer).
    pub source: String,
}

/// A question the run asked and how it was answered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct QaItem {
    /// The model's question.
    pub question: String,
    /// The person's answer; none when nobody could answer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub answer: Option<String>,
    /// Who answered.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub by: Option<String>,
}

/// The person's choice: an approach and what they kept or added.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Choice {
    /// 1-based, of the last report's approaches.
    pub approach: usize,
    /// Kept and added constraints.
    pub constraints: Vec<Item>,
    /// Kept and added open questions.
    pub open_questions: Vec<Item>,
    /// Who chose.
    pub by: String,
    /// Through what.
    pub via: Via,
    /// `prompt` (picked at a terminal or in the app) or `flag` (`--choose`).
    pub basis: String,
    /// Unix seconds.
    pub at: u64,
    /// The brief's digest (hex SHA-256 of its body).
    pub digest: String,
    /// The brief's size.
    pub bytes: usize,
}

/// One run of a session.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct RunRef {
    /// The agentplane run.
    pub run_id: String,
    /// `explore` or `resume`.
    pub kind: String,
    /// What the person added when resuming.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub words: Option<String>,
    /// How it ended.
    pub status: String,
}

/// The unit a session started.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Started {
    /// The unit.
    pub unit: String,
    /// Unix seconds.
    pub at: u64,
}

/// One exploration of one idea in one project.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ExploreSession {
    /// 1.
    pub version: u32,
    /// `ex-` and hex.
    pub id: String,
    /// The person's idea.
    pub idea: String,
    /// The skill that explores.
    pub skill: String,
    /// `flag`, `format` or `fallback`.
    pub selected_because: String,
    /// `open`, `chosen` or `started`.
    pub state: String,
    /// Unix seconds.
    pub created_at: u64,
    /// Unix seconds.
    pub updated_at: u64,
    /// Its runs, in order.
    #[serde(default)]
    pub runs: Vec<RunRef>,
    /// Each run's report, in order.
    #[serde(default)]
    pub reports: Vec<ExploreReport>,
    /// The questions asked and their answers.
    #[serde(default)]
    pub qa: Vec<QaItem>,
    /// The choices made, the last one standing.
    #[serde(default)]
    pub choices: Vec<Choice>,
    /// The unit it started, once.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started: Option<Started>,
}

/// One session in a list.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct SessionEntry {
    /// `ex-…`.
    pub id: String,
    /// The idea.
    pub idea: String,
    /// The skill that explores it.
    pub skill: String,
    /// `open`, `chosen` or `started`.
    pub state: String,
    /// Unix seconds.
    pub updated_at: u64,
    /// The chosen brief's digest.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<String>,
    /// The unit it started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
}

/// `spec explore`'s payload: one session as it stands, or the list.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ExplorePayload {
    /// The session (none for a list).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session: Option<ExploreSession>,
    /// The last run's approaches (the model's).
    #[serde(default)]
    pub approaches: Vec<Approach>,
    /// The constraints to keep or drop, by source.
    #[serde(default)]
    pub constraints: Vec<Item>,
    /// The open questions, by source.
    #[serde(default)]
    pub open_questions: Vec<Item>,
    /// The person's last choice.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub choice: Option<Choice>,
    /// The brief it made, verbatim.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub brief: Option<String>,
    /// The project's sessions, newest first (`--list`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sessions: Option<Vec<SessionEntry>>,
}

impl ExploreSession {
    /// What the person chooses from now: the last report's approaches and
    /// its constraints and open questions (with the questions nobody
    /// could answer), and what the person added at their last choice —
    /// a resume keeps their words.
    #[must_use]
    pub fn on_the_table(&self) -> (Vec<Approach>, Vec<Item>, Vec<Item>) {
        let Some(r) = self.reports.last() else {
            return (Vec::new(), Vec::new(), Vec::new());
        };
        let item = |text: &String, source: &str| Item {
            text: text.clone(),
            source: source.to_owned(),
        };
        let mut questions: Vec<Item> = r.open_questions.iter().map(|q| item(q, "model")).collect();
        for q in self.qa.iter().filter(|q| q.answer.is_none()) {
            if !questions.iter().any(|i| i.text == q.question) {
                questions.push(item(&q.question, "nobody"));
            }
        }
        let mut constraints: Vec<Item> = r.constraints.iter().map(|c| item(c, "model")).collect();
        if let Some(c) = self.choices.last() {
            for (kept, mine) in [
                (&mut constraints, &c.constraints),
                (&mut questions, &c.open_questions),
            ] {
                for i in mine.iter().filter(|i| i.source == "person") {
                    if !kept.iter().any(|k| k.text == i.text) {
                        kept.push(i.clone());
                    }
                }
            }
        }
        (r.approaches.clone(), constraints, questions)
    }

    /// Its line in a list.
    #[must_use]
    pub fn entry(&self) -> SessionEntry {
        SessionEntry {
            id: self.id.clone(),
            idea: self.idea.clone(),
            skill: self.skill.clone(),
            state: self.state.clone(),
            updated_at: self.updated_at,
            digest: self.choices.last().map(|c| c.digest.clone()),
            unit: self.started.as_ref().map(|s| s.unit.clone()),
        }
    }
}

/// A composed brief.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Brief {
    /// The whole text, its header line first.
    pub text: String,
    /// Hex SHA-256 of the body (everything after the header line).
    pub digest: String,
    /// The text's size.
    pub bytes: usize,
}

/// Whether the fallback's report stands: at least three approaches with
/// distinct titles, each with a trade-off and a risk. A format's own skill
/// is not held to this (its shape is its own).
///
/// # Errors
///
/// What is missing, in words the model is told.
pub fn validate_fallback(r: &ExploreReport) -> Result<(), String> {
    if r.approaches.len() < 3 {
        return Err(format!(
            "give at least three approaches (you gave {})",
            r.approaches.len()
        ));
    }
    let mut seen = std::collections::BTreeSet::new();
    for a in &r.approaches {
        let title = a.title.trim().to_lowercase();
        if title.is_empty() {
            return Err("every approach needs a title".to_owned());
        }
        if !seen.insert(title) {
            return Err(format!(
                "two approaches are both called \"{}\"",
                a.title.trim()
            ));
        }
        if a.tradeoffs.iter().all(|t| t.trim().is_empty()) {
            return Err(format!(
                "\"{}\" needs at least one trade-off",
                a.title.trim()
            ));
        }
        if a.risks.iter().all(|t| t.trim().is_empty()) {
            return Err(format!("\"{}\" needs at least one risk", a.title.trim()));
        }
    }
    Ok(())
}

fn bullets(out: &mut String, items: &[String]) {
    if items.is_empty() {
        out.push_str("- none\n");
    }
    for i in items {
        out.push_str(&format!("- {}\n", i.trim()));
    }
}

/// The brief the person's choice makes, deterministically: the approach
/// verbatim, then the kept constraints and open questions. `hash` is the
/// hex SHA-256 the runtime supplies; the digest covers the body only, so
/// the header can name it.
///
/// # Errors
///
/// The brief is larger than [`BRIEF_MAX`].
pub fn compose(
    approach: &Approach,
    constraints: &[Item],
    questions: &[Item],
    session: &str,
    hash: impl Fn(&[u8]) -> String,
) -> Result<Brief, String> {
    let mut body = String::new();
    body.push_str("\n## Approach\n\n");
    body.push_str(approach.title.trim());
    body.push('\n');
    body.push_str(approach.summary.trim());
    body.push('\n');
    if !approach.tradeoffs.is_empty() {
        body.push_str(&format!("Trade-offs: {}\n", approach.tradeoffs.join("; ")));
    }
    if !approach.risks.is_empty() {
        body.push_str(&format!("Risks: {}\n", approach.risks.join("; ")));
    }
    body.push_str("\n## Constraints\n\n");
    bullets(
        &mut body,
        &constraints
            .iter()
            .map(|i| i.text.clone())
            .collect::<Vec<_>>(),
    );
    body.push_str("\n## Open questions\n\n");
    bullets(
        &mut body,
        &questions.iter().map(|i| i.text.clone()).collect::<Vec<_>>(),
    );
    let digest = hash(body.as_bytes());
    let text = format!(
        "Explore brief {} (session {session})\n{body}",
        digest.get(..12).unwrap_or(&digest)
    );
    if text.len() > BRIEF_MAX {
        return Err(format!(
            "the brief is {} KiB; a brief is at most 16 KiB — shorten its constraints",
            text.len().div_ceil(1024)
        ));
    }
    let bytes = text.len();
    Ok(Brief {
        text,
        digest,
        bytes,
    })
}

/// The digest of a stored brief's body (after its header line).
#[must_use]
pub fn body_digest(text: &str, hash: impl Fn(&[u8]) -> String) -> String {
    let body = text.split_once('\n').map_or("", |(_, b)| b);
    hash(format!("\n{}", body.strip_prefix('\n').unwrap_or(body)).as_bytes())
}

/// What a session event does to its state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Event {
    /// The person chose an approach.
    Choose,
    /// A new run of the session.
    Resume,
    /// The spec was started from its brief.
    Start,
}

/// The state after `event` in `state`.
///
/// # Errors
///
/// The event does not apply: why, in words.
pub fn next_state(state: &str, event: Event) -> Result<&'static str, &'static str> {
    match (state, event) {
        ("open" | "chosen", Event::Choose) => Ok("chosen"),
        ("open" | "chosen", Event::Resume) => Ok("open"),
        ("chosen", Event::Start) => Ok("started"),
        ("open", Event::Start) => Err("choose an approach first"),
        ("started", _) => Err("this session already started a spec; explore again"),
        _ => Err("not a state of a session"),
    }
}

/// What a resumed run carries of the session: earlier questions with the
/// person's answers, earlier approaches (the model's), the person's notes
/// — verbatim, newest first, at most [`CARRIED_MAX`] bytes. Never a summary.
#[must_use]
pub fn carried(s: &ExploreSession) -> String {
    let mut parts: Vec<String> = Vec::new();
    if let Some(c) = s.choices.last() {
        let notes: Vec<&str> = c
            .constraints
            .iter()
            .chain(&c.open_questions)
            .filter(|i| i.source == "person")
            .map(|i| i.text.as_str())
            .collect();
        if !notes.is_empty() {
            parts.push(format!(
                "<context kind=\"notes\" source=\"person\">\n{}\n</context>",
                notes.join("\n")
            ));
        }
    }
    for q in s.qa.iter().rev() {
        let answer = q.answer.as_deref().map_or_else(
            || "(nobody could answer)".to_owned(),
            |a| {
                format!(
                    "{a}\n(answered by {})",
                    q.by.as_deref().unwrap_or("the person")
                )
            },
        );
        parts.push(format!(
            "<context kind=\"question\" source=\"person\">\nQ: {}\nA: {answer}\n</context>",
            q.question
        ));
    }
    for r in s.reports.iter().rev() {
        let list: Vec<String> = r
            .approaches
            .iter()
            .enumerate()
            .map(|(i, a)| format!("{}. {} — {}", i + 1, a.title, a.summary))
            .collect();
        parts.push(format!(
            "<context kind=\"approaches\" source=\"model output\">\n{}\n</context>",
            list.join("\n")
        ));
    }
    let mut out = String::new();
    for p in parts {
        if out.len() + p.len() + 2 > CARRIED_MAX {
            break;
        }
        out.push_str(&p);
        out.push_str("\n\n");
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    fn h(b: &[u8]) -> String {
        // A stand-in hash for the pure tests: the bytes' length and sum.
        format!(
            "{:032x}{:032x}",
            b.len(),
            b.iter().map(|x| u64::from(*x)).sum::<u64>()
        )
    }

    fn approach(t: &str) -> Approach {
        Approach {
            title: t.to_owned(),
            summary: format!("{t} summary"),
            tradeoffs: vec!["slower".to_owned()],
            risks: vec!["drift".to_owned()],
        }
    }

    fn report(n: usize) -> ExploreReport {
        ExploreReport {
            summary: "s".to_owned(),
            status: "done".to_owned(),
            approaches: (0..n).map(|i| approach(&format!("A{i}"))).collect(),
            constraints: vec![],
            open_questions: vec![],
        }
    }

    #[test]
    fn the_fallback_needs_three_distinct_approaches_each_with_a_cost() {
        assert!(validate_fallback(&report(3)).is_ok());
        assert!(validate_fallback(&report(2)).unwrap_err().contains("three"));
        let mut dup = report(3);
        dup.approaches[2].title = "a0".to_owned();
        assert!(validate_fallback(&dup).unwrap_err().contains("both called"));
        let mut free = report(3);
        free.approaches[1].risks.clear();
        assert!(validate_fallback(&free).unwrap_err().contains("risk"));
    }

    #[test]
    fn a_brief_names_its_digest_and_the_digest_covers_its_body() {
        let c = [Item {
            text: "no new dependency".to_owned(),
            source: "person".to_owned(),
        }];
        let b = compose(&approach("CSV"), &c, &[], "ex-1", h).unwrap();
        assert!(b.text.starts_with(&format!(
            "Explore brief {} (session ex-1)\n",
            &b.digest[..12]
        )));
        assert!(b.text.contains("## Approach\n\nCSV\nCSV summary\n"));
        assert!(b.text.contains("- no new dependency\n"));
        assert_eq!(body_digest(&b.text, h), b.digest);
        let changed = b.text.replace("CSV summary", "XLSX summary");
        assert_ne!(body_digest(&changed, h), b.digest);
    }

    #[test]
    fn states_follow_the_session() {
        assert_eq!(next_state("open", Event::Choose), Ok("chosen"));
        assert_eq!(next_state("chosen", Event::Resume), Ok("open"));
        assert_eq!(next_state("chosen", Event::Start), Ok("started"));
        assert!(next_state("open", Event::Start).is_err());
        assert!(next_state("started", Event::Resume).is_err());
    }

    proptest! {
        #[test]
        fn compose_is_deterministic_and_bounded(title in "[a-zA-Z ]{1,40}", n in 0usize..400) {
            let items: Vec<Item> = (0..n).map(|i| Item { text: format!("constraint {i}"), source: "model".to_owned() }).collect();
            let a = compose(&approach(&title), &items, &[], "ex-1", h);
            let b = compose(&approach(&title), &items, &[], "ex-1", h);
            prop_assert_eq!(&a, &b);
            if let Ok(brief) = a {
                prop_assert!(brief.bytes <= BRIEF_MAX);
                prop_assert_eq!(body_digest(&brief.text, h), brief.digest);
            }
        }

        #[test]
        fn carried_keeps_every_answer_labelled_within_its_cap(n in 0usize..40) {
            let s = ExploreSession {
                version: 1, id: "ex-1".to_owned(), idea: "i".to_owned(), skill: FALLBACK.to_owned(),
                selected_because: "fallback".to_owned(), state: "open".to_owned(), created_at: 0, updated_at: 0,
                runs: vec![], reports: vec![report(3)],
                qa: (0..n).map(|i| QaItem { question: format!("q{i}?"), answer: Some(format!("a{i}")), by: Some("Ada".to_owned()) }).collect(),
                choices: vec![], started: None,
            };
            let c = carried(&s);
            prop_assert!(c.len() <= CARRIED_MAX);
            for i in 0..n {
                let pair = format!("Q: q{i}?\nA: a{i}\n(answered by Ada)");
                prop_assert!(c.contains(&pair), "missing {}", pair);
            }
            prop_assert!(c.contains("source=\"model output\""));
        }
    }
}