Fix misleading docs and error for ClaudeAgentOptions.user - #1233
Open
mmjerge wants to merge 1 commit into
Open
Conversation
The user option is passed to subprocess.Popen(user=...). Popen resolves
it with getpwnam() and runs the CLI as that OS account. The docstring
called it a session user identifier. Callers who believed it got:
CLIConnectionError: Failed to start Claude Code: "getpwnam(): name
not found: 'customer-42'"
Correct the docstring. Translate KeyError, PermissionError and
ValueError from the spawn into an error that names options.user and
explains the semantics, only when the option is set. Add regression
tests for both paths; the first fails without the fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ClaudeAgentOptions.usersays it is an "optional user identifier associated with the session." However, this is incorrect. The value goes tosubprocess.Popen(user=...). Popen resolves it withgetpwnam()and callssetreuid()in the child. The CLI then runs as that OS account.Pass an application-level ID, as the docstring suggests, and this is what comes back:
Nothing in the message points at
options.user.History
#134 added the field to run the CLI as a custom OS user. It shipped without a docstring. #873 added docstrings in bulk and described the field as a session identifier. The published SDK reference on platform.claude.com carries the same text, so the mislabeling propagates to the official docs.
Failure modes, observed
KeyErrorfromgetpwnam(), wrapped in the generic connect error. Reproduced on macOS, Python 3.12, against this checkout.PermissionError.ValueError; Popen does not supportuserthere.admin,postgres): the CLI silently runs as that user, with that user's HOME and therefore that user's~/.claudecredentials and settings. No error. No log.The change
types.py— the docstring now states what the option does.subprocess_cli.py—KeyError,PermissionError, andValueErrorfrom the spawn are translated into aCLIConnectionErrorthat namesoptions.userand explains the semantics. Only when the option is set; other spawn failures keep the generic message. I wrapped the failure rather than pre-validating withpwd.getpwnam(): pre-validation duplicates Popen's resolution logic and adds a TOCTOU window for nothing.test_transport.py, next to the existingtest_connect_as_different_user. The first fails on main and passes with the fix. The second pins the generic path so the new message cannot leak into unrelated failures.Verification
ruff check,ruff format --check,mypy src/ scripts/: clean.The setuid behavior itself is left alone. It is a deliberate feature from #134. If a warning is wanted when the switch actually happens under root, I am happy to add one.