Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(reindex): reindex adds semantic search after a keyless init, and every run names its embedder (#4240)

Raghav Chamadiya1c6f38403ac4
diff --git a/packages/cli/src/repowise/cli/commands/decision_review_cmd.py b/packages/cli/src/repowise/cli/commands/decision_review_cmd.py
index 5ab23ca61..227291dbf 100644
--- a/packages/cli/src/repowise/cli/commands/decision_review_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/decision_review_cmd.py
@@ -282,7 +282,11 @@ def dedupe_command(
and every folded id keeps resolving.
"""
from repowise.cli.commands.decision_cmd import _resolve_decision_repo
- from repowise.cli.providers.embedders import build_embedder, resolve_embedder_for_repo
+ from repowise.cli.providers.embedders import (
+ build_embedder,
+ resolve_embedder_for_repo,
+ semantic_search_command,
+ )
from repowise.cli.providers.vector_store import build_vector_store
from repowise.core.analysis.decisions.semantic_match import DEFAULT_DEDUP_TAU
from repowise.core.providers.embedding.base import MockEmbedder
@@ -298,7 +302,7 @@ def dedupe_command(
"no_real_embedder",
"Decisions can only be compared with a real embedder, not test vectors.",
fmt,
- remedy="Set an embedder key and run 'repowise reindex'.",
+ remedy=f"Run '{semantic_search_command(repo_path)}'.",
)
vector_store = build_vector_store(repo_path, embedder)
if vector_store is None:
@@ -306,7 +310,7 @@ def dedupe_command(
"no_vector_store",
"This repo has no vector store to compare decisions with.",
fmt,
- remedy="Set an embedder key and run 'repowise reindex'.",
+ remedy=f"Run '{semantic_search_command(repo_path)}'.",
)
async def _run():
diff --git a/packages/cli/src/repowise/cli/commands/generate_cmd/command.py b/packages/cli/src/repowise/cli/commands/generate_cmd/command.py
index b76252317..7262fd3fa 100644
--- a/packages/cli/src/repowise/cli/commands/generate_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/generate_cmd/command.py
@@ -31,7 +31,7 @@ from repowise.cli.helpers import (
run_async,
save_state,
)
-from repowise.cli.providers import resolve_embedder
+from repowise.cli.providers import model_run_embedder
from repowise.cli.ui import load_dotenv
from repowise.core.cost_estimator import STRUCTURAL_PAGE_TYPES
from repowise.core.docs_mode import docs_mode_state_fields, resolve_docs_mode
@@ -102,7 +102,7 @@ def _dry_run_provider(
return None
-def _make_gate(dry_run: bool):
+def _make_gate(dry_run: bool, embedder: str | None = None):
"""Return the ``gate_cost`` callback the engine invokes before generating.
Prints the estimate, stops on ``--dry-run``, and enforces the LLM-cost
@@ -112,7 +112,12 @@ def _make_gate(dry_run: bool):
def gate_cost(
cost_plans: Any, provider: Any, repo_path: Path, *, yes: bool, dry_run: bool
) -> bool:
- from repowise.cli.commands.init_cmd.generation import cost_gate_blocks, format_cost
+ from repowise.cli.commands.init_cmd.generation import (
+ cost_gate_blocks,
+ embedding_note,
+ format_cost,
+ )
+ from repowise.cli.providers.embedders import resolve_embedding_model
from repowise.core.cost_estimator import estimate_cost
if provider is None:
@@ -129,7 +134,12 @@ def _make_gate(dry_run: bool):
est = None
try:
est = estimate_cost(
- cost_plans, provider.provider_name, provider.model_name, repo_path=repo_path
+ cost_plans,
+ provider.provider_name,
+ provider.model_name,
+ repo_path=repo_path,
+ embedder=embedder,
+ embedding_model=resolve_embedding_model(embedder) if embedder else None,
)
except Exception as exc:
console.print(f"[yellow]Cost estimate unavailable ({exc}).[/yellow]")
@@ -140,6 +150,9 @@ def _make_gate(dry_run: bool):
f"Writing [bold]{pages}[/bold] pages with "
f"[cyan]{provider.model_name}[/cyan]. Estimated [bold]{format_cost(est)}[/bold]."
)
+ embedding = embedding_note(est)
+ if embedding:
+ console.print(f"[dim]{embedding}[/dim]")
if dry_run:
console.print("[dim]Dry run, nothing generated.[/dim]")
@@ -309,14 +322,8 @@ def generate_command(
# search as useless as it was before, with nothing in the output saying so.
# `update --full` has re-resolved here since it was written; `generate`
# never did. Same rule, same message.
- embedder_name = cfg.get("embedder")
- embedder_upgraded = False
- if not embedder_name or embedder_name == "mock":
- resolved = resolve_embedder(None)
- if resolved != (embedder_name or "mock"):
- console.print(f"Embedder: [cyan]{resolved}[/cyan] (was mock, index-only's default).")
- embedder_name = resolved
- embedder_upgraded = True
+ embedder_name, stated, embedder_upgraded = model_run_embedder(cfg.get("embedder"))
+ console.print(f"Embedder: [cyan]{stated}[/cyan].")
start = time.monotonic()
outcome = run_async(
@@ -330,7 +337,7 @@ def generate_command(
embedder_name=embedder_name,
yes=yes,
dry_run=dry_run,
- gate_cost=_make_gate(dry_run),
+ gate_cost=_make_gate(dry_run, embedder_name or "mock"),
interactive=interactive,
)
)
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_finish.py b/packages/cli/src/repowise/cli/commands/init_cmd/_finish.py
index 3ab40aafe..24164720e 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_finish.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_finish.py
@@ -151,8 +151,27 @@ def _docs_mode(run: InitRun) -> str:
return "deterministic"
+def _search_scope(run: InitRun) -> dict[str, Any]:
+ """The search leg of the scope: what works now, and the command that adds the rest."""
+ from repowise.cli.providers import semantic_search_command, semantic_search_status
+
+ result = run.result
+ if not result.generated_pages:
+ # Fast mode has no pages for reindex to embed.
+ return {"full_text": "unavailable", "semantic": "unavailable", "next_command": None}
+ semantic = semantic_search_status(run.scope_embedder, getattr(result, "embed_failed_pages", 0))
+ return {
+ "full_text": "available",
+ "semantic": semantic,
+ "next_command": (
+ semantic_search_command(run.repo_path, run.scope_embedder)
+ if semantic == "unavailable"
+ else None
+ ),
+ }
+
+
def _stamp_scope(run: InitRun, base_state: dict[str, Any], docs_mode: str) -> None:
- from repowise.cli.providers import semantic_search_status
from repowise.core.generation.selection import count_documentable_files
from repowise.core.index_scope import dropped_files_scope, file_page_scope, stamp_index_scope
@@ -195,15 +214,7 @@ def _stamp_scope(run: InitRun, base_state: dict[str, Any], docs_mode: str) -> No
"reused": False,
"model_cost_possible": docs_mode == "llm",
},
- search={
- "full_text": "available" if result.generated_pages else "unavailable",
- "semantic": (
- semantic_search_status(scope_embedder, getattr(result, "embed_failed_pages", 0))
- if result.generated_pages
- else "unavailable"
- ),
- "next_command": "repowise reindex" if result.generated_pages else None,
- },
+ search=_search_scope(run),
upgrade={
"status": "pending" if docs_mode != "llm" else "not_applicable",
"retryable": docs_mode != "llm",
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_generate.py b/packages/cli/src/repowise/cli/commands/init_cmd/_generate.py
index cc8d6fe47..d8c140cb5 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_generate.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_generate.py
@@ -17,6 +17,7 @@ from ._options import InitRun
from .generation import (
concept_page_count,
cost_gate_declined,
+ embedding_note,
estimate_generation,
format_cost,
page_type_label,
@@ -273,6 +274,7 @@ def _run_generation_phase(
repo_path=repo_path,
skip_tests=skip_tests,
skip_infra=skip_infra,
+ embedder=embedder_name_resolved,
)
_print_generation_plan(est, result, provider, concurrency=concurrency, onboarding=onboarding)
@@ -293,6 +295,9 @@ def _run_generation_phase(
structural = structural_page_summary(plans)
if structural:
console.print(f" [dim]{structural}[/dim]")
+ embedding = embedding_note(est)
+ if embedding:
+ console.print(f" [dim]{embedding}[/dim]")
if cost_gate_declined(est, yes=yes, message=" Continue?"):
console.print(" Skipping the model. [dim]Building the wiki from structure instead.[/dim]")
console.print(
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_options.py b/packages/cli/src/repowise/cli/commands/init_cmd/_options.py
index 473f68698..704f4ea3f 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_options.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_options.py
@@ -86,6 +86,8 @@ class InitRun:
resolved_commit_limit: int = 0
resolved_follow_renames: bool = False
embedder_name_resolved: str = ""
+ # Why that embedder: the flag, pin or env key behind it, for the run to state.
+ embedder_reason: str = ""
embedder_was_requested: bool = False
prior_docs_mode: str | None = None
first_index: bool = False
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_prepare.py b/packages/cli/src/repowise/cli/commands/init_cmd/_prepare.py
index f5e6aec7d..ffd9f4016 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_prepare.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_prepare.py
@@ -28,7 +28,7 @@ from repowise.cli.helpers import (
resolve_max_file_pages,
resolve_reasoning,
)
-from repowise.cli.providers import resolve_embedder
+from repowise.cli.providers import resolve_embedder_choice
from repowise.cli.providers.embedders import embedder_was_requested as _embedder_was_requested
from repowise.cli.ui import WARN, load_dotenv
from repowise.core.docs_mode import resolve_docs_mode
@@ -149,7 +149,7 @@ def _resolve_settings(run: InitRun) -> None:
if opts.follow_renames:
config["follow_renames"] = True
- run.embedder_name_resolved = resolve_embedder(opts.embedder_name)
+ run.embedder_name_resolved, run.embedder_reason = resolve_embedder_choice(opts.embedder_name)
run.embedder_was_requested = _embedder_was_requested(opts.embedder_name, config.get("embedder"))
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_provider.py b/packages/cli/src/repowise/cli/commands/init_cmd/_provider.py
index 879cb0366..7f99d49ea 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_provider.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_provider.py
@@ -9,7 +9,7 @@ import click
from repowise.cli.errors import reasoned_error
from repowise.cli.helpers import console, resolve_explicit_provider_or_prompt, run_async
-from repowise.cli.providers import resolve_embedder
+from repowise.cli.providers import resolve_embedder_choice, template_run_embedder
from repowise.cli.ui import OK, OWL_SPINNER, VALUE, WARN, print_index_only_intro
from ._options import InitRun
@@ -73,13 +73,26 @@ def _print_provider_summary(run: InitRun) -> None:
console.print(
f" Provider: [{VALUE}]{provider.provider_name}[/] / Model: [{VALUE}]{provider.model_name}[/]"
)
- console.print(f" Embedder: [{VALUE}]{run.embedder_name_resolved}[/]")
+ console.print(f" Embedder: [{VALUE}]{_stated_embedder(run)}[/]")
if run.language != "en":
console.print(f" Language: [{VALUE}]{run.language}[/]")
if run.resolved_reasoning != "auto":
console.print(f" Reasoning: [{VALUE}]{run.resolved_reasoning}[/]")
+def _stated_embedder(run: InitRun) -> str:
+ """The embedder this run will use and why, e.g. ``gemini (GEMINI_API_KEY is set)``."""
+ name, reason = run.embedder_name_resolved, run.embedder_reason
+ if run.provider is None:
+ # The template wiki keeps a free run free; the generation phase says so.
+ keyless = template_run_embedder(name, run.embedder_was_requested)
+ if keyless != name:
+ name, reason = keyless, "keyless run"
+ if name == "mock":
+ return f"mock ({reason}, semantic search off)"
+ return f"{name} ({reason})"
+
+
def _verify_provider(provider: Any, reasoning: str) -> None:
"""One tiny generate call, so a bad key fails before the index is built."""
from repowise.core.providers.llm.base import ProviderError
@@ -111,7 +124,9 @@ def resolve_docs_provider(run: InitRun) -> None:
# Provider resolution may have just set the API key in os.environ.
# Re-resolve the embedder so the display and the embed path honor the key
# the user just pasted, rather than the pre-prompt "mock" fallback.
- run.embedder_name_resolved = resolve_embedder(run.opts.embedder_name)
+ run.embedder_name_resolved, run.embedder_reason = resolve_embedder_choice(
+ run.opts.embedder_name
+ )
_print_provider_summary(run)
# Nothing to verify when there is no provider: this run renders from
# templates and never calls a model.
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/generation.py b/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
index 93a1a0e50..a623ea2cb 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
@@ -28,6 +28,7 @@ from repowise.cli.cost_gate import (
confirm_cost_gate,
cost_gate_blocks,
cost_gate_declined,
+ embedding_note,
format_cost,
)
from repowise.cli.helpers import console, run_async
@@ -59,6 +60,7 @@ __all__ = [
"confirm_cost_gate",
"cost_gate_blocks",
"cost_gate_declined",
+ "embedding_note",
"estimate_generation",
"format_cost",
"page_type_label",
@@ -74,8 +76,9 @@ def estimate_generation(
repo_path: Any,
skip_tests: bool,
skip_infra: bool,
+ embedder: str | None = None,
) -> tuple[list[Any], Any]:
- """Build the generation plan and price it. One plan, one estimate.
+ """Build the generation plan and price it, embedding included. One plan, one estimate.
Every structural page type is free, and the concept tree is a total cover of
the production files, so there is nothing to ration and no coverage level to
@@ -85,6 +88,7 @@ def estimate_generation(
Returns ``(plans, estimate)``.
"""
from repowise.cli.cost_estimator import build_generation_plan, estimate_cost
+ from repowise.cli.providers.embedders import resolve_embedding_model
# Curated modules from the in-memory index result, so the plan/cost
# estimate selects the same module set generation will (the artifact
@@ -104,6 +108,8 @@ def estimate_generation(
provider.provider_name,
provider.model_name,
repo_path=repo_path,
+ embedder=embedder,
+ embedding_model=resolve_embedding_model(embedder) if embedder else None,
)
return plans, est
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
index ecb53312e..77635f5ed 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
@@ -411,6 +411,7 @@ def _stamp_full_init_scope(
resolved_commit_limit: int,
max_file_pages: int | None,
embedder_name_resolved: str,
+ repo_path: Path,
) -> None:
"""Attach the canonical scope receipt for a successful model-backed init."""
from repowise.core.generation.selection import count_documentable_files
@@ -426,7 +427,7 @@ def _stamp_full_init_scope(
)
else:
pages = {"effective_cap": None, "eligible": None, "generated": None, "omitted": None}
- from repowise.cli.providers import semantic_search_status
+ from repowise.cli.providers import semantic_search_command, semantic_search_status
semantic = semantic_search_status(
embedder_name_resolved, getattr(result, "embed_failed_pages", 0)
@@ -451,7 +452,11 @@ def _stamp_full_init_scope(
search={
"full_text": "available",
"semantic": semantic,
- "next_command": "repowise reindex" if semantic == "unavailable" else None,
+ "next_command": (
+ semantic_search_command(repo_path, embedder_name_resolved)
+ if semantic == "unavailable"
+ else None
+ ),
},
upgrade={"status": "not_applicable", "retryable": False, "completed_stages": []},
)
@@ -519,6 +524,7 @@ def save_full_state_and_config(
resolved_commit_limit=resolved_commit_limit,
max_file_pages=max_file_pages,
embedder_name_resolved=embedder_name_resolved,
+ repo_path=repo_path,
)
# Same pattern as git_tier: `repowise update` reads this back so its
# graph rebuild keeps the init run's submodule boundary semantics.
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/reporting.py b/packages/cli/src/repowise/cli/commands/init_cmd/reporting.py
index 2b188664f..e02717cdc 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/reporting.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/reporting.py
@@ -158,6 +158,7 @@ def show_completion(
# Unavailable also describes a keyless run; only a failed write earns
# the failure line.
_embed_failed = getattr(result, "embed_failed_pages", 0)
+ _semantic_on = _scope["search"]["semantic"] == "available"
_graph_final = result.graph_builder.graph()
_dc_unreachable = sum(
@@ -270,7 +271,7 @@ def show_completion(
# status and MCP report for the same run.
if _embed_failed:
_search_note = "Full-text\n search works now."
- elif _scope["search"]["semantic"] == "available":
+ elif _semantic_on:
_search_note = "Full-text\n and semantic search work now."
else:
_search_note = (
@@ -285,9 +286,8 @@ def show_completion(
# two upgrades available from here and the command for each, so
# the headless path (no editor setup, no `generate` row in
# build_contextual_next_steps) still learns about them (#3105).
- console.print(
- f" [dim]{keyless_upgrade_note(_scope['search']['semantic'] == 'available')}[/dim]"
- )
+ _note = keyless_upgrade_note(_semantic_on, _scope["search"]["next_command"])
+ console.print(f" [dim]{_note}[/dim]")
_print_embed_failure(_embed_failed)
else:
console.print(
@@ -354,6 +354,11 @@ def show_completion(
)
console.print()
_print_embed_failure(_embed_failed)
+ if not _semantic_on and not _embed_failed and _scope["search"]["next_command"]:
+ console.print(
+ " [dim]Semantic search is off. Run "
+ f"[bold]{_scope['search']['next_command']}[/bold] to turn it on.[/dim]"
+ )
_render_defect_accuracy(result)
# A concise, dynamic MCP note (who is connected, how others connect)
# replaces the old wall of manual per-client config: init already wrote
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
index cffe30e35..8f73aa643 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
@@ -45,7 +45,11 @@ from repowise.cli.helpers import (
save_config_partial,
save_state,
)
-from repowise.cli.providers import resolve_embedder, semantic_search_status
+from repowise.cli.providers import (
+ resolve_embedder,
+ semantic_search_command,
+ semantic_search_status,
+)
from repowise.cli.state_persistence import build_kg_state, save_knowledge_graph_json
from repowise.cli.ui import (
BRAND,
@@ -75,6 +79,7 @@ from .generation import (
CostGateDeclined,
concept_page_count,
cost_gate_declined,
+ embedding_note,
estimate_generation,
format_cost,
run_repo_generation,
@@ -133,6 +138,7 @@ def _run_workspace_generation(
repo_path=repo_path,
skip_tests=skip_tests,
skip_infra=skip_infra,
+ embedder=embedder_name_resolved,
)
# Same decision and same stakes as the single-repo flow, so the count and
@@ -145,6 +151,9 @@ def _run_workspace_generation(
structural = structural_page_summary(plans)
if structural:
console.print(f" [dim]{structural}[/dim]")
+ embedding = embedding_note(est)
+ if embedding:
+ console.print(f" [dim]{embedding}[/dim]")
# Declines rather than raises when it cannot ask (see the note in init's
# _run_generation_phase): a raise would land in the caller's generic error
@@ -627,6 +636,11 @@ def _stamp_repo_scope(
scope_config = load_config(repo_path)
configured_cap = resolve_max_file_pages(config=scope_config)
scope_embedder = _scope_embedder(ctx, docs)
+ semantic = (
+ semantic_search_status(scope_embedder, getattr(result, "embed_failed_pages", 0))
+ if result.generated_pages
+ else "unavailable"
+ )
state["run_mode"] = ctx.run_mode
state["git_tier"] = "essential" if ctx.run_mode == "fast" else "full"
unavailable = [] if getattr(result, "health_report", None) is not None else ["health"]
@@ -653,12 +667,12 @@ def _stamp_repo_scope(
},
search={
"full_text": "available" if result.generated_pages else "unavailable",
- "semantic": (
- semantic_search_status(scope_embedder, getattr(result, "embed_failed_pages", 0))
- if result.generated_pages
- else "unavailable"
+ "semantic": semantic,
+ "next_command": (
+ semantic_search_command(repo_path, scope_embedder)
+ if result.generated_pages and semantic == "unavailable"
+ else None
),
- "next_command": "repowise reindex" if result.generated_pages else None,
},
upgrade={
"status": "pending" if docs_mode != "llm" else "not_applicable",
diff --git a/packages/cli/src/repowise/cli/commands/reindex_cmd.py b/packages/cli/src/repowise/cli/commands/reindex_cmd.py
index 9f4313fa5..5f15e2abc 100644
--- a/packages/cli/src/repowise/cli/commands/reindex_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/reindex_cmd.py
@@ -25,7 +25,8 @@ _INPUT_TOO_LONG_RETRIES = 4
"--embedder",
type=click.Choice(["gemini", "openai", "openrouter", "ollama", "edenai", "mock", "auto"]),
default="auto",
- help="Embedder to use. 'auto' detects from env vars / config.",
+ help="Embedder to use. 'auto' keeps the repo's embedder, or detects one from env keys "
+ "when the repo has none or only the keyless mock.",
)
@click.option(
"--batch-size",
@@ -57,40 +58,10 @@ async def _reindex(repo_path, embedder_name: str, batch_size: int) -> None:
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
- from repowise.cli.providers.embedders import build_embedder, resolve_embedder_for_repo
from repowise.core.persistence.database import create_engine, init_db
from repowise.core.persistence.models import Page
- from repowise.core.providers.embedding.base import MockEmbedder
- # --- Resolve embedder ---
- if embedder_name == "auto":
- # Resolve through the repo, not the environment. `_resolve_embedder(None)`
- # reads REPOWISE_EMBEDDER and API keys only, so `auto` never saw the
- # `embedder` pin in config.yaml despite this flag's help text promising
- # "env vars / config". A repo pinned to a keyless embedder therefore
- # aborted with "No real embedder available", and a repo pinned to one
- # embedder in a shell exporting a different provider's key had its table
- # REWRITTEN by that other provider — the exact writer/reader split
- # `resolve_embedder_for_repo` was added to prevent, and one this command
- # is on the writing side of.
- embedder_name = resolve_embedder_for_repo(repo_path)
-
- embedder_impl = build_embedder(embedder_name, repo_path)
- # Guard on the RESOLVED name, not the flag. `mock` reached here two ways:
- # asked for, or fallen back to because nothing else was configured. Only the
- # second is an error. Now that `auto` reads the pin, `embedder: mock` in
- # config.yaml is a third way, and it is as deliberate as passing the flag —
- # comparing against `requested_embedder` would abort a repo that is pinned
- # to mock on purpose.
- if isinstance(embedder_impl, MockEmbedder) and embedder_name != "mock":
- console.print(
- "[red]No real embedder available. Set a real embedder key, configure Ollama, or pass --embedder mock for test vectors.[/red]"
- )
- raise click.Abort()
- if embedder_name == "mock":
- console.print("[yellow]Using mock embedder (deterministic test vectors)[/yellow]")
- else:
- console.print(f"[green]Using {embedder_name} embedder[/green]")
+ embedder_name, embedder_impl = _embedder_for(repo_path, embedder_name)
# --- Create LanceDB vector store ---
lance_dir = Path(repo_path) / ".repowise" / "lancedb"
@@ -328,6 +299,48 @@ async def _reindex(repo_path, embedder_name: str, batch_size: int) -> None:
raise click.Abort()
+def _embedder_for(repo_path, embedder_name: str) -> tuple[str, object]:
+ """Resolve, build and announce the embedder; exit when auto finds none."""
+ from repowise.cli.providers.embedders import build_embedder, semantic_search_setup_hint
+ from repowise.core.providers.embedding.base import MockEmbedder
+ from repowise.core.providers.embedding.resolve import (
+ embedder_reason,
+ resolve_reindex_embedder,
+ )
+
+ reason = "--embedder"
+ if embedder_name == "auto":
+ # Through the repo, not only the environment: the pin names the backend
+ # that wrote the table, so a key for another provider must not rewrite
+ # it. A mock pin is the exception (see resolve_reindex_embedder).
+ embedder_name, source = resolve_reindex_embedder(repo_path)
+ reason = embedder_reason(embedder_name, source, repo_path=repo_path)
+ # Mock vectors carry no meaning, so rebuilding them adds nothing. Only
+ # an explicit REPOWISE_EMBEDDER=mock (or the flag) still means mock.
+ if embedder_name == "mock" and source in ("config.yaml", "default"):
+ console.print(
+ "[yellow]No embedder found, so semantic search stays off.[/yellow] "
+ f"{semantic_search_setup_hint()}"
+ )
+ raise click.exceptions.Exit(1)
+
+ embedder_impl = build_embedder(embedder_name, repo_path)
+ # A named backend that could not be built fell back to mock: an error, not
+ # a run to finish with test vectors.
+ if isinstance(embedder_impl, MockEmbedder) and embedder_name != "mock":
+ console.print(
+ "[red]No real embedder available. Set a real embedder key, configure Ollama, or pass --embedder mock for test vectors.[/red]"
+ )
+ raise click.Abort()
+ if embedder_name == "mock":
+ console.print(
+ f"Embedder: [yellow]mock[/yellow] ({reason}). Test vectors, no semantic search."
+ )
+ else:
+ console.print(f"Embedder: [green]{embedder_name}[/green] ({reason}).")
+ return embedder_name, embedder_impl
+
+
def _batch_recovery_plan(
exc: Exception,
items: list[tuple[str, str, dict]],
diff --git a/packages/cli/src/repowise/cli/commands/search_cmd.py b/packages/cli/src/repowise/cli/commands/search_cmd.py
index aa73db989..df7d7317f 100644
--- a/packages/cli/src/repowise/cli/commands/search_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/search_cmd.py
@@ -267,7 +267,7 @@ def search_command(
_ta.emit_full(payload)
return
_ta.emit_error(payload, fmt, extra={"query": query, "mode": tool_mode})
- _warn_if_lexical_only(payload, mode, notices)
+ _warn_if_lexical_only(payload, mode, notices, repo_path)
projected = project(payload, query, multi=False)
if fmt == "json":
emit_json(projected)
@@ -290,7 +290,7 @@ def _run_search(repo_path, query: str, limit: int, tool_mode: str) -> dict:
return _ta.run(repo_path, _factory, "search_codebase")
-def _warn_if_lexical_only(payload: dict, requested: str, notices) -> None:
+def _warn_if_lexical_only(payload: dict, requested: str, notices, repo_path=None) -> None:
"""Say when ``--mode semantic`` was answered lexically, and why.
``_meta`` carries ``semantic_search: false`` whenever the vector leg could
@@ -315,9 +315,11 @@ def _warn_if_lexical_only(payload: dict, requested: str, notices) -> None:
why = meta.get("embedder_warning") or (
"No embedder configured for this repo, so there is no semantic index to search."
)
+ from repowise.cli.providers import semantic_search_command
+
notices.print(
- f"[yellow]{why}[/yellow]\nShowing full-text results instead. Set an embedder "
- "key and run [cyan]repowise reindex[/cyan] for semantic search."
+ f"[yellow]{why}[/yellow]\nShowing full-text results instead. Run "
+ f"[cyan]{semantic_search_command(repo_path)}[/cyan] for semantic search."
)
@@ -361,7 +363,7 @@ def _fan_out(
)
continue
payloads.append(payload)
- _warn_if_lexical_only(payload, mode, notices)
+ _warn_if_lexical_only(payload, mode, notices, rp)
for rank, item in enumerate(payload.get("results") or []):
item["repo"] = rp.name
merged.append((rank, item))
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
index d3cf236bc..9af1983c8 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
@@ -102,7 +102,7 @@ def _fail_on_embed_failure(
let a scripted run, or the post-commit log, read a failed write as a
healthy index.
"""
- from repowise.cli.providers import embed_failure_message
+ from repowise.cli.providers import embed_failure_message, semantic_search_command
from repowise.core.index_scope import stamp_index_scope
message = embed_failure_message(embedder, embed_failed_pages)
@@ -112,7 +112,10 @@ def _fail_on_embed_failure(
stamp_index_scope(
state,
load_config(repo_path),
- search={"semantic": "unavailable", "next_command": "repowise reindex"},
+ search={
+ "semantic": "unavailable",
+ "next_command": semantic_search_command(repo_path, embedder),
+ },
)
save_state(repo_path, state)
if emitter is not None:
diff --git a/packages/cli/src/repowise/cli/commands/upgrade_flow.py b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
index 3b7d8813d..667d81198 100644
--- a/packages/cli/src/repowise/cli/commands/upgrade_flow.py
+++ b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
@@ -60,8 +60,11 @@ def _gate_cost(
repo_path: Path,
*,
yes: bool,
+ embedder: str = "mock",
) -> None:
- """Print the estimated spend and confirm past the gate. Raises on decline.
+ """Print the estimated spend, embedding included, and confirm past the gate.
+
+ Raises on decline.
Best-effort on the estimate itself: a failure there prints the reason and
lets the run proceed rather than blocking an upgrade on a number that is
@@ -71,13 +74,22 @@ def _gate_cost(
from repowise.cli.commands.init_cmd.generation import (
COST_GATE_USD,
cost_gate_declined,
+ embedding_note,
format_cost,
structural_page_summary,
)
+ from repowise.cli.providers.embedders import resolve_embedding_model
from repowise.core.cost_estimator import build_generation_plan, estimate_cost
plans = build_generation_plan(parsed_files, graph_builder, config)
- est = estimate_cost(plans, provider.provider_name, provider.model_name, repo_path=repo_path)
+ est = estimate_cost(
+ plans,
+ provider.provider_name,
+ provider.model_name,
+ repo_path=repo_path,
+ embedder=embedder,
+ embedding_model=resolve_embedding_model(embedder),
+ )
except Exception as exc:
console.print(f"[yellow]Cost estimate unavailable ({exc}); continuing.[/yellow]")
return
@@ -87,6 +99,9 @@ def _gate_cost(
structural = structural_page_summary(plans)
if structural:
console.print(f" [dim]{structural}[/dim]")
+ embedding = embedding_note(est)
+ if embedding:
+ console.print(f" [dim]{embedding}[/dim]")
# Nothing to ask on a run that cannot be asked. Without this the confirm
# reads EOF, raises, and the caller reports a successful run that generated
@@ -296,7 +311,15 @@ async def _run_upgrade(
# Show the bill before running it, as init does: the user reaching for
# `--full` has never been shown a cost for this repo.
- _gate_cost(parsed_files, graph_builder, config, provider, repo_path, yes=yes)
+ _gate_cost(
+ parsed_files,
+ graph_builder,
+ config,
+ provider,
+ repo_path,
+ yes=yes,
+ embedder=embedder_name or "mock",
+ )
# 5. Generate the docs the fast index skipped, embedding as we go.
cost_tracker = _upgrade_cost_tracker(sf, repo_id)
@@ -348,8 +371,12 @@ async def _run_upgrade(
total_pages = await _repository_page_count(sf, repo_id, fallback=len(generated_pages))
await engine.dispose()
+ from repowise.cli.providers import semantic_search_command
from repowise.core.generation.selection import count_documentable_files
+ from repowise.core.providers.embedding.base import MockEmbedder
+ # Mock vectors are hashes: a store built on them is no semantic search.
+ semantic_on = vector_store is not None and not isinstance(embedder, MockEmbedder)
return (
generated_pages,
total_pages,
@@ -368,8 +395,10 @@ async def _run_upgrade(
},
"search": {
"full_text": "available",
- "semantic": "available" if vector_store is not None else "unavailable",
- "next_command": "repowise reindex" if vector_store is None else None,
+ "semantic": "available" if semantic_on else "unavailable",
+ "next_command": (
+ None if semantic_on else semantic_search_command(repo_path, embedder_name)
+ ),
},
"embedding_error": embedding_error,
"stub_fallbacks": stub_fallbacks,
@@ -840,14 +869,10 @@ def _upgrade_embedder_name(cfg: dict[str, Any]) -> str | None:
paying a model here, and the mock would leave the written wiki invisible to
semantic search, so re-resolve; the answer is recorded for later updates.
"""
- embedder_name = cfg.get("embedder")
- if not embedder_name or embedder_name == "mock":
- from repowise.cli.providers import resolve_embedder
-
- resolved = resolve_embedder(None)
- if resolved != (embedder_name or "mock"):
- console.print(f"Embedder: [cyan]{resolved}[/cyan] (was mock, index-only's default).")
- embedder_name = resolved
+ from repowise.cli.providers import model_run_embedder
+
+ embedder_name, stated, _replaced = model_run_embedder(cfg.get("embedder"))
+ console.print(f"Embedder: [cyan]{stated}[/cyan].")
return embedder_name
@@ -1003,6 +1028,7 @@ def _report_upgrade(elapsed: float, generated_pages: list[Any], outcome: dict[st
_report_placeholders(outcome.get("stub_fallbacks", 0), outcome.get("stub_reasons", []))
if outcome["search"]["semantic"] != "available":
console.print(
- "[yellow]Semantic search is unavailable.[/yellow] Set an embedder key and run "
- "[bold]repowise reindex[/bold] (embedding calls only; no model calls)."
+ "[yellow]Semantic search is off.[/yellow] Run "
+ f"[bold]{outcome['search']['next_command'] or 'repowise reindex'}[/bold] to turn it on "
+ "(embedding calls only, no model calls)."
)
diff --git a/packages/cli/src/repowise/cli/cost_gate.py b/packages/cli/src/repowise/cli/cost_gate.py
index e733180b5..c023af7ca 100644
--- a/packages/cli/src/repowise/cli/cost_gate.py
+++ b/packages/cli/src/repowise/cli/cost_gate.py
@@ -144,3 +144,16 @@ def format_cost(est: Any) -> str:
cost_str += " (calibrated on this repo's history)"
return cost_str
return f"${est.estimated_cost_usd:.2f} USD"
+
+
+def embedding_note(est: Any) -> str | None:
+ """One line on the embedding share of *est*, ``None`` when none was priced."""
+ embedder = getattr(est, "embedder", "")
+ if not embedder or embedder == "mock":
+ return None
+ pages = f"{est.total_pages:,} pages"
+ usd = est.embedding_cost_usd
+ if not usd:
+ return f"Embedding {pages} with {embedder} adds no cost."
+ amount = "under $0.01" if usd < 0.01 else f"about ${usd:.2f}"
+ return f"Includes {amount} USD to embed {pages} with {embedder}."
diff --git a/packages/cli/src/repowise/cli/providers/__init__.py b/packages/cli/src/repowise/cli/providers/__init__.py
index d375c7b4a..8f0fa69a5 100644
--- a/packages/cli/src/repowise/cli/providers/__init__.py
+++ b/packages/cli/src/repowise/cli/providers/__init__.py
@@ -16,9 +16,13 @@ from repowise.cli.providers.cost_tracking import (
from repowise.cli.providers.embedders import (
build_embedder,
embedder_was_requested,
+ model_run_embedder,
pin_names_an_embedder,
resolve_embedder,
+ resolve_embedder_choice,
resolve_embedder_for_repo,
+ semantic_search_command,
+ semantic_search_setup_hint,
template_run_embedder,
)
from repowise.cli.providers.vector_store import (
@@ -38,9 +42,13 @@ __all__ = [
"existing_vector_dim",
"flush_cost_tracker",
"make_cost_tracker",
+ "model_run_embedder",
"pin_names_an_embedder",
"resolve_embedder",
+ "resolve_embedder_choice",
"resolve_embedder_for_repo",
+ "semantic_search_command",
+ "semantic_search_setup_hint",
"semantic_search_status",
"template_run_embedder",
]
diff --git a/packages/cli/src/repowise/cli/providers/embedders.py b/packages/cli/src/repowise/cli/providers/embedders.py
index aa5f356d8..9939db6f1 100644
--- a/packages/cli/src/repowise/cli/providers/embedders.py
+++ b/packages/cli/src/repowise/cli/providers/embedders.py
@@ -40,14 +40,66 @@ def resolve_embedder(embedder_flag: str | None, env: Mapping[str, str] | None =
this is the choice ``init`` makes afresh. Order: flag > ``REPOWISE_EMBEDDER``
> detection from keys > ``~/.repowise/config.yaml``'s ``embedder`` > mock.
"""
- from repowise.core.providers.embedding.resolve import resolve_embedder_name
+ return resolve_embedder_choice(embedder_flag, env)[0]
+
+
+def resolve_embedder_choice(
+ embedder_flag: str | None, env: Mapping[str, str] | None = None
+) -> tuple[str, str]:
+ """:func:`resolve_embedder` plus why it was picked, for the run to state."""
+ from repowise.core.providers.embedding.resolve import embedder_reason, resolve_embedder_name
overlay = env or {}
def getenv(name: str) -> str | None:
return os.environ.get(name) or overlay.get(name) or None
- return resolve_embedder_name(embedder_flag, getenv=getenv, use_pin=False)[0]
+ name, source = resolve_embedder_name(embedder_flag, getenv=getenv, use_pin=False)
+ return name, embedder_reason(name, source, getenv=getenv)
+
+
+def model_run_embedder(pinned: str | None) -> tuple[str | None, str, bool]:
+ """``(name, stated, replaced_mock)`` for a run that pays a model.
+
+ A keyless run pins ``mock`` because it promised no spend. A run already
+ paying a model re-resolves it, or the written pages stay unsearchable.
+ *stated* is the ``gemini (GEMINI_API_KEY is set)`` text the run prints.
+ """
+ name, reason, upgraded = pinned, "pinned in .repowise/config.yaml", False
+ if not pinned or pinned == "mock":
+ resolved, reason = resolve_embedder_choice(None)
+ if resolved != (pinned or "mock"):
+ if pinned == "mock":
+ reason += ", replacing the keyless mock"
+ name, upgraded = resolved, True
+ stated = name or "mock"
+ if stated == "mock":
+ reason += ", semantic search off"
+ return name, f"{stated} ({reason})", upgraded
+
+
+def semantic_search_command(repo_path: Any = None, used: str | None = None) -> str:
+ """The one command that turns semantic search on for *repo_path* here.
+
+ *used* is the embedder the run wrote with: a real one that failed is the
+ one to retry. Otherwise names the embedder ``reindex`` would pick now, or
+ with none detectable the local one, since it needs no key.
+ """
+ from repowise.core.providers.embedding.resolve import resolve_reindex_embedder
+
+ name = used if used and used != "mock" else resolve_reindex_embedder(repo_path)[0]
+ return f"repowise reindex --embedder {'ollama' if name == 'mock' else name}"
+
+
+def semantic_search_setup_hint() -> str:
+ """How to get an embedder when none is detectable, for a one-line note."""
+ from repowise.core.providers.embedding.resolve import EMBEDDER_KEY_ENVS
+
+ keys = " or ".join(EMBEDDER_KEY_ENVS[name][0] for name in ("gemini", "openai"))
+ return (
+ "Run [bold]repowise reindex --embedder ollama[/bold] with Ollama running, "
+ f"or set {keys} and run [bold]repowise reindex[/bold]."
+ )
def pin_names_an_embedder(pinned_embedder: Any) -> bool:
diff --git a/packages/core/src/repowise/core/cost_estimator/estimator.py b/packages/core/src/repowise/core/cost_estimator/estimator.py
index 488c29bb6..ec2c0d926 100644
--- a/packages/core/src/repowise/core/cost_estimator/estimator.py
+++ b/packages/core/src/repowise/core/cost_estimator/estimator.py
@@ -6,7 +6,7 @@ from pathlib import Path
from .calibration import load_telemetry_averages
from .heuristics import HEURISTIC_VARIANCE, heuristic_tokens
-from .pricing import lookup_cost
+from .pricing import lookup_cost, lookup_embedding_cost
from .types import CostEstimate, CostRange, PageTypePlan
# Page types rendered from structure. They make no provider call, so they cost
@@ -42,6 +42,8 @@ def estimate_cost(
model_name: str,
*,
repo_path: Path | str | None = None,
+ embedder: str | None = None,
+ embedding_model: str | None = None,
) -> CostEstimate:
"""Estimate token counts and USD cost from a generation plan.
@@ -49,6 +51,8 @@ def estimate_cost(
in ``.repowise/db.sqlite``, the estimate is calibrated against the
actual averages from that repo. Otherwise the static heuristics in
:mod:`heuristics` are used and a wider variance bracket is reported.
+
+ With *embedder*, every planned page is also priced for embedding.
"""
telemetry: dict[str, tuple[float, float]] = {}
if repo_path is not None:
@@ -68,6 +72,9 @@ def estimate_cost(
input_rate, output_rate = lookup_cost(model_name) or (0.0, 0.0)
median_cost = (total_input / 1000) * input_rate + (total_output / 1000) * output_rate
+ embed_tokens, embed_cost = _embedding_cost(total_pages, embedder, embedding_model)
+ median_cost += embed_cost
+
# Tighter variance when telemetry calibrated us; wider for cold-start.
variance = 0.10 if is_calibrated else HEURISTIC_VARIANCE
cost_range = CostRange(
@@ -86,4 +93,23 @@ def estimate_cost(
model_name=model_name,
cost_range=cost_range,
is_calibrated=is_calibrated,
+ embedder=embedder or "",
+ estimated_embedding_tokens=embed_tokens,
+ embedding_cost_usd=embed_cost,
)
+
+
+def _embedding_cost(pages: int, embedder: str | None, model: str | None) -> tuple[int, float]:
+ """``(tokens, usd)`` to embed *pages* pages, priced at the per-input cap.
+
+ Ceiling: every page is counted at the embed cap (about 4 characters per
+ token), so this over-estimates; pages run shorter. Upgrade path: average
+ the stored page lengths when a prior index exists.
+ """
+ if not embedder or not pages:
+ return 0, 0.0
+ from repowise.core.persistence.vector_store._base import EMBED_TEXT_MAX_CHARS
+
+ tokens = pages * (EMBED_TEXT_MAX_CHARS // 4)
+ rate = lookup_embedding_cost(embedder, model) or 0.0
+ return tokens, (tokens / 1000) * rate
diff --git a/packages/core/src/repowise/core/cost_estimator/pricing.py b/packages/core/src/repowise/core/cost_estimator/pricing.py
index 3ba844832..789750245 100644
--- a/packages/core/src/repowise/core/cost_estimator/pricing.py
+++ b/packages/core/src/repowise/core/cost_estimator/pricing.py
@@ -130,3 +130,30 @@ def lookup_cost(model_name: str) -> tuple[float, float] | None:
best_prefix = prefix
best_rates = rates
return best_rates
+
+
+# Embedding rates, USD per 1K input tokens. Keyed by model; each embedder's
+# row is its constructor default, so an unset model prices as that default.
+_EMBED_COST_BY_MODEL: dict[str, float] = {
+ "gemini-embedding-001": 0.00015, # $0.15 / MTok
+ "text-embedding-3-small": 0.00002, # $0.02 / MTok
+ "text-embedding-3-large": 0.00013, # $0.13 / MTok
+ "text-embedding-ada-002": 0.0001, # $0.10 / MTok
+ "amazon.titan-embed-text-v2:0": 0.00002, # $0.02 / MTok, before Eden AI's fee
+}
+_EMBED_DEFAULT_MODEL: dict[str, str] = {
+ "gemini": "gemini-embedding-001",
+ "openai": "text-embedding-3-small",
+ "openrouter": "gemini-embedding-001",
+ "edenai": "amazon.titan-embed-text-v2:0",
+}
+# Local or test vectors: free whatever the model.
+_FREE_EMBEDDERS = frozenset({"mock", "ollama"})
+
+
+def lookup_embedding_cost(embedder: str, model: str | None = None) -> float | None:
+ """USD per 1K tokens to embed with *embedder*, ``None`` when unpriced."""
+ if embedder in _FREE_EMBEDDERS:
+ return 0.0
+ name = (model or _EMBED_DEFAULT_MODEL.get(embedder, "")).lower().rsplit("/", 1)[-1]
+ return _EMBED_COST_BY_MODEL.get(name)
diff --git a/packages/core/src/repowise/core/cost_estimator/types.py b/packages/core/src/repowise/core/cost_estimator/types.py
index 2f84b5232..b59e7d2e9 100644
--- a/packages/core/src/repowise/core/cost_estimator/types.py
+++ b/packages/core/src/repowise/core/cost_estimator/types.py
@@ -46,3 +46,8 @@ class CostEstimate:
# True when the estimate was calibrated against historical
# ``wiki_pages`` rows for this repo (tighter than heuristics alone).
is_calibrated: bool = False
+ # Embedding is part of the bill: ``estimated_cost_usd`` and the range
+ # include it. Empty ``embedder`` means the caller did not price it.
+ embedder: str = ""
+ estimated_embedding_tokens: int = 0
+ embedding_cost_usd: float = 0.0
diff --git a/packages/core/src/repowise/core/providers/embedding/resolve.py b/packages/core/src/repowise/core/providers/embedding/resolve.py
index 109b4265f..4a5ccc047 100644
--- a/packages/core/src/repowise/core/providers/embedding/resolve.py
+++ b/packages/core/src/repowise/core/providers/embedding/resolve.py
@@ -131,6 +131,45 @@ def resolve_embedder_name(
return (name.lower(), source) if name and source else (DEFAULT_EMBEDDER, "default")
+def resolve_reindex_embedder(repo_path: Path | str | None) -> tuple[str, str]:
+ """``(name, source)`` that ``reindex --embedder auto`` writes with.
+
+ The repo's pin wins, except a ``mock`` pin: a keyless run persists that on
+ its way out, so it is no choice, and keeping it means reindex never adds
+ semantic search. A real embedder detectable now replaces it.
+ """
+ name, source = resolve_embedder_name(repo_path=repo_path)
+ if name != DEFAULT_EMBEDDER or source != "config.yaml":
+ return name, source
+ fresh = resolve_embedder_name(repo_path=repo_path, use_pin=False)
+ return fresh if fresh[0] != DEFAULT_EMBEDDER else (name, source)
+
+
+_SOURCE_REASONS: dict[str, str] = {
+ "flag": "--embedder",
+ "env:REPOWISE_EMBEDDER": "REPOWISE_EMBEDDER is set",
+ "config.yaml": "pinned in .repowise/config.yaml",
+ "global": "set in ~/.repowise/config.yaml",
+ "default": "no embedder key found",
+}
+
+
+def embedder_reason(
+ name: str,
+ source: str,
+ *,
+ repo_path: Path | str | None = None,
+ getenv: EnvLookup | None = None,
+) -> str:
+ """Why *name* was picked, in a few words: the flag, pin, or env key behind it."""
+ if source != "autodetect":
+ return _SOURCE_REASONS.get(source, source)
+ lookup = getenv or repo_getenv(repo_path)
+ env_vars = dict(_DETECT_SIGNALS).get(name, ())
+ var = next((v for v in env_vars if lookup(v)), None)
+ return f"{var} is set" if var else "detected"
+
+
class KeyLookup(NamedTuple):
"""The resolved key, and the places consulted, for a degraded-run message."""
@@ -261,9 +300,11 @@ __all__ = [
"KeyLookup",
"detect_embedder",
"embedder_kwargs",
+ "embedder_reason",
"embedding_model_for",
"global_config_embedder",
"read_global_embedder_config",
"resolve_embedder_key",
"resolve_embedder_name",
+ "resolve_reindex_embedder",
]
diff --git a/tests/unit/cli/test_embed_failure_reporting.py b/tests/unit/cli/test_embed_failure_reporting.py
index be7812cbc..2204f07f1 100644
--- a/tests/unit/cli/test_embed_failure_reporting.py
+++ b/tests/unit/cli/test_embed_failure_reporting.py
@@ -49,7 +49,7 @@ def test_only_a_real_embedder_that_failed_is_an_error():
assert "pip install --force-reinstall lancedb" in message
-def test_the_full_init_scope_marks_a_failed_embed_unavailable():
+def test_the_full_init_scope_marks_a_failed_embed_unavailable(tmp_path):
from repowise.cli.commands.init_cmd.persistence import _stamp_full_init_scope
def _stamp(failed: int) -> dict:
@@ -67,6 +67,7 @@ def test_the_full_init_scope_marks_a_failed_embed_unavailable():
resolved_commit_limit=10,
max_file_pages=None,
embedder_name_resolved="openai",
+ repo_path=tmp_path,
)
return state["index_scope"]["search"]
@@ -76,7 +77,8 @@ def test_the_full_init_scope_marks_a_failed_embed_unavailable():
"next_command": None,
}
assert _stamp(3)["semantic"] == "unavailable"
- assert _stamp(3)["next_command"] == "repowise reindex"
+ # The embedder that failed is the one to retry.
+ assert _stamp(3)["next_command"] == "repowise reindex --embedder openai"
def test_run_repo_generation_records_the_failed_count(tmp_path, monkeypatch):
@@ -265,7 +267,7 @@ def test_update_marks_semantic_unavailable_and_fails(tmp_path):
state = json.loads((tmp_path / ".repowise" / "state.json").read_text(encoding="utf-8"))
assert state["index_scope"]["search"]["semantic"] == "unavailable"
- assert state["index_scope"]["search"]["next_command"] == "repowise reindex"
+ assert state["index_scope"]["search"]["next_command"] == "repowise reindex --embedder openai"
emitter.error.assert_called_once()
@@ -470,4 +472,4 @@ def test_update_with_a_failing_store_exits_nonzero(tmp_path, monkeypatch, extra_
assert "Embedding failed for" in " ".join(result.output.split())
state = json.loads((repo / ".repowise" / "state.json").read_text(encoding="utf-8"))
assert state["index_scope"]["search"]["semantic"] == "unavailable"
- assert state["index_scope"]["search"]["next_command"] == "repowise reindex"
+ assert state["index_scope"]["search"]["next_command"] == "repowise reindex --embedder ollama"
diff --git a/tests/unit/cli/test_embedder_resolution.py b/tests/unit/cli/test_embedder_resolution.py
index 0d282c04b..1176f62a2 100644
--- a/tests/unit/cli/test_embedder_resolution.py
+++ b/tests/unit/cli/test_embedder_resolution.py
@@ -112,6 +112,100 @@ def test_reader_prefers_the_pinned_embedder_over_a_detected_key(
assert providers.resolve_embedder_for_repo(tmp_path) == "mock"
+# --- reindex, the writer that adds semantic search, skips a mock pin --------
+
+
+def _no_embedder_anywhere(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
+ _clear_embedder_env(monkeypatch)
+ monkeypatch.setattr(
+ "repowise.core.providers.embedding.resolve.global_config_path",
+ lambda: tmp_path / "no-global.yaml",
+ )
+
+
+def test_reindex_replaces_a_mock_pin_when_a_key_is_present(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """The pin a reader trusts is the one reindex rewrites: mock is no choice."""
+ from repowise.core.providers.embedding.resolve import (
+ embedder_reason,
+ resolve_reindex_embedder,
+ )
+
+ _no_embedder_anywhere(monkeypatch, tmp_path)
+ _write_config(tmp_path, embedder="mock")
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
+
+ name, source = resolve_reindex_embedder(tmp_path)
+ assert (name, source) == ("openai", "autodetect")
+ assert embedder_reason(name, source, repo_path=tmp_path) == "OPENAI_API_KEY is set"
+ assert providers.semantic_search_command(tmp_path) == "repowise reindex --embedder openai"
+
+
+def test_reindex_keeps_a_real_pin_over_another_key(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ from repowise.core.providers.embedding.resolve import resolve_reindex_embedder
+
+ _no_embedder_anywhere(monkeypatch, tmp_path)
+ _write_config(tmp_path, embedder="gemini")
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
+
+ assert resolve_reindex_embedder(tmp_path) == ("gemini", "config.yaml")
+
+
+def test_an_exported_mock_still_means_mock(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ from repowise.core.providers.embedding.resolve import resolve_reindex_embedder
+
+ _no_embedder_anywhere(monkeypatch, tmp_path)
+ _write_config(tmp_path, embedder="mock")
+ monkeypatch.setenv("REPOWISE_EMBEDDER", "mock")
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
+
+ assert resolve_reindex_embedder(tmp_path) == ("mock", "env:REPOWISE_EMBEDDER")
+
+
+def test_with_no_embedder_the_command_names_the_local_one(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ from repowise.core.providers.embedding.resolve import resolve_reindex_embedder
+
+ _no_embedder_anywhere(monkeypatch, tmp_path)
+ _write_config(tmp_path, embedder="mock")
+
+ assert resolve_reindex_embedder(tmp_path) == ("mock", "config.yaml")
+ assert providers.semantic_search_command(tmp_path) == "repowise reindex --embedder ollama"
+ # A real embedder that failed is the one to retry, whatever is detectable.
+ assert (
+ providers.semantic_search_command(tmp_path, "gemini")
+ == "repowise reindex --embedder gemini"
+ )
+
+
+def test_a_model_run_states_its_embedder_and_why(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _no_embedder_anywhere(monkeypatch, tmp_path)
+ monkeypatch.setenv("GEMINI_API_KEY", "g-test")
+
+ assert providers.model_run_embedder("mock") == (
+ "gemini",
+ "gemini (GEMINI_API_KEY is set, replacing the keyless mock)",
+ True,
+ )
+ assert providers.model_run_embedder("openai") == (
+ "openai",
+ "openai (pinned in .repowise/config.yaml)",
+ False,
+ )
+ monkeypatch.delenv("GEMINI_API_KEY")
+ assert providers.model_run_embedder(None) == (
+ None,
+ "mock (no embedder key found, semantic search off)",
+ False,
+ )
+
+
def test_an_explicit_env_override_still_beats_the_pin(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
diff --git a/tests/unit/cli/test_reindex_cmd.py b/tests/unit/cli/test_reindex_cmd.py
index c6fbbc248..69de5bdda 100644
--- a/tests/unit/cli/test_reindex_cmd.py
+++ b/tests/unit/cli/test_reindex_cmd.py
@@ -319,25 +319,82 @@ async def test_reindex_auto_honours_the_repo_pinned_embedder(
assert built["name"] == "ollama"
-async def test_reindex_does_not_abort_on_a_deliberately_pinned_mock_embedder(
- monkeypatch,
- tmp_path: Path,
-) -> None:
- """`embedder: mock` in config.yaml is a choice, not a missing embedder.
-
- The abort exists for the fallback case — nothing configured, so build_embedder
- quietly hands back a MockEmbedder and a real reindex would write meaningless
- vectors. A repo that pins mock has said what it wants, the same as passing
- --embedder mock, and must not be treated as unconfigured.
- """
+_EMBEDDER_KEYS = (
+ "REPOWISE_EMBEDDER",
+ "GEMINI_API_KEY",
+ "GOOGLE_API_KEY",
+ "OPENAI_API_KEY",
+ "OPENROUTER_API_KEY",
+ "OLLAMA_EMBEDDING_MODEL",
+ "EDENAI_API_KEY",
+)
+
+
+def _pin_mock(tmp_path: Path, monkeypatch) -> str:
+ """A repo a keyless init left pinned to mock, in a shell with no embedder."""
db_url = f"sqlite+aiosqlite:///{tmp_path / 'wiki.db'}"
(tmp_path / ".repowise").mkdir(parents=True, exist_ok=True)
- (tmp_path / ".repowise" / "config.yaml").write_text(
- "embedder: mock\n", encoding="utf-8"
+ (tmp_path / ".repowise" / "config.yaml").write_text("embedder: mock\n", encoding="utf-8")
+ for var in _EMBEDDER_KEYS:
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setattr(
+ "repowise.core.providers.embedding.resolve.global_config_path",
+ lambda: tmp_path / "no-global.yaml",
)
-
- monkeypatch.delenv("REPOWISE_EMBEDDER", raising=False)
monkeypatch.setattr(reindex_cmd, "get_db_url_for_repo", lambda _repo_path: db_url)
+ return db_url
+
+
+async def test_auto_replaces_a_mock_pin_with_a_detected_embedder(
+ monkeypatch, tmp_path: Path
+) -> None:
+ """A keyless init pins mock on its way out; that is no choice to keep.
+
+ Keeping it meant ``repowise reindex`` never added semantic search, while
+ every surface said it would.
+ """
+ _pin_mock(tmp_path, monkeypatch)
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
+ built: dict[str, str] = {}
+
+ def fake_build_embedder(name: str, _repo_path: object):
+ built["name"] = name
+ raise click.Abort()
+
+ monkeypatch.setattr("repowise.cli.providers.embedders.build_embedder", fake_build_embedder)
+
+ with pytest.raises(click.Abort):
+ await reindex_cmd._reindex(tmp_path, "auto", batch_size=20)
+
+ assert built["name"] == "openai"
+
+
+async def test_auto_on_a_mock_pin_with_no_embedder_prints_the_command(
+ monkeypatch, tmp_path: Path, capsys
+) -> None:
+ """Rebuilding test vectors adds nothing: stop, exit 1, name the way on."""
+ _pin_mock(tmp_path, monkeypatch)
+
+ def fail_build(*_args: object):
+ raise AssertionError("no embedder should be built")
+
+ monkeypatch.setattr("repowise.cli.providers.embedders.build_embedder", fail_build)
+
+ with pytest.raises(click.exceptions.Exit) as exc:
+ await reindex_cmd._reindex(tmp_path, "auto", batch_size=20)
+
+ assert exc.value.exit_code == 1
+ said = " ".join(capsys.readouterr().out.split())
+ assert "semantic search stays off" in said
+ assert "repowise reindex --embedder ollama" in said
+ assert "GEMINI_API_KEY" in said
+ assert not (tmp_path / ".repowise" / "lancedb").exists()
+
+
+async def test_an_explicit_mock_still_means_mock(monkeypatch, tmp_path: Path) -> None:
+ """``--embedder mock`` is a request for test vectors, honoured as asked."""
+ _pin_mock(tmp_path, monkeypatch)
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test")
monkeypatch.setattr(
"repowise.core.persistence.database.create_engine", lambda _url: _DummyEngine()
)
@@ -348,5 +405,5 @@ async def test_reindex_does_not_abort_on_a_deliberately_pinned_mock_embedder(
monkeypatch.setattr("repowise.core.persistence.database.init_db", fake_init_db)
monkeypatch.setattr("sqlalchemy.ext.asyncio.async_sessionmaker", _sessionmaker)
- # No Abort: reaches the normal empty-database path and returns.
- await reindex_cmd._reindex(tmp_path, "auto", batch_size=20)
+ # No Abort and no Exit: reaches the normal empty-database path and returns.
+ await reindex_cmd._reindex(tmp_path, "mock", batch_size=20)
diff --git a/tests/unit/test_providers/test_cost_estimator.py b/tests/unit/test_providers/test_cost_estimator.py
index 382135b19..535b965c9 100644
--- a/tests/unit/test_providers/test_cost_estimator.py
+++ b/tests/unit/test_providers/test_cost_estimator.py
@@ -93,3 +93,45 @@ def test_estimate_cost_zero_for_mock():
plans = [PageTypePlan("module_page", 5, 4)]
est = estimate_cost(plans, "mock", "mock")
assert est.estimated_cost_usd == 0.0
+
+
+def test_embedding_is_part_of_the_estimate():
+ """Every page is embedded, structural ones included, so all are priced."""
+ from repowise.core.cost_estimator.pricing import lookup_embedding_cost
+ from repowise.core.persistence.vector_store._base import EMBED_TEXT_MAX_CHARS
+
+ plans = [PageTypePlan("file_page", 1000, 2)]
+ est = estimate_cost(plans, "gemini", "gemini-3.1-flash-lite-preview", embedder="openai")
+ tokens = 1000 * (EMBED_TEXT_MAX_CHARS // 4)
+ assert est.estimated_embedding_tokens == tokens
+ assert est.embedding_cost_usd == pytest.approx(tokens / 1000 * 0.00002)
+ assert est.estimated_cost_usd == pytest.approx(est.embedding_cost_usd)
+ assert est.cost_range.high > est.embedding_cost_usd
+
+ assert lookup_embedding_cost("gemini") == 0.00015
+ assert lookup_embedding_cost("openai", "text-embedding-3-large") == 0.00013
+ assert lookup_embedding_cost("ollama", "anything") == 0.0
+ assert lookup_embedding_cost("mock") == 0.0
+
+
+def test_local_and_unpriced_embedding_adds_nothing():
+ plans = [PageTypePlan("file_page", 10, 2)]
+ for embedder in ("ollama", "mock", None):
+ est = estimate_cost(plans, "mock", "mock", embedder=embedder)
+ assert est.embedding_cost_usd == 0.0
+ assert est.estimated_cost_usd == 0.0
+
+
+def test_the_embedding_note_names_the_share():
+ from repowise.cli.cost_gate import embedding_note
+
+ plans = [PageTypePlan("file_page", 1000, 2)]
+ note = embedding_note(estimate_cost(plans, "mock", "mock", embedder="gemini"))
+ assert note == "Includes about $0.60 USD to embed 1,000 pages with gemini."
+ few = [PageTypePlan("file_page", 3, 2)]
+ tiny = embedding_note(estimate_cost(few, "mock", "mock", embedder="openai"))
+ assert tiny == "Includes under $0.01 USD to embed 3 pages with openai."
+ free = embedding_note(estimate_cost(plans, "mock", "mock", embedder="ollama"))
+ assert free == "Embedding 1,000 pages with ollama adds no cost."
+ assert embedding_note(estimate_cost(plans, "mock", "mock", embedder="mock")) is None
+ assert embedding_note(estimate_cost(plans, "mock", "mock")) is None
diff --git a/tests/unit/test_providers/test_provider_specs.py b/tests/unit/test_providers/test_provider_specs.py
index 044e89fbe..6eac07dcd 100644
--- a/tests/unit/test_providers/test_provider_specs.py
+++ b/tests/unit/test_providers/test_provider_specs.py
@@ -85,8 +85,10 @@ _ALLOWED: dict[str, frozenset[str]] = {
"core/src/repowise/core/ingestion/git_indexer/agent_provenance.py": frozenset(
{"gemini", "opencode"}
),
- # Price keys are model families (``deepseek-v4``, ``kimi-k2``, ``gemini``).
- "core/src/repowise/core/cost_estimator/pricing.py": frozenset({"deepseek", "gemini", "kimi"}),
+ # Price keys are model families (``deepseek-v4``, ``kimi-k2``, ``gemini``),
+ # and embedding rates are keyed by embedder.
+ "core/src/repowise/core/cost_estimator/pricing.py": frozenset({"deepseek", "gemini", "kimi"})
+ | _EMBEDDERS,
# Third-party SDK names found in a scanned repo's imports.
"core/src/repowise/core/ingestion/external_systems/classifier.py": frozenset(
{"anthropic", "openai"}