tryharness 0.7.68 → 0.7.70

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.
@@ -44,6 +44,7 @@ import { member } from "./members.mjs";
44
44
  import { WINDOW_TOOLS } from "./windowAsks.mjs";
45
45
  import { createCallSessions } from "./callSessions.mjs";
46
46
  import { VIEW_COMMAND, VIEW_DROP_COMMAND } from "./liveViews.mjs";
47
+ import { computerAddress } from "./computerAddress.mjs";
47
48
 
48
49
  export { FROM_MACHINE };
49
50
 
@@ -60,15 +61,6 @@ const VIEWS_SEAL = "h2v";
60
61
  // the buddy's Swarm tab reads and sets them on each computer: a worker asks home, home asks the worker that has it.
61
62
  const MODELS_COMMAND = "machine.models";
62
63
 
63
- /**
64
- * How a computer is written after # in a message ("Work Mac" is #Work-Mac): its name with spaces as dashes and anything
65
- * but letters, digits, dots, dashes and underscores left out. Every menu writes it this way and computerNamed reads it.
66
- */
67
- export const computerHandle = (label) =>
68
- String(label ?? "")
69
- .trim()
70
- .replace(/\s+/g, "-")
71
- .replace(/[^\w.-]/g, "");
72
64
  // Every computer's running agent sessions, and one session's latest messages read on the computer it runs on, for the
73
65
  // buddy's Agents tab, the phone and PiP: a worker asks home, home asks the worker that has it.
74
66
  const LIVE_COMMAND = "machine.live";
@@ -145,8 +137,10 @@ export function createOwnMachines({
145
137
  changed = () => {},
146
138
  onNotice = () => {},
147
139
  // The folders worked in here with the repository each holds: [{ path, repository }].
140
+ // `fresh: true` rereads their current repository identities for an inventory request.
148
141
  folders = () => [],
149
142
  repositoryOf = () => null,
143
+ inventoryChanged = () => {},
150
144
  homeBack = () => {},
151
145
  // On home: a worker's call for the board's changes (boardMirror.mjs since).
152
146
  answerMirror = null,
@@ -186,11 +180,27 @@ export function createOwnMachines({
186
180
  // A proxy link's model call from one of the person's cloud computers, on the computer holding the link
187
181
  // (proxyLinks.mjs answer); null where none is held.
188
182
  answerLink = null,
183
+ // Bounded GPU operations, admitted only over the pinned Home's sealed ask.
184
+ answerPrompt = null,
189
185
  // A cloud computer's own SSH keys (cloudSsh.mjs allow): { key, by } in, null on a computer that takes none.
190
186
  ownSsh = null,
191
187
  }) {
192
188
  // On home: each worker's folders and the repository each holds, as it said them.
193
189
  const infos = new Map();
190
+ const inventoryRequests = new Map();
191
+ // Previous folders help diagnose a reconnect, never authorize placement.
192
+ const inventoryOf = (id) => {
193
+ const info = infos.get(id);
194
+ return {
195
+ state: info?.current ? "ready" : info ? "stale" : "unknown",
196
+ receivedAt: info?.receivedAt ?? null,
197
+ repositories: [
198
+ ...new Set((info?.folders ?? []).map((folder) => folder.repository)),
199
+ ],
200
+ pending: Boolean(inventoryRequests.get(id)?.pending),
201
+ error: inventoryRequests.get(id)?.error ?? null,
202
+ };
203
+ };
194
204
  // The account's other computers as Harness last listed them: { id, label, role, connected, agents, publicKey,
195
205
  // standInFor }, and this one as Harness lists it.
196
206
  let siblings = [];
@@ -302,6 +312,8 @@ export function createOwnMachines({
302
312
  const leftHome = machine.role === "home" && role === "worker";
303
313
  // A stand-in going back hands over what it wrote (boardMirror.mjs handOver); it brings no board of its own.
304
314
  const wasStandIn = Boolean(store.meta("standing-in-for"));
315
+ for (const info of infos.values()) info.current = false;
316
+ inventoryRequests.clear();
305
317
  machine.role = role;
306
318
  // Setup's code is never a home's join code: a home is not waiting to join anything (M4).
307
319
  if (role === "home") machine.joinCode = null;
@@ -347,6 +359,27 @@ export function createOwnMachines({
347
359
  if (someoneStandsIn()) returning();
348
360
  if (!homeListed()?.connected) toldHome = false;
349
361
  else if (!isHome()) void flush();
362
+ if (isHome()) {
363
+ for (const sibling of siblings) {
364
+ const prior = before.get(sibling.id);
365
+ if (
366
+ !sibling.connected ||
367
+ !prior?.connected ||
368
+ prior.publicKey !== sibling.publicKey
369
+ ) {
370
+ const info = infos.get(sibling.id);
371
+ if (info) info.current = false;
372
+ inventoryRequests.delete(sibling.id);
373
+ }
374
+ if (
375
+ sibling.connected &&
376
+ confirmed(sibling) &&
377
+ inventoryOf(sibling.id).state !== "ready"
378
+ )
379
+ void refreshInventory(sibling.id);
380
+ }
381
+ inventoryChanged();
382
+ }
350
383
  };
351
384
 
352
385
  const rename = async (label) => {
@@ -1200,7 +1233,7 @@ export function createOwnMachines({
1200
1233
  if (session?.id === "none") session = null;
1201
1234
  try {
1202
1235
  for (;;) {
1203
- const [next, ...rest] = outbox();
1236
+ const [next] = outbox();
1204
1237
  if (!next) break;
1205
1238
  const key = await keyFor(pinned.public_key, machine.id);
1206
1239
  const answered = await deliverSealed(
@@ -1219,7 +1252,9 @@ export function createOwnMachines({
1219
1252
  break;
1220
1253
  }
1221
1254
  const result = answered.result;
1222
- keepOut(rest);
1255
+ // A receipt can be queued while this delivery is awaiting its answer.
1256
+ // Remove only the delivered item, preserving concurrently appended work.
1257
+ keepOut(outbox().filter((item) => item.requestId !== next.requestId));
1223
1258
  log(
1224
1259
  `sent ${next.command} to your home computer: ${result?.ok === false && result.code !== "replayed" ? result.error : "taken"}`
1225
1260
  );
@@ -1425,6 +1460,9 @@ export function createOwnMachines({
1425
1460
  // Kept, sealed, until home is back; the call carries when it was written.
1426
1461
  const { requestId, envelope } = await send(pinned, call, { queued: true });
1427
1462
  keepOut([...outbox(), { requestId, envelope, command: call.command }]);
1463
+ // Home may have reconnected during the failed call, after its connection
1464
+ // notification already drained the queue. Give this newly queued item a turn.
1465
+ if (homeListed()?.connected) void flush();
1428
1466
  return {
1429
1467
  ok: true,
1430
1468
  queued: true,
@@ -1757,6 +1795,62 @@ export function createOwnMachines({
1757
1795
  }).catch(() => null);
1758
1796
  };
1759
1797
 
1798
+ // Home can restart without breaking the worker's connection to Harness.
1799
+ // Ask explicitly instead of relying on that worker noticing a goodbye.
1800
+ const refreshInventory = async (workerId, { force = false } = {}) => {
1801
+ const sibling = siblings.find((peer) => peer.id === workerId);
1802
+ if (!isHome() || !sibling?.connected || !confirmed(sibling))
1803
+ return {
1804
+ ok: false,
1805
+ error: "inventory needs a connected, confirmed worker",
1806
+ };
1807
+ if (!force && inventoryRequests.has(workerId))
1808
+ return { ok: true, inventory: inventoryOf(workerId) };
1809
+ const request = { id: randomUUID(), pending: true, error: null };
1810
+ inventoryRequests.set(workerId, request);
1811
+ const info = infos.get(workerId);
1812
+ if (info) info.current = false;
1813
+ changed();
1814
+ // A sealed answer completes the snapshot, rather than acknowledging only
1815
+ // its notice. Old workers refuse the unknown question. A missing transport
1816
+ // answer gets one retry; no separate deadline or timer is introduced here.
1817
+ for (let attempt = 0; attempt < 2; attempt++) {
1818
+ const answer = await askWorker(workerId, { inventory: true }).catch(
1819
+ () => null
1820
+ );
1821
+ if (inventoryRequests.get(workerId) !== request) break;
1822
+ if (
1823
+ answer?.ok === true &&
1824
+ learn(workerId, { request: request.id, folders: answer.folders })
1825
+ )
1826
+ break;
1827
+ if (answer) break;
1828
+ }
1829
+ if (inventoryRequests.get(workerId) === request) {
1830
+ request.pending = false;
1831
+ request.error = "Repository inventory did not arrive";
1832
+ changed();
1833
+ inventoryChanged(workerId);
1834
+ }
1835
+ return {
1836
+ ok: inventoryOf(workerId).state === "ready",
1837
+ inventory: inventoryOf(workerId),
1838
+ };
1839
+ };
1840
+
1841
+ const reportInventory = async (request) => {
1842
+ const home = pinnedHome();
1843
+ if (isHome() || !home) return;
1844
+ const here = folders({ fresh: true });
1845
+ const reported = await callOnce(home, {
1846
+ command: "board.machine.info",
1847
+ agent: "user",
1848
+ folders: here,
1849
+ inventoryRequest: request,
1850
+ });
1851
+ if (reported?.ok === true) toldHome = JSON.stringify(here);
1852
+ };
1853
+
1760
1854
  // A notice is sealed by the computer that sent it as home: the home this computer follows, or another it confirmed (one
1761
1855
  // that moved home to itself, or stands in), tried in turn. Harness's word on who sent it counts for nothing.
1762
1856
  const openNoticeFromAny = async (body) => {
@@ -1793,6 +1887,12 @@ export function createOwnMachines({
1793
1887
  }
1794
1888
  // Everything else is the word of the home this computer follows; another's is heard and set aside.
1795
1889
  if (from !== acceptedHome()) return { ok: true };
1890
+ if (!isHome() && typeof notice.inventoryRequest === "string") {
1891
+ void reportInventory(notice.inventoryRequest).catch((error) =>
1892
+ log(`repository inventory was not reported: ${error.message}`)
1893
+ );
1894
+ return { ok: true };
1895
+ }
1796
1896
  if (typeof notice.removed === "string") {
1797
1897
  removedByHome(notice.removed);
1798
1898
  return { ok: true };
@@ -1990,22 +2090,26 @@ export function createOwnMachines({
1990
2090
  // a question for the windows only from the few they answer for home (M6).
1991
2091
  // A cloud home's call through a proxy link this computer holds is answered for that home alone (proxyLinks.mjs).
1992
2092
  const answer =
1993
- call.home === true
1994
- ? await becomeHome(call.computers)
1995
- : call.link && answerLink
1996
- ? await answerLink(pinned.machine_id, call.link)
1997
- : call.models
1998
- ? await ownModels(call.models)
1999
- : call.live
2000
- ? await ownLive(call.live)
2001
- : call.ssh
2002
- ? ownSshAllow(call.ssh)
2003
- : WINDOW_TOOLS.has(call.tool)
2004
- ? await askOwnWindows(call)
2005
- : {
2006
- ok: false,
2007
- error: `home cannot ask this computer's windows for ${call.tool}`,
2008
- };
2093
+ call.prompt && answerPrompt
2094
+ ? await answerPrompt(call.prompt, pinned.machine_id)
2095
+ : call.inventory === true
2096
+ ? { ok: true, folders: folders({ fresh: true }) }
2097
+ : call.home === true
2098
+ ? await becomeHome(call.computers)
2099
+ : call.link && answerLink
2100
+ ? await answerLink(pinned.machine_id, call.link)
2101
+ : call.models
2102
+ ? await ownModels(call.models)
2103
+ : call.live
2104
+ ? await ownLive(call.live)
2105
+ : call.ssh
2106
+ ? ownSshAllow(call.ssh)
2107
+ : WINDOW_TOOLS.has(call.tool)
2108
+ ? await askOwnWindows(call)
2109
+ : {
2110
+ ok: false,
2111
+ error: `home cannot ask this computer's windows for ${call.tool}`,
2112
+ };
2009
2113
  return {
2010
2114
  envelope: await sealAskAnswer(
2011
2115
  key,
@@ -2072,6 +2176,7 @@ export function createOwnMachines({
2072
2176
  connected: sibling.connected,
2073
2177
  agents: sibling.agents,
2074
2178
  confirmed: confirmed(sibling),
2179
+ ...(isHome() ? { inventory: inventoryOf(sibling.id) } : {}),
2075
2180
  // Harness runs it (a cloud computer): what home lets it do is an agents' computer's, never the person's.
2076
2181
  hosted: store.ownMachine(sibling.id)?.hosted === true,
2077
2182
  standsInForThis: sibling.standInFor === machine.id,
@@ -2572,20 +2677,23 @@ export function createOwnMachines({
2572
2677
 
2573
2678
  /**
2574
2679
  * For the coordinator: the person's computers with this agent, each with whether it is online and has the thread's
2575
- * repository. Undefined while the agent is on this computer alone.
2680
+ * repository, including an agent installed on this computer alone.
2576
2681
  */
2577
2682
  const computersFor = (agent, workspace) => {
2578
2683
  const computers = computersWith(agent);
2579
- if (!computers.some((computer) => !computer.here)) return undefined;
2580
2684
  const repository = workspace ? repositoryOf(workspace) : null;
2581
2685
  return computers.map(({ id, label, online, here }) => ({
2686
+ id,
2582
2687
  label,
2583
2688
  online,
2689
+ ...(!here ? { inventory: inventoryOf(id) } : {}),
2584
2690
  ...(repository
2585
2691
  ? {
2586
2692
  thread_repository: here
2587
2693
  ? true
2588
- : namesOf(infos.get(id)?.folders ?? []).includes(repository),
2694
+ : inventoryOf(id).state === "ready"
2695
+ ? namesOf(infos.get(id).folders).includes(repository)
2696
+ : null,
2589
2697
  }
2590
2698
  : {}),
2591
2699
  }));
@@ -2593,7 +2701,33 @@ export function createOwnMachines({
2593
2701
 
2594
2702
  /** On home: what a worker said it has (its folders and their repositories), sealed, so work can go to it. */
2595
2703
  const learn = (id, info) => {
2596
- infos.set(String(id), { folders: info.folders ?? [] });
2704
+ id = String(id);
2705
+ if (!store.ownMachine(id)) return false;
2706
+ if (info.request && inventoryRequests.get(id)?.id !== info.request)
2707
+ return false;
2708
+ if (
2709
+ !Array.isArray(info.folders) ||
2710
+ info.folders.some(
2711
+ (folder) =>
2712
+ typeof folder?.path !== "string" ||
2713
+ !path.isAbsolute(folder.path) ||
2714
+ typeof folder.repository !== "string"
2715
+ )
2716
+ )
2717
+ return false;
2718
+ const folders = info.folders.slice(0, 200).map((folder) => ({
2719
+ path: folder.path.slice(0, 1024),
2720
+ repository: folder.repository.slice(0, 300),
2721
+ }));
2722
+ const receivedAt = new Date().toISOString();
2723
+ infos.set(id, { folders, receivedAt, current: true });
2724
+ inventoryRequests.delete(id);
2725
+ log(
2726
+ `repository inventory received from ${labelOf(id) ?? id}: ${info.folders.length} folders`
2727
+ );
2728
+ changed();
2729
+ inventoryChanged(id);
2730
+ return true;
2597
2731
  };
2598
2732
  const namesOf = (list) => [
2599
2733
  ...new Set(list.map((folder) => folder.repository).filter(Boolean)),
@@ -2618,6 +2752,7 @@ export function createOwnMachines({
2618
2752
  for (const sibling of siblings) {
2619
2753
  if (!sibling.agents.includes(agent) || !store.ownMachine(sibling.id))
2620
2754
  continue;
2755
+ if (inventoryOf(sibling.id).state !== "ready") continue;
2621
2756
  const folder = infos
2622
2757
  .get(sibling.id)
2623
2758
  ?.folders.find((each) => each.repository === repository)?.path;
@@ -2635,9 +2770,12 @@ export function createOwnMachines({
2635
2770
  const label = labelOf(id) ?? "that computer";
2636
2771
  if (!sibling || !store.ownMachine(id))
2637
2772
  return { error: `${label} is not connected` };
2773
+ if (!sibling.connected) return { error: `${label} is offline` };
2638
2774
  if (!sibling.agents.includes(agent))
2639
2775
  return { error: `${nameOf(agent)} is not on ${label}` };
2640
2776
  if (!workspace) return { machine: id, folder: null };
2777
+ if (inventoryOf(id).state !== "ready")
2778
+ return { error: `${label}'s repository inventory is not ready` };
2641
2779
  const repository = repositoryOf(workspace);
2642
2780
  const folder = repository
2643
2781
  ? infos.get(id)?.folders.find((each) => each.repository === repository)
@@ -2661,28 +2799,16 @@ export function createOwnMachines({
2661
2799
  },
2662
2800
  ...siblings.map((sibling) => ({
2663
2801
  ...sibling,
2664
- repositories: namesOf(infos.get(sibling.id)?.folders ?? []),
2802
+ repositories:
2803
+ inventoryOf(sibling.id).state === "ready"
2804
+ ? namesOf(infos.get(sibling.id).folders)
2805
+ : [],
2665
2806
  })),
2666
2807
  ];
2667
2808
 
2668
- /**
2669
- * The computer a message names with # (`#pop-os`), one of the person's own by its exact name in any case; null when
2670
- * it names none. Anything else after a # (an issue number, a heading) is not a computer.
2671
- */
2672
- const computerNamed = (text) => {
2673
- const computers = all();
2674
- if (computers.length < 2) return null;
2675
- for (const match of String(text || "").matchAll(
2676
- /(?:^|[\s(])#([A-Za-z0-9][\w.-]*)/g
2677
- )) {
2678
- const name = match[1].replace(/[.,;:!?]+$/, "").toLowerCase();
2679
- const found = computers.find(
2680
- (computer) => computerHandle(computer.label).toLowerCase() === name
2681
- );
2682
- if (found) return { id: found.id, label: found.label };
2683
- }
2684
- return null;
2685
- };
2809
+ /** The computers a message sends its agents to, of the person's own as they are now (computerAddress.mjs). */
2810
+ const addressIn = (text, { target = null, agentOf } = {}) =>
2811
+ computerAddress({ text, computers: all(), target, agentOf });
2686
2812
 
2687
2813
  /** The name beside an agent's words from that computer ("Codex (linux)"): only once the person has more than one. */
2688
2814
  const shownLabel = (id) => (all().length > 1 ? labelOf(id) : null);
@@ -2771,6 +2897,10 @@ export function createOwnMachines({
2771
2897
  answerAs,
2772
2898
  pushViews,
2773
2899
  askWorkerWindows,
2900
+ promptTo: (workerId, call) =>
2901
+ isHome() && store.ownMachine(workerId)?.hosted !== true
2902
+ ? askWorker(workerId, { prompt: call })
2903
+ : Promise.resolve(null),
2774
2904
  linkCall,
2775
2905
  tellWorkers,
2776
2906
  fromLink,
@@ -2803,9 +2933,10 @@ export function createOwnMachines({
2803
2933
  status,
2804
2934
  all,
2805
2935
  learn,
2936
+ refreshInventory,
2806
2937
  placeElsewhere,
2807
2938
  placeOn,
2808
- computerNamed,
2939
+ addressIn,
2809
2940
  computersWith,
2810
2941
  computersFor,
2811
2942
  labelOf,