Skip to content

core/src/paths

The workspace path helpers (#59) — THE one place that walks the filesystem for roots.

Before this module, seven call sites each computed the repo root by counting .. segments off their own import.meta.url — a shape that breaks silently when a file moves (dev.ts carried 4×".."). An anchor test (no-duplicate-paths.test.ts) now refuses an eighth copy: fileURLToPath and inline .enso path assembly outside this module fail the suite by name.

⚠ REALPATH FIRST, then walk — load-bearing, observed live (PR #58 receipts): bun does NOT realpath import.meta.url through the print-harness symlink mirror (printHarnessDir), so a walk from the un-resolved module path starts inside the mirror. The mirror’s entries are symlinks back into the real tree, so resolving first lands the walk in the real checkout; walking first would search the wrong ancestry.

⚠ These helpers run under node/bun only (node:fs). They are barrel-exported and the browser bundle tolerates that because nothing browser-side imports them — probed 2026-09-04: bun build index.html --target=browser tree-shakes the module out.

containsPathFragment(absolutePath, fragments): boolean

Defined in: packages/core/src/paths.ts:965

True when the path contains any of the fragments, case-insensitively.

⚠ The path is compared WITH a trailing separator appended (#53 S1.2). Directory-shaped fragments carry a trailing / (/.aws/, /.ssh/, /.git/) so that they match as a segment and not as a name prefix — but resolve() strips the trailing slash a model supplies, so the directory ITSELF (/home/u/.aws) never contained /.aws/ and passed. grep pattern=aws_secret_access_key path=~/.aws went through the reader gate that way. Appending the separator makes the directory match its own fragment; file-shaped fragments (/.npmrc, /.kube/config) are unaffected because they still occur as a substring.

string

readonly string[]

boolean


textLooksLikeSecretPath(text, fragments): boolean

Defined in: packages/core/src/paths.ts:982

True when the TEXT of a path-shaped string carries credential material, by directory fragment or by basename.

No filesystem access and no resolution: this is the question to ask about a shell word whose value is not knowable from the text ("$HOME/.enso/auth.json" — the harness’s shell-secret-paths.ts), where the harness’s looksLikeSecret realpath leg has nothing to resolve. Here, not in the harness, so the command judge’s script resolution (#469) asks the same question the guards do.

string

readonly string[]

boolean

Defined in: packages/core/src/paths.ts:259

Thrown when no ancestor of the start path is the named package.

  • Error

new PackageDirNotFoundError(startPath, packageName): PackageDirNotFoundError

Defined in: packages/core/src/paths.ts:260

string

string

PackageDirNotFoundError

Error.constructor


Defined in: packages/core/src/paths.ts:59

Thrown when no ancestor of the start path is a workspace root.

  • Error

new RepoRootNotFoundError(startPath): RepoRootNotFoundError

Defined in: packages/core/src/paths.ts:60

string

RepoRootNotFoundError

Error.constructor


Defined in: packages/core/src/paths.ts:377

What moving the checkout’s .enso/ into ENSO_HOME did (#421).

readonly from: string

Defined in: packages/core/src/paths.ts:379

The checkout’s .enso/ the files came from.

readonly held: boolean

Defined in: packages/core/src/paths.ts:390

The checkout HAS an old .enso/ that this launch did not move, because ENSO_HOME was set explicitly (migrateCheckoutEnsoOnLaunch). Said, so it is not silently ignored.

readonly kept: readonly string[]

Defined in: packages/core/src/paths.ts:385

Files LEFT in from: to already holds one at that path. Never overwritten — the user decides.

readonly moved: readonly string[]

Defined in: packages/core/src/paths.ts:383

Files moved, relative to both.

readonly to: string

Defined in: packages/core/src/paths.ts:381

ENSO_HOME.


Defined in: packages/core/src/paths.ts:710

What migrateSessionsIntoProjects did: files moved, files left because the target held one, directories whose cwd is gone.

readonly kept: string[]

Defined in: packages/core/src/paths.ts:714

Legacy files left in place: the project folder already held one of that name.

readonly moved: string[]

Defined in: packages/core/src/paths.ts:712

Files now in their project folder.

readonly orphaned: string[]

Defined in: packages/core/src/paths.ts:716

Legacy directories left in place: a session’s cwd no longer exists, so it has no project.

readonly unreadable: string[]

Defined in: packages/core/src/paths.ts:718

Legacy files left in place: no header with a cwd on the first line — not pi’s shape, or the tail a pre-#552 process kept appending after a move.


const HARNESS_PACKAGE_NAME: "enso" = 'enso'

Defined in: packages/core/src/paths.ts:242

The harness package’s manifest name (packages/harness/package.json): what an installed harness is found by (#950). ⚠ The name is not settled for publishing (#948). A rename changes this constant and the manifest together, and paths.test.ts fails when they differ.


const WEB_PACKAGE_NAME: "@enso/web" = '@enso/web'

Defined in: packages/core/src/paths.ts:249

The web package’s manifest name (packages/web/package.json), pinned the same way.


checkoutEnsoDir(repoRoot): string

Defined in: packages/core/src/paths.ts:368

The checkout-local .enso/ every runtime path lived in before #421. Read by the migration (migrateCheckoutEnso) and nothing else.

string

string


checkoutOrInstalledRoot(startPathOrFileUrl, packageName): string

Defined in: packages/core/src/paths.ts:321

Where a package’s own files live, for a reader that wants the checkout when there is one: the Enso checkout root when the package is a workspace member of one, else the directory it is installed in (#950). The guards’ harnessRoot is this. In a checkout it stays the root, where #421’s migration leaves what it could not move. Installed, it is the package, the one directory that is Enso’s.

string

string

string


checkoutRootOfPackage(packageDir): string | undefined

Defined in: packages/core/src/paths.ts:305

The Enso checkout a package directory is a workspace member of, or undefined when the package is installed rather than checked out (#950). A member sits at <root>/packages/<name> under a root whose package.json declares workspaces. Nothing else counts.

⚠ Not “the nearest workspace root above”. A package installed into the user’s own monorepo has one of those, and it is the user’s: its .env, its .git and its .enso/ are not Enso’s to read or to migrate. The layout check is what keeps an install from adopting someone else’s checkout.

string

string | undefined


ensoAgentDir(home): string

Defined in: packages/core/src/paths.ts:508

pi’s scratch agent dir — settings, extensions, auth, sessions, trust. Shared by the TUI launcher and the web server. ⚠ Deliberately NOT ~/.pi/agent: the operator’s own pi packages and credentials stay invisible to Enso’s sessions (agent-host.ts).

string

string


ensoConfigPath(home): string

Defined in: packages/core/src/paths.ts:899

The user’s config file (#59, #421): ENSO_HOME/config.json. Absent is the defaults (parseEnsoConfig).

string

string


ensoHome(environment): string

Defined in: packages/core/src/paths.ts:355

The user-level Enso directory (#421): ENSO_HOME when set, else ~/.enso — pi’s own pattern, PI_CODING_AGENT_DIR else ~/.pi/agent. Every runtime path resolves under it: pi’s agent dir, the secrets file, the logs, the project list. One per user, so a second checkout or worktree shares its logins, sessions and projects, and nothing runtime lives in a source tree.

Read from the environment it is given, never from process.env directly, so a test passes its own and the launchers pass theirs — and a child the terminal launcher spawns inherits the same.

Readonly<Record<string, string | undefined>>

string


ensoLogsDir(home): string

Defined in: packages/core/src/paths.ts:574

Where the harness’s log lives (#132): ENSO_HOME/logs/enso-YYYY-MM-DD.jsonl, ONE file per day that every process appends to (dated, never renamed, so a second writer is safe — see web/server/logging.ts).

Under ENSO_HOME so bun never autoloads from it and the write gate already knows it (guard.ts).

string

string


ensoPermissionRulesPath(home): string

Defined in: packages/core/src/paths.ts:872

The permission policy pi runs under (#446, #496): ENSO_HOME/permissions.json. The host and the terminal launcher run preparePermissionPolicy (@enso/core/permission-rules) on it, which seeds it and links the permission engine’s config path to it before the engine reads it.

string

string


ensoProjectsDir(home): string

Defined in: packages/core/src/paths.ts:610

Everything Enso holds ABOUT its projects (#552): ENSO_HOME/projects/<id>/, one folder per project id, holding the project’s sessions (projectSessionsDir) and, with #551, its threads’ trees and snapshots. What a project says about ITSELF (<project>/.enso/config.json) stays in the checkout, trust-gated; pi’s trust store stays pi’s. The registry (ensoProjectsPath) is the one map from id to path.

string

string


ensoProjectsPath(home): string

Defined in: packages/core/src/paths.ts:584

The projects registered from a page (#395): ENSO_HOME/projects.json. Server STATE, not config — the roots they may sit under are config (ENSO_HOME/config.json).

string

string


ensoPromptsDir(home): string

Defined in: packages/core/src/paths.ts:861

The prompt library (#443): ENSO_HOME/prompts/<id>.md, one markdown file per prompt. The user’s, never committed with a project.

string

string


ensoSecretsPath(home): string

Defined in: packages/core/src/paths.ts:539

The harness’s secrets file (#89): ENSO_HOME/secrets.env, NOT .env at the root.

bun auto-loads .env* from the cwd into EVERY bun process — including a bun -e the model runs from the repo root — so a root .env reaches a tool child no matter what the parent scrubbed (verified with env -i).

ENSO_HOME is write-protected (ENSO_PERSISTENCE_WRITE_DIRECTORIES) and read-denied on BOTH tool routes by the /.enso/ fragment in DEFAULT_GUARD_POLICY: the reader gate for every tool carrying a path, and the shell deny (#204) for a literal path word in bash/powershell. ⚠ The shell leg is a bar, not a boundary — shell-secret-paths.ts states what a runtime-assembled name still gets past. ⚠ An ENSO_HOME whose path has no .enso segment is NOT matched by that fragment: keep the directory named .enso.

⚠ THE core/src! QUALIFIER IS LOAD-BEARING, and the obvious simplification does not work (#188). guard.ts is not imported here, so TypeScript’s own link resolution — which TypeDoc prefers by default — has nothing in scope, and a bare {@link DEFAULT_GUARD_POLICY} is an unresolved link. The documented root-scoped form {@link !DEFAULT_GUARD_POLICY} does not help either: with several entry points, the PROJECT’s children are the modules, not their symbols, so resolution from the root finds nothing. Tested, all three spellings. The module source is therefore named, which couples this link to the entry-point layout in typedoc.json — and that coupling breaks LOUDLY, because an unresolved link is a fatal warning in the api-docs gate.

string

string


fileUrlToPath(fileUrl): string

Defined in: packages/core/src/paths.ts:207

Decode a file:// URL to the filesystem path it names.

The ONLY fileURLToPath call site in the repository (the anchor test enforces it). Two callers with different reasons share it: findRepoRoot accepts import.meta.url, and the harness guard decodes a file:// path a tool was handed — pi’s own tools decode one before touching the filesystem, so the guard must judge the decoded path (#53).

string

string


findRepoRoot(startPathOrFileUrl): string

Defined in: packages/core/src/paths.ts:224

The repository root: the nearest ancestor whose package.json declares workspaces.

Accepts a filesystem path OR a file:// URL, so callers pass import.meta.url directly and never touch fileURLToPath themselves (the anchor test enforces that). The start path is realpath-resolved BEFORE walking — see the module header for why.

⚠ This finds the root of the checkout the given FILE lives in — for a module inside this repo that is always the Enso checkout, never the session cwd. That distinction is what config.ts relies on; do not “fix” this to consult process.cwd().

string

string


harnessPackageDir(repoRoot): string

Defined in: packages/core/src/paths.ts:881

The harness package — the directory enso.ts hands to pi’s -e.

string

string


installedPackageDir(startPathOrFileUrl, packageName): string

Defined in: packages/core/src/paths.ts:283

The directory the named package is installed in: the nearest ancestor of the start path whose package.json is named packageName (#950). It works the same in this checkout (packages/harness) and in an install (…/node_modules/enso). findRepoRoot cannot do that: an installed package has no workspace root above it, so that walk throws.

Takes a path or a file:// URL and realpaths first, as findRepoRoot does and for the same reason: the print-harness mirror (printHarnessDir) is symlinks back into the real tree.

⚠ Matched by NAME, not “the first package.json”: a package may hold another manifest below its root (a fixture, a nested tool), and the first one found would be the wrong directory, with no error to say so.

string

string

string


isFileUrl(candidate): boolean

Defined in: packages/core/src/paths.ts:193

True when the string is a file:// URL rather than a filesystem path.

string

boolean


migrateCheckoutEnso(repoRoot, home): EnsoHomeMigration

Defined in: packages/core/src/paths.ts:414

Move the checkout’s .enso/ into ENSO_HOME (#421) — pi’s runMigrations pattern: automatic on startup, idempotent, never overwriting. Each file whose path is free in ENSO_HOME is moved; one that is taken is left where it is and reported in kept, so two checkouts’ logins or sessions conflict loudly instead of one silently replacing the other. Emptied directories are removed, the checkout’s .enso/ with them once nothing is left in it.

print-harness/ is not moved but deleted: a build cache of the checkout, regenerated at its new place (printHarnessDir) on the next terminal launch, whose relative links would break moved.

⚠ THE PROJECT’S OWN FILES STAY (#754): the top-level config.json, config.local.json and prompts/ are the checkout’s project tiers now (projectConfigPath, projectLocalConfigPath, projectPromptsDir), committed or kept with it and read trust-gated as the project’s. Moved, the repository’s shared config would become the user’s config for every project.

Tolerant of a second launcher migrating at the same moment (bun run dev starts two): a file the other process moved first is skipped, not reported. ENSO_HOME is created 0700 — it holds credentials.

string

string

EnsoHomeMigration


migrateCheckoutEnsoOnLaunch(repoRoot, environment): EnsoHomeMigration

Defined in: packages/core/src/paths.ts:438

The migration a LAUNCH runs (#421): into the default ENSO_HOME (~/.enso) only. With ENSO_HOME set explicitly nothing moves — the checkout’s old .enso/ is held and reported, when it holds anything besides the project’s own files (#754), which stay either way.

⚠ Why: a launcher run from a checkout with a scratch or test ENSO_HOME (ENSO_HOME=/tmp/x) would otherwise move that checkout’s logins and sessions into the scratch directory, to be lost with it — which happened, once, while this was being built. ~/.enso is the one place a user’s state is meant to live, so it is the one place a launch moves it to without being asked. Someone who chose another home moves the old directory by hand, and the launch says so.

string

Readonly<Record<string, string | undefined>>

EnsoHomeMigration


migrateSessionsIntoProjects(agentDir, projectsDir): SessionMigration

Defined in: packages/core/src/paths.ts:807

The migration a LAUNCH runs after migrateCheckoutEnsoOnLaunch (#552): every session pi filed under its default <agentDir>/sessions/<encoded-cwd>/ moves to its project’s folder, projects/<projectIdOf(realpath(cwd))>/sessions/, so both surfaces read and write one place. Each file is moved by moveEntry: never over one the target already holds (kept). Two launchers may run this at once (the web and a terminal start together): a file or directory the other moved or removed first is gone, not an error, at every step. A session whose cwd no longer exists has no canonical path and no project; its directory is reported orphaned and left where it is. A file with no readable header is unreadable and left too. Emptied legacy directories are removed.

⚠ A file a RUNNING pre-#552 process still appends to is split by the move: pi appends by path (appendFileSync(this.sessionFile)), so its next entry recreates a headerless file at the old path — which the next launch reports unreadable, by name, for the user to join by hand. The launchers say so; a server of this version opens legacy files only for orphans, whose directory is never moved while it is an orphan.

⚠ A fork’s header names its parent by FILE PATH (#433); after a move that path is stale. The host resolves a parent by its file’s basename when the path misses (agent-host.ts#parentThreadIdOf), since pi names a file <timestamp>_<id>.jsonl. pi’s own headers are not rewritten.

string

string

SessionMigration


printHarnessDir(repoRoot): string

Defined in: packages/core/src/paths.ts:561

Where the print variant of the harness package is materialized (#20): a build cache of THIS checkout, so it stays in the checkout — its links point into this checkout’s harness, and two checkouts sharing one ENSO_HOME would otherwise relink it to whichever launched last (#421). Under node_modules/.cache, the conventional home of a regenerated artifact, gitignored and outside every .enso the guards protect.

string

string


projectConfigApprovalsPath(projectsDir, projectId): string

Defined in: packages/core/src/paths.ts:700

The project config tiers the operator approved (#753): <projectsDir>/<id>/config-approvals.json, the contents of the project’s .enso/config.json and .enso/config.local.json as approved on its page. ⚠ Under ENSO_HOME, beside the project’s sessions, never in the project: the agent works in the project, and the approval is what stands between its writes there and what applies.

string

string

string


projectConfigPath(projectDirectory): string

Defined in: packages/core/src/paths.ts:910

A project’s own config (#421): <project>/.enso/config.json. Read only once the project is trusted, over the user’s (ensoConfigPath); the project directory only, never its parents, as pi reads <cwd>/.pi/.

string

string


projectIdOf(canonicalPath): string

Defined in: packages/core/src/paths.ts:597

The id of a canonical project directory: its path, hashed — sixteen hex digits (isEnsoProjectId). The key everything Enso holds ABOUT a project is filed under (ensoProjectsDir, #552), and projects.json the only id↔path map. ⚠ The path must be the CANONICAL one (realpath), as the registry makes it: the same directory, one id. A moved checkout is a new id; its folder stays behind until moved by hand (#552’s decision).

string

string


projectIdOfCwd(projectsDir, cwd): string

Defined in: packages/core/src/paths.ts:688

The project a cwd belongs to, by id: the one a minted tree’s path names, else the cwd’s own (its canonical path, hashed — projectIdOf). ⚠ A THREAD ON A TREE RUNS IN A DIRECTORY THAT IS NO PROJECT: everything filed or served by project — the session directory, the snapshot store, the server’s projects, the approved project config (#753) — asks this first.

string

string

string


projectLocalConfigPath(projectDirectory): string

Defined in: packages/core/src/paths.ts:921

A project’s local config (#754): <project>/.enso/config.local.json, gitignored — one person’s overrides for one project, over the shared projectConfigPath. Read only once the project is trusted, like the shared file.

string

string


projectPromptsDir(projectDirectory): string

Defined in: packages/core/src/paths.ts:932

A project’s own prompt library (#470): <project>/.enso/prompts/<id>.md, committed with the project and shared by everyone who works in it. Read only once the project is trusted, like its config, and never written by Enso: a shared file is changed in the repository, not from the manager.

string

string


projectSessionsDir(projectsDir, projectId): string

Defined in: packages/core/src/paths.ts:621

A project’s sessions: <projectsDir>/<id>/sessions, the directory pi is handed as its session directory for every thread of the project — whatever cwd the thread runs in (a #551 tree is not the project), so the thread is always filed under its project and found there.

string

string

string


projectSnapshotsDir(projectsDir, projectId): string

Defined in: packages/core/src/paths.ts:633

A project’s snapshot stores: <projectsDir>/<id>/snapshots/<thread-id> is one thread’s bare repository of its tree as each run began (#551, @enso/core/snapshot-store). Beside the thread’s session, under its project; to be removed with the thread by #551 slice 2’s dispose — nothing removes one yet.

string

string

string


projectTreesDir(projectsDir, projectId): string

Defined in: packages/core/src/paths.ts:644

A project’s thread trees: <projectsDir>/<id>/trees/<thread-id> is the directory one thread runs in (#551 slice 2, @enso/core/thread-tree) — a worktree of the checkout, or a copy — never the checkout itself. pi’s cwd for the thread, so the session header records it.

string

string

string


readTextIfPresent(path): string | undefined

Defined in: packages/core/src/paths.ts:943

A file’s text, or undefined when there is no file — for a file whose absence means its defaults (the config). Any other failure to read it throws: a file that is there and unreadable is not a missing one.

string

string | undefined


realpathOrSelf(path): string

Defined in: packages/core/src/paths.ts:74

realpathSync, or the input unchanged when the path (or a segment of it) does not exist.

string

string


replaceFileText(path, text, options?): void

Defined in: packages/core/src/paths.ts:134

Replace the file at path with text (or bytes — a file put back as it was, #945) in one step: written beside the file it names, then renamed over it — a reader sees the old file or the new one, never half of one. Settings’ writes go through here: the permission policy and Enso’s rule record beside it (#474, #945), config.json (#924, #932, #933, #934), secrets.env (#932, #933) and the project config approvals (#753).

⚠ AT THE FILE THE PATH NAMES, NOT THE LINK: a config or secrets file kept as a symlink (into a dotfiles repo, say) stays a link, and the file behind it changes — or is made, when the link points at a file not there yet; a link into a directory not there is an error, never replaced. The file keeps its own mode, or takes mode whatever it was before (secrets.env is owner-only, #932, #933); a new file without one gets the umask’s. The staged file is always newly made, so it is never readable wider for a moment. Nothing staged is left behind when the write or the rename throws: the error is rethrown.

⚠ THE MODIFICATION TIME MUST MOVE. The permission engine reloads a policy only when the file’s mtimeMs differs from the one it last read (config/policy-loader.ts), and Linux stamps it from a coarse clock — one tick is milliseconds. A write landing in the same tick as the last one (a reset just after a hand edit) kept the OLD policy in force, silently. Found by agent-host.test.ts’s reset test.

string

string | Uint8Array<ArrayBufferLike>

number

void


resolvePackageDir(packageName, fromDirectory): string

Defined in: packages/core/src/paths.ts:336

The directory of packageName as Node resolves it from fromDirectory’s package (#950). That is the workspace link in node_modules in a checkout, and a declared dependency in an install. Realpath’d, so a workspace link lands on the package itself.

⚠ Resolves <name>/package.json, so a package whose exports hides its manifest cannot be resolved here. Neither Enso package declares exports that would.

string

string

string


rootDotenvPath(repoRoot): string

Defined in: packages/core/src/paths.ts:548

The root .env — where secrets must NOT live (#89); reported as misplaced when they do.

string

string


threadTreeRoot(projectsDir, path): string | undefined

Defined in: packages/core/src/paths.ts:671

The root of the minted tree a path is in — <projectsDir>/<id>/trees/<thread-id> for the tree’s root or anything under it — or undefined for any other path. The shape treeProjectId reads, cut at the tree: what the guard judges a session’s own files relative to (#615), so a tree under ENSO_HOME is a workspace like any checkout and not the harness’s own directory.

string

string

string | undefined


treeProjectId(projectsDir, path): string | undefined

Defined in: packages/core/src/paths.ts:658

The project id a minted tree’s path names — <projectsDir>/<id>/trees/<thread-id>, the tree’s root or anything under it — or undefined for any other path. ⚠ The one way a thread’s cwd becomes its project: a thread on a tree runs in a directory that is no project, and every reader that files or serves by project (the session directory, the snapshot store, the server’s projects) asks this first. Read off the path alone, so it holds for a tree whose project is no longer registered (it is then simply not served).

string

string

string | undefined


webPackageDir(repoRoot): string

Defined in: packages/core/src/paths.ts:890

The web package — the cwd both dev surfaces run in.

string

string