gcf-board 0.28.0 → 0.30.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.
Files changed (33) hide show
  1. package/README.md +3 -3
  2. package/dist/server/aif-entry.js +76 -0
  3. package/dist/server/app.js +60 -6
  4. package/dist/server/chat.js +61 -1
  5. package/dist/server/cli.js +23 -4
  6. package/dist/server/desktop.js +1 -0
  7. package/dist/server/moves.js +3 -2
  8. package/dist/server/reviewPresets.js +35 -0
  9. package/dist/server/runs.js +6 -3
  10. package/dist/server/talks.js +29 -11
  11. package/dist/shared/board.js +45 -0
  12. package/dist/web/assets/index-BhGhi45o.css +1 -0
  13. package/dist/web/assets/index-zKctM0qB.js +123 -0
  14. package/dist/web/index.html +2 -2
  15. package/node_modules/gcf-session/lib/adapter/claude.js +1 -1
  16. package/node_modules/gcf-session/lib/adapter/codex.js +137 -0
  17. package/node_modules/gcf-session/lib/chat-input.js +14 -1
  18. package/node_modules/gcf-session/lib/chat.js +36 -18
  19. package/node_modules/gcf-session/lib/cli.js +7 -2
  20. package/node_modules/gcf-session/lib/index.js +10 -0
  21. package/node_modules/gcf-session/lib/job.js +93 -15
  22. package/node_modules/gcf-session/lib/limit.js +9 -5
  23. package/node_modules/gcf-session/lib/proc.js +5 -1
  24. package/node_modules/gcf-session/lib/session.js +39 -19
  25. package/node_modules/gcf-session/lib/store.js +14 -9
  26. package/node_modules/gcf-session/lib/thread.js +14 -7
  27. package/node_modules/gcf-session/lib/usage.js +23 -5
  28. package/node_modules/gcf-session/lib/wake.js +141 -0
  29. package/node_modules/gcf-session/package.json +1 -1
  30. package/node_modules/gcf-session/types/index.d.ts +52 -2
  31. package/package.json +1 -1
  32. package/dist/web/assets/index-BCfiz95-.css +0 -1
  33. package/dist/web/assets/index-CcsKtA2D.js +0 -119
@@ -24,17 +24,22 @@ export function writeAtomic(file, text) {
24
24
  const tmp = `${file}.${process.pid}.tmp`;
25
25
  try {
26
26
  writeFileSync(tmp, text);
27
- for (let i = 0; ; i++)
28
- try {
29
- renameSync(tmp, file);
30
-
31
- return;
32
- } catch (e) {
33
- if (!HELD.has(e.code) || i >= 10) throw e;
34
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
35
- }
27
+ renameHeld(tmp, file);
36
28
  } catch (e) {
37
29
  rmSync(tmp, { force: true });
38
30
  throw e;
39
31
  }
40
32
  }
33
+
34
+ /** Rename `tmp` over `file`, again for a file held for a moment. */
35
+ function renameHeld(tmp, file) {
36
+ for (let i = 0; ; i++)
37
+ try {
38
+ renameSync(tmp, file);
39
+
40
+ return;
41
+ } catch (e) {
42
+ if (!HELD.has(e.code) || i >= 10) throw e;
43
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
44
+ }
45
+ }
@@ -220,9 +220,7 @@ function markOnce(dir, [kind, key], t, last = false) {
220
220
  * of the thread, a handoff's too, runs by it. A started thread is bound to nothing.
221
221
  */
222
222
  export function createThread(dir, { item = null, entry = null, start = null } = {}) {
223
- if (item && entry) throw new RangeError('a thread is bound to an item or an entry, not both');
224
- if (start && (item || entry)) throw new RangeError('a started thread is bound to nothing');
225
- const [kind, key] = item ? ['items', item] : ['entries', entry];
223
+ const [kind, key] = markKey(item, entry, start);
226
224
  const had = key ? boundTo(dir, kind, key) : null;
227
225
  if (had) return had;
228
226
  mkdirSync(path.join(chatsDir(dir), kind), { recursive: true });
@@ -236,6 +234,14 @@ export function createThread(dir, { item = null, entry = null, start = null } =
236
234
  return t;
237
235
  }
238
236
 
237
+ /** The mark of a new thread, `[kind, key]` (`createThread`); a RangeError for a bound one twice. */
238
+ function markKey(item, entry, start) {
239
+ if (item && entry) throw new RangeError('a thread is bound to an item or an entry, not both');
240
+ if (start && (item || entry)) throw new RangeError('a started thread is bound to nothing');
241
+
242
+ return item ? ['items', item] : ['entries', entry];
243
+ }
244
+
239
245
  /** A thread's folder made exclusively under a fresh id: the id. */
240
246
  function newDir(dir) {
241
247
  for (;;) {
@@ -353,17 +359,17 @@ function flagsOf({ dir, td, spec, adapter }, settings) {
353
359
  * `answerMin`, `handoffPct`, `window`, `skill`, `mainLock`, `yolo` (a file: present, the engine
354
360
  * allows each call with no card, AD-54), `timeoutMin`, `pick` (the project's
355
361
  * model and effort, under the thread's own, over the agent's defaults). `stopped()` says the
356
- * operator stopped it.
357
- * @param {{stopped?: () => boolean}} [opts]
362
+ * operator stopped it; `wake` ends a limit wait early (t1 d1).
363
+ * @param {{stopped?: () => boolean, wake?: import('./wake.js').Wake}} [opts]
358
364
  */
359
365
  export async function runThread(dir, t, spec, opts = {}) {
360
- const { stopped = () => false } = opts;
366
+ const { stopped = () => false, wake } = opts;
361
367
  const td = threadDir(dir, t);
362
368
  const rec = readThread(dir, t);
363
369
  const adapter = adapterFor(spec.agent);
364
370
  const last = rec.sessions.at(-1) ?? null;
365
371
  const s = {
366
- ...{ dir, t, td, rec, spec, adapter, stopped },
372
+ ...{ dir, t, td, rec, spec, adapter, stopped, wake },
367
373
  flags: flagsOf({ dir, td, spec, adapter }, settingsOf(td, adapter, spec.rules)),
368
374
  // The model and effort of the session starting now: read afresh for each (agent.json).
369
375
  pick: {},
@@ -599,6 +605,7 @@ function jobOf(s, record) {
599
605
  return s.flags(kind, s.summary, s.pick);
600
606
  },
601
607
  stopped: s.stopped,
608
+ wake: s.wake,
602
609
  halt: true,
603
610
  clock: stopClock,
604
611
  chatFile: path.join(s.td, 'chat.jsonl'),
@@ -95,7 +95,10 @@ export function reasonOf(e, err, timeoutMs = ASK_TIMEOUT_MS) {
95
95
  function killAll(child) {
96
96
  if (!child.pid) return;
97
97
  if (process.platform === 'win32')
98
- spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });
98
+ spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], {
99
+ windowsHide: true,
100
+ stdio: 'ignore',
101
+ });
99
102
  else
100
103
  try {
101
104
  process.kill(-child.pid, 'SIGKILL');
@@ -250,7 +253,7 @@ const scopedTo = (limit, model) => {
250
253
  };
251
254
 
252
255
  /**
253
- * @typedef {{go: true} | {wait: true, until: Date, reserve: true}
256
+ * @typedef {{go: true} | {wait: true, until: Date, reserve: true, pct?: number, keep?: number}
254
257
  * | {stop: true, why: string, reserve: {weekly?: boolean, until?: string | null}}} Reserve
255
258
  */
256
259
 
@@ -268,8 +271,9 @@ export const reserveOfReading = (reading, model, pct, now = Date.now()) =>
268
271
 
269
272
  /**
270
273
  * The reserve's gate (Hold-Plan d10) over the limits: the 5-hour window at `100 - pct` or more
271
- * waits for its reset (`{wait, until, reserve}`); the week's, all models or the run's, or a reset
272
- * past the longest wait, stops (`{stop, why: 'reserve', reserve}`); else `{go}`. No limits: go.
274
+ * waits for its reset (`{wait, until, reserve, pct, keep}`: `pct` the window's percent, `keep`
275
+ * the reserve's); the week's, all models or the run's, or a reset past the longest wait, stops
276
+ * (`{stop, why: 'reserve', reserve}`); else `{go}`. No limits: go.
273
277
  * @param {any[] | null | undefined} limits
274
278
  * @param {string | null | undefined} model
275
279
  * @param {number} pct
@@ -292,5 +296,19 @@ export function reserveOf(limits, model, pct, now = Date.now()) {
292
296
  if (!until || Number.isNaN(until.getTime()) || until.getTime() - now > MAX_WAIT_MS)
293
297
  return { stop: true, why: 'reserve', reserve: { until: session.resets_at ?? null } };
294
298
 
295
- return { wait: true, until, reserve: true };
299
+ return { wait: true, until, reserve: true, pct: session.percent, keep: pct };
296
300
  }
301
+
302
+ /**
303
+ * Whether a reading taken since `since` (ms) says the limit is back (Operator-Tasks-Plan t1 d1):
304
+ * not failed, with windows, and under `pct`'s reserve (0: the limit itself). An empty list of
305
+ * windows says nothing.
306
+ * @param {any} reading
307
+ * @param {number} since
308
+ * @param {{model?: string | null, pct?: number, now?: number}} [o]
309
+ */
310
+ export const limitBack = (reading, since, { model = null, pct = 0, now = Date.now() } = {}) =>
311
+ Boolean(reading?.limits?.length) &&
312
+ !reading.failed &&
313
+ Date.parse(reading.at) >= since &&
314
+ 'go' in reserveOf(reading.limits, model, pct, now);
@@ -0,0 +1,141 @@
1
+ // The early wake of a usage-limit wait (Operator-Tasks-Plan t1 d1): the operator's mark, which
2
+ // only they can set (they know the limit was reset early), or a fresh `/usage` reading that says
3
+ // the limit is back. A card's run and a project chat thread build theirs here alike.
4
+ import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
5
+ import path from 'node:path';
6
+ import { alive } from './proc.js';
7
+ import { THREAD_ID, threadDir } from './thread.js';
8
+ import { USAGE_MAX_AGE_MS, limitBack, readUsage } from './usage.js';
9
+
10
+ /**
11
+ * The claude that answers `/usage` for an agent: GCF_USAGE_AGENT, else claude for a claude
12
+ * agent; null for another agent, which has no `/usage` (the framework is stack-agnostic).
13
+ * @param {string | null | undefined} agent
14
+ */
15
+ export function usageCommand(agent) {
16
+ if (process.env.GCF_USAGE_AGENT) return process.env.GCF_USAGE_AGENT;
17
+
18
+ return /^"?claude(\.exe|\.cmd)?"?(\s|$)/.test(String(agent ?? '')) ? 'claude' : null;
19
+ }
20
+
21
+ /**
22
+ * @typedef {object} Wake the job's `wake` (job.js `limitWait`)
23
+ * @property {(o: {now: number, until: Date, pct?: number, usage?: boolean}) => void} begin a
24
+ * wait starts: an old mark goes, readings count from `now`; `pct` the reserve's percent (0:
25
+ * the limit's wait); `usage` false: only the mark wakes it
26
+ * @property {(now: number) => 'operator' | 'usage' | null} by what woke the wait, if anything
27
+ * @property {() => void} end the wait ended: no more asks
28
+ */
29
+
30
+ /**
31
+ * The wake of a limit wait. `mark` is the operator's file (`<logs>/<id>.wake`,
32
+ * `chats/<t>/wake`); `reading()` gives the reading another watch already keeps (automation's
33
+ * reserve), else `command` is asked every `pollMs` while a wait lives; neither: the mark alone.
34
+ * `onWake(by, until)` hears each wake.
35
+ * @param {{mark: string, model?: string | null, command?: string | null,
36
+ * reading?: (() => any) | null, pollMs?: number,
37
+ * onWake?: (by: 'operator' | 'usage', until: Date) => void}} o
38
+ * @returns {Wake}
39
+ */
40
+ export function wakeWatch({
41
+ mark,
42
+ model = null,
43
+ command = null,
44
+ reading = null,
45
+ pollMs = USAGE_MAX_AGE_MS,
46
+ onWake,
47
+ }) {
48
+ let since = 0;
49
+ let pct = 0;
50
+ let usage = false;
51
+ /** @type {Date} */
52
+ let until = new Date(0);
53
+ let own = null;
54
+ /** @type {ReturnType<typeof setInterval> | null} */
55
+ let timer = null;
56
+ const read = async () => {
57
+ try {
58
+ own = await readUsage({ command: /** @type {string} */ (command), maxAgeMs: pollMs });
59
+ } catch {
60
+ // No reading: the wait goes on by the clock and the mark.
61
+ }
62
+ };
63
+
64
+ const woke = (by) => {
65
+ onWake?.(by, until);
66
+
67
+ return by;
68
+ };
69
+
70
+ return {
71
+ begin(o) {
72
+ // A mark left from an earlier wait, or written as one ended, would end this one at once.
73
+ rmSync(mark, { force: true });
74
+ ({ now: since, until } = o);
75
+ pct = o.pct ?? 0;
76
+ usage = o.usage !== false && Boolean(reading || command);
77
+ if (!usage || reading) return;
78
+ void read();
79
+ timer = setInterval(() => void read(), pollMs);
80
+ timer.unref?.();
81
+ },
82
+ by(now) {
83
+ if (existsSync(mark)) {
84
+ rmSync(mark, { force: true });
85
+
86
+ return woke('operator');
87
+ }
88
+
89
+ return usage && limitBack(reading ? reading() : own, since, { model, pct, now })
90
+ ? woke('usage')
91
+ : null;
92
+ },
93
+ end() {
94
+ if (timer) clearInterval(timer);
95
+ timer = null;
96
+ },
97
+ };
98
+ }
99
+
100
+ /** The last wait event of a chat file's lines: `limit` while a wait lives, else another. */
101
+ export function lastWaitEvent(text) {
102
+ const lines = text.split('\n').filter(Boolean);
103
+ for (let i = lines.length - 1; i >= 0; i--) {
104
+ const type = /"type":"(session|session_end|limit|limit_end)"/.exec(lines[i])?.[1];
105
+ if (type) return type;
106
+ }
107
+
108
+ return null;
109
+ }
110
+
111
+ /**
112
+ * Whether project chat thread `t` waits for the limit's reset now (t1 d1): its feed's last wait
113
+ * event is `limit` and its opener lives.
114
+ * @param {string} dir the project
115
+ * @param {string} t
116
+ */
117
+ export function threadWaits(dir, t) {
118
+ const td = threadDir(dir, t);
119
+ try {
120
+ const text = readFileSync(path.join(td, 'chat.jsonl'), 'utf8');
121
+ const pid = Number(readFileSync(path.join(td, 'open.lock'), 'utf8').trim());
122
+
123
+ return lastWaitEvent(text) === 'limit' && alive(pid);
124
+ } catch {
125
+ // No feed or no opener: nothing waits.
126
+ return false;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Wake thread `t`'s limit wait (t1 d4): its mark, only while a wait lives. Returns what it did.
132
+ * @param {string} dir the project
133
+ * @param {string} t
134
+ */
135
+ export function wakeThread(dir, t) {
136
+ if (!THREAD_ID.test(t)) throw new Error(`${t} is not a thread id`);
137
+ if (!threadWaits(dir, t)) return `nothing waits in thread ${t}`;
138
+ writeFileSync(path.join(threadDir(dir, t), 'wake'), `${new Date().toISOString()}\n`);
139
+
140
+ return `woke thread ${t}: its session goes on now`;
141
+ }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gcf-session",
3
- "version": "0.19.2",
3
+ "version": "0.20.0",
4
4
  "private": true,
5
5
  "description": "The session engine of GCF: one agent session and a run as a chain of them, the chat protocol, the provider adapters. Bundled by gcf-harness-aif-ext and gcf-board, never published.",
6
6
  "type": "module",
@@ -25,7 +25,8 @@ export type ChatEvent = At &
25
25
  cost_usd: number | null;
26
26
  limit_resets?: string | null;
27
27
  }
28
- | { type: 'limit'; until: string }
28
+ | { type: 'limit'; until: string; reserve?: boolean; pct?: number }
29
+ | { type: 'limit_end'; by: 'operator' | 'usage' | 'clock' | 'stop' | 'hold' }
29
30
  /** The operator's Hold or the quota's reserve: the session hands over (Hold-Plan d4). */
30
31
  | { type: 'held'; reason: 'hold' | 'reserve' | string }
31
32
  /** A project chat's: the operator closed its agent; `dropped` messages it never took in. */
@@ -265,7 +266,7 @@ export interface UsageReading {
265
266
  }
266
267
  export type ReserveGate =
267
268
  | { go: true }
268
- | { wait: true; until: Date; reserve: true }
269
+ | { wait: true; until: Date; reserve: true; pct?: number; keep?: number }
269
270
  | { stop: true; why: 'reserve'; reserve: { weekly?: boolean; until?: string | null } };
270
271
  export declare const RESERVE_PCT: number;
271
272
  export declare const USAGE_MAX_AGE_MS: number;
@@ -303,3 +304,52 @@ export declare function reserveOfReading(
303
304
  pct: number,
304
305
  now?: number,
305
306
  ): ReserveGate;
307
+
308
+ // The early wake of a usage-limit wait (Operator-Tasks-Plan t1).
309
+ export declare function limitBack(
310
+ reading: UsageReading | null | undefined,
311
+ since: number,
312
+ o?: { model?: string | null; pct?: number; now?: number },
313
+ ): boolean;
314
+ export declare function usageCommand(agent: string | null | undefined): string | null;
315
+ export interface Wake {
316
+ begin(o: { now: number; until: Date; pct?: number; usage?: boolean }): void;
317
+ by(now: number): 'operator' | 'usage' | null;
318
+ end(): void;
319
+ }
320
+ export declare function wakeWatch(o: {
321
+ mark: string;
322
+ model?: string | null;
323
+ command?: string | null;
324
+ reading?: (() => UsageReading | null) | null;
325
+ pollMs?: number;
326
+ onWake?: (by: 'operator' | 'usage', until: Date) => void;
327
+ }): Wake;
328
+ export declare function lastWaitEvent(text: string): string | null;
329
+ export declare function threadWaits(dir: string, t: string): boolean;
330
+ export declare function wakeThread(dir: string, t: string): string;
331
+
332
+ // The Codex CLI as a reviewer (Operator-Tasks-Plan t2 d4).
333
+ export interface CodexModel {
334
+ slug: string;
335
+ family: string;
336
+ version: number[];
337
+ priority: number;
338
+ efforts: string[];
339
+ }
340
+ export interface CodexCommand {
341
+ exe: string;
342
+ pre: string[];
343
+ shell: boolean;
344
+ }
345
+ export declare function codexCommand(): CodexCommand;
346
+ export declare function codexInstalled(cmd?: CodexCommand): boolean;
347
+ export declare function codexModels(o?: {
348
+ cache?: string;
349
+ cmd?: CodexCommand;
350
+ }): CodexModel[] | null;
351
+ export declare function familyOf(slug: string): { family: string; version: number[] } | null;
352
+ export declare function slugOf(models: CodexModel[] | null, model: string): string | null;
353
+ export declare function codexPresets(
354
+ models: CodexModel[] | null,
355
+ ): { model: string; slug: string; efforts: string[] }[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gcf-board",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "description": "A local web board for GCF game projects: shows `gcf board --json` and runs the same ai-factory gcf commands an operator runs in a console",
5
5
  "type": "module",
6
6
  "license": "MIT",