Skip to main content

Module pathctx

Module pathctx 

Source
Expand description

The directory context the harness supplies (HP-19): the working directory a command runs in, and the project root. It exists to make relative-path classification honest — cd /etc && echo > ./x must be seen as writing /etc/x, not a worktree file.

The context is ambient for one command evaluation: a single cwd/root pair threads logically through the whole recursive verdict tree (script → pipeline → cmd → redirect → leaf). Rather than add a pass-through parameter to ~15 recursive functions across cst, handlers, and engine, it lives in a scoped thread-local, installed by enter at the top of an evaluation and read at exactly two leaves: legacy is_safe_write_target and engine classify_locus, both via place.

Everything is fail-open to today’s behavior: with no cwd/root (or an unresolvable path) place returns the path as written, so the classifiers behave exactly as before — the signal tightens when present, never a regression when absent.

Re-exports§

pub use item_shape::Binding;
pub use item_shape::Facts;
pub use item_shape::ItemShape;
pub use item_shape::ItemShapeGuard;
pub use item_shape::binding;
pub use item_shape::enter_loop_shape;
pub use item_shape::enter_stdin_shape;
pub use item_shape::loop_shape;
pub use item_shape::stdin_shape;

Modules§

anchor
How much a write depends on the folder it runs in, for a harness that does not say which folder that is (docs/design/unknown-folder-writes.md §4–§6).
folder
The unknown-folder mode of one evaluation: which folder level is in force, where each relative path is placed, and which command leaves wrote without naming a path (docs/design/unknown-folder-writes.md).
item_shape
What the values behind a loop variable or a piped stream can look like, as far as flags go.

Structs§

Guard
LoopGuard
Pops the loop binding when dropped.
PathCtx
The working directory and project root for the command under evaluation. Both optional: a harness may supply neither (e.g. opencode), and classification falls back to the relative-is-worktree assumption.
StdinReprGuard
VarGuard

Enums§

Root
The project root a command is judged against.

Functions§

cwd
The ambient working directory, if known.
enter
enter_cwd
Run f with the ambient cwd temporarily replaced (root unchanged) — used by intra-line cd tracking as it walks a chain’s statements. Restored on drop.
enter_loop_var
Bind loop variable name to its list’s representative items for the duration of the guard (the loop body’s classification). Nested loops stack; the latest binding of a name wins.
enter_stdin_repr
Bind the representative PATH of the items arriving on stdin, for the duration of the guard — set by the pipeline walker to the previous stage’s output-path locus. An operand-injecting consumer (xargs) reads it so find / | xargs cat gates the injected operand at /, while find ./src | xargs cat gates it at the workspace (mirrors find -exec’s {} binding).
enter_var
Bind name to a CERTAIN literal value (a VAR=/path assignment, or $1 at a function call) for the duration of the guard. The latest binding of a name wins, loop or not. The caller binds only values it is certain of — an uncertain value (VAR=$(cmd), VAR=$UNBOUND) is bound to the unpinnable sentinel so $VAR still fail-closes rather than resolving to a stale or dropped value.
expand_vars
Expand any bound variable ($name / ${name}) in path to its value, or a loop variable to its representative list item — the read representative when want_write is false, the write representative when true. Unbound $… is left untouched (so it still fail-closes to machine). Returns path unchanged when nothing is bound.
in_session_scratchpad
join_cwd
Resolve a cd target to a new working directory. cur is the current cwd, needed for a relative target. Used by intra-line cd tracking (HP-19 #2).
join_cwd_mode
names_a_loop_var
Whether text refers to a loop variable whose items are not known: its representative stands in for a locus, not for the text the shell will hold, so a copy of it is not a known value.
note_reported_cwd
Record the folder the harness reported. runs_there is whether the harness runs the command in it (HookFormat::cwd_is_the_commands); when not, the folder is never taken for where it runs.
root
The workspace ROOT (project dir), falling back to cwd (the hook defaults root to cwd), or Root::Unknown when the context names a folder that is not a usable one: the unknown-folder sentinel, or a cd that could not be resolved.
stdin_item_repr
The current stdin-item representative, or None when the source is unknown (no pipe / an unmodeled producer) — in which case the consumer worst-cases the injected operand.
under_temp
Whether path names a temporary directory, including a case variant, the macOS data-volume firmlink spelling (/System/Volumes/Data/tmp), and /var/folders. Shared by folder-code, config-dir and locus checks. This recognizes a place to refuse; it does not admit the path as scratch storage. A case variant of /tmp stays machine for writes on a case-sensitive volume.
under_temp_root
Whether path is under a temporary-filesystem root. /tmp and /var/tmp count on every platform. On macOS, so do /private/tmp, /private/var/tmp, and /var/folders/<segment>/<segment>/T with its /private spelling. $TMPDIR counts too.