Skip to content

fix: four bugs in cli.py - streaming crash, plaintext private key, dead code, tool_choice default#272

Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/cli-streaming-crash-and-security-bugs
Open

fix: four bugs in cli.py - streaming crash, plaintext private key, dead code, tool_choice default#272
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/cli-streaming-crash-and-security-bugs

Conversation

@verseon0980
Copy link
Copy Markdown

Bugs Fixed

This PR fixes four separate bugs all found in src/opengradient/cli.py.

Bug 1 - CLI streaming completely broken: RuntimeError when using --stream flag

The chat command called asyncio.run() twice in sequence for the streaming path.
The first call created the async generator inside event loop 1 and then closed
that loop. The second call inside print_streaming_chat_result() created a new
event loop 2 and tried to iterate the generator from the already closed loop 1.

This caused: RuntimeError: Task attached to a different loop

Every single user who runs opengradient chat --stream gets this crash.
Streaming via the CLI was completely non-functional.

Fix: Introduced a new async function _stream_chat_and_print() that creates
the generator and consumes it inside a single asyncio.run() call so the
generator and its consumer always share the same event loop.

Bug 2 - Private key stored in world-readable file: wallet can be stolen

The config file at ~/.opengradient_config.json was written with default OS
permissions which are typically 0644 on Linux and macOS. This means any other
user on the same machine, any background process, any malicious package, or
any shared server environment can read the file and steal the private key.

The config show command correctly masks the private key on screen, but the
file itself was completely unprotected.

Fix: Added os.chmod(OG_CONFIG_FILE, 0o600) immediately after writing the
config file. This restricts access to the file owner only. Added import os
at the top of the file.

Bug 3 - Dead unreachable code referencing undefined variable image_data

The generate_image command had several lines of code after a
raise NotImplementedError statement. These lines could never execute.
Worse, one of the unreachable lines referenced image_data which is never
defined anywhere in the function. If someone removed the raise to start
implementing the feature, they would immediately get:
NameError: name image_data is not defined

Fix: Removed the dead unreachable code after the raise and wrapped the
command in a proper try/except that catches NotImplementedError and prints
a clean error message.

Bug 4 - tool_choice CLI option defaulted to empty string instead of None

The --tool-choice option had default="" instead of default=None. When a
user does not pass --tool-choice, the value arrives as an empty string
and gets forwarded to llm.chat(tool_choice=""). An empty string behaves
differently from None throughout the codebase and causes inconsistent
behavior between CLI usage and direct Python API usage.

Fix: Changed default="" to default=None on the --tool-choice click option.

Files Changed

  • src/opengradient/cli.py: all four fixes above

Signed-off-by: verseon0980 <klokrc74@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant