Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/conformance/tests/hosted_source_permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 6 additions & 18 deletions studio/shell/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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(/<dyad-chat-summary>([\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 }); }
Expand All @@ -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(/<dyad-spec-patch>([\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);
}
Expand All @@ -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;
Expand All @@ -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";
Expand Down
89 changes: 67 additions & 22 deletions studio/shell/live/app.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand All @@ -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 : "");
}
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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", "<?": "blt", "=": "eq" };
const TOKEN = /\s*("(?:[^"\\]|\\.)*"|:=|=>|::|<=\?|<\?|=\?|&&|\|\||[()[\]{},:|+=]|[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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down
Loading
Loading