tryharness 0.7.32 → 0.7.34

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.
@@ -0,0 +1,245 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { spawn } from "node:child_process";
4
+ import { randomBytes } from "node:crypto";
5
+ import { run } from "./commands.mjs";
6
+ import { listJsonl } from "./files.mjs";
7
+ import { parseSessionFile } from "./parsers.mjs";
8
+ import { historyRoots } from "./paths.mjs";
9
+
10
+ export function createSessionIndex({
11
+ store,
12
+ detected,
13
+ installedAgents,
14
+ state,
15
+ saveStatus,
16
+ log,
17
+ }) {
18
+ const opencodeExecutable =
19
+ detected.find((entry) => entry.agent === "opencode")?.path || null;
20
+ let opencodeProcess = null;
21
+ let opencodeBase = null;
22
+ let opencodeStarting = null;
23
+ let opencodeAuthorization = null;
24
+ function ensureOpencodeHelper() {
25
+ if (!opencodeExecutable) return Promise.resolve(null);
26
+ if (opencodeProcess && opencodeBase && opencodeAuthorization) {
27
+ return Promise.resolve({
28
+ base: opencodeBase,
29
+ authorization: opencodeAuthorization,
30
+ });
31
+ }
32
+ if (opencodeStarting) return opencodeStarting;
33
+ opencodeStarting = new Promise((resolve) => {
34
+ const username = "tryharness";
35
+ const password = randomBytes(32).toString("base64url");
36
+ const authorization =
37
+ "Basic " + Buffer.from(`${username}:${password}`).toString("base64");
38
+ const child = spawn(
39
+ opencodeExecutable,
40
+ ["serve", "--port", "0", "--hostname", "127.0.0.1", "--pure"],
41
+ {
42
+ stdio: ["ignore", "pipe", "pipe"],
43
+ env: {
44
+ ...process.env,
45
+ OPENCODE_SERVER_USERNAME: username,
46
+ OPENCODE_SERVER_PASSWORD: password,
47
+ },
48
+ }
49
+ );
50
+ opencodeProcess = child;
51
+ let output = "";
52
+ let settled = false;
53
+ const finish = (value) => {
54
+ if (settled) return;
55
+ settled = true;
56
+ clearTimeout(timer);
57
+ opencodeStarting = null;
58
+ resolve(value);
59
+ };
60
+ const inspect = (chunk) => {
61
+ output = (output + String(chunk)).slice(-4_000);
62
+ const match = output.match(
63
+ /opencode server listening on (http:\/\/127\.0\.0\.1:\d+)/
64
+ );
65
+ if (!match) return;
66
+ opencodeBase = match[1];
67
+ opencodeAuthorization = authorization;
68
+ finish({ base: opencodeBase, authorization });
69
+ };
70
+ child.stdout.on("data", inspect);
71
+ child.stderr.on("data", inspect);
72
+ child.on("error", (error) => {
73
+ log(`opencode helper failed: ${error.message}`);
74
+ finish(null);
75
+ });
76
+ child.on("exit", (code) => {
77
+ if (code && code !== 0) log(`opencode helper exited with code ${code}`);
78
+ if (opencodeProcess === child) {
79
+ opencodeProcess = null;
80
+ opencodeBase = null;
81
+ opencodeAuthorization = null;
82
+ }
83
+ finish(null);
84
+ });
85
+ const timer = setTimeout(() => {
86
+ log("opencode helper did not become ready in 5 seconds");
87
+ try {
88
+ child.kill();
89
+ } catch {}
90
+ finish(null);
91
+ }, 5_000);
92
+ });
93
+ return opencodeStarting;
94
+ }
95
+
96
+ let syncRunning = false;
97
+ let syncAgain = false;
98
+ let targetedOpencode = [];
99
+ async function indexFile(agent, item) {
100
+ const sessionId = path.basename(item.file);
101
+ if (!store.needsIndex(agent, sessionId, item.mtime, item.size))
102
+ return false;
103
+ const parsed = await parseSessionFile(agent, item.file);
104
+ store.indexParsed({
105
+ agent,
106
+ sessionId,
107
+ source: item.file,
108
+ parsed,
109
+ mtime: item.mtime,
110
+ size: item.size,
111
+ });
112
+ return true;
113
+ }
114
+
115
+ async function readOpencodeSession(sessionId, cwd) {
116
+ const query = cwd ? `?directory=${encodeURIComponent(cwd)}` : "";
117
+ try {
118
+ const helper = await ensureOpencodeHelper();
119
+ if (!helper) return false;
120
+ const headers = { authorization: helper.authorization };
121
+ const infoResponse = await fetch(
122
+ `${helper.base}/session/${encodeURIComponent(sessionId)}${query}`,
123
+ { headers, signal: AbortSignal.timeout(5_000) }
124
+ );
125
+ if (!infoResponse.ok) return false;
126
+ const info = await infoResponse.json();
127
+ const updated = info?.updated ?? info?.time?.updated;
128
+ const parsedMtime =
129
+ typeof updated === "number"
130
+ ? updated
131
+ : typeof updated === "string"
132
+ ? Date.parse(updated)
133
+ : NaN;
134
+ const mtime = Number.isFinite(parsedMtime) ? parsedMtime : Date.now();
135
+ if (!store.needsIndex("opencode", sessionId, mtime, 0)) return false;
136
+ const messagesResponse = await fetch(
137
+ `${helper.base}/session/${encodeURIComponent(sessionId)}/message${query}`,
138
+ { headers, signal: AbortSignal.timeout(15_000) }
139
+ );
140
+ if (!messagesResponse.ok) return false;
141
+ const messages = await messagesResponse.json();
142
+ const raw = JSON.stringify({ info, messages });
143
+ store.indexRaw({
144
+ agent: "opencode",
145
+ sessionId,
146
+ source: `opencode:${sessionId}`,
147
+ raw,
148
+ cwd: cwd || info?.directory,
149
+ mtime,
150
+ size: 0,
151
+ });
152
+ return true;
153
+ } catch {
154
+ return false;
155
+ }
156
+ }
157
+
158
+ async function sync(agentFilter = null) {
159
+ if (syncRunning) {
160
+ syncAgain = true;
161
+ return;
162
+ }
163
+ syncRunning = true;
164
+ state.indexing = true;
165
+ saveStatus();
166
+ try {
167
+ for (const agent of ["claude-code", "codex", "pi"]) {
168
+ if (
169
+ !installedAgents.includes(agent) ||
170
+ (agentFilter && agentFilter !== agent)
171
+ )
172
+ continue;
173
+ const files = historyRoots(agent)
174
+ .flatMap(listJsonl)
175
+ .sort((a, b) => b.mtime - a.mtime);
176
+ for (const item of files) {
177
+ try {
178
+ await indexFile(agent, item);
179
+ } catch (error) {
180
+ log(`index ${agent} ${item.file}: ${error.message}`);
181
+ }
182
+ await new Promise((resolve) => setImmediate(resolve));
183
+ }
184
+ }
185
+ if (
186
+ installedAgents.includes("opencode") &&
187
+ (!agentFilter || agentFilter === "opencode")
188
+ ) {
189
+ const requested = targetedOpencode.splice(0);
190
+ if (requested.length) {
191
+ for (const item of requested)
192
+ await readOpencodeSession(item.sessionId, item.cwd);
193
+ } else {
194
+ const listed = run(
195
+ "opencode",
196
+ ["session", "list", "--format", "json"],
197
+ { timeout: 20_000, maxBuffer: 20_000_000 }
198
+ );
199
+ if (listed.ok) {
200
+ let rows = [];
201
+ try {
202
+ rows = JSON.parse(listed.stdout);
203
+ } catch {}
204
+ for (const row of Array.isArray(rows) ? rows : [])
205
+ if (row?.id) await readOpencodeSession(row.id, row.directory);
206
+ }
207
+ }
208
+ }
209
+ state.lastIndexedAt = new Date().toISOString();
210
+ state.lastError = null;
211
+ } catch (error) {
212
+ state.lastError = error.message;
213
+ log(`sync failed: ${error.stack || error.message}`);
214
+ } finally {
215
+ state.indexing = false;
216
+ syncRunning = false;
217
+ saveStatus();
218
+ if (syncAgain) {
219
+ syncAgain = false;
220
+ setImmediate(() => void sync());
221
+ }
222
+ }
223
+ }
224
+
225
+ function sourceRaw(agent, sessionId) {
226
+ const row = store.findSource(agent, sessionId);
227
+ if (!row?.source || row.source.startsWith("opencode:")) return null;
228
+ // Compatibility fetches are explicit reads. Return the complete source instead of
229
+ // silently changing their meaning to "the last N bytes"; normal 0.4 search/read
230
+ // traffic is served from the normalized SQLite transcript and never ships raw logs.
231
+ try {
232
+ return {
233
+ raw: fs.readFileSync(row.source, "utf8"),
234
+ cwd: row.cwd || undefined,
235
+ };
236
+ } catch {
237
+ return null;
238
+ }
239
+ }
240
+
241
+ const target = (request) => targetedOpencode.push(request);
242
+ const stop = () => opencodeProcess?.kill();
243
+
244
+ return { sync, sourceRaw, target, stop };
245
+ }
package/lib/store.mjs CHANGED
@@ -27,6 +27,15 @@ export const DEFAULT_DAILY_RUNS = 10;
27
27
  export const FAILURE_CAUSES = ["person", "start", "exit", "agent"];
28
28
  // An agent's record leaves out failures that say nothing about it: the person's stop and a run that never started.
29
29
  const AGENTS_OWN = "not (status = 'failed' and failure in ('person', 'start'))";
30
+ // A room post as readers get it. A file attached in the room names it, so a reader can show the file itself.
31
+ const roomPostOf = ({ source, ...row }) => ({
32
+ ...row,
33
+ seq: Number(row.seq),
34
+ mentions: row.mentions ? JSON.parse(row.mentions) : [],
35
+ ...(row.kind === "file" && String(source).startsWith("file:")
36
+ ? { file: String(source).slice(5) }
37
+ : {}),
38
+ });
30
39
 
31
40
  export function openStore(file) {
32
41
  const db = new Database(file);
@@ -1027,21 +1036,31 @@ export function openStore(file) {
1027
1036
  return post ? { ...post, mentions } : null;
1028
1037
  },
1029
1038
  roomPosts(goalId, since = 0, limit = 200) {
1030
- // A file attached in the room names it, so a reader can show the file itself.
1031
1039
  return execute(
1032
1040
  "all",
1033
1041
  "select id, seq, kind, author, text, mentions, source, created_at from board_contributions where task_id = ? and seq > ? and kind != 'held' order by seq asc limit ?",
1034
1042
  goalId,
1035
1043
  Number(since) || 0,
1036
1044
  Math.max(1, Math.min(500, Number(limit) || 200))
1037
- ).map(({ source, ...row }) => ({
1038
- ...row,
1039
- seq: Number(row.seq),
1040
- mentions: row.mentions ? JSON.parse(row.mentions) : [],
1041
- ...(row.kind === "file" && String(source).startsWith("file:")
1042
- ? { file: String(source).slice(5) }
1043
- : {}),
1044
- }));
1045
+ ).map(roomPostOf);
1046
+ },
1047
+ // The newest posts before `before` (every post when it is not given), oldest first, and the seq to read before
1048
+ // for the ones earlier still (null at the thread's start). roomPosts reads from the start, which in a thread past
1049
+ // its limit is the oldest posts (October 17: Codex was woken with posts 172 to 201 of 294).
1050
+ recentRoomPosts(goalId, { before = null, limit = 30 } = {}) {
1051
+ const count = Math.max(1, Math.min(500, Number(limit) || 30));
1052
+ const rows = execute(
1053
+ "all",
1054
+ "select id, seq, kind, author, text, mentions, source, created_at from board_contributions where task_id = ? and seq < ? and kind != 'held' order by seq desc limit ?",
1055
+ goalId,
1056
+ Number(before) > 0 ? Number(before) : Number.MAX_SAFE_INTEGER,
1057
+ count + 1
1058
+ );
1059
+ const posts = rows.slice(0, count).reverse().map(roomPostOf);
1060
+ return {
1061
+ posts,
1062
+ earlierBefore: rows.length > count ? (posts[0]?.seq ?? null) : null,
1063
+ };
1045
1064
  },
1046
1065
  // What each agent's finished runs' receipts showed (receipts.mjs, kept as receipt:<task>): { agent: { tested, untested, failing } }.
1047
1066
  receiptCounts() {
package/lib/swarm.mjs CHANGED
@@ -4,16 +4,11 @@ import path from "node:path";
4
4
  import { spawnSync } from "node:child_process";
5
5
  import { readRunSteps } from "./runner.mjs";
6
6
  import { nameOf, resolveMember } from "./members.mjs";
7
- import {
8
- modelOf,
9
- setNote,
10
- swarmBlock,
11
- swarmCards,
12
- swarmDigest,
13
- } from "./cards.mjs";
7
+ import { setNote, swarmBlock, swarmCards, swarmDigest } from "./cards.mjs";
14
8
  import {
15
9
  DEFAULT_ROUTING,
16
10
  PICKABLE,
11
+ agentModelSettings,
17
12
  choiceOf,
18
13
  listingOf,
19
14
  picksIn,
@@ -100,6 +95,7 @@ export function createSwarm({
100
95
  authorizeRun,
101
96
  pickable,
102
97
  listModels,
98
+ harnessModel,
103
99
  folders,
104
100
  runsIn,
105
101
  threadFiles = () => ({ files: [], rendering: [] }),
@@ -496,6 +492,15 @@ export function createSwarm({
496
492
  const named = request.target ? resolveMember(request.target) : null;
497
493
  if (named && !named.ok)
498
494
  return { ok: false, error: `no agent called "${request.target}"` };
495
+ // Harness's own: a provider, then a model of Harness's catalog on it (harnessModel.mjs).
496
+ if (named?.id === "harness")
497
+ return request.refresh === true && agent === "user"
498
+ ? {
499
+ pending: harnessModel
500
+ .refresh()
501
+ .then(() => ({ ok: true, harness: harnessModel.view() })),
502
+ }
503
+ : { ok: true, harness: harnessModel.view() };
499
504
  const ids = pickable().filter((id) => !named || id === named.id);
500
505
  if (named && !ids.length)
501
506
  return {
@@ -512,16 +517,20 @@ export function createSwarm({
512
517
  id,
513
518
  name: nameOf(id),
514
519
  choice: choiceOf(store, id),
515
- ownModel: modelOf(id),
520
+ ownModel: agentModelSettings(id).model,
516
521
  listedAt: listed?.at ?? null,
517
522
  ...(listed?.error ? { error: listed.error } : {}),
518
523
  models: listed?.models ?? [],
519
524
  };
520
525
  }),
526
+ ...(named ? {} : { harness: harnessModel.view() }),
521
527
  });
528
+ // Harness's catalog is read the first time it is shown, once the server can send it.
522
529
  return request.refresh === true && agent === "user"
523
530
  ? { pending: listModels().then(listing) }
524
- : listing();
531
+ : !named && !harnessModel.listed() && agent === "user"
532
+ ? { pending: harnessModel.refresh().then(listing) }
533
+ : listing();
525
534
  }
526
535
  // Any of the person's agents may start a thread and stop one it started. The thread carries the agent's trust, so
527
536
  // nothing in it runs without the person's Start, and one started through Harness gets no coordinator rounds (they
@@ -647,6 +656,15 @@ export function createSwarm({
647
656
  // The person's choice of model and effort for one agent: a model it lists, "auto", or "default" for its own settings.
648
657
  if (command === "board.swarm.model") {
649
658
  const named = resolveMember(request.target);
659
+ // Harness's: a provider and one of its models there, or "default" for the person's last Harness model.
660
+ if (named.ok && named.id === "harness") {
661
+ if (agent !== "user")
662
+ return { ok: false, error: "only the person picks Harness's model" };
663
+ const set = harnessModel.set(request.provider, request.model);
664
+ return set.ok
665
+ ? { ok: true, agent: "harness", choice: set.choice }
666
+ : set;
667
+ }
650
668
  if (!named.ok || !PICKABLE.has(named.id))
651
669
  return {
652
670
  ok: false,
@@ -1058,6 +1076,7 @@ export function createSwarm({
1058
1076
  // A one-time check-in held while the thread was paused runs now.
1059
1077
  schedules?.tick();
1060
1078
  notifyBoard(store.getTask(thread.id));
1079
+ coordinator.resumed(thread.id);
1061
1080
  return { ok: true, thread: summary(store.getTask(thread.id)) };
1062
1081
  }
1063
1082
  // The person moves the thread to another folder between turns. Each agent starts a new session there, as a session