feat(state): add scan-state package for npm/python delta uploads#131
Open
swarit-stepsecurity wants to merge 4 commits into
Open
feat(state): add scan-state package for npm/python delta uploads#131swarit-stepsecurity wants to merge 4 commits into
swarit-stepsecurity wants to merge 4 commits into
Conversation
Signed-off-by: Swarit Pandey <swarit@stepsecurity.io>
Signed-off-by: Swarit Pandey <swarit@stepsecurity.io>
There was a problem hiding this comment.
Pull request overview
This PR introduces a new internal/state package that persists device-side scan state (projects, globals, and pending removals) to enable delta uploads for npm and Python across runs.
Changes:
- Add
StateJSON schema with load/save, reconciliation, partitioning, and commit-after-upload semantics. - Add canonical JSON hashing (
sha256:) to make output hashing stable across JSON key reordering. - Add unit tests covering load/save behavior, partitioning rules, full-sync horizon logic, and removal pending/ack lifecycle.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| internal/state/state.go | Implements the on-disk scan state model and core reconciliation/commit logic. |
| internal/state/state_test.go | Adds tests for load/save, reconcile/partition behavior, full-sync rules, and removals lifecycle. |
| internal/state/hash.go | Adds canonical JSON hashing to stabilize hashes across key reordering. |
| internal/state/hash_test.go | Adds tests for hash stability, format, and malformed JSON fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+86
to
+88
| // Load reads scan-state.json. Missing file, parse error, or schema mismatch | ||
| // returns a fresh empty state — the next run becomes a full sync naturally. | ||
| // The error is non-nil only to surface why fallback happened, for logging. |
Comment on lines
+124
to
+126
| func (s *State) Save(path string) error { | ||
| dir := filepath.Dir(path) | ||
| if err := os.MkdirAll(dir, 0o750); err != nil { |
Comment on lines
+164
to
+168
| if s.LastFullSyncAt.IsZero() { | ||
| return true | ||
| } | ||
| return now.Sub(s.LastFullSyncAt) > horizon | ||
| } |
Comment on lines
+421
to
+429
| func (s *State) projectMap(ecosystem string) map[string]ProjectEntry { | ||
| switch ecosystem { | ||
| case EcosystemNPM: | ||
| return s.NPMProjects | ||
| case EcosystemPython: | ||
| return s.PythonProjects | ||
| } | ||
| return nil | ||
| } |
Comment on lines
+431
to
+439
| func (s *State) globalMap(ecosystem string) map[string]GlobalEntry { | ||
| switch ecosystem { | ||
| case EcosystemNPM: | ||
| return s.NPMGlobal | ||
| case EcosystemPython: | ||
| return s.PythonGlobal | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Type of change
Testing
./stepsecurity-dev-machine-guard --verbose./stepsecurity-dev-machine-guard --json | python3 -m json.toolmake lintmake testRelated Issues