bmo 0.6.0

Local-first SQLite-backed CLI issue tracker for AI agents
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
use axum::{
    extract::{Path, Query, State},
    http::{StatusCode, header},
    response::sse::{Event, Sse},
    response::{Html, IntoResponse, Json},
};
use futures_util::stream;
use minijinja::context;
use serde::Deserialize;

use crate::db::{AddCommentInput, Repository, open_db};
use crate::model::{IssueFilter, Kind, Priority, Status};

use super::AppState;

// ── Query parameter structs ───────────────────────────────────────────────────

/// Query parameters accepted by GET /api/issues.
#[derive(Debug, Default, Deserialize)]
pub struct IssueQuery {
    pub limit: Option<usize>,
    pub offset: Option<usize>,
    pub q: Option<String>,
    pub status: Option<String>,
    pub kind: Option<String>,
    pub priority: Option<String>,
    /// defaults to false; pass ?all=true to include done issues
    pub all: Option<bool>,
}

/// Query parameters accepted by GET /api/board.
#[derive(Debug, Default, Deserialize)]
pub struct BoardQuery {
    pub limit: Option<usize>,
}

const DEFAULT_LIMIT: usize = 50;

// TODO: replace with per-session identity once authentication exists
const WEB_COMMENT_AUTHOR: &str = "web";

// ── Static asset handlers ─────────────────────────────────────────────────────

pub async fn favicon() -> impl IntoResponse {
    let bytes = include_bytes!("../../assets/bmo-clear-bg.ico");
    ([(header::CONTENT_TYPE, "image/x-icon")], bytes.as_ref()).into_response()
}

pub async fn logo() -> impl IntoResponse {
    let bytes = include_bytes!("../../assets/bmo-full.png");
    ([(header::CONTENT_TYPE, "image/png")], bytes.as_ref()).into_response()
}

// ── HTML page handlers ────────────────────────────────────────────────────────

pub async fn board_page(State(state): State<AppState>) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;

        // Fetch all board columns — one query per status (5 total) — then
        // build the ordered column list for the template.
        let by_status = repo.list_issues_by_status(DEFAULT_LIMIT)?;

        let col_defs: &[(&str, &str, Status)] = &[
            ("backlog", "Backlog", Status::Backlog),
            ("todo", "Todo", Status::Todo),
            ("in_progress", "In Progress", Status::InProgress),
            ("review", "Review", Status::Review),
            ("done", "Done", Status::Done),
        ];

        let columns: Vec<serde_json::Value> = col_defs
            .iter()
            .map(
                |(col_key, label, status)| -> anyhow::Result<serde_json::Value> {
                    let col_issues_raw = by_status.get(status).map(Vec::as_slice).unwrap_or(&[]);
                    let col_issues: Vec<serde_json::Value> = col_issues_raw
                        .iter()
                        .map(|i| serde_json::to_value(i).map_err(|e| anyhow::anyhow!(e)))
                        .collect::<Result<Vec<_>, _>>()?;
                    Ok(serde_json::json!({
                        "status": col_key,
                        "label": label,
                        "issues": col_issues,
                    }))
                },
            )
            .collect::<Result<Vec<_>, _>>()?;

        let tmpl = state.env.get_template("board.html")?;
        let html = tmpl.render(context!(columns => columns))?;
        anyhow::Ok(html)
    })
    .await;

    match result {
        Ok(Ok(html)) => Html(html).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
    }
}

pub async fn issue_list_page(State(state): State<AppState>) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let tmpl = state.env.get_template("issue_list.html")?;
        let html = tmpl.render(context!())?;
        anyhow::Ok(html)
    })
    .await;

    match result {
        Ok(Ok(html)) => Html(html).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
    }
}

pub async fn issue_detail_page(
    State(state): State<AppState>,
    Path(id): Path<i64>,
) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;
        let issue = repo.get_issue(id)?;
        if let Some(issue) = issue {
            let comments = repo.list_comments(id)?;
            let relations = repo.list_relations(id)?;
            let issue_json = serde_json::to_value(&issue)?;
            let comments_json: Vec<serde_json::Value> = comments
                .iter()
                .map(|c| serde_json::to_value(c).map_err(|e| anyhow::anyhow!(e)))
                .collect::<Result<Vec<_>, _>>()?;
            let relations_json: Vec<serde_json::Value> = relations
                .iter()
                .map(|r| serde_json::to_value(r).map_err(|e| anyhow::anyhow!(e)))
                .collect::<Result<Vec<_>, _>>()?;
            let tmpl = state.env.get_template("issue.html")?;
            let html = tmpl.render(context!(issue => issue_json, comments => comments_json, relations => relations_json))?;
            anyhow::Ok(Some(html))
        } else {
            anyhow::Ok(None)
        }
    })
    .await;

    match result {
        Ok(Ok(Some(html))) => Html(html).into_response(),
        Ok(Ok(None)) => {
            (StatusCode::NOT_FOUND, Html("Issue not found".to_string())).into_response()
        }
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
    }
}

// ── JSON API handlers ─────────────────────────────────────────────────────────

pub async fn api_issue_list(
    State(state): State<AppState>,
    Query(params): Query<IssueQuery>,
) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;

        let limit = params.limit.unwrap_or(DEFAULT_LIMIT);
        let offset = params.offset.unwrap_or(0);

        // Parse optional single-value filters into the Vec<T> that IssueFilter expects.
        let status_filter: Option<Vec<Status>> = params
            .status
            .as_deref()
            .map(|s| s.parse::<Status>().map(|v| vec![v]))
            .transpose()
            .map_err(|e| anyhow::anyhow!("invalid status: {e}"))?;

        let kind_filter: Option<Vec<Kind>> = params
            .kind
            .as_deref()
            .map(|s| s.parse::<Kind>().map(|v| vec![v]))
            .transpose()
            .map_err(|e| anyhow::anyhow!("invalid kind: {e}"))?;

        let priority_filter: Option<Vec<Priority>> = params
            .priority
            .as_deref()
            .map(|s| s.parse::<Priority>().map(|v| vec![v]))
            .transpose()
            .map_err(|e| anyhow::anyhow!("invalid priority: {e}"))?;

        let findall = params.all.unwrap_or(false);

        let filter = IssueFilter {
            findall,
            status: status_filter.clone(),
            kind: kind_filter.clone(),
            priority: priority_filter.clone(),
            search: params.q.clone(),
            limit: Some(limit),
            offset: Some(offset),
            ..Default::default()
        };

        // Count total matching records (without limit/offset) for pagination metadata.
        let count_filter = IssueFilter {
            findall,
            status: status_filter,
            kind: kind_filter,
            priority: priority_filter,
            search: params.q,
            limit: None,
            offset: None,
            ..Default::default()
        };

        let issues = repo.list_issues(filter)?;
        let total = repo.count_issues(count_filter)? as usize;

        let issues_json: Vec<serde_json::Value> = issues
            .iter()
            .map(|i| serde_json::to_value(i).map_err(|e| anyhow::anyhow!(e)))
            .collect::<Result<Vec<_>, _>>()?;

        anyhow::Ok((issues_json, total, limit, offset))
    })
    .await;

    match result {
        Ok(Ok((data, total, limit, offset))) => Json(serde_json::json!({
            "ok": true,
            "data": data,
            "total": total,
            "limit": limit,
            "offset": offset,
        }))
        .into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn api_issue_detail(
    State(state): State<AppState>,
    Path(id): Path<i64>,
) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;
        repo.get_issue(id)
    })
    .await;

    match result {
        Ok(Ok(Some(issue))) => Json(serde_json::json!({"ok": true, "data": issue})).into_response(),
        Ok(Ok(None)) => (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({"ok": false, "error": "issue not found"})),
        )
            .into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn api_board(
    State(state): State<AppState>,
    Query(params): Query<BoardQuery>,
) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;
        let per_column_limit = params.limit.unwrap_or(DEFAULT_LIMIT);

        // Fetch all board columns — one query per status (5 total).
        let by_status = repo.list_issues_by_status(per_column_limit)?;

        let col_defs: &[(&str, &str, Status)] = &[
            ("backlog", "Backlog", Status::Backlog),
            ("todo", "Todo", Status::Todo),
            ("in_progress", "In Progress", Status::InProgress),
            ("review", "Review", Status::Review),
            ("done", "Done", Status::Done),
        ];

        let columns: Vec<serde_json::Value> = col_defs
            .iter()
            .map(
                |(col_key, label, status)| -> anyhow::Result<serde_json::Value> {
                    let col_issues_raw = by_status.get(status).map(Vec::as_slice).unwrap_or(&[]);
                    let col_json: Vec<serde_json::Value> = col_issues_raw
                        .iter()
                        .map(|i| serde_json::to_value(i).map_err(|e| anyhow::anyhow!(e)))
                        .collect::<Result<Vec<_>, _>>()?;
                    Ok(serde_json::json!({
                        "status": col_key,
                        "label": label,
                        "issues": col_json,
                    }))
                },
            )
            .collect::<Result<Vec<_>, _>>()?;

        anyhow::Ok(serde_json::json!({ "columns": columns }))
    })
    .await;

    match result {
        Ok(Ok(data)) => Json(serde_json::json!({"ok": true, "data": data})).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

pub async fn api_stats(State(state): State<AppState>) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;
        repo.get_stats()
    })
    .await;

    match result {
        Ok(Ok(stats)) => Json(serde_json::json!({"ok": true, "data": stats})).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

// ── Comment body ──────────────────────────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub struct PostCommentBody {
    pub body: String,
}

/// Typed sentinel for domain-level failures in [`api_post_comment`].
#[derive(Debug)]
pub enum CommentError {
    /// The referenced issue does not exist.
    NotFound,
    /// The issue is currently in-progress; agent has priority.
    InProgress,
}

// ── POST /api/issues/:id/comments ─────────────────────────────────────────────

/// Post a comment on an issue.
///
/// Returns 409 if the issue is currently in-progress (agent has priority).
/// Returns 400 if the comment body is empty.
/// Returns 404 if the issue does not exist.
/// Returns 201 with the created comment on success.
pub async fn api_post_comment(
    State(state): State<AppState>,
    Path(id): Path<i64>,
    Json(payload): Json<PostCommentBody>,
) -> impl IntoResponse {
    // Validate body is non-empty before hitting the DB.
    let body_text = payload.body.trim().to_string();
    if body_text.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            Json(serde_json::json!({"ok": false, "error": "body must not be empty"})),
        )
            .into_response();
    }

    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;

        // Fetch the issue to confirm it exists and check its status.
        let issue = repo.get_issue(id)?;
        let issue = match issue {
            Some(i) => i,
            None => return anyhow::Ok(Err(CommentError::NotFound)),
        };

        // Conflict: agent has priority when issue is in-progress.
        if issue.status == Status::InProgress {
            return anyhow::Ok(Err(CommentError::InProgress));
        }

        let comment = repo.add_comment(&AddCommentInput {
            issue_id: id,
            body: body_text,
            author: Some(WEB_COMMENT_AUTHOR.to_string()),
        })?;

        anyhow::Ok(Ok(comment))
    })
    .await;

    match result {
        Ok(Ok(Ok(comment))) => (
            StatusCode::CREATED,
            Json(serde_json::json!({"ok": true, "data": comment})),
        )
            .into_response(),
        Ok(Ok(Err(CommentError::NotFound))) => (
            StatusCode::NOT_FOUND,
            Json(serde_json::json!({"ok": false, "error": "issue not found"})),
        )
            .into_response(),
        Ok(Ok(Err(CommentError::InProgress))) => (
            StatusCode::CONFLICT,
            Json(serde_json::json!({"ok": false, "error": "Issue is in progress — agent has priority"})),
        )
            .into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

// ── GET /api/events (SSE) ─────────────────────────────────────────────────────

/// SSE stream that emits a `board_updated` event whenever the board changes.
///
/// Subscribes to the shared [`AppState::events_tx`] broadcast channel, which is
/// fed by a single background poller in [`super`].  This avoids opening the DB
/// once per connected client per tick — all clients share the same 3-second
/// poll.
///
/// If the subscriber's channel buffer overflows (i.e. a client is very slow),
/// `RecvError::Lagged` is returned; we log a warning and continue rather than
/// dropping the connection.
pub async fn api_events(
    State(state): State<AppState>,
) -> Sse<impl stream::Stream<Item = Result<Event, std::convert::Infallible>>> {
    let mut rx = state.events_tx.subscribe();
    let mut shutdown = state.shutdown.clone();

    let sse_stream = async_stream::stream! {
        loop {
            tokio::select! {
                result = rx.recv() => {
                    match result {
                        Ok(data) => {
                            yield Ok::<Event, std::convert::Infallible>(
                                Event::default().event("board_updated").data(data)
                            );
                        }
                        Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
                            eprintln!("bmo SSE: subscriber lagged, skipped {n} message(s)");
                            // Continue — don't drop the connection.
                        }
                        Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                            // Broadcaster shut down; end the stream.
                            break;
                        }
                    }
                }
                _ = shutdown.changed() => { break; }
            }
        }
    };

    Sse::new(sse_stream).keep_alive(
        axum::response::sse::KeepAlive::new()
            .interval(tokio::time::Duration::from_secs(15))
            .text("ping"),
    )
}

// ── GET /graph ────────────────────────────────────────────────────────────────

pub async fn graph_page(State(state): State<AppState>) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let tmpl = state.env.get_template("graph.html")?;
        let html = tmpl.render(context!())?;
        anyhow::Ok(html)
    })
    .await;

    match result {
        Ok(Ok(html)) => Html(html).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Html(format!("<pre>Error: {e}</pre>")),
        )
            .into_response(),
    }
}

// ── GET /api/graph ────────────────────────────────────────────────────────────

pub async fn api_graph(State(state): State<AppState>) -> impl IntoResponse {
    let result = tokio::task::spawn_blocking(move || {
        let repo = open_db(&state.db_path)?;

        // Fetch all issues so we can identify active ones and their parents.
        let all_issues = repo.list_issues(IssueFilter {
            findall: true,
            ..Default::default()
        })?;
        use std::collections::{HashMap, HashSet};

        // Index all issues by id for parent lookups.
        let by_id: HashMap<i64, _> = all_issues.iter().map(|i| (i.id, i)).collect();

        // Active issues: status != done.
        let active_ids: HashSet<i64> = all_issues
            .iter()
            .filter(|i| i.status != Status::Done)
            .map(|i| i.id)
            .collect();

        // Collect done immediate parents of any active issue.
        //
        // Design decision: only one level up (direct parent_id) is included here.
        // Pulling in the full ancestor chain would drag in large swaths of the
        // hierarchy that are irrelevant to day-to-day work, making the graph
        // noisy and hard to read.  One level is enough to provide context for
        // an active issue without overwhelming the view.
        //
        // Future work: if multi-level traversal is ever wanted, consider adding
        // a `?depth=N` query parameter to this endpoint and walking the ancestor
        // chain up to N steps.
        let completed_parent_ids: HashSet<i64> = all_issues
            .iter()
            .filter(|i| active_ids.contains(&i.id))
            .filter_map(|i| i.parent_id)
            .filter(|pid| {
                // Only include if the parent is done and not already active.
                by_id
                    .get(pid)
                    .map(|p| p.status == Status::Done)
                    .unwrap_or(false)
                    && !active_ids.contains(pid)
            })
            .collect();

        // Union of node ids that will appear in the graph.
        let visible_ids: HashSet<i64> = active_ids.union(&completed_parent_ids).copied().collect();

        // Build node list: active nodes first, then completed parents.
        let mut nodes: Vec<serde_json::Value> = Vec::new();
        for i in &all_issues {
            if active_ids.contains(&i.id) {
                nodes.push(serde_json::json!({
                    "id":        i.id,
                    "title":     i.title,
                    "status":    i.status.label(),
                    "priority":  i.priority.label(),
                    "kind":      i.kind.label(),
                    "completed": false,
                }));
            } else if completed_parent_ids.contains(&i.id) {
                nodes.push(serde_json::json!({
                    "id":        i.id,
                    "title":     i.title,
                    "status":    i.status.label(),
                    "priority":  i.priority.label(),
                    "kind":      i.kind.label(),
                    "completed": true,
                }));
            }
        }

        // Fetch all relations; only keep edges where both endpoints are visible.
        let relations = repo.list_all_relations()?;
        let edges: Vec<serde_json::Value> = relations
            .iter()
            .filter(|r| visible_ids.contains(&r.from_id) && visible_ids.contains(&r.to_id))
            .map(|r| {
                serde_json::json!({
                    "from": r.from_id,
                    "to":   r.to_id,
                    "kind": r.kind.label(),
                })
            })
            .collect();

        anyhow::Ok(serde_json::json!({ "nodes": nodes, "edges": edges }))
    })
    .await;

    match result {
        Ok(Ok(data)) => Json(serde_json::json!({"ok": true, "data": data})).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"ok": false, "error": e.to_string()})),
        )
            .into_response(),
    }
}

/// Returns a snapshot string representing the current board state.
///
/// Uses the maximum `updated_at` timestamp combined with the total issue count
/// so that both edits and additions/deletions are detected.
///
/// Public so that the background broadcaster in [`super`] can call it.
pub fn board_snapshot(db_path: &std::path::Path) -> anyhow::Result<String> {
    let repo = open_db(db_path)?;
    let (count, max_updated) = repo.board_snapshot_stats()?;
    let max_updated_str = max_updated.map(|t| t.to_rfc3339()).unwrap_or_default();
    Ok(format!("{count}:{max_updated_str}"))
}