Skip to content

Fix #775 (doctor DuckDB check) + friendly Langfuse auth error - #790

Merged
anilmurty merged 2 commits into
mainfrom
fix/775-doctor-duckdb-langfuse-auth-hint
Nov 15, 2026
Merged

anilmurty merged 2 commits into
mainfrom
fix/775-doctor-duckdb-langfuse-auth-hint

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

A fresh, healthy install sees tj doctor report its database as unwritable. This PR fixes that check and the contradictory storage-settings warning that comes from the same cause. It also stops tj backfill langfuse from dumping an httpx traceback when Langfuse refuses the key.

Partially addresses #775 (items 1–2).

Summary

  • tj doctor's "DuckDB writable" check reuses the process's live backend connection when it holds the same file. Otherwise it opens the file with the backend's own settings (connect_bounded) instead of a bare duckdb.connect.
  • connect_bounded (renamed from _connect_bounded so the CLI can share it) falls back to the default settings only when DuckDB rejected a setting. A held lock or a configuration mismatch now propagates unchanged and is no longer logged as "rejected".
  • tj backfill langfuse catches httpx.HTTPStatusError at the CLI layer. It prints one line naming the key-pair format and exits 1 with no traceback.
  • Added a DuckDBBackend.db_path read-only property so doctor can tell whether the live backend holds the file it is checking.

#775 item 1: doctor's red "DuckDB writable"

Symptom. ✗ DuckDB writable: Cannot open database: Connection Error: Can't open a connection to same database file with a different configuration than existing connections on a healthy install.

Root cause. main opens the backend through connect_bounded, which sets memory_limit, threads and temp_directory, before it dispatches to doctor. _check_db then opened a bare duckdb.connect(path). DuckDB keeps one instance per file per process and refuses a second connection whose settings differ. The lock downgrade only matched conflicting lock / could not set lock, so the error came out as level: error.

Fix. If ctx.obj["db"] is a DuckDBBackend holding the same resolved path, the probe runs SELECT 1 on that connection. That connection was opened read-write at startup, so the file and its write lock are already held. Otherwise the probe opens the file through connect_bounded with the configured settings, which DuckDB's instance cache accepts. Two behaviours are unchanged and covered by tests: an unopenable path is still reported as error, and a lock held by another process is still downgraded to info.

#775 item 2: the self-contradicting fallback warning

Symptom. storage.memory_limit='2GB' / storage.threads=4 rejected by DuckDB; falling back to 2GB / 4.

Root cause. The fallback in connect_bounded caught every duckdb.Error. A lock held by the daemon (IOException) and a configuration mismatch (ConnectionException) were both reported as rejected settings. The connect was then retried with the identical values and failed again. The lock case also fires on every CLI command run while tj serve holds the lock, just before main switches to the API backend.

Fix. IOException and ConnectionException are re-raised untouched. The error text still contains "lock", so main's lock detection and API fallback work as before. The retry is skipped when the values already are the defaults. A genuine rejection still degrades to the defaults, and the warning now includes DuckDB's reason: … rejected by DuckDB (<reason>); using the defaults 2GB / 4 instead.

Friendly Langfuse auth error

Symptom. After #787, a 401/403 from Langfuse surfaced as a raw httpx.HTTPStatusError traceback.

Fix. cmd_backfill.langfuse catches it and raises a ClickException, so the user gets one line and exit 1. The core adapter is untouched, so the core/cli boundary holds. Wording matches docs/backfill/langfuse.md and the adapter's _auth_headers:

  • Single key (sent as Bearer): Langfuse rejected the key (HTTP 401). A single key is sent as a Bearer token, which cannot read observations; Langfuse's public API needs the project's public and secret key joined with a colon: --api-key pk-lf-...:sk-lf-...
  • Key pair: Langfuse rejected the key pair (HTTP 401). Check that --api-key is this project's public and secret key joined with a colon: --api-key pk-lf-...:sk-lf-...
  • No key: Langfuse refused the request without credentials (HTTP 401). Pass the project's public and secret key: --api-key pk-lf-...:sk-lf-...

The secret is never echoed back.

What's also in this PR

  • Any other HTTP status from Langfuse (for example a 500) is caught in the same except and printed as Langfuse returned HTTP <n> for <url>. with the query string dropped, instead of a traceback. It is the same handler, and leaving it out would have kept the traceback for every non-auth status.

Tests / Verification

Added to tests/unit/test_doctor_paths.py:

  • test_check_db_is_ok_beside_the_backend_connection: opens via core.db.open_db, then runs _check_db with that backend.
  • test_check_db_is_ok_beside_a_live_connection_it_was_not_handed: same live connection, probe not given the backend.
  • test_doctor_command_reports_the_database_writable: end to end through tj --json doctor.
  • test_check_db_still_reports_an_unopenable_database_as_an_error
  • test_check_db_still_downgrades_a_lock_held_elsewhere
  • test_connect_bounded_does_not_blame_the_settings_for_a_config_mismatch
  • test_connect_bounded_does_not_blame_the_settings_for_a_held_lock
  • test_connect_bounded_still_degrades_a_rejected_override

Added to tests/unit/test_ingest_langfuse.py. These go through the Click runner with an httpx.MockTransport client injected into _fetch_url:

  • test_cli_bearer_key_refusal_names_the_key_pair_format[401|403]
  • test_cli_key_pair_refusal_says_check_the_pair[401|403]
  • test_cli_missing_key_refusal_asks_for_the_key_pair
  • test_cli_other_http_error_is_concise

Run against origin/main's tokenjam/, 12 of these fail and the regression guards for unchanged behaviour pass. The three connect_bounded tests fail on main partly because the public name does not exist there. The two "does not blame" cases also fail on main's behaviour, because main logs "rejected" in both of them.

  • env -u FORCE_COLOR python -m pytest tests/unit/ tests/integration/ -q -n auto: 5894–5896 passed. A different 3–5 tests failed on each of two runs (test_xdist_isolation, test_summarize_relocate, test_summarize_cli). All of them pass serially, none touch these files, and the machine was running other agents' suites at the same time.
  • Serial run of those files plus test_cli.py, test_advertised_commands_are_invocable.py and test_no_tracker_ids_in_printed_strings.py: 490 passed.
  • ruff check tokenjam/: clean. mypy tokenjam/: no issues in 288 files.
  • Live run of tj --db <scratch> doctor: ✓ DuckDB writable: Database accessible, with no fallback warning.
  • Live run of tj backfill langfuse --source-url https://cloud.langfuse.com --api-key sk-lf-bogus against real Langfuse Cloud: it returned 401 and the command printed the single-key line above with exit 1. The bogus key-pair case printed the key-pair line.

What's NOT in this PR

🤖 Generated with Claude Code

https://claude.ai/code/session_011uyjtSF8w9YGsDDujqydev

tj doctor reported a healthy database as unwritable. main opens the
backend with bounded settings (memory_limit, threads, temp_directory)
before dispatching, and _check_db then opened a bare second connection
to the same file. DuckDB keeps one instance per file per process and
refuses a second connection with different settings, so every healthy
install saw a red error. The probe now asks the live backend when it
holds the same file, and otherwise opens the file with the backend's
own settings via connect_bounded.

The settings fallback in connect_bounded caught every duckdb.Error, so
a held lock or a configuration mismatch was announced as the settings
being rejected, followed by a "fallback" to the same values. It now
degrades only on a genuine setting rejection, skips the pointless retry
when the values already are the defaults, and says what it is doing.

tj backfill langfuse let a 401/403 escape as an httpx traceback. The
CLI now catches HTTPStatusError and prints one line naming the key-pair
format (--api-key pk-lf-...:sk-lf-...), with a specific note for a
single key, which is sent as Bearer and cannot read observations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011uyjtSF8w9YGsDDujqydev
@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

Fixes the database check in tj doctor and makes Langfuse HTTP errors easier to read.

  • Doctor checks DuckDB through the connection that matches the file.
  • DuckDB connection errors no longer trigger a misleading settings fallback.
  • Langfuse backfill turns HTTP failures into clear command-line messages.

No actionable issues were found. Tests were inspected, not run.

Reviews (2) · Last reviewed commit: "Merge branch 'main' into fix/775-doctor-..." · Reviewed by Greptile

@anilmurty
anilmurty merged commit 0e1c596 into main Nov 15, 2026
6 checks passed
@anilmurty
anilmurty deleted the fix/775-doctor-duckdb-langfuse-auth-hint branch November 15, 2026 19:47
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.

0.7.0 fast-follow: doctor's DuckDB check, --db handling in init --cloud, and three smaller papercuts

1 participant