Skip to content

Sweep private CODEX_HOME directories left by dead owners - #241

Merged
yansfil merged 2 commits into
mainfrom
fix/codex-home-sweep
Sep 28, 2026
Merged

yansfil merged 2 commits into
mainfrom
fix/codex-home-sweep

Conversation

@yansfil

@yansfil yansfil commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Related: #116, #92

Summary

Closes #116.
A codex app-server's private CODEX_HOME is removed in Drop, which a kill -9 never runs, so homes piled up in $TMPDIR.
Each CodexAppServerBackend now sweeps, on a detached thread when it is built, the hide-ai-codex-home-<pid>-<nanos> directories whose owner pid is gone.
A live pid keeps its homes even if the pid was reused; unparsed names, non-directories, symlinks and other users' entries are never touched.
One sweep removes at most 32 and logs ai.codex_home.swept (removed, bytes, failed, unreadable, capped, first error kind), which hided now writes to Logs/core.jsonl; no screen changes, so no screenshot.

before: dead=344 live=3 total_kb=300088   (this machine's $TMPDIR, pid checked with kill -0)
after:  dead=0   live=2 total_kb=5512     (after the workspace test lane built backends)

Review

  • Please decide: a reused pid that is alive keeps the dead owner's home (the task said to err toward keeping); the issue proposed also sweeping when the live pid "is not a hide-ai process". I did not add process-identity checks.
  • Please decide: one summary event per sweep (count, bytes) instead of the issue's one line per removed directory with its pid, as the batch task asked.
  • Please decide: the core sink forwards every AI event, not only the sweep's, so Project Memory's ai.attempt/ai.request.finished now also land in core.jsonl (1 MB rotating); forward only failures if that is too much.
  • Files to watch: hide-ai/src/codex_home.rs sweep/pid_alive - failure path that deletes files; check the keep-on-doubt rules.
  • Files to watch: herdr-core/src/ai.rs DiagnosticLogSink - replaces three NoopLogSinks; diagnostic! only try_sends, and no router or backend here runs under Mutex<Runtime> (Settings reader worker, Project Memory write thread).
  • Files to watch: hide-ai/src/codex.rs CodexAppServerBackend::new - spawns the sweep thread and never blocks the caller.

Evidence

  • Confirmed: codex_home unit tests with fixture roots: a dead owner (a reaped child's pid) is removed and the auth.json symlink target survives; a live owner (own pid), five malformed names, a plain file and a top-level symlink are kept; pid 1 (EPERM) is kept; a limit of 2 over 3 homes removes 2 then 1; an unremovable dead home logs failed=1 with first_error=permission denied.
  • Confirmed: ai::tests in herdr-core: an event becomes {"kind", "component": "ai", ...} with absent fields omitted.
  • Confirmed: cargo test -p hide-ai all pass (45 unit, 13 claude_cli, 8 codex_app_server, 3 process_ownership); scripts/verify-cargo.sh lint clean; check-no-workstation-identity.sh, check-capability-readers-off-lock.sh pass.
  • Confirmed: real-path sweep on this machine: the workspace test lane builds real backends, and the dead-owner home count went 344 to 0 while both live-owner homes stayed.
  • Not confirmed: locally scripts/verify-cargo.sh test --no-fail-fast passes every binary except hide-agent-hooks hook_deadline, a regression on main from 23aca6d (not this change; hide-agent-hooks reaches hide-ai through hide-memory); CI's rust workspace lane passed on the first push.
  • Not confirmed: an ai.codex_home.swept line in a running hided's core.jsonl; the sink is covered by the unit test and the shared diagnostic! path, not a live daemon run.
  • Not confirmed: no live codex on this machine, so no real kill -9 of a codex owner followed by a restart; Linux behavior (pid namespaces with a shared /tmp would make a live owner look dead) is untested.
More evidence

First code review found no path that deletes a live owner's home or follows a symlink out of $TMPDIR (remove_dir_all on rustc 1.97.1 walks with O_NOFOLLOW).
Fixed from it: the doc omitted the thread-spawn case of sweep_failed, and wrongly claimed the CI kill test leaves a home (its owner is a stand-in that never creates one).
Second review (of the log routing) found the records lacked the component key other core diagnostics carry; fixed.
Follow-ups: 32 or more permanently unremovable homes that sort first would block the sweep on every start; integration tests sweep the developer's real $TMPDIR (harmless, dead owners only); an orphaned app-server can outlive a killed owner for a few seconds, during which its home may be swept; the context-label plugin's EventLog could reuse the new Serialize instead of its own key=value format.

AI tooling: mostly AI tools; the keep-on-doubt rules and the before/after counts were checked against this machine's $TMPDIR.

A codex app-server's private home is removed in Drop, which a kill -9 never
runs, so leftovers accumulated in $TMPDIR (344 dead-owner homes, about 290 MB,
on one machine). Each CodexAppServerBackend now sweeps them on a thread of its
own when it is built: a hide-ai-codex-home-<pid>-<nanos> directory is removed
only when it is a plain directory the user owns and no process has that pid,
so a live or reused pid keeps its homes. At most 32 are removed per sweep, and
the result is logged as ai.codex_home.swept with counts and bytes.

CodexHome moves to its own module with the sweep and its fixture tests.
…failed

hided built its provider routers and backends with a no-op log sink, so
ai.codex_home.swept never reached any log in the product. The core now
forwards every AI event to its diagnostic log (Logs/core.jsonl) as a record
with component ai and the event name as kind; AiLogEvent serializes without
its absent fields for that.

The sweep now counts directory listing errors instead of dropping them and
logs the kind of the first removal or listing error, and a test pins that a
pid belonging to another user (EPERM) keeps its home.
@yansfil
yansfil merged commit 3b6d19b into main Sep 28, 2026
7 checks passed
@yansfil
yansfil deleted the fix/codex-home-sweep branch September 28, 2026 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hide-ai: sweep private CODEX_HOME directories left by an owner that died without Drop

1 participant