Skip to main content

Module session

Module session 

Source
Expand description

Harness construction + session-storage wiring. Mirrors the Rust-side equivalent of the TS packages/coding-agent/src/core/sdk.ts (createAgentSession) — build the env, tools, durable session storage, and AgentHarnessOptions, then AgentHarness::create.

v1 scope cuts vs the TS SDK (tracked in docs/m6-cli-open-questions.md):

  • Skill / prompt-template / context-file discovery IS wired (--no-skills/-ns, --no-prompt-templates/-np, --no-context-files/ -nc each suppress one channel; project .pi/<sub> + global agent_dir()<sub> discovery with project-wins dedupe via crate::resource_dirs; SYSTEM.md/APPEND_SYSTEM.md project-wins precedence). Extension resources_discover (B5b) feeds the SAME loaders: a plugin’s discovered skill/prompt paths merge with the static dirs and re-run through load_skills/load_prompt_templates (individual .md files load too — load_skills accepts both dirs and files). Theme discovery is accepted but ignored (rpi has no theme system — documented divergence). Trust gating remains deferred — project resources are discovered unconditionally (a copied .pi/ drops in and works).
  • No --models cycling, no ModelRuntime/multi-provider. One model, one provider (Anthropic), resolved up-front by crate::provider.
  • Built-in tools: read, bash, edit, write plus the read-only grep/find/ls (the TS createCodingTools default set). grep/find use an in-process FileSystem+regex/globset implementation (documented divergence from the TS rg/fd shell-out; see docs/m4-tools-open-questions.md).
  • Session restore (-c/-r/--session) is partially supported: a fresh session is always created. The harness’s create rejects sessions that already have records unless allow_existing_session is enabled. The interactive -c/-r/--session paths enable that mode and replay the existing branch before appending new messages. See SessionSelection.

Structs§

ReloadContext
Everything /reload needs to rebuild extension + resource state into a live harness. Built once in build (alongside the harness) and held by the TUI (cloned into the reload callback the bridge carries + the /reload command handler). The harness itself is NOT held here — the TUI already owns a &AgentHarness / a clone; passing it at the call site keeps this struct free of a harness back-reference (so it can be Clone and moved into the reload callback without borrowing the harness).
ReloadOutcome
The outcome of a reload: a human-readable status line for the transcript (counts of what reloaded), and whether any load diagnostics appeared.

Enums§

BuildError
A harness-build error.
OpenError
Errors from open_session_by_id, split so the CLI can map them to BuildError while the TUI can surface a friendlier note.
SessionSelection
How the user asked to select a session. v1 honors NoSession (ephemeral InMemorySessionStorage), New (a fresh JSONL file), and — new this pass — Latest / ById, which restore an existing JSONL session on launch (--continue/-c, --resume/-r, --session <id|path>). The restored transcript renders into the TUI on startup and the run continues appending to the same file.

Constants§

BUILTIN_TOOL_NAMES
The built-in tool names v1 ships, in the order the TS createCodingTools registers them: the mutating set (read/bash/edit/write) followed by the read-only search set (grep/find/ls).

Functions§

bash_options
Build the tool list per --tools/--exclude-tools/--no-tools/ --no-builtin-tools. Mirrors the TS tools/excludeTools/noTools resolution in createAgentSession. Default bash timeout: 120s when the model doesn’t pass one (prevents a forgotten timeout from hanging the run forever — the “卡住” report). RPI_BASH_TIMEOUT overrides; a model-supplied timeout always wins.
build
Build the AgentHarness from the resolved model + parsed args + cwd.
default_session_dir
The default session directory: <cwd>/.pi/sessions. Mirrors the TS getDefaultSessionDir (.pi/agent/sessions in TS; v1 uses .pi/sessions under the project — a documented divergence).
default_system_prompt
The default coding system prompt. A condensed port of the TS packages/coding-agent/src/core/system-prompt.ts base prompt — the pi-internal docs/skills/context-file sections are omitted (v1 has none of that machinery), leaving the role + tools + guidelines core.
list_session_metadata
List the JSONL session metadata under the default session dir, newest first. Shared by startup restore and the TUI /session hot-switch.
open_session_by_id
Open a session whose id matches exactly or by file-name containment (so --session 01a02… / a partial id / a full file name all work). The TUI /session hot-switch calls this with the selector’s item value.
reload_extension_resources
Re-run extension + resource discovery and push the rebuilt state into the live harness via the B5d setters. The old ExtensionSession + ActionBridge are invalidated + swapped in ReloadContext’s cells. This is the single routine both /reload (TUI) and a plugin’s runtime_action(Reload) drive (the latter via the mailbox signal).
select_session
Decide the session selection from parsed args + the resolved cwd.

Type Aliases§

ActionBridgeCell
The cell that holds the live ActionBridge across a /reload. A plugin stores the bridge’s raw user_data pointer during register; on reload the old bridge is invalidated (in-flight calls fail fast) and the fresh one stored. The fresh session’s plugins are handed the fresh bridge pointer.
ExtensionSessionCell
The cell that holds the live ExtensionSession across a /reload. Cloned into every site that needs the current session (the TUI, the reload callback). On reload the old session is replaced out (its active flag flipped + its keepalive dropped, unmapping the old cdylibs) and the fresh one stored. Carried as a plain ExtensionSession (not Option) — a none() placeholder fills the slot while the fresh one is being built.