Repository navigation
fix(ENG-2966): stackone-defender - fall back to Tier 1 and warn when the classifier can't load #52
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce37187
fix(ENG-2966): stackone-defender - fall back to Tier 1 and warn when …
hiskudin e80c7ed
fix(ENG-2966): per-session degraded notice, recover from transient lo…
hiskudin 7f42ca4
fix(ENG-2966): log the classifier's load error, scan empty keys, docu…
hiskudin a516c00
fix(ENG-2966): keep warmup warnings on a throw, prune old degraded ma…
hiskudin 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
35 changes: 35 additions & 0 deletions
35
plugins/security/stackone-defender/scripts/build-defense.mjs
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,35 @@ | ||
| /** | ||
| * Builds the daemon's PromptDefense. | ||
| * | ||
| * When the Tier 2 model or ONNX runtime fails to load, @stackone/defender allows every | ||
| * result and only warns on the console, which the daemon's ignored stdio swallows. With | ||
| * Tier 1 off in the shipped config that would scan nothing, so fall back to Tier 1 | ||
| * patterns and report the degraded state to the caller. | ||
| */ | ||
| export async function buildDefense(PromptDefense, options, log) { | ||
| const defense = new PromptDefense(options); | ||
| // The library reports why a load failed only through console.warn, which nobody sees. | ||
| const warnings = []; | ||
| const consoleWarn = console.warn; | ||
| console.warn = (...args) => | ||
| warnings.push(args.map((a) => (a instanceof Error ? a.message : String(a))).join(" ")); | ||
| try { | ||
| await defense.warmupTier2(); | ||
| } finally { | ||
| console.warn = consoleWarn; | ||
| if (warnings.length > 0) log("warmup warnings", { warnings }); | ||
| } | ||
| if (defense.isTier2Ready()) return { defense, tier2Ready: true }; | ||
|
|
||
| log("Tier 2 unavailable, falling back to Tier 1 patterns"); | ||
| // Tier 1 only reads fields with "risky" names (description, body, text...), and the hook | ||
| // wraps plain tool output as { output }, so widen it to every field or it scans nothing. | ||
| const fallback = new PromptDefense({ | ||
| ...options, | ||
| enableTier1: true, | ||
| enableTier2: false, | ||
| // /^/, not /./, so an empty key ("") is scanned too. | ||
| config: { ...options.config, riskyFields: { fieldNames: [], fieldPatterns: [/^/] } }, | ||
| }); | ||
| return { defense: fallback, tier2Ready: false }; | ||
| } | ||
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
37 changes: 37 additions & 0 deletions
37
plugins/security/stackone-defender/scripts/degraded-notice.mjs
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,37 @@ | ||
| import { closeSync, openSync, readdirSync, statSync, unlinkSync } from "fs"; | ||
| import { join } from "path"; | ||
|
|
||
| /** | ||
| * Whether this session should be told the classifier is unavailable. One marker file per | ||
| * session, created with `wx`, so concurrent sessions and parallel hooks each claim it once. | ||
| * Without a session id there is nothing to key on, so no notice. | ||
| */ | ||
| const PREFIX = "defender-degraded-"; | ||
| const MARKER_TTL_MS = 7 * 24 * 60 * 60 * 1000; | ||
|
|
||
| export function claimDegradedNotice(dir, sessionId) { | ||
| if (!sessionId) return false; | ||
| const path = join(dir, `${PREFIX}${String(sessionId).replace(/[^\w-]/g, "_")}`); | ||
| try { | ||
| closeSync(openSync(path, "wx")); | ||
| pruneOldMarkers(dir); | ||
| return true; | ||
| } catch (err) { | ||
| // Unrecordable for any reason other than an existing claim: noisy, never silent. | ||
| return err.code !== "EEXIST"; | ||
| } | ||
| } | ||
|
|
||
| // A machine that stays degraded gains a marker per conversation; drop week-old ones. | ||
| function pruneOldMarkers(dir) { | ||
| try { | ||
| const cutoff = Date.now() - MARKER_TTL_MS; | ||
| for (const name of readdirSync(dir)) { | ||
| if (!name.startsWith(PREFIX)) continue; | ||
| const file = join(dir, name); | ||
| if (statSync(file).mtimeMs < cutoff) unlinkSync(file); | ||
| } | ||
| } catch { | ||
| // Pruning is housekeeping; a failure only leaves old empty files behind. | ||
| } | ||
| } |
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
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.