Skip to content

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

Merged
thymikee merged 3 commits into
mainfrom
t3/cli-state-atomic-writes
Nov 20, 2026
Merged

thymikee merged 3 commits into
mainfrom
t3/cli-state-atomic-writes

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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.

View guided diff Turn on auto-fix

… of rewriting them in place

Both writers truncated the destination and wrote into it, so a concurrent
reader could see an empty or partial file. They now publish a complete file
through the shared temp-and-rename helper.

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

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.44 MB 6.44 MB +83 B
Package (unpacked) 6.44 MB 6.44 MB +83 B
Package (download) 1.96 MB 1.96 MB -1 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 18.4 ms 22.6 ms +4.2 ms
CLI --help 41.1 ms 50.0 ms +8.8 ms

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on 045ce74. The verdict and the full list are in the summary comment.

Comment thread src/cli/auth-session.ts Resolved
Comment thread src/cli/auth-session.ts Resolved
… any umask

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

Copy link
Copy Markdown
Member Author

The PR is ready at f880ad6, with no blocking problems in the code. CI is still running (Smoke Tests and Coverage), and none of those jobs has failed. Please wait for them to go green before merge. The diff only touches the auth-session and generated remote config writes, which device smoke routes do not run, and Coverage runs the new unit tests. There are no conflicts.

Windows has no CI here: libuv opens files with FILE_SHARE_DELETE, so the rename-over-open-file replace should succeed, but it is not exercised.

Not blocking: you can take or leave these. (1) The old chmod in auth-session.ts was wrapped in try/catch, and the exactMode chmod in atomic-publish.ts is not, so login and connect now throw on a state directory whose filesystem rejects chmod with EPERM. That is fine if #4783 admission already refuses those roots. If not, please say in the PR body that the best-effort chmod is gone on purpose. (2) The mode assertions in auth-session.test.ts and auth-session-umask.test.ts are not guarded for win32, unlike the sibling tests, so they would fail on Windows. (3) In auth-session-umask.test.ts the writes and the umask change run before the try/finally, so a throw leaks the temporary directory. Opening the try right after mkdtempForTestSync fixes it.

On the earlier threads: the exact-mode one is fixed at this head, since exactMode: true now gives 0600 under umask 0700, so it can be resolved: #4797 (comment). The symlink one does not apply, because rename replacing a linked cli-session.json is the intended #4783 rule and root admission refuses planted links, so it can be resolved too: #4797 (comment).

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
…temp dir inside its cleanup

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 92d5720

Since the review of 045ce74: 1 new · 0 still open · 2 no longer raised (1 marked fixed)

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
src/cli/auth-session.ts:299 🆕 Non-best-effort chmod now fails login/connect on filesystems without chmod

Reviewed with this repository's own Sentinel configuration (74771e19193d).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 92d5720. The verdict and the full list are in the summary comment.

Comment thread src/cli/auth-session.ts Resolved
@thymikee
thymikee merged commit 78d628b into main Nov 20, 2026
18 checks passed
@thymikee
thymikee deleted the t3/cli-state-atomic-writes branch November 20, 2026 11:34
thymikee added a commit to pcbeingused333/agent-device that referenced this pull request Nov 20, 2026
* origin/main: (70 commits)
  refactor(apple-runner): one RUNNER_ERROR_CODES row per runner error code with parity/docs gates (callstack#4774)
  fix(android): require acknowledgements for test IME typing (callstack#4722)
  feat(scroll): add scroll refresh for pull-to-refresh (callstack#4770)
  refactor(host-kit): add request Scope and use it for the wait capture deadline (callstack#4794)
  fix(android): let permission all change nothing on an app that declares no permissions (callstack#4796)
  fix(cli): replace the CLI session and generated remote config instead of rewriting them in place (callstack#4797)
  fix(host-kit): accept a group-writable script directory the user owns (callstack#4799)
  fix(cli): escape control characters at the CLI's stdout and stderr writes (callstack#4778)
  fix(audit): do not report rows under a persistent bar that scrolling reveals (callstack#4775)
  fix(daemon): refuse an unsafe state directory before first use (callstack#4783)
  refactor: settle remaining single-deadline races through settleWithin (callstack#4788)
  refactor(host-kit): settle deadline races via settleWithin with R87 race ratchet (callstack#4766)
  fix(android): authenticate and bound persistent snapshot helper sessions (callstack#4729)
  fix(daemon): bound upload ingress and finalization (callstack#4721)
  chore(gates): ratchet wire-published reason literals by value (R89) (callstack#4779)
  feat(audit): evaluate role-less clickable views on Android (callstack#4780)
  fix(mutation): keep umask-setting tests out of Stryker's thread pool (callstack#4785)
  feat(audit): report controls that share one accessible name (callstack#4771)
  fix(contracts): pair open runtime hints with their apply operation (callstack#4754)
  refactor(daemon): report startup progress over the launch channel instead of marker files (callstack#4777)
  ...
thymikee added a commit that referenced this pull request Nov 20, 2026
* origin/main:
  feat(gesture): hold before the move with gesture pan --hold-ms (#4767)
  docs: clarify device verification and security proposals (#4805)
  fix(daemon-client): say that a hung daemon is stopped before ensureDaemon starts a new one (#4803)
  test: remove five timing races from CI (#4800)
  refactor(remote): save a remote script from memory with the local publisher (#4802)
  refactor(apple-runner): one RUNNER_ERROR_CODES row per runner error code with parity/docs gates (#4774)
  fix(android): require acknowledgements for test IME typing (#4722)
  feat(scroll): add scroll refresh for pull-to-refresh (#4770)
  refactor(host-kit): add request Scope and use it for the wait capture deadline (#4794)
  fix(android): let permission all change nothing on an app that declares no permissions (#4796)
  fix(cli): replace the CLI session and generated remote config instead of rewriting them in place (#4797)
  fix(host-kit): accept a group-writable script directory the user owns (#4799)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant