Hmbown/CodeWhale · error · anyhow::Error

sub-agent state path must include a file name

Error message

sub-agent state path must include a file name

What it means

Sub-agent state paths are canonicalized against a state root and must name an actual file: `Path::file_name()` must return `Some`. A path that ends in `..`, is a bare drive/root prefix, or otherwise has no final component cannot be joined onto a resolved parent, so the library rejects it.

Solutions

  1. Pass a path ending in an actual file name, e.g. `state/session.json` instead of `state/session.json/` or `state/..`.
  2. Normalize the path before calling (strip trailing separators and `.`/`..` components).
  3. If you have a directory, append the intended file name before invoking the API.
  4. Check config-driven path construction for trailing separators or `.` components.

Example fix

// before
let path = Path::new("/state/subagent/..");
// after
let path = Path::new("/state/subagent/session.json");
Defensive patterns

Strategy: validation

Validate before calling

fn state_path_has_file_name(p: &std::path::Path) -> bool {
    p.file_name().is_some()
}
// guard before calling: state_path_has_file_name(&path)

Prevention

When it happens

Trigger: Calling a sub-agent state API with a path like `/state/dir/..`, `C:\`, or any path whose final component is empty, so `absolute.file_name()` is `None`.

Common situations: Building the state path by string concatenation that ends with a separator or `..`; passing a directory instead of a file path; constructing paths programmatically from config values with trailing slashes.

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/4ee4d527829d77a8. Report an issue: GitHub.

Appendix: source

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

    // is migrated on load (see load_state).
    checked_subagent_state_path(
        &state_root,
        &Path::new(".codewhale")
            .join("state")
            .join(SUBAGENT_STATE_FILE),
    )
}

fn checked_subagent_state_path(state_root: &Path, path: &Path) -> Result<PathBuf> {
    let state_root = normalize_subagent_workspace(state_root);
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        state_root.join(path)
    };
    let file_name = absolute
        .file_name()
        .ok_or_else(|| anyhow!("sub-agent state path must include a file name"))?;
    let parent = absolute
        .parent()
        .ok_or_else(|| anyhow!("sub-agent state path must include a parent directory"))?;
    // Resolve through the deepest existing ancestor so Windows short/long names
    // and verbatim (`\\?\`) spellings match the already-canonical state root.
    // A missing parent used to fall back to lexical normalization and could keep
    // an 8.3 component (for example `RUNNER~1`) that no longer prefixes the root.
    let parent = resolve_path_for_containment(parent)?;
    let state_path = parent.join(file_name);
    if !path_is_within_state_root(&state_path, &state_root) {
        return Err(anyhow!(
            "sub-agent state path must stay within state root: {}",
            state_path.display()
        ));
    }
    reject_root_relative_symlinks(&state_root, &state_path)?;
    Ok(state_path)
}

View on GitHub (pinned to 73e0f67d83)