Skip to content

fix(uninstall): make --purge remove installer binaries, PATH edits and daemon state - #4320

Closed
kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:claude/uninstall-purge-bin-host
Closed

kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:claude/uninstall-purge-bin-host

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Before this change, gaia uninstall --purge left gaia-tui and gaia-agent on PATH. The installer's PATH block also stayed in every shell rc file (or its entries stayed on the Windows user PATH), and ~/.gaia/host kept the daemon's client token and custody store. Now the purge stops the daemon, then deletes ~/.gaia/bin, ~/.gaia/traces and the daemon state. It also removes exactly the installer's rc block or PATH entries and leaves every other line and entry as it was.

One decision a reviewer should confirm: the custody database (agent memory, sessions, audit history) is now deleted with host/. It still survives an agent uninstall, which is the case its docstring was written for. A full product purge already deletes chat history, so keeping this store seemed inconsistent. The CLI reference warns about it, and the reasoning is in the docstring.

🔍 Technical details
  • rc cleanup matches the marker line plus an export PATH="$PATH:…" line whose entries are all ~/.gaia/venv/bin / ~/.gaia/bin. That covers the current shape and the pre-0.23 venv-only shape across .zshrc, .bashrc, .bash_profile and .profile. It also drops the blank line the installer wrote before the marker. Hand-edited exports are kept and reported. Symlinked rc files are edited through the link.
  • Windows edits HKCU\Environment\Path through winreg, which does not expand values. The value kind is kept and the change is broadcast with WM_SETTINGCHANGE. installer/nsis/gaia-path.ps1 isn't in the wheel, so it can't be called at runtime. This applies the same rules as that script.
  • A GAIA_HOME other than ~/.gaia skips the rc/PATH cleanup, because those edits still point at ~/.gaia. A GAIA_DAEMON_HOME outside the GAIA home is skipped with a note, so the containment roots are not widened.
  • If the daemon can't be stopped, or its state can't be checked because psutil/requests are missing, the command exits 1 before deleting anything.
  • The test module now clears inherited GAIA_HOME/GAIA_DAEMON_HOME. Before, 18 existing tests failed whenever those were exported, and the isolation rule for test runs requires exporting them.
  • TestPurgeRemoval::test_refuses_to_purge_a_live_embedded_instance also fails on unmodified main locally. This change doesn't touch it.

Test plan

  • New tests fail on main (13 of 14; the GAIA_HOME guard test passes on both) and pass here: pytest tests/unit/installer/test_uninstall_command.py -q, run with GAIA_HOME/GAIA_DAEMON_HOME set to temp dirs
  • pytest tests/unit/installer/ tests/unit/test_lemonade_macos_uninstall.py tests/unit/test_amd_gaia_urls.py tests/unit/test_daemon.py tests/unit/test_daemon_client.py -q: all pass except the embedded-instance test above, which also fails on main
  • black, isort, flake8 and pylint (repo configs) are clean on the changed files
  • gaia uninstall --purge --dry-run lists ~/.gaia/bin, ~/.gaia/traces and ~/.gaia/host and changes nothing
  • On a machine set up by install.sh: gaia uninstall --purge --yes removes the rc block, and a new shell no longer finds gaia-tui
  • On Windows after install.ps1: the user PATH loses only ~\.gaia\venv\Scripts and ~\.gaia\bin

Fixes #4225, fixes #3290

…d daemon state

After `gaia uninstall --purge`, gaia-tui and gaia-agent stayed on PATH, every
shell rc file kept the installer's PATH block (or the Windows user PATH kept its
entries), and ~/.gaia/host kept the daemon's client token and custody store.

- Purge now covers ~/.gaia/bin, ~/.gaia/traces and the daemon's host dir. A
  running daemon is stopped first so it cannot rewrite instance.json; if it
  won't stop, nothing is deleted.
- The custody database goes with host/ on purpose: a full product purge
  removes all user data. It still survives an agent uninstall.
- The "# Added by GAIA installer" block is removed from .zshrc, .bashrc,
  .bash_profile and .profile, in both the current and the pre-0.23 venv-only
  shape. The rest of each file stays byte-identical, symlinked rc files are
  edited in place, and hand-edited exports are left alone with a note.
- On Windows, the installer's entries are removed from HKCU\Environment\Path
  with the value kind kept and other entries (including %VARS%) unexpanded.
- A GAIA_HOME or GAIA_DAEMON_HOME outside ~/.gaia leaves the installer edits
  and daemon state alone rather than widening the deletion roots.

Fixes amd#4225, fixes amd#3290
@github-actions github-actions Bot added documentation Documentation changes tests Test changes daemon Daemon supervisor / sidecar control plane labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions — the logic is careful and unusually well tested for a destructive command, but two things should close before merge: the maintainer decision the author flagged, and the two unchecked test-plan boxes.

gaia uninstall --purge now finishes the job: it stops the daemon, deletes the installer's binaries, traces and daemon state, and removes exactly the PATH lines the installer added — leaving every other line and entry byte-identical.

What needs a decision: the purge now deletes the daemon's custody database, which holds agent memory, sessions and audit history. The author raised this deliberately and the docs warn about it, but it reverses a stated design intent that audit rows persist, so it needs an explicit yes from a maintainer rather than a silent merge.

What's worth changing: if one shell startup file can't be read, the command currently refuses to delete anything at all — the user's actual request (remove my data) is blocked by a side task (tidy up PATH). Failing loudly is right; failing totally is not, especially since the same PR already handles the write-side failure the gentler way. Report the unreadable file, skip it, purge the rest, and still exit non-zero.

Before merge: the Windows user-PATH rewrite and the real shell-file removal are the two highest-risk paths in this change and neither has run outside a hand-written stand-in for the Windows registry. Those are exactly the two unchecked boxes in the test plan — worth finishing rather than trusting the fake.

Real-world evidence

No automated evidence bundle was produced for this run, so the verdict rests on static review plus the tests in the diff. The PR's own test plan asserts the dry-run lists the new paths and changes nothing, but the output isn't shown; the two on-machine items (POSIX rc removal after install.sh, Windows PATH after install.ps1) are explicitly unchecked. For a command whose failure mode is deleting or corrupting a user's shell config, pasting the real gaia uninstall --purge --dry-run output and the before/after of one rc file would be cheap and convincing.

🔍 Technical details

Issues

🟡 A single unreadable rc file (or registry read) aborts the whole purge (src/gaia/installer/uninstall_command.py:270, :392)

_rc_edit_candidates and _windows_path_entries_to_remove raise UninstallPlanError at plan time, and run() turns that into EXIT_FS_ERROR before a single path is deleted. A root-owned or mode-000 ~/.profile therefore blocks removal of ~/.gaia/chat, documents, venv — none of which the unreadable file has anything to do with. The write side of the same feature already does this better: _edit_rc_file reports the failure, returns False, and execute_plan downgrades the exit code via all_ok. Suggest making the read path match — skip the file with a [error] line, continue the purge, exit non-zero. Same loudness, no stranded data.

🟡 The Windows PATH rewrite is only ever exercised against _FakeWinreg (tests/unit/installer/test_uninstall_command.py:869)

_FakeWinreg is a good-faith stand-in, but it is the contract as assumed, not as winreg implements it. Untested for real: that QueryValueEx round-trips REG_EXPAND_SZ unexpanded through SetValueEx, and that SendMessageTimeoutW with an un-argtypes'd HWND_BROADCAST actually returns non-zero. This is the #1655 shape from CLAUDE.md — the mock proves the call happened, not that it is valid. The test-plan box for a real install.ps1 machine covers it; please check it.

Nits

🟢 The GAIA_DAEMON_HOME note fires when that variable was never set (src/gaia/installer/uninstall_command.py:669)

With a custom GAIA_HOME and a default ~/.gaia/host, host_inside is false and the user is told their daemon state is outside the GAIA home "(GAIA_DAEMON_HOME)" — a variable they never touched. Naming the actual cause would read better; test_custom_gaia_home_leaves_installer_edits_alone doesn't assert the text, so this is invisible to CI.

🟢 The Windows data-loss comment now documents the wrong call (src/gaia/installer/uninstall_command.py:1396)

The new _stop_daemon call landed between the comment about gaia.log file handlers and the _close_gaia_log_handlers call it describes.

    if plan.stop_daemon and not _stop_daemon(printer=printer):
        return EXIT_ABORTED

    # Windows data-loss guard: gaia.log is held open by a FileHandler
    # attached by gaia.logger. Detach + close it before rmtree walks into
    # ~/.gaia so the file can actually be removed.
    if purge:

🟢 ~/.gaia/traces/ is missing from the "Where things live" table (docs/guides/install.mdx:177)

cli.mdx names traces as a purge target; the install guide's table doesn't list the directory at all.

| Terminal hub and agent binaries (one-line installer) | `~/.gaia/bin/` |
| Agent run traces | `~/.gaia/traces/` |
| Daemon state (token, custody database, logs) | `~/.gaia/host/` |

Strengths

  • The rc-file handling is the careful version. Matching the marker plus a recognised export shape (current and pre-0.23), keeping hand-edited blocks and reporting them, resolving symlinks so a dotfiles repo is edited in place rather than replaced, and surrogateescape so a non-UTF-8 .profile survives byte-identical — each of those is a real failure mode, and each has a test that asserts bytes, not just absence.
  • Windows PATH entries are normalised before comparison, not string-matched. C:\Users\ME\.gaia\venv\Scripts\ is removed and ...\.gaia\binaries is kept — the prefix-collision bug install.ps1's own -notlike "*$_*" check would hit.
  • The daemon is stopped before its state is deleted, and a daemon that won't die aborts rather than half-deleting. test_running_daemon_is_stopped_before_host_is_deleted asserts the ordering (instance.json still present at shutdown time), which is the invariant that actually matters; the mocked request_shutdown / wait_until_gone / terminate_instance signatures match the real gaia.daemon API.
  • The autouse fixture clearing inherited GAIA_HOME / GAIA_DAEMON_HOME is a real fix, not incidental cleanup — it makes 18 pre-existing tests survive the environment the project's own isolation rule tells you to export.

--purge read every shell rc file at plan time and raised on the first
one it could not open, so the command exited before deleting anything.
A root-owned or mode-000 ~/.profile therefore kept ~/.gaia/chat,
documents and venv on disk -- none of which that file has anything to
do with. The user asked to remove their data; tidying up PATH is a side
task and must not veto it.

The write side already had this right: _edit_rc_file reports the
failure, returns False, and execute_plan downgrades the exit code. The
read side now matches -- the unreadable file is recorded on the plan,
printed as [error], and the run still exits EXIT_FS_ERROR. Same
loudness, nothing stranded.

UninstallPlanError had no remaining raise site afterwards, so the class,
its handler and its docstring clause are gone rather than left as dead
plumbing.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Fixed the "one unreadable file blocks the whole purge" finding and resynced with main. I am not merging this: after reading the custody-database question closely, I think it genuinely needs a maintainer's yes, and I've set out below exactly what I could and couldn't verify so that decision is a short one.

The decision I'm leaving to you

--purge now deletes the daemon's custody database — agent memory, sessions, and audit history — along with the rest of ~/.gaia/host.

The case for it is strong and I want to say so plainly: --purge already deletes chat history, documents, and the venv, and the code comment that guards this store scopes its promise to "survives ... an agent uninstall" — which this PR does not change. On a plain reading, nothing is being reversed.

What stops me signing it off is the clause right next to it: "audit rows must persist — §0.19". I searched the tree and could not find §0.19 — no spec section, no governance document, nothing that defines what that requirement actually covers. So I can't confirm whether it's scoped to agent uninstall (in which case this PR is fine) or is a broader retention rule that a full purge is also meant to honour. That's not a code-quality judgement I can make from the code; it needs whoever wrote §0.19. Combined with the fact that the deletion is irreversible and lands in a product that ships a governance layer, guessing costs more than asking.

Concretely, what would close this: confirm that §0.19's audit-retention requirement is scoped to agent uninstall and does not bind gaia uninstall --purge. If it is, this ships as-is. If it isn't, host/ needs to come off the purge list, or the custody DB carved out of it.

🔍 Technical details

What I fixed

🟡 A single unreadable rc file (or registry read) aborted the whole purge. _rc_edit_candidates and _windows_path_entries_to_remove raised UninstallPlanError at plan time, which run() turned into EXIT_FS_ERROR before a single path was deleted — so a root-owned ~/.profile kept ~/.gaia/chat, documents and venv on disk.

Both now return their messages instead of raising. UninstallPlan gained blocked_edits, execute_plan prints each as [error] and sets all_ok = False, so the run still exits EXIT_FS_ERROR — the same shape _edit_rc_file already used on the write side. Same loudness, nothing stranded.

UninstallPlanError had no remaining raise site afterwards, so the class, its except clause in run(), and the Raises: entry in build_plan's docstring are removed rather than left as dead plumbing.

Two tests in TestUnreadablePathEditDoesNotBlockThePurge: the unreadable file is reported rather than raised (with an actionable message), and a purge carrying a blocked edit still deletes chat and venv while exiting EXIT_FS_ERROR. The second mirrors the existing test_containment_refusal_does_not_abort_the_rest_of_the_plan, which is the same principle for deletes.

What I could not verify — and it's the thing you flagged

🟡 The Windows PATH rewrite still has only _FakeWinreg behind it. I deliberately did not exercise the real path: the only honest test is gaia uninstall --purge on a machine set up by install.ps1, and running that here would destroy a real GAIA install and rewrite a real user PATH. Both of those test-plan boxes are still the right gate, and they still need a throwaway machine.

Worth knowing while you plan that: on Windows, the POSIX rc tests cannot pass at all, so the Windows CI leg gives no signal on them either. _installer_posix_path_dirs builds its entries with str(Path(...)), which yields \fake\home\user\.gaia\bin on Windows, while the test fixture writes the rc line with forward slashes — nothing matches, so the installer block is never recognised. The production path is unaffected (_is_windows() routes Windows to the registry instead), so this is a harness limitation rather than a bug, and I left it alone rather than widen the diff. It does mean the rc-removal logic is only ever proven on POSIX.

Merge and tests

Merged origin/main (clean, no conflicts). Net diff is 5 files. No conflict markers in the tree; uninstall_command.py AST-parses.

pytest tests/unit/installer/ tests/unit/test_daemon.py tests/unit/test_daemon_client.py → 189 passed, 23 skipped, 5 failed, 3 errors.

I checked the failures against the branch tip before my change and introduced none — the set is identical. They're all Windows-only artifacts: the POSIX shebang-resolution tests, test_sh_parses_under_dash (needs dash), the three POSIX rc tests above, and TestGaiaHomeEnvVar. Merging main took this file from 11 failures down to 4, so main had already fixed several.

python util/lint.py --black --isort clean.

Nits from the review, not addressed

Left alone deliberately to keep the diff on the blocking issue: the GAIA_DAEMON_HOME note that fires when the variable was never set, the Windows data-loss comment that now sits above the wrong call, and the ~/.gaia/traces/ row missing from the install guide's table. All three are one-liners worth folding in once the custody question is settled.

@itomek

itomek commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Landed on main as #4413 (merge commit cb92072bf), which carries this branch plus the fixes below. Every commit here is an ancestor of what merged — git merge-base --is-ancestor confirms d4c253ce, 3cb30004 and 9c5ba1e6 — so nothing was dropped or rewritten, and your authorship is intact on all three.

The custody-database question you escalated is answered, and it went your way. §0.19 does exist — it is docs/plans/agent-ui-agent-capabilities-plan.md:606-616, "Audit trail — a host-custody sink, not agent-private". Its whole subject is agent-private storage, and the "uninstall" in it is the agent uninstall, so it does not bind gaia uninstall --purge. The quote is in #4413's body so the next reader doesn't have to re-find it.

Safe to close this one.

🔍 What changed on the way in
  • The black failure is fixed — tests/unit/installer/test_uninstall_command.py:1760 wanted single outer quotes. It came in with 3cb30004 and contradicted this PR's own ticked "black clean" box.
  • All three deferred bot nits are folded in. The GAIA_DAEMON_HOME note was tightened further than the bot asked: the bot only spotted the custom-GAIA_HOME path, but a symlinked ~/.gaia/host reaches the same branch with neither variable exported, so the note now names a variable only when one is actually set. A new test pins all three readings and fails on the old wording.
  • The purge was re-run end to end against a populated throwaway $HOME — custody DB + WAL, agent dirs, traces, bin, PATH edits in .bashrc/.profile — and survived five adversarial escape attempts (symlink out of $HOME, relative ../, GAIA_HOME elsewhere). pytest tests/unit/installer/ is 205 passed, 6 skipped. Full transcript is on fix(uninstall): --purge removes installer binaries, PATH edits and daemon state #4413.

Still unverified, and left unchecked rather than ticked: the Windows install.ps1 PATH removal. winreg is only ever reached through a monkeypatched _FakeWinreg in the tests, so REG_EXPAND_SZ round-tripping has never run against a real registry. That needs a throwaway Windows machine.

kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Sep 29, 2026
…emon state (amd#4413)

`gaia uninstall --purge` used to leave the machine half-uninstalled:
`gaia-tui` and `gaia-agent` stayed on PATH, the installer's `export
PATH` block stayed in every shell rc file, and `~/.gaia/host` kept the
daemon's client token and custody store. The purge now stops the daemon
first, then deletes `~/.gaia/bin`, `~/.gaia/traces` and the daemon
state, and removes exactly the installer's rc block (or its Windows PATH
entries) while leaving every other line byte-identical. A shell rc file
it cannot read is reported and skipped rather than aborting the whole
purge, so tidying up PATH can no longer strand the user data the command
was asked to delete.

This replaces amd#4320 — same work, rebuilt on current `main` with the
maintainer's open question answered and the three review nits folded in.
Please close amd#4320 in favour of this.

**The custody-database question is answered: §0.19 does not bind `gaia
uninstall --purge`.** The clause Kalin could not locate is in
`docs/plans/agent-ui-agent-capabilities-plan.md`, §0.19 "Audit trail — a
host-custody sink, not agent-private" (lines 606-616). Its subject is
agent-*private* storage, and the uninstall it names is the *agent*
uninstall — §0.20 "Uninstall — data lifecycle" opens "§0.5 uninstall
stops the sidecar + removes the binary". §0.19 argues the audit sink
must move to host custody so an agent uninstall cannot erase it; it says
nothing about the whole-product purge, which already deletes chat
history and documents. Verbatim:

> and email's action log is agent-*private* (§0.9) so the dashboard
can't read
> it and uninstall deletes it. **Every consequential action** ... must
be appended to a **host-owned audit
> log** via `POST /host/v1/audit`

<details>
<summary>🔍 Technical details</summary>

**Nits folded in** (all three the author listed as deferred):
- The "daemon state is outside the GAIA home" note named
`GAIA_DAEMON_HOME` unconditionally. Two other paths reach that branch
with it unset — a custom `GAIA_HOME`, and a symlinked `~/.gaia/host`
with neither variable exported. It now names a variable only when one is
set. A new test pins all three readings and fails on the old wording.
- The `gaia.log` file-handler comment had a later `_stop_daemon` call
inserted between it and the call it describes; moved back.
- `~/.gaia/traces/` added to the install guide's "Where things live"
table.

**Lint:** `black` failed on `main`'s CI for this branch's predecessor —
`rc.write_text("export PATH=\"...\"")` at
`tests/unit/installer/test_uninstall_command.py` wants single outer
quotes. Fixed; `black` and `isort` are clean here.

**Ancestry** — every commit from amd#4320 is an ancestor of this branch,
brought in by merge so authorship is preserved:

```
$ for c in d4c253c 3cb3000 9c5ba1e; do git merge-base --is-ancestor $c HEAD && echo "$c ANCESTOR"; done
d4c253c ANCESTOR
3cb3000 ANCESTOR
9c5ba1e ANCESTOR
```
</details>

## Test plan

- [x] `pytest tests/unit/installer/` → **205 passed, 6 skipped**
- [x] `.venv/bin/python util/lint.py --black --isort` → both PASS
- [x] Real purge against a populated, fully isolated `HOME` — removes
`bin`, `traces`, `host` (custody.db + WAL + shm + token), edits
`.bashrc`/`.profile` byte-exactly, leaves unrelated files unchanged by
md5 (transcript below)
- [x] 5 adversarial containment cases — symlinked `traces/`, symlinked
`host/`, `GAIA_HOME` escaping via `../../`, `GAIA_HOME=$HOME`,
`GAIA_DAEMON_HOME` outside. The out-of-`HOME` tree is byte-identical
afterwards (transcript below)
- [x] `tests/unit/` full suite: 32 failed / 17721 passed on this branch
vs 31 failed / 17705 passed on a clean checkout of `main` at 143c697.
The single extra is
`test_chat_completion_stream::test_client_disconnect_cancels_the_agent`,
which is order-dependent — it fails on plain `main` too when that file
is run alone, and an unrelated PR (amd#4414) hits it in CI on the same
`main`
- [ ] **On Windows after `install.ps1`: the user PATH loses only
`~\.gaia\venv\Scripts` and `~\.gaia\bin`.** Not verified — needs a
throwaway Windows machine. The registry path has only `_FakeWinreg`
behind it; nobody has run it against real `winreg`.

Fixes amd#4225, fixes amd#3290

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

daemon Daemon supervisor / sidecar control plane documentation Documentation changes tests Test changes

Projects

None yet

3 participants