Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
feat(studio): add the live DemoSource implementation over MCP
FABIOTESS5a8ce59afd5d
diff --git a/studio/src/lib/__tests__/live-source.test.ts b/studio/src/lib/__tests__/live-source.test.tsnew file mode 100644index 00000000..592af190--- /dev/null+++ b/studio/src/lib/__tests__/live-source.test.ts@@ -0,0 +1,100 @@+import { describe, it, expect, vi } from 'vitest'+import { createLiveSource } from '../live-source'++function sparqlJson(bindings: Record<string, string>[]): string {+ return JSON.stringify({+ results: {+ bindings: bindings.map((row) =>+ Object.fromEntries(Object.entries(row).map(([k, v]) => [k, { value: v }])),+ ),+ },+ })+}++describe('LiveSource', () => {+ it('maps the class and subclass queries into a GraphView', async () => {+ const sparqlQuery = vi.fn(async (q: string) =>+ q.includes('rdfs:subClassOf ?b')+ ? sparqlJson([{ a: 'ex:Dataset', b: 'ex:Resource' }])+ : sparqlJson([{ c: 'ex:Dataset', l: 'Dataset' }]),+ )+ const callTool = vi.fn(async () => '[]')+ const src = createLiveSource({ sparqlQuery, callTool })++ const g = await src.graph()+ expect(g.classes).toContainEqual({ iri: 'ex:Dataset', label: 'Dataset' })+ expect(g.edges).toContainEqual({ source: 'ex:Dataset', target: 'ex:Resource' })+ })++ it('surfaces an engine error rather than returning an empty graph', async () => {+ const sparqlQuery = vi.fn(async () => {+ throw new Error('engine not listening')+ })+ const callTool = vi.fn(async () => '[]')+ const src = createLiveSource({ sparqlQuery, callTool })++ await expect(src.graph()).rejects.toThrow('engine not listening')+ })++ // PIVOT (see docs/superpowers/plans/2026-08-24-studio-public-port.md): findings+ // no longer come from a document contradiction scan. onto_contradiction_scan is+ // not a tool the engine exposes. The demonstration is now the validator finding:+ // corpus documents checked against SHACL shapes (demo/dcat_conformance.py runs+ // the same measurement offline). The live implementation therefore calls the+ // engine's real onto_shacl tool and joins each violation's focus node against+ // the corpus's prov:wasDerivedFrom provenance triples, the same predicate+ // Graph3D.tsx and demo/contradiction_scan.py already rely on, to name the real+ // document that asserted the offending fact.+ it('maps SHACL violations into findings that cite the document named by provenance', async () => {+ const callTool = vi.fn(async (name: string, args?: Record<string, unknown>) => {+ expect(name).toBe('onto_shacl')+ expect(args).toMatchObject({ inline: false })+ return JSON.stringify({+ conforms: false,+ violation_count: 1,+ violations: [+ {+ severity: 'Violation',+ focus_node: 'ex:conformance',+ path: 'https://example.org/dcat#distribution',+ constraint: 'minCount',+ message: 'Property <...distribution> has fewer than 1 values',+ },+ ],+ })+ })+ const sparqlQuery = vi.fn(async () =>+ sparqlJson([{ thing: 'ex:conformance', doc: 'profile-readme.md' }]),+ )+ const src = createLiveSource({ sparqlQuery, callTool })++ const found = await src.findings()+ expect(found).toHaveLength(1)+ expect(found[0].subject).toBe('ex:conformance')+ expect(found[0].kind).toBe('conformance')+ expect(found[0].claims.map((c) => c.document)).toEqual(['profile-readme.md'])+ })++ it('surfaces an engine error from findings() rather than returning an empty array', async () => {+ const callTool = vi.fn(async () => {+ throw new Error('engine not listening')+ })+ const sparqlQuery = vi.fn(async () => sparqlJson([]))+ const src = createLiveSource({ sparqlQuery, callTool })++ await expect(src.findings()).rejects.toThrow('engine not listening')+ })++ // onto_shacl reports a bad shapes path as a successful tool call carrying an+ // {"error": ...} payload, not a thrown exception. That must surface as a+ // rejection too, or a missing corpus would silently look like a clean one.+ it('surfaces a SHACL tool error payload rather than treating it as zero findings', async () => {+ const callTool = vi.fn(async () =>+ JSON.stringify({ error: 'Cannot read shapes file: No such file or directory' }),+ )+ const sparqlQuery = vi.fn(async () => sparqlJson([]))+ const src = createLiveSource({ sparqlQuery, callTool })++ await expect(src.findings()).rejects.toThrow('Cannot read shapes file')+ })+})diff --git a/studio/src/lib/live-source.ts b/studio/src/lib/live-source.tsnew file mode 100644index 00000000..f387cc5c--- /dev/null+++ b/studio/src/lib/live-source.ts@@ -0,0 +1,149 @@+import type { Chunk, Claim, Contradiction, Decision, DemoSource, Document, GraphView } from './demo-source'+import { sparqlQuery, callTool } from './mcp-client'++export interface LiveDeps {+ sparqlQuery: typeof sparqlQuery+ callTool: typeof callTool+}++const PREFIXES = `PREFIX owl: <http://www.w3.org/2002/07/owl#>+PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>`++// The same class and subclass queries Graph3D.tsx issues (studio/src/components/Graph3D.tsx),+// moved here so the component reads through DemoSource instead of talking to the engine directly.+const CLASSES = `${PREFIXES}+SELECT ?c ?l WHERE {+ { ?c a owl:Class } UNION { ?c rdfs:subClassOf ?x }+ OPTIONAL { ?c rdfs:label ?l }+ FILTER(!isBlank(?c))+} LIMIT 300`++const SUBCLASSES = `${PREFIXES}+SELECT ?a ?b WHERE {+ ?a rdfs:subClassOf ?b .+ FILTER(!isBlank(?a) && !isBlank(?b))+}`++// The provenance convention already used elsewhere in this codebase+// (Graph3D.tsx's document-and-entity projection, demo/contradiction_scan.py's+// PROVENANCE query): a fact extracted from a source document carries+// prov:wasDerivedFrom pointing at a resource labelled with that document's name.+const PROVENANCE = `PREFIX prov: <http://www.w3.org/ns/prov#>+PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>+SELECT ?thing ?doc WHERE {+ ?thing prov:wasDerivedFrom ?source .+ ?source rdfs:label ?doc .+}`++// The corpus this demonstration validates: DCAT-US's own recovered SHACL+// shapes, the exact file demo/dcat_conformance.py validates against+// (demo/corpus/dcat-us/recovered-shapes.ttl). Passed as a path rather than+// inline content: the live source runs in the webview and has no filesystem+// access of its own, only the engine process does.+const SHAPES_PATH = 'demo/corpus/dcat-us/recovered-shapes.ttl'++function rows(raw: string): Record<string, string>[] {+ const parsed = JSON.parse(raw)+ const bindings = parsed?.results?.bindings ?? []+ return bindings.map((b: Record<string, { value: string }>) =>+ Object.fromEntries(Object.entries(b).map(([k, v]) => [k, v.value])),+ )+}++interface ShaclViolation {+ severity: string+ focus_node: string+ path?: string+ constraint: string+ message: string+}++interface ShaclReport {+ error?: string+ conforms?: boolean | null+ violation_count?: number+ violations?: ShaclViolation[]+ warning?: string+}++export function createLiveSource(deps: LiveDeps): DemoSource {+ return {+ async corpus(): Promise<Document[]> {+ return JSON.parse(await deps.callTool('corpus_documents'))+ },++ async graph(): Promise<GraphView> {+ const [classRows, subRows] = await Promise.all([+ deps.sparqlQuery(CLASSES).then(rows),+ deps.sparqlQuery(SUBCLASSES).then(rows),+ ])+ return {+ classes: classRows.map((r) => (r.l ? { iri: r.c, label: r.l } : { iri: r.c })),+ properties: [],+ edges: subRows.map((r) => ({ source: r.a, target: r.b })),+ }+ },++ // Findings no longer come from a document-contradiction scan: no+ // onto_contradiction_scan tool exists on the engine (see the PIVOT section+ // of docs/superpowers/plans/2026-08-24-studio-public-port.md). The+ // demonstration is the validator finding instead: corpus documents checked+ // against SHACL shapes, the same measurement demo/dcat_conformance.py runs+ // offline. This calls the engine's real onto_shacl tool and joins each+ // violation's focus node against the corpus's own provenance triples to+ // name the document that asserted the offending fact.+ //+ // Two failure shapes are both surfaced, never swallowed: a thrown engine+ // error (network down, session lost) propagates as-is, and onto_shacl's+ // own {"error": ...} response (a missing or unreadable shapes file) is+ // turned into a thrown error too. Neither collapses into an empty array,+ // because an empty findings list must mean "validated clean", never+ // "the engine could not be asked".+ async findings(): Promise<Contradiction[]> {+ const raw = await deps.callTool('onto_shacl', { shapes: SHAPES_PATH, inline: false })+ const report = JSON.parse(raw) as ShaclReport+ if (report.error) {+ throw new Error(`SHACL validation failed: ${report.error}`)+ }+ const violations = report.violations ?? []++ const provenance = rows(await deps.sparqlQuery(PROVENANCE))+ const docsByThing = new Map<string, string[]>()+ for (const r of provenance) {+ if (!r.thing || !r.doc) continue+ const list = docsByThing.get(r.thing) ?? []+ list.push(r.doc)+ docsByThing.set(r.thing, list)+ }++ return violations.map((v, i) => {+ // If the store carries no provenance for this focus node, cite the+ // node itself rather than a name invented for the occasion.+ const documents = docsByThing.get(v.focus_node) ?? [v.focus_node]+ const claims: Claim[] = documents.map((document) => ({+ document,+ predicate: v.constraint,+ object: v.message,+ }))+ return {+ id: `shacl:${v.focus_node}|${v.path ?? ''}|${v.constraint}|${i}`,+ subject: v.focus_node,+ kind: 'conformance' as const,+ claims,+ }+ })+ },++ async resolve(id: string, decision: Decision): Promise<void> {+ await deps.callTool('onto_apply', { finding: id, decision: decision.kind })+ await deps.callTool('onto_save')+ },++ async *ask(question: string): AsyncIterable<Chunk> {+ const answer = await deps.callTool('agent_ask', { question })+ yield { type: 'text', value: answer }+ },+ }+}++export const liveSource = () => createLiveSource({ sparqlQuery, callTool })