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
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
// What a watch does when it trips (docs/senses-plan.md S3.2, the response table): everything, in one place, so nothing
|
|
2
|
+
// is half built (owner, November 12: "7 shoudl just be moved to 3.2 entirely so that nothing is half implemented"). A
|
|
3
|
+
// response is one of four, cheapest first: set a fact; tell the person; tap an agent that is working (a note it reads
|
|
4
|
+
// at its next step); wake agents (a new turn in their thread). A check-in is a wake on the clock: its post in the
|
|
5
|
+
// thread, then the agents it names or the lead's round. Three more protect (S5.1): hold an agent's next calls, pause a
|
|
6
|
+
// process, save a copy of files. Only a reflex (reflexes.mjs), code the system arms, may give them: never a watch an
|
|
7
|
+
// agent or the person armed, never a model. They are written down and carried out once, like the rest.
|
|
8
|
+
//
|
|
9
|
+
// Each response is written down before it is carried out and carried out once: a daemon stopped in between carries it
|
|
10
|
+
// out at its next start. Wakes for one thread that come within a moment of each other go as one, never mixing whose go
|
|
11
|
+
// they carry. A wake caused from outside the thread starts the thread's wake count again, as a check-in does
|
|
12
|
+
// (threadRuns.mjs wakeCount); one the thread's own work caused counts toward its wake limit. A chain of responses, each
|
|
13
|
+
// setting off the next (a watch that sets a fact another watch follows), stops where a chain of work handed on without
|
|
14
|
+
// the person does (MAX_HANDOFFS): a fact keeps how many steps led to it (facts.mjs), so the count crosses computers.
|
|
15
|
+
//
|
|
16
|
+
// A watch briefed in words (S9.1) is read by attention (attention.mjs) at each trip before it is told or wakes anyone:
|
|
17
|
+
// held back when it does not matter as the brief says, or handed to another agent of the thread's team. Facts and
|
|
18
|
+
// protective responses are code's and are never read.
|
|
19
|
+
//
|
|
20
|
+
// A watch is answered on the computer that reads it. Telling the person and tapping an agent working there happen
|
|
21
|
+
// there; a fact is written on home and a wake starts there, so another computer hands those to home
|
|
22
|
+
// (`board.watch.answer`), which carries them out from the watch as home keeps it, once however often it is asked.
|
|
23
|
+
|
|
24
|
+
export const FIRE_TABLES = `
|
|
25
|
+
create table if not exists board_watch_fires (
|
|
26
|
+
id integer primary key autoincrement,
|
|
27
|
+
watch text not null,
|
|
28
|
+
edge text not null,
|
|
29
|
+
text text not null,
|
|
30
|
+
response text not null,
|
|
31
|
+
thread text,
|
|
32
|
+
cause integer,
|
|
33
|
+
own integer not null default 0,
|
|
34
|
+
depth integer not null default 1,
|
|
35
|
+
origin text,
|
|
36
|
+
seen text,
|
|
37
|
+
value real,
|
|
38
|
+
state text not null,
|
|
39
|
+
outcome text,
|
|
40
|
+
at text not null,
|
|
41
|
+
done_at text
|
|
42
|
+
);
|
|
43
|
+
create index if not exists board_watch_fires_state on board_watch_fires(state);
|
|
44
|
+
create unique index if not exists board_watch_fires_origin on board_watch_fires(origin);
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
// Wakes for one thread within this long of each other go as one.
|
|
48
|
+
const COLLAPSE_MS = 1_500;
|
|
49
|
+
// A response another computer could not hand to home yet is tried again this often.
|
|
50
|
+
const RETRY_MS = 30_000;
|
|
51
|
+
|
|
52
|
+
// Whose go a wake carries: the person's for a watch or check-in they set, the lead's for its own check-in, and for an
|
|
53
|
+
// agent's own watch the dispatcher's, which only ever wakes that agent.
|
|
54
|
+
const goOf = (response) =>
|
|
55
|
+
response.do === "agent" ? "agent" : response.by === "lead" ? "lead" : "user";
|
|
56
|
+
|
|
57
|
+
// What the woken agents are told woke them, before what happened.
|
|
58
|
+
const whyOf = (response) => {
|
|
59
|
+
if (response.post)
|
|
60
|
+
return `a check-in ${response.by === "lead" ? "the lead set" : "you set"} for this thread`;
|
|
61
|
+
if (response.wait) return "the wait you set";
|
|
62
|
+
return response.do === "agent"
|
|
63
|
+
? "your own watch on this thread"
|
|
64
|
+
: "a watch the person set on this thread";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** The protective responses (S5.1): a reflex's alone. */
|
|
68
|
+
export const PROTECTIVE = new Set(["hold", "pause", "save"]);
|
|
69
|
+
|
|
70
|
+
// What a working agent reads at its next step when its own watch, or its wait (S3.3), answers.
|
|
71
|
+
const tapWordsOf = (response, text) =>
|
|
72
|
+
`${response.wait ? "The wait you set" : "Your watch on this thread"}: ${text}`;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The response table. `fire({ watch, edge, text, cause, own, hops, origin, seen })` records one response and carries it
|
|
76
|
+
* out (`seen` what the watch read, for attention):
|
|
77
|
+
* `watch` is a row as threadWatches.mjs gives it (with its `response`), and the caller is following it, so it is on;
|
|
78
|
+
* `edge` what happened ("tripped", "cleared", "blind", "sees", "due", and a wait's "timeout"); `text` what it says,
|
|
79
|
+
* holding no one's words; `own` whether the thread's own work caused it; `hops` the steps of a chain that led to it;
|
|
80
|
+
* `origin` the computer and fire it came from, for one another computer handed here. Returns `{ id, done }`, `done`
|
|
81
|
+
* resolving to the response's `{ state, outcome }`.
|
|
82
|
+
*
|
|
83
|
+
* The daemon gives the ways to act: `notify(text)`; `wake({ thread, agents, round, text, why, by, own, posts, causes })`,
|
|
84
|
+
* which returns what became of it when that was not a turn; `tap({ session, agent }, words)`, the words whole;
|
|
85
|
+
* `setFact(key, value, { hops, source })`; and `toHome(fire)` on a computer that is not home.
|
|
86
|
+
* `tappable(agent, session)` whether that session's own hook takes a note at its next step; `workingIn(thread, agent)` is
|
|
87
|
+
* that agent's run working on this computer, or null; `teamOf(thread)` and
|
|
88
|
+
* `statusOf(thread)` read the thread.
|
|
89
|
+
*/
|
|
90
|
+
export function createWatchResponses({
|
|
91
|
+
store,
|
|
92
|
+
events,
|
|
93
|
+
maxHops,
|
|
94
|
+
isHome = () => true,
|
|
95
|
+
toHome = async () => ({ ok: false, code: "home_offline" }),
|
|
96
|
+
notify = () => {},
|
|
97
|
+
wake = () => {},
|
|
98
|
+
tap = () => false,
|
|
99
|
+
// Whether a session's own hook takes a note at its next step (heard from that session: daemon.mjs).
|
|
100
|
+
tappable = () => false,
|
|
101
|
+
setFact = () => {},
|
|
102
|
+
// Carries out a protective response (reflexes.mjs): what it did in a few words, or null when it could not.
|
|
103
|
+
protect = () => null,
|
|
104
|
+
workingIn = () => null,
|
|
105
|
+
teamOf = () => [],
|
|
106
|
+
// Attention (attention.mjs judge): null where it is not wired. `cardOf(agent)` what the model reads of a team member.
|
|
107
|
+
attend = null,
|
|
108
|
+
cardOf = (agent) => ({ id: agent }),
|
|
109
|
+
statusOf = () => null,
|
|
110
|
+
log = () => {},
|
|
111
|
+
now = () => Date.now(),
|
|
112
|
+
}) {
|
|
113
|
+
const db = store.db;
|
|
114
|
+
const batches = new Map();
|
|
115
|
+
// Fires being carried out now (in a wake about to go, or on their way home): a resume leaves them alone.
|
|
116
|
+
const inFlight = new Set();
|
|
117
|
+
const waiting = new Map();
|
|
118
|
+
let retry = null;
|
|
119
|
+
|
|
120
|
+
const rowOf = (id) =>
|
|
121
|
+
db.get("select * from board_watch_fires where id = ?", [id]);
|
|
122
|
+
|
|
123
|
+
const finish = (fire, state, outcome) => {
|
|
124
|
+
inFlight.delete(fire.id);
|
|
125
|
+
const { changes } = db.run(
|
|
126
|
+
"update board_watch_fires set state = ?, outcome = ?, done_at = ? where id = ? and state = 'pending'",
|
|
127
|
+
[state, outcome, new Date(now()).toISOString(), fire.id]
|
|
128
|
+
);
|
|
129
|
+
if (changes)
|
|
130
|
+
events.append({
|
|
131
|
+
source: "watch-response",
|
|
132
|
+
kind: state === "done" ? "answered" : "skipped",
|
|
133
|
+
subject: fire.watch,
|
|
134
|
+
text: `${fire.edge}: ${outcome}`,
|
|
135
|
+
// Its thread and edge: a wake carried out on home is in the thread's record on the other computers too
|
|
136
|
+
// (nerves.mjs).
|
|
137
|
+
data: {
|
|
138
|
+
fire: fire.id,
|
|
139
|
+
response: JSON.parse(fire.response).do,
|
|
140
|
+
thread: fire.thread ?? null,
|
|
141
|
+
edge: fire.edge,
|
|
142
|
+
},
|
|
143
|
+
cause: fire.cause ?? null,
|
|
144
|
+
});
|
|
145
|
+
for (const resolve of waiting.get(fire.id) ?? [])
|
|
146
|
+
resolve({ state, outcome });
|
|
147
|
+
waiting.delete(fire.id);
|
|
148
|
+
return outcome;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// Whether the watch is still on for a fire carried out after a wait: it is, unless it was ended or paused after the
|
|
152
|
+
// fire was written (a check-in that ended by running its last run still carries that run out).
|
|
153
|
+
const stillOn = (fire) => {
|
|
154
|
+
const row = db.get(
|
|
155
|
+
"select state, updated_at from board_watches where id = ?",
|
|
156
|
+
[fire.watch]
|
|
157
|
+
);
|
|
158
|
+
if (!row) return false;
|
|
159
|
+
return row.state === "on" || row.updated_at <= fire.at;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const flush = (key) => {
|
|
163
|
+
const batch = batches.get(key);
|
|
164
|
+
batches.delete(key);
|
|
165
|
+
if (!batch) return;
|
|
166
|
+
const status = statusOf(batch.thread);
|
|
167
|
+
const going = [];
|
|
168
|
+
for (const entry of batch.entries) {
|
|
169
|
+
if (status !== "open")
|
|
170
|
+
finish(entry.fire, "skipped", `the thread is ${status ?? "gone"}`);
|
|
171
|
+
else if (!stillOn(entry.fire))
|
|
172
|
+
finish(entry.fire, "skipped", "the watch ended before it went");
|
|
173
|
+
else going.push(entry);
|
|
174
|
+
}
|
|
175
|
+
if (!going.length) return;
|
|
176
|
+
const agents = [...new Set(going.flatMap((entry) => entry.agents))];
|
|
177
|
+
const round = going.some((entry) => entry.round);
|
|
178
|
+
const woken = [...agents, ...(round ? ["the lead"] : [])].join(", ");
|
|
179
|
+
try {
|
|
180
|
+
const said = wake({
|
|
181
|
+
thread: batch.thread,
|
|
182
|
+
agents,
|
|
183
|
+
round,
|
|
184
|
+
// What a watch read is quoted as data (S3.4); a check-in's words are the person's or the lead's, as written.
|
|
185
|
+
text: going
|
|
186
|
+
.map((entry) =>
|
|
187
|
+
entry.post ? entry.fire.text : `"${entry.fire.text}"`
|
|
188
|
+
)
|
|
189
|
+
.join("\n"),
|
|
190
|
+
why: [...new Set(going.map((entry) => entry.why))].join("; "),
|
|
191
|
+
by: batch.by,
|
|
192
|
+
// One caused from outside the thread starts its wake count again for all of them.
|
|
193
|
+
own: going.every((entry) => entry.own),
|
|
194
|
+
posts: going.filter((entry) => entry.post).map((entry) => entry.post),
|
|
195
|
+
// Where each began, for the picture the woken agent reads (wakePicture.mjs).
|
|
196
|
+
causes: going
|
|
197
|
+
.map((entry) => entry.fire.cause)
|
|
198
|
+
.filter((cause) => cause !== null && cause !== undefined),
|
|
199
|
+
});
|
|
200
|
+
const woke =
|
|
201
|
+
going.length > 1
|
|
202
|
+
? `woke ${woken}, one wake for ${going.length}`
|
|
203
|
+
: `woke ${woken}`;
|
|
204
|
+
// The daemon says what became of it when it was not simply a turn (the thread paused at its wake limit).
|
|
205
|
+
const outcome = said ? String(said).slice(0, 500) : woke;
|
|
206
|
+
for (const entry of going) finish(entry.fire, "done", outcome);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
log(`watch wake in ${batch.thread}: ${error.message}`);
|
|
209
|
+
for (const entry of going)
|
|
210
|
+
finish(entry.fire, "skipped", `the wake failed: ${error.message}`);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// Wakes for one thread within COLLAPSE_MS go as one, never mixing whose go they carry.
|
|
215
|
+
const wakeSoon = (fire, entry) => {
|
|
216
|
+
const by = goOf(entry.response);
|
|
217
|
+
const key = `${fire.thread}\n${by}`;
|
|
218
|
+
let batch = batches.get(key);
|
|
219
|
+
if (!batch) {
|
|
220
|
+
batch = { thread: fire.thread, by, entries: [], timer: null };
|
|
221
|
+
batches.set(key, batch);
|
|
222
|
+
batch.timer = setTimeout(() => flush(key), COLLAPSE_MS);
|
|
223
|
+
batch.timer.unref?.();
|
|
224
|
+
}
|
|
225
|
+
inFlight.add(fire.id);
|
|
226
|
+
batch.entries.push({
|
|
227
|
+
fire,
|
|
228
|
+
agents: entry.agents,
|
|
229
|
+
round: entry.round,
|
|
230
|
+
why: whyOf(entry.response),
|
|
231
|
+
own: Boolean(fire.own),
|
|
232
|
+
post: entry.response.post
|
|
233
|
+
? { text: fire.text, mentions: entry.agents }
|
|
234
|
+
: null,
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// A worker alone (open item 3; owner, November 14: "Reflexes + started work"): with home out of reach, a wake whose
|
|
239
|
+
// agents are all at work on this computer in that thread is told to them at their next step, as their own watch's
|
|
240
|
+
// would be. Anything else waits for home: a new turn, a fact, and a wake for an agent not at work here, since starting
|
|
241
|
+
// work or spending is home's to decide.
|
|
242
|
+
const aloneTap = (fire) => {
|
|
243
|
+
const response = JSON.parse(fire.response);
|
|
244
|
+
if (response.do !== "wake" || !fire.thread) return false;
|
|
245
|
+
const named = response.agents ?? (response.agent ? [response.agent] : []);
|
|
246
|
+
if (!named.length) return false;
|
|
247
|
+
const running = named.map((agent) => workingIn(fire.thread, agent));
|
|
248
|
+
if (
|
|
249
|
+
!running.every(
|
|
250
|
+
(each, index) => each?.session && tappable(named[index], each.session)
|
|
251
|
+
)
|
|
252
|
+
)
|
|
253
|
+
return false;
|
|
254
|
+
const why = whyOf(response);
|
|
255
|
+
const words = `${why[0].toUpperCase()}${why.slice(1)}: ${fire.text}`;
|
|
256
|
+
for (const each of running) tap(each, words);
|
|
257
|
+
finish(
|
|
258
|
+
fire,
|
|
259
|
+
"done",
|
|
260
|
+
`home was out of reach: told ${named.join(", ")} at work here, at the next step`
|
|
261
|
+
);
|
|
262
|
+
return true;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// On a computer that is not home: a fact or a wake is home's to carry out, so it goes there.
|
|
266
|
+
const handHome = (fire) => {
|
|
267
|
+
inFlight.add(fire.id);
|
|
268
|
+
Promise.resolve()
|
|
269
|
+
.then(() =>
|
|
270
|
+
toHome({
|
|
271
|
+
watch: fire.watch,
|
|
272
|
+
edge: fire.edge,
|
|
273
|
+
text: fire.text,
|
|
274
|
+
own: Boolean(fire.own),
|
|
275
|
+
hops: fire.depth - 1,
|
|
276
|
+
origin: String(fire.id),
|
|
277
|
+
})
|
|
278
|
+
)
|
|
279
|
+
.then(
|
|
280
|
+
(answer) => {
|
|
281
|
+
if (answer?.ok === false && answer.code === "home_offline") {
|
|
282
|
+
inFlight.delete(fire.id);
|
|
283
|
+
aloneTap(fire);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (answer?.ok === false)
|
|
287
|
+
return finish(fire, "skipped", `home refused it: ${answer.error}`);
|
|
288
|
+
finish(
|
|
289
|
+
fire,
|
|
290
|
+
answer?.state === "skipped" ? "skipped" : "done",
|
|
291
|
+
`on home: ${answer?.outcome ?? "carried out"}`
|
|
292
|
+
);
|
|
293
|
+
},
|
|
294
|
+
() => inFlight.delete(fire.id)
|
|
295
|
+
);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// The watch's brief for attention, if it has one.
|
|
299
|
+
const briefOf = (fire) => {
|
|
300
|
+
const row = db.get("select detail from board_watches where id = ?", [
|
|
301
|
+
fire.watch,
|
|
302
|
+
]);
|
|
303
|
+
const detail = row?.detail ? JSON.parse(row.detail) : null;
|
|
304
|
+
return typeof detail?.brief === "string" && detail.brief
|
|
305
|
+
? detail.brief
|
|
306
|
+
: null;
|
|
307
|
+
};
|
|
308
|
+
const ATTENDED = new Set(["tell", "wake", "agent"]);
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Attention reads a briefed watch's trip first (S9.1), then carries it out with what it heard: held back when it does
|
|
312
|
+
* not matter, a wake handed to whom it picked from the team. Returns false when there is nothing to read.
|
|
313
|
+
*/
|
|
314
|
+
const attendFirst = (fire, response) => {
|
|
315
|
+
const brief = attend && ATTENDED.has(response.do) ? briefOf(fire) : null;
|
|
316
|
+
if (!brief || !fire.thread) return false;
|
|
317
|
+
const named = response.agents?.[0] ?? response.agent ?? null;
|
|
318
|
+
const team = teamOf(fire.thread);
|
|
319
|
+
inFlight.add(fire.id);
|
|
320
|
+
Promise.resolve()
|
|
321
|
+
.then(() =>
|
|
322
|
+
attend({
|
|
323
|
+
brief,
|
|
324
|
+
// The line, and what the watch read behind it ("the fact changed" says nothing of its value).
|
|
325
|
+
happened: fire.seen ? `${fire.text}. ${fire.seen}` : fire.text,
|
|
326
|
+
team: team.map((agent) => cardOf(agent)),
|
|
327
|
+
named: response.do === "tell" ? "the person" : named,
|
|
328
|
+
})
|
|
329
|
+
)
|
|
330
|
+
.catch(() => ({ matters: true, who: named, by: "code" }))
|
|
331
|
+
.then((heard) => {
|
|
332
|
+
inFlight.delete(fire.id);
|
|
333
|
+
if (!heard.matters)
|
|
334
|
+
return finish(
|
|
335
|
+
fire,
|
|
336
|
+
"skipped",
|
|
337
|
+
"attention held it back: not what its brief asks for"
|
|
338
|
+
);
|
|
339
|
+
carryOut(fire, heard);
|
|
340
|
+
});
|
|
341
|
+
return true;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
/** Carries out one recorded response, checking what may have changed since it was written. */
|
|
345
|
+
const carryOut = (fire, heard = null) => {
|
|
346
|
+
const response = JSON.parse(fire.response);
|
|
347
|
+
const thread = fire.thread;
|
|
348
|
+
if (fire.depth > maxHops)
|
|
349
|
+
return finish(
|
|
350
|
+
fire,
|
|
351
|
+
"skipped",
|
|
352
|
+
`its chain is too long (${fire.depth} steps)`
|
|
353
|
+
);
|
|
354
|
+
if (thread) {
|
|
355
|
+
const status = statusOf(thread);
|
|
356
|
+
if (status !== "open")
|
|
357
|
+
return finish(fire, "skipped", `the thread is ${status ?? "gone"}`);
|
|
358
|
+
}
|
|
359
|
+
if (!heard && attendFirst(fire, response)) return;
|
|
360
|
+
if (response.do === "tell") {
|
|
361
|
+
notify(fire.text);
|
|
362
|
+
return finish(fire, "done", "told the person");
|
|
363
|
+
}
|
|
364
|
+
if (response.do === "fact") {
|
|
365
|
+
if (!isHome()) return handHome(fire);
|
|
366
|
+
const key = String(response.key);
|
|
367
|
+
setFact(
|
|
368
|
+
key,
|
|
369
|
+
{ state: fire.edge, says: fire.text, at: fire.at, fire: fire.id },
|
|
370
|
+
{ hops: fire.depth, source: `watch:${fire.watch}` }
|
|
371
|
+
);
|
|
372
|
+
return finish(fire, "done", `set the fact ${key}`);
|
|
373
|
+
}
|
|
374
|
+
if (PROTECTIVE.has(response.do)) {
|
|
375
|
+
const did = protect(response, fire);
|
|
376
|
+
return did
|
|
377
|
+
? finish(fire, "done", did)
|
|
378
|
+
: finish(fire, "skipped", `it could not ${response.do}`);
|
|
379
|
+
}
|
|
380
|
+
if (response.do !== "agent" && response.do !== "wake")
|
|
381
|
+
return finish(fire, "skipped", `there is no response ${response.do}`);
|
|
382
|
+
if (!thread)
|
|
383
|
+
return finish(fire, "skipped", "an agent is woken in its thread");
|
|
384
|
+
const asked = response.agents ?? (response.agent ? [response.agent] : []);
|
|
385
|
+
// Attention may hand a watch's wake to another agent of the thread's team; an agent's own watch stays its own.
|
|
386
|
+
const handed =
|
|
387
|
+
response.do === "wake" && heard?.who && !asked.includes(heard.who)
|
|
388
|
+
? [heard.who]
|
|
389
|
+
: null;
|
|
390
|
+
const named = handed ?? asked;
|
|
391
|
+
// A check-in wakes whom it names, as before it was a watch; a watch only agents still in the thread.
|
|
392
|
+
const team = response.post ? null : teamOf(thread);
|
|
393
|
+
const agents = team ? named.filter((agent) => team.includes(agent)) : named;
|
|
394
|
+
if (named.length && !agents.length)
|
|
395
|
+
return finish(
|
|
396
|
+
fire,
|
|
397
|
+
"skipped",
|
|
398
|
+
`${named.join(", ")} ${named.length === 1 ? "is" : "are"} no longer in the thread`
|
|
399
|
+
);
|
|
400
|
+
// Cheapest first: an agent of its own watch, working here now, reads a note at its next step if its hook takes one.
|
|
401
|
+
if (response.do === "agent") {
|
|
402
|
+
const running = workingIn(thread, response.agent);
|
|
403
|
+
if (
|
|
404
|
+
running?.session &&
|
|
405
|
+
tappable(response.agent, running.session) &&
|
|
406
|
+
tap(running, tapWordsOf(response, fire.text))
|
|
407
|
+
)
|
|
408
|
+
return finish(
|
|
409
|
+
fire,
|
|
410
|
+
"done",
|
|
411
|
+
`tapped ${response.agent} at its next step`
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
if (!isHome()) return handHome(fire);
|
|
415
|
+
wakeSoon(fire, { response, agents, round: !named.length });
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const run = (fire) => {
|
|
419
|
+
try {
|
|
420
|
+
carryOut(fire);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
log(`watch ${fire.watch}: ${error.message}`);
|
|
423
|
+
finish(fire, "skipped", `it failed: ${error.message}`);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/** What was written and never carried out (the daemon stopped in between, or home was away) is carried out, once. */
|
|
428
|
+
const resume = () => {
|
|
429
|
+
for (const fire of db.all(
|
|
430
|
+
"select * from board_watch_fires where state = 'pending' order by id"
|
|
431
|
+
)) {
|
|
432
|
+
if (inFlight.has(fire.id)) continue;
|
|
433
|
+
const response = JSON.parse(fire.response);
|
|
434
|
+
// One that waited in a wake, or on a worker, may meanwhile have been ended.
|
|
435
|
+
if (
|
|
436
|
+
(response.do === "agent" || response.do === "wake") &&
|
|
437
|
+
isHome() &&
|
|
438
|
+
!stillOn(fire)
|
|
439
|
+
)
|
|
440
|
+
finish(fire, "skipped", "the watch ended before it went");
|
|
441
|
+
else run(fire);
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
return {
|
|
446
|
+
fire({
|
|
447
|
+
watch,
|
|
448
|
+
edge,
|
|
449
|
+
text,
|
|
450
|
+
cause = null,
|
|
451
|
+
own = false,
|
|
452
|
+
hops = 0,
|
|
453
|
+
origin = null,
|
|
454
|
+
seen = null,
|
|
455
|
+
value = null,
|
|
456
|
+
}) {
|
|
457
|
+
const response = watch.response;
|
|
458
|
+
if (!response?.do) return null;
|
|
459
|
+
// Holding, pausing and saving are a reflex's alone.
|
|
460
|
+
if (PROTECTIVE.has(response.do) && watch.reflex !== true) return null;
|
|
461
|
+
// Asked again by the computer that handed it here: what it came to, once.
|
|
462
|
+
if (origin) {
|
|
463
|
+
const before = db.get(
|
|
464
|
+
"select * from board_watch_fires where origin = ?",
|
|
465
|
+
[String(origin)]
|
|
466
|
+
);
|
|
467
|
+
if (before)
|
|
468
|
+
return {
|
|
469
|
+
id: before.id,
|
|
470
|
+
done:
|
|
471
|
+
before.state !== "pending"
|
|
472
|
+
? Promise.resolve({
|
|
473
|
+
state: before.state,
|
|
474
|
+
outcome: before.outcome,
|
|
475
|
+
})
|
|
476
|
+
: waiting.has(before.id)
|
|
477
|
+
? new Promise((resolve) =>
|
|
478
|
+
waiting.get(before.id).push(resolve)
|
|
479
|
+
)
|
|
480
|
+
: Promise.resolve({
|
|
481
|
+
state: "pending",
|
|
482
|
+
outcome: "home is carrying it out",
|
|
483
|
+
}),
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
const { lastInsertRowid } = db.run(
|
|
487
|
+
"insert into board_watch_fires(watch, edge, text, response, thread, cause, own, depth, origin, seen, value, state, at) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?)",
|
|
488
|
+
[
|
|
489
|
+
String(watch.id),
|
|
490
|
+
String(edge),
|
|
491
|
+
String(text),
|
|
492
|
+
JSON.stringify(response),
|
|
493
|
+
watch.thread ?? null,
|
|
494
|
+
cause,
|
|
495
|
+
own ? 1 : 0,
|
|
496
|
+
Math.max(0, Number(hops) || 0) + 1,
|
|
497
|
+
origin ? String(origin) : null,
|
|
498
|
+
seen ? String(seen).slice(0, 2_000) : null,
|
|
499
|
+
Number.isFinite(value) ? value : null,
|
|
500
|
+
new Date(now()).toISOString(),
|
|
501
|
+
]
|
|
502
|
+
);
|
|
503
|
+
const fire = rowOf(Number(lastInsertRowid));
|
|
504
|
+
const done = new Promise((resolve) => waiting.set(fire.id, [resolve]));
|
|
505
|
+
run(fire);
|
|
506
|
+
return { id: fire.id, done };
|
|
507
|
+
},
|
|
508
|
+
resume,
|
|
509
|
+
/** How many of a watch's answers are written here and not carried out yet. */
|
|
510
|
+
pending(watch) {
|
|
511
|
+
return Number(
|
|
512
|
+
db.get(
|
|
513
|
+
"select count(*) as n from board_watch_fires where watch = ? and state = 'pending'",
|
|
514
|
+
[String(watch)]
|
|
515
|
+
)?.n ?? 0
|
|
516
|
+
);
|
|
517
|
+
},
|
|
518
|
+
/** Starts carrying out what was left; a computer that is not home tries again what home has not taken yet. */
|
|
519
|
+
start() {
|
|
520
|
+
resume();
|
|
521
|
+
retry = setInterval(() => {
|
|
522
|
+
if (!isHome()) resume();
|
|
523
|
+
}, RETRY_MS);
|
|
524
|
+
retry.unref?.();
|
|
525
|
+
},
|
|
526
|
+
stop() {
|
|
527
|
+
clearInterval(retry);
|
|
528
|
+
for (const batch of batches.values()) clearTimeout(batch.timer);
|
|
529
|
+
batches.clear();
|
|
530
|
+
inFlight.clear();
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
}
|