tokmd-format 1.14.0

Output formatting and serialization (Markdown, JSON, CSV) for tokmd.
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
//! Derive packet preset sections from sibling bundle files.

use std::collections::BTreeMap;

use tokmd_types::{
    ManualCandidateRecord, PacketPresetInput, PacketSiblingInputs, ReviewCardRecord,
    TokmdPacketsManifest,
};

/// Resolve the effective preset input from manifest and optional sibling files.
pub fn resolve_preset_input(
    manifest: &TokmdPacketsManifest,
    siblings: &PacketSiblingInputs,
    preset: &str,
) -> (Option<PacketPresetInput>, Vec<String>) {
    let mut notes = Vec::new();

    if let Some(manifest_input) = manifest.preset_inputs.get(preset) {
        if !manifest_input.sections.is_empty() {
            return (Some(manifest_input.clone()), notes);
        }
        notes.push(format!(
            "manifest `preset_inputs[{preset}]` is present but has no `sections`; attempting sibling supplementation"
        ));
        if let Some(mut derived) = derive_from_siblings(siblings, preset, &mut notes) {
            derived
                .limitations
                .extend(manifest_input.limitations.iter().cloned());
            derived
                .missing_sections
                .extend(manifest_input.missing_sections.iter().cloned());
            return (Some(derived), notes);
        }
        return (Some(manifest_input.clone()), notes);
    }

    if let Some(derived) = derive_from_siblings(siblings, preset, &mut notes) {
        return (Some(derived), notes);
    }

    notes.push(format!(
        "no manifest `preset_inputs[{preset}]` and no derivable sibling sections"
    ));
    (None, notes)
}

fn derive_from_siblings(
    siblings: &PacketSiblingInputs,
    preset: &str,
    notes: &mut Vec<String>,
) -> Option<PacketPresetInput> {
    let candidate = siblings
        .manual_candidates
        .as_ref()
        .and_then(|file| file.candidates.first());

    let cards = siblings.cards.as_ref().map(|file| file.cards.as_slice());

    match preset {
        "bun-ub-handoff" => candidate.map(|c| derive_handoff(c, notes)),
        "bun-ub-pr-body" => candidate.map(|c| derive_pr_body(c, notes)),
        "bun-ub-ledger-note" => candidate.map(|c| derive_ledger_note(c, notes)),
        "bun-ub-review-map" => Some(derive_review_map(candidate, cards, notes)),
        "bun-ub-next-pick" => candidate.map(|c| derive_next_pick(c, notes)),
        _ => None,
    }
}

fn derive_handoff(candidate: &ManualCandidateRecord, notes: &mut Vec<String>) -> PacketPresetInput {
    notes.push(
        "sections derived from `manual-candidates.json` because manifest `preset_inputs` was absent or empty"
            .into(),
    );
    let mut sections = BTreeMap::new();
    if let Some(body) = candidate_identity(candidate) {
        sections.insert("candidate_identity".into(), body);
    }
    if let Some(body) = candidate.operation_family.as_deref() {
        sections.insert("stable_byte_family".into(), body.into());
    }
    if let Some(body) = candidate.safe_caller.as_deref() {
        sections.insert("safe_js_caller_route".into(), body.into());
    }
    if let Some(body) = candidate.location_text.as_deref() {
        sections.insert("rust_native_seam".into(), body.into());
    }
    if let Some(body) = candidate.proof_mode.as_deref() {
        sections.insert("proof_mode".into(), body.into());
    }
    if let Some(body) = candidate.invariant.as_deref() {
        sections.insert("invariant_at_risk".into(), body.into());
    }
    if let Some(body) = candidate.fix_boundary.as_deref() {
        sections.insert("suggested_fix_boundary".into(), body.into());
    }
    if let Some(body) = join_lines(&candidate.test_targets) {
        sections.insert("test_or_witness_target".into(), body);
    }
    if let Some(body) = join_lines(&candidate.do_not_touch) {
        sections.insert("do_not_touch".into(), body);
    }

    let missing_sections = missing_handoff_sections(&sections);
    PacketPresetInput {
        sections,
        limitations: vec![
            "sibling-derived handoff sections are partial; producer `preset_inputs` is authoritative when present"
                .into(),
        ],
        missing_sections,
    }
}

fn derive_pr_body(candidate: &ManualCandidateRecord, notes: &mut Vec<String>) -> PacketPresetInput {
    notes.push(
        "sections derived from `manual-candidates.json` because manifest `preset_inputs` was absent or empty"
            .into(),
    );
    let mut sections = BTreeMap::new();
    if let Some(body) = candidate.title.as_deref() {
        sections.insert("problem_statement".into(), body.into());
    }
    if let Some(body) = candidate.invariant.as_deref() {
        sections.insert("user_visible_or_invariant_risk".into(), body.into());
    }
    if let Some(body) = candidate
        .pr_aperture
        .as_deref()
        .or(candidate.fix_boundary.as_deref())
    {
        sections.insert("smallest_changed_surface".into(), body.into());
    }
    if let Some(body) = join_lines(&candidate.do_not_touch) {
        sections.insert("non_goals".into(), body);
    }
    sections.insert(
        "exact_claims_not_made".into(),
        "No UB proof; no runtime oracle executed by this renderer.".into(),
    );

    PacketPresetInput {
        sections,
        limitations: vec![
            "sibling-derived PR-body sections omit compatibility oracle and external evidence unless producer supplies `preset_inputs`"
                .into(),
        ],
        missing_sections: vec![],
    }
}

fn derive_ledger_note(
    candidate: &ManualCandidateRecord,
    notes: &mut Vec<String>,
) -> PacketPresetInput {
    notes.push(
        "sections derived from `manual-candidates.json` because manifest `preset_inputs` was absent or empty"
            .into(),
    );
    let mut sections = BTreeMap::new();
    if let Some(body) = candidate.id.as_deref() {
        sections.insert("seed_or_candidate_id".into(), body.into());
    }
    sections.insert(
        "ledger_state_transition".into(),
        "not available from sibling index alone".into(),
    );
    if let Some(body) = join_lines(&candidate.do_not_touch) {
        sections.insert("remaining_outside_aperture".into(), body);
    }

    PacketPresetInput {
        sections,
        limitations: vec![
            "ledger note requires producer transition metadata; sibling index does not justify state changes"
                .into(),
        ],
        missing_sections: vec![
            "old ledger state".into(),
            "new ledger state".into(),
            "evidence or PR receipt".into(),
        ],
    }
}

fn derive_review_map(
    candidate: Option<&ManualCandidateRecord>,
    cards: Option<&[ReviewCardRecord]>,
    notes: &mut Vec<String>,
) -> PacketPresetInput {
    notes.push(
        "sections derived from sibling bundle files because manifest `preset_inputs` was absent or empty"
            .into(),
    );
    let mut sections = BTreeMap::new();
    let mut limitations = Vec::new();

    if let Some(candidate) = candidate {
        if let Some(body) = candidate_identity(candidate) {
            sections.insert("manual_candidate_identity".into(), body);
        }
        if let Some(body) = candidate.location_text.as_deref() {
            sections.insert("changed_unsafe_native_seams".into(), body.into());
        }
    } else {
        limitations.push(
            "`manual-candidates.json` had no candidates for review-map supplementation".into(),
        );
    }

    if let Some(cards) = cards {
        if cards.is_empty() {
            limitations.push("`cards.json` was present but contained no ReviewCards".into());
        } else {
            let body = cards
                .iter()
                .filter_map(review_card_line)
                .collect::<Vec<_>>()
                .join("\n");
            sections.insert("reviewcard_ids_and_seams".into(), body);
        }
    } else {
        limitations.push("`cards.json` was not ingested; ReviewCard seams are unavailable".into());
    }

    sections.insert(
        "explicit_no_posting_boundary".into(),
        "tokmd render does not post comments and sibling ingestion does not authorize posting"
            .into(),
    );

    PacketPresetInput {
        sections,
        limitations,
        missing_sections: vec!["comment-plan.json selection summary".into()],
    }
}

fn derive_next_pick(
    candidate: &ManualCandidateRecord,
    notes: &mut Vec<String>,
) -> PacketPresetInput {
    notes.push(
        "sections derived from `manual-candidates.json` because manifest `preset_inputs` was absent or empty"
            .into(),
    );
    let mut sections = BTreeMap::new();
    if let Some(body) = candidate_identity(candidate) {
        sections.insert("ranked_next_candidate".into(), body);
    }
    if let Some(body) = candidate.proof_mode.as_deref() {
        sections.insert("proof_mode".into(), body.into());
    }
    if let Some(body) = candidate
        .pr_aperture
        .as_deref()
        .or(candidate.fix_boundary.as_deref())
    {
        sections.insert("smallest_first_pr".into(), body.into());
    }
    if let Some(body) = join_lines(&candidate.do_not_touch) {
        sections.insert("non_goals".into(), body);
    }

    PacketPresetInput {
        sections,
        limitations: vec![
            "next-pick ranking is not recomputed; first manual candidate is surfaced for formatting only"
                .into(),
        ],
        missing_sections: vec!["owner lane".into(), "dependencies or parked-followup unblock".into()],
    }
}

fn candidate_identity(candidate: &ManualCandidateRecord) -> Option<String> {
    match (candidate.id.as_deref(), candidate.title.as_deref()) {
        (Some(id), Some(title)) => Some(format!("{id} / {title}")),
        (Some(id), None) => Some(id.into()),
        (None, Some(title)) => Some(title.into()),
        (None, None) => None,
    }
}

fn review_card_line(card: &ReviewCardRecord) -> Option<String> {
    let id = card.id.as_deref()?;
    let location = card
        .location_text
        .as_deref()
        .or(card.unsafe_operation.as_deref())
        .unwrap_or("location unknown");
    Some(format!("ReviewCard {id} @ {location}"))
}

fn join_lines(values: &[String]) -> Option<String> {
    if values.is_empty() {
        None
    } else {
        Some(values.join("; "))
    }
}

fn missing_handoff_sections(sections: &BTreeMap<String, String>) -> Vec<String> {
    const REQUIRED: &[&str] = &[
        "candidate_identity",
        "stable_byte_family",
        "safe_js_caller_route",
        "rust_native_seam",
        "proof_mode",
        "suggested_fix_boundary",
        "test_or_witness_target",
        "do_not_touch",
        "ledger_state_and_next_action",
        "pr_aperture_and_stop_line",
    ];
    REQUIRED
        .iter()
        .filter(|name| !sections.contains_key(**name))
        .map(|name| (*name).to_string())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokmd_types::{
        MANUAL_CANDIDATES_SCHEMA, ManualCandidatesFile, PacketSiblingInputs, TOKMD_PACKETS_SCHEMA,
        TokmdPacketsManifest,
    };

    fn sample_candidate() -> ManualCandidateRecord {
        ManualCandidateRecord {
            id: Some("seed-42".into()),
            title: Some("MarkdownObject UTF-8 boundary".into()),
            invariant: Some("UTF-8 bounds".into()),
            safe_caller: Some("TextDecoder.decode".into()),
            location_text: Some("src/runtime/api/MarkdownObject.rs:120".into()),
            proof_mode: Some("manual witness required".into()),
            fix_boundary: Some("narrow UTF-8 validation at JS seam only".into()),
            test_targets: vec!["utf8-boundary-fixture".into()],
            ..ManualCandidateRecord::default()
        }
    }

    #[test]
    fn derives_handoff_from_manual_candidates_when_manifest_empty() {
        let manifest = TokmdPacketsManifest {
            schema: TOKMD_PACKETS_SCHEMA.into(),
            producer: None,
            inputs_present: vec!["manual-candidates.json".into()],
            inputs_absent: vec![],
            non_claims: vec![],
            preset_inputs: BTreeMap::new(),
        };
        let siblings = PacketSiblingInputs {
            manual_candidates: Some(ManualCandidatesFile {
                schema_version: MANUAL_CANDIDATES_SCHEMA.into(),
                candidates: vec![sample_candidate()],
            }),
            ..PacketSiblingInputs::default()
        };
        let (input, notes) = resolve_preset_input(&manifest, &siblings, "bun-ub-handoff");
        let input = input.expect("derived input");
        assert!(input.sections.contains_key("candidate_identity"));
        assert!(
            notes
                .iter()
                .any(|note| note.contains("manual-candidates.json"))
        );
        assert!(!input.missing_sections.is_empty());
    }

    #[test]
    fn manifest_preset_inputs_take_precedence_over_siblings() {
        let mut sections = BTreeMap::new();
        sections.insert("candidate_identity".into(), "from manifest".into());
        let manifest = TokmdPacketsManifest {
            schema: TOKMD_PACKETS_SCHEMA.into(),
            producer: None,
            inputs_present: vec!["manual-candidates.json".into()],
            inputs_absent: vec![],
            non_claims: vec![],
            preset_inputs: BTreeMap::from([(
                "bun-ub-handoff".into(),
                PacketPresetInput {
                    sections,
                    limitations: vec![],
                    missing_sections: vec![],
                },
            )]),
        };
        let siblings = PacketSiblingInputs {
            manual_candidates: Some(ManualCandidatesFile {
                schema_version: MANUAL_CANDIDATES_SCHEMA.into(),
                candidates: vec![sample_candidate()],
            }),
            ..PacketSiblingInputs::default()
        };
        let (input, notes) = resolve_preset_input(&manifest, &siblings, "bun-ub-handoff");
        let input = input.expect("manifest input");
        assert_eq!(
            input.sections.get("candidate_identity").map(String::as_str),
            Some("from manifest")
        );
        assert!(notes.is_empty());
    }
}