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 .rpi/<sub> + legacy .rpi/<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). Package themes are parsed by the TUI when selected via settings or --theme. Project resources load by default without a prompt. --no-approve or a stored negative trust.json decision disables them explicitly.
  • No ModelRuntime/multi-provider registry. The resolver supports the built-in Anthropic/OpenAI-compatible providers and models.json, but runtime catalog mutation remains outside this layer.
  • Built-in tools: read, bash, edit, write, and the read-only docs lookup tool. The former rpi-only grep, find, ls, and powershell tools remain library modules but are not registered by the CLI.
  • 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.
SessionSummary
A cheap, display-oriented summary of one JSONL session file.

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 coding tools registered by the CLI by default.

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 is <cwd>/.rpi/sessions.
default_system_prompt
The default coding system prompt. A condensed port of the TS packages/coding-agent/src/core/system-prompt.ts base prompt.
dispatch_session_event_async
Dispatch a no-payload lifecycle event (e.g. SessionStart, SessionShutdown, the rpi-specific BeforeTuiStart) to every extension handler subscribed to that tag, with veto + timeout semantics (P1).
format_session_bytes
Compact byte count (1.2MB / 34KB / 512B), shared by the startup picker and the in-TUI /session selector.
list_session_files_sync
Synchronously list every JSONL session file under dir, newest-first by modification time.
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.
session_display_label
The human label for a session row: its recorded name, else the first user prompt, else a marker that says the file has no transcript to show. Shared by the startup picker and the in-TUI /session selector.
short_session_id
The first 8 characters of a session id, enough to disambiguate two rows that share a timestamp without shouting the whole UUID.
summarize_session_file
Summarize one session file for the pickers. Best-effort: an unreadable or malformed file yields a summary with empty: false and no label, so it is still selectable rather than silently dropped.

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) and the fresh one stored atomically. In-flight callbacks retain the old keepalive until they return; callers never observe an empty placeholder during the swap.