Reintent

Stamping a result

Keep a result with the choices it was made under, know when it no longer holds, and get it back when they return.

System Two wrote the message inside Sam's card for the warm card. Sam's app keeps it, since a model's answer costs time and money. Then Sam tries funny. The warm message no longer fits the card, and it is still a good message: when Sam goes back to warm, it fits again. The app needs to know, for each kept result, whether it holds for the card on the screen.

You will learn

  • How under stamps a result with the choice values it was made under
  • How stale says why a result no longer holds
  • How createCache keeps results and gives back the newest that holds

A stamp, and when it is stale

The message is kept with a stamp. After each button, What happened says whether it still holds:

/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoose, 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 type Joke = 'pun' | 'tease';

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

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}!`;
  if (joke === 'tease') return `90? You don't look a day over 89, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

/** What a button does: the intent gives it to every evaluation (index.intent.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });

export default function App() {
  const tone = useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
  // Only a funny card has a joke.
  const joke =
    tone === 'funny'
      ? useChoose(choices<Joke>('pun', 'tease'), { of: 'card', name: 'joke', defaultValue: 'pun' })
      : undefined;
  return host('div', {
    children: [
      host('p', {
        children: TONES.map((v) =>
          host('button', {
            disabled: v === tone,
            onClick: action.decide({ of: 'card', name: 'tone' }, v),
            children: v,
          }),
        ),
      }),
      host('div', {
        style: {
          maxWidth: 380,
          padding: 24,
          borderRadius: 12,
          background: LOOK[tone].background,
          color: '#3a2a1c',
        },
        children: [
          host('h1', { style: { color: LOOK[tone].accent }, children: title(GRANDMA, tone, joke) }),
          host('p', { children: 'Wishing you a wonderful day.' }),
          host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${GRANDMA.from}` }),
        ],
      }),
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

under(reads) makes the stamp, a Stamp. It holds the value of every choice the evaluation read, by key, and the context they were read in ('@ctx'). stale(stamped, reads) takes a result with its stamp, a Stamped ({ value, under }), and compares the stamp with an evaluation now. It lists what moved: a choice whose value is different, or one that is no longer read. An empty list means the result holds.

Press funny: [ 'card#tone' ], since the message was written for another tone. Press warm: [], and it holds again. A stamp is about values, so nothing has to be thrown away. The same values make the same stamp hold.

under(reads, of) stamps only the choices of the entities in of. A message about the card then stays fresh when something else on the page moves.

A cache of results

The app keeps every message it was given, each with its stamp, and asks the model only when none holds:

/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoose, 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 type Joke = 'pun' | 'tease';

export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };

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}!`;
  if (joke === 'tease') return `90? You don't look a day over 89, ${p.to}!`;
  return `${p.age} laps around the sun, ${p.to}!`;
}

/** What a button does: the intent gives it to every evaluation (index.intent.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });

export default function App() {
  const tone = useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
  // Only a funny card has a joke.
  const joke =
    tone === 'funny'
      ? useChoose(choices<Joke>('pun', 'tease'), { of: 'card', name: 'joke', defaultValue: 'pun' })
      : undefined;
  return host('div', {
    children: [
      host('p', {
        children: TONES.map((v) =>
          host('button', {
            disabled: v === tone,
            onClick: action.decide({ of: 'card', name: 'tone' }, v),
            children: v,
          }),
        ),
      }),
      host('div', {
        style: {
          maxWidth: 380,
          padding: 24,
          borderRadius: 12,
          background: LOOK[tone].background,
          color: '#3a2a1c',
        },
        children: [
          host('h1', { style: { color: LOOK[tone].accent }, children: title(GRANDMA, tone, joke) }),
          host('p', { children: 'Wishing you a wonderful day.' }),
          host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${GRANDMA.from}` }),
        ],
      }),
    ],
  });
}
ChoicesC

Starting…

Realizationλ

Starting…

What happened

Nothing logged.

createCache() keeps that list, newest first, each entry a Stamped. put(value, stamp) adds a result. get(reads) gives the newest whose stamp holds, or undefined. Here write stands in for System Two and counts its calls.

Press funny. No message holds, so message #2 is written. Press warm: message #1 comes back with no call. Press funny again for #2, kept. The funny card's stamp also holds its joke, so deciding the tease in Choices writes message #3.

get(reads, { check }) also asks the result itself. check(value) returns the problems it finds, and a result with any is passed over.

A result made in another context does not hold there

The stamp keeps the context the choices were read in. A layout written for the tablet (Along an axis) does not hold on the phone, even when every choice has the same value there, and stale lists '@ctx'. Pass { carry: true } to stale or get when it should hold.

Recap

  • under(reads) stamps a result with the values it was made under and their context; of keeps it to some entities.
  • stale(stamped, reads) lists what moved; an empty list means the result holds.
  • createCache() keeps results; get(reads) gives back the newest that holds, so an earlier answer returns with its values.
  • A stamp holds only in its own context, unless carry: true.

Last updated on

Edit on GitHub