tryharness 0.7.31 → 0.7.33

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,20 @@ 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'))";
30
+ // A room post as readers get it. A file attached in the room names it, so a reader can show the file itself.
31
+ const roomPostOf = ({ source, ...row }) => ({
32
+ ...row,
33
+ seq: Number(row.seq),
34
+ mentions: row.mentions ? JSON.parse(row.mentions) : [],
35
+ ...(row.kind === "file" && String(source).startsWith("file:")
36
+ ? { file: String(source).slice(5) }
37
+ : {}),
38
+ });
25
39
 
26
40
  export function openStore(file) {
27
41
  const db = new Database(file);
@@ -87,6 +101,7 @@ export function openStore(file) {
87
101
  usage text,
88
102
  run_model text,
89
103
  removed_at text,
104
+ failure text,
90
105
  created_at text not null,
91
106
  updated_at text not null
92
107
  );
@@ -188,6 +203,33 @@ export function openStore(file) {
188
203
  error text
189
204
  );
190
205
  create index if not exists swarm_rounds_goal_idx on swarm_rounds(goal_id, at);
206
+ create table if not exists board_reactions (
207
+ post_id text not null,
208
+ agent text not null,
209
+ goal_id text not null,
210
+ post_seq integer not null,
211
+ about_seq integer,
212
+ verdict text not null,
213
+ judgments text,
214
+ at text not null,
215
+ primary key(post_id, agent)
216
+ );
217
+ create table if not exists board_lessons (
218
+ id text primary key,
219
+ about text not null,
220
+ agent text,
221
+ text text not null,
222
+ scope text not null,
223
+ folder text,
224
+ source text not null default 'said',
225
+ evidence text not null default '[]',
226
+ status text not null default 'active',
227
+ replaced_by text,
228
+ vector blob,
229
+ created_at text not null,
230
+ updated_at text not null
231
+ );
232
+ create virtual table if not exists board_lessons_fts using fts5(id unindexed, text, tokenize='unicode61');
191
233
  create table if not exists board_vectors (
192
234
  contribution_id text primary key,
193
235
  version text not null,
@@ -284,6 +326,19 @@ export function openStore(file) {
284
326
  db.exec("alter table board_tasks add column run_model text");
285
327
  if (!boardColumns.has("removed_at") && boardColumns.size)
286
328
  db.exec("alter table board_tasks add column removed_at text");
329
+ // Why a failed task failed (FAILURE_CAUSES). Tasks failed before it was kept are read from the fixed results the
330
+ // daemon wrote for each cause; any other failed task was reported failed by its agent.
331
+ if (!boardColumns.has("failure") && boardColumns.size)
332
+ db.exec(`alter table board_tasks add column failure text;
333
+ update board_tasks set failure = case
334
+ when result in ('Stopped by you.', 'Stopped by the owner.', 'Removed by you.', 'Stopped.') then 'person'
335
+ when result like 'T3 Code did not take it:%' or result like 'Could not start %'
336
+ or result like 'Another run is already working in %' or result = 'Invalid task run id.' then 'start'
337
+ when result like '% exited with code %' or result like '% stopped on an error from its model:%'
338
+ or result like 'T3 Code ended the turn%' or result like '%The turn may still be going in T3 Code.'
339
+ or result = 'This run was interrupted earlier and is not started again.' then 'exit'
340
+ else 'agent' end
341
+ where status = 'failed';`);
287
342
  if (!boardColumns.has("trust") && boardColumns.size)
288
343
  db.exec(
289
344
  "alter table board_tasks add column trust text not null default 'agent'; alter table board_tasks add column origin_chain text not null default '[]'"
@@ -981,21 +1036,31 @@ export function openStore(file) {
981
1036
  return post ? { ...post, mentions } : null;
982
1037
  },
983
1038
  roomPosts(goalId, since = 0, limit = 200) {
984
- // A file attached in the room names it, so a reader can show the file itself.
985
1039
  return execute(
986
1040
  "all",
987
1041
  "select id, seq, kind, author, text, mentions, source, created_at from board_contributions where task_id = ? and seq > ? and kind != 'held' order by seq asc limit ?",
988
1042
  goalId,
989
1043
  Number(since) || 0,
990
1044
  Math.max(1, Math.min(500, Number(limit) || 200))
991
- ).map(({ source, ...row }) => ({
992
- ...row,
993
- seq: Number(row.seq),
994
- mentions: row.mentions ? JSON.parse(row.mentions) : [],
995
- ...(row.kind === "file" && String(source).startsWith("file:")
996
- ? { file: String(source).slice(5) }
997
- : {}),
998
- }));
1045
+ ).map(roomPostOf);
1046
+ },
1047
+ // The newest posts before `before` (every post when it is not given), oldest first, and the seq to read before
1048
+ // for the ones earlier still (null at the thread's start). roomPosts reads from the start, which in a thread past
1049
+ // its limit is the oldest posts (October 17: Codex was woken with posts 172 to 201 of 294).
1050
+ recentRoomPosts(goalId, { before = null, limit = 30 } = {}) {
1051
+ const count = Math.max(1, Math.min(500, Number(limit) || 30));
1052
+ const rows = execute(
1053
+ "all",
1054
+ "select id, seq, kind, author, text, mentions, source, created_at from board_contributions where task_id = ? and seq < ? and kind != 'held' order by seq desc limit ?",
1055
+ goalId,
1056
+ Number(before) > 0 ? Number(before) : Number.MAX_SAFE_INTEGER,
1057
+ count + 1
1058
+ );
1059
+ const posts = rows.slice(0, count).reverse().map(roomPostOf);
1060
+ return {
1061
+ posts,
1062
+ earlierBefore: rows.length > count ? (posts[0]?.seq ?? null) : null,
1063
+ };
999
1064
  },
1000
1065
  // What each agent's finished runs' receipts showed (receipts.mjs, kept as receipt:<task>): { agent: { tested, untested, failing } }.
1001
1066
  receiptCounts() {
@@ -1024,6 +1089,297 @@ export function openStore(file) {
1024
1089
  }
1025
1090
  return counts;
1026
1091
  },
1092
+ // What the swarm learned (lessons.mjs): each lesson with the person's words it stands on (`evidence`: the thread,
1093
+ // post and quote), active until a later one replaces it.
1094
+ addLesson({ about, agent, text, scope, folder, evidence }) {
1095
+ const id = randomUUID();
1096
+ const now = new Date().toISOString();
1097
+ execute(
1098
+ "run",
1099
+ `insert into board_lessons(id, about, agent, text, scope, folder, evidence, created_at, updated_at)
1100
+ values(?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1101
+ id,
1102
+ about,
1103
+ agent ?? null,
1104
+ text,
1105
+ scope,
1106
+ folder ?? null,
1107
+ JSON.stringify([evidence]),
1108
+ now,
1109
+ now
1110
+ );
1111
+ execute(
1112
+ "run",
1113
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1114
+ id,
1115
+ text
1116
+ );
1117
+ return this.getLesson(id);
1118
+ },
1119
+ getLesson(id) {
1120
+ const row = execute(
1121
+ "get",
1122
+ "select * from board_lessons where id = ?",
1123
+ id
1124
+ );
1125
+ if (!row) return null;
1126
+ const { vector, ...lesson } = row;
1127
+ const evidence = JSON.parse(lesson.evidence || "[]");
1128
+ return {
1129
+ ...lesson,
1130
+ evidence,
1131
+ // Times the person said it again after an agent had been given it (lessons.mjs).
1132
+ missed: evidence.filter((entry) => entry.carried).length,
1133
+ embedded: Boolean(vector),
1134
+ };
1135
+ },
1136
+ // The person said it again: the lesson keeps its words and gains the new evidence.
1137
+ confirmLesson(id, evidence) {
1138
+ const lesson = this.getLesson(id);
1139
+ if (!lesson || lesson.status !== "active") return null;
1140
+ execute(
1141
+ "run",
1142
+ "update board_lessons set evidence = ?, updated_at = ? where id = ?",
1143
+ JSON.stringify([...lesson.evidence, evidence].slice(-20)),
1144
+ new Date().toISOString(),
1145
+ id
1146
+ );
1147
+ return this.getLesson(id);
1148
+ },
1149
+ // The person changed or reversed it: a new lesson replaces the old one, which stays as the record of what was.
1150
+ replaceLesson(id, lesson) {
1151
+ const old = this.getLesson(id);
1152
+ if (!old || old.status !== "active") return null;
1153
+ const added = this.addLesson(lesson);
1154
+ execute(
1155
+ "run",
1156
+ "update board_lessons set status = 'replaced', replaced_by = ?, updated_at = ? where id = ?",
1157
+ added.id,
1158
+ new Date().toISOString(),
1159
+ id
1160
+ );
1161
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1162
+ return added;
1163
+ },
1164
+ // The person's own edit: their words become the lesson's, and it is read again for meaning.
1165
+ editLesson(id, text) {
1166
+ const lesson = this.getLesson(id);
1167
+ if (!lesson || lesson.status !== "active") return null;
1168
+ execute(
1169
+ "run",
1170
+ "update board_lessons set text = ?, source = 'person', vector = null, updated_at = ? where id = ?",
1171
+ text,
1172
+ new Date().toISOString(),
1173
+ id
1174
+ );
1175
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1176
+ execute(
1177
+ "run",
1178
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1179
+ id,
1180
+ text
1181
+ );
1182
+ return this.getLesson(id);
1183
+ },
1184
+ // The person took it back in a thread with nothing in its place: it leaves the work and stays on record as replaced,
1185
+ // by nothing, with the words that took it back.
1186
+ takeBackLesson(id, evidence) {
1187
+ const lesson = this.getLesson(id);
1188
+ if (!lesson || lesson.status !== "active") return null;
1189
+ execute(
1190
+ "run",
1191
+ "update board_lessons set status = 'replaced', replaced_by = null, evidence = ?, vector = null, updated_at = ? where id = ?",
1192
+ JSON.stringify(
1193
+ [...lesson.evidence, { ...evidence, tookBack: true }].slice(-20)
1194
+ ),
1195
+ new Date().toISOString(),
1196
+ id
1197
+ );
1198
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1199
+ return this.getLesson(id);
1200
+ },
1201
+ // A lesson replaced, taken back or removed is in the work again, as it was (the person's Undo, lessons.mjs).
1202
+ restoreLesson(id, { dropTookBack = false } = {}) {
1203
+ const lesson = this.getLesson(id);
1204
+ if (!lesson || lesson.status === "active") return lesson;
1205
+ const evidence = dropTookBack
1206
+ ? lesson.evidence.filter((entry) => !entry.tookBack)
1207
+ : lesson.evidence;
1208
+ execute(
1209
+ "run",
1210
+ "update board_lessons set status = 'active', replaced_by = null, evidence = ?, updated_at = ? where id = ?",
1211
+ JSON.stringify(evidence),
1212
+ new Date().toISOString(),
1213
+ id
1214
+ );
1215
+ execute(
1216
+ "run",
1217
+ "insert into board_lessons_fts(id, text) values(?, ?)",
1218
+ id,
1219
+ lesson.text
1220
+ );
1221
+ return this.getLesson(id);
1222
+ },
1223
+ // What the person's posts in a thread changed in what the swarm keeps, for the line under each post: a lesson kept
1224
+ // or changed by the post (its first words), or taken back by it. Saying a lesson again changes nothing to show.
1225
+ lessonsFromThread(goalId) {
1226
+ const rows = execute(
1227
+ "all",
1228
+ "select id from board_lessons where evidence like ? order by created_at",
1229
+ `%"goalId":"${String(goalId).replace(/[%_"]/g, "")}"%`
1230
+ ).map((row) => this.getLesson(row.id));
1231
+ const replaced = new Set(
1232
+ execute(
1233
+ "all",
1234
+ "select replaced_by from board_lessons where replaced_by is not null"
1235
+ ).map((row) => row.replaced_by)
1236
+ );
1237
+ const lines = [];
1238
+ for (const lesson of rows) {
1239
+ const first = lesson.evidence[0];
1240
+ // A lesson the person removed, or undid, has no line; one a later post changed keeps its line, without Undo.
1241
+ if (
1242
+ first?.goalId === goalId &&
1243
+ first.postId &&
1244
+ lesson.status !== "removed"
1245
+ )
1246
+ lines.push({
1247
+ postId: first.postId,
1248
+ lessonId: lesson.id,
1249
+ change: replaced.has(lesson.id) ? "changed" : "kept",
1250
+ text: lesson.text,
1251
+ about: lesson.about,
1252
+ agent: lesson.agent,
1253
+ undoable: lesson.status === "active",
1254
+ });
1255
+ for (const entry of lesson.evidence)
1256
+ if (entry.tookBack && entry.goalId === goalId && entry.postId)
1257
+ lines.push({
1258
+ postId: entry.postId,
1259
+ lessonId: lesson.id,
1260
+ change: "taken back",
1261
+ text: lesson.text,
1262
+ about: lesson.about,
1263
+ agent: lesson.agent,
1264
+ undoable: lesson.status !== "active",
1265
+ });
1266
+ }
1267
+ return lines;
1268
+ },
1269
+ // The person took it out: it reaches no work and is matched by nothing, and stays on record as removed.
1270
+ removeLesson(id) {
1271
+ const lesson = this.getLesson(id);
1272
+ if (!lesson || lesson.status !== "active") return null;
1273
+ execute(
1274
+ "run",
1275
+ "update board_lessons set status = 'removed', vector = null, updated_at = ? where id = ?",
1276
+ new Date().toISOString(),
1277
+ id
1278
+ );
1279
+ execute("run", "delete from board_lessons_fts where id = ?", id);
1280
+ return this.getLesson(id);
1281
+ },
1282
+ lessons({ status = "active", limit = 200 } = {}) {
1283
+ return execute(
1284
+ "all",
1285
+ "select id from board_lessons where status = ? order by updated_at desc limit ?",
1286
+ status,
1287
+ Math.max(1, Math.min(1000, Number(limit) || 200))
1288
+ ).map((row) => this.getLesson(row.id));
1289
+ },
1290
+ // Active lessons sharing words with a text, best first.
1291
+ lessonsMatching(query, limit = 6) {
1292
+ const terms = [
1293
+ ...new Set(
1294
+ String(query)
1295
+ .toLowerCase()
1296
+ .match(/[\p{L}\p{N}_-]+/gu) || []
1297
+ ),
1298
+ ]
1299
+ .filter((term) => term.length > 2)
1300
+ .slice(0, 24);
1301
+ if (!terms.length) return [];
1302
+ const match = terms
1303
+ .map((term) => `"${term.replaceAll('"', '""')}"`)
1304
+ .join(" OR ");
1305
+ return execute(
1306
+ "all",
1307
+ `select f.id from board_lessons_fts f join board_lessons l on l.id = f.id
1308
+ where board_lessons_fts match ? and l.status = 'active' order by bm25(board_lessons_fts) limit ?`,
1309
+ match,
1310
+ limit
1311
+ ).map((row) => this.getLesson(row.id));
1312
+ },
1313
+ // What the person has said about one agent, newest first, for its card.
1314
+ lessonsAbout(agent, limit = 5) {
1315
+ return execute(
1316
+ "all",
1317
+ "select text from board_lessons where status = 'active' and about = 'agent' and agent = ? order by updated_at desc limit ?",
1318
+ agent,
1319
+ limit
1320
+ ).map((row) => row.text);
1321
+ },
1322
+ setLessonVector(id, vector) {
1323
+ execute(
1324
+ "run",
1325
+ "update board_lessons set vector = ? where id = ?",
1326
+ vector,
1327
+ id
1328
+ );
1329
+ },
1330
+ lessonVectors() {
1331
+ return execute(
1332
+ "all",
1333
+ "select id, vector from board_lessons where status = 'active' and vector is not null"
1334
+ );
1335
+ },
1336
+ // The person's reaction to an agent's work (reactions.mjs): a verdict per post of theirs for each agent it is about;
1337
+ // `aboutSeq` is the agent's post, null when the post reacts to what the agents made together.
1338
+ recordReaction({
1339
+ postId,
1340
+ goalId,
1341
+ postSeq,
1342
+ aboutSeq,
1343
+ agent,
1344
+ verdict,
1345
+ judgments,
1346
+ }) {
1347
+ execute(
1348
+ "run",
1349
+ `insert or replace into board_reactions(post_id, goal_id, post_seq, about_seq, agent, verdict, judgments, at)
1350
+ values(?, ?, ?, ?, ?, ?, ?, ?)`,
1351
+ postId,
1352
+ goalId,
1353
+ postSeq,
1354
+ aboutSeq,
1355
+ agent,
1356
+ verdict,
1357
+ judgments ? JSON.stringify(judgments) : null,
1358
+ new Date().toISOString()
1359
+ );
1360
+ },
1361
+ // The person's verdicts on each agent's work, for its card: { agent: { accepted, changes, rejected } }.
1362
+ verdictCounts() {
1363
+ const counts = {};
1364
+ for (const row of execute(
1365
+ "all",
1366
+ "select agent, verdict, count(*) as n from board_reactions where verdict != 'neither' group by agent, verdict"
1367
+ )) {
1368
+ const entry = (counts[row.agent] ??= {
1369
+ accepted: 0,
1370
+ changes: 0,
1371
+ rejected: 0,
1372
+ });
1373
+ const key =
1374
+ row.verdict === "approves"
1375
+ ? "accepted"
1376
+ : row.verdict === "approves_with_changes"
1377
+ ? "changes"
1378
+ : "rejected";
1379
+ entry[key] += Number(row.n);
1380
+ }
1381
+ return counts;
1382
+ },
1027
1383
  // Posts the guard held (firewall.mjs) since a moment: kept apart from the room, counted for the person's digest.
1028
1384
  heldCount(goalId, since = "") {
1029
1385
  return Number(
@@ -1074,13 +1430,15 @@ export function openStore(file) {
1074
1430
  ).n
1075
1431
  );
1076
1432
  },
1077
- wakeCount(goalId, agent) {
1433
+ // `since`: count only wakes after this time (ISO), or all of them.
1434
+ wakeCount(goalId, agent, since = null) {
1078
1435
  return Number(
1079
1436
  execute(
1080
1437
  "get",
1081
- "select count(*) as n from board_tasks where parent_id = ? and kind = 'wake' and target_agent = ?",
1438
+ "select count(*) as n from board_tasks where parent_id = ? and kind = 'wake' and target_agent = ? and created_at > ?",
1082
1439
  goalId,
1083
- agent
1440
+ agent,
1441
+ since ?? ""
1084
1442
  ).n
1085
1443
  );
1086
1444
  },
@@ -1115,7 +1473,7 @@ export function openStore(file) {
1115
1473
  );
1116
1474
  execute(
1117
1475
  "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')",
1476
+ "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
1477
  reason,
1120
1478
  now,
1121
1479
  now,
@@ -1352,8 +1710,9 @@ export function openStore(file) {
1352
1710
  modelWork(agent) {
1353
1711
  return execute(
1354
1712
  "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`,
1713
+ `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
1714
+ where coalesce(claimed_by, target_agent) = ? and json_extract(run_model, '$.model') is not null and status in ('done', 'failed') and ${AGENTS_OWN}
1715
+ order by updated_at desc limit 2000`,
1357
1716
  agent
1358
1717
  ).map((row) => ({
1359
1718
  ...row,
@@ -2195,9 +2554,9 @@ export function openStore(file) {
2195
2554
  agentWork() {
2196
2555
  return execute(
2197
2556
  "all",
2198
- `select coalesce(claimed_by, target_agent) as agent, status, target_agent is not null as targeted,
2557
+ `select coalesce(claimed_by, target_agent) as agent, status, failure, target_agent is not null as targeted,
2199
2558
  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
2559
+ where kind in ('task', 'wake') and removed_at is null and coalesce(claimed_by, target_agent) is not null and ${AGENTS_OWN}
2201
2560
  order by updated_at desc limit 5000`
2202
2561
  );
2203
2562
  },
@@ -2267,7 +2626,7 @@ export function openStore(file) {
2267
2626
  running.push(taskId);
2268
2627
  execute(
2269
2628
  "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')",
2629
+ "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
2630
  now,
2272
2631
  now,
2273
2632
  taskId
@@ -2354,7 +2713,12 @@ export function openStore(file) {
2354
2713
  : `task is for ${task.holder}`
2355
2714
  );
2356
2715
  },
2357
- reportTask(id, agent, { status, result = "" }, expectedVersion = null) {
2716
+ reportTask(
2717
+ id,
2718
+ agent,
2719
+ { status, result = "", failure = "agent" },
2720
+ expectedVersion = null
2721
+ ) {
2358
2722
  if (!["done", "failed", "needs_input"].includes(status))
2359
2723
  return {
2360
2724
  ok: false,
@@ -2364,13 +2728,15 @@ export function openStore(file) {
2364
2728
  const written = execute(
2365
2729
  "run",
2366
2730
  `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
2731
+ holder = case when @status = 'needs_input' then origin_agent else null end,
2732
+ failure = case when @status = 'failed' then @failure else null end
2368
2733
  where id = @id and status in ('claimed', 'running') and holder = @agent and (@expected is null or version = @expected)`,
2369
2734
  {
2370
2735
  id,
2371
2736
  agent,
2372
2737
  status,
2373
2738
  result,
2739
+ failure: FAILURE_CAUSES.includes(failure) ? failure : "agent",
2374
2740
  now,
2375
2741
  expected: expectedVersion == null ? null : Number(expectedVersion),
2376
2742
  }