Skip to content

fix(paths): one ~/.perp path helper; HOME unset is an error, not ./~/.perp - #101

Merged
Hiksang merged 7 commits into
mainfrom
fix/perp-dir-path
Nov 15, 2026
Merged

Hiksang merged 7 commits into
mainfrom
fix/perp-dir-path

Conversation

@Hiksang

@Hiksang Hiksang commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

resolve(process.env.HOME || "~", ".perp") appeared in 18 places. path.resolve does not expand "~", so when HOME was unset (cron, a systemd unit without User= / Environment=HOME, some containers) perp-cli silently wrote settings, nonces, the execution log, agent keystores and the bot quarantine under ./~/.perp in the current directory. That is a fallback SSOT Rule #2 forbids, on files that guard funds.

  • One helper, src/paths.ts (homeDir(), perpDir(), perpPath(), owsPath()): 31 sites in 23 files now use it. Paths are computed at call time, not at module import.
  • No home directory is an error: INVALID_PARAMS with a remediation (how to set HOME in systemd / crontab). A relative HOME such as "~" is refused too. Nothing is written to the cwd.
  • os.homedir() is deliberately not used: on POSIX with HOME unset it falls back to the passwd entry, so a process or a test that cleared HOME would silently reach the real home's nonce / quarantine / keystore files. On Windows, USERPROFILE is used when HOME is not set.
  • The OWS binary lookup had the same bug (4 sites): with HOME unset, ./~/.ows/bin/ows in the cwd could be executed and put on PATH. Fixed with owsPath().
  • A guard test fails if HOME || "~", a direct process.env.HOME read, a { HOME } destructure or homedir() reappears outside src/paths.ts.

For a user with HOME set, every path is the same as before (the reviewer compared the old and new expression at each site), so existing keystores, nonces and settings are found unchanged.

Behaviour changes — please read

  • Every command fails when HOME is unset, including features that used homedir() before and so kept working (equity history, jobs, bot logs, trade journal, funding history). Users running bots from cron or systemd without HOME must set it. perp --help and --version still work.
  • Legacy state is detected, not migrated. A user who ran without HOME has state under <cwd>/~/.perp (for systemd, /~/.perp). Once HOME is set, that state would no longer be read: quarantined symbols would become tradable again and nonces would restart. So perp and perp-mcp refuse at startup while <cwd>/~/.perp exists, naming both directories, what to merge (quarantine entries, the higher nonces, lighter-agents/ keystores) and how to dismiss it (remove or rename the legacy directory). Nothing is moved automatically.
    • Limit: only the current working directory is checked. Legacy state left in a different directory than the one perp is now started from is not detected.
  • Windows under MSYS / Git Bash when HOME differs from USERPROFILE: the five former homedir() callers (equity / funding history, jobs, bot logs, trade journal) now follow HOME like the rest of the state. The funds-guarding files always followed HOME and do not move.
  • Internal exports changed from constants to functions: QUARANTINE_FILE → quarantineFile(), ENV_FILE → envFile(). All uses in src and tests are updated. The package exports only ./ws, so no external consumer is affected.

After an order is sent, a path failure cannot turn a fill into a reported failure: the execution log only warns, a failed quarantine write stays in memory, a failed quarantine read counts the symbol as quarantined (fail-closed), and nonce paths resolve before signing. The reviewer traced each of these.

Size: 39 files, +555 / −206. This is above the 500-line guideline. It was not split because a partial migration leaves the guard test red; most lines are a mechanical constant-to-function change.

Reported only, not changed (pre-existing):

Checklist

  • pnpm test passes (337 files / 7281 tests; 22 new)
  • tsc --noEmit and typecheck:test pass
  • No secrets, API keys, or private keys included
  • Breaking change? Behaviour with HOME unset; see above

Testing standard (docs/TESTING.md §7)

  • One behaviour per test; no test.skip / .only; tests run in a temp cwd with an isolated HOME and never touch the real ~/.perp or ~/.ows
  • New tests fail before the fix (run against origin/main sources by the author and again by the reviewer). Exceptions, stated plainly: the --help / --version tests only fail against the first commits of this branch, since those already worked on main; the "~" literal guard matches 21 places on main but nothing on the previous branch head.
  • No fixture change

An independent review approved the first three commits and raised one MEDIUM (orphaned legacy state) and five LOW items. All are applied in the last two commits, which were read by the session lead but not re-reviewed.

Live ↔ Unit Test Cross-Validation

The reviewer ran perp --json and perp-mcp with HOME cleared in a scratch cwd: a clean error, exit 1, and no ./~ directory. Not tested on Windows.

Merge note

CHANGELOG.md [Unreleased] conflicts with the other open PRs (keep both sides). This PR touches many files lightly, so small textual conflicts with #97, #99 and #100 are likely.

🤖 Generated with Claude Code

Hiksang and others added 5 commits November 13, 2026 13:55
…n error

`resolve(process.env.HOME || "~", ".perp")` does not expand "~": with HOME
unset (cron, systemd, containers) settings, Aster/RISEx nonces, the execution
log, the bot quarantine, arb state, agent locks and keystores were read from
and written to ./~/.perp in the working directory (SSOT Rule #2).

src/paths.ts (homeDir/perpDir/perpPath/owsPath) resolves at call time from
HOME, or USERPROFILE on Windows when HOME is unset, and throws INVALID_PARAMS
with remediation otherwise (also for a non-absolute HOME). os.homedir() is
not used: its passwd fallback would reach the real home when HOME is cleared.

Module-level path constants become functions; QUARANTINE_FILE becomes
quarantineFile(). Catch blocks that only report a failure (execution log,
quarantine persist / fail-closed entry) name the path literally so they
never throw from the error path.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… the helper

- perp / perp-mcp load ~/.perp/.env via perpPath(); the CLI loads it inside
  loadStartupSettings() so an unset HOME is printed like a bad settings.json
  (text or --json envelope, exit 1).
- commands/init ENV_FILE becomes envFile(); wallet.json store and the OWS CLI
  lookup (~/.ows/bin/ows, which could run a binary under ./~/.ows/bin in the
  working directory) go through perpPath()/owsPath().
- jobs, equity history, bot logs, trade journal and funding history used
  os.homedir(); they now resolve at call time from the same helper.
- funding-history.test points HOME at its temp dir instead of mocking os.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s.ts

Source-scanning test (readme-cli-sync style): fails if `HOME || "~"` /
`HOME ?? "~"`, a direct process.env.HOME read or an os.homedir() call
reappears in src outside src/paths.ts. CHANGELOG [Unreleased] entry for the
behaviour change (HOME unset now errors).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Runs without HOME left quarantine entries, Aster/RISEx nonces, Lighter
keystores and settings in <cwd>/~/.perp. Reading ~/.perp instead would
un-quarantine symbols and restart nonces (fail-open). assertNoLegacyCwdPerpDir()
refuses with INVALID_PARAMS naming both directories; nothing is moved
(Rule #2). One existsSync at CLI / MCP startup, before any command, never on
an order path. Only the cwd can be checked (noted in the CHANGELOG).

Also: perp-mcp reports HOME / legacy errors as one stderr line + exit 1
instead of a stack trace; `perp --help` / `--version` work without HOME;
win32 HOME-before-USERPROFILE order explained in paths.ts and CHANGELOG.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… test

The execution-log / quarantine catch blocks name $HOME/.perp/... (they must
not call the throwing helper). The guard also rejects `const { HOME } =
process.env` and a literal "~" segment before ".perp" / ".ows".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

⚠️ Large PR — 761 changed lines across 39 files (lockfile and snapshots excluded).

If it holds more than one logical change, consider splitting it for review.

Hiksang and others added 2 commits November 15, 2026 18:36
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Hiksang
Hiksang merged commit 1fb98e4 into main Nov 15, 2026
4 checks passed
@Hiksang
Hiksang deleted the fix/perp-dir-path branch November 15, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant