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(security): update rescans the files it changed (#3530)

Raghav Chamadiyaf871d26d4380
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 c03cad94f..8aafd824f 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
@@ -1891,6 +1891,7 @@ def run_update(
file_diffs=file_diffs,
knowledge_graph_result=knowledge_graph_result,
parsed_files=parsed_files,
+ source_map=source_map,
degraded=degraded,
# The repo's docs mode, not this run's page count: a commit
# that touched no source file re-renders nothing, and that is
@@ -2523,6 +2524,7 @@ def run_update(
doc_drift_report=doc_drift_report,
decay_paths=affected.decay_only,
parsed_files=parsed_files,
+ source_map=source_map,
git_decay_map=git_decay_map,
full_git_summary=(full_git_summaries[0] if full_git_summaries else None),
reconcile_full_scope=traversal_config_changed,
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index 83507a986..37b4a2500 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -428,6 +428,7 @@ def _persist_index_only_update(
file_diffs: list | None = None,
knowledge_graph_result: Any | None = None,
parsed_files: list | None = None,
+ source_map: dict | None = None,
degraded: list[str] | None = None,
pages_rendered: int = 0,
template_wiki: bool = False,
@@ -480,6 +481,7 @@ def _persist_index_only_update(
file_diffs=file_diffs,
knowledge_graph_result=knowledge_graph_result,
parsed_files=parsed_files,
+ source_map=source_map,
git_decay_map=git_decay_map,
full_git_summary=full_git_summary,
history_window_changed=history_window_changed,
@@ -719,6 +721,7 @@ def _persist_full_update(
doc_drift_report: Any | None = None,
decay_paths: list[str] | None = None,
parsed_files: list | None = None,
+ source_map: dict | None = None,
git_decay_map: dict | None = None,
full_git_summary: Any | None = None,
reconcile_full_scope: bool = False,
@@ -761,6 +764,7 @@ def _persist_full_update(
doc_drift_report=doc_drift_report,
decay_paths=decay_paths,
parsed_files=parsed_files,
+ source_map=source_map,
git_decay_map=git_decay_map,
full_git_summary=full_git_summary,
reconcile_full_scope=reconcile_full_scope,
@@ -791,6 +795,7 @@ async def _persist_full_update_async(
doc_drift_report: Any | None = None,
decay_paths: list[str] | None = None,
parsed_files: list | None = None,
+ source_map: dict | None = None,
git_decay_map: dict | None = None,
full_git_summary: Any | None = None,
reconcile_full_scope: bool = False,
@@ -1285,6 +1290,24 @@ async def _persist_full_update_async(
except Exception as exc:
_skip("Symbol persist", exc)
+ try:
+ from repowise.core.pipeline.persist import persist_incremental_security_findings
+
+ with timed(timings, "persist.security"):
+ await persist_incremental_security_findings(
+ session,
+ repo_id,
+ parsed_files,
+ source_map,
+ (
+ [parsed.file_info.path for parsed in parsed_files or []]
+ if reconcile_full_scope
+ else [fd.path for fd in file_diffs]
+ ),
+ )
+ except Exception as exc:
+ _skip("Security persist", exc)
+
# Refresh graph_edges for the changed files (delete-then-insert of
# each file's outgoing edges). Without this, adjacency fossilizes at
# the last full index and Phase E flow-path answers decay on every
diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.py
index 10b72b4c3..fae4f9bb5 100644
--- a/packages/core/src/repowise/core/pipeline/incremental.py
+++ b/packages/core/src/repowise/core/pipeline/incremental.py
@@ -1919,6 +1919,7 @@ async def persist_incremental_index(
file_diffs: list[Any] | None = None,
knowledge_graph_result: Any | None = None,
parsed_files: list[Any] | None = None,
+ source_map: dict[str, Any] | None = None,
git_decay_map: dict | None = None,
full_git_summary: Any | None = None,
history_window_changed: bool = False,
@@ -2185,6 +2186,16 @@ async def persist_incremental_index(
except Exception as exc:
_skip("Symbol persist", exc, range_scoped=True)
+ try:
+ from repowise.core.pipeline.persist import persist_incremental_security_findings
+
+ with timed(timings, "persist.security"):
+ await persist_incremental_security_findings(
+ session, repo_id, parsed_files, source_map, changed_paths
+ )
+ except Exception as exc:
+ _skip("Security persist", exc, range_scoped=True)
+
# Refresh graph_edges for the changed files. The full-init path was
# historically the only writer of edges, so adjacency froze at the
# last full index: new imports/calls stayed invisible and dropped
diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.py
index 76212710b..95db66b09 100644
--- a/packages/core/src/repowise/core/pipeline/persist.py
+++ b/packages/core/src/repowise/core/pipeline/persist.py
@@ -1731,10 +1731,39 @@ async def persist_security_findings(result: Any, session: Any, repo_id: str) ->
it off ``file_info`` yields empty text and a scan that can never fire.
Resume views without a ``source_map`` degrade to the symbol-name scan.
"""
+ source_map = getattr(result, "source_map", None) or {}
+ await _replace_security_findings(session, repo_id, result.parsed_files, source_map)
+
+
+async def persist_incremental_security_findings(
+ session: Any,
+ repo_id: str,
+ parsed_files: list[Any] | None,
+ source_map: dict[str, Any] | None,
+ changed_paths: list[str],
+) -> None:
+ """Rescan the changed and added files' working-tree security findings.
+
+ Without this, update kept every finding from the last full index: new
+ secrets in changed files were missed and old rows pointed at moved lines.
+ Deleted files are dropped by ``prune_deleted_file_rows``. No source map
+ means no text to scan, and scanning empty text would wipe real findings,
+ so that case is a no-op.
+ """
+ if not parsed_files or not source_map:
+ return
+ changed = set(changed_paths)
+ targets = [pf for pf in parsed_files if pf.file_info.path in changed]
+ if targets:
+ await _replace_security_findings(session, repo_id, targets, source_map)
+
+
+async def _replace_security_findings(
+ session: Any, repo_id: str, parsed_files: Any, source_map: dict[str, Any]
+) -> None:
from repowise.core.analysis.security_scan import SecurityScanner, scan_source_map
- source_map = getattr(result, "source_map", None) or {}
- findings_by_file, scanned_paths = scan_source_map(result.parsed_files, source_map)
+ findings_by_file, scanned_paths = scan_source_map(parsed_files, source_map)
await SecurityScanner(session, repo_id).replace_findings(findings_by_file, scanned_paths)
diff --git a/packages/core/src/repowise/core/workspace/update.py b/packages/core/src/repowise/core/workspace/update.py
index 9d2d4e3d7..838edc46e 100644
--- a/packages/core/src/repowise/core/workspace/update.py
+++ b/packages/core/src/repowise/core/workspace/update.py
@@ -593,6 +593,7 @@ async def _incremental_repo_update(
file_diffs=file_diffs,
knowledge_graph_result=kg,
parsed_files=parsed_files,
+ source_map=source_map,
git_decay_map=git_decay_map,
full_git_summary=full_git_summaries[0] if full_git_summaries else None,
failed_steps=failed_steps,
diff --git a/tests/unit/cli/test_security_update_rescan.py b/tests/unit/cli/test_security_update_rescan.py
new file mode 100644
index 000000000..d483e8063
--- /dev/null
+++ b/tests/unit/cli/test_security_update_rescan.py
@@ -0,0 +1,130 @@
+"""Every update path rescans the files it changed for security findings.
+
+Before this, only ``init`` and a scanner version bump wrote working-tree
+findings, so an update left new findings in changed files unseen and old
+rows pointing at lines that had moved.
+"""
+
+from __future__ import annotations
+
+import asyncio
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+from sqlalchemy import text
+
+_HASH = "import hashlib\n\n\ndef f(x):\n return hashlib.md5(x)\n"
+
+
+async def _seed(repo_path: Path) -> None:
+ """Rows as the last full index wrote them: ``a.py`` before an edit moved
+ its finding, and a stale row on untouched ``c.py`` that must stay put."""
+ from repowise.cli.helpers import get_db_url_for_repo
+ from repowise.core.persistence import (
+ create_engine,
+ create_session_factory,
+ get_session,
+ init_db,
+ upsert_repository,
+ )
+
+ engine = create_engine(get_db_url_for_repo(repo_path))
+ await init_db(engine)
+ async with get_session(create_session_factory(engine)) as session:
+ repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))
+ for path, line in (("a.py", 2), ("c.py", 9)):
+ await session.execute(
+ text(
+ "INSERT INTO security_findings (repository_id, file_path, kind, "
+ "severity, snippet, line_number, commit_sha, detected_at) "
+ "VALUES (:r, :p, 'weak_hash', 'low', 'x', :l, '', datetime('now'))"
+ ),
+ {"r": repo.id, "p": path, "l": line},
+ )
+ await engine.dispose()
+
+
+async def _rows(repo_path: Path) -> set[tuple]:
+ from repowise.cli.helpers import get_db_url_for_repo
+ from repowise.core.persistence import create_engine, create_session_factory, get_session
+
+ engine = create_engine(get_db_url_for_repo(repo_path))
+ async with get_session(create_session_factory(engine)) as session:
+ res = await session.execute(
+ text("SELECT file_path, kind, line_number FROM security_findings")
+ )
+ rows = {tuple(r) for r in res.fetchall()}
+ await engine.dispose()
+ return rows
+
+
+def _repo(tmp_path: Path):
+ from repowise.cli.commands.update_cmd.incremental import _build_repo_graph
+
+ (tmp_path / ".repowise").mkdir()
+ (tmp_path / "c.py").write_text("def c():\n return 3\n", encoding="utf-8")
+ asyncio.run(_seed(tmp_path))
+ # The edit: a.py's finding moves to line 5, and b.py is new.
+ (tmp_path / "a.py").write_text(_HASH, encoding="utf-8")
+ (tmp_path / "b.py").write_text(_HASH, encoding="utf-8")
+ parsed_files, source_map, graph_builder, _structure, _count = _build_repo_graph(
+ tmp_path, [], collect_sources=True
+ )
+ return parsed_files, source_map, graph_builder
+
+
+def _persist_index_only(tmp_path, parsed_files, source_map, graph_builder, changed):
+ from repowise.core.pipeline.incremental import persist_incremental_index
+
+ asyncio.run(
+ persist_incremental_index(
+ tmp_path,
+ graph_builder,
+ {},
+ None,
+ None,
+ changed,
+ parsed_files=parsed_files,
+ source_map=source_map,
+ log=lambda *_a, **_k: None,
+ )
+ )
+
+
+def _persist_docs_mode(tmp_path, parsed_files, source_map, graph_builder, changed):
+ from repowise.cli.commands.update_cmd.persistence import _persist_full_update_async
+
+ asyncio.run(
+ _persist_full_update_async(
+ repo_path=tmp_path,
+ repo_name=tmp_path.name,
+ generated_pages=[],
+ file_diffs=[SimpleNamespace(path=p, old_path=None, status="modified") for p in changed],
+ git_meta_map={},
+ new_decision_markers=[],
+ decision_vector_store=None,
+ provider=None,
+ partial_health_report=None,
+ dead_code_report=None,
+ graph_builder=graph_builder,
+ knowledge_graph_result=None,
+ degraded=[],
+ parsed_files=parsed_files,
+ source_map=source_map,
+ )
+ )
+
+
[email protected]("persist", [_persist_index_only, _persist_docs_mode])
+def test_update_rescans_changed_and_added_files(tmp_path: Path, persist) -> None:
+ parsed_files, source_map, graph_builder = _repo(tmp_path)
+
+ persist(tmp_path, parsed_files, source_map, graph_builder, ["a.py", "b.py"])
+
+ rows = asyncio.run(_rows(tmp_path))
+ assert ("a.py", "weak_hash", 5) in rows
+ assert ("a.py", "weak_hash", 2) not in rows
+ assert ("b.py", "weak_hash", 5) in rows
+ # Scoped to the change: an untouched file's rows are not this run's to rewrite.
+ assert ("c.py", "weak_hash", 9) in rows
diff --git a/tests/unit/pipeline/test_incremental_failure_isolation.py b/tests/unit/pipeline/test_incremental_failure_isolation.py
index 02399f78e..d67ee82d8 100644
--- a/tests/unit/pipeline/test_incremental_failure_isolation.py
+++ b/tests/unit/pipeline/test_incremental_failure_isolation.py
@@ -141,6 +141,7 @@ def test_the_range_scoped_set_is_exactly_these_steps():
"Dead-code persist",
"Health persist",
"Symbol persist",
+ "Security persist",
"Graph edges persist",
"External systems refresh",
"Tombstone full-text removal",