tryharness 0.7.68 → 0.7.70

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.
@@ -4,7 +4,7 @@ import os from "node:os";
4
4
  import { spawnSync } from "node:child_process";
5
5
  import { PS } from "./nudge.mjs";
6
6
  import { paths } from "./paths.mjs";
7
- import { MACHINE_RUNNERS } from "./members.mjs";
7
+ import { MACHINE_RUNNERS, MEMBERS, nameOf } from "./members.mjs";
8
8
  import { runCapability, runningPids } from "./runner.mjs";
9
9
  import {
10
10
  descendsFrom,
@@ -13,6 +13,11 @@ import {
13
13
  seesPeers,
14
14
  } from "./peerProcess.mjs";
15
15
 
16
+ /**
17
+ * Set by the daemon on a board call Harness's server made for a hosted agent (daemon.mjs, the bridge's board message):
18
+ * no JSON a local caller or another computer writes can set it, unlike the hostedCaller field, which only ever narrows.
19
+ */
20
+ export const HOSTED_CALLER = Symbol("a board call Harness's server made");
16
21
  /** The run Harness started that made a socket call, set by checkCaller; no JSON a caller writes can set it. */
17
22
  export const RUN_CALLER = Symbol("the run that called");
18
23
  /** How the run that made a call was held in, so what it asks for is held in too: "review", "restricted" or null. */
@@ -60,6 +65,15 @@ const PERSON_READS = new Set([
60
65
  "board.swarm.list",
61
66
  "board.swarm.read",
62
67
  ]);
68
+ /**
69
+ * The person's own agents that run elsewhere and can be handed work (Grok Bot, Muse): the only agents whose asks a coding
70
+ * agent's standing permission may take, and only when the person named them on it (board.standing.set proposers).
71
+ * Harness's server, another person's agents and any other name never qualify.
72
+ */
73
+ export const PROPOSERS = MEMBERS.filter(
74
+ (entry) => entry.road === "hosted" && entry.runs === "self" && entry.target
75
+ ).map((entry) => entry.id);
76
+
63
77
  const runMayAsk = (command) =>
64
78
  command.startsWith("board.") ||
65
79
  command.startsWith("mcp.") ||
@@ -120,7 +134,7 @@ export function createRunAuthority({ store, installedAgents }) {
120
134
  );
121
135
  const standingRules = () => store.standing(runners(), startOfToday());
122
136
  // null when the run may start now; otherwise why it waits for the person.
123
- const standingVerdict = ({ trust, target, workspace }) => {
137
+ const standingVerdict = ({ trust, target, workspace, capacity = true }) => {
124
138
  if (trust !== "user")
125
139
  return "Another agent wrote it, so it never starts without the person.";
126
140
  const rule = standingRules().find((entry) => entry.agent === target);
@@ -132,7 +146,7 @@ export function createRunAuthority({ store, installedAgents }) {
132
146
  return rule.mode === "default"
133
147
  ? `${target} has not worked in ${workspace} before, so its first run there waits for the person.`
134
148
  : `${workspace} is outside the folders ${target} may run in without asking (${rule.workspaces.join(", ")}).`;
135
- if (rule.runs_today >= rule.daily_cap)
149
+ if (capacity && rule.runs_today >= rule.daily_cap)
136
150
  return `${target} already started ${rule.runs_today} runs today without asking, its daily limit.`;
137
151
  return null;
138
152
  };
@@ -150,6 +164,144 @@ export function createRunAuthority({ store, installedAgents }) {
150
164
  : { started: false, reason: `It could not be authorized. ${start}` };
151
165
  };
152
166
 
167
+ // Why a proposed turn did not start, by what the store found (authorizeProposed): the end of "<agent> waits for your
168
+ // Start: ...".
169
+ const proposedWhy = (why, { target, proposer, folder }) =>
170
+ ({
171
+ off: "its standing permission is off.",
172
+ "not-proposer": `it starts on ${nameOf(proposer)}'s asks only if you let it: npx tryharness board standing ${target} --from ${proposer}`,
173
+ outside: `${folder} is outside the folders it may run in without asking.`,
174
+ cap: "it already started its runs for today without asking, its daily limit.",
175
+ gone: "its turn could not be authorized.",
176
+ })[why];
177
+
178
+ // Why an ask from one of the person's own agents elsewhere cannot use a standing permission at all, or null.
179
+ // `hosted`: it came from Harness's server itself (HOSTED_CALLER), never a local process or another computer.
180
+ const askRefusal = (ask) => {
181
+ const proposer = String(ask?.proposer ?? "");
182
+ return !PROPOSERS.includes(proposer)
183
+ ? `${nameOf(proposer)} is not one of your own agents that may start it.`
184
+ : ask.hosted !== true || ask.window
185
+ ? `${nameOf(proposer)}'s ask did not come through Harness as its asks do.`
186
+ : ask.limited
187
+ ? `${nameOf(proposer)} asked from a held-in run.`
188
+ : ask.guard?.verdict !== "clean" || ask.guard.unavailable
189
+ ? `the guard on this computer did not read ${nameOf(proposer)}'s words as clean.`
190
+ : null;
191
+ };
192
+ // Authorized on the rule in one transaction (store.mjs authorizeProposed), its receipt naming the folder checked
193
+ // and the computer it is for, which proposedHold checks again before it starts.
194
+ const authorizeOnRule = (task, proposer, folder, scratch = null) => {
195
+ const why = store.authorizeProposed(task.id, {
196
+ agent: task.target_agent,
197
+ proposer,
198
+ since: startOfToday(),
199
+ covered: (rule) => !folder || covers(rule, folder),
200
+ folder,
201
+ scratch,
202
+ });
203
+ return why
204
+ ? {
205
+ started: false,
206
+ reason: proposedWhy(why, {
207
+ target: task.target_agent,
208
+ proposer,
209
+ folder,
210
+ }),
211
+ }
212
+ : { started: true };
213
+ };
214
+
215
+ /**
216
+ * One of the person's own agents elsewhere (PROPOSERS) asked a coding agent here for a turn in a thread whose own work
217
+ * does not run on the person's go (threadAuthority.mjs threadMayRun: another agent wrote it). The turn starts only
218
+ * on the coding agent's standing permission, where the person named that asker on it: its words reached here through
219
+ * Harness's server and the guard on this computer read them clean, from no restricted run, review or linked window.
220
+ * It counts against the agent's daily limit like any standing run and keeps the agent-written label; the thread's
221
+ * own folder is the one checked (null: a fresh scratch folder of the thread's, which holds nothing of the person's).
222
+ *
223
+ * `ask`: { proposer, hosted, guard, limited, window }, as the thread heard it. Returns { started } or { started:
224
+ * false, reason }, the reason ending "<agent> waits for your Start: ".
225
+ */
226
+ const admitProposed = (wake, ask, folder) => {
227
+ const reason = askRefusal(ask);
228
+ return reason
229
+ ? { started: false, reason }
230
+ : authorizeOnRule(wake, String(ask.proposer), folder);
231
+ };
232
+ /**
233
+ * The task itself one of the person's own agents elsewhere posted for a coding agent (post_task), started on the same
234
+ * standing permission and the same checks as admitProposed: the poster is the asker, so no second ask is needed. A
235
+ * task naming no folder runs in its thread's scratch folder: `scratch`, made here when it runs here, or null for
236
+ * another computer, which makes its own (machineRuns.mjs).
237
+ */
238
+ const admitPosted = (task, ask, { scratch = null } = {}) => {
239
+ const reason =
240
+ askRefusal(ask) ??
241
+ (task.origin_agent !== ask.proposer ||
242
+ task.trust !== "agent" ||
243
+ task.kind !== "task" ||
244
+ !task.parent_id ||
245
+ !RUNNERS.has(task.target_agent)
246
+ ? "it is not a task that agent posted for a coding agent."
247
+ : null);
248
+ return reason
249
+ ? { started: false, reason }
250
+ : authorizeOnRule(
251
+ task,
252
+ String(ask.proposer),
253
+ task.run_workspace ?? null,
254
+ task.run_workspace ? null : scratch
255
+ );
256
+ };
257
+
258
+ // Why a turn admitProposed or admitPosted started may no longer start, or null: the person set the agent to wait,
259
+ // took the asker off the rule, no longer covers the folder it was admitted in, lowered the day's limit below the
260
+ // runs already started, or the task now names another agent, folder or computer than the receipt it was admitted
261
+ // with; or its thread is no longer open. Its run is already
262
+ // counted for the day, so the limit is checked again only when the rule changed after it was admitted.
263
+ const proposedHold = (task) => {
264
+ if (task.run_authorized_by !== "standing") return null;
265
+ const proposal = store.standingProposal(task.id);
266
+ if (!proposal) return null;
267
+ const [rule] = store.standing([task.target_agent], startOfToday());
268
+ const thread = task.parent_id ? store.getTask(task.parent_id) : null;
269
+ const changed = rule?.set_at !== proposal.rule_set_at;
270
+ return rule?.mode === "off"
271
+ ? `${nameOf(task.target_agent)} is set to wait for your Start.`
272
+ : !rule?.proposers.includes(proposal.proposer)
273
+ ? `you took ${nameOf(proposal.proposer)}'s asks off ${nameOf(task.target_agent)}'s standing permission.`
274
+ : proposal.folder && !covers(rule, proposal.folder)
275
+ ? `${proposal.folder} is no longer among the folders it may run in without asking.`
276
+ : changed && rule.runs_today > rule.daily_cap
277
+ ? "you lowered its daily limit below the runs it already started today."
278
+ : (Object.hasOwn(proposal, "machine") &&
279
+ (task.target_machine ?? null) !== proposal.machine) ||
280
+ (Object.hasOwn(proposal, "agent") &&
281
+ task.target_agent !== proposal.agent) ||
282
+ (Object.hasOwn(proposal, "workspace") &&
283
+ (task.run_workspace ?? null) !== proposal.workspace)
284
+ ? "it changed after it was admitted: another agent, folder or computer."
285
+ : thread?.status !== "open"
286
+ ? `the thread is ${thread?.status ?? "gone"}.`
287
+ : null;
288
+ };
289
+ /**
290
+ * Checked just before a turn admitProposed started is handed to an agent, here or to another computer: one that may
291
+ * no longer start waits for the person's Start again, and its thread hears why. Why it was held, or null.
292
+ */
293
+ const recheckProposed = (task) => {
294
+ const why = proposedHold(task);
295
+ if (why && store.withdrawProposed(task.id) && task.parent_id)
296
+ store.roomPost({
297
+ goalId: task.parent_id,
298
+ author: "harness",
299
+ kind: "note",
300
+ text: `${nameOf(task.target_agent)} waits for your Start: ${why}`,
301
+ });
302
+ return why;
303
+ };
304
+
153
305
  /**
154
306
  * Who a call on the local socket is, before anything acts on it; a refusal to answer with, or null. A call carrying a
155
307
  * run's capability is that run's agent with that run's limits, whatever agent it names. Without one, a call as the
@@ -168,6 +320,18 @@ export function createRunAuthority({ store, installedAgents }) {
168
320
  request[RUN_CALLER] = run;
169
321
  if (!runMayAsk(command))
170
322
  return "A run Harness started cannot do that; only the person can.";
323
+ if (
324
+ run.dispatchScope &&
325
+ !new Set([
326
+ "board.dispatch.workspace",
327
+ "board.dispatch.propose",
328
+ "board.dispatch.view",
329
+ "board.dispatch.apply",
330
+ "board.dispatch.scope.list",
331
+ "board.dispatch.scope.read",
332
+ ]).has(command)
333
+ )
334
+ return "This run has only its scoped workspace and dispatch tools.";
171
335
  if (run.restricted && command.startsWith("mcp."))
172
336
  return "A restricted run reaches this computer's board, not the person's Harness.";
173
337
  return null;
@@ -204,6 +368,10 @@ export function createRunAuthority({ store, installedAgents }) {
204
368
  standingRules,
205
369
  standingVerdict,
206
370
  applyStanding,
371
+ admitProposed,
372
+ admitPosted,
373
+ proposedHold,
374
+ recheckProposed,
207
375
  checkCaller,
208
376
  markPrompt,
209
377
  };
package/lib/runBrief.mjs CHANGED
@@ -182,7 +182,7 @@ export function createRunBriefs({
182
182
  name: "provenance",
183
183
  priority: "core",
184
184
  source: "who wrote the task",
185
- text: provenanceLine(task),
185
+ text: provenanceLine(task, person?.displayName),
186
186
  },
187
187
  {
188
188
  name: "objective",
package/lib/runWorker.mjs CHANGED
@@ -17,6 +17,7 @@ export const restrictedRun = (store, task) =>
17
17
  export function createRunWorker({
18
18
  RESCAN_MS,
19
19
  authority,
20
+ dispatches = null,
20
21
  briefs,
21
22
  contextEngine,
22
23
  installedAgents,
@@ -85,6 +86,8 @@ export function createRunWorker({
85
86
  }
86
87
  if (!RUNNERS.has(run.target_agent) || !run.run_workspace) return null;
87
88
  if (!run.run_authorized_at) return `${who} waits for your Start.`;
89
+ const refusal = dispatches?.refusal(run.id);
90
+ if (refusal) return refusal;
88
91
  if (runsHeld)
89
92
  return `${who} starts when your Mac is no longer running with its lid closed.`;
90
93
  const holder = activeRuns.get(slotOf({ ...run, parent_id: goalId }));
@@ -115,6 +118,11 @@ export function createRunWorker({
115
118
  only: onlyItsOwn(),
116
119
  })) {
117
120
  const agent = task.target_agent;
121
+ if (dispatches?.refusal(task.id)) continue;
122
+ if (authority.recheckProposed?.(task)) {
123
+ notifyBoard(store.getTask(task.id));
124
+ continue;
125
+ }
118
126
  // Where a task runs is settled once, before its slot: a run T3 carries out holds its thread, not a folder.
119
127
  if (task.kind === "task" && store.meta(`t3-run:${task.id}`) === null)
120
128
  store.meta(`t3-run:${task.id}`, taskInT3(task, agent) ? "1" : "0");
@@ -148,6 +156,15 @@ export function createRunWorker({
148
156
  void startBoardRun(task, agent);
149
157
  }
150
158
  };
159
+ // A turn started on another of the person's agents' asks, checked again once its preparation (which may wait on the
160
+ // model) is done: one that may no longer start goes back to wait for the person's Start (runAuthority.mjs).
161
+ const heldOnStart = (task) => {
162
+ const why = authority.proposedHold?.(store.getTask(task.id));
163
+ if (!why) return null;
164
+ store.requeueRun(task.id);
165
+ authority.recheckProposed(store.getTask(task.id));
166
+ return why;
167
+ };
151
168
  const startBoardRun = async (task, agent) => {
152
169
  const wake = task.kind === "wake";
153
170
  const room = !wake && task.parent_id ? store.getTask(task.parent_id) : null;
@@ -156,7 +173,7 @@ export function createRunWorker({
156
173
  resumeSession,
157
174
  again,
158
175
  t3: inT3,
159
- } = await briefs.prepare(task, agent);
176
+ } = dispatches?.briefFor(task) ?? (await briefs.prepare(task, agent));
160
177
  const startedAt = new Date().toISOString();
161
178
  const stoppedFor = Boolean(again);
162
179
  // The session it resumes is the run's from its first moment, so Open never resumes it beside the run before the
@@ -164,17 +181,33 @@ export function createRunWorker({
164
181
  if (resumeSession && !task.session_id)
165
182
  store.setRunSession(task.id, resumeSession);
166
183
  const readOnly = store.meta(`review:${task.id}`) === "1";
167
- const restricted = restrictedRun(store, task);
184
+ const dispatchOptions = dispatches?.optionsFor(task) ?? {};
185
+ const restricted =
186
+ restrictedRun(store, task) || Boolean(dispatchOptions.dispatchScope);
168
187
  // Work another person asked for runs with only this person's own agent settings: the repository's own (its
169
188
  // .claude hooks, .pi, opencode.json) can be that person's, and would run here (security audit P4, October 25).
170
189
  const ownSettingsOnly = store.meta(`other-person-run:${task.id}`) === "1";
171
190
  const chosen = await modelForRun(task, agent);
172
191
  store.setRunModel(task.id, chosen);
173
192
  const { model, effort } = chosen;
193
+ // Preparation may await context/model selection. Recheck a queued dispatch
194
+ // immediately before any executor or secret-use grant receives the task.
195
+ const refusal = dispatches?.begin(task.id) ?? heldOnStart(task);
196
+ if (refusal) {
197
+ activeRuns.delete(slotOf(task));
198
+ store.requeueRun(task.id);
199
+ log(`board task ${task.id} held before execution: ${refusal}`);
200
+ notifyBoard(store.getTask(task.id));
201
+ return;
202
+ }
174
203
  const t3 = inT3 ? t3Turn(task, agent, { model, effort }) : null;
175
204
  // The person's environment grants (vault.mjs), never for a run they did not authorize: the values are in this
176
205
  // run's environment, and what it says is scrubbed of them.
177
- const secrets = restricted ? null : vaultEnv(task, agent);
206
+ // Nor for a turn another of the person's agents asked for on standing permission: that grants a start, no secrets.
207
+ const secrets =
208
+ restricted || store.standingProposal(task.id)
209
+ ? null
210
+ : vaultEnv(task, agent);
178
211
  const granted = secrets && Object.keys(secrets.env).length ? secrets : null;
179
212
  startTurn(task);
180
213
  taskRuns.startTask(
@@ -191,7 +224,11 @@ export function createRunWorker({
191
224
  ...(stoppedFor ? { again: true } : {}),
192
225
  ...(resumeSession && !t3 ? { resumeSession } : {}),
193
226
  ...(readOnly ? { readOnly } : {}),
194
- ...(restricted ? { restricted } : {}),
227
+ ...(dispatchOptions.dispatchScope
228
+ ? dispatchOptions
229
+ : restricted
230
+ ? { restricted }
231
+ : {}),
195
232
  ...(ownSettingsOnly ? { ownSettingsOnly } : {}),
196
233
  ...(t3 ? { t3 } : {}),
197
234
  ...(granted ? { env: granted.env, scrub: granted.values } : {}),
@@ -244,11 +281,11 @@ export function createRunWorker({
244
281
  setImmediate(runBoard);
245
282
  if (extra?.usage) store.setRunUsage(task.id, extra.usage);
246
283
  // Its changes are committed before it reports, so the audit of its report sees what was not.
247
- commitRun(task);
284
+ if (!dispatchOptions.dispatchScope) commitRun(task);
248
285
  // A run that failed without reaching its agent's own report ended early: the runner says why.
249
286
  const reported = store.reportTask(task.id, agent, {
250
287
  status: kind,
251
- result: text || "",
288
+ result: dispatches?.resultFor(task, text || "") ?? (text || ""),
252
289
  // Stopped for the person's steer (threadRuns.mjs): the person ended it, not the agent or its provider.
253
290
  failure: store.meta(`steered:${task.id}`)
254
291
  ? "person"
@@ -395,6 +432,7 @@ export function createRunWorker({
395
432
  runBoard,
396
433
  interruptAll,
397
434
  hold,
435
+ held: () => runsHeld,
398
436
  activeRuns: {
399
437
  get: (slot) => activeRuns.get(slot),
400
438
  values: () => activeRuns.values(),
package/lib/runner.mjs CHANGED
@@ -22,8 +22,9 @@ const issueCapability = (task) => {
22
22
  capabilities.set(token, {
23
23
  id: task.runId,
24
24
  agent: task.agent,
25
- restricted: task.restricted === true,
25
+ restricted: task.restricted === true || Boolean(task.dispatchScope),
26
26
  review: task.readOnly === true,
27
+ dispatchScope: task.dispatchScope ?? null,
27
28
  });
28
29
  return token;
29
30
  };
@@ -316,12 +317,15 @@ const readOnlyClaude = (task) => [
316
317
  // Codex's review: its read-only permission profile, with the same places denied to every command it runs (the run's
317
318
  // own folder stays readable when it lies inside one) and no web search. Probed with `codex sandbox` 0.164: a denied
318
319
  // file is "Operation not permitted", the folder reads, writes and the daemon's socket are refused.
319
- const codexReview = (task, home = os.homedir()) => {
320
+ const codexReview = (task, executable, home = os.homedir()) => {
320
321
  const places = Object.fromEntries(
321
322
  secretPlaces(home).map((place) => [place, "deny"])
322
323
  );
323
324
  places[path.join(home, ".codex", "shell_snapshots")] = "read";
324
325
  if (task.cwd) places[task.cwd] = "read";
326
+ // Standalone Codex lives inside its denied credential home and reexecutes
327
+ // itself to load AGENTS.md. Only its confirmed, resolved file needs access.
328
+ if (executable) places[executable] = "read";
325
329
  const table = Object.entries(places)
326
330
  .map(
327
331
  ([place, access]) => `${JSON.stringify(place)}=${JSON.stringify(access)}`
@@ -342,7 +346,54 @@ const codexReview = (task, home = os.homedir()) => {
342
346
  /** The coding agents whose runs Harness can hold to read-only: only they review or audit. */
343
347
  export const READ_ONLY_AGENTS = new Set(["claude-code", "codex"]);
344
348
 
345
- export function agentInvocation(task) {
349
+ export function agentInvocation(task, { executable = null } = {}) {
350
+ if (task.dispatchScope) {
351
+ if (task.agent !== "claude-code")
352
+ throw new Error(
353
+ "No mediated workspace executor is registered for this agent"
354
+ );
355
+ const mcp = {
356
+ mcpServers: {
357
+ harness: {
358
+ command: process.execPath,
359
+ args: [
360
+ fileURLToPath(new URL("./mcp-stdio.mjs", import.meta.url)),
361
+ "--agent",
362
+ "claude-code",
363
+ "--restricted",
364
+ "--scope",
365
+ task.dispatchScope,
366
+ ],
367
+ },
368
+ },
369
+ };
370
+ return {
371
+ command: "claude",
372
+ args: [
373
+ "-p",
374
+ task.brief,
375
+ "--output-format",
376
+ "stream-json",
377
+ "--verbose",
378
+ ...modelFlags(task),
379
+ "--tools",
380
+ "",
381
+ "--bare",
382
+ "--restricted",
383
+ "--allowedTools",
384
+ "mcp__harness__dispatch_work mcp__harness__scope_workspace",
385
+ "--permission-mode",
386
+ "dontAsk",
387
+ "--strict-mcp-config",
388
+ "--mcp-config",
389
+ JSON.stringify(mcp),
390
+ "--setting-sources",
391
+ "",
392
+ "--settings",
393
+ JSON.stringify({ disableAllHooks: true }),
394
+ ],
395
+ };
396
+ }
346
397
  if (task.agent === "claude-code") {
347
398
  return {
348
399
  command: "claude",
@@ -381,7 +432,7 @@ export function agentInvocation(task) {
381
432
  );
382
433
  if (task.agent === "codex") {
383
434
  return {
384
- command: "codex",
435
+ command: executable ?? "codex",
385
436
  args: [
386
437
  "exec",
387
438
  ...(task.resumeSession ? ["resume", task.resumeSession] : []),
@@ -392,7 +443,7 @@ export function agentInvocation(task) {
392
443
  // read-only.
393
444
  ...(task.readOnly
394
445
  ? [
395
- ...codexReview(task),
446
+ ...codexReview(task, executable),
396
447
  "-c",
397
448
  'approval_policy="never"',
398
449
  "-c",
@@ -733,7 +784,12 @@ export function runAgentTask(task, emit, observe = () => {}, hooks = {}) {
733
784
  const program = agentBinary(task.agent, invocation.command);
734
785
  if (!program)
735
786
  throw new Error(`${task.agent} is not installed on this computer.`);
736
- invocation = { ...invocation, command: program };
787
+ // Seatbelt checks symlink traversal too: invoke the same exact file granted
788
+ // to the review helper, without exposing its installation directory.
789
+ invocation =
790
+ task.agent === "codex" && task.readOnly
791
+ ? agentInvocation(task, { executable: fs.realpathSync(program) })
792
+ : { ...invocation, command: program };
737
793
  } catch (error) {
738
794
  if (capability) capabilities.delete(capability);
739
795
  emit("failed", error.message, { failure: "start" });