From af46ce78ffbdf2b88e2df752c7cb101dd6787b85 Mon Sep 17 00:00:00 2001 From: "Christopher S. Aondona" Date: Sun, 15 Nov 2026 21:08:28 +0100 Subject: [PATCH 01/18] feat(playground): add strict schema-2 authoring workbench boundary --- playground/workbench.ts | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 playground/workbench.ts diff --git a/playground/workbench.ts b/playground/workbench.ts new file mode 100644 index 00000000..2665782d --- /dev/null +++ b/playground/workbench.ts @@ -0,0 +1,83 @@ +import { + ArtifactValidationError, + ThroughlineAuthoringError, + compileArtifact, + parseThlBody, +} from "../src/index.js"; +import type { CompiledTrace } from "../src/v2/model.js"; + +/** + * The public playground deliberately uses the *same* strict read-side + * validation and compiler as a production consumer. It never executes source + * or converts prose into an invented artifact. + */ +export type WorkbenchResult = + | Readonly<{ kind: "ready"; trace: CompiledTrace }> + | Readonly<{ kind: "intent"; message: string }> + | Readonly<{ kind: "invalid"; message: string; path?: string; code?: string }>; + +export const STARTER_SOURCE = JSON.stringify({ + schemaVersion: 2, + id: "my_first_trace", + origins: [{ id: "author", kind: "authored" }], + entities: [{ id: "counter", label: "Counter" }], + relations: [], + initialState: { counter: { status: "known", value: 0 } }, + steps: [{ + id: "increment", + label: "Increment counter", + events: [{ + id: "counterWrite", + type: "write", + originId: "author", + entityId: "counter", + path: [], + value: { status: "known", value: 1 }, + }], + }], +}, null, 2); + +/** Accept a single pasted Markdown fence, without treating arbitrary Markdown as code. */ +export function unwrapThlFence(value: string): string { + const trimmed = value.trim(); + const match = /^(~{3,}|`{3,})thl[ \t]*\r?\n([\s\S]*?)\r?\n\1[ \t]*$/.exec(trimmed); + return match ? match[2] : value; +} + +export function compileWorkbenchSource(source: string): WorkbenchResult { + try { + const parsed = parseThlBody(source); + if (parsed.kind === "intent") { + return { + kind: "intent", + message: "This is descriptive intent, not runnable JSON. To visualize prose, materialize a schema-2 artifact with the CLI and approve its sidecar in your content repository.", + }; + } + // Always compile, not merely parse. This catches atomic write conflicts, + // sent-message presence errors and expanded-work budget violations. + return { kind: "ready", trace: compileArtifact(parsed.value) }; + } catch (error) { + if (error instanceof ArtifactValidationError) { + return { + kind: "invalid", + path: error.path, + message: error.message.startsWith(error.path + ": ") + ? error.message.slice(error.path.length + 2) + : error.message, + }; + } + if (error instanceof ThroughlineAuthoringError) { + return { kind: "invalid", code: error.code, message: error.message }; + } + return { kind: "invalid", message: "Unable to read this artifact. Check the JSON and try again." }; + } +} + +/** Only successful compiler output can be formatted or exported as an artifact. */ +export function formatWorkbenchSource(result: WorkbenchResult): string | null { + return result.kind === "ready" ? JSON.stringify(result.trace.artifact, null, 2) : null; +} + +export function markdownForSource(source: string): string { + return "```thl\n" + source.trimEnd() + "\n```\n"; +} From 4b0fe9b369b97b6c3e1c98c27b57b15de61e69ae Mon Sep 17 00:00:00 2001 From: "Christopher S. Aondona" Date: Sun, 15 Nov 2026 21:11:05 +0100 Subject: [PATCH 02/18] feat(playground): replace reference viewer with public authoring studio --- playground/main.tsx | 538 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 443 insertions(+), 95 deletions(-) diff --git a/playground/main.tsx b/playground/main.tsx index efd5fa52..2c80a5e3 100644 --- a/playground/main.tsx +++ b/playground/main.tsx @@ -1,118 +1,466 @@ -import { StrictMode, useState } from "react"; +import { StrictMode, useEffect, useMemo, useRef, useState } from "react"; +import type { ReactNode } from "react"; import { createRoot } from "react-dom/client"; -import { compileArtifact, serializeArtifact } from "../src/index.js"; import { EXAMPLES } from "../src/examples.js"; import { ThroughlineGraphPlayer } from "../src/react/index.js"; +import { + STARTER_SOURCE, + compileWorkbenchSource, + formatWorkbenchSource, + markdownForSource, + unwrapThlFence, +} from "./workbench.js"; import "../src/styles/throughline.css"; import "./style.css"; type Theme = "auto" | "light" | "dark"; -type Viewport = "desktop" | "tablet" | "mobile"; +type Viewport = "wide" | "tablet" | "mobile"; +type MobilePanel = "editor" | "preview"; +type IconName = "arrow" | "book" | "check" | "chevron" | "code" | "copy" | + "download" | "error" | "expand" | "file" | "plus" | "refresh" | "warning"; -const examples = EXAMPLES.map(example => ({ +const REFERENCES = EXAMPLES.map(example => ({ ...example, - trace: compileArtifact(example.artifact), - source: "~~~thl\n" + serializeArtifact(example.artifact) + "\n~~~", + source: JSON.stringify(example.artifact, null, 2), })); +function Icon({ name, size = 18 }: { name: IconName; size?: number }) { + return ( + + ); +} + +function BrandMark() { + return ( + + ); +} + +/** The highlight layer is presentation only. The textarea is the accessible editor. */ +function highlightJson(source: string): ReactNode { + if (source.length > 120_000) return source; + const tokens: ReactNode[] = []; + const pattern = /"(?:\\.|[^"\\])*"(?=\s*:)|"(?:\\.|[^"\\])*"|\b(?:true|false|null)\b|-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|[{}\[\]:,]/g; + let previous = 0; + let match: RegExpExecArray | null; + let count = 0; + while ((match = pattern.exec(source)) !== null) { + if (match.index > previous) tokens.push(source.slice(previous, match.index)); + const value = match[0]; + let role = "punctuation"; + if (value.startsWith('"')) { + role = source.slice(pattern.lastIndex).trimStart().startsWith(":") ? "key" : "string"; + } else if (/^(true|false|null)$/.test(value)) role = "boolean"; + else if (/^-?\d/.test(value)) role = "number"; + tokens.push({value}); + previous = pattern.lastIndex; + } + if (previous < source.length) tokens.push(source.slice(previous)); + return tokens; +} + +function cursorLabel(value: string, index: number): string { + const before = value.slice(0, index); + const line = before.split("\n").length; + return "Ln " + line + ", Col " + (before.length - before.lastIndexOf("\n")); +} + +function downloadArtifact(id: string, json: string) { + const blob = new Blob([json + "\n"], { type: "application/json;charset=utf-8" }); + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = id + ".json"; + document.body.append(link); + link.click(); + link.remove(); + window.setTimeout(() => URL.revokeObjectURL(url), 0); +} + function App() { - const [exampleId, setExampleId] = useState("factorial"); + const [source, setSource] = useState(REFERENCES[0].source); + const [compiledSource, setCompiledSource] = useState(REFERENCES[0].source); + const [referenceChoice, setReferenceChoice] = useState(REFERENCES[0].id); + const [loadedReferenceId, setLoadedReferenceId] = useState(REFERENCES[0].id); + const [loadedSource, setLoadedSource] = useState(REFERENCES[0].source); const [theme, setTheme] = useState("auto"); - const [viewport, setViewport] = useState("desktop"); - const current = examples.find(example => example.id === exampleId); + const [viewport, setViewport] = useState("wide"); + const [mobilePanel, setMobilePanel] = useState("editor"); + const [cursor, setCursor] = useState("Ln 1, Col 1"); + const [notice, setNotice] = useState(""); + const highlightRef = useRef(null); + const editorRef = useRef(null); + + // Compile only the settled edit, and never execute or fetch its contents. + useEffect(() => { + const timeout = window.setTimeout(() => setCompiledSource(source), 260); + return () => window.clearTimeout(timeout); + }, [source]); + const result = useMemo(() => compileWorkbenchSource(compiledSource), [compiledSource]); + const syntax = useMemo(() => highlightJson(source), [source]); + const checking = source !== compiledSource; + const ready = !checking && result.kind === "ready"; + const loadedReference = loadedReferenceId && source === loadedSource + ? REFERENCES.find(item => item.id === loadedReferenceId) + : undefined; + const modified = source !== loadedSource; + const lineCount = source.split("\n").length; + + function edit(next: string) { + setSource(next); + setNotice(""); + } + + function replaceWith(next: string, referenceId: string | null) { + if (modified && !window.confirm("Replace your current edits? Unsaved changes will be lost.")) return; + setSource(next); + setCompiledSource(next); + setLoadedSource(next); + setLoadedReferenceId(referenceId); + setNotice(""); + setCursor("Ln 1, Col 1"); + if (editorRef.current) { + editorRef.current.scrollTop = 0; + editorRef.current.scrollLeft = 0; + } + if (highlightRef.current) { + highlightRef.current.scrollTop = 0; + highlightRef.current.scrollLeft = 0; + } + } + + function format() { + const current = compileWorkbenchSource(source); + const formatted = formatWorkbenchSource(current); + if (formatted === null) { + setNotice("Fix validation errors before formatting."); + setCompiledSource(source); + return; + } + edit(formatted); + setCompiledSource(formatted); + setNotice("Formatted schema-2 JSON."); + } + + async function copySnippet() { + const current = compileWorkbenchSource(source); + if (current.kind !== "ready") { + setNotice("Only validated artifacts can be copied as thl snippets."); + setCompiledSource(source); + return; + } + try { + await navigator.clipboard.writeText(markdownForSource(source)); + setNotice("Validated thl snippet copied."); + } catch { + setNotice("Clipboard access was denied. Select the editor text to copy it manually."); + } + } + + function download() { + const current = compileWorkbenchSource(source); + const json = formatWorkbenchSource(current); + if (!json || current.kind !== "ready") { + setNotice("Fix validation errors before exporting."); + setCompiledSource(source); + return; + } + downloadArtifact(current.trace.artifact.id, json); + setNotice("Validated JSON exported."); + } return ( -
-
-
-

Throughline / schema-2 development preview

-

Reference evidence and player review

-

- Explore independently checked, authored models. Graphs and sequences - reveal declared evidence; they do not execute source code or infer - undocumented events. -

-
- -
- - - +
+ Skip to editor +
+ +
+ +
+
+
+
Interactive visualization studio
+

Make state visible.

+

Author a trace, validate its semantics, and explore each change as a graph, sequence, or state table. Start with a reference or build your own.

+
+
+
+
Built for claritySchema 2 · Runs in your browser
+
-
- {current ? ( - - ) : null} -
+
+
+
+ +
+
+ + +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+ +
+
+
+
+

Source editor

Schema-2 artifact JSON

+
+ artifact.json +
+
+ JSON / thl +
+ + + +
+
+
+ +