fix(cli): replace the CLI session and generated remote config instead of rewriting them in place

Merged
TH
7d ago
+104-12

Problem

writeCliSession and writeGeneratedRemoteConfig truncated the destination and wrote into it. A reader that opened the file during the write saw an empty or partial file:

  • readCliSession returns null for a partial record, so a command that runs while auth login writes reports that no session exists.
  • A generated remote config has a content-addressed name. Two connect runs with the same profile write the same path, so one can read the profile while the other has it truncated.

The window is small. It matters most on a shared host that runs several agents against one state directory.

Change

Both writers publish through publishFileSync from @agent-device/host-kit/file (temporary sibling, then rename). This is the rule that #4783 set for sensitive files in the state directory. The best-effort chmod after the write is deleted: the helper sets the temporary file to exactly 0600 before the rename (exactMode), under any umask, and an older, wider file is replaced. That chmod is no longer best-effort, on purpose: #4783 admits the state directory as an owned, private directory before these writes, so a session file that cannot be made private is now an error and not a silently wider file.

Evidence

  • New tests hold a file descriptor that was opened before the rewrite, then assert that it still reads the earlier complete contents, that the path has the new contents, and that no temporary sibling remains.
    • src/cli/__tests__/auth-session.test.ts: "a rewritten CLI session replaces the file, so a reader never sees a partial record". It also asserts that a 0644 session file ends with no group or other bits.
    • src/cli/connection/generated-config.test.ts: "a rewritten generated remote config replaces the file, so a reader never sees a partial profile".
  • With the production change reverted, both tests fail (2 failed, 16 passed). On the branch, 18 passed.
  • Lint, typecheck, the layering scan and the eager-closure gate (913 tests) pass locally. No allowlist, baseline or budget rows.

Size

Production: +12 āˆ’12, net 0 lines. No new module on an entry closure, so the built CLI startup cost does not change.

Part of #4649 (follow-up to #4783).

šŸ¤– Generated with Claude Code


Autofix with [code]smith
Open in [code]smith Ā· Need help on this PR? Tag @codesmith-bot with what you need.