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
//! `minutes doc`: the live meeting doc (RFC 0008, phase 2).
//!
//! A thin command-line layer over [`minutes_core::meeting_doc::MeetingDocStore`].
//! The store enforces section ownership, `agents.allow` grants, optimistic
//! revisions and citations; this module only parses arguments and prints.
use anyhow::{bail, Context, Result};
use clap::Subcommand;
use minutes_core::meeting_doc::{
AgendaStatus, AgentAgendaUpdate, AgentSectionWrite, Citation, MeetingDocStore, SectionKind,
UserDecisionAction, WriteOutcome,
};
use std::io::Read;
/// Subcommands of `minutes doc`.
#[derive(Subcommand, Debug)]
pub enum DocAction {
/// Create the meeting doc for the current (or given) capture session
Init {
/// Capture session ID (defaults to the active live transcript session)
#[arg(long)]
session: Option<String>,
/// Meeting title shown at the top of the doc
#[arg(long)]
title: Option<String>,
/// Agenda item (repeatable)
#[arg(long = "agenda")]
agenda: Vec<String>,
},
/// Print the meeting doc
Show {
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
/// Only this section (notes, agenda, outline, advice, questions, decisions); JSON only
#[arg(long)]
section: Option<String>,
/// Output format: md (the projection) or json (the record, with section revisions)
#[arg(long, default_value = "md", value_parser = ["md", "json"])]
format: String,
},
/// Add your own note to the doc's notes section
Note {
/// The note text
text: String,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Add your own agenda item
AddAgenda {
/// The agenda item text
text: String,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Agent write to outline, advice, questions or decisions (needs an agents.allow grant)
Write {
/// Section: outline, advice, questions or decisions
#[arg(long)]
section: String,
/// Section revision you read before writing (from `minutes doc show --format json`)
#[arg(long)]
rev: u64,
/// Agent identifier, as granted in ~/.minutes/agents.allow
#[arg(long)]
agent: String,
/// Citation as <source_kind>:<event_id>, e.g. transcript_final:line-12 (repeatable, at least one)
#[arg(long = "cite", required = true)]
cite: Vec<String>,
/// Content text; use `-` or omit to read from stdin
#[arg(long)]
text: Option<String>,
/// Expiry in seconds for an advice card
#[arg(long)]
ttl: Option<u64>,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Agent update of an agenda item's status (needs an agents.allow grant)
Agenda {
/// Agenda item ID (a1, a2, ...)
#[arg(long)]
item: String,
/// New status: open, covered or skipped
#[arg(long)]
status: String,
/// Agenda section revision you read before writing
#[arg(long)]
rev: u64,
/// Agent identifier, as granted in ~/.minutes/agents.allow
#[arg(long)]
agent: String,
/// Citation as <source_kind>:<event_id> (repeatable, at least one)
#[arg(long = "cite", required = true)]
cite: Vec<String>,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Accept a proposed question or decision
Accept {
/// Section: questions or decisions
#[arg(long)]
section: String,
/// Item ID
#[arg(long)]
item: String,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Dismiss a proposed question or decision
Dismiss {
/// Section: questions or decisions
#[arg(long)]
section: String,
/// Item ID
#[arg(long)]
item: String,
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
/// Freeze agent sections (capture has stopped); your notes and accept/dismiss still work
Freeze {
/// Require the doc to belong to this capture session
#[arg(long)]
session: Option<String>,
},
}
/// Run a `minutes doc` subcommand against the default store.
pub fn cmd_doc(action: DocAction) -> Result<()> {
run(
action,
&MeetingDocStore::default_store(),
&mut std::io::stdout(),
)
}
fn parse_citations(specs: &[String]) -> Result<Vec<Citation>> {
specs
.iter()
.map(|spec| Citation::parse_spec(spec).map_err(anyhow::Error::from))
.collect()
}
fn read_content(text: Option<String>) -> Result<String> {
match text {
Some(text) if text != "-" => Ok(text),
_ => {
let mut buf = String::new();
std::io::stdin()
.read_to_string(&mut buf)
.context("reading content from stdin")?;
Ok(buf)
}
}
}
fn active_capture_session() -> Option<String> {
#[cfg(feature = "whisper")]
{
minutes_core::live_transcript::session_status()
.session_id
.filter(|id| !id.trim().is_empty())
}
#[cfg(not(feature = "whisper"))]
{
None
}
}
fn report(outcome: &WriteOutcome, what: &str, out: &mut impl std::io::Write) -> Result<()> {
writeln!(out, "{what} (doc revision {}).", outcome.doc.revision)?;
if outcome.projection_tamper_detected {
eprintln!(
"[minutes] meeting-doc.md had been edited directly; it was rewritten from the record. Edit the doc with `minutes doc` instead."
);
}
Ok(())
}
fn run(action: DocAction, store: &MeetingDocStore, out: &mut impl std::io::Write) -> Result<()> {
match action {
DocAction::Init {
session,
title,
agenda,
} => {
let session = match session.or_else(active_capture_session) {
Some(session) => session,
None => bail!(
"no active capture session; start `minutes live` or `minutes record`, or pass --session"
),
};
let doc = store.init(session, title, &agenda)?;
writeln!(
out,
"Meeting doc created for session {} with {} agenda item(s): {}",
doc.session_id,
doc.agenda.items.len(),
store.projection_path().display()
)?;
}
DocAction::Show {
session,
section,
format,
} => {
let doc = store.load(session.as_deref())?;
match (format.as_str(), section) {
("json", None) => writeln!(out, "{}", serde_json::to_string_pretty(&doc)?)?,
("json", Some(section)) => {
let kind = SectionKind::parse(§ion)?;
let value = serde_json::to_value(&doc)?;
let part = value
.get(kind.as_str())
.cloned()
.unwrap_or(serde_json::Value::Null);
writeln!(out, "{}", serde_json::to_string_pretty(&part)?)?;
}
(_, Some(_)) => bail!("--section needs --format json"),
_ => write!(out, "{}", doc.render_markdown())?,
}
}
DocAction::Note { text, session } => {
let outcome = store.append_user_note(session.as_deref(), &text, None)?;
report(&outcome, "Note added", out)?;
}
DocAction::AddAgenda { text, session } => {
let outcome = store.add_agenda_item(session.as_deref(), &text)?;
report(&outcome, "Agenda item added", out)?;
}
DocAction::Write {
section,
rev,
agent,
cite,
text,
ttl,
session,
} => {
let content = read_content(text)?;
let outcome = store.apply_agent_write(AgentSectionWrite {
session_id: session,
section: SectionKind::parse(§ion)?,
expected_rev: rev,
agent_id: agent,
content,
citations: parse_citations(&cite)?,
ttl_secs: ttl,
})?;
report(&outcome, "Section written", out)?;
}
DocAction::Agenda {
item,
status,
rev,
agent,
cite,
session,
} => {
let outcome = store.apply_agent_agenda_status(AgentAgendaUpdate {
session_id: session,
item_id: item,
status: AgendaStatus::parse(&status)?,
expected_rev: rev,
agent_id: agent,
citations: parse_citations(&cite)?,
})?;
report(&outcome, "Agenda status updated", out)?;
}
DocAction::Accept {
section,
item,
session,
} => decide(store, session, §ion, &item, "accept", out)?,
DocAction::Dismiss {
section,
item,
session,
} => decide(store, session, §ion, &item, "dismiss", out)?,
DocAction::Freeze { session } => {
let outcome = store.freeze(session.as_deref())?;
report(&outcome, "Agent sections frozen", out)?;
}
}
Ok(())
}
fn decide(
store: &MeetingDocStore,
session: Option<String>,
section: &str,
item: &str,
action: &str,
out: &mut impl std::io::Write,
) -> Result<()> {
let outcome = store.decide_proposal(
session.as_deref(),
SectionKind::parse(section)?,
item,
UserDecisionAction::parse(action)?,
)?;
let what = if action == "accept" {
"Accepted"
} else {
"Dismissed"
};
report(&outcome, what, out)
}
#[cfg(test)]
mod tests {
use super::*;
fn store(dir: &std::path::Path) -> MeetingDocStore {
MeetingDocStore::in_dir(
dir.join("live-doc"),
dir.join("agents.allow"),
Some(dir.join("current-notes.md")),
)
}
fn run_ok(action: DocAction, store: &MeetingDocStore) -> String {
let mut out = Vec::new();
run(action, store, &mut out).unwrap();
String::from_utf8(out).unwrap()
}
fn write(section: &str, rev: u64, text: &str) -> DocAction {
DocAction::Write {
section: section.into(),
rev,
agent: "claude".into(),
cite: vec!["transcript_final:line-3".into()],
text: Some(text.into()),
ttl: None,
session: None,
}
}
#[test]
fn doc_cli_round_trip_respects_grants_revisions_and_freeze() {
let dir = tempfile::tempdir().unwrap();
let store = store(dir.path());
run_ok(
DocAction::Init {
session: Some("s-1".into()),
title: Some("Pricing".into()),
agenda: vec!["Q1 pricing".into()],
},
&store,
);
// Default deny: no agents.allow grant yet.
let mut out = Vec::new();
assert!(run(write("outline", 0, "1. Pricing"), &store, &mut out).is_err());
std::fs::write(
dir.path().join("agents.allow"),
"claude: live_doc.outline\n",
)
.unwrap();
assert!(run_ok(write("outline", 0, "1. Pricing"), &store).contains("Section written"));
// Stale revision and an ungranted section are both refused.
assert!(run(write("outline", 0, "x"), &store, &mut out).is_err());
assert!(run(write("advice", 0, "x"), &store, &mut out).is_err());
run_ok(
DocAction::Note {
text: "my note".into(),
session: None,
},
&store,
);
let md = run_ok(
DocAction::Show {
session: None,
section: None,
format: "md".into(),
},
&store,
);
assert!(md.contains("1. Pricing") && md.contains("my note"));
assert!(!md.contains("__"), "projection must not nest italics: {md}");
let outline = run_ok(
DocAction::Show {
session: None,
section: Some("outline".into()),
format: "json".into(),
},
&store,
);
let outline: serde_json::Value = serde_json::from_str(&outline).unwrap();
assert_eq!(outline["rev"], 1);
// A doc for another session is refused.
assert!(run(
DocAction::Show {
session: Some("other".into()),
section: None,
format: "md".into(),
},
&store,
&mut out,
)
.is_err());
run_ok(DocAction::Freeze { session: None }, &store);
assert!(run(write("outline", 1, "late"), &store, &mut out).is_err());
// The user can still add notes after the freeze.
run_ok(
DocAction::Note {
text: "after".into(),
session: None,
},
&store,
);
}
#[test]
fn show_section_requires_json() {
let dir = tempfile::tempdir().unwrap();
let store = store(dir.path());
run_ok(
DocAction::Init {
session: Some("s-1".into()),
title: None,
agenda: Vec::new(),
},
&store,
);
let mut out = Vec::new();
let err = run(
DocAction::Show {
session: None,
section: Some("outline".into()),
format: "md".into(),
},
&store,
&mut out,
)
.unwrap_err();
assert!(err.to_string().contains("--format json"));
}
}