// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // The readers a share link reaches besides Mojo (lingo, matho, speco, x86-64) answer any text with diagnostics, and // the hub and the workspace never let a reader's exception out: each limit proven just past it with small inputs // (docs/limits.md lists every limit; tests/mojo-robustness.test.mjs has Mojo's). import test from 'node:test'; import assert from 'node:assert/strict'; import { readLingo } from '../dist/services/lingo.js'; import { parseMatho } from '../dist/services/matho.js'; import { parseSpec, toAscii, toMath } from '../dist/services/speco.js'; import { parseAsm, runAsmProgram } from '../dist/services/asm.js'; import { SERVICES } from '../dist/services/index.js'; import { SyncHub, LINKED, FAULT_ID } from '../dist/sync/hub.js'; import { Workspace } from '../dist/common/workspace.js'; const ids = (r) => r.diagnostics.filter((d) => d.severity === 'error').map((d) => d.id); /** The app's path for a pane: a workspace opening it, and an edit through the hub. */ const everyPath = (lang, src) => { const ws = new Workspace({ sources: { [lang]: src }, lang }); ws.allDiagnostics(); ws.trace(); ws.proof(); ws.lineMaps(); new SyncHub({ pseudo: '' }).edit(lang, src); new SyncHub({ [lang]: src }).fill(); }; test('lingo: a sentence nested past 200 levels is one too-deep error, and one past 1,000 words and symbols is too-long', () => { const deep = [ `To f X:\n The answer is ${'('.repeat(300)}X${')'.repeat(300)}.`, `To f X:\n If ${'not '.repeat(300)}X is 1, the answer is 1.\n The answer is 0.`, `To f X:\n The answer is ${'the length of '.repeat(300)}X.`, `To f X:\n The answer is ${'minus '.repeat(300)}X.`, `To f X:\n Start with C at 0.\n${Array.from({ length: 250 }, (_, k) => ' ' + ' '.repeat(k) + 'If X is 1:').join('\n')}\n ${' '.repeat(250)}Add 1 to C.\n The answer is C.`, ]; for (const src of deep) { const r = readLingo(src); assert.equal(r.ok, false); assert.deepEqual(ids(r).filter((x) => x === 'too-deep'), ['too-deep'], src.slice(0, 60)); assert.match(r.diagnostics.find((d) => d.id === 'too-deep').message, /nested more than 200 levels deep/); everyPath('lingo', src); } const long = readLingo(`To f X:\n The answer is X${' plus 1'.repeat(600)}.`); assert.deepEqual(ids(long).filter((x) => x === 'too-long'), ['too-long']); assert.match(long.diagnostics.find((d) => d.id === 'too-long').message, /more than 1,000 words and symbols/); // a line continued under a step joins it one token at a time (push(...tokens) passed 150,000 arguments) const continued = `To f X:\n The answer is X\n ${'+ 1 '.repeat(75000)}`; assert.ok(ids(readLingo(continued)).includes('too-long')); for (const src of [`To f X:\n The answer is ${'('.repeat(30000)}X${')'.repeat(30000)}.`, `To f X:\n The answer is X${' plus 1'.repeat(30000)}.`]) everyPath('lingo', src); // every value is tried against every routine's first line: a program is read with its first 200 routines and phrases const many = readLingo(Array.from({ length: 300 }, (_, k) => `To get x${k} of X:\n The answer is X.`).join('\n')); assert.deepEqual(ids(many).filter((x) => x === 'too-long'), ['too-long']); assert.equal(many.diagnostics.find((d) => d.id === 'too-long').line, 401); // a program is read to its 2,000th line, and its pseudocode checked only when it is at most 2,000 lines long const lines = readLingo(`To f X:\n Start with C at 0.\n${' Add 1 to C.\n'.repeat(30000)} The answer is C.`); assert.match(lines.diagnostics.find((d) => d.id === 'too-long').message, /more than 2,000 lines long/); const arms = readLingo(`To f X:\n If X is 0, the answer is 0.\n${Array.from({ length: 1500 }, (_, k) => ` Otherwise if X is ${k + 1}, the answer is ${k}.`).join('\n')}\n The answer is 1.`); assert.match(arms.diagnostics.find((d) => d.id === 'too-long').message, /more than 2,000 lines of pseudocode/); // just under the limits, the same shapes read for (const src of [`To f X:\n The answer is ${'('.repeat(150)}X${')'.repeat(150)}.`, `To f X:\n The answer is X${' plus 1'.repeat(300)}.`]) { assert.ok(!ids(readLingo(src)).some((x) => x === 'too-deep' || x === 'too-long')); } assert.equal(readLingo(`To f X:\n The answer is X${' plus 1'.repeat(300)}.`).ok, true); }); test('matho: an equation nested past 200 levels is matho-too-deep, and one past 2,000 symbols matho-too-long', () => { for (const src of [ `f(x) = ${'('.repeat(300)}x${')'.repeat(300)}`, `f(x) = ${'\\frac{1}{'.repeat(300)}x${'}'.repeat(300)}`, `f(x) = ${'\\sqrt{'.repeat(300)}x${'}'.repeat(300)}`, `f(x) = ${'-'.repeat(300)}x`, `f(x) = ${'\\sin '.repeat(300)}x`, `f(x) = x${'^{x'.repeat(300)}${'}'.repeat(300)}`, ]) { const r = parseMatho(src); assert.deepEqual(ids(r), ['matho-too-deep'], src.slice(0, 40)); assert.match(r.diagnostics[0].message, /nested more than 200 levels deep/); everyPath('matho', src); } const long = parseMatho(`f(x) = x${' + 1'.repeat(1000)}`); assert.deepEqual(ids(long), ['matho-too-long']); for (const src of [`f(x) = ${'('.repeat(30000)}x${')'.repeat(30000)}`, `f(x) = x${' + 1'.repeat(30000)}`, `f(x) = ${'\\frac{1}{'.repeat(30000)}x`]) { assert.deepEqual(ids(parseMatho(src)), ['matho-too-long']); everyPath('matho', src); } assert.equal(parseMatho(`f(x) = ${'('.repeat(150)}x${')'.repeat(150)}`).ok, true); // 600 factorials are 601 symbols, and 3,000 lines of pseudocode: not checked as pseudocode const fact = parseMatho(`f(n) = n${'!'.repeat(600)}`); assert.deepEqual(ids(fact), ['matho-too-long']); assert.match(fact.diagnostics.at(-1).message, /more than 2,000 lines of pseudocode/); // and a pane holds at most 500 equations (lowering 30,000 took 40 seconds) const eqs = parseMatho(Array.from({ length: 30000 }, (_, k) => `f${k}(x) = x + ${k}`).join('\n')); assert.deepEqual(ids(eqs), ['matho-too-long']); assert.equal(eqs.diagnostics[0].line, 501); }); test('speco: a claim of 30,000 nested bars reads in linear time (each bar trimmed and searched the whole claim)', { timeout: 10000 }, () => { const big = '|{ K ∈ [0, X) : '.repeat(30000) + 'TRUE' + ' }|'.repeat(30000); const t0 = performance.now(); const doc = parseSpec(`SPEC F(X)\n ENSURE result = ${big}`); assert.equal(doc.units[0].ensures.length, 1); assert.ok(performance.now() - t0 < 5000); toMath(toAscii(big)); // and what it reads is what it read before assert.equal(toAscii('|{ K ∈ [0, |T|) : T[K] = B }|'), 'LENGTH({ K IN [0, LENGTH(T)) : T[K] == B })'); assert.equal(toAscii('X AND |T| = 0'), 'X AND LENGTH(T) == 0'); }); test('x86-64: a setup region past 1 MiB is refused, and writing a large region at every step stops at 128 MiB of copies', () => { const p = parseAsm('l:\n mov byte ptr [rdi], 1\n jmp l'); assert.ok(p.ok); for (const size of ['1048577', '4000000000', '-5', '0x7fffffffffffffff']) { assert.throws(() => runAsmProgram(p, { maxSteps: 100, setup: `rdi = 0x1000\nmemory 0x1000 zero ${size}` }), /maps regions of 0 to 1,048,576 bytes/); } const ws = new Workspace({ sources: { asm: 'l:\n mov byte ptr [rdi], 1\n jmp l' }, lang: 'asm', setup: 'rdi = 0x1000\nmemory 0x1000 zero 4000000000' }); assert.equal(ws.trace(), null); assert.match(ws.runError, /1,048,576 bytes/); // each step keeps its own copy of a region it writes: 1 MiB written at every step stops after 128 of them const r = runAsmProgram(p, { maxSteps: 20000, setup: 'rdi = 0x1000\nmemory 0x1000 zero 1048576' }); assert.match(r.error.message, /stopped: the steps so far wrote to memory regions totaling 128 MiB/); assert.ok(r.count < 300); // an endless loop stops at the step limit, as it always did assert.equal(runAsmProgram(parseAsm('l:\n jmp l'), { maxSteps: 500 }).truncated, true); }); test('a reader that throws leaves one engine-stop diagnostic on its pane; nothing throws out of the workspace or the hub', () => { const src = 'To count the B bases in T:\n Start with Count at 0.\n For each letter c of T:\n If c is B, add 1 to Count.\n The answer is Count.\n'; const parse = SERVICES.lingo.parse; SERVICES.lingo.parse = () => { throw new TypeError('injected'); }; try { const ws = new Workspace({ sources: { lingo: src }, lang: 'lingo' }); const ds = ws.diagnostics('lingo'); assert.equal(ds.length, 1); assert.equal(ds[0].id, FAULT_ID); assert.match(ds[0].message, /could not read this pane \(injected\)/); ws.allDiagnostics(); ws.trace(); ws.proof(); ws.lineMaps(); ws.callText(); ws.readsInput(); } finally { SERVICES.lingo.parse = parse; } // the hub: a translator that throws marks the other panes stale with the reason, and keeps their text const hub = new SyncHub({ pseudo: 'FUNCTION F(X : INTEGER) RETURNS INTEGER\n RETURN X\nENDFUNCTION\n', matho: 'f(x) = x' }); hub.generateMatho = () => { throw new Error('injected'); }; const r = hub.edit('pseudo', 'FUNCTION F(X : INTEGER) RETURNS INTEGER\n RETURN X + 1\nENDFUNCTION\n'); assert.equal(r.authority, 'pseudo'); assert.equal(r.sources.matho, 'f(x) = x'); for (const l of LINKED.filter((x) => x !== 'pseudo')) assert.match(r.stale[l], /could not follow this change \(injected\)/); assert.equal(r.mojo.diagnostics[0].id, FAULT_ID); const filler = new SyncHub({ lingo: src }); filler.lowerOne = () => { throw new Error('injected'); }; const f = filler.fill(); assert.equal(f.sources.lingo, src); assert.match(f.stale.pseudo, /injected/); // the prover: a fault is said on the speco pane, and proof() is null as for a program that does not check class Faulty extends Workspace { proofOf() { throw new Error('injected'); } } const w = new Faulty({ sources: { pseudo: 'FUNCTION F(X : INTEGER) RETURNS INTEGER\n RETURN X\nENDFUNCTION\n' }, lang: 'pseudo' }); assert.equal(w.proof(), null); assert.equal(w.diagnostics('speco').at(-1).id, FAULT_ID); });