use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use anyhow::{Context, Result, bail};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use crate::agent::{self, Invocation, SeatState};
use crate::config::Config;
use crate::plan;
use crate::queue::{Queue, Source, Task};
pub const SCHEMA: u32 = 1;
const TURN_TIMEOUT: Duration = Duration::from_secs(900);
const SEAT: &str = "talk";
const MAGI_NOTE: &str = "magi: ";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Who {
Operator,
Agent,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Turn {
pub who: Who,
pub body: String,
pub at: Timestamp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TalkStatus {
Open,
Closed,
}
impl TalkStatus {
pub fn open(self) -> bool {
matches!(self, Self::Open)
}
pub fn as_str(self) -> &'static str {
match self {
Self::Open => "open",
Self::Closed => "closed",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Talk {
pub schema: u32,
pub id: String,
pub repo: PathBuf,
pub agent: String,
pub status: TalkStatus,
pub turns: Vec<Turn>,
pub created_at: Timestamp,
pub updated_at: Timestamp,
seat: SeatState,
}
impl Talk {
pub fn short(&self) -> &str {
short(&self.id)
}
}
#[derive(Debug, Clone)]
pub struct Talks {
root: PathBuf,
lock: Arc<Mutex<()>>,
}
impl Talks {
pub fn open() -> Self {
Self::at(crate::run::home().join("talks"))
}
pub fn at(root: PathBuf) -> Self {
Self {
root,
lock: Arc::new(Mutex::new(())),
}
}
fn guard(&self) -> MutexGuard<'_, ()> {
self.lock.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn path_of(&self, id: &str) -> PathBuf {
self.root.join(format!("{id}.json"))
}
pub fn artifacts_of(&self, id: &str) -> PathBuf {
self.root.join(format!("{id}.artifacts"))
}
pub fn put(&self, t: &mut Talk) -> Result<()> {
std::fs::create_dir_all(&self.root)
.with_context(|| format!("create {}", self.root.display()))?;
t.updated_at = Timestamp::now();
let body = serde_json::to_string_pretty(t).context("serialize talk")?;
let path = self.path_of(&t.id);
let tmp = path.with_extension("json.tmp");
std::fs::write(&tmp, &body).with_context(|| format!("write {}", tmp.display()))?;
std::fs::rename(&tmp, &path).with_context(|| format!("replace {}", path.display()))?;
Ok(())
}
pub fn get(&self, id: &str) -> Result<Talk> {
let resolved = self.resolve_id(id)?;
read_path(&self.path_of(&resolved))
}
pub fn list(&self) -> Vec<Talk> {
let mut all: Vec<Talk> = std::fs::read_dir(&self.root)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|x| x == "json"))
.filter_map(|p| read_path(&p).ok())
.collect();
all.sort_unstable_by(|a, b| {
let rank = |t: &Talk| u8::from(!t.status.open());
rank(a).cmp(&rank(b)).then_with(|| b.id.cmp(&a.id))
});
all
}
pub fn resolve_id(&self, prefix: &str) -> Result<String> {
if self.path_of(prefix).is_file() {
return Ok(prefix.to_owned());
}
let hits: Vec<String> = self
.list()
.into_iter()
.map(|t| t.id)
.filter(|id| id.starts_with(prefix) || id.ends_with(prefix))
.collect();
match hits.len() {
1 => Ok(hits.into_iter().next().expect("exactly one hit")),
0 => bail!("no talk matches `{prefix}`"),
_ => bail!(
"`{prefix}` matches {} talks: {}",
hits.len(),
hits.join(", ")
),
}
}
pub fn revision(&self) -> u64 {
std::fs::read_dir(&self.root)
.into_iter()
.flatten()
.flatten()
.filter_map(|e| e.metadata().ok())
.filter_map(|m| m.modified().ok())
.filter_map(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_millis() as u64)
.max()
.unwrap_or(0)
}
pub fn count_open(&self) -> usize {
self.list().iter().filter(|t| t.status.open()).count()
}
}
pub fn begin(store: &Talks, cfg: &Config, repo: PathBuf, agent: Option<&str>) -> Result<Talk> {
let repo = repo.canonicalize().unwrap_or(repo);
let want = agent.or(cfg.roles.planner.as_deref());
let spec = plan::pick(&cfg.agents, want, &plan::installed)?;
let now = Timestamp::now();
let mut talk = Talk {
schema: SCHEMA,
id: new_id(),
repo,
agent: spec.id.clone(),
status: TalkStatus::Open,
turns: Vec::new(),
created_at: now,
updated_at: now,
seat: SeatState::new(SEAT, &spec.id, crate::rng::entropy()),
};
store.put(&mut talk)?;
Ok(talk)
}
pub fn record(talk: &mut Talk, store: &Talks, text: &str) -> Result<String> {
let _guard = store.guard();
if let Ok(fresh) = store.get(&talk.id) {
talk.status = fresh.status;
}
if !talk.status.open() {
bail!(
"talk {} is {} and takes no more turns",
talk.short(),
talk.status.as_str()
);
}
let text = text.trim();
if text.is_empty() {
bail!("nothing to say");
}
talk.turns.push(Turn {
who: Who::Operator,
body: text.to_owned(),
at: Timestamp::now(),
});
store.put(talk)?;
Ok(text.to_owned())
}
pub async fn say(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
let text = record(talk, store, text)?;
turn(talk, store, cfg, &text).await
}
pub async fn respond(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
turn(talk, store, cfg, text).await
}
pub fn close(talk: &mut Talk, store: &Talks) -> Result<()> {
let _guard = store.guard();
let mut fresh = store.get(&talk.id).unwrap_or_else(|_| talk.clone());
fresh.status = TalkStatus::Closed;
store.put(&mut fresh)?;
*talk = fresh;
Ok(())
}
async fn turn(talk: &mut Talk, store: &Talks, cfg: &Config, text: &str) -> Result<()> {
let spec = cfg
.agents
.iter()
.find(|a| a.id == talk.agent)
.with_context(|| {
format!(
"talk {} was opened with agent `{}`, which is no longer in \
the roster; restore it in magi.toml or start a new \
conversation",
talk.short(),
talk.agent
)
})?;
let resuming = agent::has_session(spec.kind, &talk.seat, cfg.graph.sessions);
let body = if talk.seat.turns == 0 {
format!(
"{}\n\n# Operator\n\n{text}",
briefing(&talk.repo, &cfg.graph.language)
)
} else if resuming {
text.to_owned()
} else {
format!("{}\n\n{text}", transcript(talk))
};
let artifacts = store.artifacts_of(&talk.id);
let stem = format!("turn-{}", talk.seat.turns + 1);
let cache_dir = cfg.cache_dir();
let inv = Invocation {
cwd: &talk.repo,
prompt: &body,
timeout: TURN_TIMEOUT,
allow_write: false,
sessions: cfg.graph.sessions,
artifacts: &artifacts,
stem: &stem,
run: &talk.id,
node: "chat",
cache_dir: cache_dir.as_deref(),
};
let outcome = agent::invoke(spec, &mut talk.seat, &inv).await;
let note = |why: String| Turn {
who: Who::Agent,
body: format!("{MAGI_NOTE}{why}"),
at: Timestamp::now(),
};
let (reply, failure) = match outcome {
Err(e) => (
note(format!("could not run agent `{}`: {e}", talk.agent)),
Some(format!("could not run agent `{}`: {e}", talk.agent)),
),
Ok(out) if out.quota_exhausted() => {
let reset = out
.quota
.as_ref()
.and_then(|q| q.reset.clone())
.map_or_else(String::new, |r| format!(" (resets {r})"));
let why = format!(
"agent `{}` is out of quota{reset}; your message is saved, so \
say it again when the window reopens",
talk.agent
);
(note(why.clone()), Some(why))
}
Ok(out) if out.timed_out => {
let why = format!(
"agent `{}` did not answer within {}s; your message is saved",
talk.agent,
TURN_TIMEOUT.as_secs()
);
(note(why.clone()), Some(why))
}
Ok(out) if !out.usable() => {
let why = format!(
"agent `{}` produced no answer (exit {}); your message is saved",
talk.agent,
out.exit_code
.map_or_else(|| "unknown".to_owned(), |c| c.to_string())
);
(note(why.clone()), Some(why))
}
Ok(out) => (
Turn {
who: Who::Agent,
body: out.text.trim().to_owned(),
at: Timestamp::now(),
},
None,
),
};
let _guard = store.guard();
if let Ok(fresh) = store.get(&talk.id) {
talk.status = fresh.status;
}
talk.turns.push(reply);
store.put(talk)?;
match failure {
Some(why) => bail!("{why}"),
None => Ok(()),
}
}
fn transcript(talk: &Talk) -> String {
let mut out = String::from(
"This conversation cannot resume on the CLI's side, so here is \
everything said so far; answer only the last message.\n",
);
for t in &talk.turns {
let who = match t.who {
Who::Operator => "operator",
Who::Agent => "you",
};
out.push_str(&format!("\n## {who}\n\n{}\n", t.body.trim()));
}
out
}
pub fn briefing(repo: &Path, language: &str) -> String {
let mut out = format!(
"You are magi's standing conversation partner for its operator, who \
usually has this open on a phone. Keep replies short: no preamble, \
no restating what they just said.\n\n\
# Repository\n\n{repo}\n\n\
You may look around: read files, run shell commands, search history, \
run tests - whatever answers the question. Do not write files. \
Implementing a change is not this conversation's job; a separate, \
blind competition of agents does that, and a repository this \
conversation has already edited would make their diffs unjudgeable.\n\n\
# When the operator wants something done\n\n\
Run:\n\n\
magi task add --solo --repo {repo} <instruction>\n\n\
and tell the operator the task id it prints, so they can follow it \
from the Queue. Write <instruction> so that an implementer who has \
never seen this conversation can act on it alone - it is everything \
they get. Use --solo: it runs the task through one implementer \
straight into review instead of the usual multi-agent competition, \
which is the right shape for a change this conversation has already \
settled, rather than one still worth several independent takes.\n",
repo = repo.display(),
);
out.push_str(&language_note(language));
out
}
fn language_note(language: &str) -> String {
if language.trim().is_empty() || language.eq_ignore_ascii_case("en") {
String::new()
} else {
format!("\nHold this conversation in {language}.\n")
}
}
pub fn tasks_of(queue: &Queue, talk_id: &str) -> Vec<Task> {
let mut tasks: Vec<Task> = queue
.list()
.into_iter()
.filter(|t| matches!(&t.source, Source::Agent { run, .. } if run == talk_id))
.collect();
tasks.sort_unstable_by(|a, b| a.id.cmp(&b.id));
tasks
}
fn read_path(path: &Path) -> Result<Talk> {
let body = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
serde_json::from_str(&body).with_context(|| format!("parse {}", path.display()))
}
fn short(id: &str) -> &str {
id.split('-').next_back().unwrap_or(id)
}
fn new_id() -> String {
let stamp = jiff::Zoned::now().strftime("%Y%m%d-%H%M%S");
let seed = crate::rng::entropy();
format!("{stamp}-{:04x}", (seed ^ (seed >> 32)) & 0xffff)
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use crate::config::{AgentKind, AgentSpec, Graph};
use crate::queue::{Queue, Source, Task};
use super::*;
fn store() -> (tempfile::TempDir, Talks) {
let tmp = tempfile::tempdir().expect("tempdir");
let talks = Talks::at(tmp.path().join("talks"));
(tmp, talks)
}
fn mock_agent(dir: &Path, script: &str, env: BTreeMap<String, String>) -> AgentSpec {
let path = dir.join("mock-talk-agent.sh");
std::fs::write(&path, script).expect("write mock");
AgentSpec {
id: "mock".to_owned(),
kind: AgentKind::Command,
model: None,
command: vec!["sh".to_owned(), path.to_string_lossy().into_owned()],
extra_args: Vec::new(),
env,
prompt_delivery: None,
}
}
fn config(spec: AgentSpec) -> Config {
Config {
agents: vec![spec],
graph: Graph {
language: "en".to_owned(),
..Graph::default()
},
..Config::default()
}
}
const REPLY: &str = "#!/bin/sh\ncat >/dev/null\nprintf '%s\\n' \"$MOCK_REPLY\"\n";
const BROKEN: &str = "#!/bin/sh\ncat >/dev/null\nexit 3\n";
const ECHO: &str = "#!/bin/sh\ncat\n";
fn env(reply: &str) -> BTreeMap<String, String> {
BTreeMap::from([("MOCK_REPLY".to_owned(), reply.to_owned())])
}
#[test]
fn the_frozen_json_field_names_round_trip_through_disk() {
let (tmp, talks) = store();
let mut talk = Talk {
schema: SCHEMA,
id: "20260904-014455-ab12".to_owned(),
repo: tmp.path().to_owned(),
agent: "sonnet".to_owned(),
status: TalkStatus::Open,
turns: Vec::new(),
created_at: Timestamp::now(),
updated_at: Timestamp::now(),
seat: SeatState::new(SEAT, "sonnet", 7),
};
talks.put(&mut talk).expect("put");
let raw = std::fs::read_to_string(talks.path_of(&talk.id)).expect("read back");
let v: serde_json::Value = serde_json::from_str(&raw).expect("parse");
for field in [
"schema",
"id",
"repo",
"agent",
"status",
"turns",
"created_at",
"updated_at",
] {
assert!(v.get(field).is_some(), "missing field `{field}`");
}
assert_eq!(v["schema"], 1);
assert_eq!(v["status"], "open");
let back = talks.get(&talk.id).expect("get");
assert_eq!(back.id, talk.id);
assert_eq!(back.status, TalkStatus::Open);
}
#[test]
fn opening_a_talk_takes_no_agent_turn() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
let cfg = config(spec);
let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
assert_eq!(talk.status, TalkStatus::Open);
assert!(talk.turns.is_empty(), "nothing has been said yet");
let on_disk = talks.get(&talk.id).expect("get");
assert_eq!(on_disk.turns.len(), 0);
}
#[tokio::test]
async fn the_first_turn_carries_the_briefing_and_later_turns_do_not() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), ECHO, BTreeMap::new());
let cfg = config(spec);
let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
say(&mut talk, &talks, &cfg, "what does the queue module do?")
.await
.expect("first turn");
let first_prompt = &talk.turns[1].body;
assert!(first_prompt.contains("magi task add --solo"));
assert!(first_prompt.contains("what does the queue module do?"));
say(&mut talk, &talks, &cfg, "and how is it locked?")
.await
.expect("second turn");
let second_prompt = &talk.turns[3].body;
assert!(
!second_prompt.contains("magi task add --solo"),
"the briefing is sent once, not on every turn: {second_prompt}"
);
assert!(second_prompt.contains("and how is it locked?"));
}
#[tokio::test]
async fn say_appends_the_operator_turn_then_the_agent_turn() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), REPLY, env("go ahead"));
let cfg = config(spec);
let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
say(&mut talk, &talks, &cfg, "can I rename this function?")
.await
.expect("say");
assert_eq!(talk.turns.len(), 2);
assert_eq!(talk.turns[0].who, Who::Operator);
assert_eq!(talk.turns[0].body, "can I rename this function?");
assert_eq!(talk.turns[1].who, Who::Agent);
assert_eq!(talk.turns[1].body, "go ahead");
assert_eq!(talks.get(&talk.id).expect("get").turns, talk.turns);
}
#[tokio::test]
async fn a_failed_turn_keeps_the_operator_message_and_says_what_happened() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), BROKEN, BTreeMap::new());
let cfg = config(spec);
let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
let err = say(&mut talk, &talks, &cfg, "check the tests")
.await
.expect_err("a turn with no answer is an error");
assert!(err.to_string().contains("no answer"), "{err}");
let on_disk = talks.get(&talk.id).expect("get");
assert_eq!(on_disk.turns.len(), 2);
assert_eq!(on_disk.turns[0].body, "check the tests");
let note = &on_disk.turns[1];
assert_eq!(note.who, Who::Agent);
assert!(note.body.starts_with(MAGI_NOTE), "{}", note.body);
assert!(note.body.contains("your message is saved"));
}
#[test]
fn closing_is_idempotent_and_a_closed_talk_takes_no_more_turns() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), REPLY, env("hi"));
let cfg = config(spec);
let mut talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
close(&mut talk, &talks).expect("close");
assert_eq!(talk.status, TalkStatus::Closed);
close(&mut talk, &talks).expect("closing twice is not an error");
let err = record(&mut talk, &talks, "still there?").expect_err("closed talks refuse");
assert!(err.to_string().contains("closed"));
let _ = &cfg; }
#[tokio::test]
async fn a_close_that_lands_while_a_turn_is_in_flight_is_not_undone_by_the_reply() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), REPLY, env("here you go"));
let cfg = config(spec);
let mut in_flight = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
let mut closed_elsewhere = talks.get(&in_flight.id).expect("reread");
close(&mut closed_elsewhere, &talks).expect("close");
assert_eq!(
talks.get(&in_flight.id).expect("reread").status,
TalkStatus::Closed,
"the close landed on disk before the turn finished"
);
assert_eq!(in_flight.status, TalkStatus::Open);
respond(&mut in_flight, &talks, &cfg, "one more question")
.await
.expect("the turn itself still completes");
let on_disk = talks.get(&in_flight.id).expect("reread");
assert_eq!(
on_disk.status,
TalkStatus::Closed,
"a close must stick even when a turn that started before it finishes after it"
);
assert!(
on_disk.turns.iter().any(|t| t.body == "here you go"),
"the in-flight turn's own reply is still recorded: {:?}",
on_disk.turns
);
}
#[test]
fn a_close_that_lands_before_record_is_called_is_not_undone_by_it() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), REPLY, env("hi"));
let cfg = config(spec);
let mut stale = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
let mut closed_elsewhere = talks.get(&stale.id).expect("reread");
close(&mut closed_elsewhere, &talks).expect("close");
assert_eq!(
talks.get(&stale.id).expect("reread").status,
TalkStatus::Closed,
"the close landed on disk before record was called"
);
assert_eq!(stale.status, TalkStatus::Open);
let err = record(&mut stale, &talks, "still there?")
.expect_err("a close that landed first must be honored, not overwritten");
assert!(err.to_string().contains("closed"));
let on_disk = talks.get(&stale.id).expect("reread");
assert_eq!(
on_disk.status,
TalkStatus::Closed,
"record must not resurrect a conversation closed while its snapshot was stale"
);
assert!(
on_disk.turns.is_empty(),
"the rejected turn must not have been appended: {:?}",
on_disk.turns
);
let _ = &cfg; }
#[test]
fn close_blocks_on_records_guard_rather_than_interleaving_with_it() {
let (tmp, talks) = store();
let spec = mock_agent(tmp.path(), REPLY, env("hi"));
let cfg = config(spec);
let talk = begin(&talks, &cfg, tmp.path().to_owned(), None).expect("begin");
let held = talks.guard();
let talks2 = talks.clone();
let id = talk.id.clone();
let closing = std::thread::spawn(move || {
let mut talk = talks2.get(&id).expect("get");
close(&mut talk, &talks2).expect("close");
});
std::thread::sleep(Duration::from_millis(50));
assert!(
!closing.is_finished(),
"close must wait for the guard, not read and write while it is held - \
a re-read alone narrows this window without closing it"
);
drop(held);
closing.join().expect("close thread panicked");
assert_eq!(
talks.get(&talk.id).expect("reread").status,
TalkStatus::Closed,
"once the guard is free, close still lands"
);
let _ = &cfg; }
#[test]
fn list_puts_open_talks_before_closed_ones() {
let (tmp, talks) = store();
let make = |id: &str, status: TalkStatus| {
let mut t = Talk {
schema: SCHEMA,
id: id.to_owned(),
repo: tmp.path().to_owned(),
agent: "mock".to_owned(),
status,
turns: Vec::new(),
created_at: Timestamp::now(),
updated_at: Timestamp::now(),
seat: SeatState::new(SEAT, "mock", 7),
};
talks.put(&mut t).expect("put");
};
make("20260901-000000-0001", TalkStatus::Open);
make("20260902-000000-0002", TalkStatus::Open);
make("20260903-000000-0003", TalkStatus::Closed);
let ids: Vec<String> = talks.list().into_iter().map(|t| t.id).collect();
assert_eq!(
ids,
[
"20260902-000000-0002",
"20260901-000000-0001",
"20260903-000000-0003"
]
);
assert_eq!(talks.count_open(), 2);
}
#[test]
fn tasks_of_finds_only_this_talks_own_tasks() {
let dir = tempfile::tempdir().expect("tempdir");
let queue = Queue::at(dir.path().join("queue"));
let mut mine = Task::new(
"rework the loader".to_owned(),
"rework the loader".to_owned(),
PathBuf::from("/repo"),
Source::Agent {
run: "20260904-014455-ab12".to_owned(),
node: "chat".to_owned(),
},
);
queue.put(&mut mine).expect("put mine");
let mut theirs = Task::new(
"unrelated".to_owned(),
"unrelated".to_owned(),
PathBuf::from("/repo"),
Source::Agent {
run: "20260904-090000-zz99".to_owned(),
node: "implement".to_owned(),
},
);
queue.put(&mut theirs).expect("put theirs");
let mut human = Task::new(
"typed by hand".to_owned(),
"typed by hand".to_owned(),
PathBuf::from("/repo"),
Source::Human,
);
queue.put(&mut human).expect("put human");
let found = tasks_of(&queue, "20260904-014455-ab12");
assert_eq!(found.len(), 1);
assert_eq!(found[0].id, mine.id);
}
#[test]
fn the_briefing_names_solo_task_add_and_not_the_task_file_spec() {
let brief = briefing(Path::new("/repo"), "en");
assert!(brief.contains("magi task add --solo"));
assert!(!brief.contains(plan::TASK_FILE_SPEC));
assert!(brief.contains("/repo"));
assert!(!brief.contains("Hold this conversation in"));
}
#[test]
fn the_briefing_names_the_language_when_it_is_not_english() {
let brief = briefing(Path::new("/repo"), "Japanese");
assert!(brief.contains("Hold this conversation in Japanese"));
}
}