Skip to main content

Kernel

Struct Kernel 

Source
pub struct Kernel { /* private fields */ }
Expand description

The Kernel (核) — executes kaish code.

This is the primary interface for running kaish commands. It owns all the runtime state: variables, tools, VFS, jobs, and persistence.

Implementations§

Source§

impl Kernel

Source

pub fn new(config: KernelConfig) -> Result<Self>

Create a new kernel with the given configuration.

Source

pub fn transient() -> Result<Self>

Create a transient kernel (no persistence).

Source

pub fn with_backend( backend: Arc<dyn KernelBackend>, config: KernelConfig, configure_vfs: impl FnOnce(&mut VfsRouter), ) -> Result<Self>

Create a kernel with a custom backend and /v/* virtual path support.

This is the constructor for embedding kaish in other systems that provide their own storage backend (e.g., CRDT-backed storage in kaijutsu).

A VirtualOverlayBackend routes paths automatically:

  • /v/* → Internal VFS (JobFs at /v/jobs, MemoryFs at /v/blobs)
  • Everything else → Your custom backend

The optional configure_vfs closure lets you add additional virtual mounts (e.g., /v/docs for CRDT blocks) after the built-in mounts are set up.

Note: The config’s vfs_mode is ignored — all non-/v/* path routing is handled by your custom backend. The config is only used for name, cwd, skip_validation, and interactive.

§Example
// Simple: default /v/* mounts only
let kernel = Kernel::with_backend(backend, config, |_| {})?;

// With custom mounts
let kernel = Kernel::with_backend(backend, config, |vfs| {
    vfs.mount_arc("/v/docs", docs_fs);
    vfs.mount_arc("/v/g", git_fs);
})?;
Source

pub fn name(&self) -> &str

Get the kernel name.

Source

pub fn init_terminal(&mut self)

Initialize terminal state for interactive job control.

Call this after kernel creation when running as an interactive REPL and stdin is a TTY. Sets up process groups and signal handling.

Source

pub fn cancel(&self)

Cancel the current execution.

This cancels the current cancellation token, causing any execution loop to exit at the next checkpoint with exit code 130 (SIGINT). A fresh token is installed for the next execute() call.

Source

pub fn is_cancelled(&self) -> bool

Check if the current execution has been cancelled.

Source

pub async fn execute(&self, input: &str) -> Result<ExecResult>

Execute kaish source code.

Returns the result of the last statement executed.

Source

pub async fn execute_streaming( &self, input: &str, on_output: &mut dyn FnMut(&ExecResult), ) -> Result<ExecResult>

Execute kaish source code with a per-statement callback.

Each statement’s result is passed to on_output as it completes, allowing callers to flush output incrementally (e.g., print builtin output immediately rather than buffering until the script finishes).

External commands in interactive mode already stream to the terminal via Stdio::inherit(), so the callback mainly handles builtins.

Source

pub async fn get_var(&self, name: &str) -> Option<Value>

Get a variable value.

Source

pub async fn set_var(&self, name: &str, value: Value)

Set a variable value.

Source

pub async fn set_positional( &self, script_name: impl Into<String>, args: Vec<String>, )

Set positional parameters ($0 script name and $1-$9 args).

Source

pub async fn list_vars(&self) -> Vec<(String, Value)>

List all variables.

Source

pub async fn cwd(&self) -> PathBuf

Get current working directory.

Source

pub async fn set_cwd(&self, path: PathBuf)

Set current working directory.

Source

pub async fn last_result(&self) -> ExecResult

Get the last result ($?).

Source

pub async fn has_function(&self, name: &str) -> bool

Check if a user-defined function exists.

Source

pub fn tool_schemas(&self) -> Vec<ToolSchema>

Get available tool schemas.

Source

pub fn jobs(&self) -> Arc<JobManager>

Get job manager.

Source

pub fn vfs(&self) -> Arc<VfsRouter>

Get VFS router.

Source

pub async fn reset(&self) -> Result<()>

Reset kernel to initial state.

Clears in-memory variables and resets cwd to root. History is not cleared (it persists across resets).

Source

pub async fn shutdown(self) -> Result<()>

Shutdown the kernel.

Trait Implementations§

Source§

impl CommandDispatcher for Kernel

Source§

fn dispatch<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, cmd: &'life1 Command, ctx: &'life2 mut ExecContext, ) -> Pin<Box<dyn Future<Output = Result<ExecResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Dispatch a command through the Kernel’s full resolution chain.

This is the single path for all command execution when called from the pipeline runner. It provides the full dispatch chain: user tools → builtins → .kai scripts → external commands → backend tools.

Auto Trait Implementations§

§

impl !Freeze for Kernel

§

impl !RefUnwindSafe for Kernel

§

impl Send for Kernel

§

impl Sync for Kernel

§

impl Unpin for Kernel

§

impl UnsafeUnpin for Kernel

§

impl !UnwindSafe for Kernel

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

Source§

type Proj<U: 'src> = U

Source§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more