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/daemon.mjs CHANGED
@@ -34,6 +34,8 @@ import { keepProfile } from "./profile.mjs";
34
34
  import { createSwarmMemory } from "./swarmMemory.mjs";
35
35
  import { createWorktrees } from "./worktrees.mjs";
36
36
  import { createLanded } from "./landed.mjs";
37
+ import { createOpenRequests } from "./openRequests.mjs";
38
+ import { openHere } from "./openHere.mjs";
37
39
  import { createFolders, repositoryName } from "./folders.mjs";
38
40
  import { createRepoLinks } from "./repoLinks.mjs";
39
41
  import { notifyDesktop } from "./notify.mjs";
@@ -1357,7 +1359,6 @@ export async function startDaemon() {
1357
1359
  pauseGoal: actions.pause,
1358
1360
  relayRequest: sealed.request,
1359
1361
  replaceHeld,
1360
- searchContext: local.searchContext,
1361
1362
  sendThreadMessage: sealed.sendMessage,
1362
1363
  setupNote,
1363
1364
  slotOf: (...args) => slotOf(...args),
@@ -1517,13 +1518,15 @@ export async function startDaemon() {
1517
1518
  store,
1518
1519
  environment,
1519
1520
  worktrees,
1520
- atlas: () => (local.ready(["search"]) ? local.contextEngine.atlas : null),
1521
- scope: (folder) => local.contextEngine.scope(folder),
1521
+ resolveNamed: (text, folder) =>
1522
+ local.ready(["search"]) && folder
1523
+ ? local.contextEngine.mentions(text, folder)
1524
+ : { files: [], symbols: [] },
1522
1525
  log,
1523
1526
  });
1524
- const frameFor = (threadId, options) => {
1527
+ const frameFor = async (threadId, options) => {
1525
1528
  try {
1526
- return frames.build(threadId, options);
1529
+ return await frames.build(threadId, options);
1527
1530
  } catch (error) {
1528
1531
  log(`task frame for ${threadId}: ${error.message}`);
1529
1532
  return null;
@@ -1722,9 +1725,9 @@ export async function startDaemon() {
1722
1725
  why,
1723
1726
  }),
1724
1727
  log,
1725
- frame: (threadId) =>
1728
+ frame: async (threadId) =>
1726
1729
  coordinatorMode() === "local" || bridge.has("swarm_frame")
1727
- ? frames.roundFacts(frameFor(threadId))
1730
+ ? frames.roundFacts(await frameFor(threadId))
1728
1731
  : null,
1729
1732
  });
1730
1733
  const guestRequests = createGuestRequests({
@@ -1868,6 +1871,38 @@ export async function startDaemon() {
1868
1871
  activeRuns: () => activeRuns.values(),
1869
1872
  scrub: (value) => vault.scrubDeep(value),
1870
1873
  });
1874
+ // Open, asked for by the person at another of their computers: planned here, carried out by this computer's buddy.
1875
+ const openRequests = createOpenRequests({
1876
+ plan: async (open) => {
1877
+ // A thread in this computer's T3 Code opens by T3's own link to it, read here.
1878
+ if (open.thread) {
1879
+ await environments.ready()?.catch(() => null);
1880
+ const thread = environments
1881
+ .roots()
1882
+ .find((root) => root.id === String(open.thread));
1883
+ return thread?.source?.url
1884
+ ? {
1885
+ ok: true,
1886
+ opensIn: thread.source.environmentName,
1887
+ link: thread.source.url,
1888
+ }
1889
+ : { ok: false, error: "that thread is not in T3 Code here" };
1890
+ }
1891
+ return liveCommand(
1892
+ {
1893
+ command: "board.live.focus",
1894
+ to: open.to,
1895
+ sessionId: open.sessionId ?? null,
1896
+ folder: open.folder ?? null,
1897
+ fresh: open.fresh === true,
1898
+ },
1899
+ "user"
1900
+ );
1901
+ },
1902
+ waits: boardWaits,
1903
+ openHere,
1904
+ log,
1905
+ });
1871
1906
 
1872
1907
  // Lid-Closed Mode's Standby: when anything last came in (a phone message, a wake, a new task).
1873
1908
  let listenerAt = Date.now();
@@ -1961,7 +1996,12 @@ export async function startDaemon() {
1961
1996
  places
1962
1997
  ? places.command(request, agent)
1963
1998
  : { ok: false, error: "the daemon is still starting" },
1964
- live: (request, { agent }) => liveCommand(request, agent),
1999
+ live: (request, { agent }) =>
2000
+ request.command !== "board.live.opens"
2001
+ ? liveCommand(request, agent)
2002
+ : agent === "user"
2003
+ ? openRequests.take()
2004
+ : { ok: false, error: "only the person's buddy opens things here" },
1965
2005
  prompt: (request, { agent }) => {
1966
2006
  // A session a run started is that run's, never one the person typed in.
1967
2007
  if (!request[RUN_CALLER]) authority.markPrompt(request.pids, agent);
@@ -2252,6 +2292,7 @@ export async function startDaemon() {
2252
2292
  });
2253
2293
  return result?.pending ? await result.pending : result;
2254
2294
  }
2295
+ if (live?.open) return openRequests.open(live.open);
2255
2296
  if (live?.read)
2256
2297
  return liveCommand(
2257
2298
  {
@@ -2723,6 +2764,11 @@ export async function startDaemon() {
2723
2764
  ? ownMachines.readSession(request)
2724
2765
  : { ok: false, error: "only the person reads their sessions" }
2725
2766
  ),
2767
+ "machines.live.open": onMachines((request) =>
2768
+ request.agent === "user"
2769
+ ? ownMachines.openSession(request)
2770
+ : { ok: false, error: "only the person opens their sessions" }
2771
+ ),
2726
2772
  "machines.model": onMachines((request) =>
2727
2773
  request.agent === "user"
2728
2774
  ? ownMachines.setModel(request)
@@ -14,8 +14,9 @@ import { ASK_TIMEOUT_MS } from "./bridge.mjs";
14
14
  // for windows, pages of the board, every one still sealed with the pair's key. The link is a second road, not a
15
15
  // second set of rules.
16
16
  // - A worker dials home. A request with no link, or with no answer within the time it has through Harness, gets null
17
- // and goes through Harness; a link that went quiet is dropped and dialed again when it is next needed (a connection
18
- // can freeze for good on some networks: holepunchto/libudx#333).
17
+ // and goes through Harness; the link itself stays. It closes when HyperDHT's keep-alive finds the other side gone,
18
+ // and is dialed again when it is next needed (a connection can freeze for good on some networks:
19
+ // holepunchto/libudx#333). A new link counts once one round trip over it answers.
19
20
 
20
21
  // The public HyperDHT network's own starting points unless the computer is told others (a local test network).
21
22
  const bootstrapOf = (value) =>
@@ -74,8 +75,6 @@ export function createDirectLink({
74
75
  }
75
76
  };
76
77
 
77
- const peerIdOf = (peer) =>
78
- [...peers.entries()].find(([, each]) => each === peer)?.[0] ?? null;
79
78
  const peerOfKey = (hex) =>
80
79
  [...peers.entries()].find(([, peer]) => peer.key === hex)?.[0] ?? null;
81
80
 
@@ -132,7 +131,6 @@ export function createDirectLink({
132
131
  const before = peer.socket;
133
132
  peer.socket = socket;
134
133
  peer.verified = verified;
135
- peer.heardAt = Date.now();
136
134
  if (before && before !== socket) {
137
135
  release(peer, before);
138
136
  before.destroy();
@@ -142,7 +140,6 @@ export function createDirectLink({
142
140
  let buffered = "";
143
141
  socket.on("data", (chunk) => {
144
142
  const current = peers.get(id);
145
- if (current?.socket === socket) current.heardAt = Date.now();
146
143
  buffered += chunk.toString("utf8");
147
144
  if (buffered.length > MAX_FRAME_BYTES) return socket.destroy();
148
145
  let at;
@@ -204,7 +201,6 @@ export function createDirectLink({
204
201
  socket: null,
205
202
  pending: new Map(),
206
203
  dialing: null,
207
- heardAt: 0,
208
204
  });
209
205
  return;
210
206
  }
@@ -297,19 +293,15 @@ export function createDirectLink({
297
293
  }
298
294
  return send(peer, peer.socket, body);
299
295
  };
296
+ // One request over the link and its answer; null when none came in time. A slow answer is not a dead link: that one
297
+ // call goes through Harness instead and the link stays (owner, October 28: links to pop-os and MBP-2 were torn down
298
+ // and dialed again about 20 times in 10 minutes, each time one push to a busy computer went 15 s unanswered). The
299
+ // link closes when HyperDHT's own keep-alive finds the other side gone (`close` and `error` in attach).
300
300
  const send = (peer, socket, body, waitMs = timeoutMs) => {
301
- const id = peerIdOf(peer);
302
301
  const i = ++sequence;
303
- const sentAt = Date.now();
304
302
  return new Promise((resolve) => {
305
303
  const timer = setTimeout(() => {
306
304
  peer.pending.delete(i);
307
- // Nothing at all heard from the other computer for as long as Harness would have taken: the link is dropped and
308
- // dialed again when next needed. Heard from meanwhile, only this request was slow and the link stays.
309
- if (peer.socket === socket && peer.heardAt < sentAt) {
310
- log(`the direct link with ${id} went quiet; going through Harness`);
311
- socket.destroy();
312
- }
313
305
  resolve(null);
314
306
  }, waitMs);
315
307
  peer.pending.set(i, { resolve, timer, socket });
@@ -325,11 +317,33 @@ export function createDirectLink({
325
317
  at: peer.socket?.rawStream?.remoteHost ?? null,
326
318
  }));
327
319
 
320
+ /**
321
+ * The listener started afresh, the person's computers' keys kept: on home it once stayed unreachable for 30 minutes
322
+ * after a restart (every dial from pop-os HOLEPUNCH_ABORTED) until the daemon restarted again, when both workers
323
+ * linked within seconds (owner, October 28: home restarts its listener). Every link drops and is made again.
324
+ */
325
+ const restart = async () => {
326
+ for (const peer of peers.values())
327
+ if (peer.socket) {
328
+ const socket = peer.socket;
329
+ release(peer, socket);
330
+ peer.socket = null;
331
+ socket.destroy();
332
+ }
333
+ await server?.close().catch(() => {});
334
+ await node?.destroy().catch(() => {});
335
+ server = null;
336
+ node = null;
337
+ starting = null;
338
+ log("direct links: started the listener again");
339
+ return start();
340
+ };
341
+
328
342
  const stop = async () => {
329
343
  for (const peer of peers.values()) peer.socket?.destroy();
330
344
  await server?.close().catch(() => {});
331
345
  await node?.destroy().catch(() => {});
332
346
  };
333
347
 
334
- return { key, allow, disallow, dial, request, status, stop };
348
+ return { key, allow, disallow, dial, request, status, restart, stop };
335
349
  }
@@ -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
+ }