clustervision 0.16.0

clustervision CLI — search, read, and port AI coding-agent sessions across harnesses.
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
//! The inbox's routes on `cv serve` (what `cv task serve` was): reads shaped for the one inline
//! page (`serve_tasks_page.html`, served at `/inbox`) and the writes its buttons make, which record
//! **the same events the CLI records**, so the page and the shell are one store. Every write is
//! stamped `web:<who>`. The HTTP model (Host check, CORS allow-list, bearer token, the
//! non-loopback refusal) is `cv serve`'s; this module never sees a socket.
//!
//! Routes:
//! - `GET /api/inbox?who=`                the inbox page (`task::inbox_page`, `all=1` lifts the window)
//! - `POST /api/task/<id>/resolve`        `{choice}` | `{accept_default: true}` | `{confirm: true}` (a
//!   provisional resolution), `{note}` optional
//! - `POST /api/task/<id>/done`           `{observed}` optional
//! - `POST /api/task/<id>/note`           `{text}`
//! - `POST /api/task/<id>/discuss`        `{text}` optional → a `NEEDS DISCUSSION:` note + the `discuss` tag
//! - `POST /api/task/<id>/claim`          claim for `who`
//! - `POST /api/task/<id>/release`
//! - `POST /api/task/<id>/reopen`         a closed task comes back as a NEW task (the record stays)
//! - `GET /api/events?since=&kind=&by=&not_by=&assignee=&task=`  JSON lines — the same query as
//!   `cv task events`, so a poller and the page see one feed.
//! - `GET /api/lanes?session=<id>`        the sub-agent forest of a session (`cv lanes --tasks`),
//!   running first, each lane with the `tasks` its endpoint holds.
//! - `GET /api/backlog?repo=&state=&tag=&all=1` every open task (`cv task list` with no window).
//!
//! Every POST takes `who` in the body (else `?who=`, else the server's `--who`).

use anyhow::{bail, Result};
use chrono::Utc;
use cv_core::api::Query;
use cv_core::task::{self, TaskEventKind, TaskStore};
use serde_json::{json, Value};

use super::task_ops::{self, DecisionSpec, EventFilter, Since};

/// The inbox page: one inline HTML file, no external JS/CSS, no build step.
pub(crate) const PAGE: &str = include_str!("serve_tasks_page.html");

/// A task-route answer: JSON, or the newline-delimited JSON `/api/events` streams.
pub(crate) enum Answer {
    Json(u16, Value),
    Ndjson(String),
}

/// The inbox routes, or `None` when the path is not one of them (the read API answers it).
pub(crate) fn route(post: bool, parts: &[&str], q: &Query, body: &Value, default_who: Option<&str>) -> Option<Answer> {
    let json = |(status, v): (u16, Value)| Some(Answer::Json(status, v));
    match (post, parts) {
        (false, ["api", "inbox"]) => json(api_inbox(default_who, q)),
        (false, ["api", "events"]) => Some(match api_events(q) {
            Ok(lines) => Answer::Ndjson(lines),
            Err(e) => Answer::Json(400, json!({"error": e.to_string()})),
        }),
        (false, ["api", "lanes"]) => json(api_lanes(q)),
        (false, ["api", "backlog"]) => json(api_backlog(q)),
        (true, ["api", "task", id, verb]) => {
            let who = body
                .get("who")
                .and_then(Value::as_str)
                .map(str::to_string)
                .or_else(|| q.get("who"))
                .or_else(|| default_who.map(str::to_string));
            json(match who {
                Some(who) => api_act(id, verb, &who, body),
                None => (400, json!({"error": "who? pass `who` in the body or start with --who"})),
            })
        }
        _ => None,
    }
}

fn with_replay(f: impl FnOnce(cv_core::task::ReplayOutcome) -> (u16, Value)) -> (u16, Value) {
    match task::replay() {
        Ok(o) => f(o),
        Err(e) => (500, json!({"error": e.to_string()})),
    }
}

fn api_inbox(default_who: Option<&str>, q: &Query) -> (u16, Value) {
    let Some(who) = q.get("who").or_else(|| default_who.map(str::to_string)) else {
        return (400, json!({"error": "who? pass ?who=<name> or start with --who"}));
    };
    let now = Utc::now();
    let window = if q.flag("all") {
        None
    } else {
        match q.get("since") {
            Some(s) => match task::parse_since(&s, now) {
                Ok(t) => Some(t),
                Err(e) => return (400, json!({"error": e})),
            },
            None => Some(now - chrono::Duration::days(14)),
        }
    };
    with_replay(|outcome| {
        let caller = task::default_endpoint();
        let page = task::inbox_page(&outcome, &who, caller.as_deref(), window, false, now);
        let mut v = serde_json::to_value(&page).expect("page serializes");
        if let Some(o) = v.as_object_mut() {
            o.insert("warnings".into(), json!(outcome.warnings));
        }
        (200, v)
    })
}

/// `cv lanes <session>` as JSON: every sub-agent, running first, then by start time (newest
/// first). The page polls this beside the inbox so the orchestrator's forest and the human's
/// decisions sit on one screen.
fn api_lanes(q: &Query) -> (u16, Value) {
    let Some(session) = q.get("session") else {
        return (400, json!({"error": "which session? pass ?session=<id prefix>"}));
    };
    let (r, _adapter) = match crate::util::resolve(&session, None) {
        Ok(x) => x,
        Err(e) => return (404, json!({"error": e.to_string()})),
    };
    let mut lanes = cv_core::lanes::lanes_of(&r);
    // Each lane joined to the tasks its endpoint holds (the page shows the task + last note).
    if let Ok(outcome) = task::replay() {
        cv_core::lanes::attach_tasks(&mut lanes, &outcome.model);
    }
    lanes.sort_by(|a, b| {
        b.is_running()
            .cmp(&a.is_running())
            .then_with(|| b.started_at.cmp(&a.started_at))
    });
    let running = lanes.iter().filter(|l| l.is_running()).count();
    let stranded = lanes.iter().filter(|l| l.stranded).count();
    let done = lanes.iter().filter(|l| l.is_done()).count();
    (
        200,
        json!({
            "session": r.id,
            "counts": {"total": lanes.len(), "running": running, "done": done, "stranded": stranded},
            "lanes": lanes,
        }),
    )
}

/// `cv task list` with no time window: the whole open backlog (or everything with `all=1`),
/// one compact row per task, newest activity first.
fn api_backlog(q: &Query) -> (u16, Value) {
    with_replay(|outcome| {
        let filter = task::TaskFilter {
            state: q.get("state"),
            assignee: q.get("assignee"),
            repo: q.get("repo").map(std::path::PathBuf::from),
            include_terminal: q.flag("all"),
            tag: q.get("tag"),
            touched_since: None,
            or_involving: None,
            decisions: None,
        };
        let mut tasks = match task::list(&outcome.model, &filter) {
            Ok(t) => t,
            Err(e) => return (400, json!({"error": e})),
        };
        tasks.sort_by_key(|t| std::cmp::Reverse(t.last_ts));
        let now = Utc::now();
        let rows: Vec<Value> = tasks
            .iter()
            .map(|t| {
                json!({
                    "id": t.task_id,
                    "short": &t.task_id[..t.task_id.len().min(13)],
                    "title": t.title,
                    "state": task::effective_display(t),
                    "assignee": t.assignee,
                    "opened_by": t.opened_by,
                    "repo": t.repo,
                    "issue": t.issue,
                    "tags": t.tags,
                    "blocked": task::is_blocked(&outcome.model, t),
                    "decision": t.decision.is_some(),
                    "notes": t.notes.len(),
                    "last_ts": t.last_ts,
                    "age": task::age_short(t.last_ts, now),
                    "body": t.body,
                })
            })
            .collect();
        let mut repos: Vec<String> = tasks
            .iter()
            .filter_map(|t| t.repo.as_ref().map(|p| p.display().to_string()))
            .collect();
        repos.sort();
        repos.dedup();
        (
            200,
            json!({"count": rows.len(), "repos": repos, "tasks": rows, "warnings": outcome.warnings}),
        )
    })
}

fn api_events(q: &Query) -> Result<String> {
    let outcome = task::replay()?;
    let now = Utc::now();
    let task_id = match q.get("task") {
        Some(p) => Some(
            task::resolve_id(&outcome.model, &p)
                .map_err(|e| anyhow::anyhow!(e))?
                .to_string(),
        ),
        None => None,
    };
    let f = EventFilter {
        since: Since::parse(q.get("since").as_deref(), now)?,
        kinds: q
            .get("kind")
            .map(|k| k.split(',').map(task_ops::kind_tag).collect())
            .unwrap_or_default(),
        by: q.get("by"),
        not_by: q.get("not_by"),
        assignee: q.get("assignee"),
        task: task_id,
    };
    let mut out = String::new();
    for ev in task_ops::select_events(&outcome, &f) {
        out.push_str(&serde_json::to_string(&task_ops::event_json(ev, &outcome.model))?);
        out.push('\n');
    }
    Ok(out)
}

/// One write, as `web:<who>`: the same `TaskEventKind`s the CLI verbs append.
fn api_act(id: &str, verb: &str, who: &str, body: &Value) -> (u16, Value) {
    match act(id, verb, who, body) {
        Ok(v) => (200, v),
        Err(e) => (400, json!({"error": e.to_string()})),
    }
}

fn act(id: &str, verb: &str, who: &str, body: &Value) -> Result<Value> {
    let outcome = task::replay()?;
    let id = task::resolve_id(&outcome.model, id)
        .map_err(|e| anyhow::anyhow!(e))?
        .to_string();
    let t = &outcome.model.tasks[&id];
    let by = format!("web:{}", who.trim());
    let store = TaskStore::default_store();
    let text = |k: &str| {
        body.get(k)
            .and_then(Value::as_str)
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
    };
    let appended = match verb {
        "resolve" => {
            let accept = body.get("accept_default").and_then(Value::as_bool).unwrap_or(false);
            let confirm = body.get("confirm").and_then(Value::as_bool).unwrap_or(false);
            let answer = task_ops::Answer::from_flags(text("choice"), accept, confirm)?;
            let out = task_ops::resolve(&store, &outcome.model, &by, &id, answer, text("note"), false)?;
            vec![out.event]
        }
        "done" => {
            let out = task::append(
                &store,
                Some(&id),
                &by,
                TaskEventKind::Done {
                    observed: text("observed"),
                    check: None,
                },
                Vec::new(),
            )?;
            vec![out.event]
        }
        "note" => {
            let Some(t) = text("text") else {
                bail!("a note needs text")
            };
            let out = task::append(
                &store,
                Some(&id),
                &by,
                TaskEventKind::Noted {
                    text: t,
                    session_ref: None,
                },
                Vec::new(),
            )?;
            vec![out.event]
        }
        "discuss" => {
            // One unit: the note and the `discuss` tag land together.
            let detail = text("text").unwrap_or_else(|| "(no detail)".into());
            let out = task::append_events(
                &store,
                vec![
                    task::new_event(
                        Some(&id),
                        &by,
                        TaskEventKind::Noted {
                            text: format!("NEEDS DISCUSSION: {detail}"),
                            session_ref: None,
                        },
                    ),
                    task::new_event(
                        Some(&id),
                        &by,
                        TaskEventKind::Tagged {
                            tags: vec![task::DISCUSS_TAG.into()],
                        },
                    ),
                ],
                Vec::new(),
            )?;
            out.events
        }
        "claim" => {
            let out = task::append(
                &store,
                Some(&id),
                &by,
                TaskEventKind::Claimed {
                    assignee: who.trim().to_string(),
                },
                Vec::new(),
            )?;
            vec![out.event]
        }
        "release" => {
            let out = task::append(&store, Some(&id), &by, TaskEventKind::Released {}, Vec::new())?;
            vec![out.event]
        }
        "reopen" => {
            if !t.state.is_terminal() {
                bail!("task is still open");
            }
            // Terminal is terminal: the record of the close stays. The task comes back as a NEW
            // task carrying the same title/body/assignee (and, for a decision, the same options),
            // tagged `reopened`, and each side notes the other — all as ONE unit.
            let assignee = t.assignee.clone().unwrap_or_else(|| who.trim().to_string());
            let mut events = match &t.decision {
                Some(d) => task_ops::pose_events(
                    &by,
                    &DecisionSpec {
                        title: t.title.clone(),
                        body: t.body.clone(),
                        for_who: assignee,
                        default_choice: d.default_choice.clone(),
                        options: d.options.clone(),
                        deadline: None,
                        repo: t.repo.clone(),
                        issue: t.issue.clone(),
                        tags: vec!["reopened".into()],
                        source: d.source.clone(),
                        blocks: Vec::new(),
                        provisional: false,
                    },
                )?,
                None => {
                    let opened = task::new_event(
                        None,
                        &by,
                        TaskEventKind::Opened {
                            title: t.title.clone(),
                            body: t.body.clone(),
                            repo: t.repo.clone(),
                            issue: t.issue.clone(),
                            channel: None,
                            assignee: Some(assignee),
                        },
                    );
                    let new_id = opened.task_id.clone();
                    vec![
                        opened,
                        task::new_event(
                            Some(&new_id),
                            &by,
                            TaskEventKind::Tagged {
                                tags: vec!["reopened".into()],
                            },
                        ),
                    ]
                }
            };
            let new_id = events[0].task_id.clone();
            events.push(task::new_event(
                Some(&new_id),
                &by,
                TaskEventKind::Noted {
                    text: format!("reopened from {} ({})", &id[..13], task::effective_display(t)),
                    session_ref: None,
                },
            ));
            // The closed one stays closed: a note never changes state.
            events.push(task::new_event(
                Some(&id),
                &by,
                TaskEventKind::Noted {
                    text: format!("reopened as {}", &new_id[..13]),
                    session_ref: None,
                },
            ));
            task::append_events(&store, events, Vec::new())?.events
        }
        other => bail!("unknown action {other:?}"),
    };
    let after = task::replay()?;
    let state = after
        .model
        .tasks
        .get(&appended.last().map(|e| e.task_id.clone()).unwrap_or(id.clone()))
        .map(task::effective_display);
    Ok(json!({
        "ok": true,
        "by": by,
        "events": appended,
        "task_state": state,
        "warnings": after.warnings,
    }))
}