Reintent

Letting System Two write a part

A model writes a new intent component for one part of the program. The intent tries each draft in its place, asks again with the problems, and proposes the one that passes.

In Reprogramming the intent, a function in the sandbox stood in for System Two and answered with a card written in advance. Sam's family wants a card everyone signs, one line for each person at the dinner, and none of the card's possibilities has that. Here a model writes the part. It answers with a new intent component, made of the app's intent components and the host's elements. The intent tries each draft and asks again when one is refused, and Sam accepts the card the last one draws.

You will learn

  • How a program declares the part, what a draft may use, and the rules it is checked by
  • What model.reprogram is given, and what it answers
  • What the intent checks before a draft becomes a proposal
  • What Sam accepts, and what comes back the next time
  • When to use a World and when a component, and what the model and Sam each see

The part

index.intent.tsx
<Family.Provider value={{ id: 'grandma', names: ['Ann', 'Bo'] }}>
  <World id="card" onUnresolved={reprogram({ goal: 'a card the whole family signs' })}>
    <Card to="Grandma" />
  </World>
</Family.Provider>

<World id="card"> names the part System Two may write. When a choice in it cannot settle, the intent asks model.reprogram for a new program for that part. The part reads the family from a context outside it, so whatever is written for it runs under that Provider.

The World is written in the program's tree, where the intent can find it. A World inside a component's own code holds code; to reprogram a part there, the component is declared by its id (A component by its id).

Elements, imports and drafts

const intent = createIntent({
  program,
  elements: exportCatalog(cardCatalog),
  reprogram: { attempts: 3, imports: { Card } },
  model: { judge, reprogram: write },
});

elements are the host elements a draft may use, the lowercase tags of its JSX, as the contract exportCatalog gives (Catalogs). A draft whose λ uses an element it does not have, or props it does not take, is refused. imports are the intent components a draft may import, the capitalized tags. reprogram.attempts is how many drafts System Two may write for one reprogramming. All are defaults for the whole intent. A World says otherwise for its own part, reprogram({ goal, attempts: 5, elements }), and so does a host that asks directly, intent.reprogram({ prompt, attempts }). elements was called catalog before, and the old name still works.

What the model is given

model.reprogram gets a ReprogramRequest. It holds what a prompt needs:

function promptFor(req: ReprogramRequest) {
  return [
    `Rewrite the part "${req.scope!.boundary}" of a Reintent intent program. Goal: ${req.why?.goal ?? req.prompt}`,
    req.scope!.text ? `The part now:\n${req.scope!.text}` : `The part now: <Card to="Grandma" />`,
    `Answer with one .intent.tsx module. Its default export is the new part. Write its JSX for @app/card.`,
    `Elements you may write:\n${JSON.stringify(req.elements?.kinds)}`,
    `You may import from '@reintent/core' and from './card.intent' (${Object.keys(req.imports ?? {})}).`,
    `Rules: ${JSON.stringify(req.rules ?? {})}`,
    ...(req.refused ? [`Your last answer was refused:\n- ${req.refused.problems.join('\n- ')}`] : []),
  ].join('\n\n');
}
  • The goal, in why.goal from the World's reprogram({ goal }), or prompt from intent.reprogram({ prompt }).
  • The part's text, in scope.text, once a draft that wrote the part has been accepted. The part the developer wrote has none, so the host describes it itself.
  • The host elements, in elements: every element with its about, its props as JSON Schema, its children and events.
  • The intent components, in imports, by name. The host reads their signatures and TSDoc from its source for the prompt.
  • The rules, in rules, each with 'error' or 'warn'.
  • Why the last draft was refused, in refused.problems, from the second draft on. Each problem is a sentence a model can read.

Writing a draft

async function write(req: ReprogramRequest): Promise<ProposalDraft> {
  const text = await askModel(promptFor(req));
  // The app's module, with the imports as the request gives them, so the rules see what is drawn.
  const modules = { './card.intent': { ...cards, ...req.imports } };
  const { program } = loadProgram(text, { modules, catalogs });
  return { program, text, summary: 'a card the family signs' };
}

askModel is the app's own call to its model. loadProgram turns the text into a program, with only @reintent/core and the modules the host passes (A program System Two wrote). A text it cannot load throws ProgramRefused with its reasons, and the intent counts that as one draft refused and asks again. Any other error, the model being offline, fails the request at once (When a request fails). The draft returns the text with the program, so the next request can show it to the model.

What the intent checks

Before a draft becomes a proposal, the intent puts it in <World id="card"> and realizes the whole program once, where it would be shown, sending nothing. The part reads Family from the Provider around the World, under the layers and input the intent has now. The draft is refused when it fails there, when its tree is not a host's data, or when its λ uses an element elements does not have.

Say the model's first draft draws each name with a sign node. The elements have signature and no sign, so the intent refuses it and asks again, with the sentence that names the kind in refused.problems. The second draft writes signature and passes. After attempts refused drafts, the request fails with ProgramRefused and the last problems, and the World takes its next step.

A host that asks with nothing on a screen names where the drafts are tried, with the same contexts that accept takes: intent.reprogram({ prompt, contexts }).

Rules

Rules are checks a draft must pass, configured as ESLint configures its rules:

reprogram({
  goal: 'a card the whole family signs',
  imports: { Card },
  rules: { 'keep-decisions': 'error', 'use/Card': 'warn' },
});
  • keep-decisions: the draft keeps every decision a person made that the program reads now. The intent realizes the program and the draft in the same contexts and compares what they read. Sam chose warm for the card's tone, and a draft that no longer reads the tone would drop it. Reading the same identity with candidates that no longer admit warm also fails.
  • use/<Name>: only uses inside the replacement count; a sibling cannot satisfy this rule. the draft draws the import Name. The draft gets each import wrapped once, so the trial knows what it drew.

'error' refuses the draft and asks again, with the problem in refused.problems: rule keep-decisions: drops the decision on card#tone ("warm"). 'warn' lets the draft through and reports. 'off' turns off a rule set further out. The intent's reprogram.rules are the defaults, and a World, a component or a request overrides them by name. The names are typed, a Rules keyed by the imports, so 'use/Moon' without a Moon import does not compile, and a name that is not a rule throws before System Two is asked. Each rule's Severity is one of the three.

A preference that cannot be checked is not a rule. It goes in the component's TSDoc, where people and models read it:

/** The card everyone signs. Use it for any card: it keeps the card's tone. */
export function Card(p: { to: string }) { … }

What Sam accepts

The proposal waits for Sam like any other (Every proposal). Its realization is what the draft drew when the intent tried it, the card with a line for Ann and one for Bo, and the host shows Sam that. Sam accepts the card.

intent.review(p).accept();

Accepting replaces what <World id="card"> holds and keeps the rest. Before it does, the intent tries the program once more, on the input and screens there are now, against the same elements and the rules set to 'error', and throws ProgramRejected when it fails. The next time System Two is asked for this part, scope.text holds the text Sam accepted. With accept(migrate), the new program and migrated choices are checked together before commit. Failure leaves state, history and subscriptions unchanged; success is one undo step. Undo takes the part back, and the text with it.

The checks keep out mistakes

lib/program refuses what a model writes by mistake or overreach. A module it is allowed to import still runs with whatever it reaches, and a text can loop forever. That an intent stays pure rests on the model, and on these checks. To isolate code from someone hostile, load it where the platform enforces it, in a sandboxed, cross-origin frame with a CSP or in another process.

A component by its id

The signing card can live in a component of the app's own code, Card, rather than in the program's tree. reprogrammable gives it an id, and the intent finds its code by that id:

const Signing = reprogrammable('signing', SigningV1, {
  imports: { Heading, Line },
  elements: exportCatalog(cardCatalog),
  rules: { 'keep-decisions': 'error', 'use/Line': 'error' },
  onUnresolved: reprogram({ goal: 'a card the whole family signs' }),
});

function Card(p: { to: string }) {
  return (
    <card to={p.to}>
      <Signing to={p.to} />
    </card>
  );
}

<Signing> draws the version of signing the intent holds now, SigningV1 until Sam accepts another, in a <World id="signing"> with the onUnresolved given here. A draft for it is a component of the same props, and scope.part says so. The request's imports are what write gives loadProgram as its modules. Sam's host can also ask for it by name, intent.reprogram({ at: 'signing', prompt }). Accepting it is a new version of the program with the same tree. Each intent has its own versions, a Reprogrammable keeps none of its own, and undo takes the component back to the version before.

A World or a component

<World id>reprogrammable(id, initial)
Likeediting the JSX written at one callediting a function's definition
What changeswhat this boundary holds in the program's treethe implementation of this id
A draft isa piece of program, reading context and inputa component of the same props
Used in several placeseach place its own boundaryone accept changes every use in the intent
Where it can bethe program's treeanywhere, in a component's code too

A World says "this part here may be rethought". A reprogrammable component says "this part may evolve". To change one use of a component alone, give that use its own id, or put a World there.

elementsimports
Arehost elements, the lowercase tagsintent components, the capitalized tags
LikeTypeScript's JSX.IntrinsicElementsa module's imports
Going past themalways refused: the host cannot draw itrefused when it loads: the import is not there
Using themoptionaloptional; a use/<Name> rule requires one

elements are the hard boundary: whatever a draft writes, its λ uses only these. imports are the abstractions there already are, and a draft may build its own from the elements instead.

What the model and Sam see

  • The model sees what a developer sees: the elements' props, the components' signatures and TSDoc, the rules and what they found. Nothing is written for the model alone.
  • The same results go to both. A rule set to 'error' that fails is a problem the model rewrites from. Every result is on the proposal, in checks, and Sam reads the same sentence the model did.
  • Every step is data. The request is keyed by its stamp, an answer is recorded and replays (Recordings), and a proposal crosses to a host with its checks (Handles).

Recap

  • <World id onUnresolved={reprogram({ goal })}> in the program's tree names a part System Two may write.
  • createIntent({ elements, reprogram: { attempts, imports, rules } }) sets the defaults for the intent; reprogram({ … }) and intent.reprogram set them for their own.
  • model.reprogram builds the prompt from the request (why.goal, scope.text, elements, imports, rules, refused), calls the model and loads the text.
  • The intent tries each draft in its part, against the elements and the rules, and asks again with the problems. A draft that passes is a proposal with its realization and checks.
  • Sam accepts the card. The accepted text comes back as scope.text.
  • reprogrammable(id, initial, { imports, elements, rules, onUnresolved }) names a part in a component's own code; intent.reprogram({ at }) asks for it by name.

Props declared with Zod

Use defineComponent from @reintent/catalog to give an intent component data props, inferred types, defaults, and a runtime check from one Zod declaration:

import { z } from 'zod';
import { defineComponent } from '@reintent/catalog';
import { host, reprogrammable } from '@reintent/core';

const Heading = defineComponent(
  z.object({ text: z.string().min(1).describe('The heading'), size: z.number().int().min(1).default(2) }),
  ({ text, size }) => host('heading', { text, size }),
);
const Story = reprogrammable('story', Heading, { imports: { Heading } });

<Heading text="Hello" /> passes size: 2 to its implementation. An empty text, a size below one, or an undeclared data prop fails before execution. JSX children pass through separately; key belongs to JSX. Neither belongs in the data schema. The schema uses the same JSON Schema subset as host elements; unsupported refinements and transforms are refused at declaration.

The component's ComponentContract exposes contract.props as JSON and contract.parse as a pure check with defaults. reprogrammable retains this contract when its implementation changes. Each executed instance is checked, including trial evaluations and the check before accepting a proposal. An invalid call to an imported component refuses the draft and gives the model the problem for its next attempt.

The model request includes scope.propsSchema for the component being reprogrammed and importSchemas for imports that declare a contract. Include these JSON values in the prompt, alongside the goal and rules; .describe() supplies descriptions without extracting TSDoc. Ordinary components without a schema still work, but have no declared props check.

These checks validate actual values. They do not prove a generated function handles every future input. loadProgram transpiles TypeScript; it does not type-check generated source. Rules, trial evaluation, and a person's acceptance remain separate requirements.

Last updated on

Edit on GitHub