Skip to content

Allow headless commands with connected BYOK providers (fixes #22) - #26

Open
nandal wants to merge 1 commit into
openhackai:mainfrom
nandal:fix/issue-22-headless-byok-auth
Open

nandal wants to merge 1 commit into
openhackai:mainfrom
nandal:fix/issue-22-headless-byok-auth

Conversation

@nandal

@nandal nandal commented Oct 12, 2026

Copy link
Copy Markdown

Summary

Fixes #22.

Headless entry points (--scan, --hack, --plan, --agent) previously gated execution strictly on settings.openhack_api_key. When using a BYOK provider (such as OpenAI, Anthropic, Ollama, etc.), model calls only use the provider's resolved credentials and never touch openhack_api_key. Gating on openhack_api_key alone prevented using BYOK providers in CI or headless environments without an OpenHack account key.

Changes

  • Replaced the repetitive openhack_api_key gate across _cmd_scan, _cmd_hack, _cmd_plan, and _cmd_agent with a unified _require_auth() helper in openhack/__main__.py.
  • For BYOK providers (settings.llm_provider != "openhack"), verify connection via providers.is_connected(settings.llm_provider). If credentials or required environment variables are missing, display an informative error pointing to the specific environment variable required.
  • In openhack/config.py, mapped user-saved provider config to LLM_PROVIDER in _build_settings so persisted provider choices apply to headless scans.
  • In openhack/providers.py, allowed keyless local providers when LLM_PROVIDER is set in environment.
  • Added unit tests in tests/test_cli_dispatch.py verifying auth handling for both connected/disconnected BYOK providers and logged in/out OpenHack accounts.

@greptile-apps

greptile-apps Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[Medium impact] Refactors authentication checks for headless commands.

Fix the rejection of keyless providers selected through .env before merging.

Fix All in CodexFindings

  1. P1 Keyless commands reject `.env` settings ▶
  2. P2 Saved credentials break the test ▶
Fix with agent prompt
### Issue 1
openhack/__main__.py:30-31
If `LLM_PROVIDER=ollama` is set in `.env`, with no saved provider or `OLLAMA_API_KEY`, `_require_auth()` rejects a valid keyless setup. `Settings` reads `.env`, but `is_connected()` checks only saved config and `os.environ`. All four headless commands stop and ask for a key that Ollama does not need. This also breaks runs that previously passed with an OpenHack key.

Use the resolved provider selection for the keyless check and add a `.env` regression test.

### Issue 2
tests/test_cli_dispatch.py:123
This test patches `all_credentials()`, but `is_connected()` calls `get_credential()`, which reads saved credentials directly. If a developer has a saved OpenAI API key or ChatGPT login, `_require_auth()` returns `True` and the test fails despite working code.

Patch `openhack.providers.get_credential` to return `None`, or give the test an isolated credential store.

```suggestion
    monkeypatch.setattr("openhack.providers.get_credential", lambda name: None)
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR lets headless commands use connected BYOK providers instead of requiring an OpenHack account key.

  • Headless commands can use connected BYOK providers.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Headless command] --> B[Read settings.llm_provider]
  B --> C{OpenHack?}
  C -->|Yes| D[Check OpenHack key]
  C -->|No| E[Check providers.is_connected]
  D --> F{Accepted?}
  E --> F
  F -->|Yes| G[Run command with selected provider]
  F -->|No| H[Print setup error and stop]
Loading

Reviews (1) · Last reviewed commit: "Allow headless commands with connected B..." · Reviewed by Greptile

Comment thread openhack/__main__.py
Comment on lines +30 to +31
if providers.is_connected(active_provider):
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Keyless commands reject .env settings

If LLM_PROVIDER=ollama is set in .env, with no saved provider or OLLAMA_API_KEY, _require_auth() rejects a valid keyless setup. Settings reads .env, but is_connected() checks only saved config and os.environ. All four headless commands stop and ask for a key that Ollama does not need. This also breaks runs that previously passed with an OpenHack key.

Use the resolved provider selection for the keyless check and add a .env regression test.

Prompt To Fix With AI
This is a comment left during a code review.
Path: openhack/__main__.py
Line: 30-31

Comment:
**Keyless commands reject `.env` settings**

If `LLM_PROVIDER=ollama` is set in `.env`, with no saved provider or `OLLAMA_API_KEY`, `_require_auth()` rejects a valid keyless setup. `Settings` reads `.env`, but `is_connected()` checks only saved config and `os.environ`. All four headless commands stop and ask for a key that Ollama does not need. This also breaks runs that previously passed with an OpenHack key.

Use the resolved provider selection for the keyless check and add a `.env` regression test.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex Fix in Claude Code Fix in Cursor Fix in Conductor

monkeypatch.setattr(settings, "llm_provider", "openai")
monkeypatch.setattr(settings, "openhack_api_key", None)
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
monkeypatch.setattr("openhack.provider_auth.all_credentials", lambda: {})

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Saved credentials break the test

This test patches all_credentials(), but is_connected() calls get_credential(), which reads saved credentials directly. If a developer has a saved OpenAI API key or ChatGPT login, _require_auth() returns True and the test fails despite working code.

Patch openhack.providers.get_credential to return None, or give the test an isolated credential store.

Suggested change
monkeypatch.setattr("openhack.provider_auth.all_credentials", lambda: {})
monkeypatch.setattr("openhack.providers.get_credential", lambda name: None)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/test_cli_dispatch.py
Line: 123

Comment:
**Saved credentials break the test**

This test patches `all_credentials()`, but `is_connected()` calls `get_credential()`, which reads saved credentials directly. If a developer has a saved OpenAI API key or ChatGPT login, `_require_auth()` returns `True` and the test fails despite working code.

Patch `openhack.providers.get_credential` to return `None`, or give the test an isolated credential store.

```suggestion
    monkeypatch.setattr("openhack.providers.get_credential", lambda name: None)
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex Fix in Claude Code Fix in Cursor Fix in Conductor

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.

Headless --scan requires OPENHACK_API_KEY even when a BYOK provider is configured

1 participant