-
Notifications
You must be signed in to change notification settings - Fork 94
feat(deployment): add behavioural workload signals with replay tooling #3983
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
5 commits
Select commit
Hold shift + click to select a range
a2d4194
feat(deployment): add behavioural workload signals with replay tooling
baktun14 08a83eb
fix(deployment): score behavioural signals only on evidence a shell f…
baktun14 898caaf
fix(deployment): read connections under the state the collector reported
baktun14 b41124b
fix(deployment): keep a blank relay endpoint variable from failing co…
baktun14 e6b1c35
fix(deployment): apply defaults to blank settings and compare sizes u…
baktun14 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
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
101 changes: 101 additions & 0 deletions
101
apps/api/src/workload-abuse/lib/behavioural-signals/accel-without-artifacts.spec.ts
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,101 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import type { ProbeEvidenceAccelerator, ProbeEvidenceArtifact } from "@src/workload-abuse/model-schemas"; | ||
| import { evaluateAccelWithoutArtifacts } from "./accel-without-artifacts"; | ||
| import type { BehaviouralSignalParams, ProbeEvidenceSnapshot } from "./types"; | ||
|
|
||
| describe("evaluateAccelWithoutArtifacts", () => { | ||
| it("fires when a process holds the accelerator and nothing large sits on disk", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifacts: [{ path: "/opt/run", sizeBytes: 4_194_304 }] }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toEqual({ | ||
| signal: "accel_without_artifacts", | ||
| detail: { heaviestVramMb: 18_000, largestArtifactMb: 4 } | ||
| }); | ||
| }); | ||
|
|
||
| it("reports the heaviest process when several share the accelerator", () => { | ||
| const { snapshot, params } = setup({ | ||
| accelerator: [ | ||
| { | ||
| name: "accelerator-0", | ||
| utilPct: 99, | ||
| memUsedMb: 20_000, | ||
| memTotalMb: 24_576, | ||
| processes: [ | ||
| { pid: 10, name: "worker", vramMb: 2_000 }, | ||
| { pid: 11, name: "worker", vramMb: 16_000 } | ||
| ] | ||
| } | ||
| ], | ||
| artifacts: [] | ||
| }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)?.detail).toEqual({ heaviestVramMb: 16_000, largestArtifactMb: 0 }); | ||
| }); | ||
|
|
||
| it("stays silent when the workload carries large files", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifacts: [{ path: "/models/weights", sizeBytes: 8_589_934_592 }] }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toBeNull(); | ||
| }); | ||
|
|
||
| it("stays silent when no accelerator was reported", () => { | ||
| const { snapshot, params } = setup({ accelerator: null, artifacts: [] }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toBeNull(); | ||
| }); | ||
|
|
||
| it("stays silent when accelerator memory stays under the threshold", () => { | ||
| const { snapshot, params } = setup({ vramMb: 200, artifacts: [], accelMinVramMb: 1_024 }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toBeNull(); | ||
| }); | ||
|
|
||
| it("fires when the largest artifact sits just under the threshold", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifacts: [{ path: "/opt/run", sizeBytes: 268_016_025 }], artifactMinMb: 256 }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)?.detail).toEqual({ heaviestVramMb: 18_000, largestArtifactMb: 255 }); | ||
| }); | ||
|
|
||
| it("stays silent when the largest artifact reaches the threshold exactly", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifacts: [{ path: "/opt/run", sizeBytes: 268_435_456 }], artifactMinMb: 256 }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toBeNull(); | ||
| }); | ||
|
|
||
| it("stays silent when the probe collected no disk section", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifacts: null }); | ||
|
|
||
| expect(evaluateAccelWithoutArtifacts(snapshot, params)).toBeNull(); | ||
| }); | ||
|
|
||
| function setup(input: { | ||
| vramMb?: number; | ||
| accelerator?: ProbeEvidenceAccelerator[] | null; | ||
| artifacts?: ProbeEvidenceArtifact[] | null; | ||
| accelMinVramMb?: number; | ||
| artifactMinMb?: number; | ||
| }) { | ||
| const accelerator = | ||
| input.accelerator !== undefined | ||
| ? input.accelerator | ||
| : [ | ||
| { | ||
| name: "accelerator-0", | ||
| utilPct: 98, | ||
| memUsedMb: 20_480, | ||
| memTotalMb: 24_576, | ||
| processes: [{ pid: 1234, name: "worker", vramMb: input.vramMb ?? 18_000 }] | ||
| } | ||
| ]; | ||
| const snapshot: ProbeEvidenceSnapshot = { shellStatus: "completed", accelerator, artifacts: input.artifacts ?? null, netShape: null }; | ||
| const params: BehaviouralSignalParams = { | ||
| accelMinVramMb: input.accelMinVramMb ?? 1_024, | ||
| artifactMinMb: input.artifactMinMb ?? 256, | ||
| relayEndpoints: [] | ||
| }; | ||
|
|
||
| return { snapshot, params }; | ||
| } | ||
| }); |
27 changes: 27 additions & 0 deletions
27
apps/api/src/workload-abuse/lib/behavioural-signals/accel-without-artifacts.ts
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,27 @@ | ||
| import { BEHAVIOURAL_SIGNALS, type BehaviouralFinding, type BehaviouralSignalParams, type ProbeEvidenceSnapshot } from "./types"; | ||
|
|
||
| const BYTES_PER_MB = 1024 * 1024; | ||
|
|
||
| /** A snapshot without a disk section reports nothing rather than an empty disk, so it cannot support this signal. */ | ||
| export function evaluateAccelWithoutArtifacts(snapshot: ProbeEvidenceSnapshot, params: BehaviouralSignalParams): BehaviouralFinding | null { | ||
| const heaviestVramMb = findHeaviestVramMb(snapshot); | ||
|
|
||
| if (heaviestVramMb < params.accelMinVramMb) return null; | ||
| if (!snapshot.artifacts) return null; | ||
|
|
||
| const largestArtifactMb = findLargestArtifactMb(snapshot); | ||
|
|
||
| if (largestArtifactMb >= params.artifactMinMb) return null; | ||
|
|
||
| return { signal: BEHAVIOURAL_SIGNALS.accelWithoutArtifacts, detail: { heaviestVramMb, largestArtifactMb: Math.floor(largestArtifactMb) } }; | ||
| } | ||
|
|
||
| function findHeaviestVramMb(snapshot: ProbeEvidenceSnapshot): number { | ||
| const vramPerProcess = (snapshot.accelerator ?? []).flatMap(accelerator => accelerator.processes.map(process => process.vramMb)); | ||
| return vramPerProcess.reduce((heaviest, vramMb) => Math.max(heaviest, vramMb), 0); | ||
| } | ||
|
|
||
| function findLargestArtifactMb(snapshot: ProbeEvidenceSnapshot): number { | ||
| const sizes = (snapshot.artifacts ?? []).map(artifact => artifact.sizeBytes / BYTES_PER_MB); | ||
| return sizes.reduce((largest, sizeMb) => Math.max(largest, sizeMb), 0); | ||
| } |
65 changes: 65 additions & 0 deletions
65
apps/api/src/workload-abuse/lib/behavioural-signals/evaluate-behavioural-signals.spec.ts
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,65 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { evaluateBehaviouralSignals, isBehaviouralCandidate } from "./evaluate-behavioural-signals"; | ||
| import type { BehaviouralSignalParams, ProbeEvidenceSnapshot } from "./types"; | ||
|
|
||
| describe("evaluateBehaviouralSignals", () => { | ||
| it("returns both findings when the snapshot matches both signals", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifactBytes: 1_024, connections: [] }); | ||
|
|
||
| expect(evaluateBehaviouralSignals(snapshot, params).map(finding => finding.signal)).toEqual(["accel_without_artifacts", "network_isolated"]); | ||
| }); | ||
|
|
||
| it("returns one finding when the workload carries weights but talks to nobody", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifactBytes: 8_589_934_592, connections: [] }); | ||
|
|
||
| expect(evaluateBehaviouralSignals(snapshot, params).map(finding => finding.signal)).toEqual(["network_isolated"]); | ||
| }); | ||
|
|
||
| it("returns nothing when the snapshot matches neither signal", () => { | ||
| const { snapshot, params } = setup({ | ||
| vramMb: 18_000, | ||
| artifactBytes: 8_589_934_592, | ||
| connections: [{ localPort: 8_080, remoteIp: "203.0.113.9", remotePort: 51_000, count: 2, state: "established" }] | ||
| }); | ||
|
|
||
| expect(evaluateBehaviouralSignals(snapshot, params)).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns nothing when the shell that collected the snapshot was cut short", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifactBytes: 1_024, connections: [], shellStatus: "output_capped" }); | ||
|
|
||
| expect(evaluateBehaviouralSignals(snapshot, params)).toEqual([]); | ||
| }); | ||
|
|
||
| it("treats the two signals together as a candidate", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifactBytes: 1_024, connections: [] }); | ||
|
|
||
| expect(isBehaviouralCandidate(evaluateBehaviouralSignals(snapshot, params))).toBe(true); | ||
| }); | ||
|
|
||
| it("treats a single signal as no candidate", () => { | ||
| const { snapshot, params } = setup({ vramMb: 18_000, artifactBytes: 8_589_934_592, connections: [] }); | ||
|
|
||
| expect(isBehaviouralCandidate(evaluateBehaviouralSignals(snapshot, params))).toBe(false); | ||
| }); | ||
|
|
||
| function setup(input: { | ||
| vramMb: number; | ||
| artifactBytes: number; | ||
| connections: NonNullable<ProbeEvidenceSnapshot["netShape"]>["connections"]; | ||
| shellStatus?: string; | ||
| }) { | ||
| const snapshot: ProbeEvidenceSnapshot = { | ||
| shellStatus: input.shellStatus ?? "completed", | ||
| accelerator: [ | ||
| { name: "accelerator-0", utilPct: 97, memUsedMb: 20_480, memTotalMb: 24_576, processes: [{ pid: 1234, name: "worker", vramMb: input.vramMb }] } | ||
| ], | ||
| artifacts: [{ path: "/opt/payload", sizeBytes: input.artifactBytes }], | ||
| netShape: { listenPorts: [8_080], connections: input.connections } | ||
| }; | ||
| const params: BehaviouralSignalParams = { accelMinVramMb: 1_024, artifactMinMb: 256, relayEndpoints: [] }; | ||
|
|
||
| return { snapshot, params }; | ||
| } | ||
| }); |
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.