1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! `croft pair`: the AI pilot that streams a Claude conversation's tokens
//! straight into the collab seat, so a co-editing session sees the edit
//! arrive token by token instead of as one bulk insert (docs/MULTIPLAYER.md,
//! "croft pair").
//!
//! This module's pure core is the fence machine: the model is taught (via
//! the pair system prompt) to wrap edits in a fenced protocol inside its
//! streamed TEXT output:
//!
//! ```text
//! <<<EDIT file:START_ROW:START_COL-END_ROW:END_COL>>>
//! <replacement text>
//! <<<END>>>
//! ```
//!
//! Coordinates are 0-based CHARACTER positions against the buffer text the
//! pilot injected into the turn; byte offsets only ever come from
//! [`crate::collab::byte_offset`] (never treat a column as bytes). The
//! machine is fed raw `text_delta` fragments split at arbitrary points and
//! emits events; everything outside a well-formed fence is commentary and is
//! never applied to a buffer.
mod apply;
mod caret;
mod claude_cli;
mod fence;
pub(crate) mod local;
mod notes;
mod offsets;
pub(crate) mod proactive;
mod provider;
mod repl;
mod seat;
mod state;
mod turns;
mod voice;
use apply::*;
#[cfg(test)]
pub(crate) use claude_cli::FAKE_CLAUDE;
pub(crate) use claude_cli::claude_command;
use claude_cli::*;
use fence::*;
#[cfg(test)]
pub(crate) use notes::inject_note;
pub(crate) use notes::inject_note_now;
use notes::*;
use offsets::*;
pub use provider::PairConfig;
pub use provider::Provider;
use provider::*;
pub use repl::run;
#[cfg(test)]
use repl::*;
pub(crate) use seat::Pilot;
pub(crate) use seat::seat_local;
pub(crate) use seat::seat_pilot;
use seat::*;
pub(crate) use state::StagedTurn;
pub(crate) use state::lock_briefly;
pub(crate) use turns::compose_ask_turn;
pub(crate) use turns::compose_reply_turn;
pub(crate) use turns::compose_yield_turn;
pub(crate) use turns::send_turn;
pub(crate) use turns::write_user_turn;
use turns::*;
use voice::*;
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,
};
// ---------------------------------------------------------------------------
// Pilot runtime: spawn the claude CLI in stream-json mode, parse its token
// stream through the fence machine, and apply fences live through a collab
// guest seat. Cancel rides the relay (CollabMsg::StreamCancel) and lands as
// a control_request interrupt on claude's stdin.
// ---------------------------------------------------------------------------
/// Shared pilot state: the collab seat plus per-turn stream bookkeeping.
/// Locked briefly by the reader thread (apply), the pump thread (remote
/// shifts, cancel), and the driver (turn injection); never held across a
/// sleep or a child-process write.
pub(crate) struct PairState {
session: CollabSession,
region: Option<StreamRegion>,
/// The current fence's body is being dropped (unusable header, missing
/// owner, or a post-cancel fence).
discarding: bool,
/// A cancel landed this turn: every further fence event is ignored
/// until the turn's result, and the next turn opens with a note.
cancelled: bool,
/// claude's init advertised interrupt_receipt_v1.
can_interrupt: bool,
/// Between sending a user message and seeing its result.
turn_active: bool,
/// Whose buffer rides in the next turn's injection.
target_file: Option<String>,
/// Prepended to the next user turn (set by cancel).
pending_note: Option<String>,
/// Bumped by every [`Self::stage_turn`]; pairs with
/// [`StagedTurn::stage_gen`] so a deferred rollback can recognise that
/// a newer turn has staged and drop itself.
stage_gen: u64,
/// Anchored navigator notes; offsets kept fresh by the pump.
notes: Vec<Note>,
/// Next note id (monotonic; ids are never reused within a seat).
next_note_id: u64,
/// The NOTE fence currently streaming: (file, 0-based row, body so far).
note_in_flight: Option<(String, usize, String)>,
/// The in-process host's sink: when seated inside croft the pilot's
/// voice (commentary, notes, diagnostics) goes here instead of
/// stdout/stderr, which belong to the TUI. None = terminal REPL.
events: Option<Sender<crate::pair_host::PairEvent>>,
/// This turn is a yielded one: the navigator may only comment. EDIT
/// fences are discarded at the host, not just discouraged in the
/// prompt. Reset at each turn's result.
comment_only: bool,
/// Where the seat wants its visible caret parked (file, 0-based row)
/// once the file's bootstrap lands; the pump resolves it. The caret is
/// the navigator's persistent presence between edits.
pending_caret: Option<(String, usize)>,
/// What the navigator last saw of each file (yield turns diff against
/// this instead of resending history).
last_seen: std::collections::HashMap<String, String>,
}
#[cfg(test)]
mod tests;