tryharness 0.7.30 → 0.7.32

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/lib/store.mjs CHANGED
@@ -22,6 +22,11 @@ function bindings(values) {
22
22
  }
23
23
 
24
24
  export const DEFAULT_DAILY_RUNS = 10;
25
+ // Why a failed task failed: the person stopped or removed it, its run never started (the app it runs in closed, its
26
+ // folder busy), its run ended early (its process exited, its model's provider failed), or its agent reported failure.
27
+ export const FAILURE_CAUSES = ["person", "start", "exit", "agent"];
28
+ // An agent's record leaves out failures that say nothing about it: the person's stop and a run that never started.
29
+ const AGENTS_OWN = "not (status = 'failed' and failure in ('person', 'start'))";
25
30
 
26
31
  export function openStore(file) {
27
32
  const db = new Database(file);
@@ -87,6 +92,7 @@ export function openStore(file) {
87
92
  usage text,
88
93
  run_model text,
89
94
  removed_at text,
95
+ failure text,
90
96
  created_at text not null,
91
97
  updated_at text not null
92
98
  );
@@ -188,6 +194,33 @@ export function openStore(file) {
188
194
  error text
189
195
  );
190
196
  create index if not exists swarm_rounds_goal_idx on swarm_rounds(goal_id, at);
197
+ create table if not exists board_reactions (
198
+ post_id text not null,
199
+ agent text not null,
200
+ goal_id text not null,
201
+ post_seq integer not null,
202
+ about_seq integer,
203
+ verdict text not null,
204
+ judgments text,
205
+ at text not null,
206
+ primary key(post_id, agent)
207
+ );
208
+ create table if not exists board_lessons (
209
+ id text primary key,
210
+ about text not null,
211
+ agent text,
212
+ text text not null,
213
+ scope text not null,
214
+ folder text,
215
+ source text not null default 'said',
216
+ evidence text not null default '[]',
217
+ status text not null default 'active',
218
+ replaced_by text,
219
+ vector blob,
220
+ created_at text not null,
221
+ updated_at text not null
222
+ );
223
+ create virtual table if not exists board_lessons_fts using fts5(id unindexed, text, tokenize='unicode61');
191
224
  create table if not exists board_vectors (
192
225
  contribution_id text primary key,
193
226
  version text not null,
@@ -284,6 +317,19 @@ export function openStore(file) {
284
317
  db.exec("alter table board_tasks add column run_model text");
285
318
  if (!boardColumns.has("removed_at") && boardColumns.size)
286
319
  db.exec("alter table board_tasks add column removed_at text");
320
+ // Why a failed task failed (FAILURE_CAUSES). Tasks failed before it was kept are read from the fixed results the
321
+ // daemon wrote for each cause; any other failed task was reported failed by its agent.
322
+ if (!boardColumns.has("failure") && boardColumns.size)
323
+ db.exec(`alter table board_tasks add column failure text;
324
+ update board_tasks set failure = case
325
+ when result in ('Stopped by you.', 'Stopped by the owner.', 'Removed by you.', 'Stopped.') then 'person'
326
+ when result like 'T3 Code did not take it:%' or result like 'Could not start %'
327
+ or result like 'Another run is already working in %' or result = 'Invalid task run id.' then 'start'
328
+ when result like '% exited with code %' or result like '% stopped on an error from its model:%'
329
+ or result like 'T3 Code ended the turn%' or result like '%The turn may still be going in T3 Code.'
330
+ or result = 'This run was interrupted earlier and is not started again.' then 'exit'
331
+ else 'agent' end
332
+ where status = 'failed';`);
287
333
  if (!boardColumns.has("trust") && boardColumns.size)
288
334
  db.exec(
289
335
  "alter table board_tasks add column trust text not null default 'agent'; alter table board_tasks add column origin_chain text not null default '[]'"
@@ -1024,6 +1070,297 @@ export function openStore(file) {
1024
1070
  }
1025
1071
  return counts;
1026
1072
  },
1073
+ // What the swarm learned (lessons.mjs): each lesson with the person's words it stands on (`evidence`: the thread,
1074
+ // post and quote), active until a later one replaces it.
1075
+ addLesson({ about, agent, text, scope, folder, evidence }) {
1076
+ const id = randomUUID();
1077
+ const now = new Date().toISOString();
1078
+ execute(
1079
+ "run",
1080
+ `insert into board_lessons(id, about, agent, text, scope, folder, evidence, created_at, updated_at)
1081
+ values(?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1082
+ id,
1083
+ about,
1084
+ agent ?? null,
1085
+ text,
1086
+ scope,
1087
+ folder ?? null,
1088
+ JSON.stringify([evidence]),
1089
+ now,
1090
+ now
1091
+ );
1092
+ execute(
1093
+ "run",
1094
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1095
+ id,
1096
+ text
1097
+ );
1098
+ return this.getLesson(id);
1099
+ },
1100
+ getLesson(id) {
1101
+ const row = execute(
1102
+ "get",
1103
+ "select * from board_lessons where id = ?",
1104
+ id
1105
+ );
1106
+ if (!row) return null;
1107
+ const { vector, ...lesson } = row;
1108
+ const evidence = JSON.parse(lesson.evidence || "[]");
1109
+ return {
1110
+ ...lesson,
1111
+ evidence,
1112
+ // Times the person said it again after an agent had been given it (lessons.mjs).
1113
+ missed: evidence.filter((entry) => entry.carried).length,
1114
+ embedded: Boolean(vector),
1115
+ };
1116
+ },
1117
+ // The person said it again: the lesson keeps its words and gains the new evidence.
1118
+ confirmLesson(id, evidence) {
1119
+ const lesson = this.getLesson(id);
1120
+ if (!lesson || lesson.status !== "active") return null;
1121
+ execute(
1122
+ "run",
1123
+ "update board_lessons set evidence = ?, updated_at = ? where id = ?",
1124
+ JSON.stringify([...lesson.evidence, evidence].slice(-20)),
1125
+ new Date().toISOString(),
1126
+ id
1127
+ );
1128
+ return this.getLesson(id);
1129
+ },
1130
+ // The person changed or reversed it: a new lesson replaces the old one, which stays as the record of what was.
1131
+ replaceLesson(id, lesson) {
1132
+ const old = this.getLesson(id);
1133
+ if (!old || old.status !== "active") return null;
1134
+ const added = this.addLesson(lesson);
1135
+ execute(
1136
+ "run",
1137
+ "update board_lessons set status = 'replaced', replaced_by = ?, updated_at = ? where id = ?",
1138
+ added.id,
1139
+ new Date().toISOString(),
1140
+ id
1141
+ );
1142
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1143
+ return added;
1144
+ },
1145
+ // The person's own edit: their words become the lesson's, and it is read again for meaning.
1146
+ editLesson(id, text) {
1147
+ const lesson = this.getLesson(id);
1148
+ if (!lesson || lesson.status !== "active") return null;
1149
+ execute(
1150
+ "run",
1151
+ "update board_lessons set text = ?, source = 'person', vector = null, updated_at = ? where id = ?",
1152
+ text,
1153
+ new Date().toISOString(),
1154
+ id
1155
+ );
1156
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1157
+ execute(
1158
+ "run",
1159
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1160
+ id,
1161
+ text
1162
+ );
1163
+ return this.getLesson(id);
1164
+ },
1165
+ // The person took it back in a thread with nothing in its place: it leaves the work and stays on record as replaced,
1166
+ // by nothing, with the words that took it back.
1167
+ takeBackLesson(id, evidence) {
1168
+ const lesson = this.getLesson(id);
1169
+ if (!lesson || lesson.status !== "active") return null;
1170
+ execute(
1171
+ "run",
1172
+ "update board_lessons set status = 'replaced', replaced_by = null, evidence = ?, vector = null, updated_at = ? where id = ?",
1173
+ JSON.stringify(
1174
+ [...lesson.evidence, { ...evidence, tookBack: true }].slice(-20)
1175
+ ),
1176
+ new Date().toISOString(),
1177
+ id
1178
+ );
1179
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1180
+ return this.getLesson(id);
1181
+ },
1182
+ // A lesson replaced, taken back or removed is in the work again, as it was (the person's Undo, lessons.mjs).
1183
+ restoreLesson(id, { dropTookBack = false } = {}) {
1184
+ const lesson = this.getLesson(id);
1185
+ if (!lesson || lesson.status === "active") return lesson;
1186
+ const evidence = dropTookBack
1187
+ ? lesson.evidence.filter((entry) => !entry.tookBack)
1188
+ : lesson.evidence;
1189
+ execute(
1190
+ "run",
1191
+ "update board_lessons set status = 'active', replaced_by = null, evidence = ?, updated_at = ? where id = ?",
1192
+ JSON.stringify(evidence),
1193
+ new Date().toISOString(),
1194
+ id
1195
+ );
1196
+ execute(
1197
+ "run",
1198
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1199
+ id,
1200
+ lesson.text
1201
+ );
1202
+ return this.getLesson(id);
1203
+ },
1204
+ // What the person's posts in a thread changed in what the swarm keeps, for the line under each post: a lesson kept
1205
+ // or changed by the post (its first words), or taken back by it. Saying a lesson again changes nothing to show.
1206
+ lessonsFromThread(goalId) {
1207
+ const rows = execute(
1208
+ "all",
1209
+ "select id from board_lessons where evidence like ? order by created_at",
1210
+ `%"goalId":"${String(goalId).replace(/[%_"]/g, "")}"%`
1211
+ ).map((row) => this.getLesson(row.id));
1212
+ const replaced = new Set(
1213
+ execute(
1214
+ "all",
1215
+ "select replaced_by from board_lessons where replaced_by is not null"
1216
+ ).map((row) => row.replaced_by)
1217
+ );
1218
+ const lines = [];
1219
+ for (const lesson of rows) {
1220
+ const first = lesson.evidence[0];
1221
+ // A lesson the person removed, or undid, has no line; one a later post changed keeps its line, without Undo.
1222
+ if (
1223
+ first?.goalId === goalId &&
1224
+ first.postId &&
1225
+ lesson.status !== "removed"
1226
+ )
1227
+ lines.push({
1228
+ postId: first.postId,
1229
+ lessonId: lesson.id,
1230
+ change: replaced.has(lesson.id) ? "changed" : "kept",
1231
+ text: lesson.text,
1232
+ about: lesson.about,
1233
+ agent: lesson.agent,
1234
+ undoable: lesson.status === "active",
1235
+ });
1236
+ for (const entry of lesson.evidence)
1237
+ if (entry.tookBack && entry.goalId === goalId && entry.postId)
1238
+ lines.push({
1239
+ postId: entry.postId,
1240
+ lessonId: lesson.id,
1241
+ change: "taken back",
1242
+ text: lesson.text,
1243
+ about: lesson.about,
1244
+ agent: lesson.agent,
1245
+ undoable: lesson.status !== "active",
1246
+ });
1247
+ }
1248
+ return lines;
1249
+ },
1250
+ // The person took it out: it reaches no work and is matched by nothing, and stays on record as removed.
1251
+ removeLesson(id) {
1252
+ const lesson = this.getLesson(id);
1253
+ if (!lesson || lesson.status !== "active") return null;
1254
+ execute(
1255
+ "run",
1256
+ "update board_lessons set status = 'removed', vector = null, updated_at = ? where id = ?",
1257
+ new Date().toISOString(),
1258
+ id
1259
+ );
1260
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1261
+ return this.getLesson(id);
1262
+ },
1263
+ lessons({ status = "active", limit = 200 } = {}) {
1264
+ return execute(
1265
+ "all",
1266
+ "select id from board_lessons where status = ? order by updated_at desc limit ?",
1267
+ status,
1268
+ Math.max(1, Math.min(1000, Number(limit) || 200))
1269
+ ).map((row) => this.getLesson(row.id));
1270
+ },
1271
+ // Active lessons sharing words with a text, best first.
1272
+ lessonsMatching(query, limit = 6) {
1273
+ const terms = [
1274
+ ...new Set(
1275
+ String(query)
1276
+ .toLowerCase()
1277
+ .match(/[\p{L}\p{N}_-]+/gu) || []
1278
+ ),
1279
+ ]
1280
+ .filter((term) => term.length > 2)
1281
+ .slice(0, 24);
1282
+ if (!terms.length) return [];
1283
+ const match = terms
1284
+ .map((term) => `"${term.replaceAll('"', '""')}"`)
1285
+ .join(" OR ");
1286
+ return execute(
1287
+ "all",
1288
+ `select f.id from board_lessons_fts f join board_lessons l on l.id = f.id
1289
+ where board_lessons_fts match ? and l.status = 'active' order by bm25(board_lessons_fts) limit ?`,
1290
+ match,
1291
+ limit
1292
+ ).map((row) => this.getLesson(row.id));
1293
+ },
1294
+ // What the person has said about one agent, newest first, for its card.
1295
+ lessonsAbout(agent, limit = 5) {
1296
+ return execute(
1297
+ "all",
1298
+ "select text from board_lessons where status = 'active' and about = 'agent' and agent = ? order by updated_at desc limit ?",
1299
+ agent,
1300
+ limit
1301
+ ).map((row) => row.text);
1302
+ },
1303
+ setLessonVector(id, vector) {
1304
+ execute(
1305
+ "run",
1306
+ "update board_lessons set vector = ? where id = ?",
1307
+ vector,
1308
+ id
1309
+ );
1310
+ },
1311
+ lessonVectors() {
1312
+ return execute(
1313
+ "all",
1314
+ "select id, vector from board_lessons where status = 'active' and vector is not null"
1315
+ );
1316
+ },
1317
+ // The person's reaction to an agent's work (reactions.mjs): a verdict per post of theirs for each agent it is about;
1318
+ // `aboutSeq` is the agent's post, null when the post reacts to what the agents made together.
1319
+ recordReaction({
1320
+ postId,
1321
+ goalId,
1322
+ postSeq,
1323
+ aboutSeq,
1324
+ agent,
1325
+ verdict,
1326
+ judgments,
1327
+ }) {
1328
+ execute(
1329
+ "run",
1330
+ `insert or replace into board_reactions(post_id, goal_id, post_seq, about_seq, agent, verdict, judgments, at)
1331
+ values(?, ?, ?, ?, ?, ?, ?, ?)`,
1332
+ postId,
1333
+ goalId,
1334
+ postSeq,
1335
+ aboutSeq,
1336
+ agent,
1337
+ verdict,
1338
+ judgments ? JSON.stringify(judgments) : null,
1339
+ new Date().toISOString()
1340
+ );
1341
+ },
1342
+ // The person's verdicts on each agent's work, for its card: { agent: { accepted, changes, rejected } }.
1343
+ verdictCounts() {
1344
+ const counts = {};
1345
+ for (const row of execute(
1346
+ "all",
1347
+ "select agent, verdict, count(*) as n from board_reactions where verdict != 'neither' group by agent, verdict"
1348
+ )) {
1349
+ const entry = (counts[row.agent] ??= {
1350
+ accepted: 0,
1351
+ changes: 0,
1352
+ rejected: 0,
1353
+ });
1354
+ const key =
1355
+ row.verdict === "approves"
1356
+ ? "accepted"
1357
+ : row.verdict === "approves_with_changes"
1358
+ ? "changes"
1359
+ : "rejected";
1360
+ entry[key] += Number(row.n);
1361
+ }
1362
+ return counts;
1363
+ },
1027
1364
  // Posts the guard held (firewall.mjs) since a moment: kept apart from the room, counted for the person's digest.
1028
1365
  heldCount(goalId, since = "") {
1029
1366
  return Number(
@@ -1074,13 +1411,15 @@ export function openStore(file) {
1074
1411
  ).n
1075
1412
  );
1076
1413
  },
1077
- wakeCount(goalId, agent) {
1414
+ // `since`: count only wakes after this time (ISO), or all of them.
1415
+ wakeCount(goalId, agent, since = null) {
1078
1416
  return Number(
1079
1417
  execute(
1080
1418
  "get",
1081
- "select count(*) as n from board_tasks where parent_id = ? and kind = 'wake' and target_agent = ?",
1419
+ "select count(*) as n from board_tasks where parent_id = ? and kind = 'wake' and target_agent = ? and created_at > ?",
1082
1420
  goalId,
1083
- agent
1421
+ agent,
1422
+ since ?? ""
1084
1423
  ).n
1085
1424
  );
1086
1425
  },
@@ -1115,7 +1454,7 @@ export function openStore(file) {
1115
1454
  );
1116
1455
  execute(
1117
1456
  "run",
1118
- "update board_tasks set status = 'failed', result = ?, holder = null, reported_at = ?, updated_at = ?, version = version + 1 where parent_id = ? and status in ('open', 'claimed', 'needs_input', 'running')",
1457
+ "update board_tasks set status = 'failed', result = ?, failure = 'person', holder = null, reported_at = ?, updated_at = ?, version = version + 1 where parent_id = ? and status in ('open', 'claimed', 'needs_input', 'running')",
1119
1458
  reason,
1120
1459
  now,
1121
1460
  now,
@@ -1352,8 +1691,9 @@ export function openStore(file) {
1352
1691
  modelWork(agent) {
1353
1692
  return execute(
1354
1693
  "all",
1355
- `select json_extract(run_model, '$.model') as model, status, coalesce(run_authorized_at, created_at) as started_at, reported_at, usage from board_tasks
1356
- where coalesce(claimed_by, target_agent) = ? and json_extract(run_model, '$.model') is not null and status in ('done', 'failed') order by updated_at desc limit 2000`,
1694
+ `select json_extract(run_model, '$.model') as model, status, failure, coalesce(run_authorized_at, created_at) as started_at, reported_at, usage from board_tasks
1695
+ where coalesce(claimed_by, target_agent) = ? and json_extract(run_model, '$.model') is not null and status in ('done', 'failed') and ${AGENTS_OWN}
1696
+ order by updated_at desc limit 2000`,
1357
1697
  agent
1358
1698
  ).map((row) => ({
1359
1699
  ...row,
@@ -2195,9 +2535,9 @@ export function openStore(file) {
2195
2535
  agentWork() {
2196
2536
  return execute(
2197
2537
  "all",
2198
- `select coalesce(claimed_by, target_agent) as agent, status, target_agent is not null as targeted,
2538
+ `select coalesce(claimed_by, target_agent) as agent, status, failure, target_agent is not null as targeted,
2199
2539
  coalesce(run_authorized_at, created_at) as started_at, reported_at from board_tasks
2200
- where kind in ('task', 'wake') and removed_at is null and coalesce(claimed_by, target_agent) is not null
2540
+ where kind in ('task', 'wake') and removed_at is null and coalesce(claimed_by, target_agent) is not null and ${AGENTS_OWN}
2201
2541
  order by updated_at desc limit 5000`
2202
2542
  );
2203
2543
  },
@@ -2267,7 +2607,7 @@ export function openStore(file) {
2267
2607
  running.push(taskId);
2268
2608
  execute(
2269
2609
  "run",
2270
- "update board_tasks set status = 'failed', result = 'Removed by you.', holder = null, reported_at = ?, updated_at = ?, version = version + 1 where id = ? and kind != 'goal' and status in ('open', 'claimed', 'needs_input', 'running')",
2610
+ "update board_tasks set status = 'failed', result = 'Removed by you.', failure = 'person', holder = null, reported_at = ?, updated_at = ?, version = version + 1 where id = ? and kind != 'goal' and status in ('open', 'claimed', 'needs_input', 'running')",
2271
2611
  now,
2272
2612
  now,
2273
2613
  taskId
@@ -2354,7 +2694,12 @@ export function openStore(file) {
2354
2694
  : `task is for ${task.holder}`
2355
2695
  );
2356
2696
  },
2357
- reportTask(id, agent, { status, result = "" }, expectedVersion = null) {
2697
+ reportTask(
2698
+ id,
2699
+ agent,
2700
+ { status, result = "", failure = "agent" },
2701
+ expectedVersion = null
2702
+ ) {
2358
2703
  if (!["done", "failed", "needs_input"].includes(status))
2359
2704
  return {
2360
2705
  ok: false,
@@ -2364,13 +2709,15 @@ export function openStore(file) {
2364
2709
  const written = execute(
2365
2710
  "run",
2366
2711
  `update board_tasks set status = @status, result = @result, reported_at = @now, updated_at = @now, version = version + 1,
2367
- holder = case when @status = 'needs_input' then origin_agent else null end
2712
+ holder = case when @status = 'needs_input' then origin_agent else null end,
2713
+ failure = case when @status = 'failed' then @failure else null end
2368
2714
  where id = @id and status in ('claimed', 'running') and holder = @agent and (@expected is null or version = @expected)`,
2369
2715
  {
2370
2716
  id,
2371
2717
  agent,
2372
2718
  status,
2373
2719
  result,
2720
+ failure: FAILURE_CAUSES.includes(failure) ? failure : "agent",
2374
2721
  now,
2375
2722
  expected: expectedVersion == null ? null : Number(expectedVersion),
2376
2723
  }
package/lib/swarm.mjs CHANGED
@@ -106,6 +106,10 @@ export function createSwarm({
106
106
  whyWaiting = () => null,
107
107
  goals = null,
108
108
  reofferHarness = () => {},
109
+ schedules = null,
110
+ // The person's post read for their reaction and for lessons (reactions.mjs, lessons.mjs), and what was learned.
111
+ heardPerson = () => {},
112
+ lessons = null,
109
113
  }) {
110
114
  // What the person let a thread spend of their Harness credits, in dollars, or null for no limit of its own.
111
115
  const ceilingOf = (threadId) => {
@@ -138,9 +142,16 @@ export function createSwarm({
138
142
 
139
143
  // Who a message is for: the agents it names, else whoever coordinates, which brings in every agent with a part in it.
140
144
  // Where the coordinator cannot assemble (the privacy setting, a Harness without it) the thread's only agent takes it.
141
- // `@swarm` is for the whole swarm (the agents it names hear it from the post itself).
145
+ // `@swarm` is for the whole swarm: whoever coordinates brings in the rest, and the agents it names hear it from the post
146
+ // itself (October 16: a message naming an orchestrator and a role for each agent reached none of them).
142
147
  const addressed = (thread, text) => {
143
- if (SWARM.test(text)) return [thread.goal.facilitator];
148
+ if (SWARM.test(text))
149
+ return [
150
+ ...new Set([
151
+ thread.goal.facilitator,
152
+ ...mentionsIn(text, thread.goal.team),
153
+ ]),
154
+ ];
144
155
  const named = mentionsIn(text, thread.goal.team);
145
156
  if (named.length) return named;
146
157
  const agents = agentsOf(thread);
@@ -270,6 +281,8 @@ export function createSwarm({
270
281
  },
271
282
  // The goal as the swarm keeps it (goals.mjs): what the person wants, its checks, where it stands, every version.
272
283
  goal: goals?.record(thread.id) ?? null,
284
+ // When the thread comes back on its own (schedules.mjs): what for, how often, who set it, the next run.
285
+ checkIns: schedules?.list({ threadId: thread.id }) ?? [],
273
286
  ceilingUsd: ceilingOf(thread.id),
274
287
  changes: worktrees.summary(thread.id),
275
288
  };
@@ -364,6 +377,8 @@ export function createSwarm({
364
377
  ok: true,
365
378
  thread: summary(thread),
366
379
  posts: store.roomPosts(thread.id, 0, 500),
380
+ // What each of the person's posts changed in what the swarm keeps (lessons.mjs), for the line under the post.
381
+ kept: store.lessonsFromThread?.(thread.id) ?? [],
367
382
  runs,
368
383
  // Posts held for the person (who wrote one, when, never its words), and the post that replaced each one an agent
369
384
  // posted again.
@@ -445,20 +460,34 @@ export function createSwarm({
445
460
  ok: true,
446
461
  threads: store.searchThreads(query, { goalIds: mine, k: 8 }),
447
462
  sessions: request.teamOnly ? [] : store.search({ query, limit: 5 }),
463
+ // What the person told the swarm (lessons.mjs), to a caller on this computer.
464
+ lessons:
465
+ request.teamOnly || request.hostedCaller === true
466
+ ? []
467
+ : store.lessonsMatching(query, 5).map((lesson) => ({
468
+ about: lesson.about,
469
+ agent: lesson.agent,
470
+ text: lesson.text,
471
+ quote: lesson.evidence.at(-1)?.quote ?? null,
472
+ })),
448
473
  };
449
474
  }
450
475
  // Who can be asked: every other agent's card (cards.mjs), and the same cards as the block an agent is told when its
451
476
  // session starts. Open to every caller, so an agent picks a name instead of guessing one.
452
477
  if (command === "board.swarm.agents") {
478
+ // The person's words from other threads stay on this computer: a caller through Harness gets the cards alone.
479
+ const local = request.hostedCaller !== true;
453
480
  const agents = swarmCards(
454
481
  store,
455
- roster().filter((entry) => entry.id !== agent)
482
+ roster().filter((entry) => entry.id !== agent),
483
+ { lessons: local }
456
484
  );
485
+ const told = local ? (lessons?.told() ?? []) : [];
457
486
  return {
458
487
  ok: true,
459
488
  agents,
460
- text: swarmBlock(agents),
461
- digest: swarmDigest(agents),
489
+ text: swarmBlock(agents, Date.now(), told),
490
+ digest: swarmDigest(agents, told),
462
491
  };
463
492
  }
464
493
  // Each coding agent's models as it lists them, the person's choice for it and their routing words (models.mjs). Open
@@ -759,6 +788,7 @@ export function createSwarm({
759
788
  });
760
789
  wakeFor(thread.id, first, "user");
761
790
  if (!to) heardBy(opened, first, text);
791
+ heardPerson(opened, first);
762
792
  notifyBoard(store.getTask(thread.id));
763
793
  return { ok: true, thread: summary(store.getTask(thread.id)) };
764
794
  };
@@ -770,9 +800,127 @@ export function createSwarm({
770
800
  .then((suggested) => begin(suggested.folder)),
771
801
  };
772
802
  }
803
+ // What the swarm learned from the person (lessons.mjs), the person's to read: only the person gets this far.
804
+ if (command === "board.swarm.lessons")
805
+ return {
806
+ ok: true,
807
+ lessons: (
808
+ lessons?.list({
809
+ status: request.replaced === true ? "replaced" : "active",
810
+ }) ?? []
811
+ ).map((lesson) => {
812
+ const last = lesson.evidence.at(-1);
813
+ return {
814
+ ...lesson,
815
+ // The last two times the person said it, with the thread's title, for the buddy's open lesson.
816
+ said: lesson.evidence
817
+ .filter((entry) => entry.quote && !entry.tookBack)
818
+ .slice(-2)
819
+ .map((entry) => ({
820
+ quote: entry.quote,
821
+ thread: store.getTask(entry.goalId)?.objective ?? null,
822
+ at: entry.at,
823
+ })),
824
+ from: last
825
+ ? {
826
+ thread: store.getTask(last.goalId)?.objective ?? null,
827
+ seq: last.seq,
828
+ quote: last.quote,
829
+ }
830
+ : null,
831
+ };
832
+ }),
833
+ };
834
+ if (command === "board.swarm.lessons.remove") {
835
+ const removed = lessons?.remove(request.id);
836
+ return removed
837
+ ? { ok: true, lesson: removed }
838
+ : { ok: false, error: "no lesson starts with that id" };
839
+ }
840
+ if (command === "board.swarm.lessons.undo")
841
+ return {
842
+ pending: (
843
+ lessons?.undo(
844
+ String(request.lessonId || ""),
845
+ String(request.change || "")
846
+ ) ?? Promise.resolve(null)
847
+ ).then((lesson) =>
848
+ lesson
849
+ ? { ok: true, lesson }
850
+ : { ok: false, error: "that cannot be undone now" }
851
+ ),
852
+ };
853
+ if (command === "board.swarm.lessons.edit")
854
+ return {
855
+ pending: (
856
+ lessons?.edit(request.id, request.text) ?? Promise.resolve(null)
857
+ ).then((edited) =>
858
+ edited
859
+ ? { ok: true, lesson: edited }
860
+ : {
861
+ ok: false,
862
+ error:
863
+ "no lesson starts with that id, or the new words are empty",
864
+ }
865
+ ),
866
+ };
867
+ // Check-ins (schedules.mjs), the person's: only the person gets this far, and the lead sets its own in its rounds
868
+ // (coordinator.mjs). Every thread's, or ending one by its own id.
869
+ if (command === "board.swarm.checkins" && !request.id)
870
+ return {
871
+ ok: true,
872
+ checkIns: schedules?.list({ all: request.all === true }) ?? [],
873
+ };
874
+ if (command === "board.swarm.checkin.remove") {
875
+ const removed = schedules?.remove(String(request.checkIn || "")) ?? {
876
+ ok: false,
877
+ error: "this daemon keeps no check-ins",
878
+ };
879
+ if (removed.ok) notifyBoard(store.getTask(removed.schedule.threadId));
880
+ return removed;
881
+ }
773
882
  const thread = store.getTask(String(request.id || ""));
774
883
  if (!thread || thread.kind !== "goal")
775
884
  return { ok: false, error: "thread not found" };
885
+ if (command === "board.swarm.checkins")
886
+ return {
887
+ ok: true,
888
+ checkIns: schedules?.list({ threadId: thread.id }) ?? [],
889
+ };
890
+ // The person's check-in on this thread: when, what for, and who it wakes (the lead when it names no one), only agents
891
+ // that can be in a thread.
892
+ if (command === "board.swarm.checkin") {
893
+ if (!schedules)
894
+ return { ok: false, error: "this daemon keeps no check-ins" };
895
+ const agents = Array.isArray(request.agents)
896
+ ? request.agents.map(String)
897
+ : [];
898
+ const unknown = agents.filter((name) => !roomAgents.includes(name));
899
+ if (unknown.length)
900
+ return { ok: false, error: `no such agent: ${unknown.join(", ")}` };
901
+ const set = schedules.add({
902
+ threadId: thread.id,
903
+ what: request.what,
904
+ agents,
905
+ cadence: request.cadence,
906
+ by: "user",
907
+ maxRuns: request.maxRuns ?? null,
908
+ endsAt: request.endsAt ?? null,
909
+ });
910
+ if (!set.ok) return set;
911
+ store.roomPost({
912
+ goalId: thread.id,
913
+ author: "user",
914
+ kind: "note",
915
+ text: `You set a check-in (${set.schedule.when}): ${set.schedule.what}`,
916
+ });
917
+ notifyBoard(store.getTask(thread.id));
918
+ return {
919
+ ok: true,
920
+ checkIn: set.schedule,
921
+ thread: summary(store.getTask(thread.id)),
922
+ };
923
+ }
776
924
  if (command === "board.swarm.read")
777
925
  return read(thread, request.hostedCaller === true);
778
926
  if (command === "board.swarm.diff")
@@ -907,6 +1055,8 @@ export function createSwarm({
907
1055
  kind: "note",
908
1056
  text: "Resumed by you.",
909
1057
  });
1058
+ // A one-time check-in held while the thread was paused runs now.
1059
+ schedules?.tick();
910
1060
  notifyBoard(store.getTask(thread.id));
911
1061
  return { ok: true, thread: summary(store.getTask(thread.id)) };
912
1062
  }