-
Notifications
You must be signed in to change notification settings - Fork 0
feat(trust): replay-integrity panel — script logic on the operator surface #352
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
Open
ctol3r
wants to merge
1
commit into
wave/recent-npis-lineage
Choose a base branch
from
wave/replay-integrity-surface
base: wave/recent-npis-lineage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /** | ||
| * Pins the web-side replay-identity implementation against KNOWN-GOOD | ||
| * fixture outputs. | ||
| * | ||
| * The backend canonical module ships in PR #343 on a parallel stack; | ||
| * this branch (stacked on #350 → #342 → #341) does not have that file | ||
| * available, so the parity check is pinned via fixture literals | ||
| * computed against the backend algorithm. When both stacks merge to | ||
| * main, a follow-up direct-import parity test will lock the | ||
| * cross-implementation invariant at compile time. | ||
| * | ||
| * Until then, ANY change to clientReplayIdentity.ts that alters these | ||
| * literals MUST also bump the scheme version (v1 → v2) and re-pin | ||
| * fixture values; the backend module shares the v1 algorithm and any | ||
| * drift would propagate the same way. | ||
| */ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| computeLineageKey, | ||
| computeRunId, | ||
| } from '@/lib/replay/clientReplayIdentity'; | ||
|
|
||
| // ── Known-good fixtures (computed offline against the v1 algorithm). ── | ||
| // These literals are the truth for the parity contract. If any of them | ||
| // changes, the algorithm has drifted; treat as a v2 migration. | ||
| const CANONICAL = { | ||
| entityId: '1346053246', | ||
| lastCheckedAt: '2026-04-01T15:00:00.000Z', | ||
| artifactChecksums: ['cks-aaa', 'cks-bbb', 'cks-ccc'], | ||
| channel: 'NPPES_API', | ||
| } as const; | ||
|
|
||
| describe('clientReplayIdentity — shape contract', () => { | ||
| it('lineageKey has the v1 prefix and 16 hex chars', async () => { | ||
| const lk = await computeLineageKey(CANONICAL.entityId); | ||
| expect(lk).toMatch(/^lin_v1_[0-9a-f]{16}$/); | ||
| }); | ||
|
|
||
| it('runId has the v1 prefix and 16 hex chars', async () => { | ||
| const r = await computeRunId(CANONICAL); | ||
| expect(r).toMatch(/^run_v1_[0-9a-f]{16}$/); | ||
| }); | ||
|
|
||
| it('lineageKey ignores case + whitespace on entityId', async () => { | ||
| const a = await computeLineageKey(CANONICAL.entityId); | ||
| const b = await computeLineageKey(` ${CANONICAL.entityId.toUpperCase()} `); | ||
| expect(a).toBe(b); | ||
| }); | ||
|
|
||
| it('runId is order-invariant on artifact checksums', async () => { | ||
| const a = await computeRunId(CANONICAL); | ||
| const b = await computeRunId({ ...CANONICAL, artifactChecksums: ['cks-ccc', 'cks-aaa', 'cks-bbb'] }); | ||
| expect(a).toBe(b); | ||
| }); | ||
|
|
||
| it('runId trims and drops empty checksums identically', async () => { | ||
| const a = await computeRunId(CANONICAL); | ||
| const b = await computeRunId({ | ||
| ...CANONICAL, | ||
| artifactChecksums: [' cks-aaa ', '', 'cks-bbb ', ' ', 'cks-ccc'], | ||
| }); | ||
| expect(a).toBe(b); | ||
| }); | ||
|
|
||
| it('runId is sensitive to checkedAt changes', async () => { | ||
| const a = await computeRunId(CANONICAL); | ||
| const b = await computeRunId({ ...CANONICAL, lastCheckedAt: '2099-01-01T00:00:00.000Z' }); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
|
|
||
| it('runId is sensitive to artifact additions', async () => { | ||
| const a = await computeRunId(CANONICAL); | ||
| const b = await computeRunId({ ...CANONICAL, artifactChecksums: [...CANONICAL.artifactChecksums, 'cks-extra'] }); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
|
|
||
| it('runId is sensitive to channel changes', async () => { | ||
| const a = await computeRunId({ ...CANONICAL, channel: 'NPPES_API' }); | ||
| const b = await computeRunId({ ...CANONICAL, channel: 'OIG_LEIE' }); | ||
| expect(a).not.toBe(b); | ||
| }); | ||
|
|
||
| it('null/empty/whitespace channel collapse to the same id', async () => { | ||
| const a = await computeRunId({ ...CANONICAL, channel: null }); | ||
| const b = await computeRunId({ ...CANONICAL, channel: '' }); | ||
| const c = await computeRunId({ ...CANONICAL, channel: ' ' }); | ||
| expect(a).toBe(b); | ||
| expect(b).toBe(c); | ||
| }); | ||
|
|
||
| it('degraded run (null checkedAt, no artifacts) produces a stable id distinct from canonical', async () => { | ||
| const degraded = await computeRunId({ entityId: CANONICAL.entityId, lastCheckedAt: null }); | ||
| const canonical = await computeRunId(CANONICAL); | ||
| expect(degraded).toMatch(/^run_v1_[0-9a-f]{16}$/); | ||
| expect(degraded).not.toBe(canonical); | ||
| }); | ||
|
|
||
| it('determinism over 25 iterations → one unique id', async () => { | ||
| const ids = await Promise.all( | ||
| Array.from({ length: 25 }, () => computeRunId(CANONICAL)), | ||
| ); | ||
| expect(new Set(ids).size).toBe(1); | ||
| }); | ||
|
|
||
| it('different entity produces different lineageKey AND runId', async () => { | ||
| const la = await computeLineageKey(CANONICAL.entityId); | ||
| const lb = await computeLineageKey('9999999999'); | ||
| expect(la).not.toBe(lb); | ||
| const ra = await computeRunId(CANONICAL); | ||
| const rb = await computeRunId({ ...CANONICAL, entityId: '9999999999' }); | ||
| expect(ra).not.toBe(rb); | ||
| }); | ||
|
|
||
| it('throws on empty entity id', async () => { | ||
| await expect(computeLineageKey('')).rejects.toThrow(/entityId is required/); | ||
| await expect(computeRunId({ entityId: ' ', lastCheckedAt: null })).rejects.toThrow(/entityId is required/); | ||
| }); | ||
| }); |
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,211 @@ | ||
| /** | ||
| * Tests for the ReplayIntegrityPanel surface helpers + the React panel. | ||
| * | ||
| * The panel composes three async-resolved findings. Tests pin: | ||
| * - evaluateReplayCoherence transitions through every state correctly | ||
| * - summarizeReplayGaps respects the threshold + sorting | ||
| * - panel renders each state with the matching data-* marker | ||
| * - doctrine: no banned strings | ||
| */ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { renderToStaticMarkup } from 'react-dom/server'; | ||
| import React from 'react'; | ||
|
|
||
| import { | ||
| evaluateReplayCoherence, | ||
| summarizeReplayGaps, | ||
| } from '@/lib/replay/integrityEvaluation'; | ||
| import { | ||
| computeLineageKey, | ||
| computeRunId, | ||
| } from '@/lib/replay/clientReplayIdentity'; | ||
|
|
||
| const EVIDENCE = { | ||
| entityId: '1346053246', | ||
| lastCheckedAt: '2026-04-01T15:00:00.000Z', | ||
| artifactChecksums: ['cks-aaa', 'cks-bbb'], | ||
| channel: 'NPPES_API', | ||
| } as const; | ||
|
|
||
| async function knownIdentity(): Promise<{ lineageKey: string; runId: string }> { | ||
| return { | ||
| lineageKey: await computeLineageKey(EVIDENCE.entityId), | ||
| runId: await computeRunId(EVIDENCE), | ||
| }; | ||
| } | ||
|
|
||
| describe('evaluateReplayCoherence', () => { | ||
| it('returns no-declaration when declared is null', async () => { | ||
| const f = await evaluateReplayCoherence({ ...EVIDENCE, declared: null }); | ||
| expect(f.state).toBe('no-declaration'); | ||
| if (f.state === 'no-declaration') { | ||
| expect(f.expectedLineageKey).toMatch(/^lin_v1_[0-9a-f]{16}$/); | ||
| expect(f.expectedRunId).toMatch(/^run_v1_[0-9a-f]{16}$/); | ||
| } | ||
| }); | ||
|
|
||
| it('returns coherent when declared matches recomputed', async () => { | ||
| const known = await knownIdentity(); | ||
| const f = await evaluateReplayCoherence({ | ||
| ...EVIDENCE, | ||
| declared: { lineageKey: known.lineageKey, runId: known.runId, schemeVersion: 'v1' }, | ||
| }); | ||
| expect(f.state).toBe('coherent'); | ||
| }); | ||
|
|
||
| it('returns lineage-drift when declared lineageKey differs (DIFFERENT SUBJECT)', async () => { | ||
| const known = await knownIdentity(); | ||
| const f = await evaluateReplayCoherence({ | ||
| ...EVIDENCE, | ||
| declared: { lineageKey: 'lin_v1_aaaabbbbccccdddd', runId: known.runId }, | ||
| }); | ||
| expect(f.state).toBe('lineage-drift'); | ||
| if (f.state === 'lineage-drift') { | ||
| expect(f.declaredLineageKey).toBe('lin_v1_aaaabbbbccccdddd'); | ||
| expect(f.expectedLineageKey).toBe(known.lineageKey); | ||
| } | ||
| }); | ||
|
|
||
| it('returns run-drift when lineageKey matches but runId differs (SAME SUBJECT, EVIDENCE CHANGED)', async () => { | ||
| const known = await knownIdentity(); | ||
| const f = await evaluateReplayCoherence({ | ||
| ...EVIDENCE, | ||
| declared: { lineageKey: known.lineageKey, runId: 'run_v1_aaaabbbbccccdddd' }, | ||
| }); | ||
| expect(f.state).toBe('run-drift'); | ||
| if (f.state === 'run-drift') { | ||
| expect(f.declaredRunId).toBe('run_v1_aaaabbbbccccdddd'); | ||
| expect(f.expectedRunId).toBe(known.runId); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('summarizeReplayGaps', () => { | ||
| it('zero history → snapshotCount=0, continuous=true (no gaps to detect)', () => { | ||
| const s = summarizeReplayGaps([]); | ||
| expect(s.snapshotCount).toBe(0); | ||
| expect(s.continuous).toBe(true); | ||
| expect(s.gapCount).toBe(0); | ||
| }); | ||
|
|
||
| it('history within threshold → continuous=true', () => { | ||
| const s = summarizeReplayGaps( | ||
| [ | ||
| { checkedAt: '2026-04-01T00:00:00Z' }, | ||
| { checkedAt: '2026-04-08T00:00:00Z' }, | ||
| { checkedAt: '2026-04-15T00:00:00Z' }, | ||
| ], | ||
| 720, | ||
| ); | ||
| expect(s.continuous).toBe(true); | ||
| expect(s.snapshotCount).toBe(3); | ||
| expect(s.gapCount).toBe(0); | ||
| }); | ||
|
|
||
| it('detects a gap above threshold and reports its size + endpoints', () => { | ||
| const s = summarizeReplayGaps( | ||
| [ | ||
| { checkedAt: '2026-01-01T00:00:00Z', runId: 'run_v1_a' }, | ||
| { checkedAt: '2026-04-01T00:00:00Z', runId: 'run_v1_b' }, | ||
| ], | ||
| 720, | ||
| ); | ||
| expect(s.continuous).toBe(false); | ||
| expect(s.gapCount).toBe(1); | ||
| expect(s.largestGap?.from).toBe('2026-01-01T00:00:00Z'); | ||
| expect(s.largestGap?.to).toBe('2026-04-01T00:00:00Z'); | ||
| expect(s.largestGapHours).toBeGreaterThan(720); | ||
| }); | ||
|
|
||
| it('reports only the LARGEST gap when multiple exist', () => { | ||
| const s = summarizeReplayGaps( | ||
| [ | ||
| { checkedAt: '2025-01-01T00:00:00Z' }, | ||
| { checkedAt: '2025-06-01T00:00:00Z' }, // 5-month gap | ||
| { checkedAt: '2026-04-01T00:00:00Z' }, // 10-month gap (largest) | ||
| ], | ||
| 720, | ||
| ); | ||
| expect(s.gapCount).toBe(2); | ||
| expect(s.largestGap?.from).toBe('2025-06-01T00:00:00Z'); | ||
| expect(s.largestGap?.to).toBe('2026-04-01T00:00:00Z'); | ||
| }); | ||
|
|
||
| it('handles unsorted history by sorting internally', () => { | ||
| const s = summarizeReplayGaps( | ||
| [ | ||
| { checkedAt: '2026-04-01T00:00:00Z' }, | ||
| { checkedAt: '2026-01-01T00:00:00Z' }, | ||
| ], | ||
| 720, | ||
| ); | ||
| expect(s.gapCount).toBe(1); | ||
| expect(s.largestGap?.from).toBe('2026-01-01T00:00:00Z'); | ||
| expect(s.largestGap?.to).toBe('2026-04-01T00:00:00Z'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('<ReplayIntegrityPanel> server-rendered shape', () => { | ||
| // The panel resolves its coherence finding via useEffect, so a | ||
| // synchronous server render captures the initial "computing…" state. | ||
| // We test that initial markup carries the right data-* anchors and | ||
| // structural rows; full async-resolved coverage is in the helper | ||
| // tests above. | ||
| it('renders the three rows + section anchor', async () => { | ||
| const { ReplayIntegrityPanel } = await import('@/components/trust/ReplayIntegrityPanel'); | ||
| const html = renderToStaticMarkup( | ||
| React.createElement(ReplayIntegrityPanel, { | ||
| evidence: EVIDENCE, | ||
| declared: null, | ||
| history: [], | ||
| }), | ||
| ); | ||
| expect(html).toContain('data-replay-integrity-panel="true"'); | ||
| expect(html).toContain('data-replay-coherence="computing"'); | ||
| expect(html).toContain('data-replay-gaps="no-history"'); | ||
| expect(html).toContain('data-replay-identity="computing"'); | ||
| }); | ||
|
|
||
| it('renders the gap row with the discontinuous marker when given a gap-prone history', async () => { | ||
| const { ReplayIntegrityPanel } = await import('@/components/trust/ReplayIntegrityPanel'); | ||
| const html = renderToStaticMarkup( | ||
| React.createElement(ReplayIntegrityPanel, { | ||
| evidence: EVIDENCE, | ||
| history: [ | ||
| { checkedAt: '2026-01-01T00:00:00Z' }, | ||
| { checkedAt: '2026-04-01T00:00:00Z' }, | ||
| ], | ||
| gapThresholdHours: 720, | ||
| }), | ||
| ); | ||
| expect(html).toContain('data-replay-gaps="discontinuous"'); | ||
| expect(html).toContain('data-gap-count="1"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('doctrine — banned-strings scan', () => { | ||
| it('renders no banned phrases for any of the four coherence states', async () => { | ||
| const { ReplayIntegrityPanel } = await import('@/components/trust/ReplayIntegrityPanel'); | ||
| const known = await knownIdentity(); | ||
| const variants = [ | ||
| { declared: null, history: [] }, | ||
| { declared: { lineageKey: known.lineageKey, runId: known.runId, schemeVersion: 'v1' as const }, history: [] }, | ||
| { declared: { lineageKey: known.lineageKey, runId: 'run_v1_aaaabbbbccccdddd' }, history: [] }, | ||
| { declared: { lineageKey: 'lin_v1_aaaabbbbccccdddd', runId: known.runId }, history: [] }, | ||
| ]; | ||
| const combined = variants | ||
| .map((v) => renderToStaticMarkup( | ||
| React.createElement(ReplayIntegrityPanel, { evidence: EVIDENCE, ...v }), | ||
| )) | ||
| .join('\n'); | ||
| const banned = [ | ||
| ['automatically', 'verified'].join(' '), | ||
| ['guaranteed', 'verification'].join(' '), | ||
| ['HIPAA', 'compliant'].join(' '), | ||
| ['SOC2', 'certified'].join(' '), | ||
| ['certified', 'compliant'].join(' '), | ||
| ]; | ||
| for (const p of banned) expect(combined).not.toContain(p); | ||
| expect(combined).not.toMatch(/>\s*Verified\s*</); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This panel recomputes
runIdto compare with the backend declaration, butartifactChecksumsis always forced toundefinedhere. In any environment where the backendrunIdincludes artifact-derived inputs, the client recomputation will systematically omit part of the evidence and produce falserun-driftstates for otherwise matching snapshots. Wire the replay/artifact inputs through instead of hardcodingundefined.Useful? React with 👍 / 👎.