Skip to content

feat(state): add scan-state package for npm/python delta uploads#131

Open
swarit-stepsecurity wants to merge 4 commits into
step-security:mainfrom
swarit-stepsecurity:swarit/feat/wt/scan-state-package
Open

feat(state): add scan-state package for npm/python delta uploads#131
swarit-stepsecurity wants to merge 4 commits into
step-security:mainfrom
swarit-stepsecurity:swarit/feat/wt/scan-state-package

Conversation

@swarit-stepsecurity

Copy link
Copy Markdown
Member

What does this PR do?

Type of change

  • Bug fix
  • Enhancement
  • Documentation

Testing

  • Tested on macOS (version: ___)
  • Binary runs without errors: ./stepsecurity-dev-machine-guard --verbose
  • JSON output is valid: ./stepsecurity-dev-machine-guard --json | python3 -m json.tool
  • No secrets or credentials included
  • Lint passes: make lint
  • Tests pass: make test

Related Issues

Comment thread internal/state/state.go Fixed
Comment thread internal/state/state.go Fixed
Signed-off-by: Swarit Pandey <swarit@stepsecurity.io>
Signed-off-by: Swarit Pandey <swarit@stepsecurity.io>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 State JSON 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 thread internal/state/state.go
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 thread internal/state/state.go
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 thread internal/state/state.go
Comment on lines +164 to +168
if s.LastFullSyncAt.IsZero() {
return true
}
return now.Sub(s.LastFullSyncAt) > horizon
}
Comment thread internal/state/state.go
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 thread internal/state/state.go
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
}
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.

3 participants