1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! The harnesses, the modes and one turn's plan (§40.50 items 4, 5, 8):
//! which executable, which argv vector, which variables beside the
//! server's own. The prompt is never part of a plan; it arrives on the
//! harness's standard input.
use std::path::Path;
use super::events::Event;
use super::{agy, claude, codex, cursor, muse, opencode, pi, vibe};
mod check;
pub(super) use check::executable;
pub use check::{
PROMPT_BYTES, Placed, bare_key, home, model_ok, place, prompt_ok, session_ok, which,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Agent {
OpenCode,
Codex,
ClaudeCode,
Pi,
Cursor,
Vibe,
Agy,
Muse,
}
impl Agent {
pub const ALL: [Self; 8] = [
Self::OpenCode,
Self::Codex,
Self::ClaudeCode,
Self::Pi,
Self::Cursor,
Self::Vibe,
Self::Agy,
Self::Muse,
];
pub const NAMES: [&'static str; 8] = [
"opencode",
"codex",
"claude-code",
"pi",
"cursor",
"vibe",
"agy",
"muse",
];
pub fn parse(name: &str) -> Option<Self> {
Self::ALL.into_iter().find(|agent| agent.name() == name)
}
/// The tool's name for it, which is also the record verb's `--agent`.
pub fn name(self) -> &'static str {
match self {
Self::OpenCode => "opencode",
Self::Codex => "codex",
Self::ClaudeCode => "claude-code",
Self::Pi => "pi",
Self::Cursor => "cursor",
Self::Vibe => "vibe",
Self::Agy => "agy",
Self::Muse => "muse",
}
}
/// The writer kind the record verb files this child's turns under, and
/// the store names its conversation by: the tool's name, but Muse Code's
/// own `muse-code`, as its ordinary recording names it (X6b).
pub fn kind(self) -> &'static str {
match self {
Self::Muse => "muse-code",
other => other.name(),
}
}
/// The bytes the harness reads on stdin for `prompt`: the prompt itself,
/// or for Antigravity one stream-json `user` line (X6a).
pub fn stdin(self, prompt: &[u8]) -> Vec<u8> {
match self {
Self::Agy => agy::stdin(prompt),
_ => prompt.to_vec(),
}
}
/// Whether neosian names a new session's ID itself, so it is known
/// before the harness announces it: Claude Code's `--session-id` and
/// Pi's (X5a).
pub fn names_session(self) -> bool {
matches!(self, Self::ClaudeCode | Self::Pi | Self::Muse)
}
/// The executable looked up on the server's PATH.
pub fn program(self) -> &'static str {
match self {
Self::OpenCode => "opencode",
Self::Codex => "codex",
Self::ClaudeCode => "claude",
Self::Pi => "pi",
Self::Cursor => "cursor-agent",
Self::Vibe => "vibe",
Self::Agy => "agy",
Self::Muse => "muse",
}
}
/// One line of the harness's headless output, read.
pub fn read(self, line: &str) -> Vec<Event> {
let Ok(serde_json::Value::Object(event)) = serde_json::from_str(line) else {
return Vec::new();
};
match self {
Self::OpenCode => opencode::read(&event),
Self::Codex => codex::read(&event),
Self::ClaudeCode => claude::read(&event),
Self::Pi => pi::read(&event),
Self::Cursor => cursor::read(&event),
Self::Vibe => vibe::read(&event),
Self::Agy => agy::read(&event),
Self::Muse => muse::read(&event),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
ReadOnly,
Write,
}
impl Mode {
pub const NAMES: [&'static str; 2] = ["read-only", "write"];
pub fn parse(name: &str) -> Option<Self> {
match name {
"read-only" => Some(Self::ReadOnly),
"write" => Some(Self::Write),
_ => None,
}
}
pub fn name(self) -> &'static str {
match self {
Self::ReadOnly => "read-only",
Self::Write => "write",
}
}
}
/// A Codex registration key that exists, with the tools it already
/// disables and its transport (X4c).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CodexKey {
pub key: String,
pub disabled: Vec<String>,
/// The tools the entry already limits itself to, when it does.
pub enabled: Option<Vec<String>>,
pub transport: super::transport::Transport,
}
/// This neosian's own MCP server as a read-only child gets it (#431): the
/// running executable and its store selection, never a command read from a
/// file. With `mounted`, every mount is in `args` and read-only, so every
/// tool may be allowed; without (a profile selects the mounts), only the
/// tools that cannot change state are.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct McpServer {
pub command: String,
pub args: Vec<String>,
pub mounted: bool,
}
/// The key the child's strict MCP configuration names this server under.
pub const SERVER_KEY: &str = "neosian";
/// The neosian tools that cannot change state, allowed by name when the
/// mounts cannot be made read-only.
pub const READ_ONLY_TOOLS: [&str; 10] = [
"list_skills",
"load_skill",
"recall_turn",
"search_history",
"docs",
"memory_history",
"search_memory",
"upkeep_review",
"knowledge",
"changes",
];
/// What one turn needs to be planned.
pub struct Request<'a> {
pub agent: Agent,
pub mode: Mode,
pub model: Option<&'a str>,
pub directory: &'a Path,
/// The harness session to continue; None starts a new one.
pub resume: Option<&'a str>,
/// The session ID neosian chose for a new Claude Code session.
pub new_session: &'a str,
/// The turn's private directory (Codex's TMPDIR, holding the marker).
pub private: &'a Path,
/// Codex: which of our keys its configuration has.
pub codex_keys: &'a [CodexKey],
/// OpenCode: the `OPENCODE_CONFIG_CONTENT` the server itself carries.
pub opencode_config: Option<&'a str>,
/// The calling session, which the child's record hook names as parent.
pub parent: Option<&'a str>,
/// Read-only: this neosian's server, for Claude Code's strict
/// configuration and Codex's entry arguments.
pub server: Option<&'a McpServer>,
/// Codex read-only: the other configured MCP servers, switched off.
pub codex_off: &'a [String],
/// Only a launch under neosian's common policy changes a preset (X4b).
pub confined: bool,
/// Pi: its agent directory, the child's session directory and, to
/// continue, the session's exact file (X5a).
pub pi: Option<&'a pi::Paths>,
/// Vibe and Muse Code: the child's private home (X6a, X6b).
pub child_home: Option<&'a Path>,
/// A Vibe or Muse child's one MCP server, neosian's own (X7b).
pub memory: Option<&'a super::server::ChildServer>,
}
/// One turn's command: argv[0] is the program, resolved by the caller.
#[derive(Debug, PartialEq, Eq)]
pub struct Plan {
pub argv: Vec<String>,
pub env: Vec<(String, String)>,
}
/// Why a plan cannot be made.
#[derive(Debug, PartialEq, Eq)]
pub enum Unplanned {
OpenCodeConfig,
/// Pi's directories, or the file of the session to continue, are missing.
PiState,
/// A directory Cursor is given is not UTF-8.
CursorPath,
/// A Vibe or Muse Code child's private home is missing or not UTF-8.
ChildHome,
}
impl Unplanned {
/// The watcher's refusal.
pub fn sentence(&self) -> String {
let say = super::text::say;
match self {
Self::OpenCodeConfig => say("bad_opencode_config", &[]),
Self::PiState => say("watch_bad_field", &[("field", "pi")]),
Self::CursorPath => say("watch_bad_field", &[("field", "directory")]),
Self::ChildHome => say("watch_bad_field", &[("field", "child_home")]),
}
}
}
/// The variables the child's record hook reads (§40.50 amended, #430).
pub const PARENT_ENV: &str = "NEOSIAN_HARNESS_PARENT";
pub const SESSION_ENV: &str = "NEOSIAN_HARNESS_SESSION";
pub fn plan(request: &Request) -> Result<Plan, Unplanned> {
let mut plan = match request.agent {
Agent::ClaudeCode => claude::plan(request),
Agent::Codex => codex::plan(request),
Agent::OpenCode => opencode::plan(request)?,
Agent::Pi => pi::plan(request)?,
Agent::Cursor => cursor::plan(request)?,
Agent::Vibe => vibe::plan(request)?,
Agent::Agy => agy::plan(request)?,
Agent::Muse => muse::plan(request)?,
};
let directory = request.directory.display().to_string();
plan.env
.push((super::CHILD_ENV.to_string(), "1".to_string()));
// `opencode run` takes its project from PWD, not the working directory.
plan.env.push(("PWD".to_string(), directory));
if let Some(parent) = request.parent {
plan.env.push((PARENT_ENV.to_string(), parent.to_string()));
}
let known = match request.resume {
Some(session) => Some(session),
None => request.agent.names_session().then_some(request.new_session),
};
if let Some(session) = known {
plan.env
.push((SESSION_ENV.to_string(), session.to_string()));
}
Ok(plan)
}
#[cfg(test)]
mod inputs;
#[cfg(test)]
mod read_only;
#[cfg(test)]
mod tests;