gcf-board 0.8.2 → 0.10.0

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,12 +4,14 @@
4
4
  // started from a console shows on the board too (its lock or session mark makes the item
5
5
  // `running`); only its report is not here.
6
6
  // In `run_mode: main` the checkout takes one run at a time: a run asked for meanwhile waits in
7
- // the project's queue and starts when the one before it ends.
7
+ // the project's queue and starts when the one before it ends. The queue, its pause, the reports
8
+ // and the trace live in `<board home>/queue.json` too: a board started again goes on with them.
8
9
  import { spawn } from 'node:child_process';
9
10
  import { randomUUID } from 'node:crypto';
10
- import { mkdirSync, readdirSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
11
+ import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, } from 'node:fs';
11
12
  import path from 'node:path';
12
13
  import { fileURLToPath } from 'node:url';
14
+ import { writeAtomicSync } from './atomic.js';
13
15
  import { briefError } from './cli.js';
14
16
  import { alive, broadcast } from './events.js';
15
17
  import { HttpError } from './http-error.js';
@@ -29,13 +31,14 @@ const queues = new Map();
29
31
  const paused = new Set();
30
32
  // The paused projects whose pause the usage limit set: a run that ended paused by it (AD-19).
31
33
  const limitPaused = new Set();
34
+ // When the usage limit that paused a project's queue resets, ISO; none when it was not one.
35
+ const pausedUntil = new Map();
32
36
  // Runs going on for a project the operator removed: their end touches no project state.
33
37
  const forgotten = new WeakSet();
38
+ // The projects whose runs a board started again is taking in: their queue waits until it is done.
39
+ const adopting = new Set();
34
40
  // The runs in flight by their record's id: a project added back must not take one twice.
35
41
  const flights = new Map();
36
- // shortcut: reports and acts live in memory, like the queue: a board restart forgets the ones
37
- // it showed (a run in flight is not lost, its record waits); persist them with the queue if
38
- // restarts become common.
39
42
  const reports = new Map();
40
43
  const acts = new Map();
41
44
  const KEEP_REPORTS = 20;
@@ -45,10 +48,176 @@ function trace(project, act, text) {
45
48
  const list = [...(acts.get(project) ?? []), { at: new Date().toISOString(), act, text }];
46
49
  acts.set(project, list.slice(-KEEP_ACTS));
47
50
  }
51
+ const stateFile = () => path.join(homeDir(), 'queue.json');
52
+ // The folder of each project the board keeps state of: the key `queue.json` checks on a restart.
53
+ const paths = new Map();
54
+ // What a board started again read from `queue.json` and has not given a project yet; null: not
55
+ // read yet, 'bad': there but unreadable.
56
+ let loaded = null;
57
+ /**
58
+ * Write what the board keeps of every project, whole, at once. A project not adopted yet keeps
59
+ * the part it had: the board adopts its projects one by one.
60
+ * shortcut: two boards on one home read and write the same file, and both go on with its queue;
61
+ * two boards on one home is not a setup the board offers (a second start opens the first).
62
+ */
63
+ function save() {
64
+ loaded ??= readSaved();
65
+ const names = new Set([...queues.keys(), ...paused, ...reports.keys(), ...acts.keys()]);
66
+ const projects = loaded !== 'bad' ? { ...loaded } : {};
67
+ for (const name of names) {
68
+ const where = paths.get(name);
69
+ if (!where)
70
+ continue;
71
+ projects[name] = {
72
+ path: where,
73
+ queue: (queues.get(name) ?? []).map(({ entry: _e, p: _p, ...q }) => q),
74
+ paused_by: runsOf(name).paused_by,
75
+ paused_until: pausedUntil.get(name) ?? null,
76
+ reports: reports.get(name) ?? [],
77
+ acts: acts.get(name) ?? [],
78
+ };
79
+ }
80
+ try {
81
+ mkdirSync(homeDir(), { recursive: true });
82
+ // `seq`: the last report id given, so a report closed before a restart keeps its id to itself.
83
+ writeAtomicSync(stateFile(), `${JSON.stringify({ seq: reportSeq, projects }, null, 2)}\n`);
84
+ }
85
+ catch (e) {
86
+ // The board goes on in memory; a restart then starts with the queue last written.
87
+ console.error(`gcf-board: the run queue was not saved: ${e.message}`);
88
+ }
89
+ }
90
+ /** The project's runs changed: keep them, and tell its board. */
91
+ function changed(project) {
92
+ save();
93
+ broadcast({ type: 'board', project });
94
+ }
95
+ /**
96
+ * `queue.json` as a board before this one left it: none is no state; one that cannot be read
97
+ * (locked, not JSON, not a map of projects) is 'bad', and its parts are lost.
98
+ */
99
+ function readSaved() {
100
+ let text = '';
101
+ // A file a scanner holds for a moment is read again, as a write tries again (atomic.ts).
102
+ for (let i = 0;; i++)
103
+ try {
104
+ text = readFileSync(stateFile(), 'utf8');
105
+ break;
106
+ }
107
+ catch (e) {
108
+ const code = e.code ?? '';
109
+ if (code === 'ENOENT')
110
+ return {};
111
+ if (!['EPERM', 'EACCES', 'EBUSY'].includes(code) || i >= 10)
112
+ return setAside();
113
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
114
+ }
115
+ try {
116
+ const v = JSON.parse(text);
117
+ if (typeof v?.projects !== 'object' || v.projects === null || Array.isArray(v.projects))
118
+ return setAside();
119
+ if (Number.isInteger(v.seq))
120
+ reportSeq = Math.max(reportSeq, v.seq);
121
+ return v.projects;
122
+ }
123
+ catch {
124
+ return setAside();
125
+ }
126
+ }
127
+ /** A kept file that cannot be read goes beside it (`queue.json.bad`): the next save must not wipe it. */
128
+ function setAside() {
129
+ try {
130
+ renameSync(stateFile(), `${stateFile()}.bad`);
131
+ }
132
+ catch {
133
+ // Still held: the next save writes over it, the trace has said it was lost.
134
+ }
135
+ return 'bad';
136
+ }
137
+ /** The projects on the board now: the saved parts of any other go (a later one of that name starts empty). */
138
+ export function keepProjects(names) {
139
+ loaded ??= readSaved();
140
+ if (loaded === 'bad')
141
+ return;
142
+ for (const name of Object.keys(loaded))
143
+ if (!names.includes(name))
144
+ delete loaded[name];
145
+ }
146
+ const SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/;
147
+ const WORD = /^[A-Za-z0-9][\w.-]*$/;
148
+ const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
149
+ const list = (v) => (Array.isArray(v) ? v : []);
150
+ const pick = (v) => (typeof v === 'string' && WORD.test(v) ? v : null);
151
+ /**
152
+ * A queued request as `queue.json` holds it, or null: its ids go to the harness's command line,
153
+ * so each must be an item id; its mode one of the two; its pick plain words.
154
+ */
155
+ function queuedOf(v) {
156
+ if (!isObject(v))
157
+ return null;
158
+ const ids = list(v.ids);
159
+ if (!ids.length || !ids.every((id) => typeof id === 'string' && SLUG.test(id)))
160
+ return null;
161
+ if (v.mode !== 'main' && v.mode !== 'worktree')
162
+ return null;
163
+ if (typeof v.harness !== 'string' || !SLUG.test(v.harness))
164
+ return null;
165
+ const agent = isObject(v.agent)
166
+ ? { model: pick(v.agent.model), effort: pick(v.agent.effort) }
167
+ : undefined;
168
+ return {
169
+ ids: ids,
170
+ mode: v.mode,
171
+ harness: v.harness,
172
+ ...(v.fresh === true ? { fresh: true } : {}),
173
+ ...(v.waitLimit === true ? { waitLimit: true } : {}),
174
+ ...(agent ? { agent } : {}),
175
+ };
176
+ }
177
+ /** The state a board before this one kept of project `p`: its queue, pause, reports and trace. */
178
+ function restore(entry, p) {
179
+ loaded ??= readSaved();
180
+ paths.set(p.name, p.path);
181
+ if (loaded === 'bad') {
182
+ trace(p.name, 'queue-lost', `${stateFile()} could not be read (kept as queue.json.bad): the board starts with no queue`);
183
+ // Said once: the next project starts empty with no line of its own.
184
+ loaded = {};
185
+ return;
186
+ }
187
+ const s = loaded[p.name];
188
+ delete loaded[p.name];
189
+ if (!isObject(s))
190
+ return;
191
+ if (s.path !== p.path) {
192
+ trace(p.name, 'queue-lost', `the queue kept for ${String(s.path)} is not this folder's`);
193
+ return;
194
+ }
195
+ const queue = list(s.queue)
196
+ .map(queuedOf)
197
+ .filter((q) => q !== null);
198
+ if (queue.length)
199
+ queues.set(p.name, queue.map((q) => ({ ...q, entry, p })));
200
+ if (s.paused_by === 'limit' || s.paused_by === 'failure')
201
+ paused.add(p.name);
202
+ if (s.paused_by === 'limit')
203
+ limitPaused.add(p.name);
204
+ const until = s.paused_by === 'limit' ? s.paused_until : null;
205
+ if (typeof until === 'string' && !Number.isNaN(Date.parse(until)))
206
+ pausedUntil.set(p.name, until);
207
+ const kept = list(s.reports).filter((r) => isObject(r) && typeof r.id === 'string' && Array.isArray(r.ids));
208
+ if (kept.length)
209
+ reports.set(p.name, kept);
210
+ const did = list(s.acts).filter((a) => isObject(a) && typeof a.act === 'string' && typeof a.text === 'string');
211
+ if (did.length)
212
+ acts.set(p.name, did);
213
+ // Report ids go on after the kept ones, as after a file before `seq`.
214
+ for (const r of kept)
215
+ reportSeq = Math.max(reportSeq, Number(r.id.slice(1)) || 0);
216
+ }
48
217
  /** The operator closed a run report. */
49
218
  export function closeReport(project, id) {
50
219
  reports.set(project, (reports.get(project) ?? []).filter((r) => r.id !== id));
51
- broadcast({ type: 'board', project });
220
+ changed(project);
52
221
  }
53
222
  export const activeRuns = (project) => [...active].filter((r) => r.project === project);
54
223
  /** What runs and what waits, for a project; a paused queue waits for `resumeQueue`. */
@@ -56,9 +225,17 @@ export function runsOf(project) {
56
225
  return {
57
226
  active: activeRuns(project),
58
227
  queued: (queues.get(project) ?? []).flatMap((q) => q.ids),
228
+ // The pick a queued item goes with, by its id: only the ones that have one.
229
+ queued_with: Object.fromEntries((queues.get(project) ?? []).flatMap((q) => q.agent && (q.agent.model || q.agent.effort) ? q.ids.map((id) => [id, q.agent]) : [])),
59
230
  paused: paused.has(project),
60
231
  // Why it waits: a run the usage limit ended paused, or a failure; the operator resumes it.
61
- paused_by: paused.has(project) ? (limitPaused.has(project) ? 'limit' : 'failure') : null,
232
+ paused_by: (paused.has(project)
233
+ ? limitPaused.has(project)
234
+ ? 'limit'
235
+ : 'failure'
236
+ : null),
237
+ // When the usage limit that paused it resets, ISO: none for a pause of another kind.
238
+ paused_until: paused.has(project) ? (pausedUntil.get(project) ?? null) : null,
62
239
  reports: reports.get(project) ?? [],
63
240
  acts: acts.get(project) ?? [],
64
241
  };
@@ -70,8 +247,11 @@ export function forgetProject(project) {
70
247
  queues.delete(project);
71
248
  paused.delete(project);
72
249
  limitPaused.delete(project);
250
+ pausedUntil.delete(project);
73
251
  reports.delete(project);
74
252
  acts.delete(project);
253
+ paths.delete(project);
254
+ save();
75
255
  }
76
256
  /**
77
257
  * A run some of whose items the usage limit ended paused: the harness waits for a reset it
@@ -83,11 +263,16 @@ function limited(run, result) {
83
263
  if (!hit.length)
84
264
  return '';
85
265
  const project = run.p.name;
266
+ const resets = hit.find((i) => i.limit_resets)?.limit_resets ?? '';
86
267
  if (run.mode === 'main') {
87
268
  paused.add(project);
88
269
  limitPaused.add(project);
270
+ // An ISO reset (harness 0.12) shows as a time; the words of an older one only in the report.
271
+ if (resets && !Number.isNaN(Date.parse(resets)))
272
+ pausedUntil.set(project, resets);
273
+ else
274
+ pausedUntil.delete(project);
89
275
  }
90
- const resets = hit.find((i) => i.limit_resets)?.limit_resets ?? '';
91
276
  // A harness before 0.12 does not wait for the reset by itself.
92
277
  const old = result?.limit_wait
93
278
  ? ''
@@ -99,6 +284,8 @@ function limited(run, result) {
99
284
  }
100
285
  /** Start the next queued batch when the checkout is free: after a run, or on the operator's resume. */
101
286
  function advance(project) {
287
+ if (adopting.has(project))
288
+ return;
102
289
  if (paused.has(project) || activeRuns(project).some((r) => r.mode === 'main'))
103
290
  return;
104
291
  const next = queues.get(project)?.shift();
@@ -114,8 +301,9 @@ export function resumeQueue(project) {
114
301
  trace(project, 'queue-resumed', 'the operator resumed the queue');
115
302
  paused.delete(project);
116
303
  limitPaused.delete(project);
304
+ pausedUntil.delete(project);
117
305
  advance(project);
118
- broadcast({ type: 'board', project });
306
+ changed(project);
119
307
  }
120
308
  /** Take an item out of the queue before its run starts. */
121
309
  export function unqueue(project, id) {
@@ -123,7 +311,7 @@ export function unqueue(project, id) {
123
311
  .map((q) => ({ ...q, ids: q.ids.filter((x) => x !== id) }))
124
312
  .filter((q) => q.ids.length);
125
313
  queues.set(project, queue);
126
- broadcast({ type: 'board', project });
314
+ changed(project);
127
315
  }
128
316
  /**
129
317
  * Whether a main run ended in a way the next one would repeat: no report at all (the harness
@@ -166,11 +354,18 @@ function isItem(i) {
166
354
  }
167
355
  function spawnRun(q, mode) {
168
356
  const { entry, p, ids, harness } = q;
169
- const run = { project: p.name, ids, started: new Date().toISOString(), mode };
357
+ const model = q.agent?.model ?? null;
358
+ const effort = q.agent?.effort ?? null;
359
+ const run = { project: p.name, ids, started: new Date().toISOString(), mode, model, effort };
170
360
  active.add(run);
171
361
  const args = [entry, harness, 'run', ...ids, '--json'];
172
362
  if (q.fresh)
173
363
  args.push('--fresh');
364
+ // This run's pick: the items keep their own (Board-V2-Data-Plan §2.5).
365
+ if (model)
366
+ args.push('--model', model);
367
+ if (effort)
368
+ args.push('--effort', effort);
174
369
  if (q.waitLimit)
175
370
  args.push('--wait-limit');
176
371
  // A board's run is always open to the operator: its chat waits the project's answer time
@@ -195,6 +390,8 @@ function spawnRun(q, mode) {
195
390
  started: run.started,
196
391
  harness,
197
392
  pid: relay.pid ?? 0,
393
+ model,
394
+ effort,
198
395
  };
199
396
  try {
200
397
  if (relay.pid) {
@@ -211,9 +408,9 @@ function spawnRun(q, mode) {
211
408
  return run;
212
409
  }
213
410
  /**
214
- * A run's end: its report from the end file its relay wrote, or what went wrong. `live`: a
215
- * board saw it end, so a usage limit or a stalled queue acts on it; a run that ended while no
216
- * board was up only reports (a reset counted from now could be a day late).
411
+ * A run's end: its report from the end file its relay wrote, or what went wrong. A usage limit or
412
+ * a stalled queue pauses the queue `queue.json` kept even when the run ended while no board was
413
+ * up (`live` false): the report says so.
217
414
  */
218
415
  function land(q, run, rid, live, message = '') {
219
416
  // A spawn that fails fires both error and exit: one end only.
@@ -240,7 +437,7 @@ function land(q, run, rid, live, message = '') {
240
437
  active.delete(run);
241
438
  if (q.mode === 'main')
242
439
  advance(q.p.name);
243
- broadcast({ type: 'board', project: run.project });
440
+ changed(run.project);
244
441
  return;
245
442
  }
246
443
  // Held by a scanner for a moment: report now; the end stays for a board after this one,
@@ -263,22 +460,25 @@ function land(q, run, rid, live, message = '') {
263
460
  message = "the run's relay ended without a result";
264
461
  // A run of a project removed meanwhile only reports: it pauses nothing.
265
462
  const kept = !forgotten.has(run);
266
- const acting = kept && live;
267
- const stop = acting && mode === 'main' && queues.get(p.name)?.length && stalled(ok, result);
463
+ const stop = kept && mode === 'main' && queues.get(p.name)?.length && stalled(ok, result);
268
464
  if (stop) {
269
465
  paused.add(p.name);
270
466
  limitPaused.delete(p.name);
467
+ pausedUntil.delete(p.name);
271
468
  }
272
469
  const note = !kept
273
470
  ? ''
274
- : !live
275
- ? 'it ended while the board was stopped: nothing was paused'
276
- : limited(q, result) ||
277
- (stop ? 'the queue is paused: fix the cause, then resume it on the board' : '');
471
+ : [
472
+ live ? '' : 'it ended while the board was stopped',
473
+ limited(q, result) ||
474
+ (stop ? 'the queue is paused: fix the cause, then resume it on the board' : ''),
475
+ ]
476
+ .filter(Boolean)
477
+ .join('; ');
278
478
  if (stop)
279
479
  trace(p.name, 'queue-paused', `after ${ids.join(', ')}: ${note}`);
280
480
  for (const i of result?.items ?? [])
281
- if (acting && i.handoffs)
481
+ if (kept && i.handoffs)
282
482
  trace(p.name, 'handoff', `${i.id}: ${i.handoffs} context handoff(s) to a fresh session`);
283
483
  // A harness before 0.11 refuses the chat flags every board run passes, one before 0.12 the
284
484
  // limit wait of a queued start: say what to update.
@@ -305,7 +505,7 @@ function land(q, run, rid, live, message = '') {
305
505
  // The checkout is free now, for whatever queue the name has: a project added back meanwhile.
306
506
  if (mode === 'main')
307
507
  advance(p.name);
308
- broadcast({ type: 'board', project: p.name });
508
+ changed(p.name);
309
509
  }
310
510
  /**
311
511
  * Whether the relay of a run found on adoption still goes: its pid lives and it touched its
@@ -321,74 +521,101 @@ function going(rid, pid) {
321
521
  }
322
522
  }
323
523
  /**
324
- * The runs a board before this one started for the project: one going shows as active until it
325
- * ends, one that ended meanwhile as its report, one whose relay died with no end as a failure.
524
+ * What a board before this one left for the project: first its queue, pause, reports and trace
525
+ * (`queue.json`), then its runs: one going shows as active until it ends, one that ended
526
+ * meanwhile as its report, one whose relay died with no end as a failure, which pauses the
527
+ * queue. Only then a queue not paused and with no main run going goes on.
326
528
  * shortcut: the records of a project removed while no board was up stay in the board home until
327
529
  * it is added back; sweep the ones of unregistered projects at the start if they pile up.
328
530
  */
329
531
  export function adoptRuns(entry, p) {
330
- let names;
331
532
  try {
332
- names = readdirSync(flightsDir());
533
+ restore(entry, p);
333
534
  }
334
- catch {
335
- return;
535
+ catch (e) {
536
+ // The rest of the board's start must go on: this project only starts with no queue.
537
+ trace(p.name, 'queue-lost', `the kept queue could not be read: ${e.message}`);
336
538
  }
337
- for (const name of names) {
338
- if (!name.endsWith('.json') || name.endsWith('.end.json'))
339
- continue;
340
- const rid = name.slice(0, -'.json'.length);
341
- if (flights.has(rid))
342
- continue;
343
- let f;
539
+ // Its queue goes on only once every run of it is in: one that ended may pause it.
540
+ adopting.add(p.name);
541
+ try {
542
+ let names;
344
543
  try {
345
- f = JSON.parse(readFileSync(recordOf(rid), 'utf8'));
544
+ names = readdirSync(flightsDir());
346
545
  }
347
546
  catch {
348
- continue;
547
+ names = [];
349
548
  }
350
- if (f.project !== p.name || f.path !== p.path)
351
- continue;
352
- const run = { project: p.name, ids: f.ids, started: f.started, mode: f.mode };
353
- const q = { entry, p, ids: f.ids, mode: f.mode, harness: f.harness };
354
- active.add(run);
355
- flights.set(rid, run);
356
- // shortcut: off Windows a dead relay leaves its harness going (no job object), reported
357
- // failed here; record the harness pid if that bites. A board started within a beat of the
358
- // machine waking takes a run going since before the sleep for a dead one (its beat is late):
359
- // a main queue then starts in a busy checkout, and the end that relay writes stays behind.
360
- // Once adopted, only the pid's death ends it: a sleep stops the beats too.
361
- if (going(rid, f.pid)) {
362
- const poll = setInterval(() => {
363
- if (alive(f.pid))
364
- return;
365
- clearInterval(poll);
366
- land(q, run, rid, true);
367
- }, 2000);
368
- poll.unref();
549
+ for (const name of names) {
550
+ if (!name.endsWith('.json') || name.endsWith('.end.json'))
551
+ continue;
552
+ const rid = name.slice(0, -'.json'.length);
553
+ if (flights.has(rid))
554
+ continue;
555
+ let f;
556
+ try {
557
+ f = JSON.parse(readFileSync(recordOf(rid), 'utf8'));
558
+ }
559
+ catch {
560
+ continue;
561
+ }
562
+ if (f.project !== p.name || f.path !== p.path)
563
+ continue;
564
+ const agent = { model: f.model ?? null, effort: f.effort ?? null };
565
+ const run = { project: p.name, ids: f.ids, started: f.started, mode: f.mode, ...agent };
566
+ const q = { entry, p, ids: f.ids, mode: f.mode, harness: f.harness, agent };
567
+ active.add(run);
568
+ flights.set(rid, run);
569
+ // shortcut: off Windows a dead relay leaves its harness going (no job object), reported
570
+ // failed here; record the harness pid if that bites. A board started within a beat of the
571
+ // machine waking takes a run going since before the sleep for a dead one (its beat is late):
572
+ // a main queue then starts in a busy checkout, and the end that relay writes stays behind.
573
+ // Once adopted, only the pid's death ends it: a sleep stops the beats too.
574
+ if (going(rid, f.pid)) {
575
+ const poll = setInterval(() => {
576
+ if (alive(f.pid))
577
+ return;
578
+ clearInterval(poll);
579
+ land(q, run, rid, true);
580
+ }, 2000);
581
+ poll.unref();
582
+ }
583
+ else
584
+ land(q, run, rid, false);
369
585
  }
370
- else
371
- land(q, run, rid, false);
372
586
  }
373
- broadcast({ type: 'board', project: p.name });
587
+ finally {
588
+ adopting.delete(p.name);
589
+ }
590
+ const waiting = queues.get(p.name) ?? [];
591
+ if (waiting.length && !paused.has(p.name) && !activeRuns(p.name).some((r) => r.mode === 'main')) {
592
+ trace(p.name, 'queue-restored', 'the board started again: the queue goes on');
593
+ advance(p.name);
594
+ }
595
+ changed(p.name);
374
596
  }
375
597
  /**
376
598
  * Start a batch in the project's run mode, or queue it behind the main run going on. Its end
377
599
  * is a run report with the harness report, or what went wrong. `fresh` starts new agent
378
600
  * sessions where the harness would resume the interrupted ones. `harness` is the domain's
379
601
  * (`domain.harness`); the tests take GCF's. Its sessions are open to the operator's chat.
602
+ * `agent`: a model and effort for this run only, over each item's own.
380
603
  */
381
- export function startRun(entry, p, ids, mode = 'worktree', fresh = false, harness = 'gcf-harness') {
604
+ export function startRun(entry, p, ids, mode = 'worktree', fresh = false, harness = 'gcf-harness', agent) {
382
605
  // A second click on the same card: the harness would refuse it on its lock, noisily.
606
+ paths.set(p.name, p.path);
383
607
  const { active: going, queued } = runsOf(p.name);
384
608
  const busy = ids.filter((id) => queued.includes(id) || going.some((r) => r.ids.includes(id)));
385
609
  if (busy.length)
386
610
  throw new HttpError(409, `already running from this board: ${busy.join(', ')}`);
387
611
  // A paused queue holds a new main run too: it would repeat the failure, and jump the queue.
388
612
  if (mode === 'main' && (paused.has(p.name) || going.some((r) => r.mode === 'main'))) {
389
- queues.set(p.name, [...(queues.get(p.name) ?? []), { entry, p, ids, mode, fresh, harness }]);
390
- broadcast({ type: 'board', project: p.name });
613
+ queues.set(p.name, [
614
+ ...(queues.get(p.name) ?? []),
615
+ { entry, p, ids, mode, fresh, harness, agent },
616
+ ]);
617
+ changed(p.name);
391
618
  return { queued: ids };
392
619
  }
393
- return { started: spawnRun({ entry, p, ids, mode, fresh, harness }, mode) };
620
+ return { started: spawnRun({ entry, p, ids, mode, fresh, harness, agent }, mode) };
394
621
  }
@@ -11,7 +11,7 @@ import { appendFileSync, existsSync, lstatSync, mkdirSync, readFileSync, statSyn
11
11
  import os from 'node:os';
12
12
  import path from 'node:path';
13
13
  import { fileURLToPath } from 'node:url';
14
- import { THREAD_ID, createThread, listThreads, presetsOf, readThread, setThreadAgent, threadAgent, threadDir, threadOf, } from 'gcf-session';
14
+ import { THREAD_ID, createThread, listThreads, presetsOf, readThread, setThreadAgent, threadAgent, threadDir, threadOf, threadOfEntry, } from 'gcf-session';
15
15
  import { writeAtomic } from './atomic.js';
16
16
  import { alive } from './events.js';
17
17
  import { HttpError } from './http-error.js';
@@ -131,6 +131,8 @@ export function talkSpec(p, board, mcp) {
131
131
  ...(board.context?.window ? { window: board.context.window } : {}),
132
132
  skill: chat.skill,
133
133
  mainLock: path.join(p.path, '.gcf-session', 'runs', '.main-run.lock'),
134
+ // The project's model and effort: a thread that picked none runs on them.
135
+ pick: Object.fromEntries(['model', 'effort'].flatMap((k) => board.agent?.[k] ? [[k, board.agent[k]]] : [])),
134
136
  };
135
137
  }
136
138
  /** A command's output in the project, or empty. */
@@ -238,6 +240,16 @@ export function talkLive(p, t) {
238
240
  return false;
239
241
  }
240
242
  }
243
+ /**
244
+ * What a thread's engine runs with: a thread's agent is a session of the operator's, whoever
245
+ * started the board. A board started from a run or a session passes neither that one's owner
246
+ * nor its maker on (`GCF-By`): the thread's moves are `session`.
247
+ */
248
+ export function talkEnv(env = process.env) {
249
+ // GCF_RUN_ITEM too: a run's guards would take the thread's agent for that run's.
250
+ const { CLAUDE_CODE_SESSION_ID: _, GCF_RUN_OWNER: __, GCF_MOVE_BY: ___, GCF_RUN_ITEM: ____, ...rest } = env;
251
+ return { ...rest, GCF_MOVE_BY: 'session' };
252
+ }
241
253
  // When the board last started each thread: a second start meanwhile only finds the lock held.
242
254
  const starts = new Map();
243
255
  /**
@@ -250,6 +262,7 @@ export function startTalk(p, t) {
250
262
  starts.set(td, Date.now());
251
263
  spawn(process.execPath, [RELAY, end, end, OPEN, 'open', p.path, t], {
252
264
  cwd: p.path,
265
+ env: talkEnv(),
253
266
  detached: true,
254
267
  stdio: 'ignore',
255
268
  windowsHide: true,
@@ -263,6 +276,13 @@ export function openTalk(p, item) {
263
276
  const had = item ? threadOf(p.path, item) : null;
264
277
  return { id: had ?? createThread(p.path, { item }), created: !had };
265
278
  }
279
+ /** The thread bound to inbox entry `entry` (Draft with agent, decision 22), made if none is. */
280
+ export function entryTalk(p, entry) {
281
+ const had = threadOfEntry(p.path, entry);
282
+ return { id: had ?? createThread(p.path, { entry }), created: !had };
283
+ }
284
+ /** The inbox entry thread `t` drafts an item from; null for any other thread. */
285
+ export const talkEntry = (p, t) => readThread(p.path, talkId(t)).entry ?? null;
266
286
  const lineId = () => randomBytes(6).toString('hex');
267
287
  /**
268
288
  * Write the operator's input into thread `t` (AD-35: each line its `id`); a message's pictures
@@ -451,6 +471,7 @@ export function cardIn(events) {
451
471
  tool: e.tool,
452
472
  ...(e.reason ? { reason: e.reason } : {}),
453
473
  ...(e.always ? { always: e.always } : {}),
474
+ ...(e.always && e.always_wide ? { always_wide: true } : {}),
454
475
  });
455
476
  else if (e.type === 'question')
456
477
  open.set(e.request_id, {
@@ -495,6 +516,8 @@ function sayOf(td) {
495
516
  }
496
517
  return null;
497
518
  }
519
+ /** Whether the operator sent thread `t` a message yet. */
520
+ export const talkSaid = (p, t) => sayOf(dirOf(p, t)) !== null;
498
521
  /** What the list shows of thread `th` (Board-PRD §5.13). */
499
522
  async function talkOf(p, th, ids) {
500
523
  const td = threadDir(p.path, th.id);
@@ -510,6 +533,7 @@ async function talkOf(p, th, ids) {
510
533
  id: th.id,
511
534
  title,
512
535
  item: th.item,
536
+ entry: th.entry ?? null,
513
537
  created_at: th.created_at,
514
538
  last_at: events.at(-1)?.at ?? null,
515
539
  cost_usd: Math.round(cost * 100) / 100,
@@ -1,3 +1,11 @@
1
1
  // The read model of `ai-factory gcf board --json` (Methodology §11): the board refuses a schema
2
2
  // it does not know. Types only, plus the version this board reads.
3
3
  export const BOARD_SCHEMA = 3;
4
+ // Where a project keeps files given to an item or an inbox entry (Board-PRD §5.3, §5.6): only
5
+ // there may the board hand out a file that is not text. One list: a later kind is one line.
6
+ const ATTACHED = [
7
+ /^gcf\/inbox\/in-[0-9a-f]{8}\/attachments\/./,
8
+ /^gcf\/work\/[a-z0-9]+(-[a-z0-9]+)*\/attachments\/./,
9
+ ];
10
+ /** Whether project path `rel` lies where attachments are: the board serves it as one. */
11
+ export const attachedShape = (rel) => ATTACHED.some((r) => r.test(rel));