tryharness 0.7.83 → 0.7.86

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 (215) hide show
  1. package/README.md +21 -19
  2. package/assets/dsh-plugin.mjs +75 -4
  3. package/assets/herdr-mod/harness/main.js +367 -0
  4. package/assets/herdr-mod/harness/mod.json +6 -0
  5. package/assets/opencode-plugin.js +87 -22
  6. package/assets/pi-extension.ts +81 -2
  7. package/cli.mjs +544 -208
  8. package/lib/adapters.mjs +62 -27
  9. package/lib/agentBinaries.mjs +6 -8
  10. package/lib/agentClaims.mjs +168 -0
  11. package/lib/agentReadTools.json +415 -0
  12. package/lib/agentSignIn.mjs +17 -18
  13. package/lib/agentWiring.mjs +114 -0
  14. package/lib/archetypes.js +10 -11
  15. package/lib/attention.mjs +100 -0
  16. package/lib/audit.mjs +1 -1
  17. package/lib/backupSetup.mjs +6 -28
  18. package/lib/bankrApprovals.mjs +86 -0
  19. package/lib/boardAccess.mjs +40 -66
  20. package/lib/boardBackup.mjs +86 -85
  21. package/lib/boardCommands.mjs +28 -0
  22. package/lib/boardFileCommands.mjs +29 -36
  23. package/lib/boardFiles.mjs +83 -15
  24. package/lib/boardMirror.mjs +78 -11
  25. package/lib/boardProtocol.json +191 -123
  26. package/lib/boardThreads.mjs +19 -37
  27. package/lib/boardTools.mjs +22 -15
  28. package/lib/boardWatches.mjs +43 -15
  29. package/lib/bridge.mjs +7 -3
  30. package/lib/browserAgentTools.mjs +59 -0
  31. package/lib/browserWake.mjs +0 -6
  32. package/lib/cards.mjs +52 -0
  33. package/lib/circling.mjs +189 -0
  34. package/lib/cleanup.mjs +234 -0
  35. package/lib/cloudBoot.mjs +21 -4
  36. package/lib/cloudSave.mjs +46 -5
  37. package/lib/compaction.mjs +8 -1
  38. package/lib/completion.mjs +358 -0
  39. package/lib/computerDevices.mjs +853 -0
  40. package/lib/connectionStatus.mjs +34 -0
  41. package/lib/connectors.mjs +139 -1
  42. package/lib/coordinator.mjs +108 -14
  43. package/lib/coreModels.mjs +36 -12
  44. package/lib/daemon.mjs +2426 -338
  45. package/lib/daemonLock.mjs +103 -0
  46. package/lib/deviceDoor.mjs +308 -0
  47. package/lib/deviceIdentity.mjs +114 -0
  48. package/lib/deviceList.mjs +172 -0
  49. package/lib/deviceSeal.mjs +74 -0
  50. package/lib/dispatcher.mjs +497 -26
  51. package/lib/dispatches.mjs +3 -3
  52. package/lib/environment.mjs +2 -1
  53. package/lib/environments.mjs +67 -41
  54. package/lib/events.mjs +318 -0
  55. package/lib/facts.mjs +170 -0
  56. package/lib/fileSource.mjs +540 -0
  57. package/lib/files.mjs +47 -2
  58. package/lib/gitHost.mjs +39 -1
  59. package/lib/goalCommands.mjs +12 -80
  60. package/lib/handover.mjs +21 -16
  61. package/lib/harnessOffers.mjs +140 -15
  62. package/lib/herdr.mjs +100 -15
  63. package/lib/herdrAppCall.mjs +19 -0
  64. package/lib/herdrCore.mjs +59 -0
  65. package/lib/herdrRuns.mjs +1126 -0
  66. package/lib/hookDialects.mjs +235 -50
  67. package/lib/identityLink.mjs +81 -0
  68. package/lib/inference.mjs +31 -2
  69. package/lib/jamAccess.mjs +82 -0
  70. package/lib/{projectBoard.mjs → jamBoard.mjs} +5 -4
  71. package/lib/{projectGit.mjs → jamGit.mjs} +53 -15
  72. package/lib/jamKey.mjs +274 -0
  73. package/lib/{projectPieces.mjs → jamPieces.mjs} +35 -29
  74. package/lib/{projectSettings.mjs → jamSettings.mjs} +12 -22
  75. package/lib/jamStore.mjs +973 -0
  76. package/lib/jamUpgrade.mjs +112 -0
  77. package/lib/jams.mjs +3079 -0
  78. package/lib/kanban.mjs +13 -7
  79. package/lib/landed.mjs +5 -1
  80. package/lib/library.mjs +41 -22
  81. package/lib/limits.mjs +91 -0
  82. package/lib/lineDiff.mjs +97 -0
  83. package/lib/lineup.mjs +18 -13
  84. package/lib/link.mjs +584 -0
  85. package/lib/localStack.mjs +75 -16
  86. package/lib/localmodels.mjs +58 -78
  87. package/lib/loopBackstop.mjs +2 -1
  88. package/lib/machine.mjs +544 -0
  89. package/lib/machineLink.mjs +446 -0
  90. package/lib/machineRoutes.mjs +68 -19
  91. package/lib/machineRuns.mjs +85 -4
  92. package/lib/machineSeal.mjs +6 -31
  93. package/lib/manager.mjs +134 -12
  94. package/lib/mcp-stdio.mjs +492 -215
  95. package/lib/mcpRelay.mjs +34 -35
  96. package/lib/members.json +1 -1
  97. package/lib/modelRoute.mjs +162 -0
  98. package/lib/nativeHost.mjs +81 -90
  99. package/lib/nativeSenses.mjs +258 -0
  100. package/lib/nerves.mjs +523 -0
  101. package/lib/network.mjs +117 -278
  102. package/lib/networkPlaces.mjs +578 -0
  103. package/lib/nextStep.mjs +95 -0
  104. package/lib/notify.mjs +2 -2
  105. package/lib/nudge.mjs +116 -10
  106. package/lib/originals.mjs +216 -0
  107. package/lib/outsideEvents.mjs +383 -0
  108. package/lib/outsideRuns.mjs +230 -0
  109. package/lib/ownMachines.mjs +674 -256
  110. package/lib/pain.mjs +215 -0
  111. package/lib/pairing.mjs +91 -11
  112. package/lib/paths.mjs +42 -3
  113. package/lib/peerProcess.mjs +21 -7
  114. package/lib/peopleLink.mjs +268 -0
  115. package/lib/personNotices.mjs +3 -2
  116. package/lib/phones.mjs +252 -542
  117. package/lib/placeCards.mjs +282 -0
  118. package/lib/placeChecks.mjs +95 -0
  119. package/lib/placeCommands.mjs +8 -17
  120. package/lib/placeRequests.mjs +152 -0
  121. package/lib/placeStore.mjs +295 -0
  122. package/lib/placeTool.d.ts +12 -0
  123. package/lib/placeTool.js +40 -0
  124. package/lib/places.mjs +63 -14
  125. package/lib/plans.mjs +53 -8
  126. package/lib/presence.mjs +270 -0
  127. package/lib/privateNetwork.d.ts +2 -0
  128. package/lib/privateNetwork.js +93 -0
  129. package/lib/proposals.mjs +300 -0
  130. package/lib/publicFetch.d.ts +32 -0
  131. package/lib/publicFetch.js +248 -0
  132. package/lib/reach.mjs +128 -0
  133. package/lib/rebuildable.mjs +72 -0
  134. package/lib/recall.mjs +216 -0
  135. package/lib/record.mjs +13 -0
  136. package/lib/reflexes.mjs +392 -0
  137. package/lib/relayLimits.mjs +31 -2
  138. package/lib/releaseNotice.mjs +198 -0
  139. package/lib/removeHarness.mjs +15 -4
  140. package/lib/repoLinks.mjs +204 -35
  141. package/lib/runAuthority.mjs +102 -39
  142. package/lib/runBrief.mjs +93 -40
  143. package/lib/runRequests.mjs +52 -14
  144. package/lib/runUsage.mjs +31 -0
  145. package/lib/runWorker.mjs +74 -25
  146. package/lib/runner.mjs +289 -45
  147. package/lib/runtime-install.mjs +37 -8
  148. package/lib/schedules.mjs +171 -47
  149. package/lib/screenWatches.mjs +221 -0
  150. package/lib/sealed.mjs +12 -30
  151. package/lib/sealedThreads.mjs +198 -392
  152. package/lib/secretStore.mjs +178 -0
  153. package/lib/secrets.mjs +11 -0
  154. package/lib/seen.mjs +3 -9
  155. package/lib/selfUpdate.mjs +51 -15
  156. package/lib/senseCall.mjs +1514 -0
  157. package/lib/senseHook.mjs +114 -0
  158. package/lib/senseTools.mjs +1298 -0
  159. package/lib/sessionBriefs.mjs +87 -0
  160. package/lib/sessionControl.mjs +63 -13
  161. package/lib/sessionFiles.mjs +85 -0
  162. package/lib/setupChoices.mjs +12 -3
  163. package/lib/setupMode.mjs +70 -25
  164. package/lib/sharedKeys.mjs +166 -0
  165. package/lib/sleep.mjs +396 -0
  166. package/lib/sources.mjs +306 -0
  167. package/lib/speech.mjs +166 -411
  168. package/lib/spoken.mjs +210 -0
  169. package/lib/steerHook.mjs +3 -1
  170. package/lib/steerQueue.mjs +40 -11
  171. package/lib/store.mjs +323 -130
  172. package/lib/stuck.mjs +85 -7
  173. package/lib/swarm.mjs +157 -49
  174. package/lib/swarmDevices.mjs +25 -85
  175. package/lib/swarmMemory.mjs +93 -9
  176. package/lib/t3.mjs +73 -3
  177. package/lib/taskCommands.mjs +51 -60
  178. package/lib/taskRooms.mjs +30 -4
  179. package/lib/taskRules.json +29 -0
  180. package/lib/taskRules.mjs +21 -0
  181. package/lib/taskRuns.mjs +74 -11
  182. package/lib/team.mjs +2 -1
  183. package/lib/threadActions.mjs +37 -9
  184. package/lib/threadAuthority.mjs +4 -4
  185. package/lib/threadBriefs.mjs +173 -43
  186. package/lib/threadNotes.mjs +320 -0
  187. package/lib/threadRecord.mjs +491 -0
  188. package/lib/threadRuns.mjs +216 -108
  189. package/lib/threadView.mjs +27 -30
  190. package/lib/threadWatches.mjs +1962 -0
  191. package/lib/timeSense.mjs +839 -0
  192. package/lib/tripwires.mjs +454 -0
  193. package/lib/ui.mjs +1 -1
  194. package/lib/vault.mjs +560 -45
  195. package/lib/vaultCommands.mjs +5 -3
  196. package/lib/vaultPeers.mjs +165 -0
  197. package/lib/vaultTools.d.ts +4 -1
  198. package/lib/vaultTools.js +22 -1
  199. package/lib/verdicts.mjs +12 -26
  200. package/lib/versions.mjs +117 -0
  201. package/lib/waitTool.mjs +278 -0
  202. package/lib/wakePicture.mjs +268 -0
  203. package/lib/wakeQueue.mjs +108 -0
  204. package/lib/watchResponses.mjs +533 -0
  205. package/lib/watches.mjs +290 -145
  206. package/lib/windowAsks.mjs +80 -17
  207. package/lib/worktrees.mjs +159 -9
  208. package/npm-shrinkwrap.json +138 -631
  209. package/package.json +7 -6
  210. package/release.json +211 -129
  211. package/lib/callSessions.mjs +0 -51
  212. package/lib/directLink.mjs +0 -349
  213. package/lib/peopleBoard.mjs +0 -131
  214. package/lib/projects.mjs +0 -2917
  215. package/lib/roomChannels.mjs +0 -667
@@ -0,0 +1,32 @@
1
+ export type Refusal = {
2
+ reason: "invalid_url" | "not_https" | "private_network";
3
+ message: string;
4
+ };
5
+ export declare class PublicFetchRefused extends TypeError {
6
+ readonly refusal: Refusal;
7
+ constructor(refusal: Refusal);
8
+ }
9
+ export type Resolved = { address: string; family: number };
10
+ export type Outgoing = {
11
+ method: string;
12
+ headers: Headers;
13
+ body?: Buffer;
14
+ signal?: AbortSignal;
15
+ };
16
+ /** How a name resolves, and one request sent to the checked address. */
17
+ export type Network = {
18
+ resolve(hostname: string): Promise<Resolved[]>;
19
+ send(url: URL, address: Resolved, request: Outgoing): Promise<Response>;
20
+ };
21
+ export type GuardRules = { httpsOnly: boolean; who: string };
22
+ export declare const systemNetwork: Network;
23
+ export declare const publicNetwork: Network;
24
+ export function publicFetchRefusal(
25
+ raw: string,
26
+ network: Network,
27
+ rules: GuardRules
28
+ ): Promise<Refusal | null>;
29
+ export function createPublicFetch(
30
+ network?: Network,
31
+ rules?: Partial<GuardRules>
32
+ ): typeof fetch;
@@ -0,0 +1,248 @@
1
+ import { lookup as dnsLookup } from "node:dns/promises";
2
+ import http from "node:http";
3
+ import https from "node:https";
4
+ import { isIP } from "node:net";
5
+ import { Readable } from "node:stream";
6
+ import zlib from "node:zlib";
7
+ import { isPrivateAddress, isPrivateNetworkHost } from "./privateNetwork.js";
8
+
9
+ // The one way Harness fetches an address a person, an agent or a site gave it (a custom model endpoint, a wake
10
+ // webhook, a place, a site an automation uses), on its server (src/server/net/publicFetch.ts) and on the person's
11
+ // computer (places.mjs). Such an address must never steer the caller into its own network: Fly's private 6PN, the
12
+ // metadata service, the database, the person's router or printer, another app's loopback port. So, per request:
13
+ // - the host is resolved here, and every address it resolves to must be public (a name that also resolves to a
14
+ // private address is refused, not tried in turn);
15
+ // - the connection goes to the address that was checked, so a second lookup cannot answer differently (DNS
16
+ // rebinding) between the check and the connect;
17
+ // - redirects are never left to the platform: each hop is checked again before it is followed, and a redirect to
18
+ // another origin drops the request's authorization;
19
+ // - where `httpsOnly`, the address must be https.
20
+ // Like fetch, it sets no deadline of its own; callers pass a signal.
21
+
22
+ /** A request the guard would not make. A TypeError, as fetch's own network failures are. */
23
+ export class PublicFetchRefused extends TypeError {
24
+ constructor(refusal) {
25
+ super(refusal.message);
26
+ this.name = "PublicFetchRefused";
27
+ this.refusal = refusal;
28
+ }
29
+ }
30
+
31
+ export const systemNetwork = {
32
+ resolve: (hostname) => dnsLookup(hostname, { all: true, verbatim: true }),
33
+ send,
34
+ };
35
+
36
+ // fetch's own limit (WHATWG fetch, "HTTP-redirect fetch").
37
+ const MAX_REDIRECTS = 20;
38
+ const REDIRECTS = new Set([301, 302, 303, 307, 308]);
39
+ const NULL_BODY = new Set([101, 103, 204, 205, 304]);
40
+
41
+ function refusalForUrl(url, { httpsOnly, who }) {
42
+ if (url.protocol !== "https:" && url.protocol !== "http:")
43
+ return { reason: "invalid_url", message: "Only web addresses are called." };
44
+ if (url.protocol === "http:" && httpsOnly)
45
+ return {
46
+ reason: "not_https",
47
+ message: `${who} calls ${url.host} only over https.`,
48
+ };
49
+ if (url.username || url.password)
50
+ return {
51
+ reason: "invalid_url",
52
+ message: "An address with a user name or password in it is not called.",
53
+ };
54
+ if (isPrivateNetworkHost(url.hostname))
55
+ return { reason: "private_network", message: privateMessage(url, who) };
56
+ return null;
57
+ }
58
+ const privateMessage = (url, who) =>
59
+ `${url.host} is on a private network, which ${who} does not call.`;
60
+
61
+ // The checked address the request will connect to, or why there is none.
62
+ async function vetted(url, network, rules) {
63
+ const refused = refusalForUrl(url, rules);
64
+ if (refused) return refused;
65
+ const host = url.hostname.replace(/^\[|\]$/g, "");
66
+ const family = isIP(host);
67
+ if (family) return { address: host, family };
68
+ let addresses;
69
+ try {
70
+ addresses = await network.resolve(host);
71
+ } catch {
72
+ throw new TypeError(`${url.host} could not be found.`);
73
+ }
74
+ if (addresses.length === 0)
75
+ throw new TypeError(`${url.host} could not be found.`);
76
+ if (addresses.some((one) => isPrivateAddress(one.address)))
77
+ return {
78
+ reason: "private_network",
79
+ message: privateMessage(url, rules.who),
80
+ };
81
+ return addresses[0];
82
+ }
83
+
84
+ /** Why a request to `raw` would be refused before it is sent, or null when it would go out. */
85
+ export async function publicFetchRefusal(raw, network, rules) {
86
+ let url;
87
+ try {
88
+ url = new URL(raw);
89
+ } catch {
90
+ return { reason: "invalid_url", message: "That is not a web address." };
91
+ }
92
+ try {
93
+ const target = await vetted(url, network, rules);
94
+ return "reason" in target ? target : null;
95
+ } catch {
96
+ // A name that does not resolve is not refused here: the request fails as fetch's would.
97
+ return null;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * fetch, through the guard. `network`: how names resolve and requests go (tests script a third-party site beneath
103
+ * the guard). `httpsOnly`: refuse plain http. `who`: the caller's name in a refusal.
104
+ */
105
+ export function createPublicFetch(
106
+ network = systemNetwork,
107
+ { httpsOnly = true, who = "Harness" } = {}
108
+ ) {
109
+ const rules = { httpsOnly, who };
110
+ return async (input, init) => {
111
+ const request = new Request(input, init);
112
+ const signal = init?.signal ?? undefined;
113
+ const mode = init?.redirect ?? "follow";
114
+ let url = new URL(request.url);
115
+ let method = request.method;
116
+ const headers = new Headers(request.headers);
117
+ let body = request.body
118
+ ? Buffer.from(await request.arrayBuffer())
119
+ : undefined;
120
+ for (let hop = 0; ; hop++) {
121
+ const target = await vetted(url, network, rules);
122
+ if ("reason" in target) throw new PublicFetchRefused(target);
123
+ const response = await network.send(url, target, {
124
+ method,
125
+ headers: new Headers(headers),
126
+ body,
127
+ signal,
128
+ });
129
+ const location = response.headers.get("location");
130
+ if (!REDIRECTS.has(response.status) || !location || mode === "manual")
131
+ return withUrl(response, url, hop > 0);
132
+ if (mode === "error")
133
+ throw new TypeError(`${url.host} redirected, which was not allowed.`);
134
+ await response.body?.cancel();
135
+ if (hop >= MAX_REDIRECTS)
136
+ throw new TypeError(`${url.host} redirected too many times.`);
137
+ const next = new URL(location, url);
138
+ // As fetch does: a 303, or a 301/302 after a POST, continues as a GET without the body.
139
+ if (
140
+ (response.status === 303 && method !== "HEAD") ||
141
+ ((response.status === 301 || response.status === 302) &&
142
+ method === "POST")
143
+ ) {
144
+ method = "GET";
145
+ body = undefined;
146
+ for (const name of ["content-type", "content-length"])
147
+ headers.delete(name);
148
+ }
149
+ // A credential meant for one origin never follows a redirect to another.
150
+ if (next.origin !== url.origin) {
151
+ headers.delete("authorization");
152
+ headers.delete("cookie");
153
+ }
154
+ url = next;
155
+ }
156
+ };
157
+ }
158
+
159
+ /**
160
+ * The network the guard sends on in this process. Process-wide: the server's modules may load in two module graphs.
161
+ * Only a test changes it, to script a third-party site beneath the guard (tests/lib/scriptedSites.ts).
162
+ */
163
+ export const publicNetwork = (globalThis.__harnessPublicNetwork ??= {
164
+ ...systemNetwork,
165
+ });
166
+
167
+ function send(url, target, request) {
168
+ const secure = url.protocol === "https:";
169
+ const port = Number(url.port) || (secure ? 443 : 80);
170
+ const headers = Object.fromEntries(request.headers.entries());
171
+ headers["accept-encoding"] ??= "gzip, deflate, br";
172
+ if (request.body) headers["content-length"] = String(request.body.length);
173
+ // The connection goes to the address checked above, whatever a later lookup would say.
174
+ const pinned = (_hostname, options, callback) => {
175
+ if (options?.all) callback(null, [target]);
176
+ else callback(null, target.address, target.family);
177
+ };
178
+ return new Promise((resolve, reject) => {
179
+ const outgoing = (secure ? https : http).request(
180
+ {
181
+ protocol: url.protocol,
182
+ host: url.hostname.replace(/^\[|\]$/g, ""),
183
+ port,
184
+ path: `${url.pathname}${url.search}`,
185
+ method: request.method,
186
+ headers,
187
+ lookup: pinned,
188
+ // A fresh connection per request: a pooled one could have been opened to another address.
189
+ agent: false,
190
+ ...(request.signal ? { signal: request.signal } : {}),
191
+ },
192
+ (incoming) => {
193
+ const status = incoming.statusCode ?? 0;
194
+ const responseHeaders = new Headers();
195
+ for (let i = 0; i < incoming.rawHeaders.length; i += 2)
196
+ responseHeaders.append(
197
+ incoming.rawHeaders[i],
198
+ incoming.rawHeaders[i + 1]
199
+ );
200
+ const decoded = decode(incoming, responseHeaders);
201
+ resolve(
202
+ new Response(
203
+ NULL_BODY.has(status) || request.method === "HEAD"
204
+ ? null
205
+ : Readable.toWeb(decoded),
206
+ {
207
+ status,
208
+ statusText: incoming.statusMessage,
209
+ headers: responseHeaders,
210
+ }
211
+ )
212
+ );
213
+ }
214
+ );
215
+ outgoing.on("error", (error) =>
216
+ reject(
217
+ error.name === "AbortError"
218
+ ? (request.signal?.reason ?? error)
219
+ : new TypeError("fetch failed", { cause: error })
220
+ )
221
+ );
222
+ outgoing.end(request.body);
223
+ });
224
+ }
225
+
226
+ // fetch hands back the decoded body; so does this.
227
+ function decode(incoming, headers) {
228
+ const encoding = headers.get("content-encoding")?.trim().toLowerCase();
229
+ const decoder =
230
+ encoding === "gzip" || encoding === "x-gzip"
231
+ ? zlib.createGunzip()
232
+ : encoding === "deflate"
233
+ ? zlib.createInflate()
234
+ : encoding === "br"
235
+ ? zlib.createBrotliDecompress()
236
+ : null;
237
+ if (!decoder) return incoming;
238
+ headers.delete("content-encoding");
239
+ headers.delete("content-length");
240
+ incoming.on("error", (error) => decoder.destroy(error));
241
+ return incoming.pipe(decoder);
242
+ }
243
+
244
+ function withUrl(response, url, redirected) {
245
+ Object.defineProperty(response, "url", { value: url.toString() });
246
+ Object.defineProperty(response, "redirected", { value: redirected });
247
+ return response;
248
+ }
package/lib/reach.mjs ADDED
@@ -0,0 +1,128 @@
1
+ import { createHash } from "node:crypto";
2
+ import { ADDRESS_WAIT_MS } from "./completion.mjs";
3
+ import { readNumber } from "./watches.mjs";
4
+
5
+ // Reach (docs/senses-plan.md S7.3): a source that looks on a timer at something nobody sends. It reads and wakes; it
6
+ // never acts. Three kinds, each one value with where it came from and how old it is:
7
+ // - `web`: an address the person or an agent names, read with a plain GET; one number from it (`field`, `count` or
8
+ // `age`, as the daemon watch reads a command's output), or its first line. Free.
9
+ // - `quotient`: a prediction market's odds, read through Harness's server at Quotient's price for one lookup, counted
10
+ // against the thread's spending (src/server/swarm/reach.ts). Quotient is a research agent and never a source for
11
+ // moving money.
12
+ // - `bankr`: the person's own Bankr wallet, its total or one holding in US dollars, through Harness's server.
13
+ // A look that fails is a reading that cannot read, never a zero. How often it looks is the watch's own timer, as a
14
+ // command watch's (sources.mjs createCommand).
15
+
16
+ export const REACH_FROM = ["web", "quotient", "bankr"];
17
+
18
+ /** A look at a web address: its status, then one value from its body. */
19
+ async function lookAtWeb({ address, field, count, age }, now) {
20
+ let response;
21
+ try {
22
+ response = await fetch(address, {
23
+ redirect: "manual",
24
+ signal: AbortSignal.timeout(ADDRESS_WAIT_MS),
25
+ });
26
+ } catch (error) {
27
+ return {
28
+ error:
29
+ error?.name === "TimeoutError"
30
+ ? "it did not answer in time"
31
+ : "it did not answer",
32
+ };
33
+ }
34
+ if (response.status >= 400) {
35
+ void response.body?.cancel().catch(() => {});
36
+ return { error: `it answered ${response.status}` };
37
+ }
38
+ const body = await response.text().catch(() => null);
39
+ if (body === null) return { error: "its answer could not be read" };
40
+ const measure = {
41
+ ...(field ? { field } : {}),
42
+ ...(count ? { count } : {}),
43
+ ...(age ? { age } : {}),
44
+ };
45
+ if (!Object.keys(measure).length)
46
+ return { value: body.trim().split("\n")[0].slice(0, 200) };
47
+ const value = readNumber(body, measure, now());
48
+ return value === null
49
+ ? { error: `it has no ${field ?? count ?? age} to read` }
50
+ : { value };
51
+ }
52
+
53
+ /**
54
+ * The reach source (sources.mjs adapter). `ask(type, input)` asks Harness's server; `ceilingOf(thread)` is what the
55
+ * person let a thread spend, or null.
56
+ */
57
+ export function createReachKind({
58
+ ask,
59
+ ceilingOf = () => null,
60
+ now = () => Date.now(),
61
+ }) {
62
+ const look = (options) => {
63
+ if (options.from === "web") return lookAtWeb(options, now);
64
+ // Billed under the watch's own key: the same watch's looks are one line of the thread's spending.
65
+ const key = createHash("sha256")
66
+ .update(JSON.stringify(options))
67
+ .digest("hex")
68
+ .slice(0, 16);
69
+ const input =
70
+ options.from === "quotient"
71
+ ? {
72
+ from: "quotient",
73
+ market: options.market,
74
+ thread: options.thread,
75
+ watch: key,
76
+ ceilingUsd: ceilingOf(options.thread),
77
+ }
78
+ : {
79
+ from: "bankr",
80
+ holding: options.holding ?? null,
81
+ thread: options.thread ?? null,
82
+ watch: key,
83
+ };
84
+ return Promise.resolve()
85
+ .then(() => ask("reach_read", input))
86
+ .then(
87
+ (answer) =>
88
+ answer?.ok
89
+ ? {
90
+ value: answer.value,
91
+ asOf: answer.asOf ?? null,
92
+ from: answer.source,
93
+ costMicroUsd: answer.costMicroUsd ?? 0,
94
+ }
95
+ : { error: answer?.error ?? "Harness did not answer" },
96
+ (error) => ({ error: `Harness could not be asked: ${error.message}` })
97
+ );
98
+ };
99
+ return {
100
+ describe: (options) =>
101
+ `looks at ${
102
+ options.from === "web"
103
+ ? options.address
104
+ : options.from === "quotient"
105
+ ? `market ${options.market} on Quotient`
106
+ : `your Bankr wallet${options.holding ? `'s ${options.holding}` : ""}`
107
+ } every ${options.everySeconds} s`,
108
+ start(options, io) {
109
+ let stopped = false;
110
+ let timer = null;
111
+ const read = () =>
112
+ look(options).then((result) => {
113
+ if (stopped) return;
114
+ io.report({ at: now(), ...result });
115
+ timer = setTimeout(read, options.everySeconds * 1000);
116
+ timer.unref?.();
117
+ });
118
+ timer = setTimeout(read, 0);
119
+ timer.unref?.();
120
+ return {
121
+ stop() {
122
+ stopped = true;
123
+ clearTimeout(timer);
124
+ },
125
+ };
126
+ },
127
+ };
128
+ }
@@ -0,0 +1,72 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ // What Harness may delete because it can be made again (docs/senses-plan.md S2.6): dependency folders, build output and
5
+ // caches, known by kind and never by git's ignore list, since what agents make can be ignored too (a thread's video
6
+ // takes were). Each kind is recognised by what the tool that made it leaves beside it or inside it.
7
+
8
+ const CACHES = new Set([
9
+ "__pycache__",
10
+ ".pytest_cache",
11
+ ".mypy_cache",
12
+ ".ruff_cache",
13
+ ".turbo",
14
+ ".parcel-cache",
15
+ ]);
16
+
17
+ const has = (folder, name) => fs.existsSync(path.join(folder, name));
18
+
19
+ /** What kind of rebuildable folder this is, or null when it may hold something that cannot be made again. */
20
+ export function rebuildableKind(folder) {
21
+ const name = path.basename(folder);
22
+ const parent = path.dirname(folder);
23
+ if (name === "node_modules" && has(parent, "package.json"))
24
+ return "dependencies";
25
+ if (name === ".next" && has(parent, "package.json")) return "build output";
26
+ if (CACHES.has(name)) return "cache";
27
+ if ((name === ".venv" || name === "venv") && has(folder, "pyvenv.cfg"))
28
+ return "dependencies";
29
+ // The standard mark a tool leaves in a folder that is only a cache (Rust's target, among others).
30
+ if (has(folder, "CACHEDIR.TAG")) return "cache";
31
+ return null;
32
+ }
33
+
34
+ /** Every rebuildable folder under `root`, not looking inside one, nor inside git's own folder. */
35
+ export function rebuildableUnder(root, { depth = 6, most = 50_000 } = {}) {
36
+ const found = [];
37
+ let looked = 0;
38
+ const walk = (folder, level) => {
39
+ if (level > depth || looked > most) return;
40
+ let entries;
41
+ try {
42
+ entries = fs.readdirSync(folder, { withFileTypes: true });
43
+ } catch {
44
+ return;
45
+ }
46
+ for (const entry of entries) {
47
+ looked++;
48
+ if (!entry.isDirectory() || entry.name === ".git") continue;
49
+ const full = path.join(folder, entry.name);
50
+ const kind = rebuildableKind(full);
51
+ if (kind) found.push({ path: full, kind });
52
+ else walk(full, level + 1);
53
+ }
54
+ };
55
+ walk(root, 0);
56
+ return found;
57
+ }
58
+
59
+ /** The bytes a folder or file holds, counted file by file (links not followed). */
60
+ export async function sizeOf(where) {
61
+ let total = 0;
62
+ const walk = async (item) => {
63
+ const stat = await fs.promises.lstat(item).catch(() => null);
64
+ if (!stat) return;
65
+ if (stat.isDirectory()) {
66
+ const entries = await fs.promises.readdir(item).catch(() => []);
67
+ for (const entry of entries) await walk(path.join(item, entry));
68
+ } else total += stat.size;
69
+ };
70
+ await walk(where);
71
+ return total;
72
+ }
package/lib/recall.mjs ADDED
@@ -0,0 +1,216 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { harnessHome } from "./paths.mjs";
5
+ import { recallRoot } from "./senseCall.mjs";
6
+ import { PARENT, SENSE, putSteer } from "./steerQueue.mjs";
7
+
8
+ // Recall (docs/senses-plan.md S4.6): the lesson from last time surfacing unasked when the same thing appears. An agent's
9
+ // hook after a call asks here (senseCall.mjs askRecall) with the shape of an error a tool gave back, or a file the
10
+ // session reads for the first time, once each a session. The daemon looks up the lessons the person taught the swarm
11
+ // (lessons.mjs, the store's full-text index) and the log's past happenings, and hands back only a match, as one note at
12
+ // the session's next step. An error's shape goes in the log as a happening, so the next time it is seen it is known:
13
+ // how often and when it came before, and from which agent.
14
+
15
+ // Words too common in errors to tie one to a lesson.
16
+ const COMMON = new Set(
17
+ `error errors failed failure cannot could not with from that this have been when were what your there their into
18
+ which will would should does done line file files path read write undefined null true false expected received found
19
+ module modules value values type types object property properties function called call command code exit status
20
+ test tests unable invalid unknown missing`
21
+ .split(/\s+/)
22
+ .filter(Boolean)
23
+ );
24
+ const LESSONS_ASKED = 12;
25
+ const LESSONS_GIVEN = 2;
26
+
27
+ const termsOf = (text) =>
28
+ new Set(
29
+ (String(text).match(/[\p{L}\p{N}_]{4,}/gu) ?? [])
30
+ .map((term) => term.toLowerCase())
31
+ .filter((term) => !COMMON.has(term))
32
+ );
33
+ // A code a tool names an error by (ECONNREFUSED, ENOENT), or a named error type (SyntaxError).
34
+ const isCode = (term, shape) =>
35
+ new RegExp(`\\b${term}\\b`, "i").test(shape) &&
36
+ (/^e[a-z]{4,}$/.test(term) || /.+(error|exception)$/.test(term));
37
+
38
+ const ago = (at, now) => {
39
+ const minutes = Math.max(0, Math.round((now - Date.parse(at)) / 60_000));
40
+ if (minutes < 60) return `${Math.max(1, minutes)} min ago`;
41
+ const hours = Math.round(minutes / 60);
42
+ return hours < 48 ? `${hours} h ago` : `${Math.round(hours / 24)} days ago`;
43
+ };
44
+
45
+ /** `store` (lessons, the log's table), `events` (the log), `nameOf(agent)`. `start()` answers asks; `stop()`. */
46
+ export function createRecall({
47
+ store,
48
+ events,
49
+ nameOf = (agent) => agent,
50
+ home = harnessHome,
51
+ now = () => Date.now(),
52
+ log = () => {},
53
+ }) {
54
+ let watcher = null;
55
+ let sweeper = null;
56
+ let sweeping = false;
57
+
58
+ // A lesson that holds here and is for this agent: everywhere, or learned in this folder; about this agent or not
59
+ // about any one agent (lessons.mjs forWork).
60
+ const fits = (lesson, agent, folder) =>
61
+ (lesson.scope === "everywhere" ||
62
+ (folder &&
63
+ lesson.folder &&
64
+ path.resolve(lesson.folder) === path.resolve(folder))) &&
65
+ (lesson.about !== "agent" || lesson.agent === agent);
66
+
67
+ // Lessons that share at least two telling words with an error's shape, or the code it names.
68
+ const lessonsForError = (shape, agent, folder) => {
69
+ const wanted = termsOf(shape);
70
+ return store
71
+ .lessonsMatching(shape, LESSONS_ASKED)
72
+ .filter((lesson) => lesson && fits(lesson, agent, folder))
73
+ .filter((lesson) => {
74
+ const shared = [...termsOf(lesson.text)].filter((term) =>
75
+ wanted.has(term)
76
+ );
77
+ return shared.length >= 2 || shared.some((term) => isCode(term, shape));
78
+ })
79
+ .slice(0, LESSONS_GIVEN);
80
+ };
81
+
82
+ // Lessons that name a file: its path in the folder, or its name with its ending.
83
+ const lessonsForFile = (file, cwd, agent) => {
84
+ const name = path.basename(file);
85
+ const rel = path.relative(cwd || "/", file);
86
+ if (!name.includes(".")) return [];
87
+ return store
88
+ .lessonsMatching(name, LESSONS_ASKED)
89
+ .filter((lesson) => lesson && fits(lesson, agent, cwd))
90
+ .filter((lesson) => {
91
+ const text = lesson.text.toLowerCase();
92
+ return (
93
+ text.includes(rel.toLowerCase()) ||
94
+ new RegExp(
95
+ `(^|[^\\w.])${name.toLowerCase().replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}($|[^\\w])`
96
+ ).test(text)
97
+ );
98
+ })
99
+ .slice(0, LESSONS_GIVEN);
100
+ };
101
+
102
+ // The times an error of this shape came before.
103
+ const pastOf = (id) =>
104
+ store.db.all(
105
+ "select at, data from board_events where source = 'senses' and kind = 'error' and subject = ? order by seq desc limit 50",
106
+ [id]
107
+ );
108
+
109
+ const lessonLine = (lesson, about) =>
110
+ `A lesson ${about} (${lesson.id.slice(0, 8)}, from the person): ${lesson.text}`;
111
+
112
+ const answer = (ask) => {
113
+ const lines = [];
114
+ const at = now();
115
+ if (ask.shape) {
116
+ const id = crypto
117
+ .createHash("sha1")
118
+ .update(ask.shape)
119
+ .digest("hex")
120
+ .slice(0, 12);
121
+ const past = pastOf(id);
122
+ if (past.length) {
123
+ const last = past[0];
124
+ const data = (() => {
125
+ try {
126
+ return JSON.parse(last.data ?? "{}");
127
+ } catch {
128
+ return {};
129
+ }
130
+ })();
131
+ const where =
132
+ data.folder && ask.cwd && data.folder === ask.cwd
133
+ ? " in this folder"
134
+ : "";
135
+ lines.push(
136
+ `This error was seen ${past.length === 1 ? "once" : `${past.length} times`} before, last ${ago(last.at, at)} (${nameOf(data.agent)}${where}).`
137
+ );
138
+ }
139
+ for (const lesson of lessonsForError(ask.shape, ask.agent, ask.cwd))
140
+ lines.push(lessonLine(lesson, "that may fit this error"));
141
+ events.append({
142
+ source: "senses",
143
+ kind: "error",
144
+ subject: id,
145
+ text: ask.shape,
146
+ data: { agent: ask.agent, folder: ask.cwd },
147
+ });
148
+ }
149
+ for (const file of ask.files ?? [])
150
+ for (const lesson of lessonsForFile(file, ask.cwd, ask.agent))
151
+ lines.push(
152
+ lessonLine(lesson, `about ${path.relative(ask.cwd || "/", file)}`)
153
+ );
154
+ if (!lines.length) return;
155
+ putSteer(
156
+ {
157
+ session: ask.session,
158
+ agent: PARENT,
159
+ kind: SENSE,
160
+ text: `Recall: ${lines.join(" ")}`,
161
+ },
162
+ home
163
+ );
164
+ };
165
+
166
+ const sweep = () => {
167
+ if (sweeping) return;
168
+ sweeping = true;
169
+ try {
170
+ let names = [];
171
+ try {
172
+ names = fs
173
+ .readdirSync(recallRoot(home))
174
+ .filter((name) => name.endsWith(".json"))
175
+ .sort();
176
+ } catch {}
177
+ for (const name of names) {
178
+ const file = path.join(recallRoot(home), name);
179
+ let ask = null;
180
+ try {
181
+ ask = JSON.parse(fs.readFileSync(file, "utf8"));
182
+ } catch {}
183
+ fs.rmSync(file, { force: true });
184
+ if (!ask?.session) continue;
185
+ try {
186
+ answer(ask);
187
+ } catch (error) {
188
+ log(`recall: ${error.message}`);
189
+ }
190
+ }
191
+ } finally {
192
+ sweeping = false;
193
+ }
194
+ };
195
+
196
+ return {
197
+ start() {
198
+ fs.mkdirSync(recallRoot(home), { recursive: true, mode: 0o700 });
199
+ sweep();
200
+ try {
201
+ watcher = fs.watch(recallRoot(home), () =>
202
+ setTimeout(sweep, 50).unref?.()
203
+ );
204
+ watcher.on("error", () => {});
205
+ } catch (error) {
206
+ log(`recall: cannot watch for asks: ${error.message}`);
207
+ }
208
+ sweeper = setInterval(sweep, 30_000);
209
+ sweeper.unref?.();
210
+ },
211
+ stop() {
212
+ watcher?.close();
213
+ clearInterval(sweeper);
214
+ },
215
+ };
216
+ }