kranz-server 0.4.2

REST and WebSocket mission host for Kranz.
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
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
//! `GET /api/tickets` and `GET /api/tickets/:slug` — read-only backlog
//! surface over `.kranz/tickets/` (docs/protocol.md). Every ticket is
//! re-parsed from disk per request, matching the rest of this crate's
//! no-cache discipline.

use crate::error::ApiError;
use crate::read_work::ReadWork;
use crate::ServerState;
use axum::body::Bytes;
use axum::extract::{Path as UrlPath, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{Extension, Json};
use kranz_engine::ticket::Ticket;
use serde_json::{json, Value};
use std::sync::Arc;

/// `GET /api/tickets` — a summary row per parseable ticket under
/// `.kranz/tickets/`: slug, priority, pipeline state, title, and blockedBy.
pub(crate) async fn list_tickets(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            let rows: Vec<Value> = Ticket::list(server.host.repo_root())
                .iter()
                .map(|ticket| ticket_summary_json(server.host.repo_root(), ticket))
                .collect();
            Ok(Json(Value::Array(rows)))
        })
        .await
}

/// `GET /api/tickets/:slug` — the full parsed ticket plus `needsContext`
/// (the orchestrator's clarifying questions, if any were appended) and
/// `wrongPlan` (the planner's escalation reason, likewise). 400 for an
/// invalid/traversal slug (checked at the route boundary before any
/// filesystem access), 404 for a slug with no ticket file.
pub(crate) async fn get_ticket(
    Extension(reads): Extension<ReadWork>,
    State(server): State<Arc<ServerState>>,
    UrlPath(slug): UrlPath<String>,
) -> Result<Json<Value>, ApiError> {
    reads
        .run(move || {
            Ticket::ensure_valid_slug(&slug)
                .map_err(|e| ApiError::bad_request(format!("invalid ticket slug '{slug}': {e}")))?;

            let path = Ticket::tickets_dir(server.host.repo_root()).join(format!("{slug}.md"));
            if !path.is_file() {
                return Err(ApiError::not_found(format!("unknown ticket '{slug}'")));
            }
            let ticket = Ticket::load(&path)
                .map_err(|e| ApiError::internal(format!("failed to parse ticket '{slug}': {e}")))?;

            Ok(Json(ticket_full_json(server.host.repo_root(), &ticket)))
        })
        .await
}

/// `POST /api/tickets/:slug/draft` — long-running: mirrors `POST
/// /api/missions/:id/start` by creating the planning mission synchronously
/// (so a real mission id is available for the response) and spawning the
/// draft turns as a background task via [`kranz_server::MissionHost::draft_async`].
/// `202 {"missionId":"m-…"}`; progress is observable over that mission's
/// `GET /api/missions/:id/ws` feed and the terminal outcome (Review vs
/// NeedsContext) via `GET /api/tickets/:slug`. 400 for an invalid slug, 404
/// for a missing ticket — both checked synchronously before anything spawns.
pub(crate) async fn draft_ticket(
    State(server): State<Arc<ServerState>>,
    UrlPath(slug): UrlPath<String>,
) -> Result<impl IntoResponse, ApiError> {
    Ticket::ensure_valid_slug(&slug)
        .map_err(|e| ApiError::bad_request(format!("invalid ticket slug '{slug}': {e}")))?;
    let mission_id = server.host.draft_async(&slug, false).await?;
    Ok((
        StatusCode::ACCEPTED,
        Json(json!({ "missionId": mission_id })),
    ))
}

/// `POST /api/tickets` — create a ticket from `{"slug", "title", "goal"?,
/// "context"?}`. Scaffolds `.kranz/tickets/<slug>.md` via
/// [`kranz_engine::ticket::Ticket::scaffold`] — the same template the CLI's
/// `kranz ticket new` uses, so goal/context land in the ticket body and a
/// subsequent `GET /api/tickets/:slug` round-trips them. 400 for an invalid
/// slug or a missing/blank `title`, 409 if the slug already has a ticket,
/// `201` with the created ticket's summary JSON (same shape as a list row).
pub(crate) async fn create_ticket(
    State(server): State<Arc<ServerState>>,
    body: Bytes,
) -> Result<impl IntoResponse, ApiError> {
    let value = crate::host::parse_body(&body)?;
    let slug = value
        .get("slug")
        .and_then(Value::as_str)
        .filter(|s| !s.is_empty())
        .ok_or_else(|| ApiError::bad_request("missing required field 'slug'"))?;
    let title = value
        .get("title")
        .and_then(Value::as_str)
        .filter(|s| !s.is_empty())
        .ok_or_else(|| ApiError::bad_request("missing required field 'title'"))?;
    let goal = value.get("goal").and_then(Value::as_str);
    let context = value.get("context").and_then(Value::as_str);

    Ticket::ensure_valid_slug(slug)
        .map_err(|e| ApiError::bad_request(format!("invalid ticket slug '{slug}': {e}")))?;

    let path = Ticket::scaffold(server.host.repo_root(), slug, title, goal, context)?;
    let ticket = Ticket::load(&path).map_err(|e| {
        ApiError::internal(format!("failed to parse scaffolded ticket '{slug}': {e}"))
    })?;

    Ok((
        StatusCode::CREATED,
        Json(ticket_summary_json(server.host.repo_root(), &ticket)),
    ))
}

/// `POST /api/tickets/:slug/approve` — optional body `{"force": bool}`
/// (default `false`) → `200 {"approved":true,"missionId":"m-…"}`. Runs the
/// same gate the CLI's `kranz ticket approve` runs
/// ([`kranz_engine::deps::approve_ticket`]): 409 naming the unsatisfied
/// blocker when blocked and `force` is false; 409 with the cycle path on a
/// `blocked-by` cycle even when `force` is true; 409 when the ticket is not
/// in Review.
pub(crate) async fn approve_ticket(
    State(server): State<Arc<ServerState>>,
    UrlPath(slug): UrlPath<String>,
    body: Bytes,
) -> Result<Json<Value>, ApiError> {
    Ticket::ensure_valid_slug(&slug)
        .map_err(|e| ApiError::bad_request(format!("invalid ticket slug '{slug}': {e}")))?;
    let value = crate::host::parse_body(&body)?;
    let force = value.get("force").and_then(Value::as_bool).unwrap_or(false);
    let approved = server.host.approve_ticket(&slug, force)?;
    Ok(Json(
        json!({ "approved": true, "missionId": approved.mission_id }),
    ))
}

/// Summary projection for the list route: no goal/context/body text, just
/// enough to render a backlog table.
fn ticket_summary_json(repo_root: &std::path::Path, ticket: &Ticket) -> Value {
    let state = Ticket::read_state(repo_root, &ticket.slug);
    json!({
        "slug": ticket.slug,
        "priority": ticket.priority,
        "state": state,
        "title": ticket.title,
        "blockedBy": ticket.blocked_by,
        "trigger": ticket.trigger,
        "isBlocked": kranz_engine::deps::is_blocked(repo_root, &ticket.slug).unwrap_or(false),
        "missionId": Ticket::mission_for(repo_root, &ticket.slug),
        "merged": kranz_engine::merged::ticket_merged(repo_root, &ticket.slug),
    })
}

/// Full projection for the show route: every parsed field plus the derived
/// `needsContext` question list and the `wrongPlan` escalation reason.
fn ticket_full_json(repo_root: &std::path::Path, ticket: &Ticket) -> Value {
    let state = Ticket::read_state(repo_root, &ticket.slug);
    json!({
        "slug": ticket.slug,
        "title": ticket.title,
        "priority": ticket.priority,
        "schedule": ticket.schedule,
        "blockedBy": ticket.blocked_by,
        "goal": ticket.goal,
        "context": ticket.context,
        "scopingAnswers": ticket.scoping_answers,
        "acceptanceHints": ticket.acceptance_hints,
        "taskClass": ticket.task_class,
        "reviewArtifact": ticket.review_artifact,
        "reviewOutput": ticket.review_output,
        "trigger": ticket.trigger,
        "state": state,
        "needsContext": needs_context_questions(&ticket.raw_body),
        "wrongPlan": wrong_plan_reason(&ticket.raw_body),
        "isBlocked": kranz_engine::deps::is_blocked(repo_root, &ticket.slug).unwrap_or(false),
        "missionId": Ticket::mission_for(repo_root, &ticket.slug),
        "merged": kranz_engine::merged::ticket_merged(repo_root, &ticket.slug),
    })
}

/// Port of `kranz_cli::backlog::section_block`'s heading scan, but
/// returning the bare question strings (bullet items, marker stripped)
/// instead of the raw markdown block — a REST consumer wants data, not
/// text to re-render.
fn needs_context_questions(raw_body: &str) -> Vec<String> {
    let mut collecting = false;
    let mut out = Vec::new();
    for line in raw_body.lines() {
        let is_section = line.trim_start().starts_with("##");
        if collecting && is_section {
            break; // next section ends the block
        }
        if is_section
            && line
                .trim_start_matches('#')
                .trim()
                .to_ascii_lowercase()
                .starts_with("needs context")
        {
            collecting = true;
            continue;
        }
        if collecting {
            if let Some(item) = bullet_item(line) {
                out.push(item);
            }
        }
    }
    out
}

/// The planner's wrong-plan escalation reason: the text of the `## Wrong
/// plan (from orchestrator)` section (verbatim paragraph, not bullets), if
/// one was appended. Same heading-scan port family as
/// [`needs_context_questions`].
fn wrong_plan_reason(raw_body: &str) -> Option<String> {
    let mut collecting = false;
    let mut out: Vec<&str> = Vec::new();
    for line in raw_body.lines() {
        let is_section = line.trim_start().starts_with("##");
        if collecting && is_section {
            break; // next section ends the block
        }
        if is_section
            && line
                .trim_start_matches('#')
                .trim()
                .to_ascii_lowercase()
                .starts_with("wrong plan")
        {
            collecting = true;
            continue;
        }
        if collecting {
            out.push(line);
        }
    }
    let text = out.join("\n").trim().to_string();
    if text.is_empty() {
        None
    } else {
        Some(text)
    }
}

/// The content of a dash bullet (`- item`), trimmed, else None. Mirrors
/// `kranz_engine::ticket`'s private helper of the same name.
fn bullet_item(line: &str) -> Option<String> {
    let t = line.trim_start();
    for marker in ["- ", "* ", "+ "] {
        if let Some(rest) = t.strip_prefix(marker) {
            let item = rest.trim().to_string();
            if !item.is_empty() {
                return Some(item);
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use http_body_util::BodyExt;
    use kranz_engine::event_log::{EventLog, LockForce};
    use kranz_engine::events::EventKind;
    use kranz_engine::paths::MissionPaths;
    use kranz_engine::ticket::TicketState;
    use kranz_engine::types::{
        Assertion, AssertionCheck, MissionConfig, Plan, PlanFeature, PlanMilestone,
    };
    use std::path::{Path, PathBuf};
    use std::time::Duration;
    use tempfile::TempDir;
    use tower::ServiceExt;

    const TICKET_BODY: &str = "\
---
title: Sample
priority: 2
---

## Goal
Ship the thing.
";

    fn write_ticket(repo: &std::path::Path, slug: &str) {
        let dir = Ticket::tickets_dir(repo);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join(format!("{slug}.md")), TICKET_BODY).unwrap();
    }

    fn load(repo: &std::path::Path, slug: &str) -> Ticket {
        Ticket::load(&Ticket::tickets_dir(repo).join(format!("{slug}.md"))).unwrap()
    }

    #[test]
    fn pipeline_summary_json_surfaces_recorded_mission_id() {
        let tmp = TempDir::new().unwrap();
        write_ticket(tmp.path(), "linked");
        Ticket::record_mission(tmp.path(), "linked", "m-abc123").unwrap();

        let ticket = load(tmp.path(), "linked");
        let json = ticket_summary_json(tmp.path(), &ticket);
        assert_eq!(json["missionId"], "m-abc123");
    }

    #[test]
    fn pipeline_full_json_surfaces_recorded_mission_id() {
        let tmp = TempDir::new().unwrap();
        write_ticket(tmp.path(), "linked");
        Ticket::record_mission(tmp.path(), "linked", "m-abc123").unwrap();

        let ticket = load(tmp.path(), "linked");
        let json = ticket_full_json(tmp.path(), &ticket);
        assert_eq!(json["missionId"], "m-abc123");
    }

    #[test]
    fn flight_rules_review_class_ticket_json_surfaces_artifact_contract() {
        let tmp = TempDir::new().unwrap();
        let dir = Ticket::tickets_dir(tmp.path());
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("spec.md"),
            "---\ntitle: Review spec\ntask-class: spec-review\nreview-artifact: docs/spec.md\nreview-output: reviews/spec.md\n---\n\n## Goal\nReview it.\n",
        )
        .unwrap();
        let ticket = load(tmp.path(), "spec");
        let json = ticket_full_json(tmp.path(), &ticket);
        assert_eq!(json["taskClass"], "spec-review");
        assert_eq!(json["reviewArtifact"], "docs/spec.md");
        assert_eq!(json["reviewOutput"], "reviews/spec.md");
    }

    #[test]
    fn pipeline_missing_mission_id_is_null_in_both_projections() {
        let tmp = TempDir::new().unwrap();
        write_ticket(tmp.path(), "unlinked");

        let ticket = load(tmp.path(), "unlinked");
        assert_eq!(
            ticket_summary_json(tmp.path(), &ticket)["missionId"],
            Value::Null
        );
        assert_eq!(
            ticket_full_json(tmp.path(), &ticket)["missionId"],
            Value::Null
        );
    }

    async fn body_json(response: axum::response::Response) -> Value {
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        serde_json::from_slice(&bytes).unwrap()
    }

    fn post(uri: &str, body: Value) -> Request<Body> {
        Request::builder()
            .method("POST")
            .uri(uri)
            .header("content-type", "application/json")
            .header(crate::TOKEN_HEADER, "tok")
            .body(Body::from(body.to_string()))
            .unwrap()
    }

    #[tokio::test]
    async fn pipeline_create_ticket_succeeds_and_appears_in_list_as_new() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router_with_token(
            tmp.path().to_path_buf(),
            None,
            crate::MutationAuthority::new("tok").unwrap(),
        );

        let response = app
            .clone()
            .oneshot(post(
                "/api/tickets",
                json!({"slug": "new-thing", "title": "New Thing"}),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::CREATED);
        let created = body_json(response).await;
        assert_eq!(created["slug"], "new-thing");
        assert_eq!(created["title"], "New Thing");
        assert_eq!(created["state"], "new");
        assert_eq!(created["missionId"], Value::Null);

        let list_response = app
            .oneshot(
                Request::builder()
                    .uri("/api/tickets")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(list_response.status(), StatusCode::OK);
        let rows = body_json(list_response).await;
        let rows = rows.as_array().unwrap();
        let row = rows
            .iter()
            .find(|r| r["slug"] == "new-thing")
            .expect("created ticket present in list");
        assert_eq!(row["state"], "new");
    }

    #[tokio::test]
    async fn pipeline_create_ticket_invalid_slug_returns_400() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router_with_token(
            tmp.path().to_path_buf(),
            None,
            crate::MutationAuthority::new("tok").unwrap(),
        );

        let response = app
            .oneshot(post(
                "/api/tickets",
                json!({"slug": "../escape", "title": "Bad"}),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn pipeline_create_ticket_duplicate_slug_returns_409() {
        let tmp = TempDir::new().unwrap();
        write_ticket(tmp.path(), "dup");
        let app = crate::router_with_token(
            tmp.path().to_path_buf(),
            None,
            crate::MutationAuthority::new("tok").unwrap(),
        );

        let response = app
            .oneshot(post(
                "/api/tickets",
                json!({"slug": "dup", "title": "Duplicate"}),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::CONFLICT);
    }

    #[tokio::test]
    async fn pipeline_create_ticket_goal_and_context_round_trip_through_get() {
        let tmp = TempDir::new().unwrap();
        let app = crate::router_with_token(
            tmp.path().to_path_buf(),
            None,
            crate::MutationAuthority::new("tok").unwrap(),
        );

        let response = app
            .clone()
            .oneshot(post(
                "/api/tickets",
                json!({
                    "slug": "round-trip",
                    "title": "Round Trip",
                    "goal": "ship the thing",
                    "context": "some background",
                }),
            ))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::CREATED);

        let get_response = app
            .oneshot(
                Request::builder()
                    .uri("/api/tickets/round-trip")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(get_response.status(), StatusCode::OK);
        let json = body_json(get_response).await;
        assert_eq!(json["goal"], "ship the thing");
        assert_eq!(json["context"], "some background");
    }

    // -----------------------------------------------------------------
    // `merged` bit on the ticket projection (F1.2)
    // -----------------------------------------------------------------

    static ENV_ISOLATION: std::sync::Once = std::sync::Once::new();

    /// Mask the host's global/system git config so identity, signing and
    /// hooks never leak into the throwaway repos (mirrors
    /// crates/server/tests/server_test.rs's isolate_git_env).
    fn isolate_git_env() {
        ENV_ISOLATION.call_once(|| {
            let missing = std::env::temp_dir().join(format!(
                "kranz-tickets-test-no-config-{}",
                std::process::id()
            ));
            std::env::set_var("GIT_CONFIG_GLOBAL", &missing);
            std::env::set_var("GIT_CONFIG_SYSTEM", &missing);
            if let Ok(ceiling) = std::fs::canonicalize(std::env::temp_dir()) {
                std::env::set_var("GIT_CEILING_DIRECTORIES", ceiling);
            }
        });
    }

    fn git_available() -> bool {
        std::process::Command::new("git")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn setup() -> bool {
        isolate_git_env();
        if git_available() {
            true
        } else {
            kranz_engine::test_capability::skip(
                kranz_engine::test_capability::capability::GIT,
                "git is not on PATH",
            );
            false
        }
    }

    fn raw_git(dir: &Path, args: &[&str]) -> String {
        let out = std::process::Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .expect("spawn git");
        assert!(
            out.status.success(),
            "git {args:?} failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        String::from_utf8_lossy(&out.stdout).into_owned()
    }

    /// Fresh repo on branch `main` with one seed commit; returns
    /// (tempdir, canonicalized root, seed commit sha).
    fn init_repo() -> (TempDir, PathBuf, String) {
        let dir = TempDir::new().unwrap();
        let init = std::process::Command::new("git")
            .args(["init", "-b", "main"])
            .current_dir(dir.path())
            .output()
            .expect("spawn git init");
        if !init.status.success() {
            raw_git(dir.path(), &["init"]);
            raw_git(dir.path(), &["symbolic-ref", "HEAD", "refs/heads/main"]);
        }
        raw_git(dir.path(), &["config", "user.name", "test"]);
        raw_git(dir.path(), &["config", "user.email", "test@example.com"]);
        std::fs::write(dir.path().join("README.md"), "seed\n").unwrap();
        raw_git(dir.path(), &["add", "-A"]);
        raw_git(dir.path(), &["commit", "-m", "seed"]);
        let root = std::fs::canonicalize(dir.path()).expect("canonicalize repo root");
        let base_sha = raw_git(&root, &["rev-parse", "HEAD"]).trim().to_string();
        (dir, root, base_sha)
    }

    fn sample_plan() -> Plan {
        Plan {
            goal: "Ship the demo".into(),
            validation_contract: vec![Assertion {
                id: "a-1".into(),
                statement: "cargo test passes".into(),
                check: AssertionCheck::Command,
                command: Some("cargo test".into()),
                pty_script: None,
                negative_control: None,
            }],
            milestones: vec![PlanMilestone {
                title: "M1".into(),
                features: vec![PlanFeature {
                    title: "F1".into(),
                    spec: "build the thing".into(),
                    validation_criteria: vec!["it works".into()],
                }],
            }],
            considered_alternatives: None,
            command_grants: vec![],
            touch_set: vec![],
            standards_manifest: None,
            reviewer_independence: None,
        }
    }

    /// Seed a mission whose plan is approved (base_sha pinned to the repo's
    /// seed commit) and whose event log then folds to `MissionStatus::Complete`.
    /// When `create_branch` and `merge_into_base` are both true, the mission
    /// branch is merged into `main` before the events are appended.
    fn seed_complete_mission(
        repo_root: &Path,
        id: &str,
        base_sha: &str,
        create_branch: bool,
        merge_into_base: bool,
    ) {
        let paths = MissionPaths::new(repo_root, id);
        let branch = format!("kranz/mission-{id}");
        let mut log = EventLog::acquire(&paths, id, Duration::ZERO, LockForce::No).unwrap();
        log.append(EventKind::MissionCreated {
            goal: "Ship the demo".into(),
            base_branch: "main".into(),
            mission_branch: branch.clone(),
            config: MissionConfig::default(),
        })
        .unwrap();
        log.append(EventKind::PlanApproved {
            plan: sample_plan(),
            base_sha: Some(base_sha.to_string()),
        })
        .unwrap();
        log.append(EventKind::MissionCompleted {}).unwrap();
        drop(log);

        if create_branch {
            raw_git(repo_root, &["checkout", "-b", &branch, base_sha]);
            std::fs::write(repo_root.join("feature.txt"), "new feature\n").unwrap();
            raw_git(repo_root, &["add", "--", "feature.txt"]);
            raw_git(repo_root, &["commit", "-m", "add feature"]);
            raw_git(repo_root, &["checkout", "main"]);
            if merge_into_base {
                raw_git(repo_root, &["merge", "--no-ff", "--no-edit", &branch]);
            }
        }
    }

    #[tokio::test]
    async fn ticket_projection_merged_false_for_done_ticket_on_unmerged_mission() {
        if !setup() {
            return;
        }
        let (_dir, repo_root, base_sha) = init_repo();
        write_ticket(&repo_root, "unmerged-work");
        Ticket::record_mission(&repo_root, "unmerged-work", "m-unmerged").unwrap();
        Ticket::write_state(&repo_root, "unmerged-work", TicketState::Done, None).unwrap();
        seed_complete_mission(&repo_root, "m-unmerged", &base_sha, true, false);

        let app = crate::router(repo_root.clone(), None);

        let list_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/api/tickets")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(list_response.status(), StatusCode::OK);
        let rows = body_json(list_response).await;
        let row = rows
            .as_array()
            .unwrap()
            .iter()
            .find(|r| r["slug"] == "unmerged-work")
            .expect("ticket present in list");
        assert_eq!(row["merged"], false);

        let show_response = app
            .oneshot(
                Request::builder()
                    .uri("/api/tickets/unmerged-work")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(show_response.status(), StatusCode::OK);
        let json = body_json(show_response).await;
        assert_eq!(json["merged"], false);
    }

    #[tokio::test]
    async fn ticket_projection_merged_true_for_done_ticket_on_merged_mission() {
        if !setup() {
            return;
        }
        let (_dir, repo_root, base_sha) = init_repo();
        write_ticket(&repo_root, "merged-work");
        Ticket::record_mission(&repo_root, "merged-work", "m-merged").unwrap();
        Ticket::write_state(&repo_root, "merged-work", TicketState::Done, None).unwrap();
        seed_complete_mission(&repo_root, "m-merged", &base_sha, true, true);

        let app = crate::router(repo_root.clone(), None);

        let list_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/api/tickets")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(list_response.status(), StatusCode::OK);
        let rows = body_json(list_response).await;
        let row = rows
            .as_array()
            .unwrap()
            .iter()
            .find(|r| r["slug"] == "merged-work")
            .expect("ticket present in list");
        assert_eq!(row["merged"], true);

        let show_response = app
            .oneshot(
                Request::builder()
                    .uri("/api/tickets/merged-work")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(show_response.status(), StatusCode::OK);
        let json = body_json(show_response).await;
        assert_eq!(json["merged"], true);
    }

    #[tokio::test]
    async fn ticket_projection_merged_null_for_new_unlinked_ticket() {
        let tmp = TempDir::new().unwrap();
        write_ticket(tmp.path(), "fresh");
        let app = crate::router(tmp.path().to_path_buf(), None);

        let list_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/api/tickets")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(list_response.status(), StatusCode::OK);
        let rows = body_json(list_response).await;
        let row = rows
            .as_array()
            .unwrap()
            .iter()
            .find(|r| r["slug"] == "fresh")
            .expect("ticket present in list");
        assert!(row["merged"].is_null());

        let show_response = app
            .oneshot(
                Request::builder()
                    .uri("/api/tickets/fresh")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(show_response.status(), StatusCode::OK);
        let json = body_json(show_response).await;
        assert!(json["merged"].is_null());
    }
}