Skip to content

Keep inherited batch database provenance consistent in status output #5083

Description

@Widthdom

Summary

When batch --db <path> supplies the database for a child status command, the child reads counts and index metadata from that database but builds response metadata and Git freshness against the current working directory's canonical repository/database.

One response therefore mixes two repositories and can report a false head_changed: true.

Reproduction

Verified on origin/main at 8d537125aae747c1008ad31b3d59364cb43652f0 with cdidx 1.42.0.

Create or reuse a database for a different scratch project, then run from the CodeIndex repository:

{"command":"status","args":["--json","--compact"]}
{"command":"search","args":["anything","--json","--limit","1"]}
dotnet ./src/CodeIndex/bin/Debug/net8.0/cdidx.dll batch \
  --db /tmp/cdidx-batch-repro/.cdidx/codeindex.db \
  --json-summary < /tmp/cdidx-batch-repro/commands.ndjson

Observed in the child status response:

  • file/symbol counts and indexed_head came from the scratch database;
  • metadata.db_path pointed at the canonical CodeIndex worktree database;
  • runtime Git HEAD/freshness came from the CodeIndex worktree;
  • head_changed was consequently true even though a direct status --db /tmp/.../codeindex.db was consistent.

Expected behavior

A database inherited from the batch parent must be treated exactly like a child-explicit --db for:

  • data reads;
  • reported database path;
  • project-root resolution;
  • indexed/runtime Git provenance;
  • freshness and readiness decisions;
  • all JSON metadata and diagnostics.

No field in one child response should silently describe a different repository.

Implementation guidance

  • Propagate a single resolved database/project context into child dispatch, not only a thread-static reader.
  • Prefer injecting the inherited --db into the child options or passing a typed resolved context through query execution.
  • Do not re-resolve canonical DB/project metadata from CWD once a parent-pinned DB is active.
  • Ensure nested/batched execution cannot leak context between child records, including exceptions and cancellation.
  • Keep child-explicit --db precedence and mismatch validation deterministic.

Likely implementation areas:

  • src/CodeIndex/Cli/BatchExecution.cs
  • src/CodeIndex/Cli/QueryCommandRunner.Database.cs
  • src/CodeIndex/Cli/QueryCommandRunner.Status.cs
  • batch dispatch in src/CodeIndex/Cli/QueryCommandRunner.Batch.cs

Required tests

Use two repositories/databases with deliberately different paths, file counts, indexed commits, and runtime HEADs.

Cover:

  1. parent-only --db;
  2. child-explicit --db;
  3. any permitted parent/child override or mismatch case;
  4. multiple children to prove no context leakage;
  5. text, regular JSON, compact JSON, and JSON summary provenance.

Assert every provenance/freshness field belongs to the same selected database. Add English and Japanese changelog fragments.

Regression history

This is a follow-up to #4354, which fixed mixed database behavior when a child supplied an explicit --db. The uncovered case is parent inheritance. It is also related to the CWD Git-provenance leak addressed in #152. Both are closed; no open exact duplicate was found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codebugSomething isn't workingtests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions