From 18d244fe05827c4cd3ab9aa85819f5f177c29761 Mon Sep 17 00:00:00 2001 From: Melwinjoel20 Date: Wed, 23 Sep 2026 01:26:46 +0100 Subject: [PATCH] feat(memory-bank): store git commit hash per run --- src/secfoo/runner.py | 17 +++++++++++++++++ src/secfoo/storage/models.py | 2 +- src/secfoo/storage/repository.py | 6 +++++- src/secfoo/storage/schema.sql | 4 +++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/secfoo/runner.py b/src/secfoo/runner.py index 6b34786..fa33757 100644 --- a/src/secfoo/runner.py +++ b/src/secfoo/runner.py @@ -39,6 +39,20 @@ STDERR_EXCERPT_LIMIT = 2000 +def _git_head_commit(path: Path) -> str | None: + """Return the HEAD commit SHA for a git repo at `path`, or None if + the directory isn't a git repo or git isn't available.""" + import subprocess + try: + result = subprocess.run( + ["git", "-C", str(path), "rev-parse", "HEAD"], + capture_output=True, text=True, timeout=10, + ) + return result.stdout.strip() if result.returncode == 0 else None + except Exception: + return None + + @dataclass class RunOutcome: run_uuid: str @@ -206,6 +220,8 @@ def _run_single_skill( adapter = get_adapter(agent_id) result = adapter.run(prompt, workdir=target_ctx.local_path, timeout=timeout) + # Memory Bank: capture HEAD SHA so future runs can diff against this baseline. + target_commit = _git_head_commit(target_ctx.local_path) if on_skill_complete: on_skill_complete(skill.name, result.status) @@ -233,6 +249,7 @@ def _run_single_skill( input_tokens=result.input_tokens, output_tokens=result.output_tokens, cost_usd=result.cost_usd, + target_commit=target_commit, # Memory Bank: persisted for incremental diff rescans. ) if result.status == "success": diff --git a/src/secfoo/storage/models.py b/src/secfoo/storage/models.py index f2dfab2..42e98aa 100644 --- a/src/secfoo/storage/models.py +++ b/src/secfoo/storage/models.py @@ -41,7 +41,7 @@ class RunRecord: output_tokens: int | None = None cost_usd: float | None = None project_display_name: str | None = None - cost_usd: float | None = None + target_commit: str | None = None # Memory Bank: git HEAD SHA at scan time. @dataclass(frozen=True) diff --git a/src/secfoo/storage/repository.py b/src/secfoo/storage/repository.py index 075b6fd..819a67d 100644 --- a/src/secfoo/storage/repository.py +++ b/src/secfoo/storage/repository.py @@ -72,6 +72,8 @@ def use_db_path(path: Path): "input_tokens": "ALTER TABLE runs ADD COLUMN input_tokens INTEGER", "output_tokens": "ALTER TABLE runs ADD COLUMN output_tokens INTEGER", "cost_usd": "ALTER TABLE runs ADD COLUMN cost_usd REAL", + # Memory Bank: git HEAD SHA at scan time; foundation for incremental diff-based rescans. + "target_commit": "ALTER TABLE runs ADD COLUMN target_commit TEXT", } _ASSESSMENTS_MIGRATIONS = { @@ -290,12 +292,13 @@ def complete_run( input_tokens: int | None = None, output_tokens: int | None = None, cost_usd: float | None = None, + target_commit: str | None = None, # Memory Bank: HEAD SHA of the scanned repo. ) -> None: self._conn.execute( "UPDATE runs SET status = ?, exit_code = ?, finished_at = ?, duration_seconds = ?, " "report_path = ?, prompt_path = ?, stderr_excerpt = ?, critical_count = ?, high_count = ?, " "medium_count = ?, low_count = ?, info_count = ?, input_tokens = ?, output_tokens = ?, " - "cost_usd = ? WHERE run_uuid = ?", + "cost_usd = ?, target_commit = ? WHERE run_uuid = ?", ( status, exit_code, @@ -312,6 +315,7 @@ def complete_run( input_tokens, output_tokens, cost_usd, + target_commit, run_uuid, ), ) diff --git a/src/secfoo/storage/schema.sql b/src/secfoo/storage/schema.sql index f6f431c..6047185 100644 --- a/src/secfoo/storage/schema.sql +++ b/src/secfoo/storage/schema.sql @@ -70,7 +70,9 @@ CREATE TABLE IF NOT EXISTS runs ( cost_usd REAL, -- Set once `secfoo cloud` has pushed this run to the enterprise -- portal. NULL means never synced (offline, or no `cloud login` yet). - cloud_synced_at TEXT + cloud_synced_at TEXT, + -- Memory Bank: git HEAD SHA at scan time; enables future incremental diff-based rescans. + target_commit TEXT ); -- Files manually uploaded to an assessment (AI-BOM inventories, or other