Reintent

A program System Two wrote

Load the program text a model wrote, with only the modules you give it, and turn it into a proposal.

Sam asks for a card with a poem inside. The possibilities the card has cannot say that: System Two has to write a new program. A model writes text, the source of an .intent.tsx file, and the intent needs a program. The text should be made of the card's own components, and nothing else should reach the page.

You will learn

  • How loadProgram turns a model's text into a program
  • What the text may import and use, and what is refused
  • How a host tries it, and answers a reprogramming with it
  • How a change of K over time is an edit of the program

Loading the text

import { loadProgram } from '@reintent/lib/program';
import * as cards from './card.intent';

const text = `
  import { entity, host, useChoose } from '@reintent/core';
  import { Card } from './card.intent';

  const poem = entity('poem', { form: { kinds: ['haiku', 'limerick'], judge: 'Which poem suits the card?' } });
  const Poem = poem(() => host('poem', { form: useChoose(poem.form) }));

  export default <Card to="Grandma"><Poem /></Card>;
`;
const { program } = loadProgram(text, { modules: { './card.intent': cards } });

loadProgram(text, { modules }) compiles the text (TypeScript and JSX), evaluates it with @reintent/core and the modules you pass as the only modules there are, and returns its default export as the program, with everything else it exports. The text may declare entities of its own, as the poem here, and use the card's components. Pass each module under the specifier the text writes, as the module's exports (ProgramModules, in ProgramOptions). The result is a LoadedProgram. Its default export may also be a function root: then the program, and root, is that function, which the intent calls with its input (createIntent({ program, input }), 0045 §3).

What is refused

Before anything runs, the text is checked, and checkProgram(text, { modules }) gives the same list without loading:

  • an import from anywhere else, a name the module does not export (import { Lamp } from './card.intent' when it has none), import(), import.meta or require;
  • from @reintent/core, a name that runs an intent rather than writes one (createIntent, recorded, the host's checks): the text may import intentNames, unless the host widens it with core;
  • JSX written for a catalog (/** @jsxImportSource @app/catalog */) the host did not give in catalogs;
  • a name the text neither declares nor imports, but a few pure globals such as Math and JSON: no fetch, window, document, process, eval, Function, Date or timers;
  • Math.random: an intent is pure;
  • constructor, prototype, __proto__, as names or strings.

A refused text, one that does not compile, one that fails while it runs, or one whose default export is not an element, throws ProgramRefused, with every reason in reasons. The check guards against what a model writes by mistake or overreach. An allow-list of imports is not a guarantee of purity: an allowed module's functions run with whatever they reach, and a text can loop forever. A worker is not a permission sandbox either: it has the network and timers. To isolate code from someone hostile, run the loader where the platform enforces it: a sandboxed, cross-origin frame with a CSP, or another process.

Trying it first

Before the host offers a program to Sam, it realizes it once under what the intent has now, sending nothing: tryProgram(program, { layers, input, provide, catalog }). The intent tries it on its input again when Sam accepts it. A program that throws there, or whose λ its catalog does not take, is not offered; the Tried result says why, every reason.

const loaded = loadProgram(text, { modules, catalogs: { '@app/card': cardCatalog } });
const tried = tryProgram(loaded.program, { layers: intent.layers, catalog: loaded.catalog });
if (!tried.ok) console.log(tried.errors);

Answering a reprogramming

The host's reprogram answers with the loaded program, and the proposal waits for Sam like any other (Reprogramming). Recorded, the recording keeps the text, not a program's name, and recordedReprogram(recordings, toDraft) looks it up the way recorded does and turns it into the proposal:

const intent = createIntent({
  program: <Card to="Grandma" />,
  model: {
    ...recorded(recordings),
    reprogram: recordedReprogram(recordings, (answer) => {
      const a = answer as { text: string; summary: string };
      return { program: loadProgram(a.text, { modules }).program, summary: a.summary };
    }),
  },
});

K over time is an edit of the program

Sam drags a card's sticker somewhere none of its places are. The card's places are its possibilities, K, and K grows only by a new program. The card component makes its places from its props, typed where it is written:

export function Sticker(p: { id: string; places?: Record<string, { at: [number, number] }> }) {
  const K = { ...CORNERS, ...p.places };
  const where = useChoose(choices(Object.keys(K)), { of: p.id, name: 'where', defaultValue: 'top' });
  return host('sticker', { at: K[where].at });
}

The new place is an edit of that one element of the program, with @reintent/lib/program-edit. editProps(text, { tag: 'Sticker', id: 's1' }, { places }) gives the text with the element's props set, and the base text stays as it was; propsIn reads the props it writes as data. A program that is elements, not text, is edited with editElement. When a model answers with a JSON Patch of the declaration rather than the declaration itself, applyPatch(value, ops) gives the new one, each op a PatchOp. The host offers the new text as a proposal, and once Sam accepts it, the drag is a decision for the new place:

const next = editProps(text, { tag: 'Sticker', id: 's1' }, { places: { ...had, here: { at: [120, 40] } } });
const p = await intent.reprogram({ prompt: 'the sticker, here' }); // the host's reprogram answers with `next`
intent.review(p).accept();
intent.decide({ of: 's1', name: 'where' }, 'here');

An edit names one instance by its component and id; written in several places (the cases of a branch), it is edited in each. None is refused with ProgramEditRefused, and so are several elements no id tells apart: two stickers are two elements, and a place one learned is not the other's. An element is named by an ElementAt.

Recap

  • loadProgram(text, { modules }) gives the program a model wrote, made of your components and the core.
  • Imports, globals, impurity and escapes are refused before anything runs; checkProgram lists them.
  • tryProgram realizes it once under the intent's layers and checks its catalog, before anyone accepts it.
  • A reprogramming answers with the loaded program (recordedReprogram from recordings); a person accepts it.
  • A new possibility over time is an edit of one element's props (editProps, editElement): a new program version, offered and accepted.

Last updated on

Edit on GitHub