kamiyo-reality-fork-cli 0.4.0

Native CLI for KAMIYO Reality Fork — repo-aware launch stress tests and on-chain agent management.
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
use std::path::Path;

use crate::types::*;

fn clamp(v: f64) -> f64 {
    v.clamp(0.0, 1.0)
}

fn average(vals: &[f64]) -> f64 {
    if vals.is_empty() {
        return 0.0;
    }
    vals.iter().sum::<f64>() / vals.len() as f64
}

pub fn percent(v: f64) -> String {
    format!("{}%", (v * 100.0).round() as i64)
}

fn trim_post(v: &str) -> String {
    if v.len() <= 280 {
        v.to_string()
    } else {
        format!("{}...", &v[..277])
    }
}

fn compact_text(value: &str, max: usize) -> String {
    let normalized: String = value.split_whitespace().collect::<Vec<_>>().join(" ");
    if normalized.len() <= max {
        normalized
    } else {
        format!("{}...", &normalized[..max.saturating_sub(3)])
    }
}

fn sample<T: Clone>(vals: &[T], count: usize) -> Vec<T> {
    vals.iter().take(count).cloned().collect()
}

fn summarize_axis(id: AxisId, score: f64) -> String {
    match id {
        AxisId::Immediacy => {
            if score >= 0.78 {
                "A builder can reach first value quickly because the repo exposes concrete commands and local material."
            } else if score >= 0.58 {
                "There is a viable first run, but setup still asks for more context than a breakout launch should."
            } else {
                "First value is still buried behind setup, explanation, or external dependencies."
            }
        }
        AxisId::Clarity => {
            if score >= 0.78 {
                "The docs lead with a concrete outcome instead of making readers reverse-engineer the point."
            } else if score >= 0.58 {
                "The story is understandable, but it still leans too hard on features over a single killer use case."
            } else {
                "The public story is still diffuse enough that strangers will ask what the product actually does."
            }
        }
        AxisId::Proof => {
            if score >= 0.78 {
                "There is enough evidence in the repo to make the product feel like more than a demo."
            } else if score >= 0.58 {
                "The technical proof is real, but the repo still needs sharper public examples or case studies."
            } else {
                "The repo does not yet provide enough proof that the product changes real decisions."
            }
        }
        AxisId::Distribution => {
            if score >= 0.78 {
                "Install and update paths are strong enough that distribution will help instead of hurt the product."
            } else if score >= 0.58 {
                "There is a credible install path, but friction is still visible in packaging or runtime requirements."
            } else {
                "Distribution friction is still high enough to block curiosity before the product can impress anyone."
            }
        }
        AxisId::Shareability => {
            if score >= 0.78 {
                "Runs produce or imply artifacts that a builder can paste into a thread, doc, or PR without extra work."
            } else if score >= 0.58 {
                "The product can be explained publicly, but the repo still lacks enough instantly shareable proof objects."
            } else {
                "There is still too little output a builder would want to show another human."
            }
        }
        AxisId::Trust => {
            if score >= 0.78 {
                "The repo shows enough tests, CI, and release discipline to make strangers less defensive."
            } else if score >= 0.58 {
                "The repo feels serious, but some release or reliability signals are still missing from the first impression."
            } else {
                "A public launch would force builders to trust the product more than the repo currently earns."
            }
        }
    }
    .into()
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct RepoSignals {
    pub has_readme: f64,
    pub docs_score: f64,
    pub command_docs_score: f64,
    pub install_score: f64,
    pub local_mode_score: f64,
    pub outcome_score: f64,
    pub example_score: f64,
    pub artifact_score: f64,
    pub proof_score: f64,
    pub ci_score: f64,
    pub commit_score: f64,
    pub manifest_score: f64,
    pub lock_score: f64,
    pub changelog_score: f64,
    pub clean_score: f64,
    pub license_score: f64,
    pub env_score: f64,
    pub framework_bonus: f64,
    pub solana_bonus: f64,
    pub split_runtime_penalty: f64,
    pub external_dependency_penalty: f64,
}

pub fn derive_repo_scores(repo: &RepoContext) -> RepoSignals {
    let has_readme = if repo.readme_path.is_some() { 1.0 } else { 0.0 };
    let docs_score = clamp(repo.docs.len() as f64 / 8.0);
    let command_docs_score = clamp(
        (repo.install_commands.len() + repo.local_run_commands.len()) as f64 / 6.0,
    );
    let install_score = if !repo.install_commands.is_empty() {
        clamp(0.6 + repo.install_commands.len() as f64 / 8.0)
    } else if !repo.manifests.is_empty() {
        0.35
    } else {
        0.0
    };

    let has_local_non_curl = repo
        .local_run_commands
        .iter()
        .any(|c| !regex!(r"(?i)^curl\b").is_match(c));
    let has_fixtures_or_examples = !repo.fixtures.is_empty() || !repo.examples.is_empty();
    let local_mode_score = if has_local_non_curl
        && (has_fixtures_or_examples
            || repo.remote_dependency_notes.len() < repo.local_run_commands.len())
    {
        1.0
    } else if has_fixtures_or_examples {
        0.72
    } else {
        0.22
    };

    let doc_text = [
        repo.readme_excerpt.as_deref().unwrap_or(""),
        &repo.artifact_notes.join(" "),
        &repo.remote_dependency_notes.join(" "),
    ]
    .join(" ");

    let outcome_hits = regex!(r"(?i)\b(launch|ship|deploy|review|simulate|stress-test|decision|workflow|agent|builder|artifact|report|pr|spec)\b").find_iter(&doc_text).count();
    let artifact_text_hits = regex!(r"(?i)\b(report|artifact|decision|trace|html|markdown)\b").find_iter(&doc_text).count();

    let example_score = clamp((repo.examples.len() + repo.fixtures.len()) as f64 / 6.0);

    let artifact_base = if !repo.assets.is_empty() || !repo.artifact_notes.is_empty() {
        clamp(0.55 + (repo.assets.len() + repo.artifact_notes.len()) as f64 / 8.0)
    } else {
        0.0
    };
    let artifact_score = clamp(artifact_base.max(if artifact_text_hits > 0 { 0.58 } else { 0.0 }));

    let proof_score = clamp(repo.tests.len() as f64 / 10.0);
    let ci_score = if !repo.ci.is_empty() { 1.0 } else { 0.0 };
    let commit_score = clamp(repo.git.recent_commits.len() as f64 / 6.0);
    let manifest_score = clamp(repo.manifests.len() as f64 / 4.0);
    let lock_score = clamp(repo.locks.len() as f64 / 4.0);
    let changelog_score = if repo.docs.iter().any(|f| {
        Path::new(f)
            .file_name()
            .map(|n| n.to_string_lossy().to_uppercase().starts_with("CHANGELOG"))
            .unwrap_or(false)
    }) {
        1.0
    } else {
        0.0
    };
    let clean_score = if repo.git.branch.is_none() {
        0.7
    } else if repo.git.changed_files.is_empty() {
        1.0
    } else {
        clamp(1.0 - repo.git.changed_files.len() as f64 / 28.0)
    };
    let license_score = if !repo.licenses.is_empty() { 1.0 } else { 0.0 };
    let env_score = if !repo.env_examples.is_empty() {
        1.0
    } else {
        0.0
    };

    let mentions_cargo = repo
        .install_commands
        .iter()
        .any(|c| regex!(r"(?i)^cargo install\b").is_match(c));
    let mentions_node = repo
        .runtime_notes
        .iter()
        .any(|n| regex!(r"(?i)Node\.js|node 20|nodejs").is_match(n));
    let split_runtime_penalty = if mentions_cargo && mentions_node {
        0.24
    } else {
        0.0
    };
    let external_dependency_penalty = if repo.remote_dependency_notes.is_empty() {
        0.0
    } else if local_mode_score >= 0.7 {
        0.08
    } else {
        0.22
    };

    let has_solana = repo.frameworks.iter().any(|f| f.starts_with("solana"));
    let framework_bonus = clamp(repo.frameworks.len() as f64 / 5.0);
    let solana_bonus = if has_solana { 0.12 } else { 0.0 };

    RepoSignals {
        has_readme,
        docs_score,
        command_docs_score,
        install_score,
        local_mode_score,
        outcome_score: clamp(outcome_hits as f64 / 12.0),
        example_score,
        artifact_score,
        proof_score,
        ci_score,
        commit_score,
        manifest_score,
        lock_score,
        changelog_score,
        clean_score,
        license_score,
        env_score,
        framework_bonus,
        solana_bonus,
        split_runtime_penalty,
        external_dependency_penalty,
    }
}

pub fn build_axes(repo: &RepoContext, s: &RepoSignals) -> Vec<Axis> {
    let immediacy = clamp(
        0.32 * s.command_docs_score + 0.28 * s.local_mode_score + 0.22 * s.example_score
            + 0.18 * s.has_readme
            - s.external_dependency_penalty,
    );
    let clarity = clamp(
        0.34 * s.has_readme + 0.26 * s.command_docs_score + 0.22 * s.outcome_score
            + 0.18 * s.docs_score,
    );
    let proof = clamp(
        0.4 * s.proof_score + 0.25 * s.ci_score + 0.2 * s.example_score + 0.15 * s.commit_score,
    );
    let distribution = clamp(
        0.34 * s.install_score + 0.24 * s.manifest_score + 0.2 * s.lock_score
            + 0.22 * s.changelog_score
            + 0.08 * s.framework_bonus
            - s.split_runtime_penalty,
    );
    let shareability = clamp(
        0.34 * s.artifact_score + 0.24 * s.example_score + 0.2 * s.docs_score
            + 0.22 * s.command_docs_score,
    );
    let trust = clamp(
        0.35 * s.proof_score + 0.25 * s.ci_score + 0.15 * s.license_score
            + 0.15 * s.clean_score
            + 0.1 * s.env_score
            + 0.06 * s.framework_bonus,
    );

    let _ = repo;

    [
        (AxisId::Immediacy, immediacy),
        (AxisId::Clarity, clarity),
        (AxisId::Proof, proof),
        (AxisId::Distribution, distribution),
        (AxisId::Shareability, shareability),
        (AxisId::Trust, trust),
    ]
    .into_iter()
    .map(|(id, score)| Axis {
        label: id.label().into(),
        summary: summarize_axis(id, score),
        id,
        score,
    })
    .collect()
}

pub fn build_actions(axes: &[Axis]) -> Vec<String> {
    let action_for = |id: AxisId| -> &'static str {
        match id {
            AxisId::Immediacy => "Make one zero-config flow the public front door. If it needs a backend, ship a local mode or a public demo endpoint.",
            AxisId::Clarity => "Rewrite the README and launch copy around one user outcome, not the full command inventory.",
            AxisId::Proof => "Publish three real cases where the product changed a ship or no-ship decision.",
            AxisId::Distribution => "Pick one primary install path and demote extra runtime friction to the background.",
            AxisId::Shareability => "Emit HTML, Markdown, and JSON artifacts by default and give people a screenshot-worthy report.",
            AxisId::Trust => "Surface tests, CI, and hard runtime requirements in the first screen of the docs.",
        }
    };

    let mut sorted = axes.to_vec();
    sorted.sort_by(|a, b| a.score.total_cmp(&b.score).then(a.id.label().cmp(b.id.label())));

    let weakest: Vec<String> = sorted
        .iter()
        .filter(|a| a.score < 0.76)
        .map(|a| action_for(a.id).to_string())
        .collect();

    if !weakest.is_empty() {
        let mut seen = std::collections::HashSet::new();
        return weakest
            .into_iter()
            .filter(|s| seen.insert(s.clone()))
            .take(4)
            .collect();
    }

    vec![
        "Record a 90-second repo-to-report demo and pin it next to the install command.".into(),
        "Ship a GitHub Action or PR comment flow so the product lands inside existing builder habits.".into(),
        "Collect five external runs and turn the strongest one into a public case study.".into(),
    ]
}

pub fn build_branches(
    axes: &[Axis],
    actions: &[String],
    repo: &RepoContext,
) -> Vec<Branch> {
    let scores: std::collections::HashMap<AxisId, f64> =
        axes.iter().map(|a| (a.id, a.score)).collect();
    let readiness = average(&axes.iter().map(|a| a.score).collect::<Vec<_>>());
    let strength = average(&[scores[&AxisId::Immediacy], scores[&AxisId::Proof], scores[&AxisId::Trust]]);
    let weakest_gtm = [
        scores[&AxisId::Clarity],
        scores[&AxisId::Distribution],
        scores[&AxisId::Shareability],
    ]
    .iter()
    .copied()
    .fold(f64::INFINITY, f64::min);

    let branch_scores = [
        (BranchId::ShipNow, clamp(0.55 * readiness + 0.25 * weakest_gtm + 0.2 * strength)),
        (
            BranchId::NarrowLaunch,
            clamp(
                0.35 * strength + 0.25 * scores[&AxisId::Immediacy] + 0.2 * scores[&AxisId::Clarity]
                    + 0.2 * (1.0 - weakest_gtm),
            ),
        ),
        (
            BranchId::DelayForProof,
            clamp(
                0.4 * (1.0 - average(&[scores[&AxisId::Proof], scores[&AxisId::Trust]]))
                    + 0.2 * (1.0 - scores[&AxisId::Distribution])
                    + 0.2 * (1.0 - scores[&AxisId::Shareability])
                    + 0.2 * (1.0 - scores[&AxisId::Clarity]),
            ),
        ),
        (
            BranchId::ParkIt,
            clamp(
                0.55 * (1.0 - readiness)
                    + 0.25 * (1.0 - average(&[scores[&AxisId::Clarity], scores[&AxisId::Trust]]))
                    + 0.2 * (1.0 - scores[&AxisId::Proof]),
            ),
        ),
    ];

    let mut branches: Vec<Branch> = branch_scores
        .iter()
        .map(|&(id, score)| {
            build_single_branch(id, score, &scores, readiness, strength, weakest_gtm, actions, repo)
        })
        .collect();

    branches.sort_by(|a, b| {
        b.score
            .total_cmp(&a.score)
            .then(a.id.order().cmp(&b.id.order()))
    });
    branches
}

fn build_single_branch(
    id: BranchId,
    score: f64,
    scores: &std::collections::HashMap<AxisId, f64>,
    _readiness: f64,
    strength: f64,
    weakest_gtm: f64,
    actions: &[String],
    repo: &RepoContext,
) -> Branch {
    match id {
        BranchId::ShipNow => Branch {
            id,
            label: "Launch the current product now".into(),
            stance: "Broad launch".into(),
            score,
            summary: "Launch the full current surface now and learn in public without another major packaging pass.".into(),
            advantages: vec![
                format!("Immediacy is already at {}.", percent(scores[&AxisId::Immediacy])),
                format!("Trust and proof together average {}.", percent(average(&[scores[&AxisId::Trust], scores[&AxisId::Proof]]))),
                "You get real external signal immediately instead of optimizing in a vacuum.".into(),
            ],
            risks: vec![
                format!("The weakest go-to-market axis is still only {}.", percent(weakest_gtm)),
                "You will spend launch energy explaining the product instead of showing one impossible-to-miss use case.".into(),
            ],
            next_moves: vec![
                "Lead with the generated report artifact, not the command list.".into(),
                "Record one repo-to-report walkthrough before the announcement thread.".into(),
                "Treat the first five external runs as message refinement, not validation theater.".into(),
            ],
        },
        BranchId::NarrowLaunch => Branch {
            id,
            label: "Launch one impossible-to-miss workflow".into(),
            stance: "Flagship launch".into(),
            score,
            summary: "Make one repo-native workflow the product, and demote everything else to supporting machinery.".into(),
            advantages: vec![
                format!("Core strength is already {} across immediacy, proof, and trust.", percent(strength)),
                "You can force the public story to match the strongest technical surface.".into(),
                "The HTML, Markdown, and JSON artifact path becomes the thing people remember and share.".into(),
            ],
            risks: vec![
                "You have to cut or hide commands that do not reinforce the flagship path.".into(),
                "Breadth will look smaller at launch, even if the product is stronger.".into(),
            ],
            next_moves: vec![
                format!("Make `reality-fork run launch --repo .` the front door for {}.", repo.name),
                "Move secondary commands below the flagship workflow in docs and posts.".into(),
                actions.first().cloned().unwrap_or_else(|| "Ship one public case study built from a real run artifact.".into()),
            ],
        },
        BranchId::DelayForProof => Branch {
            id,
            label: "Delay and harden".into(),
            stance: "Proof-first".into(),
            score,
            summary: "Hold the broad public launch until the product has stronger external proof, packaging, and trust signals.".into(),
            advantages: vec![
                "You avoid burning audience attention on a message that still needs another pass.".into(),
                "You buy time to turn strong internals into undeniable public proof.".into(),
            ],
            risks: vec![
                "Momentum cools off if the hardening phase drifts without a deadline.".into(),
                "The team may hide behind polish work instead of confronting the product wedge.".into(),
            ],
            next_moves: vec![
                actions.first().cloned().unwrap_or_else(|| "Close the weakest public axis first.".into()),
                actions.get(1).cloned().unwrap_or_else(|| "Publish a real case study before reopening launch planning.".into()),
                "Set a brutal ship gate: if a builder is not impressed in three minutes, the launch is still early.".into(),
            ],
        },
        BranchId::ParkIt => Branch {
            id,
            label: "Park the product".into(),
            stance: "No launch".into(),
            score,
            summary: "Stop spending launch calories until the wedge is sharper and the product earns attention on first contact.".into(),
            advantages: vec![
                "You avoid a weak public story calcifying around the project.".into(),
                "The team can extract the strongest primitives without pretending they are already a product.".into(),
            ],
            risks: vec![
                "You lose external learning entirely for this cycle.".into(),
                "The product can become a permanent internal tool if there is no return date.".into(),
            ],
            next_moves: vec![
                "Freeze launch work and write down the one future use case worth reviving.".into(),
                "Keep only the primitives that support that wedge.".into(),
                "Reopen launch planning only when the first-run artifact is strong enough to post without apology.".into(),
            ],
        },
    }
}

pub fn verdict_reason(branch: &Branch, axes: &[Axis], actions: &[String]) -> String {
    let mut by_score_desc = axes.to_vec();
    by_score_desc.sort_by(|a, b| b.score.total_cmp(&a.score).then(a.id.label().cmp(b.id.label())));
    let strengths: Vec<String> = by_score_desc
        .iter()
        .take(2)
        .map(|a| a.id.label().to_lowercase())
        .collect();

    let mut by_score_asc = axes.to_vec();
    by_score_asc.sort_by(|a, b| a.score.total_cmp(&b.score).then(a.id.label().cmp(b.id.label())));
    let weakest: Vec<String> = by_score_asc
        .iter()
        .take(2)
        .map(|a| a.id.label().to_lowercase())
        .collect();

    let s0 = strengths.first().map(|s| s.as_str()).unwrap_or("immediacy");
    let s1 = strengths.get(1).map(|s| s.as_str()).unwrap_or("proof");
    let w0 = weakest.first().map(|s| s.as_str()).unwrap_or("clarity");
    let w1 = weakest.get(1).map(|s| s.as_str()).unwrap_or("distribution");

    match branch.id {
        BranchId::ShipNow => format!(
            "The weakest outward-facing axis is strong enough to support a broad launch, and the repo already shows real {} and {}.",
            s0, s1
        ),
        BranchId::NarrowLaunch => format!(
            "The core engine is credible, but the strongest external story is still one flagship workflow. {}",
            actions.first().map(|s| s.as_str()).unwrap_or("Lead with one impossible-to-miss path.")
        ),
        BranchId::DelayForProof => format!(
            "The current repo is still too weak on {} and {} for a broad public push. Shipping now would create more confusion than pull.",
            w0, w1
        ),
        BranchId::ParkIt => format!(
            "The wedge is not sharp enough yet. The repo is still weakest on {} and {}, so launch work would mostly be noise.",
            w0, w1
        ),
    }
}

pub fn build_signals(
    repo: &RepoContext,
    scores: &RepoSignals,
    axes: &[Axis],
) -> Vec<Signal> {
    let mut signals = Vec::new();

    if !repo.install_commands.is_empty() || !repo.local_run_commands.is_empty() {
        let mut citations: Vec<String> = Vec::new();
        if let Some(rp) = &repo.readme_path {
            citations.push(rp.clone());
        }
        citations.extend(repo.docs.iter().take(3).cloned());
        signals.push(Signal {
            id: "doc-commands".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Immediacy,
            statement: "Docs expose concrete commands instead of forcing builders to start from source.".into(),
            detail: format!(
                "Found {} install commands and {} run commands in the docs.",
                repo.install_commands.len(),
                repo.local_run_commands.len()
            ),
            weight: 0.88,
            citations: sample(&citations, 3),
            inferred: false,
        });
    }

    if !repo.examples.is_empty() || !repo.fixtures.is_empty() {
        let mut cites: Vec<String> = repo.examples.iter().chain(repo.fixtures.iter()).cloned().collect();
        cites.truncate(4);
        signals.push(Signal {
            id: "local-material".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Shareability,
            statement: "The repo already contains local material a builder can touch on the first run.".into(),
            detail: format!(
                "Found {} example paths and {} fixture paths.",
                repo.examples.len(),
                repo.fixtures.len()
            ),
            weight: 0.81,
            citations: cites,
            inferred: false,
        });
    }

    if !repo.tests.is_empty() {
        signals.push(Signal {
            id: "tests-present".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Proof,
            statement: "The repo carries technical proof instead of pure positioning.".into(),
            detail: format!("Found {} test files in the scanned tree.", repo.tests.len()),
            weight: 0.86,
            citations: sample(&repo.tests, 4),
            inferred: false,
        });
    }

    if !repo.ci.is_empty() {
        signals.push(Signal {
            id: "ci-present".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Trust,
            statement: "Release discipline is visible from the repo surface.".into(),
            detail: format!("Found {} CI configuration files.", repo.ci.len()),
            weight: 0.73,
            citations: sample(&repo.ci, 3),
            inferred: false,
        });
    }

    if repo.git.changed_files.is_empty() && repo.git.commit.is_some() {
        signals.push(Signal {
            id: "clean-tree".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Trust,
            statement: "The working tree is clean at the time of analysis.".into(),
            detail: format!(
                "No uncommitted changes were detected on {}.",
                repo.git.branch.as_deref().unwrap_or("the current branch")
            ),
            weight: 0.62,
            citations: vec!["git:status".into()],
            inferred: false,
        });
    }

    if !repo.licenses.is_empty() {
        signals.push(Signal {
            id: "license-present".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Trust,
            statement: "The repo includes an explicit license surface.".into(),
            detail: format!(
                "Found {} license file{}.",
                repo.licenses.len(),
                if repo.licenses.len() == 1 { "" } else { "s" }
            ),
            weight: 0.58,
            citations: sample(&repo.licenses, 2),
            inferred: false,
        });
    }

    if !repo.frameworks.is_empty() {
        let solana: Vec<&String> = repo.frameworks.iter().filter(|f| f.starts_with("solana")).collect();
        let label = if !solana.is_empty() {
            format!(
                "Solana ecosystem detected ({})",
                solana.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ")
            )
        } else {
            format!("Recognized frameworks: {}", repo.frameworks.join(", "))
        };
        signals.push(Signal {
            id: "framework-detected".into(),
            signal_type: SignalType::Supporting,
            axis: AxisId::Distribution,
            statement: label,
            detail: format!(
                "Detected {} framework{} from project markers.",
                repo.frameworks.len(),
                if repo.frameworks.len() == 1 { "" } else { "s" }
            ),
            weight: if !solana.is_empty() { 0.85 } else { 0.78 },
            citations: vec![],
            inferred: false,
        });
    }

    if !repo.remote_dependency_notes.is_empty() {
        signals.push(Signal {
            id: "remote-dependency".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Immediacy,
            statement: "Advanced flows still depend on a separate API surface.".into(),
            detail: compact_text(repo.remote_dependency_notes.first().unwrap_or(&String::new()), 180),
            weight: 0.93,
            citations: sample(
                &repo
                    .remote_dependency_notes
                    .iter()
                    .filter_map(|n| n.split(": ").next().map(String::from))
                    .collect::<Vec<_>>(),
                3,
            ),
            inferred: false,
        });
    }

    if scores.split_runtime_penalty > 0.0 {
        signals.push(Signal {
            id: "split-runtime".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Distribution,
            statement: "The public install path still exposes multi-runtime friction.".into(),
            detail: "The docs mention Cargo install and a Node runtime requirement together.".into(),
            weight: 0.89,
            citations: sample(
                &repo
                    .runtime_notes
                    .iter()
                    .filter_map(|n| n.split(": ").next().map(String::from))
                    .collect::<Vec<_>>(),
                3,
            ),
            inferred: false,
        });
    }

    if !repo.git.changed_files.is_empty() {
        signals.push(Signal {
            id: "dirty-tree".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Trust,
            statement: "The repo is not launch-clean right now.".into(),
            detail: format!(
                "{} changed file{} were detected in git status.",
                repo.git.changed_files.len(),
                if repo.git.changed_files.len() == 1 { "" } else { "s" }
            ),
            weight: 0.77,
            citations: vec!["git:status".into()],
            inferred: false,
        });
    }

    let shareability_score = axes
        .iter()
        .find(|a| a.id == AxisId::Shareability)
        .map(|a| a.score)
        .unwrap_or(0.0);
    if shareability_score < 0.66 {
        let mut cites = Vec::new();
        if let Some(rp) = &repo.readme_path {
            cites.push(rp.clone());
        }
        cites.extend(repo.assets.iter().take(3).cloned());
        signals.push(Signal {
            id: "artifact-gap".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Shareability,
            statement: "The repo still lacks enough instantly shareable proof objects.".into(),
            detail: "There are not yet enough visible report, screenshot, or public artifact cues in the repo surface.".into(),
            weight: 0.84,
            citations: sample(&cites, 3),
            inferred: true,
        });
    }

    let clarity_score = axes
        .iter()
        .find(|a| a.id == AxisId::Clarity)
        .map(|a| a.score)
        .unwrap_or(0.0);
    if clarity_score < 0.68 {
        let mut cites = Vec::new();
        if let Some(rp) = &repo.readme_path {
            cites.push(rp.clone());
        }
        cites.extend(repo.docs.iter().take(3).cloned());
        signals.push(Signal {
            id: "story-gap".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Clarity,
            statement: "The public story still reads weaker than the underlying engineering.".into(),
            detail: "The docs expose commands and features, but the breakout user outcome is still not obvious enough.".into(),
            weight: 0.82,
            citations: sample(&cites, 3),
            inferred: true,
        });
    }

    let proof_score_val = axes
        .iter()
        .find(|a| a.id == AxisId::Proof)
        .map(|a| a.score)
        .unwrap_or(0.0);
    if proof_score_val < 0.62 {
        signals.push(Signal {
            id: "proof-gap".into(),
            signal_type: SignalType::Risk,
            axis: AxisId::Proof,
            statement: "The repo still needs more public proof that the product changes decisions.".into(),
            detail: "Tests alone do not create external demand; case studies and concrete caught-failures are still missing.".into(),
            weight: 0.78,
            citations: sample(&repo.tests, 3),
            inferred: true,
        });
    }

    signals.sort_by(|a, b| {
        b.weight
            .total_cmp(&a.weight)
            .then(a.id.cmp(&b.id))
    });
    signals
}

pub fn build_posts(
    repo: &RepoContext,
    branch: &Branch,
    verdict: &Verdict,
    axes: &[Axis],
) -> Posts {
    let mut by_score_desc = axes.to_vec();
    by_score_desc.sort_by(|a, b| b.score.total_cmp(&a.score).then(a.id.label().cmp(b.id.label())));
    let top_axes: Vec<String> = by_score_desc
        .iter()
        .take(2)
        .map(|a| format!("{} {}", a.id.label(), percent(a.score)))
        .collect();

    let mut by_score_asc = axes.to_vec();
    by_score_asc.sort_by(|a, b| a.score.total_cmp(&b.score).then(a.id.label().cmp(b.id.label())));
    let weak_axes: Vec<String> = by_score_asc
        .iter()
        .take(2)
        .map(|a| format!("{} {}", a.id.label(), percent(a.score)))
        .collect();

    let announcement = trim_post(&format!(
        "Reality Fork launch verdict for {}: {}. {} Top signals: {}.",
        repo.name,
        branch.label,
        verdict.reason,
        top_axes.join(" | ")
    ));

    let thread = vec![
        trim_post(&format!(
            "Reality Fork scored {} at {} launch readiness. Verdict: {}.",
            repo.name,
            percent(verdict.readiness),
            branch.label
        )),
        trim_post(&format!(
            "Strongest signals: {}. Weakest: {}.",
            top_axes.join(" | "),
            weak_axes.join(" | ")
        )),
        trim_post(&format!("Next move: {}", branch.next_moves.first().map(|s| s.as_str()).unwrap_or("Ship the strongest artifact path first."))),
    ];

    Posts {
        announcement,
        thread,
    }
}

pub fn create_launch_run_with(
    repo: RepoContext,
    prompt: Option<String>,
    title: Option<String>,
) -> LaunchRun {
    let scores = derive_repo_scores(&repo);
    let axes = build_axes(&repo, &scores);
    let actions = build_actions(&axes);
    let branches = build_branches(&axes, &actions, &repo);
    let winner = &branches[0]; // always 4 branches from build_branches
    let readiness = average(&axes.iter().map(|a| a.score).collect::<Vec<_>>());
    let reason = verdict_reason(winner, &axes, &actions);

    let verdict = Verdict {
        winner_branch_id: winner.id,
        label: winner.label.clone(),
        reason,
        score: winner.score,
        readiness,
    };

    let posts = build_posts(&repo, winner, &verdict, &axes);
    let signals = build_signals(&repo, &scores, &axes);

    let prompt_text = prompt
        .filter(|s| !s.trim().is_empty())
        .unwrap_or_else(|| "Should we ship this now?".into());
    let title_text = title
        .filter(|s| !s.trim().is_empty())
        .unwrap_or_else(|| format!("{} launch reality fork", repo.name));

    LaunchRun {
        kind: "launch".into(),
        version: 1,
        generated_at: chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
        title: title_text,
        prompt: prompt_text,
        repo,
        axes,
        signals,
        branches,
        verdict,
        actions,
        posts,
    }
}