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.
- package/README.md +21 -19
- package/assets/dsh-plugin.mjs +75 -4
- package/assets/herdr-mod/harness/main.js +367 -0
- package/assets/herdr-mod/harness/mod.json +6 -0
- package/assets/opencode-plugin.js +87 -22
- package/assets/pi-extension.ts +81 -2
- package/cli.mjs +544 -208
- package/lib/adapters.mjs +62 -27
- package/lib/agentBinaries.mjs +6 -8
- package/lib/agentClaims.mjs +168 -0
- package/lib/agentReadTools.json +415 -0
- package/lib/agentSignIn.mjs +17 -18
- package/lib/agentWiring.mjs +114 -0
- package/lib/archetypes.js +10 -11
- package/lib/attention.mjs +100 -0
- package/lib/audit.mjs +1 -1
- package/lib/backupSetup.mjs +6 -28
- package/lib/bankrApprovals.mjs +86 -0
- package/lib/boardAccess.mjs +40 -66
- package/lib/boardBackup.mjs +86 -85
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +29 -36
- package/lib/boardFiles.mjs +83 -15
- package/lib/boardMirror.mjs +78 -11
- package/lib/boardProtocol.json +191 -123
- package/lib/boardThreads.mjs +19 -37
- package/lib/boardTools.mjs +22 -15
- package/lib/boardWatches.mjs +43 -15
- package/lib/bridge.mjs +7 -3
- package/lib/browserAgentTools.mjs +59 -0
- package/lib/browserWake.mjs +0 -6
- package/lib/cards.mjs +52 -0
- package/lib/circling.mjs +189 -0
- package/lib/cleanup.mjs +234 -0
- package/lib/cloudBoot.mjs +21 -4
- package/lib/cloudSave.mjs +46 -5
- package/lib/compaction.mjs +8 -1
- package/lib/completion.mjs +358 -0
- package/lib/computerDevices.mjs +853 -0
- package/lib/connectionStatus.mjs +34 -0
- package/lib/connectors.mjs +139 -1
- package/lib/coordinator.mjs +108 -14
- package/lib/coreModels.mjs +36 -12
- package/lib/daemon.mjs +2426 -338
- package/lib/daemonLock.mjs +103 -0
- package/lib/deviceDoor.mjs +308 -0
- package/lib/deviceIdentity.mjs +114 -0
- package/lib/deviceList.mjs +172 -0
- package/lib/deviceSeal.mjs +74 -0
- package/lib/dispatcher.mjs +497 -26
- package/lib/dispatches.mjs +3 -3
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +67 -41
- package/lib/events.mjs +318 -0
- package/lib/facts.mjs +170 -0
- package/lib/fileSource.mjs +540 -0
- package/lib/files.mjs +47 -2
- package/lib/gitHost.mjs +39 -1
- package/lib/goalCommands.mjs +12 -80
- package/lib/handover.mjs +21 -16
- package/lib/harnessOffers.mjs +140 -15
- package/lib/herdr.mjs +100 -15
- package/lib/herdrAppCall.mjs +19 -0
- package/lib/herdrCore.mjs +59 -0
- package/lib/herdrRuns.mjs +1126 -0
- package/lib/hookDialects.mjs +235 -50
- package/lib/identityLink.mjs +81 -0
- package/lib/inference.mjs +31 -2
- package/lib/jamAccess.mjs +82 -0
- package/lib/{projectBoard.mjs → jamBoard.mjs} +5 -4
- package/lib/{projectGit.mjs → jamGit.mjs} +53 -15
- package/lib/jamKey.mjs +274 -0
- package/lib/{projectPieces.mjs → jamPieces.mjs} +35 -29
- package/lib/{projectSettings.mjs → jamSettings.mjs} +12 -22
- package/lib/jamStore.mjs +973 -0
- package/lib/jamUpgrade.mjs +112 -0
- package/lib/jams.mjs +3079 -0
- package/lib/kanban.mjs +13 -7
- package/lib/landed.mjs +5 -1
- package/lib/library.mjs +41 -22
- package/lib/limits.mjs +91 -0
- package/lib/lineDiff.mjs +97 -0
- package/lib/lineup.mjs +18 -13
- package/lib/link.mjs +584 -0
- package/lib/localStack.mjs +75 -16
- package/lib/localmodels.mjs +58 -78
- package/lib/loopBackstop.mjs +2 -1
- package/lib/machine.mjs +544 -0
- package/lib/machineLink.mjs +446 -0
- package/lib/machineRoutes.mjs +68 -19
- package/lib/machineRuns.mjs +85 -4
- package/lib/machineSeal.mjs +6 -31
- package/lib/manager.mjs +134 -12
- package/lib/mcp-stdio.mjs +492 -215
- package/lib/mcpRelay.mjs +34 -35
- package/lib/members.json +1 -1
- package/lib/modelRoute.mjs +162 -0
- package/lib/nativeHost.mjs +81 -90
- package/lib/nativeSenses.mjs +258 -0
- package/lib/nerves.mjs +523 -0
- package/lib/network.mjs +117 -278
- package/lib/networkPlaces.mjs +578 -0
- package/lib/nextStep.mjs +95 -0
- package/lib/notify.mjs +2 -2
- package/lib/nudge.mjs +116 -10
- package/lib/originals.mjs +216 -0
- package/lib/outsideEvents.mjs +383 -0
- package/lib/outsideRuns.mjs +230 -0
- package/lib/ownMachines.mjs +674 -256
- package/lib/pain.mjs +215 -0
- package/lib/pairing.mjs +91 -11
- package/lib/paths.mjs +42 -3
- package/lib/peerProcess.mjs +21 -7
- package/lib/peopleLink.mjs +268 -0
- package/lib/personNotices.mjs +3 -2
- package/lib/phones.mjs +252 -542
- package/lib/placeCards.mjs +282 -0
- package/lib/placeChecks.mjs +95 -0
- package/lib/placeCommands.mjs +8 -17
- package/lib/placeRequests.mjs +152 -0
- package/lib/placeStore.mjs +295 -0
- package/lib/placeTool.d.ts +12 -0
- package/lib/placeTool.js +40 -0
- package/lib/places.mjs +63 -14
- package/lib/plans.mjs +53 -8
- package/lib/presence.mjs +270 -0
- package/lib/privateNetwork.d.ts +2 -0
- package/lib/privateNetwork.js +93 -0
- package/lib/proposals.mjs +300 -0
- package/lib/publicFetch.d.ts +32 -0
- package/lib/publicFetch.js +248 -0
- package/lib/reach.mjs +128 -0
- package/lib/rebuildable.mjs +72 -0
- package/lib/recall.mjs +216 -0
- package/lib/record.mjs +13 -0
- package/lib/reflexes.mjs +392 -0
- package/lib/relayLimits.mjs +31 -2
- package/lib/releaseNotice.mjs +198 -0
- package/lib/removeHarness.mjs +15 -4
- package/lib/repoLinks.mjs +204 -35
- package/lib/runAuthority.mjs +102 -39
- package/lib/runBrief.mjs +93 -40
- package/lib/runRequests.mjs +52 -14
- package/lib/runUsage.mjs +31 -0
- package/lib/runWorker.mjs +74 -25
- package/lib/runner.mjs +289 -45
- package/lib/runtime-install.mjs +37 -8
- package/lib/schedules.mjs +171 -47
- package/lib/screenWatches.mjs +221 -0
- package/lib/sealed.mjs +12 -30
- package/lib/sealedThreads.mjs +198 -392
- package/lib/secretStore.mjs +178 -0
- package/lib/secrets.mjs +11 -0
- package/lib/seen.mjs +3 -9
- package/lib/selfUpdate.mjs +51 -15
- package/lib/senseCall.mjs +1514 -0
- package/lib/senseHook.mjs +114 -0
- package/lib/senseTools.mjs +1298 -0
- package/lib/sessionBriefs.mjs +87 -0
- package/lib/sessionControl.mjs +63 -13
- package/lib/sessionFiles.mjs +85 -0
- package/lib/setupChoices.mjs +12 -3
- package/lib/setupMode.mjs +70 -25
- package/lib/sharedKeys.mjs +166 -0
- package/lib/sleep.mjs +396 -0
- package/lib/sources.mjs +306 -0
- package/lib/speech.mjs +166 -411
- package/lib/spoken.mjs +210 -0
- package/lib/steerHook.mjs +3 -1
- package/lib/steerQueue.mjs +40 -11
- package/lib/store.mjs +323 -130
- package/lib/stuck.mjs +85 -7
- package/lib/swarm.mjs +157 -49
- package/lib/swarmDevices.mjs +25 -85
- package/lib/swarmMemory.mjs +93 -9
- package/lib/t3.mjs +73 -3
- package/lib/taskCommands.mjs +51 -60
- package/lib/taskRooms.mjs +30 -4
- package/lib/taskRules.json +29 -0
- package/lib/taskRules.mjs +21 -0
- package/lib/taskRuns.mjs +74 -11
- package/lib/team.mjs +2 -1
- package/lib/threadActions.mjs +37 -9
- package/lib/threadAuthority.mjs +4 -4
- package/lib/threadBriefs.mjs +173 -43
- package/lib/threadNotes.mjs +320 -0
- package/lib/threadRecord.mjs +491 -0
- package/lib/threadRuns.mjs +216 -108
- package/lib/threadView.mjs +27 -30
- package/lib/threadWatches.mjs +1962 -0
- package/lib/timeSense.mjs +839 -0
- package/lib/tripwires.mjs +454 -0
- package/lib/ui.mjs +1 -1
- package/lib/vault.mjs +560 -45
- package/lib/vaultCommands.mjs +5 -3
- package/lib/vaultPeers.mjs +165 -0
- package/lib/vaultTools.d.ts +4 -1
- package/lib/vaultTools.js +22 -1
- package/lib/verdicts.mjs +12 -26
- package/lib/versions.mjs +117 -0
- package/lib/waitTool.mjs +278 -0
- package/lib/wakePicture.mjs +268 -0
- package/lib/wakeQueue.mjs +108 -0
- package/lib/watchResponses.mjs +533 -0
- package/lib/watches.mjs +290 -145
- package/lib/windowAsks.mjs +80 -17
- package/lib/worktrees.mjs +159 -9
- package/npm-shrinkwrap.json +138 -631
- package/package.json +7 -6
- package/release.json +211 -129
- package/lib/callSessions.mjs +0 -51
- package/lib/directLink.mjs +0 -349
- package/lib/peopleBoard.mjs +0 -131
- package/lib/projects.mjs +0 -2917
- package/lib/roomChannels.mjs +0 -667
package/lib/dispatches.mjs
CHANGED
|
@@ -57,7 +57,7 @@ export function createDispatches({
|
|
|
57
57
|
guard,
|
|
58
58
|
notifyBoard,
|
|
59
59
|
inferenceRefusal = () => null,
|
|
60
|
-
|
|
60
|
+
jamRoom = () => false,
|
|
61
61
|
}) {
|
|
62
62
|
const snapshot = (task) => ({
|
|
63
63
|
id: task.id,
|
|
@@ -97,7 +97,7 @@ export function createDispatches({
|
|
|
97
97
|
!["open", "running"].includes(root.status) ||
|
|
98
98
|
digest(rootSource(root)) !== scope.root_digest ||
|
|
99
99
|
store.getThread(root.id) ||
|
|
100
|
-
|
|
100
|
+
jamRoom(root.id)
|
|
101
101
|
)
|
|
102
102
|
return "The owner objective changed or is no longer active.";
|
|
103
103
|
try {
|
|
@@ -213,7 +213,7 @@ export function createDispatches({
|
|
|
213
213
|
root.parent_id ||
|
|
214
214
|
!["open", "running"].includes(root.status) ||
|
|
215
215
|
store.getThread(root.id) ||
|
|
216
|
-
|
|
216
|
+
jamRoom(root.id)
|
|
217
217
|
)
|
|
218
218
|
return { ok: false, code: "owner_root_required" };
|
|
219
219
|
return store.dispatchTransaction(() => {
|
package/lib/environment.mjs
CHANGED
|
@@ -46,7 +46,8 @@ function* personSteps(platform) {
|
|
|
46
46
|
)?.[1] ?? null)
|
|
47
47
|
: null;
|
|
48
48
|
const idle = (yield [
|
|
49
|
-
|
|
49
|
+
// By its whole path: it lives in /usr/sbin, which a daemon started by the app's launcher does not have on its PATH.
|
|
50
|
+
"/usr/sbin/ioreg",
|
|
50
51
|
["-c", "IOHIDSystem", "-d", "4", "-r", "-k", "HIDIdleTime"],
|
|
51
52
|
])?.match(/"HIDIdleTime"\s*=\s*(\d+)/)?.[1];
|
|
52
53
|
return { app, idleSeconds: idle ? Math.round(Number(idle) / 1e9) : null };
|
package/lib/environments.mjs
CHANGED
|
@@ -2,7 +2,8 @@ import fs from "node:fs";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { detectAgents } from "./commands.mjs";
|
|
4
4
|
import { member, nameOf } from "./members.mjs";
|
|
5
|
-
import { t3Access, t3Step, t3Tool } from "./t3.mjs";
|
|
5
|
+
import { sendFilesToT3, t3Access, t3Step, t3Tool } from "./t3.mjs";
|
|
6
|
+
import { namedSessionFiles, withSavedPaths } from "./sessionFiles.mjs";
|
|
6
7
|
import { t3Checkouts, t3Committers, t3Link } from "./places.mjs";
|
|
7
8
|
|
|
8
9
|
const DRIVER_AGENT = {
|
|
@@ -324,24 +325,51 @@ export function createEnvironments({
|
|
|
324
325
|
})();
|
|
325
326
|
return refreshing;
|
|
326
327
|
};
|
|
328
|
+
// A thread's items in order, from `after` (or its first item) to its last, and T3's newest answer about the thread.
|
|
329
|
+
// T3 pages them: a page ends at the count asked for or, since November 13 (#33455), at about 30,000 bytes. A read
|
|
330
|
+
// with no position starts at the first item on a T3 before that date and is the newest page since (#33428), which
|
|
331
|
+
// says so with `earlierPosition`: the rest is read back from there. Older T3 refuses `afterPosition: -1`.
|
|
332
|
+
async function readItems(args, after = null) {
|
|
333
|
+
const page = (position) =>
|
|
334
|
+
t3Tool("t3_thread_read", { ...args, limit: 100, ...position });
|
|
335
|
+
// Every page moves on from the last, or the read is not all of the thread and nothing is made of it.
|
|
336
|
+
const moved = (to, from, onward) => {
|
|
337
|
+
if (!Number.isInteger(to) || (from !== null && !onward(to, from)))
|
|
338
|
+
throw new Error("T3 Code returned an incomplete page of a thread.");
|
|
339
|
+
return to;
|
|
340
|
+
};
|
|
341
|
+
const first = await page(after === null ? {} : { afterPosition: after });
|
|
342
|
+
const items = [...(first.items ?? [])];
|
|
343
|
+
let earlier = after === null ? (first.earlierPosition ?? null) : null;
|
|
344
|
+
while (earlier !== null) {
|
|
345
|
+
const back = await page({ beforePosition: earlier });
|
|
346
|
+
items.unshift(...(back.items ?? []));
|
|
347
|
+
earlier =
|
|
348
|
+
back.earlierPosition == null
|
|
349
|
+
? null
|
|
350
|
+
: moved(back.earlierPosition, earlier, (to, from) => to < from);
|
|
351
|
+
}
|
|
352
|
+
let latest = first;
|
|
353
|
+
let at = after;
|
|
354
|
+
while (latest.hasMore === true) {
|
|
355
|
+
at = moved(latest.nextPosition, at, (to, from) => to > from);
|
|
356
|
+
latest = await page({ afterPosition: at });
|
|
357
|
+
items.push(...(latest.items ?? []));
|
|
358
|
+
}
|
|
359
|
+
return { items, latest };
|
|
360
|
+
}
|
|
327
361
|
// The thread's newest items, as far back as the person's last message: it and the agent's message it answers.
|
|
328
362
|
async function lastExchange(entry) {
|
|
329
|
-
const items =
|
|
330
|
-
|
|
331
|
-
for (let page = 0; page < EXCHANGE_ITEMS / 100; page++) {
|
|
332
|
-
const read = await t3Tool("t3_thread_read", {
|
|
363
|
+
const { items } = await readItems(
|
|
364
|
+
{
|
|
333
365
|
threadId: entry.threadId,
|
|
334
366
|
view: "messages",
|
|
335
|
-
limit: 100,
|
|
336
367
|
// An agent's `Deciding:` line ends its message: room for all of it.
|
|
337
368
|
maxCharsPerItem: 20_000,
|
|
338
369
|
runLimit: 1,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
if (!read.hasMore || read.nextPosition === after) break;
|
|
343
|
-
after = read.nextPosition;
|
|
344
|
-
}
|
|
370
|
+
},
|
|
371
|
+
Math.max(0, (entry.itemCount ?? 0) - EXCHANGE_ITEMS) || null
|
|
372
|
+
);
|
|
345
373
|
const said = items.filter((item) =>
|
|
346
374
|
["user_message", "assistant_message"].includes(item.type)
|
|
347
375
|
);
|
|
@@ -514,32 +542,12 @@ export function createEnvironments({
|
|
|
514
542
|
const posts = [];
|
|
515
543
|
const timeline = [];
|
|
516
544
|
for (const entry of selected.values()) {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const page = await t3Tool("t3_thread_read", {
|
|
524
|
-
threadId: entry.threadId,
|
|
525
|
-
view: "activity",
|
|
526
|
-
limit: 100,
|
|
527
|
-
maxCharsPerItem: 12_000,
|
|
528
|
-
runLimit: 5,
|
|
529
|
-
...(after === null ? {} : { afterPosition: after }),
|
|
530
|
-
});
|
|
531
|
-
detail = page;
|
|
532
|
-
items.push(...(page.items ?? []));
|
|
533
|
-
more = page.hasMore === true;
|
|
534
|
-
if (
|
|
535
|
-
more &&
|
|
536
|
-
(!Number.isInteger(page.nextPosition) ||
|
|
537
|
-
positions.has(page.nextPosition))
|
|
538
|
-
)
|
|
539
|
-
throw new Error("T3 Code returned an incomplete activity page.");
|
|
540
|
-
positions.add(page.nextPosition);
|
|
541
|
-
after = page.nextPosition;
|
|
542
|
-
}
|
|
545
|
+
const { items, latest: detail } = await readItems({
|
|
546
|
+
threadId: entry.threadId,
|
|
547
|
+
view: "activity",
|
|
548
|
+
maxCharsPerItem: 12_000,
|
|
549
|
+
runLimit: 5,
|
|
550
|
+
});
|
|
543
551
|
const actual = { ...entry, ...detail.thread };
|
|
544
552
|
if (
|
|
545
553
|
!actual.activeRunId &&
|
|
@@ -696,14 +704,22 @@ export function createEnvironments({
|
|
|
696
704
|
if (!["send", "steer", "stop"].includes(action))
|
|
697
705
|
return { ok: false, error: "Choose send, steer or stop." };
|
|
698
706
|
const text = String(request.text || "").trim();
|
|
699
|
-
|
|
707
|
+
const { files, error: filesError } = namedSessionFiles(request.files);
|
|
708
|
+
if (filesError) return { ok: false, error: filesError };
|
|
709
|
+
if (action !== "stop" && ((!text && !files.length) || text.length > 50_000))
|
|
700
710
|
return {
|
|
701
711
|
ok: false,
|
|
702
712
|
error: "Write a message of at most 50,000 characters.",
|
|
703
713
|
};
|
|
704
714
|
const identity = createHash("sha256")
|
|
705
715
|
.update(
|
|
706
|
-
JSON.stringify({
|
|
716
|
+
JSON.stringify({
|
|
717
|
+
id,
|
|
718
|
+
action,
|
|
719
|
+
text,
|
|
720
|
+
runId: request.runId ?? null,
|
|
721
|
+
files: files.map((file) => file.id),
|
|
722
|
+
})
|
|
707
723
|
)
|
|
708
724
|
.digest("hex");
|
|
709
725
|
const receiptKey = `outside-control:${t3Access().environmentId}:${requestId}`;
|
|
@@ -770,11 +786,21 @@ export function createEnvironments({
|
|
|
770
786
|
})),
|
|
771
787
|
};
|
|
772
788
|
}
|
|
789
|
+
if (files.length) {
|
|
790
|
+
// T3's own attachment; if T3 refuses that, the agent there still gets where the file is saved on this computer.
|
|
791
|
+
const delivery = await sendFilesToT3(entry.threadId, text, files).catch(
|
|
792
|
+
(error) => {
|
|
793
|
+
if (error.code !== "refused") throw error;
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
);
|
|
797
|
+
if (delivery) return { ok: true, delivery };
|
|
798
|
+
}
|
|
773
799
|
return {
|
|
774
800
|
ok: true,
|
|
775
801
|
...(await t3Tool("t3_thread_send", {
|
|
776
802
|
threadId: entry.threadId,
|
|
777
|
-
message: text,
|
|
803
|
+
message: withSavedPaths(text, files),
|
|
778
804
|
mode: action === "steer" ? "steer" : "auto",
|
|
779
805
|
clientRequestId: requestId,
|
|
780
806
|
})),
|
package/lib/events.mjs
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// The log: what happened on this computer, each happening kept once, numbered in order and never rewritten
|
|
2
|
+
// (docs/senses-plan.md, "How the parts fit"). A happening is something that occurred: a step the local stack took, a
|
|
3
|
+
// time that came, a watch going off, a source that can no longer read. What is true now is not a happening but a
|
|
4
|
+
// state: a source keeps its latest reading itself (sources.mjs), and what every computer should read the same is a fact
|
|
5
|
+
// (facts.mjs). The log is kept on each computer: the board copy does not carry it (boardMirror.mjs) and nothing in it
|
|
6
|
+
// goes to Harness's server. What another of the person's computers asked for arrives over the nerves between them
|
|
7
|
+
// (nerves.mjs, S6) and is kept here too, under the computer it happened on and its number there, once.
|
|
8
|
+
|
|
9
|
+
// How long a happening is kept (owner, November 12: 30 days). A real day on the owner's computer makes a few thousand
|
|
10
|
+
// (docs/senses-plan.md S0.7), so the log stays small.
|
|
11
|
+
const KEEP_DAYS = 30;
|
|
12
|
+
const DAY_MS = 86_400_000;
|
|
13
|
+
// And at most 100 MB (owner, November 12), so a source that runs away cannot grow it: 350,000 happenings at the 282
|
|
14
|
+
// bytes each measured with this table (docs/senses-plan.md S2). Counted at the start and every thousand written.
|
|
15
|
+
const KEEP_ROWS = 350_000;
|
|
16
|
+
const COUNT_EVERY = 1_000;
|
|
17
|
+
// What one read hands back at most; a reader that wants more reads again from the last one it got.
|
|
18
|
+
const READ_LIMIT = 100;
|
|
19
|
+
|
|
20
|
+
export const EVENT_TABLES = `
|
|
21
|
+
create table if not exists board_events (
|
|
22
|
+
seq integer primary key autoincrement,
|
|
23
|
+
at text not null,
|
|
24
|
+
source text not null,
|
|
25
|
+
kind text not null,
|
|
26
|
+
subject text,
|
|
27
|
+
text text not null default '',
|
|
28
|
+
ms integer,
|
|
29
|
+
data text,
|
|
30
|
+
cause integer,
|
|
31
|
+
hops integer not null default 0,
|
|
32
|
+
trust text not null default 'owner'
|
|
33
|
+
);
|
|
34
|
+
create index if not exists board_events_at on board_events(at);
|
|
35
|
+
create index if not exists board_events_source on board_events(source, seq);
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
// A happening that arrived from another computer (nerves.mjs): the computer it happened on and its number there. A
|
|
39
|
+
// happening of this computer has neither. Added to a log from before S6.
|
|
40
|
+
export const EVENT_ARRIVALS = `
|
|
41
|
+
create unique index if not exists board_events_origin on board_events(machine, origin) where machine is not null;
|
|
42
|
+
`;
|
|
43
|
+
export function addArrivalColumns(db) {
|
|
44
|
+
const columns = new Set(
|
|
45
|
+
db.all("pragma table_info(board_events)").map((column) => column.name)
|
|
46
|
+
);
|
|
47
|
+
if (!columns.has("machine"))
|
|
48
|
+
db.exec("alter table board_events add column machine text");
|
|
49
|
+
if (!columns.has("origin"))
|
|
50
|
+
db.exec("alter table board_events add column origin integer");
|
|
51
|
+
db.exec(EVENT_ARRIVALS);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const happeningOf = (row) => ({
|
|
55
|
+
seq: Number(row.seq),
|
|
56
|
+
at: row.at,
|
|
57
|
+
source: row.source,
|
|
58
|
+
kind: row.kind,
|
|
59
|
+
subject: row.subject ?? null,
|
|
60
|
+
text: row.text,
|
|
61
|
+
...(row.ms === null ? {} : { ms: Number(row.ms) }),
|
|
62
|
+
data: row.data ? JSON.parse(row.data) : null,
|
|
63
|
+
cause: row.cause === null ? null : Number(row.cause),
|
|
64
|
+
hops: Number(row.hops),
|
|
65
|
+
trust: row.trust,
|
|
66
|
+
// Where it happened, for one that arrived from another computer (nerves.mjs), and its number there.
|
|
67
|
+
...(row.machine ? { machine: row.machine, origin: Number(row.origin) } : {}),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The log, in the board's store on this computer: `append` records a happening, `read` gives happenings after a number
|
|
72
|
+
* or a time. `maxHops`: how long a chain of
|
|
73
|
+
* happenings, each caused by the one before, may grow before the next is left out (the daemon gives the limit on work
|
|
74
|
+
* agents hand on without the person, runAuthority.mjs MAX_HANDOFFS, so a loop between sources and what answers them
|
|
75
|
+
* cannot run on its own).
|
|
76
|
+
*/
|
|
77
|
+
export function createEvents({
|
|
78
|
+
store,
|
|
79
|
+
maxHops,
|
|
80
|
+
log = () => {},
|
|
81
|
+
now = () => Date.now(),
|
|
82
|
+
}) {
|
|
83
|
+
const db = store.db;
|
|
84
|
+
const oldestAt = () =>
|
|
85
|
+
db.get("select min(at) as at from board_events")?.at ?? null;
|
|
86
|
+
let oldest = oldestAt();
|
|
87
|
+
// Told the last number about to be forgotten, before it goes: sleep folds what it has not yet (sleep.mjs).
|
|
88
|
+
let beforeForget = () => {};
|
|
89
|
+
const forgetting = (throughSeq) => {
|
|
90
|
+
try {
|
|
91
|
+
beforeForget(throughSeq);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
log(`log: folding before forgetting failed: ${error.message}`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// At most KEEP_ROWS, the oldest going first: at the first happening written and every COUNT_EVERY after.
|
|
98
|
+
const keepToCap = () => {
|
|
99
|
+
const past = db.get(
|
|
100
|
+
"select seq from board_events order by seq desc limit 1 offset ?",
|
|
101
|
+
[KEEP_ROWS]
|
|
102
|
+
);
|
|
103
|
+
if (!past) return;
|
|
104
|
+
forgetting(past.seq);
|
|
105
|
+
db.run("delete from board_events where seq <= ?", [past.seq]);
|
|
106
|
+
oldest = oldestAt();
|
|
107
|
+
};
|
|
108
|
+
// Forgetting is checked as each happening is written, so the log needs no timer of its own.
|
|
109
|
+
let written = 0;
|
|
110
|
+
const forgetOld = () => {
|
|
111
|
+
const cutoff = new Date(now() - KEEP_DAYS * DAY_MS).toISOString();
|
|
112
|
+
if (oldest && oldest < cutoff) {
|
|
113
|
+
const last = db.get(
|
|
114
|
+
"select max(seq) as seq from board_events where at < ?",
|
|
115
|
+
[cutoff]
|
|
116
|
+
)?.seq;
|
|
117
|
+
if (last) forgetting(last);
|
|
118
|
+
db.run("delete from board_events where at < ?", [cutoff]);
|
|
119
|
+
oldest = oldestAt();
|
|
120
|
+
}
|
|
121
|
+
// The first write after a start counts too, once what folds the log before it forgets is listening.
|
|
122
|
+
if (written++ % COUNT_EVERY === 0) keepToCap();
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// A happening's place in its chain: one more than its cause's, or the start of a chain when it has none. A cause the
|
|
126
|
+
// log has already forgotten counts as one step, never as a fresh start.
|
|
127
|
+
const hopsAfter = (cause) => {
|
|
128
|
+
if (cause === null) return 0;
|
|
129
|
+
const row = db.get("select hops from board_events where seq = ?", [cause]);
|
|
130
|
+
return row ? Number(row.hops) + 1 : 1;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Who hears each happening as it is written (pain told at once, atWork.mjs). Most hear only this computer's: another
|
|
134
|
+
// computer's disk is not this one's pain. The nerves and the readers of a thread hear what arrives too.
|
|
135
|
+
const followers = new Set();
|
|
136
|
+
const everywhere = new Set();
|
|
137
|
+
const tell = (kept, who) => {
|
|
138
|
+
for (const follower of who)
|
|
139
|
+
try {
|
|
140
|
+
follower(kept);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
log(`log: a follower failed: ${error.message}`);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
/** What is told the last number about to be forgotten, before the log forgets (sleep.mjs fold). */
|
|
148
|
+
onForget(fold) {
|
|
149
|
+
beforeForget = fold;
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* Hears each happening of this computer as it is kept, and with `{ arrivals: true }` each one that arrives from
|
|
153
|
+
* another computer too; returns what stops it. A follower that throws is left out of that one.
|
|
154
|
+
*/
|
|
155
|
+
follow(onHappening, { arrivals = false } = {}) {
|
|
156
|
+
const set = arrivals ? everywhere : followers;
|
|
157
|
+
set.add(onHappening);
|
|
158
|
+
return () => set.delete(onHappening);
|
|
159
|
+
},
|
|
160
|
+
/**
|
|
161
|
+
* Records a happening: `source` (who reports it), `kind`, the `subject` it is about, a `text` that holds no one's
|
|
162
|
+
* words, `ms` it took, small `data`, the `cause` (the number of the happening that led to it) and `trust` ("owner"
|
|
163
|
+
* for what comes from the person's own computer, "untrusted" for what came from outside). The happening as kept,
|
|
164
|
+
* or null when its chain is already too long.
|
|
165
|
+
*/
|
|
166
|
+
append({
|
|
167
|
+
source,
|
|
168
|
+
kind,
|
|
169
|
+
subject = null,
|
|
170
|
+
text = "",
|
|
171
|
+
ms = null,
|
|
172
|
+
data = null,
|
|
173
|
+
cause = null,
|
|
174
|
+
trust = "owner",
|
|
175
|
+
}) {
|
|
176
|
+
const hops = hopsAfter(cause);
|
|
177
|
+
if (hops > maxHops) {
|
|
178
|
+
log(
|
|
179
|
+
`log: left out ${source} ${kind}: it would be ${hops} steps from what started it`
|
|
180
|
+
);
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
forgetOld();
|
|
184
|
+
const at = new Date(now()).toISOString();
|
|
185
|
+
const values = [
|
|
186
|
+
at,
|
|
187
|
+
String(source),
|
|
188
|
+
String(kind),
|
|
189
|
+
subject === null ? null : String(subject),
|
|
190
|
+
String(text),
|
|
191
|
+
ms === null ? null : Math.round(ms),
|
|
192
|
+
data === null ? null : JSON.stringify(data),
|
|
193
|
+
cause,
|
|
194
|
+
hops,
|
|
195
|
+
trust === "untrusted" ? "untrusted" : "owner",
|
|
196
|
+
];
|
|
197
|
+
const { lastInsertRowid } = db.run(
|
|
198
|
+
"insert into board_events(at, source, kind, subject, text, ms, data, cause, hops, trust) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
199
|
+
values
|
|
200
|
+
);
|
|
201
|
+
oldest ??= at;
|
|
202
|
+
const kept = happeningOf({
|
|
203
|
+
seq: lastInsertRowid,
|
|
204
|
+
at,
|
|
205
|
+
source: values[1],
|
|
206
|
+
kind: values[2],
|
|
207
|
+
subject: values[3],
|
|
208
|
+
text: values[4],
|
|
209
|
+
ms: values[5],
|
|
210
|
+
data: values[6],
|
|
211
|
+
cause,
|
|
212
|
+
hops,
|
|
213
|
+
trust: values[9],
|
|
214
|
+
});
|
|
215
|
+
tell(kept, followers);
|
|
216
|
+
tell(kept, everywhere);
|
|
217
|
+
return kept;
|
|
218
|
+
},
|
|
219
|
+
/**
|
|
220
|
+
* Keeps a happening another computer sent (nerves.mjs): `machine` is where it happened and `origin` its number
|
|
221
|
+
* there; `cause` names its cause the same way (`{ machine, origin }`, or this computer's own number), found here
|
|
222
|
+
* when it is still kept. Its time,
|
|
223
|
+
* chain and trust are kept as it happened. Null for one already here: a repeat is ignored.
|
|
224
|
+
*/
|
|
225
|
+
receive({
|
|
226
|
+
machine,
|
|
227
|
+
origin,
|
|
228
|
+
at,
|
|
229
|
+
source,
|
|
230
|
+
kind,
|
|
231
|
+
subject = null,
|
|
232
|
+
text = "",
|
|
233
|
+
ms = null,
|
|
234
|
+
data = null,
|
|
235
|
+
cause = null,
|
|
236
|
+
hops = 0,
|
|
237
|
+
trust = "owner",
|
|
238
|
+
}) {
|
|
239
|
+
if (!machine || !Number.isSafeInteger(Number(origin))) return null;
|
|
240
|
+
forgetOld();
|
|
241
|
+
// Its cause: one of this computer's own by its number here, or one that arrived too.
|
|
242
|
+
const causeHere =
|
|
243
|
+
typeof cause === "number"
|
|
244
|
+
? (db.get("select seq from board_events where seq = ?", [cause])
|
|
245
|
+
?.seq ?? null)
|
|
246
|
+
: cause?.machine
|
|
247
|
+
? (db.get(
|
|
248
|
+
"select seq from board_events where machine = ? and origin = ?",
|
|
249
|
+
[String(cause.machine), Number(cause.origin)]
|
|
250
|
+
)?.seq ?? null)
|
|
251
|
+
: null;
|
|
252
|
+
const values = [
|
|
253
|
+
String(at),
|
|
254
|
+
String(source),
|
|
255
|
+
String(kind),
|
|
256
|
+
subject === null ? null : String(subject),
|
|
257
|
+
String(text),
|
|
258
|
+
ms === null ? null : Math.round(ms),
|
|
259
|
+
data === null ? null : JSON.stringify(data),
|
|
260
|
+
causeHere,
|
|
261
|
+
Math.max(0, Number(hops) || 0),
|
|
262
|
+
trust === "untrusted" ? "untrusted" : "owner",
|
|
263
|
+
String(machine),
|
|
264
|
+
Number(origin),
|
|
265
|
+
];
|
|
266
|
+
const { changes, lastInsertRowid } = db.run(
|
|
267
|
+
"insert or ignore into board_events(at, source, kind, subject, text, ms, data, cause, hops, trust, machine, origin) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
268
|
+
values
|
|
269
|
+
);
|
|
270
|
+
if (!changes) return null;
|
|
271
|
+
oldest ??= values[0];
|
|
272
|
+
const kept = happeningOf({
|
|
273
|
+
seq: lastInsertRowid,
|
|
274
|
+
at: values[0],
|
|
275
|
+
source: values[1],
|
|
276
|
+
kind: values[2],
|
|
277
|
+
subject: values[3],
|
|
278
|
+
text: values[4],
|
|
279
|
+
ms: values[5],
|
|
280
|
+
data: values[6],
|
|
281
|
+
cause: causeHere,
|
|
282
|
+
hops: values[8],
|
|
283
|
+
trust: values[9],
|
|
284
|
+
machine: values[10],
|
|
285
|
+
origin: values[11],
|
|
286
|
+
});
|
|
287
|
+
tell(kept, everywhere);
|
|
288
|
+
return kept;
|
|
289
|
+
},
|
|
290
|
+
/** One happening by its number, or null when the log has forgotten it. */
|
|
291
|
+
get(seq) {
|
|
292
|
+
const row = db.get("select * from board_events where seq = ?", [
|
|
293
|
+
Number(seq),
|
|
294
|
+
]);
|
|
295
|
+
return row ? happeningOf(row) : null;
|
|
296
|
+
},
|
|
297
|
+
/**
|
|
298
|
+
* Happenings oldest first: after the number `after`, or after the time `since` (ISO), from the named `sources`
|
|
299
|
+
* only when given, at most `limit`. With neither, the newest: a reader new to the log starts at its end.
|
|
300
|
+
*/
|
|
301
|
+
read({ after = 0, since = "", sources = null, limit = READ_LIMIT } = {}) {
|
|
302
|
+
const named = sources?.length
|
|
303
|
+
? ` and source in (${sources.map(() => "?").join(", ")})`
|
|
304
|
+
: "";
|
|
305
|
+
const fromEnd = !(Number(after) > 0) && !since;
|
|
306
|
+
const rows = db.all(
|
|
307
|
+
`select * from board_events where seq > ? and at > ?${named} order by seq ${fromEnd ? "desc" : "asc"} limit ?`,
|
|
308
|
+
[
|
|
309
|
+
Number(after) || 0,
|
|
310
|
+
String(since || ""),
|
|
311
|
+
...(sources?.length ? sources.map(String) : []),
|
|
312
|
+
Math.min(Number(limit) || READ_LIMIT, READ_LIMIT),
|
|
313
|
+
]
|
|
314
|
+
);
|
|
315
|
+
return (fromEnd ? rows.reverse() : rows).map(happeningOf);
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
package/lib/facts.mjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Facts: what is true now, each with where it came from and since when, the same on every computer of the person
|
|
2
|
+
// (docs/senses-plan.md, "How the parts fit"). The board copy carries them (boardMirror.mjs MIRRORED), so a worker reads
|
|
3
|
+
// home's facts from its copy, and a fact is written on home alone: a worker's board command goes there
|
|
4
|
+
// (machineRoutes.mjs). A fact about one computer names it; one about the whole swarm names none.
|
|
5
|
+
//
|
|
6
|
+
// A fact is written only when what it says changes, never on every reading: each one is copied to every computer and
|
|
7
|
+
// moves every open page. So its age is how long it has said what it says.
|
|
8
|
+
|
|
9
|
+
// A fact is small: at most what a check-in's words or a watch's command may be (schedules.mjs, watches.mjs).
|
|
10
|
+
const VALUE_CHARS = 2_000;
|
|
11
|
+
const KEY_CHARS = 200;
|
|
12
|
+
|
|
13
|
+
export const FACT_TABLES = `
|
|
14
|
+
create table if not exists board_facts (
|
|
15
|
+
key text not null,
|
|
16
|
+
machine text not null default '',
|
|
17
|
+
value text not null,
|
|
18
|
+
source text not null,
|
|
19
|
+
at text not null,
|
|
20
|
+
hops integer not null default 0,
|
|
21
|
+
primary key(key, machine)
|
|
22
|
+
);
|
|
23
|
+
`;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The facts in the board's store. `get(key, { machine })` is a fact as it stands, with its age, or null; `set(key,
|
|
27
|
+
* value, { source, machine, hops })` says it, and changes nothing when it already says that. `hops`: how many steps of
|
|
28
|
+
* a chain of watches' responses led to it (watchResponses.mjs), 0 for anything else; the copy carries it to every
|
|
29
|
+
* computer, so a chain that crosses computers is counted whole. `onSet(listener)` hears each fact this computer writes,
|
|
30
|
+
* so a watch here reads it at once rather than at its next look.
|
|
31
|
+
*/
|
|
32
|
+
export function createFacts({ store, now = () => Date.now() }) {
|
|
33
|
+
const db = store.db;
|
|
34
|
+
const listeners = new Set();
|
|
35
|
+
const factOf = (row) =>
|
|
36
|
+
row && {
|
|
37
|
+
key: row.key,
|
|
38
|
+
machine: row.machine || null,
|
|
39
|
+
value: JSON.parse(row.value),
|
|
40
|
+
source: row.source,
|
|
41
|
+
at: row.at,
|
|
42
|
+
hops: Number(row.hops ?? 0),
|
|
43
|
+
ageSeconds: Math.max(0, Math.round((now() - Date.parse(row.at)) / 1000)),
|
|
44
|
+
};
|
|
45
|
+
const rowOf = (key, machine) =>
|
|
46
|
+
db.get("select * from board_facts where key = ? and machine = ?", [
|
|
47
|
+
String(key),
|
|
48
|
+
machine ? String(machine) : "",
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const get = (key, { machine = null } = {}) => factOf(rowOf(key, machine));
|
|
52
|
+
|
|
53
|
+
const set = (key, value, { source, machine = null, hops = 0 }) => {
|
|
54
|
+
const name = String(key ?? "").trim();
|
|
55
|
+
if (!name || name.length > KEY_CHARS)
|
|
56
|
+
return {
|
|
57
|
+
ok: false,
|
|
58
|
+
error: `a fact needs a name of at most ${KEY_CHARS} characters`,
|
|
59
|
+
};
|
|
60
|
+
const said = JSON.stringify(value ?? null);
|
|
61
|
+
if (said.length > VALUE_CHARS)
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
error: `a fact says at most ${VALUE_CHARS} characters`,
|
|
65
|
+
};
|
|
66
|
+
const existing = rowOf(name, machine);
|
|
67
|
+
if (existing?.value === said) return { ok: true, fact: factOf(existing) };
|
|
68
|
+
db.run(
|
|
69
|
+
"insert into board_facts(key, machine, value, source, at, hops) values(?, ?, ?, ?, ?, ?) on conflict(key, machine) do update set value = excluded.value, source = excluded.source, at = excluded.at, hops = excluded.hops",
|
|
70
|
+
[
|
|
71
|
+
name,
|
|
72
|
+
machine ? String(machine) : "",
|
|
73
|
+
said,
|
|
74
|
+
String(source),
|
|
75
|
+
new Date(now()).toISOString(),
|
|
76
|
+
Math.max(0, Math.round(Number(hops) || 0)),
|
|
77
|
+
]
|
|
78
|
+
);
|
|
79
|
+
for (const listener of listeners) listener(name, machine || null);
|
|
80
|
+
return { ok: true, fact: factOf(rowOf(name, machine)) };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
get,
|
|
85
|
+
set,
|
|
86
|
+
onSet(listener) {
|
|
87
|
+
listeners.add(listener);
|
|
88
|
+
return () => listeners.delete(listener);
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* The board's fact commands, from the person or an agent on one of the person's computers (`machine`: the computer
|
|
92
|
+
* the call came from): `board.fact.get` { key, computer } reads one, about a computer (its id, or true for the
|
|
93
|
+
* caller's own) or the swarm; `board.fact.set` { key, value, computer } says one, about the caller's own computer
|
|
94
|
+
* when `computer` is true, the swarm otherwise. Never a hosted caller: facts stay on the person's computers.
|
|
95
|
+
*/
|
|
96
|
+
command(request, { agent, machine }) {
|
|
97
|
+
if (request.hostedCaller)
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: "facts are only for the person and agents on their computers",
|
|
101
|
+
};
|
|
102
|
+
const about =
|
|
103
|
+
request.computer === true
|
|
104
|
+
? machine
|
|
105
|
+
: typeof request.computer === "string"
|
|
106
|
+
? request.computer
|
|
107
|
+
: null;
|
|
108
|
+
if (request.command === "board.fact.get")
|
|
109
|
+
return {
|
|
110
|
+
ok: true,
|
|
111
|
+
fact: get(String(request.key ?? ""), { machine: about }),
|
|
112
|
+
};
|
|
113
|
+
if (request.command === "board.fact.set")
|
|
114
|
+
return set(request.key, request.value, {
|
|
115
|
+
source: agent,
|
|
116
|
+
machine: request.computer === true ? machine : null,
|
|
117
|
+
});
|
|
118
|
+
return { ok: false, error: `unknown command ${request.command}` };
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// How often a fact a watch follows is read again: a read of one row, so a few seconds costs nothing.
|
|
124
|
+
const FACT_LOOK_MS = 5_000;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The fact kind for sources.mjs: `{ key, machine }`, a fact as this computer's board says it (home's own, or its copy
|
|
128
|
+
* on a worker). A reading is `{ value, there, hops }`, given once at the start and again each time the fact changes:
|
|
129
|
+
* at once when this computer writes it, within FACT_LOOK_MS when the copy brings it from home.
|
|
130
|
+
*/
|
|
131
|
+
export function createFactKind({ facts, now = () => Date.now() }) {
|
|
132
|
+
return {
|
|
133
|
+
describe: ({ key, machine }) =>
|
|
134
|
+
`reads the fact ${key}${machine ? ` about computer ${machine}` : ""} every ${FACT_LOOK_MS / 1000} s`,
|
|
135
|
+
start({ key, machine = null }, io) {
|
|
136
|
+
let last;
|
|
137
|
+
let timer = null;
|
|
138
|
+
const look = () => {
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
const fact = facts.get(String(key), { machine });
|
|
141
|
+
const said = fact ? JSON.stringify(fact.value) : null;
|
|
142
|
+
if (said !== last) {
|
|
143
|
+
last = said;
|
|
144
|
+
io.report({
|
|
145
|
+
at: now(),
|
|
146
|
+
value: fact?.value ?? null,
|
|
147
|
+
there: Boolean(fact),
|
|
148
|
+
hops: fact?.hops ?? 0,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
timer = setTimeout(look, FACT_LOOK_MS);
|
|
152
|
+
timer.unref?.();
|
|
153
|
+
};
|
|
154
|
+
// On the next turn, not inside the write: what answers this reading may write a fact itself.
|
|
155
|
+
const stopHearing =
|
|
156
|
+
facts.onSet?.((written, about) => {
|
|
157
|
+
if (written === String(key) && about === (machine || null))
|
|
158
|
+
setImmediate(look);
|
|
159
|
+
}) ?? (() => {});
|
|
160
|
+
timer = setTimeout(look, 0);
|
|
161
|
+
timer.unref?.();
|
|
162
|
+
return {
|
|
163
|
+
stop() {
|
|
164
|
+
stopHearing();
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|