Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle escaped quotes in PS1 metadata parsing #6827

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions openhands/events/observation/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@ class CmdOutputMetadata(BaseModel):
def to_ps1_prompt(cls) -> str:
"""Convert the required metadata into a PS1 prompt."""
prompt = CMD_OUTPUT_PS1_BEGIN
# Create the JSON string with proper escaping
json_str = json.dumps(
{
'pid': '$!',
'exit_code': '$?',
'username': r'\u',
'hostname': r'\h',
'working_dir': r'$(pwd)',
'py_interpreter_path': r'$(which python 2>/dev/null || echo "")',
'username': r'\u', # Raw string to avoid double escaping
'hostname': r'\h', # Raw string to avoid double escaping
'working_dir': '$(pwd)',
'py_interpreter_path': '$(which python 2>/dev/null || echo "")',
},
indent=2,
)
# Make sure we escape double quotes in the JSON string
# So that PS1 will keep them as part of the output
prompt += json_str.replace('"', r'\"')
prompt += json_str
prompt += CMD_OUTPUT_PS1_END + '\n' # Ensure there's a newline at the end
return prompt

Expand All @@ -56,7 +55,8 @@ def matches_ps1_metadata(cls, string: str) -> list[re.Match[str]]:
matches = []
for match in CMD_OUTPUT_METADATA_PS1_REGEX.finditer(string):
try:
json.loads(match.group(1).strip()) # Try to parse as JSON
# Parse the JSON as is
json.loads(match.group(1).strip())
matches.append(match)
except json.JSONDecodeError:
logger.warning(
Expand All @@ -69,7 +69,8 @@ def matches_ps1_metadata(cls, string: str) -> list[re.Match[str]]:
@classmethod
def from_ps1_match(cls, match: re.Match[str]) -> Self:
"""Extract the required metadata from a PS1 prompt."""
metadata = json.loads(match.group(1))
# Parse the JSON as is
metadata = json.loads(match.group(1).strip())
# Create a copy of metadata to avoid modifying the original
processed = metadata.copy()
# Convert numeric fields
Expand Down
Loading
Loading