Skip to main content

Module wrap

Module wrap 

Source
Expand description

ai-memory wrap <agent> — cross-platform Rust replacement for the shell wrappers PR-1 of issue #487 shipped in the integration recipes.

§What it does

  1. Calls cli::boot::run in-process, capturing its stdout into a buffer. No subprocess; no shell. The --no-boot flag skips this step so a misconfigured DB path doesn’t block the agent.
  2. Builds a system-context string of the form <preamble>\n\n<boot output> where the preamble explains to the downstream agent that it has ai-memory access.
  3. Spawns the wrapped agent (std::process::Command) with the system-context delivered via the chosen strategy:
    • SystemFlag<agent> <flag> "<system_msg>" <trailing args...>
    • SystemEnv<env_name>=<system_msg> <agent> <trailing args...>
    • MessageFile — write <system_msg> to a NamedTempFile, pass <flag> <tempfile_path> to the agent, drop the tempfile on exit so it is cleaned up by the OS.
    • Auto — resolved at runtime from a built-in lookup table (default_strategy).
  4. Forwards the parent’s stdin / stdout / stderr unmodified (Stdio::inherit).
  5. Returns the wrapped agent’s exit code as the wrap subcommand’s exit code, so wrappers compose cleanly with shell pipelines and CI gates that branch on $?.

§Why Rust, not bash + PowerShell

The user directive on issue #487 PR-6 was: implementation should be predominantly Rust with config hooks. PR-1 shipped per-recipe bash and PowerShell wrappers, which doubled the maintenance surface and couldn’t run in restricted Windows / containerized environments without a shell. A single cross-platform Rust subcommand eliminates both problems — it’s the same code path on macOS / Linux / Windows / Docker / Kubernetes / Nix / etc.

§Lookup table

crate::llm_cli_wrap::default_strategy resolves the unflagged form ai-memory wrap <agent> -- <args> to the right delivery mechanism for the agents we can identify by name today. Unknown agents fall through to --system <msg> because that’s the most common contract across OpenAI-compatible CLIs. Future PRs (notably PR-7) can extend the table by adding match arms.

§Substrate split (#1183)

The per-CLI-binary WrapStrategy enum + the per-vendor table live in crate::llm_cli_wrap, adjacent to crate::llm’s alias tables, so the per-vendor substrate has one home per concern (HTTP wire shape in llm.rs, CLI ABI in llm_cli_wrap.rs). The CLI-binary-name detection logic that PICKS a WrapStrategy stays HERE because it’s CLI-specific (clap WrapArgs overrides → table fallback).

Structs§

WrapArgs
Args for ai-memory wrap. Designed so the simplest form (ai-memory wrap codex -- "hello") just works — every flag has a defaulted value or the lookup table fills it in.

Functions§

os_str_to_string_lossy
Public helper for callers (tests + future PR-7 recipe additions) that want to format an OsStr argv element back to UTF-8 for assertions / logging. Falls back to the lossy form so platforms with non-UTF-8 paths don’t panic.
run
ai-memory wrap entry point. Returns the wrapped agent’s exit code so daemon_runtime can std::process::exit(code) on a non-zero outcome — that’s how shell pipelines and CI gates branch on the agent’s success.