tryharness 0.7.42 → 0.7.44

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.
Files changed (46) hide show
  1. package/cli.mjs +118 -5
  2. package/lib/actorRoster.mjs +16 -2
  3. package/lib/adapters.mjs +2 -1
  4. package/lib/answers.mjs +30 -0
  5. package/lib/boardCommands.mjs +9 -1
  6. package/lib/boardFiles.mjs +1 -1
  7. package/lib/boardProtocol.json +1 -0
  8. package/lib/bridge.mjs +49 -9
  9. package/lib/cards.mjs +22 -4
  10. package/lib/compaction.mjs +90 -0
  11. package/lib/context.mjs +19 -11
  12. package/lib/coordinator.mjs +176 -12
  13. package/lib/daemon.mjs +435 -49
  14. package/lib/fit.mjs +65 -0
  15. package/lib/folders.mjs +20 -0
  16. package/lib/frames.mjs +3 -2
  17. package/lib/goalCommands.mjs +12 -0
  18. package/lib/handover.mjs +128 -0
  19. package/lib/loopBackstop.mjs +104 -0
  20. package/lib/machineIdentity.mjs +43 -0
  21. package/lib/machineRoutes.mjs +97 -0
  22. package/lib/machineRuns.mjs +133 -0
  23. package/lib/machineSeal.mjs +157 -0
  24. package/lib/mcp-stdio.mjs +54 -11
  25. package/lib/nudge.mjs +211 -44
  26. package/lib/oneLine.mjs +27 -0
  27. package/lib/ownMachines.mjs +748 -0
  28. package/lib/paths.mjs +1 -0
  29. package/lib/plans.mjs +3 -2
  30. package/lib/provenance.mjs +4 -1
  31. package/lib/runBrief.mjs +346 -0
  32. package/lib/runWorker.mjs +24 -173
  33. package/lib/runner.mjs +26 -2
  34. package/lib/store.mjs +232 -16
  35. package/lib/stuck.mjs +198 -0
  36. package/lib/swarm.mjs +3 -1
  37. package/lib/taskCommands.mjs +268 -31
  38. package/lib/taskRuns.mjs +9 -0
  39. package/lib/team.json +192 -0
  40. package/lib/team.mjs +417 -0
  41. package/lib/threadRuns.mjs +24 -9
  42. package/lib/threadView.mjs +13 -0
  43. package/lib/trails.mjs +49 -0
  44. package/lib/wakes.mjs +201 -69
  45. package/lib/worktrees.mjs +9 -0
  46. package/package.json +1 -1
package/lib/wakes.mjs CHANGED
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
  import { createHmac, randomBytes, randomUUID } from "node:crypto";
5
+ import { askerOf } from "./answers.mjs";
5
6
  import { resolveCommand, run } from "./commands.mjs";
6
7
  import { ensureDir, readJson, writeJsonAtomic } from "./files.mjs";
7
8
  import { paths } from "./paths.mjs";
@@ -291,112 +292,243 @@ const ANSWER_WINDOW_MS = 10 * 60_000,
291
292
  MAX_PER_HOUR = 20;
292
293
  // A refused wake is tried again after each of these until one goes through (the server's rule for Grok too).
293
294
  export const RETRY_DELAYS_MS = [30_000, 120_000, 600_000];
295
+ // Failures that mean the wake never reached the gateway. Any other thrown error (a reset, a dropped connection) came
296
+ // after the request may have landed, so a result told that way is never sent again.
297
+ const NEVER_SENT = new Set([
298
+ "ECONNREFUSED",
299
+ "ENOTFOUND",
300
+ "EHOSTUNREACH",
301
+ "ENETUNREACH",
302
+ "EAI_AGAIN",
303
+ ]);
304
+ const FINISHED = ["done", "failed", "needs_input"];
305
+ const escalationKey = (agent) => `wake-escalation:${agent}`;
306
+
294
307
  /**
295
308
  * The daemon's wakes for local agents, the same rules as the server's for Grok: new open work for the agent wakes it
296
309
  * unless a wake is unanswered (then one more follows its report), never its own posts, at most 20 an hour, and a
297
- * refused wake is retried until one gets through or the retries run out.
310
+ * refused wake is retried until one gets through or the retries run out. Every attempt is kept on the board
311
+ * (store.recordWake), so a restart neither forgets a retry that was due nor starts the count again. When the retries
312
+ * run out the agent's escalation opens once (`escalate`: a note in each thread its work waits in, a desktop notice);
313
+ * more refusals refresh it, and only a thread not yet told hears it. It closes when the agent answers: its gateway
314
+ * takes a wake, or it uses the board.
298
315
  */
299
316
  export function createLocalWaker({
317
+ store,
300
318
  log = () => {},
301
319
  send = sendLocalWake,
302
320
  targets = localWakes,
303
321
  retryDelaysMs = RETRY_DELAYS_MS,
304
- } = {}) {
305
- const state = new Map();
306
- const told = new Set();
307
- const of = (agent) =>
308
- state.get(agent) ??
309
- (state.set(agent, {
310
- outstandingAt: 0,
311
- more: false,
312
- sent: [],
313
- failures: 0,
314
- lastFailure: null,
315
- }),
316
- state.get(agent));
317
- // A wake that holds is out until the agent reports; a result to read is sent only when none is out and holds nothing.
318
- // `force`: work it was woken for was not picked up (handoffs.mjs), so this wake goes past the one still out.
319
- const wake = async (agent, taskId, { holds = true, force = false } = {}) => {
322
+ escalate = () => {},
323
+ }) {
324
+ // A wake that holds is out until the agent reports; `more`: work came meanwhile, so one more wake follows the report.
325
+ const outstanding = new Map();
326
+ const retries = new Map();
327
+ const telling = new Set();
328
+ const failedAttempt = (row) =>
329
+ row.outcome === "refused" || (row.outcome === "unsure" && row.holds);
330
+ // The agent's refused wakes since the last that went through, oldest first.
331
+ const failuresOf = (agent) =>
332
+ store.wakesSinceSent(agent).filter(failedAttempt);
333
+ const spent = (failures) => failures.length > retryDelaysMs.length;
334
+
335
+ const escalationOf = (agent) => {
336
+ try {
337
+ return JSON.parse(store.meta(escalationKey(agent)) || "null");
338
+ } catch {
339
+ return null;
340
+ }
341
+ };
342
+ // The open threads where work waits for the agent.
343
+ const waitingThreads = (agent) => [
344
+ ...new Set(
345
+ store
346
+ .unfinishedWork()
347
+ .filter(
348
+ (task) =>
349
+ task.status === "open" &&
350
+ task.target_agent === agent &&
351
+ task.thread_status === "open"
352
+ )
353
+ .map((task) => task.parent_id)
354
+ ),
355
+ ];
356
+ const escalateOnce = (agent, detail) => {
357
+ const open = escalationOf(agent);
358
+ const told = open?.threads ?? [];
359
+ const fresh = waitingThreads(agent).filter((id) => !told.includes(id));
360
+ const at = new Date().toISOString();
361
+ store.meta(
362
+ escalationKey(agent),
363
+ JSON.stringify({
364
+ at: open?.at ?? at,
365
+ refreshedAt: at,
366
+ detail,
367
+ threads: [...told, ...fresh],
368
+ })
369
+ );
370
+ log(`wake ${agent}: retries spent (${detail})${open ? ", refreshed" : ""}`);
371
+ if (!open || fresh.length)
372
+ escalate({ agent, detail, threads: fresh, first: !open });
373
+ };
374
+ const clearEscalation = (agent, why) => {
375
+ if (!escalationOf(agent)) return;
376
+ store.meta(escalationKey(agent), "");
377
+ log(`wake ${agent}: answering again (${why})`);
378
+ };
379
+ // An agent that used the board since its escalation opened is answering.
380
+ const noticeAnswers = () => {
381
+ for (const agent of LOCAL_WAKE_AGENTS) {
382
+ const open = escalationOf(agent);
383
+ if (!open) continue;
384
+ const seenAt = store
385
+ .agentsSeen()
386
+ .find((row) => row.agent === agent)?.last_seen_at;
387
+ if (seenAt && seenAt > open.at)
388
+ clearEscalation(agent, "it used the board");
389
+ }
390
+ };
391
+
392
+ // The next retry, at the time the last refusal set: armed again when the daemon starts.
393
+ const arm = (agent) => {
394
+ clearTimeout(retries.get(agent));
395
+ retries.delete(agent);
396
+ const failures = failuresOf(agent);
397
+ if (!failures.length || spent(failures)) return;
398
+ const last = failures.at(-1);
399
+ const due = Date.parse(last.at) + retryDelaysMs[failures.length - 1];
400
+ const timer = setTimeout(
401
+ () => {
402
+ retries.delete(agent);
403
+ const now = failuresOf(agent);
404
+ // A later attempt armed its own retry.
405
+ if (now.length === failures.length && now.at(-1).at === last.at)
406
+ void attempt(agent, last.task_id, { holds: last.holds });
407
+ },
408
+ Math.max(0, due - Date.now())
409
+ );
410
+ timer.unref?.();
411
+ retries.set(agent, timer);
412
+ };
413
+
414
+ const attempt = async (agent, taskId, { holds, about = null }) => {
320
415
  const target = targets()[agent];
321
416
  if (!target) return "none";
322
- const s = of(agent),
323
- now = Date.now();
324
- if (!force && s.outstandingAt && now - s.outstandingAt < ANSWER_WINDOW_MS) {
325
- s.more = true;
326
- log(`wake ${agent}: merged (${taskId})`);
327
- return "merged";
328
- }
329
- s.sent = s.sent.filter((at) => now - at < 3_600_000);
330
- if (s.sent.length >= MAX_PER_HOUR) {
417
+ const hourAgo = new Date(Date.now() - 3_600_000).toISOString();
418
+ if (store.wakesTried(agent, hourAgo) >= MAX_PER_HOUR) {
419
+ store.recordWake({ agent, taskId, about, holds, outcome: "capped" });
331
420
  log(`wake ${agent}: capped at ${MAX_PER_HOUR} an hour (${taskId})`);
332
421
  return "capped";
333
422
  }
334
- s.outstandingAt = holds ? now : 0;
335
- s.more = false;
336
- s.sent.push(now);
423
+ if (holds) outstanding.set(agent, { at: Date.now(), more: false });
424
+ else outstanding.delete(agent);
337
425
  const sent = await send(agent, target).catch((error) => ({
338
426
  ok: false,
339
427
  detail: error.message,
428
+ unsure: !NEVER_SENT.has(error.cause?.code ?? error.code),
340
429
  }));
430
+ const outcome = sent.ok ? "sent" : sent.unsure ? "unsure" : "refused";
431
+ store.recordWake({
432
+ agent,
433
+ taskId,
434
+ about,
435
+ holds,
436
+ outcome,
437
+ detail: sent.detail,
438
+ });
341
439
  log(
342
- `wake ${agent}: ${sent.ok ? "sent" : "failed"}, ${sent.detail} (${taskId})`
440
+ `wake ${agent}: ${sent.ok ? "sent" : sent.unsure ? "may have landed" : "failed"}, ${sent.detail} (${taskId})`
343
441
  );
344
442
  if (sent.ok) {
345
- s.failures = 0;
346
- s.lastFailure = null;
443
+ clearEscalation(agent, "its gateway took a wake");
444
+ arm(agent);
347
445
  return "sent";
348
446
  }
349
- s.outstandingAt = 0;
350
- s.failures += 1;
351
- s.lastFailure = sent.detail;
352
- const failures = s.failures,
353
- delay = retryDelaysMs[failures - 1];
354
- if (delay !== undefined)
355
- setTimeout(() => {
356
- if (of(agent).failures === failures)
357
- void wake(agent, taskId, { holds });
358
- }, delay).unref?.();
359
- return "failed";
447
+ outstanding.delete(agent);
448
+ if (spent(failuresOf(agent))) escalateOnce(agent, sent.detail);
449
+ else arm(agent);
450
+ return sent.unsure ? "unsure" : "failed";
360
451
  };
452
+
453
+ // `force`: work it was woken for was not picked up (handoffs.mjs), so this wake goes past the one still out.
454
+ // `about`: the result this wake tells the agent of, kept so it is told once.
455
+ const wake = async (
456
+ agent,
457
+ taskId,
458
+ { holds = true, force = false, about = null } = {}
459
+ ) => {
460
+ if (!targets()[agent]) return "none";
461
+ const out = outstanding.get(agent);
462
+ if (!force && out && Date.now() - out.at < ANSWER_WINDOW_MS) {
463
+ out.more = true;
464
+ store.recordWake({ agent, taskId, about, holds, outcome: "merged" });
465
+ log(`wake ${agent}: merged (${taskId})`);
466
+ return "merged";
467
+ }
468
+ // A refused wake waiting for its retry carries this one too: the wake names no task, and new work does not start
469
+ // the count of retries again.
470
+ const failures = failuresOf(agent);
471
+ if (!force && failures.length && !spent(failures)) {
472
+ store.recordWake({ agent, taskId, about, holds, outcome: "merged" });
473
+ log(`wake ${agent}: waits for its retry (${taskId})`);
474
+ return "merged";
475
+ }
476
+ return attempt(agent, taskId, { holds, about });
477
+ };
478
+
361
479
  return {
362
480
  wake,
481
+ /** Arms the retries that were due when the daemon stopped, and escalates what ran out meanwhile. */
482
+ start() {
483
+ for (const agent of LOCAL_WAKE_AGENTS) {
484
+ if (!targets()[agent]) continue;
485
+ const failures = failuresOf(agent);
486
+ if (spent(failures) && !escalationOf(agent))
487
+ escalateOnce(agent, failures.at(-1).detail);
488
+ else arm(agent);
489
+ }
490
+ },
491
+ stop() {
492
+ for (const timer of retries.values()) clearTimeout(timer);
493
+ retries.clear();
494
+ },
363
495
  /** Per agent: refused wakes in a row and the last reason; past the retries it is not answering. */
364
496
  status: () =>
365
497
  Object.fromEntries(
366
- [...state].map(([agent, s]) => [
367
- agent,
368
- {
369
- failures: s.failures,
370
- lastFailure: s.lastFailure,
371
- answering: s.failures <= retryDelaysMs.length,
372
- },
373
- ])
498
+ store.wakeAgents().map((agent) => {
499
+ const failures = failuresOf(agent);
500
+ return [
501
+ agent,
502
+ {
503
+ failures: failures.length,
504
+ lastFailure: failures.at(-1)?.detail ?? null,
505
+ answering: !spent(failures),
506
+ },
507
+ ];
508
+ })
374
509
  ),
375
510
  onChange(task) {
376
511
  if (!task) return;
377
- // The agent that asked for the work hears when it finishes, fails or asks back (never for work it did itself),
378
- // once per result: what finished is in its list_tasks.
379
- const asker = task.origin_agent;
512
+ noticeAnswers();
513
+ // The agent that asked for the work hears when it finishes, fails or asks back (answers.mjs), once per result,
514
+ // across restarts: what finished is in its list_tasks.
515
+ const asker = askerOf(task);
516
+ const about = `${task.id}:${task.status}`;
380
517
  if (
381
- ["done", "failed", "needs_input"].includes(task.status) &&
382
518
  LOCAL_WAKE_AGENTS.includes(asker) &&
383
- asker !== task.claimed_by &&
384
- asker !== task.target_agent &&
385
- !["wake", "goal"].includes(task.kind) &&
386
- !told.has(`${task.id}:${task.status}`)
519
+ !telling.has(about) &&
520
+ !store.wakeTold(asker, about)
387
521
  ) {
388
- told.add(`${task.id}:${task.status}`);
389
- if (told.size > 10_000) told.delete(told.values().next().value);
390
- void wake(asker, task.id, { holds: false });
522
+ telling.add(about);
523
+ if (telling.size > 10_000)
524
+ telling.delete(telling.values().next().value);
525
+ void wake(asker, task.id, { holds: false, about });
391
526
  }
392
527
  if (task.claimed_by && LOCAL_WAKE_AGENTS.includes(task.claimed_by)) {
393
- const s = of(task.claimed_by);
394
- if (["done", "failed", "needs_input"].includes(task.status)) {
395
- s.outstandingAt = 0;
396
- if (s.more) {
397
- s.more = false;
398
- void wake(task.claimed_by, task.id);
399
- }
528
+ if (FINISHED.includes(task.status)) {
529
+ const out = outstanding.get(task.claimed_by);
530
+ outstanding.delete(task.claimed_by);
531
+ if (out?.more) void wake(task.claimed_by, task.id);
400
532
  }
401
533
  return;
402
534
  }
package/lib/worktrees.mjs CHANGED
@@ -318,9 +318,18 @@ export function createWorktrees({ store, root }) {
318
318
  });
319
319
  };
320
320
 
321
+ /** The commit the thread's branch is at now: what a check or review saw. Null for a thread with no copy of its own. */
322
+ const head = (threadId) => {
323
+ const tree = get(threadId);
324
+ if (!tree || tree.state !== "open" || !fs.existsSync(tree.path))
325
+ return null;
326
+ return git(tree.path, ["rev-parse", "HEAD"]).out || null;
327
+ };
328
+
321
329
  return {
322
330
  folderFor,
323
331
  commitTurn,
332
+ head,
324
333
  diff,
325
334
  summary,
326
335
  merge,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tryharness",
3
- "version": "0.7.42",
3
+ "version": "0.7.44",
4
4
  "description": "Harness Relay: the daemon that turns the agents on this machine into your personal swarm. Memory, history, and work handed between any of them.",
5
5
  "bin": {
6
6
  "tryharness": "cli.mjs"