SLOPSHOPPER

Twenty Forty-Wait

A 2048 game above the prompt to keep your attention in Claude Code while it works, with a live view of Claude's work and a your-turn card when it stops.

bandguardcommandpromptmodeltimer
★ 1v0.2.1Apache-2.0updated 2026-10-16leonidb/claude-code-mods/mods/twenty-forty-wait
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · twenty-forty-wait
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /fortywait ⎿ twenty-forty-wait: Twenty Forty-Wait on. Run /fortywait again to turn it off. ⟨Claude Code's own drawing⟩ ▣ client module ./board.tsx No activity yet · Claude's work shows here ○ playable while Claude works ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
⟨Claude Code's own drawing⟩ ▣ client module ./board.tsx No activity yet · Claude's work shows here ○ playable while Claude works
README

Twenty Forty-Wait

Don't let your attention wander off while waiting for Claude Code's answer.

A game of 2048 played above the Claude Code prompt while Claude works, then the board locking in grey beside the your-turn card when Claude stops

A small 2048 game sits right above the prompt, so you can stay in Claude Code while it works instead of switching to another tab. Claude's output still appears above it as usual: glance at it or follow along whenever you want, and play a few moves when you feel like it.

2048 was chosen because the attention it gets is entirely up to you: as much or as little as you like, whenever you like. Leave it at any moment and pick it up where you left off. Click the board to play, press Tab to type.

The game also shows what Claude is doing. When Claude stops, the board locks behind a ✓ YOUR TURN card that says what happened and what to do next.

Install

/plugin install twenty-forty-wait --marketplace leonidb/claude-code-mods

Run /fortywait to show the game; run it again to hide it.

Needs Claude Code 2.1.289 or later. Tested with Claude Code 2.1.296.

Use

  • Play: click the board, then use the arrows.
  • Type: press Tab or Esc, or click the prompt.
  • When Claude stops: a long turn gets 5 seconds to finish your move, then the board locks until your next prompt.
  • One game per session. Your best score is kept.

What leaves your machine

Only Claude's own words, for the card's summary. After a turn of 10 seconds or longer that ends normally with a reply of 200 characters or more, that reply is sent to Claude Haiku, with fixed instructions, to write the summary: the whole reply if it's 8,000 characters or less, otherwise its first 2,000 and last 6,000 characters. That text can include anything Claude wrote in the reply, such as file paths, quoted output or code. It goes through the same connection as the rest of your Claude Code session (Anthropic, or the provider or gateway your session uses, such as Bedrock or Vertex), and adds a little to your usage.

Nothing else from the session is sent: not your prompts, tool calls or files. Nothing at all is sent before your first /fortywait, or after you run it again. A reply already on its way to Haiku at that moment can't be called back.

The mod keeps two values in Claude Code's plugin storage: whether the game is on, and your best score.

Known limits

  • Entering the game takes a click; there's no keyboard way in yet.
  • After Esc or a prompt click, the label updates once you type. Tab switches at once.
  • Small windows show one line: the full game needs about 24 rows.
  • While you play, Claude Code's shortcuts wait until you press Tab. Ctrl+C still interrupts.
  • --resume starts a new game.

Update and uninstall

Third-party marketplaces don't auto-update by default. To update:

/plugin marketplace update leonidb /plugin update twenty-forty-wait@leonidb

Or turn on auto-update: /plugin, then Marketplaces, select leonidb, then Enable auto-update.

To uninstall:

/plugin uninstall twenty-forty-wait@leonidb


An independent community mod, not affiliated with or endorsed by Anthropic. Apache License 2.0. Design notes: DESIGN.md.

Source 6 files
hooks/register.tsx 583 lines
1import { atom, read, update } from 'claude-code'
2import type {
3  EngineInterface,
4  Register,
5  RenderElement,
6  RenderInput,
7} from 'claude-code'
8
9import type { Model } from '../types'
10import { bandTree, stackedTree } from './band'
11import {
12  INITIAL_MODEL,
13  MODEL_SHAPE,
14  afterBoardPost,
15  afterDialog,
16  afterFault,
17  afterHandoff,
18  afterPrompt,
19  afterRestore,
20  afterSummary,
21  afterTick,
22  afterToggle,
23  afterToolCall,
24  afterToolDone,
25  afterTurnEnd,
26  afterTurnStart,
27  bandOf,
28  boardPostOf,
29  handoffOf,
30  isDealt,
31  isReadFirst,
32  isTicking,
33  isTimeShown,
34  modelOf,
35  rowsTakenOf,
36  toggleNoteOf,
37  toolSucceeded,
38  turnKeyOf,
39  writeOf,
40} from './logic'
41import type {
42  Effect,
43  SaveEffect,
44  Step,
45  StoreRead,
46  SummarizeEffect,
47  TurnKey,
48} from './logic'
49
50const model = atom(
51  { plugin: 'twenty-forty-wait', key: 'model' } as const,
52  INITIAL_MODEL,
53  { shape: MODEL_SHAPE },
54)
55
56// The time the band shows. Written with each change the band shows and by
57// the ticker each second, so the band redraws as it moves.
58const now = atom({ plugin: 'twenty-forty-wait', key: 'now' } as const, 0)
59
60// One module load's handles. `section` runs every piece of work that reads or
61// writes the model, one after another in the order it came in; `writes`
62// chains the durable writes; `timer` is the ticker while the band needs it.
63// A tick (between faults), a handoff for one board, or a restore the band
64// asked for waits in line at most once, and so do prompt edits that come one
65// after another with nothing between: the edit waiting in line, until it
66// starts or anything else comes in after it.
67type Load = {
68  section: Promise<unknown>
69  writes: Map<string, Promise<unknown>>
70  timer: { cancel: () => void } | undefined
71  isTickWaiting: boolean
72  handoffWaiting: number | undefined
73  isRestoreWaiting: boolean
74  editWaiting: object | undefined
75  isFaulted: boolean
76  isRestored: boolean
77}
78
79function inOrder<T>(load: Load, work: () => Promise<T>): Promise<T> {
80  const run = load.section.then(work)
81  load.section = run.catch(() => undefined)
82  return run
83}
84
85// A handoff the render asked for, done now if its timer never ran (another
86// plugin may refuse timers): the next work in line carries it out first.
87async function handedBack($: EngineInterface, load: Load): Promise<void> {
88  const epoch = load.handoffWaiting
89  if (epoch === undefined) return
90  load.handoffWaiting = undefined
91  await commit($, latest => afterHandoff(latest, epoch))
92}
93
94// Runs observation work in this load's order, with the time the event came
95// in, after restoring a session whose state Claude Code emptied. The event
96// itself is never held: the hook hands it on at once. Work that fails stops
97// the board in its own place in line, before anything after it, in case the
98// band is stuck.
99function observe(
100  $: EngineInterface,
101  load: Load,
102  work: (at: number) => Promise<void>,
103): void {
104  const arrived = $.clock.now()
105  load.editWaiting = undefined
106  void inOrder(load, async () => {
107    try {
108      await rescued($, load)
109      await handedBack($, load)
110      await work(await arrived)
111    } catch {
112      await faulted($, load)
113    }
114  })
115}
116
117// Stops the board: the ticker first, and the band stops drawing it, so both
118// hold even if nothing can be written, then the model. The ticker's waiting
119// tick is forgotten, so a later ticker runs even if that tick failed before
120// its work began; a tick still in line runs, beside the later ticker's.
121async function faulted($: EngineInterface, load: Load): Promise<void> {
122  load.timer?.cancel()
123  load.timer = undefined
124  load.isTickWaiting = false
125  load.isFaulted = true
126  // The band read nothing while it was hidden, so no write redraws it.
127  $.ui.invalidate('ui.render')
128  await commit($, afterFault).catch(() => undefined)
129}
130
131// Restores the session's band from what was kept: on or off for a session
132// with no choice of its own, the best score, and a new game for a session
133// with none. The band shows from here on.
134async function restored($: EngineInterface, load: Load): Promise<void> {
135  const kept = {
136    isOn: await keptOn($),
137    best: await keptValue($, 'best'),
138  }
139  const at = await $.clock.now()
140  const step = await commit($, current =>
141    afterRestore(current, kept, Math.random),
142  )
143  void runEffects($, load, step.effects)
144  load.isRestoreWaiting = false
145  load.isFaulted = false
146  load.isRestored = true
147  $.ui.invalidate('ui.render')
148  await settled($, load, at)
149}
150
151// A `/clear`, or a resume inside Claude Code, ends the session: Claude Code
152// empties the mod's session state and no session start follows. A load that
153// has restored and then finds a board with no tiles restores again, as a new
154// session would.
155async function rescued($: EngineInterface, load: Load): Promise<void> {
156  if (!load.isRestored || isDealt(modelOf(await read($, model)).game)) return
157  await restored($, load)
158}
159
160// Applies a transition to the model. A transition that changes nothing
161// writes nothing; otherwise it is one compare-and-set write, and `update` may
162// run it more than once, so the effects are those of the run written.
163async function commit(
164  $: EngineInterface,
165  transition: (current: Model) => Step,
166): Promise<Step & { isChanged: boolean }> {
167  const kept = modelOf(await read($, model))
168  const first = transition(kept)
169  if (first.model === kept) return { ...first, isChanged: false }
170  let effects = first.effects
171  const written = await update($, model, value => {
172    const step = transition(modelOf(value))
173    effects = step.effects
174    return step.model
175  })
176  return { model: written, effects, isChanged: true }
177}
178
179// After a change at `at`: a lock that came due is committed first, whatever
180// arrived, so the ticker never stops with a lock left uncommitted. Then the
181// band's time moves when the band shows time, and the ticker runs exactly
182// while the band needs the clock.
183async function settled(
184  $: EngineInterface,
185  load: Load,
186  at: number,
187): Promise<void> {
188  const latest = await commit($, current => afterTick(current, at))
189  if (isTimeShown(latest.model)) await update($, now, () => at)
190  const isNeeded = isTicking(latest.model, at)
191  if (isNeeded && load.timer === undefined) {
192    load.timer = $.clock.every(1000, () => {
193      if (load.isTickWaiting) return
194      load.isTickWaiting = true
195      observe($, load, async tickAt => {
196        load.isTickWaiting = false
197        await settled($, load, tickAt)
198      })
199    })
200  } else if (!isNeeded && load.timer !== undefined) {
201    load.timer.cancel()
202    load.timer = undefined
203  }
204}
205
206// Carries out a commit's effects: durable writes in line behind earlier
207// ones, and summary calls on their own, none once a failure stopped the band,
208// even if that couldn't be written. Resolves once the writes have landed.
209function runEffects(
210  $: EngineInterface,
211  load: Load,
212  effects: Effect[],
213): Promise<unknown> {
214  const saves: SaveEffect[] = []
215  for (const effect of effects) {
216    switch (effect.kind) {
217      case 'save':
218        saves.push(effect)
219        break
220      case 'summarize':
221        if (!load.isFaulted) summarize($, load, effect)
222        break
223    }
224  }
225  return queueWrites($, load, saves)
226}
227
228// Asks the model for a turn's summary, outside the line so a slow call holds
229// nothing up, and hands the result back in line, where the turn it belongs
230// to decides whether it is shown. A call that fails says so in its result; a
231// call refused outright is logged, and the card keeps its placeholder.
232function summarize(
233  $: EngineInterface,
234  load: Load,
235  effect: SummarizeEffect,
236): void {
237  $.model.complete(effect.request).then(
238    result => {
239      observe($, load, async at => {
240        await committedAt($, load, at, current =>
241          afterSummary(current, effect.turn, result),
242        )
243      })
244    },
245    (error: unknown) => {
246      $.ui.log(`could not summarize: ${String(error)}`, { to: 'debug' })
247    },
248  )
249}
250
251// Commits an observation together with any lock that came due by then, in
252// one read, so a stopped ticker never leaves a lock waiting; the band's time
253// moves only when something changed.
254async function committedAt(
255  $: EngineInterface,
256  load: Load,
257  at: number,
258  transition: (current: Model) => Step,
259): Promise<void> {
260  const step = await commit($, current => {
261    const observed = transition(current)
262    const due = afterTick(observed.model, at)
263    return { model: due.model, effects: [...observed.effects, ...due.effects] }
264  })
265  if (step.isChanged) await settled($, load, at)
266}
267
268// Queues durable writes, each behind the earlier writes of its own key, and
269// resolves once these have landed; a slow best score never holds up on/off.
270// A best score reads what is stored first, since another session may have
271// kept a higher one meanwhile. A write that fails is logged, and its queue
272// goes on.
273function queueWrites(
274  $: EngineInterface,
275  load: Load,
276  effects: SaveEffect[],
277): Promise<unknown> {
278  const landed: Promise<unknown>[] = []
279  for (const effect of effects) {
280    const queued = (load.writes.get(effect.key) ?? Promise.resolve())
281      .then(async () => {
282        const stored: StoreRead = isReadFirst(effect)
283          ? await storedOf($, effect.key)
284          : { isRead: false }
285        const write = writeOf(effect, stored)
286        if (write !== null) await $.store.set(write.key, write.value)
287      })
288      .catch((error: unknown) => {
289        $.ui.log(`could not save ${effect.key}: ${String(error)}`, {
290          to: 'debug',
291        })
292      })
293    load.writes.set(effect.key, queued)
294    landed.push(queued)
295  }
296  return Promise.all(landed)
297}
298
299// What the store holds under a key now, or that it couldn't be read.
300async function storedOf($: EngineInterface, key: string): Promise<StoreRead> {
301  try {
302    return { isRead: true, value: await $.store.get(key) }
303  } catch (error) {
304    $.ui.log(`could not read ${key}: ${String(error)}`, { to: 'debug' })
305    return { isRead: false }
306  }
307}
308
309// Whether a session with no choice of its own starts with the band on: as
310// `/fortywait` last left it, in any session.
311function keptOn($: EngineInterface): Promise<unknown> {
312  return keptValue($, 'isOn')
313}
314
315// A value kept from an earlier session, or undefined when it can't be read.
316async function keptValue($: EngineInterface, key: string): Promise<unknown> {
317  try {
318    return await $.store.get(key)
319  } catch (error) {
320    $.ui.log(`could not read ${key}: ${String(error)}`, { to: 'debug' })
321    return undefined
322  }
323}
324
325// The band as drawn for this render: what's beneath it on top, as it came,
326// and the game in the rows left under it, or what's beneath alone when the
327// game isn't drawn.
328async function drawn(
329  $: EngineInterface,
330  load: Load,
331  e: RenderInput<'AbovePrompt'>,
332  current: Model,
333  below: RenderElement,
334): Promise<RenderElement> {
335  const view = bandOf(current, await read($, now), {
336    columns: e.props.bodyColumns,
337    maxRows: e.props.maxRows,
338    isFullscreen: e.viewport?.isFullscreen,
339    hasSurvey: e.props.hasSurvey,
340    others: rowsTakenOf(below, e.props.bodyColumns),
341  })
342  // Drawing can't write; a board that held the keys but isn't drawn is
343  // handed back right after, once per board.
344  const epoch = handoffOf(current, view)
345  if (epoch !== null && load.handoffWaiting !== epoch) {
346    load.handoffWaiting = epoch
347    $.clock.after(0, () => {
348      observe($, load, async at => {
349        load.handoffWaiting = undefined
350        await commit($, latest => afterHandoff(latest, epoch))
351        await settled($, load, at)
352      })
353    })
354  }
355  if (view.kind === 'none') return below
356  const ui = $.ui.resolve(e) as Parameters<typeof bandTree>[0]
357  const own = bandTree(ui, view)
358  return stackedTree(ui, below, own)
359}
360
361/**
362 * Wires the mod into Claude Code. At each session start or reload it offers
363 * `/fortywait` and restores the kept choice, and restores again when Claude
364 * Code empties the session's state; `/fortywait` switches the band on or off.
365 * Turn, tool and dialog events move the band's model, one after another in
366 * the order they came, while each event goes on unchanged and unheld; the
367 * ticker moves the band's time, and the band above the prompt draws it.
368 */
369export const register: Register = on => {
370  const load: Load = {
371    section: Promise.resolve(),
372    writes: new Map(),
373    timer: undefined,
374    isTickWaiting: false,
375    handoffWaiting: undefined,
376    isRestoreWaiting: false,
377    editWaiting: undefined,
378    isFaulted: false,
379    isRestored: false,
380  }
381
382  on('session.start', ($, e, next) => {
383    // The restore takes its place in line before anything can wait on I/O,
384    // so a `/fortywait` run meanwhile comes after it; the session starts at
385    // once, whatever the restore waits on.
386    void inOrder(load, async () => {
387      try {
388        await $.command.register({
389          name: 'fortywait',
390          description:
391            'Turn Twenty Forty-Wait on or off: a game above the prompt while Claude works',
392          immediate: true,
393        })
394        await restored($, load)
395      } catch {
396        await faulted($, load)
397      }
398    })
399    return next(e)
400  }).catch(($, e, next) => next(e))
401
402  on('command.run', { command: 'fortywait' }, async $ => {
403    const { step, written } = await inOrder(load, async () => {
404      await rescued($, load)
405      const at = await $.clock.now()
406      const step = await commit($, afterToggle)
407      load.isFaulted = false
408      $.ui.invalidate('ui.render')
409      const written = runEffects($, load, step.effects)
410      await settled($, load, at).catch(() => faulted($, load))
411      return { step, written }
412    })
413    await written
414    return { text: toggleNoteOf(step.model) }
415  })
416
417  on('turn.start', ($, e, next) => {
418    observe($, load, async at => {
419      await commit($, current =>
420        afterTurnStart(current, { turnId: e.turnId, at }),
421      )
422      await settled($, load, at)
423    })
424    return next(e)
425  }).catch(($, e, next) => next(e))
426
427  on('tool.call', ($, e, next) => {
428    const call = { tool: String(e.tool), input: e }
429    // The turn this call ran in, so its result counts only there.
430    let turn: TurnKey | null = null
431    observe($, load, async at => {
432      const step = await commit($, current =>
433        afterToolCall(current, { ...call, at, agentId: e.agentId }),
434      )
435      turn = turnKeyOf(step.model)
436      await settled($, load, at)
437    })
438    const result = next(e)
439    void result.then(
440      done => {
441        observe($, load, async at => {
442          await commit($, current =>
443            afterToolDone(current, {
444              ...call,
445              isOk: toolSucceeded(done),
446              turn,
447            }),
448          )
449          await settled($, load, at)
450        })
451      },
452      () => undefined,
453    )
454    return result
455  }).catch(($, e, next) => next(e))
456
457  on('turn.complete', ($, e, next) => {
458    observe($, load, async at => {
459      const step = await commit($, current =>
460        afterTurnEnd(current, {
461          turnId: e.turnId,
462          reason: e.reason,
463          answer: e.answer,
464          durationMs: e.durationMs,
465          at,
466          agentId: e.agentId,
467        }),
468      )
469      void runEffects($, load, step.effects)
470      await settled($, load, at)
471    })
472    return next(e)
473  }).catch(($, e, next) => next(e))
474
475  // A permission prompt or a question, from any loop: the dialog takes the
476  // keys from the board.
477  on('classic.PermissionRequest', ($, e, next) => {
478    observe($, load, async at => {
479      await commit($, afterDialog)
480      await settled($, load, at)
481    })
482    return next(e)
483  }).catch(($, e, next) => next(e))
484
485  // The board posted its clicks and keys. Only queued here: the count the
486  // model acknowledged reaches the board through its props at the next draw.
487  on('ui.message', { component: 'AbovePrompt' }, ($, e, next) => {
488    const post = boardPostOf(e.data)
489    if (post !== null) {
490      observe($, load, async at => {
491        const step = await commit($, current =>
492          afterBoardPost(current, e.element, post, at, Math.random),
493        )
494        void runEffects($, load, step.effects)
495        await settled($, load, at)
496      })
497    }
498    return next(e)
499  }).catch(($, e, next) => next(e))
500
501  // An edit or a send in the prompt: if the game still had the keys, the
502  // person left it in a way Claude Code doesn't report. Observed only: the
503  // edit and the prompt go on unchanged. Edits typed one after another wait
504  // in line as one, since each says the same.
505  on('prompt.edit', ($, e, next) => {
506    if (load.editWaiting === undefined) {
507      const edit = {}
508      observe($, load, async at => {
509        if (load.editWaiting === edit) load.editWaiting = undefined
510        await committedAt($, load, at, current =>
511          afterPrompt(current, { kind: 'edit' }),
512        )
513      })
514      load.editWaiting = edit
515    }
516    return next(e)
517  }).catch(($, e, next) => next(e))
518
519  on('prompt.submit', ($, e, next) => {
520    const origin = String((e.origin as { kind?: unknown } | undefined)?.kind)
521    observe($, load, async at => {
522      await committedAt($, load, at, current =>
523        afterPrompt(current, { kind: 'submit', origin }),
524      )
525    })
526    return next(e)
527  }).catch(($, e, next) => next(e))
528
529  // The board widget failed: it stops being drawn, and the keys go back.
530  on('ui.fault', ($, e, next) => {
531    observe($, load, () => faulted($, load))
532    return next(e)
533  }).catch(($, e, next) => next(e))
534
535  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
536    // Nothing of the mod is drawn until this load has restored the session's
537    // band, nor once a fault stopped it, even if that couldn't be written.
538    if (
539      (e.surface !== 'terminal' && e.surface !== 'desktop') ||
540      !load.isRestored ||
541      load.isFaulted
542    ) {
543      return next(e)
544    }
545    const current = modelOf(await read($, model))
546    // Claude Code emptied the session's state: drawing can't write, so the
547    // restore runs right after, asked for once until one lands, and Claude
548    // Code draws its own band meanwhile.
549    if (!isDealt(current.game)) {
550      if (!load.isRestoreWaiting) {
551        load.isRestoreWaiting = true
552        $.clock.after(0, () => observe($, load, () => Promise.resolve()))
553      }
554      return next(e)
555    }
556    // What other mods draw beneath this one stays, on top, and the game has
557    // the rows left under it.
558    const below = await next(e)
559    try {
560      return await drawn($, load, e, current, below)
561    } catch {
562      // The band's own drawing failed, after what's beneath was drawn:
563      // Claude Code draws that alone, and the board stops, right after.
564      $.clock.after(0, () => {
565        observe($, load, () => faulted($, load))
566      })
567      return below
568    }
569  }).catch(($, e, next) => {
570    // The band's own drawing failed: Claude Code draws its own, and the board
571    // stops, right after. A failure beneath, once handed on, is not ours, nor
572    // is a re-entry (read as a string: older Claude Code types list the kinds
573    // differently); a timeout always is, as only the hook's own time counts.
574    const kind = String(next.error.kind)
575    if ((!next.called || kind === 'timeout') && kind !== 're-entry') {
576      $.clock.after(0, () => {
577        observe($, load, () => faulted($, load))
578      })
579    }
580    return next(e)
581  })
582}
583
hooks/band.tsx 162 lines
1import type { ElementTable, RenderElement } from 'claude-code'
2
3import { BOARD_COLUMNS, SIDE_GAP } from './logic'
4import type {
5  BandView,
6  CardView,
7  MinimizedView,
8  ModeView,
9  StatusView,
10} from './logic'
11
12/** The elements the band draws with, from the surface's table. */
13export type BandElements = Pick<
14  ElementTable<'terminal'>,
15  'Box' | 'Text' | 'Client'
16>
17
18/**
19 * Draws what's beneath the band as it came, on top, and the band's own tree
20 * under it, right above the prompt. Nothing is decided here.
21 */
22export function stackedTree(
23  ui: BandElements,
24  below: RenderElement,
25  own: RenderElement,
26): RenderElement {
27  const { Box } = ui
28  return (
29    <Box flexDirection="column">
30      {below}
31      {own}
32    </Box>
33  )
34}
35
36/**
37 * Draws the band's view as it is: the board beside the card or the live
38 * view, or above it when narrow, and the mode row at the bottom, unless it
39 * has nothing to say. Each text is one row of its own, cut at the band's
40 * width, so the band is never taller than its layout. Nothing is decided here.
41 */
42export function bandTree(
43  ui: BandElements,
44  view: Exclude<BandView, { kind: 'none' }>,
45): RenderElement {
46  const { Box, Text } = ui
47  if (view.kind === 'minimized') {
48    return (
49      <Text wrap="truncate-end">
50        {headTree(ui, view.tone, view.head)}
51        <Text dimColor>{view.tail}</Text>
52      </Text>
53    )
54  }
55  const { Client } = ui
56  return (
57    <Box flexDirection="column" flexGrow={1}>
58      <Box flexDirection={view.direction} columnGap={SIDE_GAP}>
59        <Client
60          key={view.boardKey}
61          module="./board.tsx"
62          props={{
63            ...view.board,
64            frame: view.frame,
65            count: view.count,
66          }}
67          width={BOARD_COLUMNS}
68        />
69        <Box flexDirection="column" flexGrow={1}>
70          {view.card !== null
71            ? cardTree(ui, view.card)
72            : view.status !== null
73              ? statusTree(ui, view.status)
74              : null}
75        </Box>
76      </Box>
77      {view.mode.line !== '' ? modeTree(ui, view.mode) : null}
78    </Box>
79  )
80}
81
82// The mode row: highlighted while the game has the keys, dimmed otherwise.
83function modeTree(ui: BandElements, mode: ModeView): RenderElement {
84  const { Text } = ui
85  return mode.isActive ? (
86    <Text color="cyan" bold wrap="truncate-end">
87      {mode.line}
88    </Text>
89  ) : (
90    <Text dimColor wrap="truncate-end">
91      {mode.line}
92    </Text>
93  )
94}
95
96function cardTree(ui: BandElements, card: CardView): RenderElement {
97  const { Box, Text } = ui
98  return (
99    <Box flexDirection="column" flexGrow={1}>
100      <Text color="green" bold wrap="truncate-end">
101        {card.heading}
102      </Text>
103      {card.stats !== null ? (
104        <Text dimColor wrap="truncate-end">
105          {card.stats}
106        </Text>
107      ) : null}
108      {card.rows.map(row => (
109        <Text wrap="truncate-end">{row}</Text>
110      ))}
111      {card.files !== null ? (
112        <Text dimColor wrap="truncate-end">
113          {card.files}
114        </Text>
115      ) : null}
116    </Box>
117  )
118}
119
120// A state's first words in its tone: plain while Claude works, the banner
121// while a turn's end fades (Claude Code's warning colour, bold, is for this
122// warning alone), the card's heading, or dim while idle.
123function headTree(
124  ui: BandElements,
125  tone: MinimizedView['tone'],
126  text: string,
127): RenderElement {
128  const { Text } = ui
129  return tone === 'working' ? (
130    <Text>{text}</Text>
131  ) : tone === 'waiting' ? (
132    <Text color="warning" bold>
133      {text}
134    </Text>
135  ) : tone === 'card' ? (
136    <Text color="green" bold>
137      {text}
138    </Text>
139  ) : (
140    <Text dimColor>{text}</Text>
141  )
142}
143
144function statusTree(ui: BandElements, status: StatusView): RenderElement {
145  const { Box, Text } = ui
146  return (
147    <Box flexDirection="column" flexGrow={1}>
148      <Text wrap="truncate-end">
149        {headTree(ui, status.tone, status.headline)}
150      </Text>
151      {status.current.map(line => (
152        <Text wrap="truncate-end">{line}</Text>
153      ))}
154      {status.before.map(line => (
155        <Text dimColor wrap="truncate-end">
156          {line}
157        </Text>
158      ))}
159    </Box>
160  )
161}
162
hooks/logic.ts 1716 lines
1import type { Activity, Game, Mode, Model, StopReason, Turn } from '../types'
2import { canMove, newGame, played } from './game'
3import type { Move, Random } from './game'
4
5// Types and constants
6
7/**
8 * The shape tag the model is kept under in `$.state`. A value kept under
9 * another tag reads as absent. Bump it only for a change `modelOf` can't read
10 * (an added field keeps the tag and gets its default), and only in a release.
11 */
12export const MODEL_SHAPE = 'model/1'
13
14/** Cells on the 4×4 board. */
15export const CELLS = 16
16
17// More than any real game can hold, and far below where numbers stop being
18// exact: merges never raise the sum, so play stays exact from any board kept.
19const MAX_BOARD_SUM = 2 ** 30
20
21/** A game with no tiles, before the first ones are dealt. */
22export const EMPTY_GAME: Game = frozen({
23  cells: new Array<number>(CELLS).fill(0),
24  score: 0,
25  best: 0,
26  isOver: false,
27  hasWon: false,
28  moves: 0,
29})
30
31/** No turn seen yet. */
32export const IDLE_TURN: Turn = frozen({
33  turnId: '',
34  phase: 'idle',
35  startedAt: 0,
36  endedAt: 0,
37  tools: 0,
38  current: null,
39  recent: [],
40  files: [],
41  reason: '',
42  hasAnswer: false,
43  summary: '',
44  next: '',
45  lockAt: 0,
46  isShown: false,
47})
48
49/** The model of a fresh install: off, idle, keys with the prompt. */
50export const INITIAL_MODEL: Model = frozen({
51  isOn: false,
52  isSeeded: false,
53  turn: IDLE_TURN,
54  mode: 'input',
55  game: EMPTY_GAME,
56  board: { epoch: 0, count: 0 },
57  hasStrayKey: false,
58  isFaulted: false,
59})
60
61/** A value to keep across sessions, written after the model is committed. */
62export type SaveEffect =
63  | { kind: 'save'; key: 'isOn'; value: boolean }
64  | { kind: 'save'; key: 'best'; value: number }
65
66/**
67 * A call to the model for a handed-back turn's summary, exactly as it is to
68 * be sent; its result comes back into `afterSummary` for that turn.
69 */
70export type SummarizeEffect = {
71  kind: 'summarize'
72  turn: SummaryKey
73  request: {
74    model: string
75    system: string
76    prompt: string
77    maxTokens: number
78    timeoutMs: number
79  }
80}
81
82/** Work a transition asks the shell to do once the model is committed. */
83export type Effect = SaveEffect | SummarizeEffect
84
85/** What a transition returns: the next model, and the effects to carry out. */
86export type Step = { model: Model; effects: Effect[] }
87
88const PHASES: readonly Turn['phase'][] = ['idle', 'running', 'done']
89const REASONS: readonly StopReason[] = [
90  'answer',
91  'aborted',
92  'refusal',
93  'error',
94  '',
95]
96const MODES: readonly Mode[] = ['game', 'input']
97
98/** A turn this long or longer fades before it locks; a shorter one locks at once. */
99export const LONG_TURN_MS = 10_000
100
101/** How long a long turn's board stays playable while it fades. */
102export const FADE_MS = 5_000
103
104// The current call and the two before it.
105const RECENT = 3
106// Long enough to read, short enough for one line of the band.
107const TARGET_LENGTH = 72
108// The most characters in each line of a summary. Haiku is asked for under
109// 120, and a line somewhat over that still shows: the card wraps it and gives
110// way for it, and cuts the end only where the panel has no room left.
111const SUMMARY_PART = 200
112// Room for two lines at the cap even in dense text (paths, names, numbers,
113// about 3 characters a token), so an answer cut off by the budget is always
114// longer than two such lines, and refused rather than shown cut.
115const SUMMARY_TOKENS = 250
116// The reply the summary is made from: whole up to this many characters, and
117// past it its beginning and its end, where it says what was done, what was
118// found and what is asked.
119const SUMMARY_WHOLE = 8000
120const SUMMARY_HEAD = 2000
121// A shorter reply is as quick to read as its summary would be, and a summary
122// of it could only restate it or add to it: it isn't sent.
123const SUMMARY_MIN = 200
124const SUMMARY_TAIL = 6000
125const SUMMARY_SYSTEM =
126  'You write one- or two-line status notes for a developer glancing back at ' +
127  'their terminal.'
128const SUMMARY_ASK =
129  'In two short lines (about 15 words each, under 200 characters in all), ' +
130  'first give the gist of what was done or found, then what the developer ' +
131  'should do next: review, answer its question, test, decide. Use only what ' +
132  "the reply says: don't add work, changes or results it doesn't mention. " +
133  'There is ' +
134  'always a next step; never say nothing is needed. Plain text, no markdown. ' +
135  'Reply with exactly two lines: the gist on the first, the next step on the ' +
136  'second. Keep each line under 120 characters.'
137const EDIT_TOOLS: readonly string[] = [
138  'Edit',
139  'Write',
140  'MultiEdit',
141  'NotebookEdit',
142]
143
144// Reading state
145
146/**
147 * The model from whatever `$.state` holds. Every field is checked, and one
148 * that doesn't fit falls back to its initial value, so a value written by
149 * older code, or by a forgotten shape bump, can never break the band.
150 */
151export function modelOf(raw: unknown): Model {
152  if (!isRecord(raw)) return INITIAL_MODEL
153  const board = isRecord(raw.board) ? raw.board : {}
154  return {
155    isOn: raw.isOn === true,
156    isSeeded: raw.isSeeded === true,
157    turn: turnOf(raw.turn),
158    mode: oneOf(raw.mode, MODES, INITIAL_MODEL.mode),
159    game: gameOf(raw.game),
160    board: { epoch: countOf(board.epoch), count: countOf(board.count) },
161    hasStrayKey: raw.hasStrayKey === true,
162    isFaulted: raw.isFaulted === true,
163  }
164}
165
166/**
167 * A game from a kept value. A board that isn't 16 tiles, each 0 or a power of
168 * two from 2, or that holds more than any real game could, is dropped for an
169 * empty one; the best score survives if it is a score, and is never lower than
170 * the game's own score. Whether the game is over, or won, comes from the board
171 * itself, whatever was kept.
172 */
173export function gameOf(raw: unknown): Game {
174  if (!isRecord(raw)) return EMPTY_GAME
175  const best = countOf(raw.best)
176  const cells = tilesOf(raw.cells)
177  if (
178    cells === null ||
179    cells.reduce((sum, value) => sum + value, 0) > MAX_BOARD_SUM
180  ) {
181    return { ...EMPTY_GAME, best }
182  }
183  const score = countOf(raw.score)
184  return {
185    cells,
186    score,
187    best: Math.max(best, score),
188    isOver: !canMove(cells),
189    hasWon: raw.hasWon === true || cells.some(value => value >= 2048),
190    moves: countOf(raw.moves),
191  }
192}
193
194/**
195 * The best score to keep: the higher of the kept one and the current one. A
196 * kept value that isn't a score counts as none, so the result is never below
197 * either.
198 */
199export function bestOf(kept: unknown, current: number): number {
200  return Math.max(countOf(kept), countOf(current))
201}
202
203/** What a store read gave: the value kept, or that it couldn't be read. */
204export type StoreRead = { isRead: true; value: unknown } | { isRead: false }
205
206/**
207 * Whether what is stored must be read before this value is written: only for
208 * the best score, which may only rise. On/off is written straight away.
209 */
210export function isReadFirst(effect: SaveEffect): boolean {
211  switch (effect.key) {
212    case 'isOn':
213      return false
214    case 'best':
215      return true
216  }
217}
218
219/**
220 * What to write for a value to keep, given what the store holds now, or null
221 * to write nothing. On/off is this session's own choice and always written.
222 * The best score is written only when it beats what is stored, which another
223 * session may have raised meanwhile, and never over a store that couldn't be
224 * read, where it could lower a higher one.
225 */
226export function writeOf(
227  effect: SaveEffect,
228  stored: StoreRead,
229): SaveEffect | null {
230  switch (effect.key) {
231    case 'isOn':
232      return effect
233    case 'best': {
234      if (!stored.isRead) return null
235      const value = bestOf(stored.value, effect.value)
236      return value === stored.value ? null : { ...effect, value }
237    }
238  }
239}
240
241// On and off
242
243/**
244 * `/fortywait`: switches the band on or off and keeps the choice for later
245 * sessions; the choice is this session's from now on. Either way the keys
246 * belong to the prompt; switching off also retires the board, so a board
247 * drawn before can't take keys again. The band doesn't follow turns while
248 * off, so switching on starts from an idle turn, with no stale card, whose
249 * board waits for the next turn; it also clears a fault.
250 */
251export function afterToggle(model: Model): Step {
252  const isOn = !model.isOn
253  return {
254    model: {
255      ...model,
256      isOn,
257      isSeeded: true,
258      mode: 'input',
259      hasStrayKey: false,
260      board: isOn ? model.board : retired(model.board),
261      turn: isOn ? IDLE_TURN : model.turn,
262      isFaulted: isOn ? false : model.isFaulted,
263    },
264    effects: [{ kind: 'save', key: 'isOn', value: isOn }],
265  }
266}
267
268/**
269 * A session starts, the mod reloads, or Claude Code emptied the session's
270 * state (see `isDealt`). The session's own on/off stands; only a session that
271 * has none yet takes it from the store, on if it was last switched on. The
272 * best score is the higher of the kept one and the model's, and a best the
273 * store doesn't have yet (its save failed) is saved again.
274 * A session with no game yet gets a new one; a reload keeps the game as it
275 * was, with no new tile. A reload also drops any board that held the keys, so
276 * the keys are the prompt's and the board is retired, and new code gets a
277 * fresh chance after a fault; the turn, and any lock, carry on.
278 */
279export function afterRestore(
280  model: Model,
281  kept: { isOn: unknown; best: unknown },
282  random: Random,
283): Step {
284  const best = bestOf(kept.best, model.game.best)
285  return {
286    model: {
287      ...model,
288      isOn: model.isSeeded ? model.isOn : kept.isOn === true,
289      isSeeded: true,
290      mode: 'input',
291      hasStrayKey: false,
292      isFaulted: false,
293      board: retired(model.board),
294      game: dealt({ ...model.game, best }, random),
295    },
296    effects:
297      best > bestOf(kept.best, 0)
298        ? [{ kind: 'save', key: 'best', value: best }]
299        : [],
300  }
301}
302
303/**
304 * Whether the game has its tiles. Every restore deals, and nothing after it
305 * takes the last tile away, so a board without tiles, once restored, means
306 * Claude Code emptied the session's state: a `/clear` or a resume inside
307 * Claude Code, which no session start follows.
308 */
309export function isDealt(game: Game): boolean {
310  return game.cells.some(value => value !== 0)
311}
312
313/** What `/fortywait` answers. */
314export function toggleNoteOf(model: Model): string {
315  return model.isOn
316    ? 'Twenty Forty-Wait on. Run /fortywait again to turn it off.'
317    : 'Twenty Forty-Wait off.'
318}
319
320// Turn and hand-back
321
322/** A turn of the main loop starting, as the shell saw it. */
323export type TurnStart = { turnId: string; at: number; agentId?: string }
324
325/** A tool call, from the main loop or a subagent, as the shell saw it. */
326export type ToolCall = {
327  tool: string
328  input: unknown
329  at: number
330  agentId?: string
331}
332
333/** Which turn a tool call ran in: its id and when it started. */
334export type TurnKey = { turnId: string; startedAt: number }
335
336/**
337 * A tool call that has returned: whether it succeeded, and the turn it was
338 * called in (null if none was running).
339 */
340export type ToolDone = {
341  tool: string
342  input: unknown
343  isOk: boolean
344  turn: TurnKey | null
345}
346
347/** A turn ending, as the shell saw it. */
348export type TurnEnd = {
349  turnId: string
350  reason: string
351  answer: string
352  durationMs: number
353  at: number
354  agentId?: string
355}
356
357/**
358 * A turn of the main loop starts, the person's or an automated one: the band
359 * shows it live, and any lock lifts. A lock that came due before the start is
360 * handed back first. The mode stays as it was otherwise. A repeated start of
361 * the running turn, subagent turns, and turns while the band is off change
362 * nothing.
363 */
364export function afterTurnStart(model: Model, start: TurnStart): Step {
365  if (
366    !model.isOn ||
367    start.agentId !== undefined ||
368    (model.turn.phase === 'running' && model.turn.turnId === start.turnId)
369  ) {
370    return unchanged(model)
371  }
372  const at = timeOf(start.at)
373  return {
374    model: {
375      ...locked(model, at),
376      turn: {
377        ...IDLE_TURN,
378        turnId: start.turnId,
379        phase: 'running',
380        startedAt: at,
381        isShown: true,
382      },
383    },
384    effects: [],
385  }
386}
387
388/**
389 * A tool call while a turn runs: it becomes the current call, the two before
390 * it stay in view, and the count goes up. Subagents' calls count as the
391 * turn's work. A main-loop call with no turn seen (the band was switched on
392 * mid-turn) starts following the turn from here; a subagent's never does,
393 * since it may outlive the turn.
394 */
395export function afterToolCall(model: Model, call: ToolCall): Step {
396  const isAdopted = model.turn.phase === 'idle' && call.agentId === undefined
397  if (!model.isOn || (model.turn.phase !== 'running' && !isAdopted)) {
398    return unchanged(model)
399  }
400  const activity = callOf(call.tool, call.input)
401  const turn = isAdopted
402    ? {
403        ...IDLE_TURN,
404        phase: 'running' as const,
405        startedAt: timeOf(call.at),
406        isShown: true,
407      }
408    : model.turn
409  return {
410    model: {
411      ...model,
412      turn: {
413        ...turn,
414        tools: turn.tools + 1,
415        current: activity,
416        recent: [...turn.recent, activity].slice(-RECENT),
417      },
418    },
419    effects: [],
420  }
421}
422
423/**
424 * Whether a tool call's result is a success: not refused and not an error.
425 */
426export function toolSucceeded(result: unknown): boolean {
427  return (
428    isRecord(result) && result.deny === undefined && result.isError !== true
429  )
430}
431
432/** The running turn's key, which a tool call keeps to report back to; null when none runs. */
433export function turnKeyOf(model: Model): TurnKey | null {
434  return model.isOn && model.turn.phase === 'running'
435    ? { turnId: model.turn.turnId, startedAt: model.turn.startedAt }
436    : null
437}
438
439/**
440 * A tool call returned. A file counts as changed only once an edit to it has
441 * succeeded, each file counts once, and only for the turn the call ran in:
442 * a call that outlives its turn changes nothing.
443 */
444export function afterToolDone(model: Model, done: ToolDone): Step {
445  const input = isRecord(done.input) ? done.input : {}
446  const path = input.file_path ?? input.notebook_path
447  const running = turnKeyOf(model)
448  if (
449    running === null ||
450    done.turn === null ||
451    done.turn.turnId !== running.turnId ||
452    done.turn.startedAt !== running.startedAt ||
453    !done.isOk ||
454    !EDIT_TOOLS.includes(done.tool) ||
455    typeof path !== 'string' ||
456    model.turn.files.includes(path)
457  ) {
458    return unchanged(model)
459  }
460  return {
461    model: {
462      ...model,
463      turn: { ...model.turn, files: [...model.turn.files, path] },
464    },
465    effects: [],
466  }
467}
468
469/**
470 * The main loop's turn ends, for any reason: the turn is handed back. A turn of
471 * 10 s or longer that ended on its own while the game has the keys, and isn't
472 * over, fades for 5 s, the game still playable, and then locks; any other turn
473 * locks at once (see `isLocked`): a shorter or interrupted one, one that ends
474 * in input mode or on a finished game. At the lock the keys go to the prompt
475 * and the board is retired. The turn's length is the longer of what the band
476 * saw and what Claude Code reports, so a turn followed from mid-way is timed
477 * whole. A long turn that ended normally with a reply asks the model, once, for
478 * the card's summary. The late end of an older turn (one that began before the
479 * turn handed back ended), a repeated end, subagent turns, and turns while the
480 * band is off change nothing; a turn that began after, whose start went unseen,
481 * is handed back as usual, after any lock that came due before it.
482 */
483export function afterTurnEnd(model: Model, end: TurnEnd): Step {
484  const isRunning = model.turn.phase === 'running'
485  if (
486    !model.isOn ||
487    end.agentId !== undefined ||
488    (isRunning &&
489      model.turn.turnId !== '' &&
490      model.turn.turnId !== end.turnId) ||
491    (model.turn.phase === 'done' &&
492      (end.turnId === model.turn.turnId ||
493        timeOf(end.at) - timeOf(end.durationMs) < model.turn.endedAt))
494  ) {
495    return unchanged(model)
496  }
497  const at = timeOf(end.at)
498  const length = Math.max(
499    isRunning ? at - model.turn.startedAt : 0,
500    timeOf(end.durationMs),
501  )
502  const reason = oneOf(end.reason, REASONS, '')
503  const isLong = length >= LONG_TURN_MS && reason !== 'aborted'
504  // A lock that came due before this end is handed back first, so a fade
505  // whose lock wasn't committed yet never carries the game into another.
506  const due = locked(model, at)
507  const ended: Model = {
508    ...due,
509    turn: {
510      ...due.turn,
511      turnId: end.turnId,
512      phase: 'done',
513      startedAt: Math.max(0, at - length),
514      endedAt: at,
515      current: null,
516      reason,
517      hasAnswer: end.answer.trim().length > 0,
518      summary: '',
519      next: '',
520      lockAt: isLong ? at + FADE_MS : at,
521      isShown: true,
522    },
523  }
524  // Only a turn that ended normally with a reply long enough to need one: for
525  // any other stop the reason is the news, and its placeholder already says
526  // what to do. The reply is counted as the request reads it, in code points.
527  const isSummarized =
528    !model.isFaulted &&
529    isLong &&
530    reason === 'answer' &&
531    Array.from(end.answer.trim()).length >= SUMMARY_MIN
532  return {
533    model: locked(ended, at),
534    effects: isSummarized
535      ? [
536          {
537            kind: 'summarize',
538            turn: { turnId: end.turnId, endedAt: at },
539            request: summaryRequestOf(end.answer),
540          },
541        ]
542      : [],
543  }
544}
545
546/**
547 * Time passes: once a fading turn's lock is due, the lock takes effect and
548 * holds from then on, and a board that had the keys gives them to the prompt.
549 */
550export function afterTick(model: Model, now: number): Step {
551  return { model: locked(model, timeOf(now)), effects: [] }
552}
553
554/**
555 * Whether the board is locked behind the your-turn card: a turn handed back,
556 * once its fade is over, and at once while the game doesn't have the keys or
557 * is over. The fade is only for finishing a move, so a turn that ends in
558 * input mode or on a finished game locks as it ends, and leaving the game, or
559 * the move that ends it, ends the fade.
560 */
561export function isLocked(model: Model, now: number): boolean {
562  return (
563    model.isOn &&
564    model.turn.phase === 'done' &&
565    model.turn.isShown &&
566    (timeOf(now) >= model.turn.lockAt ||
567      model.mode !== 'game' ||
568      model.game.isOver)
569  )
570}
571
572/**
573 * Whether the board can be played: while a turn runs, and while a turn's end
574 * fades with the game still on. Idle, before any turn or since the band was
575 * switched on, and once locked, it can't be entered: the game is for the
576 * wait, and Claude isn't working.
577 */
578export function isPlayable(model: Model, now: number): boolean {
579  return (
580    model.isOn &&
581    (model.turn.phase === 'running' ||
582      (model.turn.phase === 'done' &&
583        model.turn.isShown &&
584        !isLocked(model, now)))
585  )
586}
587
588/**
589 * How far the board has faded, from 0 to 1: 0 while a turn runs, rising over
590 * a turn's end in the game, and 1 while the board can't be played, idle or
591 * locked. Off, nothing is drawn, and it is 0.
592 */
593export function fadeOf(model: Model, now: number): number {
594  if (!model.isOn || model.turn.phase === 'running') return 0
595  if (!isPlayable(model, now)) return 1
596  return Math.min(
597    1,
598    Math.max(0, 1 - (model.turn.lockAt - timeOf(now)) / FADE_MS),
599  )
600}
601
602/**
603 * Whether the band needs the clock: while the board can be played, a turn
604 * runs or its end fades in the game. A faulted band draws nothing and needs
605 * none.
606 */
607export function isTicking(model: Model, now: number): boolean {
608  return !model.isFaulted && isPlayable(model, now)
609}
610
611/** Whether the band shows the time at all, so time is worth keeping: on, and not stopped by a fault. */
612export function isTimeShown(model: Model): boolean {
613  return model.isOn && !model.isFaulted
614}
615
616// Lifecycle
617
618/**
619 * Claude Code shows a permission prompt or a question: the dialog takes the
620 * keys, so they leave the board and the board is retired. While a turn runs
621 * nothing locks, and the turn's end hands back as usual; during a turn's end,
622 * leaving the game ends the fade (see `isLocked`).
623 */
624export function afterDialog(model: Model): Step {
625  if (!model.isOn) return unchanged(model)
626  return { model: toPrompt(model), effects: [] }
627}
628
629/**
630 * A part of the mod failed: the band stops drawing the board, the keys go to
631 * the prompt and the board is retired, so nothing can keep them.
632 */
633export function afterFault(model: Model): Step {
634  return { model: { ...toPrompt(model), isFaulted: true }, effects: [] }
635}
636
637/**
638 * The band found, while drawing, that the board can't keep the keys (it has
639 * no room, or a survey took the band) and drew it out. If the game still held
640 * the keys under the board it saw, they go to the prompt and that board is
641 * retired; otherwise the request is stale and changes nothing.
642 */
643export function afterHandoff(model: Model, epochSeen: number): Step {
644  return model.mode === 'game' && model.board.epoch === epochSeen
645    ? { model: toPrompt(model), effects: [] }
646    : unchanged(model)
647}
648
649// Mode and board input
650
651/** The board widget's key under the current epoch; input counts only from it. */
652export function boardKeyOf(model: Model): string {
653  return `board-${model.board.epoch}`
654}
655
656/** One input the board saw: a click on it, or a key while it had the keys. */
657export type BoardInput =
658  | { kind: 'click' }
659  | { kind: 'key'; key: string; ctrl: boolean; shift: boolean; meta: boolean }
660
661/**
662 * What the board posts: every input not yet acknowledged, oldest first, and
663 * the number of the last one. The board numbers its inputs on from the count
664 * it was last acknowledged, so an input sent again keeps its number and
665 * counts once. An entry that is no input is null: it takes its number and
666 * changes nothing.
667 */
668export type BoardPost = { count: number; inputs: (BoardInput | null)[] }
669
670/** The most inputs the board keeps unacknowledged, and so sends in one post. */
671export const BOARD_BACKLOG = 64
672
673/** The post the board sent, or null when the data isn't one. */
674export function boardPostOf(data: unknown): BoardPost | null {
675  if (!isRecord(data) || !Array.isArray(data.inputs)) return null
676  const inputs: unknown[] = data.inputs
677  const count = data.count
678  return typeof count === 'number' &&
679    Number.isSafeInteger(count) &&
680    count >= inputs.length &&
681    inputs.length <= BOARD_BACKLOG
682    ? { count, inputs: Array.from(inputs, inputOf) }
683    : null
684}
685
686/**
687 * The board posted. A lock that came due takes effect first. Only the board
688 * drawn now is heard: a post from a retired board changes nothing, nor does
689 * an input counted before, so a backlog sent again applies once. A post that
690 * starts past the next number lost inputs the board couldn't keep, maybe a
691 * Tab, so the keys go back to the prompt and the board is retired. Inputs
692 * apply in order until one hands the keys back, which retires the board and
693 * drops the rest; otherwise the count acknowledged moves to the post's last.
694 *
695 * - A click enters the game, and a click on a finished game deals a new one,
696 *   while the board can be played: a turn runs, or its end fades in the
697 *   game. A click on the board otherwise, idle or locked, can't be refused,
698 *   since it moves the keys itself, so the board is retired and the keys go
699 *   straight back.
700 * - In the game, the arrows play, also while a turn's end fades; Tab hands
701 *   the keys back. Any other key, Claude Code's shortcuts included, only
702 *   shows how to leave.
703 * - A key while the model says the prompt has the keys means the board got
704 *   them unseen: they go back, so the band and the keys agree again.
705 */
706export function afterBoardPost(
707  model: Model,
708  element: string,
709  post: BoardPost,
710  now: number,
711  random: Random,
712): Step {
713  const due = locked(model, now)
714  if (!due.isOn || due.isFaulted || element !== boardKeyOf(due)) {
715    return unchanged(due)
716  }
717  const first = post.count - post.inputs.length + 1
718  if (first > due.board.count + 1) return { model: toPrompt(due), effects: [] }
719  let current = due
720  let effects: Effect[] = []
721  for (const [k, input] of post.inputs.entries()) {
722    if (input === null || first + k <= due.board.count) continue
723    const step = afterInput(current, input, now, random)
724    current = step.model
725    effects = [
726      ...effects.filter(
727        kept =>
728          !step.effects.some(
729            e =>
730              e.kind === 'save' && kept.kind === 'save' && e.key === kept.key,
731          ),
732      ),
733      ...step.effects,
734    ]
735    if (current.board.epoch !== due.board.epoch) {
736      return { model: current, effects }
737    }
738  }
739  return post.count > current.board.count
740    ? {
741        model: { ...current, board: { ...current.board, count: post.count } },
742        effects,
743      }
744    : { model: current, effects }
745}
746
747/** Activity in the prompt box: an edit, or a prompt submitted from somewhere. */
748export type PromptActivity =
749  { kind: 'edit' } | { kind: 'submit'; origin: string }
750
751/**
752 * The person used the prompt box while the model still gives the game the
753 * keys: they left the board in a way Claude Code doesn't report (Esc, a click
754 * on the prompt, Ctrl+C while idle, collapsing the band), and the band shows
755 * input mode from now on; during a turn's end that also ends the fade. The
756 * board isn't retired: the keys already left it, and a click on it right
757 * after must still count while a turn runs. A prompt from anywhere but
758 * the person's own typing (a notification, a peer, a plugin) says nothing
759 * about the keys.
760 */
761export function afterPrompt(model: Model, activity: PromptActivity): Step {
762  const isTyped = activity.kind === 'edit' || activity.origin === 'composer'
763  if (model.mode !== 'game' || !isTyped) return unchanged(model)
764  return {
765    model: { ...model, mode: 'input', hasStrayKey: false },
766    effects: [],
767  }
768}
769
770// Card text
771
772/** The model's summary for the card: what Claude did, and the next step. */
773export type Summary = { line: string; next: string }
774
775/**
776 * Which handed-back turn a summary is for: its id and when it ended, so a
777 * summary never lands on another turn, even one that reuses the id.
778 */
779export type SummaryKey = { turnId: string; endedAt: number }
780
781/**
782 * What the model is asked for a summary: its instructions, and Claude's final
783 * reply, nothing else. A reply of up to 8,000 characters goes whole; a longer
784 * one as its first 2,000 and last 6,000, with a mark where its middle is left
785 * out, so nothing is sent twice. It is cut between characters, never inside
786 * one. The token budget holds two lines at the cap even in dense text, so an
787 * answer the budget cut off is always too long for the card to accept.
788 */
789export function summaryRequestOf(answer: string): SummarizeEffect['request'] {
790  const characters = Array.from(answer.trim())
791  const reply =
792    characters.length <= SUMMARY_WHOLE
793      ? `The assistant's final reply:\n\n${characters.join('')}`
794      : "The beginning and the end of the assistant's final reply, with its " +
795        `middle left out:\n\n${characters.slice(0, SUMMARY_HEAD).join('')}` +
796        `\n\n[… the middle of the reply is left out …]\n\n` +
797        characters.slice(-SUMMARY_TAIL).join('')
798  return {
799    model: 'haiku',
800    system: SUMMARY_SYSTEM,
801    prompt: `${SUMMARY_ASK}\n\n${reply}`,
802    maxTokens: SUMMARY_TOKENS,
803    timeoutMs: 20_000,
804  }
805}
806
807/**
808 * A summary from the model, fit for the card: answered, and exactly two
809 * lines, the gist and the next step, each non-empty and at most 200
810 * characters, for the card to wrap. Its words are not judged. Markdown
811 * emphasis, list marks (arrows too) and labels such as "Gist:" or "Next:"
812 * are dropped.
813 * Otherwise null, and the card shows the placeholder.
814 */
815export function summaryOf(result: unknown): Summary | null {
816  if (!isRecord(result) || result.isAnswered !== true) return null
817  const lines = textOf(result.text)
818    .split(/\r?\n/)
819    .map(line =>
820      line
821        .replace(/\*\*|__|`/g, '')
822        .replace(/^\s*(?:#+|[-*•→]|->|\d+[.)])\s+/, '')
823        .replace(
824          /^\s*(?:summary|gist|done|did|result|what (?:claude )?did|next(?:\s+step)?)\s*:\s*/i,
825          '',
826        )
827        .replace(/\s+/g, ' ')
828        .trim(),
829    )
830    .filter(line => line.length > 0)
831  const [line = '', next = ''] = lines
832  return lines.length !== 2 ||
833    Array.from(line).length > SUMMARY_PART ||
834    Array.from(next).length > SUMMARY_PART
835    ? null
836    : { line, next }
837}
838
839/**
840 * The model's summary arrives for a turn. It is shown only if it is a usable
841 * summary, the band is on, and that turn, by its id and end, is still the one
842 * handed back, so a late summary never lands on another turn's card.
843 */
844export function afterSummary(
845  model: Model,
846  turn: SummaryKey,
847  result: unknown,
848): Step {
849  const summary = summaryOf(result)
850  return summary !== null &&
851    model.isOn &&
852    model.turn.phase === 'done' &&
853    model.turn.turnId === turn.turnId &&
854    model.turn.endedAt === turn.endedAt
855    ? {
856        model: {
857          ...model,
858          turn: { ...model.turn, summary: summary.line, next: summary.next },
859        },
860        effects: [],
861      }
862    : unchanged(model)
863}
864
865/**
866 * The card's line when there is no summary: a fixed sentence for how the turn
867 * ended, always ending in the next step. Claude's answer itself never shows.
868 */
869export function placeholderOf(turn: Turn): string {
870  if (turn.reason === 'aborted') return 'Interrupted — your turn.'
871  if (turn.reason === 'refusal') {
872    return 'Claude declined — your turn: read why and rephrase.'
873  }
874  if (turn.reason === 'error') {
875    return 'Stopped on an error — your turn: check the transcript.'
876  }
877  return turn.reason === 'answer' && turn.hasAnswer
878    ? 'Claude finished — your turn: read the reply.'
879    : 'Stopped without a reply — your turn.'
880}
881
882// View models
883
884/**
885 * The card shown when the turn is handed back, row by row: the heading, the
886 * stats, the summary as one paragraph and the files changed. When the
887 * paragraph needs more rows than are left, the files give way first, then
888 * the stats join the heading, so the card never takes more rows than it has.
889 */
890export type CardView = {
891  heading: string
892  /** The stats row, or null once the stats joined the heading. */
893  stats: string | null
894  /** The paragraph, whole, and as drawn: word-wrapped to its rows. */
895  text: string
896  rows: string[]
897  /** The files changed, or null once their row gave way. */
898  files: string | null
899}
900
901/** Where the card or the live view is drawn: its width, and the rows it has. */
902export type SideRoom = { width: number; rows: number }
903
904// A side of any width, and a card's usual rows: the band cuts each row.
905const WIDE: SideRoom = { width: Number.POSITIVE_INFINITY, rows: 5 }
906
907/**
908 * The live view of a turn, or the idle line before any turn. Its tone says
909 * how the first row is drawn: plain while Claude works, as a banner while a
910 * turn's end fades (the only time the band warns), and dim while idle. Then
911 * come the rows that matter now, and dimmer ones under them.
912 */
913export type StatusView = {
914  tone: 'working' | 'waiting' | 'idle'
915  headline: string
916  current: string[]
917  before: string[]
918}
919
920/**
921 * Where the band is drawn: its body columns, the rows the engine lets it take,
922 * whether the terminal is fullscreen (unknown counts as not), whether a
923 * Claude Code survey holds the band, and the rows other mods' content above
924 * the game takes, none when there is none.
925 */
926export type Room = {
927  columns: number
928  maxRows: number
929  isFullscreen?: boolean
930  hasSurvey?: boolean
931  others?: number
932}
933
934/**
935 * The band's bottom row: where the keys go and how to switch, or an empty
936 * line when the card already says it, and then the row isn't drawn. This
937 * row is highlighted while the game has the keys, and dimmed otherwise; the
938 * tiles never dim for the mode, only for the turn: dimming step by step over
939 * a turn's end, and grey while the board can't be played, idle or locked.
940 */
941export type ModeView = { line: string; isActive: boolean }
942
943/**
944 * How the board's frame is drawn: highlighted while the model gives the game
945 * the keys, the fade's warning while a turn's end fades with the game on, and
946 * dim otherwise. Like the mode row, it follows the mode the model knows, so
947 * after Esc or a prompt click it changes at the first edit or send.
948 */
949export type Frame = 'game' | 'fade' | 'dim'
950
951/**
952 * The minimized band: one line whose head shows the state, drawn in its
953 * tone as the full band draws it (the live view's first row, or the card's
954 * heading), and a tail that says the rest needs more rows, then, on the
955 * card's line, its paragraph.
956 */
957export type MinimizedView = {
958  kind: 'minimized'
959  tone: StatusView['tone'] | 'card'
960  head: string
961  tail: string
962}
963
964/**
965 * What the band draws: nothing, one minimized line, the full band with the
966 * board.
967 */
968export type BandView =
969  | { kind: 'none' }
970  | MinimizedView
971  | {
972      kind: 'full'
973      direction: 'row' | 'column'
974      status: StatusView | null
975      card: CardView | null
976      board: BoardView
977      /** The board widget's key, and the count of its inputs acknowledged. */
978      boardKey: string
979      count: number
980      frame: Frame
981      mode: ModeView
982    }
983
984/** Body columns from which the board and the activity sit side by side. */
985export const ROW_COLUMNS = 64
986
987/** Columns between the board and the card or live view beside it. */
988export const SIDE_GAP = 2
989
990/** Columns the board takes: four 6-column tiles, a column between each, and its frame. */
991export const BOARD_COLUMNS = 29
992
993// Rows each part of the band takes, as the band draws them: the board in its
994// frame with its caption row, the card or the live view, and the mode row,
995// the band's bottom row.
996const BOARD_ROWS = 7
997const SIDE_ROWS = 5
998const MODE_ROWS = 1
999
1000// What the one-line view says after the state: how to see the rest.
1001const ONE_LINE_HINT = ' · more rows show the game'
1002
1003/**
1004 * The rows the full band takes: the board beside the card or live view, or
1005 * above it when narrow, and the mode row under both. Behind the card the band
1006 * draws no mode row and takes a row fewer, but the count keeps it, so the band
1007 * doesn't turn to one line and back as the card comes and goes.
1008 */
1009export function bandRowsOf(direction: 'row' | 'column'): number {
1010  const body =
1011    direction === 'row'
1012      ? Math.max(BOARD_ROWS, SIDE_ROWS)
1013      : BOARD_ROWS + SIDE_ROWS
1014  return body + MODE_ROWS
1015}
1016
1017/**
1018 * The card for a turn handed back: how long it took and what it did, then
1019 * the summary as one paragraph, word-wrapped over the rows it needs in the
1020 * room's width, so it reads in full. Only when the paragraph needs more rows
1021 * than the heading, the stats and the files leave do the files give way, and
1022 * then the stats join the heading in short; where even that isn't enough,
1023 * the paragraph ends where the band cuts it.
1024 */
1025export function cardOf(turn: Turn, room: SideRoom = WIDE): CardView {
1026  const count = turn.files.length
1027  const elapsed = elapsedText(turn.endedAt - turn.startedAt)
1028  const text = paragraphOf(turn)
1029  const needed = rowsOf(text, room.width)
1030  const isFilesShown = needed <= room.rows - 3
1031  const isStatsJoined = needed > room.rows - 2
1032  const rows = wrapped(text, {
1033    width: room.width,
1034    rows: Math.max(1, room.rows - (isStatsJoined ? 1 : isFilesShown ? 3 : 2)),
1035  })
1036  return {
1037    heading: isStatsJoined
1038      ? `✓ YOUR TURN · ${elapsed} · ${plural(turn.tools, 'tool')} · ${plural(count, 'file')}`
1039      : '✓ YOUR TURN',
1040    stats: isStatsJoined
1041      ? null
1042      : `${elapsed} · ${plural(turn.tools, 'tool call')} · ${plural(count, 'file')} changed`,
1043    text,
1044    rows,
1045    files: isFilesShown
1046      ? turn.files.map(path => oneLine(shortPath(path))).join('  ')
1047      : null,
1048  }
1049}
1050
1051/**
1052 * The summary as one plain paragraph: the gist, then the next step, each a
1053 * sentence of its own; or, with no summary, the placeholder for how the turn
1054 * ended, whole.
1055 */
1056export function paragraphOf(turn: Turn): string {
1057  return turn.summary.length > 0
1058    ? `${sentenceOf(turn.summary)} ${sentenceOf(turn.next)}`
1059    : placeholderOf(turn)
1060}
1061
1062// A sentence: its first letter capitalized, after any opening quote or
1063// bracket, and a full stop unless it already ends in one, a question or
1064// exclamation mark or an ellipsis (closing quotes or brackets after it
1065// included).
1066function sentenceOf(text: string): string {
1067  const sentence = text.replace(
1068    /^([\p{Ps}\p{Pi}"']*)(\p{Ll})/u,
1069    (_, opening: string, letter: string) => opening + letter.toUpperCase(),
1070  )
1071  return /[.!?…][\p{Pe}\p{Pf}"']*$/u.test(sentence) ? sentence : `${sentence}.`
1072}
1073
1074/**
1075 * The live view: idle before any turn, the running turn's work, or the wait
1076 * before a lock, its banner in the words that fit the room's width.
1077 */
1078export function statusOf(
1079  model: Model,
1080  now: number,
1081  room: SideRoom = WIDE,
1082): StatusView {
1083  const turn = model.turn
1084  if (turn.phase === 'running') {
1085    return {
1086      tone: 'working',
1087      headline: `● Claude is working · ${elapsedText(timeOf(now) - turn.startedAt)} · ${plural(turn.tools, 'tool call')}`,
1088      current: [
1089        turn.current === null
1090          ? 'thinking…'
1091          : `${turn.current.tool}  ${turn.current.target}`.trimEnd(),
1092      ],
1093      before: turn.recent
1094        .slice(0, -1)
1095        .slice(-(RECENT - 1))
1096        .map(call => `${call.tool}  ${call.target}`.trimEnd()),
1097    }
1098  }
1099  if (turn.phase === 'done' && turn.isShown) {
1100    const left = Math.max(1, Math.ceil((turn.lockAt - timeOf(now)) / 1000))
1101    // Under the banner, the paragraph wraps as on the card, over the rest.
1102    return {
1103      tone: 'waiting',
1104      headline: bannerOf(left, room.width),
1105      current: wrapped(paragraphOf(turn), {
1106        width: room.width,
1107        rows: Math.max(1, room.rows - 1),
1108      }),
1109      before: [],
1110    }
1111  }
1112  return {
1113    tone: 'idle',
1114    headline: "No activity yet · Claude's work shows here",
1115    current: [],
1116    before: [],
1117  }
1118}
1119
1120/**
1121 * The band for this moment and this room. Off, after a fault, or while a
1122 * survey holds the band, it draws nothing. With room it
1123 * draws the board beside the live view or the card, or above it when narrow;
1124 * without room the whole band shrinks to one line. Under other mods' content
1125 * the game has the rows left, by the same rule, and with not one row left
1126 * it draws nothing.
1127 */
1128export function bandOf(model: Model, now: number, room: Room): BandView {
1129  if (!model.isOn || model.isFaulted || room.hasSurvey === true) {
1130    return { kind: 'none' }
1131  }
1132  const others = Math.max(0, room.others ?? 0)
1133  if (others > 0 && room.maxRows - others < 1) return { kind: 'none' }
1134  const left = others > 0 ? { ...room, maxRows: room.maxRows - others } : room
1135  const isCardShown = isLocked(model, now)
1136  const direction = room.columns >= ROW_COLUMNS ? 'row' : 'column'
1137  const isRoomy =
1138    fits(bandRowsOf(direction), left) && room.columns >= BOARD_COLUMNS
1139  if (!isRoomy) {
1140    // The hint comes right after the state, so the band cuts what follows it
1141    // rather than the hint. The fade's banner takes the form that leaves the
1142    // hint its room, and the card's paragraph follows only where there is a
1143    // column past the hint, so the cut's ellipsis never lands on it. Below
1144    // 37 columns, and on the working and idle lines, whose state is fixed,
1145    // the hint can still be cut.
1146    const status = statusOf(model, now, {
1147      width: room.columns - ONE_LINE_HINT.length,
1148      rows: SIDE_ROWS,
1149    })
1150    const heading = '✓ YOUR TURN'
1151    return isCardShown
1152      ? {
1153          kind: 'minimized',
1154          tone: 'card',
1155          head: heading,
1156          tail:
1157            room.columns > heading.length + ONE_LINE_HINT.length
1158              ? `${ONE_LINE_HINT} · ${paragraphOf(model.turn)}`
1159              : ONE_LINE_HINT,
1160        }
1161      : {
1162          kind: 'minimized',
1163          tone: status.tone,
1164          head: status.headline,
1165          tail: ONE_LINE_HINT,
1166        }
1167  }
1168  // Beside the board the card has the board's rows, so the board and the
1169  // card keep their height; under it, the card's own, at the band's width.
1170  const side: SideRoom =
1171    direction === 'row'
1172      ? { width: room.columns - BOARD_COLUMNS - SIDE_GAP, rows: BOARD_ROWS }
1173      : { width: room.columns, rows: SIDE_ROWS }
1174  return {
1175    kind: 'full',
1176    direction,
1177    status: isCardShown ? null : statusOf(model, now, side),
1178    card: isCardShown ? cardOf(model.turn, side) : null,
1179    board: boardOf(model.game, fadeOf(model, now)),
1180    boardKey: boardKeyOf(model),
1181    count: model.board.count,
1182    frame: frameOf(model, now),
1183    mode: modeOf(model, now),
1184  }
1185}
1186
1187/** The board's frame: the game's while it has the keys, the fade's while a turn's end fades in the game, dim otherwise. */
1188export function frameOf(model: Model, now: number): Frame {
1189  if (model.mode !== 'game' || !isPlayable(model, now)) return 'dim'
1190  return model.turn.phase === 'done' ? 'fade' : 'game'
1191}
1192
1193/**
1194 * The mode row: when the board can't be played, none behind the card, which
1195 * already says it's your turn, or that the board waits for Claude while idle;
1196 * in the game, the move to finish during a turn's end, which already says how
1197 * to leave, and otherwise the arrows, or how to leave after a stray key; or
1198 * the prompt, where a finished game says a click deals a new one.
1199 */
1200export function modeOf(model: Model, now: number): ModeView {
hooks/board.tsx 107 lines
1import type { ClientModule, ClientSurface, JsonValue } from 'claude-code'
2
3import { BOARD_BACKLOG } from './logic'
4import type { BoardProps, Tile } from './logic'
5
6// The inputs not yet acknowledged, each with its number, and the last number
7// given. Dropped with the instance; a new one numbers on from the count the
8// model acknowledged, which its props carry.
9type Local = { last: number; backlog: { number: number; input: JsonValue }[] }
10
11const TILE_WIDTH = 6
12
13/**
14 * The 2048 board in the band: its tiles in a frame, highlighted while the
15 * game has the keys, in Claude Code's warning colour while a turn's end fades
16 * with the game on, and the scores under it. It draws its props and posts
17 * every click and key it gets; the hooks decide what they mean.
18 *
19 * Each input gets the next number and is posted at once together with every
20 * input before it the model hasn't acknowledged yet, so an input lost
21 * between frames arrives with the next one, and one sent twice counts once.
22 */
23const Board: ClientModule<BoardProps, Local> = (props, surface) => {
24  // Set again on each call, so an input is numbered and trimmed against the
25  // count acknowledged in the latest props.
26  surface.onKey(event => {
27    sent(surface, props.count, {
28      kind: 'key',
29      key: event.key,
30      ...(event.ctrl === true ? { ctrl: true } : {}),
31      ...(event.shift === true ? { shift: true } : {}),
32      ...(event.meta === true ? { meta: true } : {}),
33    })
34  })
35  surface.onPointer(event => {
36    if (event.type === 'down' && event.button === 'left') {
37      sent(surface, props.count, { kind: 'click' })
38    }
39  })
40  const { Box, Text } = surface.elements
41  const rows = [0, 1, 2, 3].map(row => (
42    <Text wrap="truncate-end">
43      {props.tiles.slice(row * 4, row * 4 + 4).flatMap((tile, column) => {
44        const drawn = tileTree(surface, tile)
45        return column === 0 ? [drawn] : [<Text> </Text>, drawn]
46      })}
47    </Text>
48  ))
49  return (
50    <Box flexDirection="column">
51      {props.frame === 'game' ? (
52        <Box flexDirection="column" borderStyle="round" borderColor="cyan">
53          {rows}
54        </Box>
55      ) : props.frame === 'fade' ? (
56        <Box flexDirection="column" borderStyle="round" borderColor="warning">
57          {rows}
58        </Box>
59      ) : (
60        <Box flexDirection="column" borderStyle="round" borderDimColor>
61          {rows}
62        </Box>
63      )}
64      <Text dimColor wrap="truncate-end">
65        {props.caption}
66      </Text>
67    </Box>
68  )
69}
70
71export default Board
72
73// Numbers the input, keeps it with the backlog not yet acknowledged, and
74// posts them all. Past the bound the oldest go, and the model, seeing the
75// gap, hands the keys back.
76function sent(
77  surface: ClientSurface<Local>,
78  acknowledged: number,
79  input: JsonValue,
80): void {
81  const kept = surface.state ?? { last: acknowledged, backlog: [] }
82  const last = Math.max(kept.last, acknowledged) + 1
83  const backlog = [
84    ...kept.backlog.filter(entry => entry.number > acknowledged),
85    { number: last, input },
86  ].slice(-BOARD_BACKLOG)
87  surface.setState({ last, backlog })
88  surface.post({ count: last, inputs: backlog.map(entry => entry.input) })
89}
90
91// One tile, its value centred in a fixed width; an empty cell in the
92// theme's own text colour, dimmed.
93function tileTree(surface: ClientSurface<Local>, tile: Tile) {
94  const { Text } = surface.elements
95  const left = Math.floor((TILE_WIDTH - tile.text.length) / 2)
96  const label = tile.text
97    .padStart(tile.text.length + Math.max(0, left))
98    .padEnd(TILE_WIDTH)
99  return tile.isEmpty ? (
100    <Text dimColor>{label}</Text>
101  ) : (
102    <Text backgroundColor={tile.background} color={tile.color} bold>
103      {label}
104    </Text>
105  )
106}
107
hooks/game.ts 125 lines
1import type { Game } from '../types'
2
3/** A direction the tiles slide in. */
4export type Move = 'left' | 'right' | 'up' | 'down'
5
6/** A source of random numbers in [0, 1), passed in so the rules stay pure. */
7export type Random = () => number
8
9const SIDE = 4
10
11// For each move, the board's lines as cell indexes, each listed from the edge
12// the tiles slide toward.
13const LINES: Record<Move, number[][]> = {
14  left: rows(),
15  right: rows().map(row => [...row].reverse()),
16  up: columns(),
17  down: columns().map(column => [...column].reverse()),
18}
19
20/** A new game: two tiles on an empty board, with the best score carried over. */
21export function newGame(best: number, random: Random): Game {
22  const empty = new Array<number>(SIDE * SIDE).fill(0)
23  return {
24    cells: spawned(spawned(empty, random), random),
25    score: 0,
26    best,
27    isOver: false,
28    hasWon: false,
29    moves: 0,
30  }
31}
32
33/**
34 * The board with one new tile in a random empty cell: a 2 nine times in ten,
35 * otherwise a 4. A full board comes back as it was.
36 */
37export function spawned(cells: readonly number[], random: Random): number[] {
38  const empty = cells.flatMap((value, at) => (value === 0 ? [at] : []))
39  const next = [...cells]
40  const at = empty[indexOf(random(), empty.length)]
41  if (at !== undefined) next[at] = random() < 0.9 ? 2 : 4
42  return next
43}
44
45/** Whether any move would change the board: an empty cell, or two equal tiles side by side. */
46export function canMove(cells: readonly number[]): boolean {
47  return [...rows(), ...columns()].some(line =>
48    line.some((at, k) => {
49      const value = cells[at] ?? 0
50      const after = line[k + 1]
51      return value === 0 || (after !== undefined && cells[after] === value)
52    }),
53  )
54}
55
56/**
57 * The game after a move. Tiles slide toward the move's edge, and each pair of
58 * equal tiles meeting there merges once into their sum, which is added to the
59 * score. Then a new tile appears. A move that changes nothing returns the very
60 * same game, with no new tile and no move counted, and so does any move on a
61 * finished game.
62 */
63export function played(game: Game, move: Move, random: Random): Game {
64  if (game.isOver) return game
65  const cells = [...game.cells]
66  let gained = 0
67  for (const line of LINES[move]) {
68    const result = slid(line.map(at => game.cells[at] ?? 0))
69    line.forEach((at, k) => {
70      cells[at] = result.values[k] ?? 0
71    })
72    gained += result.gained
73  }
74  if (cells.every((value, at) => value === game.cells[at])) return game
75  const next = spawned(cells, random)
76  const score = game.score + gained
77  return {
78    cells: next,
79    score,
80    best: Math.max(game.best, score),
81    isOver: !canMove(next),
82    hasWon: game.hasWon || next.some(value => value >= 2048),
83    moves: game.moves + 1,
84  }
85}
86
87// One line slid toward its first cell: equal neighbours merge once, left to
88// right, so 2 2 2 2 becomes 4 4 and never 8.
89function slid(line: readonly number[]): { values: number[]; gained: number } {
90  const tiles = line.filter(value => value !== 0)
91  const values: number[] = []
92  let gained = 0
93  for (let k = 0; k < tiles.length; k++) {
94    const value = tiles[k] ?? 0
95    if (tiles[k + 1] === value) {
96      values.push(value * 2)
97      gained += value * 2
98      k++
99    } else {
100      values.push(value)
101    }
102  }
103  while (values.length < line.length) values.push(0)
104  return { values, gained }
105}
106
107function rows(): number[][] {
108  return range(SIDE).map(row => range(SIDE).map(column => row * SIDE + column))
109}
110
111function columns(): number[][] {
112  return range(SIDE).map(column => range(SIDE).map(row => row * SIDE + column))
113}
114
115function range(count: number): number[] {
116  return Array.from({ length: count }, (_, k) => k)
117}
118
119// A random number turned into an index below `count`, even when the source
120// strays outside [0, 1).
121function indexOf(random: number, count: number): number {
122  if (!Number.isFinite(random)) return 0
123  return Math.min(count - 1, Math.max(0, Math.floor(random * count)))
124}
125
types/index.d.ts 60 lines
1/** One tool call as the band names it: the tool and what it works on. */
2export type Activity = { tool: string; target: string }
3
4/** Why a turn stopped, as Claude Code reports it; empty before any stop. */
5export type StopReason = 'answer' | 'aborted' | 'refusal' | 'error' | ''
6
7/** The main loop's current or last turn. */
8export type Turn = {
9  turnId: string
10  phase: 'idle' | 'running' | 'done'
11  startedAt: number
12  endedAt: number
13  tools: number
14  current: Activity | null
15  recent: Activity[]
16  files: string[]
17  reason: StopReason
18  /** Whether the turn ended with an answer, not an empty one. */
19  hasAnswer: boolean
20  /** The model's summary of what Claude did, or empty for the placeholder. */
21  summary: string
22  /** The next step the summary gives the person; set with `summary`. */
23  next: string
24  /** When the board locks; 0 once the lock has taken effect, so it holds. */
25  lockAt: number
26  isShown: boolean
27}
28
29/** A 2048 game: 16 cells in rows, 0 for an empty cell. */
30export type Game = {
31  cells: number[]
32  score: number
33  best: number
34  isOver: boolean
35  hasWon: boolean
36  moves: number
37}
38
39/** Where the keys go: the game, or the prompt. */
40export type Mode = 'game' | 'input'
41
42/** Everything the band shows and decides on, kept as one value. */
43export type Model = {
44  isOn: boolean
45  /** Whether this session's on/off is settled; until then the store seeds it. */
46  isSeeded: boolean
47  turn: Turn
48  mode: Mode
49  game: Game
50  board: { epoch: number; count: number }
51  hasStrayKey: boolean
52  isFaulted: boolean
53}
54
55declare module 'claude-code' {
56  interface PluginState {
57    'twenty-forty-wait': { model: Shaped<Model>; now: number }
58  }
59}
60