tryharness 0.7.78 → 0.7.81

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.
@@ -0,0 +1,219 @@
1
+ // A shared project's board: the one board every person in the project and their agents see, built on each computer
2
+ // from the project's record (projects.mjs), so every copy shows the same cards. It holds:
3
+ // - the project's pieces and the work agents said they are doing (plan_work) as cards, their column, title and holder
4
+ // following the record: an open piece is To do, one held or work under way In progress, work done Review, merged
5
+ // Done; work that clashes is blocked;
6
+ // - the cards agents and people add (update_board, a task handed out in the room): a `card` entry made by the side
7
+ // that added it.
8
+ // The record's rule holds: each side writes only its own entries. A side's changes to a card (its column, note,
9
+ // blocked, its tasks and trail) are one `card-edit` entry of that side's per card, each field stamped with when it was
10
+ // set; every computer reads a field's newest value across all sides, so moves from either side land the same way.
11
+ import { oneLine } from "./oneLine.mjs";
12
+
13
+ export const BOARD_KINDS = ["card", "card-edit"];
14
+ const FIELDS = ["title", "column", "agent", "note", "blocked", "needsYou"];
15
+ // A card from the record is moved by the record: these of its fields are never set on the board.
16
+ const RECORD_FIELDS = new Set(["title", "column", "agent"]);
17
+ const HISTORY_LIMIT = 30;
18
+ const WORK_COLUMNS = {
19
+ checking: "doing",
20
+ unchecked: "doing",
21
+ clear: "doing",
22
+ overlap: "doing",
23
+ clash: "doing",
24
+ review: "review",
25
+ merged: "done",
26
+ };
27
+
28
+ /** The one entry a side keeps for its changes to one card; an entry under another id is not that side's. */
29
+ export const editIdOf = (cardId, side) => `card-edit:${cardId}:${side}`;
30
+
31
+ export function createProjectBoard({ items, write, local, shared, pieces }) {
32
+ const now = () => new Date().toISOString();
33
+ const named = (p, field, value, toShared) =>
34
+ field === "agent" && value
35
+ ? toShared
36
+ ? shared(p, value)
37
+ : local(p, value)
38
+ : (value ?? null);
39
+ const editsOf = (p) => {
40
+ const byCard = new Map();
41
+ for (const edit of items(p.id, "card-edit")) {
42
+ const cardId = String(edit.body.card ?? "");
43
+ if (edit.id !== editIdOf(cardId, edit.side)) continue;
44
+ byCard.set(cardId, [...(byCard.get(cardId) ?? []), edit]);
45
+ }
46
+ return byCard;
47
+ };
48
+
49
+ // A card as every side's changes leave it: each field its newest value, every side's tasks and trail together.
50
+ const fold = (p, base, edits) => {
51
+ const newest = {};
52
+ for (const edit of edits)
53
+ for (const [field, change] of Object.entries(edit.body.fields ?? {})) {
54
+ const settable =
55
+ [...FIELDS, "removed"].includes(field) &&
56
+ !(base.derived && RECORD_FIELDS.has(field));
57
+ if (!settable) continue;
58
+ if (!newest[field] || String(change.at) > String(newest[field].at))
59
+ newest[field] = change;
60
+ }
61
+ const card = { ...base };
62
+ for (const [field, change] of Object.entries(newest))
63
+ card[field] = named(p, field, change.value, false);
64
+ card.tasks = [...new Set(edits.flatMap((edit) => edit.body.tasks ?? []))];
65
+ card.history = edits
66
+ .flatMap((edit) =>
67
+ (edit.body.history ?? []).map((entry) => ({
68
+ ...entry,
69
+ by: local(p, entry.by),
70
+ }))
71
+ )
72
+ .sort((a, b) => String(a.at).localeCompare(String(b.at)))
73
+ .slice(-HISTORY_LIMIT);
74
+ card.at = [base.at, ...edits.map((edit) => edit.updatedAt)]
75
+ .filter(Boolean)
76
+ .sort()
77
+ .at(-1);
78
+ return card;
79
+ };
80
+
81
+ const blank = {
82
+ note: null,
83
+ blocked: null,
84
+ needsYou: null,
85
+ tasks: [],
86
+ history: [],
87
+ };
88
+ // The pieces and the work under way, as the record has them.
89
+ const fromRecord = (p) => [
90
+ ...pieces.cards(p).map((piece) => ({
91
+ ...blank,
92
+ id: piece.id,
93
+ title: oneLine(piece.title, 120),
94
+ column: piece.column,
95
+ agent: piece.owner ? local(p, piece.owner) : null,
96
+ needsYou: piece.offered ? "a person's OK for its handoff" : null,
97
+ by: local(p, piece.by),
98
+ derived: "piece",
99
+ at: piece.at,
100
+ })),
101
+ ...items(p.id, "work")
102
+ .filter((work) => !work.body.piece && WORK_COLUMNS[work.body.status])
103
+ .map((work) => {
104
+ const agent = local(p, `${work.side}/${work.body.agent}`);
105
+ const clash = work.body.status === "clash";
106
+ return {
107
+ ...blank,
108
+ id: work.id,
109
+ title: oneLine(work.body.summary, 120),
110
+ column: WORK_COLUMNS[work.body.status],
111
+ agent,
112
+ blocked: clash
113
+ ? oneLine(
114
+ `clashes with other work${work.body.check?.reason ? `: ${work.body.check.reason}` : ""}`,
115
+ 240
116
+ )
117
+ : null,
118
+ by: agent,
119
+ derived: "work",
120
+ at: work.at,
121
+ };
122
+ }),
123
+ ];
124
+ const added = (p) =>
125
+ items(p.id, "card").map((entry) => ({
126
+ ...blank,
127
+ id: entry.id,
128
+ title: oneLine(entry.body.title, 120),
129
+ column: "todo",
130
+ agent: null,
131
+ by: local(p, entry.body.by ?? `${entry.side}/person`),
132
+ auto: Boolean(entry.body.auto),
133
+ at: entry.at,
134
+ }));
135
+ const every = (p) => {
136
+ const edits = editsOf(p);
137
+ return [...fromRecord(p), ...added(p)].map((card) =>
138
+ fold(p, card, edits.get(card.id) ?? [])
139
+ );
140
+ };
141
+
142
+ /** The board's cards, as on every computer in the project. */
143
+ const load = (p) => every(p).filter((card) => !card.removed);
144
+
145
+ /** The cards as this side left them: what it added and changed becomes its own entries, and goes across. */
146
+ const save = (p, cards) => {
147
+ const before = new Map(every(p).map((card) => [card.id, card]));
148
+ const edits = editsOf(p);
149
+ const at = now();
150
+ const record = (card, changes, history, tasks) => {
151
+ const id = editIdOf(card.id, p.me);
152
+ const mine = (edits.get(card.id) ?? []).find((edit) => edit.id === id);
153
+ write(
154
+ p,
155
+ "card-edit",
156
+ {
157
+ card: card.id,
158
+ fields: { ...(mine?.body.fields ?? {}), ...changes },
159
+ history: [
160
+ ...(mine?.body.history ?? []),
161
+ ...history.map((entry) => ({ ...entry, by: shared(p, entry.by) })),
162
+ ].slice(-HISTORY_LIMIT),
163
+ tasks,
164
+ },
165
+ id
166
+ );
167
+ };
168
+ for (const card of cards) {
169
+ const was = before.get(card.id);
170
+ if (!was)
171
+ write(
172
+ p,
173
+ "card",
174
+ {
175
+ title: card.title,
176
+ by: shared(p, card.by ?? "user"),
177
+ auto: Boolean(card.auto),
178
+ },
179
+ card.id
180
+ );
181
+ const start = was ?? { ...card, ...blank, column: "todo", agent: null };
182
+ const changes = {};
183
+ for (const field of FIELDS) {
184
+ if (card.derived && RECORD_FIELDS.has(field)) continue;
185
+ const value = card[field] ?? null;
186
+ if (JSON.stringify(value) === JSON.stringify(start[field] ?? null))
187
+ continue;
188
+ changes[field] = { value: named(p, field, value, true), at };
189
+ }
190
+ const seen = new Set(
191
+ (was?.history ?? []).map((entry) => `${entry.at}|${entry.text}`)
192
+ );
193
+ const history = (card.history ?? []).filter(
194
+ (entry) => !seen.has(`${entry.at}|${entry.text}`)
195
+ );
196
+ // Tasks are each side's own: another side's task ids stay in its entry.
197
+ const theirs = new Set(
198
+ (edits.get(card.id) ?? [])
199
+ .filter((edit) => edit.side !== p.me)
200
+ .flatMap((edit) => edit.body.tasks ?? [])
201
+ );
202
+ const tasks = (card.tasks ?? []).filter((id) => !theirs.has(id));
203
+ const mineBefore = (was?.tasks ?? []).filter((id) => !theirs.has(id));
204
+ const tasksChanged = JSON.stringify(tasks) !== JSON.stringify(mineBefore);
205
+ if (Object.keys(changes).length || history.length || tasksChanged)
206
+ record(card, changes, history, tasks);
207
+ }
208
+ const kept = new Set(cards.map((card) => card.id));
209
+ for (const [id, was] of before) {
210
+ if (kept.has(id) || was.removed || was.derived) continue;
211
+ const mine = (edits.get(id) ?? [])
212
+ .filter((edit) => edit.side === p.me)
213
+ .flatMap((edit) => edit.body.tasks ?? []);
214
+ record(was, { removed: { value: true, at } }, [], mine);
215
+ }
216
+ };
217
+
218
+ return { load, save };
219
+ }
@@ -871,12 +871,38 @@ export function createPieces({
871
871
  return item(id);
872
872
  };
873
873
 
874
+ /** The pieces as the project's board shows them (projectBoard.mjs), read without changing anything. */
875
+ const cards = (p) =>
876
+ items(p.id, "piece")
877
+ .filter((piece) => !piece.body.withdrawn)
878
+ .map((piece) => {
879
+ const state = stateOf(p, piece.id);
880
+ const done = doneOf(p, piece.id);
881
+ const column = done
882
+ ? done.body.status === "merged"
883
+ ? "done"
884
+ : "review"
885
+ : state.owner
886
+ ? "doing"
887
+ : "todo";
888
+ return {
889
+ id: piece.id,
890
+ title: piece.body.summary,
891
+ column,
892
+ owner: state.owner,
893
+ offered: Boolean(state.offered),
894
+ by: piece.body.by ?? `${piece.side}/person`,
895
+ at: piece.at,
896
+ };
897
+ });
898
+
874
899
  return {
875
900
  changed,
876
901
  accepted,
877
902
  declined,
878
903
  command,
879
904
  view,
905
+ cards,
880
906
  strengthsOf,
881
907
  brief,
882
908
  refresh,
package/lib/projects.mjs CHANGED
@@ -5,6 +5,7 @@ import { DISPATCHER, nameOf } from "./members.mjs";
5
5
  import { secretReason } from "./secrets.mjs";
6
6
  import { createRoomChannels, agreementHash } from "./roomChannels.mjs";
7
7
  import { createPieces, RECORD_KINDS } from "./projectPieces.mjs";
8
+ import { BOARD_KINDS, createProjectBoard, editIdOf } from "./projectBoard.mjs";
8
9
  import {
9
10
  SETTINGS,
10
11
  SETTING_KEYS,
@@ -932,9 +933,16 @@ export function createProjects({
932
933
  "setting",
933
934
  "note",
934
935
  ...RECORD_KINDS,
936
+ ...BOARD_KINDS,
935
937
  ].includes(entry.kind)
936
938
  )
937
939
  return { ok: true };
940
+ // A side's changes to a card are its one entry for that card; under another id it could stand in for another's.
941
+ if (
942
+ entry.kind === "card-edit" &&
943
+ entry.id !== editIdOf(entry.body?.card, entry.side)
944
+ )
945
+ return { ok: false, error: "a card's changes in the wrong place" };
938
946
  // An answer is its side's own entry for one proposal; one under another id could stand in for this person's.
939
947
  if (
940
948
  entry.kind === "answer" &&
@@ -2423,6 +2431,8 @@ export function createProjects({
2423
2431
  changedFiles,
2424
2432
  branchSlug,
2425
2433
  });
2434
+ // The project's one board, the same on every computer in it (projectBoard.mjs).
2435
+ const board = createProjectBoard({ items, write, local, shared, pieces });
2426
2436
 
2427
2437
  // ---- the board commands ----
2428
2438
 
@@ -2799,6 +2809,15 @@ export function createProjects({
2799
2809
  learned,
2800
2810
  refresh,
2801
2811
  isProjectRoom: (goalId) => Boolean(projectOfRoom(goalId)),
2812
+ /** A project room's board, kept in the project's record so every computer in it shows the same cards. */
2813
+ boardOf: (goalId) => {
2814
+ const p = projectOfRoom(goalId);
2815
+ if (!p) return null;
2816
+ return {
2817
+ load: () => board.load(project(p.id)),
2818
+ save: (cards) => board.save(project(p.id), cards),
2819
+ };
2820
+ },
2802
2821
  /**
2803
2822
  * What a project's room waits on the person for, as the thread's needs-you items: an OK the other side or an agent
2804
2823
  * asked for (agree), and an agent's post kept here until the person shares it (share).
@@ -11,6 +11,7 @@ import {
11
11
  nameOf,
12
12
  speaksThroughHarness,
13
13
  } from "./members.mjs";
14
+ import { FROM_MACHINE } from "./machineRoutes.mjs";
14
15
  import { runCapability, runningPids } from "./runner.mjs";
15
16
  import {
16
17
  descendsFrom,
@@ -30,9 +31,19 @@ export const RUN_CALLER = Symbol("the run that called");
30
31
  // for an agent whose run posts work again could go on forever. Past this many hand-offs in a row with no word from the
31
32
  // person, the next one waits for their Start. In place of a daily limit the person never set.
32
33
  export const MAX_HANDOFFS = 10;
34
+ /**
35
+ * The run that made a call, { id, review, restricted }, or null: one Harness started on this computer (checkCaller),
36
+ * or one on another of the person's computers, as that computer sealed it with the call (ownMachines.mjs callHome).
37
+ * Only a call home opened from another computer carries FROM_MACHINE, so no local caller can name a run here.
38
+ */
39
+ export const callerRun = (request) => {
40
+ if (request?.[RUN_CALLER]) return request[RUN_CALLER];
41
+ const sealed = request?.[FROM_MACHINE] !== undefined ? request.fromRun : null;
42
+ return sealed && typeof sealed === "object" ? sealed : null;
43
+ };
33
44
  /** How the run that made a call was held in, so what it asks for is held in too: "review", "restricted" or null. */
34
45
  export const callerLimit = (request) => {
35
- const run = request?.[RUN_CALLER];
46
+ const run = callerRun(request);
36
47
  return run?.review ? "review" : run?.restricted ? "restricted" : null;
37
48
  };
38
49
  /** Whether a call came from a run that may not act on the person's go: a restricted run or a read-only review. */
@@ -244,26 +255,29 @@ export function createRunAuthority({ store, installedAgents }) {
244
255
 
245
256
  // Why an ask from one of the person's own agents cannot use a standing permission at all, or null. `hosted`: it came
246
257
  // from Harness's server itself (HOSTED_CALLER), never a local process or another computer; `here`: from a process on
247
- // this computer, claiming no Harness origin. Each agent's ask must come the one way that agent speaks. The guard's flag
258
+ // this computer or another the person drives, claiming no Harness origin. Each agent's ask must come the one way that agent speaks. The guard's flag
248
259
  // is a label for readers (it cannot tell the person's agents coordinating from planted orders); words it reads as
249
260
  // orders aimed at the person's agents wait for the person, as do words through Harness's server it could not read
250
261
  // (security audit S4): that is where planted instructions would reach for their secrets. An agent on this computer
251
- // needs no reading, as in a thread (threadAuthority.mjs personsGo).
262
+ // or another the person drives needs no reading, as in a thread (threadAuthority.mjs personsGo); one on a cloud
263
+ // computer Harness runs for them (`cloud`) is read like words through Harness's server.
252
264
  const askRefusal = (ask) => {
253
265
  const proposer = String(ask?.proposer ?? "");
266
+ const read = speaksThroughHarness(proposer) || ask?.cloud === true;
254
267
  return !PROPOSERS.includes(proposer)
255
268
  ? `${nameOf(proposer)} is not one of your own agents that may start it.`
256
269
  : speaksThroughHarness(proposer) && (ask.hosted !== true || ask.window)
257
270
  ? `${nameOf(proposer)}'s ask did not come through Harness as its asks do.`
258
271
  : !speaksThroughHarness(proposer) &&
259
- (ask.hosted === true || ask.here !== true || ask.window)
260
- ? `${nameOf(proposer)}'s ask did not come from this computer as its asks do.`
272
+ (ask.hosted === true ||
273
+ (ask.here !== true && ask.cloud !== true) ||
274
+ ask.window)
275
+ ? `${nameOf(proposer)}'s ask did not come from one of your computers as its asks do.`
261
276
  : ask.limited
262
277
  ? `${nameOf(proposer)} asked from a held-in run.`
263
- : speaksThroughHarness(proposer) &&
264
- (!ask.guard || ask.guard.unavailable)
278
+ : read && (!ask.guard || ask.guard.unavailable)
265
279
  ? `the guard on this computer could not read ${nameOf(proposer)}'s words.`
266
- : ask.guard.verdict === "hold"
280
+ : ask.guard?.verdict === "hold"
267
281
  ? `the guard on this computer read ${nameOf(proposer)}'s words as orders aimed at your agents.`
268
282
  : (ask.handoffs ?? 0) > MAX_HANDOFFS
269
283
  ? `your agents have handed this work on ${MAX_HANDOFFS} times in a row without you, so a loop cannot run on its own.`
@@ -302,7 +316,7 @@ export function createRunAuthority({ store, installedAgents }) {
302
316
  * It counts as one of the agent's standing runs and keeps the agent-written label; the thread's own folder is the one
303
317
  * checked (null: a fresh scratch folder of the thread's, which holds nothing of the person's).
304
318
  *
305
- * `ask`: { proposer, hosted, here, guard, limited, window, handoffs }, as the thread heard it. Returns { started } or { started:
319
+ * `ask`: { proposer, hosted, here, cloud, guard, limited, window, handoffs }, as the thread heard it. Returns { started } or { started:
306
320
  * false, reason }, the reason ending "<agent> waits for your Start: ".
307
321
  */
308
322
  const admitProposed = (wake, ask, folder) => {
package/lib/runBrief.mjs CHANGED
@@ -179,6 +179,8 @@ export function createRunBriefs({
179
179
  const refs = task.context_refs.map(
180
180
  (ref) => `- ${ref.kind}: ${ref.ref}${ref.note ? ` (${ref.note})` : ""}`
181
181
  );
182
+ // Every run in a thread, its turns too, starts from the thread's board: it is where the thread's work is managed.
183
+ const thread = task.parent_id ? store.getTask(task.parent_id) : null;
182
184
  return [
183
185
  {
184
186
  name: "provenance",
@@ -186,6 +188,16 @@ export function createRunBriefs({
186
188
  source: "who wrote the task",
187
189
  text: provenanceLine(task, person?.displayName),
188
190
  },
191
+ ...(thread?.kind === "goal"
192
+ ? [
193
+ {
194
+ name: "thread board",
195
+ priority: "core",
196
+ source: `thread ${thread.id}`,
197
+ text: boardBrief(thread.id),
198
+ },
199
+ ]
200
+ : []),
189
201
  {
190
202
  name: "objective",
191
203
  priority: "core",
@@ -205,18 +217,7 @@ export function createRunBriefs({
205
217
  text: refs.length ? `Context:\n${refs.join("\n")}` : "",
206
218
  pointer: `Context: ${refs.length} reference${refs.length === 1 ? "" : "s"}; get_task ${task.id} lists them.`,
207
219
  },
208
- ...(room?.kind === "goal"
209
- ? [
210
- roomSection(room, agent),
211
- {
212
- name: "thread board",
213
- priority: "recommended",
214
- source: `thread ${room.id}`,
215
- text: boardBrief(room.id),
216
- pointer: `This thread has a board of cards the person follows its work on: keep yours current with update_board (thread ${room.id}).`,
217
- },
218
- ]
219
- : []),
220
+ ...(room?.kind === "goal" ? [roomSection(room, agent)] : []),
220
221
  // Who the agent works for, from the profile setup imported (profile.mjs); like lessons, only for agents here.
221
222
  {
222
223
  name: "the person",
@@ -335,6 +336,11 @@ export function createRunBriefs({
335
336
  const memory = await memoryNotes(task, wake, dryRun);
336
337
  const local = await localContext(task, agent, { wake, room, dryRun });
337
338
  const body = sectionsFor({ task, agent, wake, room, local, memory });
339
+ // The earlier session's words follow what the run is asked.
340
+ const asked =
341
+ body.findIndex((section) =>
342
+ ["brief", "wake brief"].includes(section.name)
343
+ ) + 1;
338
344
  sections = [
339
345
  {
340
346
  name: "T3 catch-up",
@@ -343,7 +349,7 @@ export function createRunBriefs({
343
349
  text: t3 && task.parent_id ? t3CatchUp(task.parent_id, agent) : "",
344
350
  },
345
351
  ...notes,
346
- ...body.slice(0, 3),
352
+ ...body.slice(0, asked),
347
353
  ...(handed
348
354
  ? [
349
355
  {
@@ -354,7 +360,7 @@ export function createRunBriefs({
354
360
  },
355
361
  ]
356
362
  : []),
357
- ...body.slice(3),
363
+ ...body.slice(asked),
358
364
  ];
359
365
  }
360
366
  const fitted = fitSections(sections, BRIEF_BUDGET);
package/lib/store.mjs CHANGED
@@ -2668,14 +2668,17 @@ export function openStore(file) {
2668
2668
  ).n
2669
2669
  );
2670
2670
  },
2671
- postsBy(goalId, author, since) {
2671
+ // `machine`: only posts from that computer, or from none named.
2672
+ postsBy(goalId, author, since, machine = null) {
2672
2673
  return Number(
2673
2674
  execute(
2674
2675
  "get",
2675
- "select count(*) as n from board_contributions where task_id = ? and author = ? and created_at >= ? and kind != 'held'",
2676
+ "select count(*) as n from board_contributions where task_id = ? and author = ? and created_at >= ? and kind != 'held' and (? is null or machine is null or machine = ?)",
2676
2677
  goalId,
2677
2678
  author,
2678
- since
2679
+ since,
2680
+ machine,
2681
+ machine
2679
2682
  ).n
2680
2683
  );
2681
2684
  },