1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use super::*;
#[cfg(not(target_family = "wasm"))]
use crate::rollout::RolloutInfo;
/// Cheap, cloneable command handle for an owned agent driver.
pub struct Nanocodex {
pub(super) commands: mpsc::Sender<Command>,
pub(super) events: EventSink,
pub(super) next_turn: Arc<AtomicU64>,
pub(super) lineage_id: Arc<str>,
pub(super) session_id: SessionId,
pub(super) durability: Durability,
pub(super) shutdown: DriverShutdown,
}
impl Clone for Nanocodex {
fn clone(&self) -> Self {
Self {
commands: self.commands.clone(),
events: self.events.clone(),
next_turn: Arc::clone(&self.next_turn),
lineage_id: Arc::clone(&self.lineage_id),
session_id: self.session_id,
durability: self.durability.clone(),
shutdown: self.shutdown.clone(),
}
}
}
/// Weak child-agent capability for the driver that owns one tool runtime.
///
/// A tools factory receives a fresh handle for every agent driver. Holding the
/// handle does not keep its agent alive.
#[derive(Clone)]
pub struct AgentHandle {
pub(super) commands: mpsc::WeakSender<Command>,
}
impl AgentHandle {
/// Starts a clean agent with the containing driver's private configuration,
/// service factory, workspace policy, and per-agent tools factory.
///
/// The child receives a new session, cache lineage, conversation, driver,
/// WebSocket, and tool runtime. It does not inherit conversation history.
///
/// # Errors
///
/// Returns an error after the containing driver has stopped.
pub async fn spawn(&self) -> Result<(Nanocodex, AgentEvents)> {
let commands = self.commands()?;
request_spawn(&commands).await
}
/// Forks the containing agent's latest safe model boundary.
///
/// # Errors
///
/// Returns an error before the first prompt reaches a safe boundary, or
/// after the containing agent driver has stopped.
pub async fn fork(&self) -> Result<(Nanocodex, AgentEvents)> {
let commands = self.commands()?;
request_fork(&commands, None).await
}
fn commands(&self) -> Result<mpsc::Sender<Command>> {
self.commands.upgrade().ok_or(NanocodexError::AgentStopped)
}
}
impl Nanocodex {
/// Starts configuring an agent from a reusable [`OpenAi`] client recipe.
#[must_use]
pub fn builder<F>(openai: OpenAi<F>) -> NanocodexBuilder<F>
where
F: ResponsesServiceFactory,
{
let (config, factory) = into_openai_parts(openai);
NanocodexBuilder {
config,
tools: ToolsConfiguration::Shared(Tools::default()),
workspace: None,
session_id: None,
prompt_cache: PromptCacheConfig::default(),
codex: CodexCompatibility::default(),
resume: None,
factory,
}
}
/// Returns the stable identity used by events, transport metadata, and any rollout.
#[must_use]
pub const fn session_id(&self) -> SessionId {
self.session_id
}
/// Returns the Codex-compatible rollout identity and path when recording is enabled.
#[cfg(not(target_family = "wasm"))]
#[cfg_attr(docsrs, doc(cfg(not(target_family = "wasm"))))]
#[must_use]
pub const fn rollout(&self) -> Option<&RolloutInfo> {
self.durability.info()
}
/// Retries any pending rollout write and waits for a durable file flush.
///
/// This is a no-op when rollout recording is disabled. CLI consumers call
/// it at completed turn boundaries so persistence failures are user-visible.
/// Flushing does not stop the live writer; call [`Self::shutdown`] at an
/// explicit application or session boundary.
///
/// # Errors
///
/// Returns an error when the configured rollout cannot be written.
#[cfg(not(target_family = "wasm"))]
#[cfg_attr(docsrs, doc(cfg(not(target_family = "wasm"))))]
pub async fn flush_rollout(&self) -> Result<()> {
self.durability.flush().await
}
/// Gracefully stops this agent and waits for all owned resources to close.
///
/// Shutdown globally invalidates this handle and every clone. It cancels an
/// active turn, terminalizes all other accepted turns in FIFO order, waits
/// for model and tool cleanup, and flushes and closes the rollout writer. A
/// returned `Ok(())` therefore establishes a durable boundary suitable for
/// an immediate same-process rollout resume.
///
/// Dropping the final handle retains the existing implicit cancellation
/// behavior, but offers no future that can join resource cleanup. Use this
/// method at an explicit application or session boundary.
///
/// # Errors
///
/// Returns the shared cleanup result. The first caller initiates shutdown;
/// concurrent and later callers on any clone await or reuse that same
/// result.
pub async fn shutdown(&self) -> Result<()> {
let (initiate, receiver) = self.shutdown.request();
if initiate && self.commands.send(Command::Shutdown).await.is_err() {
let outcome = match self.durability.shutdown().await {
Ok(()) => Err(NanocodexError::AgentStopped),
Err(error) => Err(error),
};
self.shutdown.complete(outcome);
}
match receiver.await {
Ok(Ok(())) => Ok(()),
Ok(Err(error)) => Err(NanocodexError::Shutdown(error)),
Err(_) => Err(NanocodexError::AgentStopped),
}
}
/// Accepts the agent's prompt and immediately returns its turn handle.
///
/// # Errors
///
/// Returns an error for an empty prompt or if the driver stopped.
pub async fn prompt(&self, prompt: impl Into<Prompt>) -> Result<Turn> {
let prompt = prompt.into();
if prompt.instruction.is_empty() {
return Err(NanocodexError::InvalidRequest(
"prompt instruction must not be empty".to_owned(),
));
}
let key = TurnKey(self.next_turn.fetch_add(1, Ordering::Relaxed));
let parent = tracing::Span::current();
let parent = (!parent.is_disabled()).then_some(parent);
let (events, event_stream) = self.events.mirrored_channel();
let (result, receiver) = oneshot::channel();
if self
.commands
.send(Command::Prompt {
key,
prompt,
thinking: None,
fast_mode: None,
parent,
events,
result,
})
.await
.is_err()
{
return Err(NanocodexError::AgentStopped);
}
Ok(Turn {
control: TurnControl {
key,
commands: self.commands.clone(),
},
events: event_stream,
result: receiver,
})
}
/// Changes the reasoning effort for subsequently accepted turns.
///
/// An active turn and prompts already queued by the driver retain the
/// effort they captured when accepted.
///
/// # Errors
///
/// Returns an error if the agent driver has stopped.
pub async fn set_thinking(&self, thinking: Thinking) -> Result<()> {
request_command(&self.commands, |result| Command::SetThinking {
thinking,
result,
})
.await
}
/// Enables or disables priority processing for subsequently accepted turns.
///
/// An active turn and prompts already queued by the driver retain the mode
/// they captured when accepted.
///
/// # Errors
///
/// Returns an error if the agent driver has stopped.
pub async fn set_fast_mode(&self, enabled: bool) -> Result<()> {
request_command(&self.commands, |result| Command::SetFastMode {
enabled,
result,
})
.await
}
/// Immediately compacts this agent's retained conversation.
///
/// Compaction preserves the agent's cache identity, tools, transport, and
/// cached project instructions. The next prompt receives a full developer,
/// `AGENTS.md`, and environment-context reinjection before its user input.
/// If a turn is active, that turn is cancelled and compaction runs before
/// prompts that were queued behind it.
///
/// ```
/// # use nanocodex_agent::{Nanocodex, Result};
/// # async fn compact_after_a_turn(agent: &Nanocodex) -> Result<()> {
/// agent
/// .prompt("Inspect the parser and explain the failing test.")
/// .await?
/// .result()
/// .await?;
/// agent.compact().await?;
/// let result = agent
/// .prompt("Now implement the smallest correct parser fix.")
/// .await?
/// .result()
/// .await?;
/// assert!(!result.final_message().is_empty());
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns a model or driver-stopped error. Rollout writes follow the same
/// retry-on-[`Self::flush_rollout`] contract as prompt turns.
pub async fn compact(&self) -> Result<()> {
let parent = tracing::Span::current();
let parent = (!parent.is_disabled()).then_some(parent);
request_command(&self.commands, |result| Command::Compact { parent, result }).await
}
/// Starts a clean sibling agent with the same private configuration,
/// workspace policy, service factory, and tools factory.
///
/// The sibling receives a new session, cache lineage, conversation,
/// WebSocket, and tool runtime. It does not inherit conversation history.
///
/// # Errors
///
/// Returns an error after this agent's driver has stopped.
pub async fn spawn(&self) -> Result<(Self, AgentEvents)> {
request_spawn(&self.commands).await
}
/// Forks from the latest safe model boundary into an independently driven
/// agent.
///
/// The child receives a fresh WebSocket and tool runtime while sharing the
/// immutable transcript, inherited incremental delta, and prompt-cache
/// lineage. Partial model output and unmatched tool calls are excluded.
///
/// # Errors
///
/// Returns an error before the first prompt reaches a safe boundary, or
/// when the driver has stopped.
pub async fn fork(&self) -> Result<(Self, AgentEvents)> {
self.request_fork(None).await
}
/// Forks from an exact historical completed turn while this agent may keep
/// advancing on its current branch.
///
/// # Errors
///
/// Returns an error when the result belongs to another conversation or the
/// driver stopped.
pub async fn fork_from(&self, completed: &TurnResult) -> Result<(Self, AgentEvents)> {
if completed.checkpoint.lineage_id() != self.lineage_id.as_ref() {
return Err(NanocodexError::CheckpointLineageMismatch);
}
self.request_fork(Some(Arc::clone(&completed.checkpoint)))
.await
}
async fn request_fork(
&self,
checkpoint: Option<Arc<CommittedSession>>,
) -> Result<(Self, AgentEvents)> {
request_fork(&self.commands, checkpoint).await
}
}
async fn request_fork(
commands: &mpsc::Sender<Command>,
checkpoint: Option<Arc<CommittedSession>>,
) -> Result<(Nanocodex, AgentEvents)> {
request_command(commands, |result| Command::Fork { checkpoint, result }).await
}
async fn request_spawn(commands: &mpsc::Sender<Command>) -> Result<(Nanocodex, AgentEvents)> {
request_command(commands, |result| Command::Spawn { result }).await
}
pub(super) async fn request_command<T>(
commands: &mpsc::Sender<Command>,
command: impl FnOnce(oneshot::Sender<Result<T>>) -> Command,
) -> Result<T> {
let (result, receiver) = oneshot::channel();
commands
.send(command(result))
.await
.map_err(|_| NanocodexError::AgentStopped)?;
receiver.await.map_err(|_| NanocodexError::AgentStopped)?
}