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.

feat(studio): tokenise chat questions before any model sees them

FABIOTESS012fa420e950
diff --git a/studio/src-tauri/sidecars/agent/dist/index.js b/studio/src-tauri/sidecars/agent/dist/index.js
index 04b9d613..ea074b3b 100644
--- a/studio/src-tauri/sidecars/agent/dist/index.js
+++ b/studio/src-tauri/sidecars/agent/dist/index.js
@@ -1,4 +1,5 @@
import { query } from '@anthropic-ai/claude-agent-sdk';
+import { tokeniseInbound } from './tokenise.js';
import * as readline from 'readline';
const ENGINE_URL = 'http://localhost:8080/mcp';
const SYSTEM_PROMPT = `You are an ontology engineering assistant with MCP tools for the Open Ontologies engine.
@@ -44,7 +45,7 @@ async function runTurn(prompt) {
prompt,
options: {
systemPrompt: SYSTEM_PROMPT,
- model: 'claude-opus-4-6',
+ model: 'claude-opus-4-8',
mcpServers: { 'ontology-engine': { type: 'http', url: ENGINE_URL } },
allowedTools: ['mcp__ontology-engine__*'],
tools: [],
@@ -356,6 +357,16 @@ Call onto_stats after. Do NOT save yet.`,
}
// --- Handle a chat message ---
async function handleMessage(userMessage, mode = 'sketch') {
+ // Same protection at the chat entrance as at ingestion: detect and
+ // tokenise sensitive values before ANY provider sees the text. The keyed
+ // scheme matches examples/document-privacy/tokenisation.py, so a value
+ // mentioned in a question still joins to its graph node.
+ const tok = tokeniseInbound(userMessage);
+ if (tok.count > 0) {
+ userMessage = tok.text;
+ send({ type: 'text',
+ content: `_${tok.count} sensitive value(s) tokenised before the model saw your question._\n` });
+ }
try {
const isBuildLike = isBuildRequest(userMessage) || isSketchRequest(userMessage);
if (isBuildLike) {
diff --git a/studio/src-tauri/sidecars/agent/dist/tokenise.js b/studio/src-tauri/sidecars/agent/dist/tokenise.js
new file mode 100644
index 00000000..77ea7c88
--- /dev/null
+++ b/studio/src-tauri/sidecars/agent/dist/tokenise.js
@@ -0,0 +1,34 @@
+import { createHmac } from 'node:crypto';
+/**
+ * Tokenisation at the chat entrance.
+ *
+ * Ingestion tokenises documents before any model sees them; a question typed
+ * into the chat went to the model untouched, which is a leak path the moment
+ * the provider is remote. The same detection patterns and the same keyed
+ * token scheme as the pipeline (TOK_{KIND}_{hmac[:12]}) run here, so a value
+ * mentioned in a question maps to the SAME token the ingested documents use:
+ * the model can still join "ask about Dr Jane Smith" to the graph node,
+ * without ever seeing the name.
+ */
+const KEY = process.env.ONTO_VAULT_KEY ?? 'demo-key-not-for-production';
+const PATTERNS = [
+ ['EMAIL', /\b[\w.+-]+@[\w-]+\.[\w.]{2,}\b/g],
+ ['PHONE', /\b(?:\+\d{1,3}[ -]?)?(?:\(?\d{3,5}\)?[ -]?){2,3}\d{3,4}\b/g],
+ ['PERSON', /\b(?:Dr|Prof|Mr|Mrs|Ms)\.? [A-Z][a-z]+ [A-Z][a-z]+\b/g],
+ ['ID', /\b[A-Z]{2,4}-\d{4,}\b/g],
+];
+function token(kind, value) {
+ const digest = createHmac('sha256', KEY).update(value).digest('hex').slice(0, 12);
+ return `TOK_${kind}_${digest}`;
+}
+export function tokeniseInbound(text) {
+ let out = text;
+ let count = 0;
+ for (const [kind, pattern] of PATTERNS) {
+ out = out.replace(pattern, (m) => {
+ count += 1;
+ return token(kind, m);
+ });
+ }
+ return { text: out, count };
+}
diff --git a/studio/src-tauri/sidecars/agent/index.ts b/studio/src-tauri/sidecars/agent/index.ts
index 462404e9..e58c82ca 100644
--- a/studio/src-tauri/sidecars/agent/index.ts
+++ b/studio/src-tauri/sidecars/agent/index.ts
@@ -1,4 +1,5 @@
import { query } from '@anthropic-ai/claude-agent-sdk';
+import { tokeniseInbound } from './tokenise.js';
import type { Query } from '@anthropic-ai/claude-agent-sdk';
import * as readline from 'readline';
@@ -385,6 +386,17 @@ Call onto_stats after. Do NOT save yet.`,
// --- Handle a chat message ---
async function handleMessage(userMessage: string, mode: 'sketch' | 'build' = 'sketch'): Promise<void> {
+ // Same protection at the chat entrance as at ingestion: detect and
+ // tokenise sensitive values before ANY provider sees the text. The keyed
+ // scheme matches examples/document-privacy/tokenisation.py, so a value
+ // mentioned in a question still joins to its graph node.
+ const tok = tokeniseInbound(userMessage);
+ if (tok.count > 0) {
+ userMessage = tok.text;
+ send({ type: 'text',
+ content: `_${tok.count} sensitive value(s) tokenised before the model saw your question._\n` });
+ }
+
try {
const isBuildLike = isBuildRequest(userMessage) || isSketchRequest(userMessage);
if (isBuildLike) {
diff --git a/studio/src-tauri/sidecars/agent/package-lock.json b/studio/src-tauri/sidecars/agent/package-lock.json
index 4abb0113..015fef61 100644
--- a/studio/src-tauri/sidecars/agent/package-lock.json
+++ b/studio/src-tauri/sidecars/agent/package-lock.json
@@ -11,7 +11,7 @@
"@anthropic-ai/claude-agent-sdk": "^0.2.76"
},
"devDependencies": {
- "@types/node": "^22.0.0",
+ "@types/node": "^22.20.1",
"typescript": "^5.7.0"
}
},
@@ -343,9 +343,9 @@
}
},
"node_modules/@types/node": {
- "version": "22.19.15",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.15.tgz",
- "integrity": "sha512-F0R/h2+dsy5wJAUe3tAU6oqa2qbWY5TpNfL/RGmo1y38hiyO1w3x2jPtt76wmuaJI4DQnOBu21cNXQ2STIUUWg==",
+ "version": "22.20.1",
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-22.20.1.tgz",
+ "integrity": "sha512-EANqOCF9QFyra+4pfxUcX9STKJpCLjMbObVzljIJomAWSnuSIEAvyzEU53GaajbXJEgdh0iEcPL+DGvpUd4k1Q==",
"dev": true,
"license": "MIT",
"dependencies": {
diff --git a/studio/src-tauri/sidecars/agent/package.json b/studio/src-tauri/sidecars/agent/package.json
index 84ee1c57..5ff56a37 100644
--- a/studio/src-tauri/sidecars/agent/package.json
+++ b/studio/src-tauri/sidecars/agent/package.json
@@ -9,7 +9,7 @@
"start": "node dist/index.js"
},
"devDependencies": {
- "@types/node": "^22.0.0",
+ "@types/node": "^22.20.1",
"typescript": "^5.7.0"
},
"dependencies": {
diff --git a/studio/src-tauri/sidecars/agent/tokenise.ts b/studio/src-tauri/sidecars/agent/tokenise.ts
new file mode 100644
index 00000000..85a2b63d
--- /dev/null
+++ b/studio/src-tauri/sidecars/agent/tokenise.ts
@@ -0,0 +1,39 @@
+import { createHmac } from 'node:crypto';
+
+/**
+ * Tokenisation at the chat entrance.
+ *
+ * Ingestion tokenises documents before any model sees them; a question typed
+ * into the chat went to the model untouched, which is a leak path the moment
+ * the provider is remote. The same detection patterns and the same keyed
+ * token scheme as the pipeline (TOK_{KIND}_{hmac[:12]}) run here, so a value
+ * mentioned in a question maps to the SAME token the ingested documents use:
+ * the model can still join "ask about Dr Jane Smith" to the graph node,
+ * without ever seeing the name.
+ */
+
+const KEY = process.env.ONTO_VAULT_KEY ?? 'demo-key-not-for-production';
+
+const PATTERNS: Array<[string, RegExp]> = [
+ ['EMAIL', /\b[\w.+-]+@[\w-]+\.[\w.]{2,}\b/g],
+ ['PHONE', /\b(?:\+\d{1,3}[ -]?)?(?:\(?\d{3,5}\)?[ -]?){2,3}\d{3,4}\b/g],
+ ['PERSON', /\b(?:Dr|Prof|Mr|Mrs|Ms)\.? [A-Z][a-z]+ [A-Z][a-z]+\b/g],
+ ['ID', /\b[A-Z]{2,4}-\d{4,}\b/g],
+];
+
+function token(kind: string, value: string): string {
+ const digest = createHmac('sha256', KEY).update(value).digest('hex').slice(0, 12);
+ return `TOK_${kind}_${digest}`;
+}
+
+export function tokeniseInbound(text: string): { text: string; count: number } {
+ let out = text;
+ let count = 0;
+ for (const [kind, pattern] of PATTERNS) {
+ out = out.replace(pattern, (m) => {
+ count += 1;
+ return token(kind, m);
+ });
+ }
+ return { text: out, count };
+}