tryharness 0.7.54 → 0.7.58

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 (156) hide show
  1. package/README.md +15 -16
  2. package/cli.mjs +384 -62
  3. package/lib/activity.mjs +10 -10
  4. package/lib/adapters.mjs +16 -0
  5. package/lib/agentBinaries.mjs +71 -0
  6. package/lib/agentRuntimeConfig.mjs +1 -1
  7. package/lib/agentRuntimes.mjs +9 -3
  8. package/lib/agentSignIn.mjs +271 -41
  9. package/lib/answers.mjs +5 -5
  10. package/lib/archetypes.d.ts +64 -0
  11. package/lib/archetypes.js +989 -0
  12. package/lib/asks.mjs +4 -4
  13. package/lib/audit.mjs +36 -13
  14. package/lib/backupSetup.mjs +4 -2
  15. package/lib/boardAccess.mjs +17 -11
  16. package/lib/boardBackup.mjs +6 -6
  17. package/lib/boardFileCommands.mjs +6 -6
  18. package/lib/boardFiles.mjs +13 -12
  19. package/lib/boardMirror.mjs +82 -47
  20. package/lib/boardProtocol.json +30 -1
  21. package/lib/boardWaits.mjs +2 -3
  22. package/lib/book.mjs +6 -6
  23. package/lib/bridge.mjs +5 -5
  24. package/lib/cards.mjs +2 -2
  25. package/lib/cloudAgents.mjs +216 -0
  26. package/lib/cloudAi.mjs +1312 -0
  27. package/lib/cloudAssistants.mjs +242 -0
  28. package/lib/cloudBoot.mjs +81 -86
  29. package/lib/cloudSave.mjs +197 -0
  30. package/lib/cloudSsh.mjs +178 -0
  31. package/lib/context.mjs +3 -3
  32. package/lib/contractGate.mjs +34 -12
  33. package/lib/control-client.mjs +9 -1
  34. package/lib/controlSocket.mjs +86 -0
  35. package/lib/coordinator.mjs +103 -101
  36. package/lib/daemon.mjs +1128 -2865
  37. package/lib/desk.mjs +7 -6
  38. package/lib/desktop.mjs +72 -68
  39. package/lib/directLink.mjs +16 -5
  40. package/lib/environment.mjs +2 -1
  41. package/lib/environments.mjs +14 -6
  42. package/lib/firewall.mjs +9 -9
  43. package/lib/folders.mjs +18 -14
  44. package/lib/frames.mjs +2 -3
  45. package/lib/gitHost.mjs +3 -3
  46. package/lib/goalCommands.mjs +25 -7
  47. package/lib/goals.mjs +7 -7
  48. package/lib/guestRequests.mjs +94 -0
  49. package/lib/handoffs.mjs +8 -7
  50. package/lib/handover.mjs +4 -4
  51. package/lib/harnessModel.mjs +6 -6
  52. package/lib/harnessOffers.mjs +266 -0
  53. package/lib/inference.mjs +26 -8
  54. package/lib/jobAnswers.mjs +190 -0
  55. package/lib/landed.mjs +1 -1
  56. package/lib/lane.mjs +42 -11
  57. package/lib/lessons.mjs +16 -16
  58. package/lib/library.mjs +3 -3
  59. package/lib/lineup.mjs +3 -0
  60. package/lib/linkRelay.mjs +574 -0
  61. package/lib/localAgentAsk.mjs +74 -0
  62. package/lib/localStack.mjs +536 -0
  63. package/lib/localmodels.mjs +6 -4
  64. package/lib/loopBackstop.mjs +4 -4
  65. package/lib/machineIdentity.mjs +9 -1
  66. package/lib/machineRoutes.mjs +181 -2
  67. package/lib/machineRuns.mjs +76 -1
  68. package/lib/machineSeal.mjs +39 -12
  69. package/lib/mcp-stdio.mjs +96 -26
  70. package/lib/mcpRelay.mjs +1 -1
  71. package/lib/members.json +13 -0
  72. package/lib/members.mjs +1 -1
  73. package/lib/models.mjs +77 -23
  74. package/lib/museSession.mjs +6 -1
  75. package/lib/nativeHost.mjs +35 -7
  76. package/lib/network.mjs +50 -5
  77. package/lib/notify.mjs +7 -5
  78. package/lib/nudge.mjs +3 -3
  79. package/lib/ocr.mjs +9 -9
  80. package/lib/onboard.mjs +141 -50
  81. package/lib/oneLine.mjs +7 -7
  82. package/lib/ownMachines.mjs +928 -149
  83. package/lib/pairing.mjs +39 -8
  84. package/lib/paths.mjs +15 -1
  85. package/lib/peerProcess.mjs +124 -0
  86. package/lib/personModel.mjs +3 -3
  87. package/lib/personNotices.mjs +151 -0
  88. package/lib/phones.mjs +490 -38
  89. package/lib/placeCommands.mjs +4 -4
  90. package/lib/places.mjs +70 -2
  91. package/lib/profile.mjs +67 -14
  92. package/lib/projectGit.mjs +158 -14
  93. package/lib/projectPieces.mjs +902 -0
  94. package/lib/projectSettings.mjs +7 -4
  95. package/lib/projects.mjs +384 -62
  96. package/lib/provenance.mjs +5 -1
  97. package/lib/proxyLinks.mjs +267 -0
  98. package/lib/reactions.mjs +7 -5
  99. package/lib/readiness.mjs +12 -7
  100. package/lib/record.mjs +19 -6
  101. package/lib/redact.mjs +4 -4
  102. package/lib/relayLimits.mjs +62 -0
  103. package/lib/release.mjs +170 -0
  104. package/lib/releaseKeys.json +6 -0
  105. package/lib/repoLinks.mjs +4 -4
  106. package/lib/roomChannels.mjs +24 -0
  107. package/lib/roomPage.mjs +1 -1
  108. package/lib/runAuthority.mjs +101 -0
  109. package/lib/runBrief.mjs +19 -8
  110. package/lib/runWorker.mjs +14 -5
  111. package/lib/runner.mjs +215 -25
  112. package/lib/runtime-install.mjs +143 -9
  113. package/lib/schedules.mjs +2 -2
  114. package/lib/screens.mjs +1 -1
  115. package/lib/sealed.mjs +11 -2
  116. package/lib/sealedThreads.mjs +671 -0
  117. package/lib/secretPatterns.json +1 -1
  118. package/lib/secrets.mjs +4 -4
  119. package/lib/seen.mjs +6 -6
  120. package/lib/serverWords.mjs +175 -0
  121. package/lib/service.mjs +22 -12
  122. package/lib/sessionControl.mjs +6 -6
  123. package/lib/specialties.mjs +6 -6
  124. package/lib/specialtyKeys.js +4 -5
  125. package/lib/specialtyNotice.mjs +7 -7
  126. package/lib/specialtySnapshot.mjs +5 -5
  127. package/lib/speech.mjs +5 -5
  128. package/lib/store.mjs +140 -39
  129. package/lib/stuck.mjs +1 -1
  130. package/lib/swarm.mjs +167 -16
  131. package/lib/swarmDevices.mjs +319 -0
  132. package/lib/swarmMemory.mjs +27 -25
  133. package/lib/t3.mjs +13 -13
  134. package/lib/taskCommands.mjs +44 -11
  135. package/lib/taskRooms.mjs +125 -0
  136. package/lib/taskRuns.mjs +36 -1
  137. package/lib/team.mjs +41 -18
  138. package/lib/threadActions.mjs +315 -0
  139. package/lib/threadAuthority.mjs +101 -0
  140. package/lib/threadBriefs.mjs +178 -0
  141. package/lib/threadLine.mjs +2 -2
  142. package/lib/threadRuns.mjs +138 -43
  143. package/lib/threadView.mjs +5 -5
  144. package/lib/usageTotals.mjs +2 -3
  145. package/lib/vault.mjs +101 -10
  146. package/lib/vaultCloud.mjs +2 -2
  147. package/lib/vaultCommands.mjs +15 -6
  148. package/lib/vaultTools.js +2 -2
  149. package/lib/verdicts.mjs +328 -0
  150. package/lib/wakes.mjs +1 -1
  151. package/lib/watches.mjs +2 -2
  152. package/lib/windowAsks.mjs +13 -5
  153. package/lib/worktrees.mjs +112 -18
  154. package/npm-shrinkwrap.json +1093 -0
  155. package/package.json +11 -10
  156. package/release.json +220 -0
@@ -0,0 +1,94 @@
1
+ import { t3GuestRequests, t3Link } from "./places.mjs";
2
+
3
+ // T3 Code's guest links (beta, October 2026) let someone without T3 follow one of the person's threads and send
4
+ // messages into it; each waits for the person's Send or Dismiss in T3, and a Send runs it with the thread's permission
5
+ // mode, which can be full access. T3 asks the person to check the words first. This reads each waiting message with the
6
+ // guard on this computer (firewall.mjs), so a guest's words never leave it, and puts the message, the guard's verdict
7
+ // and what a Send would run it with where the person sees what waits on them. Send and Dismiss stay in T3.
8
+
9
+ /**
10
+ * What waits on the person from T3 guests: `refresh` reads T3's record again and has the guard read each new
11
+ * message; `needsOf` is a thread's waiting messages as needs-you items. `changed` hears when either moved.
12
+ */
13
+ export function createGuestRequests({
14
+ guard,
15
+ log = () => {},
16
+ changed = () => {},
17
+ read = () => t3GuestRequests(),
18
+ linkOf = (threadId) => t3Link(threadId),
19
+ }) {
20
+ let waiting = new Map();
21
+ // The guard's reading of each message, by its id: a message's words never change while it waits.
22
+ const verdicts = new Map();
23
+ let refreshing = null;
24
+ let said = "";
25
+
26
+ const tell = () => {
27
+ const now = JSON.stringify([[...waiting], [...verdicts]]);
28
+ if (now === said) return;
29
+ said = now;
30
+ changed();
31
+ };
32
+
33
+ async function readNew() {
34
+ for (const [, request] of waiting)
35
+ for (const message of request.pending) {
36
+ if (verdicts.has(message.id)) continue;
37
+ verdicts.set(message.id, "reading");
38
+ try {
39
+ const read = await guard(message.text);
40
+ verdicts.set(
41
+ message.id,
42
+ read.unavailable ? "unavailable" : read.verdict
43
+ );
44
+ } catch (error) {
45
+ log(`guest request guard: ${error.message}`);
46
+ verdicts.set(message.id, "unavailable");
47
+ }
48
+ tell();
49
+ }
50
+ }
51
+
52
+ function refresh() {
53
+ if (refreshing) return refreshing;
54
+ refreshing = (async () => {
55
+ try {
56
+ waiting = await read();
57
+ const live = new Set(
58
+ [...waiting.values()].flatMap((request) =>
59
+ request.pending.map((message) => message.id)
60
+ )
61
+ );
62
+ for (const id of verdicts.keys())
63
+ if (!live.has(id)) verdicts.delete(id);
64
+ tell();
65
+ await readNew();
66
+ } catch (error) {
67
+ log(`guest requests: ${error.message}`);
68
+ } finally {
69
+ refreshing = null;
70
+ }
71
+ })();
72
+ return refreshing;
73
+ }
74
+
75
+ /** A thread's waiting guest messages, oldest first, as the buddy's needs-you card reads them. */
76
+ function needsOf(threadId, agent) {
77
+ const request = waiting.get(threadId);
78
+ if (!request) return [];
79
+ return request.pending.map((message) => ({
80
+ id: `guest:${message.id}`,
81
+ kind: "guest",
82
+ agent,
83
+ who: request.guestName ?? "Your guest",
84
+ text: message.text,
85
+ guard: verdicts.get(message.id) ?? "reading",
86
+ access: request.runtimeMode,
87
+ files: message.files,
88
+ link: linkOf(threadId),
89
+ at: message.submittedAt,
90
+ }));
91
+ }
92
+
93
+ return { refresh, needsOf };
94
+ }
package/lib/handoffs.mjs CHANGED
@@ -1,20 +1,21 @@
1
1
  import { nameOf } from "./members.mjs";
2
2
 
3
3
  // Work handed to an agent that has to be woken (Grok Bot by email, OpenClaw and Hermes on their gateways) is watched
4
- // until the agent picks it up. A wake can be accepted and still never start a run (October 19: two of Grok Bot's
5
- // emails went unanswered for 2 h and 20 min), so a missed pickup is never silent: the agent is woken again at 3
6
- // minutes, and at 10 the work moves to Harness, whose server takes it at once. The thread says so each time, and the
7
- // person can put the work back.
4
+ // until the agent picks it up. A wake can be accepted and still never start a run (two of Grok Bot's emails once went
5
+ // unanswered for 2 h and 20 min), so a missed pickup is never silent: the agent is woken again at 3 minutes, and at 10
6
+ // the work moves to Harness, whose server takes it at once. The thread says so each time, and the person can put the
7
+ // work back.
8
8
  //
9
- // The 3 and 10 minutes: Grok Bot's 36 email wakes from October 15 to 19 were claimed 13 to 59 s after sending, so 3
10
- // minutes is three times its slowest normal pickup (owner, October 19).
9
+ // The 3 and 10 minutes: Grok Bot's 36 email wakes over five days were claimed 13 to 59 s after sending, so 3
10
+ // minutes is three times its slowest normal pickup.
11
11
 
12
12
  export const HANDOFF_TIMING = { nudgeMs: 3 * 60_000, moveMs: 10 * 60_000 };
13
13
  // A setup check waits as long as setup does (onboard.mjs, ten minutes), then it is off the board.
14
14
  const SETUP_CHECK_MS = 15 * 60_000;
15
15
  export const WATCHED = new Set(["grok-bot", "openclaw", "hermes"]);
16
16
 
17
- // A setup check (onboard.mjs), marked since October 19, or by the exact objective setup posts from before that.
17
+ // A setup check (onboard.mjs): marked as one in the store, or, from a setup older than the mark, known by its exact
18
+ // objective.
18
19
  export const isSetupCheck = (store, task) =>
19
20
  Boolean(store.meta(`setup-check:${task.id}`)) ||
20
21
  task.objective === "Setup check from Harness: reply ready";
package/lib/handover.mjs CHANGED
@@ -8,10 +8,10 @@ import { oneLine } from "./oneLine.mjs";
8
8
  // turn and lose its record to a summary, so before resuming, the relay reads the session's last model call from the
9
9
  // agent's own file and, past the line, starts a fresh session that is told where the old one is and what was said last.
10
10
 
11
- // Where the line sits, measured on the owner's machine, October 20, 2026:
12
- // - Codex compacted 90 times since September 15; the last call before each carried a median 87% of the window Codex
13
- // reported, and 77% or more in 9 of 10 (the lowest 58%). Claude Code's automatic compactions of 1M-token sessions came
14
- // at 96.6% or more.
11
+ // Where the line sits, measured on one developer machine:
12
+ // - Codex compacted 90 times in five weeks; the last call before each carried a median 87% of the window Codex
13
+ // reported, and 77% or more in 9 of 10 (the lowest 58%). Claude Code's automatic compactions of 1M-token sessions
14
+ // came at 96.6% or more.
15
15
  // - A thread turn that resumed a session grew it by up to 39,469 tokens before its last call (13 turns of Claude Code and
16
16
  // Codex on the board with their session files still on disk; median 9,773 to 13,309).
17
17
  // So a session is resumed only while its last call plus one such turn stays under 77% of the window.
@@ -1,9 +1,9 @@
1
- // The model Harness answers with in the swarm's threads, picked in the buddy as every other agent's is (owner,
2
- // October 17: "harness behavior should be no different than the rest", a model "from the harness catalog based on the
3
- // provider selected"). A provider first: Harness credits, or one of the person's own keys or their endpoint once they
4
- // let Harness Relay keep it (lane.mjs); then one of the models Harness's catalog offers on it, the list the server sends
5
- // (src/server/swarm/harnessModels.ts), or the endpoint's own models. With no pick Harness answers with the person's last
6
- // Harness model, as before. Harness's server checks the pick again before a task runs on it.
1
+ // The model Harness answers with in the swarm's threads, picked in the buddy as every other agent's is: Harness
2
+ // behaves no differently from the rest, with a model from Harness's catalog for the provider selected. A provider
3
+ // first: Harness credits, or one of the person's own keys or their endpoint once they let Harness Relay keep it
4
+ // (lane.mjs); then one of the models Harness's catalog offers on it, the list Harness's server sends, or the endpoint's
5
+ // own models. With no pick Harness answers with the person's last Harness model, as before. Harness's server checks the
6
+ // pick again before a task runs on it.
7
7
 
8
8
  const catalogKey = "models:harness";
9
9
  const choiceKey = "model-choice:harness";
@@ -0,0 +1,266 @@
1
+ import { speakerOf } from "./coordinator.mjs";
2
+
3
+ // The words of a task asked before they were kept apart from its brief.
4
+ const ASKED_WORDS =
5
+ /^Asked in the thread "[\s\S]*?" \(thread [0-9a-f-]+\):\n\n([\s\S]*?)\n\nRead the thread/;
6
+
7
+ // Posts that say something, as against the coordinator's notes, held posts and rounds.
8
+ const said = (post) => !["note", "held", "round"].includes(post.kind);
9
+
10
+ /**
11
+ * Harness's server takes a task given to Harness in a thread while no Harness window is open, only while this
12
+ * computer's coordinator runs on Harness: on the privacy setting the thread's words stay here and the task waits for a
13
+ * window. Harness holds the task meanwhile, so a window that opens does not take it too; what the server leaves for the
14
+ * browser goes back on the board, and the window takes it over when it next opens.
15
+ *
16
+ * `boardCommand()` and `asks()` are made after this module. `serverHas(feature)` says what the connected Harness
17
+ * supports; `lane` and `model` are the person's own model lane (lane.mjs) and their pick for Harness
18
+ * (harnessModel.mjs).
19
+ */
20
+ export function createHarnessOffers({
21
+ store,
22
+ askHarness,
23
+ connected,
24
+ serverHas,
25
+ privacy,
26
+ boardCommand,
27
+ notifyBoard,
28
+ asks,
29
+ lane,
30
+ model,
31
+ files,
32
+ }) {
33
+ // Harness's tasks its server is working, by task, with their thread: an ask for Harness there goes into that turn.
34
+ const turns = new Map();
35
+
36
+ // What the person let a thread spend of their Harness credits, for a Harness that checks it.
37
+ const ceiling = (goalId) => {
38
+ const limit = Number(store.meta(`spend-ceiling:${goalId}`));
39
+ return serverHas("swarm_goal") &&
40
+ store.meta(`spend-ceiling:${goalId}`) &&
41
+ Number.isFinite(limit)
42
+ ? { ceilingUsd: limit }
43
+ : {};
44
+ };
45
+
46
+ // The newest 30 posts, as every agent's brief starts from the newest, and the person's first words when they fell out
47
+ // of them (in a long thread the roles they set were gone), each with its number for a Harness that cites posts by it.
48
+ const transcriptOf = (goal) => {
49
+ const latest = store
50
+ .recentRoomPosts(goal.id, { limit: 90 })
51
+ .posts.filter(said)
52
+ .slice(-30);
53
+ const opening = store
54
+ .roomPosts(goal.id, 0, 100)
55
+ .find((post) => said(post) && post.author === "user");
56
+ const numbered = serverHas("swarm_goal");
57
+ return [
58
+ ...(opening && !latest.some((post) => post.seq === opening.seq)
59
+ ? [opening]
60
+ : []),
61
+ ...latest,
62
+ ].map((post) => ({
63
+ ...(numbered ? { seq: post.seq } : {}),
64
+ // The coordinator's posts read as the coordinator's, not as Harness's own.
65
+ author: speakerOf(store, goal, post),
66
+ kind: post.kind,
67
+ text: post.text.slice(0, 19_000),
68
+ }));
69
+ };
70
+
71
+ const offer = (goal, taskId, asker, words) => {
72
+ // Not offered: the thread's waits say why; on the privacy setting no note says it.
73
+ const unoffered = privacy()
74
+ ? "on the privacy setting the thread's words stay on this computer"
75
+ : !connected()
76
+ ? "this computer is not connected to Harness right now"
77
+ : !serverHas("harness_task")
78
+ ? "Harness's server cannot take thread tasks yet"
79
+ : null;
80
+ if (unoffered) {
81
+ store.meta(`harness-wait:${taskId}`, unoffered);
82
+ return;
83
+ }
84
+ if (
85
+ !boardCommand()({ command: "board.claim", agent: "harness", id: taskId })
86
+ ?.ok
87
+ )
88
+ return;
89
+ const transcript = transcriptOf(goal);
90
+ // Back to the board for a window, as the person would put it back, with what the thread should hear.
91
+ const back = (post) => {
92
+ if (post) store.roomPost({ goalId: goal.id, author: "harness", ...post });
93
+ boardCommand()({ command: "board.release", agent: "user", id: taskId });
94
+ notifyBoard(store.getTask(goal.id));
95
+ };
96
+ // The thread hears why once for each reason, not once for each task: each @harness is its own task, and the note
97
+ // would come every time even when an open window took the task a second later. The thread's waits keep saying why
98
+ // for as long as a task waits.
99
+ const waits = (why) => {
100
+ store.meta(`harness-wait:${taskId}`, why);
101
+ const told = store.meta(`harness-wait-said:${goal.id}`) === why;
102
+ if (!told) store.meta(`harness-wait-said:${goal.id}`, why);
103
+ back(
104
+ told
105
+ ? null
106
+ : {
107
+ kind: "note",
108
+ text: `This waits for your Harness window: ${why}.`,
109
+ }
110
+ );
111
+ };
112
+ const attached = serverHas("harness_files")
113
+ ? {
114
+ files: files
115
+ .placeFiles(goal)
116
+ .filter((file) => file.present)
117
+ .slice(-50)
118
+ .map(files.harnessFileView),
119
+ }
120
+ : {};
121
+ // The person's own key or endpoint goes only when Harness's server says the task needs it, or with the model the
122
+ // person picked for Harness on it, and only if they let Harness Relay keep it; otherwise the thread says where to
123
+ // allow it.
124
+ const held = serverHas("harness_task_lane") ? lane.held() : null;
125
+ const picked = serverHas("harness_model") ? model.forTask() : null;
126
+ const ask = (withLane) =>
127
+ askHarness(
128
+ "harness_task",
129
+ {
130
+ taskId,
131
+ thread: { id: goal.id, title: goal.objective.slice(0, 500) },
132
+ asker,
133
+ words: String(words).slice(0, 20_000),
134
+ transcript,
135
+ ...attached,
136
+ ...ceiling(goal.id),
137
+ ...(picked ? { model: picked.model } : {}),
138
+ },
139
+ null,
140
+ withLane
141
+ ? {
142
+ lane: {
143
+ modelId: withLane.modelId,
144
+ credentialSource: withLane.credentialSource,
145
+ },
146
+ }
147
+ : null
148
+ ).then((reply) =>
149
+ reply?.ok &&
150
+ !reply.taken &&
151
+ reply.why === "own_key" &&
152
+ held &&
153
+ !withLane
154
+ ? ask(held)
155
+ : reply
156
+ );
157
+ // The asks its turn read are answered with it; whoever made them hears its answer as the task's asker does.
158
+ const turnEnded = (outcome) => {
159
+ turns.delete(taskId);
160
+ const read = outcome?.status === "done" ? (outcome.read ?? []) : [];
161
+ const askers = asks().turnEnded(goal.id, taskId, read);
162
+ if (askers.length)
163
+ store.meta(`harness-also-asked:${taskId}`, JSON.stringify(askers));
164
+ };
165
+ const settle = (outcome) => {
166
+ // The thread used what the person let it spend: they are asked once for each limit, and the task waits.
167
+ if (!outcome.taken && outcome.why === "ceiling") {
168
+ const limit = store.meta(`spend-ceiling:${goal.id}`);
169
+ const asked = store.meta(`spend-ceiling-asked:${goal.id}`) === limit;
170
+ store.meta(`spend-ceiling-asked:${goal.id}`, limit);
171
+ return back(
172
+ asked
173
+ ? null
174
+ : {
175
+ kind: "question",
176
+ text: `Harness has reached what this thread may spend: ${outcome.detail}. Raise the limit to go on (npx tryharness swarm ceiling ${goal.id} <dollars>), or say if the swarm should go on without Harness's part.`,
177
+ }
178
+ );
179
+ }
180
+ if (!outcome.taken)
181
+ return outcome.why === "window_open"
182
+ ? back(null)
183
+ : waits(
184
+ outcome.why === "own_key" && serverHas("harness_task_lane")
185
+ ? `${outcome.detail}. To let Harness answer here while your window is closed, turn on "Harness Relay may use it" in Harness's settings, under Models`
186
+ : outcome.detail || "Harness could not take it here"
187
+ );
188
+ // Its words are Harness's server's, not the window's: they wake no one with the person's go (jobChanged).
189
+ if (outcome.status === "done")
190
+ store.meta(`harness-served:${taskId}`, "1");
191
+ if (outcome.status === "done")
192
+ return boardCommand()({
193
+ command: "board.report",
194
+ agent: "harness",
195
+ id: taskId,
196
+ status: "done",
197
+ result: outcome.text,
198
+ });
199
+ if (outcome.status === "window") {
200
+ // What is left needs the browser: offered to Harness's server again it would only be handed back.
201
+ store.meta(`harness-window:${taskId}`, "1");
202
+ return back({
203
+ kind: "post",
204
+ text: `${outcome.text}\n\nThe rest needs your browser: ${outcome.windowOpen ? "your Harness window is taking it over" : "Harness takes it over when your Harness window next opens"}.`,
205
+ });
206
+ }
207
+ waits(
208
+ `Harness could not finish it here${outcome.text ? ` (${outcome.text.slice(0, 300)})` : ""}`
209
+ );
210
+ };
211
+ turns.set(taskId, goal.id);
212
+ ask(picked?.lane ?? null).then(
213
+ (reply) => {
214
+ // askHarness spreads Harness's answer into the reply.
215
+ const outcome = reply?.ok ? reply : null;
216
+ turnEnded(outcome);
217
+ if (!outcome)
218
+ return waits(
219
+ `Harness could not take it here (${reply?.error || "no answer"})`
220
+ );
221
+ settle(outcome);
222
+ },
223
+ (error) => {
224
+ turnEnded(null);
225
+ waits(`Harness could not take it here (${error.message})`);
226
+ }
227
+ );
228
+ };
229
+
230
+ /** A task just asked of Harness in a thread: its words are kept so it can be offered again, then it is offered. */
231
+ const offerAsk = (goal, taskId, asker, words) => {
232
+ store.meta(
233
+ `harness-ask:${taskId}`,
234
+ JSON.stringify({ asker, words: String(words) })
235
+ );
236
+ offer(goal, taskId, asker, words);
237
+ };
238
+
239
+ // Harness's parts left waiting for a window go back to Harness's server when what kept them may have changed: the
240
+ // person's next Harness turn ran on Harness credits, their Resident changed or their last Harness window closed (the
241
+ // server's harness_ready), or this computer reached Harness again. The server takes each one it now can; the rest
242
+ // keep waiting, saying nothing new. What the server already left for the browser stays for the window.
243
+ const reofferAll = () => {
244
+ for (const goal of store.goals()) {
245
+ if (goal.status !== "open") continue;
246
+ for (const run of store.threadRuns(goal.id)) {
247
+ if (run.target_agent !== "harness" || run.status !== "open") continue;
248
+ if (store.meta(`harness-window:${run.id}`)) continue;
249
+ const task = store.getTask(run.id);
250
+ let asked = null;
251
+ try {
252
+ asked = JSON.parse(store.meta(`harness-ask:${run.id}`) || "null");
253
+ } catch {}
254
+ const words = asked?.words ?? ASKED_WORDS.exec(task.brief || "")?.[1];
255
+ if (!words) continue;
256
+ offer(goal, run.id, asked?.asker ?? task.origin_agent, words);
257
+ }
258
+ }
259
+ };
260
+
261
+ /** The task Harness's server is working in a thread now, if any. */
262
+ const turnOf = (threadId) =>
263
+ [...turns].find(([, thread]) => thread === threadId)?.[0] ?? null;
264
+
265
+ return { offerAsk, reofferAll, turnOf };
266
+ }
package/lib/inference.mjs CHANGED
@@ -3,6 +3,7 @@ import net from "node:net";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
5
  import { spawn, spawnSync } from "node:child_process";
6
+ import { randomBytes } from "node:crypto";
6
7
  import {
7
8
  MODELS,
8
9
  modelById,
@@ -123,10 +124,9 @@ export function createPower({
123
124
  );
124
125
  },
125
126
  /**
126
- * Whether the dream waits. It runs whenever the computer is on (owner, October 22: "if the computer is just on
127
- * obviously"), but not hot, below 20% on battery, or in Low Power Mode, unless Lid-Closed Mode holds the Mac awake:
128
- * it turns Low Power Mode on with the lid closed, and the dream should run then ("if insomnia is on dream mode
129
- * should run on low power").
127
+ * Whether the dream waits. It runs whenever the computer is on, but not hot, below 20% on battery, or in Low Power
128
+ * Mode, unless Lid-Closed Mode holds the Mac awake: it turns Low Power Mode on with the lid closed, and the dream
129
+ * runs then, on low power.
130
130
  */
131
131
  dreamWaits() {
132
132
  const now = this.state();
@@ -152,6 +152,12 @@ export function createInference({
152
152
  }) {
153
153
  ensureDir(root);
154
154
  const deviceFile = path.join(root, "device.json");
155
+ // The model server answers only this daemon: a key of its own, read from an owner-only file (not the command line,
156
+ // which any program here can list), and no slots endpoint, which shows what was last sent to a model. The models'
157
+ // own servers the router starts on other local ports take no key (llama.cpp's router does not pass one on).
158
+ const apiKey = randomBytes(32).toString("base64url");
159
+ const keyFile = path.join(root, "llama-key");
160
+ const keyed = { authorization: `Bearer ${apiKey}` };
155
161
  let server = null;
156
162
  let starting = null;
157
163
  const queues = { llama: [], onnx: [] };
@@ -203,6 +209,7 @@ export function createInference({
203
209
  const launch = async (gpu) => {
204
210
  const binary = llamaServer(root) ?? (await ensureLlama({ root, gpu, log }));
205
211
  const port = await freePort();
212
+ fs.writeFileSync(keyFile, `${apiKey}\n`, { mode: 0o600 });
206
213
  const child = spawn(
207
214
  binary,
208
215
  [
@@ -215,6 +222,9 @@ export function createInference({
215
222
  "--port",
216
223
  String(port),
217
224
  "--no-webui",
225
+ "--api-key-file",
226
+ keyFile,
227
+ "--no-slots",
218
228
  ],
219
229
  { cwd: path.dirname(binary), stdio: ["ignore", "ignore", "pipe"] }
220
230
  );
@@ -229,7 +239,7 @@ export function createInference({
229
239
  `the local model server stopped: ${tail.trim().split("\n").at(-1) || child.exitCode}`
230
240
  );
231
241
  try {
232
- if ((await fetch(`${base}/health`)).ok)
242
+ if ((await fetch(`${base}/health`, { headers: keyed })).ok)
233
243
  return {
234
244
  child,
235
245
  base,
@@ -351,7 +361,7 @@ export function createInference({
351
361
  async function post(base, pathname, body) {
352
362
  const response = await fetch(`${base}${pathname}`, {
353
363
  method: "POST",
354
- headers: { "content-type": "application/json" },
364
+ headers: { "content-type": "application/json", ...keyed },
355
365
  body: JSON.stringify(body),
356
366
  });
357
367
  const text = await response.text();
@@ -703,7 +713,11 @@ export function createInference({
703
713
  input: ["warm up"],
704
714
  });
705
715
  ready.marker = (
706
- await (await fetch(`${ready.base}/props?model=media`)).json()
716
+ await (
717
+ await fetch(`${ready.base}/props?model=media`, {
718
+ headers: keyed,
719
+ })
720
+ ).json()
707
721
  ).media_marker;
708
722
  }
709
723
  const out = [];
@@ -752,7 +766,11 @@ export function createInference({
752
766
  if (server && server.child.exitCode === null) {
753
767
  try {
754
768
  llama = (
755
- (await (await fetch(`${server.base}/models`)).json()).data ?? []
769
+ (
770
+ await (
771
+ await fetch(`${server.base}/models`, { headers: keyed })
772
+ ).json()
773
+ ).data ?? []
756
774
  ).map((model) => ({
757
775
  id: model.id,
758
776
  state: model.status?.value ?? null,