Reintent

When no possibility fits

What a <World> tries, cheapest first, how a host reads a choice that cannot settle, and where one goes when nothing handles it.

In Quick start, at the formal family dinner, System One isn't sure enough of any tone for Sam's card, and the tone asks Sam straight away. Sam is busy getting ready for the dinner. System One could look again first, this time with the goal of the card in mind, and Sam's app should show what is going on.

You will learn

  • The steps a <World> takes for a choice that cannot settle, and their order
  • How a choice goes on to the next World out, with what was tried
  • How a host reads the unresolved choices in a Realization
  • Where a choice goes when nothing handles it: intent.onUnresolved

Steps, cheapest first

/** @jsxImportSource @reintent/core */
import {
  action,
  choices,
  createContext,
  host,
  rejudge,
  useChoiceStatus,
  useGenerate,
  World,
  type Id,
} from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

/** What a button does: the intent gives it to every evaluation (index.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
/** What “no preference” does: forget the person's decision, so the choice resolves on its own again. */
export const Forget = createContext<(id: Id) => void>('Forget', { key: () => undefined });

export const LOOK: Record<Tone, { background: string; accent: string }> = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: Tone, joke?: Joke) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
  return useChoiceStatus(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model', 'human'],
  });
}

export function useTone() {
  return useToneStatus().value;
}

/** Which joke: a question only a funny card has. Until someone picks one, it is the pun. */
export function useJokeStatus(tone: Tone) {
  return tone === 'funny'
    ? useChoiceStatus(choices<Joke>(...JOKES), {
        of: 'card',
        name: 'joke',
        defaultValue: 'pun',
      })
    : undefined;
}

export function useJoke(tone: Tone) {
  return useJokeStatus(tone)?.value;
}

export function Card(p: Person) {
  const tone = useTone();
  const joke = useJoke(tone);
  // The words nobody listed: System Two writes them, once for each set of inputs.
  const message = useGenerate<string>({
    of: 'card',
    name: 'message',
    instruction:
      'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
    uses: { to: p.to, age: p.age, tone, joke },
  });
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone, joke) }),
      host('p', { children: message.value ?? '…' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
    ],
  });
}

export function Buttons() {
  const tone = useToneStatus();
  const joke = useJokeStatus(tone.value);
  const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) =>
    host('p', {
      children: [
        ...values.map((v) =>
          // A person's decision is pressed; a value the card took on its own is only marked.
          host('button', {
            disabled: now.source === 'decision' && v === now.value,
            ...(now.source !== 'decision' &&
              v === now.value && { style: { textDecoration: 'underline dotted' } }),
            onClick: action.decide({ of: 'card', name }, v),
            children: v,
          }),
        ),
        // The person's way of not deciding: forget the decision, and the card picks again.
        host('button', {
          disabled: now.source !== 'decision',
          onClick: action.forget({ of: 'card', name }),
          style: {
            marginLeft: 12,
            borderStyle: 'dashed',
            fontStyle: 'italic',
            ...(now.source === 'decision' && { background: 'transparent' }),
          },
          children: 'no preference',
        }),
      ],
    });
  // Unresolved: the card shows its question. Whether a person is asked is the intent's (questions()).
  const asking = tone.status === 'unresolved';
  return host('div', {
    children: [
      asking &&
        host('p', {
          style: { fontWeight: 600 },
          children: 'Not sure which tone suits her. Which would you like?',
        }),
      row('tone', TONES, tone),
      joke && row('joke', JOKES, joke),
    ],
  });
}

export default function App() {
  // Not sure enough: System One looks again with the goal in mind, and then the tone asks Sam.
  return (
    <World minMargin={0.5} onUnresolved={rejudge({ goal: 'a birthday card Grandma will love' })}>
      {host('div', { children: [<Card {...GRANDMA} />, <Buttons />] })}
    </World>
  );
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

Say “It is for a formal family dinner.” Formal leads warm 0.60 to 0.30, short of the 0.5 the card needs. The <World> around the card says what to do then: onUnresolved={rejudge({ goal })}, a step it takes once, before anyone is asked:

  1. rejudge asks System One the same question again, with the World's goal as a hint. It is the third judge call in What happened. Reminded of the goal, System One is even less sure, formal 0.45 to warm 0.42, and its answer takes the place of the first one. The lead is still short.
  2. With the step taken, the tone asks a person, as its ask: ['model', 'human'] says, and intent.questions() lists it. The card's own question above its buttons follows the tone's status, so it shows as soon as the tone is unresolved.

There are two more steps. reprogram asks System Two to reprogram what the World holds; the World needs an id (Reprogramming one part). regenerate sends a generated choice's request to System Two again, with the same instruction and inputs, for fresh candidates. A list takes several, one each time, in order: [rejudge({ goal }), reprogram({ goal })]. End it with 'fallback', and once they are taken the choice settles for its default. Each is a Step.

A person comes last

A choice's ask waits for the steps of the Worlds around it. Every step is taken once, and then the person is asked.

The steps start over when a person says something

When a person decides, constrains or prefers something about the choice, says a sentence, or the program changes, the steps already taken for it are forgotten. The next time it cannot settle, the World starts again from the first step.

A step only where it helps

rejudge applies to a choice System One judges, and regenerate to a generated one. In this sandbox the joke has no question, so rejudge is not for it, and it stays a pun. A choice with the reason 'unmet' skips rejudge, since System One only weighs the possibilities there are.

Worlds inside Worlds

Here the card has a World of its own, which only rejudges, and the root around it puts another one, which settles for the default:

/** @jsxImportSource @reintent/dom */
import { action, choices, rejudge, useChoiceStatus, useGenerate, World, type Content } from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];

export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

export const LOOK: Record<Tone, { background: string; accent: string }> = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: Tone, joke?: Joke) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
  return useChoiceStatus(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model', 'human'],
  });
}

export function useTone() {
  return useToneStatus().value;
}

/** Which joke: a question only a funny card has. Until someone picks one, it is the pun. */
export function useJokeStatus(tone: Tone) {
  return tone === 'funny'
    ? useChoiceStatus(choices<Joke>(...JOKES), {
        of: 'card',
        name: 'joke',
        defaultValue: 'pun',
      })
    : undefined;
}

export function useJoke(tone: Tone) {
  return useJokeStatus(tone)?.value;
}

export function Card(p: Person) {
  const tone = useTone();
  const joke = useJoke(tone);
  // The words nobody listed: System Two writes them, once for each set of inputs.
  const message = useGenerate<string>({
    of: 'card',
    name: 'message',
    instruction:
      'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
    uses: { to: p.to, age: p.age, tone, joke },
  });
  return (
    <div
      style={{
        maxWidth: 380,
        padding: '1.5em',
        borderRadius: 12,
        background: LOOK[tone].background,
        color: '#3a2a1c',
      }}
    >
      <h1 style={{ color: LOOK[tone].accent }}>{title(p, tone, joke)}</h1>
      <p>{message.value ?? '…'}</p>
      <p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
    </div>
  );
}

export function Buttons() {
  const tone = useToneStatus();
  const joke = useJokeStatus(tone.value);
  const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
    <p>
      {values.map((v) => (
        // A person's decision is pressed; a value the card took on its own is only marked.
        <button
          key={v}
          disabled={now.source === 'decision' && v === now.value}
          style={
            now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
          }
          onClick={action.decide({ of: 'card', name }, v)}
        >
          {v}
        </button>
      ))}
      {/* The person's way of not deciding: forget the decision, and the card picks again. */}
      <button
        disabled={now.source !== 'decision'}
        onClick={action.forget({ of: 'card', name })}
        style={{
          marginLeft: 12,
          borderStyle: 'dashed',
          fontStyle: 'italic',
          ...(now.source === 'decision' && { background: 'transparent' }),
        }}
      >
        no preference
      </button>
    </p>
  );
  // Unresolved: the card shows its question. Whether a person is asked is the intent's (questions()).
  const asking = tone.status === 'unresolved';
  return (
    <div>
      {asking && <p style={{ fontWeight: 600 }}>Not sure which tone suits her. Which would you like?</p>}
      {row('tone', TONES, tone)}
      {joke && row('joke', JOKES, joke)}
    </div>
  );
}

/** The card with a World of its own: unsure, it asks System One again, with its goal in mind. */
export function GrandmasCard(p: Person) {
  return (
    <World minMargin={0.5} onUnresolved={rejudge({ goal: 'a birthday card Grandma will love' })}>
      <div>
        <Card {...p} />
        <Buttons />
      </div>
    </World>
  );
}

/** The root: the card for whoever the props say, and Sam's app's controls in its children (App.tsx). */
export default function App(p: Person & { children?: Content }) {
  // Sam's app around the card: what the card's World lets through, it settles for the default.
  return (
    <World onUnresolved="fallback">
      {p.children}
      <GrandmasCard {...p} />
    </World>
  );
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

In the app, click “It is for a formal family dinner.” The card's World rejudges. The tone is still unsure, and with its step taken, the card's World lets it through to the next one out, the way an error boundary throws again. The root's World falls back ('fallback'), and the tone asks Sam. Each World takes its own steps, so the card behaves the same wherever it goes, and the root around it decides what comes after.

What a host sees

The box above the card in the sandbox above is Sam's app reading what cannot settle. App.tsx sits in the card's children and reads it with useReintent((s) => s.unresolved), from the evaluation the card beside it draws: every choice it could not settle, an UnresolvedObservation, the host's view of an Unresolved. Each one says:

FieldWhat it says
key, idwhich choice cannot settle
reason'unsure'; 'none' when no possibility is left; 'unmet' when a person decided data the possibilities do not hold, or System One judged none of them is what was asked for (cause: 'judged')
wantedfor 'unmet', what the person decided
goal, allowthe World that holds it now: its goal and its steps
boundarythat World's id, when it has one
triedthe steps of the Worlds inside it, which let it through
distthe weights when it was reported

After the sentence, the app shows that the tone is unsure, held by “a birthday card Grandma will love”, which may rejudge. Once the step is taken, the box is empty, and the tone asks Sam.

When nothing handles it

/** @jsxImportSource @reintent/core */
import {
  action,
  choices,
  createContext,
  host,
  useChoiceStatus,
  useGenerate,
  World,
  type Id,
} from '@reintent/core';

export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];

/** What a button does: the intent gives it to every evaluation (index.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
/** What “no preference” does: forget the person's decision, so the choice resolves on its own again. */
export const Forget = createContext<(id: Id) => void>('Forget', { key: () => undefined });

export const LOOK: Record<Tone, { background: string; accent: string }> = {
  warm: { background: '#fdebd3', accent: '#b5562b' },
  funny: { background: '#fff2a8', accent: '#7a4fd6' },
  formal: { background: '#f1eee8', accent: '#2b2b2b' },
};

export function title(p: Person, tone: Tone, joke?: Joke) {
  if (tone === 'formal') return `Many happy returns, ${p.to}.`;
  if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
  return joke === 'tease'
    ? `${p.to}, you don’t look a day over ${p.age - 1}!`
    : `${p.age} laps around the sun, ${p.to}!`;
}

/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
  return useChoiceStatus(choices<Tone>(...TONES), {
    of: 'card',
    name: 'tone',
    defaultValue: 'warm',
    question: 'Which tone suits a birthday card for Grandma, turning 90?',
    ask: ['model'],
  });
}

export function useTone() {
  return useToneStatus().value;
}

/** Which joke: a question only a funny card has. Until someone picks one, it is the pun. */
export function useJokeStatus(tone: Tone) {
  return tone === 'funny'
    ? useChoiceStatus(choices<Joke>(...JOKES), {
        of: 'card',
        name: 'joke',
        defaultValue: 'pun',
      })
    : undefined;
}

export function useJoke(tone: Tone) {
  return useJokeStatus(tone)?.value;
}

export function Card(p: Person) {
  const tone = useTone();
  const joke = useJoke(tone);
  // The words nobody listed: System Two writes them, once for each set of inputs.
  const message = useGenerate<string>({
    of: 'card',
    name: 'message',
    instruction:
      'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
    uses: { to: p.to, age: p.age, tone, joke },
  });
  return host('div', {
    style: {
      maxWidth: 380,
      padding: '1.5em',
      borderRadius: 12,
      background: LOOK[tone].background,
      color: '#3a2a1c',
    },
    children: [
      host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone, joke) }),
      host('p', { children: message.value ?? '…' }),
      host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
    ],
  });
}

export function Buttons() {
  const tone = useToneStatus();
  const joke = useJokeStatus(tone.value);
  const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) =>
    host('p', {
      children: [
        ...values.map((v) =>
          // A person's decision is pressed; a value the card took on its own is only marked.
          host('button', {
            disabled: now.source === 'decision' && v === now.value,
            ...(now.source !== 'decision' &&
              v === now.value && { style: { textDecoration: 'underline dotted' } }),
            onClick: action.decide({ of: 'card', name }, v),
            children: v,
          }),
        ),
        // The person's way of not deciding: forget the decision, and the card picks again.
        host('button', {
          disabled: now.source !== 'decision',
          onClick: action.forget({ of: 'card', name }),
          style: {
            marginLeft: 12,
            borderStyle: 'dashed',
            fontStyle: 'italic',
            ...(now.source === 'decision' && { background: 'transparent' }),
          },
          children: 'no preference',
        }),
      ],
    });
  // Nothing here asks a person: the buttons are all the card has.
  return host('div', {
    children: [row('tone', TONES, tone), joke && row('joke', JOKES, joke)],
  });
}

export default function App() {
  // Nothing around the card handles the tone: sent up, it goes to the intent.
  return (
    <World minMargin={0.5} onUnresolved="forward">
      {host('div', { children: [<Card {...GRANDMA} />, <Buttons />] })}
    </World>
  );
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

Here the World around the card sends up what it cannot settle: onUnresolved="forward". Say the sentence. Nothing further out handles the tone, so it reaches the intent's root, which reports it with nothing allowed. Choices says nothing handled it, and the card shows its default meanwhile. Nobody is asked, and intent.questions() stays empty. The intent hands it on, once, to each intent.onUnresolved listener: here the console.log in index.intent.tsx. With no listener and a model that reprograms, the intent asks System Two to reprogram the whole program, with the choice as why, and a proposal comes back (Reprogramming the intent).

A reprogramming the intent asks by itself, from a World's reprogram step or with no listener at the root, can fail: System Two's drafts refused, or the model unreachable. The intent hands each failure to intent.onProposalFailed with why, the World's scope, the error and the refused reasons, a ProposalFailure. With no listener it writes the failure to the console.

A choice no World says anything about takes the root's default: an unsure one falls back to its default, and one that fits nothing ('none', 'unmet') goes to the host the same way.

Recap

  • <World onUnresolved> takes steps for a choice that cannot settle: rejudge, regenerate, reprogram, one each time, in order.
  • rejudge asks System One again with the goal as a hint. reprogram has System Two reprogram the World's part. regenerate asks System Two again for a generated choice's candidates. A person is asked after the steps.
  • With every step taken, a World lets the choice through to the next one out, with its steps in tried.
  • A host reads unresolved from the Realization.
  • What nothing handles goes to intent.onUnresolved, or, with no listener, to System Two for a reprogramming.
  • A reprogramming the intent asked by itself that fails goes to intent.onProposalFailed, or to the console.

Last updated on

Edit on GitHub