Skip to content

Commit e568d97

Browse files
authored
Revert "Revert "Add username parameter to AsyncBashSession" (#8767)"
This reverts commit c76809a.
1 parent 6491142 commit e568d97

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

openhands/runtime/action_execution_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ async def run(
390390
try:
391391
if action.is_static:
392392
path = action.cwd or self._initial_cwd
393-
result = await AsyncBashSession.execute(action.command, path)
393+
result = await AsyncBashSession.execute(
394+
action.command, path, self.username
395+
)
394396
obs = CmdOutputObservation(
395397
content=result.content,
396398
exit_code=result.exit_code,

openhands/runtime/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ async def clone_or_init_repo(
346346
'No repository selected. Initializing a new git repository in the workspace.'
347347
)
348348
action = CmdRunAction(
349-
command='git init',
349+
command=f'git init && git config --global --add safe.directory {self.workspace_root}'
350350
)
351351
self.run_action(action)
352352
else:

openhands/runtime/utils/async_bash.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import asyncio
22
import os
3+
import pwd
34

45
from openhands.runtime.base import CommandResult
56

67

78
class AsyncBashSession:
89
@staticmethod
9-
async def execute(command: str, work_dir: str) -> CommandResult:
10+
async def execute(
11+
command: str, work_dir: str, username: str | None = None
12+
) -> CommandResult:
1013
"""Execute a command in the bash session asynchronously."""
1114
work_dir = os.path.abspath(work_dir)
1215

@@ -17,12 +20,24 @@ async def execute(command: str, work_dir: str) -> CommandResult:
1720
if not command:
1821
return CommandResult(content='', exit_code=0)
1922

23+
env = {}
24+
if username:
25+
try:
26+
user_info = pwd.getpwnam(username)
27+
env['HOME'] = user_info.pw_dir
28+
env['USER'] = username
29+
env['LOGNAME'] = username
30+
except KeyError:
31+
raise ValueError(f'User {username} does not exist.')
32+
# Prepare to run the command
2033
try:
2134
process = await asyncio.subprocess.create_subprocess_shell(
2235
command,
2336
stdout=asyncio.subprocess.PIPE,
2437
stderr=asyncio.subprocess.PIPE,
2538
cwd=work_dir,
39+
user=username,
40+
env=env,
2641
)
2742

2843
try:

tests/unit/test_runtime_git_tokens.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ async def test_clone_or_init_repo_no_repo_with_user_id(temp_dir):
234234
# Verify that git init was called
235235
assert len(runtime.run_action_calls) == 1
236236
assert isinstance(runtime.run_action_calls[0], CmdRunAction)
237-
assert runtime.run_action_calls[0].command == 'git init'
237+
assert (
238+
runtime.run_action_calls[0].command
239+
== f'git init && git config --global --add safe.directory {runtime.workspace_root}'
240+
)
238241
assert result == ''
239242

240243

@@ -255,7 +258,10 @@ async def test_clone_or_init_repo_no_repo_no_user_id_no_workspace_base(temp_dir)
255258
# Verify that git init was called
256259
assert len(runtime.run_action_calls) == 1
257260
assert isinstance(runtime.run_action_calls[0], CmdRunAction)
258-
assert runtime.run_action_calls[0].command == 'git init'
261+
assert (
262+
runtime.run_action_calls[0].command
263+
== f'git init && git config --global --add safe.directory {runtime.workspace_root}'
264+
)
259265
assert result == ''
260266

261267

0 commit comments

Comments
 (0)