Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(studio): stop replay source leaking mutable fixture references

FABIOTESS7923ef1ccb76
diff --git a/studio/src/lib/__tests__/replay-source.test.ts b/studio/src/lib/__tests__/replay-source.test.ts
index a6645e35..3b6e53f6 100644
--- a/studio/src/lib/__tests__/replay-source.test.ts
+++ b/studio/src/lib/__tests__/replay-source.test.ts
@@ -1,7 +1,8 @@
import { describe, it, expect } from 'vitest'
-import { createReplaySource } from '../replay-source'
+import { createReplaySource, type ReplayFixtures } from '../replay-source'
+import type { Chunk } from '../demo-source'
-const fixtures = {
+const fixtures: ReplayFixtures = {
corpus: [{ id: 'README', title: 'README', text: 'an implementation of the W3C DCAT standard' }],
graph: { classes: [{ iri: 'ex:Dataset' }], properties: [], edges: [] },
findings: [
@@ -33,6 +34,27 @@ describe('ReplaySource', () => {
expect(fixtures.findings).toHaveLength(1)
})
+ it('does not hand out live references into the claim fixtures', async () => {
+ const src = createReplaySource(fixtures)
+ const found = await src.findings()
+ found[0].claims[0].object = 'mutated-in-place'
+ expect(fixtures.findings[0].claims[0].object).toBe('dcat-conformant')
+ })
+
+ it('does not hand out a live reference into the corpus fixtures', async () => {
+ const src = createReplaySource(fixtures)
+ const found = await src.corpus()
+ found[0].title = 'mutated-in-place'
+ expect(fixtures.corpus[0].title).toBe('README')
+ })
+
+ it('does not hand out a live reference into the graph fixtures', async () => {
+ const src = createReplaySource(fixtures)
+ const found = await src.graph()
+ found.classes[0].label = 'mutated-in-place'
+ expect(fixtures.graph.classes[0].label).toBeUndefined()
+ })
+
it('streams a scripted answer', async () => {
const src = createReplaySource(fixtures)
const out: string[] = []
@@ -40,10 +62,12 @@ describe('ReplaySource', () => {
expect(out.join('')).toContain('disagree')
})
- it('answers unknown questions without throwing', async () => {
+ it('marks an unscripted answer with a distinct chunk type instead of relying on wording', async () => {
const src = createReplaySource(fixtures)
- const out: string[] = []
- for await (const chunk of src.ask('unscripted question')) out.push(chunk.value)
- expect(out.join('')).not.toHaveLength(0)
+ const chunks: Chunk[] = []
+ for await (const chunk of src.ask('unscripted question')) chunks.push(chunk)
+ expect(chunks).toHaveLength(1)
+ expect(chunks[0].type).toBe('unscripted')
+ expect(chunks[0].value.length).toBeGreaterThan(0)
})
})
diff --git a/studio/src/lib/demo-source.ts b/studio/src/lib/demo-source.ts
index 304f441e..1f04c04b 100644
--- a/studio/src/lib/demo-source.ts
+++ b/studio/src/lib/demo-source.ts
@@ -12,7 +12,7 @@ export interface GraphView {
edges: { source: string; target: string }[]
}
export type Decision = { kind: 'accept' | 'reject'; note?: string }
-export interface Chunk { type: 'text' | 'tool_call'; value: string }
+export interface Chunk { type: 'text' | 'tool_call' | 'unscripted'; value: string }
export interface DemoSource {
corpus(): Promise<Document[]>
diff --git a/studio/src/lib/replay-source.ts b/studio/src/lib/replay-source.ts
index 41886c6d..145c59c8 100644
--- a/studio/src/lib/replay-source.ts
+++ b/studio/src/lib/replay-source.ts
@@ -1,40 +1,55 @@
-import type { Chunk, Contradiction, Decision, DemoSource, Document, GraphView } from './demo-source'
+import type { Chunk, Claim, Contradiction, Decision, DemoSource, Document, GraphView } from './demo-source'
+// A later artifact bundle carries a fifth key ('compare') alongside the
+// four this replay reads. The index signature makes that tolerance
+// explicit in the type rather than incidental to how a caller happens to
+// construct the object, so a literal with the extra key compiles.
export interface ReplayFixtures {
corpus: Document[]
graph: GraphView
findings: Contradiction[]
chat: Record<string, Chunk[]>
+ [key: string]: unknown
}
-const UNSCRIPTED: Chunk[] = [
- {
- type: 'text',
- value:
- 'This is the offline replay of the demonstration. Only the scripted questions are answered here. Run the desktop application against the engine for a live session.',
- },
-]
+const UNSCRIPTED_CHUNK: Chunk = {
+ type: 'unscripted',
+ value:
+ 'This is the offline replay of the demonstration. Only the scripted questions are answered here. Run the desktop application against the engine for a live session.',
+}
+
+const cloneDocument = (d: Document): Document => ({ ...d })
+const cloneClaim = (c: Claim): Claim => ({ ...c })
+const cloneContradiction = (f: Contradiction): Contradiction => ({ ...f, claims: f.claims.map(cloneClaim) })
+const cloneGraph = (g: GraphView): GraphView => ({
+ classes: g.classes.map((c) => ({ ...c })),
+ properties: g.properties.map((p) => ({ ...p })),
+ edges: g.edges.map((e) => ({ ...e })),
+})
export function createReplaySource(fixtures: ReplayFixtures): DemoSource {
- const findings = fixtures.findings.map((f) => ({ ...f, claims: [...f.claims] }))
const ledger: { id: string; decision: Decision }[] = []
return {
async corpus() {
- return fixtures.corpus
+ return fixtures.corpus.map(cloneDocument)
},
async graph() {
- return fixtures.graph
+ return cloneGraph(fixtures.graph)
},
async findings() {
- return findings
+ return fixtures.findings.map(cloneContradiction)
},
async resolve(id, decision) {
ledger.push({ id, decision })
},
async *ask(question) {
const scripted = fixtures.chat[question.trim().toLowerCase()] ?? fixtures.chat[question]
- for (const chunk of scripted ?? UNSCRIPTED) yield chunk
+ if (scripted) {
+ for (const chunk of scripted) yield chunk
+ } else {
+ yield UNSCRIPTED_CHUNK
+ }
},
}
}