Repository navigation
v0 Wave K: findings + policy carry-over verification #44
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| PRRT_kwDOTyoI9s6YxjZz Fixed — `verifyPolicyAgainstGraph` now normalizes `checks/` prefixes, so bare check ids like `typecheck` match steps like `checks/typecheck` and align with `evaluatePolicy`. | ||
| PRRT_kwDOTyoI9s6Yxmn2 Fixed — the rule iteration now guards `checkIds` against `null`, `undefined`, and non-array values, treating them as no check filter. | ||
| PRRT_kwDOTyoI9s6Yxn9P Fixed — bare check ids are canonical; `verifyPolicyAgainstGraph` and `evaluatePolicy` normalize `checks/` prefixes and `evaluatePolicy` matches rule-qualified finding ids (`<checkId>:<ruleId>`). `extractFindings` also strips `checks/` before normalization. | ||
| PRRT_kwDOTyoI9s6Yxn9R Fixed — `verifyPolicyAgainstGraph` now validates `policy.failOn` and `graph.project.pipelines` and returns structural errors in `PolicyVerification.errors` without throwing. | ||
| PRRT_kwDOTyoI9s6Y3M-8 Fixed — `specs/15-findings/spec.md` now declares `compareBaseline(findings, baseline)` matching the implementation and tests. | ||
| PRRT_kwDOTyoI9s6Y3M_A Fixed — `specs/16-policy/spec.md` now defines that `failOn[].checkIds` refer to check steps (IDs starting with `checks/`), with normalization for `checks/` prefix and rule-qualified findings; implementation and tests are aligned. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { verifyPolicyAgainstGraph } from "../verify.js"; | ||
| import { DEFAULT_POLICY } from "../policy.js"; | ||
| import type { Policy } from "../types.js"; | ||
| import type { DefinitionGraph } from "@sverka/core"; | ||
|
|
||
| function makeGraph(stepIds: string[]): DefinitionGraph { | ||
| return { | ||
| project: { | ||
| id: "test", | ||
| pipelines: [{ | ||
| id: "ci", | ||
| inputs: {}, | ||
| entries: [], | ||
| steps: stepIds.map((id) => ({ | ||
| id, | ||
| runtime: { mode: "host" as const }, | ||
| operations: [{ kind: "shell" as const, command: "echo hi" }], | ||
| inputs: [], | ||
| outputs: [], | ||
| dependencies: [], | ||
| })), | ||
| outputs: [], | ||
| }], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function makePolicy(checkIds?: string[]): Policy { | ||
| return { | ||
| name: "test", | ||
| default: "pass", | ||
| failOn: checkIds | ||
| ? [{ severity: "high", onlyNew: false, checkIds }] | ||
| : [{ severity: "high", onlyNew: false }], | ||
| }; | ||
| } | ||
|
|
||
| describe("verifyPolicyAgainstGraph", () => { | ||
| it("valid policy (all checkIds match) → valid=true", () => { | ||
| const graph = makeGraph(["checks/typecheck", "checks/lint"]); | ||
| const policy = makePolicy(["checks/typecheck", "checks/lint"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(true); | ||
| expect(result.unknownCheckIds).toEqual([]); | ||
| }); | ||
|
|
||
| it("unknown checkId → valid=false, listed", () => { | ||
| const graph = makeGraph(["checks/typecheck"]); | ||
| const policy = makePolicy(["checks/typecheck", "checks/unknown"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.unknownCheckIds).toEqual(["checks/unknown"]); | ||
| }); | ||
|
|
||
| it("policy with no checkIds → valid=true", () => { | ||
| const graph = makeGraph(["checks/typecheck"]); | ||
| const policy = makePolicy(); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(true); | ||
| expect(result.unknownCheckIds).toEqual([]); | ||
| }); | ||
|
|
||
| it("multiple unknown checkIds → all listed", () => { | ||
| const graph = makeGraph(["checks/typecheck"]); | ||
| const policy = makePolicy(["checks/typecheck", "checks/foo", "checks/bar"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.unknownCheckIds).toHaveLength(2); | ||
| expect(result.unknownCheckIds).toContain("checks/foo"); | ||
| expect(result.unknownCheckIds).toContain("checks/bar"); | ||
| }); | ||
|
|
||
| it("DEFAULT_POLICY (no checkIds) → valid=true", () => { | ||
| const graph = makeGraph([]); | ||
| const result = verifyPolicyAgainstGraph(DEFAULT_POLICY, graph); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it("empty graph → unknown checkIds reported", () => { | ||
| const graph = makeGraph([]); | ||
| const policy = makePolicy(["checks/typecheck"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.unknownCheckIds).toEqual(["checks/typecheck"]); | ||
| }); | ||
|
|
||
| it("deduplicates checkIds across rules", () => { | ||
| const graph = makeGraph(["checks/typecheck"]); | ||
| const policy: Policy = { | ||
| name: "test", | ||
| default: "pass", | ||
| failOn: [ | ||
| { severity: "high", onlyNew: false, checkIds: ["checks/unknown"] }, | ||
| { severity: "medium", onlyNew: false, checkIds: ["checks/unknown"] }, | ||
| ], | ||
| }; | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(false); | ||
| // "checks/unknown" appears in both rules but should be listed once. | ||
| expect(result.unknownCheckIds).toEqual(["checks/unknown"]); | ||
| }); | ||
|
|
||
| it("accepts bare checkIds that match checks/<id> steps", () => { | ||
| const graph = makeGraph(["checks/typecheck", "checks/lint"]); | ||
| const policy = makePolicy(["typecheck", "lint"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(true); | ||
| expect(result.unknownCheckIds).toEqual([]); | ||
| }); | ||
|
|
||
| it("ignores non-check steps when validating checkIds", () => { | ||
| const graph = makeGraph(["build/compile"]); | ||
| const policy = makePolicy(["compile"]); | ||
| const result = verifyPolicyAgainstGraph(policy, graph); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.unknownCheckIds).toEqual(["compile"]); | ||
| }); | ||
|
|
||
| it("returns errors for malformed policy or graph without throwing", () => { | ||
| const badPolicy = { name: "x" } as unknown as Policy; | ||
| const badGraph = { project: {} } as unknown as DefinitionGraph; | ||
| const result = verifyPolicyAgainstGraph(badPolicy, badGraph); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.unknownCheckIds).toEqual([]); | ||
| expect(result.errors).toBeDefined(); | ||
| expect(result.errors!.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // @sverka/policy — public API | ||
| // @sverka/policy — public API. Spec 16. | ||
| export type { Verdict, Policy, FailOnRule, TriggeredFinding, | ||
| RuleResult, PolicyResult, PolicyConfig } from "./types.js"; | ||
| export { DEFAULT_POLICY, createPolicy } from "./policy.js"; | ||
| export { evaluatePolicy } from "./evaluator.js"; | ||
| export { verifyPolicyAgainstGraph } from "./verify.js"; | ||
| export type { PolicyVerification } from "./verify.js"; | ||
| export { PolicyError, type PolicyErrorCode } from "./errors.js"; |
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,93 @@ | ||
| // Policy verification against a Definition Graph. | ||
| // Spec 16 — §26, §27. Verifies that policy checkIds reference check | ||
| // steps that exist in the graph. | ||
|
|
||
| import type { DefinitionGraph } from "@sverka/core"; | ||
| import type { Policy } from "./types.js"; | ||
|
|
||
| export interface PolicyVerification { | ||
| readonly valid: boolean; | ||
| readonly unknownCheckIds: readonly string[]; | ||
| readonly errors?: readonly string[]; | ||
| } | ||
|
|
||
| /** Strip the `checks/` prefix so `checks/typecheck` and `typecheck` compare equal. */ | ||
| function normalizeCheckId(id: string): string { | ||
| return id.startsWith("checks/") ? id.slice(7) : id; | ||
| } | ||
|
|
||
| /** | ||
| * Verify that all checkIds referenced in a policy's failOn rules exist | ||
| * as check step IDs in the Definition Graph. A step is treated as a check | ||
| * when its ID starts with `checks/`. Returns a PolicyVerification | ||
| * with valid=false and the list of unknown check IDs if any are found. | ||
| * | ||
| * Does not throw for structural errors — returns them in `errors`. | ||
| */ | ||
| export function verifyPolicyAgainstGraph( | ||
| policy: Policy, | ||
| graph: DefinitionGraph, | ||
| ): PolicyVerification { | ||
| const errors = collectValidationErrors(policy, graph); | ||
| if (errors.length > 0) { | ||
| return { valid: false, unknownCheckIds: [], errors }; | ||
| } | ||
|
|
||
| const knownCheckIds = collectKnownCheckIds(graph); | ||
| const referencedCheckIds = collectReferencedCheckIds(policy); | ||
| const unknownCheckIds = findUnknownCheckIds(referencedCheckIds, knownCheckIds); | ||
|
|
||
| return { | ||
| valid: unknownCheckIds.length === 0, | ||
| unknownCheckIds, | ||
| }; | ||
| } | ||
|
|
||
| function collectValidationErrors(policy: Policy, graph: DefinitionGraph): string[] { | ||
| const errors: string[] = []; | ||
| if (!policy || typeof policy !== "object") { | ||
| errors.push("invalid policy: expected object"); | ||
| } else if (!Array.isArray(policy.failOn)) { | ||
| errors.push("invalid policy: failOn must be an array"); | ||
| } | ||
| if (!graph || typeof graph !== "object" || !Array.isArray(graph.project?.pipelines)) { | ||
| errors.push("invalid graph: project.pipelines must be an array"); | ||
| } | ||
| return errors; | ||
| } | ||
|
|
||
| function collectKnownCheckIds(graph: DefinitionGraph): Set<string> { | ||
| const knownCheckIds = new Set<string>(); | ||
| for (const pipeline of graph.project.pipelines) { | ||
| if (!Array.isArray(pipeline.steps)) continue; | ||
| for (const step of pipeline.steps) { | ||
| if (typeof step.id === "string" && step.id.startsWith("checks/")) { | ||
| knownCheckIds.add(normalizeCheckId(step.id)); | ||
| } | ||
| } | ||
| } | ||
| return knownCheckIds; | ||
| } | ||
|
|
||
| function collectReferencedCheckIds(policy: Policy): Set<string> { | ||
| const referencedCheckIds = new Set<string>(); | ||
| for (const rule of policy.failOn) { | ||
| const raw = (rule as { checkIds?: unknown }).checkIds; | ||
| if (raw === undefined || raw === null) continue; | ||
| const ids: string[] = Array.isArray(raw) ? (raw as string[]) : [String(raw)]; | ||
|
Check warning on line 77 in packages/policy/src/verify.ts
|
||
| for (const id of ids) { | ||
| if (typeof id === "string") referencedCheckIds.add(id); | ||
| } | ||
| } | ||
| return referencedCheckIds; | ||
| } | ||
|
|
||
| function findUnknownCheckIds(referenced: Set<string>, known: Set<string>): string[] { | ||
| const unknown: string[] = []; | ||
| for (const id of referenced) { | ||
| if (!known.has(normalizeCheckId(id))) { | ||
| unknown.push(id); | ||
| } | ||
| } | ||
| return unknown; | ||
| } | ||
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.