rpi-cli 0.34.0

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Native-only production assembly, using real runtime Providers and builtin
//! tools. Inputs are supplied by a trusted Host owner, never model arguments.
//! No CLI Args, settings discovery, process env mutation or subprocess Agent
//! fallback occurs here. Only an explicit private nativeHooks resource binding
//! can load trusted code, through owned deferred assembly and strict custody.

use super::{
    binding_resources::NativeHookCallbacks,
    ordinary::{BackendError, BackendResult, OrdinaryBinding},
    AgentMode, AgentSpec, ExecutionMode,
};
use rpi_harness::types::{AgentHarnessOptions, AgentHarnessStreamOptions, HarnessTool, ToolReplay};
use rpi_runtime::{ModelRequest, ProviderRegistry, RuntimeConfig};
use rpi_tools::{tools::*, OsExecutionEnv};
use std::{
    collections::{BTreeMap, BTreeSet},
    path::PathBuf,
    sync::Arc,
};

/// A trusted host's explicit, noninteractive grant. Default is no tools. This
/// does not represent a user's interactive approval or a filesystem sandbox.
/// IPC/model-facing callers must never be able to mint this from their input.
#[derive(Default)]
pub enum NativeToolPolicy {
    #[default]
    DenyAll,
    Preapproved(BTreeSet<String>),
}
impl NativeToolPolicy {
    fn allows(&self, name: &str) -> bool {
        matches!(self, Self::Preapproved(names) if names.contains(name))
    }
}

/// Per-Agent private inputs. Deliberately not Debug/Serialize: runtime and
/// value maps can contain secrets. Provider values and tool environment are
/// separate; provider credentials are never copied into a shell baseline.
pub struct NativeBindingProfile {
    pub runtime: RuntimeConfig,
    /// Closed value source, including any proxy/header/key variables. Missing
    /// values and secret commands do not fall back to process env or shell.
    pub provider_values: BTreeMap<String, String>,
    /// Complete tool baseline, including an absolute HOME (USERPROFILE on
    /// Windows). The trusted host chooses what a granted local shell may see.
    pub tool_environment: BTreeMap<String, String>,
    pub shell_path: PathBuf,
    pub tools: NativeToolPolicy,
    pub thinking: rpi_ai::ThinkingLevel,
    pub stream_options: AgentHarnessStreamOptions,
    /// Closed frozen resources. The historical field name also covers explicitly
    /// approved native hook resources; it never enables ambient discovery.
    pub instruction_resources: Option<Arc<super::NativeBindingResources>>,
}
impl NativeBindingProfile {
    /// Prepare one fresh execution environment per Agent; never share process
    /// cleanup ownership across Agents, even if they use the same workspace.
    /// The immutable trusted inputs remain reusable. Resolve closed Provider
    /// inputs here, before callers may create a Session or register metadata;
    /// persisted resource authority is checked later against readonly facts.
    pub async fn bind(&self, expected: AgentSpec) -> BackendResult<OrdinaryBinding> {
        let (resolved, env) = self
            .resolve_binding(&expected, AgentMode::Persistent)
            .await?;
        let stream_options = self.stream_options.clone();
        let resources = self.instruction_resources.clone();
        let configured_resources = resources.clone();
        let binder: super::ordinary::OrdinaryBinder = Box::new(move |spec, env, facts| {
            if spec != &expected {
                return Err(BackendError::Binding(
                    "prepared Agent identity or launch changed",
                ));
            }
            // Do not silently restore an immutable exact resource declaration
            // using a native-only prompt/tool set. This also validates corrupt
            // persisted declarations before any repair or writer acquisition.
            if configured_resources.is_none() {
                crate::resource_selection::validate_session_binding(&facts.values, None).map_err(
                    |_| {
                        BackendError::Binding("saved resources require their exact resource binder")
                    },
                )?;
            }
            if facts.values.iter().any(|(key, _)| {
                key.starts_with("rpi.resource-inventory.")
                    || (key.starts_with("rpi.resource-selection.")
                        && key != "rpi.resource-selection.v1")
            }) {
                return Err(BackendError::Binding(
                    "saved resource inventory or version is unsupported by native binding",
                ));
            }
            let tools = native_tools(spec, &env)?;
            let granted: BTreeSet<_> = spec.launch.tools.iter().cloned().collect();
            let before_tool_call: rpi_agent::BeforeToolCall = Arc::new(move |context, signal| {
                let allowed = granted.contains(&context.tool_call.name) && !signal.is_cancelled();
                Box::pin(async move {
                    (!allowed).then(|| rpi_agent::BeforeToolCallResult {
                        block: true,
                        reason: Some("tool not granted by Host profile".into()),
                        ..Default::default()
                    })
                })
            });
            let mut options = AgentHarnessOptions {
                system_prompt: Some(crate::session::default_system_prompt_with_tools(
                    &spec.launch.cwd.to_string_lossy(),
                    rpi_ai::model::prior_reasoning_is_replayed(&resolved.model),
                    &spec.launch.tools,
                )),
                model: resolved.model,
                thinking_level: resolved.thinking_level,
                models: vec![resolved.provider],
                tools,
                active_tool_names: spec.launch.tools.clone(),
                stream_options,
                before_tool_call: Some(before_tool_call),
                ..Default::default()
            };
            if let Some(resources) = &configured_resources {
                resources.configure_parts(&mut options.system_prompt, &mut options.resources);
            }
            Ok(options)
        });
        let mut binding =
            if let Some(resources) = resources.as_ref().filter(|resources| resources.has_hooks()) {
                let frozen = resources.clone();
                let owner = super::NativeExtensionCustody::new();
                let assembly_owner = owner.clone();
                OrdinaryBinding::new_deferred(
                    env,
                    Box::new(move |spec, env, facts| {
                        let options = binder(spec, env, facts)?;
                        // Pure saved-fact validation must precede staging/registration.
                        frozen.validate(&facts.values, &options.tools)?;
                        let spec = spec.clone();
                        Ok(Box::pin(async move {
                            frozen.assemble(&spec, assembly_owner, options).await
                        }))
                    }),
                )?
                .with_extensions(owner)
            } else {
                OrdinaryBinding::new(env, binder)?
            };
        binding.instruction_resources = resources;
        Ok(binding)
    }

    /// Pure Mini preflight: closed Provider/path/resource validation only. No
    /// raw Agent, DLL load, Session or catalog mutation occurs here.
    #[cfg(unix)]
    pub(super) async fn preflight_mini(&self, expected: &AgentSpec) -> BackendResult<()> {
        drop(self.resolve_binding(expected, AgentMode::Mini).await?);
        Ok(())
    }

    /// Build the existing raw Agent loop with no Harness/Session construction.
    /// Caller must already retain `custody` in the owning Host entry before
    /// awaiting this method; dropping the request waiter is not cleanup.
    pub async fn bind_mini_agent(
        &self,
        expected: AgentSpec,
        catalog: super::HostCatalog,
        custody: Arc<super::NativeExtensionCustody>,
    ) -> BackendResult<NativeMiniAgent> {
        let (resolved, env) = self.resolve_binding(&expected, AgentMode::Mini).await?;
        let tools = native_tools(&expected, &env)?;
        let granted: BTreeSet<_> = expected.launch.tools.iter().cloned().collect();
        let before_tool_call: rpi_agent::BeforeToolCall = Arc::new(move |context, signal| {
            let allowed = granted.contains(&context.tool_call.name) && !signal.is_cancelled();
            Box::pin(async move {
                (!allowed).then(|| rpi_agent::BeforeToolCallResult {
                    block: true,
                    reason: Some("tool not granted by Host profile".into()),
                    ..Default::default()
                })
            })
        });
        let mut prompt = Some(crate::session::default_system_prompt_with_tools(
            &expected.launch.cwd.to_string_lossy(),
            rpi_ai::model::prior_reasoning_is_replayed(&resolved.model),
            &expected.launch.tools,
        ));
        let mut provenance = rpi_harness::types::AgentHarnessResources::empty();
        if let Some(resources) = &self.instruction_resources {
            resources.configure_parts(&mut prompt, &mut provenance);
        }
        let callbacks = match self.instruction_resources.as_ref() {
            Some(resources) if resources.has_hooks() => {
                custody.claim(catalog)?;
                resources
                    .assemble_callbacks(&expected, custody.clone())
                    .await?
            }
            _ => NativeHookCallbacks::default(),
        };
        let provider = resolved.provider;
        let provider_hooks = callbacks.provider_hooks.clone();
        let stream_options = self.stream_options.clone();
        let stream = rpi_agent::async_stream_fn(move |model, context, options| {
            let provider = provider.clone();
            let hooks = provider_hooks.clone();
            let pinned = stream_options.clone();
            Box::pin(async move {
                let mut context = std::borrow::Cow::Borrowed(context);
                let mut options = pinned.merge_into(options.clone());
                if let Some(hooks) = &hooks {
                    if let Some(patch) = hooks.transform_context(model, &context) {
                        if patch != rpi_ai::ContextPatch::default() {
                            patch.apply(context.to_mut());
                        }
                    }
                    if let Some(patch) = hooks.before_request_async(model, &context, &options).await
                    {
                        patch.apply(&mut options);
                    }
                }
                provider.stream_simple(model, &context, &options).await
            })
        });
        let mut builder = rpi_agent::AgentBuilder::new()
            .model(resolved.model)
            .thinking_level(resolved.thinking_level)
            .system_prompt(prompt.unwrap_or_default())
            .tools(tools.into_iter().map(|tool| tool.tool).collect())
            .async_stream_fn(stream)
            .before_tool_call(before_tool_call);
        if let Some(after_tool_call) = callbacks.after_tool_call {
            builder = builder.after_tool_call(after_tool_call);
        }
        if let Some(provider_hooks) = callbacks.provider_hooks {
            builder = builder.provider_hooks(provider_hooks);
        }
        let agent = builder
            .build()
            .map_err(|_| BackendError::Binding("native mini Agent construction failed"))?;
        Ok(NativeMiniAgent {
            agent,
            custody: self
                .instruction_resources
                .as_ref()
                .is_some_and(|resources| resources.has_hooks())
                .then_some(custody),
        })
    }

    async fn resolve_binding(
        &self,
        expected: &AgentSpec,
        mode: AgentMode,
    ) -> BackendResult<(rpi_runtime::ResolvedModel, Arc<OsExecutionEnv>)> {
        expected
            .validate()
            .map_err(|_| BackendError::Binding("invalid Agent metadata"))?;
        if expected.mode != mode || expected.execution != ExecutionMode::InProcess {
            return Err(BackendError::Binding("native binding mode is unsupported"));
        }
        if expected.launch.resource_profile.as_deref()
            != self
                .instruction_resources
                .as_ref()
                .map(|resources| resources.identity())
        {
            return Err(BackendError::Binding(
                "resource profile requires its exact frozen instruction binding",
            ));
        }
        if let Some(resources) = &self.instruction_resources {
            resources.validate_tools(&expected.launch.tools)?;
        }
        if expected
            .launch
            .tools
            .iter()
            .any(|name| !self.tools.allows(name))
        {
            return Err(BackendError::Binding(
                "requested tool is not preapproved by Host policy",
            ));
        }
        if expected
            .launch
            .tools
            .iter()
            .any(|name| !crate::session::BUILTIN_TOOL_NAMES.contains(&name.as_str()))
        {
            return Err(BackendError::Binding(
                "native binding does not implement requested tool",
            ));
        }
        let home_key = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
        if !self.shell_path.is_absolute()
            || self
                .tool_environment
                .get(home_key)
                .is_none_or(|home| !std::path::Path::new(home).is_absolute())
            || self.tool_environment.iter().any(|(name, value)| {
                name.is_empty()
                    || name.contains(['=', '\0'])
                    || value.contains('\0')
                    || rpi_tools::is_host_private_env(name)
            })
        {
            return Err(BackendError::Binding(
                "invalid explicit tool environment or shell path",
            ));
        }
        let unresolved_header = self.runtime.models.providers.values().any(|provider| {
            provider
                .headers
                .iter()
                .chain(
                    provider
                        .models
                        .iter()
                        .filter_map(|model| model.headers.as_ref()),
                )
                .flat_map(|headers| headers.values())
                .any(|raw| {
                    rpi_runtime::values::resolve_config_value(
                        raw,
                        None,
                        &self.provider_values,
                        false,
                    )
                    .is_none()
                })
        });
        let command_key = self
            .runtime
            .models
            .providers
            .values()
            .filter_map(|provider| provider.api_key.as_deref())
            .any(|key| key.starts_with('!'))
            || self.runtime.credentials.values().any(|credential| {
                matches!(credential,
                    rpi_runtime::config::Credential::ApiKey { key: Some(key), .. } if key.starts_with('!'))
            });
        if unresolved_header || command_key {
            return Err(BackendError::Binding(
                "native binding requires fully resolved headers and non-command credentials",
            ));
        }
        let model = expected
            .launch
            .model
            .as_ref()
            .ok_or(BackendError::Binding("model reference required"))?;
        let resolved = self
            .runtime
            .resolve(
                &ModelRequest {
                    provider: Some(&model.provider),
                    model: Some(&model.model),
                    thinking: Some(self.thinking),
                    ..Default::default()
                },
                &ProviderRegistry::default(),
                &self.provider_values,
            )
            .map_err(|_| {
                BackendError::Binding("explicit Provider configuration could not be resolved")
            })?;
        if resolved.model.provider != model.provider || resolved.model.id != model.model {
            return Err(BackendError::Binding(
                "Provider resolution must match the exact model reference",
            ));
        }
        let cwd = tokio::fs::canonicalize(&expected.launch.cwd).await?;
        let shell = tokio::fs::canonicalize(&self.shell_path).await?;
        if !tokio::fs::metadata(&shell).await?.is_file() {
            return Err(BackendError::Binding("explicit shell must be a file"));
        }
        let env = Arc::new(
            OsExecutionEnv::with_cwd(cwd)
                .with_environment(self.tool_environment.clone().into_iter().collect())
                .with_shell_path(shell),
        );
        Ok((resolved, env))
    }
}

/// Raw, non-persistent Agent and its optional native DLL settlement owner.
/// It contains no Harness, AgentLane, Session, journal or writer.
pub struct NativeMiniAgent {
    agent: rpi_agent::Agent,
    custody: Option<Arc<super::NativeExtensionCustody>>,
}
impl NativeMiniAgent {
    pub fn agent(&self) -> &rpi_agent::Agent {
        &self.agent
    }
    pub fn cancel(&self) {
        self.agent.abort();
    }
    /// Close through the existing Agent and native DLL settlement barriers.
    /// On failure this value and its custody must remain owned for retry.
    pub async fn close(&self) -> BackendResult<()> {
        self.agent.abort();
        self.agent.wait_for_idle().await;
        if let Some(custody) = &self.custody {
            custody.close().await?;
        }
        Ok(())
    }
}

fn native_tools(spec: &AgentSpec, env: &Arc<OsExecutionEnv>) -> BackendResult<Vec<HarnessTool>> {
    let ctx = ExecutionToolContext::new(env.clone(), Some(env.clone()));
    spec.launch
        .tools
        .iter()
        .map(|name| {
            Ok(match name.as_str() {
                "read" => HarnessTool::new(create_read_tool(
                    &ctx,
                    Some(ReadToolOptions {
                        image_processor: Some(Arc::new(
                            rpi_tools::image_processing::InlineImageProcessor,
                        )),
                        ..Default::default()
                    }),
                ))
                .with_replay(ToolReplay::Safe),
                "bash" => HarnessTool::new(create_bash_tool(
                    &ctx,
                    Some(BashToolOptions {
                        default_timeout: Some(rpi_tools::tools::bash::DEFAULT_BASH_TIMEOUT_SECONDS),
                        ..Default::default()
                    }),
                )),
                "edit" => HarnessTool::new(create_edit_tool(&ctx)),
                "write" => HarnessTool::new(create_write_tool(&ctx)),
                "docs" => HarnessTool::new(crate::docs_tool::create_docs_tool())
                    .with_replay(ToolReplay::Safe),
                _ => return Err(BackendError::Binding("unsupported native tool")),
            })
        })
        .collect()
}

#[cfg(all(test, unix))]
#[path = "bindings_tests.rs"]
mod tests;