Hmbown/CodeWhale · error · anyhow::Error

sub-agent transcript artifact must have a parent directory

Error message

sub-agent transcript artifact must have a parent directory

What it means

prepare_subagent_transcript_parent ensures the parent directory of a transcript artifact exists before writing it, after first rejecting root-relative symlink escapes from the state root. Path::parent() returns None only for a path with no parent component (e.g. a bare relative name or a root path), which the code treats as invalid and refuses with this anyhow error. This guards against writing transcripts to an ambiguous location.

Solutions

  1. Pass a full artifact path that includes its directory, e.g. <state_root>/transcripts/<session>.jsonl
  2. Check the configuration value that supplies the transcript path — it is likely empty or root-only
  3. Normalize the path before the call so it always resolves under a concrete directory

Example fix

// before
let artifact = Path::new("transcript.jsonl");
// after
let artifact = state_root.join("transcripts").join("session-42.jsonl");
Defensive patterns

Strategy: validation

Validate before calling

if path.parent().is_none() {
    return Err("transcript artifact path must include a parent directory".into());
}

Type guard

fn has_parent(path: &Path) -> bool {
    path.parent().map(|p| !p.as_os_str().is_empty()).unwrap_or(false)
}

Try / catch

match prepare_transcript(&state_root, &path) {
    Ok(()) => {},
    Err(e) if e.to_string().contains("parent directory") =>
        return Err(anyhow!("invalid transcript path {:?}: use <dir>/<file>", path)),
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Passing a transcript artifact path with no parent directory component — a bare filename relative to no directory (like "transcript.jsonl" at a root) or a filesystem root itself — into the sub-agent transcript preparation path.

Common situations: A caller building the transcript path from an empty config value or from string joining that dropped the directory component, yielding a bare filename or "/".

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/18cf283d8518026e. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/tools/subagent/mod.rs:8957

    use std::os::unix::fs::OpenOptionsExt;

    fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOFOLLOW)
        .open(path)
        .map_err(Into::into)
}

#[cfg(not(unix))]
fn open_subagent_state_file(path: &Path) -> Result<fs::File> {
    fs::File::open(path).map_err(Into::into)
}

fn prepare_subagent_transcript_parent(state_root: &Path, path: &Path) -> Result<()> {
    reject_root_relative_symlinks(state_root, path)?;
    let parent = path
        .parent()
        .ok_or_else(|| anyhow!("sub-agent transcript artifact must have a parent directory"))?;
    fs::create_dir_all(parent)?;
    // Re-check after creation so a pre-existing component cannot redirect the
    // private transcript outside the state root.
    reject_root_relative_symlinks(state_root, path)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(parent, fs::Permissions::from_mode(0o700))?;
    }
    Ok(())
}

fn create_private_subagent_transcript(state_root: &Path, path: &Path, bytes: &[u8]) -> Result<()> {
    prepare_subagent_transcript_parent(state_root, path)?;
    let mut file = open_private_subagent_transcript(path, false)?;
    file.write_all(bytes)?;
    file.sync_all()?;
    Ok(())

View on GitHub (pinned to 73e0f67d83)