pub(crate) mod local;
pub(crate) mod proactive;
use std::io::{BufRead, Read as _, Write as _};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use serde_json::{Value, json};
use crate::collab::{
CollabChannel, CollabEvent, CollabRole, CollabSession, ResolvedSpan, position,
};
const EDIT_MARKER: &str = "<<<EDIT ";
const NOTE_MARKER: &str = "<<<NOTE ";
const END_MARKER: &str = "<<<END>>>";
#[derive(Debug)]
pub enum FenceEvent {
Commentary(String),
EditStart {
file: String,
start: (usize, usize),
end: (usize, usize),
},
EditBody(String),
EditEnd,
EditAbort,
NoteStart { file: String, row: usize },
NoteBody(String),
NoteEnd,
NoteAbort,
}
enum FenceState {
Outside,
Body {
held_newline: bool,
at_line_start: bool,
is_note: bool,
},
}
pub struct FenceMachine {
buf: String,
state: FenceState,
}
impl FenceMachine {
pub fn new() -> Self {
Self {
buf: String::new(),
state: FenceState::Outside,
}
}
pub fn push(&mut self, delta: &str) -> Vec<FenceEvent> {
self.buf.push_str(delta);
let mut events = Vec::new();
loop {
match self.state {
FenceState::Outside => {
let Some(nl) = self.buf.find('\n') else {
break;
};
let line: String = self.buf.drain(..=nl).collect();
let trimmed = &line[..line.len() - 1];
if let Some(start) = parse_header(trimmed) {
events.push(start);
self.state = FenceState::Body {
held_newline: false,
at_line_start: true,
is_note: false,
};
} else if let Some(start) = parse_note_header(trimmed) {
events.push(start);
self.state = FenceState::Body {
held_newline: false,
at_line_start: true,
is_note: true,
};
} else {
events.push(FenceEvent::Commentary(line));
}
}
FenceState::Body {
ref mut held_newline,
ref mut at_line_start,
is_note,
} => {
let body_event = if is_note {
FenceEvent::NoteBody
} else {
FenceEvent::EditBody
};
if *at_line_start {
match self.buf.find('\n') {
Some(nl) => {
if &self.buf[..nl] == END_MARKER {
self.buf.drain(..=nl);
events.push(if is_note {
FenceEvent::NoteEnd
} else {
FenceEvent::EditEnd
});
self.state = FenceState::Outside;
} else {
let mut body = String::new();
if *held_newline {
body.push('\n');
}
body.push_str(&self.buf[..nl]);
self.buf.drain(..=nl);
events.push(body_event(body));
*held_newline = true;
}
}
None => {
if END_MARKER.starts_with(self.buf.as_str()) {
break;
}
let mut body = String::new();
if *held_newline {
body.push('\n');
*held_newline = false;
}
body.push_str(&self.buf);
self.buf.clear();
events.push(body_event(body));
*at_line_start = false;
break;
}
}
} else {
match self.buf.find('\n') {
Some(nl) => {
if nl > 0 {
events.push(body_event(self.buf[..nl].to_string()));
}
self.buf.drain(..=nl);
*held_newline = true;
*at_line_start = true;
}
None => {
if !self.buf.is_empty() {
events.push(body_event(std::mem::take(&mut self.buf)));
}
break;
}
}
}
}
}
}
events
}
pub fn finish(&mut self) -> Vec<FenceEvent> {
let mut events = Vec::new();
match self.state {
FenceState::Outside => {
if !self.buf.is_empty() {
let mut tail = std::mem::take(&mut self.buf);
let last_start = tail.rfind('\n').map_or(0, |i| i + 1);
let line = tail[last_start..].trim_end_matches('\r');
if parse_header(line).is_some() || parse_note_header(line).is_some() {
tail.truncate(last_start);
}
if !tail.is_empty() {
events.push(FenceEvent::Commentary(tail));
}
}
}
FenceState::Body {
at_line_start,
is_note,
..
} => {
if at_line_start && self.buf == END_MARKER {
events.push(if is_note {
FenceEvent::NoteEnd
} else {
FenceEvent::EditEnd
});
} else if is_note {
events.push(FenceEvent::NoteAbort);
} else {
events.push(FenceEvent::EditAbort);
}
self.buf.clear();
}
}
self.state = FenceState::Outside;
events
}
}
fn parse_header(line: &str) -> Option<FenceEvent> {
let inner = line.strip_prefix(EDIT_MARKER)?.strip_suffix(">>>")?;
parse_char_range_header(inner).or_else(|| parse_whole_line_header(inner))
}
fn parse_char_range_header(inner: &str) -> Option<FenceEvent> {
let (rest, ec) = inner.rsplit_once(':')?;
let (rest, mid) = rest.rsplit_once(':')?;
let (sc, er) = mid.split_once('-')?;
let (file, sr) = rest.rsplit_once(':')?;
if file.is_empty() {
return None;
}
Some(FenceEvent::EditStart {
file: file.to_string(),
start: (sr.parse().ok()?, sc.parse().ok()?),
end: (er.parse().ok()?, ec.parse().ok()?),
})
}
fn parse_whole_line_header(inner: &str) -> Option<FenceEvent> {
let (file, range) = inner.rsplit_once(':')?;
let (sr, er) = range.split_once('-')?;
if file.is_empty() {
return None;
}
if let Some((_, tail)) = file.rsplit_once(':')
&& !tail.is_empty()
&& tail.chars().all(|c| c.is_ascii_digit())
{
return None;
}
Some(FenceEvent::EditStart {
file: file.to_string(),
start: (sr.parse().ok()?, 0),
end: (er.parse().ok()?, usize::MAX),
})
}
fn parse_note_header(line: &str) -> Option<FenceEvent> {
let inner = line.strip_prefix(NOTE_MARKER)?.strip_suffix(">>>")?;
let (file, row) = inner.rsplit_once(':')?;
if file.is_empty() {
return None;
}
Some(FenceEvent::NoteStart {
file: file.to_string(),
row: row.parse().ok()?,
})
}
pub fn range_bytes(lines: &[String], start: (usize, usize), end: (usize, usize)) -> (usize, usize) {
(
crate::collab::byte_offset(lines, start.0, start.1),
crate::collab::byte_offset(lines, end.0, end.1),
)
}
fn note_offset(lines: &[String], row: usize) -> usize {
let row = row.min(lines.len().saturating_sub(1));
crate::collab::byte_offset(lines, row, 0)
}
fn shift_notes(notes: &mut [Note], file: &str, spans: &[ResolvedSpan]) {
for n in notes.iter_mut().filter(|n| n.file == file) {
for span in spans {
n.offset = shift_offset(n.offset, span);
}
}
}
pub fn shift_offset(x: usize, span: &ResolvedSpan) -> usize {
if span.at + span.deleted <= x {
x + span.inserted.len() - span.deleted
} else if span.at < x {
span.at + span.inserted.len()
} else {
x
}
}
const ALLOWED_TOOLS: &str = "mcp__croft-collab__collab_open,mcp__croft-collab__collab_read,\
mcp__croft-collab__collab_status,Read,Grep,Glob";
const LIVE_TIMEOUT: Duration = Duration::from_secs(4);
const PAIR_SYSTEM_PROMPT: &str = r#"You are pair-programming INSIDE the croft editor: your streamed text is parsed live and fenced edits are applied to the shared buffer token by token, so your human partner watches the edit appear as you write it.
To edit a file, emit an edit fence directly in your text output:
<<<EDIT <file>:<start_row>:<start_col>-<end_row>:<end_col>>>>
<replacement text>
<<<END>>>
Rules:
- The header carries the file plus EXACTLY FOUR integers: START_ROW:START_COL-END_ROW:END_COL. Never omit the rows — for a single-line edit on line N the header reads <file>:N:C1-N:C2 (e.g. replacing columns 15..19 of line 0 of demo.txt is `<<<EDIT demo.txt:0:15-0:19>>>`, NOT `demo.txt:15:19`). A header without all four numbers is ignored as plain text and your edit does not happen.
- <file> is the workspace-relative path. Coordinates are 0-based CHARACTER positions (not bytes) into the file's CURRENT text: the range start..end (start inclusive, end exclusive) is deleted and the fence body replaces it. To insert without deleting, use a zero-width range (start == end).
- Compute coordinates against the buffer text in the latest user message (the --- CURRENT BUFFER --- block) or from the mcp__croft-collab__collab_read tool. If you emit several fences in one reply, later fences must use coordinates that account for your earlier fences' changes.
- The header and <<<END>>> each sit alone on their own line. The body between them is applied verbatim: real code only, no markdown fences, no commentary.
- Everything outside a fence is commentary shown to your partner in the terminal; it is never applied to any buffer.
- Start the fence as soon as you know the edit; stream the body naturally rather than planning silently.
Example (replace lines 3-4 of src/lib.rs, where line 4 is 12 chars long):
<<<EDIT src/lib.rs:3:0-4:12>>>
fn renamed() -> u32 {
41 + 1
}
<<<END>>>
To pin a remark to a specific line WITHOUT editing anything, emit a note fence:
<<<NOTE <file>:<row>>>>
<what you want to say about that line>
<<<END>>>
Note rules:
- The header carries the file plus EXACTLY ONE integer, the 0-based row the note speaks about (e.g. `<<<NOTE demo.txt:4>>>` pins a remark to line 4 of demo.txt). Your partner sees it attached to that line in their editor.
- Notes never change any buffer. Use them for review remarks, questions, and change proposals.
Some user messages number the buffer: each line arrives prefixed with `N|` where N is its 0-based row. The prefix is a label, not content — character columns still count from the first character AFTER the first `|`, and NOTE/EDIT rows use exactly those N values.
Turn kinds:
- An ask turn names a task and where you were invoked: you may edit with EDIT fences and remark with NOTE fences.
- A yielded turn says it is COMMENT-ONLY: your partner handed you the floor to review, not to type. EDIT fences on such a turn are DISCARDED by the editor. Speak in NOTE fences anchored to the lines you mean, propose changes there, and wait to be asked before editing.
A participant can cancel your stream mid-edit; the streamed text is then reverted and your next user message starts with a note saying so. When that happens, stop that approach and ask what they want instead."#;
const LOCAL_MODEL_PROMPT_SUFFIX: &str = r#"Local-model addendum:
- PREFER the whole-line EDIT header form: <<<EDIT <file>:<start_row>-<end_row>>>> with exactly TWO integers. It replaces those entire lines — both rows 0-based, both INCLUSIVE — with the fence body, so you never count characters. Replacing line 1 whole is `<<<EDIT greet.py:1-1>>>`.
- Rewrite whole lines instead of splicing inside a line; use the four-integer form only when you must keep part of a line.
- You have no tools: everything you need is in the message. Keep replies short — the fences plus at most a couple of commentary lines."#;
#[derive(Clone, Debug, PartialEq)]
pub enum Provider {
Claude,
Local { base_url: String },
}
impl Provider {
pub fn from_record(provider: Option<&str>, base_url: Option<&str>) -> Self {
match provider {
Some("ollama") => Provider::Local {
base_url: base_url.unwrap_or("http://localhost:11434").to_string(),
},
_ => Provider::Claude,
}
}
}
pub(crate) fn system_prompt(provider: &Provider) -> std::borrow::Cow<'static, str> {
match provider {
Provider::Claude => std::borrow::Cow::Borrowed(PAIR_SYSTEM_PROMPT),
Provider::Local { .. } => std::borrow::Cow::Owned(format!(
"{PAIR_SYSTEM_PROMPT}\n\n{LOCAL_MODEL_PROMPT_SUFFIX}"
)),
}
}
pub struct PairConfig {
pub socket: PathBuf,
pub workspace: PathBuf,
pub name: String,
pub model: Option<String>,
pub task: Option<String>,
pub provider: Provider,
}
struct StreamRegion {
file: String,
start: usize,
anchor: usize,
original: String,
displaced: Vec<(u64, usize)>,
}
pub(crate) struct StagedTurn {
file: String,
pub(crate) prior_seen: Option<String>,
prior_comment_only: bool,
prior_target: Option<String>,
stage_gen: u64,
}
pub struct Note {
pub id: u64,
pub file: String,
pub offset: usize,
pub body: String,
}
pub(crate) struct PairState {
session: CollabSession,
region: Option<StreamRegion>,
discarding: bool,
cancelled: bool,
can_interrupt: bool,
turn_active: bool,
target_file: Option<String>,
pending_note: Option<String>,
stage_gen: u64,
notes: Vec<Note>,
next_note_id: u64,
note_in_flight: Option<(String, usize, String)>,
events: Option<Sender<crate::pair_host::PairEvent>>,
comment_only: bool,
pending_caret: Option<(String, usize)>,
last_seen: std::collections::HashMap<String, String>,
}
impl PairState {
pub(crate) fn new(
session: CollabSession,
events: Option<Sender<crate::pair_host::PairEvent>>,
) -> Self {
Self {
session,
region: None,
discarding: false,
cancelled: false,
can_interrupt: false,
turn_active: false,
target_file: None,
pending_note: None,
stage_gen: 0,
notes: Vec::new(),
next_note_id: 1,
note_in_flight: None,
events,
comment_only: false,
pending_caret: None,
last_seen: std::collections::HashMap::new(),
}
}
pub(crate) fn doc_lines(&self, file: &str) -> Option<Vec<String>> {
self.session
.doc_text(file)
.map(|t| t.split('\n').map(String::from).collect())
}
pub(crate) fn notes_in<'a>(&'a self, file: &'a str) -> impl Iterator<Item = &'a Note> {
self.notes.iter().filter(move |n| n.file == file)
}
pub(crate) fn turn_active(&self) -> bool {
self.turn_active
}
pub(crate) fn clear_all_notes(&mut self) {
self.notes.clear();
}
pub(crate) fn remove_note(&mut self, id: u64) {
self.notes.retain(|n| n.id != id);
}
pub(crate) fn append_to_note(&mut self, id: u64, line: &str) {
if let Some(n) = self.notes.iter_mut().find(|n| n.id == id) {
n.body.push('\n');
n.body.push_str(line);
}
}
fn take_note_id(&mut self) -> u64 {
let id = self.next_note_id;
self.next_note_id += 1;
id
}
pub(crate) fn begin_turn(
&mut self,
file: &str,
content: &str,
comment_only: bool,
) -> Option<String> {
self.comment_only = comment_only;
self.target_file = Some(file.to_string());
self.session.request_file(file);
self.last_seen.insert(file.to_string(), content.to_string())
}
pub(crate) fn stage_turn(
&mut self,
file: &str,
content: &str,
comment_only: bool,
) -> StagedTurn {
let prior_comment_only = self.comment_only;
let prior_target = self.target_file.clone();
let prior_seen = self.begin_turn(file, content, comment_only);
self.stage_gen += 1;
StagedTurn {
file: file.to_string(),
prior_seen,
prior_comment_only,
prior_target,
stage_gen: self.stage_gen,
}
}
pub(crate) fn unstage_turn(&mut self, staged: StagedTurn) {
if self.stage_gen != staged.stage_gen {
return;
}
self.stage_gen = staged.stage_gen - 1;
self.comment_only = staged.prior_comment_only;
self.target_file = staged.prior_target;
match staged.prior_seen {
Some(old) => {
self.last_seen.insert(staged.file, old);
}
None => {
self.last_seen.remove(&staged.file);
}
}
}
pub(crate) fn park_caret(&mut self, file: &str, row: usize) {
self.pending_caret = (!self.send_parked_caret(file, row)).then(|| (file.to_string(), row));
}
fn caret_now(&mut self, file: &str, row: usize, col: usize) {
self.pending_caret = None;
self.session.send_caret(file, row, col);
}
pub(crate) fn my_site_ids(&self) -> Vec<u64> {
self.session.my_site_ids()
}
pub(crate) fn last_seen_of(&self, file: &str) -> Option<String> {
self.last_seen.get(file).cloned()
}
fn send_parked_caret(&mut self, file: &str, row: usize) -> bool {
let Some(lines) = self.doc_lines(file) else {
return false;
};
let row = row.min(lines.len().saturating_sub(1));
self.session.send_caret(file, row, 0);
true
}
fn resolve_pending_caret(&mut self) {
let Some((file, row)) = self.pending_caret.clone() else {
return;
};
if self.send_parked_caret(&file, row) || !self.session.is_bootstrapping(&file) {
self.pending_caret = None;
}
}
}
pub(crate) fn lock_briefly(
state: &Mutex<PairState>,
) -> Option<std::sync::MutexGuard<'_, PairState>> {
let deadline = std::time::Instant::now() + Duration::from_millis(100);
loop {
if let Ok(guard) = state.try_lock() {
return Some(guard);
}
if std::time::Instant::now() > deadline {
return None;
}
std::thread::sleep(Duration::from_millis(1));
}
}
fn diag(state: &Mutex<PairState>, level: crate::output::OutputLevel, text: &str) {
let seated = lock_briefly(state).is_none_or(|st| st.events.is_some());
if seated {
crate::output::push("Navigator", level, text);
} else {
println!("{text}");
let _ = std::io::Write::flush(&mut std::io::stdout());
}
}
fn say(state: &Mutex<PairState>, text: &str) {
let sink = state.lock().unwrap().events.clone();
match sink {
Some(tx) => {
let _ = tx.send(crate::pair_host::PairEvent::Commentary(text.to_string()));
}
None => {
print!("{text}");
let _ = std::io::Stdout::flush(&mut std::io::stdout());
}
}
}
fn wait_live(state: &Mutex<PairState>, file: &str) -> bool {
let deadline = Instant::now() + LIVE_TIMEOUT;
loop {
let st = state.lock().unwrap();
if st.session.is_live(file) {
return true;
}
if !st.session.is_bootstrapping(file) || Instant::now() >= deadline {
return false;
}
drop(st);
std::thread::sleep(Duration::from_millis(20));
}
}
#[derive(Clone)]
pub(crate) enum TurnSink {
Claude(Arc<Mutex<Option<ChildStdin>>>),
Local(Sender<String>),
}
impl TurnSink {
fn send_user(&self, content: &str) -> Result<()> {
match self {
TurnSink::Claude(writer) => {
let msg = json!({
"type": "user",
"message": { "role": "user", "content": content },
});
let mut w = writer.lock().unwrap();
let w = w.as_mut().context("claude stdin already closed")?;
writeln!(w, "{msg}").context("writing user turn to claude")?;
w.flush().context("flushing claude stdin")
}
TurnSink::Local(turns) => {
anyhow::ensure!(
turns.send(content.to_string()).is_ok(),
"the local turn worker is gone"
);
Ok(())
}
}
}
}
struct ChildGuard(Child);
impl Drop for ChildGuard {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
pub(crate) struct TurnEnd {
pub(crate) is_error: bool,
pub(crate) cancelled: bool,
pub(crate) text: String,
}
fn claude_search_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
if let Some(home) = std::env::var_os("HOME") {
let home = PathBuf::from(home);
dirs.push(home.join(".claude").join("local"));
dirs.push(home.join(".local").join("bin"));
dirs.push(home.join(".npm-global").join("bin"));
}
dirs.push(PathBuf::from("/opt/homebrew/bin"));
dirs.push(PathBuf::from("/usr/local/bin"));
dirs
}
fn resolve_claude_in(on_path: bool, dirs: &[PathBuf]) -> Option<String> {
if on_path {
return Some(String::from("claude"));
}
dirs.iter()
.map(|d| d.join("claude"))
.find(|p| p.is_file())
.map(|p| p.to_string_lossy().into_owned())
}
fn claude_program() -> String {
resolve_claude_in(
crate::lsp::manager::is_on_path("claude"),
&claude_search_dirs(),
)
.unwrap_or_else(|| String::from("claude"))
}
pub(crate) fn claude_command(cfg: &PairConfig) -> Result<Command> {
let exe = std::env::current_exe().context("resolving croft binary path")?;
let mcp_config = json!({
"mcpServers": {
"croft-collab": {
"command": exe.to_string_lossy(),
"args": [
"collab-agent",
"--workspace", cfg.workspace.to_string_lossy(),
"--name", format!("{}-reader", cfg.name),
],
}
}
});
let mut cmd = Command::new(claude_program());
cmd.arg("-p")
.args(["--input-format", "stream-json"])
.args(["--output-format", "stream-json"])
.arg("--verbose")
.arg("--include-partial-messages")
.args(["--permission-mode", "dontAsk"])
.args(["--allowedTools", ALLOWED_TOOLS])
.arg("--strict-mcp-config")
.args(["--mcp-config", &mcp_config.to_string()])
.args(["--append-system-prompt", &system_prompt(&cfg.provider)])
.current_dir(&cfg.workspace);
if let Some(model) = &cfg.model {
cmd.args(["--model", model]);
}
Ok(cmd)
}
pub fn run(cfg: PairConfig) -> Result<()> {
let pilot = match &cfg.provider {
Provider::Claude => seat_pilot(&cfg.socket, &cfg.name, claude_command(&cfg)?, None)?,
Provider::Local { base_url } => {
let model = cfg
.model
.as_deref()
.context("a local provider needs --model (there is no CLI default)")?;
seat_local(&cfg.socket, &cfg.name, base_url, model, None)?
}
};
let stdin = std::io::stdin();
run_pilot(pilot, &cfg.name, cfg.task, &mut stdin.lock())
}
fn connect_session(socket: &Path, name: &str) -> Result<CollabSession> {
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if let Some(ch) = CollabChannel::connect(socket, CollabRole::Guest) {
return Ok(CollabSession::new(ch, name.to_string()));
}
anyhow::ensure!(
Instant::now() < deadline,
"collab relay never came up at {}",
socket.display()
);
std::thread::sleep(Duration::from_millis(10));
}
}
pub(crate) struct Pilot {
pub(crate) state: Arc<Mutex<PairState>>,
pub(crate) sink: TurnSink,
pub(crate) turn_rx: Receiver<TurnEnd>,
stop: Arc<AtomicBool>,
transport: Transport,
}
enum Transport {
Claude {
guard: ChildGuard,
reader: std::thread::JoinHandle<()>,
pump: std::thread::JoinHandle<()>,
},
Local { pump: std::thread::JoinHandle<()> },
}
impl Pilot {
pub(crate) fn reader_finished(&self) -> bool {
match &self.transport {
Transport::Claude { reader, .. } => reader.is_finished(),
Transport::Local { .. } => false,
}
}
pub(crate) fn shutdown(self) {
let Pilot {
state,
sink,
turn_rx: _,
stop,
transport,
} = self;
{
let mut st = state.lock().unwrap();
revert_region(&mut st);
}
stop.store(true, Ordering::Relaxed);
match transport {
Transport::Claude {
mut guard,
reader,
pump,
} => {
if let TurnSink::Claude(writer) = &sink {
writer.lock().unwrap().take(); }
let grace = Instant::now() + Duration::from_secs(2);
loop {
match guard.0.try_wait() {
Ok(Some(_)) => break,
Ok(None) if Instant::now() < grace => {
std::thread::sleep(Duration::from_millis(50))
}
_ => {
let _ = guard.0.kill();
break;
}
}
}
let _ = pump.join();
let _ = reader.join();
}
Transport::Local { pump } => {
state.lock().unwrap().cancelled = true;
drop(sink); let _ = pump.join();
}
}
}
}
pub(crate) fn seat_pilot(
socket: &Path,
name: &str,
mut cmd: Command,
events: Option<Sender<crate::pair_host::PairEvent>>,
) -> Result<Pilot> {
let session = connect_session(socket, name)?;
let state = Arc::new(Mutex::new(PairState::new(session, events.clone())));
cmd.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(unix)]
unsafe {
use std::os::unix::process::CommandExt;
cmd.pre_exec(|| {
libc::setsid();
Ok(())
});
}
let mut child = cmd.spawn().context("spawning the claude CLI")?;
let child_stdin = child.stdin.take().context("claude stdin missing")?;
let child_stdout = child.stdout.take().context("claude stdout missing")?;
let child_stderr = child.stderr.take().context("claude stderr missing")?;
let guard = ChildGuard(child);
let sink = TurnSink::Claude(Arc::new(Mutex::new(Some(child_stdin))));
let stop = Arc::new(AtomicBool::new(false));
let (turn_tx, turn_rx) = std::sync::mpsc::channel::<TurnEnd>();
let reader = {
let state = Arc::clone(&state);
std::thread::spawn(move || {
let mut fence = FenceMachine::new();
let mut decoder = crate::mcp::transport::LineDecoder::new();
let mut stdout = child_stdout;
let mut buf = [0u8; 8192];
loop {
let n = match stdout.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => n,
};
decoder.feed(&buf[..n]);
while let Some(msg) = decoder.next_message() {
handle_claude_event(&state, &mut fence, &msg, &turn_tx);
}
}
})
};
{
let events = events.clone();
std::thread::spawn(move || {
let mut reader = std::io::BufReader::new(child_stderr);
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line) {
Ok(0) | Err(_) => break,
Ok(_) => match &events {
Some(_) => {
crate::output::push(
"Navigator",
crate::output::OutputLevel::Info,
&format!("[claude] {}", line.trim_end()),
);
}
None => eprint!("[claude] {line}"),
},
}
}
});
}
let pump = {
let state = Arc::clone(&state);
let sink = sink.clone();
let stop = Arc::clone(&stop);
std::thread::spawn(move || {
let req_id = AtomicU64::new(0);
while !stop.load(Ordering::Relaxed) {
pump_session(&state, &sink, &req_id);
std::thread::sleep(Duration::from_millis(25));
}
})
};
Ok(Pilot {
state,
sink,
turn_rx,
stop,
transport: Transport::Claude {
guard,
reader,
pump,
},
})
}
pub(crate) fn seat_local(
socket: &Path,
name: &str,
base_url: &str,
model: &str,
events: Option<Sender<crate::pair_host::PairEvent>>,
) -> Result<Pilot> {
let session = connect_session(socket, name)?;
let state = Arc::new(Mutex::new(PairState::new(session, events)));
let stop = Arc::new(AtomicBool::new(false));
let (turns_tx, turns_rx) = std::sync::mpsc::channel::<String>();
let (end_tx, turn_rx) = std::sync::mpsc::channel::<TurnEnd>();
let sink = TurnSink::Local(turns_tx);
{
let state = Arc::clone(&state);
let (base_url, model) = (base_url.to_string(), model.to_string());
let system = system_prompt(&Provider::Local {
base_url: base_url.clone(),
})
.into_owned();
std::thread::spawn(move || {
let mut messages: Vec<Value> = Vec::new();
while let Ok(body) = turns_rx.recv() {
messages.push(json!({ "role": "user", "content": body }));
local::stream_turn(&base_url, &model, &system, &mut messages, &state, &end_tx);
}
});
}
let pump = {
let state = Arc::clone(&state);
let sink = sink.clone();
let stop = Arc::clone(&stop);
std::thread::spawn(move || {
let req_id = AtomicU64::new(0);
while !stop.load(Ordering::Relaxed) {
pump_session(&state, &sink, &req_id);
std::thread::sleep(Duration::from_millis(25));
}
})
};
Ok(Pilot {
state,
sink,
turn_rx,
stop,
transport: Transport::Local { pump },
})
}
fn run_pilot(
pilot: Pilot,
name: &str,
task: Option<String>,
input: &mut dyn BufRead,
) -> Result<()> {
println!("croft pair: '{name}' seated; type a task ('@<file> <task>' to focus a buffer)");
let mut pending = task;
let mut hung_up = false;
loop {
let line = match pending.take() {
Some(t) => t,
None => {
let mut line = String::new();
match input.read_line(&mut line) {
Ok(0) | Err(_) => break,
Ok(_) => line.trim().to_string(),
}
}
};
if line.is_empty() {
continue;
}
send_turn(&pilot.state, &pilot.sink, &line)?;
match pilot.turn_rx.recv() {
Ok(end) if end.cancelled => println!("\n[turn cancelled]"),
Ok(end) if end.is_error => println!("\n[turn failed: {}]", end.text),
Ok(_) => println!("\n[turn done]"),
Err(_) => {
hung_up = true;
break;
}
}
}
pilot.shutdown();
anyhow::ensure!(!hung_up, "claude exited mid-conversation");
Ok(())
}
fn handle_claude_event(
state: &Mutex<PairState>,
fence: &mut FenceMachine,
msg: &Value,
turn_tx: &Sender<TurnEnd>,
) {
match msg.get("type").and_then(Value::as_str) {
Some("system") if msg.get("subtype").and_then(Value::as_str) == Some("init") => {
let can = msg
.get("capabilities")
.and_then(Value::as_array)
.is_some_and(|caps| {
caps.iter()
.any(|c| c.as_str() == Some("interrupt_receipt_v1"))
});
state.lock().unwrap().can_interrupt = can;
}
Some("stream_event") => {
let delta = msg.pointer("/event/delta");
if delta.and_then(|d| d.get("type")).and_then(Value::as_str) == Some("text_delta")
&& let Some(text) = delta.and_then(|d| d.get("text")).and_then(Value::as_str)
{
for event in fence.push(text) {
apply_fence_event(state, event);
}
}
}
Some("result") => {
if !state.lock().unwrap().turn_active {
return;
}
for event in fence.finish() {
apply_fence_event(state, event);
}
let mut st = state.lock().unwrap();
let cancelled = st.cancelled;
st.turn_active = false;
st.cancelled = false;
st.discarding = false;
st.comment_only = false;
drop(st);
let _ = turn_tx.send(TurnEnd {
is_error: msg
.get("is_error")
.and_then(Value::as_bool)
.unwrap_or(false),
cancelled,
text: msg
.get("result")
.and_then(Value::as_str)
.unwrap_or_default()
.to_string(),
});
}
_ => {} }
}
fn apply_fence_event(state: &Mutex<PairState>, event: FenceEvent) {
match event {
FenceEvent::Commentary(text) => {
say(state, &text);
}
FenceEvent::EditStart { file, start, end } => {
{
let mut st = state.lock().unwrap();
if st.cancelled {
st.discarding = true;
return;
}
if st.comment_only {
st.discarding = true;
drop(st);
diag(
state,
crate::output::OutputLevel::Warn,
"[pair] edit suppressed: this is a yielded, comment-only turn",
);
return;
}
st.target_file = Some(file.clone());
st.session.request_file(&file);
}
if wait_live(state, &file) {
let mut st = state.lock().unwrap();
open_region(&mut st, &file, start, end);
} else {
state.lock().unwrap().discarding = true;
diag(
state,
crate::output::OutputLevel::Warn,
&format!(
"[pair] no live croft session serves {file}; edit dropped \
(start croft in this workspace first)"
),
);
}
}
FenceEvent::EditBody(delta) => {
let mut st = state.lock().unwrap();
if st.cancelled || st.discarding {
return;
}
let Some((file, anchor)) = st.region.as_ref().map(|r| (r.file.clone(), r.anchor))
else {
return;
};
let Some(doc) = st.session.doc_text(&file).map(str::to_string) else {
return;
};
let anchor = anchor.min(doc.len());
let new = format!("{}{}{}", &doc[..anchor], delta, &doc[anchor..]);
st.session.local_change(&file, &new);
let span = ResolvedSpan {
at: anchor,
deleted: 0,
inserted: delta.clone(),
};
shift_notes(&mut st.notes, &file, std::slice::from_ref(&span));
let next = anchor + delta.len();
if let Some(r) = st.region.as_mut() {
r.anchor = next;
}
let lines: Vec<String> = new.split('\n').map(String::from).collect();
let (row, col) = position(&lines, next);
st.caret_now(&file, row, col);
}
FenceEvent::EditEnd => {
let mut st = state.lock().unwrap();
st.discarding = false;
if st.cancelled {
return; }
if let Some(r) = st.region.take() {
st.session.send_stream_state(&r.file, false);
}
}
FenceEvent::EditAbort => {
let mut st = state.lock().unwrap();
st.discarding = false;
if st.cancelled {
st.region = None;
return;
}
revert_region(&mut st);
}
FenceEvent::NoteStart { file, row } => {
let mut st = state.lock().unwrap();
if st.cancelled {
return;
}
st.session.request_file(&file);
st.note_in_flight = Some((file, row, String::new()));
}
FenceEvent::NoteBody(delta) => {
let mut st = state.lock().unwrap();
if let Some((_, _, body)) = st.note_in_flight.as_mut() {
body.push_str(&delta);
}
}
FenceEvent::NoteEnd => {
let pending = {
let mut st = state.lock().unwrap();
let pending = st.note_in_flight.take();
if st.cancelled { None } else { pending }
};
let Some((file, row, body)) = pending else {
return;
};
if !wait_live(state, &file) {
diag(
state,
crate::output::OutputLevel::Warn,
&format!(
"[pair] no live croft session serves {file}; note dropped \
(start croft in this workspace first)"
),
);
return;
}
let mut st = state.lock().unwrap();
let Some(lines) = st.doc_lines(&file) else {
drop(st);
diag(
state,
crate::output::OutputLevel::Warn,
&format!(
"[pair] no live croft session serves {file}; note dropped \
(start croft in this workspace first)"
),
);
return;
};
let offset = note_offset(&lines, row);
let row_now = position(&lines, offset).0;
st.caret_now(&file, row_now, 0);
let id = st.take_note_id();
let sink = st.events.clone();
st.notes.push(Note {
id,
file: file.clone(),
offset,
body: body.clone(),
});
match sink {
Some(tx) => {
let _ = tx.send(crate::pair_host::PairEvent::NoteAdded {
file,
row: row_now,
body,
});
}
None => {
drop(st);
say(state, &format!("[note {file}:{}] {body}\n", row_now + 1));
}
}
}
FenceEvent::NoteAbort => {
state.lock().unwrap().note_in_flight = None;
}
}
}
fn open_region(st: &mut PairState, file: &str, start: (usize, usize), end: (usize, usize)) {
let Some(doc) = st.session.doc_text(file).map(str::to_string) else {
st.discarding = true;
return;
};
let lines: Vec<String> = doc.split('\n').map(String::from).collect();
let (s, e) = range_bytes(&lines, start, end);
let (s, e) = (s.min(doc.len()), e.min(doc.len()));
if s > e {
st.discarding = true;
crate::output::push(
"Navigator",
crate::output::OutputLevel::Warn,
"fence range is inverted; edit dropped",
);
return;
}
let original = doc[s..e].to_string();
let new = format!("{}{}", &doc[..s], &doc[e..]);
st.session.local_change(file, &new);
let displaced: Vec<(u64, usize)> = st
.notes
.iter()
.filter(|n| n.file == file && (s..e).contains(&n.offset))
.map(|n| (n.id, n.offset - s))
.collect();
let span = ResolvedSpan {
at: s,
deleted: e - s,
inserted: String::new(),
};
shift_notes(&mut st.notes, file, std::slice::from_ref(&span));
st.region = Some(StreamRegion {
file: file.to_string(),
start: s,
anchor: s,
original,
displaced,
});
st.discarding = false;
st.session.send_stream_state(file, true);
let lines: Vec<String> = new.split('\n').map(String::from).collect();
let (row, col) = position(&lines, s);
st.caret_now(file, row, col);
}
fn revert_region(st: &mut PairState) {
let Some(r) = st.region.take() else {
return;
};
if let Some(doc) = st.session.doc_text(&r.file).map(str::to_string) {
let start = r.start.min(doc.len());
let anchor = r.anchor.clamp(start, doc.len());
let new = format!("{}{}{}", &doc[..start], r.original, &doc[anchor..]);
st.session.local_change(&r.file, &new);
let span = ResolvedSpan {
at: start,
deleted: anchor - start,
inserted: r.original.clone(),
};
shift_notes(&mut st.notes, &r.file, std::slice::from_ref(&span));
for (id, delta) in &r.displaced {
if let Some(n) = st
.notes
.iter_mut()
.find(|n| n.id == *id && n.file == r.file)
{
n.offset = start + delta;
}
}
}
st.session.send_stream_state(&r.file, false);
}
fn pump_session(state: &Mutex<PairState>, sink: &TurnSink, req_id: &AtomicU64) {
let mut interrupt = false;
{
let mut st = state.lock().unwrap();
let events = st.session.poll(|_| None);
for event in events {
match event {
CollabEvent::RemoteEdit { file, spans } => {
if let Some(r) = st.region.as_mut()
&& r.file == file
{
for span in &spans {
r.start = shift_offset(r.start, span);
r.anchor = shift_offset(r.anchor, span);
}
}
shift_notes(&mut st.notes, &file, &spans);
}
CollabEvent::StreamCancel => {
if !st.turn_active || st.cancelled {
continue;
}
st.cancelled = true;
st.pending_note = Some(
"Note: your previous streamed edit was cancelled and reverted by a \
participant. Stop that approach and ask what they want instead."
.to_string(),
);
revert_region(&mut st);
interrupt = st.can_interrupt;
match st.events.clone() {
Some(tx) => {
let _ = tx.send(crate::pair_host::PairEvent::Commentary(
"[stream cancelled by a participant; reverted]".to_string(),
));
}
None => println!("\n[stream cancelled by a participant; reverted]"),
}
}
_ => {}
}
}
st.resolve_pending_caret();
}
if interrupt {
let TurnSink::Claude(writer) = sink else {
return; };
if let Some(w) = writer.lock().unwrap().as_mut() {
let id = req_id.fetch_add(1, Ordering::Relaxed);
let msg = json!({
"type": "control_request",
"request_id": format!("croft-pair-{id}"),
"request": { "subtype": "interrupt" },
});
let _ = writeln!(w, "{msg}");
let _ = w.flush();
}
}
}
fn parse_task_line(line: &str) -> (Option<String>, String) {
let Some(rest) = line.strip_prefix('@') else {
return (None, line.to_string());
};
match rest.split_once(char::is_whitespace) {
Some((file, task)) => (Some(file.to_string()), task.trim().to_string()),
None => (Some(rest.to_string()), String::new()),
}
}
fn compose_turn_text(task: &str, file: Option<&str>, buffer: Option<&str>) -> String {
let mut content = String::new();
content.push_str(task);
match (file, buffer) {
(Some(file), Some(text)) => {
content.push_str(&format!(
"\n\n--- CURRENT BUFFER ({file}) ---\n{text}\n--- END BUFFER ---"
));
}
(Some(file), None) => {
content.push_str(&format!(
"\n\nTarget file: {file} (its buffer is not shared yet; \
read it with the collab tools before editing)"
));
}
(None, _) => {}
}
content
}
fn numbered(content: &str) -> String {
content
.split('\n')
.enumerate()
.map(|(i, l)| format!("{i}|{l}"))
.collect::<Vec<_>>()
.join("\n")
}
pub(crate) fn compose_ask_turn(
file: &str,
range: (usize, usize),
selection: &str,
instruction: &str,
content: &str,
) -> String {
let mut text = String::new();
let (s, e) = range;
if s == e {
text.push_str(&format!(
"Your partner invoked you on {file} at line {s} (0-based).\n"
));
} else {
text.push_str(&format!(
"Your partner invoked you on {file} at lines {s}-{e} (0-based).\n"
));
}
if !selection.is_empty() {
text.push_str(&format!(
"--- SELECTED LINES ---\n{selection}\n--- END SELECTED ---\n"
));
}
text.push_str(&format!("\nTask: {instruction}\n"));
text.push_str(&format!(
"\n--- CURRENT BUFFER ({file}, each line prefixed with its 0-based \
number and '|') ---\n{}\n--- END BUFFER ---",
numbered(content)
));
text
}
pub(crate) fn compose_yield_turn(file: &str, content: &str, diff: Option<&str>) -> String {
let mut text = String::from(
"Your partner yielded the turn: review the file below and speak \
through NOTE fences anchored to the lines you mean. This turn is \
COMMENT-ONLY: any EDIT fence will be discarded by the host. \
Propose changes in your notes and wait to be asked.\n",
);
if let Some(diff) = diff {
text.push_str(&format!(
"\n--- CHANGES SINCE YOUR LAST LOOK ---\n{diff}\n--- END CHANGES ---\n"
));
}
text.push_str(&format!(
"\n--- CURRENT BUFFER ({file}, each line prefixed with its 0-based \
number and '|') ---\n{}\n--- END BUFFER ---",
numbered(content)
));
text
}
pub(crate) fn compose_reply_turn(
file: &str,
row: usize,
note_body: &str,
reply: &str,
content: &str,
) -> String {
format!(
"Your partner replied to your note on {file} at line {row} (0-based).\n\
--- YOUR NOTE ---\n{note_body}\n--- END NOTE ---\n\
--- THEIR REPLY ---\n{reply}\n--- END REPLY ---\n\
Answer with NOTE fences (anchor follow-ups to the lines you mean; \
re-anchoring to line {row} continues this box). This turn is \
COMMENT-ONLY: any EDIT fence will be discarded by the host.\n\
\n--- CURRENT BUFFER ({file}, each line prefixed with its 0-based \
number and '|') ---\n{}\n--- END BUFFER ---",
numbered(content)
)
}
#[cfg(test)]
pub(crate) fn inject_note(
state: &Mutex<PairState>,
file: &str,
row: usize,
body: &str,
) -> Option<u64> {
state.lock().unwrap().session.request_file(file);
if !wait_live(state, file) {
return None;
}
inject_note_now(state, file, row, body)
}
pub(crate) fn inject_note_now(
state: &Mutex<PairState>,
file: &str,
row: usize,
body: &str,
) -> Option<u64> {
let mut st = lock_briefly(state)?;
if !st.session.is_live(file) {
return None;
}
let lines = st.doc_lines(file)?;
let offset = note_offset(&lines, row);
st.caret_now(file, position(&lines, offset).0, 0);
let id = st.take_note_id();
st.notes.push(Note {
id,
file: file.to_string(),
offset,
body: body.to_string(),
});
Some(id)
}
pub(crate) fn send_turn(state: &Mutex<PairState>, sink: &TurnSink, line: &str) -> Result<()> {
let (file, task) = parse_task_line(line);
if let Some(file) = &file {
let mut st = state.lock().unwrap();
st.target_file = Some(file.clone());
st.session.request_file(file);
drop(st);
let deadline = Instant::now() + LIVE_TIMEOUT;
while Instant::now() < deadline {
let st = state.lock().unwrap();
if st.session.is_live(file) || !st.session.is_bootstrapping(file) {
break;
}
drop(st);
std::thread::sleep(Duration::from_millis(20));
}
}
let (file, buffer) = {
let st = state.lock().unwrap();
let file = st.target_file.clone();
let buffer = file
.as_deref()
.and_then(|f| st.session.doc_text(f))
.map(str::to_string);
(file, buffer)
};
if file.is_some() && buffer.is_none() {
diag(
state,
crate::output::OutputLevel::Warn,
"[pair] target file is not live (no owner answered); \
sending the task with the file name only",
);
}
let body = compose_turn_text(&task, file.as_deref(), buffer.as_deref());
write_user_turn(state, sink, body)
}
pub(crate) fn write_user_turn(
state: &Mutex<PairState>,
sink: &TurnSink,
body: String,
) -> Result<()> {
let (content, note) = {
let mut st = state.lock().unwrap();
let note = st.pending_note.take();
st.turn_active = true;
(with_pending_note(note.clone(), body), note)
};
let sent = sink.send_user(&content);
if sent.is_err() {
let mut st = state.lock().unwrap();
st.turn_active = false;
if st.pending_note.is_none() {
st.pending_note = note;
}
}
sent
}
fn with_pending_note(note: Option<String>, body: String) -> String {
match note {
Some(n) => format!("{n}\n\n{body}"),
None => body,
}
}
#[cfg(test)]
pub(crate) const FAKE_CLAUDE: &str = r#"
import json, sys
log = open(sys.argv[1], "a")
mode = sys.argv[2]
# The real claude CLI is chatty on stderr while its MCP servers start; the
# tee must route this to OUTPUT, never into the model's comment boxes.
print("mcp server starting", file=sys.stderr)
sys.stderr.flush()
def emit(obj):
sys.stdout.write(json.dumps(obj) + "\n")
sys.stdout.flush()
def delta(text):
emit({"type": "stream_event", "event": {
"type": "content_block_delta",
"delta": {"type": "text_delta", "text": text}}})
emit({"type": "system", "subtype": "init",
"capabilities": ["interrupt_receipt_v1"]})
line = sys.stdin.readline()
if not line:
sys.exit(0)
log.write(line)
log.flush()
if mode == "hang":
# Read one turn, then never respond: turn_active stays true so the host
# reports busy and rejects a second turn.
import time
time.sleep(60)
sys.exit(0)
if mode == "notes":
delta("Reviewing.\n")
delta("<<<NOTE demo.txt:1>>>\n")
delta("second line could be tighter\n")
delta("<<<END>>>\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "ok"})
# Stay seated like the real CLI: wait for the next turn (or EOF).
sys.stdin.readline()
sys.exit(0)
delta("Let me fix that.\n")
delta("<<<EDIT demo.txt:0:6-0:11>")
delta(">>\n")
delta("streamed")
if mode == "cancel":
line2 = sys.stdin.readline()
log.write(line2)
log.flush()
delta(" MORE-AFTER-CANCEL")
delta("\n<<<END>>>\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "cancelled turn"})
else:
delta(" edit")
delta("\n<<<END>>>\nDone.\n")
emit({"type": "result", "subtype": "success",
"is_error": False, "result": "ok"})
if mode == "linger":
# The real claude CLI does NOT reliably exit on stdin EOF (MCP
# teardown lingers); the pilot must not gamble its own exit on it.
import time
time.sleep(60)
"#;
#[cfg(test)]
mod tests {
use super::*;
use crate::collab::{ResolvedSpan, relay_serve};
use crate::lsp::manager::is_on_path;
#[test]
#[ignore = "needs a live Ollama; run explicitly with CROFT_SPIKE_OLLAMA=1"]
fn ollama_live_smoke_drives_seat_local_end_to_end() {
if std::env::var("CROFT_SPIKE_OLLAMA").is_err() {
eprintln!("skip: set CROFT_SPIKE_OLLAMA=1 to run the live smoke");
return;
}
let url = std::env::var("CROFT_SPIKE_OLLAMA_URL")
.unwrap_or_else(|_| "http://localhost:11434".into());
let model =
std::env::var("CROFT_SPIKE_OLLAMA_MODEL").unwrap_or_else(|_| "qwen3-coder:30b".into());
let harness = OwnerHarness::start("def greet(name):\n return \"hi \" + name\n");
let pilot = seat_local(&harness.socket, "pilot", &url, &model, None).unwrap();
send_turn(
&pilot.state,
&pilot.sink,
"@demo.txt Rewrite greet to return the f-string f\"Hello, {name}!\" \
instead of concatenation, using an EDIT fence.",
)
.unwrap();
let end = pilot
.turn_rx
.recv_timeout(Duration::from_secs(300))
.expect("turn ends");
assert!(!end.is_error, "turn failed: {}", end.text);
harness.wait_until("the f-string edit to converge", |h| {
h.doc().is_some_and(|d| d.contains("f\"Hello, {name}!\""))
});
let doc = harness.doc().unwrap();
assert!(
!doc.contains("+ name"),
"the old concatenation must be gone:\n{doc}"
);
pilot.shutdown();
}
use std::sync::atomic::{AtomicBool, Ordering};
#[test]
fn turn_sink_claude_writes_stream_json_to_stdin() {
let mut child = Command::new("cat")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("spawn cat");
let stdin = child.stdin.take().unwrap();
let stdout = child.stdout.take().unwrap();
let sink = TurnSink::Claude(Arc::new(Mutex::new(Some(stdin))));
sink.send_user("hello\nworld").unwrap();
let TurnSink::Claude(w) = &sink else {
unreachable!()
};
w.lock().unwrap().take(); let mut line = String::new();
std::io::BufReader::new(stdout)
.read_line(&mut line)
.unwrap();
let _ = child.wait();
let v: Value = serde_json::from_str(&line).expect("one stream-json line");
assert_eq!(v["type"], "user");
assert_eq!(v["message"]["role"], "user");
assert_eq!(v["message"]["content"], "hello\nworld");
}
#[test]
fn local_provider_appends_line_range_guidance() {
let prompt = system_prompt(&Provider::Local {
base_url: String::from("http://localhost:11434"),
});
assert!(prompt.starts_with(PAIR_SYSTEM_PROMPT));
assert!(
prompt.contains("<start_row>-<end_row>"),
"suffix must teach the two-integer whole-line header"
);
}
#[test]
fn claude_provider_prompt_is_unchanged() {
assert_eq!(system_prompt(&Provider::Claude), PAIR_SYSTEM_PROMPT);
}
fn settle<T: PartialEq>(expected: &T, mut read: impl FnMut() -> T) -> T {
let deadline = Instant::now() + Duration::from_secs(60);
loop {
let got = read();
if &got == expected || Instant::now() >= deadline {
return got;
}
std::thread::sleep(Duration::from_millis(10));
}
}
struct OwnerHarness {
_dir: tempfile::TempDir,
socket: PathBuf,
owner: Arc<Mutex<CollabSession>>,
events: Arc<Mutex<Vec<CollabEvent>>>,
stop: Arc<AtomicBool>,
pump: Option<std::thread::JoinHandle<()>>,
}
impl OwnerHarness {
fn start(text: &'static str) -> Self {
let dir = tempfile::tempdir().unwrap();
let socket = dir.path().join("p.collab.sock");
{
let s = socket.clone();
std::thread::spawn(move || {
let _ = relay_serve(&s);
});
}
let deadline = Instant::now() + Duration::from_secs(5);
let owner = loop {
if let Some(ch) = CollabChannel::connect(&socket, CollabRole::Owner) {
break CollabSession::new(ch, "owner".into());
}
assert!(Instant::now() < deadline, "relay never came up");
std::thread::sleep(Duration::from_millis(10));
};
let owner = Arc::new(Mutex::new(owner));
let events: Arc<Mutex<Vec<CollabEvent>>> = Arc::new(Mutex::new(Vec::new()));
let stop = Arc::new(AtomicBool::new(false));
let pump = {
let (owner, events, stop) = (owner.clone(), events.clone(), stop.clone());
std::thread::spawn(move || {
while !stop.load(Ordering::Relaxed) {
{
let mut o = owner.lock().unwrap();
let ev = o.poll(|_| Some(text.to_string()));
events.lock().unwrap().extend(ev);
}
std::thread::sleep(Duration::from_millis(5));
}
})
};
Self {
_dir: dir,
socket,
owner,
events,
stop,
pump: Some(pump),
}
}
fn doc(&self) -> Option<String> {
self.owner
.lock()
.unwrap()
.doc_text("demo.txt")
.map(str::to_string)
}
fn wait_until(&self, what: &str, mut cond: impl FnMut(&Self) -> bool) {
let deadline = Instant::now() + Duration::from_secs(60);
while !cond(self) {
assert!(Instant::now() < deadline, "timed out waiting for {what}");
std::thread::sleep(Duration::from_millis(10));
}
}
fn stream_states(&self) -> Vec<(String, bool)> {
self.events
.lock()
.unwrap()
.iter()
.filter_map(|e| match e {
CollabEvent::StreamState { name, active, .. } => Some((name.clone(), *active)),
_ => None,
})
.collect()
}
fn remote_edit_count(&self) -> usize {
self.events
.lock()
.unwrap()
.iter()
.filter(|e| matches!(e, CollabEvent::RemoteEdit { .. }))
.count()
}
fn carets(&self) -> Vec<(String, usize, usize)> {
self.events
.lock()
.unwrap()
.iter()
.filter_map(|e| match e {
CollabEvent::Caret { name, row, col, .. } => Some((name.clone(), *row, *col)),
_ => None,
})
.collect()
}
}
impl Drop for OwnerHarness {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
if let Some(p) = self.pump.take() {
let _ = p.join();
}
}
}
fn serve_sse_once(deltas: Vec<&'static str>) -> (String, std::thread::JoinHandle<()>) {
let mut body = String::new();
for d in deltas {
let ev = json!({
"type": "content_block_delta",
"index": 0,
"delta": { "type": "text_delta", "text": d },
});
body.push_str(&format!("event: content_block_delta\ndata: {ev}\n\n"));
}
body.push_str("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n");
serve_http_once(format!(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
connection: close\r\n\r\n{body}"
))
}
fn serve_http_once(resp: String) -> (String, std::thread::JoinHandle<()>) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base_url = format!("http://{}", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
let (mut sock, _) = listener.accept().unwrap();
let mut req = Vec::new();
let mut buf = [0u8; 4096];
loop {
let n = sock.read(&mut buf).unwrap_or(0);
if n == 0 {
break;
}
req.extend_from_slice(&buf[..n]);
let text = String::from_utf8_lossy(&req).to_ascii_lowercase();
if let Some(head_end) = text.find("\r\n\r\n") {
let content_length = text[..head_end]
.lines()
.find_map(|l| l.strip_prefix("content-length:"))
.and_then(|v| v.trim().parse::<usize>().ok())
.unwrap_or(0);
if req.len() >= head_end + 4 + content_length {
break;
}
}
}
let _ = sock.write_all(resp.as_bytes());
});
(base_url, server)
}
fn pumped_state(
harness: &OwnerHarness,
) -> (
Arc<Mutex<PairState>>,
Arc<AtomicBool>,
std::thread::JoinHandle<()>,
) {
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Arc::new(Mutex::new(PairState::new(session, None)));
let stop = Arc::new(AtomicBool::new(false));
let pump = {
let (state, stop) = (state.clone(), stop.clone());
let sink = TurnSink::Claude(Arc::new(Mutex::new(None)));
std::thread::spawn(move || {
let req_id = AtomicU64::new(0);
while !stop.load(Ordering::Relaxed) {
pump_session(&state, &sink, &req_id);
std::thread::sleep(Duration::from_millis(5));
}
})
};
(state, stop, pump)
}
#[test]
fn ui_reads_answer_instantly_while_the_pilot_mutex_is_held() {
let harness = OwnerHarness::start("hello world");
let host = crate::pair_host::PairHost::spawn(PairConfig {
socket: harness.socket.clone(),
workspace: harness._dir.path().to_path_buf(),
name: String::from("nav"),
model: Some(String::from("m")),
task: None,
provider: Provider::Local {
base_url: String::from("http://127.0.0.1:9"),
},
})
.unwrap();
let state = host.state_for_tests().expect("pilot state");
let (locked_tx, locked_rx) = std::sync::mpsc::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
let holder = std::thread::spawn(move || {
let _guard = state.lock().unwrap();
locked_tx.send(()).unwrap();
let _ = release_rx.recv();
});
locked_rx.recv().unwrap();
let (done_tx, done_rx) = std::sync::mpsc::channel();
let reader = std::thread::spawn(move || {
let busy = host.is_busy();
let _ = host.caret_sites();
let _ = host.notes_snapshot("demo.txt");
let seen = host.last_seen("demo.txt");
let rejected = host.send_yield_turn("demo.txt", "x").is_err();
done_tx.send((busy, seen, rejected)).unwrap();
drop(host);
});
let (busy, seen, rejected) = done_rx
.recv_timeout(Duration::from_secs(2))
.expect("UI reads must answer instantly while the pilot mutex is held");
assert!(busy, "contended = the pilot is mid-work = busy");
assert_eq!(seen, None, "no baseline is better than a blocked frame");
assert!(rejected, "a send under contention reports mid-turn");
release_tx.send(()).unwrap();
holder.join().unwrap();
reader.join().unwrap();
}
#[test]
fn the_seat_stays_busy_until_turn_done_is_consumed() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("stream");
let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
host.send_yield_turn("demo.txt", "hello world").unwrap();
let state = host.state_for_tests().expect("pilot state");
let deadline = Instant::now() + Duration::from_secs(60);
while state.lock().unwrap().turn_active() {
assert!(Instant::now() < deadline, "the fake turn never finished");
std::thread::sleep(Duration::from_millis(5));
}
assert!(
host.is_busy(),
"the turn's end has not been consumed yet: the seat is still busy"
);
let refused = host
.send_yield_turn("demo.txt", "hello world")
.expect_err("a send in the finish window must be refused");
assert!(
refused.to_string().contains("mid-turn"),
"the refusal names the reason: {refused}"
);
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if host
.poll()
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
{
break;
}
assert!(Instant::now() < deadline, "TurnDone never arrived");
std::thread::sleep(Duration::from_millis(5));
}
assert!(
!settle(&false, || host.is_busy()),
"consumed: the seat is idle again"
);
drop(host);
}
#[test]
fn a_failed_yield_leaves_no_look_behind() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let mut cmd = Command::new("python3");
cmd.arg("-c")
.arg("import os,time; os.close(0); os.close(1); time.sleep(60)");
let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
loop {
if host
.poll()
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::Died(_)))
{
break;
}
assert!(Instant::now() < deadline, "the child never died");
std::thread::sleep(Duration::from_millis(10));
}
let err = host.send_yield_turn("demo.txt", "hello world").unwrap_err();
assert!(
format!("{err:#}").contains("claude"),
"the send must fail at the claude sink, not before staging: {err:#}"
);
assert_eq!(
host.last_seen("demo.txt"),
None,
"a failed yield keeps no baseline"
);
assert!(
!settle(&false, || host.is_busy()),
"a failed yield keeps no busy latch"
);
drop(host);
}
#[test]
fn a_failed_send_unlatches_the_turn_and_keeps_the_cancel_note() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
state.lock().unwrap().pending_note = Some(String::from("cancelled: f.rs"));
let dead = TurnSink::Claude(Arc::new(Mutex::new(None)));
let err = write_user_turn(&state, &dead, String::from("hi"))
.expect_err("a dead stdin must fail the send");
assert!(err.to_string().contains("stdin"), "{err}");
{
let st = state.lock().unwrap();
assert!(
!st.turn_active,
"a turn that never left must not stay active"
);
assert_eq!(
st.pending_note.as_deref(),
Some("cancelled: f.rs"),
"the cancel note is still owed to the model"
);
}
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn join_teardowns_reaps_a_dropped_hosts_child() {
if !crate::lsp::manager::is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let marker = format!("croft-teardown-{}", std::process::id());
let mut cmd = std::process::Command::new("python3");
cmd.arg("-c")
.arg("import time; time.sleep(120)")
.arg(&marker);
let host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
let alive = |m: &str| {
std::process::Command::new("pgrep")
.args(["-f", m])
.output()
.map(|o| !o.stdout.is_empty())
.unwrap_or(false)
};
assert!(alive(&marker), "the sleeper child must be running");
drop(host);
crate::pair_host::join_teardowns();
assert!(
!alive(&marker),
"after join_teardowns the dropped host's child must be reaped"
);
}
#[test]
fn an_unterminated_fence_header_never_leaks_into_commentary() {
let mut m = FenceMachine::new();
let mut events = m.push("thinking\n<<<NOTE src/f.rs:12>>>");
events.extend(m.finish());
let commentary = commentary_of(&events);
assert!(commentary.contains("thinking"));
assert!(
!commentary.contains("<<<NOTE"),
"protocol residue leaked: {commentary:?}"
);
let mut events = m.push("<<<EDIT src/f.rs:0:0-0:5>>>");
events.extend(m.finish());
assert!(
!commentary_of(&events).contains("<<<EDIT"),
"an EDIT header tail is the same class"
);
let mut events = m.push("<<<EDIT garbage");
events.extend(m.finish());
assert!(
commentary_of(&events).contains("<<<EDIT garbage"),
"a malformed tail stays commentary"
);
}
#[test]
fn park_caret_waits_for_bootstrap_then_broadcasts() {
let harness = OwnerHarness::start("l0\nl1\nl2");
let (state, stop, pump) = pumped_state(&harness);
{
let mut st = state.lock().unwrap();
st.begin_turn("demo.txt", "l0\nl1\nl2", false); st.park_caret("demo.txt", 1); }
harness.wait_until("the parked caret to broadcast", |h| {
h.carets()
.iter()
.any(|(n, r, c)| n == "pilot" && *r == 1 && *c == 0)
});
state.lock().unwrap().park_caret("demo.txt", 99);
harness.wait_until("the clamped caret", |h| {
h.carets().iter().any(|(n, r, _)| n == "pilot" && *r == 2)
});
stop.store(true, Ordering::Relaxed);
let _ = pump.join();
}
#[test]
fn a_landed_note_parks_the_pilot_caret_at_its_row() {
let harness = OwnerHarness::start("l0\nl1\nl2");
let (state, stop, pump) = pumped_state(&harness);
apply_fence_event(
&state,
FenceEvent::NoteStart {
file: "demo.txt".into(),
row: 2,
},
);
apply_fence_event(&state, FenceEvent::NoteBody("look here".into()));
apply_fence_event(&state, FenceEvent::NoteEnd);
harness.wait_until("the note's caret", |h| {
h.carets().iter().any(|(n, r, _)| n == "pilot" && *r == 2)
});
stop.store(true, Ordering::Relaxed);
let _ = pump.join();
}
#[test]
fn ask_and_reply_turns_park_the_navigator_caret() {
let harness = OwnerHarness::start("l0\nl1\nl2");
let mut cmd = Command::new("cat");
cmd.stdin(Stdio::piped());
let asker =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "asker", None, cmd).unwrap();
asker
.send_ask_turn("demo.txt", (1, 1), "", "look at this", "l0\nl1\nl2")
.unwrap();
harness.wait_until("the ask turn's caret", |h| {
h.carets().iter().any(|(n, r, _)| n == "asker" && *r == 1)
});
assert!(
!asker.caret_sites().is_empty(),
"the seat exposes its per-file site ids (its wire identity)"
);
let mut cmd = Command::new("cat");
cmd.stdin(Stdio::piped());
let replier =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "replier", None, cmd).unwrap();
replier
.send_reply_turn("demo.txt", 2, "seed note", "why here?", "l0\nl1\nl2")
.unwrap();
harness.wait_until("the reply turn's caret", |h| {
h.carets().iter().any(|(n, r, _)| n == "replier" && *r == 2)
});
}
#[test]
fn a_stale_rollback_never_stomps_a_newer_turn() {
let harness = OwnerHarness::start("hello");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
let mut st = state.lock().unwrap();
let staged_a = st.stage_turn("demo.txt", "one", false);
let _staged_b = st.stage_turn("demo.txt", "two", true);
st.unstage_turn(staged_a); assert_eq!(
st.last_seen_of("demo.txt").as_deref(),
Some("two"),
"a stale rollback must not replace the live turn's baseline"
);
assert!(
st.comment_only,
"a stale rollback must not unlatch the live turn's comment-only"
);
}
#[test]
fn a_parked_rollback_blocks_sends_until_it_applies() {
let harness = OwnerHarness::start("hello");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
let staged = state.lock().unwrap().stage_turn("demo.txt", "one", false);
let parked = Mutex::new(Some(staged));
assert!(!crate::pair_host::drain_parked(&parked, || None));
assert!(parked.lock().unwrap().is_some(), "the undo is not lost");
assert!(crate::pair_host::drain_parked(&parked, || Some(
state.lock().unwrap()
)));
assert!(parked.lock().unwrap().is_none());
assert_eq!(
state.lock().unwrap().last_seen_of("demo.txt"),
None,
"the parked undo restored the pre-turn baseline"
);
}
#[test]
fn nested_rollbacks_unwind_to_the_original_state() {
let harness = OwnerHarness::start("hello");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
let mut st = state.lock().unwrap();
let staged_a = st.stage_turn("demo.txt", "one", false);
let staged_b = st.stage_turn("demo.txt", "two", true);
st.unstage_turn(staged_b); st.unstage_turn(staged_a); assert_eq!(
st.last_seen_of("demo.txt"),
None,
"both failed turns unwound: no baseline remains"
);
assert!(!st.comment_only, "the original comment-only state is back");
assert_eq!(st.target_file, None, "the original target is back");
}
#[test]
fn an_unsolicited_result_ends_no_turn() {
let harness = OwnerHarness::start("hello");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
let mut fence = FenceMachine::new();
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let result = json!({ "type": "result", "is_error": false, "result": "done" });
state.lock().unwrap().turn_active = true;
handle_claude_event(&state, &mut fence, &result, &turn_tx);
assert!(
turn_rx.try_recv().is_ok(),
"the genuine result ends the turn"
);
state.lock().unwrap().stage_turn("demo.txt", "one", true);
handle_claude_event(&state, &mut fence, &result, &turn_tx);
assert!(
turn_rx.try_recv().is_err(),
"a duplicate result must not queue a phantom TurnEnd"
);
assert!(
state.lock().unwrap().comment_only,
"a duplicate result must not clear the newer turn's comment-only staging"
);
}
#[test]
fn an_open_region_supersedes_a_pending_caret_park() {
let harness = OwnerHarness::start("l0\nl1\nl2");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
{
let mut st = state.lock().unwrap();
st.begin_turn("demo.txt", "l0\nl1\nl2", false);
st.park_caret("demo.txt", 1);
assert!(st.pending_caret.is_some(), "not live yet: the park waits");
}
let deadline = Instant::now() + Duration::from_secs(5);
loop {
let mut st = state.lock().unwrap();
let _ = st.session.poll(|_| None);
if st.session.is_live("demo.txt") {
break;
}
drop(st);
assert!(Instant::now() < deadline, "demo.txt never went live");
std::thread::sleep(Duration::from_millis(5));
}
let mut st = state.lock().unwrap();
open_region(&mut st, "demo.txt", (0, 0), (0, 0));
assert!(
st.pending_caret.is_none(),
"the edit's caret supersedes the pending park"
);
}
#[test]
fn an_inverted_fence_range_drops_the_edit_via_output_not_stderr() {
let harness = OwnerHarness::start("l0\nl1\nl2\nl3\nl4\nl5");
let session = connect_session(&harness.socket, "pilot").unwrap();
let state = Mutex::new(PairState::new(session, None));
state
.lock()
.unwrap()
.begin_turn("demo.txt", "l0\nl1\nl2\nl3\nl4\nl5", false);
let deadline = Instant::now() + Duration::from_secs(5);
loop {
let mut st = state.lock().unwrap();
let _ = st.session.poll(|_| None);
if st.session.is_live("demo.txt") {
break;
}
drop(st);
assert!(Instant::now() < deadline, "demo.txt never went live");
std::thread::sleep(Duration::from_millis(5));
}
let baseline = crate::output::snapshot("Navigator")
.unwrap_or_default()
.len();
let mut st = state.lock().unwrap();
open_region(&mut st, "demo.txt", (4, 0), (2, usize::MAX));
assert!(st.discarding, "the inverted edit's body must be discarded");
assert_eq!(
st.session.doc_text("demo.txt"),
Some("l0\nl1\nl2\nl3\nl4\nl5"),
"nothing may be applied"
);
let lines = crate::output::snapshot("Navigator").unwrap_or_default();
assert!(
lines
.iter()
.skip(baseline)
.any(|l| l.text.contains("inverted")),
"the drop is reported on the OUTPUT channel"
);
}
#[test]
fn stream_turn_applies_a_fenced_edit() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let (base_url, server) = serve_sse_once(vec![
"Let me fix that.\n",
"<<<EDIT demo.txt:0:6-0:11>",
">>\n",
"streamed",
" edit",
"\n<<<END>>>\nDone.\n",
]);
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "fix demo.txt" })];
local::stream_turn(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
harness.wait_until("the streamed edit to converge", |h| {
h.doc().as_deref() == Some("hello streamed edit")
});
assert!(
harness.remote_edit_count() >= 3,
"the edit must arrive as multiple streamed ops, got {}",
harness.remote_edit_count()
);
let end = turn_rx.try_recv().expect("turn ended");
assert!(!end.is_error && !end.cancelled);
assert_eq!(messages.len(), 2, "assistant reply appended");
assert_eq!(messages[1]["role"], "assistant");
assert!(
messages[1]["content"]
.as_str()
.unwrap()
.contains("<<<EDIT demo.txt:0:6-0:11>>>"),
"conversation history carries the fence verbatim"
);
assert!(!state.lock().unwrap().turn_active());
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn stream_turn_names_a_dead_endpoint() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let dead = {
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
format!("http://{}", l.local_addr().unwrap())
};
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&dead,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error);
assert!(
end.text.contains(&dead),
"the failure names the endpoint: {}",
end.text
);
assert!(!state.lock().unwrap().turn_active());
assert!(
messages.is_empty(),
"the unanswered user message is popped so the next ask starts clean"
);
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn a_mid_stream_drop_reports_failure_and_balances_the_conversation() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let ev = json!({
"type": "content_block_delta",
"index": 0,
"delta": { "type": "text_delta", "text": "half a rep" },
});
let (base_url, server) = serve_http_once(format!(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
connection: close\r\n\r\nevent: content_block_delta\ndata: {ev}\n\n"
));
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
let end = turn_rx.try_recv().expect("turn ended");
assert!(
end.is_error,
"an unclean stream end is a failure: {}",
end.text
);
assert!(
messages.is_empty(),
"the conversation stays balanced for the next ask"
);
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn an_http_error_body_reaches_the_surfaced_failure() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let (base_url, server) = serve_http_once(String::from(
"HTTP/1.1 404 Not Found\r\ncontent-type: application/json\r\n\
connection: close\r\n\r\n{\"error\":\"model 'qwn3' not found, try pulling it\"}",
));
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&base_url,
"qwn3",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error);
assert!(
end.text.contains("not found, try pulling it"),
"the body's own words must survive: {}",
end.text
);
assert!(messages.is_empty());
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn a_token_limit_truncation_is_a_failed_turn() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let delta = json!({
"type": "content_block_delta",
"index": 0,
"delta": { "type": "text_delta", "text": "prose" },
});
let md = json!({
"type": "message_delta",
"delta": { "stop_reason": "max_tokens" },
});
let (base_url, server) = serve_http_once(format!(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
connection: close\r\n\r\n\
event: content_block_delta\ndata: {delta}\n\n\
event: message_delta\ndata: {md}\n\n\
event: message_stop\ndata: {{\"type\":\"message_stop\"}}\n\n"
));
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error, "truncation must not read as success");
assert!(end.text.contains("truncated"), "{}", end.text);
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn the_credential_never_rides_cleartext_to_a_remote_host() {
use local::auth_for;
assert_eq!(
auth_for("http://box:8080", Some("secret")),
(String::from("croft"), None),
);
assert_eq!(
auth_for("http://localhost.evil.com:80", Some("secret")),
(String::from("croft"), None),
);
let bearer = Some(String::from("Bearer t"));
assert_eq!(
auth_for("http://localhost:11434", Some("t")),
(String::from("t"), bearer.clone()),
);
assert_eq!(
auth_for("http://127.0.0.1:11434", Some("t")),
(String::from("t"), bearer.clone()),
);
assert_eq!(
auth_for("http://[::1]:11434", Some("t")),
(String::from("t"), bearer.clone()),
);
assert_eq!(
auth_for("https://gw.example.com", Some("t")),
(String::from("t"), bearer),
);
assert_eq!(
auth_for("http://localhost:11434", None),
(String::from("croft"), None)
);
assert_eq!(
auth_for("http://localhost:pass@evil.example", Some("secret")),
(String::from("croft"), None),
);
assert_eq!(
auth_for("http://127.0.0.2:11434", Some("t")),
(String::from("t"), Some(String::from("Bearer t"))),
"any 127.x address is loopback"
);
}
#[test]
fn a_trickling_stream_is_cut_at_the_deadline() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base_url = format!("http://{}", listener.local_addr().unwrap());
let stop_trickle = Arc::new(AtomicBool::new(false));
let server = {
let stop_trickle = Arc::clone(&stop_trickle);
std::thread::spawn(move || {
let (mut sock, _) = listener.accept().unwrap();
let mut req = [0u8; 4096];
let _ = sock.read(&mut req);
let _ =
sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
while !stop_trickle.load(Ordering::Relaxed) {
if sock.write_all(b".").is_err() {
break;
}
std::thread::sleep(Duration::from_millis(20));
}
})
};
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
let started = Instant::now();
local::stream_turn_until(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
Instant::now() + Duration::from_millis(300),
);
let elapsed = started.elapsed();
stop_trickle.store(true, Ordering::Relaxed);
let _ = server.join();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
assert!(
elapsed < Duration::from_secs(3),
"the ceiling must interrupt the trickle promptly, took {elapsed:?}"
);
assert!(messages.is_empty(), "the conversation stays balanced");
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn a_silent_stream_body_is_cut_at_the_deadline() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base_url = format!("http://{}", listener.local_addr().unwrap());
std::thread::spawn(move || {
let (mut sock, _) = listener.accept().unwrap();
let mut req = [0u8; 4096];
let _ = sock.read(&mut req);
let _ = sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
std::thread::sleep(Duration::from_secs(4));
});
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
let started = Instant::now();
local::stream_turn_until(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
Instant::now() + Duration::from_millis(300),
);
let elapsed = started.elapsed();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
assert!(
elapsed < Duration::from_secs(3),
"the ceiling must interrupt a silent body promptly, took {elapsed:?}"
);
assert!(messages.is_empty(), "the conversation stays balanced");
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn a_deadline_cut_releases_the_connection() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base_url = format!("http://{}", listener.local_addr().unwrap());
let (eof_tx, eof_rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let (mut sock, _) = listener.accept().unwrap();
let mut req = [0u8; 4096];
let _ = sock.read(&mut req);
let _ = sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\r\n");
let mut probe = [0u8; 64];
loop {
match sock.read(&mut probe) {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
let _ = eof_tx.send(());
});
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn_until(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
Instant::now() + Duration::from_millis(300),
);
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error, "a deadline cut is a failure: {}", end.text);
eof_rx.recv_timeout(Duration::from_secs(2)).expect(
"the deadline cut must close the connection, not park it until the socket timeout",
);
assert!(messages.is_empty(), "the conversation stays balanced");
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn malformed_sse_data_fails_the_turn() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let (base_url, server) = serve_http_once(String::from(
"HTTP/1.1 200 OK\r\ncontent-type: text/event-stream\r\n\
connection: close\r\n\r\ndata: {not json\n\n\
event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
));
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error);
assert!(end.text.contains("malformed"), "{}", end.text);
assert!(messages.is_empty());
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn a_redirect_is_refused_not_followed() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
let (base_url, server) = serve_http_once(String::from(
"HTTP/1.1 302 Found\r\nlocation: http://127.0.0.1:1/v1/messages\r\n\
connection: close\r\n\r\n",
));
let (turn_tx, turn_rx) = std::sync::mpsc::channel();
let mut messages = vec![json!({ "role": "user", "content": "hi" })];
state.lock().unwrap().turn_active = true;
local::stream_turn(
&base_url,
"test-model",
PAIR_SYSTEM_PROMPT,
&mut messages,
&state,
&turn_tx,
);
server.join().unwrap();
let end = turn_rx.try_recv().expect("turn ended");
assert!(end.is_error);
assert!(
end.text.contains("302"),
"the redirect is refused with its status, not followed: {}",
end.text
);
assert!(messages.is_empty());
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn seat_local_drives_a_turn_end_to_end() {
let harness = OwnerHarness::start("hello world");
let (base_url, server) = serve_sse_once(vec![
"Let me fix that.\n",
"<<<EDIT demo.txt:0:6-0:11>",
">>\n",
"streamed",
" edit",
"\n<<<END>>>\nDone.\n",
]);
let pilot = seat_local(&harness.socket, "pilot", &base_url, "test-model", None).unwrap();
assert!(!pilot.reader_finished(), "a local seat never loses a child");
send_turn(&pilot.state, &pilot.sink, "fix demo.txt").unwrap();
let end = pilot
.turn_rx
.recv_timeout(Duration::from_secs(60))
.expect("turn ends");
assert!(!end.is_error && !end.cancelled, "{}", end.text);
server.join().unwrap();
harness.wait_until("the streamed edit to converge", |h| {
h.doc().as_deref() == Some("hello streamed edit")
});
harness.wait_until("stream-state inactive", |h| {
h.stream_states().contains(&("pilot".to_string(), false))
});
let start = Instant::now();
pilot.shutdown();
assert!(
start.elapsed() < Duration::from_secs(2),
"local shutdown must not block on a grace-kill"
);
}
#[test]
fn pair_host_spawns_local_backend_without_a_child() {
let harness = OwnerHarness::start("hello world");
let no_model = PairConfig {
socket: harness.socket.clone(),
workspace: harness._dir.path().to_path_buf(),
name: String::from("claude"),
model: None,
task: None,
provider: Provider::Local {
base_url: String::from("http://127.0.0.1:9"),
},
};
let err = match crate::pair_host::PairHost::spawn(no_model) {
Err(e) => e,
Ok(_) => panic!("a local provider without a model must refuse to seat"),
};
assert!(
err.to_string().contains("--model"),
"the refusal must hint at --model: {err}"
);
let (base_url, server) = serve_sse_once(vec![
"<<<EDIT demo.txt:0-0>",
">>\nhello streamed edit\n",
"<<<END>>>\n",
]);
let mut host = crate::pair_host::PairHost::spawn(PairConfig {
socket: harness.socket.clone(),
workspace: harness._dir.path().to_path_buf(),
name: String::from("claude"),
model: Some(String::from("test-model")),
task: None,
provider: Provider::Local { base_url },
})
.unwrap();
assert!(!host.is_busy());
host.send_ask_turn("demo.txt", (0, 0), "", "rewrite line 0", "hello world")
.unwrap();
server.join().unwrap();
harness.wait_until("the whole-line edit to converge", |h| {
h.doc().as_deref() == Some("hello streamed edit")
});
harness.wait_until("the turn to end", |h| {
let _ = h; host.poll().iter().any(|e| {
matches!(
e,
crate::pair_host::PairEvent::TurnDone {
cancelled: false,
failed: None
}
)
})
});
}
#[test]
fn local_navigator_badge_names_the_model() {
let harness = OwnerHarness::start("hello world");
let host = crate::pair_host::PairHost::spawn(PairConfig {
socket: harness.socket.clone(),
workspace: harness._dir.path().to_path_buf(),
name: String::from("claude"),
model: Some(String::from("test-model")),
task: None,
provider: Provider::Local {
base_url: String::from("http://127.0.0.1:9"),
},
})
.unwrap();
assert_eq!(host.title(), "claude (test-model)");
let mut cmd = Command::new("cat");
cmd.stdin(Stdio::piped());
let host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
assert_eq!(host.title(), "navigator");
}
fn spawn_pilot(
socket: &Path,
script: &Path,
log: &Path,
mode: &str,
) -> std::thread::JoinHandle<Result<()>> {
let mut cmd = Command::new("python3");
cmd.arg(script).arg(log).arg(mode);
let socket = socket.to_path_buf();
std::thread::spawn(move || {
let pilot = seat_pilot(&socket, "pilot", cmd, None)?;
run_pilot(
pilot,
"pilot",
Some("fix demo.txt".into()),
&mut std::io::Cursor::new(Vec::new()),
)
})
}
#[test]
fn pilot_streams_a_fenced_edit_into_the_owner() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let pilot = spawn_pilot(&harness.socket, &script, &log, "stream");
harness.wait_until("the streamed edit to converge", |h| {
h.doc().as_deref() == Some("hello streamed edit")
});
harness.wait_until("stream-state inactive", |h| {
h.stream_states().contains(&("pilot".to_string(), false))
});
assert!(
harness
.stream_states()
.contains(&("pilot".to_string(), true)),
"owner saw the stream start"
);
assert!(
harness.remote_edit_count() >= 3,
"the edit must arrive as multiple streamed ops, got {}",
harness.remote_edit_count()
);
pilot.join().unwrap().expect("pilot exits cleanly");
}
#[test]
fn pilot_exit_does_not_hang_on_a_lingering_claude() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let pilot = spawn_pilot(&harness.socket, &script, &log, "linger");
harness.wait_until("the streamed edit to converge", |h| {
h.doc().as_deref() == Some("hello streamed edit")
});
let deadline = Instant::now() + Duration::from_secs(30);
while !pilot.is_finished() {
assert!(
Instant::now() < deadline,
"pilot exit hung on the lingering claude child"
);
std::thread::sleep(Duration::from_millis(50));
}
pilot.join().unwrap().expect("pilot exits cleanly");
}
#[test]
fn pilot_cancel_reverts_and_interrupts() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let pilot = spawn_pilot(&harness.socket, &script, &log, "cancel");
harness.wait_until("the partial body to converge", |h| {
h.doc().is_some_and(|d| d.contains("streamed"))
});
harness.owner.lock().unwrap().send_stream_cancel();
harness.wait_until("the revert to converge", |h| {
h.doc().as_deref() == Some("hello world")
});
pilot.join().unwrap().expect("pilot exits cleanly");
assert_eq!(
harness.doc().as_deref(),
Some("hello world"),
"post-cancel deltas must never land"
);
harness.wait_until("stream-state inactive", |h| {
h.stream_states().contains(&("pilot".to_string(), false))
});
let log_text = std::fs::read_to_string(&log).unwrap();
assert!(
log_text.contains("control_request") && log_text.contains("interrupt"),
"the interrupt must land on claude's stdin: {log_text:?}"
);
}
fn run_chunks(input: &str, chunk: usize) -> Vec<FenceEvent> {
let mut m = FenceMachine::new();
let mut events = Vec::new();
let chars: Vec<char> = input.chars().collect();
for piece in chars.chunks(chunk) {
let s: String = piece.iter().collect();
events.extend(m.push(&s));
}
events.extend(m.finish());
events
}
fn body_of(events: &[FenceEvent]) -> String {
events
.iter()
.filter_map(|e| match e {
FenceEvent::EditBody(s) => Some(s.as_str()),
_ => None,
})
.collect()
}
fn commentary_of(events: &[FenceEvent]) -> String {
events
.iter()
.filter_map(|e| match e {
FenceEvent::Commentary(s) => Some(s.as_str()),
_ => None,
})
.collect()
}
fn note_body_of(events: &[FenceEvent]) -> String {
events
.iter()
.filter_map(|e| match e {
FenceEvent::NoteBody(s) => Some(s.as_str()),
_ => None,
})
.collect()
}
#[test]
fn note_fence_parses_across_delta_splits() {
let input = "Look at this.\n\
<<<NOTE src/f.rs:12>>>\n\
the caller in cli.rs:88\nstill expects Config\n\
<<<END>>>\n\
trailing\n";
for chunk in [1, 3, 7, input.len()] {
let events = run_chunks(input, chunk);
let start = events
.iter()
.find_map(|e| match e {
FenceEvent::NoteStart { file, row } => Some((file.clone(), *row)),
_ => None,
})
.unwrap_or_else(|| panic!("no NoteStart at chunk {chunk}"));
assert_eq!(start, ("src/f.rs".to_string(), 12), "chunk {chunk}");
assert_eq!(
note_body_of(&events),
"the caller in cli.rs:88\nstill expects Config",
"chunk {chunk}"
);
assert_eq!(
events
.iter()
.filter(|e| matches!(e, FenceEvent::NoteEnd))
.count(),
1,
"chunk {chunk}"
);
let commentary = commentary_of(&events);
assert!(commentary.contains("Look at this."), "chunk {chunk}");
assert!(commentary.contains("trailing"), "chunk {chunk}");
assert!(!commentary.contains("<<<NOTE"), "chunk {chunk}");
}
}
#[test]
fn malformed_note_headers_become_commentary() {
for bad in [
"<<<NOTE src/f.rs>>>\nbody\n<<<END>>>\n",
"<<<NOTE src/f.rs:x>>>\nbody\n<<<END>>>\n",
"<<<NOTE :3>>>\nbody\n<<<END>>>\n",
"<<<NOTE>>>\nbody\n<<<END>>>\n",
] {
let events = run_chunks(bad, 5);
assert!(
!events
.iter()
.any(|e| matches!(e, FenceEvent::NoteStart { .. })),
"{bad:?} must not start a note"
);
}
}
#[test]
fn unterminated_note_aborts_at_finish() {
let mut m = FenceMachine::new();
let mut events = m.push("<<<NOTE f.rs:3>>>\nhalf a thought");
events.extend(m.finish());
assert!(events.iter().any(|e| matches!(e, FenceEvent::NoteAbort)));
assert!(!events.iter().any(|e| matches!(e, FenceEvent::NoteEnd)));
}
#[test]
fn edit_and_note_fences_coexist_in_one_turn() {
let input = "<<<EDIT a.rs:0:0-0:0>>>\nnew\n<<<END>>>\n\
<<<NOTE b.rs:4>>>\nremark\n<<<END>>>\n";
let events = run_chunks(input, 2);
assert_eq!(body_of(&events), "new");
assert_eq!(note_body_of(&events), "remark");
assert_eq!(
events
.iter()
.filter(|e| matches!(e, FenceEvent::EditEnd))
.count(),
1
);
assert_eq!(
events
.iter()
.filter(|e| matches!(e, FenceEvent::NoteEnd))
.count(),
1
);
}
#[test]
fn fence_parses_identically_across_arbitrary_delta_splits() {
let input = "I will fix the loop.\n\
<<<EDIT src/f.rs:3:0-5:10>>>\n\
for x in xs {\n go(x);\n}\n\
<<<END>>>\n\
Done.\n";
for chunk in [1, 2, 3, 7, input.len()] {
let events = run_chunks(input, chunk);
let start = events
.iter()
.find_map(|e| match e {
FenceEvent::EditStart { file, start, end } => {
Some((file.clone(), *start, *end))
}
_ => None,
})
.unwrap_or_else(|| panic!("no EditStart at chunk {chunk}"));
assert_eq!(
start,
("src/f.rs".to_string(), (3, 0), (5, 10)),
"chunk {chunk}"
);
assert_eq!(
body_of(&events),
"for x in xs {\n go(x);\n}",
"chunk {chunk}"
);
assert_eq!(
events
.iter()
.filter(|e| matches!(e, FenceEvent::EditEnd))
.count(),
1,
"chunk {chunk}"
);
let commentary = commentary_of(&events);
assert!(
commentary.contains("I will fix the loop."),
"chunk {chunk}: {commentary:?}"
);
assert!(commentary.contains("Done."), "chunk {chunk}");
assert!(
!commentary.contains("<<<EDIT"),
"header must not leak into commentary at chunk {chunk}"
);
}
let events = run_chunks(input, 1);
let bodies = events
.iter()
.filter(|e| matches!(e, FenceEvent::EditBody(_)))
.count();
assert!(bodies > 1, "body must stream, got {bodies} event(s)");
}
#[test]
fn parse_header_accepts_whole_line_range() {
let event = parse_header("<<<EDIT demo.txt:3-5>>>").expect("whole-line header parses");
let FenceEvent::EditStart { file, start, end } = event else {
panic!("not an EditStart: {event:?}");
};
assert_eq!(file, "demo.txt");
assert_eq!(start, (3, 0));
assert_eq!(end, (5, usize::MAX));
}
#[test]
fn whole_line_edit_replaces_entire_rows() {
let buffer = "def greet(name):\n return \"hi \" + name\n";
let input = "<<<EDIT greet.py:1-1>>>\n return f\"Hello, {name}!\"\n<<<END>>>\n";
let events = run_chunks(input, 3);
let (start, end) = events
.iter()
.find_map(|e| match e {
FenceEvent::EditStart { start, end, .. } => Some((*start, *end)),
_ => None,
})
.expect("whole-line fence opens an edit");
let lines: Vec<String> = buffer.split('\n').map(String::from).collect();
let (s, e) = range_bytes(&lines, start, end);
let result = format!("{}{}{}", &buffer[..s], body_of(&events), &buffer[e..]);
assert_eq!(result, "def greet(name):\n return f\"Hello, {name}!\"\n");
}
#[test]
fn truncated_four_int_header_stays_commentary() {
assert!(parse_header("<<<EDIT src/f.rs:3:0-5>>>").is_none());
}
#[test]
fn malformed_headers_become_commentary() {
for bad in [
"<<<EDIT src/f.rs:3:0-5>>>\nbody\n<<<END>>>\n",
"<<<EDIT src/f.rs>>>\nbody\n<<<END>>>\n",
"<<<EDIT src/f.rs:a:0-5:10>>>\nbody\n<<<END>>>\n",
"<<<EDIT>>>\nbody\n<<<END>>>\n",
] {
let events = run_chunks(bad, 5);
assert!(
!events
.iter()
.any(|e| matches!(e, FenceEvent::EditStart { .. })),
"{bad:?} must not start an edit"
);
assert!(
commentary_of(&events).contains("body"),
"{bad:?}: the un-fenced body is commentary"
);
}
}
#[test]
fn end_marker_strips_exactly_one_trailing_newline() {
let input = "<<<EDIT f:0:0-0:0>>>\nabc\n\n<<<END>>>\n";
for chunk in [1, 4, input.len()] {
assert_eq!(body_of(&run_chunks(input, chunk)), "abc\n", "chunk {chunk}");
}
}
#[test]
fn end_marker_with_trailing_chars_is_body() {
let input = "<<<EDIT f:0:0-0:0>>>\nkeep <<<END>>>ish\n<<<END>>>\n";
for chunk in [1, 3, input.len()] {
assert_eq!(
body_of(&run_chunks(input, chunk)),
"keep <<<END>>>ish",
"chunk {chunk}"
);
}
}
#[test]
fn multiple_fences_in_one_turn() {
let input = "<<<EDIT a:0:0-0:0>>>\nfirst\n<<<END>>>\n\
between\n\
<<<EDIT b:1:2-3:4>>>\nsecond\n<<<END>>>\n";
let events = run_chunks(input, 2);
let starts: Vec<String> = events
.iter()
.filter_map(|e| match e {
FenceEvent::EditStart { file, .. } => Some(file.clone()),
_ => None,
})
.collect();
assert_eq!(starts, ["a", "b"]);
assert_eq!(body_of(&events), "firstsecond");
assert_eq!(
events
.iter()
.filter(|e| matches!(e, FenceEvent::EditEnd))
.count(),
2
);
assert!(commentary_of(&events).contains("between"));
}
#[test]
fn unterminated_fence_aborts_at_finish() {
let mut m = FenceMachine::new();
let mut events = m.push("<<<EDIT f:0:0-0:0>>>\npartial body");
events.extend(m.finish());
assert!(
events.iter().any(|e| matches!(e, FenceEvent::EditAbort)),
"{events:?}"
);
let events = {
let mut e = m.push("plain text\n");
e.extend(m.finish());
e
};
assert!(commentary_of(&events).contains("plain text"));
}
#[test]
fn end_marker_at_end_of_turn_closes_the_fence() {
let mut m = FenceMachine::new();
let mut events = m.push("<<<EDIT f:0:0-0:0>>>\nbody\n<<<END>>>");
events.extend(m.finish());
assert!(events.iter().any(|e| matches!(e, FenceEvent::EditEnd)));
assert_eq!(body_of(&events), "body");
}
#[test]
fn header_files_with_colons_and_dashes_parse() {
let input = "<<<EDIT a-b:c.rs:10:2-11:0>>>\nx\n<<<END>>>\n";
let events = run_chunks(input, input.len());
match events
.iter()
.find(|e| matches!(e, FenceEvent::EditStart { .. }))
{
Some(FenceEvent::EditStart { file, start, end }) => {
assert_eq!(file, "a-b:c.rs");
assert_eq!((*start, *end), ((10, 2), (11, 0)));
}
other => panic!("expected EditStart, got {other:?}"),
}
}
#[test]
fn shift_offset_handles_before_straddle_and_after() {
let ins = ResolvedSpan {
at: 2,
deleted: 0,
inserted: "abc".into(),
};
assert_eq!(shift_offset(10, &ins), 13);
let del = ResolvedSpan {
at: 6,
deleted: 4,
inserted: String::new(),
};
assert_eq!(shift_offset(10, &del), 6);
let straddle = ResolvedSpan {
at: 8,
deleted: 6,
inserted: "Z".into(),
};
assert_eq!(shift_offset(10, &straddle), 9);
let after = ResolvedSpan {
at: 10,
deleted: 3,
inserted: "wxyz".into(),
};
assert_eq!(shift_offset(10, &after), 10);
let insert_at = ResolvedSpan {
at: 10,
deleted: 0,
inserted: "Q".into(),
};
assert_eq!(shift_offset(10, &insert_at), 11);
}
#[test]
fn shift_offset_sequences_track_a_region() {
let mut start = 20usize;
let mut anchor = 30usize;
let spans = [
ResolvedSpan {
at: 0,
deleted: 0,
inserted: "aaaaa".into(),
},
ResolvedSpan {
at: 40,
deleted: 5,
inserted: String::new(),
},
ResolvedSpan {
at: 23,
deleted: 5,
inserted: String::new(),
},
];
for span in &spans {
start = shift_offset(start, span);
anchor = shift_offset(anchor, span);
}
assert_eq!(start, 23);
assert_eq!(anchor, 30);
assert!(start <= anchor);
}
#[test]
fn fence_range_converts_to_byte_offsets() {
let lines: Vec<String> = ["let s = \"héllo\";", "next"]
.iter()
.map(|s| s.to_string())
.collect();
let (start, end) = range_bytes(&lines, (0, 10), (1, 2));
assert_eq!(start, crate::collab::byte_offset(&lines, 0, 10));
assert_eq!(end, crate::collab::byte_offset(&lines, 1, 2));
assert!(start < end);
}
#[test]
fn pair_host_emits_note_added_and_snapshots_rows() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world\nsecond line");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("notes");
let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
host.send_task("@demo.txt look this over").unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
let mut events: Vec<crate::pair_host::PairEvent> = Vec::new();
while !(events
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
&& events
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::NoteAdded { .. })))
{
assert!(
Instant::now() < deadline,
"no TurnDone + NoteAdded; saw {events:?}"
);
events.extend(host.poll());
std::thread::sleep(Duration::from_millis(10));
}
let note = events
.iter()
.find_map(|e| match e {
crate::pair_host::PairEvent::NoteAdded {
file, row, body, ..
} => Some((file.clone(), *row, body.clone())),
_ => None,
})
.unwrap_or_else(|| panic!("no NoteAdded; saw {events:?}"));
assert_eq!(note.0, "demo.txt");
assert_eq!(note.1, 1, "anchored to the fenced row");
assert!(note.2.contains("second line could be tighter"));
let expected = vec![(1, 1, "second line could be tighter".to_string())];
assert_eq!(
settle(&expected, || host.notes_snapshot("demo.txt")),
expected,
"(id, row, body): the first note of the seat gets id 1"
);
drop(host); }
#[test]
fn a_turn_is_rejected_while_another_is_streaming() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("line zero\nline one");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("hang"); let host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
host.send_ask_turn("demo.txt", (0, 0), "", "do a thing", "line zero\nline one")
.unwrap();
assert!(host.is_busy(), "the ask turn is streaming");
let err = host
.send_yield_turn("demo.txt", "line zero\nline one")
.unwrap_err();
assert!(
err.to_string().contains("mid-turn"),
"expected a mid-turn rejection, got: {err}"
);
assert!(host.is_busy(), "the original ask turn is untouched");
drop(host);
}
#[test]
fn host_adds_appends_and_removes_notes_by_id() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world\nsecond line");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("notes");
let host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
let id_a = host
.add_note("demo.txt", 0, "turn summary")
.expect("demo.txt is served by the owner");
let id_b = host.add_note("demo.txt", 1, "second thought").unwrap();
assert_ne!(id_a, id_b, "every note gets its own id");
let snap = host.notes_snapshot("demo.txt");
assert_eq!(
snap,
vec![
(id_a, 0, "turn summary".to_string()),
(id_b, 1, "second thought".to_string()),
]
);
host.append_to_note(id_a, "you: tell me more");
let snap = host.notes_snapshot("demo.txt");
assert_eq!(snap[0].2, "turn summary\nyou: tell me more");
host.remove_note(id_a);
let snap = host.notes_snapshot("demo.txt");
assert_eq!(snap.len(), 1, "only the ignored note is gone");
assert_eq!(snap[0].0, id_b);
drop(host);
}
#[test]
fn notes_survive_a_new_turn_on_the_same_file() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world\nsecond line");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("notes");
let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
host.send_task("@demo.txt look this over").unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
while !host
.poll()
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
{
assert!(Instant::now() < deadline, "no TurnDone");
std::thread::sleep(Duration::from_millis(10));
}
let deadline = Instant::now() + Duration::from_secs(60);
let before = loop {
let snap = host.notes_snapshot("demo.txt");
if snap.len() == 1 {
break snap;
}
assert!(
Instant::now() < deadline,
"the scripted note landed: {snap:?}"
);
std::thread::sleep(Duration::from_millis(10));
};
host.send_yield_turn("demo.txt", "hello world\nsecond line")
.unwrap();
assert_eq!(
settle(&before, || host.notes_snapshot("demo.txt")),
before,
"open boxes survive the next turn"
);
drop(host);
}
#[test]
fn compose_reply_turn_carries_note_context_and_buffer() {
let text = compose_reply_turn(
"demo.txt",
3,
"this loop allocates per iteration",
"is that actually hot?",
"a\nb",
);
assert!(
text.contains("replied to your note on demo.txt at line 3 (0-based)"),
"names the note's anchor: {text}"
);
assert!(
text.contains("this loop allocates per iteration"),
"quotes the note body: {text}"
);
assert!(
text.contains("is that actually hot?"),
"carries the reply: {text}"
);
assert!(
text.contains("COMMENT-ONLY"),
"reply turns stay comment-only: {text}"
);
assert!(
text.contains("0|a\n1|b"),
"grounds the model with the numbered buffer: {text}"
);
}
#[test]
fn dropping_the_host_does_not_block_the_ui_thread() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("line zero\nline one");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("linger"); let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "nav", None, cmd).unwrap();
host.send_ask_turn("demo.txt", (0, 0), "", "do a thing", "line zero\nline one")
.unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
while !host
.poll()
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
{
assert!(Instant::now() < deadline, "turn never finished");
std::thread::sleep(Duration::from_millis(10));
}
let t = Instant::now();
drop(host);
assert!(
t.elapsed() < Duration::from_secs(1),
"Drop blocked on the grace-kill for {:?}; teardown must be detached",
t.elapsed()
);
}
#[test]
fn claude_resolves_to_an_absolute_path_off_a_stripped_path() {
assert_eq!(
resolve_claude_in(true, &[]).as_deref(),
Some("claude"),
"an on-PATH claude spawns by bare name"
);
let empty = tempfile::tempdir().unwrap();
assert_eq!(
resolve_claude_in(false, &[empty.path().to_path_buf()]),
None
);
let dir = tempfile::tempdir().unwrap();
let claude = dir.path().join("claude");
std::fs::write(&claude, "#!/bin/sh\n").unwrap();
let got = resolve_claude_in(false, &[dir.path().to_path_buf()]).unwrap();
assert_eq!(got, claude.to_string_lossy());
assert!(
Path::new(&got).is_absolute(),
"GUI launch must get an absolute path, got {got}"
);
}
#[test]
fn system_prompt_teaches_note_fence_and_yield_rules() {
assert!(PAIR_SYSTEM_PROMPT.contains("<<<NOTE <file>:<row>>>>"));
assert!(PAIR_SYSTEM_PROMPT.contains("<<<NOTE demo.txt:4>>>"));
assert!(PAIR_SYSTEM_PROMPT.contains("COMMENT-ONLY"));
assert!(PAIR_SYSTEM_PROMPT.contains("N|"));
assert!(PAIR_SYSTEM_PROMPT.contains("0-based row"));
}
#[test]
fn compose_ask_turn_numbers_content_and_includes_selection() {
let text = compose_ask_turn(
"src/a.rs",
(3, 5),
"let x = 1;\nlet y = 2;",
"simplify these",
"l0\nl1\nl2\nlet x = 1;\nmid\nlet y = 2;",
);
assert!(text.contains("simplify these"));
assert!(text.contains("lines 3-5"));
assert!(text.contains("0-based"));
assert!(text.contains("--- SELECTED LINES ---"));
assert!(text.contains("let y = 2;"));
assert!(text.contains("0|l0"));
assert!(text.contains("3|let x = 1;"));
let one = compose_ask_turn("src/a.rs", (2, 2), "", "why is this here", "a\nb\nc");
assert!(one.contains("line 2"));
assert!(!one.contains("--- SELECTED LINES ---"));
assert!(one.contains("2|c"));
}
#[test]
fn compose_yield_turn_carries_diff_and_forbids_edits() {
let with_diff = compose_yield_turn(
"src/a.rs",
"line one\nline 2\n",
Some("@@ -1,2 +1,2 @@\n line one\n-line two\n+line 2\n"),
);
assert!(with_diff.contains("COMMENT-ONLY"));
assert!(with_diff.contains("--- CHANGES SINCE YOUR LAST LOOK ---"));
assert!(with_diff.contains("+line 2"));
assert!(with_diff.contains("0|line one"));
let first_look = compose_yield_turn("src/a.rs", "just this\n", None);
assert!(first_look.contains("COMMENT-ONLY"));
assert!(!first_look.contains("--- CHANGES SINCE YOUR LAST LOOK ---"));
}
#[test]
fn yield_turn_suppresses_edits() {
if !is_on_path("python3") {
eprintln!("SKIPPED: python3 not on PATH");
return;
}
let harness = OwnerHarness::start("hello world");
let script = harness._dir.path().join("fake_claude.py");
std::fs::write(&script, FAKE_CLAUDE).unwrap();
let log = harness._dir.path().join("stdin.log");
let mut cmd = Command::new("python3");
cmd.arg(&script).arg(&log).arg("stream"); let mut host =
crate::pair_host::PairHost::spawn_cmd(&harness.socket, "navigator", None, cmd).unwrap();
host.send_yield_turn("demo.txt", "hello world").unwrap();
let deadline = Instant::now() + Duration::from_secs(60);
let mut events: Vec<crate::pair_host::PairEvent> = Vec::new();
while !events
.iter()
.any(|e| matches!(e, crate::pair_host::PairEvent::TurnDone { .. }))
{
assert!(Instant::now() < deadline, "no TurnDone; saw {events:?}");
events.extend(host.poll());
std::thread::sleep(Duration::from_millis(10));
}
assert_eq!(harness.remote_edit_count(), 0, "edit must be suppressed");
assert!(
!events.iter().any(|e| matches!(
e,
crate::pair_host::PairEvent::Commentary(c)
if c.contains("suppressed") || c.contains("[claude]")
)),
"diagnostics must not become comment boxes; saw {events:?}"
);
let wait_output = |needle: &str| {
let deadline = Instant::now() + Duration::from_secs(5);
loop {
let lines = crate::output::snapshot("Navigator").unwrap_or_default();
if lines.iter().any(|l| l.text.contains(needle)) {
return;
}
assert!(
Instant::now() < deadline,
"OUTPUT never carried {needle:?}: {lines:?}"
);
std::thread::sleep(Duration::from_millis(10));
}
};
wait_output("suppressed");
wait_output("[claude] mcp server starting");
drop(host);
}
#[test]
fn note_offsets_shift_on_remote_edit() {
let mut notes = vec![
Note {
id: 1,
file: "a.rs".into(),
offset: 10,
body: "n1".into(),
},
Note {
id: 2,
file: "b.rs".into(),
offset: 10,
body: "n2".into(),
},
];
let spans = [
ResolvedSpan {
at: 0,
deleted: 0,
inserted: "abc".into(),
},
ResolvedSpan {
at: 40,
deleted: 2,
inserted: String::new(),
},
];
shift_notes(&mut notes, "a.rs", &spans);
assert_eq!(notes[0].offset, 13);
assert_eq!(notes[1].offset, 10, "other file's note must not move");
}
#[test]
fn an_aborted_edit_un_shifts_the_notes_it_moved() {
let harness = OwnerHarness::start("hello world");
let (state, stop, pump) = pumped_state(&harness);
{
let mut st = state.lock().unwrap();
st.begin_turn("demo.txt", "hello world", false);
for offset in [8usize, 0, 2] {
let id = st.take_note_id();
st.notes.push(Note {
id,
file: String::from("demo.txt"),
offset,
body: String::from("remark"),
});
}
}
apply_fence_event(
&state,
FenceEvent::EditStart {
file: String::from("demo.txt"),
start: (0, 0),
end: (0, 5),
},
);
apply_fence_event(
&state,
FenceEvent::EditBody(String::from("hi there friend")),
);
apply_fence_event(&state, FenceEvent::EditAbort);
let offsets: Vec<usize> = state
.lock()
.unwrap()
.notes
.iter()
.map(|n| n.offset)
.collect();
assert_eq!(
offsets,
vec![8, 0, 2],
"the revert must restore every note anchor the edit had moved"
);
stop.store(true, Ordering::Relaxed);
pump.join().unwrap();
}
#[test]
fn note_anchor_row_clamps_past_eof() {
let lines: Vec<String> = ["ab", "cd"].iter().map(|s| s.to_string()).collect();
assert_eq!(note_offset(&lines, 0), 0);
assert_eq!(note_offset(&lines, 1), 3);
assert_eq!(note_offset(&lines, 99), 3);
}
#[test]
fn compose_turn_always_names_target_file() {
let live = compose_turn_text("fix the error", Some("src/a.rs"), Some("body"));
assert!(live.contains("--- CURRENT BUFFER (src/a.rs) ---"));
assert!(live.contains("body"));
let dead = compose_turn_text("fix the error", Some("src/a.rs"), None);
assert!(
dead.contains("src/a.rs"),
"file must be named without a live buffer: {dead}"
);
assert!(dead.contains("fix the error"));
let noted = with_pending_note(Some("stream was cancelled".into()), "task".into());
assert!(noted.starts_with("stream was cancelled"));
assert!(noted.contains("task"));
}
}