Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(workspace): an added repo gets the state init writes, so the first update has nothing to redo (#4310)
Raghav Chamadiya06b911e7f31f
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.pyindex 4fb1edd1e..bdb71b2a5 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py@@ -626,6 +626,8 @@ def _stamp_repo_scope(from repowise.core.index_scope import dropped_files_scope, stamp_index_scoperesult, repo_provider, docs_mode = index.result, index.provider, docs.docs_mode+ # A model can write the docs without mining decisions (workspace add).+ scope_provider = ctx.provider if repo_provider is None and docs_mode == "llm" else repo_providerscope_config = load_config(repo_path)configured_cap = resolve_max_file_pages(config=scope_config)scope_embedder = _scope_embedder(ctx, docs)@@ -653,8 +655,8 @@ def _stamp_repo_scope("skipped": ["generation"] if ctx.run_mode == "fast" else [],},provider={- "name": repo_provider.provider_name if repo_provider is not None else None,- "model": repo_provider.model_name if repo_provider is not None else None,+ "name": scope_provider.provider_name if scope_provider is not None else None,+ "model": scope_provider.model_name if scope_provider is not None else None,"embedder": scope_embedder,"reused": False,"model_cost_possible": docs_mode == "llm",diff --git a/packages/cli/src/repowise/cli/commands/workspace_cmd/_index.py b/packages/cli/src/repowise/cli/commands/workspace_cmd/_index.pyindex f1a3e84bf..bdebe6304 100644--- a/packages/cli/src/repowise/cli/commands/workspace_cmd/_index.py+++ b/packages/cli/src/repowise/cli/commands/workspace_cmd/_index.py@@ -4,7 +4,7 @@ from __future__ import annotationsfrom datetime import UTCfrom pathlib import Path-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Anyfrom repowise.cli.helpers import (console,@@ -14,7 +14,7 @@ from repowise.cli.helpers import (run_async,warn,)-from repowise.core.docs_mode import DocsMode, docs_mode_state_fields, resolve_docs_mode+from repowise.core.docs_mode import DocsMode, resolve_docs_modefrom repowise.core.index_scope import fast_to_full_commandif TYPE_CHECKING:@@ -120,99 +120,100 @@ def _run_index_for_repo(base commit), saves provider/model into ``config.yaml`` when docs ran,and re-runs cross-repo hooks so contracts/co-changes are fresh."""- from repowise.cli.helpers import ensure_repowise_dir, save_config+ from repowise.cli.commands.init_cmd.workspace import _RepoDocs+ from repowise.cli.helpers import ensure_repowise_dirconsole.print(f" Indexing [cyan]{alias}[/cyan]…")# Reuse the primary repo's provider/embedder/exclude settings when the# caller hasn't overridden them.primary_cfg = _load_primary_config(ws_root, ws_config)- embedder_name = primary_cfg.get("embedder", "mock")- exclude_patterns = list(primary_cfg.get("exclude_patterns") or [])- commit_limit = primary_cfg.get("commit_limit")-# ``provider`` is None exactly when no model runs.provider = Noneif docs_mode == "llm":- provider = _resolve_docs_provider(- repo_path,- provider_name or primary_cfg.get("provider"),- model or primary_cfg.get("model"),- )- if provider is None:- docs_mode = "deterministic"+ provider = _resolve_docs_provider(repo_path, primary_cfg, provider_name, model)+ docs_mode = "llm" if provider is not None else "deterministic"ensure_repowise_dir(repo_path)_inherit_distill_verdict(repo_path, primary_cfg)- try:- file_count, symbol_count = _index_added_repo(repo_path, commit_limit, exclude_patterns)- console.print(f" [green]✓[/green] {file_count} files, {symbol_count:,} symbols")- except Exception as exc:- warn(f"Indexing failed for '{alias}': {exc}")+ ctx = _added_repo_ctx(provider, ws_config, primary_cfg, concurrency=concurrency, save_key=save_key)+ index = _index_added_repo(repo_path, alias, ctx)+ if index is None:return+ docs = _RepoDocs(docs_mode=docs_mode, skip_reason=docs_skip_reason)+ _document_added_repo(repo_path, ctx, index, docs)+ _save_added_repo_config(repo_path, ctx, primary_cfg)+ head, state = _save_added_repo_state(repo_path, ctx, index, docs)+ _record_in_workspace(ws_root, ws_config, alias, head)+ _note_missing_docs(alias, state, docs.skip_reason)- generated_pages = 0- resolved_reasoning = resolve_reasoning(config=primary_cfg)- if docs_mode != "none":- generated_pages, error = _build_added_repo_docs(- repo_path=repo_path,- provider=provider,- embedder_name=embedder_name,- concurrency=concurrency,- reasoning=resolved_reasoning,- exclude_patterns=exclude_patterns,- )++def _document_added_repo(repo_path: Path, ctx: Any, index: Any, docs: Any) -> None:+ """Build the documentation *docs* asks for into the index result; never raises."""+ pages: list[Any] = []+ if docs.docs_mode != "none":+ pages, error = _build_added_repo_docs(repo_path, ctx)if error is not None:- docs_skip_reason = error+ docs.skip_reason = error# A model run keeps its provider state so `update --docs` retries it.- if provider is None:- docs_mode = "none"-- head, state = _save_added_repo_state(repo_path, provider, generated_pages, docs_mode)-- # Persist provider settings into the added repo's config.yaml so future- # `repowise update` runs don't have to re-prompt.- if provider is not None:- save_config(- repo_path,- provider.provider_name,- provider.model_name,- embedder_name,- exclude_patterns=exclude_patterns or None,- commit_limit=int(commit_limit) if commit_limit else None,- reasoning=resolved_reasoning,- save_key=save_key,- )+ if ctx.provider is None:+ docs.docs_mode = "none"+ index.result.generated_pages = pages+ docs.pages_generated = len(pages)- _record_in_workspace(ws_root, ws_config, alias, head)- # Honest completion notice — exact remediation command for the- # docs-skipped case.- if resolve_docs_mode(state) == "none":- reason = docs_skip_reason or "docs disabled"- console.print(f"\n[yellow]Note:[/yellow] '{alias}' indexed without docs ({reason}).")- console.print(- f" Run [bold]{fast_to_full_command(alias)}[/bold] to build its documentation."- )---def _build_added_repo_docs(*, provider: object | None, **kwargs: object) -> tuple[int, str | None]:+def _build_added_repo_docs(repo_path: Path, ctx: Any) -> tuple[list[Any], str | None]:"""Generate the added repo's documentation as ``(pages, error)``; never raises."""+ provider = ctx.providertry:- pages = _generate_docs_for_added_repo(provider=provider, **kwargs) # type: ignore[arg-type]+ pages = _generate_docs_for_added_repo(+ repo_path=repo_path,+ provider=provider,+ concurrency=ctx.concurrency,+ reasoning=ctx.resolved_reasoning,+ exclude_patterns=ctx.exclude_patterns,+ )except Exception as exc:console.print(f" [yellow]Documentation failed: {exc}[/yellow]")- return 0, f"generation error: {exc}"+ return [], f"generation error: {exc}"source = ("built from code"if provider is Noneelse f"written by {getattr(provider, 'model_name', 'the model')}")- console.print(f" [green]✓[/green] Documentation: {pages} pages, {source}")+ console.print(f" [green]✓[/green] Documentation: {len(pages)} pages, {source}")return pages, None+def _save_added_repo_config(repo_path: Path, ctx: Any, primary_cfg: dict) -> None:+ """Save the model settings into config.yaml when a model ran, so `update` need not ask."""+ if ctx.provider is None:+ return+ from repowise.cli.helpers import save_config++ commit_limit = primary_cfg.get("commit_limit")+ save_config(+ repo_path,+ ctx.provider.provider_name,+ ctx.provider.model_name,+ primary_cfg.get("embedder", "mock"),+ exclude_patterns=ctx.exclude_patterns or None,+ commit_limit=int(commit_limit) if commit_limit else None,+ reasoning=ctx.resolved_reasoning,+ save_key=ctx.save_key,+ )+++def _note_missing_docs(alias: str, state: dict, skip_reason: str | None) -> None:+ """Honest completion notice, with the exact command, when no docs were built."""+ if resolve_docs_mode(state) != "none":+ return+ reason = skip_reason or "docs disabled"+ console.print(f"\n[yellow]Note:[/yellow] '{alias}' indexed without docs ({reason}).")+ console.print(f" Run [bold]{fast_to_full_command(alias)}[/bold] to build its documentation.")++def _load_primary_config(ws_root: Path,ws_config: WorkspaceConfig, # type: ignore[name-defined]@@ -234,17 +235,22 @@ def _load_primary_config(def _resolve_docs_provider(- repo_path: Path, provider_name: str | None, model: str | None+ repo_path: Path, primary_cfg: dict, provider_name: str | None, model: str | None) -> object | None:"""The model provider, or None (with a notice) when it does not resolve.- The caller then builds the documentation from code rather than leave the+ An unset *provider_name* or *model* falls back to the primary repo's. On+ None the caller builds the documentation from code rather than leave therepo with none."""from repowise.cli.helpers import resolve_providertry:- provider = resolve_provider(provider_name, model, repo_path=repo_path)+ provider = resolve_provider(+ provider_name or primary_cfg.get("provider"),+ model or primary_cfg.get("model"),+ repo_path=repo_path,+ )except Exception as exc:console.print(f" [yellow]Provider unavailable ({exc}); building documentation from code.[/yellow]"@@ -257,55 +263,95 @@ def _resolve_docs_provider(return provider-def _index_added_repo(- repo_path: Path, commit_limit: object, exclude_patterns: list[str]-) -> tuple[int, int]:- """Full-index the repo, returning ``(file_count, symbol_count)``.+def _index_added_repo(repo_path: Path, alias: str, ctx: Any) -> Any:+ """Full-index the repo into init's per-repo ``_RepoIndex``; None (warned) on failure.Shared full-index step (run pipeline, persist, export the curated KGartifact so doc generation can load curated module grouping) — the same- helper the workspace updater's first-time indexing uses.+ helper the workspace updater's first-time indexing uses. No model mines+ decisions here, so the index carries no provider."""+ from repowise.cli.commands.init_cmd.workspace import _RepoIndex+ from repowise.core.pipeline import PhaseTimingRecorder+ from repowise.core.pipeline.full_index import index_repo_full- async def _do_index() -> tuple[int, int]:- from repowise.core.ingestion.git_indexer._constants import _DEFAULT_COMMIT_LIMIT- from repowise.core.pipeline.full_index import index_repo_full-- result = await index_repo_full(- repo_path,- commit_depth=int(commit_limit) if commit_limit else _DEFAULT_COMMIT_LIMIT,- exclude_patterns=exclude_patterns,+ recorder = PhaseTimingRecorder()+ try:+ result = run_async(+ index_repo_full(+ repo_path,+ commit_depth=ctx.resolved_commit_limit,+ exclude_patterns=ctx.exclude_patterns,+ progress=recorder,+ ))- return result.file_count, result.symbol_count+ except Exception as exc:+ warn(f"Indexing failed for '{alias}': {exc}")+ return None+ console.print(f" [green]✓[/green] {result.file_count} files, {result.symbol_count:,} symbols")+ return _RepoIndex(result=result, callback=recorder, provider=None)+++def _added_repo_ctx(+ provider: object | None,+ ws_config: WorkspaceConfig, # type: ignore[name-defined]+ primary_cfg: dict,+ *,+ concurrency: int,+ save_key: bool,+) -> Any:+ """Workspace init's run settings for this one repo, so its state is init's.++ Exclude patterns, reasoning and the commit limit come from the primary repo.+ """+ from repowise.cli.commands.init_cmd.workspace import _WorkspaceCtx+ from repowise.core.generation.styles import DEFAULT_STYLE+ from repowise.core.ingestion.git_indexer._constants import _DEFAULT_COMMIT_LIMIT- return run_async(_do_index())+ commit_limit = primary_cfg.get("commit_limit")+ return _WorkspaceCtx(+ provider=provider,+ ws_config=ws_config,+ editor_options=None,+ index_only=provider is None,+ dry_run=False,+ force=False,+ follow_renames=False,+ include_submodules=False,+ exclude_patterns=list(primary_cfg.get("exclude_patterns") or []),+ skip_tests=False,+ skip_infra=False,+ concurrency=concurrency,+ test_run=False,+ yes=False,+ resume=False,+ onboarding=True,+ wiki_style=DEFAULT_STYLE,+ language="en",+ resolved_reasoning=resolve_reasoning(config=primary_cfg),+ # Pages go to full-text search only; none are embedded.+ embedder_name_resolved=None, # type: ignore[arg-type]+ embedder_was_requested=False,+ resolved_commit_limit=int(commit_limit) if commit_limit else _DEFAULT_COMMIT_LIMIT,+ save_key=save_key,+ )def _save_added_repo_state(- repo_path: Path, provider: object | None, generated_pages: int, docs_mode: DocsMode+ repo_path: Path, ctx: Any, index: Any, docs: Any) -> tuple[str | None, dict]:- """Write ``state.json`` so `repowise update` has a baseline commit.+ """Write the state workspace init writes for a repo; returns ``(head, state)``.- Returns ``(head, state)``.+ Called after the add's config writes, which the config fingerprint reads."""+ from repowise.cli.commands.init_cmd.persistence import stamp_fresh_index+ from repowise.cli.commands.init_cmd.workspace import _repo_statefrom repowise.cli.helpers import get_head_commit, save_state- from repowise.core.ingestion.git_indexer import GIT_HISTORY_VERSION, GIT_HISTORY_VERSION_KEYhead = get_head_commit(repo_path)- state: dict = {- "last_sync_commit": head,- "total_pages": generated_pages,- **docs_mode_state_fields(docs_mode),- # The full index just walked git under the current history rules.- GIT_HISTORY_VERSION_KEY: GIT_HISTORY_VERSION,- }- if provider is not None:- state["provider"] = provider.provider_name- state["model"] = provider.model_name- # `workspace add` freshly full-indexes the repo, so this from-scratch state- # is a current-format store, not one predating the concept tree. Stamp the- # terminal version rather than clamping to v1 and nagging a repo this run- # just built to re-index itself.+ state = _repo_state(head, repo_path, ctx, index, docs, [])+ stamp_fresh_index(state, repo_path, index.result)+ # Freshly full-indexed, so stamp the terminal store format.save_state(repo_path, state, full_index=True)return head, state@@ -338,12 +384,11 @@ def _generate_docs_for_added_repo(*,repo_path: Path,provider: object | None,- embedder_name: str,concurrency: int,reasoning: str,exclude_patterns: list[str],-) -> int:- """Generate wiki pages for a newly-added workspace repo.+) -> list[Any]:+ """Generate wiki pages for a newly-added workspace repo; returns them.With no *provider* the pages are built from code: templates, no model call.@@ -400,7 +445,7 @@ def _generate_docs_for_added_repo(provider, assembler, config, language=config.language, repo_path=repo_path)- async def _do() -> int:+ async def _do() -> list[Any]:pages = await generator.generate_all(parsed_files,source_map,@@ -425,6 +470,6 @@ def _generate_docs_for_added_repo(await fts.ensure_index()await fts.index_pages(pages)await engine.dispose()- return len(pages)+ return pagesreturn run_async(_do())diff --git a/tests/unit/cli/test_workspace_add_index.py b/tests/unit/cli/test_workspace_add_index.pyindex 52bcc690d..8607a4c87 100644--- a/tests/unit/cli/test_workspace_add_index.py+++ b/tests/unit/cli/test_workspace_add_index.py@@ -84,7 +84,7 @@ def test_docs_off_writes_state_and_stamps_the_entry(ws, capsys):def test_docs_on_records_the_provider(ws, monkeypatch, capsys):ws_root, cfg, calls = wsmonkeypatch.setattr("repowise.cli.helpers.resolve_provider", lambda *a, **k: _provider())- monkeypatch.setattr(_index, "_generate_docs_for_added_repo", lambda **_k: 5)+ monkeypatch.setattr(_index, "_generate_docs_for_added_repo", lambda **_k: [object()] * 5)_run(ws_root, cfg, docs_mode="llm", save_key=False)state = load_state(ws_root / "added")@@ -92,6 +92,14 @@ def test_docs_on_records_the_provider(ws, monkeypatch, capsys):assert state["provider"] == "fake"assert state["model"] == "fake-1"assert resolve_docs_mode(state) == "llm"+ # The scope names the docs model; add mines no decisions, so lanes stay pending.+ from repowise.core.decision_lanes import PENDING_DECISION_LANES_KEY, lanes_needing_model+ from repowise.core.index_scope import resolve_index_scope++ scope_provider = resolve_index_scope(state)["provider"]+ assert (scope_provider["name"], scope_provider["model"]) == ("fake", "fake-1")+ assert lanes_needing_model(ws_root / "added")+ assert state[PENDING_DECISION_LANES_KEY] == list(lanes_needing_model(ws_root / "added"))(args, kwargs), = calls["save_config"]assert args[1:3] == ("fake", "fake-1")assert kwargs["save_key"] is False@@ -110,7 +118,7 @@ def test_provider_failure_falls_back_to_built_from_code(ws, monkeypatch, capsys)def fake_generate(**kwargs):seen.update(kwargs)- return 4+ return [object()] * 4monkeypatch.setattr("repowise.cli.helpers.resolve_provider", boom)monkeypatch.setattr(_index, "_generate_docs_for_added_repo", fake_generate)@@ -136,7 +144,7 @@ def test_default_builds_from_code_without_a_model(ws, monkeypatch, capsys):def fake_generate(**kwargs):seen.update(kwargs)- return 7+ return [object()] * 7monkeypatch.setattr("repowise.cli.helpers.resolve_provider", no_model)monkeypatch.setattr(_index, "_generate_docs_for_added_repo", fake_generate)@@ -182,3 +190,39 @@ def test_index_failure_stops_before_any_state(ws, monkeypatch):assert "last_sync_commit" not in load_state(ws_root / "added")assert calls["hooks"] == []assert WorkspaceConfig.load(ws_root).get_repo("added").last_commit_at_index is None+++def test_state_matches_what_workspace_init_writes(ws, monkeypatch):+ """The added repo carries init's stamps and scope, so the first update has nothing to redo."""+ from repowise.cli.helpers import config_fingerprint+ from repowise.core.analysis.health import HEALTH_ANALYZER_VERSION+ from repowise.core.analysis.security_scan import SECURITY_SCANNER_VERSION+ from repowise.core.index_scope import resolve_index_scope++ ws_root, cfg, _calls = ws+ coverage = SimpleNamespace(to_dict=lambda: {"mode": "full", "commits": 12})++ async def fake_index(repo_path, **_kwargs):+ return SimpleNamespace(+ file_count=3,+ symbol_count=1234,+ git_summary=SimpleNamespace(history_coverage=coverage),+ health_report=object(),+ )++ monkeypatch.setattr("repowise.core.pipeline.full_index.index_repo_full", fake_index)+ monkeypatch.setattr(_index, "_generate_docs_for_added_repo", lambda **_k: [object()] * 2)+ _run(ws_root, cfg, docs_mode="deterministic")++ repo = ws_root / "added"+ state = load_state(repo)+ assert state["config_fingerprint"] == config_fingerprint(repo)+ assert state["config_dependency_fingerprints"]+ assert state["renderer_fingerprint"]+ assert state["health_analyzer_version"] == HEALTH_ANALYZER_VERSION+ assert state["security_scanner_version"] == SECURITY_SCANNER_VERSION+ assert state["git_history_coverage"] == {"mode": "full", "commits": 12}+ scope = resolve_index_scope(state)+ assert scope["git_tier"] == "full"+ assert scope["content_provenance"] == "template"+ assert scope["search"]["full_text"] == "available"diff --git a/tests/unit/cli/test_workspace_kg_artifact_wiring.py b/tests/unit/cli/test_workspace_kg_artifact_wiring.pyindex 413d91238..a269e152e 100644--- a/tests/unit/cli/test_workspace_kg_artifact_wiring.py+++ b/tests/unit/cli/test_workspace_kg_artifact_wiring.py@@ -82,7 +82,6 @@ def test_generate_docs_for_added_repo_passes_repo_path(tmp_path):_generate_docs_for_added_repo(repo_path=repo_path,provider=object(),- embedder_name="fake",concurrency=1,reasoning="low",exclude_patterns=[],