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.
package/lib/store.mjs CHANGED
@@ -32,6 +32,11 @@ export const DEFAULT_DAILY_RUNS = 10;
32
32
  export const FAILURE_CAUSES = ["person", "start", "exit", "agent"];
33
33
  // An agent's record leaves out failures that say nothing about it: the person's stop and a run that never started.
34
34
  const AGENTS_OWN = "not (status = 'failed' and failure in ('person', 'start'))";
35
+ // What an asker hears of under finished (finishedFor), and the receipt naming one such result: the full task id, the
36
+ // status it reached and when it was reported, so a task asked again and reported anew is a new result to tell.
37
+ const FINISHED_STATUSES = "('done', 'failed', 'needs_input')";
38
+ const RESULT_RECEIPT =
39
+ "t.id || ':' || t.status || ':' || coalesce(t.reported_at, t.updated_at)";
35
40
  // What the swarm's memory can show (swarmMemory.mjs): what was said, made, checked or asked in the person's threads and
36
41
  // tasks. A contribution's thread is its own task when that is a thread, else its task's parent thread.
37
42
  export const MEMORY_KINDS = [
@@ -176,7 +181,8 @@ export function openStore(file) {
176
181
  mode text not null,
177
182
  workspaces text not null,
178
183
  daily_cap integer not null,
179
- set_at text not null
184
+ set_at text not null,
185
+ proposers text not null default '[]'
180
186
  );
181
187
  create table if not exists board_goals (
182
188
  goal_id text primary key,
@@ -193,6 +199,12 @@ export function openStore(file) {
193
199
  key text primary key,
194
200
  value text not null
195
201
  );
202
+ create table if not exists board_dispatch_advisories (
203
+ seq integer primary key autoincrement,
204
+ task_id text not null,
205
+ value text not null
206
+ );
207
+ create index if not exists dispatch_advisories_task on board_dispatch_advisories(task_id,seq);
196
208
  create table if not exists board_agents (
197
209
  agent text primary key,
198
210
  last_seen_at text not null
@@ -442,6 +454,14 @@ export function openStore(file) {
442
454
  db.exec(
443
455
  "alter table board_standing add column mode text not null default 'named'"
444
456
  );
457
+ if (
458
+ !db
459
+ .all("pragma table_info(board_standing)")
460
+ .some((column) => column.name === "proposers")
461
+ )
462
+ db.exec(
463
+ "alter table board_standing add column proposers text not null default '[]'"
464
+ );
445
465
  if (
446
466
  !db
447
467
  .all("pragma table_info(board_contributions)")
@@ -569,6 +589,15 @@ export function openStore(file) {
569
589
  // What the auditor's code found comparing a report's claims with its receipts (audit.mjs).
570
590
  if (!boardColumns.has("audit") && boardColumns.size)
571
591
  db.exec("alter table board_tasks add column audit text");
592
+ // Only the asker's explicit receipt establishes delivery. A pre-receipt board has no such evidence; its results
593
+ // remain available after upgrade rather than assuming an unrelated last-seen timestamp meant they were read.
594
+ db.exec(`create table if not exists board_result_acks (
595
+ agent text not null,
596
+ receipt text not null,
597
+ task_id text not null,
598
+ acked_at text not null,
599
+ primary key (agent, receipt)
600
+ )`);
572
601
 
573
602
  for (const [table, columns] of [
574
603
  ["board_tasks", ["returned"]],
@@ -665,6 +694,12 @@ export function openStore(file) {
665
694
  };
666
695
 
667
696
  const roomPostListeners = [];
697
+ const publishRoomPost = (goalId, post) => {
698
+ for (const listener of roomPostListeners)
699
+ try {
700
+ listener(goalId, post);
701
+ } catch {}
702
+ };
668
703
 
669
704
  // When each agent last called the board, by computer. The buddy, every agent and the other computers call several
670
705
  // times a second, and each write waits for the disk (4 s in every 50 on the owner's daemon, October 28), so the times
@@ -1146,12 +1181,42 @@ export function openStore(file) {
1146
1181
  new Date().toISOString()
1147
1182
  );
1148
1183
  },
1184
+ // Back to the default; the agents the person let start it (setStandingProposers) stay theirs to take back.
1149
1185
  resetStanding(agent) {
1186
+ const row = execute(
1187
+ "get",
1188
+ "select proposers from board_standing where agent = ?",
1189
+ agent
1190
+ );
1191
+ if (!row) return false;
1192
+ if (JSON.parse(row.proposers || "[]").length)
1193
+ return (
1194
+ execute(
1195
+ "run",
1196
+ "update board_standing set mode = 'default', workspaces = '[]', daily_cap = ?, set_at = ? where agent = ?",
1197
+ DEFAULT_DAILY_RUNS,
1198
+ new Date().toISOString(),
1199
+ agent
1200
+ ).changes === 1
1201
+ );
1150
1202
  return (
1151
1203
  execute("run", "delete from board_standing where agent = ?", agent)
1152
1204
  .changes === 1
1153
1205
  );
1154
1206
  },
1207
+ // The person's own agents elsewhere whose clean asks in a thread start this agent's turn on its standing permission
1208
+ // (runAuthority.mjs admitProposed). Its mode, folders and daily limit stay as they are.
1209
+ setStandingProposers(agent, proposers) {
1210
+ execute(
1211
+ "run",
1212
+ `insert into board_standing(agent, mode, workspaces, daily_cap, set_at, proposers) values(?, 'default', '[]', ?, ?, ?)
1213
+ on conflict(agent) do update set proposers = excluded.proposers, set_at = excluded.set_at`,
1214
+ agent,
1215
+ DEFAULT_DAILY_RUNS,
1216
+ new Date().toISOString(),
1217
+ JSON.stringify(proposers)
1218
+ );
1219
+ },
1155
1220
  // One rule per agent installed here, with the runs it started since `since`.
1156
1221
  standing(agents, since) {
1157
1222
  const rows = new Map(
@@ -1177,14 +1242,17 @@ export function openStore(file) {
1177
1242
  workspaces,
1178
1243
  daily_cap: row ? Number(row.daily_cap) : DEFAULT_DAILY_RUNS,
1179
1244
  set_at: row?.set_at || null,
1180
- runs_today: Number(
1181
- execute(
1182
- "get",
1183
- "select count(*) as n from board_tasks where target_agent = ? and run_authorized_by = 'standing' and run_authorized_at >= ?",
1184
- agent,
1185
- since
1186
- ).n
1187
- ),
1245
+ proposers: JSON.parse(row?.proposers || "[]"),
1246
+ runs_today:
1247
+ Number(this.meta(`prompt-quota:${since}:${agent}`) || "0") +
1248
+ Number(
1249
+ execute(
1250
+ "get",
1251
+ "select count(*) as n from board_tasks where target_agent = ? and run_authorized_by = 'standing' and run_authorized_at >= ?",
1252
+ agent,
1253
+ since
1254
+ ).n
1255
+ ),
1188
1256
  };
1189
1257
  });
1190
1258
  },
@@ -1201,6 +1269,141 @@ export function openStore(file) {
1201
1269
  String(value)
1202
1270
  );
1203
1271
  },
1272
+ // Proposal identity, task and grant must appear together, even after a crash.
1273
+ dispatchTransaction(operation) {
1274
+ db.exec("begin immediate");
1275
+ try {
1276
+ const result = operation();
1277
+ db.exec("commit");
1278
+ return result;
1279
+ } catch (error) {
1280
+ if (db.inTransaction) db.exec("rollback");
1281
+ throw error;
1282
+ }
1283
+ },
1284
+ appendDispatchAdvisory(id, value) {
1285
+ if (this.meta(`dispatch-advisories:${id}`)) this.dispatchAdvisories(id);
1286
+ execute(
1287
+ "run",
1288
+ "insert into board_dispatch_advisories(task_id,value) values(?,?)",
1289
+ id,
1290
+ JSON.stringify(value)
1291
+ );
1292
+ },
1293
+ dispatchAdvisories(id, before = null) {
1294
+ // Migrate the earlier unbounded meta array without losing its audit records.
1295
+ const key = `dispatch-advisories:${id}`;
1296
+ if (this.meta(key))
1297
+ this.dispatchTransaction(() => {
1298
+ for (const record of JSON.parse(this.meta(key) || "[]"))
1299
+ execute(
1300
+ "run",
1301
+ "insert into board_dispatch_advisories(task_id,value) values(?,?)",
1302
+ id,
1303
+ JSON.stringify(record)
1304
+ );
1305
+ execute("run", "delete from board_meta where key = ?", key);
1306
+ });
1307
+ const rows = execute(
1308
+ "all",
1309
+ "select seq,value from board_dispatch_advisories where task_id = ? and (? is null or seq < ?) order by seq desc limit 32",
1310
+ id,
1311
+ before,
1312
+ before
1313
+ );
1314
+ return {
1315
+ records: rows.map((row) => JSON.parse(row.value)),
1316
+ next_before: rows.length === 32 ? rows.at(-1).seq : null,
1317
+ };
1318
+ },
1319
+ // The task row is the durable execution outbox. Reserve its sole approved
1320
+ // start and authorize that same row atomically; a retry cannot create a task.
1321
+ commitTaskDispatch({
1322
+ id,
1323
+ expectedGrant,
1324
+ expectedVersion,
1325
+ nextGrant,
1326
+ targetAgent = null,
1327
+ scopeRef = null,
1328
+ expectedScope = null,
1329
+ nextScope = null,
1330
+ quota = null,
1331
+ }) {
1332
+ db.exec("begin immediate");
1333
+ try {
1334
+ const task = this.getTask(id);
1335
+ const key = `dispatch-grant:${id}`;
1336
+ const rule = quota
1337
+ ? this.standing([quota.agent], quota.period)[0]
1338
+ : null;
1339
+ if (
1340
+ this.meta(key) !== expectedGrant ||
1341
+ task?.version !== expectedVersion ||
1342
+ task.status !== "open" ||
1343
+ task.run_authorized_at ||
1344
+ (scopeRef &&
1345
+ this.meta(`dispatch-scope:${scopeRef}`) !== expectedScope) ||
1346
+ (quota &&
1347
+ (quota.remaining < 1 ||
1348
+ this.meta(quota.key) !== quota.expected ||
1349
+ rule.daily_cap !== quota.limit ||
1350
+ rule.set_at !== quota.rule_epoch ||
1351
+ rule.mode !== quota.rule_mode ||
1352
+ rule.runs_today >= rule.daily_cap))
1353
+ ) {
1354
+ db.exec("commit");
1355
+ return false;
1356
+ }
1357
+ if (targetAgent)
1358
+ execute(
1359
+ "run",
1360
+ "update board_tasks set target_agent = ?, holder = ? where id = ?",
1361
+ targetAgent,
1362
+ targetAgent,
1363
+ id
1364
+ );
1365
+ const authorized = this.authorizeRun(id, "dispatch");
1366
+ if (!authorized.ok) {
1367
+ db.exec("rollback");
1368
+ return false;
1369
+ }
1370
+ this.meta(key, nextGrant);
1371
+ if (scopeRef) this.meta(`dispatch-scope:${scopeRef}`, nextScope);
1372
+ if (quota) this.meta(quota.key, quota.next);
1373
+ db.exec("commit");
1374
+ return true;
1375
+ } catch (error) {
1376
+ if (db.inTransaction) db.exec("rollback");
1377
+ throw error;
1378
+ }
1379
+ },
1380
+ replaceDispatchGrant({ id, expectedGrant, nextGrant }) {
1381
+ return (
1382
+ execute(
1383
+ "run",
1384
+ "update board_meta set value = ? where key = ? and value = ?",
1385
+ nextGrant,
1386
+ `dispatch-grant:${id}`,
1387
+ expectedGrant
1388
+ ).changes === 1
1389
+ );
1390
+ },
1391
+ failUnstartedDispatch(id, reason) {
1392
+ const now = new Date().toISOString();
1393
+ return (
1394
+ execute(
1395
+ "run",
1396
+ `update board_tasks set status = 'failed', failure = 'start', result = ?,
1397
+ holder = null, reported_at = ?, updated_at = ?, version = version + 1
1398
+ where id = ? and origin_agent = 'user' and status = 'open'
1399
+ and run_authorized_at is null and claimed_by is null`,
1400
+ reason,
1401
+ now,
1402
+ now,
1403
+ id
1404
+ ).changes === 1
1405
+ );
1406
+ },
1204
1407
  // The person's choices about what each agent is good at (specialties.mjs owns the rules; these keep the rows).
1205
1408
  specialtyChoices(agent) {
1206
1409
  return execute(
@@ -1531,11 +1734,7 @@ export function openStore(file) {
1531
1734
  );
1532
1735
  const made = post ? { ...post, mentions } : null;
1533
1736
  // Whoever follows a room's posts (a shared project carries them to the other side) hears each one as it is made.
1534
- if (made)
1535
- for (const listener of roomPostListeners)
1536
- try {
1537
- listener(goalId, made);
1538
- } catch {}
1737
+ if (made) publishRoomPost(goalId, made);
1539
1738
  return made;
1540
1739
  },
1541
1740
  onRoomPost(listener) {
@@ -2438,6 +2637,73 @@ export function openStore(file) {
2438
2637
  )
2439
2638
  );
2440
2639
  },
2640
+ // Retire only an unstarted agent ask, with the task and room receipts in
2641
+ // the same transaction. The route supplies relevance; the store preserves
2642
+ // ownership if another worker claimed or changed the task in the meantime.
2643
+ retireRouteAsk(goalId, id, expectedVersion, reason) {
2644
+ db.exec("begin immediate");
2645
+ try {
2646
+ const task = this.getTask(id);
2647
+ const untouched =
2648
+ task?.kind === "task" &&
2649
+ task.parent_id === goalId &&
2650
+ task.origin_agent !== "user" &&
2651
+ task.status === "open" &&
2652
+ task.version === expectedVersion &&
2653
+ task.context_refs.some((ref) => ref.kind === "ask") &&
2654
+ !task.claimed_by &&
2655
+ !task.claimed_at &&
2656
+ !task.claimed_machine &&
2657
+ !task.target_machine &&
2658
+ !task.session_id &&
2659
+ !task.run_workspace &&
2660
+ !task.run_authorized_at &&
2661
+ !task.usage &&
2662
+ !this.meta(`harness-window:${id}`) &&
2663
+ !this.meta(`harness-served:${id}`);
2664
+ if (!untouched) {
2665
+ db.exec("commit");
2666
+ return false;
2667
+ }
2668
+ const at = new Date().toISOString();
2669
+ const text = `Canceled unstarted route ask ${id} for ${task.target_agent}: ${reason}. No work was reported as completed.`;
2670
+ const retired = execute(
2671
+ "run",
2672
+ `update board_tasks set status = 'failed', failure = 'start', result = ?, holder = null,
2673
+ reported_at = ?, updated_at = ?, version = version + 1 where id = ? and status = 'open' and version = ?`,
2674
+ text,
2675
+ at,
2676
+ at,
2677
+ id,
2678
+ expectedVersion
2679
+ );
2680
+ if (retired.changes !== 1) {
2681
+ db.exec("commit");
2682
+ return false;
2683
+ }
2684
+ this.addContribution({
2685
+ taskId: id,
2686
+ kind: "receipt",
2687
+ author: "harness",
2688
+ text,
2689
+ source: "route",
2690
+ });
2691
+ const receipt = this.addContribution({
2692
+ taskId: goalId,
2693
+ kind: "receipt",
2694
+ author: "harness",
2695
+ text,
2696
+ source: "room",
2697
+ });
2698
+ db.exec("commit");
2699
+ // Other computers hear the receipt only after its cancellation is durable.
2700
+ publishRoomPost(goalId, { ...receipt, mentions: [] });
2701
+ return true;
2702
+ } catch (error) {
2703
+ if (db.inTransaction) db.exec("rollback");
2704
+ throw error;
2705
+ }
2706
+ },
2441
2707
  // Stop: every open child closes; the caller stops what is running.
2442
2708
  // `agent`: only that agent's runs, the rest of the goal's work going on (a shared project's piece released).
2443
2709
  closeGoalChildren(goalId, reason, agent = null) {
@@ -2719,6 +2985,87 @@ export function openStore(file) {
2719
2985
  );
2720
2986
  return written.changes === 1;
2721
2987
  },
2988
+ /**
2989
+ * A thread's turn that one of the person's own agents elsewhere asked for, or the task it posted itself for a coding
2990
+ * agent, started on its agent's standing permission: the rule read, the day's runs counted and the row authorized in
2991
+ * one transaction, so two asks at once never both take the last run of the day. It counts as one of the day's
2992
+ * standing runs, whoever asked. `covered`: whether the rule covers `folder`, the folder checked (runAuthority.mjs).
2993
+ * `scratch`: where a posted task naming no folder runs on this computer; another computer's makes its own. The
2994
+ * receipt keeps the rule's revision, the folder checked, and the agent, folder and computer it was admitted to run
2995
+ * with, for the check again before it starts (runAuthority.mjs proposedHold). Null when
2996
+ * it started; otherwise why not.
2997
+ */
2998
+ authorizeProposed(
2999
+ id,
3000
+ { agent, proposer, since, covered, folder = null, scratch = null }
3001
+ ) {
3002
+ db.exec("begin immediate");
3003
+ try {
3004
+ const rule = this.standing([agent], since)[0];
3005
+ const why =
3006
+ rule.mode === "off"
3007
+ ? "off"
3008
+ : !rule.proposers.includes(proposer)
3009
+ ? "not-proposer"
3010
+ : !covered(rule)
3011
+ ? "outside"
3012
+ : rule.runs_today >= rule.daily_cap
3013
+ ? "cap"
3014
+ : null;
3015
+ const now = new Date().toISOString();
3016
+ const written =
3017
+ !why &&
3018
+ execute(
3019
+ "run",
3020
+ `update board_tasks set run_authorized_by = 'standing', run_authorized_at = @now, updated_at = @now, version = version + 1,
3021
+ run_workspace = case when kind = 'task' then coalesce(run_workspace, @scratch) else run_workspace end
3022
+ where id = @id and status = 'open' and trust = 'agent' and target_agent = @agent and run_authorized_at is null
3023
+ and id not in (select task_id from board_threads)
3024
+ and ((kind = 'wake' and (run_workspace is not null or target_machine is not null))
3025
+ or (kind = 'task' and origin_agent = @proposer and parent_id is not null
3026
+ and coalesce(run_workspace, @scratch, target_machine) is not null))`,
3027
+ { id, agent, proposer, scratch, now }
3028
+ ).changes === 1;
3029
+ if (written)
3030
+ this.meta(
3031
+ `standing-proposal:${id}`,
3032
+ JSON.stringify({
3033
+ proposer,
3034
+ rule_set_at: rule.set_at,
3035
+ folder,
3036
+ agent,
3037
+ workspace: this.getTask(id).run_workspace ?? null,
3038
+ machine: this.getTask(id).target_machine ?? null,
3039
+ at: now,
3040
+ })
3041
+ );
3042
+ db.exec("commit");
3043
+ return why ?? (written ? null : "gone");
3044
+ } catch (error) {
3045
+ if (db.inTransaction) db.exec("rollback");
3046
+ throw error;
3047
+ }
3048
+ },
3049
+ /** Who asked for a turn started on standing permission (authorizeProposed), or null. */
3050
+ standingProposal(id) {
3051
+ try {
3052
+ return JSON.parse(this.meta(`standing-proposal:${id}`) || "null");
3053
+ } catch {
3054
+ return null;
3055
+ }
3056
+ },
3057
+ // A turn started on another agent's ask that may no longer start: it waits for the person's Start again.
3058
+ withdrawProposed(id) {
3059
+ const now = new Date().toISOString();
3060
+ const written = execute(
3061
+ "run",
3062
+ `update board_tasks set run_authorized_by = null, run_authorized_at = null, updated_at = @now, version = version + 1
3063
+ where id = @id and status = 'open' and run_authorized_by = 'standing'`,
3064
+ { id, now }
3065
+ );
3066
+ if (written.changes === 1) this.meta(`standing-proposal:${id}`, "");
3067
+ return written.changes === 1;
3068
+ },
2722
3069
  // Tasks a computer should run now: authorized, still open, for an agent installed there. `machine`: that computer;
2723
3070
  // home runs the tasks meant for it and for no computer in particular, a worker (`only`) just those meant for it.
2724
3071
  runnableTasks(agents, machine = null, { only = false } = {}) {
@@ -3556,17 +3903,67 @@ export function openStore(file) {
3556
3903
  null
3557
3904
  );
3558
3905
  },
3559
- // Work an agent asked for that finished, failed or came back with a question since it last called the board: how an
3560
- // asker that cannot be woken (the Claude app, ChatGPT) learns of its answers.
3561
- finishedFor(agent, since) {
3906
+ // Work an agent asked for that finished, failed or came back with a question and whose result it has not yet
3907
+ // acknowledged: how an asker that cannot be woken (the Claude app, ChatGPT), or one woken by mail (Grok Bot), learns
3908
+ // of its answers. Any other board call, a list or a read leaves a result here; only the asker's own receipt for that
3909
+ // very result (ackResult) takes it off, so a result is never spent by an unrelated call or a restart.
3910
+ finishedFor(agent) {
3562
3911
  return execute(
3563
3912
  "all",
3564
- `select id, objective, target_agent, claimed_by, status, updated_at from board_tasks
3565
- where origin_agent = ? and kind = 'task' and removed_at is null and status in ('done', 'failed', 'needs_input') and updated_at > ?
3913
+ `select id, objective, target_agent, claimed_by, status, parent_id, updated_at, reported_at,
3914
+ ${RESULT_RECEIPT} as receipt
3915
+ from board_tasks t
3916
+ where origin_agent = ? and kind = 'task' and removed_at is null and status in ${FINISHED_STATUSES}
3917
+ and not exists (select 1 from board_result_acks a where a.agent = t.origin_agent and a.receipt = ${RESULT_RECEIPT})
3566
3918
  order by updated_at desc limit 20`,
3919
+ agent
3920
+ ).map((row) => ({ ...row, root_id: this.rootIdOf(row.id) }));
3921
+ },
3922
+ // The asker's receipt for one result, exactly as finished gave it: the task, its status and when it was reported.
3923
+ // Another agent's receipt, or one for a result the task no longer holds (asked again, answered and reported anew),
3924
+ // takes nothing. A receipt grants nothing either; it only stops the result being told again.
3925
+ ackResult(agent, taskId, receipt) {
3926
+ const current = execute(
3927
+ "get",
3928
+ `select origin_agent, ${RESULT_RECEIPT} as receipt from board_tasks t
3929
+ where id = ? and kind = 'task' and removed_at is null and status in ${FINISHED_STATUSES}`,
3930
+ taskId
3931
+ );
3932
+ if (!current || current.origin_agent !== agent)
3933
+ return {
3934
+ acked: false,
3935
+ reason:
3936
+ "only the agent that asked for this work can acknowledge its result",
3937
+ };
3938
+ if (current.receipt !== String(receipt))
3939
+ return {
3940
+ acked: false,
3941
+ reason: "that receipt is not this task's current result",
3942
+ receipt: current.receipt,
3943
+ };
3944
+ execute(
3945
+ "run",
3946
+ "insert or ignore into board_result_acks(agent, receipt, task_id, acked_at) values (?, ?, ?, ?)",
3567
3947
  agent,
3568
- since || "1970-01-01T00:00:00.000Z"
3948
+ current.receipt,
3949
+ taskId,
3950
+ new Date().toISOString()
3569
3951
  );
3952
+ return { acked: true, receipt: current.receipt };
3953
+ },
3954
+ // The task a chain of handoffs started from: the asker's original request, so a result finds its way back to it.
3955
+ rootIdOf(taskId) {
3956
+ let id = taskId;
3957
+ for (let depth = 0; depth < 50; depth++) {
3958
+ const parent = execute(
3959
+ "get",
3960
+ "select parent_id from board_tasks where id = ?",
3961
+ id
3962
+ )?.parent_id;
3963
+ if (!parent || parent === taskId) return id;
3964
+ id = parent;
3965
+ }
3966
+ return id;
3570
3967
  },
3571
3968
  // Messages another person's agent sent on this agent's tasks since it last read the board from `machine`.
3572
3969
  unreadFor(agent, machine = null) {
package/lib/swarm.mjs CHANGED
@@ -163,6 +163,8 @@ export function createSwarm({
163
163
  roomAgents,
164
164
  mentionsIn,
165
165
  wakeFor,
166
+ // The computers a message sends its agents to (threadActions.mjs computerAddress).
167
+ computerAddress,
166
168
  notifyBoard,
167
169
  goalCommand,
168
170
  stopThreadRuns,
@@ -1107,6 +1109,10 @@ export function createSwarm({
1107
1109
  ok: false,
1108
1110
  error: `name who the thread is for with @, one of: ${roomAgents.filter((name) => name !== agent && name !== "harness").join(", ")}`,
1109
1111
  };
1112
+ // Where its agents work is settled before the thread exists: a choice that cannot be read starts nothing.
1113
+ const target = request.targetMachine ?? null;
1114
+ const address = computerAddress(text, agent, { target });
1115
+ if (!address.ok) return { ok: false, error: address.error };
1110
1116
  const objective =
1111
1117
  text
1112
1118
  .split("\n")
@@ -1131,21 +1137,32 @@ export function createSwarm({
1131
1137
  const [from] = store.threadsWorkedIn(agent);
1132
1138
  if (from)
1133
1139
  linkThreads(store.getTask(from), store.getTask(thread.id), agent);
1134
- const first = store.roomPost({
1135
- goalId: thread.id,
1136
- author: agent,
1137
- kind: "post",
1138
- text,
1139
- mentions: addressed(store.getTask(thread.id), text),
1140
- machine,
1141
- });
1142
- wakeFor(thread.id, first, agent);
1143
- notifyBoard(store.getTask(thread.id));
1144
- return {
1145
- ok: true,
1146
- thread: summary(store.getTask(thread.id)),
1147
- note: "Agents here run only after the person's Start; read the thread with read_room and add to it with post_to_room.",
1140
+ // The first message crosses the same admission seam as every later post:
1141
+ // guard readings and actual caller limits must survive thread creation.
1142
+ const posted = goalCommand(
1143
+ {
1144
+ ...request,
1145
+ command: "board.room.post",
1146
+ id: thread.id,
1147
+ text,
1148
+ ask: [...team],
1149
+ },
1150
+ agent,
1151
+ machine
1152
+ );
1153
+ const started = (result) => {
1154
+ notifyBoard(store.getTask(thread.id));
1155
+ return {
1156
+ ...result,
1157
+ thread: summary(store.getTask(thread.id)),
1158
+ note:
1159
+ result.note ??
1160
+ "Read the thread with read_room and add to it with post_to_room; its tasks report whether work is running or waiting.",
1161
+ };
1148
1162
  };
1163
+ return posted?.pending
1164
+ ? { pending: Promise.resolve(posted.pending).then(started) }
1165
+ : started(posted);
1149
1166
  }
1150
1167
  if (agent !== "user" && command === "board.swarm.stop") {
1151
1168
  const thread = store.getTask(String(request.id || ""));
@@ -1413,6 +1430,9 @@ export function createSwarm({
1413
1430
  ]);
1414
1431
  const picked = picksIn(store, text, roomAgents);
1415
1432
  if (picked.error) return { ok: false, error: picked.error };
1433
+ const target = request.targetMachine ?? null;
1434
+ const address = computerAddress(text, "user", { target });
1435
+ if (!address.ok) return { ok: false, error: address.error };
1416
1436
  if (from) {
1417
1437
  const agentName = String(from.agent || ""),
1418
1438
  sessionId = String(from.sessionId || "");
@@ -1477,7 +1497,7 @@ export function createSwarm({
1477
1497
  };
1478
1498
  const hear = ({ id, first }) => {
1479
1499
  const opened = store.getTask(id);
1480
- wakeFor(id, first, "user");
1500
+ wakeFor(id, first, "user", undefined, { target });
1481
1501
  if (!to) heardBy(opened, first, text);
1482
1502
  heardPerson(opened, first);
1483
1503
  };