tryharness 0.7.53 → 0.7.57
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 +23 -19
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +484 -90
- package/lib/activity.mjs +10 -10
- package/lib/adapters.mjs +173 -222
- package/lib/agentBinaries.mjs +71 -0
- package/lib/agentRuntimeConfig.mjs +1 -1
- package/lib/agentRuntimes.mjs +34 -18
- package/lib/agentSignIn.mjs +456 -0
- package/lib/answers.mjs +5 -5
- package/lib/archetypes.d.ts +64 -0
- package/lib/archetypes.js +989 -0
- package/lib/asks.mjs +4 -4
- package/lib/audit.mjs +666 -0
- package/lib/backupSetup.mjs +143 -0
- package/lib/boardAccess.mjs +24 -11
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardFileCommands.mjs +6 -6
- package/lib/boardFiles.mjs +13 -12
- package/lib/boardMirror.mjs +124 -25
- package/lib/boardProtocol.json +88 -2
- package/lib/boardWaits.mjs +101 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +6 -6
- package/lib/cards.mjs +138 -28
- package/lib/cloudAgents.mjs +216 -0
- package/lib/cloudAi.mjs +1312 -0
- package/lib/cloudAssistants.mjs +242 -0
- package/lib/cloudBoot.mjs +119 -0
- package/lib/cloudSave.mjs +197 -0
- package/lib/cloudSsh.mjs +178 -0
- package/lib/commands.mjs +7 -2
- package/lib/context.mjs +19 -5
- package/lib/contractGate.mjs +399 -0
- package/lib/control-client.mjs +9 -1
- package/lib/controlSocket.mjs +86 -0
- package/lib/coordinator.mjs +252 -144
- package/lib/daemon.mjs +1351 -2767
- package/lib/desk.mjs +240 -0
- package/lib/desktop.mjs +178 -74
- package/lib/directLink.mjs +16 -5
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +247 -9
- package/lib/firewall.mjs +9 -9
- package/lib/folders.mjs +20 -16
- package/lib/frames.mjs +2 -3
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +25 -7
- package/lib/goals.mjs +7 -7
- package/lib/guestRequests.mjs +94 -0
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +4 -4
- package/lib/harnessModel.mjs +6 -6
- package/lib/harnessOffers.mjs +266 -0
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +26 -8
- package/lib/jobAnswers.mjs +190 -0
- package/lib/landed.mjs +887 -0
- package/lib/lane.mjs +42 -11
- package/lib/lessons.mjs +16 -16
- package/lib/library.mjs +3 -3
- package/lib/lineup.mjs +3 -0
- package/lib/linkRelay.mjs +574 -0
- package/lib/live.mjs +284 -13
- package/lib/localAgentAsk.mjs +74 -0
- package/lib/localStack.mjs +536 -0
- package/lib/localmodels.mjs +6 -4
- package/lib/loopBackstop.mjs +4 -4
- package/lib/machineIdentity.mjs +9 -1
- package/lib/machineRoutes.mjs +181 -2
- package/lib/machineRuns.mjs +76 -1
- package/lib/machineSeal.mjs +39 -12
- package/lib/mcp-stdio.mjs +163 -27
- package/lib/mcpRelay.mjs +1 -1
- package/lib/members.json +29 -2
- package/lib/members.mjs +1 -1
- package/lib/models.mjs +85 -31
- package/lib/museSession.mjs +21 -13
- package/lib/nativeHost.mjs +36 -8
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +50 -5
- package/lib/notify.mjs +7 -5
- package/lib/nudge.mjs +50 -19
- package/lib/ocr.mjs +9 -9
- package/lib/onboard.mjs +26 -20
- package/lib/oneLine.mjs +7 -7
- package/lib/ownMachines.mjs +1223 -140
- package/lib/pairing.mjs +60 -16
- package/lib/paths.mjs +19 -1
- package/lib/peerProcess.mjs +124 -0
- package/lib/personModel.mjs +89 -0
- package/lib/personNotices.mjs +151 -0
- package/lib/phones.mjs +538 -31
- package/lib/placeCommands.mjs +4 -4
- package/lib/places.mjs +400 -12
- package/lib/profile.mjs +67 -14
- package/lib/projectGit.mjs +158 -14
- package/lib/projectPieces.mjs +902 -0
- package/lib/projectSettings.mjs +7 -4
- package/lib/projects.mjs +384 -62
- package/lib/provenance.mjs +5 -1
- package/lib/proxyLinks.mjs +267 -0
- package/lib/reactions.mjs +7 -5
- package/lib/readiness.mjs +145 -23
- package/lib/record.mjs +128 -0
- package/lib/redact.mjs +4 -4
- package/lib/relayLimits.mjs +62 -0
- package/lib/release.mjs +170 -0
- package/lib/releaseKeys.json +6 -0
- package/lib/repoLinks.mjs +4 -4
- package/lib/roomChannels.mjs +24 -0
- package/lib/roomPage.mjs +1 -1
- package/lib/runAuthority.mjs +101 -0
- package/lib/runBrief.mjs +19 -8
- package/lib/runWorker.mjs +31 -7
- package/lib/runner.mjs +223 -29
- package/lib/runtime-install.mjs +143 -9
- package/lib/schedules.mjs +2 -2
- package/lib/screens.mjs +1 -1
- package/lib/sealed.mjs +11 -2
- package/lib/sealedThreads.mjs +671 -0
- package/lib/secretPatterns.json +1 -1
- package/lib/secrets.mjs +4 -4
- package/lib/seen.mjs +6 -6
- package/lib/serverWords.mjs +175 -0
- package/lib/service.mjs +22 -12
- package/lib/sessionControl.mjs +378 -90
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +57 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/speech.mjs +5 -5
- package/lib/store.mjs +420 -36
- package/lib/stuck.mjs +1 -1
- package/lib/swarm.mjs +304 -37
- package/lib/swarmDevices.mjs +319 -0
- package/lib/swarmMemory.mjs +27 -25
- package/lib/t3.mjs +24 -15
- package/lib/taskCommands.mjs +80 -20
- package/lib/taskRooms.mjs +125 -0
- package/lib/taskRuns.mjs +45 -3
- package/lib/team.json +255 -148
- package/lib/team.mjs +554 -190
- package/lib/threadActions.mjs +315 -0
- package/lib/threadAuthority.mjs +101 -0
- package/lib/threadBriefs.mjs +178 -0
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +209 -50
- package/lib/threadView.mjs +20 -7
- package/lib/usageTotals.mjs +2 -3
- package/lib/vault.mjs +101 -10
- package/lib/vaultCloud.mjs +2 -2
- package/lib/vaultCommands.mjs +15 -6
- package/lib/vaultTools.js +2 -2
- package/lib/verdicts.mjs +328 -0
- package/lib/wakes.mjs +1 -1
- package/lib/watches.mjs +2 -2
- package/lib/windowAsks.mjs +13 -5
- package/lib/worktrees.mjs +157 -25
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +12 -11
- package/release.json +220 -0
- package/lib/modelTiers.json +0 -1183
- package/lib/modelTiers.mjs +0 -133
package/lib/asks.mjs
CHANGED
|
@@ -11,10 +11,10 @@ import { nameOf } from "./members.mjs";
|
|
|
11
11
|
// Agents on Harness's server (Bankr, Quotient, Muse) keep their tasks there and take asks side by side. A send that may
|
|
12
12
|
// have reached Harness is looked up by its id before it goes again, so a retry never makes a second task.
|
|
13
13
|
//
|
|
14
|
-
// An ask for an agent whose earlier ask is running as a turn that takes words mid-turn (Harness on its server) goes
|
|
15
|
-
// that turn instead of waiting for it: the turn's answer answers both. One the turn ended without reading waits as
|
|
16
|
-
// other, and goes to the agent as its own task (
|
|
17
|
-
//
|
|
14
|
+
// An ask for an agent whose earlier ask is running as a turn that takes words mid-turn (Harness on its server) goes
|
|
15
|
+
// into that turn instead of waiting for it: the turn's answer answers both. One the turn ended without reading waits as
|
|
16
|
+
// any other, and goes to the agent as its own task (an ask that waited behind Harness's turn once went to Harness after
|
|
17
|
+
// that turn had already answered it).
|
|
18
18
|
|
|
19
19
|
const OPEN_RUN = ["open", "claimed", "running", "needs_input"];
|
|
20
20
|
// Sends lost on the way to Harness before the ask is given up and said as failed.
|
package/lib/audit.mjs
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { CLOUD_HOMED, MACHINE_RUNNERS, nameOf } from "./members.mjs";
|
|
3
|
+
import { receiptOfTransaction, receiptsForThread } from "./book.mjs";
|
|
4
|
+
import { offices } from "./specialties.mjs";
|
|
5
|
+
import { underperforming } from "./record.mjs";
|
|
6
|
+
import { namedFiles } from "./worktrees.mjs";
|
|
7
|
+
import { READ_ONLY_AGENTS } from "./runner.mjs";
|
|
8
|
+
|
|
9
|
+
// The auditor: whether work said to be done was done, after the fact.
|
|
10
|
+
//
|
|
11
|
+
// - Code, on every report, no model: each claim a report makes in its structured fields (report_task's `claims`) is
|
|
12
|
+
// compared with what the board recorded: the run's receipt (receipts.mjs), the sub-pieces still open, the chain
|
|
13
|
+
// receipts filed in the book, whether its change is committed on its thread's branch, and whether the agent was
|
|
14
|
+
// answering when the work was assigned. A claim made only in the report's words is not audited: code never reads
|
|
15
|
+
// intent from prose. The result goes in board_tasks.audit, and the record (record.mjs) reads a report whose claims
|
|
16
|
+
// its receipts do not bear out as rejected. A mismatch is against the agent, or against the dispatcher when the
|
|
17
|
+
// dispatcher routed work to an agent that was not answering; only the agent's count toward `ok`.
|
|
18
|
+
// - An agent, weekly: a read-only task for the appointed auditor with the week's record and Harness's routing. Its one
|
|
19
|
+
// short report goes straight to the person (board.audit.week), never through the liaison and never as a notice: the
|
|
20
|
+
// liaison is Jev, and the auditor audits Jev.
|
|
21
|
+
|
|
22
|
+
export const CLAIM_TESTS = ["passed", "failed", "not_run"];
|
|
23
|
+
// How long a finished run's receipt may take to be read from its session file (runWorker.mjs receiptFor) before a
|
|
24
|
+
// claim that needs it is audited without it.
|
|
25
|
+
const RECEIPT_WAIT_MS = 15 * 60_000;
|
|
26
|
+
// Spend reported within a cent and a percent of what was settled is the same spend.
|
|
27
|
+
const SPEND_SLACK = (settledUsd) => 0.01 + settledUsd * 0.01;
|
|
28
|
+
const WEEK_MS = 7 * 24 * 60 * 60_000;
|
|
29
|
+
// Node's timers stop while the Mac sleeps, so the wall clock is read on a short poll (schedules.mjs does the same).
|
|
30
|
+
const POLL_MS = 30 * 60_000;
|
|
31
|
+
const SWEEP_MS = 60_000;
|
|
32
|
+
const REPORTED = ["done", "failed"];
|
|
33
|
+
const NOT_RUNS = ["goal", "group"];
|
|
34
|
+
const WEEK_LAST = "audit-week-last";
|
|
35
|
+
const WEEK_REPORT = "audit-week-report";
|
|
36
|
+
const BRIEF_LINES = 40;
|
|
37
|
+
|
|
38
|
+
const usd = (value) => `$${Number(value).toFixed(2)}`;
|
|
39
|
+
const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
40
|
+
|
|
41
|
+
/** A report's structured claims, checked to the shape the audit reads, or null when it makes none. */
|
|
42
|
+
export function claimsOf(raw) {
|
|
43
|
+
if (!raw || typeof raw !== "object") return null;
|
|
44
|
+
const claims = {};
|
|
45
|
+
if (CLAIM_TESTS.includes(raw.tests)) claims.tests = raw.tests;
|
|
46
|
+
if (Array.isArray(raw.files_changed))
|
|
47
|
+
claims.files_changed = raw.files_changed
|
|
48
|
+
.filter((file) => typeof file === "string" && file.trim())
|
|
49
|
+
.slice(0, 100)
|
|
50
|
+
.map((file) => file.slice(0, 500));
|
|
51
|
+
if (Array.isArray(raw.sent))
|
|
52
|
+
claims.sent = raw.sent
|
|
53
|
+
.filter((sent) => sent && typeof sent === "object")
|
|
54
|
+
.slice(0, 20)
|
|
55
|
+
.map((sent) => ({
|
|
56
|
+
hash:
|
|
57
|
+
typeof sent.hash === "string" ? sent.hash.trim().slice(0, 120) : null,
|
|
58
|
+
chain: typeof sent.chain === "string" ? sent.chain.slice(0, 40) : null,
|
|
59
|
+
what: typeof sent.what === "string" ? sent.what.slice(0, 200) : null,
|
|
60
|
+
}));
|
|
61
|
+
const spent = Number(raw.spent_usd);
|
|
62
|
+
if (raw.spent_usd != null && Number.isFinite(spent) && spent >= 0)
|
|
63
|
+
claims.spent_usd = spent;
|
|
64
|
+
return Object.keys(claims).length ? claims : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const auditOf = (task) => {
|
|
68
|
+
if (!task?.audit) return null;
|
|
69
|
+
if (typeof task.audit === "object") return task.audit;
|
|
70
|
+
try {
|
|
71
|
+
return JSON.parse(task.audit);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const writeAudit = (store, taskId, audit) =>
|
|
78
|
+
store.db.run("update board_tasks set audit = ? where id = ?", [
|
|
79
|
+
JSON.stringify(audit),
|
|
80
|
+
String(taskId),
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
/** Keeps a report's claims with its task, for the audit that follows the report. */
|
|
84
|
+
export function recordClaims(store, taskId, raw) {
|
|
85
|
+
const claims = claimsOf(raw);
|
|
86
|
+
if (!claims) return null;
|
|
87
|
+
const kept = auditOf(store.getTask(taskId));
|
|
88
|
+
writeAudit(store, taskId, { ...kept, claims });
|
|
89
|
+
return claims;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const receiptOf = (store, taskId) => {
|
|
93
|
+
try {
|
|
94
|
+
return JSON.parse(store.meta(`receipt:${taskId}`) || "null");
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const mismatch = (kind, detail, against = "agent") => ({
|
|
101
|
+
kind,
|
|
102
|
+
against,
|
|
103
|
+
detail,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// "Tests pass": a run whose receipt shows a passing test after its last change (or a passing test with nothing changed).
|
|
107
|
+
function auditTests(claims, receipt, waits) {
|
|
108
|
+
if (claims.tests !== "passed") return [];
|
|
109
|
+
if (!receipt)
|
|
110
|
+
return waits
|
|
111
|
+
? "tests"
|
|
112
|
+
: [
|
|
113
|
+
mismatch(
|
|
114
|
+
"tests_not_recorded",
|
|
115
|
+
"Said the tests pass, but no test run is recorded for this work."
|
|
116
|
+
),
|
|
117
|
+
];
|
|
118
|
+
if (receipt.verdict === "tested") return [];
|
|
119
|
+
if (receipt.verdict === "no changes" && receipt.testOk === true) return [];
|
|
120
|
+
if (receipt.verdict === "tests failing")
|
|
121
|
+
return [
|
|
122
|
+
mismatch(
|
|
123
|
+
"tests_failing",
|
|
124
|
+
`Said the tests pass, but its last test after its last change, \`${receipt.test}\`, failed.`
|
|
125
|
+
),
|
|
126
|
+
];
|
|
127
|
+
return [
|
|
128
|
+
mismatch(
|
|
129
|
+
"tests_not_recorded",
|
|
130
|
+
"Said the tests pass, but no passing test after its last change is recorded."
|
|
131
|
+
),
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// "Changed these files": a run whose receipt shows files changed.
|
|
136
|
+
function auditFiles(claims, receipt, waits) {
|
|
137
|
+
if (!claims.files_changed?.length) return [];
|
|
138
|
+
if (!receipt)
|
|
139
|
+
return waits
|
|
140
|
+
? "files"
|
|
141
|
+
: [
|
|
142
|
+
mismatch(
|
|
143
|
+
"files_not_recorded",
|
|
144
|
+
`Said it changed ${plural(claims.files_changed.length, "file")}, but no run of it is recorded to show the change.`
|
|
145
|
+
),
|
|
146
|
+
];
|
|
147
|
+
return receipt.verdict === "no changes"
|
|
148
|
+
? [
|
|
149
|
+
mismatch(
|
|
150
|
+
"no_files_changed",
|
|
151
|
+
`Said it changed ${plural(claims.files_changed.length, "file")}, but its run changed none.`
|
|
152
|
+
),
|
|
153
|
+
]
|
|
154
|
+
: [];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// "Sent": each send names its transaction, and a transaction Harness read on chain landed.
|
|
158
|
+
function auditSent(store, claims) {
|
|
159
|
+
return (claims.sent ?? []).flatMap((sent) => {
|
|
160
|
+
const what = sent.what ? ` ${sent.what}` : "";
|
|
161
|
+
if (!sent.hash)
|
|
162
|
+
return [
|
|
163
|
+
mismatch(
|
|
164
|
+
"sent_without_hash",
|
|
165
|
+
`Said it sent${what}, with no transaction hash.`
|
|
166
|
+
),
|
|
167
|
+
];
|
|
168
|
+
const filed = receiptOfTransaction(store, sent.hash);
|
|
169
|
+
if (filed && filed.operation.status !== "succeeded")
|
|
170
|
+
return [
|
|
171
|
+
mismatch(
|
|
172
|
+
"sent_not_confirmed",
|
|
173
|
+
`Said it sent${what} in ${sent.hash}, but Harness's read of the chain shows it ${String(filed.operation.status).replace(/_/g, " ")}.`
|
|
174
|
+
),
|
|
175
|
+
];
|
|
176
|
+
return [];
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Spend reported against what Harness settled against the person's limits for the thread's onchain work.
|
|
181
|
+
function auditSpend(store, task, claims) {
|
|
182
|
+
if (!(claims.spent_usd > 0)) return [];
|
|
183
|
+
const thread = task.parent_id ?? task.id;
|
|
184
|
+
const settledUsd =
|
|
185
|
+
receiptsForThread(store, thread).reduce(
|
|
186
|
+
(sum, receipt) => sum + (receipt.settledMicroUsd ?? 0),
|
|
187
|
+
0
|
|
188
|
+
) / 1_000_000;
|
|
189
|
+
if (!settledUsd)
|
|
190
|
+
return [
|
|
191
|
+
mismatch(
|
|
192
|
+
"spend_unreserved",
|
|
193
|
+
`Reported spending ${usd(claims.spent_usd)}, but nothing is recorded as settled against the person's limits for this work.`
|
|
194
|
+
),
|
|
195
|
+
];
|
|
196
|
+
return claims.spent_usd > settledUsd + SPEND_SLACK(settledUsd)
|
|
197
|
+
? [
|
|
198
|
+
mismatch(
|
|
199
|
+
"spend_over_reserved",
|
|
200
|
+
`Reported spending ${usd(claims.spent_usd)}, more than the ${usd(settledUsd)} settled against the person's limits for this work.`
|
|
201
|
+
),
|
|
202
|
+
]
|
|
203
|
+
: [];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// "Made", for code: a run whose change git would not commit on its thread's branch (threadRuns.mjs) did not make it.
|
|
207
|
+
function auditCommitted(store, task) {
|
|
208
|
+
let left = null;
|
|
209
|
+
try {
|
|
210
|
+
left = JSON.parse(store.meta(`uncommitted:${task.id}`) || "null");
|
|
211
|
+
} catch {}
|
|
212
|
+
if (!left?.files?.length) return [];
|
|
213
|
+
return [
|
|
214
|
+
mismatch(
|
|
215
|
+
"not_committed",
|
|
216
|
+
`Its change is not committed on the thread's branch: ${namedFiles(left.files)}.`
|
|
217
|
+
),
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// A piece of a shared project this run handed on stays its own until the receiver reports (projectPieces.mjs keeps
|
|
222
|
+
// the run's open handoffs, and audits it again when one is reported).
|
|
223
|
+
const openHandoffs = (store, task) => {
|
|
224
|
+
try {
|
|
225
|
+
return JSON.parse(store.meta(`handoffs-open:${task.id}`) || "[]");
|
|
226
|
+
} catch {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
function auditPieces(store, task) {
|
|
232
|
+
if (task.status !== "done") return [];
|
|
233
|
+
const open = store
|
|
234
|
+
.children(task.id)
|
|
235
|
+
.filter((child) => !["done", "failed", "unknown"].includes(child.status));
|
|
236
|
+
const handed = openHandoffs(store, task);
|
|
237
|
+
return [
|
|
238
|
+
...(open.length
|
|
239
|
+
? [
|
|
240
|
+
mismatch(
|
|
241
|
+
"piece_open",
|
|
242
|
+
`Reported done while ${plural(open.length, "sub-piece")} ${open.length === 1 ? "is" : "are"} still open.`
|
|
243
|
+
),
|
|
244
|
+
]
|
|
245
|
+
: []),
|
|
246
|
+
...handed.map((handoff) =>
|
|
247
|
+
mismatch(
|
|
248
|
+
"piece_open",
|
|
249
|
+
`Reported done while the piece it handed to ${handoff.to} has no report yet.`
|
|
250
|
+
)
|
|
251
|
+
),
|
|
252
|
+
];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Compares a reported task's claims with its receipts and writes the result to board_tasks.audit:
|
|
257
|
+
* `{ ok, mismatches: [{ kind, against: "agent" | "dispatcher", detail }], waiting, claims, at }`. `waiting` names claims
|
|
258
|
+
* whose receipt has not been read yet; they are audited again when it is. Null for a task that has not reported.
|
|
259
|
+
*/
|
|
260
|
+
export function auditReport(store, taskId, { at = Date.now() } = {}) {
|
|
261
|
+
const task = store.getTask(taskId);
|
|
262
|
+
if (!task || !REPORTED.includes(task.status) || NOT_RUNS.includes(task.kind))
|
|
263
|
+
return null;
|
|
264
|
+
const kept = auditOf(task) ?? {};
|
|
265
|
+
const claims = kept.claims ?? {};
|
|
266
|
+
const receipt = receiptOf(store, task.id);
|
|
267
|
+
const reportedAt = Date.parse(task.reported_at || task.updated_at) || at;
|
|
268
|
+
// Only a run this computer started reads a receipt, and only for work that finished.
|
|
269
|
+
const receiptComing =
|
|
270
|
+
!receipt &&
|
|
271
|
+
task.status === "done" &&
|
|
272
|
+
MACHINE_RUNNERS.includes(task.target_agent) &&
|
|
273
|
+
Boolean(task.session_id && task.run_workspace) &&
|
|
274
|
+
at - reportedAt < RECEIPT_WAIT_MS;
|
|
275
|
+
const waiting = [];
|
|
276
|
+
const fromReceipt = [auditTests, auditFiles].flatMap((check) => {
|
|
277
|
+
const found = check(claims, receipt, receiptComing);
|
|
278
|
+
if (typeof found === "string") {
|
|
279
|
+
waiting.push(found);
|
|
280
|
+
return [];
|
|
281
|
+
}
|
|
282
|
+
return found;
|
|
283
|
+
});
|
|
284
|
+
const offline = store.meta(`assigned-offline:${task.id}`);
|
|
285
|
+
const mismatches = [
|
|
286
|
+
...fromReceipt,
|
|
287
|
+
...auditSent(store, claims),
|
|
288
|
+
...auditSpend(store, task, claims),
|
|
289
|
+
...auditCommitted(store, task),
|
|
290
|
+
...auditPieces(store, task),
|
|
291
|
+
...(offline
|
|
292
|
+
? [
|
|
293
|
+
mismatch(
|
|
294
|
+
"assigned_offline",
|
|
295
|
+
`Assigned to ${nameOf(task.target_agent)} while ${offline}.`,
|
|
296
|
+
"dispatcher"
|
|
297
|
+
),
|
|
298
|
+
]
|
|
299
|
+
: []),
|
|
300
|
+
];
|
|
301
|
+
const audit = {
|
|
302
|
+
ok: !mismatches.some((found) => found.against === "agent"),
|
|
303
|
+
mismatches,
|
|
304
|
+
waiting,
|
|
305
|
+
...(Object.keys(claims).length ? { claims } : {}),
|
|
306
|
+
at: new Date(at).toISOString(),
|
|
307
|
+
};
|
|
308
|
+
writeAudit(store, task.id, audit);
|
|
309
|
+
return audit;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ── Who audits weekly ──
|
|
313
|
+
|
|
314
|
+
/** Why an agent cannot hold the auditor office, or null. The dispatcher is never the auditor. */
|
|
315
|
+
export function auditorRefusal(
|
|
316
|
+
agentId,
|
|
317
|
+
{ privacy = false, onMachine = () => false } = {}
|
|
318
|
+
) {
|
|
319
|
+
const id = String(agentId || "");
|
|
320
|
+
if (!id || id === "user") return "the auditor is one of the person's agents";
|
|
321
|
+
if (id === "harness")
|
|
322
|
+
return "Harness is the dispatcher, and the auditor audits the dispatcher";
|
|
323
|
+
if (CLOUD_HOMED.includes(id))
|
|
324
|
+
return `${nameOf(id)} works on Harness's server and cannot read the board`;
|
|
325
|
+
if (privacy && !onMachine(id))
|
|
326
|
+
return `on the privacy setting only an agent on this computer audits, and ${nameOf(id)} is not one`;
|
|
327
|
+
// A coding agent here audits in a run Harness starts, read-only; one with no read-only mode would run unrestricted
|
|
328
|
+
// on a brief built from the week's agent-written records.
|
|
329
|
+
if (MACHINE_RUNNERS.includes(id) && !READ_ONLY_AGENTS.has(id))
|
|
330
|
+
return `${nameOf(id)} has no read-only mode, so it cannot audit`;
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// The agent the person appointed auditor (specialties.mjs offices), or null.
|
|
335
|
+
const appointedAuditor = (store) => {
|
|
336
|
+
const entry = offices(store).find((one) => one.id === "auditor");
|
|
337
|
+
return entry?.appointed ? entry.holder : null;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// The week's main makers: the agents with the most finished work, and the models it ran on.
|
|
341
|
+
function makersOf(store, since) {
|
|
342
|
+
const rows = store.db.all(
|
|
343
|
+
`select coalesce(claimed_by, target_agent) as agent, run_model, count(*) as n from board_tasks
|
|
344
|
+
where kind not in ('goal', 'group') and status = 'done' and reported_at >= ? group by 1, 2 order by n desc`,
|
|
345
|
+
[since]
|
|
346
|
+
);
|
|
347
|
+
const counts = new Map();
|
|
348
|
+
const models = new Set();
|
|
349
|
+
for (const row of rows) {
|
|
350
|
+
if (!row.agent) continue;
|
|
351
|
+
counts.set(row.agent, (counts.get(row.agent) ?? 0) + Number(row.n));
|
|
352
|
+
try {
|
|
353
|
+
const model = JSON.parse(row.run_model || "null")?.model;
|
|
354
|
+
if (model) models.add(String(model).toLowerCase());
|
|
355
|
+
} catch {}
|
|
356
|
+
}
|
|
357
|
+
const ranked = [...counts].sort((a, b) => b[1] - a[1]);
|
|
358
|
+
return {
|
|
359
|
+
agents: new Set(ranked.slice(0, 2).map(([agent]) => agent)),
|
|
360
|
+
models,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// ── The week's record, as the weekly auditor reads it ──
|
|
365
|
+
|
|
366
|
+
function weekRecord(store, since) {
|
|
367
|
+
const work = store.db.all(
|
|
368
|
+
`select coalesce(claimed_by, target_agent) as agent, status, count(*) as n from board_tasks
|
|
369
|
+
where kind not in ('goal', 'group') and status in ('done', 'failed') and reported_at >= ? group by 1, 2`,
|
|
370
|
+
[since]
|
|
371
|
+
);
|
|
372
|
+
const byAgent = new Map();
|
|
373
|
+
for (const row of work) {
|
|
374
|
+
if (!row.agent) continue;
|
|
375
|
+
const entry = byAgent.get(row.agent) ?? { done: 0, failed: 0 };
|
|
376
|
+
entry[row.status] += Number(row.n);
|
|
377
|
+
byAgent.set(row.agent, entry);
|
|
378
|
+
}
|
|
379
|
+
const marks = store.db
|
|
380
|
+
.all(
|
|
381
|
+
`select id, coalesce(claimed_by, target_agent) as agent, objective, audit from board_tasks
|
|
382
|
+
where audit is not null and reported_at >= ? order by reported_at`,
|
|
383
|
+
[since]
|
|
384
|
+
)
|
|
385
|
+
.flatMap((row) => {
|
|
386
|
+
const audit = auditOf(row);
|
|
387
|
+
return (audit?.mismatches ?? []).map(
|
|
388
|
+
(found) =>
|
|
389
|
+
`- ${row.id} (${nameOf(row.agent)}, ${found.against === "dispatcher" ? "the dispatcher's routing" : "its report"}): ${found.detail}`
|
|
390
|
+
);
|
|
391
|
+
});
|
|
392
|
+
const routing = store.db.all(
|
|
393
|
+
`select agent, action, count(*) as n from swarm_rounds where at >= ? group by agent, action order by n desc`,
|
|
394
|
+
[since]
|
|
395
|
+
);
|
|
396
|
+
const handedOn = routing
|
|
397
|
+
.filter((row) => row.action === "hand_on" && row.agent)
|
|
398
|
+
.map((row) => `${nameOf(row.agent)} ${row.n}`);
|
|
399
|
+
const undecided = routing
|
|
400
|
+
.filter((row) => row.action == null)
|
|
401
|
+
.reduce((sum, row) => sum + Number(row.n), 0);
|
|
402
|
+
const verdicts = store.db.all(
|
|
403
|
+
`select agent, verdict, count(*) as n from board_reactions where verdict != 'neither' and at >= ? group by agent, verdict`,
|
|
404
|
+
[since]
|
|
405
|
+
);
|
|
406
|
+
const receipts = store.db.all(
|
|
407
|
+
"select outcome, count(*) as n from book_receipts where filed_at >= ? group by outcome",
|
|
408
|
+
[since]
|
|
409
|
+
);
|
|
410
|
+
return {
|
|
411
|
+
work: [...byAgent].map(
|
|
412
|
+
([agent, counts]) =>
|
|
413
|
+
`${nameOf(agent)} ${counts.done} done, ${counts.failed} failed`
|
|
414
|
+
),
|
|
415
|
+
marks,
|
|
416
|
+
handedOn,
|
|
417
|
+
undecided,
|
|
418
|
+
verdicts: verdicts.map(
|
|
419
|
+
(row) => `${nameOf(row.agent)} ${row.verdict} ${row.n}`
|
|
420
|
+
),
|
|
421
|
+
receipts: receipts.map((row) => `${row.outcome} ${row.n}`),
|
|
422
|
+
underperforming: underperforming(store).map(
|
|
423
|
+
(entry) =>
|
|
424
|
+
`${nameOf(entry.agent)} on ${entry.model}: ${entry.accepted} of ${entry.judged} ${entry.specialty} runs accepted`
|
|
425
|
+
),
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function weekBrief(record, from, to) {
|
|
430
|
+
const listed = (title, lines, none) => [
|
|
431
|
+
title,
|
|
432
|
+
...(lines.length ? lines.slice(0, BRIEF_LINES) : [none]),
|
|
433
|
+
...(lines.length > BRIEF_LINES
|
|
434
|
+
? [`(and ${lines.length - BRIEF_LINES} more)`]
|
|
435
|
+
: []),
|
|
436
|
+
];
|
|
437
|
+
return [
|
|
438
|
+
`The swarm's week from ${from.slice(0, 10)} to ${to.slice(0, 10)}, as the board recorded it. You are its auditor this week: read it for what code cannot see, such as work funnelled to one agent, checkers that never send anything back, the same reason for sending work back again and again (a rule the person might set), or specialties at odds with verdicts. Write one short report for the person, at most ten lines, each fact with the task or round it comes from. This is read-only: do not post work, change files or act on anything.`,
|
|
439
|
+
"",
|
|
440
|
+
...listed("Work reported, by agent:", record.work, "No work was reported."),
|
|
441
|
+
"",
|
|
442
|
+
...listed(
|
|
443
|
+
"Audit marks (code compared each report's claims with its receipts):",
|
|
444
|
+
record.marks,
|
|
445
|
+
"No marks."
|
|
446
|
+
),
|
|
447
|
+
"",
|
|
448
|
+
...listed(
|
|
449
|
+
"Harness's routing, work handed on by agent:",
|
|
450
|
+
record.handedOn,
|
|
451
|
+
"Harness handed nothing on."
|
|
452
|
+
),
|
|
453
|
+
`Rounds no one decided: ${record.undecided}.`,
|
|
454
|
+
"",
|
|
455
|
+
...listed(
|
|
456
|
+
"The person's verdicts, by agent:",
|
|
457
|
+
record.verdicts,
|
|
458
|
+
"No verdicts."
|
|
459
|
+
),
|
|
460
|
+
"",
|
|
461
|
+
...listed(
|
|
462
|
+
"Onchain receipts filed in the book, by outcome:",
|
|
463
|
+
record.receipts,
|
|
464
|
+
"No onchain work."
|
|
465
|
+
),
|
|
466
|
+
"",
|
|
467
|
+
...listed(
|
|
468
|
+
"Agents not living up to a specialty:",
|
|
469
|
+
record.underperforming,
|
|
470
|
+
"None."
|
|
471
|
+
),
|
|
472
|
+
].join("\n");
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* The auditor on this computer: every report audited as it lands (`changed`), receipts read late audited again, and
|
|
477
|
+
* the weekly task for the appointed auditor, whose report the person reads with board.audit.week.
|
|
478
|
+
*/
|
|
479
|
+
export function createAuditor({
|
|
480
|
+
store,
|
|
481
|
+
log = () => {},
|
|
482
|
+
notifyBoard = () => {},
|
|
483
|
+
// Why an agent would not pick up work now ("it was not answering"), or null when it would.
|
|
484
|
+
offline = () => null,
|
|
485
|
+
privacy = () => false,
|
|
486
|
+
onMachine = () => false,
|
|
487
|
+
// Agents on this computer the daemon runs (installed and in the lineup), in the order to prefer.
|
|
488
|
+
runners = () => [],
|
|
489
|
+
// The model an agent runs on now, when known, so the default auditor is on a model other than the week's makers'.
|
|
490
|
+
modelOf = () => null,
|
|
491
|
+
// Only the computer that holds the board audits it.
|
|
492
|
+
holdsBoard = () => true,
|
|
493
|
+
folder,
|
|
494
|
+
now = Date.now,
|
|
495
|
+
}) {
|
|
496
|
+
/** Who audits this week: the person's appointment, else an agent off the week's main makers and their models. */
|
|
497
|
+
const auditor = () => {
|
|
498
|
+
const rules = { privacy: privacy(), onMachine };
|
|
499
|
+
const appointed = appointedAuditor(store);
|
|
500
|
+
if (appointed && !auditorRefusal(appointed, rules))
|
|
501
|
+
return { agent: appointed, how: "appointed" };
|
|
502
|
+
const since = new Date(now() - WEEK_MS).toISOString();
|
|
503
|
+
const makers = makersOf(store, since);
|
|
504
|
+
const candidates = runners().filter(
|
|
505
|
+
(agent) => !auditorRefusal(agent, rules)
|
|
506
|
+
);
|
|
507
|
+
const otherModel = (agent) =>
|
|
508
|
+
!makers.models.has(String(modelOf(agent) ?? "").toLowerCase());
|
|
509
|
+
const notMaker = candidates.filter((agent) => !makers.agents.has(agent));
|
|
510
|
+
const agent =
|
|
511
|
+
notMaker.find(otherModel) ?? notMaker[0] ?? candidates[0] ?? null;
|
|
512
|
+
return agent ? { agent, how: "default" } : { agent: null, how: null };
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const weekTask = (task) => {
|
|
516
|
+
try {
|
|
517
|
+
return JSON.parse(store.meta(`audit-week:${task.id}`) || "null");
|
|
518
|
+
} catch {
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
/** Posts the week's audit task for the auditor, now. */
|
|
524
|
+
const postWeek = () => {
|
|
525
|
+
const holder = auditor();
|
|
526
|
+
if (!holder.agent)
|
|
527
|
+
return {
|
|
528
|
+
ok: false,
|
|
529
|
+
error: "no agent here can audit: appoint an auditor in the Swarm map",
|
|
530
|
+
};
|
|
531
|
+
const to = new Date(now()).toISOString();
|
|
532
|
+
const from = new Date(now() - WEEK_MS).toISOString();
|
|
533
|
+
const runs = runners().includes(holder.agent);
|
|
534
|
+
if (runs) fs.mkdirSync(folder, { recursive: true });
|
|
535
|
+
const task = store.postTask({
|
|
536
|
+
objective: `Weekly audit, ${from.slice(0, 10)} to ${to.slice(0, 10)}`,
|
|
537
|
+
brief: weekBrief(weekRecord(store, from), from, to),
|
|
538
|
+
originAgent: "harness",
|
|
539
|
+
targetAgent: holder.agent,
|
|
540
|
+
// The person's appointment is their go for this one read-only run; an agent that runs itself picks it up.
|
|
541
|
+
runWorkspace: runs ? folder : null,
|
|
542
|
+
runAuthorizedBy: runs ? "appointment" : null,
|
|
543
|
+
});
|
|
544
|
+
store.meta(`review:${task.id}`, "1");
|
|
545
|
+
store.meta(
|
|
546
|
+
`audit-week:${task.id}`,
|
|
547
|
+
JSON.stringify({ from, to, how: holder.how })
|
|
548
|
+
);
|
|
549
|
+
store.meta(WEEK_LAST, to);
|
|
550
|
+
log(`weekly audit posted for ${holder.agent} (${holder.how})`);
|
|
551
|
+
notifyBoard(store.getTask(task.id));
|
|
552
|
+
return { ok: true, task: store.getTask(task.id) };
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// The auditor's report, kept for the person exactly as written.
|
|
556
|
+
const keepReport = (task, week) => {
|
|
557
|
+
const report = {
|
|
558
|
+
task: task.id,
|
|
559
|
+
from: week.from,
|
|
560
|
+
to: week.to,
|
|
561
|
+
agent: task.claimed_by ?? task.target_agent,
|
|
562
|
+
status: task.status,
|
|
563
|
+
text: String(task.result || ""),
|
|
564
|
+
at: task.reported_at,
|
|
565
|
+
};
|
|
566
|
+
store.meta(WEEK_REPORT, JSON.stringify(report));
|
|
567
|
+
return report;
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
/** A task changed (notifyBoard): its assignment noted, its report audited, the week's report kept. */
|
|
571
|
+
const changed = (task, previous) => {
|
|
572
|
+
if (!task || NOT_RUNS.includes(task.kind) || !holdsBoard()) return;
|
|
573
|
+
// Assigned now, to an agent that would not pick it up: the dispatcher's routing, audited with the report.
|
|
574
|
+
if (
|
|
575
|
+
!previous &&
|
|
576
|
+
task.status === "open" &&
|
|
577
|
+
task.target_agent &&
|
|
578
|
+
task.origin_agent !== "user" &&
|
|
579
|
+
now() - Date.parse(task.created_at) < 120_000 &&
|
|
580
|
+
!store.meta(`assigned-offline:${task.id}`)
|
|
581
|
+
) {
|
|
582
|
+
const why = offline(task.target_agent);
|
|
583
|
+
if (why) store.meta(`assigned-offline:${task.id}`, why);
|
|
584
|
+
}
|
|
585
|
+
if (!REPORTED.includes(task.status)) return;
|
|
586
|
+
if (previous?.status === task.status && auditOf(task)?.at) return;
|
|
587
|
+
const audit = auditReport(store, task.id, { at: now() });
|
|
588
|
+
if (audit && !audit.ok)
|
|
589
|
+
log(
|
|
590
|
+
`audit of ${task.id}: ${audit.mismatches.map((found) => found.kind).join(", ")}`
|
|
591
|
+
);
|
|
592
|
+
const week = weekTask(task);
|
|
593
|
+
if (week) keepReport(store.getTask(task.id), week);
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// Reports whose receipt was still being read are audited again once it is, or once it is too late to come.
|
|
597
|
+
const sweep = () => {
|
|
598
|
+
if (!holdsBoard()) return;
|
|
599
|
+
for (const row of store.db.all(
|
|
600
|
+
"select id from board_tasks where audit is not null and instr(audit, '\"waiting\":[]') = 0 and instr(audit, '\"waiting\":[') > 0"
|
|
601
|
+
))
|
|
602
|
+
auditReport(store, row.id, { at: now() });
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
const due = () => {
|
|
606
|
+
const last = store.meta(WEEK_LAST);
|
|
607
|
+
// The first week counts from when the auditor first ran here.
|
|
608
|
+
if (!last) {
|
|
609
|
+
store.meta(WEEK_LAST, new Date(now()).toISOString());
|
|
610
|
+
return false;
|
|
611
|
+
}
|
|
612
|
+
return now() - Date.parse(last) >= WEEK_MS;
|
|
613
|
+
};
|
|
614
|
+
const tick = () => {
|
|
615
|
+
if (!holdsBoard() || !due()) return;
|
|
616
|
+
const posted = postWeek();
|
|
617
|
+
if (!posted.ok) log(`weekly audit: ${posted.error}`);
|
|
618
|
+
};
|
|
619
|
+
const weekTimer = setInterval(tick, POLL_MS);
|
|
620
|
+
weekTimer.unref?.();
|
|
621
|
+
const sweepTimer = setInterval(sweep, SWEEP_MS);
|
|
622
|
+
sweepTimer.unref?.();
|
|
623
|
+
setImmediate(tick);
|
|
624
|
+
|
|
625
|
+
/** The person's and agents' audit commands (board.audit.*). */
|
|
626
|
+
const command = (request, agent) => {
|
|
627
|
+
if (request.command === "board.audit.task") {
|
|
628
|
+
const task = store.getTask(String(request.id || ""));
|
|
629
|
+
if (!task) return { ok: false, error: "task not found" };
|
|
630
|
+
return { ok: true, audit: auditOf(task) };
|
|
631
|
+
}
|
|
632
|
+
if (request.command === "board.audit.week") {
|
|
633
|
+
// The weekly report is the person's, and reaches them without the liaison.
|
|
634
|
+
if (agent !== "user" || request.hostedCaller)
|
|
635
|
+
return { ok: false, error: "the weekly audit goes to the person" };
|
|
636
|
+
if (request.run === true) return postWeek();
|
|
637
|
+
let report = null;
|
|
638
|
+
try {
|
|
639
|
+
report = JSON.parse(store.meta(WEEK_REPORT) || "null");
|
|
640
|
+
} catch {}
|
|
641
|
+
return {
|
|
642
|
+
ok: true,
|
|
643
|
+
auditor: auditor(),
|
|
644
|
+
report,
|
|
645
|
+
last: store.meta(WEEK_LAST),
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
return { ok: false, error: `unknown audit command ${request.command}` };
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
return {
|
|
652
|
+
changed,
|
|
653
|
+
// A run's receipt was read after its report: the claims that waited for it are audited now.
|
|
654
|
+
receiptRead: (taskId) => {
|
|
655
|
+
if (holdsBoard()) auditReport(store, taskId, { at: now() });
|
|
656
|
+
},
|
|
657
|
+
command,
|
|
658
|
+
auditor,
|
|
659
|
+
postWeek,
|
|
660
|
+
sweep,
|
|
661
|
+
stop: () => {
|
|
662
|
+
clearInterval(weekTimer);
|
|
663
|
+
clearInterval(sweepTimer);
|
|
664
|
+
},
|
|
665
|
+
};
|
|
666
|
+
}
|