kaish_kernel/tools/context.rs
1//! Execution context for tools.
2
3use std::collections::HashMap;
4use std::path::{Component, Path, PathBuf};
5use std::sync::Arc;
6
7use async_trait::async_trait;
8
9use crate::ast::Value;
10use crate::backend::{KernelBackend, LocalBackend};
11use crate::dispatch::PipelinePosition;
12use crate::ignore_config::IgnoreConfig;
13use crate::interpreter::{ExecResult, Scope};
14use crate::output_limit::OutputLimitConfig;
15use crate::scheduler::{JobManager, PipeReader, PipeWriter, StderrStream};
16use crate::tools::ToolRegistry;
17use crate::trash::TrashBackend;
18use crate::vfs::VfsRouter;
19use kaish_vfs::ByteBudget;
20use tokio::sync::oneshot;
21use tokio_util::sync::CancellationToken;
22
23use crate::interpreter::OutputFormat;
24
25use super::traits::ToolSchema;
26
27/// Output context determines how command output should be formatted.
28///
29/// Different contexts prefer different output formats:
30/// - **Interactive** — Pretty columns, colors, traditional tree (TTY/REPL)
31/// - **Piped** — Raw output for pipeline processing
32/// - **Model** — Token-efficient compact formats (MCP server / agent context)
33/// - **Script** — Non-interactive script execution
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
35#[non_exhaustive]
36pub enum OutputContext {
37 /// Interactive TTY/REPL - use human-friendly format with colors.
38 #[default]
39 Interactive,
40 /// Output to another command - use raw output for pipes.
41 Piped,
42 /// MCP server / agent context - use token-efficient model format.
43 Model,
44 /// Non-interactive script - use raw output.
45 Script,
46}
47
48/// Why kaish will not run an external command for this dispatch — distinct
49/// from "not found in PATH", which is a different condition entirely (see
50/// `crate::tools::builtin::spawn::virtual_cwd_error` for that one).
51///
52/// The two variants name different audiences, not just different remedies:
53/// [`Self::NotCompiled`] is a build-time fact only whoever built this binary
54/// can change; [`Self::ConfiguredOff`] is a runtime policy an embedder set
55/// and can change. A caller must not collapse them into one message.
56///
57/// Lives here (not behind `#[cfg(feature = "subprocess")]`) because both
58/// `Kernel::try_execute_external` and `dispatch.rs`'s test-only
59/// `BackendDispatcher::try_external` need it from their `not(subprocess)`
60/// arm too, where the `spawn` module — gated on that same feature — is not
61/// compiled in.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum ExternalCommandsUnavailable {
64 /// This binary was built without the `subprocess` capability.
65 NotCompiled,
66 /// This binary has the capability, but this kernel's configuration
67 /// turned it off.
68 ConfiguredOff,
69}
70
71impl ExternalCommandsUnavailable {
72 /// The condition, named precisely and completely — no remedy. A remedy
73 /// depends on the embedder's own tool surface, which kaish cannot see
74 /// (one caller's fix is "use the other tool"; another's is "reconfigure
75 /// the kernel"), so the message stops at the fact.
76 pub fn condition(self) -> &'static str {
77 match self {
78 Self::NotCompiled => "external commands are not available in this build of the shell",
79 Self::ConfiguredOff => "external commands are disabled on this shell",
80 }
81 }
82}
83
84/// Refuse to run `name` as an external command, exit 127 — the same code
85/// POSIX gives "command not found", since from here it's indistinguishable
86/// to a caller checking only the exit code. The message is what carries the
87/// distinction; see [`ExternalCommandsUnavailable::condition`].
88pub fn external_commands_unavailable_error(name: &str, reason: ExternalCommandsUnavailable) -> ExecResult {
89 ExecResult::failure(127, format!("{name}: {}", reason.condition()))
90}
91
92/// Outcome of attempting to run a command as an external process — what
93/// `Kernel::try_execute_external` and `dispatch.rs`'s test-only
94/// `BackendDispatcher::try_external` return instead of `Option<ExecResult>`.
95///
96/// The distinction matters past this call: only [`Self::NotFound`] means
97/// "keep looking" in the ordinary sense. [`Self::Unavailable`] also means
98/// "keep looking" — a backend-registered tool of the same name is a separate
99/// capability kaish still tries — but the reason must survive that lookup so
100/// the final "nothing claimed this name" message can name it, instead of the
101/// fallthrough re-deriving the wrong "command not found".
102// Without `subprocess`, both spawn sites' stubs only ever produce
103// `Unavailable(NotCompiled)` — `Ran`/`NotFound` are real variants, just
104// unreachable in that build, not leftover code.
105#[cfg_attr(not(feature = "subprocess"), allow(dead_code))]
106pub(crate) enum ExternalCommandOutcome {
107 /// A command was resolved and run; this IS the final result.
108 /// Boxed: `ExecResult` dwarfs the other two variants, and clippy's
109 /// `large_enum_variant` fires on the difference.
110 Ran(Box<ExecResult>),
111 /// Nothing on PATH matches `name`, and no explicit-path form resolved
112 /// either — a genuine "not found" specific to external resolution.
113 NotFound,
114 /// kaish did not attempt a PATH lookup or spawn at all. See
115 /// [`ExternalCommandsUnavailable`] for which of the two reasons.
116 Unavailable(ExternalCommandsUnavailable),
117}
118
119/// Execution context passed to tools.
120///
121/// Provides access to the backend (for file operations and tool dispatch),
122/// scope, and other kernel state.
123pub struct ExecContext {
124 /// Kernel backend for I/O operations.
125 ///
126 /// This is the preferred way to access filesystem operations.
127 /// Use `backend.read()`, `backend.write()`, etc.
128 pub backend: Arc<dyn KernelBackend>,
129 /// Variable scope.
130 pub scope: Scope,
131 /// Current working directory (VFS path).
132 pub cwd: PathBuf,
133 /// Previous working directory (for `cd -`).
134 pub prev_cwd: Option<PathBuf>,
135 /// Standard input for the tool (from a redirect, heredoc, here-string, or
136 /// `ExecuteOptions::stdin`). Bytes-typed (GH #176) so a `< binfile`
137 /// redirect over non-UTF-8 content reaches a byte-aware builtin intact
138 /// instead of erroring at redirect setup; a text-only builtin still
139 /// refuses it loudly when it calls `read_stdin_to_text`.
140 pub stdin: Option<Vec<u8>>,
141 /// Structured data from pipeline (pre-parsed JSON from previous command).
142 /// Tools can check this before parsing stdin to avoid redundant JSON parsing.
143 pub stdin_data: Option<Value>,
144 /// Sideband receiver for the previous stage's structured `.data`, set by the
145 /// concurrent pipeline runner. Resolved lazily via [`Self::resolve_stdin`]
146 /// AFTER the pipe is drained — never pre-read — so a streaming upstream that
147 /// only sends its data after writing the pipe can't deadlock a consumer that
148 /// awaits it. Non-`Clone`, so it's moved on resolve.
149 pub stdin_data_rx: Option<oneshot::Receiver<Option<Value>>>,
150 /// Streaming pipe input (set when this command is in a concurrent pipeline).
151 pub pipe_stdin: Option<PipeReader>,
152 /// Streaming pipe output (set when this command is in a concurrent pipeline).
153 pub pipe_stdout: Option<PipeWriter>,
154 /// Tool schemas for help command.
155 ///
156 /// `Arc<[…]>` rather than `Vec`: the full builtin schema catalog (~70
157 /// entries, each with its own `Vec`s and `String`s) is snapshotted into a
158 /// fresh `ExecContext` at every command dispatch and pipeline/fork child. As
159 /// a `Vec` that was a deep clone of the whole catalog per command; as an
160 /// `Arc<[…]>` it's a refcount bump (GH #48, item 8). Immutable after the
161 /// kernel seeds it, so a shared slice is the right shape.
162 pub tool_schemas: Arc<[ToolSchema]>,
163 /// Tool registry reference (for tools that need to inspect available tools).
164 pub tools: Option<Arc<ToolRegistry>>,
165 /// Job manager for background jobs (optional).
166 pub job_manager: Option<Arc<JobManager>>,
167 /// Kernel stderr stream for real-time error output from pipeline stages.
168 ///
169 /// When set, pipeline stages write stderr here instead of buffering in
170 /// `ExecResult.err`. This allows stderr from all stages to stream to
171 /// the terminal (or other sink) concurrently, matching bash behavior.
172 pub stderr: Option<StderrStream>,
173 /// Position of this command within a pipeline (for stdio decisions).
174 pub pipeline_position: PipelinePosition,
175 /// Whether we're running in interactive (REPL) mode.
176 pub interactive: bool,
177 /// Arm `PR_SET_PDEATHSIG(SIGKILL)` on external commands spawned from this
178 /// context, so a hard-killed kaish process cannot orphan them.
179 ///
180 /// Seeded from `KernelConfig::kill_children_on_parent_death` — read that
181 /// field for the tradeoff and the macOS gap. It lives here, not on the
182 /// `Kernel`, because both external-command spawn sites (`Kernel::
183 /// try_execute_external` and `dispatch.rs`'s `BackendDispatcher`) reach an
184 /// `ExecContext` and only one of them reaches a `Kernel`; one home keeps
185 /// the two `pre_exec` blocks from drifting.
186 ///
187 /// `false` for a stand-alone `ExecContext` built outside a kernel, which
188 /// is the pre-existing behavior.
189 pub kill_children_on_parent_death: bool,
190 /// Command aliases (name → expansion string).
191 pub aliases: HashMap<String, String>,
192 /// Ignore file configuration for file-walking tools.
193 pub ignore_config: IgnoreConfig,
194 /// Output size limit configuration for agent safety.
195 pub output_limit: OutputLimitConfig,
196 /// Whether external command execution is allowed.
197 ///
198 /// When `false`, external commands (PATH lookup, `exec`, `spawn`) are blocked.
199 /// Only kaish builtins and backend-registered tools (MCP) are available.
200 /// A blocked attempt reports [`ExternalCommandsUnavailable::ConfiguredOff`],
201 /// not "command not found".
202 pub allow_external_commands: bool,
203 /// Trash backend for safe file deletion.
204 ///
205 /// Always present when the kernel creates the context (even if `set -o trash`
206 /// is off — the backend exists so `kaish-trash list/restore/empty` work
207 /// regardless of the trash flag).
208 pub trash_backend: Option<Arc<dyn TrashBackend>>,
209 /// Terminal state for job control (interactive mode, Unix only).
210 #[cfg(all(unix, feature = "subprocess"))]
211 pub terminal_state: Option<std::sync::Arc<crate::terminal::TerminalState>>,
212 /// Command dispatcher for re-dispatching through the full resolution chain.
213 ///
214 /// When set (via `Kernel::into_arc()`), builtins like `timeout` can dispatch
215 /// inner commands through the full chain (user tools → builtins → .kai scripts
216 /// → external commands) instead of being limited to `backend.call_tool()`.
217 ///
218 /// `None` when the Kernel was not wrapped via `into_arc()`.
219 pub dispatcher: Option<Arc<dyn crate::dispatch::CommandDispatcher>>,
220 /// Cancellation token for this execution path.
221 ///
222 /// Populated by the kernel at execute entry, then propagated through pipeline
223 /// stages, foreground forks (scatter workers, concurrent pipeline stages,
224 /// `$(...)` cmdsubs), and into spawned external children. When the token
225 /// fires, externals receive SIGTERM/SIGKILL via the `wait_or_kill` helper.
226 ///
227 /// Default for stand-alone `ExecContext` constructors is a fresh, never-fired
228 /// token so non-kernel test contexts behave as before.
229 pub cancel: CancellationToken,
230 /// Per-execution output format override set by a builtin's GlobalFlags
231 /// flatten (e.g. `--json`). The dispatcher reads this after `tool.execute()`
232 /// returns and applies the format via `apply_output_format`.
233 ///
234 /// Builtins set this via `GlobalFlags::apply(ctx)`; external commands
235 /// don't touch it.
236 pub output_format: Option<OutputFormat>,
237
238 /// Shared VFS memory budget for this kernel's `MemoryFs` mounts.
239 ///
240 /// `Arc`-cloned from the owning `Kernel` (or its fork parent) so all
241 /// concurrent execution paths draw from the same pool. `None` means
242 /// unbounded. Populated by `Kernel::assemble` and forwarded through
243 /// `child_for_pipeline` / `fork_inner` so background jobs and scatter
244 /// workers see the same cap as foreground execution.
245 pub vfs_budget: Option<Arc<ByteBudget>>,
246
247 /// The per-execute timeout watchdog, when a script timeout is in effect.
248 ///
249 /// Populated by the kernel at execute entry (alongside `cancel`) and
250 /// shared through `child_for_pipeline` so forks and pipeline stages can
251 /// acquire patient holds against the same script clock. `None` when no
252 /// timeout is configured — `ToolCtx::patient` then returns an inert guard.
253 pub watchdog: Option<Arc<crate::watchdog::Watchdog>>,
254
255 /// Active overlay handle when the kernel was constructed with `overlay: true`.
256 ///
257 /// `Arc`-cloned so forks and pipeline stages share the same transaction.
258 /// `None` when no overlay is active (most kernels).
259 #[cfg(all(feature = "localfs", feature = "overlay"))]
260 pub overlay_handle: Option<Arc<crate::kernel::OverlayHandle>>,
261
262}
263
264/// What the write-model gate chose for a single truncating overwrite.
265#[derive(Debug, Clone, Copy, PartialEq, Eq)]
266pub(crate) enum MutationAction {
267 /// Write now — new file, append, excluded path, or trash off.
268 Proceed,
269 /// Snapshot the prior content to trash, then write.
270 TrashFirst,
271}
272
273/// What a snapshotted overwrite must still find at the target before it
274/// writes — the compare-and-swap expectation `overwrite_checked` enforces.
275///
276/// The trash path already holds the prior bytes (it had to copy them to the
277/// trash), so the expectation compares bytes.
278#[derive(Debug, Clone)]
279#[non_exhaustive]
280pub enum OverwriteExpectation {
281 /// The exact prior bytes, from the trash snapshot.
282 Bytes(Vec<u8>),
283}
284
285/// What each snapshotted target must still look like when the caller writes
286/// it, keyed by resolved path (see `overwrite_checked`).
287///
288/// Every existing target the trash snapshotted appears. A new file, an
289/// append, and an excluded or unsnapshotted path are absent, because none of
290/// them has prior content to lose.
291pub type GateExpectations = std::collections::HashMap<PathBuf, OverwriteExpectation>;
292
293/// Real paths the trash gate skips: host scratch under `/tmp`, where
294/// snapshotting prior content to trash is pointless. Shared by `rm`'s delete
295/// gate (`decide_rm_action`) and the overwrite gate (`decide_mutation_action`)
296/// so the exclusion can't drift between them. `Path::starts_with` is
297/// component-aware, so `/tmp_file` does not match `/tmp`.
298///
299/// Note: kaish's own in-memory VFS mounts (e.g. `/v/blobs`) have `real_path ==
300/// None`, so they are handled by the no-real-path gating path, not here — there
301/// is deliberately no lexical `/v` exclusion. Mount-coverage routing delegates
302/// unclaimed `/v/*` to the embedder's backend, whose *real* content under `/v`
303/// (a real path like `/v/cas/blob.bin`) must keep the trash safety net; a
304/// `/v` prefix exclusion here would silently strip it.
305pub(crate) fn is_trash_excluded(real_path: Option<&Path>) -> bool {
306 matches!(real_path, Some(rp) if rp.starts_with("/tmp"))
307}
308
309/// Decide whether a truncating overwrite snapshots to trash first, mirroring
310/// `rm`'s trash priority. Pure so the decision table is unit-testable in
311/// isolation.
312///
313/// - A non-existent target or an append has nothing to lose → `Proceed`.
314/// - A real path under `/tmp` (host scratch) is excluded (matches `rm`) → `Proceed`.
315/// - `TrashFirst` when trash is on **and** the prior content fits under
316/// `trash_max_size` (a file too big to snapshot can't be backed up, so it
317/// falls through, exactly like `rm`); else `Proceed`.
318///
319/// An overlay/in-memory target has `real_path == None`, so it is *not*
320/// excluded and still snapshots — the protection is about agent-operation
321/// safety, not just real-FS data (Amy, 2026-06-17).
322pub(crate) fn decide_mutation_action(
323 trash_enabled: bool,
324 real_path: Option<&Path>,
325 target_exists: bool,
326 is_append: bool,
327 file_size: u64,
328 trash_max_size: u64,
329) -> MutationAction {
330 if !target_exists || is_append {
331 return MutationAction::Proceed;
332 }
333 if is_trash_excluded(real_path) {
334 return MutationAction::Proceed;
335 }
336 if trash_enabled && file_size <= trash_max_size {
337 return MutationAction::TrashFirst;
338 }
339 // A target too large for the trash is written directly. kaish does not
340 // hold it back: nothing in the kernel decides whether an overwrite is
341 // allowed — an embedder that wants to refuse one reads the plan first.
342 MutationAction::Proceed
343}
344
345/// Overwrite `resolved` with `content`, compare-and-swapping against
346/// `expected` first when there is one. The target's current state is
347/// re-derived and must match, else a concurrent change is a loud conflict —
348/// never a silent clobber. Binary-safe (raw bytes, unlike the `String`-based
349/// `PatchOp` CAS). Shared by the byte-oriented gated builtins via
350/// `ExecContext::overwrite_checked` (`tee`/`write`/`dd`) and directly by
351/// `cp`'s free copy path.
352///
353/// This catches a change between the snapshot and the write. It does not
354/// make the write OS-atomic — a crash mid-write can still truncate (the
355/// atomic write-temp-then-rename primitive is a tracked write-model
356/// residual).
357pub(crate) async fn cas_overwrite(
358 backend: &dyn KernelBackend,
359 resolved: &Path,
360 content: &[u8],
361 expected: Option<&OverwriteExpectation>,
362) -> Result<(), crate::backend::BackendError> {
363 // A re-read or re-digest failure propagates loudly — never
364 // `unwrap_or_default()` to empty bytes, which would false-match an empty
365 // snapshot (silent overwrite) or report a bogus "file changed" for a real
366 // I/O error. A target that vanished since the gate is a change → abort.
367 match expected {
368 Some(OverwriteExpectation::Bytes(exp)) => {
369 let current = backend.read(resolved, None).await?;
370 if current != *exp {
371 return Err(concurrent_change_error(resolved));
372 }
373 }
374 None => {}
375 }
376 backend
377 .write(resolved, content, crate::backend::WriteMode::Overwrite)
378 .await
379}
380
381/// One wording for "somebody else wrote this while the write-model gate was
382/// deciding".
383fn concurrent_change_error(resolved: &Path) -> crate::backend::BackendError {
384 crate::backend::BackendError::InvalidOperation(format!(
385 "{}: changed since the write-model gate checked it (concurrent write); \
386 aborting overwrite",
387 resolved.display()
388 ))
389}
390
391impl ExecContext {
392 /// Create a new execution context with a VFS (uses LocalBackend without tools).
393 ///
394 /// This constructor is for backward compatibility and tests that don't need tool dispatch.
395 /// For full tool support, use `with_vfs_and_tools`.
396 pub fn new(vfs: Arc<VfsRouter>) -> Self {
397 Self {
398 backend: Arc::new(LocalBackend::new(vfs)),
399 scope: Scope::new(),
400 cwd: PathBuf::from("/"),
401 prev_cwd: None,
402 stdin: None,
403 stdin_data: None,
404 stdin_data_rx: None,
405 pipe_stdin: None,
406 pipe_stdout: None,
407 stderr: None,
408 tool_schemas: Vec::new().into(),
409 tools: None,
410 job_manager: None,
411 pipeline_position: PipelinePosition::Only,
412 interactive: false,
413 kill_children_on_parent_death: false,
414 aliases: HashMap::new(),
415 ignore_config: IgnoreConfig::none(),
416 output_limit: OutputLimitConfig::none(),
417 allow_external_commands: true,
418 trash_backend: None,
419 #[cfg(all(unix, feature = "subprocess"))]
420 terminal_state: None,
421 dispatcher: None,
422 cancel: CancellationToken::new(),
423 output_format: None,
424 vfs_budget: None,
425 watchdog: None,
426 #[cfg(all(feature = "localfs", feature = "overlay"))]
427 overlay_handle: None,
428 }
429 }
430
431 /// Create a new execution context with VFS and tool registry.
432 ///
433 /// This is the preferred constructor for full kaish operation where
434 /// tools need to be dispatched through the backend.
435 pub fn with_vfs_and_tools(vfs: Arc<VfsRouter>, tools: Arc<ToolRegistry>) -> Self {
436 Self {
437 backend: Arc::new(LocalBackend::with_tools(vfs, tools.clone())),
438 scope: Scope::new(),
439 cwd: PathBuf::from("/"),
440 prev_cwd: None,
441 stdin: None,
442 stdin_data: None,
443 stdin_data_rx: None,
444 pipe_stdin: None,
445 pipe_stdout: None,
446 stderr: None,
447 tool_schemas: Vec::new().into(),
448 tools: Some(tools),
449 job_manager: None,
450 pipeline_position: PipelinePosition::Only,
451 interactive: false,
452 kill_children_on_parent_death: false,
453 aliases: HashMap::new(),
454 ignore_config: IgnoreConfig::none(),
455 output_limit: OutputLimitConfig::none(),
456 allow_external_commands: true,
457 trash_backend: None,
458 #[cfg(all(unix, feature = "subprocess"))]
459 terminal_state: None,
460 dispatcher: None,
461 cancel: CancellationToken::new(),
462 output_format: None,
463 vfs_budget: None,
464 watchdog: None,
465 #[cfg(all(feature = "localfs", feature = "overlay"))]
466 overlay_handle: None,
467 }
468 }
469
470 /// Create a new execution context with a custom backend.
471 pub fn with_backend(backend: Arc<dyn KernelBackend>) -> Self {
472 Self {
473 backend,
474 scope: Scope::new(),
475 cwd: PathBuf::from("/"),
476 prev_cwd: None,
477 stdin: None,
478 stdin_data: None,
479 stdin_data_rx: None,
480 pipe_stdin: None,
481 pipe_stdout: None,
482 stderr: None,
483 tool_schemas: Vec::new().into(),
484 tools: None,
485 job_manager: None,
486 pipeline_position: PipelinePosition::Only,
487 interactive: false,
488 kill_children_on_parent_death: false,
489 aliases: HashMap::new(),
490 ignore_config: IgnoreConfig::none(),
491 output_limit: OutputLimitConfig::none(),
492 allow_external_commands: true,
493 trash_backend: None,
494 #[cfg(all(unix, feature = "subprocess"))]
495 terminal_state: None,
496 dispatcher: None,
497 cancel: CancellationToken::new(),
498 output_format: None,
499 vfs_budget: None,
500 watchdog: None,
501 #[cfg(all(feature = "localfs", feature = "overlay"))]
502 overlay_handle: None,
503 }
504 }
505
506 /// Create a context with VFS, tools, and a specific scope.
507 pub fn with_vfs_tools_and_scope(vfs: Arc<VfsRouter>, tools: Arc<ToolRegistry>, scope: Scope) -> Self {
508 Self {
509 backend: Arc::new(LocalBackend::with_tools(vfs, tools.clone())),
510 scope,
511 cwd: PathBuf::from("/"),
512 prev_cwd: None,
513 stdin: None,
514 stdin_data: None,
515 stdin_data_rx: None,
516 pipe_stdin: None,
517 pipe_stdout: None,
518 stderr: None,
519 tool_schemas: Vec::new().into(),
520 tools: Some(tools),
521 job_manager: None,
522 pipeline_position: PipelinePosition::Only,
523 interactive: false,
524 kill_children_on_parent_death: false,
525 aliases: HashMap::new(),
526 ignore_config: IgnoreConfig::none(),
527 output_limit: OutputLimitConfig::none(),
528 allow_external_commands: true,
529 trash_backend: None,
530 #[cfg(all(unix, feature = "subprocess"))]
531 terminal_state: None,
532 dispatcher: None,
533 cancel: CancellationToken::new(),
534 output_format: None,
535 vfs_budget: None,
536 watchdog: None,
537 #[cfg(all(feature = "localfs", feature = "overlay"))]
538 overlay_handle: None,
539 }
540 }
541
542 /// Create a context with a specific scope (uses LocalBackend without tools).
543 ///
544 /// For tests that don't need tool dispatch. For full tool support,
545 /// use `with_vfs_tools_and_scope`.
546 pub fn with_scope(vfs: Arc<VfsRouter>, scope: Scope) -> Self {
547 Self {
548 backend: Arc::new(LocalBackend::new(vfs)),
549 scope,
550 cwd: PathBuf::from("/"),
551 prev_cwd: None,
552 stdin: None,
553 stdin_data: None,
554 stdin_data_rx: None,
555 pipe_stdin: None,
556 pipe_stdout: None,
557 stderr: None,
558 tool_schemas: Vec::new().into(),
559 tools: None,
560 job_manager: None,
561 pipeline_position: PipelinePosition::Only,
562 interactive: false,
563 kill_children_on_parent_death: false,
564 aliases: HashMap::new(),
565 ignore_config: IgnoreConfig::none(),
566 output_limit: OutputLimitConfig::none(),
567 allow_external_commands: true,
568 trash_backend: None,
569 #[cfg(all(unix, feature = "subprocess"))]
570 terminal_state: None,
571 dispatcher: None,
572 cancel: CancellationToken::new(),
573 output_format: None,
574 vfs_budget: None,
575 watchdog: None,
576 #[cfg(all(feature = "localfs", feature = "overlay"))]
577 overlay_handle: None,
578 }
579 }
580
581 /// Create a context with a custom backend and scope.
582 pub fn with_backend_and_scope(backend: Arc<dyn KernelBackend>, scope: Scope) -> Self {
583 Self {
584 backend,
585 scope,
586 cwd: PathBuf::from("/"),
587 prev_cwd: None,
588 stdin: None,
589 stdin_data: None,
590 stdin_data_rx: None,
591 pipe_stdin: None,
592 pipe_stdout: None,
593 stderr: None,
594 tool_schemas: Vec::new().into(),
595 tools: None,
596 job_manager: None,
597 pipeline_position: PipelinePosition::Only,
598 interactive: false,
599 kill_children_on_parent_death: false,
600 aliases: HashMap::new(),
601 ignore_config: IgnoreConfig::none(),
602 output_limit: OutputLimitConfig::none(),
603 allow_external_commands: true,
604 trash_backend: None,
605 #[cfg(all(unix, feature = "subprocess"))]
606 terminal_state: None,
607 dispatcher: None,
608 cancel: CancellationToken::new(),
609 output_format: None,
610 vfs_budget: None,
611 watchdog: None,
612 #[cfg(all(feature = "localfs", feature = "overlay"))]
613 overlay_handle: None,
614 }
615 }
616
617 /// Set the available tool schemas (for help command).
618 ///
619 /// Takes a `Vec` for caller convenience and converts to the shared
620 /// `Arc<[…]>` the field stores (see the field docs; GH #48).
621 pub fn set_tool_schemas(&mut self, schemas: Vec<ToolSchema>) {
622 self.tool_schemas = schemas.into();
623 }
624
625 /// Set the tool registry reference.
626 pub fn set_tools(&mut self, tools: Arc<ToolRegistry>) {
627 self.tools = Some(tools);
628 }
629
630 /// Set the job manager for background job tracking.
631 pub fn set_job_manager(&mut self, manager: Arc<JobManager>) {
632 self.job_manager = Some(manager);
633 }
634
635 /// Set the trash backend.
636 pub fn set_trash_backend(&mut self, backend: Arc<dyn TrashBackend>) {
637 self.trash_backend = Some(backend);
638 }
639
640 /// Set stdin for this execution.
641 ///
642 /// An explicit stdin buffer (`< file`, heredoc, here-string, or a pipeline
643 /// hand-off) supersedes any inherited lazy `pipe_stdin`. Since `read_stdin_*`
644 /// prefers `pipe_stdin`, clear it here so redirect precedence holds — a
645 /// `< file` must beat a frontend-seeded piped stdin. Accepts anything
646 /// `Into<Vec<u8>>` — a `String`/`&str` (heredocs, here-strings, most
647 /// callers) or a raw `Vec<u8>` (a `< binfile` redirect, GH #176) both work.
648 pub fn set_stdin(&mut self, stdin: impl Into<Vec<u8>>) {
649 self.stdin = Some(stdin.into());
650 self.pipe_stdin = None;
651 }
652
653 /// Get stdin, consuming it.
654 pub fn take_stdin(&mut self) -> Option<Vec<u8>> {
655 self.stdin.take()
656 }
657
658 /// Set both text stdin and structured data.
659 ///
660 /// Use this when passing output through a pipeline where the previous
661 /// command produced structured data (e.g., JSON from MCP tools). The text
662 /// side is always a genuine `String` here (structured-data hand-off is a
663 /// JSON-producing pipeline stage, never binary).
664 pub fn set_stdin_with_data(&mut self, text: String, data: Option<Value>) {
665 self.stdin = Some(text.into_bytes());
666 self.stdin_data = data;
667 }
668
669 /// Take structured data if available, consuming it.
670 ///
671 /// Tools can use this to avoid re-parsing JSON that was already parsed
672 /// by a previous command in the pipeline.
673 pub fn take_stdin_data(&mut self) -> Option<Value> {
674 self.stdin_data.take()
675 }
676
677 /// Resolve stdin for a builtin that can consume *either* structured `.data`
678 /// or raw text from the previous pipeline stage (jq, scatter, …). Returns
679 /// `(Some(data), _)` when the upstream produced structured data, else
680 /// `(None, text)`.
681 ///
682 /// Ordering matters and is the whole point: the pipe is drained to text
683 /// FIRST, which runs the upstream producer to completion (it can't be parked
684 /// on pipe backpressure), and only THEN is the structured-data sideband
685 /// awaited — by which point the producer has definitely sent it (it sends
686 /// before writing/closing its pipe). A streaming upstream that emits a lot
687 /// of text before sending its (absent) data therefore can't deadlock us, and
688 /// a fast structured producer (`seq`) is no longer lost to a startup race
689 /// that a one-shot `try_recv` used to drop on the floor.
690 pub async fn resolve_stdin(&mut self) -> Result<(Option<Value>, String), String> {
691 // Data set directly on the context (not via the pipeline sideband) wins
692 // and needs no pipe — e.g. a non-pipeline caller seeded `stdin_data`.
693 if let Some(data) = self.stdin_data.take() {
694 return Ok((Some(data), String::new()));
695 }
696 // Drain the pipe (and/or buffered stdin) to text — unblocks the upstream.
697 let text = self.read_stdin_to_text().await?.unwrap_or_default();
698 // Upstream has now finished; its structured data (if any) is waiting.
699 if let Some(rx) = self.stdin_data_rx.take()
700 && let Ok(Some(data)) = rx.await
701 {
702 return Ok((Some(data), text));
703 }
704 Ok((None, text))
705 }
706
707 /// Resolve a path relative to cwd, normalizing `.` and `..` components.
708 pub fn resolve_path(&self, path: &str) -> PathBuf {
709 let raw = if path.starts_with('/') {
710 PathBuf::from(path)
711 } else {
712 self.cwd.join(path)
713 };
714 normalize_path(&raw)
715 }
716
717 /// Change the current working directory.
718 ///
719 /// Saves the old directory for `cd -` support.
720 pub fn set_cwd(&mut self, path: PathBuf) {
721 let previous = self.cwd.clone();
722 self.prev_cwd = Some(previous.clone());
723 self.cwd = path;
724 // `$PWD`/`$OLDPWD` follow the working directory from HERE, the one
725 // place it changes. They used to be whatever the process inherited and
726 // nothing ever wrote them, so `cd /tmp; echo $PWD` reported the
727 // directory the shell started in while `pwd` reported `/tmp` — a wrong
728 // value with nothing to say so, and the validator vouched for the name
729 // because `scope_tracker` lists both as known. `$OLDPWD` was worse than
730 // stale: it held a directory from the INVOKING shell's history.
731 //
732 // Here rather than in the `cd` builtin so a second writer cannot drift
733 // from the first.
734 self.sync_cwd_vars(&previous);
735 }
736
737 /// Write `$PWD`/`$OLDPWD` from the context's own directories.
738 ///
739 /// Kept next to [`Self::set_cwd`] because the kernel also calls it once at
740 /// startup: a script that reads `$PWD` before its first `cd` must not see
741 /// the inherited environment's value either.
742 pub(crate) fn sync_cwd_vars(&mut self, previous: &Path) {
743 self.scope.set_global("PWD", Value::String(self.cwd.to_string_lossy().into_owned()));
744 self.scope.set_global(
745 "OLDPWD",
746 Value::String(previous.to_string_lossy().into_owned()),
747 );
748 }
749
750 /// Get the previous working directory (for `cd -`).
751 pub fn get_prev_cwd(&self) -> Option<&PathBuf> {
752 self.prev_cwd.as_ref()
753 }
754
755 /// Read stdin as text, erroring on non-UTF-8 instead of silently
756 /// lossy-decoding it (which corrupts binary with `U+FFFD`).
757 ///
758 /// The strict counterpart to [`Self::read_stdin_to_bytes`], for text-only
759 /// builtins (`grep`, `sed`, `awk`, `cut`, `sort`, `jq`, …): a binary stream
760 /// is a loud error, not a mangle. Returns `Ok(None)` when there is no stdin
761 /// at all. The `Err` is a ready-to-use message; callers prefix their name.
762 /// See `docs/binary-data.md`.
763 pub async fn read_stdin_to_text(&mut self) -> Result<Option<String>, String> {
764 match self.read_stdin_to_bytes().await? {
765 None => Ok(None),
766 Some(bytes) => String::from_utf8(bytes).map(Some).map_err(|_| {
767 "input is not valid UTF-8 (binary data?) — pipe through base64/xxd \
768 or use a binary-aware tool (cat, dd, cmp, wc -c)"
769 .to_string()
770 }),
771 }
772 }
773
774 /// Read all of stdin as raw bytes, preserving binary intact.
775 ///
776 /// The byte-clean counterpart to [`Self::read_stdin_to_text`], for
777 /// binary-aware builtins (`base64`, `xxd`, `checksum`, `wc -c`, `cmp`, …).
778 /// Returns `Ok(None)` when there is no stdin at all (no pipe and no
779 /// buffer); an empty pipe yields `Ok(Some(vec![]))`. The buffered source is
780 /// already bytes-typed (GH #176), so this is a plain move, never a
781 /// re-encode. See `docs/binary-data.md`.
782 ///
783 /// Anything an earlier [`Self::read_stdin_line`] left behind comes first,
784 /// then the rest of the pipe — `read x; cat` gives `cat` everything after
785 /// the line `read` took, in order, and nothing twice.
786 ///
787 /// A failed pipe read is `Err`, never `Ok(None)`. "The pipe broke" and
788 /// "there was no stdin" are different facts, and collapsing them hands the
789 /// builtin a short read dressed up as empty input — `wc` would report 0
790 /// lines and exit 0 on a stream that died halfway, and the bytes already
791 /// read would go with it. [`Self::read_stdin_line`] propagates this same
792 /// error from this same reader; the two now agree. The `Err` is a
793 /// ready-to-use message; callers prefix their name.
794 pub async fn read_stdin_to_bytes(&mut self) -> Result<Option<Vec<u8>>, String> {
795 let leftover = self.stdin.take();
796 match self.pipe_stdin.take() {
797 Some(mut reader) => {
798 use tokio::io::AsyncReadExt;
799 let mut buf = leftover.unwrap_or_default();
800 reader
801 .read_to_end(&mut buf)
802 .await
803 .map_err(|e| format!("reading stdin: {e}"))?;
804 Ok(Some(buf))
805 }
806 None => Ok(leftover),
807 }
808 }
809
810 /// Read one line from stdin, leaving the rest for the next reader.
811 ///
812 /// This is the stream-shaped counterpart to [`Self::read_stdin_to_bytes`]:
813 /// it takes a single line and keeps everything after it, so `read x; read y`
814 /// binds two lines and `read x; cat` hands `cat` the remainder. Draining to
815 /// EOF for one line would discard the rest of the stream — there is no way
816 /// to put it back once a pipe has been read.
817 ///
818 /// The trailing newline is stripped, and a final line without one is still
819 /// a line. Returns `Ok(None)` at end of input — no line left, which is a
820 /// fact the caller reports, not an empty binding. `Err` on non-UTF-8, with
821 /// the same message shape as [`Self::read_stdin_to_text`].
822 pub async fn read_stdin_line(&mut self) -> Result<Option<String>, String> {
823 loop {
824 // A complete line already buffered? Take it and keep the rest.
825 if let Some(buf) = self.stdin.as_mut()
826 && let Some(nl) = buf.iter().position(|b| *b == b'\n')
827 {
828 let rest = buf.split_off(nl + 1);
829 let mut line = std::mem::replace(buf, rest);
830 line.pop(); // the '\n' itself
831 if line.last() == Some(&b'\r') {
832 line.pop();
833 }
834 // Drop an emptied buffer only when nothing can refill it, so
835 // `read_stdin_to_bytes` can still tell "no stdin" (None) from
836 // "stdin that is now empty" (Some(vec![])).
837 if self.pipe_stdin.is_none()
838 && self.stdin.as_ref().is_some_and(|b| b.is_empty())
839 {
840 self.stdin = None;
841 }
842 return decode_stdin_line(line).map(Some);
843 }
844
845 // No newline buffered — pull another chunk from the pipe. The
846 // reader stays in place: taking it would strand the remainder.
847 if let Some(reader) = self.pipe_stdin.as_mut() {
848 use tokio::io::AsyncReadExt;
849 let mut chunk = [0u8; 8192];
850 match reader.read(&mut chunk).await {
851 Ok(0) => {
852 self.pipe_stdin = None; // EOF; fall through to the tail
853 }
854 Ok(n) => {
855 self.stdin
856 .get_or_insert_with(Vec::new)
857 .extend_from_slice(&chunk[..n]);
858 }
859 Err(e) => return Err(format!("reading stdin: {e}")),
860 }
861 continue;
862 }
863
864 // Nothing left to read: whatever is buffered is the last line.
865 return match self.stdin.take() {
866 Some(buf) if !buf.is_empty() => decode_stdin_line(buf).map(Some),
867 _ => Ok(None),
868 };
869 }
870 }
871
872 /// Create a child context for a pipeline stage.
873 ///
874 /// Shares backend, tools, job_manager, aliases, cwd, and scope
875 /// but has independent stdin/stdout pipes.
876 pub fn child_for_pipeline(&self) -> Self {
877 Self {
878 backend: self.backend.clone(),
879 scope: self.scope.clone(),
880 cwd: self.cwd.clone(),
881 prev_cwd: self.prev_cwd.clone(),
882 stdin: None,
883 stdin_data: None,
884 stdin_data_rx: None,
885 pipe_stdin: None,
886 pipe_stdout: None,
887 stderr: self.stderr.clone(),
888 tool_schemas: self.tool_schemas.clone(),
889 tools: self.tools.clone(),
890 job_manager: self.job_manager.clone(),
891 pipeline_position: PipelinePosition::Only,
892 interactive: self.interactive,
893 kill_children_on_parent_death: self.kill_children_on_parent_death,
894 aliases: self.aliases.clone(),
895 ignore_config: self.ignore_config.clone(),
896 output_limit: self.output_limit.clone(),
897 allow_external_commands: self.allow_external_commands,
898 trash_backend: self.trash_backend.clone(),
899 #[cfg(all(unix, feature = "subprocess"))]
900 terminal_state: self.terminal_state.clone(),
901 dispatcher: self.dispatcher.clone(),
902 cancel: self.cancel.clone(),
903 // Output format is per-execution; child pipeline stages start fresh.
904 output_format: None,
905 // Budget is shared: the child draws from the same pool as the parent.
906 vfs_budget: self.vfs_budget.clone(),
907 // Watchdog is shared: a patient hold in a pipeline stage or fork
908 // suspends the same script clock as foreground execution.
909 watchdog: self.watchdog.clone(),
910 // Overlay handle is shared: pipeline stages share the same transaction.
911 #[cfg(all(feature = "localfs", feature = "overlay"))]
912 overlay_handle: self.overlay_handle.clone(),
913 }
914 }
915
916 /// Build an `IgnoreFilter` from the current ignore configuration.
917 ///
918 /// Returns `None` if no filtering is configured.
919 pub async fn build_ignore_filter(&self, root: &std::path::Path) -> Option<crate::walker::IgnoreFilter> {
920 use crate::backend_walker_fs::BackendWalkerFs;
921 let fs = BackendWalkerFs(self.backend.as_ref());
922 self.ignore_config.build_filter(root, &fs).await
923 }
924
925 /// Snapshot a batch of truncating overwrites into the trash, the way `rm`
926 /// snapshots deletes — so `tee`/`patch`/`sed -i` can't clobber a file
927 /// under `set -o trash` without leaving a recoverable prior copy.
928 ///
929 /// Each target is `(display_path, is_append)`. A path that doesn't exist
930 /// yet or is an append has nothing to lose and passes. For an existing
931 /// file under `set -o trash`, the prior content is copied to trash first
932 /// (via `trash_bytes`) so it's recoverable; the file is left in place for
933 /// the caller to overwrite. With trash off, every target passes: the
934 /// kernel does not decide whether an overwrite is allowed.
935 ///
936 /// `Ok(snapshots)` means every snapshot is done and the caller may write
937 /// all targets; `snapshots` maps each trash-snapshotted target's resolved
938 /// path to its prior bytes, so a byte-oriented caller can pass them as the
939 /// `expected` to `overwrite_checked` for a binary-safe compare-and-swap.
940 /// `Err(result)` is what the caller must return verbatim — a trash failure
941 /// is an error, never a fall-through to a destructive overwrite.
942 // `ExecResult` IS the error here — `Err(result)` is what the caller
943 // returns verbatim, which is the point of the signature. Boxing it to
944 // satisfy `result_large_err` would put a `*deref` at every call site to
945 // save an allocation on a path that already reads and copies file bytes.
946 #[allow(clippy::result_large_err)]
947 pub async fn snapshot_overwrites(
948 &mut self,
949 command: &str,
950 targets: &[(String, bool)],
951 ) -> Result<GateExpectations, ExecResult> {
952 let mut expectations = GateExpectations::new();
953 let trash_enabled = self.scope.trash_enabled();
954 // Fast path: nothing is trashed, so this costs one branch and
955 // allocates nothing.
956 if !trash_enabled {
957 return Ok(expectations);
958 }
959 let trash_max_size = self.scope.trash_max_size();
960
961 struct Decided {
962 display: String,
963 resolved: PathBuf,
964 action: MutationAction,
965 }
966 // Dedup by resolved path (keep first): a multi-file patch with an
967 // explicit target lists the same file once per hunk-group, and we must
968 // not snapshot it N times or list it N times in the request.
969 let mut seen = std::collections::HashSet::new();
970 let mut decided = Vec::with_capacity(targets.len());
971 for (display, is_append) in targets {
972 let resolved = self.resolve_path(display);
973 if !seen.insert(resolved.clone()) {
974 continue;
975 }
976 // `real` is used only for the exclusion decision (/tmp, /v); the
977 // snapshot reads bytes through the backend, not the real path.
978 let real = self.backend.resolve_real_path(Path::new(&resolved));
979 let exists = self.backend.exists(Path::new(&resolved)).await;
980 // Prior size decides trash eligibility (a file too big to snapshot
981 // can't be backed up). Only stat an existing target.
982 let size = if exists {
983 self.backend
984 .stat(Path::new(&resolved))
985 .await
986 .map(|e| e.size)
987 .unwrap_or(0)
988 } else {
989 0
990 };
991 let action = decide_mutation_action(
992 trash_enabled,
993 real.as_deref(),
994 exists,
995 *is_append,
996 size,
997 trash_max_size,
998 );
999 decided.push(Decided {
1000 display: display.clone(),
1001 resolved,
1002 action,
1003 });
1004 }
1005
1006 // Snapshot prior content for every trash-first target before any write,
1007 // keeping the bytes so a byte-oriented caller can CAS against them.
1008 for d in &decided {
1009 if matches!(d.action, MutationAction::TrashFirst) {
1010 match self.snapshot_for_overwrite(&d.display, &d.resolved).await {
1011 Ok(bytes) => {
1012 expectations.insert(d.resolved.clone(), OverwriteExpectation::Bytes(bytes));
1013 }
1014 Err(e) => return Err(ExecResult::failure(1, format!("{command}: {e}"))),
1015 }
1016 }
1017 }
1018 Ok(expectations)
1019 }
1020
1021 /// Copy the prior content of `resolved` into the trash before it's
1022 /// overwritten, returning those bytes for the caller's compare-and-swap.
1023 ///
1024 /// We **copy** (not move): the builtin overwrites the file in place next,
1025 /// and read-modify-write callers (`patch`, `sed -i`) still need to read it —
1026 /// the file keeps its identity, only its content changes. (`rm` *moves*
1027 /// because removal is the op; an overwrite backs up the prior bytes.) Reads
1028 /// through the backend so a real, overlay, or in-memory file is handled the
1029 /// same way. A missing trash backend or a trash failure is an error — never
1030 /// a silent fall-through to a destructive overwrite.
1031 async fn snapshot_for_overwrite(
1032 &self,
1033 display: &str,
1034 resolved: &Path,
1035 ) -> Result<Vec<u8>, String> {
1036 let trash = self
1037 .trash_backend
1038 .as_ref()
1039 .ok_or_else(|| "trash backend not available".to_string())?;
1040 let bytes = self
1041 .backend
1042 .read(resolved, None)
1043 .await
1044 .map_err(|e| format!("{display}: {e}"))?;
1045 trash
1046 .trash_bytes(Path::new(display), &bytes)
1047 .await
1048 .map_err(|e| format!("{display}: trash failed: {e}"))?;
1049 Ok(bytes)
1050 }
1051
1052 /// Overwrite `resolved` with `content`. When `expected` is `Some`, this is a
1053 /// binary-safe compare-and-swap: the current bytes are re-read and must
1054 /// equal `expected` (the gate's snapshot), else it errors — a concurrent
1055 /// change since the gate is a loud conflict, never a silent clobber. Unlike
1056 /// the `String`-based `PatchOp::Replace` CAS used by `patch`/`sed -i`, this
1057 /// operates on raw bytes, so binary overwrites (`tee`, `write`, `dd`, `cp`,
1058 /// `mv`) keep the same protection. It is *not* OS-atomic — a crash mid-write
1059 /// can still truncate; the atomic write-temp-then-rename primitive remains a
1060 /// tracked write-model residual.
1061 pub(crate) async fn overwrite_checked(
1062 &self,
1063 resolved: &Path,
1064 content: &[u8],
1065 expected: Option<&OverwriteExpectation>,
1066 ) -> Result<(), String> {
1067 cas_overwrite(&*self.backend, resolved, content, expected)
1068 .await
1069 .map_err(|e| e.to_string())
1070 }
1071
1072 /// Expand a glob pattern to matching file paths.
1073 ///
1074 /// Returns the matched paths (absolute). Used by builtins that accept glob
1075 /// patterns in their path arguments (ls, cat, head, tail, wc, etc.).
1076 pub async fn expand_glob(&self, pattern: &str) -> Result<Vec<PathBuf>, String> {
1077 use crate::backend_walker_fs::BackendWalkerFs;
1078 use crate::walker::{EntryTypes, FileWalker, GlobPath, WalkOptions};
1079
1080 let glob = GlobPath::new(pattern).map_err(|e| format!("invalid pattern: {}", e))?;
1081
1082 let root = if glob.is_anchored() {
1083 self.resolve_path("/")
1084 } else {
1085 self.resolve_path(".")
1086 };
1087
1088 let options = WalkOptions {
1089 entry_types: EntryTypes::all(),
1090 respect_gitignore: self.ignore_config.auto_gitignore(),
1091 ..WalkOptions::default()
1092 };
1093
1094 let fs = BackendWalkerFs(self.backend.as_ref());
1095 let mut walker = FileWalker::new(&fs, &root)
1096 .with_pattern(glob)
1097 .with_options(options);
1098
1099 // Note: if ignore_files contains ".gitignore" AND auto_gitignore is true,
1100 // the root .gitignore is loaded twice (once here, once by the walker).
1101 // This is harmless — merge is additive and rules are idempotent.
1102 if let Some(filter) = self.ignore_config.build_filter(&root, &fs).await {
1103 walker = walker.with_ignore(filter);
1104 }
1105
1106 walker.collect().await.map_err(|e| e.to_string())
1107 }
1108
1109 /// Expand positional arguments, resolving glob patterns to relative paths.
1110 ///
1111 /// Used by file-processing builtins (cat, head, tail, wc) that accept
1112 /// glob patterns in their path arguments. Non-string values are converted
1113 /// to strings (matching shell conventions).
1114 ///
1115 /// A `Value::Bytes` operand goes LOUD (GH #93 item 1), and `Value::Json`
1116 /// (list/record), `Value::Bool`, and `Value::Null` operands go LOUD too
1117 /// (GH #121) — none is silently dropped by a catch-all anymore. Every
1118 /// caller here falls back to reading stdin (or a generic "missing path"
1119 /// error) when the path list comes back empty, so a structured, bool, or
1120 /// null path used to vanish into a wrong data source instead of erroring.
1121 /// The match is exhaustive over all 7 `Value` variants on purpose: a
1122 /// future new variant fails to compile here until handled, rather than
1123 /// silently falling through a wildcard arm.
1124 pub async fn expand_paths(&self, positional: &[Value]) -> Result<Vec<String>, String> {
1125 let mut paths = Vec::new();
1126 for arg in positional {
1127 let s = match arg {
1128 Value::String(s) => s.clone(),
1129 Value::Int(n) => n.to_string(),
1130 Value::Float(f) => f.to_string(),
1131 Value::Bytes(_) => {
1132 crate::interpreter::value_to_text_sink_named(arg, "a path").map_err(|e| e.to_string())?
1133 }
1134 Value::Json(_) => {
1135 return Err(crate::interpreter::structured_boundary_error("a path", arg)
1136 .unwrap_or_else(|| "cannot use this value as a path".to_string()));
1137 }
1138 Value::Bool(b) => return Err(format!("cannot use a bool ({b}) as a path")),
1139 Value::Null => return Err("cannot use null as a path".to_string()),
1140 };
1141 if crate::glob::contains_glob(&s) {
1142 let expanded = self.expand_glob(&s).await?;
1143 // An absolute pattern reports absolute paths. Stripping the
1144 // cwd unconditionally is a no-op only while cwd is a real
1145 // prefix; when cwd is `/` the strip removes the leading
1146 // separator itself and turns `/tmp/a.txt` into `tmp/a.txt`.
1147 // `/` is the default cwd for an isolated kernel, so that is
1148 // the common case for an embedder, not a corner. Same guard
1149 // the kernel's own argv expansion uses.
1150 let absolute = s.starts_with('/');
1151 let root = self.resolve_path(".");
1152 for p in expanded {
1153 if absolute {
1154 paths.push(p.to_string_lossy().to_string());
1155 } else {
1156 let rel = p.strip_prefix(&root).unwrap_or(&p);
1157 paths.push(rel.to_string_lossy().to_string());
1158 }
1159 }
1160 } else {
1161 paths.push(s);
1162 }
1163 }
1164 Ok(paths)
1165 }
1166
1167 /// Default chunk size for forward file scans. Bounds the memory a
1168 /// scan-oriented builtin holds at once, independent of file size.
1169 pub const STREAM_CHUNK_SIZE: u64 = 256 * 1024;
1170
1171 /// Stream a file's bytes forward in `chunk_size` slices, handing each
1172 /// non-empty chunk to `f`.
1173 ///
1174 /// Reads are issued as positional `read_range` requests, so backends slice
1175 /// without materialising the whole file (LocalFs seeks; MemoryFs/OverlayFs
1176 /// slice their stored bytes). The loop terminates on the first empty chunk,
1177 /// which every backend returns once the offset reaches EOF. `f` returns a
1178 /// [`ControlFlow`](std::ops::ControlFlow): `Break` stops the loop early
1179 /// (e.g. a consumer that has detected binary content and will discard the
1180 /// rest), so we don't keep reading a file the caller is done with. This is
1181 /// the shared engine for scan-oriented builtins (`wc`, `checksum`, `grep`)
1182 /// that walk a file front-to-back and must not hold it all in memory.
1183 pub async fn read_file_chunked<F>(
1184 &self,
1185 path: &std::path::Path,
1186 chunk_size: u64,
1187 mut f: F,
1188 ) -> kaish_types::backend::BackendResult<()>
1189 where
1190 F: FnMut(&[u8]) -> std::ops::ControlFlow<()>,
1191 {
1192 use kaish_types::ReadRange;
1193 let mut offset = 0u64;
1194 loop {
1195 let chunk = self
1196 .backend
1197 .read(path, Some(ReadRange::bytes(offset, chunk_size)))
1198 .await?;
1199 if chunk.is_empty() {
1200 break;
1201 }
1202 offset += chunk.len() as u64;
1203 if f(&chunk).is_break() {
1204 break;
1205 }
1206 }
1207 Ok(())
1208 }
1209}
1210
1211/// The kernel's full execution context satisfies the trimmed portable
1212/// [`ToolCtx`](kaish_tool_api::ToolCtx) contract that out-of-tree tools see.
1213///
1214/// Trusted in-tree builtins recover the concrete `ExecContext` (job control,
1215/// pipes, dispatcher) through
1216/// [`ToolCtx::as_any_mut`](kaish_tool_api::ToolCtx::as_any_mut).
1217#[async_trait]
1218impl kaish_tool_api::ToolCtx for ExecContext {
1219 fn backend(&self) -> &Arc<dyn KernelBackend> {
1220 &self.backend
1221 }
1222
1223 fn cwd(&self) -> &std::path::Path {
1224 self.cwd.as_path()
1225 }
1226
1227 fn resolve_path(&self, path: &str) -> PathBuf {
1228 // Inherent methods shadow trait methods in call syntax, so the
1229 // fully-qualified inherent call here is not recursive.
1230 ExecContext::resolve_path(self, path)
1231 }
1232
1233 fn var(&self, name: &str) -> Option<Value> {
1234 self.scope.get(name).cloned()
1235 }
1236
1237 fn set_var(&mut self, name: &str, value: Value) {
1238 self.scope.set(name, value);
1239 }
1240
1241 fn set_output_format(&mut self, format: OutputFormat) {
1242 self.output_format = Some(format);
1243 }
1244
1245 fn patient(&self, budget: std::time::Duration) -> kaish_tool_api::PatientGuard {
1246 match &self.watchdog {
1247 Some(watchdog) => kaish_tool_api::PatientGuard::held(Box::new(watchdog.hold(budget))),
1248 None => kaish_tool_api::PatientGuard::inert(),
1249 }
1250 }
1251
1252 fn as_any(&self) -> &dyn std::any::Any {
1253 self
1254 }
1255
1256 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
1257 self
1258 }
1259}
1260
1261/// Decode one line of stdin as UTF-8, refusing binary rather than mangling it.
1262///
1263/// Same rule and same wording as [`ExecContext::read_stdin_to_text`] — a
1264/// line-at-a-time reader must not be the one place where `U+FFFD` creeps in.
1265fn decode_stdin_line(bytes: Vec<u8>) -> Result<String, String> {
1266 String::from_utf8(bytes).map_err(|_| {
1267 "input is not valid UTF-8 (binary data?) — pipe through base64/xxd \
1268 or use a binary-aware tool (cat, dd, cmp, wc -c)"
1269 .to_string()
1270 })
1271}
1272
1273/// Normalize a path by resolving `.` and `..` components lexically (no filesystem access).
1274fn normalize_path(path: &std::path::Path) -> PathBuf {
1275 let mut parts: Vec<Component> = Vec::new();
1276 for component in path.components() {
1277 match component {
1278 Component::CurDir => {} // skip `.`
1279 Component::ParentDir => {
1280 // Pop the last normal component, but don't pop past root
1281 if let Some(Component::Normal(_)) = parts.last() {
1282 parts.pop();
1283 } else {
1284 parts.push(component);
1285 }
1286 }
1287 _ => parts.push(component),
1288 }
1289 }
1290 if parts.is_empty() {
1291 PathBuf::from("/")
1292 } else {
1293 parts.iter().collect()
1294 }
1295}
1296
1297#[cfg(test)]
1298mod tests {
1299 use super::{decide_mutation_action, MutationAction};
1300 use std::path::Path;
1301
1302 fn decide(
1303 trash: bool,
1304 real: Option<&str>,
1305 exists: bool,
1306 append: bool,
1307 ) -> MutationAction {
1308 // Default to a small file well under the cap; the size-cap behavior
1309 // has its own dedicated test below.
1310 decide_mutation_action(trash, real.map(Path::new), exists, append, 1, 10_000_000)
1311 }
1312
1313 #[test]
1314 fn new_file_and_append_always_proceed() {
1315 // Non-existent target: nothing to lose.
1316 assert_eq!(decide(true, Some("/work/new"), false, false), MutationAction::Proceed);
1317 // Append to an existing file doesn't destroy prior content.
1318 assert_eq!(decide(true, Some("/work/log"), true, true), MutationAction::Proceed);
1319 }
1320
1321 #[test]
1322 fn an_existing_file_is_snapshotted_before_it_is_overwritten() {
1323 assert_eq!(decide(true, Some("/work/f"), true, false), MutationAction::TrashFirst);
1324 }
1325
1326 #[test]
1327 fn trash_off_proceeds() {
1328 assert_eq!(decide(false, Some("/work/f"), true, false), MutationAction::Proceed);
1329 }
1330
1331 #[test]
1332 fn tmp_is_excluded_but_a_real_v_path_is_still_trashed() {
1333 // /tmp scratch proceeds even with trash on (matches rm).
1334 assert_eq!(decide(true, Some("/tmp/scratch"), true, false), MutationAction::Proceed);
1335 // A *real* path under /v is NOT excluded: mount-coverage routing
1336 // delegates unclaimed /v/* to the embedder's backend, so its real
1337 // content under /v keeps the trash safety net.
1338 assert_eq!(decide(true, Some("/v/cas/blob.bin"), true, false), MutationAction::TrashFirst);
1339 }
1340
1341 #[test]
1342 fn file_too_big_to_trash_is_written_directly_like_rm() {
1343 // Prior content larger than the cap can't be snapshotted, so trash is
1344 // skipped and the overwrite proceeds unbacked. Nothing holds it back.
1345 let big = 100u64;
1346 let cap = 10u64;
1347 assert_eq!(
1348 decide_mutation_action(true, Some(Path::new("/work/f")), true, false, big, cap),
1349 MutationAction::Proceed
1350 );
1351 // Exactly at the cap still trashes (inclusive bound, matches rm).
1352 assert_eq!(
1353 decide_mutation_action(true, Some(Path::new("/work/f")), true, false, cap, cap),
1354 MutationAction::TrashFirst
1355 );
1356 }
1357}