Skip to main content

LoopContext

Struct LoopContext 

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

Context for resolving paths within a Ralph loop.

Encapsulates the working directory and loop identity, providing consistent path resolution for all loop-local state files.

Implementations§

Source§

impl LoopContext

Source

pub fn primary(workspace: PathBuf) -> Self

Creates context for the primary loop running in the main workspace.

The primary loop holds the loop lock and runs directly in the repository root without filesystem isolation.

Source

pub fn worktree( loop_id: impl Into<String>, worktree_path: PathBuf, repo_root: PathBuf, ) -> Self

Creates context for a worktree-based loop.

Worktree loops run in isolated git worktrees with their own .ralph/ directory, but share memories, specs, and code tasks via symlink.

§Arguments
  • loop_id - Unique identifier for this loop (e.g., “loop-1234-abcd”)
  • worktree_path - Path to the worktree directory
  • repo_root - Path to the main repository root (for symlinks)
Source

pub fn loop_id(&self) -> Option<&str>

Returns the loop identifier, if any.

Primary loops return None; worktree loops return their unique ID.

Source

pub fn is_primary(&self) -> bool

Returns true if this is the primary loop.

Source

pub fn workspace(&self) -> &Path

Returns the workspace root for this loop.

This is the directory where the loop executes:

  • Primary: the repo root
  • Worktree: the worktree directory
Source

pub fn repo_root(&self) -> &Path

Returns the main repository root.

For worktree loops, this is different from workspace() and is used to locate shared resources like the main memories file.

Source

pub fn ralph_dir(&self) -> PathBuf

Path to the .ralph/ directory for this loop.

Source

pub fn agent_dir(&self) -> PathBuf

Path to the .ralph/agent/ directory for this loop.

This directory contains agent state: memories, tasks, scratchpad, etc.

Source

pub fn events_path(&self) -> PathBuf

Path to the events JSONL file.

Each loop has its own isolated events file.

Source

pub fn current_events_marker(&self) -> PathBuf

Path to the current-events marker file.

This file contains the path to the active events file.

Source

pub fn urgent_steer_path(&self) -> PathBuf

Path to the urgent-steer marker file.

This file is created when ! arrives during an active iteration so ralph emit can block handoff until the current model turn has seen it.

Source

pub fn tasks_path(&self) -> PathBuf

Path to the tasks JSONL file.

Each loop has its own isolated tasks file.

Source

pub fn scratchpad_path(&self) -> PathBuf

Path to the scratchpad markdown file.

Each loop has its own isolated scratchpad.

Source

pub fn memories_path(&self) -> PathBuf

Path to the memories markdown file.

For primary loops, this is the actual memories file. For worktree loops, this is a symlink to the main repo’s memories.

Source

pub fn main_memories_path(&self) -> PathBuf

Path to the main repository’s memories file.

Used to create symlinks in worktree loops.

Source

pub fn context_path(&self) -> PathBuf

Path to the context markdown file.

This file contains worktree metadata (loop ID, workspace, branch, etc.) and is only created in worktree loops.

Source

pub fn specs_dir(&self) -> PathBuf

Path to the specs directory for this loop.

For primary loops, this is the actual specs directory. For worktree loops, this is a symlink to the main repo’s specs.

Source

pub fn code_tasks_dir(&self) -> PathBuf

Path to the code tasks directory for this loop.

For primary loops, this is the actual code tasks directory. For worktree loops, this is a symlink to the main repo’s code tasks. Note: This is different from tasks_path() which is for runtime task tracking.

Source

pub fn main_specs_dir(&self) -> PathBuf

Path to the main repository’s specs directory.

Used to create symlinks in worktree loops.

Source

pub fn main_code_tasks_dir(&self) -> PathBuf

Path to the main repository’s code tasks directory.

Used to create symlinks in worktree loops.

Source

pub fn summary_path(&self) -> PathBuf

Path to the summary markdown file.

Each loop has its own isolated summary.

Source

pub fn handoff_path(&self) -> PathBuf

Path to the handoff markdown file.

Generated on loop completion to provide context for the next session. Contains completed tasks, remaining work, and a ready-to-paste prompt.

Source

pub fn diagnostics_dir(&self) -> PathBuf

Path to the diagnostics directory.

Each loop has its own diagnostics output.

Source

pub fn history_path(&self) -> PathBuf

Path to the loop history JSONL file.

Event-sourced history for crash recovery and debugging.

Source

pub fn loop_lock_path(&self) -> PathBuf

Path to the loop lock file (only meaningful for primary loop detection).

Source

pub fn merge_queue_path(&self) -> PathBuf

Path to the merge queue JSONL file.

The merge queue is shared across all loops (in main repo).

Source

pub fn loop_registry_path(&self) -> PathBuf

Path to the loop registry JSON file.

The registry is shared across all loops (in main repo).

Source

pub fn planning_sessions_dir(&self) -> PathBuf

Path to the planning sessions directory.

Contains all planning session subdirectories.

Source

pub fn planning_session_dir(&self, id: &str) -> PathBuf

Path to a specific planning session directory.

§Arguments
  • id - The session ID (e.g., “20260127-143022-a7f2”)
Source

pub fn planning_conversation_path(&self, id: &str) -> PathBuf

Path to the conversation file for a planning session.

§Arguments
  • id - The session ID
Source

pub fn planning_session_metadata_path(&self, id: &str) -> PathBuf

Path to the session metadata file for a planning session.

§Arguments
  • id - The session ID
Source

pub fn planning_artifacts_dir(&self, id: &str) -> PathBuf

Path to the artifacts directory for a planning session.

§Arguments
  • id - The session ID
Source

pub fn ensure_ralph_dir(&self) -> Result<()>

Ensures the .ralph/ directory exists.

Source

pub fn ensure_agent_dir(&self) -> Result<()>

Ensures the .ralph/agent/ directory exists.

Source

pub fn ensure_specs_dir(&self) -> Result<()>

Ensures the .ralph/specs/ directory exists.

Source

pub fn ensure_code_tasks_dir(&self) -> Result<()>

Ensures the .ralph/tasks/ directory exists.

Source

pub fn ensure_directories(&self) -> Result<()>

Ensures all required directories exist.

Creates the memory symlink in a worktree pointing to main repo.

This is only relevant for worktree loops. For primary loops, this is a no-op.

§Returns
  • Ok(true) - Symlink was created
  • Ok(false) - Already exists or is primary loop
  • Err(_) - Symlink creation failed

Creates the specs symlink in a worktree pointing to main repo.

This allows worktree loops to access specs from the main repo, even when they are untracked (not committed to git).

§Returns
  • Ok(true) - Symlink was created
  • Ok(false) - Already exists or is primary loop
  • Err(_) - Symlink creation failed

Creates the code tasks symlink in a worktree pointing to main repo.

This allows worktree loops to access code task files from the main repo, even when they are untracked (not committed to git).

§Returns
  • Ok(true) - Symlink was created
  • Ok(false) - Already exists or is primary loop
  • Err(_) - Symlink creation failed
Source

pub fn generate_context_file(&self, branch: &str, prompt: &str) -> Result<bool>

Generates a context.md file in the worktree with metadata.

This file tells the agent it’s running in a worktree and provides information about the worktree context (loop ID, workspace, branch, etc.)

§Arguments
  • branch - The git branch name for this worktree
  • prompt - The prompt that started this loop
§Returns
  • Ok(true) - Context file was created
  • Ok(false) - Already exists or is primary loop
  • Err(_) - File creation failed

Sets up all worktree symlinks (memories, specs, code tasks).

Convenience method that calls all setup_*_symlink methods. Only relevant for worktree loops - no-op for primary loops.

Trait Implementations§

Source§

impl Clone for LoopContext

Source§

fn clone(&self) -> LoopContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LoopContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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