diff --git a/crates/conformance/tests/hosted_source_permission.rs b/crates/conformance/tests/hosted_source_permission.rs index 1cb237f1..16699ae4 100644 --- a/crates/conformance/tests/hosted_source_permission.rs +++ b/crates/conformance/tests/hosted_source_permission.rs @@ -21,8 +21,8 @@ fn hosted_source_permission_fa_83() { String::from_utf8_lossy(&output.stderr) ); for required in [ - "# tests 18", - "# pass 18", + "# tests 12", + "# pass 12", "# fail 0", "# cancelled 0", "# skipped 0", diff --git a/studio/shell/host.js b/studio/shell/host.js index 4922f57c..4c5daf97 100644 --- a/studio/shell/host.js +++ b/studio/shell/host.js @@ -1717,16 +1717,7 @@ async function chatTurn(chatId, intent) { let provenance = null; const committed = []; const onCommit = (oid) => { committed.push(oid); updatedFiles = true; emit("version-created", { appId, oid }); }; - let hostedSource = projectSource, hostedSession = null; try { - if (execution.sourceEffects) hostedSession = appShape.createHostedSourceSession({ - appId, turnId: intent.intentId, source: projectSource.reference, signal: abort.signal, - assertCurrent: async () => { - assertGenerationActive(abort.signal); - await assertBuildSourceCurrent(await dbReady, appId, hostedSource); - assertGenerationActive(abort.signal); - }, - }); // R4 decides the route before the request is made: a route to any model outside Foundry is // ungranted, so it is refused here and no byte of the request leaves the tab. const before = r4Request({ provider }); @@ -1779,7 +1770,7 @@ async function chatTurn(chatId, intent) { // T6: completed batches have their own committed snapshots. // A streamed parser still owns the pre-turn snapshot until it settles. Finish it before our // own transaction changes that snapshot, so ownership checks cannot mistake our commit for an edit. - if (execution.sourceEffects && hostedSession) await settleAppFrames(appId, abort.signal); + if (execution.sourceEffects) await settleAppFrames(appId, abort.signal); assertGenerationActive(abort.signal); const sm = full.match(/([\s\S]*?)<\/dyad-chat-summary>/); summary = sm ? sm[1].trim() : undefined; if (summary && (!chat.title || chat.title === "New chat")) { await updateStoredChat(await dbReady, chatId, { title: summary }); } @@ -1790,24 +1781,22 @@ async function chatTurn(chatId, intent) { const tags = { ...wrote, writes: [], deletes: [], renames: [], deps: [] }; const applied = await applyPrismTags(appId, tags, { signal: abort.signal, source: projectSource, onCommit }); let changed = applied.changed; - hostedSource = applied.source; const turn = full; - const spec = (await appAfterTurn(appId, turn, { signal: abort.signal, session: hostedSession, source: applied.source, onCommit, check: false })) || await rulesAfterTurn(appId, turn, [], { signal: abort.signal, source: applied.source, onCommit }); - hostedSource = spec.source; + const spec = (await appAfterTurn(appId, turn, { signal: abort.signal, source: applied.source, onCommit, check: false })) || await rulesAfterTurn(appId, turn, [], { signal: abort.signal, source: applied.source, onCommit }); const patchRefusals = []; for (const match of full.matchAll(/([\s\S]*?)<\/dyad-spec-patch>/g)) { assertGenerationActive(abort.signal); let ops; try { ops = JSON.parse(match[1]); } catch {} const list = ops ? (Array.isArray(ops) ? ops : [ops]) : []; const result = ops ? await patchSpec(appId, list, { author: "agent", signal: abort.signal, source: spec.source, onCommit }) : { ok: false, issues: ["The patch is not JSON."] }; - if (result.ok) { spec.changed += result.changed; spec.source = result.source; hostedSource = spec.source; spec.card = result.card; continue; } + if (result.ok) { spec.changed += result.changed; spec.source = result.source; spec.card = result.card; continue; } // One op the checker refuses does not cost the others (measured: GLM's meanings, keyed by field, took // its heading, labels and words with it): they are applied one by one, and only the refused are named. const refused = list.length > 1 ? [] : [result.issues[0]]; if (list.length > 1) for (const op of list) { assertGenerationActive(abort.signal); const one = await patchSpec(appId, [op], { author: "agent", signal: abort.signal, source: spec.source, onCommit }); - if (one.ok) { spec.changed += one.changed; spec.source = one.source; hostedSource = spec.source; spec.card = one.card; } else { const label=Array.isArray(op?.path)&&op.path.every(p=>typeof p==="string"||Number.isSafeInteger(p))?op.path.join("."):"patch";refused.push(`${label || "patch"}: ${one.issues[0]}`); } + if (one.ok) { spec.changed += one.changed; spec.source = one.source; spec.card = one.card; } else { const label=Array.isArray(op?.path)&&op.path.every(p=>typeof p==="string"||Number.isSafeInteger(p))?op.path.join("."):"patch";refused.push(`${label || "patch"}: ${one.issues[0]}`); } } patchRefusals.push(...refused); } @@ -1821,7 +1810,7 @@ async function chatTurn(chatId, intent) { if (phase && phase !== "drafting") { tmMark("read", tm.repaired ? 2 : 1); tmMark("checked"); } else tmMark("refused"); } changed += spec.changed; - if (await hasRules(appId)) { await showLive(appId).catch(() => {}); await refreshPreview(appId, { signal: abort.signal, session: hostedSession }); } + if (await hasRules(appId)) { await showLive(appId).catch(() => {}); await refreshPreview(appId, { signal: abort.signal }); } else if (speculativeLive && liveShown.has(appId) && !(await scaffoldOnly(appId))) { liveShown.delete(appId); showBuilt(appId).catch(() => {}); } updatedFiles = updatedFiles || changed > 0; const commit = committed.at(-1) || null; @@ -1837,9 +1826,8 @@ async function chatTurn(chatId, intent) { await saveMessages(chatId, messages); emit("chat:response:chunk", { chatId, invocationRef: ref, messages }); } finally { - try { if (hostedSession && !abort.signal.aborted) await settleAppFrames(appId, abort.signal); } + try { if (execution.sourceEffects && !abort.signal.aborted) await settleAppFrames(appId, abort.signal); } catch (e) { if (!abort.signal.aborted) error ||= String((e && e.message) || e); } - finally { hostedSession?.close(); } } m.abort = null; const outcome = abort.signal.aborted ? "cancelled" : error ? "errored" : "completed"; diff --git a/studio/shell/live/app.mjs b/studio/shell/live/app.mjs index 637d66d8..5f64c486 100644 --- a/studio/shell/live/app.mjs +++ b/studio/shell/live/app.mjs @@ -1,9 +1,9 @@ // The live preview of a stateful app: a LexLean module in the app shape (State, catalogue, initial, // view, step) is run as it is written, and each frame it produces is posted to the live frame. // -// lexlean.mjs reads one shape, a decision. This reads the other. Nothing the model writes is code: -// the module's data form (lexlean/semantic-module/2, from the hosted LexLean service) is interpreted -// by v2/interp.mjs, and the screen is a PrismPM browser presentation, a closed set of content kinds. +// lexlean.mjs reads one shape, a decision. This reads the other. Nothing written in a turn is code: the +// module's data form (lexlean/semantic-module/2) is read here, in the tab, and interpreted by v2/interp.mjs; +// the screen is a PrismPM browser presentation, a closed set of content kinds. No server is asked. // // A frame drawn from here is a draft. It says nothing about the spec being checked or proved. import { loadProject, NIL, cons } from "./v2/interp.mjs"; @@ -14,8 +14,7 @@ import { isStatefulModule as isAppShape } from '../application-shape.mjs'; import { reuseSentence } from "../prism/proof-reuse.mjs"; export { isAppShape }; -import { HOSTED_SOURCE_ENDPOINT as HOSTED, withHostedSourcePermission } from "../hosted-source-permission.mjs"; -export { createHostedSourceSession } from "../hosted-source-permission.mjs"; +import { withHostedSourcePermission } from "../hosted-source-permission.mjs"; const PROFILE = "prism-app"; const MODEL = "Foundation.View.Browser.V1.Model"; const OPEN = "\\begin{semanticmodule}", CLOSE = "\n\\end{semanticmodule}\n\\end{lexlean}\n"; @@ -27,7 +26,7 @@ export const STATE_PATH = "prism/app.json"; export function card(state) { if (state.phase === "proving") return "**App** · Lean is proving\n\nLean is checking this app's properties on the hosted verifier."; if (state.phase === "proved") return "**App** · proved by Lean\n\nLean proved " + (state.theorems || []).length + " theorem" + ((state.theorems || []).length === 1 ? "" : "s") + " about this app. Attestation `" + String(state.proof && state.proof.attestation_id || "").slice(0, 16) + "…`. It cannot be published yet."; - if (state.phase === "checked") return "**App** · checked\n\nThe checker accepts this app's module. Preview parsing uses separately approved hosted access. It is not proved yet, and it cannot be published yet."; + if (state.phase === "checked") return "**App** · checked\n\nThe checker accepts this app's module. It is not proved yet, and it cannot be published yet."; if (state.issues && state.issues.length) return "**App** · not checked\n\n" + state.issues.map((i) => "- " + i).join("\n") + "\n\nIt still runs in the preview as far as it can. Ask me to fix the module."; return CARD + (state.unchecked ? "\n\nIt could not be checked just now: " + state.unchecked : ""); } @@ -109,7 +108,7 @@ export const SOURCE_PATH = "prism/App.lex.tex"; /** Where the lines a model wrote for it are kept: only a shorter way to show a model the same module. */ export const NOTATION_PATH = "prism/app.notation"; /** What the chat shows once a turn has written one. It claims no more than is true. */ -export const CARD = "**App** · draft\n\nThis app is stored as a LexLean module. Preview parsing and checking require approval to send source to mcp.uor.foundation. It is not checked or proved yet, and it cannot be published yet."; +export const CARD = "**App** · draft\n\nThis app is stored as a LexLean module. It runs in the preview in this browser. It is not checked or proved yet, and it cannot be published yet."; /** What a model is told about the app shape, after the instructions for a decision. */ export const PROMPT = "\n\n# When the app keeps state: the app notation\nA decision module answers one request. If what is asked for is used over several steps (a list that grows, entries that are kept, a table of results), the shorthand above does not apply. " + notation.PROMPT; /** What a model is shown of a stateful app: its module as it stands. */ @@ -171,18 +170,64 @@ export function wholePrefix(module) { return { ready, interactive: names.includes("step"), names, source: ready ? header + "\n" + kept.map((d) => d.source).join("\n\n") + CLOSE : null }; } -/** The data form of one module, from the hosted service. It parses; it does not check. */ -export async function dataForm(source, fetchImpl = globalThis.fetch, options = {}) { - const files = [{ path: "src/App.lex.tex", content: source }]; - return withHostedSourcePermission({ purpose: "parse", files }, async (permittedFetch) => { - let id = 0; - const call = async (name, args) => { - const res = await permittedFetch(HOSTED, { method: "POST", body: JSON.stringify({ jsonrpc: "2.0", id: ++id, method: "tools/call", params: { name, arguments: args } }) }); - return (await res.json()).result.structuredContent; - }; - const draft = await call("lexlean_draft_write", { profile: PROFILE, files }); - return (await call("lexlean_draft_read", { draft: draft.draft, path: "src/App.lex.tex", view: "data" })).data; - }, options, fetchImpl); +/** The data form of one module, read in the tab: LexLean's semantic terms, as `lexlean` itself reads them + * (tools/live_notation.test.mjs holds every notation fixture to LexLean 0.4's own data form). Theorems are + * left out: the preview runs definitions, and Lean proves the theorems from the source. */ +const OPS = { "+": "add", "&&": "and", "||": "or", "=?": "beq", "<=?": "ble", "|::|<=\?|<\?|=\?|&&|\|\||[()[\]{},:|+=]|[A-Za-z_][\w.]*(?:::[\w.]+)?|\d+)/y; +export function semanticData(source) { + const body = String(source).match(/\\begin\{semanticmodule\}([\s\S]*)\\end\{semanticmodule\}/); + if (!body) throw new Error("no semantic module"); + const t = []; TOKEN.lastIndex = 0; let m; + const text = body[1]; + while (TOKEN.lastIndex < text.trimEnd().length && (m = TOKEN.exec(text))) t.push(m[1]); + if (TOKEN.lastIndex < text.trimEnd().length) throw new Error("unreadable at " + text.slice(TOKEN.lastIndex, TOKEN.lastIndex + 20)); + let i = 0; + const peek = () => t[i], next = () => t[i++], take = (s) => { if (t[i] !== s) throw new Error(`expected ${s}, read ${t[i]}`); i++; }; + const member = (s) => { const at = s.indexOf("::"); return at < 0 ? { name: s } : { module: s.slice(0, at), name: s.slice(at + 2) }; }; + const type = () => { + if (peek() === "(") { take("("); take("List"); const element = type(); take(")"); return { kind: "list", element }; } + const n = next(); return ({ Nat: { kind: "nat" }, String: { kind: "string" }, Bool: { kind: "bool" }, Bytes: { kind: "bytes" } })[n] || { kind: "named", member: member(n), arguments: [] }; + }; + const term = () => { + const a = next(); + if (a[0] === '"') return { kind: "string", value: JSON.parse(a) }; + if (/^\d/.test(a)) return { kind: "nat", value: a }; + if (a === "true" || a === "false") return { kind: "bool", value: a === "true" }; + if (a !== "(") return { kind: "var", name: a }; + const h = next(); let out; + if (h === "record") { const type = member(next()), fields = []; take("{"); while (peek() !== "}") { const field = next(); take(":="); fields.push({ field, value: term() }); if (peek() === ",") next(); } take("}"); out = { kind: "record", type, fields }; } + else if (h === "ctor") { const constructor = member(next()), args = []; while (peek() !== ")") args.push(term()); out = { kind: "constructor", constructor, arguments: args }; } + else if (h === "if") { const condition = term(); take("then"); const then_value = term(); take("else"); out = { kind: "if", condition, then_value, else_value: term() }; } + else if (h === "match") { const scrutinee = term(), branches = []; while (peek() === "|") { next(); const constructor = member(next()), binders = []; while (peek() !== "=>") binders.push(next()); take("=>"); branches.push({ constructor, binders, body: term() }); } out = { kind: "match", scrutinee, branches }; } + else if (h === "project") { const value = term(); out = { kind: "project", value, field: next() }; } + else if (h === "prim") { const operation = next(), args = []; while (peek() !== ":") args.push(term()); take(":"); out = { kind: "primitive", operation, arguments: args, result: type() }; } + else if (h === "cons") out = { kind: "cons", head: term(), tail: term() }; + else if (h === "nil") out = { kind: "nil", element: type() }; + else { i--; if (t[i] === "(" || !(t[i + 1] in OPS)) { if (t[i] !== "(") { const fn = next(), args = []; while (peek() !== ")") args.push(term()); out = { kind: "call", function: member(fn), arguments: args }; } } if (!out) { const left = term(), op = next(); out = { kind: OPS[op], left, right: term() }; } } + take(")"); return out; + }; + const list = (item) => { const out = []; take("["); while (peek() !== "]") { out.push(item()); if (peek() === ",") next(); } take("]"); return out; }; + const binder = () => { const name = next(); take(":"); return { name, type: type() }; }; + const declarations = []; + while (i < t.length) { + const kind = next(); take("{"); const d = { kind }; + while (peek() !== "}") { + const key = next(); take(":"); + if (kind === "theorem") { let depth = 0; while (!(depth === 0 && (peek() === "," || peek() === "}"))) { const s = next(); if ("([{".includes(s)) depth++; else if (")]}".includes(s)) depth--; } } + else if (key === "name" || key === "recursive_argument") d[key] = next(); + else if (key === "parameters") d.parameters = list(binder); + else if (key === "type_parameters") d.type_parameters = list(next); + else if (key === "fields") d.fields = list(() => { take("{"); take("name"); take(":"); const name = next(); take(","); take("type"); take(":"); const f = { name, type: type() }; take("}"); return f; }); + else if (key === "result") d.result = type(); + else if (key === "body") d.body = term(); + else throw new Error("unknown key " + key); + if (peek() === ",") next(); + } + take("}"); + if (kind !== "theorem") declarations.push(d); + } + return { spec: "lexlean/semantic-module/2", declarations }; } const list = (items) => items.reduceRight((tail, head) => cons(head, tail), NIL); @@ -217,7 +262,7 @@ export function createApp(data, library, name = "App") { * Follows the modules a chat turn streams for each app and posts frames as they become runnable. * `post(appId, message)` delivers a `live/presentation` message to that app's frame. */ -export function appLive({ post, library, fetchImpl = globalThis.fetch, now = () => performance.now(), permissionOptions = {} }) { +export function appLive({ post, library, now = () => performance.now() }) { const apps = new Map(); // Each wanted/attempted identity includes source and its permission scope. const entry = (appId) => { if (!apps.has(appId)) apps.set(appId, { app: null, last: null, wanted: null, attempted: null, busy: false, trust: "draft", frameSource: null, note: null, controller: null }); return apps.get(appId); }; const send = (appId) => { @@ -236,8 +281,8 @@ export function appLive({ post, library, fetchImpl = globalThis.fetch, now = () options.signal?.addEventListener("abort", abort, { once: true }); if (options.signal?.aborted) abort(); try { - const app = createApp(await dataForm(source, fetchImpl, { ...permissionOptions, ...options, signal: controller.signal }), library); - if (controller.signal.aborted || apps.get(appId) !== a || a.wanted !== requested) throw new Error("Hosted preview was superseded"); + const app = createApp(semanticData(source), library); + if (controller.signal.aborted || apps.get(appId) !== a || a.wanted !== requested) throw new Error("Preview was superseded"); const frame = app.frame(); a.app = app; a.last = { ...frame, ms: Math.round(now() - t0) }; a.frameSource = source; a.trust = requested.trust; a.note = null; } catch (e) { diff --git a/studio/tools/hosted_source_permission.test.mjs b/studio/tools/hosted_source_permission.test.mjs index f1c98621..42a400fa 100644 --- a/studio/tools/hosted_source_permission.test.mjs +++ b/studio/tools/hosted_source_permission.test.mjs @@ -1,12 +1,11 @@ import {test} from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; -import {dataForm, checkApp, checkLibrary, appLive, splitDeclarations, wholePrefix} from '../shell/live/app.mjs'; +import {checkApp, checkLibrary, splitDeclarations, wholePrefix} from '../shell/live/app.mjs'; import {authorizeHostedSource, describeHostedSource, createHostedSourceSession, withHostedSourcePermission, hostedSourceFailure, HOSTED_SOURCE_ENDPOINT} from '../shell/hosted-source-permission.mjs'; const read = path => fs.readFileSync(new URL(path, import.meta.url), 'utf8'); const module = read('../../v2/loop/answers/guests-3.verified.lex.tex'); const whole = JSON.parse(read('../../v2/loop/answers/guests-3.json')); -const library = {'Foundation.View.Browser.V1.Model': JSON.parse(read('../shell/live/v2/Model.json'))}; const files = [{path:'src/App.lex.tex',content:''}]; const input = {scope:'source',purpose:'parse',files}; const code = value => error => error.code === value; @@ -29,6 +28,8 @@ function service() { }; return {fetch,requests}; } +// A parse through the boundary itself: the preview reads its data form in the tab, so only these tests send one. +const dataForm=(source,fetchImpl,opts={})=>{const sent=[{path:'src/App.lex.tex',content:source}];return withHostedSourcePermission({purpose:'parse',files:sent},async f=>{const draft=(await (await f(HOSTED_SOURCE_ENDPOINT,rpc('lexlean_draft_write',{profile:'prism-app',files:sent}))).json()).result.structuredContent.draft;if(typeof draft!=='string'||!draft)throw hostedSourceFailure('hostedSourceMalformedResponse');return draft;},opts,fetchImpl);}; const rpc=(name,args)=>({method:'POST',body:JSON.stringify({jsonrpc:'2.0',id:1,method:'tools/call',params:{name,arguments:args}})}); function session(boundary={}) {const controller=new AbortController();let current=true;const scope=createHostedSourceSession({appId:7,turnId:'turn-one',source:'blake3:'+'a'.repeat(64),signal:controller.signal,assertCurrent:async()=>current},{...options,...boundary});return {scope,controller,change:()=>{current=false;}};} @@ -108,76 +109,8 @@ test('cancel and bounded deadline interrupt in flight requests even when fetch i await seen;if(action==='cancel')one.controller.abort();await rejection;one.scope.close(); } }); -test('streamed previews retain incremental frames with one explicit turn scope',async()=>{ - const transport=service(),posted=[];let prompts=0;const one=session({authorize:async value=>{prompts++;return approval(value);}}); - const live=appLive({library,fetchImpl:transport.fetch,post:(_app,frame)=>posted.push(frame)}); - try{const viewEnd=module.indexOf('definition{\n name: textOf'),stepEnd=module.indexOf('definition{\n name: promises'); - live.push(7,module.slice(0,viewEnd),'draft',{session:one.scope});await live.settled(7);assert.equal(posted.at(-1).interactive,false); - live.push(7,module.slice(0,stepEnd),'draft',{session:one.scope});await live.settled(7);assert.equal(posted.at(-1).interactive,true);assert.equal(prompts,1); - }finally{one.scope.close();} -}); -test('cancelled forgotten or replaced preview requests cannot publish late frames',async()=>{ - for(const action of ['cancel','forget','replace']){ - const posted=[],transport=service(),one=session();let release,started;const seen=new Promise(resolve=>started=resolve);let first=true; - const fetch=async(...args)=>{if(first){first=false;started();await new Promise(resolve=>release=resolve);}return transport.fetch(...args);}; - const live=appLive({library,fetchImpl:fetch,post:(_app,frame)=>posted.push(frame)}); - try{live.push(7,module,'draft',{session:one.scope});await seen; - if(action==='cancel')one.controller.abort();if(action==='forget')live.forget(7);if(action==='replace')live.push(7,module.replace('Guests','Visitors'),'draft',{session:one.scope}); - release();await live.settled(7);assert.equal(posted.length,action==='replace'?1:0); - }finally{one.scope.close();} - } -}); -test('denial keeps the previous frame at its own trust and settles without retry loops',async()=>{ - const posted=[],transport=service(),live=appLive({library,fetchImpl:transport.fetch,post:(_app,frame)=>posted.push(frame),permissionOptions:options}); - live.push(7,module);await live.settled(7);assert.equal(posted.length,1); - const denied=session({authorize:async()=>{throw hostedSourceFailure('hostedSourcePermissionDenied');}}); - try{live.push(7,module.replace('Guests','Visitors'),'checked',{session:denied.scope});await live.settled(7);assert.equal(posted.at(-1).trust,'draft');assert.match(posted.at(-1).note,/declined/);}finally{denied.scope.close();} -}); -test('production host shares owned session across streaming repair checking and final refresh',()=>{ +test('a build turn sends no source: its preview is read in the tab, and only an approval asks to check',()=>{ const host=read('../shell/host.js'),app=read('../shell/live/app.mjs'); - for(const text of ['appShape.createHostedSourceSession(', 'assertBuildSourceCurrent(await dbReady, appId, hostedSource)', 'session: hostedSession', 'hostedSession?.close()'])assert.ok(host.includes(text),text); - assert.ok(app.includes('withHostedSourcePermission({ purpose: "check", files }'));assert.ok(app.includes('withHostedSourcePermission({ purpose: "parse", files }')); + assert.ok(!host.includes('createHostedSourceSession(')&&!app.includes('purpose: "parse"'));assert.ok(app.includes('withHostedSourcePermission({ purpose: "check", files }'));assert.ok(app.includes('createApp(semanticData(source), library)')); assert.ok(read('../../app/src/FoundrySpecification.lex.tex').includes('name: hostedSourcePermissionBoundary,'));assert.ok(read('./shell_parse.mjs').includes('"hosted-source-permission.mjs": "mjs"')); }); -test('identical source retries only when a fresh scope replaces a refused or failed attempt',async()=>{ - for(const failure of ['denied','network']){ - let approvals=0;const transport=service(),posted=[],denied=session({authorize:async value=>{approvals++;if(failure==='denied')throw hostedSourceFailure('hostedSourcePermissionDenied');return approval(value);}}); - let failTransport=failure==='network';const fetch=async(...args)=>{if(failTransport)throw Error('private offline detail');return transport.fetch(...args);}; - const live=appLive({library,fetchImpl:fetch,post:(_app,frame)=>posted.push(frame)}); - const granted=session({authorize:async value=>{approvals++;return approval(value);}}); - try{ - live.push(7,module,'draft',{session:denied.scope});await live.settled(7);assert.equal(posted.length,0); - live.push(7,module,'draft',{session:denied.scope});await live.settled(7);assert.equal(approvals,1,'same failed scope must not repeatedly prompt'); - failTransport=false;live.push(7,module,'checked',{session:granted.scope});await live.settled(7); - assert.equal(approvals,2,'new scope must authorize identical source again');assert.equal(posted.length,1);assert.equal(posted[0].trust,'checked');assert.equal(transport.requests.length,2); - }finally{denied.scope.close();granted.scope.close();} - } -}); -test('reselecting a rendered module cancels pending different source and never relabels its late frame',async()=>{ - const transport=service(),posted=[],one=session();let hold=false,release,started;const seen=new Promise(resolve=>started=resolve); - const fetch=async(...args)=>{if(hold){hold=false;started();await new Promise(resolve=>release=resolve);}return transport.fetch(...args);}; - const live=appLive({library,fetchImpl:fetch,post:(_app,frame)=>posted.push(frame)}); - try{ - live.push(7,module,'checked',{session:one.scope});await live.settled(7);const first=structuredClone(posted[0]); - hold=true;live.push(7,module.replace('Guests','Visitors'),'draft',{session:one.scope});await seen; - live.push(7,module,'proved',{session:one.scope});assert.equal(posted.length,2);assert.deepEqual(posted[1].nodes,first.nodes);assert.equal(posted[1].trust,'proved'); - release();await live.settled(7);assert.equal(posted.length,2,'late B must not post after A was reselected'); - assert.deepEqual(posted.at(-1).nodes,first.nodes);assert.equal(posted.at(-1).trust,'proved');assert.equal(posted.at(-1).note,null); - }finally{one.scope.close();} -}); -test('same-source owner replacement and incomplete-source replacement suppress superseded work',async()=>{ - for(const action of ['scope','signal','incomplete']){ - const transport=service(),posted=[],one=session(),two=session();let release,started,hold=true;const seen=new Promise(resolve=>started=resolve); - const fetch=async(...args)=>{if(hold){hold=false;started();await new Promise(resolve=>release=resolve);}return transport.fetch(...args);}; - const live=appLive({library,fetchImpl:fetch,post:(_app,frame)=>posted.push(frame)}),first=new AbortController(),second=new AbortController(); - try{ - live.push(7,module,'draft',{session:one.scope,signal:first.signal});await seen; - if(action==='scope')live.push(7,module,'checked',{session:two.scope,signal:first.signal}); - if(action==='signal')live.push(7,module,'checked',{session:one.scope,signal:second.signal}); - if(action==='incomplete')assert.equal(live.push(7,'\\begin{lexlean}{App}','proved',{session:one.scope,signal:first.signal}),false); - release();await live.settled(7); - assert.equal(posted.length,action==='incomplete'?0:1); - if(action!=='incomplete')assert.equal(posted[0].trust,'checked'); - }finally{one.scope.close();two.scope.close();} - } -}); diff --git a/studio/tools/hosted_source_permission_browser.mjs b/studio/tools/hosted_source_permission_browser.mjs index 75b92a57..9c8a3913 100644 --- a/studio/tools/hosted_source_permission_browser.mjs +++ b/studio/tools/hosted_source_permission_browser.mjs @@ -32,7 +32,10 @@ try{ if(url.origin==='https://unapproved.invalid')redirected++; if(url.origin===base)return route.continue();return route.abort(); }); - await page.goto(base);await page.evaluate(async()=>{window.api=await import('/live/app.mjs');window.permission=await import('/hosted-source-permission.mjs');}); + await page.goto(base);await page.evaluate(async()=>{const permission=window.permission=await import('/hosted-source-permission.mjs'); + // A parse through the boundary: the shell's preview reads its data form in the tab, so only this suite sends one. + const call=(f,name,args)=>f(permission.HOSTED_SOURCE_ENDPOINT,{method:'POST',body:JSON.stringify({jsonrpc:'2.0',id:1,method:'tools/call',params:{name,arguments:args}})}).then(r=>r.json()).then(r=>r.result.structuredContent); + window.api={createHostedSourceSession:permission.createHostedSourceSession,dataForm:(source,fetchImpl,opts)=>{const files=[{path:'src/App.lex.tex',content:source}];return permission.withHostedSourcePermission({purpose:'parse',files},async f=>{const {draft}=await call(f,'lexlean_draft_write',{profile:'prism-app',files});return (await call(f,'lexlean_draft_read',{draft,path:'src/App.lex.tex',view:'data'})).data;},opts,fetchImpl);}};}); const start=async({turn=false}={})=>page.evaluate(turn=>{ window.result=null;window.controller=new AbortController();document.getElementById('prior').focus(); if(turn)window.scope=window.api.createHostedSourceSession({appId:7,turnId:'browser-turn',source:'blake3:'+'a'.repeat(64),signal:controller.signal,assertCurrent:async()=>true}); diff --git a/studio/tools/live_app.test.mjs b/studio/tools/live_app.test.mjs index 07b080fb..93162841 100644 --- a/studio/tools/live_app.test.mjs +++ b/studio/tools/live_app.test.mjs @@ -1,36 +1,18 @@ // Run: node --test studio/tools/live_app.test.mjs // The live preview of a stateful app (shell/live/app.mjs): a module in the app shape, streamed, becomes -// frames. The data forms come from fixtures, so the suite needs no network; one test, skipped unless -// LIVE_APP_HOSTED=1, asks the hosted service and compares its answer with the fixture. +// frames. The data form is read in the tab and held to the one the hosted LexLean service gave (a fixture), +// so the suite needs no network. import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; -import { isAppShape, moduleFromTurn, splitDeclarations, wholePrefix, createApp, appLive, dataForm } from "../shell/live/app.mjs"; +import { isAppShape, moduleFromTurn, splitDeclarations, wholePrefix, createApp, appLive, semanticData } from "../shell/live/app.mjs"; -import { describeHostedSource } from "../shell/hosted-source-permission.mjs"; -// Explicit fixture authorization. No test uses implicit production consent. -const permissionOptions = { authorize: async input => ({ disclosure: describeHostedSource(input), expires: Date.now() + 10000, signal: new AbortController().signal, close() {} }), online: () => true }; const read = (path) => fs.readFileSync(new URL(path, import.meta.url), "utf8"); const module = read("../../v2/loop/answers/guests-3.verified.lex.tex"); const decision = read("../../app/src/ForgeKernel.lex.tex"); const library = { "Foundation.View.Browser.V1.Model": JSON.parse(read("../shell/live/v2/Model.json")) }; const whole = JSON.parse(read("../../v2/loop/answers/guests-3.json")); -// A hosted service stand-in: answers a draft's data form from the declarations the draft holds, so a -// partial module gets a partial data form, as the real service gives. -function service() { - const calls = [], drafts = new Map(); - const fetchImpl = async (url, init) => { - const request = JSON.parse(init.body), args = request.params.arguments; - calls.push(request.params.name); - let payload; - if (request.params.name === "lexlean_draft_write") { const id = "prism-app/" + drafts.size; drafts.set(id, args.files[0].content); payload = { draft: id }; } - else { const names = new Set(splitDeclarations(drafts.get(args.draft)).declarations.map((d) => d.name)); payload = { data: { spec: whole.spec, declarations: whole.declarations.filter((d) => names.has(d.name)) } }; } - return { ok: true, json: async () => ({ result: { structuredContent: payload } }) }; - }; - return { fetchImpl, calls }; -} - test("the app shape is told from the decision shape", () => { assert.equal(isAppShape(module), true); assert.equal(isAppShape(module.slice(0, 400)), true, "from its first lines"); @@ -72,8 +54,8 @@ test("an app runs: frames are plain data, an action moves the state, a stale pre }); test("a streamed module posts a frame as soon as it is runnable, then again when step arrives", async () => { - const { fetchImpl, calls } = service(), posted = []; - const live = appLive({ post: (appId, m) => posted.push(m), library, fetchImpl, permissionOptions, now: () => 0 }); + const posted = []; + const live = appLive({ post: (appId, m) => posted.push(m), library, now: () => 0 }); const viewEnd = module.indexOf("definition{\n name: textOf"), stepEnd = module.indexOf("definition{\n name: promises"); assert.ok(viewEnd > 0 && stepEnd > viewEnd); assert.equal(live.push(7, module.slice(0, 300)), false, "nothing to run yet"); @@ -87,15 +69,15 @@ test("a streamed module posts a frame as soon as it is runnable, then again when live.push(7, module.slice(0, stepEnd)); await live.settled(7); assert.equal(posted.at(-1).interactive, true); - const before = calls.length; + const before = posted.length; live.push(7, module.slice(0, stepEnd) + "\n\ndefinition{\n name: half"); await live.settled(7); - assert.equal(calls.length, before, "an unfinished declaration asks the service nothing"); + assert.equal(posted.length, before + 1, "an unfinished declaration is left out: the frame is the one already drawn"); }); test("a press in the frame is answered with the next frame; a press from an old frame with the current one", async () => { - const { fetchImpl } = service(), posted = []; - const live = appLive({ post: (appId, m) => posted.push(m), library, fetchImpl, permissionOptions }); + const posted = []; + const live = appLive({ post: (appId, m) => posted.push(m), library }); live.push(1, module); await live.settled(1); const frame = posted.at(-1), input = frame.nodes.findIndex((n) => n.kind === "Input") + 1; live.intent(1, { revision: frame.revision, action: 1, fields: [{ node: input, text: "Grace" }] }); @@ -105,22 +87,20 @@ test("a press in the frame is answered with the next frame; a press from an old assert.equal(live.intent(99, { revision: "0", action: 1 }), false, "an app with no frame takes no press"); }); -test("a start that does not run keeps the last frame and says why; a service failure is a note, not a crash", async () => { +test("a start that does not run keeps the last frame and says why; a module that does not read is a note, not a crash", async () => { const posted = []; - const broken = async () => ({ json: async () => ({ result: { isError: true, structuredContent: { diagnostics: [{ message: "syntax" }] }, content: [{ text: "x" }] } }) }); - const live = appLive({ post: (appId, m) => posted.push(m), library, fetchImpl: broken, permissionOptions }); - live.push(3, module); await live.settled(3); + const live = appLive({ post: (appId, m) => posted.push(m), library }); + live.push(3, module.replace("name: view,", "name: view,\n body: (if ),")); await live.settled(3); assert.equal(posted.length, 0, "no frame was ever drawn, so none is posted"); - const { fetchImpl } = service(); - const good = appLive({ post: (appId, m) => posted.push(m), library, fetchImpl, permissionOptions }); + const good = appLive({ post: (appId, m) => posted.push(m), library }); good.push(3, module); await good.settled(3); assert.equal(posted.length, 1); const invalid = { ...whole, declarations: whole.declarations.map((d) => (d.name === "catalogue" ? { ...d, body: { kind: "nil", element: { kind: "string" } } } : d)) }; assert.throws(() => createApp(invalid, library).frame(), /catalogue|admits/); }); -test("the hosted service gives the data form the fixture holds", { skip: process.env.LIVE_APP_HOSTED !== "1" }, async () => { - const data = await dataForm(wholePrefix(module).source, undefined, permissionOptions); +test("the tab reads the data form the hosted service gave", () => { + const data = semanticData(wholePrefix(module).source); const names = (d) => d.declarations.map((x) => x.name); assert.deepEqual(names(data), names(whole).filter((n) => !/^(startsEmpty|clearEmpties|emptyIgnored|longIgnored|staleIgnored)$/.test(n))); assert.deepEqual(createApp(data, library).frame().nodes, createApp(whole, library).frame().nodes); diff --git a/studio/tools/live_main_integration_browser.mjs b/studio/tools/live_main_integration_browser.mjs index b38cfa92..aae71748 100644 --- a/studio/tools/live_main_integration_browser.mjs +++ b/studio/tools/live_main_integration_browser.mjs @@ -43,8 +43,9 @@ try { const stored=path=>page.evaluate(({appId,path})=>window.__host.get('files',appId+':'+path),{appId,path}); const before=await stored('prism/spec.json'); await page.goto(base+'chat?id='+made.chatId+'&appId='+appId);await page.waitForFunction(()=>!!window.electron); - // The app's preview runs on the hosted service only with the person's leave, which this browser declines. - await page.evaluate(()=>setInterval(()=>{for(const [name,no] of [['Review hosted source transfer',"Don't send"],['Review preview permissions',"Don't allow"]]){const d=document.querySelector(`dialog[aria-label="${name}"]`),b=d&&[...d.querySelectorAll('button')].find(b=>b.textContent===no);if(b)b.click();}},50)); + // The preview is read in this tab: no hosted dialog may open and no request may reach the hosted service. + const hostedAsked=[];page.on('request',r=>{if(r.url().startsWith('https://mcp.uor.foundation/'))hostedAsked.push(r.url());}); + await page.evaluate(()=>{window.__hostedDialogs=[];setInterval(()=>{for(const d of document.querySelectorAll('dialog[aria-label="Review hosted source transfer"],dialog[aria-label="Review preview permissions"]'))window.__hostedDialogs.push(d.getAttribute('aria-label'));},50);}); await deviceHold(page); await page.getByTestId('chat-input-container').getByRole('textbox').fill(idea);await page.getByTestId('chat-input-container').getByRole('textbox').press('Enter'); for(const deadline=Date.now()+60000;!(await deviceAsked(page)).length;await new Promise(r=>setTimeout(r,50)))assert.ok(Date.now()n+Math.ceil((m.content||'').length/4),0)); + assert.deepEqual(await page.evaluate(()=>window.__hostedDialogs),[],'the turn opens no hosted dialog');assert.deepEqual(hostedAsked,[],'the turn asks no server'); assert.deepEqual(errors,[]);console.log(JSON.stringify({actualRenderer:true,readerRequest:true,implementationSourceExcluded:true,readingStoredAsModule:true,moduleHiddenFromChat:true,contextMeter:true,pageExceptions:0})); }finally{if(browser)await browser.close();await new Promise(resolve=>server.close(resolve));} diff --git a/studio/tools/live_notation.test.mjs b/studio/tools/live_notation.test.mjs index eb64368c..9a69b900 100644 --- a/studio/tools/live_notation.test.mjs +++ b/studio/tools/live_notation.test.mjs @@ -2,7 +2,7 @@ // the canonical LexLean module they stand for. Claims held here: the same lines are always the same module; // the module is in the app shape with a theorem for every promise; lines that do not read are refused with // the line and the reason; and a chat turn written in the notation is the module to everything after it. -// The hosted checker and the interpreter are asked only when LIVE_APP_HOSTED=1. +// The hosted checker is asked only when LIVE_APP_HOSTED=1; the data form is read and run in the tab. // // node --test tools/live_notation.test.mjs // LIVE_APP_HOSTED=1 node --test tools/live_notation.test.mjs @@ -11,7 +11,7 @@ import assert from "node:assert/strict"; import fs from "node:fs"; import crypto from "node:crypto"; import { expand, parse, isNotation, PROMPT } from "../shell/live/notation.mjs"; -import { moduleFromTurn, isAppShape, splitDeclarations, wholePrefix, theoremsOf, promisesOf, moduleContext, notationRepairPrompt, notationCard, checkApp, checkLibrary, dataForm, createApp, PROMPT as APP_PROMPT } from "../shell/live/app.mjs"; +import { moduleFromTurn, isAppShape, splitDeclarations, wholePrefix, theoremsOf, promisesOf, moduleContext, notationRepairPrompt, notationCard, checkApp, checkLibrary, semanticData, createApp, PROMPT as APP_PROMPT } from "../shell/live/app.mjs"; import { describeHostedSource } from "../shell/hosted-source-permission.mjs"; // Only the opt-in hosted fixture uses this explicit authorization. @@ -134,14 +134,19 @@ test("the instructions teach the notation, and their example reads", () => { assert.ok(APP_PROMPT.length < 2500, "the instructions for a stateful app stay short: " + APP_PROMPT.length); }); -test("hosted: every module passes the checker and runs: a row is added, a bad one is refused, a clear empties", { skip: !hosted }, async () => { +// Each fixture's data form as LexLean 0.4.0 itself reads it (PrismPM f4656641, the semantic snapshot's App module +// without its theorems), as a SHA-256 of its keys in sorted order: the tab reads the same data form, no server. +const DATA = { "guests": "d2e7fb0a560bdc05", "notes": "22f327367fa623d4", "orders": "a77dca84258cec2a", "model-reading": "b749c65d4cf62408", "model-chores": "27fe555f5991c062", "model-feedback": "b6421110ec202324", "model-guests": "731af3b4c7cc2dbc" }; +const canon = (x) => JSON.stringify(x, (k, v) => (v && typeof v === "object" && !Array.isArray(v) ? Object.fromEntries(Object.keys(v).sort().map((key) => [key, v[key]])) : v)); +test("every module is read in the tab as LexLean reads it, and runs: a row is added, a bad one is refused, a clear empties", async () => { const library = { "Foundation.View.Browser.V1.Model": JSON.parse(read("../shell/live/v2/Model.json")) }; const checkLib = checkLibrary((p) => Promise.resolve(read("../shell/live/v2/" + p))); for (const name of Object.keys(FIXTURES)) { const { module } = expand(lines(name)); - const verdict = await checkApp(module, checkLib, undefined, permissionOptions); - assert.deepEqual({ ok: verdict.ok, issues: verdict.issues }, { ok: true, issues: [] }, name); - const app = createApp(await dataForm(module, undefined, permissionOptions), library); + if (hosted) assert.deepEqual((({ ok, issues }) => ({ ok, issues }))(await checkApp(module, checkLib, undefined, permissionOptions)), { ok: true, issues: [] }, name); + const data = semanticData(module); + assert.equal(crypto.createHash("sha256").update(canon(data)).digest("hex").slice(0, 16), DATA[name], name + " is read otherwise than LexLean reads it"); + const app = createApp(data, library); const numbered = app.frame().nodes.map((n, i) => ({ ...n, node: i + 1 })); const fields = numbered.filter((n) => n.kind === "Input" || n.kind === "Select"); const actions = numbered.filter((n) => n.kind === "Action"); diff --git a/studio/tools/main_stateful_integration.test.mjs b/studio/tools/main_stateful_integration.test.mjs index 227b2785..b85c7693 100644 --- a/studio/tools/main_stateful_integration.test.mjs +++ b/studio/tools/main_stateful_integration.test.mjs @@ -109,7 +109,7 @@ async function commitAfterFrames({cancel = false, mutate = source => source} = { const abort = new AbortController(); const pending = new Promise(resolve => {finish = resolve;}); const run = vm.runInNewContext(helper + ';async function run(){' + mutate(transition) + ';commit();}run', { - appFrames: Promise.resolve({settled: () => pending}), appApps: new Set([1]), appId: 1, hostedSession: {}, execution: {sourceEffects: true}, abort, + appFrames: Promise.resolve({settled: () => pending}), appApps: new Set([1]), appId: 1, execution: {sourceEffects: true}, abort, assertGenerationActive: signal => {if (signal?.aborted) throw Object.assign(Error('cancelled'), {code: 'generationCancelled'});}, commit: () => {commits++;}, }); @@ -125,20 +125,19 @@ test('streaming frames settle before source commits and late cancellation preven await commitAfterFrames(); await commitAfterFrames({cancel: true}); await assert.rejects(commitAfterFrames({mutate: source => source.replace('await settleAppFrames(appId, abort.signal)', 'void 0')}), {name: 'AssertionError'}); }); -test('failed preview initialization still closes permission and reaches chat completion', async () => { +test('failed preview initialization still reaches chat completion', async () => { const {helper} = frameBoundary(); - const begin = host.indexOf(' try { if (hostedSession', host.indexOf('// T6: completed batches')); + const begin = host.indexOf(' try { if (execution.sourceEffects && !abort.signal.aborted)', host.indexOf('// T6: completed batches')); const end = host.indexOf('\n m.abort = null;', begin); assert.ok(begin >= 0 && end > begin); // Exclude the brace belonging to chatTurn's outer finally, not its cleanup. const cleanup = host.slice(begin, end).replace(/\n \}\s*$/, ''); - let closed = 0; const run = vm.runInNewContext(helper + ';async function run(){let error=null;' + cleanup + ';return {error,completed:true};}run', { appFrames: Promise.resolve().then(() => {throw Error('preview unavailable');}), appApps: new Set([1]), appId: 1, - hostedSession: {close: () => closed++}, abort: new AbortController(), assertGenerationActive() {}, + execution: {sourceEffects: true}, abort: new AbortController(), assertGenerationActive() {}, }); const result = await run(); - assert.equal(closed, 1); assert.equal(result.completed, true); assert.match(result.error, /preview unavailable/); + assert.equal(result.completed, true); assert.match(result.error, /preview unavailable/); const beginFrames = host.indexOf('let appFrames = null;'), endFrames = host.indexOf('// The checker for a stateful app', beginFrames); let requests = 0; const ready = vm.runInNewContext(host.slice(beginFrames, endFrames) + ';appFramesReady', { diff --git a/studio/tools/read_only_chat_execution_browser.mjs b/studio/tools/read_only_chat_execution_browser.mjs index cce33930..b9b23f0b 100644 --- a/studio/tools/read_only_chat_execution_browser.mjs +++ b/studio/tools/read_only_chat_execution_browser.mjs @@ -67,10 +67,12 @@ try { const chat=await get(page,'chats',chatId),answer=chat.messages.at(-1);assert.ok(answer.content.includes('<prism-lexlean>'));assert.ok(!answer.content.includes(''));assert.equal(answer.commitHash,null); const journal=await get(page,'refs',`chat-queue:${chatId}`);if(!journal.receipts.find(r=>r.intentId===mode))console.log(JSON.stringify({mode,journal}));assert.equal(journal.receipts.find(r=>r.intentId===mode).outcome,'completed'); } - // Build applies the reading. Its preview runs on the hosted service only with the person's leave, which this browser declines. - await page.evaluate(()=>setInterval(()=>{const dialog=document.querySelector('dialog[aria-label="Review hosted source transfer"]'),deny=dialog&&[...dialog.querySelectorAll('button')].find(b=>b.textContent==="Don't send");if(deny)deny.click();},50)); + // Build applies the reading and reads its preview in this tab: no hosted dialog opens and nothing reaches the hosted service. + const hostedAsked=[];page.on('request',r=>{if(r.url().startsWith('https://mcp.uor.foundation/'))hostedAsked.push(r.url());}); + await page.evaluate(()=>{window.__hostedDialogs=[];setInterval(()=>{for(const d of document.querySelectorAll('dialog[aria-label="Review hosted source transfer"]'))window.__hostedDialogs.push(d.getAttribute('aria-label'));},50);}); await submit('build',idea); assert.equal((await waitCompletion('build')).outcome,'completed'); const afterBuild=await snapshot();if(afterBuild.source.reference===before.source.reference)console.log(JSON.stringify({chat:await get(page,'chats',chatId),before,afterBuild}));assert.notEqual(afterBuild.source.reference,before.source.reference,'Build must retain source mutation'); + assert.deepEqual(await page.evaluate(()=>window.__hostedDialogs),[],'Build opens no hosted dialog');assert.deepEqual(hostedAsked,[],'Build asks no server'); assert.deepEqual(errors,[]);console.log(JSON.stringify({profile:'foundry/read-only-chat-execution/1',ask:true,plan:true,approvalInert:true,publicationInert:true,sourceUnchanged:true,proofUnchanged:true,previewUnchanged:true,noRepair:true,inertMarkup:true,buildMutates:true,pageErrors:errors})); } finally {await browser?.close();await new Promise(resolve=>server.close(resolve));}