tryharness 0.7.72 → 0.7.75

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.
Files changed (51) hide show
  1. package/assets/opencode-plugin.js +70 -3
  2. package/assets/pi-extension.ts +7 -1
  3. package/cli.mjs +2 -2
  4. package/lib/asks.mjs +2 -0
  5. package/lib/boardCommands.mjs +9 -1
  6. package/lib/boardProtocol.json +23 -19
  7. package/lib/bridge.mjs +2 -0
  8. package/lib/capabilityCheck.mjs +86 -0
  9. package/lib/computerAddress.mjs +6 -14
  10. package/lib/coordinator.mjs +220 -35
  11. package/lib/daemon.mjs +160 -126
  12. package/lib/dispatchContract.json +1 -31
  13. package/lib/dispatchTools.js +8 -39
  14. package/lib/dispatches.mjs +27 -285
  15. package/lib/goalCommands.mjs +7 -3
  16. package/lib/herdr.mjs +159 -0
  17. package/lib/hookDialects.mjs +36 -2
  18. package/lib/jobAnswers.mjs +3 -0
  19. package/lib/kanban.mjs +256 -0
  20. package/lib/library.mjs +83 -7
  21. package/lib/localAgentAsk.mjs +20 -29
  22. package/lib/localStack.mjs +19 -3
  23. package/lib/machineRoutes.mjs +1 -5
  24. package/lib/manager.mjs +254 -0
  25. package/lib/mcp-stdio.mjs +72 -46
  26. package/lib/moves.mjs +365 -0
  27. package/lib/nudge.mjs +18 -3
  28. package/lib/ownMachines.mjs +43 -6
  29. package/lib/places.mjs +29 -2
  30. package/lib/runAuthority.mjs +2 -14
  31. package/lib/runBrief.mjs +14 -1
  32. package/lib/runWorker.mjs +4 -10
  33. package/lib/runner.mjs +1 -49
  34. package/lib/sessionControl.mjs +65 -1
  35. package/lib/steerHook.mjs +124 -0
  36. package/lib/steerQueue.mjs +176 -0
  37. package/lib/store.mjs +20 -29
  38. package/lib/subagentSteering.mjs +363 -0
  39. package/lib/subagents.mjs +138 -29
  40. package/lib/swarm.mjs +16 -6
  41. package/lib/taskCommands.mjs +0 -62
  42. package/lib/threadActions.mjs +4 -2
  43. package/lib/threadBriefs.mjs +9 -3
  44. package/lib/threadRuns.mjs +13 -0
  45. package/npm-shrinkwrap.json +14 -14
  46. package/package.json +4 -4
  47. package/release.json +48 -44
  48. package/lib/comfyExecution.mjs +0 -545
  49. package/lib/promptDecider.mjs +0 -471
  50. package/lib/promptFlow.mjs +0 -597
  51. package/lib/promptScopes.mjs +0 -670
package/lib/herdr.mjs ADDED
@@ -0,0 +1,159 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { spawnSync } from "node:child_process";
5
+ import { hostApp, processTable, tmuxPlace } from "./live.mjs";
6
+
7
+ // A session in a herdr pane (herdr.dev, a terminal workspace for coding agents), opened there. herdr's own server owns
8
+ // its panes, so the process tree of a session in one leads up to that server, not to a terminal app. Every pane carries
9
+ // its ids instead (HERDR_PANE_ID, HERDR_TAB_ID, HERDR_WORKSPACE_ID), the socket of the herdr it belongs to
10
+ // (HERDR_SOCKET_PATH) and herdr's own binary (HERDR_BIN_PATH). herdr's command line, pointed at that socket, focuses a
11
+ // pane, or opens a tab on a folder with a command running in it. Everything a herdr client shows follows the server's
12
+ // focus, so the terminal a client runs in comes forward like any other's.
13
+
14
+ const DEFAULT_SOCKET = path.join(
15
+ os.homedir(),
16
+ ".config",
17
+ "herdr",
18
+ "herdr.sock"
19
+ );
20
+ const isHerdr = (comm) => path.basename(comm || "") === "herdr";
21
+
22
+ /** The herdr to speak to a server with: the one its panes name, one running now, else a usual install. */
23
+ function herdrBinary(place, table) {
24
+ const running = [...table.values()]
25
+ .map((row) => row.comm)
26
+ .filter((comm) => path.isAbsolute(comm) && isHerdr(comm));
27
+ const usual = [
28
+ ...(process.env.PATH || "").split(path.delimiter),
29
+ "/opt/homebrew/bin",
30
+ "/usr/local/bin",
31
+ "/usr/bin",
32
+ path.join(os.homedir(), ".cargo", "bin"),
33
+ path.join(os.homedir(), ".local", "bin"),
34
+ path.join(os.homedir(), "bin"),
35
+ ]
36
+ .filter(Boolean)
37
+ .map((dir) => path.join(dir, "herdr"));
38
+ const named = place?.bin ? [place.bin] : [];
39
+ return (
40
+ [...named, ...running, ...usual].find((file) => fs.existsSync(file)) ?? null
41
+ );
42
+ }
43
+
44
+ /** The socket of the herdr a place belongs to; null when that herdr is not running (its socket is gone). */
45
+ function socketOf(place) {
46
+ const socket = place?.socket || DEFAULT_SOCKET;
47
+ return fs.existsSync(socket) ? socket : null;
48
+ }
49
+
50
+ /** One herdr command against a place's server: its JSON answer, or null when herdr is not there or refused. */
51
+ function herdr(place, args, table) {
52
+ const socket = socketOf(place);
53
+ const binary = socket ? herdrBinary(place, table) : null;
54
+ if (!binary) return null;
55
+ // Not yet measured against a real herdr: the same limit as the other terminals' controls (live.mjs run).
56
+ const ran = spawnSync(binary, args, {
57
+ encoding: "utf8",
58
+ timeout: 3_000,
59
+ env: { ...process.env, HERDR_SOCKET_PATH: socket },
60
+ stdio: ["ignore", "pipe", "pipe"],
61
+ });
62
+ if (ran.status !== 0) return null;
63
+ // Most of herdr's commands answer in JSON; one that prints nothing still did what it was asked.
64
+ if (!ran.stdout.trim()) return {};
65
+ try {
66
+ return JSON.parse(ran.stdout);
67
+ } catch {
68
+ return null;
69
+ }
70
+ }
71
+
72
+ /** Whether a process runs inside one of herdr's panes: a herdr (its server) is among its ancestors. */
73
+ function inPane(table, pid) {
74
+ for (
75
+ let current = table.get(pid)?.ppid, steps = 0;
76
+ current > 1 && steps < 64;
77
+ steps++
78
+ ) {
79
+ const row = table.get(current);
80
+ if (!row) return false;
81
+ if (isHerdr(row.comm)) return true;
82
+ current = row.ppid;
83
+ }
84
+ return false;
85
+ }
86
+
87
+ /**
88
+ * The terminal a herdr client runs in: a herdr process on a terminal device (the server has none) and not in one of
89
+ * herdr's own panes (a command an agent ran there), with the app around it, or tmux's client for a herdr in tmux; the
90
+ * newest when there are several. Null when no client is attached.
91
+ */
92
+ export function herdrClient(table = processTable()) {
93
+ const [pid, row] =
94
+ [...table]
95
+ .filter(
96
+ ([id, entry]) => isHerdr(entry.comm) && entry.tty && !inPane(table, id)
97
+ )
98
+ .sort((a, b) => b[0] - a[0])[0] ?? [];
99
+ if (!row) return null;
100
+ return {
101
+ pid,
102
+ tty: row.tty,
103
+ host:
104
+ hostApp(table, pid) ?? tmuxPlace(table, row.tty)?.client?.host ?? null,
105
+ };
106
+ }
107
+
108
+ /**
109
+ * Brings up the pane a running session is in: by the agent herdr sees there, else its tab. The terminal herdr is attached
110
+ * in ({ pid, tty, host }, to come forward as any terminal does), `{ attach: true }` when herdr runs with no client
111
+ * attached, or null when that herdr does not answer. The process table is read now: a client may have just attached.
112
+ */
113
+ export function showInHerdr(place, table = processTable({ fresh: true })) {
114
+ const shown =
115
+ (place?.pane && herdr(place, ["agent", "focus", place.pane], table)) ||
116
+ (place?.tab && herdr(place, ["tab", "focus", place.tab], table));
117
+ if (!shown) return null;
118
+ return herdrClient(table) ?? { attach: true };
119
+ }
120
+
121
+ /**
122
+ * Runs `command` (a session's resume, or the agent itself) in a new tab on `folder`, in the workspace the place was in
123
+ * (a new workspace on the folder when that one is gone), focused; as showInHerdr.
124
+ */
125
+ export function resumeInHerdr(
126
+ place,
127
+ command,
128
+ folder,
129
+ label,
130
+ table = processTable({ fresh: true })
131
+ ) {
132
+ const where = folder && path.isAbsolute(folder) ? ["--cwd", folder] : [];
133
+ const named = label ? ["--label", label] : [];
134
+ const tab =
135
+ (place?.workspace &&
136
+ herdr(
137
+ place,
138
+ [
139
+ "tab",
140
+ "create",
141
+ "--workspace",
142
+ place.workspace,
143
+ ...where,
144
+ ...named,
145
+ "--focus",
146
+ ],
147
+ table
148
+ )) ||
149
+ herdr(place, ["workspace", "create", ...where, ...named, "--focus"], table);
150
+ const pane = tab?.result?.root_pane?.pane_id;
151
+ if (typeof pane !== "string" || !pane) return null;
152
+ if (!herdr(place, ["pane", "run", pane, command], table)) return null;
153
+ return herdrClient(table) ?? { attach: true };
154
+ }
155
+
156
+ /** Whether herdr is on this computer, for an agent's home there; and whether the place's herdr is running now. */
157
+ export const herdrInstalled = (place, table = processTable()) =>
158
+ Boolean(herdrBinary(place, table));
159
+ export const herdrRunning = (place) => Boolean(socketOf(place));
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
  import { ensureDir, readJson } from "./files.mjs";
5
+ import { steerRoot } from "./steerQueue.mjs";
5
6
 
6
7
  // How each agent runs session hooks: the file it reads them from, that file's shape, and what it does with a hook's
7
8
  // output. Harness hooks three moments, by the names Claude Code gave them: a session starting (its board notice, where it
@@ -10,9 +11,14 @@ import { ensureDir, readJson } from "./files.mjs";
10
11
  // words (hookDialect). Every other setting and hook in a file is kept; setup replaces Harness's entries and uninstall
11
12
  // takes only them out. Read from each agent's own docs and source, October 2026.
12
13
  export const HOOK_EVENTS = ["SessionStart", "UserPromptSubmit", "Stop"];
14
+ // Claude Code alone also runs Harness at each tool call, as each subagent ends and as a permission dialog opens, for what
15
+ // the person tells one of its subagents and what a subagent asks (steerHook.mjs); its hooks name the subagent they fire in.
16
+ export const STEER_EVENTS = ["PreToolUse", "SubagentStop", "PermissionRequest"];
13
17
 
14
18
  const isHarnessHook = (entry) =>
15
- JSON.stringify(entry).includes("/lib/nudge.mjs");
19
+ ["/lib/nudge.mjs", "/lib/steerHook.mjs"].some((file) =>
20
+ JSON.stringify(entry).includes(file)
21
+ );
16
22
  const objectOr = (value) =>
17
23
  value && typeof value === "object" && !Array.isArray(value) ? value : {};
18
24
  const withOurs = (list, ours) => [
@@ -34,12 +40,20 @@ const claudeShape = ({ start = {}, startHook = {} } = {}) => ({
34
40
  : { hooks: [{ ...hook, timeout: 10 }] }
35
41
  );
36
42
  }
43
+ for (const event of STEER_EVENTS) {
44
+ if (commands[event])
45
+ next[event] = withOurs(hooks[event], {
46
+ hooks: [{ type: "command", command: commands[event], timeout: 5 }],
47
+ });
48
+ else if (Array.isArray(hooks[event]))
49
+ next[event] = withOurs(hooks[event]);
50
+ }
37
51
  return { ...settings, hooks: next };
38
52
  },
39
53
  erase: (settings) => {
40
54
  if (!settings.hooks) return settings;
41
55
  const hooks = { ...objectOr(settings.hooks) };
42
- for (const event of HOOK_EVENTS)
56
+ for (const event of [...HOOK_EVENTS, ...STEER_EVENTS])
43
57
  if (Array.isArray(hooks[event])) hooks[event] = withOurs(hooks[event]);
44
58
  return { ...settings, hooks };
45
59
  },
@@ -271,6 +285,7 @@ export function installSessionHooks(
271
285
  // Mid-session: on every prompt, one line only when the queue changed since the session last saw it.
272
286
  UserPromptSubmit: `${nudge} --if-changed`,
273
287
  Stop: `${nudge} --stop`,
288
+ ...(agent === "claude-code" ? steerCommands(runtimeRoot, nodePath) : {}),
274
289
  };
275
290
  const settings = dialect.ownFile ? {} : readJson(file, {});
276
291
  ensureDir(path.dirname(file));
@@ -280,6 +295,25 @@ export function installSessionHooks(
280
295
  );
281
296
  }
282
297
 
298
+ // Claude Code runs these on every tool call of every session, so a shell test of the steer folder goes first: Node
299
+ // starts only when a steer waits somewhere (steerQueue.mjs keeps the folder empty otherwise).
300
+ function steerCommands(runtimeRoot, nodePath) {
301
+ const hook = [
302
+ JSON.stringify(nodePath),
303
+ JSON.stringify(path.join(runtimeRoot, "lib", "steerHook.mjs")),
304
+ ].join(" ");
305
+ const guarded =
306
+ process.platform === "win32"
307
+ ? hook
308
+ : `[ -n "$(ls -A ${JSON.stringify(steerRoot())} 2>/dev/null)" ] && ${hook} || true`;
309
+ // A permission dialog is rare, and a subagent's must reach the person whether or not a steer waits.
310
+ return {
311
+ PreToolUse: guarded,
312
+ SubagentStop: guarded,
313
+ PermissionRequest: `${hook} --ask`,
314
+ };
315
+ }
316
+
283
317
  export function removeSessionHooks(
284
318
  agent,
285
319
  homeDir = os.homedir(),
@@ -27,6 +27,8 @@ export function createJobAnswers({
27
27
  actions,
28
28
  files,
29
29
  notifyBoard,
30
+ // An agent's job ended with its report in the thread (manager.mjs).
31
+ reported = () => {},
30
32
  }) {
31
33
  // What the post says: an answer to an ask follows the ask in the thread, so it stands as said; a job's result names
32
34
  // the job. What Harness made for the task is named under its answer, so every agent reading the thread can get it.
@@ -153,6 +155,7 @@ export function createJobAnswers({
153
155
  // The computer that did the job, so the thread says which Codex answered.
154
156
  machine: task.claimed_machine,
155
157
  });
158
+ if (post?.kind === "result") reported(goal, post);
156
159
  if (root && closesWithTask(goal, task, mentions)) {
157
160
  store.setGoalState(goal.id, {
158
161
  status: task.status,
package/lib/kanban.mjs ADDED
@@ -0,0 +1,256 @@
1
+ // A thread's board: the pieces of work in it as cards in four columns, To do, In progress, Review and Done, so the
2
+ // person can tell at a glance where the work stands and follow one piece without reading the whole thread. Agents keep
3
+ // it: they add a card for each piece they split the work into and move it as the work goes (update_board), and a task
4
+ // posted with a card is linked to it, so claiming that task moves the card to In progress (to Review when the task is a
5
+ // check), a failure marks it blocked and a passed check moves it to Done. Nothing waits on the board: no agent is held
6
+ // for a card, and a card nobody moves only stands still. Cards are kept per thread like its plan, so the buddy and the
7
+ // phone read the same ones (board.swarm.read).
8
+ import { randomUUID } from "node:crypto";
9
+ import { nameOf } from "./members.mjs";
10
+ import { oneLine } from "./oneLine.mjs";
11
+
12
+ export const COLUMNS = ["todo", "doing", "review", "done"];
13
+ const COLUMN_NAMES = {
14
+ todo: "To do",
15
+ doing: "In progress",
16
+ review: "Review",
17
+ done: "Done",
18
+ };
19
+ const MAX_CARDS = 40;
20
+ const TITLE_LIMIT = 120;
21
+ const NOTE_LIMIT = 240;
22
+ const HISTORY_LIMIT = 30;
23
+ // A task at one of these steps reviews the work: while it runs its card is in Review, and when it is done so is the card.
24
+ const REVIEW_STEPS = new Set(["check", "judge"]);
25
+ // A task an agent holds and works on, and one not finished yet.
26
+ const WORKING = new Set(["claimed", "running"]);
27
+ const UNFINISHED = new Set(["open", "claimed", "running", "needs_input"]);
28
+
29
+ const words = (value, limit) => oneLine(String(value ?? "")).slice(0, limit);
30
+ const whoIs = (agent) => (agent === "user" ? "You" : nameOf(agent));
31
+
32
+ export function createKanban({ store, log = () => {}, onChange = () => {} }) {
33
+ const key = (threadId) => `kanban:${threadId}`;
34
+ const cardsOf = (threadId) => {
35
+ try {
36
+ const kept = JSON.parse(store.meta(key(threadId)) || "[]");
37
+ return Array.isArray(kept) ? kept : [];
38
+ } catch {
39
+ return [];
40
+ }
41
+ };
42
+ const save = (threadId, cards) => {
43
+ store.meta(key(threadId), JSON.stringify(cards));
44
+ onChange(threadId);
45
+ };
46
+ const threadOf = (id) => {
47
+ const thread = store.getTask(String(id || ""));
48
+ return thread?.kind === "goal" ? thread : null;
49
+ };
50
+ const remember = (card, by, text) => {
51
+ card.history = [
52
+ ...(card.history ?? []),
53
+ { at: new Date().toISOString(), by, text },
54
+ ].slice(-HISTORY_LIMIT);
55
+ card.at = new Date().toISOString();
56
+ };
57
+ const moveTo = (card, column, by) => {
58
+ if (card.column === column) return false;
59
+ card.column = column;
60
+ remember(card, by, `${whoIs(by)} moved it to ${COLUMN_NAMES[column]}`);
61
+ return true;
62
+ };
63
+
64
+ /** Add a card, or change one: its column, title, note, who has it, and whether it is blocked or waits on the person. */
65
+ const update = (request, agent) => {
66
+ const thread = threadOf(request.id);
67
+ if (!thread) return { ok: false, error: "no thread has that id" };
68
+ const team = thread.goal?.team ?? [];
69
+ if (agent !== "user" && agent !== "harness" && !team.includes(agent))
70
+ return {
71
+ ok: false,
72
+ error: "only the person and the thread's agents keep its board",
73
+ };
74
+ const cards = cardsOf(thread.id);
75
+ const column =
76
+ request.column == null ? null : String(request.column).toLowerCase();
77
+ if (column && !COLUMNS.includes(column))
78
+ return {
79
+ ok: false,
80
+ error: `column is one of ${COLUMNS.join(", ")}`,
81
+ };
82
+ const cardId = request.card ? String(request.card) : null;
83
+ if (!cardId) {
84
+ const title = words(request.title, TITLE_LIMIT);
85
+ if (!title) return { ok: false, error: "a new card needs a title" };
86
+ if (cards.length >= MAX_CARDS)
87
+ return {
88
+ ok: false,
89
+ error: `a thread's board holds ${MAX_CARDS} cards; finish or remove some first`,
90
+ };
91
+ const card = {
92
+ id: randomUUID().slice(0, 8),
93
+ title,
94
+ column: column ?? "todo",
95
+ note: words(request.note, NOTE_LIMIT) || null,
96
+ agent: request.agent ? String(request.agent) : null,
97
+ blocked: words(request.blocked, NOTE_LIMIT) || null,
98
+ needsYou: words(request.needs_you, NOTE_LIMIT) || null,
99
+ tasks: [],
100
+ by: agent,
101
+ at: null,
102
+ history: [],
103
+ };
104
+ remember(card, agent, `${whoIs(agent)} added it`);
105
+ save(thread.id, [...cards, card]);
106
+ return { ok: true, card };
107
+ }
108
+ const card = cards.find((each) => each.id === cardId);
109
+ if (!card)
110
+ return { ok: false, error: "no card on this thread's board has that id" };
111
+ if (request.remove === true) {
112
+ if (agent !== "user" && agent !== card.by)
113
+ return {
114
+ ok: false,
115
+ error: "only the person or the agent that added a card removes it",
116
+ };
117
+ save(
118
+ thread.id,
119
+ cards.filter((each) => each.id !== cardId)
120
+ );
121
+ return { ok: true, removed: cardId };
122
+ }
123
+ if (column) moveTo(card, column, agent);
124
+ if (request.title != null) {
125
+ const title = words(request.title, TITLE_LIMIT);
126
+ if (title) card.title = title;
127
+ }
128
+ if (request.agent !== undefined)
129
+ card.agent = request.agent ? String(request.agent) : null;
130
+ if (request.note !== undefined) {
131
+ card.note = words(request.note, NOTE_LIMIT) || null;
132
+ if (card.note) remember(card, agent, `${whoIs(agent)}: ${card.note}`);
133
+ }
134
+ // false (or empty words) clears each.
135
+ if (request.blocked !== undefined) {
136
+ card.blocked = words(request.blocked || "", NOTE_LIMIT) || null;
137
+ remember(
138
+ card,
139
+ agent,
140
+ card.blocked
141
+ ? `${whoIs(agent)} marked it blocked: ${card.blocked}`
142
+ : `${whoIs(agent)} unblocked it`
143
+ );
144
+ }
145
+ if (request.needs_you !== undefined)
146
+ card.needsYou = words(request.needs_you || "", NOTE_LIMIT) || null;
147
+ card.at = new Date().toISOString();
148
+ save(thread.id, cards);
149
+ return { ok: true, card };
150
+ };
151
+
152
+ /** A task was posted with a card: it is linked, so what happens to the task moves the card. */
153
+ const link = (threadId, cardId, taskId) => {
154
+ const cards = cardsOf(threadId);
155
+ const card = cards.find((each) => each.id === cardId);
156
+ if (!card || card.tasks.includes(taskId)) return Boolean(card);
157
+ card.tasks = [...card.tasks, taskId];
158
+ save(threadId, cards);
159
+ // The task may have been claimed in the same breath as it was posted.
160
+ changed(store.getTask(taskId));
161
+ return true;
162
+ };
163
+
164
+ /** A task changed: the card it is linked to moves with it. */
165
+ const changed = (task) => {
166
+ const threadId = task?.parent_id;
167
+ if (!threadId || !store.meta(key(threadId))) return;
168
+ const cards = cardsOf(threadId);
169
+ const card = cards.find((each) => each.tasks.includes(task.id));
170
+ if (!card) return;
171
+ const reviews = REVIEW_STEPS.has(task.step);
172
+ const holder = task.claimed_by || task.target_agent;
173
+ let moved = false;
174
+ if (WORKING.has(task.status)) {
175
+ moved = moveTo(card, reviews ? "review" : "doing", holder);
176
+ if (card.agent !== holder) {
177
+ card.agent = holder;
178
+ moved = true;
179
+ }
180
+ if (card.blocked) {
181
+ card.blocked = null;
182
+ moved = true;
183
+ }
184
+ } else if (task.status === "failed" || task.status === "unknown") {
185
+ // The agent's own words; task.failure is only which kind of stop it was.
186
+ const why = words(task.result || "it stopped", NOTE_LIMIT);
187
+ if (card.blocked !== why) {
188
+ card.blocked = why;
189
+ remember(card, holder, `${whoIs(holder)} couldn't finish: ${why}`);
190
+ moved = true;
191
+ }
192
+ } else if (task.status === "done") {
193
+ const stillOpen = card.tasks.some(
194
+ (id) => id !== task.id && UNFINISHED.has(store.getTask(id)?.status)
195
+ );
196
+ if (reviews && !stillOpen) moved = moveTo(card, "done", holder);
197
+ else if (!stillOpen && card.column === "doing")
198
+ // The work it stands for is finished; whoever reviews it, or the agent, moves it on.
199
+ moved = moveTo(card, "review", holder);
200
+ }
201
+ if (moved) {
202
+ card.at = new Date().toISOString();
203
+ save(threadId, cards);
204
+ }
205
+ };
206
+
207
+ /** What a run in the thread is told of its board: the cards, and how to keep them. */
208
+ const brief = (threadId) => {
209
+ const cards = cardsOf(threadId);
210
+ const lines = cards.map(
211
+ (card) =>
212
+ `- [${card.id}] ${COLUMN_NAMES[card.column]}: ${card.title}${card.agent ? ` (${whoIs(card.agent)})` : ""}${card.blocked ? `, blocked: ${card.blocked}` : ""}${card.needsYou ? `, waits on the person: ${card.needsYou}` : ""}`
213
+ );
214
+ return [
215
+ "This thread has a board the person follows the work on: a card for each piece of the work, in To do, In progress, Review or Done.",
216
+ lines.length
217
+ ? `Its cards now:\n${lines.join("\n")}`
218
+ : "It has no cards yet. When the work splits into pieces, add a card for each.",
219
+ `Keep it current with update_board (thread ${threadId}): add a card for a piece of work, move yours to In progress when you start it, to Review when it is ready to be checked and to Done when it is finished, mark it blocked with why, or say it waits on the person. Post a task for a card with post_task's card, and its claim and its end move the card for you. Never wait on the board to do your work.`,
220
+ ].join("\n");
221
+ };
222
+
223
+ return {
224
+ of: (threadId) => cardsOf(threadId),
225
+ brief,
226
+ changed: (task) => {
227
+ try {
228
+ changed(task);
229
+ } catch (error) {
230
+ log(`board card move failed for ${task?.id}: ${error.message}`);
231
+ }
232
+ },
233
+ /** After board.post: link the task it made to the card it was posted for. */
234
+ posted(request, answer) {
235
+ const taskId = answer?.task?.id ?? answer?.id;
236
+ if (!request.card || !request.parentId || typeof taskId !== "string")
237
+ return answer;
238
+ const linked = link(
239
+ String(request.parentId),
240
+ String(request.card),
241
+ taskId
242
+ );
243
+ return linked
244
+ ? answer
245
+ : {
246
+ ...answer,
247
+ note: "No card on the thread's board has that id, so the task is not on a card.",
248
+ };
249
+ },
250
+ command(request, agent) {
251
+ if (String(request.command) === "board.kanban.update")
252
+ return update(request, agent);
253
+ return { ok: false, error: `unknown command ${request.command}` };
254
+ },
255
+ };
256
+ }
package/lib/library.mjs CHANGED
@@ -15,6 +15,12 @@ import { encodeWav } from "./speech.mjs";
15
15
  // against the question, and a part counts by meaning only when it stands out from all the others for that question
16
16
  // (its score in standard deviations, after each kind of part is centred on its own average). Words count beside
17
17
  // meaning, so a code or a name is found as written. Standing out is what lets a search say "nothing here".
18
+ //
19
+ // Recordings are scored against the question less what everyday questions have in common. The search model puts some
20
+ // recordings close to every question: on the person's own folders a Zoom recording's video came back for 10 of 30
21
+ // unrelated questions, its audio file for 7 and two other recordings for 5, standing out for all of them. Taking the
22
+ // common part out of text and pictures too cost more than it fixed: READMEs joined briefs about things written
23
+ // nowhere, and a screenshot lost the code written in it to a README.
18
24
 
19
25
  const MAX_FILES = 5_000;
20
26
  // A folder is walked this far to find its newest files.
@@ -32,6 +38,41 @@ const BRIEF_NEAR_BEST = 1.0;
32
38
  // scored 0.68 and up, never-said questions 0.675 at most).
33
39
  const MIN_SCALE = 200;
34
40
  const FLOOR = { image: 0.7, audio: 0.7, text: 0.7 };
41
+ // Questions about nothing of the person's in particular, across what people look for in files. What they have in
42
+ // common is taken out of a question before recordings are scored against it. None of them is a question the
43
+ // file-search or media-search evals ask.
44
+ const EVERYDAY_QUESTIONS = [
45
+ "what time does the store open",
46
+ "how do I reset my password",
47
+ "recipe for banana bread",
48
+ "the train schedule to Boston",
49
+ "notes about the product launch",
50
+ "a picture of the beach",
51
+ "who won the game last night",
52
+ "how much does the gym membership cost",
53
+ "the plumber's phone number",
54
+ "an email to my landlord",
55
+ "instructions for assembling the desk",
56
+ "my car insurance renewal",
57
+ "a recording of the team standup",
58
+ "slides for the investor meeting",
59
+ "a chart of monthly revenue",
60
+ "the wifi password",
61
+ "a photo of my kids",
62
+ "directions to the airport",
63
+ "the doctor's lab results",
64
+ "a spreadsheet of expenses",
65
+ "the warranty for my laptop",
66
+ "a podcast episode about history",
67
+ "a screenshot of a chat",
68
+ "the speech from the wedding",
69
+ "terms of service",
70
+ "a list of book recommendations",
71
+ "the hiking trail map",
72
+ "my passport scan",
73
+ "a voice memo",
74
+ "a funny video",
75
+ ];
35
76
  const SMALL_GROUP = 20;
36
77
  const WORD_WEIGHT = 2.0;
37
78
  const VECTORS_IDLE_MS = 5 * 60_000;
@@ -137,6 +178,18 @@ const hintedKinds = (text) => {
137
178
 
138
179
  const phrase = (word) => `"${word.replaceAll('"', '""')}"`;
139
180
 
181
+ const dot = (a, b) => {
182
+ let sum = 0;
183
+ for (let i = 0; i < a.length; i += 1) sum += a[i] * b[i];
184
+ return sum;
185
+ };
186
+
187
+ const average = (vectors) =>
188
+ vectors[0].map(
189
+ (_, i) =>
190
+ vectors.reduce((sum, vector) => sum + vector[i], 0) / vectors.length
191
+ );
192
+
140
193
  /**
141
194
  * Meaning scores on one scale for this query (DigUp's Searcher.meaning): each part's similarity minus the average of
142
195
  * its modality, in standard deviations of all of them together.
@@ -271,6 +324,22 @@ export function createLibrary({
271
324
  let refreshing = null;
272
325
  let skipped = [];
273
326
 
327
+ // What the everyday questions have in common: their average, embedded once.
328
+ let everyday = null;
329
+ const everydayCentre = async (lane) => {
330
+ everyday ??= inference
331
+ .embed(
332
+ EVERYDAY_QUESTIONS.map((text) => ({ kind: "query", text })),
333
+ { lane }
334
+ )
335
+ .then(average)
336
+ .catch((error) => {
337
+ everyday = null;
338
+ throw error;
339
+ });
340
+ return everyday;
341
+ };
342
+
274
343
  const vectors = () => {
275
344
  cache ??= store.vectors(version);
276
345
  clearTimeout(dropCache);
@@ -561,6 +630,8 @@ export function createLibrary({
561
630
  ? inFolders
562
631
  : threads.includes(part.source) &&
563
632
  (!readable || readable.has(part.file));
633
+ // A question without a word in it ("", "x") asks for nothing, and some part always scores highest for it.
634
+ if (!/[\p{L}\p{N}]{2,}/u.test(text)) return [];
564
635
  const words = terms(text);
565
636
  const lookup = isLookup(words);
566
637
 
@@ -569,24 +640,29 @@ export function createLibrary({
569
640
  const best = new Map();
570
641
  if (rows.length) {
571
642
  const [q] = await inference.embed([{ kind: "query", text }], { lane });
572
- const scores = rows.map((row) => {
573
- let dot = 0;
574
- for (let i = 0; i < q.length; i += 1) dot += q[i] * row.vector[i];
575
- return dot;
576
- });
643
+ const centre = await everydayCentre(lane);
644
+ const asked = q.map((value, i) => value - centre[i]);
645
+ const scores = rows.map((row) =>
646
+ dot(row.modality === "audio" ? asked : q, row.vector)
647
+ );
577
648
  const z = standOut(
578
649
  scores,
579
650
  rows.map((row) => row.modality)
580
651
  );
581
652
  const scaled = rows.length >= MIN_SCALE;
582
653
  const gate = brief ? BRIEF_Z : Z_GATE;
654
+ // The similarity to the question as asked: the floor for a small library, and what a hit reports.
655
+ const similarity = (row) => dot(q, row.vector);
583
656
  rows.forEach((row, i) => {
584
657
  if (!inScope(row)) return;
585
- const strong = scaled ? z[i] >= gate : scores[i] >= FLOOR[row.modality];
658
+ const strong = scaled
659
+ ? z[i] >= gate
660
+ : similarity(row) >= FLOOR[row.modality];
586
661
  const held = best.get(row.file);
587
662
  if (!held || z[i] > held.z)
588
- best.set(row.file, { part: row, z: z[i], cosine: scores[i], strong });
663
+ best.set(row.file, { part: row, z: z[i], strong });
589
664
  });
665
+ for (const held of best.values()) held.cosine = similarity(held.part);
590
666
  }
591
667
 
592
668
  // Words: a file holding at least half the question's words qualifies, a code or name matched as written. A brief's