tryharness 0.7.82 → 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 +193 -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 +57 -2
- 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 +2435 -333
- 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 +2 -2
- 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/onboard.mjs +6 -6
- 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 +212 -130
- 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,454 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { PARENT, SENSE, putSteer, takeSteers } from "./steerQueue.mjs";
|
|
5
|
+
import { harnessHome } from "./paths.mjs";
|
|
6
|
+
import { diffOf } from "./lineDiff.mjs";
|
|
7
|
+
import {
|
|
8
|
+
callsRoot,
|
|
9
|
+
changedText,
|
|
10
|
+
contentOf,
|
|
11
|
+
dropTrip,
|
|
12
|
+
noteElsewhere,
|
|
13
|
+
noteMade,
|
|
14
|
+
noteSeen,
|
|
15
|
+
ownChange,
|
|
16
|
+
readsOf,
|
|
17
|
+
readsRoot,
|
|
18
|
+
sessionOf,
|
|
19
|
+
tripsOf,
|
|
20
|
+
putTrip,
|
|
21
|
+
versionsOf,
|
|
22
|
+
} from "./senseCall.mjs";
|
|
23
|
+
|
|
24
|
+
// Tripwires (docs/senses-plan.md S4.3), the daemon's half: it follows the folder of each session that read files
|
|
25
|
+
// (senseCall.mjs keeps what each read), and when a file a session read changes and the session has not got that content,
|
|
26
|
+
// it works out who changed it and what changed, leaves a trip the session's next write is checked against, and tells
|
|
27
|
+
// the session at its next step. Who: another session whose own record has the new content (it wrote it), a run Harness
|
|
28
|
+
// started there, or else the user, whose changes always hold (owner, November 13). Never tripped: lockfiles, build
|
|
29
|
+
// output (the file source leaves out what the repository ignores), and a branch switched under the folder, which is
|
|
30
|
+
// one note instead of a trip for each file.
|
|
31
|
+
|
|
32
|
+
// A session that has made no tool call for this long is no longer followed; its record stays until cleanup.
|
|
33
|
+
const QUIET_MS = 12 * 3_600_000;
|
|
34
|
+
const SWEEP_MS = 10 * 60_000;
|
|
35
|
+
// What sessions read is kept as long as the log is.
|
|
36
|
+
const KEEP_MS = 30 * 86_400_000;
|
|
37
|
+
// A change this soon after the session's own write of the file, by no one else Harness knows, is its formatter's (a hook
|
|
38
|
+
// after its edit tool, format on save).
|
|
39
|
+
const FORMATTED_MS = 10_000;
|
|
40
|
+
// More files than this changed with the commit moved: one note, like a branch switch.
|
|
41
|
+
const MANY_FILES = 20;
|
|
42
|
+
const LOCKFILES = new Set([
|
|
43
|
+
"package-lock.json",
|
|
44
|
+
"npm-shrinkwrap.json",
|
|
45
|
+
"yarn.lock",
|
|
46
|
+
"pnpm-lock.yaml",
|
|
47
|
+
"bun.lockb",
|
|
48
|
+
"bun.lock",
|
|
49
|
+
"Cargo.lock",
|
|
50
|
+
"Gemfile.lock",
|
|
51
|
+
"poetry.lock",
|
|
52
|
+
"uv.lock",
|
|
53
|
+
"composer.lock",
|
|
54
|
+
"go.sum",
|
|
55
|
+
"Package.resolved",
|
|
56
|
+
"Podfile.lock",
|
|
57
|
+
"flake.lock",
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const git = (args, cwd) =>
|
|
61
|
+
new Promise((resolve) =>
|
|
62
|
+
execFile(
|
|
63
|
+
"git",
|
|
64
|
+
args,
|
|
65
|
+
{ cwd, encoding: "utf8", timeout: 5_000 },
|
|
66
|
+
(error, out) => resolve(error ? null : String(out).trim())
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
/** Where a folder's repository stands: its branch and commit, or null outside one. */
|
|
71
|
+
async function headOf(folder) {
|
|
72
|
+
const said = await git(["rev-parse", "--abbrev-ref", "HEAD", "HEAD"], folder);
|
|
73
|
+
if (!said) return null;
|
|
74
|
+
const [branch, commit] = said.split("\n");
|
|
75
|
+
return { branch, commit };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const fenced = (text) => `\`\`\`diff\n${text}\n\`\`\``;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `follow(folder, onReading)` follows a folder's file changes (fileSource.mjs readings: `{ changed, by, whileOff }`,
|
|
82
|
+
* names under the folder) and returns what stops it. `nameOf(agent)` names an agent as the person knows it. `events`
|
|
83
|
+
* is the log.
|
|
84
|
+
*/
|
|
85
|
+
export function createTripwires({
|
|
86
|
+
follow,
|
|
87
|
+
nameOf = (agent) => agent,
|
|
88
|
+
events,
|
|
89
|
+
home = harnessHome,
|
|
90
|
+
log = () => {},
|
|
91
|
+
now = () => Date.now(),
|
|
92
|
+
}) {
|
|
93
|
+
// A followed folder: what stops it, its sessions, and where its repository last stood.
|
|
94
|
+
const folders = new Map();
|
|
95
|
+
const sessions = new Map();
|
|
96
|
+
let watcher = null;
|
|
97
|
+
let sweeper = null;
|
|
98
|
+
const versions = versionsOf(home);
|
|
99
|
+
|
|
100
|
+
// When a session's last tool call ended, or its record began.
|
|
101
|
+
const lastStep = (session) => {
|
|
102
|
+
for (const name of ["step", "session.json"])
|
|
103
|
+
try {
|
|
104
|
+
return fs.statSync(path.join(readsRoot(home), session, name)).mtimeMs;
|
|
105
|
+
} catch {}
|
|
106
|
+
return 0;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const realFolder = (folder) => {
|
|
110
|
+
try {
|
|
111
|
+
return fs.realpathSync(folder);
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const seenBy = (session, file) =>
|
|
118
|
+
readsOf(session, home).find((seen) => seen.file === file) ?? null;
|
|
119
|
+
|
|
120
|
+
// Who made a file's content: a session that wrote it since this one last had the file, a run Harness started there,
|
|
121
|
+
// else the user.
|
|
122
|
+
const whoMade = (place, session, seen, blob, runs) => {
|
|
123
|
+
for (const other of place.sessions) {
|
|
124
|
+
if (other === session) continue;
|
|
125
|
+
const theirs = seenBy(other, seen.file);
|
|
126
|
+
if (
|
|
127
|
+
theirs?.blob === blob &&
|
|
128
|
+
theirs.wrote &&
|
|
129
|
+
Date.parse(theirs.at) >= Date.parse(seen.at)
|
|
130
|
+
)
|
|
131
|
+
return { by: nameOf(sessions.get(other)?.agent), user: false };
|
|
132
|
+
}
|
|
133
|
+
const others = (runs ?? []).filter(
|
|
134
|
+
(run) => String(run.sessionId ?? "") !== session
|
|
135
|
+
);
|
|
136
|
+
if (others.length)
|
|
137
|
+
return {
|
|
138
|
+
by: [
|
|
139
|
+
...new Set(others.map((run) => `${nameOf(run.agent)}'s run`)),
|
|
140
|
+
].join(" and "),
|
|
141
|
+
user: false,
|
|
142
|
+
};
|
|
143
|
+
return { by: "the user", user: true };
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// The session expected to be alone in its folder (senseTools.mjs expect, alone): another's change says so at once,
|
|
147
|
+
// and the expectation ends. Its own commands' changes never reach here as another's (the edge line leaves them out
|
|
148
|
+
// when it is said; here, a change by no one Harness knows counts).
|
|
149
|
+
const aloneBroken = (session, rel, by, place) => {
|
|
150
|
+
const file = path.join(readsRoot(home), session, "alone.json");
|
|
151
|
+
if (!fs.existsSync(file)) return;
|
|
152
|
+
// When it changed: the file's own time, or for one gone, about when the burst that told it began.
|
|
153
|
+
const changedAt =
|
|
154
|
+
fs.statSync(path.join(place.folder, rel), { throwIfNoEntry: false })
|
|
155
|
+
?.mtimeMs ?? now() - 2_500;
|
|
156
|
+
if (ownChange(session, changedAt, home)) return;
|
|
157
|
+
let alone = null;
|
|
158
|
+
try {
|
|
159
|
+
alone = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
160
|
+
} catch {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// A change made before the expectation was said does not break it.
|
|
164
|
+
if (alone && changedAt < Date.parse(alone.since ?? 0)) return;
|
|
165
|
+
fs.rmSync(file, { force: true });
|
|
166
|
+
if (!alone || Date.parse(alone.until) <= now()) return;
|
|
167
|
+
putSteer(
|
|
168
|
+
{
|
|
169
|
+
session,
|
|
170
|
+
agent: PARENT,
|
|
171
|
+
kind: SENSE,
|
|
172
|
+
text: `Your expectation broke ("${alone.what}"): ${rel} changed, by ${by ?? "someone Harness does not know"}.`,
|
|
173
|
+
},
|
|
174
|
+
home
|
|
175
|
+
);
|
|
176
|
+
events.append({
|
|
177
|
+
source: "senses",
|
|
178
|
+
kind: "expectation broken",
|
|
179
|
+
subject: `session:${session}`,
|
|
180
|
+
text: `${nameOf(sessions.get(session)?.agent)}'s expectation to be alone broke: ${rel} changed`,
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// A change to a file the session never read: kept for its edge line at its next pause (senseCall.mjs edgeAtPause),
|
|
185
|
+
// naming who made it when Harness knows: another session that wrote it, or a run Harness started there.
|
|
186
|
+
const elsewhere = (place, session, file, rel, runs) => {
|
|
187
|
+
// What the session's own command changed comes back to it at its next step instead (senseCall.mjs noteMade).
|
|
188
|
+
const changedAt =
|
|
189
|
+
fs.statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? now() - 2_500;
|
|
190
|
+
if (ownChange(session, changedAt, home)) {
|
|
191
|
+
noteMade(session, rel, home);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const blob = contentOf(file);
|
|
195
|
+
let by = null;
|
|
196
|
+
for (const other of place.sessions) {
|
|
197
|
+
if (other === session) continue;
|
|
198
|
+
const theirs = seenBy(other, file);
|
|
199
|
+
if (blob && theirs?.blob === blob && theirs.wrote)
|
|
200
|
+
by = nameOf(sessions.get(other)?.agent);
|
|
201
|
+
}
|
|
202
|
+
const others = (runs ?? []).filter(
|
|
203
|
+
(run) => String(run.sessionId ?? "") !== session
|
|
204
|
+
);
|
|
205
|
+
if (!by && others.length)
|
|
206
|
+
by = [...new Set(others.map((run) => `${nameOf(run.agent)}'s run`))].join(
|
|
207
|
+
" and "
|
|
208
|
+
);
|
|
209
|
+
// When it changed, by the file's own time: noticed only once it settles.
|
|
210
|
+
const changed =
|
|
211
|
+
fs.statSync(file, { throwIfNoEntry: false })?.mtimeMs ?? now();
|
|
212
|
+
noteElsewhere(
|
|
213
|
+
session,
|
|
214
|
+
{ rel, at: new Date(changed).toISOString(), by },
|
|
215
|
+
home
|
|
216
|
+
);
|
|
217
|
+
aloneBroken(session, rel, by, place);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// Takes back a note about a file not yet read, so only the newest change is told.
|
|
221
|
+
const unsay = (session, file) =>
|
|
222
|
+
takeSteers(session, PARENT, { event: "replaced" }, home, {
|
|
223
|
+
only: (item) => item.kind === SENSE && item.about?.trip === file,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const judge = (place, session, rel, runs, whileOff) => {
|
|
227
|
+
const file = path.join(place.folder, rel);
|
|
228
|
+
if (LOCKFILES.has(path.basename(rel))) return;
|
|
229
|
+
const seen = seenBy(session, file);
|
|
230
|
+
if (!seen) {
|
|
231
|
+
elsewhere(place, session, file, rel, runs);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const blob = contentOf(file);
|
|
235
|
+
const trip = tripsOf(session, home).find((each) => each.file === file);
|
|
236
|
+
if (blob === seen.blob) {
|
|
237
|
+
// Put back as the session has it: nothing left to tell.
|
|
238
|
+
if (trip) {
|
|
239
|
+
dropTrip(session, file, home);
|
|
240
|
+
unsay(session, file);
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (!blob || trip?.now === blob) return;
|
|
245
|
+
const who = whileOff
|
|
246
|
+
? { by: "someone while Harness was off", user: true }
|
|
247
|
+
: whoMade(place, session, seen, blob, runs);
|
|
248
|
+
if (
|
|
249
|
+
who.user &&
|
|
250
|
+
!whileOff &&
|
|
251
|
+
seen.wrote &&
|
|
252
|
+
now() - Date.parse(seen.at) <= FORMATTED_MS
|
|
253
|
+
) {
|
|
254
|
+
noteSeen(session, file, home, { wrote: true });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const before = versions.read(seen.blob);
|
|
258
|
+
const after = fs.readFileSync(file, "utf8");
|
|
259
|
+
const diff = before
|
|
260
|
+
? diffOf(before.toString("utf8"), after)
|
|
261
|
+
: { text: "", complete: false, ranges: [] };
|
|
262
|
+
versions.keep(file);
|
|
263
|
+
const next = {
|
|
264
|
+
file,
|
|
265
|
+
rel,
|
|
266
|
+
read: seen.blob,
|
|
267
|
+
now: blob,
|
|
268
|
+
by: who.by,
|
|
269
|
+
user: who.user,
|
|
270
|
+
diff: diff.text
|
|
271
|
+
? fenced(diff.text)
|
|
272
|
+
: "(the version you read is no longer kept)",
|
|
273
|
+
complete: diff.complete && Boolean(before),
|
|
274
|
+
ranges: diff.ranges,
|
|
275
|
+
at: new Date(now()).toISOString(),
|
|
276
|
+
told: false,
|
|
277
|
+
};
|
|
278
|
+
putTrip(session, next, home);
|
|
279
|
+
aloneBroken(session, rel, who.by, place);
|
|
280
|
+
unsay(session, file);
|
|
281
|
+
putSteer(
|
|
282
|
+
{
|
|
283
|
+
session,
|
|
284
|
+
agent: PARENT,
|
|
285
|
+
kind: SENSE,
|
|
286
|
+
text: changedText(next),
|
|
287
|
+
about: { trip: file },
|
|
288
|
+
},
|
|
289
|
+
home
|
|
290
|
+
);
|
|
291
|
+
events.append({
|
|
292
|
+
source: "senses",
|
|
293
|
+
kind: "tripwire",
|
|
294
|
+
subject: `session:${session}`,
|
|
295
|
+
text: `${rel} changed after ${nameOf(sessions.get(session)?.agent)} read it, by ${who.by}`,
|
|
296
|
+
data: { file: rel, by: who.by, user: who.user },
|
|
297
|
+
});
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// A branch switched (or many files moved with the commit): one note, and what each session has is taken as it is now.
|
|
301
|
+
const moved = (place, session, from, to, changed) => {
|
|
302
|
+
for (const rel of changed) {
|
|
303
|
+
const file = path.join(place.folder, rel);
|
|
304
|
+
if (!seenBy(session, file)) continue;
|
|
305
|
+
if (contentOf(file)) noteSeen(session, file, home);
|
|
306
|
+
dropTrip(session, file, home);
|
|
307
|
+
unsay(session, file);
|
|
308
|
+
}
|
|
309
|
+
const text =
|
|
310
|
+
from.branch !== to.branch
|
|
311
|
+
? `The branch here changed from ${from.branch} to ${to.branch}: files you read may be different now.`
|
|
312
|
+
: `The commit here moved (${from.commit.slice(0, 8)} to ${to.commit.slice(0, 8)}) and ${changed.length} files changed: files you read may be different now.`;
|
|
313
|
+
putSteer({ session, agent: PARENT, kind: SENSE, text }, home);
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const onReading = async (place, reading) => {
|
|
317
|
+
if (!reading?.changed?.length) return;
|
|
318
|
+
const head = await headOf(place.folder);
|
|
319
|
+
const from = place.head;
|
|
320
|
+
place.head = head ?? place.head;
|
|
321
|
+
const switched =
|
|
322
|
+
from &&
|
|
323
|
+
head &&
|
|
324
|
+
(from.branch !== head.branch ||
|
|
325
|
+
(from.commit !== head.commit && reading.changed.length > MANY_FILES));
|
|
326
|
+
for (const session of place.sessions) {
|
|
327
|
+
try {
|
|
328
|
+
if (switched) moved(place, session, from, head, reading.changed);
|
|
329
|
+
else
|
|
330
|
+
for (const rel of reading.changed)
|
|
331
|
+
judge(place, session, rel, reading.by, reading.whileOff);
|
|
332
|
+
} catch (error) {
|
|
333
|
+
log(`tripwires: ${place.folder}: ${error.message}`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const join = (session) => {
|
|
339
|
+
if (sessions.has(session)) return;
|
|
340
|
+
const meta = sessionOf(session, home);
|
|
341
|
+
const folder = meta?.cwd ? realFolder(meta.cwd) : null;
|
|
342
|
+
if (!folder) return;
|
|
343
|
+
if (now() - lastStep(session) > QUIET_MS) return;
|
|
344
|
+
sessions.set(session, { agent: meta.agent, folder });
|
|
345
|
+
let place = folders.get(folder);
|
|
346
|
+
if (!place) {
|
|
347
|
+
place = { folder, sessions: new Set(), head: null, stop: null };
|
|
348
|
+
folders.set(folder, place);
|
|
349
|
+
void headOf(folder).then((head) => (place.head ??= head));
|
|
350
|
+
place.stop = follow(folder, (reading) => onReading(place, reading));
|
|
351
|
+
}
|
|
352
|
+
place.sessions.add(session);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// A session's folder appears before its first record is written in it: it is tried again for a few seconds.
|
|
356
|
+
const joinSoon = (session, tries = 10) => {
|
|
357
|
+
setTimeout(() => {
|
|
358
|
+
join(session);
|
|
359
|
+
if (
|
|
360
|
+
!sessions.has(session) &&
|
|
361
|
+
tries > 1 &&
|
|
362
|
+
sessionOf(session, home) === null &&
|
|
363
|
+
fs.existsSync(path.join(readsRoot(home), session))
|
|
364
|
+
)
|
|
365
|
+
joinSoon(session, tries - 1);
|
|
366
|
+
}, 300).unref?.();
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const leave = (session) => {
|
|
370
|
+
const state = sessions.get(session);
|
|
371
|
+
if (!state) return;
|
|
372
|
+
sessions.delete(session);
|
|
373
|
+
const place = folders.get(state.folder);
|
|
374
|
+
place?.sessions.delete(session);
|
|
375
|
+
if (place && !place.sessions.size) {
|
|
376
|
+
place.stop?.();
|
|
377
|
+
folders.delete(state.folder);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const listedSessions = () => {
|
|
382
|
+
try {
|
|
383
|
+
return fs.readdirSync(readsRoot(home));
|
|
384
|
+
} catch {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const sweep = () => {
|
|
390
|
+
for (const session of listedSessions()) join(session);
|
|
391
|
+
for (const session of [...sessions.keys()])
|
|
392
|
+
if (now() - lastStep(session) > QUIET_MS) leave(session);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
start() {
|
|
397
|
+
fs.mkdirSync(readsRoot(home), { recursive: true, mode: 0o700 });
|
|
398
|
+
sweep();
|
|
399
|
+
// A session's first tool call makes its folder here: followed at once.
|
|
400
|
+
try {
|
|
401
|
+
watcher = fs.watch(readsRoot(home), (_, name) => {
|
|
402
|
+
if (name) joinSoon(String(name));
|
|
403
|
+
});
|
|
404
|
+
watcher.on("error", () => {});
|
|
405
|
+
} catch (error) {
|
|
406
|
+
log(`tripwires: cannot watch for sessions: ${error.message}`);
|
|
407
|
+
}
|
|
408
|
+
sweeper = setInterval(sweep, SWEEP_MS);
|
|
409
|
+
sweeper.unref?.();
|
|
410
|
+
},
|
|
411
|
+
stop() {
|
|
412
|
+
watcher?.close();
|
|
413
|
+
clearInterval(sweeper);
|
|
414
|
+
for (const place of folders.values()) place.stop?.();
|
|
415
|
+
folders.clear();
|
|
416
|
+
sessions.clear();
|
|
417
|
+
},
|
|
418
|
+
/** The sessions followed, by folder. For tests and the thread's record. */
|
|
419
|
+
followed: () =>
|
|
420
|
+
[...folders.values()].map((place) => ({
|
|
421
|
+
folder: place.folder,
|
|
422
|
+
sessions: [...place.sessions],
|
|
423
|
+
})),
|
|
424
|
+
/** Forgets what sessions read past 30 days, and kept versions past theirs. Returns how many went. */
|
|
425
|
+
trim() {
|
|
426
|
+
let gone = versions.trim();
|
|
427
|
+
// A process's last call start, from a process long gone.
|
|
428
|
+
for (const name of (() => {
|
|
429
|
+
try {
|
|
430
|
+
return fs.readdirSync(callsRoot(home));
|
|
431
|
+
} catch {
|
|
432
|
+
return [];
|
|
433
|
+
}
|
|
434
|
+
})()) {
|
|
435
|
+
const file = path.join(callsRoot(home), name);
|
|
436
|
+
const stat = fs.statSync(file, { throwIfNoEntry: false });
|
|
437
|
+
if (stat && now() - stat.mtimeMs > 86_400_000) {
|
|
438
|
+
fs.rmSync(file, { force: true });
|
|
439
|
+
gone++;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
for (const session of listedSessions()) {
|
|
443
|
+
if (sessions.has(session)) continue;
|
|
444
|
+
const folder = path.join(readsRoot(home), session);
|
|
445
|
+
if (now() - lastStep(session) < KEEP_MS) continue;
|
|
446
|
+
fs.rmSync(folder, { recursive: true, force: true });
|
|
447
|
+
for (const trip of tripsOf(session, home))
|
|
448
|
+
dropTrip(session, trip.file, home);
|
|
449
|
+
gone++;
|
|
450
|
+
}
|
|
451
|
+
return gone;
|
|
452
|
+
},
|
|
453
|
+
};
|
|
454
|
+
}
|
package/lib/ui.mjs
CHANGED
|
@@ -113,7 +113,7 @@ export function createUi({
|
|
|
113
113
|
if (!process.stdout.isTTY)
|
|
114
114
|
return {
|
|
115
115
|
start: (text) => p.log.message(fit(text)),
|
|
116
|
-
message: () =>
|
|
116
|
+
message: (text) => p.log.message(fit(text)),
|
|
117
117
|
stop: (text, code) => (code ? p.log.warn : p.log.step)(fit(text)),
|
|
118
118
|
};
|
|
119
119
|
const spin = p.spinner(),
|