tryharness 0.7.64 → 0.7.67

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/cli.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn, spawnSync } from "node:child_process";
3
+ import { validateConnection as checkConnection } from "./lib/connection.mjs";
3
4
  import fs from "node:fs";
4
5
  import os from "node:os";
5
6
  import path from "node:path";
@@ -125,36 +126,6 @@ function origin() {
125
126
  ).replace(/\/$/, "");
126
127
  }
127
128
 
128
- async function validateConnection(config) {
129
- if (!config.origin || !config.token) return false;
130
- try {
131
- const response = await fetch(
132
- String(config.origin).replace(/\/$/, "") + "/api/mcp",
133
- {
134
- method: "POST",
135
- headers: {
136
- "content-type": "application/json",
137
- authorization: "Bearer " + config.token,
138
- },
139
- body: JSON.stringify({
140
- jsonrpc: "2.0",
141
- id: 1,
142
- method: "initialize",
143
- params: {
144
- protocolVersion: "2025-06-18",
145
- capabilities: {},
146
- clientInfo: { name: "tryharness", version: VERSION },
147
- },
148
- }),
149
- signal: AbortSignal.timeout(10_000),
150
- }
151
- );
152
- return response.ok && !(await response.json().catch(() => null))?.error;
153
- } catch {
154
- return false;
155
- }
156
- }
157
-
158
129
  // Whether a browser can open here: not over SSH, and on Linux only with a display.
159
130
  const canOpenBrowser = () =>
160
131
  !process.env.SSH_CONNECTION &&
@@ -272,14 +243,19 @@ async function connect() {
272
243
 
273
244
  const pairOrigin = origin();
274
245
  let config = readJson(paths.config, {});
275
- const reusable =
276
- !has("--reconnect") &&
277
- config.origin === pairOrigin &&
278
- (await validateConnection(config));
279
- if (reusable)
246
+ const connection =
247
+ !has("--reconnect") && config.origin === pairOrigin
248
+ ? await checkConnection(config, { version: VERSION })
249
+ : "rejected";
250
+ const reusable = connection !== "rejected";
251
+ if (connection === "ok")
280
252
  ui.success(
281
253
  `Connected to Harness ${pc.dim(`· ${new URL(pairOrigin).host}`)}`
282
254
  );
255
+ else if (connection === "unreachable")
256
+ ui.warn(
257
+ `Harness did not answer just now, so this computer keeps its connection ${pc.dim(`· ${new URL(pairOrigin).host}`)}. If it really stopped working: ${relayCommand()} --reconnect`
258
+ );
283
259
  let setupCode = null;
284
260
  let linkOffered = false;
285
261
  if (!reusable) {
@@ -696,7 +672,7 @@ async function doctor() {
696
672
  console.log(`tryharness ${VERSION}`);
697
673
  const config = readJson(paths.config, {});
698
674
  console.log(
699
- `pairing: ${(await validateConnection(config)) ? "valid" : "missing or invalid"}`
675
+ `pairing: ${{ ok: "valid", rejected: "missing or invalid", unreachable: "kept (Harness did not answer)" }[await checkConnection(config, { version: VERSION })]}`
700
676
  );
701
677
  const service = serviceInfo();
702
678
  console.log(
package/lib/atlas.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { createHash } from "node:crypto";
4
- import { spawnSync } from "node:child_process";
4
+ import { spawn } from "node:child_process";
5
5
  import sqliteWasm from "node-sqlite3-wasm";
6
6
  import { fromBlob, toBlob } from "./vectors.mjs";
7
7
 
@@ -143,6 +143,7 @@ export function openAtlas(file) {
143
143
  create virtual table if not exists library_fts using fts5(file unindexed, vec unindexed, name, text);
144
144
  `);
145
145
  migrateMedia(db);
146
+ queueUnembedded(db);
146
147
  // A blob's pieces never change, so lookups are kept (a few MB for a large repository and its recent versions).
147
148
  const blobPieces = new Map();
148
149
  const run = (sql, ...values) =>
@@ -186,6 +187,7 @@ export function openAtlas(file) {
186
187
  part.line,
187
188
  part.text
188
189
  );
190
+ run("insert or ignore into pieces_waiting(hash) values(?)", hash);
189
191
  run(
190
192
  "insert into pieces_fts(hash, path, text) values(?, ?, ?)",
191
193
  hash,
@@ -227,25 +229,35 @@ export function openAtlas(file) {
227
229
  "select root, count(*) as files, max(indexed_at) as indexed_at from files group by root"
228
230
  ).map((row) => ({ ...row, files: Number(row.files) })),
229
231
  toEmbed: (version, limit = 64) =>
230
- all(
231
- "select p.hash, p.path, p.text from pieces p left join piece_vectors v on v.hash = p.hash and v.version = ? where v.hash is null limit ?",
232
- version,
233
- limit
234
- ),
232
+ version === ATLAS_VERSION
233
+ ? all(
234
+ "select p.hash, p.path, p.text from pieces_waiting w cross join pieces p on p.hash = w.hash limit ?",
235
+ limit
236
+ )
237
+ : all(
238
+ "select p.hash, p.path, p.text from pieces p left join piece_vectors v on v.hash = p.hash and v.version = ? where v.hash is null limit ?",
239
+ version,
240
+ limit
241
+ ),
235
242
  missing: (version) =>
236
243
  Number(
237
- get(
238
- "select count(*) as n from pieces p left join piece_vectors v on v.hash = p.hash and v.version = ? where v.hash is null",
239
- version
240
- ).n
244
+ version === ATLAS_VERSION
245
+ ? get("select count(*) as n from pieces_waiting").n
246
+ : get(
247
+ "select count(*) as n from pieces p left join piece_vectors v on v.hash = p.hash and v.version = ? where v.hash is null",
248
+ version
249
+ ).n
241
250
  ),
242
- putVector: (hash, version, vector) =>
251
+ putVector(hash, version, vector) {
243
252
  run(
244
253
  "insert or replace into piece_vectors(hash, version, vector) values(?, ?, ?)",
245
254
  hash,
246
255
  version,
247
256
  toBlob(vector)
248
- ),
257
+ );
258
+ if (version === ATLAS_VERSION)
259
+ run("delete from pieces_waiting where hash = ?", hash);
260
+ },
249
261
  vectors(hashes, version) {
250
262
  const out = new Map();
251
263
  const list = [...hashes];
@@ -348,6 +360,30 @@ export function openAtlas(file) {
348
360
  };
349
361
  }
350
362
 
363
+ // The pieces with no vector for ATLAS_VERSION, kept as they come and go. Finding them by joining pieces to their vectors
364
+ // read the whole atlas on every batch, and the database runs on the daemon's one thread: on an 800 MB atlas that held
365
+ // every reply for minutes. The join runs once, when the atlas is new to this version.
366
+ function queueUnembedded(db) {
367
+ db.exec("create table if not exists pieces_waiting (hash text primary key)");
368
+ const queuedFor = db.get("select value from meta where key = 'waiting_for'");
369
+ if (queuedFor?.value === ATLAS_VERSION) return;
370
+ db.exec("begin");
371
+ try {
372
+ db.run("delete from pieces_waiting");
373
+ db.run(
374
+ "insert into pieces_waiting(hash) select p.hash from pieces p left join piece_vectors v on v.hash = p.hash and v.version = ? where v.hash is null",
375
+ [ATLAS_VERSION]
376
+ );
377
+ db.run("insert or replace into meta(key, value) values('waiting_for', ?)", [
378
+ ATLAS_VERSION,
379
+ ]);
380
+ db.exec("commit");
381
+ } catch (error) {
382
+ db.exec("rollback");
383
+ throw error;
384
+ }
385
+ }
386
+
351
387
  // Recordings and screenshots indexed before the library keep their vectors: their part keys are the same.
352
388
  function migrateMedia(db) {
353
389
  const old = db.get(
@@ -495,13 +531,26 @@ function libraryTables(run, all, get) {
495
531
  };
496
532
  }
497
533
 
498
- const git = (cwd, args, input) =>
499
- spawnSync("git", args, {
500
- cwd,
501
- input,
502
- encoding: "utf8",
503
- maxBuffer: 256 * 2 ** 20,
534
+ // git runs beside the daemon, never in its way: run synchronously, every listing and hash of a repository held every
535
+ // reply (the buddy, T3, the person's other computers) for as long as git took, 12 s in a minute after a restart.
536
+ // stdout is a Buffer; status is -1 when git could not start.
537
+ export const git = (cwd, args, input) =>
538
+ new Promise((resolve) => {
539
+ const child = spawn("git", args, { cwd });
540
+ const chunks = [];
541
+ child.stdout.on("data", (chunk) => chunks.push(chunk));
542
+ child.stderr.resume();
543
+ child.on("error", () => resolve({ status: -1, stdout: Buffer.alloc(0) }));
544
+ child.on("close", (status) =>
545
+ resolve({ status, stdout: Buffer.concat(chunks) })
546
+ );
547
+ child.stdin.on("error", () => {});
548
+ child.stdin.end(input ?? "");
504
549
  });
550
+ const gitText = async (cwd, args, input) => {
551
+ const out = await git(cwd, args, input);
552
+ return { status: out.status, stdout: out.stdout.toString("utf8") };
553
+ };
505
554
 
506
555
  // A part of a larger string is kept by V8 as a slice that holds the whole string alive. The rows below outlive git's
507
556
  // output (a commit's file list is kept while history is read), so each path and blob is its own copy, or every list
@@ -509,9 +558,9 @@ const git = (cwd, args, input) =>
509
558
  const own = (text) => structuredClone(text);
510
559
 
511
560
  /** The repository's files at a commit (or the working tree when commit is null): [{ path, blob, size }]. */
512
- export function treeFiles(root, commit = null) {
561
+ export async function treeFiles(root, commit = null) {
513
562
  if (commit) {
514
- const listed = git(root, ["ls-tree", "-r", "-l", commit]);
563
+ const listed = await gitText(root, ["ls-tree", "-r", "-l", commit]);
515
564
  if (listed.status !== 0) return [];
516
565
  return listed.stdout
517
566
  .split("\n")
@@ -527,7 +576,7 @@ export function treeFiles(root, commit = null) {
527
576
  }
528
577
  // The working tree: git's index already holds the blob of every unchanged tracked file; only files changed on disk
529
578
  // and new files git does not ignore are hashed, so a pass over a large repository costs about what `git status` does.
530
- const staged = git(root, ["ls-files", "-s", "-z"]);
579
+ const staged = await gitText(root, ["ls-files", "-s", "-z"]);
531
580
  if (staged.status !== 0) return [];
532
581
  const blobs = new Map();
533
582
  for (const entry of staged.stdout.split("\0")) {
@@ -536,16 +585,13 @@ export function treeFiles(root, commit = null) {
536
585
  const [, blob, stage] = entry.slice(0, tab).split(" ");
537
586
  if (stage === "0") blobs.set(own(entry.slice(tab + 1)), own(blob));
538
587
  }
539
- const changed = git(root, ["diff", "--name-only", "-z"])
540
- .stdout.split("\0")
588
+ const changed = (await gitText(root, ["diff", "--name-only", "-z"])).stdout
589
+ .split("\0")
541
590
  .filter(Boolean);
542
- const untracked = git(root, [
543
- "ls-files",
544
- "--others",
545
- "--exclude-standard",
546
- "-z",
547
- ])
548
- .stdout.split("\0")
591
+ const untracked = (
592
+ await gitText(root, ["ls-files", "--others", "--exclude-standard", "-z"])
593
+ ).stdout
594
+ .split("\0")
549
595
  .filter(Boolean);
550
596
  const rows = [];
551
597
  const rehash = [];
@@ -563,7 +609,7 @@ export function treeFiles(root, commit = null) {
563
609
  if (!row.blob || changed.includes(rel)) rehash.push(row);
564
610
  }
565
611
  if (rehash.length) {
566
- const hashed = git(
612
+ const hashed = await gitText(
567
613
  root,
568
614
  ["hash-object", "--stdin-paths"],
569
615
  rehash.map((row) => path.join(root, row.path)).join("\n")
@@ -577,14 +623,14 @@ export function treeFiles(root, commit = null) {
577
623
  }
578
624
 
579
625
  /** File contents for blobs, read through one `git cat-file --batch`. */
580
- export function readBlobs(root, blobs) {
626
+ export async function readBlobs(root, blobs) {
581
627
  const out = new Map();
582
628
  if (!blobs.length) return out;
583
- const result = spawnSync("git", ["cat-file", "--batch"], {
584
- cwd: root,
585
- input: blobs.join("\n") + "\n",
586
- maxBuffer: 2 * 2 ** 30,
587
- });
629
+ const result = await git(
630
+ root,
631
+ ["cat-file", "--batch"],
632
+ blobs.join("\n") + "\n"
633
+ );
588
634
  const buffer = result.stdout;
589
635
  let at = 0;
590
636
  while (at < buffer.length) {
@@ -608,9 +654,11 @@ export function readBlobs(root, blobs) {
608
654
  }
609
655
 
610
656
  /** Adds the files' pieces to the atlas (new blobs only); returns how many blobs were new. */
611
- export function addFiles(atlas, root, rows) {
657
+ export async function addFiles(atlas, root, rows) {
612
658
  const fresh = rows.filter((row) => !atlas.blob(row.blob));
613
- const texts = readBlobs(root, [...new Set(fresh.map((row) => row.blob))]);
659
+ const texts = await readBlobs(root, [
660
+ ...new Set(fresh.map((row) => row.blob)),
661
+ ]);
614
662
  for (let i = 0; i < fresh.length; i += 500)
615
663
  atlas.batch(() => {
616
664
  for (const row of fresh.slice(i, i + 500)) {
@@ -87,20 +87,45 @@ function install(db) {
87
87
  gone integer not null default 0,
88
88
  unique(tbl, pk)
89
89
  )`);
90
+ // When each column of a row last changed, by the change's seq, and "*" when the row came: a worker that already has a
91
+ // row gets only the columns changed since it last followed (owner, October 28: a task row is 11.5 KB on average,
92
+ // re-sent whole for a lease or a status). A row with no "*" here (from before this) goes whole.
93
+ db.exec(`create table if not exists board_change_cols (
94
+ tbl text not null,
95
+ pk text not null,
96
+ col text not null,
97
+ seq integer not null,
98
+ primary key(tbl, pk, col)
99
+ )`);
90
100
  for (const [table, columns] of Object.entries(MIRRORED)) {
91
101
  // A delete then an insert, never "insert or replace": the conflict rule of the statement that fired the trigger
92
102
  // (a store's "insert or ignore") would override the trigger's own and drop the change.
93
103
  const changed = (prefix, gone) =>
94
104
  `delete from board_changes where tbl = '${table}' and pk = ${keyJson(prefix, columns)};
95
105
  insert into board_changes(tbl, pk, gone) values('${table}', ${keyJson(prefix, columns)}, ${gone});`;
106
+ const seqOf = `(select seq from board_changes where tbl = '${table}' and pk = ${keyJson("new", columns)})`;
107
+ const forget = (prefix) =>
108
+ `delete from board_change_cols where tbl = '${table}' and pk = ${keyJson(prefix, columns)};`;
109
+ const came = `${forget("new")}
110
+ insert into board_change_cols(tbl, pk, col, seq) values('${table}', ${keyJson("new", columns)}, '*', ${seqOf});`;
111
+ const moved = db
112
+ .all(`pragma table_info(${table})`)
113
+ .map((column) => column.name)
114
+ .filter((name) => !columns.includes(name))
115
+ .map(
116
+ (name) =>
117
+ `delete from board_change_cols where tbl = '${table}' and pk = ${keyJson("new", columns)} and col = '${name}' and old.${name} is not new.${name};
118
+ insert into board_change_cols(tbl, pk, col, seq) select '${table}', ${keyJson("new", columns)}, '${name}', ${seqOf} where old.${name} is not new.${name};`
119
+ )
120
+ .join("\n ");
96
121
  // Made again on every start, so a computer that has older triggers gets these.
97
122
  db.exec(`
98
123
  drop trigger if exists ${table}_mirror_insert;
99
124
  drop trigger if exists ${table}_mirror_update;
100
125
  drop trigger if exists ${table}_mirror_delete;
101
- create trigger ${table}_mirror_insert after insert on ${table} begin ${changed("new", 0)} end;
102
- create trigger ${table}_mirror_update after update on ${table} begin ${changed("new", 0)} end;
103
- create trigger ${table}_mirror_delete after delete on ${table} begin ${changed("old", 1)} end;
126
+ create trigger ${table}_mirror_insert after insert on ${table} begin ${changed("new", 0)} ${came} end;
127
+ create trigger ${table}_mirror_update after update on ${table} begin ${changed("new", 0)} ${moved} end;
128
+ create trigger ${table}_mirror_delete after delete on ${table} begin ${changed("old", 1)} ${forget("old")} end;
104
129
  `);
105
130
  }
106
131
  if (!db.get("select value from board_meta where key = ?", [FEED])) {
@@ -201,6 +226,42 @@ export function createBoardMirror({
201
226
  Object.values(words)
202
227
  );
203
228
  };
229
+ // Some columns of a row this computer has, as home has them now; false when it does not have the row.
230
+ const writeColumns = (table, key, cols) => {
231
+ const known = columnsOf.get(table);
232
+ const names = Object.keys(cols).filter((name) => known.has(name));
233
+ if (!names.length)
234
+ return Boolean(
235
+ db.get(
236
+ `select 1 as here from ${table} where ${whereKey(MIRRORED[table])}`,
237
+ key
238
+ )
239
+ );
240
+ const written = db.run(
241
+ `update ${table} set ${names.map((name) => `${name} = ?`).join(", ")} where ${whereKey(MIRRORED[table])}`,
242
+ [...names.map((name) => offWire(cols[name])), ...key]
243
+ );
244
+ if (!written.changes) return false;
245
+ const searched = SEARCHED[table];
246
+ if (searched) {
247
+ const row = db.get(
248
+ `select * from ${table} where ${whereKey(MIRRORED[table])}`,
249
+ key
250
+ );
251
+ const words = searched.columns(row);
252
+ db.run(`delete from ${searched.table} where id = ?`, [words.id]);
253
+ db.run(
254
+ `insert into ${searched.table}(${Object.keys(words).join(", ")}) values(${Object.keys(
255
+ words
256
+ )
257
+ .map(() => "?")
258
+ .join(", ")})`,
259
+ Object.values(words)
260
+ );
261
+ }
262
+ return true;
263
+ };
264
+ let missed = false;
204
265
  const removeRow = (table, key) => {
205
266
  const columns = MIRRORED[table];
206
267
  db.run(`delete from ${table} where ${whereKey(columns)}`, key);
@@ -224,6 +285,8 @@ export function createBoardMirror({
224
285
  after = 0,
225
286
  feed: followed = null,
226
287
  standIn = false,
288
+ // A worker that applies a row's changed columns (one older than them takes whole rows).
289
+ diffs = false,
227
290
  } = {}) => {
228
291
  const standing = standInRecord();
229
292
  if (standIn && !standing)
@@ -252,9 +315,22 @@ export function createBoardMirror({
252
315
  const row = change.gone
253
316
  ? null
254
317
  : db.get(`select * from ${change.tbl} where ${whereKey(columns)}`, key);
255
- const item = row
256
- ? { seq: change.seq, tbl: change.tbl, row: onWire(row) }
257
- : { seq: change.seq, tbl: change.tbl, gone: key };
318
+ const moved =
319
+ row && diffs && !standIn && from > 0
320
+ ? changedSince(change.tbl, change.pk, from)
321
+ : null;
322
+ const item = !row
323
+ ? { seq: change.seq, tbl: change.tbl, gone: key }
324
+ : moved
325
+ ? {
326
+ seq: change.seq,
327
+ tbl: change.tbl,
328
+ key,
329
+ cols: onWire(
330
+ Object.fromEntries(moved.map((name) => [name, row[name]]))
331
+ ),
332
+ }
333
+ : { seq: change.seq, tbl: change.tbl, row: onWire(row) };
258
334
  const size = Buffer.byteLength(JSON.stringify(item));
259
335
  if (changes.length && bytes + size > PAGE_BYTES) break;
260
336
  changes.push(item);
@@ -271,6 +347,21 @@ export function createBoardMirror({
271
347
  };
272
348
  };
273
349
 
350
+ // The columns of a row changed after `after`, for a worker that already has the row; null when it must have the row
351
+ // whole (it came after `after`, or before columns were counted).
352
+ const changedSince = (tbl, pk, after) => {
353
+ const marks = db.all(
354
+ "select col, seq from board_change_cols where tbl = ? and pk = ?",
355
+ [tbl, pk]
356
+ );
357
+ const came = marks.find((mark) => mark.col === "*");
358
+ if (!came || came.seq > after) return null;
359
+ return marks
360
+ .filter((mark) => mark.col !== "*" && mark.seq > after)
361
+ .map((mark) => mark.col)
362
+ .filter((name) => columnsOf.get(tbl).has(name));
363
+ };
364
+
274
365
  // Keys seen during a start-over, to take out what home does not have once it is complete.
275
366
  let starting = null;
276
367
 
@@ -290,11 +381,28 @@ export function createBoardMirror({
290
381
  removeRow(change.tbl, change.gone);
291
382
  continue;
292
383
  }
384
+ if (change.cols) {
385
+ // A row this computer lacks cannot take its columns: the copy lists home's board again.
386
+ if (!writeColumns(change.tbl, change.key, change.cols)) {
387
+ missed = true;
388
+ break;
389
+ }
390
+ continue;
391
+ }
293
392
  writeRow(change.tbl, change.row);
294
393
  starting
295
394
  ?.get(change.tbl)
296
395
  ?.add(JSON.stringify(columns.map((column) => change.row[column])));
297
396
  }
397
+ if (missed) {
398
+ db.exec("rollback");
399
+ missed = false;
400
+ log(
401
+ "the board copy here missed a row home changed: listing home's board again"
402
+ );
403
+ followAfresh();
404
+ return true;
405
+ }
298
406
  const done = page.to >= page.head;
299
407
  if (done && starting) sweep(starting);
300
408
  setMeta(
@@ -0,0 +1,51 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { MAX_SKEW_MS } from "./sealed.mjs";
3
+
4
+ // The sessions home holds with the person's other computers for their calls through Harness (owner, October 28). A
5
+ // call over the direct link rides the link's own encryption, which admits only the pinned computer (directLink.mjs);
6
+ // one through Harness is sealed with the pair's key and, once a computer has a session, guarded against replay here,
7
+ // in memory, instead of by a row for every request id (about 400 a minute, 320,000 a day, with the buddies reading
8
+ // the board every few seconds).
9
+ //
10
+ // - Home gives a computer a session when it asks (a handshake sealed as any call): a fresh random id, replacing the
11
+ // one it had. Home restarting forgets every session, so nothing sealed before can be played to it again.
12
+ // - Each call in a session is taken once: its request id is remembered for as long as a call that old could still be
13
+ // accepted (twice the clock skew sealed calls allow), then let go, oldest first.
14
+ // - A call with a session home does not hold is refused as "expired"; the computer asks for a new session and sends it
15
+ // again. A call home already took is "replayed".
16
+
17
+ const KEPT_MS = 2 * MAX_SKEW_MS;
18
+
19
+ export function createCallSessions({
20
+ now = Date.now,
21
+ newId = () => randomBytes(16).toString("base64url"),
22
+ } = {}) {
23
+ // workerId -> { id, seen: Map<requestId, takenAt> }, each seen map in the order its calls came
24
+ const sessions = new Map();
25
+
26
+ /** A new session for that computer, in place of any it had. */
27
+ const start = (workerId) => {
28
+ const id = newId();
29
+ sessions.set(workerId, { id, seen: new Map() });
30
+ return id;
31
+ };
32
+
33
+ /** Whether a call in `session` is "fresh" (and now taken), "replayed" or "expired". */
34
+ const take = (workerId, session, requestId) => {
35
+ const current = sessions.get(workerId);
36
+ if (!current || current.id !== session) return "expired";
37
+ const at = now();
38
+ for (const [id, takenAt] of current.seen) {
39
+ if (at - takenAt <= KEPT_MS) break;
40
+ current.seen.delete(id);
41
+ }
42
+ if (current.seen.has(requestId)) return "replayed";
43
+ current.seen.set(requestId, at);
44
+ return "fresh";
45
+ };
46
+
47
+ /** That computer is no longer one of the person's: its session ends. */
48
+ const end = (workerId) => sessions.delete(workerId);
49
+
50
+ return { start, take, end };
51
+ }
@@ -0,0 +1,39 @@
1
+ // Whether this computer's saved connection still works: "ok", "rejected" when Harness refuses its token (setup pairs
2
+ // again), or "unreachable" when Harness did not answer at all (a network blip, a timeout, a server error). An
3
+ // unreachable Harness keeps the connection: setup once opened the Connect page because a fetch failed for a moment
4
+ // (owner, October 28: "why did i get another connect prompt").
5
+ export async function validateConnection(
6
+ config,
7
+ { version, fetchImpl = fetch } = {}
8
+ ) {
9
+ if (!config.origin || !config.token) return "rejected";
10
+ let response;
11
+ try {
12
+ response = await fetchImpl(
13
+ String(config.origin).replace(/\/$/, "") + "/api/mcp",
14
+ {
15
+ method: "POST",
16
+ headers: {
17
+ "content-type": "application/json",
18
+ authorization: "Bearer " + config.token,
19
+ },
20
+ body: JSON.stringify({
21
+ jsonrpc: "2.0",
22
+ id: 1,
23
+ method: "initialize",
24
+ params: {
25
+ protocolVersion: "2025-06-18",
26
+ capabilities: {},
27
+ clientInfo: { name: "tryharness", version },
28
+ },
29
+ }),
30
+ signal: AbortSignal.timeout(10_000),
31
+ }
32
+ );
33
+ } catch {
34
+ return "unreachable";
35
+ }
36
+ if (response.status === 401 || response.status === 403) return "rejected";
37
+ if (!response.ok) return "unreachable";
38
+ return (await response.json().catch(() => null))?.error ? "rejected" : "ok";
39
+ }