Repository navigation
Feature/memory bank commit tracking - #22
Melwinjoel20 wants to merge 6 commits into
Conversation
e64bdb1 to
dea65a7
Compare
Varunpoojari
left a comment
There was a problem hiding this comment.
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.mdends with a stray# updatedline, and the branch has atest: trigger incremental scancommit. Both look like leftovers from testing.ruff checkon the touched files reports 13 problems against 10 onmain. The 3 new ones are inapi.py: twosubprocess.runcalls withoutcheck=and one bareexcept Exception.git fetch --unshallowruns 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.
0294863 to
7b93f1c
Compare
c15d945 to
0c4d220
Compare
|
@Varunpoojari 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. 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. |
|
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. |
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):
What
repository.py:get_last_commit()returns the most recent successfultarget_commitfor a project and skill;find_project_id()looks up a project without creating one;close_stale_findings()/close_stale_sast_findings()takeonly_filesand only close findings inside it (SCA has no file column, so an incremental run never closes SCA findings); new nullablescan_scopecolumn onrunsvia the existing migration list.base.py:prev_commitkwarg onAgentAdapter.run()(CLI adapters accept and ignore it);AgentResult.scan_scope(None means a full scan).api.py:_git_changed_files()diffsprev_commitagainst 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 withgit 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 thescan_scope.runner.py: passesprev_committo the adapter, savesscan_scopeon the run and passes it to the SAST, secret scanning and SCA lifecycles. No prompt inexecute_runs()(it also runs under the dashboard and CI); newalready_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--rescanbypasses the question.Testing
pytest: all passing except the 4 Mermaid dashboard tests that also fail on mainruff checkon the touched files: no new issues compared with maintests/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 inexecute_runs(), the CLI question and--rescan, and the scoped close in storage. These tests fail on the previous version of this branch.Follow-ups (not in this PR)
scan_scope(private repo).not_configuredstatus isn't allowed by therunsCHECK constraint, sosecfoo run -a apiwith no key crashes.Jira: SECFOO-13