gcf-harness-aif-ext 0.11.1 → 0.12.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 (31) hide show
  1. package/README.md +7 -0
  2. package/commands/harness.js +32 -47
  3. package/commands/implement-steps.js +45 -0
  4. package/commands/milestones.js +92 -45
  5. package/commands/run.js +255 -754
  6. package/extension.json +1 -1
  7. package/hooks/handoff-guard.js +19 -10
  8. package/hooks/implement-guard.js +50 -49
  9. package/hooks/owner.js +27 -0
  10. package/hooks/readset-guard.js +35 -21
  11. package/{commands → node_modules/gcf-session/lib}/activity.js +155 -28
  12. package/node_modules/gcf-session/lib/adapter/claude.js +226 -0
  13. package/node_modules/gcf-session/lib/adapter/index.js +33 -0
  14. package/{commands → node_modules/gcf-session/lib}/chat-input.js +67 -31
  15. package/node_modules/gcf-session/lib/chat.js +832 -0
  16. package/node_modules/gcf-session/lib/cli.js +52 -0
  17. package/node_modules/gcf-session/lib/cost.js +29 -0
  18. package/node_modules/gcf-session/lib/index.js +58 -0
  19. package/node_modules/gcf-session/lib/job.js +417 -0
  20. package/node_modules/gcf-session/lib/limit.js +132 -0
  21. package/node_modules/gcf-session/lib/lock.js +81 -0
  22. package/node_modules/gcf-session/lib/proc.js +93 -0
  23. package/node_modules/gcf-session/lib/session.js +320 -0
  24. package/node_modules/gcf-session/lib/store.js +28 -0
  25. package/node_modules/gcf-session/lib/thread.js +423 -0
  26. package/node_modules/gcf-session/lib/windows.js +46 -0
  27. package/node_modules/gcf-session/package.json +23 -0
  28. package/node_modules/gcf-session/types/index.d.ts +152 -0
  29. package/package.json +9 -3
  30. package/skills/gcf-run/SKILL.md +5 -3
  31. package/commands/chat.js +0 -415
package/README.md CHANGED
@@ -47,6 +47,13 @@ Updating keeps the hook entry valid — it points into the extension's install d
47
47
 
48
48
  0.5.1 is a fix release. During a run, `gcf/config.yml` is always readable, and a Grep inside `gcf/` reads only readset files. An import breaks a `depends:` cycle the pair checks closed and asks a D1 about it. It cuts a section longer than a segment at paragraphs, and lens and pair prompts carry a window around the quote. `/gcf-run` checks the plan against each pillar's invariant and asks each pillar's review question over the diff. Use it with core 0.4.1, which brings the parent feature into the readset.
49
49
 
50
+ 0.12.0 moves the session mechanics into the bundled session engine `gcf-session` (docs/Session-Engine.md) and needs gcf-aif-ext 0.12.0: a core without neutral rules (`agent.rules`) is refused before any item is claimed. The hooks keep their files and matchers: no new `init` is needed.
51
+ - **The usage limit waits.** A session cut by the limit with a known reset waits for it (plus 60 s) and goes on in the same session. Before the wait its record is written (`<id>.agent.json`, `ended: limit`, `resets` as ISO), so a run killed meanwhile resumes the same session; the chat gets `limit {until}`; the wait does not count against `--timeout`; `gcf-harness stop` ends it. It does not wait for the week's limit, an unknown reset, a reset over 6 h away, an item's third limit in a run, or a lessons turn: those end `paused` as before. `--wait-limit` (a queued start from the board) also holds each session while a known reset is ahead. `--json` says `limit_wait: true`.
52
+ - **Owners.** The handoff flag and the readset manifest name their owner and item: the guards act only on that session, so a project chat or an interactive claude in the same folder goes on. The guards are each a neutral decision and a claude shim, with the same files and input as before.
53
+ - **Milestones from what the work left:** the DoD's `ok` comes from the item's row of main's `gcf/.metrics/verify-log.csv` between the call and its result (`null` with none or two), a commit's from the new HEAD; a worktree's move is main's to record.
54
+ - A background subagent keeps the session open with no timer and no default answer (`background`, then `working`); the context window is known from the first turn (the live `modelUsage`), and the chat shows the share during a turn (`context`).
55
+ - `GCF_SESSION_AGENT` puts another agent in place of `claude` (tests); `GCF_RUN_AGENT` still works.
56
+
50
57
  0.11.1 reworks the chat (docs/Chat-Plan.md) and needs gcf-aif-ext 0.11.1 for its settings (an older core gives the defaults):
51
58
  - `chat:` of `gcf/config.yml` decides who waits for whom: `ask` (`d1` by default: only the human's decisions by a question card; `all`; `none`: each card is denied, the agent decides) goes to the skill as `/gcf-run <id> --ask d1|all`; `answer_timeout_min` (15) is how long a question waits, 0 when nobody watches (then `ask` is `none`). `--chat-timeout` has no default any more: it overrides the setting for one run, 0 allowed.
52
59
  - `ai-factory gcf-harness chat <id> say [text] [--text-file <path|->] [--image <path>]…`, `answer <request_id> [text] [--answers-file <path|->]`, `interrupt` writes the operator's input: checked (text up to 64 000 characters, up to 10 png/jpg/gif/webp pictures of 5 MB each, an answer to an open card only), never through a link, and only while a live session reads it. Exit 1: bad input; 3: no live chat session; 4: the write failed. A picture is copied into `.gcf-session/runs/<id>.att/`, not git; `chat-in.jsonl` names it by path.
@@ -10,8 +10,7 @@ import path from 'node:path';
10
10
  import { fileURLToPath } from 'node:url';
11
11
  import { metricsCommand } from './metrics.js';
12
12
  import { run, cliCore, DEFAULT_AGENT, DEFAULT_TIMEOUT_MIN, MAIN_RUN, stopOf } from './run.js';
13
- import { CHAT_TIMEOUT_MIN } from './chat.js';
14
- import { ChatRefusal, chatInput, readText } from './chat-input.js';
13
+ import { CHAT_TIMEOUT_MIN, ChatRefusal, alive, chatInput, killTree, readText } from 'gcf-session';
15
14
 
16
15
  const EXT_ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
17
16
  const SCHEMA = 1; // the only gcf/config.yml schema this harness drives
@@ -351,13 +350,38 @@ export function register(program) {
351
350
  .option('--json', 'machine output: {pass, reason}')
352
351
  .action(runGate);
353
352
 
353
+ registerRun(harness);
354
+
355
+ harness
356
+ .command('next [count]')
357
+ .description('The ready items that can run together now, by priority (the gate decides)')
358
+ .option('--json', 'machine output: a list of ids')
359
+ .action((count, o) => {
360
+ const ids = nextBatch(auditReport(), Math.max(1, Number(count ?? 1) || 1));
361
+ console.log(o.json ? JSON.stringify(ids) : ids.join('\n') || 'nothing can run now');
362
+ });
363
+ harness
364
+ .command('metrics')
365
+ .description('Run metrics from gcf/.metrics: reached moves, cost, plan checks, DoD, guards')
366
+ .option('--since <date>', 'only lines at or after this ISO date or time')
367
+ .option('--json', 'machine output')
368
+ .action(metricsCommand);
369
+ harness
370
+ .command('stop <id>')
371
+ .description('Stop the session that works an item; the run goes on, the item stays ready')
372
+ .action((id) => console.log(stopRun(process.cwd(), id)));
373
+ registerChat(harness);
374
+ }
375
+
376
+ /** `gcf-harness run`: the items' agent sessions, each in its checkout. */
377
+ function registerRun(harness) {
354
378
  harness
355
379
  .command('run <ids...>')
356
380
  .description('Run items at once, each in its worktree with its own agent, then integrate')
357
381
  .option(
358
382
  '--agent <command>',
359
383
  'the agent command; it gets "/gcf-run <id> --worktree", or --resume <session> to go on',
360
- process.env.GCF_RUN_AGENT ?? DEFAULT_AGENT,
384
+ process.env.GCF_SESSION_AGENT ?? process.env.GCF_RUN_AGENT ?? DEFAULT_AGENT,
361
385
  )
362
386
  .option(
363
387
  '--timeout <minutes>',
@@ -366,6 +390,10 @@ export function register(program) {
366
390
  )
367
391
  .option('--mode <mode>', 'worktree | main (default: run_mode of gcf/config.yml)')
368
392
  .option('--fresh', 'start new agent sessions: an interrupted one is not resumed')
393
+ .option(
394
+ '--wait-limit',
395
+ 'a queued start: each session first waits while the usage limit is known to reset ahead',
396
+ )
369
397
  .option(
370
398
  '--chat',
371
399
  'keep each session open to the operator: .gcf-session/runs/<id>.chat-in.jsonl',
@@ -377,26 +405,6 @@ export function register(program) {
377
405
  )
378
406
  .option('--json', 'machine output')
379
407
  .action(runItems);
380
-
381
- harness
382
- .command('next [count]')
383
- .description('The ready items that can run together now, by priority (the gate decides)')
384
- .option('--json', 'machine output: a list of ids')
385
- .action((count, o) => {
386
- const ids = nextBatch(auditReport(), Math.max(1, Number(count ?? 1) || 1));
387
- console.log(o.json ? JSON.stringify(ids) : ids.join('\n') || 'nothing can run now');
388
- });
389
- harness
390
- .command('metrics')
391
- .description('Run metrics from gcf/.metrics: reached moves, cost, plan checks, DoD, guards')
392
- .option('--since <date>', 'only lines at or after this ISO date or time')
393
- .option('--json', 'machine output')
394
- .action(metricsCommand);
395
- harness
396
- .command('stop <id>')
397
- .description('Stop the session that works an item; the run goes on, the item stays ready')
398
- .action((id) => console.log(stopRun(process.cwd(), id)));
399
- registerChat(harness);
400
408
  }
401
409
 
402
410
  function registerChat(harness) {
@@ -436,18 +444,6 @@ function chatAction(id, action, words, opts) {
436
444
  }
437
445
  }
438
446
 
439
- const WIN = process.platform === 'win32';
440
-
441
- const alive = (pid) => {
442
- try {
443
- process.kill(pid, 0);
444
-
445
- return true;
446
- } catch {
447
- return false;
448
- }
449
- };
450
-
451
447
  /**
452
448
  * A run lock: `<run pid>\n<item>\n<session pid>\n` once the session started, the run pid alone
453
449
  * before. Null when its run is gone.
@@ -463,18 +459,6 @@ function lockOf(file) {
463
459
  }
464
460
  }
465
461
 
466
- /** Kill a session and everything under it: a tree on Windows, its own group elsewhere. */
467
- function killTree(pid) {
468
- if (WIN) spawnSync('taskkill', ['/pid', String(pid), '/T', '/F'], { stdio: 'ignore' });
469
- else {
470
- try {
471
- process.kill(-pid, 'SIGKILL');
472
- } catch {
473
- // Gone already.
474
- }
475
- }
476
- }
477
-
478
462
  /**
479
463
  * Stop the session that works `id`: the one its own lock names (worktree mode), or the main
480
464
  * checkout's run when that run works this very item (main mode). Only that session goes: the
@@ -548,6 +532,7 @@ async function runItems(ids, opts) {
548
532
  timeoutMs: minutes * 60_000,
549
533
  mode: opts.mode,
550
534
  fresh: Boolean(opts.fresh),
535
+ waitLimit: Boolean(opts.waitLimit),
551
536
  chat: opts.chat ? { answerMin: answer } : null,
552
537
  });
553
538
  if (opts.json) console.log(JSON.stringify(r, null, 2));
@@ -0,0 +1,45 @@
1
+ // gcf-run step 13 in a session's neutral events (gcf-session `Neutral`): whether it invoked
2
+ // aif-implement, and the code it wrote. A session that wrote code with no aif-implement before it
3
+ // skipped the step (`OUTSIDE_IMPLEMENT`).
4
+ import { planning } from '../hooks/implement-guard.js';
5
+
6
+ /**
7
+ * What one tool call tells of step 13: `implement` when it invokes the aif-implement skill,
8
+ * `write` the file an edit names. A subagent's calls count too.
9
+ * @returns {{implement?: boolean, write?: {id: string, file: string}}}
10
+ */
11
+ export function stepOf(e) {
12
+ if (e?.type !== 'tool_call') return {};
13
+ // A plugin's skill comes as <plugin>:aif-implement.
14
+ if (e.kind === 'skill' && /(^|:)aif-implement$/.test(String(e.input?.skill ?? '')))
15
+ return { implement: true };
16
+ const file = e.input?.file_path ?? e.input?.notebook_path;
17
+
18
+ return e.kind === 'edit' && typeof file === 'string' ? { write: { id: e.id, file } } : {};
19
+ }
20
+
21
+ /**
22
+ * One session's step-13 watch in `dir`: `see(events)` takes each line's neutral events;
23
+ * `implement` and `wrote` are what it found. The code writes are kept by call id until a failed
24
+ * result drops one; `dir`'s planning files are no code.
25
+ */
26
+ export function implementWatch(dir) {
27
+ const writes = new Map();
28
+ const w = {
29
+ implement: false,
30
+ wrote: false,
31
+ see(events) {
32
+ for (const e of events) {
33
+ const s = stepOf(e);
34
+ w.implement ||= Boolean(s.implement);
35
+ if (s.write && !planning(dir, s.write.file)) writes.set(s.write.id, s.write.file);
36
+ // A refused or failed call wrote nothing: its write does not count.
37
+ if (e.type === 'tool_result' && e.error) writes.delete(e.id);
38
+ }
39
+
40
+ w.wrote = writes.size > 0;
41
+ },
42
+ };
43
+
44
+ return w;
45
+ }
@@ -1,7 +1,10 @@
1
1
  // The steps of an item's session the operator's chat shows (Board-PRD §5.13): read from the main
2
2
  // agent's tool calls and their results, never from what it says. A plan converged (the first
3
3
  // aif-implement, with the plan checks before it), the DoD (`gcf verify`), the review (a subagent
4
- // the gcf-run skill names `gcf review`), a commit, the item's move.
4
+ // the gcf-run skill names `gcf review`), a commit, the item's move. A step's `ok` is what the
5
+ // work left, not the tool's exit (fix A, AD-37): an agent pipes `gcf verify | tail`, whose exit
6
+ // is tail's. `null`: unknown.
7
+ import { readFileSync } from 'node:fs';
5
8
 
6
9
  /** The description gcf-run gives its reviewer subagent: how the review is told from the rest. */
7
10
  export const REVIEW = 'gcf review';
@@ -17,74 +20,118 @@ const BASH = [
17
20
 
18
21
  /** The step a shell command is, or null. */
19
22
  function bashStep(command) {
23
+ // `verify --mark` stamps a worktree's branch after the DoD: no DoD of its own; the rest of
24
+ // the command still counts.
25
+ const rest = command.replace(/\bai-factory\s+gcf\s+verify\b[^|&;]*\s--mark\b[^|&;]*/g, '');
20
26
  for (const [re, step] of BASH) {
21
- const m = re.exec(command);
27
+ const m = re.exec(rest);
22
28
  if (m) return step === 'move' ? { step, move: m[1] } : { step };
23
29
  }
24
30
 
25
31
  return null;
26
32
  }
27
33
 
28
- /** The step a tool call is, or null. */
29
- function stepOf(b) {
30
- const input = b.input ?? {};
31
- if (b.name === 'Bash') return bashStep(String(input.command ?? ''));
32
- const agent = b.name === 'Task' || b.name === 'Agent';
33
- if (agent && input.description === REVIEW) return { step: 'review' };
34
+ /** The step a tool call (a neutral `tool_call`) is, or null. */
35
+ function stepOf(c) {
36
+ const input = c.input ?? {};
37
+ if (c.kind === 'shell') return bashStep(String(input.command ?? ''));
38
+ if (c.kind === 'subagent' && input.description === REVIEW) return { step: 'review' };
34
39
  // A plugin's skill comes as <plugin>:aif-implement.
35
40
  const implement = /(^|:)aif-implement$/.test(String(input.skill ?? ''));
36
41
 
37
- return b.name === 'Skill' && implement ? { step: 'plan' } : null;
42
+ return c.kind === 'skill' && implement ? { step: 'plan' } : null;
38
43
  }
39
44
 
40
- /** The first line of a tool's result, for a step that failed. */
41
- function firstLine(content) {
42
- const text = Array.isArray(content)
43
- ? content.map((c) => (c?.type === 'text' ? c.text : '')).join('\n')
44
- : String(content ?? '');
45
+ /**
46
+ * Whether the item's `gcf verify` between `from` and `to` (ms) was green: its one row of
47
+ * verify-log.csv (`at,item,ok,failed,mark`, a `--mark` run aside); null with none or several.
48
+ */
49
+ function verified(file, item, from, to) {
50
+ let text;
51
+ try {
52
+ text = readFileSync(file, 'utf8');
53
+ } catch {
54
+ return null;
55
+ }
56
+
57
+ const rows = text
58
+ .split('\n')
59
+ .map((l) => l.split(','))
60
+ .filter(([at, id, , , mark]) => {
61
+ const t = Date.parse(at);
45
62
 
46
- return (text.split('\n').find((l) => l.trim()) ?? '').trim().slice(0, 200);
63
+ return id === item && mark !== '1' && t >= from && t <= to;
64
+ });
65
+
66
+ return rows.length === 1 ? rows[0][2] === '1' : null;
47
67
  }
48
68
 
49
- const blocks = (e, type) =>
50
- (Array.isArray(e.message?.content) ? e.message.content : []).filter((b) => b.type === type);
69
+ /** The first line of a tool's result, for a step that failed. */
70
+ const firstLine = (text) =>
71
+ (
72
+ String(text)
73
+ .split('\n')
74
+ .find((l) => l.trim()) ?? ''
75
+ )
76
+ .trim()
77
+ .slice(0, 200);
78
+
79
+ // A row's time and the watch's may part by this much: the same machine, two processes.
80
+ const SKEW_MS = 2000;
51
81
 
52
82
  /**
53
- * An item's watch, across its sessions: `see(e)` takes each event of a session's stream and
54
- * gives the steps it ends. The plan converges once per item, whatever session reaches it.
83
+ * An item's watch, across its sessions: `see(e)` takes each neutral event of a session (gcf-session
84
+ * `Neutral`) and gives the steps it ends. The plan converges once per item, whatever session
85
+ * reaches it. `verifyLog` is main's verify-log.csv and `item` the item, which the DoD's `ok`
86
+ * is read from; `head()` the session checkout's HEAD, which a commit's is; `worktree`: its
87
+ * moves are recorded on main, not here.
88
+ * @param {{verifyLog?: string, item?: string, head?: () => string | null, worktree?: boolean}} [o]
55
89
  */
56
- export function milestones() {
90
+ export function milestones({ verifyLog, item, head = () => null, worktree = false } = {}) {
57
91
  const calls = new Map();
58
92
  const w = { checks: 0, planned: false };
59
93
  // A call the watch knows waits for its result; the first aif-implement is the plan's step.
60
- const called = (e) =>
61
- blocks(e, 'tool_use').flatMap((b) => {
62
- const step = stepOf(b);
63
- if (step?.step !== 'plan') {
64
- if (step) calls.set(b.id, step);
94
+ const called = (e) => {
95
+ const step = stepOf(e);
96
+ if (step?.step === 'move' && worktree) return [];
97
+ if (step?.step !== 'plan') {
98
+ if (step)
99
+ calls.set(e.id, { ...step, at: Date.now(), head: step.step === 'commit' ? head() : null });
65
100
 
66
- return [];
67
- }
101
+ return [];
102
+ }
68
103
 
69
- const first = !w.planned;
70
- w.planned = true;
104
+ const first = !w.planned;
105
+ w.planned = true;
71
106
 
72
- return first ? [{ type: 'step', step: 'plan', passes: w.checks }] : [];
73
- });
74
- const ended = (e) =>
75
- blocks(e, 'tool_result').flatMap((b) => {
76
- const step = calls.get(b.tool_use_id);
77
- calls.delete(b.tool_use_id);
78
- if (!step || step.step === 'plan-check') {
79
- if (step) w.checks++;
107
+ return first ? [{ type: 'step', step: 'plan', passes: w.checks }] : [];
108
+ };
80
109
 
81
- return [];
82
- }
110
+ /** A step's `ok`: the DoD's row, a commit's new HEAD, else the tool's own result. */
111
+ const okOf = (step, e) => {
112
+ if (step.step === 'dod')
113
+ return verifyLog && item
114
+ ? verified(verifyLog, item, step.at - SKEW_MS, Date.now() + SKEW_MS)
115
+ : null;
116
+ if (step.step === 'commit' && step.head) return head() !== step.head;
83
117
 
84
- const ok = !b.is_error;
118
+ return !e.error;
119
+ };
85
120
 
86
- return [{ type: 'step', ...step, ok, ...(ok ? {} : { text: firstLine(b.content) }) }];
87
- });
121
+ const ended = (e) => {
122
+ const call = calls.get(e.id);
123
+ calls.delete(e.id);
124
+ if (!call || call.step === 'plan-check') {
125
+ if (call) w.checks++;
126
+
127
+ return [];
128
+ }
129
+
130
+ const { at: _, head: __, ...step } = call;
131
+ const ok = okOf(call, e);
132
+
133
+ return [{ type: 'step', ...step, ok, ...(ok === false ? { text: firstLine(e.text) } : {}) }];
134
+ };
88
135
 
89
136
  return {
90
137
  // A session after an earlier run's handoff goes on from a plan that converged before it.
@@ -93,10 +140,10 @@ export function milestones() {
93
140
  },
94
141
  see(e) {
95
142
  // A subagent's calls (a reviewer running the tests) are its own, not the item's steps.
96
- if (!e || e.parent_tool_use_id) return [];
97
- if (e.type === 'assistant') return called(e);
143
+ if (!e || e.sub) return [];
144
+ if (e.type === 'tool_call') return called(e);
98
145
 
99
- return e.type === 'user' && !e.isReplay ? ended(e) : [];
146
+ return e.type === 'tool_result' ? ended(e) : [];
100
147
  },
101
148
  };
102
149
  }