tryharness 0.7.66 → 0.7.68

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/places.mjs CHANGED
@@ -6,6 +6,7 @@ import { spawn, spawnSync } from "node:child_process";
6
6
  import { historyRoots, paths } from "./paths.mjs";
7
7
  import { agentEnvironment } from "./agentnode.mjs";
8
8
  import { mainCheckout } from "./worktrees.mjs";
9
+ import { kept } from "./kept.mjs";
9
10
 
10
11
  // Where a coding session runs, and how it opens there again. A session's place comes from its own environment: the
11
12
  // Harness piece inside the agent sends it when the session starts (the Claude Code and Codex hooks, nudge.mjs; the
@@ -695,13 +696,37 @@ function t3Query(home, sql) {
695
696
  );
696
697
  return found.status === 0 ? (found.stdout || "").trim() : null;
697
698
  }
698
- const t3Tables = (home) =>
699
- new Set(
700
- (
701
- t3Query(home, "select name from sqlite_master where type = 'table';") ||
702
- ""
703
- ).split("\n")
704
- );
699
+ // The same reads, answered from memory and taken again beside the daemon's thread (kept.mjs): T3's tables change only
700
+ // when T3 updates, and what it runs now is asked for on every read of the buddy's live list (t3ThreadRecent,
701
+ // t3WorkingRecently). A decision made once, such as whether a thread's round waits for T3, reads now.
702
+ function* t3QuerySteps(home, sql) {
703
+ const db = t3Database(home);
704
+ if (!db || !fs.existsSync(SQLITE)) return null;
705
+ const out = yield [SQLITE, ["-readonly", "-cmd", ".timeout 500", db, sql]];
706
+ return out === null ? null : out.trim();
707
+ }
708
+ const keptT3Query = (freshMs, staleMs, wanting) =>
709
+ kept(t3QuerySteps, {
710
+ freshMs,
711
+ staleMs,
712
+ keyOf: (home, sql) => `${home}\0${sql}`,
713
+ wanting,
714
+ });
715
+ // No answer (T3 not installed or started yet) is asked again, so a T3 that starts later is seen; so is a table the kept
716
+ // list lacks, so a T3 that updated since (or just made its tables) is seen at once.
717
+ const T3_TABLES_SQL = "select name from sqlite_master where type = 'table';";
718
+ const tableNames = (tables) => (tables || "").split("\n");
719
+ const t3QueryTables = keptT3Query(
720
+ 10 * 60_000,
721
+ Infinity,
722
+ (tables, _home, _sql, needed) =>
723
+ !tables || Boolean(needed && !tableNames(tables).includes(needed))
724
+ );
725
+ const t3QueryRecent = keptT3Query(2_000, 15_000);
726
+ const t3Tables = (home) => ({
727
+ has: (name) =>
728
+ tableNames(t3QueryTables(home, T3_TABLES_SQL, name)).includes(name),
729
+ });
705
730
 
706
731
  /** A read of T3's record that never holds the daemon (its database is gigabytes; a cold read took 5.7 s). */
707
732
  function t3QueryLater(home, sql) {
@@ -816,7 +841,7 @@ export function t3Checkouts(home = t3Home()) {
816
841
  * provider thread; 0.0.45 in provider_session_runtime), else T3's import of it. A T3 thread's own id (a turn Harness
817
842
  * handed to T3, t3.mjs runInT3) is that thread.
818
843
  */
819
- export function t3Thread(sessionId, home = t3Home()) {
844
+ export function t3Thread(sessionId, home = t3Home(), query = t3Query) {
820
845
  if (!PLAIN_ID.test(String(sessionId || ""))) return null;
821
846
  const tables = t3Tables(home);
822
847
  // The id is checked above: letters, digits and . _ : - only.
@@ -835,13 +860,17 @@ export function t3Thread(sessionId, home = t3Home()) {
835
860
  );
836
861
  if (!parts.length) return null;
837
862
  return (
838
- t3Query(
863
+ query(
839
864
  home,
840
865
  `select thread_id from (${parts.join(" union all ")}) order by at desc limit 1;`
841
866
  ) || null
842
867
  );
843
868
  }
844
869
 
870
+ /** t3Thread as of a reading up to a couple of seconds old, for the buddy's live list. */
871
+ export const t3ThreadRecent = (sessionId, home = t3Home()) =>
872
+ t3Thread(sessionId, home, t3QueryRecent);
873
+
845
874
  /** T3 Code's own link to one of its threads (t3code://threads/<environment>/<thread>). */
846
875
  export function t3Link(threadId, home = t3Home()) {
847
876
  let environment = "";
@@ -958,7 +987,7 @@ export function t3Model(agent, home = t3Home()) {
958
987
  * its folder (the thread's worktree, else its project's). Codex in T3 runs on Codex's shared background server, outside
959
988
  * T3's app, so this is how its running sessions there are found.
960
989
  */
961
- export function t3Working(agent, home = t3Home()) {
990
+ export function t3Working(agent, home = t3Home(), query = t3Query) {
962
991
  const provider = t3Provider(agent);
963
992
  const tables = t3Tables(home);
964
993
  if (
@@ -968,7 +997,7 @@ export function t3Working(agent, home = t3Home()) {
968
997
  )
969
998
  return [];
970
999
  const projects = tables.has("projection_projects");
971
- const rows = t3Query(
1000
+ const rows = query(
972
1001
  home,
973
1002
  `select p.thread_id || char(9) || json_extract(p.payload_json, '$.nativeThreadRef.nativeId') || char(9) || coalesce(json_extract(t.payload_json, '$.worktreePath'), ${projects ? "r.workspace_root, " : ""}'') || char(9) || p.updated_at
974
1003
  from orchestration_v2_projection_provider_threads p join orchestration_v2_projection_threads t on t.thread_id = p.thread_id${projects ? " left join projection_projects r on r.project_id = t.project_id" : ""}
@@ -986,6 +1015,10 @@ export function t3Working(agent, home = t3Home()) {
986
1015
  }));
987
1016
  }
988
1017
 
1018
+ /** t3Working as of a reading up to a couple of seconds old, for the buddy's live list. */
1019
+ export const t3WorkingRecently = (agent, home = t3Home()) =>
1020
+ t3Working(agent, home, t3QueryRecent);
1021
+
989
1022
  /**
990
1023
  * What a turn T3 Code ran used, from T3's own record of the provider's turns for that run (every attempt): tokens in
991
1024
  * and out, cached and cache-written. T3 records no price, so the cost stays unknown. Null when T3 has no record of it.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Calls across a worker thread's port, both ways: each side answers calls to the functions it serves and calls the
3
+ * other side's by name, getting a promise. Arguments and results are copied (structured clone), so a function or a
4
+ * class instance cannot cross; a Buffer arrives as a Uint8Array.
5
+ *
6
+ * The daemon runs its JavaScript, its WASM SQLite and every synchronous file read on one thread, so work that reads a
7
+ * large database runs in a worker (contextWorker.mjs) and asks the daemon only for what the daemon owns.
8
+ */
9
+ export function portCalls(port, serve = {}) {
10
+ let next = 0;
11
+ const waiting = new Map();
12
+
13
+ const answer = async ({ call, name, args }) => {
14
+ try {
15
+ const fn = serve[name];
16
+ if (typeof fn !== "function") throw new Error(`${name} is not served`);
17
+ const value = await fn(...args);
18
+ port.postMessage({ reply: call, value });
19
+ } catch (error) {
20
+ port.postMessage({
21
+ reply: call,
22
+ error: error instanceof Error ? error.message : String(error),
23
+ });
24
+ }
25
+ };
26
+
27
+ port.on("message", (message) => {
28
+ if (message?.call !== undefined) return void answer(message);
29
+ const pending = waiting.get(message?.reply);
30
+ if (!pending) return;
31
+ waiting.delete(message.reply);
32
+ if (message.error !== undefined) pending.reject(new Error(message.error));
33
+ else pending.resolve(message.value);
34
+ });
35
+
36
+ return {
37
+ call: (name, ...args) =>
38
+ new Promise((resolve, reject) => {
39
+ const id = next++;
40
+ waiting.set(id, { resolve, reject });
41
+ try {
42
+ port.postMessage({ call: id, name, args });
43
+ } catch (error) {
44
+ waiting.delete(id);
45
+ reject(error);
46
+ }
47
+ }),
48
+ /** Fails every call still waiting, as when the other side has gone. */
49
+ fail(why) {
50
+ for (const pending of waiting.values()) pending.reject(new Error(why));
51
+ waiting.clear();
52
+ },
53
+ };
54
+ }
package/lib/runBrief.mjs CHANGED
@@ -116,7 +116,7 @@ export function createRunBriefs({
116
116
  // The thread's task frame, refreshed now, for an agent on this computer.
117
117
  const frame =
118
118
  RUNNERS.has(agent) && threadId
119
- ? frameFor(threadId, { pack: found.pack })
119
+ ? await frameFor(threadId, { pack: found.pack })
120
120
  : null;
121
121
  found.frame = frame ? frames.briefText(frame, agent) : "";
122
122
  if (found.frame && !dryRun) {
package/lib/sealed.mjs CHANGED
@@ -138,6 +138,43 @@ export async function seal(key, direction, deviceId, requestId, value) {
138
138
  };
139
139
  }
140
140
 
141
+ // A file part for a phone that asked for bytes (board.file.get raw): the plaintext is BYTES_MAGIC, the length of a JSON
142
+ // header, the header, then the bytes themselves, so they are not base64 inside the seal as well as outside it.
143
+ // The phone tells the two apart by the first bytes (a JSON plaintext starts with "{"): src/shared/crypto/sealedChannel.ts.
144
+ export const BYTES_MAGIC = "HSB1";
145
+ export async function sealFilePart(
146
+ key,
147
+ direction,
148
+ deviceId,
149
+ requestId,
150
+ header,
151
+ bytes
152
+ ) {
153
+ const head = encoder.encode(JSON.stringify(header));
154
+ const plain = new Uint8Array(8 + head.length + bytes.length);
155
+ plain.set(encoder.encode(BYTES_MAGIC), 0);
156
+ new DataView(plain.buffer).setUint32(4, head.length);
157
+ plain.set(head, 8);
158
+ plain.set(bytes, 8 + head.length);
159
+ const iv = webcrypto.getRandomValues(new Uint8Array(12));
160
+ const ct = await subtle.encrypt(
161
+ {
162
+ name: "AES-GCM",
163
+ iv,
164
+ additionalData: aad(direction, deviceId, requestId),
165
+ },
166
+ key,
167
+ plain
168
+ );
169
+ return {
170
+ v: SEALED_VERSION,
171
+ deviceId,
172
+ requestId,
173
+ iv: b64u(iv),
174
+ ct: b64u(new Uint8Array(ct)),
175
+ };
176
+ }
177
+
141
178
  // Throws on a wrong key, a wrong direction, device or request id, or any changed byte.
142
179
  export async function open(key, direction, envelope) {
143
180
  const plain = await subtle.decrypt(
@@ -266,10 +266,11 @@ export function createSessionControl({
266
266
  // its process tree and its terminal. Codex's terminals share one background server, started from wherever Codex first
267
267
  // ran: a session there has no app or terminal of its own, and is placed by the Codex terminal working in its folder.
268
268
  const placeOfReport = (agentId, folder, request) => {
269
- const table = processTable();
270
- const pids = (Array.isArray(request.pids) ? request.pids : [])
271
- .filter((pid) => Number.isInteger(pid) && pid > 1 && table.has(pid))
272
- .slice(0, 2);
269
+ const named = (Array.isArray(request.pids) ? request.pids : []).filter(
270
+ (pid) => Number.isInteger(pid) && pid > 1
271
+ );
272
+ const table = processTable({ having: named });
273
+ const pids = named.filter((pid) => table.has(pid)).slice(0, 2);
273
274
  if (harnessRun(folder, pids)) return null;
274
275
  let env = placeEnv(request.env);
275
276
  let host = pids.map((pid) => hostApp(table, pid)).find(Boolean) ?? null;
@@ -331,7 +332,8 @@ export function createSessionControl({
331
332
  return link || command ? { link, command } : null;
332
333
  };
333
334
 
334
- const liveNow = () => {
335
+ // `fresh`: for a decision made once (send into a session, open it where it runs), the process table read now.
336
+ const liveNow = ({ fresh = false } = {}) => {
335
337
  const recent = store.recentSessions(200);
336
338
  // With the title the index gives it (its first request), when the index has it yet.
337
339
  const titleOf = (session) => {
@@ -346,7 +348,7 @@ export function createSessionControl({
346
348
  };
347
349
  const linksOk = t3LinksOk();
348
350
  const handed = handedToT3();
349
- const table = processTable();
351
+ const table = processTable({ fresh });
350
352
  const found = liveSessions({
351
353
  exclude: new Set(runningPids()),
352
354
  recent,
@@ -839,7 +841,7 @@ export function createSessionControl({
839
841
  : `One of Harness's runs is working in this ${nameOf(agentId)} session right now. Open it again once the run is done.`,
840
842
  };
841
843
  }
842
- const running = sessionId ? liveNow() : [];
844
+ const running = sessionId ? liveNow({ fresh: true }) : [];
843
845
  const live = running.find(
844
846
  (row) => row.agent === agentId && row.sessionId === sessionId
845
847
  );
@@ -895,6 +897,39 @@ export function createSessionControl({
895
897
  : { opensIn: null };
896
898
  };
897
899
 
900
+ /**
901
+ * The agent itself, fresh, for a thread it has not worked in yet (owner, October 28: every agent on a thread opens
902
+ * outside the buddy, a coding agent with no session there "fresh in its home"): where the person uses it, in the
903
+ * thread's folder, never another of its conversations there. In T3 Code that is T3 itself, not the agent's thread for
904
+ * the folder, which is another conversation.
905
+ */
906
+ const freshPlan = (agentId, folder, act) => {
907
+ const home = homeOf(agentId);
908
+ const where = folder && fs.existsSync(folder) ? folder : null;
909
+ const terminal = () => {
910
+ const { name, app } = lastResort();
911
+ return {
912
+ opensIn: name,
913
+ resume: { agent: agentId, session: null, cwd: where, app },
914
+ };
915
+ };
916
+ const plan =
917
+ home?.kind === "t3"
918
+ ? home.app
919
+ ? { opensIn: "T3 Code", app: home.app }
920
+ : null
921
+ : placePlan(agentId, home, null, where, act);
922
+ const shown =
923
+ plan ??
924
+ (CODING_AGENTS.includes(agentId) ? terminal() : { opensIn: null });
925
+ return shown.opensIn
926
+ ? {
927
+ ...shown,
928
+ note: `${nameOf(agentId)} hasn't worked in this thread yet, so it opens fresh in ${shown.opensIn}.`,
929
+ }
930
+ : shown;
931
+ };
932
+
898
933
  /**
899
934
  * Each coding agent's sessions that ended, as Open offers them: the person's newest (`last`), where the agent opens when
900
935
  * it is not running; and the newest few another agent, a run or a test started (`traces`), each saying who.
@@ -1027,14 +1062,12 @@ export function createSessionControl({
1027
1062
  if (!validAgent(agentId) || (sessionId && !SESSION_ID.test(sessionId)))
1028
1063
  return { ok: false, error: "that session can't be opened" };
1029
1064
  // Open itself: the daemon's part happens now, and the app does the rest.
1065
+ const act = request.command === "board.live.focus";
1030
1066
  return {
1031
1067
  ok: true,
1032
- ...openPlan(
1033
- agentId,
1034
- sessionId || null,
1035
- folderOf(request),
1036
- request.command === "board.live.focus"
1037
- ),
1068
+ ...(request.fresh === true && !sessionId
1069
+ ? freshPlan(agentId, folderOf(request), act)
1070
+ : openPlan(agentId, sessionId || null, folderOf(request), act)),
1038
1071
  };
1039
1072
  }
1040
1073
  if (request.command === "board.live.say") {
@@ -1042,7 +1075,7 @@ export function createSessionControl({
1042
1075
  .trim()
1043
1076
  .slice(0, 20_000);
1044
1077
  if (!text) return { ok: false, error: "a message needs text" };
1045
- const session = liveNow().find(
1078
+ const session = liveNow({ fresh: true }).find(
1046
1079
  (row) => row.agent === request.to && row.sessionId === sessionId
1047
1080
  );
1048
1081
  if (!session)
@@ -3,7 +3,7 @@ import path from "node:path";
3
3
  import { spawn } from "node:child_process";
4
4
  import { randomBytes } from "node:crypto";
5
5
  import { runAsync } from "./commands.mjs";
6
- import { listJsonl } from "./files.mjs";
6
+ import { listJsonlAsync } from "./files.mjs";
7
7
  import { parseSessionFile } from "./parsers.mjs";
8
8
  import { historyRoots } from "./paths.mjs";
9
9
 
@@ -97,6 +97,9 @@ export function createSessionIndex({
97
97
 
98
98
  let syncRunning = false;
99
99
  let syncAgain = false;
100
+ // Each session file's mtime and size when it was last found indexed: a pass skips it without asking the store until
101
+ // it changes (a pass runs about every second while agents work, over hundreds of files).
102
+ const indexedAs = new Map();
100
103
  let targetedOpencode = [];
101
104
  async function indexFile(agent, item) {
102
105
  const sessionId = path.basename(item.file);
@@ -172,12 +175,17 @@ export function createSessionIndex({
172
175
  (agentFilter && agentFilter !== agent)
173
176
  )
174
177
  continue;
175
- const files = historyRoots(agent)
176
- .flatMap(listJsonl)
178
+ const files = (
179
+ await Promise.all(historyRoots(agent).map(listJsonlAsync))
180
+ )
181
+ .flat()
177
182
  .sort((a, b) => b.mtime - a.mtime);
178
183
  for (const item of files) {
184
+ const seenAs = `${item.mtime}:${item.size}`;
185
+ if (indexedAs.get(item.file) === seenAs) continue;
179
186
  try {
180
187
  await indexFile(agent, item);
188
+ indexedAs.set(item.file, seenAs);
181
189
  } catch (error) {
182
190
  log(`index ${agent} ${item.file}: ${error.message}`);
183
191
  }
package/lib/store.mjs CHANGED
@@ -7,6 +7,9 @@ import { BOOK_TABLES } from "./book.mjs";
7
7
 
8
8
  const SCHEMA_VERSION = 11;
9
9
  const INDEX_FORMAT_VERSION = 2;
10
+ // How long agents' board-call times wait in memory before they are written together (touchAgent).
11
+ const SEEN_WRITE_MS = 5_000;
12
+ const PHONE_SEEN_WRITE_MS = 30_000;
10
13
  const { Database } = sqliteWasm;
11
14
 
12
15
  function bindings(values) {
@@ -48,6 +51,23 @@ const MEMORY_SELECT = `select c.id, c.task_id, c.seq, c.kind, c.author, c.text,
48
51
  const MEMORY_JOINS = `join board_tasks t on t.id = c.task_id left join board_tasks p on p.id = t.parent_id
49
52
  left join board_memory_marks m on m.contribution_id = c.id`;
50
53
  const MEMORY_WHERE = `c.kind in (${MEMORY_KINDS.map((kind) => `'${kind}'`).join(", ")}) and m.contribution_id is null`;
54
+ // Limits a memory query to these threads (goal ids): a post in one, or in one of its jobs. None given: every thread.
55
+ // The ids are bound as @thread0, @thread1, ...
56
+ const inThreads = (threads) => {
57
+ if (!threads) return { sql: "", params: {} };
58
+ const params = Object.fromEntries(
59
+ [...threads].map((id, i) => [`thread${i}`, String(id)])
60
+ );
61
+ const list = Object.keys(params)
62
+ .map((name) => `@${name}`)
63
+ .join(", ");
64
+ return {
65
+ sql: list
66
+ ? ` and (c.task_id in (${list}) or t.parent_id in (${list}))`
67
+ : " and 0",
68
+ params,
69
+ };
70
+ };
51
71
  const PURGED = "[deleted at the person's request]";
52
72
  // A full-text query for any of a text's words longer than two letters, or null with none.
53
73
  const anyWordOf = (text) => {
@@ -78,8 +98,10 @@ export function openStore(file) {
78
98
  const db = new Database(file);
79
99
  // The daemon is the database's single writer. DELETE mode is portable across the
80
100
  // WASM VFS on every supported Node LTS line; no native addon or ABI is involved.
101
+ // Every page the cache misses is a synchronous read on the daemon's one thread; 32 MB (SQLite's default is 2) halved
102
+ // the time of the buddy's and agents' usual reads on a copy of the owner's 256 MB database (October 28).
81
103
  db.exec(
82
- "pragma journal_mode = DELETE; pragma synchronous = NORMAL; pragma busy_timeout = 5000;"
104
+ "pragma journal_mode = DELETE; pragma synchronous = NORMAL; pragma busy_timeout = 5000; pragma cache_size = -32768;"
83
105
  );
84
106
  db.exec(`
85
107
  create table if not exists schema_info (version integer not null);
@@ -644,6 +666,46 @@ export function openStore(file) {
644
666
 
645
667
  const roomPostListeners = [];
646
668
 
669
+ // When each agent last called the board, by computer. The buddy, every agent and the other computers call several
670
+ // times a second, and each write waits for the disk (4 s in every 50 on the owner's daemon, October 28), so the times
671
+ // are kept here, answered from here, and written together within SEEN_WRITE_MS. A crash loses that much of them: an
672
+ // agent may hear again what was new in its last few seconds.
673
+ const seenNow = new Map();
674
+ let seenWrite = null;
675
+ const seenKey = (agent, machine) => `${agent}\0${machine ?? ""}`;
676
+ const writeSeen = () => {
677
+ clearTimeout(seenWrite);
678
+ seenWrite = null;
679
+ if (!seenNow.size) return;
680
+ const rows = [...seenNow.values()];
681
+ db.exec("begin");
682
+ try {
683
+ for (const { agent, machine, at } of rows)
684
+ if (machine)
685
+ execute(
686
+ "run",
687
+ "insert into board_agent_computers(agent, machine, last_seen_at) values(?, ?, ?) on conflict(agent, machine) do update set last_seen_at = excluded.last_seen_at",
688
+ agent,
689
+ machine,
690
+ at
691
+ );
692
+ else
693
+ execute(
694
+ "run",
695
+ "insert into board_agents(agent, last_seen_at) values(?, ?) on conflict(agent) do update set last_seen_at = excluded.last_seen_at",
696
+ agent,
697
+ at
698
+ );
699
+ db.exec("commit");
700
+ seenNow.clear();
701
+ } catch (error) {
702
+ db.exec("rollback");
703
+ throw error;
704
+ }
705
+ };
706
+ // A device's last call, written at most this often: it is shown as "last seen", never decided on.
707
+ const phoneSeenAt = new Map();
708
+
647
709
  return {
648
710
  db,
649
711
  needsIndex(agent, sessionId, mtime, size) {
@@ -1057,7 +1119,7 @@ export function openStore(file) {
1057
1119
  const written = execute(
1058
1120
  "run",
1059
1121
  `update board_tasks set run_authorized_by = @agent, run_authorized_at = @now, updated_at = @now, version = version + 1
1060
- where id = @id and status = 'open' and run_workspace is not null and run_authorized_at is null`,
1122
+ where id = @id and status = 'open' and (run_workspace is not null or (kind = 'wake' and target_machine is not null)) and run_authorized_at is null`,
1061
1123
  { id, agent, now }
1062
1124
  );
1063
1125
  if (written.changes === 1) return { ok: true, task: this.getTask(id) };
@@ -1506,6 +1568,26 @@ export function openStore(file) {
1506
1568
  earlierBefore: rows.length > count ? (posts[0]?.seq ?? null) : null,
1507
1569
  };
1508
1570
  },
1571
+ // The person's own posts in a room before a post number, the newest `limit` of them, oldest first, and whether
1572
+ // there are more: what a brief carries of the person's say further back than the newest posts. None they forgot or
1573
+ // replaced (board_memory_marks).
1574
+ personRoomPosts(goalId, { before, limit = 12 }) {
1575
+ const count = Math.max(1, Math.min(100, Number(limit) || 12));
1576
+ const rows = execute(
1577
+ "all",
1578
+ `select c.id, c.seq, c.kind, c.author, c.machine, c.text, c.mentions, c.source, c.created_at
1579
+ from board_contributions c left join board_memory_marks m on m.contribution_id = c.id
1580
+ where c.task_id = ? and c.seq < ? and c.author = 'user' and c.kind != 'held' and m.contribution_id is null
1581
+ order by c.seq desc limit ?`,
1582
+ goalId,
1583
+ Number(before) > 0 ? Number(before) : Number.MAX_SAFE_INTEGER,
1584
+ count + 1
1585
+ );
1586
+ return {
1587
+ posts: rows.slice(0, count).reverse().map(roomPostOf),
1588
+ more: rows.length > count,
1589
+ };
1590
+ },
1509
1591
  // What each agent's finished runs' receipts showed (receipts.mjs, kept as receipt:<task>): { agent: { tested, untested, failing } }.
1510
1592
  receiptCounts() {
1511
1593
  const counts = {};
@@ -1861,7 +1943,7 @@ export function openStore(file) {
1861
1943
  // was said or done; posts the guard held never come back; nor does anything the person had forgotten or replaced.
1862
1944
  // The first twelve words of a query, as searchThreads read them (measured with 24: long job results that
1863
1945
  // share many common words crowded out the person's own earlier posts).
1864
- memoryWords(query, limit = 60) {
1946
+ memoryWords(query, limit = 60, { threads = null } = {}) {
1865
1947
  const terms = [
1866
1948
  ...new Set(
1867
1949
  String(query)
@@ -1875,13 +1957,26 @@ export function openStore(file) {
1875
1957
  const match = terms
1876
1958
  .map((term) => `"${term.replaceAll('"', '""')}"`)
1877
1959
  .join(" OR ");
1960
+ const scope = inThreads(threads);
1878
1961
  return execute(
1879
1962
  "all",
1880
1963
  `${MEMORY_SELECT}, bm25(board_contributions_fts) as rank
1881
1964
  from board_contributions_fts f join board_contributions c on c.id = f.id ${MEMORY_JOINS}
1882
- where board_contributions_fts match @match and ${MEMORY_WHERE}
1965
+ where board_contributions_fts match @match and ${MEMORY_WHERE}${scope.sql}
1883
1966
  order by rank limit @limit`,
1884
- { match, limit: Math.max(1, Math.min(200, limit)) }
1967
+ { match, limit: Math.max(1, Math.min(200, limit)), ...scope.params }
1968
+ );
1969
+ },
1970
+ // The ids of what memory can show in these threads (their posts and their jobs'), so a search limited to them ranks
1971
+ // among them alone instead of filtering the whole swarm's best few.
1972
+ memoryIdsIn(threads) {
1973
+ const scope = inThreads(threads);
1974
+ return new Set(
1975
+ execute(
1976
+ "all",
1977
+ `select c.id from board_contributions c ${MEMORY_JOINS} where ${MEMORY_WHERE}${scope.sql}`,
1978
+ scope.params
1979
+ ).map((row) => row.id)
1885
1980
  );
1886
1981
  },
1887
1982
  memoryPosts(ids) {
@@ -2453,7 +2548,7 @@ export function openStore(file) {
2453
2548
  threadRuns(goalId) {
2454
2549
  return execute(
2455
2550
  "all",
2456
- `select id, kind, objective, origin_agent, target_agent, claimed_by, claimed_at, claimed_machine, status, holder, result, failure, session_id, run_workspace, run_authorized_at, usage, run_model, specialty, step, checked, returned, created_at, updated_at, reported_at
2551
+ `select id, kind, objective, origin_agent, target_agent, target_machine, claimed_by, claimed_at, claimed_machine, status, holder, result, failure, session_id, run_workspace, run_authorized_at, usage, run_model, specialty, step, checked, returned, created_at, updated_at, reported_at
2457
2552
  from board_tasks where parent_id = ? and kind in ('wake', 'task') order by created_at asc`,
2458
2553
  goalId
2459
2554
  ).map((row) => ({
@@ -3445,8 +3540,10 @@ export function openStore(file) {
3445
3540
  // what is new since they looked, not since the other did. An agent not yet seen there counts from when it last looked
3446
3541
  // anywhere, as it did before the board knew computers, so nothing old is told again.
3447
3542
  lastSeenAt(agent, machine = null) {
3543
+ const kept = machine ? seenNow.get(seenKey(agent, machine))?.at : null;
3448
3544
  const there = machine
3449
- ? execute(
3545
+ ? kept ||
3546
+ execute(
3450
3547
  "get",
3451
3548
  "select last_seen_at from board_agent_computers where agent = ? and machine = ?",
3452
3549
  agent,
@@ -3580,6 +3677,9 @@ export function openStore(file) {
3580
3677
  );
3581
3678
  },
3582
3679
  touchPhone(id) {
3680
+ const now = Date.now();
3681
+ if (now - (phoneSeenAt.get(id) ?? 0) < PHONE_SEEN_WRITE_MS) return;
3682
+ phoneSeenAt.set(id, now);
3583
3683
  execute(
3584
3684
  "run",
3585
3685
  "update phone_devices set last_seen_at = ? where id = ?",
@@ -3602,40 +3702,49 @@ export function openStore(file) {
3602
3702
  // When an agent last called the board, from `machine` when the call says which computer (every daemon's does): one
3603
3703
  // row a call, the same writes as before computers. A row without a computer is from before.
3604
3704
  touchAgent(agent, machine = null) {
3605
- const now = new Date().toISOString();
3606
- if (machine)
3607
- execute(
3608
- "run",
3609
- "insert into board_agent_computers(agent, machine, last_seen_at) values(?, ?, ?) on conflict(agent, machine) do update set last_seen_at = excluded.last_seen_at",
3610
- agent,
3611
- machine,
3612
- now
3613
- );
3614
- else
3615
- execute(
3616
- "run",
3617
- "insert into board_agents(agent, last_seen_at) values(?, ?) on conflict(agent) do update set last_seen_at = excluded.last_seen_at",
3618
- agent,
3619
- now
3620
- );
3705
+ seenNow.set(seenKey(agent, machine), {
3706
+ agent,
3707
+ machine,
3708
+ at: new Date().toISOString(),
3709
+ });
3710
+ seenWrite ??= setTimeout(writeSeen, SEEN_WRITE_MS);
3711
+ seenWrite.unref?.();
3621
3712
  },
3622
3713
  // Which agents called the board from which computer since `since`: where an agent that runs itself (Hermes, OpenClaw)
3623
3714
  // lives, since the daemon never starts it.
3624
3715
  agentsSeenOnComputers(since) {
3625
- return execute(
3716
+ const rows = execute(
3626
3717
  "all",
3627
3718
  "select agent, machine from board_agent_computers where last_seen_at >= ? order by agent",
3628
3719
  since
3629
3720
  );
3721
+ const listed = new Set(
3722
+ rows.map((row) => seenKey(row.agent, row.machine))
3723
+ );
3724
+ for (const kept of seenNow.values())
3725
+ if (
3726
+ kept.machine &&
3727
+ kept.at >= since &&
3728
+ !listed.has(seenKey(kept.agent, kept.machine))
3729
+ )
3730
+ rows.push({ agent: kept.agent, machine: kept.machine });
3731
+ return rows.sort((a, b) => a.agent.localeCompare(b.agent));
3630
3732
  },
3631
3733
  // When each agent last called the board, from any computer.
3632
3734
  agentsSeen() {
3633
- return execute(
3634
- "all",
3635
- `select agent, max(last_seen_at) as last_seen_at from (
3636
- select agent, last_seen_at from board_agents union all select agent, last_seen_at from board_agent_computers
3637
- ) group by agent order by last_seen_at desc`
3735
+ const latest = new Map(
3736
+ execute(
3737
+ "all",
3738
+ `select agent, max(last_seen_at) as last_seen_at from (
3739
+ select agent, last_seen_at from board_agents union all select agent, last_seen_at from board_agent_computers
3740
+ ) group by agent`
3741
+ ).map((row) => [row.agent, row.last_seen_at])
3638
3742
  );
3743
+ for (const { agent, at } of seenNow.values())
3744
+ if (!latest.has(agent) || at > latest.get(agent)) latest.set(agent, at);
3745
+ return [...latest]
3746
+ .map(([agent, last_seen_at]) => ({ agent, last_seen_at }))
3747
+ .sort((a, b) => b.last_seen_at.localeCompare(a.last_seen_at));
3639
3748
  },
3640
3749
  // Wakes sent to an agent's own gateway (wakes.mjs), one row per attempt: `outcome` is sent, refused, unsure (it may
3641
3750
  // have landed), merged into one already out, or capped. `about` names the result a wake told the agent of.
@@ -4015,6 +4124,7 @@ export function openStore(file) {
4015
4124
  );
4016
4125
  },
4017
4126
  close() {
4127
+ writeSeen();
4018
4128
  for (const statement of statements) {
4019
4129
  try {
4020
4130
  statement.finalize();