-
Notifications
You must be signed in to change notification settings - Fork 17
feat(aiagents): poll backend for hook enable/disable state #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ashishkurmi
merged 2 commits into
step-security:main
from
swarit-stepsecurity:swarit/feat/wt/ai-hooks-integration
May 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package state | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| // CacheFilename is the basename of the cache file. Lives under | ||
| // ~/.stepsecurity/ alongside config.json and ai-agent-hook-errors.jsonl. | ||
| const CacheFilename = "hooks-state.json" | ||
|
|
||
| const ( | ||
| cacheFileMode os.FileMode = 0o600 | ||
| cacheParentDirMode os.FileMode = 0o700 | ||
| ) | ||
|
|
||
| // cachePathOverride lets tests redirect reads/writes to a tempdir. | ||
| // Production leaves it empty. Mutating from outside this package is | ||
| // a test-only concern; same pattern as cli.errorLogPathOverride. | ||
| var cachePathOverride string | ||
|
|
||
| // SetCachePathForTest redirects CachePath() to the given absolute path | ||
| // and returns a restore function. Test-only; production code never | ||
| // calls this. Living on the package surface (rather than as a | ||
| // build-tagged file) keeps cross-package tests in hook/* and main_test | ||
| // able to drive the override without an internal-import trick. | ||
| func SetCachePathForTest(p string) (restore func()) { | ||
| prev := cachePathOverride | ||
| cachePathOverride = p | ||
| return func() { cachePathOverride = prev } | ||
| } | ||
|
|
||
| // CachePath returns the absolute cache path, honoring the test | ||
| // override when set. | ||
| func CachePath() string { | ||
| if cachePathOverride != "" { | ||
| return cachePathOverride | ||
| } | ||
| home, err := os.UserHomeDir() | ||
| if err != nil || home == "" { | ||
| return "" | ||
| } | ||
| return filepath.Join(home, ".stepsecurity", CacheFilename) | ||
| } | ||
|
|
||
| // Read returns (state, true) on a successful parse. Any I/O or parse | ||
| // error returns (Default(), false) — Read never surfaces an error | ||
| // because the hot path must remain fail-open. | ||
| func Read() (State, bool) { | ||
| path := CachePath() | ||
| if path == "" { | ||
| return Default(), false | ||
| } | ||
| // #nosec G304 -- path is CachePath(): either a test override set by | ||
| // SetCachePathForTest, or os.UserHomeDir() joined with the package | ||
| // constant CacheFilename. Never derived from external input. | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return Default(), false | ||
| } | ||
| var s State | ||
| if err := json.Unmarshal(b, &s); err != nil { | ||
| return Default(), false | ||
| } | ||
| if s.SchemaVersion == 0 { | ||
| // Forward-compat tolerance: missing schema_version reads as the | ||
| // current version. A future breaking change would gate on a | ||
| // specific value here. | ||
| s.SchemaVersion = SchemaVersion | ||
| } | ||
| return s, true | ||
| } | ||
|
|
||
| // Write atomically replaces the cache file. No backups are kept — the | ||
| // cache is rewritten on every reconcile tick, and orphaned backups | ||
| // would accumulate trash. Parent dir is created with 0o700. | ||
| func Write(s State) error { | ||
| path := CachePath() | ||
| if path == "" { | ||
| return errNoHomeDir | ||
| } | ||
| data, err := json.MarshalIndent(s, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| data = append(data, '\n') | ||
|
|
||
| parent := filepath.Dir(path) | ||
| if err := os.MkdirAll(parent, cacheParentDirMode); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tmp, err := os.CreateTemp(parent, "."+CacheFilename+".tmp-*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| tmpPath := tmp.Name() | ||
| defer func() { | ||
| if _, statErr := os.Stat(tmpPath); statErr == nil { | ||
| _ = os.Remove(tmpPath) | ||
| } | ||
| }() | ||
|
|
||
| if _, err := tmp.Write(data); err != nil { | ||
| _ = tmp.Close() | ||
| return err | ||
| } | ||
| if err := tmp.Sync(); err != nil { | ||
| _ = tmp.Close() | ||
| return err | ||
| } | ||
| if err := tmp.Close(); err != nil { | ||
| return err | ||
| } | ||
| if err := os.Chmod(tmpPath, cacheFileMode); err != nil { | ||
| return err | ||
| } | ||
| return os.Rename(tmpPath, path) | ||
| } | ||
|
|
||
| type cacheError string | ||
|
|
||
| func (e cacheError) Error() string { return string(e) } | ||
|
|
||
| const errNoHomeDir = cacheError("state: cannot resolve home directory") | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.