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(init): stop --force in a worktree from becoming a paid upgrade (#2339)
Nehal kanak kuniyil thottathilf8a264de965a
diff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.mdindex e6b8950dc..07516d642 100644--- a/docs/reference/CLI_REFERENCE.md+++ b/docs/reference/CLI_REFERENCE.md@@ -166,7 +166,7 @@ All three reach the indexing knobs; the LLM-only knobs appear only when model-wr| `--wiki-style` | Documentation voice/density: `comprehensive` (default), `caveman` (token-condensed, AI-first), `reference` (API-manual), `tutorial` (beginner-friendly). Interactive full runs prompt when omitted. Saved to config so `update` keeps the style. See [WIKI.md](../layers/WIKI.md#styles). || `--language` | Output language for generated wiki pages: `en` (default), `ar`, `de`, `es`, `fr`, `hi`, `it`, `ja`, `ko`, `nl`, `pl`, `pt`, `ru`, `tr`, `zh`. Code, file paths, and symbol names stay untranslated. Saved to config so `update` keeps the language. Also asked in advanced interactive mode. To switch an existing wiki's language, set the flag and re-run `init --force`. || `--resume` | Continue a previous run instead of redoing it: completed phases (indexing, analysis) are skipped, the earlier run's git tier is kept, and generation writes only the pages this repo does not have yet. Use it after an interrupted run, and after one that finished with failed pages (a provider outage, rate limiting) — pages already written are skipped with no model call, so nothing is paid for twice. Matching is per page, not per model, so switching provider still keeps what the old one wrote. |-| `--force` | Regenerate all pages even if they exist |+| `--force` | Regenerate all pages even if they exist. Re-indexes this checkout from scratch in the mode you invoked, so inside a linked worktree it also skips seeding (a re-index is what seeding exists to avoid) and runs as a normal full init. Never triggers a model: `--force --no-prose` re-renders the whole wiki from templates at no cost. Use `repowise update --full` to regenerate with a model. || `--commit-limit` | Max commits to analyze per file (default: 500, capped at 10000) || `--follow-renames` | Track file renames in git history || `--no-claude-md` | Don't generate `CLAUDE.md` |diff --git a/docs/scale/WORKTREES.md b/docs/scale/WORKTREES.mdindex b7ed5289f..1bb0182c8 100644--- a/docs/scale/WORKTREES.md+++ b/docs/scale/WORKTREES.md@@ -29,6 +29,7 @@ Auto-seeding only fires when all of these hold; otherwise repowise falls back to- The worktree has no `.repowise/state.json` yet (an already-indexed worktree is left alone).- The base checkout has a healthy index (`state.json` + `wiki.db`).- Base and worktree share the same initial commit, and the base's last synced commit is an ancestor of the worktree's HEAD.+- `--force` was not passed.## Overrides@@ -36,6 +37,7 @@ Auto-seeding only fires when all of these hold; otherwise repowise falls back to|------|--------|| `--no-seed` | Force a cold full init inside a worktree; skip auto-detection entirely. || `--seed-from <path>` | Seed from an explicit checkout instead of the auto-detected base. Useful for unusual layouts, e.g. seeding one full clone from another. All the same validations apply. |+| `--force` | Re-index this checkout from scratch, in the mode you invoked. Seeding is skipped, because a re-index is the work seeding exists to avoid. Still free: `--force --no-prose` renders the whole wiki from templates with no model. `repowise update --full` remains the only path that regenerates with a model. |`--seed-from` also works outside worktrees: any two checkouts of the same repository qualify, as long as they share history.diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.pyindex 0af3d52fc..3b617b55a 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py@@ -943,10 +943,30 @@ def init_command(detected = detect_worktree_base(repo_path)if detected is not None and base_is_seedable(detected):seed_base = detected- console.print(- f"[dim]\\[worktree][/dim] Linked worktree of {detected} detected; "- f"seeding its index."- )+ if not force:+ console.print(+ f"[dim]\\[worktree][/dim] Linked worktree of {detected} detected; "+ f"seeding its index."+ )++ # ``--force`` asks for the re-index seeding exists to avoid, so the two do+ # not combine. The block below delegates to ``run_update`` and used to hand+ # it ``full=force``, but ``full`` is not "re-index": it is ``update --full``,+ # the fast -> full upgrade that resolves a provider and regenerates the+ # whole wiki with a model. So ``init --force`` in a seedable worktree either+ # died with "No provider configured" or spent money the user never asked for+ # (#1482), and ``--index-only --force`` was swallowed the same way. Dropping+ # the seed lets the ordinary init path below run, which is exactly what+ # ``init --force`` does outside a worktree: re-index and regenerate every+ # page in the mode the user invoked, free when that mode is structural.+ # ``update --full`` stays the only paid path.+ if seed_base is not None and force:+ requested = "the index at" if not seed_from else "the requested seed"+ console.print(+ f"[dim]\\[worktree][/dim] --force re-indexes this checkout from "+ f"scratch, so {requested} {seed_base} is not used."+ )+ seed_base = Noneif seed_base is not None:seed_root = scan.root if getattr(scan, "root", None) else repo_path@@ -992,7 +1012,12 @@ def init_command(repo_alias=None,index_only=index_only,docs_flag=None,- full=force,+ # Never ``force``. This delegate is the worktree *seed*+ # catch-up, and ``full`` means ``update --full``: provider+ # resolution plus whole-repo model regeneration. A+ # ``--force`` run never reaches this block (the seed is+ # dropped above), so this is a constant, not a pass-through.+ full=False,agents_md=agents_md,concurrency=concurrency,no_cost_tracking=no_cost_tracking,diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.pyindex f4a712a3d..9203fc4c4 100644--- a/tests/integration/test_cli.py+++ b/tests/integration/test_cli.py@@ -1565,3 +1565,221 @@ class TestWorktreeAutoSeed:assert (worktree_dir / ".repowise" / "state.json").exists()finally:_remove_worktree(git_work_repo, worktree_dir)+++_TEST_PROVIDER_ENV_VARS = (+ "REPOWISE_PROVIDER",+ "ANTHROPIC_API_KEY",+ "OPENAI_API_KEY",+ "OPENROUTER_API_KEY",+ "GEMINI_API_KEY",+ "GOOGLE_API_KEY",+ "DEEPSEEK_API_KEY",+ "KIMI_API_KEY",+ "EDENAI_API_KEY",+ "LITELLM_API_KEY",+ "OLLAMA_BASE_URL",+)+++def _no_model_configured(monkeypatch) -> None:+ """Strip every provider key so a run resolves to no provider at all."""+ for var in _TEST_PROVIDER_ENV_VARS:+ monkeypatch.delenv(var, raising=False)+++class TestWorktreeInitForce:+ """``init --force`` inside a seedable worktree (#1482).++ ``--force`` is documented as "Regenerate all pages, ignoring existing" and+ is the free re-index escape hatch. The worktree seed path forwarded it to+ the delegated ``run_update`` as ``full=``, and ``full`` is ``update --full``:+ the fast -> full upgrade that resolves a provider and regenerates the whole+ wiki with a model. So ``init --force`` in a worktree died with "No provider+ configured" when no key was set, and silently billed a whole-repo model+ regeneration when one was. The fix drops the seed for a ``--force`` run, so+ the ordinary init path re-indexes in the mode the user invoked.+ """++ @staticmethod+ def _spy_on_upgrade(monkeypatch) -> list:+ """Record any ``upgrade_to_full`` call; the paid path must never run."""+ import repowise.cli.commands.upgrade_flow as upgrade_flow++ calls: list = []+ monkeypatch.setattr(+ upgrade_flow, "upgrade_to_full", lambda *a, **k: calls.append((a, k))+ )+ return calls++ def test_init_force_without_provider_reindexes_instead_of_dying(+ self, git_work_repo, monkeypatch+ ):+ """No key configured: ``init --force`` must still produce a wiki.++ Pre-fix this exited non-zero with "No provider configured", because the+ seed path routed the run into ``upgrade_to_full`` -> ``resolve_provider``.+ """+ import json++ from click.testing import CliRunner++ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ _no_model_configured(monkeypatch)++ r0 = CliRunner().invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ upgrade_calls = self._spy_on_upgrade(monkeypatch)++ worktree_dir = git_work_repo.parent / "force-no-provider"+ _git(["worktree", "add", "-b", "force-no-provider", str(worktree_dir)], git_work_repo)+ try:+ r1 = CliRunner().invoke(+ cli, ["init", str(worktree_dir), "--force"], catch_exceptions=False+ )+ assert r1.exit_code == 0, r1.output+ flat = " ".join(r1.output.split())+ assert "No provider configured" not in flat+ assert upgrade_calls == [], "init --force must not enter the paid upgrade path"+ # The seed is deliberately skipped: --force asks for the re-index+ # seeding exists to avoid, so no delegation line and no seeded index.+ assert "Delegating to update" not in flat+ assert "--force re-indexes this checkout from scratch" in flat++ state = json.loads(+ (worktree_dir / ".repowise" / "state.json").read_text(encoding="utf-8")+ )+ assert state["last_sync_commit"] == _rev_parse(worktree_dir, "HEAD")+ # The worktree gets its own store, and the fresh init wrote a wiki.+ assert (worktree_dir / ".repowise" / "wiki.db").exists()+ assert _db_scalar(worktree_dir / ".repowise" / "wiki.db", "SELECT COUNT(*) FROM wiki_pages") > 0+ repos = _db_column(+ worktree_dir / ".repowise" / "wiki.db",+ "SELECT local_path FROM repositories",+ )+ assert repos == [str(worktree_dir)], repos+ finally:+ _remove_worktree(git_work_repo, worktree_dir)++ def test_index_only_force_in_worktree_stays_free(self, git_work_repo, monkeypatch):+ """``--index-only --force`` is a template re-index, not a model run.++ The issue calls this out separately: the flag was swallowed the same way,+ so a run sold as free could reach ``resolve_provider``.+ """+ import json++ from click.testing import CliRunner++ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ _no_model_configured(monkeypatch)++ r0 = CliRunner().invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ upgrade_calls = self._spy_on_upgrade(monkeypatch)++ worktree_dir = git_work_repo.parent / "force-index-only"+ _git(["worktree", "add", "-b", "force-index-only", str(worktree_dir)], git_work_repo)+ try:+ r1 = CliRunner().invoke(+ cli,+ ["init", str(worktree_dir), "--index-only", "--force"],+ catch_exceptions=False,+ )+ assert r1.exit_code == 0, r1.output+ flat = " ".join(r1.output.split())+ assert upgrade_calls == []+ assert "No provider configured" not in flat+ assert "Building the wiki from structure" in flat+ assert "no model, no spend" in flat++ state = json.loads(+ (worktree_dir / ".repowise" / "state.json").read_text(encoding="utf-8")+ )+ assert state["docs_mode"] == "deterministic"+ finally:+ _remove_worktree(git_work_repo, worktree_dir)++ def test_force_with_provider_does_not_run_the_paid_upgrade(self, git_work_repo, monkeypatch):+ """With a provider configured, ``init --force`` must not bill a full regen.++ This is the other half of the bug: pre-fix the run printed the+ ``repowise update --full`` upgrade banner and regenerated the whole wiki+ through ``upgrade_to_full``.+ """+ from click.testing import CliRunner++ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)++ r0 = CliRunner().invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ upgrade_calls = self._spy_on_upgrade(monkeypatch)++ worktree_dir = git_work_repo.parent / "force-with-provider"+ _git(["worktree", "add", "-b", "force-with-provider", str(worktree_dir)], git_work_repo)+ try:+ r1 = CliRunner().invoke(+ cli,+ ["init", str(worktree_dir), "--force", "--provider", "mock"],+ catch_exceptions=False,+ )+ assert r1.exit_code == 0, r1.output+ flat = " ".join(r1.output.split())+ assert upgrade_calls == [], "init --force delegated to the paid full upgrade"+ # The paid upgrade prints its own banner and upgrades the git tier+ # to FULL. Pre-fix both of these appeared on this exact command.+ assert "repowise update --full" not in flat+ assert "Git tier upgraded to FULL" not in flat+ assert "--force re-indexes this checkout from scratch" in flat+ finally:+ _remove_worktree(git_work_repo, worktree_dir)++ def test_plain_init_still_seeds_and_never_asks_for_full(self, git_work_repo, monkeypatch):+ """Control: without --force the seed delegate still runs, and as free catch-up.++ The seed path must never hand ``run_update`` a ``full=True``, whatever+ the flag combination: that mapping was the whole bug.+ """+ from click.testing import CliRunner++ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)++ r0 = CliRunner().invoke(+ cli, ["init", str(git_work_repo), "--index-only"], catch_exceptions=False+ )+ assert r0.exit_code == 0, r0.output++ seen: list[dict] = []+ import repowise.cli.commands.update_cmd.command as update_command++ real_run_update = update_command.run_update++ def _recording_run_update(**kwargs):+ seen.append(kwargs)+ return real_run_update(**kwargs)++ monkeypatch.setattr(update_command, "run_update", _recording_run_update)++ worktree_dir = git_work_repo.parent / "plain-init-seed"+ _git(["worktree", "add", "-b", "plain-init-seed", str(worktree_dir)], git_work_repo)+ try:+ r1 = CliRunner().invoke(+ cli, ["init", str(worktree_dir), "--index-only"], catch_exceptions=False+ )+ assert r1.exit_code == 0, r1.output+ flat = " ".join(r1.output.split())+ assert "Worktree index seeded successfully" in flat+ assert "Delegating to update" in flat+ assert seen, "the seed path should still delegate to update"+ assert all(call["full"] is False for call in seen), seen+ finally:+ _remove_worktree(git_work_repo, worktree_dir)