From 2add595546add7ef0fe344d67d0a533a99bc9ebd Mon Sep 17 00:00:00 2001 From: Alex Gorischek Date: Fri, 20 Nov 2026 10:40:29 -0800 Subject: [PATCH 1/2] Runtime policy: the Changes scope keeps what a changed row's chips name In the Changes scope a modified policy whose scope didn't change read "no workloads": its unchanged applies-to subject wasn't context, so the narrowed model had no chip for it. The context now also follows a policy's or rule's applies-to, matches and excludes relationships to their subjects (drawn only as chips), and a workload's covering policies (covers), so a changed workload doesn't read as covered by no policy. The context moves to scope.ts, where it is tested through the same narrowing the shell does. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../renderers/runtime-policy/src/main.tsx | 12 ++--- .../renderers/runtime-policy/src/scope.ts | 19 ++++++++ .../test/runtime-policy.test.mjs | 47 ++++++++++++++++++- 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 packages/renderers/runtime-policy/src/scope.ts diff --git a/packages/renderers/runtime-policy/src/main.tsx b/packages/renderers/runtime-policy/src/main.tsx index 2c4d344b..9db107b7 100644 --- a/packages/renderers/runtime-policy/src/main.tsx +++ b/packages/renderers/runtime-policy/src/main.tsx @@ -1,21 +1,15 @@ import '@githubnext/projections-renderer-kit/react.css' import './styles.css' -import {followContext} from '@githubnext/projections-renderer-kit' import {mountRenderer} from '@githubnext/projections-renderer-kit/react' import {RuntimePolicyView} from './view.js' import {runtimePolicyFacts, runtimePolicyNouns} from './summary.js' import {accepts} from './accepts.js' +import {runtimePolicyContext} from './scope.js' mountRenderer(RuntimePolicyView, { name: 'runtime-policy', - // The Changes scope lays out only the changed policies and their context, each whole: a changed selector or probe - // keeps the probe and policy that declare it and the hook it attaches to, where it is drawn. - scope: { - layout: 'subset', - parts: 'whole', - context: (all, changes) => - followContext(all, changes, {incoming: kind => kind === 'declares' || kind === 'filters', outgoing: kind => kind === 'attaches'}), - }, + // The Changes scope lays out only the changed policies and their context, each whole (see `runtimePolicyContext`). + scope: {layout: 'subset', parts: 'whole', context: runtimePolicyContext}, accepts, // Which workloads a policy applies to is shown as chips inside the policy, not as drawn relationships. capabilities: {changeHighlighting: 'supported', comparisonModes: ['side-by-side', 'diff'], drawsRelationships: false}, diff --git a/packages/renderers/runtime-policy/src/scope.ts b/packages/renderers/runtime-policy/src/scope.ts new file mode 100644 index 00000000..21e90893 --- /dev/null +++ b/packages/renderers/runtime-policy/src/scope.ts @@ -0,0 +1,19 @@ +import {followContext} from '@githubnext/projections-renderer-kit' +import type {ComparisonModel} from '@githubnext/projections-renderer-kit' + +/** Relationships to what a policy or rule applies to, matches or excludes: drawn as chips inside it, not as rows. */ +const subjects = new Set(['applies-to', 'matches', 'excludes']) + +/** + * What the Changes scope keeps beside the changes so each can be read, each laid out whole: a changed selector or + * probe keeps the probe and policy that declare it and the hook it attaches to, where it is drawn. Chips name what + * they point at, changed or not: every policy and rule shown keeps the subjects it applies to, matches or excludes, so + * an unchanged scope reads as itself rather than as no workloads, and a workload keeps the policies covering it, so it + * doesn't read as covered by no policy. + */ +export function runtimePolicyContext(all: ComparisonModel, changes: ReadonlySet): Set { + return followContext(all, changes, { + incoming: kind => kind === 'declares' || kind === 'filters' || kind === 'covers', + outgoing: kind => kind === 'attaches' || subjects.has(kind), + }) +} diff --git a/packages/renderers/runtime-policy/test/runtime-policy.test.mjs b/packages/renderers/runtime-policy/test/runtime-policy.test.mjs index a5b9b833..72dfab55 100644 --- a/packages/renderers/runtime-policy/test/runtime-policy.test.mjs +++ b/packages/renderers/runtime-policy/test/runtime-policy.test.mjs @@ -4,7 +4,7 @@ import {readFileSync} from 'node:fs' import {createElement} from 'react' import {renderToStaticMarkup} from 'react-dom/server' import {createNativeEngine} from '@githubnext/projections-engine/native' -import {comparisonModel, extractionModel, stepEmphasis} from '@githubnext/projections-renderer-kit' +import {changesScope, comparisonModel, extractionModel, scopedModel, stepEmphasis} from '@githubnext/projections-renderer-kit' import {createRulesRuntime} from '@githubnext/projections-comparator-runtime' import {badgeIconNames} from '@githubnext/projections-renderer-sdk/protocol' import {plainProse} from '@githubnext/projections-schemas' @@ -13,6 +13,7 @@ import {chipPrefix, conditionLines, effectProse, warnings} from '../node_modules import {findingLabels, interpretationIcons} from '../node_modules/.cache/test/model.js' import {RuntimePolicyView} from '../node_modules/.cache/test/view.js' +import {runtimePolicyContext} from '../node_modules/.cache/test/scope.js' import {plain} from '../../kit/test/markup.mjs' const engine = createNativeEngine() @@ -319,3 +320,47 @@ test('in the diff, changed rows marked the same way join into one change run, as /
]*data-entity-id="hook:kprobe:sys_execve"[^>]*data-change="added" data-run-start="" data-run-end=""/, ) }) + +/** The diff in the Changes scope, as the shell narrows it: the changes, their context, each laid out whole. */ +function changesView(model, metadata) { + const scope = changesScope(model, {query: '', changesOnly: true, ids: null}) + const shown = new Set([...scope.shown, ...runtimePolicyContext(model, scope.changes)]) + return buildModel(scopedModel(model, shown, 'whole').model, 'diff', metadata) +} + +const chipNames = items => + Object.fromEntries(items.map(item => [item.entity.label, (item.scope ?? item.coveredBy).map(chip => chip.subject.entity.label)])) + +test("the Changes scope keeps what a changed policy's chips name, so an unchanged Applies to still reads", async () => { + // Only a rule changes: the policy's scope is the same in both revisions. + const edited = structuredClone(before) + const rule = edited.entities.find(entity => entity.kind === 'selector') + rule.data = {...rule.data, matchArgs: [{index: 0, operator: 'Prefix', values: ['/var/run/secrets/']}]} + const comparison = await interpreted(before, edited) + const model = comparisonModel(before, edited, comparison) + const full = changesView(model, comparison.metadata) + const all = buildModel(model, 'diff', comparison.metadata) + assert.ok(full.policies.length > 0) + const shown = chipNames(full.policies) + assert.deepEqual(shown, Object.fromEntries(Object.keys(shown).map(label => [label, chipNames(all.policies)[label]]))) + assert.ok( + Object.values(shown).every(names => names.length > 0), + 'no policy reads as applying to no workloads', + ) +}) + +test('the Changes scope keeps the policies covering a changed workload', async () => { + // Only a covered workload changes: the policies covering it don't. + const a = fixture('deployment/before.expected.json') + const b = structuredClone(a) + const covered = new Set(b.relationships.filter(relationship => relationship.kind === 'covers').map(relationship => relationship.to)) + const workload = b.entities.find(entity => entity.kind === 'workload' && covered.has(entity.id)) + workload.data = {...workload.data, replicas: 7} + const comparison = await interpreted(a, b) + const model = comparisonModel(a, b, comparison) + const scoped = chipNames(changesView(model, comparison.metadata).workloads) + const all = chipNames(buildModel(model, 'diff', comparison.metadata).workloads) + assert.deepEqual(Object.keys(scoped), [workload.label]) + assert.deepEqual(scoped[workload.label], all[workload.label]) + assert.ok(scoped[workload.label].length > 0, 'the workload reads as covered') +}) From 4638f8a2fa118d5ba258daf007f437cbe80ab03f Mon Sep 17 00:00:00 2001 From: Alex Gorischek Date: Fri, 20 Nov 2026 13:00:02 -0800 Subject: [PATCH 2/2] Runtime policy: test that Changes keeps a changed rule's matched and excluded subjects Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../test/runtime-policy.test.mjs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/renderers/runtime-policy/test/runtime-policy.test.mjs b/packages/renderers/runtime-policy/test/runtime-policy.test.mjs index 72dfab55..dde66a0a 100644 --- a/packages/renderers/runtime-policy/test/runtime-policy.test.mjs +++ b/packages/renderers/runtime-policy/test/runtime-policy.test.mjs @@ -364,3 +364,38 @@ test('the Changes scope keeps the policies covering a changed workload', async ( assert.deepEqual(scoped[workload.label], all[workload.label]) assert.ok(scoped[workload.label].length > 0, 'the workload reads as covered') }) + +test("the Changes scope keeps a changed rule's matched and excluded subjects, so its binaries still read", async () => { + // Two rules change their conditions only: one matches binaries under a prefix, one excludes a binary. + const edited = structuredClone(library) + const ruled = new Set(edited.relationships.filter(item => ['matches', 'excludes'].includes(item.kind)).map(item => item.from)) + assert.equal(ruled.size, 2) + for (const rule of edited.entities.filter(entity => ruled.has(entity.id))) + rule.data = { + ...rule.data, + conditions: [ + ...(rule.data.conditions ?? []), + {display: '`arg1` (`int`) = `4`', filter: 'matchArgs', index: 1, operator: 'Equal', values: ['4']}, + ], + } + const comparison = await interpreted(library, edited) + const model = comparisonModel(library, edited, comparison) + const rules = view => + new Map( + view.categories.flatMap(category => + category.hooks.flatMap(hook => + hook.probes.flatMap(probe => + probe.selectors.map(rule => [rule.row.id, rule.chips.map(chip => [chip.kind, chip.subject.entity.label])]), + ), + ), + ), + ) + const scoped = rules(changesView(model, comparison.metadata)) + const all = rules(buildModel(model, 'diff', comparison.metadata)) + const changed = [...ruled].map(id => model.entityRow.after.get(id)) + for (const id of changed) { + assert.ok(scoped.get(id)?.length, `${id} shows its binaries`) + assert.deepEqual(scoped.get(id), all.get(id), id) + } + assert.deepEqual(new Set(changed.flatMap(id => scoped.get(id).map(([kind]) => kind))), new Set(['matches', 'excludes'])) +})