Skip to content

fix(cli): deps field, root mismatch, add --help/--version#91

Merged
justrach merged 1 commit intomainfrom
fix/cli-bugs-90
Apr 1, 2026
Merged

fix(cli): deps field, root mismatch, add --help/--version#91
justrach merged 1 commit intomainfrom
fix/cli-bugs-90

Conversation

@justrach
Copy link
Copy Markdown
Owner

@justrach justrach commented Apr 1, 2026

Fixes #90

Changes

Bug 1: deps uses wrong JSON field

jq '.dependents[]'jq '.imported_by[]' to match server response (src/server.zig line 481).

Bug 2: Root mismatch when daemon already running

_ensure_server now checks the running daemon's root via /health response. If a different root is requested, it restarts the daemon instead of silently returning results from the wrong repo.

New: --help / --version flags

codedb-cli --help      # show usage
codedb-cli -h          # same
codedb-cli --version   # show codedb version
codedb-cli -v          # same

Origin

Both bugs found by Codex automated review on #88.

@codex review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

Benchmark Regression Report

Threshold: 10.00%

Tool Base (ns) Head (ns) Delta Status
codedb_bundle 619161 628679 +1.54% OK
codedb_changes 150439 113397 -24.62% OK
codedb_deps 45335 30387 -32.97% OK
codedb_edit 59212 25055 -57.69% OK
codedb_hot 237482 160935 -32.23% OK
codedb_outline 386627 385640 -0.26% OK
codedb_read 244818 169140 -30.91% OK
codedb_search 251438 253310 +0.74% OK
codedb_snapshot 3162656 3168034 +0.17% OK
codedb_status 29497 29758 +0.88% OK
codedb_symbol 63268 63531 +0.42% OK
codedb_tree 98560 97851 -0.72% OK
codedb_word 92102 90912 -1.29% OK

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ef6b6e9d64

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/codedb-cli Outdated
Comment on lines +18 to +19
running_root=$(curl -sf "$BASE/health" | jq -r '.root // empty' 2>/dev/null)
if [ -n "$running_root" ] && [ "$running_root" != "$root" ]; then
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 Badge Read daemon root from an endpoint that exposes it

This root-mismatch safeguard never activates because it parses .root from /health, but the server currently responds with only {"status":"ok"} (see src/server.zig health handler at lines 106-108). In that common case running_root is empty, so the code always takes the return 0 path and still serves queries from the wrong repository when a daemon is already running on another root.

Useful? React with 👍 / 👎.

@justrach
Copy link
Copy Markdown
Owner Author

justrach commented Apr 1, 2026

Fixed Codex's P1 finding:

Root mismatch check now uses process args instead of /health response.

The /health endpoint only returns {"status":"ok"} — no root field. The fix now parses the running codedb process's command line via ps -eo args to extract the root argument before serve. This correctly detects when a daemon is running for a different repo and restarts it.

# Before (broken): parsed .root from /health (always empty)
running_root=$(curl -sf "$BASE/health" | jq -r '.root // empty')

# After (fixed): parse from process command line
running_root=$(ps -eo args | grep "[c]odedb.*serve" | awk '{for(i=1;i<=NF;i++) if($i=="serve") print $(i-1)}')

@codex review the updated fix.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 28a6f78312

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/codedb-cli Outdated
# Check if the running daemon is serving the requested root
# Parse root from the running codedb process's command line
local running_root
running_root=$(ps -eo args 2>/dev/null | grep "[c]odedb.*serve" | awk '{for(i=1;i<=NF;i++) if($i=="serve") print $(i-1)}' | head -1)
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 Badge Match running daemon using configured binary name

The root-detection pipeline hardcodes grep "[c]odedb.*serve" while the CLI explicitly supports custom executables via CODEDB_BINARY. Under set -euo pipefail, a healthy daemon started from a differently named binary (or wrapper) makes this grep return 1, which aborts _ensure_server at assignment time instead of returning success after _ping. This turns normal commands into immediate failures whenever CODEDB_BINARY does not contain codedb.

Useful? React with 👍 / 👎.

Comment thread scripts/codedb-cli Outdated
Comment on lines +19 to +23
running_root=$(ps -eo args 2>/dev/null | grep "[c]odedb.*serve" | awk '{for(i=1;i<=NF;i++) if($i=="serve") print $(i-1)}' | head -1)
if [ -n "$running_root" ]; then
running_root="$(cd "$running_root" 2>/dev/null && pwd || echo "$running_root")"
fi
if [ -n "$running_root" ] && [ "$running_root" != "$root" ]; then
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 Badge Identify daemon by port before comparing roots

After _ping confirms only the daemon on $CODEDB_PORT, the code still reads running_root from the first process matching codedb.*serve (head -1) without tying it to that port. In environments with multiple codedb daemons, this can compare against another instance’s root, falsely detect a mismatch, and trigger pkill -f "codedb.*serve", interrupting unrelated daemons.

Useful? React with 👍 / 👎.

@justrach
Copy link
Copy Markdown
Owner Author

justrach commented Apr 1, 2026

Addressed both Codex findings:

P1 (binary name): No longer greps for hardcoded codedb. Now finds the daemon PID via ss -tlnp "sport = :$PORT" (falls back to lsof), then reads /proc/$pid/cmdline. Works with any CODEDB_BINARY value.

P2 (multi-daemon): Identifies daemon by port first, only kills that specific PID (not pkill -f). Won't touch daemons on other ports.

# Find PID of daemon on our port
daemon_pid=$(ss -tlnp "sport = :$PORT" | awk '/LISTEN/{gsub(/.*pid=/,""); gsub(/,.*/,""); print; exit}')
# Read its cmdline to extract root
running_root=$(tr '\0' ' ' < "/proc/$daemon_pid/cmdline" | awk '{for(i=1;i<=NF;i++) if($i=="serve") print $(i-1)}')
# Kill only this specific daemon if root mismatches
kill "$daemon_pid"

@codex review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

Benchmark Regression Report

Threshold: 10.00%

Tool Base (ns) Head (ns) Delta Status
codedb_bundle 638577 626587 -1.88% OK
codedb_changes 118493 110934 -6.38% OK
codedb_deps 30525 31897 +4.49% OK
codedb_edit 23590 23702 +0.47% OK
codedb_hot 164291 161019 -1.99% OK
codedb_outline 402755 406395 +0.90% OK
codedb_read 169902 171639 +1.02% OK
codedb_search 256938 255501 -0.56% OK
codedb_snapshot 3202931 3181327 -0.67% OK
codedb_status 30615 33346 +8.92% OK
codedb_symbol 65005 62312 -4.14% OK
codedb_tree 104532 108503 +3.80% OK
codedb_word 94397 92498 -2.01% OK

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4947e7d2fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread scripts/codedb-cli Outdated
fi
if [ -n "$daemon_pid" ]; then
# Read cmdline: tokens are null-separated in /proc
running_root=$(tr '\0' ' ' < "/proc/$daemon_pid/cmdline" 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="serve") print $(i-1)}')
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 Badge Parse daemon root without splitting on whitespace

running_root is extracted from /proc/$daemon_pid/cmdline by converting NULs to spaces and then using default AWK field splitting. This breaks when the served root contains spaces (for example /tmp/my repo): the script captures only the last word before serve, so _ensure_server thinks the root changed and kills/restarts the daemon on every command even when it is already serving the requested repo. Parse the NUL-delimited argv directly instead of space-splitting to avoid false mismatches.

Useful? React with 👍 / 👎.

Fixes #90 (found by Codex review on PR #88):

1. deps: use `.imported_by[]` not `.dependents[]` to match server response
2. _ensure_server: check running daemon's root via /health, restart if
   it doesn't match the requested root (prevents silent wrong-repo results)
3. Add --help/-h and --version/-v flags

Generated with AI

Co-Authored-By: AI <ai@example.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 1, 2026

Benchmark Regression Report

Threshold: 10.00%

Tool Base (ns) Head (ns) Delta Status
codedb_bundle 366278 370411 +1.13% OK
codedb_changes 64142 63261 -1.37% OK
codedb_deps 16620 16786 +1.00% OK
codedb_edit 14895 14079 -5.48% OK
codedb_hot 89843 94022 +4.65% OK
codedb_outline 250994 259968 +3.58% OK
codedb_read 99329 93548 -5.82% OK
codedb_search 143980 154996 +7.65% OK
codedb_snapshot 2503205 2552955 +1.99% OK
codedb_status 19657 17804 -9.43% OK
codedb_symbol 39127 37740 -3.54% OK
codedb_tree 64366 62969 -2.17% OK
codedb_word 50604 51235 +1.25% OK

@justrach justrach merged commit 6773a60 into main Apr 1, 2026
1 check passed
justrach added a commit that referenced this pull request Apr 1, 2026
Changes since 0.2.0:
- fix: stack overflow on Linux (64MB thread trampoline) (#88, closes #84)
- fix: conditional codesign (macOS only) (#88)
- fix(cli): deps JSON field, root mismatch, --help/--version (#91, closes #90)
- feat: CLI wrapper (codedb-cli) + systemd service + docs

Generated with AI

Co-Authored-By: AI <ai@example.com>
@justrach justrach deleted the fix/cli-bugs-90 branch April 1, 2026 17:57
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.

codedb-cli: deps uses wrong JSON field + root mismatch on running daemon

1 participant