tryharness 0.7.67 → 0.7.69

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 (54) hide show
  1. package/lib/boardFileCommands.mjs +5 -7
  2. package/lib/boardFiles.mjs +5 -0
  3. package/lib/boardProtocol.json +50 -64
  4. package/lib/boardWaits.mjs +6 -0
  5. package/lib/cards.mjs +13 -1
  6. package/lib/comfyExecution.mjs +545 -0
  7. package/lib/context.mjs +34 -20
  8. package/lib/contextWorker.mjs +199 -0
  9. package/lib/coordinator.mjs +224 -22
  10. package/lib/daemon.mjs +196 -14
  11. package/lib/dispatchContract.json +313 -0
  12. package/lib/dispatchTools.js +87 -0
  13. package/lib/dispatches.mjs +975 -0
  14. package/lib/environment.mjs +54 -69
  15. package/lib/files.mjs +35 -0
  16. package/lib/frames.mjs +5 -9
  17. package/lib/goalCommands.mjs +13 -10
  18. package/lib/kept.mjs +131 -0
  19. package/lib/landed.mjs +1 -34
  20. package/lib/library.mjs +18 -11
  21. package/lib/live.mjs +30 -11
  22. package/lib/localAgentAsk.mjs +27 -4
  23. package/lib/localStack.mjs +17 -13
  24. package/lib/machineRoutes.mjs +5 -0
  25. package/lib/machineRuns.mjs +2 -1
  26. package/lib/mcp-stdio.mjs +42 -1
  27. package/lib/openHere.mjs +36 -0
  28. package/lib/openRequests.mjs +56 -0
  29. package/lib/ownMachines.mjs +232 -24
  30. package/lib/phones.mjs +29 -4
  31. package/lib/places.mjs +44 -11
  32. package/lib/portCalls.mjs +54 -0
  33. package/lib/promptDecider.mjs +471 -0
  34. package/lib/promptFlow.mjs +562 -0
  35. package/lib/promptScopes.mjs +670 -0
  36. package/lib/provenance.mjs +3 -2
  37. package/lib/runAuthority.mjs +14 -2
  38. package/lib/runBrief.mjs +2 -2
  39. package/lib/runWorker.mjs +25 -5
  40. package/lib/runner.mjs +62 -6
  41. package/lib/sealed.mjs +37 -0
  42. package/lib/sessionControl.mjs +47 -14
  43. package/lib/sessionIndex.mjs +11 -3
  44. package/lib/store.mjs +362 -40
  45. package/lib/swarm.mjs +85 -34
  46. package/lib/swarmMemory.mjs +39 -16
  47. package/lib/taskCommands.mjs +125 -1
  48. package/lib/team.json +10 -0
  49. package/lib/team.mjs +287 -26
  50. package/lib/threadBriefs.mjs +43 -31
  51. package/lib/threadRuns.mjs +47 -3
  52. package/npm-shrinkwrap.json +14 -14
  53. package/package.json +4 -4
  54. package/release.json +55 -43
@@ -1,6 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import { spawnSync } from "node:child_process";
3
+ import { kept, readNow } from "./kept.mjs";
4
4
 
5
5
  // What the relay can see of the person's computer without asking for any permission: which app is in front and how
6
6
  // long since they last touched the keyboard or mouse (macOS: lsappinfo and the HID idle timer; Linux under X11 when
@@ -13,23 +13,9 @@ const REPO_TTL_MS = 10_000;
13
13
  const CALL_APPS =
14
14
  /^(?:zoom\.us|FaceTime|Microsoft Teams|Webex|Slack Huddle|Discord|Around|Google Meet|Tuple|Pop)$/i;
15
15
 
16
- const run = (command, args, options = {}) => {
17
- try {
18
- const out = spawnSync(command, args, {
19
- encoding: "utf8",
20
- timeout: 2_000,
21
- ...options,
22
- });
23
- return out.status === 0 ? out.stdout : null;
24
- } catch {
25
- return null;
26
- }
27
- };
28
-
29
16
  /** The front app's name and seconds since the last input, or nulls where the OS can't say without a permission. */
30
17
  export function readPerson({
31
18
  platform = process.platform,
32
- exec = run,
33
19
  file = process.env.HARNESS_PRESENCE_FILE,
34
20
  } = {}) {
35
21
  // Tests stand in for the OS: a JSON file { app, idleSeconds }, read instead of it (Harness's desktop end-to-end test
@@ -47,27 +33,30 @@ export function readPerson({
47
33
  return { app: null, idleSeconds: null };
48
34
  }
49
35
  }
36
+ return readNow(personSteps(platform));
37
+ }
38
+
39
+ /** readPerson's commands, as kept.mjs steps. */
40
+ function* personSteps(platform) {
50
41
  if (platform === "darwin") {
51
- const asn = exec("lsappinfo", ["front"])?.trim();
42
+ const asn = (yield ["lsappinfo", ["front"]])?.trim();
52
43
  const app = asn
53
- ? (exec("lsappinfo", ["info", "-only", "name", asn])?.match(
44
+ ? ((yield ["lsappinfo", ["info", "-only", "name", asn]])?.match(
54
45
  /"LSDisplayName"\s*=\s*"([^"]*)"/
55
46
  )?.[1] ?? null)
56
47
  : null;
57
- const idle = exec("ioreg", [
58
- "-c",
59
- "IOHIDSystem",
60
- "-d",
61
- "4",
62
- "-r",
63
- "-k",
64
- "HIDIdleTime",
48
+ const idle = (yield [
49
+ "ioreg",
50
+ ["-c", "IOHIDSystem", "-d", "4", "-r", "-k", "HIDIdleTime"],
65
51
  ])?.match(/"HIDIdleTime"\s*=\s*(\d+)/)?.[1];
66
52
  return { app, idleSeconds: idle ? Math.round(Number(idle) / 1e9) : null };
67
53
  }
68
54
  if (platform === "linux" && process.env.DISPLAY) {
69
- const idle = exec("xprintidle", []);
70
- const pid = exec("xdotool", ["getactivewindow", "getwindowpid"])?.trim();
55
+ const idle = yield ["xprintidle", []];
56
+ const pid = (yield [
57
+ "xdotool",
58
+ ["getactivewindow", "getwindowpid"],
59
+ ])?.trim();
71
60
  let app = null;
72
61
  try {
73
62
  if (pid) app = fs.readFileSync(`/proc/${pid}/comm`, "utf8").trim();
@@ -77,20 +66,23 @@ export function readPerson({
77
66
  return { app: null, idleSeconds: null };
78
67
  }
79
68
 
80
- /** A repository's branch and the files changed in it and not committed, newest change first. */
81
- export function readRepository(folder, { exec = run } = {}) {
82
- const top = exec("git", ["rev-parse", "--show-toplevel"], {
83
- cwd: folder,
84
- })?.trim();
69
+ /** A repository's branch and the files changed in it and not committed, newest change first, as kept.mjs steps. */
70
+ function* repositorySteps(folder) {
71
+ if (!folder || !fs.existsSync(folder)) return null;
72
+ const top = (yield [
73
+ "git",
74
+ ["rev-parse", "--show-toplevel"],
75
+ { cwd: folder },
76
+ ])?.trim();
85
77
  if (!top) return null;
86
78
  const branch =
87
- exec("git", ["branch", "--show-current"], { cwd: top })?.trim() || null;
79
+ (yield ["git", ["branch", "--show-current"], { cwd: top }])?.trim() || null;
88
80
  const status =
89
- exec(
81
+ (yield [
90
82
  "git",
91
83
  ["status", "--porcelain=v1", "-z", "--untracked-files=normal"],
92
- { cwd: top }
93
- ) ?? "";
84
+ { cwd: top },
85
+ ]) ?? "";
94
86
  const changed = [];
95
87
  const entries = status.split("\0");
96
88
  for (let i = 0; i < entries.length; i += 1) {
@@ -115,9 +107,11 @@ export function readRepository(folder, { exec = run } = {}) {
115
107
  });
116
108
  }
117
109
  changed.sort((a, b) => (b.at ?? 0) - (a.at ?? 0));
118
- const last = exec("git", ["log", "-1", "--format=%ct%x00%s"], {
119
- cwd: top,
120
- })?.split("\0");
110
+ const last = (yield [
111
+ "git",
112
+ ["log", "-1", "--format=%ct%x00%s"],
113
+ { cwd: top },
114
+ ])?.split("\0");
121
115
  return {
122
116
  root: top,
123
117
  branch,
@@ -133,40 +127,31 @@ export function readRepository(folder, { exec = run } = {}) {
133
127
  };
134
128
  }
135
129
 
136
- export function createEnvironment({
137
- platform = process.platform,
138
- exec = run,
139
- } = {}) {
140
- let person = null;
141
- const repos = new Map();
130
+ export function createEnvironment({ platform = process.platform } = {}) {
131
+ // Both are read beside the daemon's thread once they are this old; past a few times that, an ask waits for a new one.
132
+ const personKept = kept(() => personSteps(platform), {
133
+ freshMs: PERSON_TTL_MS,
134
+ staleMs: 4 * PERSON_TTL_MS,
135
+ });
136
+ const repositoryKept = kept(repositorySteps, {
137
+ freshMs: REPO_TTL_MS,
138
+ staleMs: 6 * REPO_TTL_MS,
139
+ });
142
140
  return {
143
141
  /** { app, idleSeconds, inCall, at }: never sent off this computer. */
144
142
  person() {
145
- if (
146
- !person ||
147
- Date.now() - person.at >
148
- (process.env.HARNESS_PRESENCE_FILE ? 0 : PERSON_TTL_MS)
149
- ) {
150
- const read = readPerson({ platform, exec });
151
- person = {
152
- ...read,
153
- inCall: Boolean(read.app && CALL_APPS.test(read.app)),
154
- at: Date.now(),
155
- };
156
- }
157
- return person;
158
- },
159
- repository(folder) {
160
- const cached = repos.get(folder);
161
- if (cached && Date.now() - cached.at < REPO_TTL_MS) return cached.value;
162
- const value =
163
- folder && fs.existsSync(folder)
164
- ? readRepository(folder, { exec })
165
- : null;
166
- repos.set(folder, { at: Date.now(), value });
167
- if (repos.size > 200) repos.delete(repos.keys().next().value);
168
- return value;
143
+ const read = process.env.HARNESS_PRESENCE_FILE
144
+ ? readPerson({ platform })
145
+ : personKept();
146
+ return {
147
+ ...read,
148
+ inCall: Boolean(read.app && CALL_APPS.test(read.app)),
149
+ at: Date.now(),
150
+ };
169
151
  },
152
+ repository: (folder) => repositoryKept(folder),
153
+ /** repository() for a caller that can wait, never holding the daemon's thread. */
154
+ repositoryLater: (folder) => repositoryKept.later(folder),
170
155
  /** Minutes since a file in folder last changed, from its uncommitted files or its last commit; null when unknown. */
171
156
  lastChanged(folder, now = Date.now()) {
172
157
  const repo = this.repository(folder);
package/lib/files.mjs CHANGED
@@ -63,6 +63,41 @@ export function listJsonl(root) {
63
63
  return found;
64
64
  }
65
65
 
66
+ /**
67
+ * listJsonl without holding the daemon's thread: each directory is read and its files' sizes taken on libuv's threads.
68
+ * Session folders are walked whenever an agent writes to one, about every second while sessions run.
69
+ */
70
+ export async function listJsonlAsync(root) {
71
+ const found = [];
72
+ const pending = [root];
73
+ while (pending.length) {
74
+ const dir = pending.pop();
75
+ let entries = [];
76
+ try {
77
+ entries = await fs.promises.readdir(dir, { withFileTypes: true });
78
+ } catch {
79
+ continue;
80
+ }
81
+ const files = [];
82
+ for (const entry of entries) {
83
+ const full = path.join(dir, entry.name);
84
+ if (entry.isDirectory()) pending.push(full);
85
+ else if (entry.isFile() && entry.name.endsWith(".jsonl"))
86
+ files.push(full);
87
+ }
88
+ const stats = await Promise.all(
89
+ files.map((file) =>
90
+ fs.promises.stat(file).then(
91
+ (stat) => ({ file, mtime: stat.mtimeMs, size: stat.size }),
92
+ () => null
93
+ )
94
+ )
95
+ );
96
+ found.push(...stats.filter(Boolean));
97
+ }
98
+ return found;
99
+ }
100
+
66
101
  // A file's type from its name, for files an agent attaches without saying; anything unknown is plain bytes.
67
102
  const MIME_TYPES = {
68
103
  png: "image/png",
package/lib/frames.mjs CHANGED
@@ -90,8 +90,8 @@ export function createFrames({
90
90
  store,
91
91
  environment,
92
92
  worktrees,
93
- atlas = () => null,
94
- scope = () => [],
93
+ // Files and code named in words, in a folder (the context engine's mentions); none without it.
94
+ resolveNamed = async () => ({ files: [], symbols: [] }),
95
95
  log = () => {},
96
96
  }) {
97
97
  const key = (threadId) => `frame:${threadId}`;
@@ -147,7 +147,7 @@ export function createFrames({
147
147
  };
148
148
 
149
149
  /** The frame for a thread, rebuilt from what is on the computer now. pack: the context engine's latest pack, if any. */
150
- const build = (threadId, { pack = null } = {}) => {
150
+ const build = async (threadId, { pack = null } = {}) => {
151
151
  const started = performance.now();
152
152
  const thread = store.getTask(threadId);
153
153
  if (!thread || thread.kind !== "goal") return null;
@@ -168,16 +168,12 @@ export function createFrames({
168
168
  const wordsHash = createHash("sha1").update(asked).digest("hex");
169
169
  let named = previous?.asked?.hash === wordsHash ? previous.asked : null;
170
170
  if (!named) {
171
- const index = atlas();
172
171
  named = {
173
172
  hash: wordsHash,
174
- ...mentions(asked, {
175
- scope: index && place ? scope(place.folder) : [],
176
- atlas: index,
177
- }),
173
+ ...(await resolveNamed(asked, place?.folder ?? null)),
178
174
  };
179
175
  }
180
- const repo = place ? environment.repository(place.folder) : null;
176
+ const repo = place ? await environment.repositoryLater(place.folder) : null;
181
177
  const repository = repo
182
178
  ? {
183
179
  branch: repo.branch,
@@ -32,7 +32,6 @@ export function createGoalCommands({
32
32
  pauseGoal,
33
33
  relayRequest,
34
34
  replaceHeld,
35
- searchContext,
36
35
  sendThreadMessage,
37
36
  setupNote,
38
37
  slotOf,
@@ -214,17 +213,21 @@ export function createGoalCommands({
214
213
  }
215
214
  : save(null);
216
215
  }
216
+ // One thread's memory, read the same way as the whole swarm's (swarm.mjs board.swarm.search): its posts and its
217
+ // jobs' results and receipts, by words and meaning, marked weak when nothing closely matches.
217
218
  if (request.command === "board.room.search") {
218
219
  if (!inRoom(goal, agent))
219
220
  return { ok: false, error: `${agent} is not on this goal's team` };
220
- return {
221
- ok: true,
222
- pending: searchContext(
223
- goal.id,
224
- String(request.query || ""),
225
- Math.max(1, Math.min(20, Number(request.limit) || 8))
226
- ),
227
- };
221
+ return swarmCommand(
222
+ {
223
+ command: "board.swarm.search",
224
+ query: request.query,
225
+ thread: goal.id,
226
+ hostedCaller: request.hostedCaller === true,
227
+ },
228
+ agent,
229
+ machine
230
+ );
228
231
  }
229
232
  if (request.command === "board.room.post") {
230
233
  if (!inRoom(goal, agent))
@@ -387,7 +390,7 @@ export function createGoalCommands({
387
390
  const notes = [
388
391
  ...(unwoken.length
389
392
  ? [
390
- `Named ${unwoken.map((name) => `@${name}`).join(", ")} without asking, so not woken. To ask an agent, put its name in ask.`,
393
+ `Named ${unwoken.map((name) => `@${name}`).join(", ")} without asking, so not woken. To ask an agent or hand it the next step, put its name in ask.`,
391
394
  ]
392
395
  : []),
393
396
  ...wakes.map(setupNote).filter(Boolean),
package/lib/kept.mjs ADDED
@@ -0,0 +1,131 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+
3
+ // Readings of the computer the daemon asks for on its one thread many times a second (the process table, which folder
4
+ // each agent works in, a repository's state, T3 Code's record), each a command or two that take tens to hundreds of ms.
5
+ // Run synchronously they held every reply; on the owner's Mac, 4 s in every 50 (October 28).
6
+ //
7
+ // A reading is written once, as steps: a generator that yields [command, args, options] for each command it needs and
8
+ // gets back the command's stdout, or null when it failed (with { anyStatus: true }, null only when it could not run).
9
+ // kept() answers from the last reading and takes the next one
10
+ // beside the daemon's thread once it is freshMs old; only the first ask, or one the caller says the last reading cannot
11
+ // answer (a process that started a moment ago), waits for a reading taken now.
12
+
13
+ const TIMEOUT_MS = 3_000;
14
+ const MAX_BUFFER = 16 << 20;
15
+
16
+ const runNow = (command, args, { anyStatus = false, ...options } = {}) => {
17
+ try {
18
+ const out = spawnSync(command, args, {
19
+ encoding: "utf8",
20
+ timeout: TIMEOUT_MS,
21
+ maxBuffer: MAX_BUFFER,
22
+ ...options,
23
+ });
24
+ if (out.error) return null;
25
+ return out.status === 0 || anyStatus ? out.stdout : null;
26
+ } catch {
27
+ return null;
28
+ }
29
+ };
30
+
31
+ const runLater = (
32
+ command,
33
+ args,
34
+ { timeout = TIMEOUT_MS, anyStatus = false, ...options } = {}
35
+ ) =>
36
+ new Promise((resolve) => {
37
+ let child;
38
+ try {
39
+ child = spawn(command, args, {
40
+ stdio: ["ignore", "pipe", "ignore"],
41
+ ...options,
42
+ });
43
+ } catch {
44
+ return resolve(null);
45
+ }
46
+ const chunks = [];
47
+ let size = 0;
48
+ child.stdout.on("data", (chunk) => {
49
+ size += chunk.length;
50
+ if (size > MAX_BUFFER) child.kill();
51
+ else chunks.push(chunk);
52
+ });
53
+ const timer = setTimeout(() => child.kill(), timeout);
54
+ child.on("error", () => {
55
+ clearTimeout(timer);
56
+ resolve(null);
57
+ });
58
+ child.on("close", (status) => {
59
+ clearTimeout(timer);
60
+ resolve(
61
+ status === 0 || anyStatus
62
+ ? Buffer.concat(chunks).toString("utf8")
63
+ : null
64
+ );
65
+ });
66
+ });
67
+
68
+ /** A reading taken now, on the daemon's thread. */
69
+ export function readNow(steps) {
70
+ let step = steps.next();
71
+ while (!step.done) step = steps.next(runNow(...step.value));
72
+ return step.value;
73
+ }
74
+
75
+ /** A reading taken beside the daemon's thread. */
76
+ export async function readLater(steps) {
77
+ let step = steps.next();
78
+ while (!step.done) step = steps.next(await runLater(...step.value));
79
+ return step.value;
80
+ }
81
+
82
+ /**
83
+ * A reading answered from memory. read(...args) gives its steps; readings are kept apart by keyOf(...args) (by
84
+ * default the first argument: a folder's repository), up to `max` of them. A reading staleMs old is not answered
85
+ * with (asked after a long quiet, it would be long out of date), and `wanting(value, ...args)` says the kept reading
86
+ * cannot answer this ask: either waits for a reading taken now.
87
+ */
88
+ export function kept(
89
+ read,
90
+ {
91
+ freshMs,
92
+ staleMs = Infinity,
93
+ max = 200,
94
+ keyOf = (first) => first ?? null,
95
+ wanting = () => false,
96
+ }
97
+ ) {
98
+ const readings = new Map();
99
+ const keep = (key, value) => {
100
+ readings.set(key, { value, at: Date.now(), again: null });
101
+ if (readings.size > max) readings.delete(readings.keys().next().value);
102
+ return value;
103
+ };
104
+ const usable = (held, args) =>
105
+ held && Date.now() - held.at < staleMs && !wanting(held.value, ...args);
106
+
107
+ const now = (...args) => {
108
+ const key = keyOf(...args);
109
+ const held = readings.get(key);
110
+ if (!usable(held, args)) return keep(key, readNow(read(...args)));
111
+ if (Date.now() - held.at >= freshMs && !held.again)
112
+ held.again = readLater(read(...args))
113
+ .then((value) => {
114
+ held.value = value;
115
+ held.at = Date.now();
116
+ })
117
+ .catch(() => {})
118
+ .finally(() => {
119
+ held.again = null;
120
+ });
121
+ return held.value;
122
+ };
123
+ /** The same reading for a caller that can wait: one that would be taken now is taken beside the daemon's thread. */
124
+ now.later = async (...args) => {
125
+ const key = keyOf(...args);
126
+ const held = readings.get(key);
127
+ if (usable(held, args)) return now(...args);
128
+ return keep(key, await readLater(read(...args)));
129
+ };
130
+ return now;
131
+ }
package/lib/landed.mjs CHANGED
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { spawn } from "node:child_process";
4
4
  import { paths } from "./paths.mjs";
5
+ import { openHere } from "./openHere.mjs";
5
6
  import { LIVE_AGENTS } from "./live.mjs";
6
7
  import { nameOf } from "./members.mjs";
7
8
  import {
@@ -858,40 +859,6 @@ export function createLanded({
858
859
  return request.onComputer === true ? openHere(plan) : plan;
859
860
  }
860
861
 
861
- /**
862
- * The phone's Open: this computer opens the work itself, where the buddy would have opened it (a T3 Code or Codex
863
- * link, or the app). The link and the app are this computer's own reading of the work, never the caller's. A test
864
- * stands in for the opener with HARNESS_OPEN_COMMAND, as notify.mjs takes HARNESS_NOTIFY_COMMAND.
865
- */
866
- function openHere(plan) {
867
- if (!plan.ok || (!plan.link && !plan.app)) return plan;
868
- const args =
869
- process.platform === "darwin"
870
- ? plan.link
871
- ? [plan.link]
872
- : ["-a", plan.app]
873
- : plan.link
874
- ? [plan.link]
875
- : null;
876
- if (!args)
877
- return {
878
- ok: false,
879
- error: `${plan.opensIn ?? "Its app"} can only be opened on this computer itself.`,
880
- };
881
- const child = spawn(
882
- process.env.HARNESS_OPEN_COMMAND ||
883
- (process.platform === "darwin" ? "open" : "xdg-open"),
884
- args,
885
- {
886
- detached: true,
887
- stdio: "ignore",
888
- }
889
- );
890
- child.on("error", () => {});
891
- child.unref();
892
- return { ok: true, opensIn: plan.opensIn, openedHere: true };
893
- }
894
-
895
862
  const COMMANDS = new Set([
896
863
  "board.landed.thread",
897
864
  "board.landed.folder",
package/lib/library.mjs CHANGED
@@ -318,7 +318,7 @@ export function createLibrary({
318
318
  store.drop(file);
319
319
  store.put(rows);
320
320
  });
321
- if (mediaReady()) await embedMedia(parts, rows, lane);
321
+ if (await mediaReady()) await embedMedia(parts, rows, lane);
322
322
  cache = null;
323
323
  return parts;
324
324
  };
@@ -359,7 +359,7 @@ export function createLibrary({
359
359
  }
360
360
  // Pictures and sound read before the image and audio part came down: each file read again once.
361
361
  const tried = new Set();
362
- while (mediaReady()) {
362
+ while (await mediaReady()) {
363
363
  const [waiting] = store.toEmbed(version, ["image", "audio"], 1);
364
364
  if (!waiting || tried.has(waiting.file)) break;
365
365
  tried.add(waiting.file);
@@ -451,7 +451,7 @@ export function createLibrary({
451
451
  let added = 0;
452
452
  const seenSkips = [];
453
453
  // The person's folders.
454
- const searched = folders();
454
+ const searched = await folders();
455
455
  const listed = [];
456
456
  for (const folder of searched) {
457
457
  const found = await crawl(folder);
@@ -486,7 +486,9 @@ export function createLibrary({
486
486
  }
487
487
  }
488
488
  // The threads' files: stored once and never changed, so each is read once.
489
- const threadList = threadFiles().filter((file) => fileKind(file.name));
489
+ const threadList = (await threadFiles()).filter((file) =>
490
+ fileKind(file.name)
491
+ );
490
492
  threadPaths.clear();
491
493
  for (const file of threadList) {
492
494
  threadPaths.set(file.id, file.path);
@@ -521,7 +523,10 @@ export function createLibrary({
521
523
  skipped = seenSkips;
522
524
  const embedded = await embedWaiting(lane);
523
525
  // Pictures and sound wait for the image and audio part of the search model.
524
- if (!mediaReady() && store.toEmbed(version, ["image", "audio"], 1).length)
526
+ if (
527
+ !(await mediaReady()) &&
528
+ store.toEmbed(version, ["image", "audio"], 1).length
529
+ )
525
530
  wantMedia();
526
531
  return {
527
532
  files: listed.length + threadList.length,
@@ -535,25 +540,27 @@ export function createLibrary({
535
540
 
536
541
  /**
537
542
  * The files closest to a question, best first, one hit per file at its best part. scope: folders (the person's added
538
- * folders: agents on this computer only) and threads (thread ids whose files to search); allow(file) drops thread
539
- * files a caller may not read. brief: only hits that stand out, for a brief nobody asked for.
543
+ * folders: agents on this computer only) and threads (thread ids whose files to search); allowed, when given, is the
544
+ * only thread files the caller may read (their ids). brief: only hits that stand out, for a brief nobody asked for.
540
545
  */
541
546
  const search = async (
542
547
  query,
543
548
  {
544
549
  folders: inFolders = false,
545
550
  threads = [],
546
- allow = () => true,
551
+ allowed = null,
547
552
  limit = 8,
548
553
  lane = "interactive",
549
554
  brief = false,
550
555
  } = {}
551
556
  ) => {
552
557
  const text = String(query).slice(0, 4_000);
558
+ const readable = allowed ? new Set(allowed) : null;
553
559
  const inScope = (part) =>
554
560
  part.source === "folders"
555
561
  ? inFolders
556
- : threads.includes(part.source) && allow(part.file);
562
+ : threads.includes(part.source) &&
563
+ (!readable || readable.has(part.file));
557
564
  const words = terms(text);
558
565
  const lookup = isLookup(words);
559
566
 
@@ -671,8 +678,8 @@ export function createLibrary({
671
678
  for (const watcher of watchers.values()) watcher.close();
672
679
  watchers.clear();
673
680
  },
674
- status: () => ({
675
- folders: folders(),
681
+ status: async () => ({
682
+ folders: await folders(),
676
683
  files: store.count(),
677
684
  folderFiles: store.files("folders").size,
678
685
  waiting: store.waiting(version),
package/lib/live.mjs CHANGED
@@ -4,10 +4,11 @@ import path from "node:path";
4
4
  import { spawnSync } from "node:child_process";
5
5
  import zlib from "node:zlib";
6
6
  import { readJson } from "./files.mjs";
7
+ import { kept } from "./kept.mjs";
7
8
  import { historyRoots } from "./paths.mjs";
8
9
  import { PS } from "./nudge.mjs";
9
10
  import { stepsOf } from "./runner.mjs";
10
- import { t3App, t3Link, t3Thread, t3Working } from "./places.mjs";
11
+ import { t3App, t3Link, t3ThreadRecent, t3WorkingRecently } from "./places.mjs";
11
12
 
12
13
  // The person's own coding sessions running on this computer right now, each with the app it runs in: a terminal, an
13
14
  // editor or T3 Code, found by walking up the session's process tree to the first process inside an app bundle. A
@@ -30,15 +31,15 @@ export const LIVE_AGENTS = [
30
31
  ];
31
32
  const PLAIN_ID = /^[A-Za-z0-9._:-]{1,128}$/;
32
33
 
33
- /** Every process: its parent, its terminal (/dev/ttys003, Linux's /dev/pts/3, or null) and its executable, by pid. */
34
- export function processTable() {
34
+ // The buddy's live list and every hook placing a session ask for the table, many times a second; it is read again
35
+ // beside the daemon's thread once it is this old (kept.mjs).
36
+ const TABLE_FRESH_MS = 2_000;
37
+ const TABLE_STALE_MS = 30_000;
38
+
39
+ function* processSteps() {
35
40
  const table = new Map();
36
- if (process.platform === "win32") return table;
37
- const ps = spawnSync(PS, ["-axo", "pid=,ppid=,tty=,comm="], {
38
- encoding: "utf8",
39
- maxBuffer: 16 << 20,
40
- });
41
- for (const line of (ps.stdout || "").split("\n")) {
41
+ const ps = yield [PS, ["-axo", "pid=,ppid=,tty=,comm="]];
42
+ for (const line of (ps || "").split("\n")) {
42
43
  const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\S+)\s+(.+)$/);
43
44
  if (match)
44
45
  table.set(Number(match[1]), {
@@ -50,6 +51,24 @@ export function processTable() {
50
51
  return table;
51
52
  }
52
53
 
54
+ const tableKept = kept(processSteps, {
55
+ freshMs: TABLE_FRESH_MS,
56
+ staleMs: TABLE_STALE_MS,
57
+ keyOf: () => "all",
58
+ wanting: (table, { having = [], fresh = false } = {}) =>
59
+ fresh || having.some((pid) => !table.has(pid)),
60
+ });
61
+
62
+ /**
63
+ * Every process: its parent, its terminal (/dev/ttys003, Linux's /dev/pts/3, or null) and its executable, by pid; up
64
+ * to TABLE_FRESH_MS old. `having`: pids that must be in it (a process that started a moment ago), read now if not.
65
+ * `fresh`: read now, for a decision made once (a process that ended a moment ago is still in a kept reading).
66
+ */
67
+ export function processTable({ having = [], fresh = false } = {}) {
68
+ if (process.platform === "win32") return new Map();
69
+ return tableKept({ having, fresh });
70
+ }
71
+
53
72
  /** True when a process runs inside tmux: a tmux server is among its ancestors. */
54
73
  function underTmux(table, pid) {
55
74
  for (let current = pid, steps = 0; current > 1 && steps < 64; steps++) {
@@ -241,7 +260,7 @@ export function liveSessions({
241
260
  // Codex in T3 Code runs on that shared server too, so no process says which T3 thread it works in: T3's own record does.
242
261
  const t3 = t3App();
243
262
  if (t3)
244
- for (const row of t3Working("codex")) {
263
+ for (const row of t3WorkingRecently("codex")) {
245
264
  if (!sessions.some((session) => session.sessionId === row.sessionId))
246
265
  sessions.push({
247
266
  agent: "codex",
@@ -285,7 +304,7 @@ export function liveSessions({
285
304
  session.host ?? tmux?.client?.host ?? hostApp(table, session.pid);
286
305
  const t3 =
287
306
  host && /^T3 Code\b/.test(host.name)
288
- ? t3Link(session.t3Thread ?? t3Thread(session.sessionId))
307
+ ? t3Link(session.t3Thread ?? t3ThreadRecent(session.sessionId))
289
308
  : null;
290
309
  return { ...session, tty, tmux: tmux?.target ?? null, host, t3 };
291
310
  });