Skip to content

fix(cli): refuse a malformed API base before sending a request - #22726

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
sauranshbhardwaj:fix/cli-validate-api-base-before-requests
Oct 21, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
sauranshbhardwaj:fix/cli-validate-api-base-before-requests

Conversation

@sauranshbhardwaj

@sauranshbhardwaj sauranshbhardwaj commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What's wrong

#19689 made omi config set api_base refuse malformed URLs. The value a command actually uses can also come from --api-base, $OMI_API_BASE, or a config saved before that check, and none of those are checked:

# sdks/python-cli/omi_cli/client.py
self._http = httpx.Client(
    base_url=profile.api_base.rstrip("/"),

httpx raises UnsupportedProtocol for a scheme-less or non-http base, which is a TransportError, so the client treats it as a network failure. Reads retry with backoff and end in "Connection failed". Writes are worse: the client reports that the server may have applied the write, although no request ever left the machine.

command before after
omi --json --api-base api.omi.me memory list about 5 s of retries, then Connection failed, exit 3 Invalid URL for 'api_base', exit 1
omi --json --api-base localhost:8000 memory create hello POST outcome unknown: the server may have applied this write, exit 3 Invalid URL for 'api_base', exit 1
OMI_API_BASE=ftp://api.omi.me omi --json goal delete g1 --yes DELETE outcome unknown, exit 3 Invalid URL for 'api_base', exit 1
a valid --api-base https://api.staging.omi.me works works

Change

config set's URL check moves into validated_url(key, value) in commands/config.py (same rules, same message), and AppContext.make_client runs it on the resolved API base. Every source of the base now fails fast with the same usage error. Profile management (config show, config set) still loads a bad saved value, so it can be fixed.

Tests

tests/test_config.py: "a malformed API base is refused before any request", 9 cases (localhost:8000, ftp://api.omi.me, http://example.test:abc × flag / env / saved config) running --json memory create hello. Each expects exit 1, empty stdout, the usage error on stderr and no "outcome unknown". All 9 fail on main and pass after. The #19689 config set tests pass unchanged, and so does the full CLI suite.

Failure-Class: none

View guided diff

@Git-on-my-level Git-on-my-level added python positive-signal Automation verified a genuine fix/quality contribution labels Oct 20, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Solid fix — verified end-to-end, no blocking issues from my side.

sdks/python-cli/omi_cli/commands/config.py — the inline check from #19689 is extracted into validated_url() with identical rules, error messages, and lazy urlsplit validation (touching .hostname/.port to force a ValueError into a clean UsageError); config set behavior is byte-for-byte the same path. sdks/python-cli/omi_cli/main.py — validating the resolved base in make_client is the right chokepoint: get_profile() applies flag > env > saved, so all three sources now fail fast before OmiClient is constructed, and config show/set on a profile with a bad saved value still works so it can be repaired. No circular-import risk — main.py already imports commands.config at module level. sdks/python-cli/tests/test_config.py — the 3 bad bases × 3 sources matrix directly covers the regression in the description (no "outcome unknown" on writes).

Verification: I ran the full sdks/python-cli pytest suite on the PR head in a clean environment with the project's pinned dependencies — 907 passed, 1 skipped, including the 9 new cases.

USER IMPACT: with a bad --api-base, $OMI_API_BASE, or stale saved API base, commands now fail immediately with "Invalid URL for 'api_base'" instead of ~5s of retries ending in "Connection failed" — or worse, a false warning that a write "may have been applied" when no request ever left the machine.

One non-blocking observation: make_local_client() resolves $OMI_LOCAL_API_URL / local_api_url without the same validated_url() pass, so a malformed local URL still surfaces as a transport error. Out of scope here, but worth a follow-up issue for consistency.

Everything I'd want as a reviewer is already here: failure matrix per source, regression tests, and a clear before/after. Nothing further requested.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

5/5 confidence bug fix (clear root cause, fix(cli) title, tests, CI green) but mergeStateStatus is DIRTY (real merge conflict, not stale) -- hard floor blocks merge and rebasing/force-pushing the branch is off-limits. Approved, left for a future clean re-sync.

`omi config set api_base` checks URLs since BasedHardware#19689, but the value a
command actually uses can also come from --api-base, $OMI_API_BASE, or
a config saved before that check, and none of those are checked. httpx
raises UnsupportedProtocol for `localhost:8000` or `ftp://...`, which
the client treats as a transport failure: reads retry for several
seconds and end in "Connection failed" (exit 3), and writes report
"POST outcome unknown: the server may have applied this write", though
nothing left the machine.

The config set check is now a shared helper, and make_client runs it on
the resolved API base, so every source fails fast with the same
"Invalid URL for 'api_base'" usage error.
@sauranshbhardwaj
sauranshbhardwaj force-pushed the fix/cli-validate-api-base-before-requests branch from 91e48bf to 15537ae Compare October 20, 2026 16:25
@Git-on-my-level

Copy link
Copy Markdown
Member

Since my last review: addressed (rebase onto current main is clean — config.py is byte-identical to the approved commit, and the main.py/test_config.py deltas vs that commit are exactly the timeout/ConfigWriteRefused work main already carries, so no new logic rode along; full sdks/python-cli suite re-run at the new head: 929 passed, 1 skipped) · still open (nothing from my prior review) · newly introduced (nothing).

Verdict: this remains correct and merge-ready from my side; the rebase changed nothing in the fix itself.

  • sdks/python-cli/omi_cli/commands/config.py: validated_url() (lines 68–89) keeps fix(cli): reject malformed URLs in config set #19689's rules verbatim — scheme in {http, https}, netloc + hostname present, lazy urlsplit errors forced via .hostname/.port into a clean UsageError — and config set (line 107) now goes through it, so set-path behavior is unchanged.
  • sdks/python-cli/omi_cli/main.py: make_client (lines 100–102) validating the resolved base is the right chokepoint — get_profile() applies flag > env > saved, so all three sources fail before OmiClient exists, while config show/set still loads a bad saved value so it can be repaired. No circular-import risk (config.py only references AppContext under TYPE_CHECKING).
  • sdks/python-cli/tests/test_config.py: the 3 bases × 3 sources matrix asserts exit 1, empty stdout, the usage error on stderr, and no "outcome unknown" — exactly the regression in the description.

Verification: full sdks/python-cli pytest suite at head 15537ae in a clean environment — 929 passed, 1 skipped, zero failures. kodjima33's approval predates the rebase, but the net diff is unchanged, so it still reflects this code.

USER IMPACT: a bad --api-base, $OMI_API_BASE, or stale saved base now fails instantly with "Invalid URL for 'api_base'" instead of ~5s of retries ending in "Connection failed" — or a false warning that a write "may have been applied" when no request ever left the machine.

Still on my wishlist, non-blocking: make_local_client() resolves $OMI_LOCAL_API_URL / local_api_url without the same validated_url() pass, so a malformed local URL still surfaces as a transport error — worth a small follow-up issue for consistency. Nothing else requested.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33
kodjima33 merged commit bcc879c into BasedHardware:main Oct 21, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

positive-signal Automation verified a genuine fix/quality contribution python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants