Skip to content

Feature/memory bank commit tracking - #22

Closed
Melwinjoel20 wants to merge 6 commits into
secfoo-com:mainfrom
Melwinjoel20:feature/memory-bank-commit-tracking
Closed

Melwinjoel20 wants to merge 6 commits into
secfoo-com:mainfrom
Melwinjoel20:feature/memory-bank-commit-tracking

Conversation

@Melwinjoel20

@Melwinjoel20 Melwinjoel20 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

Every rescan of a repository was sending the entire codebase to the model, even when only a handful of files had changed since the last scan. On a mid-sized Django project this meant ~54,000 input tokens per run, however small the change.

This implements the Memory Bank incremental diff: on a rescan with --agent api, only the files that changed since the last successful scan are sent to the model. The previous run's commit SHA (target_commit, stored since v0.3.0) is the diff baseline. A full scan is still done on the first run, when the previous commit can't be reached, or when nothing scannable changed.

Because an incremental run only sees part of the codebase, it now records which files it covered (scan_scope), and the findings lifecycle only closes findings inside that set. Findings in files the run never looked at stay open.

Results (two private Django projects, api agent, gpt-4.1-mini, one-file change between scans):

  • Project A: 54,578 to 3,833 input tokens (93% less)
  • Project B, scanned as a GitHub URL target: 39,544 to 6,290 input tokens (84% less). Findings from the first run in unchanged files stayed open after the incremental run.

What

  • repository.py: get_last_commit() returns the most recent successful target_commit for a project and skill; find_project_id() looks up a project without creating one; close_stale_findings() / close_stale_sast_findings() take only_files and only close findings inside it (SCA has no file column, so an incremental run never closes SCA findings); new nullable scan_scope column on runs via the existing migration list.
  • base.py: prev_commit kwarg on AgentAdapter.run() (CLI adapters accept and ignore it); AgentResult.scan_scope (None means a full scan).
  • api.py: _git_changed_files() diffs prev_commit against the working tree plus untracked files, so uncommitted edits are included; renames report the old path as deleted (--no-renames); a missing commit is fetched with git fetch --depth=1 origin <sha> instead of --unshallow. The collect node goes incremental only if at least one changed file is scannable, otherwise it does a full scan, and it returns the scan_scope.
  • runner.py: passes prev_commit to the adapter, saves scan_scope on the run and passes it to the SAST, secret scanning and SCA lifecycles. No prompt in execute_runs() (it also runs under the dashboard and CI); new already_scanned_skills() helper.
  • cli.py: asks "Commit X was already scanned for ... Scan again?" only on a terminal and not with --json, skips only the repeated skills, and --rescan bypasses the question.

Testing

  • pytest: all passing except the 4 Mermaid dashboard tests that also fail on main
  • ruff check on the touched files: no new issues compared with main
  • New tests/test_memory_bank.py (18 tests): findings in unchanged files stay open, fixed findings in changed files close, deleted files close their findings, an image-only commit falls back to a full scan, uncommitted and untracked files are sent, renames, unreachable commit, subfolder targets, no stdin read in execute_runs(), the CLI question and --rescan, and the scoped close in storage. These tests fail on the previous version of this branch.
  • Manual: two private Django projects (results above), including a GitHub URL target to exercise the depth-1 fetch of the previous commit

Follow-ups (not in this PR)

  • Portal ingest re-runs the findings lifecycle for synced runs without reading scan_scope (private repo).
  • Incremental mode applies to every skill on the api agent; should it be limited to file-based skills (SAST, secret scanning)?
  • An incremental run's report and severity counts cover only the changed files; the run page could label it as incremental.
  • Separate bug on main: not_configured status isn't allowed by the runs CHECK constraint, so secfoo run -a api with no key crashes.

Jira: SECFOO-13

@Varunpoojari Varunpoojari left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the rebase, Melwin. It sits cleanly on current main (6947c89), the base.py methods from #21, #23 and #26 are intact, and prev_commit is back in run(). On Linux I get 760 passed, 4 failed; the same 4 dashboard tests fail on main, so nothing new is broken.

The token saving is real and worth having. I'm asking for changes because of how an incremental scan interacts with code that already exists on main. Items 1 to 3 can produce a wrong result that looks like a clean one.

1. An incremental SAST scan closes findings in files it never looked at

update_sast_findings() closes every open finding that is not in the new report. That is correct for a full scan. After an incremental scan the report only covers the changed files, so every open finding in an unchanged file is marked closed.

Reproduced on this branch with a two-file repo (app/db.py with a SQL injection, app/views.py with an XSS). I scanned it, committed a one-line change to views.py only, and scanned again. The agent was a stand-in for the model that uses this PR's _git_changed_files() and collect_files(changed_only=...) and reports a finding only for a file it was shown:

after full scan   : SQL Injection (open), Reflected XSS (open)
model was shown   : incremental, app/views.py only
after incremental : SQL Injection (closed)   <- db.py was not changed and not scanned

Suggested direction: record on the run that it was incremental and which paths it covered, and only close findings whose file is in that set (changed or deleted). Findings elsewhere stay as they were.

2. A commit with no scannable files sends the model nothing

collect() takes the incremental branch whenever changed or deleted is non-empty, but collect_files() then drops binaries, *.lock, and the excluded directories. If that removes everything, the prompt says "this is an incremental scan" followed by no source.

Same repo, one commit that adds only logo.png:

model was shown   : incremental, 0 characters of source
after incremental : SQL Injection (closed), Reflected XSS (closed)

So committing an image marks every open finding as fixed. If the filtered set is empty, fall back to a full scan, or skip the run and say why.

3. Uncommitted changes are skipped

git diff prev..HEAD only sees commits, while the full scan reads the working tree. With a local target that has one new commit plus an uncommitted new file, the new file is not sent. Before this PR it would have been scanned. Either add the paths from git status --porcelain to the changed set, or do a full scan when the tree is dirty.

4. The "Scan again? (yes/no)" prompt is in execute_runs()

execute_runs() is shared with the dashboard (web/routes/assessments.py), so the input() call runs in places with no terminal:

  • No stdin (CI, a service, the dashboard): EOFError: EOF when reading a line, and the run crashes.
  • secfoo run --json: the prompt text is written to stdout, ahead of the JSON document that is meant to be byte-exact.
  • Answering "no" for one skill returns [] for all of them.

Suggested: move the question into cli.py, ask only when sys.stdin.isatty() (there is already a helper for that near line 155), and add a flag such as --rescan so scripts can answer it.

5. Tests

The description ticks "Added/updated tests for the behavior change", but the only test change is the prev_commit=None argument on the fake adapters. Nothing covers get_last_commit(), _git_changed_files(), collect_files(changed_only=...) or the prompt. Cases 1 to 4 above would each make a good test; I can share the script I used.

6. Smaller things

  • README.md ends with a stray # updated line, and the branch has a test: trigger incremental scan commit. Both look like leftovers from testing.
  • ruff check on the touched files reports 13 problems against 10 on main. The 3 new ones are in api.py: two subprocess.run calls without check= and one bare except Exception.
  • git fetch --unshallow runs before every diff. GitHub targets are shallow clones, so each rescan downloads the full history, and if that passes the 60 second timeout the code falls back to a full scan without saying so. git fetch --depth=1 origin <prev_commit> fetches only that one commit, which is all a two-dot diff needs (checked on a depth-1 clone).
  • For a rename, only the new path is used. The old path is not reported as deleted.

Scope note

Only --agent api uses prev_commit; the CLI agents ignore it, so they are not affected by 1 to 3. Item 4 affects every agent.

Happy to pair on item 1 if useful, since it touches the SAST lifecycle code.

@Melwinjoel20
Melwinjoel20 force-pushed the feature/memory-bank-commit-tracking branch from 0294863 to 7b93f1c Compare October 16, 2026 21:11
@Melwinjoel20
Melwinjoel20 force-pushed the feature/memory-bank-commit-tracking branch from c15d945 to 0c4d220 Compare October 16, 2026 21:38
@Melwinjoel20

Copy link
Copy Markdown
Contributor Author

@Varunpoojari
Thanks for the detailed review and the repro cases, they made this much easier to fix. All six points are addressed in the latest push, rebased on current main (including #29 and #30).

An incremental run now records the paths it covered (scan_scope on the run, files sent plus files deleted), and close_stale_findings only closes findings inside that set. SCA findings have no file, so an incremental run never closes them.
If nothing scannable changed (your logo.png case), it falls back to a full scan.
The diff is taken against the working tree plus untracked files, so uncommitted edits and new files are included.
The prompt moved out of execute_runs() into cli.py. It is only asked on a terminal and not with --json, it skips only the repeated skills, and --rescan bypasses it. execute_runs() never reads stdin.
tests/test_memory_bank.py covers each of your cases end to end with a real git repo, the api adapter and a fake model that only reports files it was shown. Those tests fail on the previous version of the branch. Happy to compare against your script too.
Fetch is now --depth=1 for the previous commit only, renames report the old path as deleted, the README line and test commit are gone, and there are no new ruff issues compared with main.

Also tested on a private Django project scanned as a GitHub URL target: the full scan used 39,544 input tokens, and the incremental scan after a one-file commit used 6,290 (84% less). The previous commit was fetched into the fresh depth-1 clone, and findings from the first run in unchanged files stayed open.

The description is updated, with a few open questions at the end: the portal ingest path would need to read scan_scope, and whether incremental mode should be limited to file-based skills like SAST and secret scanning. Happy to pair on any of it if useful.

@Melwinjoel20

Copy link
Copy Markdown
Contributor Author

Closing this as agreed with Satya: the change now lives in the private repo (rakfortltd/secfoo PR #12) with all the review points addressed, plus the portal side of the scoped findings lifecycle. It will reach this repo through the next public snapshot. Thanks for the detailed review, it shaped the final version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants