@llm4ts/shell 2.52.0 → 2.54.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.
@@ -0,0 +1,600 @@
1
+ /**
2
+ * roundtable (ADR 0046): the flow half of the meeting. The protocol — what
3
+ * settles an item, who speaks next, when to judge, draft and finish — is
4
+ * `@llm4ts/flow/Roundtable`; this module reads and writes the meeting's
5
+ * files, builds the prompts, drives one read-only seat and the judgment
6
+ * service, and reports to the person.
7
+ */
8
+ import { join } from "node:path";
9
+ import * as Effect from "effect/Effect";
10
+ import * as Schema from "effect/Schema";
11
+ import { truthOf } from "@llm4ts/core/judgment/Judgment";
12
+ import { truth } from "@llm4ts/core/judgment/Schemas";
13
+ import { cap } from "@llm4ts/flow/Context";
14
+ import { Info, RoundtableTurn, StageCompleted, StageStarted } from "@llm4ts/flow/FlowEvents";
15
+ import { AgendaPending, FlowAborted } from "@llm4ts/flow/FlowError";
16
+ import { decide, judgeOrEscalate } from "@llm4ts/flow/Judgment";
17
+ import { AgendaProposal, agendaProposalJsonSchema, assembleAgenda, assembleTurn, briefingFor, chairSpeaker, closingItem, decodeTurns, encodeTurn, itemOf, itemState, maxAgendaItems, Meeting, minAgendaItems, nextAction, parseMeeting, parsePersona, recordOwner, renderAgendaItem, renderDecisions, renderDiscussion, renderMeeting, renderOpenQuestions, renderTurn, reviewItem, syncAnswers, Turn, TurnProposal, turnProposalJsonSchema } from "@llm4ts/flow/Roundtable";
18
+ import { structuredAndPublish } from "@llm4ts/flow/Usage";
19
+ import { ScriptUsage } from "@llm4ts/runner/FlowArgs";
20
+ import { terminalSafe } from "@llm4ts/runner/Terminal";
21
+ import { bundledPersonaCards, scribeName } from "./roundtable-personas.js";
22
+ // ---- Flags ----------------------------------------------------------------------
23
+ export const roundtableUsage = [
24
+ 'usage: llm4ts run roundtable --repo <dir> "<topic>" -- --goal "<goal>" [--agents "PO,PM,..."] [--meeting <id>]',
25
+ " [--context <dir>] [--rounds <n>] [--max-turns <n>] [--no-pause]",
26
+ " llm4ts run roundtable --repo <dir> -- --meeting <id> # continue or finish a meeting",
27
+ " llm4ts run roundtable --repo <dir> -- --eject-personas # write the bundled cards to .llm4ts/personas/",
28
+ " llm4ts run roundtable --repo <dir> -- --list # the repository's meetings",
29
+ " --goal <text> what the meeting must reach (required the first time)",
30
+ " --agents <names> attendees by persona name, in any order; default: PO,PM,Sr SW Eng,Cloud Architect,DevOps,SRE",
31
+ " --meeting <id> the meeting folder under .llm4ts/meetings/; default: a slug of the topic",
32
+ " --context <dir> documents every attendee reads (standards, architecture notes)",
33
+ " --rounds <n> complete rounds an item gets before it is left open (default 3)",
34
+ " --max-turns <n> turns in the whole discussion before drafting starts (default 40)",
35
+ " --no-pause do not stop after the agenda is proposed",
36
+ "The meeting file is the state: .llm4ts/meetings/<id>/meeting.md. Answer the questions under",
37
+ "'## Answers', write under '## Feedback', and rerun."
38
+ ].join("\n");
39
+ export const parseRoundtableArgs = (argv) => Effect.gen(function* () {
40
+ let goal;
41
+ let agents;
42
+ let meeting;
43
+ let context;
44
+ let rounds;
45
+ let maxTurns;
46
+ let noPause = false;
47
+ let ejectPersonas = false;
48
+ let list = false;
49
+ const rest = [];
50
+ for (let index = 0; index < argv.length; index += 1) {
51
+ const argument = argv[index] ?? "";
52
+ const is = (name) => argument === name || argument.startsWith(`${name}=`);
53
+ const valued = (name) => {
54
+ const value = argument.includes("=") ? argument.slice(name.length + 1) : argv[index + 1];
55
+ if (!argument.includes("="))
56
+ index += 1;
57
+ return value === undefined || value.trim().length === 0 || value.startsWith("--")
58
+ ? ScriptUsage.make({ message: `${name} needs a value\n${roundtableUsage}` })
59
+ : Effect.succeed(value.trim());
60
+ };
61
+ const counted = (name) => Effect.flatMap(valued(name), (value) => /^\d+$/.test(value) && Number.parseInt(value, 10) > 0
62
+ ? Effect.succeed(Number.parseInt(value, 10))
63
+ : ScriptUsage.make({ message: `${name} needs a positive number, got: ${value}` }));
64
+ if (is("--goal"))
65
+ goal = yield* valued("--goal");
66
+ else if (is("--agents"))
67
+ agents = (yield* valued("--agents"))
68
+ .split(",")
69
+ .map((name) => name.trim())
70
+ .filter((name) => name.length > 0);
71
+ else if (is("--meeting"))
72
+ meeting = yield* valued("--meeting");
73
+ else if (is("--context"))
74
+ context = yield* valued("--context");
75
+ else if (is("--rounds"))
76
+ rounds = yield* counted("--rounds");
77
+ else if (is("--max-turns"))
78
+ maxTurns = yield* counted("--max-turns");
79
+ else if (argument === "--no-pause")
80
+ noPause = true;
81
+ else if (argument === "--eject-personas")
82
+ ejectPersonas = true;
83
+ else if (argument === "--list")
84
+ list = true;
85
+ else
86
+ rest.push(argument);
87
+ }
88
+ return { goal, agents, meeting, context, rounds, maxTurns, noPause, ejectPersonas, list, rest };
89
+ });
90
+ // ---- Places -------------------------------------------------------------------
91
+ export const meetingsDir = (workDir) => join(workDir, ".llm4ts", "meetings");
92
+ export const meetingDir = (workDir, id) => join(meetingsDir(workDir), id);
93
+ export const personasDir = (workDir) => join(workDir, ".llm4ts", "personas");
94
+ /** The folder name a topic gets: lower case, words joined by dashes, at most 60 characters. */
95
+ export const meetingSlug = (topic) => {
96
+ const slug = topic
97
+ .toLowerCase()
98
+ .replace(/[^a-z0-9]+/g, "-")
99
+ .replace(/^-+|-+$/g, "")
100
+ .slice(0, 60)
101
+ .replace(/-+$/g, "");
102
+ return slug.length === 0 ? "meeting" : slug;
103
+ };
104
+ const meetingFiles = (dir) => ({
105
+ meeting: join(dir, "meeting.md"),
106
+ turns: join(dir, "turns.jsonl"),
107
+ discussion: join(dir, "discussion.md"),
108
+ outline: join(dir, "outline.md"),
109
+ plan: join(dir, "plan.md"),
110
+ decisions: join(dir, "decisions.md"),
111
+ openQuestions: join(dir, "open-questions.md")
112
+ });
113
+ /**
114
+ * The cards the meeting uses: bundled ones, each replaced by a custom card
115
+ * of the same name (`.llm4ts/personas/<name>.md`, read by the caller).
116
+ * Attendees are named; the Scribe is always present.
117
+ */
118
+ export const resolvePersonas = Effect.fn("roundtable.resolvePersonas")(function* (custom, attendees) {
119
+ const cards = { ...bundledPersonaCards, ...custom };
120
+ const personas = new Map();
121
+ for (const [name, text] of Object.entries(cards)) {
122
+ const persona = yield* parsePersona(name, text);
123
+ personas.set(name, persona);
124
+ }
125
+ const known = [...personas.keys()].filter((name) => name !== scribeName);
126
+ const missing = attendees.filter((name) => !personas.has(name) || name === scribeName);
127
+ if (missing.length > 0) {
128
+ return yield* ScriptUsage.make({
129
+ message: `unknown persona(s): ${missing.join(", ")}; known: ${known.join(", ")} — add a card under .llm4ts/personas/<name>.md`
130
+ });
131
+ }
132
+ if (attendees.length === 0) {
133
+ return yield* ScriptUsage.make({ message: `--agents names at least one persona` });
134
+ }
135
+ const scribe = personas.get(scribeName);
136
+ if (scribe === undefined) {
137
+ return yield* ScriptUsage.make({ message: `no ${scribeName} card` });
138
+ }
139
+ const chosen = [...new Set(attendees)]
140
+ .flatMap((name) => {
141
+ const persona = personas.get(name);
142
+ return persona === undefined ? [] : [persona];
143
+ })
144
+ .sort((left, right) => left.order - right.order || left.name.localeCompare(right.name));
145
+ return { attendees: chosen, scribe };
146
+ });
147
+ /** Writes every bundled card the folder lacks; returns the paths written. */
148
+ export const ejectPersonas = Effect.fn("roundtable.ejectPersonas")(function* (files, workDir) {
149
+ const written = [];
150
+ for (const [name, text] of Object.entries(bundledPersonaCards)) {
151
+ const path = join(personasDir(workDir), `${name}.md`);
152
+ const existing = yield* files.read(path);
153
+ if (existing !== undefined)
154
+ continue;
155
+ yield* files.writeAtomic(path, text);
156
+ written.push(path);
157
+ }
158
+ return written;
159
+ });
160
+ // ---- The Scribe's output ------------------------------------------------------
161
+ export class DraftProposal extends Schema.Class("DraftProposal")({
162
+ outline: Schema.String,
163
+ plan: Schema.String
164
+ }) {
165
+ }
166
+ export const draftProposalJsonSchema = {
167
+ type: "object",
168
+ properties: { outline: { type: "string" }, plan: { type: "string" } },
169
+ required: ["outline", "plan"]
170
+ };
171
+ const personaBrief = (persona) => [
172
+ `You are the ${persona.name} in a roundtable of specialists.`,
173
+ "",
174
+ `Mandate: ${persona.mandate}`,
175
+ ...(persona.caresAbout.length === 0 ? [] : [`You care about: ${persona.caresAbout}`]),
176
+ ...(persona.pushesBackOn.length === 0 ? [] : [`You push back on: ${persona.pushesBackOn}`]),
177
+ ...(persona.neverDecidesAlone.length === 0
178
+ ? []
179
+ : [`You never decide alone: ${persona.neverDecidesAlone}`]),
180
+ ...(persona.voice.length === 0 ? [] : [`Voice: ${persona.voice}`])
181
+ ].join("\n");
182
+ const meetingBrief = (meeting) => [
183
+ `Topic: ${meeting.topic}`,
184
+ `Goal: ${meeting.goal}`,
185
+ `Attendees: ${meeting.attendees.join(", ")}`
186
+ ].join("\n");
187
+ const contextBrief = (docs, budget) => {
188
+ if (docs.length === 0)
189
+ return "";
190
+ const each = Math.max(1_000, Math.floor(budget / 2 / docs.length));
191
+ return [
192
+ "",
193
+ "Context documents (standards and notes the room works within):",
194
+ ...docs.flatMap((doc) => ["", `--- ${doc.path} ---`, cap(doc.text, each).text])
195
+ ].join("\n");
196
+ };
197
+ const agendaPrompt = (persona, meeting, docs, budget) => [
198
+ personaBrief(persona),
199
+ "",
200
+ meetingBrief(meeting),
201
+ contextBrief(docs, budget),
202
+ "",
203
+ `Propose the agenda: between ${minAgendaItems} and ${maxAgendaItems} items, each a short title and the one question the room must settle under it. Order them problem first, then solution, then how it is delivered and run. Do not add a goal check: the chair closes the meeting with one.`,
204
+ "",
205
+ 'Reply with JSON: {"items": [{"title": "...", "question": "..."}]}'
206
+ ].join("\n");
207
+ const kindsGuide = [
208
+ "Kinds of turn:",
209
+ "- position: your view on the question, with reasons.",
210
+ "- proposal: a concrete decision you want the room to take; write it so it can be recorded as is.",
211
+ "- objection: against an earlier turn (name it in answers). blocking=true only when the goal cannot be reached unless it is addressed; otherwise it is recorded as dissent.",
212
+ "- question: for the room, or for the owner (needsHuman=true) when only the person who called the meeting can answer; then state the assumption you proceed on.",
213
+ "- agreement: you accept an earlier turn (name it in answers). Close your own blocking objection this way when it has been answered.",
214
+ "- pass: nothing to add this round."
215
+ ].join("\n");
216
+ const turnPrompt = (options) => {
217
+ const { persona, meeting, item, round, caps, turns, addressed, docs, budget } = options;
218
+ const briefing = briefingFor(meeting, turns, item.number, caps);
219
+ const isReview = item.number === reviewItem(meeting).number;
220
+ return [
221
+ personaBrief(persona),
222
+ "",
223
+ meetingBrief(meeting),
224
+ contextBrief(docs, budget),
225
+ "",
226
+ "Decisions so far:",
227
+ ...(briefing.decisions.length === 0
228
+ ? ["- none yet"]
229
+ : briefing.decisions.map((decision) => `- item ${decision.item.number} (${decision.item.title}): ${decision.proposal.body} — proposed by ${decision.proposal.speaker}${decision.dissent.length === 0 ? "" : `; dissent from ${decision.dissent.map((turn) => turn.speaker).join(", ")}`}`)),
230
+ "",
231
+ "Questions parked for the owner (proceed on the stated assumption):",
232
+ ...(briefing.parked.length === 0
233
+ ? ["- none"]
234
+ : briefing.parked.map((turn) => `- #${turn.id} ${turn.speaker}: ${turn.body}${turn.assumption === undefined ? "" : ` (assuming: ${turn.assumption})`}`)),
235
+ "",
236
+ `Agenda item ${renderAgendaItem(item)}`,
237
+ `Round ${round} of at most ${caps.roundsPerItem}.`,
238
+ ...(isReview
239
+ ? [
240
+ "",
241
+ "The Scribe has written outline.md and plan.md from the discussion. Review them from your mandate: object (blocking only for what makes them wrong or unusable), agree, or pass."
242
+ ]
243
+ : []),
244
+ "",
245
+ "The discussion on this item so far:",
246
+ ...(briefing.current.length === 0
247
+ ? ["(nobody has spoken yet)"]
248
+ : briefing.current.map(renderTurn)),
249
+ "",
250
+ ...(addressed.length === 0
251
+ ? []
252
+ : ["Aimed at your earlier turns, still open:", ...addressed.map(renderTurn), ""]),
253
+ kindsGuide,
254
+ "",
255
+ `Speak as the ${persona.name}, in your voice, once. Answer turns by their #id on this item only. Keep the body to a short paragraph.`,
256
+ "",
257
+ 'Reply with JSON: {"kind": "position|proposal|objection|question|agreement|pass", "answers": [ids], "blocking": false, "needsHuman": false, "assumption": "optional", "body": "..."}'
258
+ ].join("\n");
259
+ };
260
+ const draftPrompt = (options) => {
261
+ const { scribe, meeting, docs, budget, previous } = options;
262
+ return [
263
+ personaBrief(scribe),
264
+ "",
265
+ meetingBrief(meeting),
266
+ contextBrief(docs, budget),
267
+ "",
268
+ previous === undefined
269
+ ? "Write the two deliverables from the transcript below. Say what the room settled; keep every decision's dissent visible; where a question is parked for the owner, state the assumption the document rests on. Do not invent decisions the room did not take."
270
+ : "Revise the two deliverables below so that every blocking objection from the review is addressed. Change nothing the objections do not touch.",
271
+ "",
272
+ ...(previous === undefined
273
+ ? []
274
+ : [
275
+ "Blocking objections from the review:",
276
+ ...previous.objections.map(renderTurn),
277
+ "",
278
+ "Current outline.md:",
279
+ cap(previous.outline, Math.floor(budget / 6)).text,
280
+ "",
281
+ "Current plan.md:",
282
+ cap(previous.plan, Math.floor(budget / 6)).text,
283
+ ""
284
+ ]),
285
+ "Decisions (as recorded):",
286
+ cap(options.decisions, Math.floor(budget / 6)).text,
287
+ "",
288
+ "Open questions (as recorded):",
289
+ cap(options.openQuestions, Math.floor(budget / 8)).text,
290
+ "",
291
+ "Transcript:",
292
+ cap(options.discussion, Math.floor(budget / 2)).text,
293
+ "",
294
+ "Outline sections:",
295
+ scribe.outlineTemplate ??
296
+ "## Context\n## Proposed solution\n## Risks\n## Open questions and assumptions",
297
+ "",
298
+ "Plan sections:",
299
+ scribe.planTemplate ?? "## Phases\n## Workstreams and owners\n## Risks and mitigations",
300
+ "",
301
+ 'Reply with JSON: {"outline": "<complete markdown document>", "plan": "<complete markdown document>"}'
302
+ ].join("\n");
303
+ };
304
+ /** The meeting, driven from its files until it halts for the person or is done. */
305
+ export const runRoundtable = Effect.fn("roundtable.run")(function* (deps) {
306
+ const { files, events, caps } = deps;
307
+ const dir = meetingDir(deps.workDir, deps.meetingId);
308
+ const paths = meetingFiles(dir);
309
+ const say = (message) => events.publish(Info.make({ message }));
310
+ const save = (meeting) => files.writeAtomic(paths.meeting, renderMeeting(meeting));
311
+ const loaded = yield* loadMeeting(files, paths.meeting);
312
+ let meeting;
313
+ if (loaded === undefined) {
314
+ const topic = (deps.topic ?? "").trim();
315
+ const goal = (deps.goal ?? "").trim();
316
+ if (topic.length === 0 || goal.length === 0) {
317
+ return yield* ScriptUsage.make({
318
+ message: `no meeting at ${paths.meeting}: give the topic and --goal to start one\n${roundtableUsage}`
319
+ });
320
+ }
321
+ meeting = Meeting.make({
322
+ id: deps.meetingId,
323
+ status: "agenda-proposed",
324
+ topic,
325
+ goal,
326
+ attendees: deps.attendees.map((persona) => persona.name),
327
+ agenda: [],
328
+ answers: [],
329
+ feedback: "",
330
+ drafts: 0
331
+ });
332
+ yield* save(meeting);
333
+ yield* say(`roundtable: ${paths.meeting} opened — ${meeting.attendees.join(", ")}`);
334
+ }
335
+ else {
336
+ meeting = loaded;
337
+ }
338
+ // Every attendee is a lane in the dashboard, from the first event of the run (ADR 0046).
339
+ for (const name of meeting.attendees) {
340
+ yield* events.publish(StageStarted.make({ stage: `attendee ${name}`, lane: name }));
341
+ }
342
+ const personaOf = (name) => deps.attendees.find((persona) => persona.name === name);
343
+ for (const name of meeting.attendees) {
344
+ if (personaOf(name) === undefined) {
345
+ return yield* ScriptUsage.make({
346
+ message: `the meeting lists attendee ${name} but no such persona is known; pass --agents with the meeting's attendees, or add .llm4ts/personas/${name}.md`
347
+ });
348
+ }
349
+ }
350
+ let turns = yield* decodeTurns((yield* files.read(paths.turns)) ?? "", paths.turns);
351
+ const appendTurn = (turn) => Effect.gen(function* () {
352
+ yield* files.append(paths.turns, `${encodeTurn(turn)}\n`);
353
+ turns = [...turns, turn];
354
+ yield* files.writeAtomic(paths.discussion, renderDiscussion(meeting, turns, caps));
355
+ const synced = syncAnswers(meeting, turns);
356
+ if (synced.answers.length !== meeting.answers.length) {
357
+ meeting = synced;
358
+ yield* save(meeting);
359
+ }
360
+ yield* events.publish(RoundtableTurn.make({
361
+ lane: turn.speaker,
362
+ meeting: meeting.id,
363
+ item: turn.item,
364
+ itemTitle: itemOf(meeting, turn.item)?.title ?? "",
365
+ round: turn.round,
366
+ turn: turn.id,
367
+ kind: turn.kind,
368
+ blocking: turn.blocking,
369
+ answers: turn.answers,
370
+ summary: firstLine(turn.body)
371
+ }));
372
+ });
373
+ // The item under discussion is the run's open stage.
374
+ let openItem;
375
+ const itemStage = (item) => `item ${item.number}: ${item.title}`;
376
+ const leaveItem = () => Effect.gen(function* () {
377
+ if (openItem === undefined)
378
+ return;
379
+ yield* events.publish(StageCompleted.make({ stage: itemStage(openItem) }));
380
+ openItem = undefined;
381
+ });
382
+ const enterItem = (item) => Effect.gen(function* () {
383
+ if (openItem?.number === item.number)
384
+ return;
385
+ yield* leaveItem();
386
+ yield* events.publish(StageStarted.make({ stage: itemStage(item) }));
387
+ openItem = item;
388
+ });
389
+ const writeRecords = () => Effect.gen(function* () {
390
+ yield* files.writeAtomic(paths.decisions, renderDecisions(meeting, turns, caps));
391
+ yield* files.writeAtomic(paths.openQuestions, renderOpenQuestions(meeting, turns, caps));
392
+ });
393
+ const askScribe = (previous) => Effect.gen(function* () {
394
+ const proposal = yield* structuredAndPublish(deps.seat, events, draftPrompt({
395
+ scribe: deps.scribe,
396
+ meeting,
397
+ discussion: renderDiscussion(meeting, turns, caps),
398
+ decisions: renderDecisions(meeting, turns, caps),
399
+ openQuestions: renderOpenQuestions(meeting, turns, caps),
400
+ docs: deps.context,
401
+ budget: deps.budget,
402
+ ...(previous === undefined ? {} : { previous })
403
+ }), DraftProposal, draftProposalJsonSchema, "scribe");
404
+ yield* files.writeAtomic(paths.outline, `${proposal.outline.trim()}\n`);
405
+ yield* files.writeAtomic(paths.plan, `${proposal.plan.trim()}\n`);
406
+ });
407
+ let guard = 0;
408
+ for (;;) {
409
+ guard += 1;
410
+ if (guard > caps.maxTurns * 4 + 64) {
411
+ return yield* FlowAborted.make({
412
+ message: `roundtable ${deps.meetingId}: the chair did not reach an end after ${guard} steps; the files under ${dir} are intact`
413
+ });
414
+ }
415
+ const action = nextAction(meeting, turns, caps);
416
+ switch (action._tag) {
417
+ case "Refused":
418
+ return yield* ScriptUsage.make({
419
+ message: `roundtable ${deps.meetingId}: ${action.reason}`
420
+ });
421
+ case "Done":
422
+ return {
423
+ _tag: "NothingToDo",
424
+ message: `meeting ${deps.meetingId} is done; answer a question under '## Answers' or write '## Feedback' in ${paths.meeting} to reopen it`
425
+ };
426
+ case "ProposeAgenda": {
427
+ const persona = personaOf(action.speaker) ?? deps.attendees[0];
428
+ if (persona === undefined) {
429
+ return yield* ScriptUsage.make({ message: "the meeting has no attendees" });
430
+ }
431
+ const proposal = yield* structuredAndPublish(deps.seat, events, agendaPrompt(persona, meeting, deps.context, deps.budget), AgendaProposal, agendaProposalJsonSchema, persona.name);
432
+ const items = assembleAgenda(proposal);
433
+ if (items.length === 0) {
434
+ return yield* FlowAborted.make({
435
+ message: `${persona.name} proposed no agenda item; rerun`
436
+ });
437
+ }
438
+ meeting = Meeting.make({ ...meeting, agenda: items });
439
+ yield* save(meeting);
440
+ yield* say(`roundtable: ${persona.name} proposed ${items.length} agenda item(s):\n${items.map((item) => ` ${renderAgendaItem(item)}`).join("\n")}`);
441
+ if (!deps.noPause) {
442
+ return yield* AgendaPending.make({ path: paths.meeting, items: items.length });
443
+ }
444
+ continue;
445
+ }
446
+ case "OpenDiscussion":
447
+ meeting = Meeting.make({ ...meeting, status: "discussing" });
448
+ yield* save(meeting);
449
+ yield* say(`roundtable: discussing ${meeting.agenda.length} item(s) plus the goal check, ${meeting.attendees.length} attendees, up to ${caps.roundsPerItem} rounds each`);
450
+ continue;
451
+ case "RecordOwner": {
452
+ const recorded = recordOwner(meeting, turns, action);
453
+ for (const turn of recorded) {
454
+ yield* appendTurn(turn);
455
+ yield* say(renderTurn(turn));
456
+ }
457
+ meeting = Meeting.make({ ...meeting, status: "discussing", feedback: "" });
458
+ yield* save(meeting);
459
+ continue;
460
+ }
461
+ case "Judge": {
462
+ yield* enterItem(action.item);
463
+ const result = yield* judgeOrEscalate({
464
+ judgment: deps.judgment,
465
+ reasoning: deps.seat,
466
+ events,
467
+ request: {
468
+ state: {
469
+ question: action.item.question,
470
+ objection: `${action.objection.speaker}: ${action.objection.body}`,
471
+ reply: `${action.reply.speaker}: ${action.reply.body}`
472
+ },
473
+ questions: {
474
+ resolved: truth("The reply answers the objection well enough that a reasonable objector would withdraw it.")
475
+ }
476
+ }
477
+ });
478
+ const answer = yield* truthOf(result, "resolved").pipe(Effect.option);
479
+ const resolved = answer._tag === "Some" && decide(answer.value) !== "hold" && answer.value.truth >= 0.5;
480
+ const probability = answer._tag === "Some" ? answer.value.truth.toFixed(2) : "n/a";
481
+ const chair = Turn.make({
482
+ id: turns.length + 1,
483
+ round: action.reply.round,
484
+ item: action.item.number,
485
+ speaker: chairSpeaker,
486
+ kind: resolved ? "agreement" : "position",
487
+ answers: [action.objection.id],
488
+ blocking: false,
489
+ needsHuman: false,
490
+ body: resolved
491
+ ? `Judged resolved by #${action.reply.id} (p=${probability}).`
492
+ : `Judged still open after #${action.reply.id} (p=${probability}); ${action.objection.speaker} may close it or the item is left open at the cap.`
493
+ });
494
+ yield* appendTurn(chair);
495
+ yield* say(renderTurn(chair));
496
+ continue;
497
+ }
498
+ case "Speak": {
499
+ yield* enterItem(action.item);
500
+ const persona = personaOf(action.speaker);
501
+ if (persona === undefined) {
502
+ return yield* ScriptUsage.make({ message: `no persona for ${action.speaker}` });
503
+ }
504
+ const proposal = yield* structuredAndPublish(deps.seat, events, turnPrompt({
505
+ persona,
506
+ meeting,
507
+ item: action.item,
508
+ round: action.round,
509
+ caps,
510
+ turns,
511
+ addressed: action.addressed,
512
+ docs: deps.context,
513
+ budget: deps.budget
514
+ }), TurnProposal, turnProposalJsonSchema, persona.name);
515
+ const turn = assembleTurn(proposal, {
516
+ id: turns.length + 1,
517
+ round: action.round,
518
+ item: action.item.number,
519
+ speaker: persona.name,
520
+ existing: turns
521
+ });
522
+ yield* appendTurn(turn);
523
+ yield* say(`[item ${action.item.number}, round ${action.round}] ${renderTurn(turn)}`);
524
+ const state = itemState(meeting, turns, action.item.number, caps);
525
+ if (state.settled)
526
+ yield* say(`item ${action.item.number} settled.`);
527
+ else if (state.capped)
528
+ yield* say(`item ${action.item.number} left open at the cap.`);
529
+ continue;
530
+ }
531
+ case "Draft": {
532
+ yield* leaveItem();
533
+ if (meeting.status !== "drafting") {
534
+ meeting = Meeting.make({ ...meeting, status: "drafting" });
535
+ yield* save(meeting);
536
+ }
537
+ yield* writeRecords();
538
+ yield* askScribe();
539
+ meeting = Meeting.make({ ...meeting, status: "reviewing", drafts: meeting.drafts + 1 });
540
+ yield* save(meeting);
541
+ yield* say(`scribe: draft ${meeting.drafts} written — ${paths.outline}, ${paths.plan}, ${paths.decisions}, ${paths.openQuestions}`);
542
+ continue;
543
+ }
544
+ case "Finalize": {
545
+ yield* leaveItem();
546
+ if (action.objections.length > 0) {
547
+ const outline = (yield* files.read(paths.outline)) ?? "";
548
+ const plan = (yield* files.read(paths.plan)) ?? "";
549
+ yield* askScribe({ outline, plan, objections: action.objections });
550
+ yield* say(`scribe: fix pass for ${action.objections.length} blocking objection(s)`);
551
+ }
552
+ yield* writeRecords();
553
+ meeting = Meeting.make({ ...meeting, status: "done" });
554
+ yield* save(meeting);
555
+ const parked = meeting.answers.filter((point) => (point.answer ?? "").trim().length === 0);
556
+ const closing = closingItem(meeting);
557
+ const open = [...meeting.agenda, closing].filter((item) => !itemState(meeting, turns, item.number, caps).settled);
558
+ const summary = [
559
+ `roundtable ${deps.meetingId} done after ${turns.length} turns and ${meeting.drafts} draft(s)`,
560
+ `outline: ${paths.outline}`,
561
+ `plan: ${paths.plan}`,
562
+ `decisions: ${paths.decisions}`,
563
+ `open questions: ${paths.openQuestions}${open.length === 0 ? "" : ` (${open.length} item(s) left open)`}`,
564
+ ...(parked.length === 0
565
+ ? []
566
+ : [
567
+ `${parked.length} question(s) for you under '## Answers' in ${paths.meeting}; answer and rerun to fold them in`
568
+ ])
569
+ ];
570
+ for (const line of summary)
571
+ yield* say(line);
572
+ for (const name of meeting.attendees) {
573
+ yield* events.publish(StageCompleted.make({ stage: `attendee ${name}`, lane: name }));
574
+ }
575
+ return { _tag: "Finished", summary };
576
+ }
577
+ }
578
+ }
579
+ });
580
+ /**
581
+ * The first line of a turn's body, cut short: what the dashboard shows on
582
+ * the lane. A model's reply is untrusted text on its way to a terminal:
583
+ * escape sequences and control characters are dropped, not painted.
584
+ */
585
+ const firstLine = (text) => {
586
+ const line = terminalSafe(text.split(/\r?\n/)[0] ?? "")
587
+ // eslint-disable-next-line no-control-regex
588
+ .replace(/[\u0000-\u001f\u007f-\u009f]/g, "")
589
+ .replace(/\s+/g, " ")
590
+ .trim();
591
+ return line.length > 80 ? `${line.slice(0, 77)}...` : line;
592
+ };
593
+ const loadMeeting = Effect.fn("roundtable.loadMeeting")(function* (files, path) {
594
+ const text = yield* files.read(path);
595
+ if (text === undefined)
596
+ return undefined;
597
+ return yield* parseMeeting(text, path);
598
+ });
599
+ /** Markdown of the bundled and custom cards, for `--list` and for the docs. */
600
+ export const describePersonas = (personas) => personas.map((persona) => `- ${persona.name}: ${persona.mandate}`).join("\n");