tryharness 0.7.73 → 0.7.76
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/assets/pi-extension.ts +7 -1
- package/cli.mjs +67 -4
- package/lib/actorRoster.mjs +20 -9
- package/lib/adoption.mjs +5 -3
- package/lib/audit.mjs +7 -2
- package/lib/boardFiles.mjs +4 -2
- package/lib/boardMirror.mjs +2 -0
- package/lib/boardProtocol.json +4 -0
- package/lib/bridge.mjs +3 -0
- package/lib/capabilityCheck.mjs +39 -3
- package/lib/coordinator.mjs +480 -117
- package/lib/daemon.mjs +74 -21
- package/lib/dispatcher.mjs +568 -0
- package/lib/goalCommands.mjs +21 -6
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +2 -2
- package/lib/inference.mjs +53 -3
- package/lib/jobAnswers.mjs +7 -6
- package/lib/lessons.mjs +6 -1
- package/lib/library.mjs +83 -7
- package/lib/lineup.mjs +17 -9
- package/lib/localAgentAsk.mjs +68 -29
- package/lib/localStack.mjs +19 -3
- package/lib/localmodels.mjs +59 -25
- package/lib/machineRoutes.mjs +1 -0
- package/lib/manager.mjs +97 -52
- package/lib/mcp-stdio.mjs +14 -7
- package/lib/members.json +5 -1
- package/lib/members.mjs +21 -1
- package/lib/moves.mjs +15 -2
- package/lib/nudge.mjs +5 -0
- package/lib/plans.mjs +2 -1
- package/lib/projects.mjs +16 -9
- package/lib/runAuthority.mjs +2 -1
- package/lib/runBrief.mjs +2 -2
- package/lib/runWorker.mjs +6 -6
- package/lib/snippets.mjs +5 -5
- package/lib/store.mjs +79 -10
- package/lib/swarm.mjs +56 -12
- package/lib/taskCommands.mjs +8 -8
- package/lib/taskRooms.mjs +6 -8
- package/lib/team.mjs +5 -3
- package/lib/threadActions.mjs +6 -30
- package/lib/threadAuthority.mjs +16 -3
- package/lib/threadRuns.mjs +26 -16
- package/lib/verdicts.mjs +2 -2
- package/npm-shrinkwrap.json +14 -14
- package/package.json +4 -4
- package/release.json +50 -49
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { askLocalAgent, DISPATCH_AGENTS } from "./localAgentAsk.mjs";
|
|
5
|
+
import { ensureModels, modelById, modelsPresent } from "./localmodels.mjs";
|
|
6
|
+
|
|
7
|
+
// The dispatcher slot (owner, November 1): "manager - it is its own slot", "lfm slot should act as a dispatcher not an
|
|
8
|
+
// orchestrator", "facilitator is dispatcher". Jev decides who takes the work, where and how; the dispatcher carries the
|
|
9
|
+
// decision through. It writes what needs words (a question to the person, the goal and its checks, a pause, a check-in,
|
|
10
|
+
// a brief between agents, each agent's part of a message to the swarm) and checks results against the person's words
|
|
11
|
+
// (manager.mjs). It never decides who takes the work. Agents get the person's words whole, so delivery needs no words.
|
|
12
|
+
//
|
|
13
|
+
// Any model fills it, as the person picks:
|
|
14
|
+
// - local: the relay's own llama.cpp on this computer's GPU, with base LFM3-Omni-2B (the default) or a GGUF of their own
|
|
15
|
+
// (their fine-tune), loaded in the same slot;
|
|
16
|
+
// - surplus: any model in Harness's catalog, run on Harness's server and paid from the person's Harness account;
|
|
17
|
+
// - agent: one of their coding agents here, on any model it offers, with its tools off;
|
|
18
|
+
// - endpoint: any OpenAI-compatible server they run (llama-server, LM Studio, Ollama, vLLM), on any of their computers.
|
|
19
|
+
//
|
|
20
|
+
// JOBS below is the slot's whole contract: the instructions each job gets and the JSON it answers with. A fine-tune of
|
|
21
|
+
// the dispatcher is trained on these jobs.
|
|
22
|
+
|
|
23
|
+
const BASE_MODEL = "dispatcher";
|
|
24
|
+
// The person's own GGUF in the relay's llama.cpp: one model at a time, under this id beside the base model.
|
|
25
|
+
const OWN_MODEL = "dispatcher-own";
|
|
26
|
+
const PROVIDERS = ["local", "surplus", "agent", "endpoint"];
|
|
27
|
+
export const DEFAULT_DISPATCHER = { provider: "local", model: BASE_MODEL };
|
|
28
|
+
// LFM3's own settings for agents and structured work (its model card: temperature 0.1, top_k 50, repetition 1.1).
|
|
29
|
+
const SAMPLING = { temperature: 0.1, top_k: 50, repeat_penalty: 1.1 };
|
|
30
|
+
const MAX_TOKENS = 2_400;
|
|
31
|
+
// What a job reads of the thread: its newest posts, each at most this long. The person's message a job is about is
|
|
32
|
+
// always given whole.
|
|
33
|
+
const TRANSCRIPT_POSTS = 12;
|
|
34
|
+
const POST_CHARS = 1_500;
|
|
35
|
+
|
|
36
|
+
const FAITHFUL =
|
|
37
|
+
'Refer to earlier posts by their number ("see post 37") instead of restating figures, positions, file ids or names. Never say the person approved or chose something unless you quote their own words from `transcript`.';
|
|
38
|
+
|
|
39
|
+
export const JOBS = {
|
|
40
|
+
question: `You are the dispatcher for a person's agents. Write one message to the person saying what the thread needs from them, based on \`transcript\` and \`request\`; \`category\` says what it is about (spend: money above what the thread may spend; outward: something outward-facing or hard to undo; secret: something only they have; scope: what the goal is for). If several decisions are open, put them together in this one message, each with lettered options and the option the swarm will take if they have no preference. Say in a few words what was already checked. If \`category\` is secret, never ask for the secret itself in the thread: ask them to give it privately. ${FAITHFUL} Plain words, no greeting. Also give each decision as a choice the person can answer with one tap: \`question\` in a few words, two to four short \`options\`, and \`recommended\`, the index of the option the swarm will take with no preference (or null). Return JSON {"text": string, "choices": [{"question": string, "options": [string], "recommended": number | null}]}.`,
|
|
41
|
+
goal: 'You are the dispatcher for a person\'s agents. From the person\'s own posts in `transcript` and `request`, and `goal` (the current version) when given, write the goal of the thread as it stands now. `statement`: the whole aim in one to three plain sentences in the person\'s terms, including what matters most to them (speed, quality, cost, the kind of result) where they said so. `criteria`: one to five checks that would show the goal is reached, each one sentence that a person or code can verify about the result the person wants (e.g. "The installed Python version is named in the thread"), never about the request itself or what the transcript contains. Keep a current criterion word for word when it still applies. `title`: what the work is, in two to six plain words the person would use, with no ending punctuation (e.g. "Find the installed Python version"). Return JSON {"title": string, "statement": string, "criteria": [string]}.',
|
|
42
|
+
open_item:
|
|
43
|
+
'You are the dispatcher for a person\'s agents. In one or two plain sentences, record the part of the request that could not be done: what it was, what was tried and why it did not work, based on `transcript` and `failed_jobs`. Return JSON {"text": string}.',
|
|
44
|
+
pause:
|
|
45
|
+
'You are the dispatcher for a person\'s agents. In one short sentence, say why the work cannot go on without the person, based on `transcript` and `failed_jobs`. Return JSON {"text": string}.',
|
|
46
|
+
check_in: `You are the dispatcher for a person's agents. The thread will come back on its own \`when\`. In one or two plain sentences, say what to look at then and what the next step would be, based on \`transcript\`. ${FAITHFUL} Return JSON {"text": string}.`,
|
|
47
|
+
brief: `You are the dispatcher for a person's agents. Another agent's work in \`transcript\` leads to the next step, and Jev picked \`agent\` to take it, in \`mode\` (work: change things; look: find out and answer, changing nothing; review: check work another agent did, changing nothing). Write that agent's brief: what to do next (never report a result or claim anything was done) toward \`request\` (and \`goal\`, when given). If \`decide_without_asking\` is true, tell the agent to decide the open choice with its best default, say in the thread which it chose and why, and carry on. If \`already_tried\` is not empty, say plainly what was tried and that it did not work, and ask for a different approach. If \`team_role\` is given, the agent works as that role on the thread's route: write the brief for that role. If \`sent_back\` is true, the route's check in \`transcript\` sent the work back: name the post and say what to fix. ${FAITHFUL} Plain sentences, no greeting, no sign-off, under 150 words. Never grant permissions or mention money. Return JSON {"text": string}.`,
|
|
48
|
+
parts: `You are the dispatcher for a person's agents. The person wrote \`request\` to their swarm, and Jev picked each agent in \`parts\` to take part, each in its \`mode\` (work: change things; look: find out and answer, changing nothing; review: check work another agent did, changing nothing). Every agent gets the person's message whole; write each one's part beside it: what that agent should do of what the person asked, using \`transcript\` and its card in \`agents\`, so that no two agents do the same thing. Say only what each should do: never report a result, a finding or anything as done. Where a part has a \`role\`, the agent works as that role on the thread's team. ${FAITHFUL} Plain sentences, no greeting, under 80 words for each agent. Never grant permissions or mention money. Return JSON {"parts": [{"agent": the agent's id from \`parts\`, "text": string}]}.`,
|
|
49
|
+
// The swarm's manager (manager.mjs): what one of the person's messages is, and where their asks and the goal's checks
|
|
50
|
+
// stand after an agent's report.
|
|
51
|
+
judge_message: `You keep a person's swarm of agents to what the person asked. The JSON below has the person's newest message (\`post\`), the posts before it (\`transcript\`), and the person's open asks (\`asks\`). Say what the message is. Answer with one JSON object only: {"kind": "work" (it asks the agents to do something or produce a result not already in \`asks\`) | "question" (it asks the agents something and wants an answer) | "idea" (the person is exploring an idea or talking through the agents' reasoning, not asking for it to be done) | "answer" (it answers a question or approves a choice put to the person) | "chat" (anything else), "steers": the index in \`asks\` of the ask it changes, corrects or narrows, or null, "withdraws": true if it drops that ask, else false}.`,
|
|
52
|
+
judge_report: `You keep a person's swarm of agents to what the person asked. The JSON below has an agent's report (\`post\`), the posts before it (\`transcript\`), and one item to judge it against (\`item\`): an ask of the person's (kind work or question, with the person's own words, newest word wins) or a check of the thread's goal (kind check). Answer with one JSON object only: {"state": "done" | "in_progress" | "ignored" | "contradicted" (ignored: reported finished without doing it or only part; contradicted: did something different, or claims something the thread does not show), "shown": true if something other than an agent saying so shows it done (a file, commit, command run with output, link, the result itself), else false, "taste": true if only the person's own judgment can settle it, else false}. For kind question, "done" means it was answered; for kind check, that the report shows the check reached.`,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** The JSON object in a model's answer: the whole answer, or the object inside prose or a code fence. */
|
|
56
|
+
export const objectIn = (text) => {
|
|
57
|
+
if (text && typeof text === "object" && !Array.isArray(text)) return text;
|
|
58
|
+
const trimmed = String(text ?? "").trim();
|
|
59
|
+
const from = trimmed.indexOf("{");
|
|
60
|
+
const to = trimmed.lastIndexOf("}");
|
|
61
|
+
for (const candidate of [
|
|
62
|
+
trimmed,
|
|
63
|
+
from >= 0 ? trimmed.slice(from, to + 1) : "",
|
|
64
|
+
])
|
|
65
|
+
try {
|
|
66
|
+
const value = JSON.parse(candidate);
|
|
67
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
68
|
+
return value;
|
|
69
|
+
} catch {
|
|
70
|
+
/* the next candidate */
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const clean = (value, max) =>
|
|
76
|
+
typeof value === "string" ? value.trim().slice(0, max) : "";
|
|
77
|
+
|
|
78
|
+
/** A setting as the person gave it, or the reason it cannot be one. */
|
|
79
|
+
export function dispatcherSetting(value) {
|
|
80
|
+
const provider = String(value?.provider ?? "");
|
|
81
|
+
if (!PROVIDERS.includes(provider))
|
|
82
|
+
return { error: `pick one of ${PROVIDERS.join(", ")}` };
|
|
83
|
+
const model = value.model ? String(value.model).slice(0, 200) : null;
|
|
84
|
+
if (provider === "local") {
|
|
85
|
+
if (!value.file) return { provider, model: BASE_MODEL };
|
|
86
|
+
const file = path.resolve(String(value.file));
|
|
87
|
+
if (!file.endsWith(".gguf"))
|
|
88
|
+
return { error: "your own dispatcher model is a .gguf file" };
|
|
89
|
+
return { provider, model: OWN_MODEL, file };
|
|
90
|
+
}
|
|
91
|
+
if (provider === "surplus")
|
|
92
|
+
return model
|
|
93
|
+
? { provider, model }
|
|
94
|
+
: { error: "name the Surplus model to use" };
|
|
95
|
+
if (provider === "endpoint") {
|
|
96
|
+
let url;
|
|
97
|
+
try {
|
|
98
|
+
url = new URL(String(value.url ?? ""));
|
|
99
|
+
} catch {
|
|
100
|
+
return {
|
|
101
|
+
error: "give the endpoint's address, like http://localhost:8080/v1",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (!["http:", "https:"].includes(url.protocol))
|
|
105
|
+
return {
|
|
106
|
+
error: "the endpoint's address starts with http:// or https://",
|
|
107
|
+
};
|
|
108
|
+
return { provider, url: url.toString().replace(/\/$/, ""), model };
|
|
109
|
+
}
|
|
110
|
+
const agent = String(value.agent ?? "");
|
|
111
|
+
if (!DISPATCH_AGENTS.includes(agent))
|
|
112
|
+
return { error: `the agent is one of ${DISPATCH_AGENTS.join(", ")}` };
|
|
113
|
+
return { provider, agent, model };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** What fills the slot, in a few words. */
|
|
117
|
+
export const describeDispatcher = (setting, nameOf = (id) => id) =>
|
|
118
|
+
setting.provider === "local"
|
|
119
|
+
? setting.file
|
|
120
|
+
? `your own model ${path.basename(setting.file)}, in the relay's llama.cpp on this computer`
|
|
121
|
+
: "LFM3-Omni 2B (base), in the relay's llama.cpp on this computer"
|
|
122
|
+
: setting.provider === "surplus"
|
|
123
|
+
? `${setting.model} through Surplus on Harness's server, paid from your Harness account`
|
|
124
|
+
: setting.provider === "endpoint"
|
|
125
|
+
? `${setting.model ?? "the model"} at ${setting.url}`
|
|
126
|
+
: `${nameOf(setting.agent)}${setting.model ? ` on ${setting.model}` : ""}, on this computer with its tools off`;
|
|
127
|
+
|
|
128
|
+
/** The person's own GGUF as a model the relay's llama.cpp can load: one file, under OWN_MODEL. */
|
|
129
|
+
const ownEntry = (file) => {
|
|
130
|
+
const size = fs.statSync(file).size;
|
|
131
|
+
return {
|
|
132
|
+
id: OWN_MODEL,
|
|
133
|
+
name: `Your dispatcher model (${path.basename(file)})`,
|
|
134
|
+
purpose: "Dispatches the swarm's work",
|
|
135
|
+
tier: "extended",
|
|
136
|
+
// The weights and a 16K window, as the base model measured (760 MB for 1.1 GB of weights and its projector).
|
|
137
|
+
memoryMb: Math.ceil(size / 1_048_576) + 300,
|
|
138
|
+
runtime: "llama",
|
|
139
|
+
files: [{ path: "model.gguf", size }],
|
|
140
|
+
llama: { c: 16384 },
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Where the person's own models are found for the Swarm tab's list: these folders, a few levels down. A path typed
|
|
145
|
+
// there reaches any other (no file dialog, owner rule: folders are typed or listed, never opened by the OS).
|
|
146
|
+
const MODEL_FOLDERS = ["models", "Downloads", "Documents", "Desktop"];
|
|
147
|
+
const MODEL_DEPTH = 3;
|
|
148
|
+
const SKIPPED = new Set(["node_modules", ".git", "Library", ".Trash"]);
|
|
149
|
+
|
|
150
|
+
/** The .gguf model files under the person's usual folders, largest first; projectors (mmproj) are parts, not models. */
|
|
151
|
+
export function ownModelFiles(home = os.homedir()) {
|
|
152
|
+
const found = [];
|
|
153
|
+
const walk = (dir, depth) => {
|
|
154
|
+
let entries = [];
|
|
155
|
+
try {
|
|
156
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
157
|
+
} catch {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
if (entry.name.startsWith(".") || SKIPPED.has(entry.name)) continue;
|
|
162
|
+
const at = path.join(dir, entry.name);
|
|
163
|
+
if (entry.isDirectory() && depth < MODEL_DEPTH) walk(at, depth + 1);
|
|
164
|
+
else if (
|
|
165
|
+
entry.isFile() &&
|
|
166
|
+
entry.name.endsWith(".gguf") &&
|
|
167
|
+
!/^mmproj/i.test(entry.name)
|
|
168
|
+
)
|
|
169
|
+
found.push({ file: at, bytes: fs.statSync(at).size });
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
for (const folder of MODEL_FOLDERS) walk(path.join(home, folder), 1);
|
|
173
|
+
return found.sort((a, b) => b.bytes - a.bytes).slice(0, 20);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function createDispatcher({
|
|
177
|
+
root,
|
|
178
|
+
// The person's choice, read and written where the relay keeps its settings.
|
|
179
|
+
read,
|
|
180
|
+
write: save,
|
|
181
|
+
inference,
|
|
182
|
+
// Whether the person let the relay download models (setup's "localModels": "download"), as the core models ask.
|
|
183
|
+
wanted = () => true,
|
|
184
|
+
installedAgents = () => [],
|
|
185
|
+
// Harness's server, for a Surplus model: one job, answered and paid there.
|
|
186
|
+
askHarness = null,
|
|
187
|
+
log = () => {},
|
|
188
|
+
ask = askLocalAgent,
|
|
189
|
+
ensure = ensureModels,
|
|
190
|
+
request = fetch,
|
|
191
|
+
}) {
|
|
192
|
+
const setting = () => {
|
|
193
|
+
const kept = dispatcherSetting(read() ?? DEFAULT_DISPATCHER);
|
|
194
|
+
return kept.error ? DEFAULT_DISPATCHER : kept;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
let download = null;
|
|
198
|
+
// The base model comes down in the background the first time the slot needs it; setup does not wait for it.
|
|
199
|
+
const fetchModel = (id) => {
|
|
200
|
+
if (modelsPresent(root, [id])) return Promise.resolve();
|
|
201
|
+
if (!wanted())
|
|
202
|
+
return Promise.reject(
|
|
203
|
+
new Error(
|
|
204
|
+
"this computer does not download the dispatcher's model (it is not home, or setup's local models are off)"
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
download ||= ensure({ root, ids: [id], log })
|
|
208
|
+
.then(() => log(`dispatcher: ${modelById(id)?.name ?? id} is ready`))
|
|
209
|
+
.catch((error) => {
|
|
210
|
+
log(`dispatcher: the download failed (${error.message})`);
|
|
211
|
+
throw error;
|
|
212
|
+
})
|
|
213
|
+
.finally(() => {
|
|
214
|
+
download = null;
|
|
215
|
+
});
|
|
216
|
+
return download;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// The person's own GGUF, linked into the relay's model folder and given to its llama.cpp in place of the base model.
|
|
220
|
+
const loadOwn = async (file) => {
|
|
221
|
+
if (!fs.existsSync(file)) throw new Error(`${file} is not there`);
|
|
222
|
+
const entry = ownEntry(file);
|
|
223
|
+
const linked = path.join(root, OWN_MODEL, "model.gguf");
|
|
224
|
+
fs.mkdirSync(path.dirname(linked), { recursive: true });
|
|
225
|
+
let current = null;
|
|
226
|
+
try {
|
|
227
|
+
current = fs.readlinkSync(linked);
|
|
228
|
+
} catch {
|
|
229
|
+
/* not linked yet */
|
|
230
|
+
}
|
|
231
|
+
if (current !== file) {
|
|
232
|
+
fs.rmSync(linked, { force: true });
|
|
233
|
+
fs.symlinkSync(file, linked);
|
|
234
|
+
}
|
|
235
|
+
await inference.useModel(entry);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const chatLocal = async (current, messages) => {
|
|
239
|
+
if (current.file) await loadOwn(current.file);
|
|
240
|
+
else await fetchModel(current.model);
|
|
241
|
+
const reply = await inference.chat(
|
|
242
|
+
current.model,
|
|
243
|
+
{
|
|
244
|
+
messages,
|
|
245
|
+
...SAMPLING,
|
|
246
|
+
max_tokens: MAX_TOKENS,
|
|
247
|
+
response_format: { type: "json_object" },
|
|
248
|
+
},
|
|
249
|
+
{ lane: "interactive" }
|
|
250
|
+
);
|
|
251
|
+
return String(reply?.choices?.[0]?.message?.content ?? "");
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const chatEndpoint = async (current, messages) => {
|
|
255
|
+
const response = await request(`${current.url}/chat/completions`, {
|
|
256
|
+
method: "POST",
|
|
257
|
+
headers: { "content-type": "application/json" },
|
|
258
|
+
body: JSON.stringify({
|
|
259
|
+
...(current.model ? { model: current.model } : {}),
|
|
260
|
+
messages,
|
|
261
|
+
temperature: SAMPLING.temperature,
|
|
262
|
+
max_tokens: MAX_TOKENS,
|
|
263
|
+
response_format: { type: "json_object" },
|
|
264
|
+
}),
|
|
265
|
+
signal: AbortSignal.timeout(120_000),
|
|
266
|
+
});
|
|
267
|
+
const text = await response.text();
|
|
268
|
+
if (!response.ok)
|
|
269
|
+
throw new Error(
|
|
270
|
+
`the endpoint answered HTTP ${response.status}: ${text.slice(0, 200)}`
|
|
271
|
+
);
|
|
272
|
+
return String(JSON.parse(text)?.choices?.[0]?.message?.content ?? "");
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/** One job's answer as text: its instructions and context in, the model's answer out. */
|
|
276
|
+
const writeJob = async ({ system, prompt }) => {
|
|
277
|
+
const current = setting();
|
|
278
|
+
const messages = [
|
|
279
|
+
{ role: "system", content: String(system) },
|
|
280
|
+
{ role: "user", content: String(prompt) },
|
|
281
|
+
];
|
|
282
|
+
if (current.provider === "local") return chatLocal(current, messages);
|
|
283
|
+
if (current.provider === "endpoint") return chatEndpoint(current, messages);
|
|
284
|
+
if (current.provider === "agent") {
|
|
285
|
+
if (!installedAgents().includes(current.agent))
|
|
286
|
+
throw new Error(`${current.agent} is not installed on this computer`);
|
|
287
|
+
const reply = await ask(current.agent, `${system}\n\n${prompt}`, {
|
|
288
|
+
model: current.model,
|
|
289
|
+
});
|
|
290
|
+
return JSON.stringify(reply);
|
|
291
|
+
}
|
|
292
|
+
if (!askHarness) throw new Error("Harness's server is not reachable");
|
|
293
|
+
const reply = await askHarness(
|
|
294
|
+
"dispatch_write",
|
|
295
|
+
{ model: current.model, system: String(system), prompt: String(prompt) },
|
|
296
|
+
180_000
|
|
297
|
+
);
|
|
298
|
+
if (!reply?.ok)
|
|
299
|
+
throw new Error(reply?.error || "Harness's server did not answer");
|
|
300
|
+
return String((reply.result ?? reply).text ?? "");
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
/** One job's JSON answer, or an error saying why there is none. */
|
|
304
|
+
const job = async (system, context, accept = () => true) => {
|
|
305
|
+
const output = objectIn(
|
|
306
|
+
await writeJob({ system, prompt: JSON.stringify(context) })
|
|
307
|
+
);
|
|
308
|
+
if (!output || !accept(output))
|
|
309
|
+
throw new Error("the dispatcher's model answered in the wrong shape");
|
|
310
|
+
return output;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const text = async (kind, context, max = 4_000) => {
|
|
314
|
+
const output = await job(JOBS[kind], context, (each) =>
|
|
315
|
+
Boolean(clean(each.text, max))
|
|
316
|
+
);
|
|
317
|
+
return clean(output.text, max);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Jev's decision with its words, written here: what needs words gets them from the dispatcher, and anything a
|
|
322
|
+
* coordinator wrote before is replaced, so every word in the thread is from what signs it. `facts` is the round's
|
|
323
|
+
* input; `answering` the person's message the round answers, or null. A job that fails leaves a plain line from code
|
|
324
|
+
* and its reason in `failed`, which the thread is told.
|
|
325
|
+
*/
|
|
326
|
+
const words = async (given, facts, answering = null) => {
|
|
327
|
+
const decision = { ...given };
|
|
328
|
+
const failed = [];
|
|
329
|
+
const posts = (facts?.transcript ?? []).slice(-TRANSCRIPT_POSTS);
|
|
330
|
+
const newestPerson = [...posts]
|
|
331
|
+
.reverse()
|
|
332
|
+
.find((post) => post.author === "user");
|
|
333
|
+
const request = answering ?? newestPerson ?? null;
|
|
334
|
+
const base = {
|
|
335
|
+
thread: { title: facts?.thread?.title ?? "" },
|
|
336
|
+
request: request ? { seq: request.seq, text: request.text } : null,
|
|
337
|
+
...(facts?.goal ? { goal: facts.goal } : {}),
|
|
338
|
+
transcript: posts.map(({ seq, author, kind, text }) => ({
|
|
339
|
+
seq,
|
|
340
|
+
author,
|
|
341
|
+
kind,
|
|
342
|
+
text: String(text ?? "").slice(0, POST_CHARS),
|
|
343
|
+
})),
|
|
344
|
+
};
|
|
345
|
+
const failedJobs = facts?.progress?.failedJobs ?? [];
|
|
346
|
+
// The role an agent holds on the thread's route, at the step the work moves to when Jev named one.
|
|
347
|
+
const roleOf = (agent, step) =>
|
|
348
|
+
(facts?.team?.steps ?? []).find(
|
|
349
|
+
(each) => each.agents?.includes(agent) && (!step || each.id === step)
|
|
350
|
+
)?.role ?? null;
|
|
351
|
+
const attempt = async (what, run, fallback) => {
|
|
352
|
+
try {
|
|
353
|
+
return await run();
|
|
354
|
+
} catch (error) {
|
|
355
|
+
failed.push(`${what} (${error.message})`);
|
|
356
|
+
return fallback;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
if (decision.goal || decision.goalDue) {
|
|
361
|
+
delete decision.goalDue;
|
|
362
|
+
const goal = await attempt(
|
|
363
|
+
"the goal",
|
|
364
|
+
async () => {
|
|
365
|
+
const output = await job(JOBS.goal, base, (each) => {
|
|
366
|
+
const criteria = Array.isArray(each.criteria) ? each.criteria : [];
|
|
367
|
+
return (
|
|
368
|
+
Boolean(clean(each.statement, 4_000)) &&
|
|
369
|
+
criteria.some((c) => clean(c, 500))
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
const title = clean(output.title, 64);
|
|
373
|
+
return {
|
|
374
|
+
statement: clean(output.statement, 4_000),
|
|
375
|
+
criteria: output.criteria
|
|
376
|
+
.map((criterion) => clean(criterion, 500))
|
|
377
|
+
.filter(Boolean)
|
|
378
|
+
.slice(0, 5),
|
|
379
|
+
...(title ? { title } : {}),
|
|
380
|
+
};
|
|
381
|
+
},
|
|
382
|
+
null
|
|
383
|
+
);
|
|
384
|
+
if (goal) decision.goal = goal;
|
|
385
|
+
else delete decision.goal;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (decision.openItem !== undefined || decision.loop === "stuck_on_part") {
|
|
389
|
+
const item = await attempt(
|
|
390
|
+
"what could not be done",
|
|
391
|
+
() => text("open_item", { ...base, failed_jobs: failedJobs }, 2_000),
|
|
392
|
+
null
|
|
393
|
+
);
|
|
394
|
+
if (item) decision.openItem = item;
|
|
395
|
+
else delete decision.openItem;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (decision.action === "pause")
|
|
399
|
+
decision.text = await attempt(
|
|
400
|
+
"why the work stopped",
|
|
401
|
+
() => text("pause", { ...base, failed_jobs: failedJobs }, 300),
|
|
402
|
+
"Jev stopped this thread for you."
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
if (decision.action === "ask_person") {
|
|
406
|
+
const asked = await attempt(
|
|
407
|
+
"the question for you",
|
|
408
|
+
async () => {
|
|
409
|
+
const output = await job(
|
|
410
|
+
JOBS.question,
|
|
411
|
+
{ ...base, category: decision.category ?? null },
|
|
412
|
+
(each) => Boolean(clean(each.text, 4_000))
|
|
413
|
+
);
|
|
414
|
+
return { text: clean(output.text, 4_000), choices: output.choices };
|
|
415
|
+
},
|
|
416
|
+
null
|
|
417
|
+
);
|
|
418
|
+
decision.text =
|
|
419
|
+
asked?.text ??
|
|
420
|
+
`Jev decided this needs you (${decision.category ?? "a decision"}), from post ${request?.seq ?? "the newest"} on.`;
|
|
421
|
+
if (asked?.choices) decision.choices = asked.choices;
|
|
422
|
+
else delete decision.choices;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (decision.checkIn?.time)
|
|
426
|
+
decision.checkIn = {
|
|
427
|
+
...decision.checkIn,
|
|
428
|
+
text: await attempt(
|
|
429
|
+
"the check-in",
|
|
430
|
+
() =>
|
|
431
|
+
text("check_in", {
|
|
432
|
+
...base,
|
|
433
|
+
when: String(decision.checkIn.time).replaceAll("_", " "),
|
|
434
|
+
}),
|
|
435
|
+
`See where the thread stands since post ${posts.at(-1)?.seq ?? 0} and take the next step.`
|
|
436
|
+
),
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// Answering the person, the agent gets their words whole and a routing line from code (coordinator.mjs); between
|
|
440
|
+
// agents, the dispatcher writes the brief.
|
|
441
|
+
if (decision.action === "hand_on" && !answering)
|
|
442
|
+
decision.text = await attempt(
|
|
443
|
+
"the brief",
|
|
444
|
+
() =>
|
|
445
|
+
text("brief", {
|
|
446
|
+
...base,
|
|
447
|
+
agent: decision.agent,
|
|
448
|
+
mode: decision.mode ?? "work",
|
|
449
|
+
...(decision.decide ? { decide_without_asking: true } : {}),
|
|
450
|
+
...(decision.loop === "repeating" ||
|
|
451
|
+
decision.loop === "stuck_on_part"
|
|
452
|
+
? { already_tried: failedJobs }
|
|
453
|
+
: {}),
|
|
454
|
+
...(roleOf(decision.agent, decision.team?.step)
|
|
455
|
+
? {
|
|
456
|
+
team_role: {
|
|
457
|
+
role: roleOf(decision.agent, decision.team?.step),
|
|
458
|
+
},
|
|
459
|
+
}
|
|
460
|
+
: {}),
|
|
461
|
+
...(decision.team?.outcome === "sent_back"
|
|
462
|
+
? { sent_back: true }
|
|
463
|
+
: {}),
|
|
464
|
+
}),
|
|
465
|
+
`Take the next step toward the person's message${request ? ` #${request.seq}` : ""}, from post ${posts.at(-1)?.seq ?? 0} on. Read the thread for the details.`
|
|
466
|
+
);
|
|
467
|
+
else if (decision.action === "hand_on") decision.text = "";
|
|
468
|
+
|
|
469
|
+
// One agent answering the person needs no part: it has their words whole. Parts split a message among several.
|
|
470
|
+
if (
|
|
471
|
+
decision.action === "assemble" &&
|
|
472
|
+
Array.isArray(decision.parts) &&
|
|
473
|
+
decision.parts.length === 1 &&
|
|
474
|
+
answering
|
|
475
|
+
)
|
|
476
|
+
decision.parts = decision.parts.map((part) => ({ ...part, text: "" }));
|
|
477
|
+
else if (decision.action === "assemble" && Array.isArray(decision.parts)) {
|
|
478
|
+
const picked = decision.parts.map(({ agent, mode, role }) => ({
|
|
479
|
+
agent,
|
|
480
|
+
mode: mode ?? "work",
|
|
481
|
+
...(role ? { role } : {}),
|
|
482
|
+
}));
|
|
483
|
+
const worded = await attempt(
|
|
484
|
+
"each agent's part",
|
|
485
|
+
async () => {
|
|
486
|
+
const output = await job(
|
|
487
|
+
JOBS.parts,
|
|
488
|
+
{
|
|
489
|
+
...base,
|
|
490
|
+
parts: picked,
|
|
491
|
+
agents: (facts?.candidates ?? [])
|
|
492
|
+
.filter((candidate) =>
|
|
493
|
+
picked.some((each) => each.agent === candidate.id)
|
|
494
|
+
)
|
|
495
|
+
.map(({ id, name, about }) => ({ id, name, about })),
|
|
496
|
+
},
|
|
497
|
+
(each) => Array.isArray(each.parts)
|
|
498
|
+
);
|
|
499
|
+
return new Map(
|
|
500
|
+
output.parts
|
|
501
|
+
.filter((part) => typeof part?.agent === "string")
|
|
502
|
+
.map((part) => [part.agent, clean(part.text, 2_000)])
|
|
503
|
+
);
|
|
504
|
+
},
|
|
505
|
+
new Map()
|
|
506
|
+
);
|
|
507
|
+
decision.parts = decision.parts.map((part) => ({
|
|
508
|
+
...part,
|
|
509
|
+
text: worded.get(part.agent) ?? "",
|
|
510
|
+
}));
|
|
511
|
+
}
|
|
512
|
+
return { decision, failed };
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
/** A judging job for the manager (manager.mjs): "message" or "report", its context in, its JSON out. */
|
|
516
|
+
const judge = (kind, context) => job(JOBS[`judge_${kind}`], context);
|
|
517
|
+
|
|
518
|
+
/** What the Swarm tab shows: the setting, whether its model is here to run, and the person's own model files. */
|
|
519
|
+
const state = () => {
|
|
520
|
+
const current = setting();
|
|
521
|
+
const ready =
|
|
522
|
+
current.provider !== "local"
|
|
523
|
+
? true
|
|
524
|
+
: current.file
|
|
525
|
+
? fs.existsSync(current.file)
|
|
526
|
+
: modelsPresent(root, [current.model]);
|
|
527
|
+
const entry = modelById(BASE_MODEL);
|
|
528
|
+
return {
|
|
529
|
+
setting: current,
|
|
530
|
+
ready,
|
|
531
|
+
downloading: Boolean(download),
|
|
532
|
+
baseBytes: (entry?.files ?? []).reduce((sum, file) => sum + file.size, 0),
|
|
533
|
+
ownFiles: ownModelFiles(),
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
return {
|
|
538
|
+
setting,
|
|
539
|
+
state,
|
|
540
|
+
set(value) {
|
|
541
|
+
const next = dispatcherSetting(value);
|
|
542
|
+
if (next.error) return { ok: false, error: next.error };
|
|
543
|
+
if (next.file && !fs.existsSync(next.file))
|
|
544
|
+
return { ok: false, error: `${next.file} is not there` };
|
|
545
|
+
save(next);
|
|
546
|
+
if (next.provider === "local" && !next.file)
|
|
547
|
+
void fetchModel(next.model).catch(() => {});
|
|
548
|
+
return { ok: true, setting: next };
|
|
549
|
+
},
|
|
550
|
+
// The Surplus models the person may put in the slot, from Harness's server; null when it cannot say.
|
|
551
|
+
async surplusModels() {
|
|
552
|
+
if (!askHarness) return null;
|
|
553
|
+
const reply = await askHarness("dispatch_models", {}, 15_000).catch(
|
|
554
|
+
() => null
|
|
555
|
+
);
|
|
556
|
+
return reply?.ok ? ((reply.result ?? reply).models ?? null) : null;
|
|
557
|
+
},
|
|
558
|
+
words,
|
|
559
|
+
judge,
|
|
560
|
+
write: writeJob,
|
|
561
|
+
// Started with the relay, so the base model is usually down before the first round needs it.
|
|
562
|
+
warm() {
|
|
563
|
+
const current = setting();
|
|
564
|
+
if (current.provider === "local" && !current.file)
|
|
565
|
+
void fetchModel(current.model).catch(() => {});
|
|
566
|
+
},
|
|
567
|
+
};
|
|
568
|
+
}
|
package/lib/goalCommands.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { stopAgentTask } from "./runner.mjs";
|
|
|
5
5
|
import { notifyDesktop } from "./notify.mjs";
|
|
6
6
|
import { secretReason } from "./secrets.mjs";
|
|
7
7
|
import { provenanceFor } from "./provenance.mjs";
|
|
8
|
-
import { TAGGED_ONLY, nameOf } from "./members.mjs";
|
|
8
|
+
import { DISPATCHER, TAGGED_ONLY, nameOf } from "./members.mjs";
|
|
9
9
|
import { picksIn } from "./models.mjs";
|
|
10
10
|
import { roomPage } from "./roomPage.mjs";
|
|
11
11
|
import { FROM_MACHINE, withComputer } from "./machineRoutes.mjs";
|
|
@@ -79,11 +79,14 @@ export function createGoalCommands({
|
|
|
79
79
|
reachable.includes(name)
|
|
80
80
|
);
|
|
81
81
|
if (!team.every(validAgent)) return { ok: false, error: "invalid team" };
|
|
82
|
+
// The dispatcher facilitates unless the person names an agent to (owner, November 1: "facilitator is dispatcher").
|
|
83
|
+
// It is not an agent: it joins no team and is woken by no post; the goal's rounds start as the thread changes.
|
|
82
84
|
const facilitator = request.facilitator
|
|
83
85
|
? String(request.facilitator)
|
|
84
|
-
:
|
|
86
|
+
: DISPATCHER;
|
|
85
87
|
if (!validAgent(facilitator))
|
|
86
88
|
return { ok: false, error: "invalid facilitator" };
|
|
89
|
+
const facilitatorJoins = facilitator !== DISPATCHER;
|
|
87
90
|
const workspace = request.workspace ? String(request.workspace) : null;
|
|
88
91
|
if (
|
|
89
92
|
workspace &&
|
|
@@ -109,7 +112,14 @@ export function createGoalCommands({
|
|
|
109
112
|
provenance: provenanceFor(agent, null, null, {
|
|
110
113
|
viaServer: request.hostedCaller === true,
|
|
111
114
|
}),
|
|
112
|
-
|
|
115
|
+
// An agent that sets a goal for the owner is in it, as one that starts a thread is (swarm.mjs).
|
|
116
|
+
team: [
|
|
117
|
+
...new Set([
|
|
118
|
+
...team,
|
|
119
|
+
...(agent !== "user" ? [agent] : []),
|
|
120
|
+
...(facilitatorJoins ? [facilitator] : []),
|
|
121
|
+
]),
|
|
122
|
+
],
|
|
113
123
|
facilitator,
|
|
114
124
|
workspace: workspace && path.resolve(workspace),
|
|
115
125
|
maxPosts,
|
|
@@ -121,7 +131,7 @@ export function createGoalCommands({
|
|
|
121
131
|
machine,
|
|
122
132
|
kind: "goal",
|
|
123
133
|
text: request.brief ? `${objective}\n\n${request.brief}` : objective,
|
|
124
|
-
mentions: [facilitator],
|
|
134
|
+
mentions: facilitatorJoins ? [facilitator] : [],
|
|
125
135
|
});
|
|
126
136
|
wakeFor(goal.id, post, agent, undefined, {
|
|
127
137
|
window: request.fromWindow === true,
|
|
@@ -420,10 +430,15 @@ export function createGoalCommands({
|
|
|
420
430
|
: publish(null);
|
|
421
431
|
}
|
|
422
432
|
if (request.command === "board.goal.finish") {
|
|
423
|
-
|
|
433
|
+
// Whoever set the goal for the owner answers it too: only the owner, or an agent in a turn they started, sets one.
|
|
434
|
+
if (
|
|
435
|
+
agent !== goal.goal.facilitator &&
|
|
436
|
+
agent !== "user" &&
|
|
437
|
+
agent !== goal.origin_agent
|
|
438
|
+
)
|
|
424
439
|
return {
|
|
425
440
|
ok: false,
|
|
426
|
-
error: `only ${goal.goal.facilitator} or the owner answers this goal`,
|
|
441
|
+
error: `only ${nameOf(goal.goal.facilitator)}, whoever set it or the owner answers this goal`,
|
|
427
442
|
};
|
|
428
443
|
const result = String(request.result || "")
|
|
429
444
|
.trim()
|