tryharness 0.7.67 → 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.
@@ -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) {
@@ -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) {
@@ -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();
package/lib/swarm.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
- import { spawnSync } from "node:child_process";
4
+ import { kept } from "./kept.mjs";
5
5
  import { readRunSteps } from "./runner.mjs";
6
6
  import { createSubagents } from "./subagents.mjs";
7
7
  import { MACHINE_RUNNERS, member, nameOf, resolveMember } from "./members.mjs";
@@ -99,37 +99,58 @@ export function nativeSessionId(agent, sessionId) {
99
99
  : bare;
100
100
  }
101
101
 
102
- // The folder each running coding agent works in, by agent, leaving out the runs the daemon started itself.
103
- // Null when the machine cannot say, and every session then counts as open in a terminal.
104
- export function agentProcessCwds(exclude = new Set()) {
105
- if (process.platform === "win32") return null;
106
- const ps = spawnSync("ps", ["-axo", "pid=,comm="], { encoding: "utf8" });
107
- if (ps.status !== 0) return null;
102
+ // Each running coding agent's process and the folder it works in, as kept.mjs steps; null when the machine cannot say.
103
+ function* agentProcessSteps() {
104
+ const ps = yield ["ps", ["-axo", "pid=,comm="]];
105
+ if (ps === null) return null;
108
106
  const byPid = new Map();
109
- for (const line of ps.stdout.split("\n")) {
107
+ for (const line of ps.split("\n")) {
110
108
  const match = line.trim().match(/^(\d+)\s+(.+)$/);
111
- if (!match || exclude.has(Number(match[1]))) continue;
109
+ if (!match) continue;
112
110
  const agent = Object.keys(PROCESS_NAMES).find(
113
111
  (name) => path.basename(match[2]) === PROCESS_NAMES[name]
114
112
  );
115
113
  if (agent) byPid.set(match[1], agent);
116
114
  }
117
- const cwds = new Map();
118
- if (!byPid.size) return cwds;
119
- const lsof = spawnSync(
115
+ if (!byPid.size) return [];
116
+ const lsof = yield [
120
117
  "lsof",
121
118
  ["-a", "-d", "cwd", "-Fn", "-p", [...byPid.keys()].join(",")],
122
- { encoding: "utf8" }
123
- );
124
- if (lsof.status !== 0 && !lsof.stdout) return null;
119
+ // A process gone since ps makes lsof fail, after it lists the others.
120
+ { anyStatus: true },
121
+ ];
122
+ if (lsof === null) return null;
123
+ const found = [];
125
124
  let pid = null;
126
- for (const line of lsof.stdout.split("\n")) {
125
+ for (const line of lsof.split("\n")) {
127
126
  if (line.startsWith("p")) pid = line.slice(1);
128
- else if (line.startsWith("n") && byPid.has(pid)) {
129
- const agent = byPid.get(pid);
130
- if (!cwds.has(agent)) cwds.set(agent, new Set());
131
- cwds.get(agent).add(line.slice(1));
132
- }
127
+ else if (line.startsWith("n") && byPid.has(pid))
128
+ found.push({
129
+ pid: Number(pid),
130
+ agent: byPid.get(pid),
131
+ cwd: line.slice(1),
132
+ });
133
+ }
134
+ return found;
135
+ }
136
+ // The buddy's thread list asks on every read; ps and lsof run beside the daemon's thread once this is a few seconds old.
137
+ const agentProcessesKept = kept(agentProcessSteps, {
138
+ freshMs: 3_000,
139
+ staleMs: 30_000,
140
+ keyOf: () => "all",
141
+ });
142
+
143
+ // The folder each running coding agent works in, by agent, leaving out the runs the daemon started itself.
144
+ // Null when the machine cannot say, and every session then counts as open in a terminal.
145
+ export function agentProcessCwds(exclude = new Set()) {
146
+ if (process.platform === "win32") return null;
147
+ const found = agentProcessesKept();
148
+ if (!found) return null;
149
+ const cwds = new Map();
150
+ for (const { pid, agent, cwd } of found) {
151
+ if (exclude.has(pid)) continue;
152
+ if (!cwds.has(agent)) cwds.set(agent, new Set());
153
+ cwds.get(agent).add(cwd);
133
154
  }
134
155
  return cwds;
135
156
  }
@@ -953,13 +974,16 @@ export function createSwarm({
953
974
  }
954
975
  // The swarm's shared memory, for any of the person's agents: what any agent wrote in any thread, and the coding
955
976
  // sessions on this machine. A caller limited to its own threads (a hosted agent while the machine is sealed) gets only those.
977
+ // With `thread`, that thread alone (goalCommands.mjs board.room.search), and no sessions.
956
978
  if (command === "board.swarm.search") {
957
979
  const query = String(request.query || "")
958
980
  .trim()
959
981
  .slice(0, 500);
960
982
  if (!query) return { ok: false, error: "search needs a query" };
983
+ const thread = request.thread ? String(request.thread) : null;
961
984
  const hosted = Boolean(request.teamOnly) || request.hostedCaller === true;
962
- const sessions = hosted ? [] : store.search({ query, limit: 5 });
985
+ const sessions =
986
+ hosted || thread ? [] : store.search({ query, limit: 5 });
963
987
  if (!memory)
964
988
  return { ok: true, threads: [], sessions, lessons: [], weak: true };
965
989
  return {
@@ -967,6 +991,7 @@ export function createSwarm({
967
991
  .recall(query, {
968
992
  agent,
969
993
  teamOnly: Boolean(request.teamOnly),
994
+ thread,
970
995
  hosted,
971
996
  everyFolder: true,
972
997
  })
@@ -1441,27 +1466,53 @@ export function createSwarm({
1441
1466
  );
1442
1467
  worktrees.inPlace(thread.id);
1443
1468
  }
1444
- const opened = store.getTask(thread.id);
1445
1469
  const first = store.roomPost({
1446
1470
  goalId: thread.id,
1447
1471
  author: "user",
1448
1472
  kind: "post",
1449
1473
  text,
1450
- mentions: addressed(opened, text),
1474
+ mentions: addressed(store.getTask(thread.id), text),
1451
1475
  });
1452
- wakeFor(thread.id, first, "user");
1476
+ return { id: thread.id, first };
1477
+ };
1478
+ const hear = ({ id, first }) => {
1479
+ const opened = store.getTask(id);
1480
+ wakeFor(id, first, "user");
1453
1481
  if (!to) heardBy(opened, first, text);
1454
1482
  heardPerson(opened, first);
1455
- notifyBoard(store.getTask(thread.id));
1456
- return { ok: true, thread: summary(store.getTask(thread.id)) };
1457
1483
  };
1458
- return folder || !needsFolder([...team])
1459
- ? begin(folder)
1460
- : {
1461
- pending: folders
1462
- .suggest(text, [...team])
1463
- .then((suggested) => begin(suggested.folder)),
1464
- };
1484
+ const started = ({ id }) => {
1485
+ notifyBoard(store.getTask(id));
1486
+ return { ok: true, thread: summary(store.getTask(id)) };
1487
+ };
1488
+ if (folder || !needsFolder([...team])) {
1489
+ const thread = begin(folder);
1490
+ hear(thread);
1491
+ return started(thread);
1492
+ }
1493
+ // Harness's folder pick is a model call, and Harness's server waits 10 s for this computer's answer to a window's
1494
+ // call: a start held for a slow pick reached the window as "your computer is not connected" while the thread went
1495
+ // on to start, and the person sent it again. A pick already made (the composer's preview of this message) is
1496
+ // settled before the next turn of the event loop; any other starts the thread now, its agents woken once the
1497
+ // pick lands. A folder the person chose meanwhile stands.
1498
+ const suggestion = folders.suggest(text, [...team]);
1499
+ const notYet = new Promise((resolve) => setImmediate(resolve, null));
1500
+ return {
1501
+ pending: Promise.race([suggestion, notYet]).then((suggested) => {
1502
+ const thread = begin(suggested?.folder ?? null);
1503
+ if (suggested) hear(thread);
1504
+ else
1505
+ void suggestion.then(({ folder: picked }) => {
1506
+ const now = store.getTask(thread.id);
1507
+ if (now?.status !== "open") return;
1508
+ if (picked && !now.goal.workspace)
1509
+ store.setGoalFolder(thread.id, path.resolve(picked));
1510
+ hear(thread);
1511
+ notifyBoard(store.getTask(thread.id));
1512
+ });
1513
+ return started(thread);
1514
+ }),
1515
+ };
1465
1516
  }
1466
1517
  // What the swarm learned from the person (lessons.mjs), the person's to read: only the person gets this far.
1467
1518
  if (command === "board.swarm.lessons")
@@ -111,6 +111,13 @@ export function createSwarmMemory({
111
111
  .map((thread) => thread.id)
112
112
  );
113
113
 
114
+ // The threads a search reads, or null for every one: the asker's own (`teamOnly`), one thread, or both.
115
+ const scopeOf = ({ agent, teamOnly, thread }) => {
116
+ const own = teamOnly ? threadsOf(agent) : null;
117
+ if (!thread) return own;
118
+ return !own || own.has(thread) ? new Set([thread]) : new Set();
119
+ };
120
+
114
121
  const holdsIn = (item, folder) =>
115
122
  !item.folder ||
116
123
  (folder && path.resolve(item.folder) === path.resolve(folder));
@@ -174,14 +181,16 @@ export function createSwarmMemory({
174
181
 
175
182
  /**
176
183
  * What the memory holds for a query, best first: { posts, observations, lessons, weak }. `teamOnly` limits posts to
177
- * the threads `agent` is on; `hosted` also leaves out the person's lessons and observations; `exclude(post)` drops
178
- * posts the asker already has (the room it is in).
184
+ * the threads `agent` is on; `thread` to one thread, with its jobs, and the observations and lessons that stand on
185
+ * it; `hosted` also leaves out the person's lessons and observations; `exclude(post)` drops posts the asker already
186
+ * has (the room it is in).
179
187
  */
180
188
  const recall = async (
181
189
  query,
182
190
  {
183
191
  agent = null,
184
192
  teamOnly = false,
193
+ thread = null,
185
194
  hosted = false,
186
195
  folder = null,
187
196
  // An agent's own search (swarm_memory) reads every folder, as its posts do; a brief keeps to the work's folder.
@@ -196,10 +205,11 @@ export function createSwarmMemory({
196
205
  .trim()
197
206
  .slice(0, QUERY_CHARS);
198
207
  if (!text) return { posts: [], observations: [], lessons: [], weak: true };
199
- const threads = teamOnly ? threadsOf(agent) : null;
200
- const allowed = (row) =>
201
- (!threads || (row.thread && threads.has(row.thread))) && !exclude(row);
202
- const words = store.memoryWords(text, POOL).filter(allowed);
208
+ const threads = scopeOf({ agent, teamOnly, thread });
209
+ // Both searches rank within the threads in scope, so a thread's posts are not crowded out by the whole swarm's.
210
+ const inScope = threads ? store.memoryIdsIn(threads) : null;
211
+ const allowed = (row) => !exclude(row);
212
+ const words = store.memoryWords(text, POOL, { threads }).filter(allowed);
203
213
  let meaning = [];
204
214
  let best = null;
205
215
  const [vector] = (await embed([text], "query")) ?? [];
@@ -222,7 +232,9 @@ export function createSwarmMemory({
222
232
  }
223
233
  const hits = topK(
224
234
  vector,
225
- [...cache.rows].map(([id, row]) => ({ id, vector: row })),
235
+ [...cache.rows]
236
+ .filter(([id]) => !inScope || inScope.has(id))
237
+ .map(([id, row]) => ({ id, vector: row })),
226
238
  POOL * 2
227
239
  );
228
240
  // memoryPosts drops anything marked since it was cached, and keeps the hits' order.
@@ -273,7 +285,15 @@ export function createSwarmMemory({
273
285
  }
274
286
  ordered.push(row);
275
287
  }
276
- const weak = best === null ? words.length === 0 : best < WEAK_BELOW;
288
+ // The query's own words found as they are (an error code, a commit, a file name) are a match whatever their
289
+ // meaning scores.
290
+ const verbatim = words.some((row) =>
291
+ String(row.text).toLowerCase().includes(text.toLowerCase())
292
+ );
293
+ const weak =
294
+ !verbatim && (best === null ? words.length === 0 : best < WEAK_BELOW);
295
+ const standsOnThread = (item) =>
296
+ !thread || item.evidence.some((entry) => entry.goalId === thread);
277
297
  return {
278
298
  posts: ordered.slice(0, limit).map(viewOf),
279
299
  observations: hosted
@@ -287,7 +307,7 @@ export function createSwarmMemory({
287
307
  });
288
308
  const shown = new Set(closest.map((observation) => observation.id));
289
309
  return [
290
- ...closest,
310
+ ...closest.filter(standsOnThread),
291
311
  ...(followThreads
292
312
  ? threadObservations(found, {
293
313
  folder,
@@ -300,13 +320,16 @@ export function createSwarmMemory({
300
320
  })(),
301
321
  lessons: hosted
302
322
  ? []
303
- : store.lessonsMatching(text, 5).map((lesson) => ({
304
- about: lesson.about,
305
- agent: lesson.agent,
306
- text: lesson.text,
307
- quote: lesson.evidence.at(-1)?.quote ?? null,
308
- thread: lesson.evidence.at(-1)?.goalId ?? null,
309
- })),
323
+ : store
324
+ .lessonsMatching(text, 5)
325
+ .filter(standsOnThread)
326
+ .map((lesson) => ({
327
+ about: lesson.about,
328
+ agent: lesson.agent,
329
+ text: lesson.text,
330
+ quote: lesson.evidence.at(-1)?.quote ?? null,
331
+ thread: lesson.evidence.at(-1)?.goalId ?? null,
332
+ })),
310
333
  weak,
311
334
  // The closest post's similarity, when meaning was used: what `weak` was judged on.
312
335
  nearest: best,
package/lib/team.mjs CHANGED
@@ -209,7 +209,7 @@ export function createTeam({ store, log = () => {} }) {
209
209
  const stepsOfNow = (current) => pieceOf(current)?.steps ?? [];
210
210
 
211
211
  // Where each step began and who held it, so a run is marked with the step it worked.
212
- const started = (current, step) => {
212
+ const started = (current, step, at = now()) => {
213
213
  const entry = stepsOfNow(current).find((s) => s.id === step);
214
214
  return entry
215
215
  ? [
@@ -219,7 +219,7 @@ export function createTeam({ store, log = () => {} }) {
219
219
  step,
220
220
  specialty: entry.specialty,
221
221
  agents: entry.agents,
222
- at: now(),
222
+ at,
223
223
  },
224
224
  ].slice(-40)
225
225
  : (current.history ?? []);
@@ -272,13 +272,15 @@ export function createTeam({ store, log = () => {} }) {
272
272
 
273
273
  /**
274
274
  * Keep the route the round proposed, starting at its first step, and say in the thread who holds what. `named`: the
275
- * agents the person tagged in the message; `pins`: specialty -> the agent the person pinned for it. Returns the plan
276
- * kept, or null when the proposal named nothing usable.
275
+ * agents the person tagged in the message; `pins`: specialty -> the agent the person pinned for it; `since`: when the
276
+ * person sent the message the route answers. Its first step began then, not when the round finished: an agent the
277
+ * person tagged starts at once and may report before the route is kept, and that run is the step's work. Returns the
278
+ * plan kept, or null when the proposal named nothing usable.
277
279
  */
278
280
  const set = (
279
281
  goalId,
280
282
  proposed,
281
- { members, named = [], pins = {}, by, post }
283
+ { members, named = [], pins = {}, by, post, since = null }
282
284
  ) => {
283
285
  const checked = validPlan(proposed, { members, named, pins });
284
286
  if (!checked) {
@@ -297,10 +299,11 @@ export function createTeam({ store, log = () => {} }) {
297
299
  by,
298
300
  at: now(),
299
301
  };
302
+ const begun = since ?? now();
300
303
  const kept = {
301
304
  ...fresh,
302
- stepAt: now(),
303
- history: started(fresh, steps[0].id),
305
+ stepAt: begun,
306
+ history: started(fresh, steps[0].id, begun),
304
307
  };
305
308
  write(goalId, kept);
306
309
  post?.(
@@ -473,21 +476,23 @@ export function createTeam({ store, log = () => {} }) {
473
476
  };
474
477
 
475
478
  /**
476
- * Agents were just given work outside the route (the person wrote to them, or a free round picked one): the thread
477
- * moves to the earliest step of the piece one of them holds, so the route goes on from there.
479
+ * Agents were given work outside the route (the person wrote to them, or a free round picked one) at `at`: the
480
+ * thread moves to the earliest step of the piece one of them holds, so the route goes on from there. The step they
481
+ * are already on begins again only for work given after it began: the agents the person tagged in the message a new
482
+ * route answers had it when the route began, and may have reported since.
478
483
  */
479
- const acted = (goalId, agents) => {
484
+ const acted = (goalId, agents, at = now()) => {
480
485
  const current = read(goalId);
481
486
  if (!current || !agents.length) return;
482
487
  const step = stepsOfNow(current).find((s) =>
483
488
  s.agents.some((holder) => agents.includes(holder))
484
489
  );
485
490
  if (step && step.id !== current.step) moveTo(goalId, step.id);
486
- else if (step)
491
+ else if (step && at > (current.stepAt ?? ""))
487
492
  write(goalId, {
488
493
  ...current,
489
- stepAt: now(),
490
- history: started(current, step.id),
494
+ stepAt: at,
495
+ history: started(current, step.id, at),
491
496
  });
492
497
  };
493
498