@rhizogrip/spore-cli 1.37.1 → 1.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,7 @@ spore # what each command does
13
13
  The terminal screen (`spore tui`, `spore here`) is an optional dependency: `--omit=optional` leaves it
14
14
  out, for a server, and the other commands work without it.
15
15
 
16
- Linux x64, Node 24 or newer. How spore works, what a spore is and every command:
16
+ Linux x64 or arm64, Node 24 or newer. How spore works, what a spore is and every command:
17
17
  [the spore README](https://github.com/RHIZOGRIP/spore/blob/main/packages/spore/README.md).
18
18
 
19
19
  ## License
package/bin/spore.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * Contract: ARCHITECTURE.md, "The spore command".
5
5
  *
6
6
  * This is the follower and holds nothing: `send` appends to the inbox and races
7
- * for the lock, the runner (native/spore-runner) holds the lock and the slot, and
7
+ * for the lock, the runner (native/<arch>/spore-runner) holds the lock and the slot, and
8
8
  * bin/spore-agent.js is the confined half. Ctrl-C here leaves the turn running.
9
9
  */
10
10
 
@@ -51,6 +51,7 @@ const VERBS = `Usage:
51
51
  spore wrap <name> [10m] ask the running turn to wrap up, now or in a while
52
52
  spore annotate <name> <line>|--clear a line of yours under the running turn's header, wherever it is shown
53
53
  spore model [<name> [<model>]] what it runs on and may switch to; with a model, switch from the next turn
54
+ spore flush <name> its next turn starts over, replaying nothing said before; nothing is forgotten
54
55
  spore ls [<dir>] your spores and their state
55
56
  spore tui [<name>] talk to a spore in a terminal screen; the last one opened without a name
56
57
  spore here [<dir>] the spore that works in this directory, in place — made the first time
@@ -140,6 +141,7 @@ const SHAPES = {
140
141
  wrap: withJson(2),
141
142
  annotate: withJson(ALL, 'clear'),
142
143
  model: withJson(2),
144
+ flush: withJson(1),
143
145
  define: withJson(1, 'definition', 'from', 'sporangium', 'model', 'plugin'),
144
146
  edit: withJson(1),
145
147
  remake: withJson(1),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhizogrip/spore-cli",
3
- "version": "1.37.1",
3
+ "version": "1.39.0",
4
4
  "author": {
5
5
  "name": "Christophe Le Bars",
6
6
  "email": "clb@toort.net"
@@ -32,7 +32,8 @@
32
32
  "linux"
33
33
  ],
34
34
  "cpu": [
35
- "x64"
35
+ "x64",
36
+ "arm64"
36
37
  ],
37
38
  "bin": {
38
39
  "spore": "bin/spore.js"
@@ -50,7 +51,7 @@
50
51
  "tui": "node bin/spore.js tui"
51
52
  },
52
53
  "dependencies": {
53
- "@rhizogrip/spore": "1.37.1",
54
+ "@rhizogrip/spore": "1.39.0",
54
55
  "chalk": "^6.0.1",
55
56
  "minimist": "^1.2.8"
56
57
  },
package/src/dispatch.js CHANGED
@@ -13,7 +13,7 @@ import { readFileSync } from 'node:fs'
13
13
  import { homedir } from 'node:os'
14
14
  import { basename, join, resolve } from 'node:path'
15
15
 
16
- import { addTo, allowPlugin, annotate, billingFor, bytesText, changeText, define, defaultModel, disallowPlugin, durationText, edit, extend, getKey, jobs, list, listApprovals, listPlugins, log, pause, remake, remove, removeFrom, removeSchedule, requestApproval, runningLines, schedules, setDefaultModel, setKey, setSchedule, settings, show, showPlugin, sql, stateWord, stopJob, switchModel, tick, tickReport, unmetered, unpause, upgrade, usage, wrap, CONFIG_FILE, DEFAULT_SPORANGIUM, errorText } from '@rhizogrip/spore'
16
+ import { addTo, allowPlugin, annotate, billingFor, bytesText, changeText, define, defaultModel, disallowPlugin, durationText, edit, extend, flush, getKey, jobs, list, listApprovals, listPlugins, log, pause, remake, remove, removeFrom, removeSchedule, requestApproval, runningLines, schedules, setDefaultModel, setKey, setSchedule, settings, show, showPlugin, sql, stateWord, stopJob, switchModel, tick, tickReport, unmetered, unpause, upgrade, usage, wrap, CONFIG_FILE, DEFAULT_SPORANGIUM, errorText } from '@rhizogrip/spore'
17
17
  import { diagnose, renderDoctor } from './doctor.js'
18
18
  import { hook } from './hooks.js'
19
19
  import { createServices } from './services.js'
@@ -69,6 +69,16 @@ function renderModels ({ model, models }) {
69
69
  console.log(models.length ? `or: ${models.join(', ')} — spore model <name> <model> switches from the next turn` : 'offers no other — models: [<provider>/<name>, …] in its spore.cmk adds some')
70
70
  }
71
71
 
72
+ function renderFlush ({ open }) {
73
+ console.log('flushed — its next turn starts over, replaying nothing said before; nothing is forgotten')
74
+ const still = [
75
+ ...(open.question ? [`the question it asked: ${cut(open.question)}`] : []),
76
+ ...open.approvals.map(one => `approval ${one.id} in ${one.place}, open until ${when(one.until)}`),
77
+ ...open.jobs.map(one => `job ${one.name}, running since ${when(one.startedAt)}`)
78
+ ]
79
+ if (still.length) console.log(`still open, and it is told so:\n${still.map(line => ` ${line}`).join('\n')}`)
80
+ }
81
+
72
82
  function renderSwitch ({ model, was }) {
73
83
  console.log(model === was ? `already runs on ${model}` : `runs on ${model} from the next turn — it ran on ${was}`)
74
84
  }
@@ -298,7 +308,7 @@ const cut = (text) => (text.length > 60 ? text.slice(0, 57) + '…' : text)
298
308
  const quoted = (entries) => entries.map(entry => `${saidBy(entry) ? `${saidBy(entry)}: ` : ''}${JSON.stringify(cut(saidText(entry)))}`).join(' ')
299
309
 
300
310
  function renderLog (result) {
301
- if (!result.turns.length && !result.waiting.length && !result.setAside.length && !result.held.length) {
311
+ if (!result.turns.length && !result.flushes.length && !result.waiting.length && !result.setAside.length && !result.held.length) {
302
312
  console.log('nothing has been said here yet')
303
313
  return
304
314
  }
@@ -308,10 +318,17 @@ function renderLog (result) {
308
318
  const text = `${turn.error}${detail}`
309
319
  return `${text.length > 100 ? `${text.slice(0, 99)}…` : text} `
310
320
  }
311
- for (const turn of result.turns) {
312
- console.log(` ${when(turn.endedAt)} ${(turn.stopReason || '—').padEnd(12)} ${String(turn.rounds).padStart(3)}r ` +
313
- `${('$' + turn.treeUsd.toFixed(4)).padStart(9)} ${String(turn.workUnits).padStart(4)}u ${why(turn)}${quoted(turn.asked)}`)
314
- }
321
+ const timeline = [
322
+ ...result.turns.map(turn => ({
323
+ at: turn.endedAt,
324
+ line: ` ${when(turn.endedAt)} ${(turn.stopReason || '—').padEnd(12)} ${String(turn.rounds).padStart(3)}r ` +
325
+ `${('$' + turn.treeUsd.toFixed(4)).padStart(9)} ${String(turn.workUnits).padStart(4)}u ${why(turn)}${quoted(turn.asked)}`
326
+ })),
327
+ ...result.flushes.map(one => ({ at: one.at, line: ` ${when(one.at)} ${'flushed'.padEnd(12)} ${' '.repeat(19)} by ${one.by}` }))
328
+ ]
329
+ const order = (at) => at ?? '\uffff'
330
+ timeline.sort((a, b) => order(a.at) < order(b.at) ? -1 : order(a.at) > order(b.at) ? 1 : 0)
331
+ for (const { line } of timeline) console.log(line)
315
332
  // The time column is when a turn ended, and a waiting message has not been answered by
316
333
  // one. Blank says that; when it was said is in the record for anyone who needs it.
317
334
  for (const entry of result.setAside) {
@@ -653,6 +670,14 @@ export async function manage (verb, argv) {
653
670
  const shown = show({ ws })
654
671
  return emit({ ws: shown.ws, model: shown.model, models: shown.models }, renderModels)
655
672
  }
673
+ if (verb === 'flush') {
674
+ const ws = argv._[1]
675
+ if (!ws) {
676
+ console.error('spore: flush needs a spore — spore flush <name>')
677
+ return 2
678
+ }
679
+ return emit(flush({ ws: String(ws), by: 'its operator' }), renderFlush)
680
+ }
656
681
  if (verb === 'extend') {
657
682
  const [, ws, dimension, amount] = argv._.map(String)
658
683
  if (!ws || !dimension || !amount) {
package/tui/commands.js CHANGED
@@ -3,6 +3,7 @@ export const COMMANDS = [
3
3
  { name: 'wrap', description: 'ask the running turn to wrap up, now or in a while', argumentHint: '[10m]' },
4
4
  { name: 'extend', description: 'give the running turn more, as far as its spore.cmk allows', argumentHint: '<rounds|cost|wall> <amount>' },
5
5
  { name: 'model', description: 'switch the model this spore runs on, from the next turn — a list to pick from, or one named', argumentHint: '[<model>]' },
6
+ { name: 'flush', description: 'start the conversation over from the next turn; nothing is forgotten' },
6
7
  { name: 'curate', description: 'add a model a provider lists to your mohdel catalog — this spore\'s provider unless one is named', argumentHint: '[<provider>]' },
7
8
  { name: 'pause', description: 'stop every turn and job of your spores, and start none' },
8
9
  { name: 'unpause', description: 'let turns start again' },
package/tui/index.js CHANGED
@@ -4,7 +4,7 @@ import { tmpdir } from 'node:os'
4
4
  import { basename, dirname, join } from 'node:path'
5
5
 
6
6
  import { CombinedAutocompleteProvider, Input, isKeyRelease, Markdown, matchesKey, ProcessTerminal, ScrollView, SelectList, truncateToWidth, TuiAltScreen, visibleWidth, VStack, wrapTextWithAnsi } from '@earendil-works/pi-tui'
7
- import { applyEvent, changeText, createLive, curate, durationText, errorText, extendTurn, grantOf, list, liveIn, loadMohdelEnvironment, outbox, pause, paused, readInbox, readTurns, RUNNING, send, SERVICE_STATE_DIR, show, sporePaths, stateOf, stop, switchModel, unpause, upstream, watchAll, wrapTurn } from '@rhizogrip/spore'
7
+ import { applyEvent, changeText, createLive, curate, durationText, errorText, extendTurn, flush, grantOf, list, liveIn, loadMohdelEnvironment, outbox, pause, paused, readInbox, readTurns, RUNNING, send, SERVICE_STATE_DIR, show, sporePaths, stateOf, stop, switchModel, unpause, upstream, watchAll, wrapTurn } from '@rhizogrip/spore'
8
8
  import chalk from 'chalk'
9
9
 
10
10
  import { runningJob, scheduleLine } from '../src/dispatch.js'
@@ -247,12 +247,32 @@ class Panel {
247
247
  invalidate () {}
248
248
  }
249
249
 
250
+ /**
251
+ * pi-tui gives the terminal back — the alternate screen, mouse reports, the keyboard protocol —
252
+ * only in `stop()`, so an error nothing caught would leave the shell unusable. Until the screen is
253
+ * left, such an error stops it first, then is reported and exits as Node would.
254
+ */
255
+ function stoppedOnCrash (tui) {
256
+ const crashed = (err) => {
257
+ tui.stop({ preserveScreen: true })
258
+ console.error(err)
259
+ process.exit(1)
260
+ }
261
+ process.on('uncaughtException', crashed)
262
+ process.on('unhandledRejection', crashed)
263
+ return () => {
264
+ process.off('uncaughtException', crashed)
265
+ process.off('unhandledRejection', crashed)
266
+ }
267
+ }
268
+
250
269
  /** A choice made before any spore is open: the value picked, or null when it was left. */
251
270
  export function choose ({ title, header = [], items, terminal = new ProcessTerminal() }) {
252
271
  const tui = new TuiAltScreen(terminal)
253
272
  return new Promise(resolve => {
254
273
  const done = (value) => {
255
274
  tui.stop({ preserveScreen: true })
275
+ released()
256
276
  resolve(value)
257
277
  }
258
278
  const picker = new Picker({ title, header, items, onSelect: (item) => done(item.value), onCancel: () => done(null) })
@@ -265,6 +285,7 @@ export function choose ({ title, header = [], items, terminal = new ProcessTermi
265
285
  })
266
286
  tui.setLayoutRoot(new VStack([{ component: picker, basis: 'auto' }]))
267
287
  tui.setFocus(picker)
288
+ const released = stoppedOnCrash(tui)
268
289
  tui.start()
269
290
  })
270
291
  }
@@ -304,12 +325,23 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
304
325
  state.noticeTimer = setTimeout(() => { status.notice = ''; tui.requestRender() }, Math.max(NOTICE_MS, text.length * 60))
305
326
  tui.requestRender()
306
327
  }
328
+ // spore.cmk is edited while the screen is open: one that does not parse is shown until it does.
329
+ const invalid = (err) => {
330
+ if (err.type !== 'DEFINITION_INVALID') throw err
331
+ notice(errorText(err))
332
+ }
307
333
  const notify = (text) => terminal.write(`\x1b]9;${printable(text).replace(/\n/g, ' ')}\x07`)
308
334
 
309
335
  const refresh = () => {
310
336
  const now = stateOf(state.ws)
311
337
  if (now.state !== RUNNING) state.limits = null
312
- else if (!state.limits) state.limits = show({ ws: state.ws }).turnLimits
338
+ else if (!state.limits) {
339
+ try {
340
+ state.limits = show({ ws: state.ws }).turnLimits
341
+ } catch (err) {
342
+ invalid(err)
343
+ }
344
+ }
313
345
  state.shown = { ws: state.ws, model: state.grant.model, billing: state.grant.billing, ...now, turnLimits: state.limits }
314
346
  const live = conversation.live ?? createLive()
315
347
  const at = Date.now()
@@ -332,9 +364,10 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
332
364
  }
333
365
 
334
366
  const open = (ws) => {
367
+ const grant = grantOf(ws)
335
368
  state.ws = ws
336
369
  state.attention.delete(ws)
337
- state.grant = grantOf(ws)
370
+ state.grant = grant
338
371
  state.lastSeq = 0
339
372
  remember(ws)
340
373
  reload()
@@ -362,7 +395,14 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
362
395
  label: `${state.attention.has(row.ws) ? '● ' : ''}${basename(row.ws)}`,
363
396
  description: `${row.state} · ${row.lastTurnNo} turns · $${row.treeUsd.toFixed(4)} · ${ago(row.last?.endedAt, now)}`
364
397
  })),
365
- onSelect: (item) => { closeOverlay(); open(item.value) },
398
+ onSelect: (item) => {
399
+ closeOverlay()
400
+ try {
401
+ open(item.value)
402
+ } catch (err) {
403
+ invalid(err)
404
+ }
405
+ },
366
406
  onCancel: closeOverlay
367
407
  }))
368
408
  }
@@ -397,6 +437,7 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
397
437
  following.abort()
398
438
  clearInterval(ticking)
399
439
  tui.stop({ preserveScreen: true })
440
+ released()
400
441
  finish(0)
401
442
  }
402
443
 
@@ -465,6 +506,15 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
465
506
  onCancel: closeOverlay
466
507
  }))
467
508
  }
509
+ if (name === 'flush') {
510
+ try {
511
+ const { open } = flush({ ws: state.ws, by: 'its operator' })
512
+ const still = (open.question ? 1 : 0) + open.approvals.length + open.jobs.length
513
+ return notice(`flushed — the next turn starts over; nothing is forgotten${still ? `, and it is told of ${still} thing${still > 1 ? 's' : ''} still open` : ''}`)
514
+ } catch (err) {
515
+ return notice(errorText(err))
516
+ }
517
+ }
468
518
  if (name === 'curate') {
469
519
  const provider = args[0] ?? show({ ws: state.ws }).model.split('/')[0]
470
520
  loadMohdelEnvironment()
@@ -529,7 +579,7 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
529
579
  if (!trimmed) return
530
580
  editor.setText('')
531
581
  const command = parseCommand(trimmed)
532
- if (command) return run(command)
582
+ if (command) return run(command).catch(invalid)
533
583
  say(trimmed.startsWith('//') ? trimmed.slice(1) : trimmed)
534
584
  }
535
585
 
@@ -637,6 +687,7 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
637
687
 
638
688
  open(ws)
639
689
  if (greeting) notice(greeting)
690
+ const released = stoppedOnCrash(tui)
640
691
  tui.start()
641
692
  const ticking = setInterval(refresh, REFRESH_MS)
642
693
  follow().catch(err => notice(`stopped following the spores — ${err.message}`))