gcf-aif-ext 0.24.0 → 0.25.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.
package/lib/plan-check.js CHANGED
@@ -3,13 +3,74 @@
3
3
  // Whether the plan is any good is the improve passes' job; this only says it is usable. Its
4
4
  // form against the `plan` profile (CNL-P: blocks, one idea a line, no fact twice) is reported
5
5
  // as `format` lines, never a problem: a long line must not cost the item its paid passes.
6
- import { existsSync, readFileSync } from 'node:fs';
6
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
7
7
  import path from 'node:path';
8
8
  import { bodyIssues } from 'cnlp-kit';
9
9
  import { glossaryAvoids, GLOSSARY_NAME } from 'cnlp-kit/config';
10
10
  import { buildLedger } from './ledger.js';
11
11
  import { buildContext } from './context.js';
12
12
  import { loadDocProfile } from './design.js';
13
+ import { readConfig } from './gcf-paths.js';
14
+
15
+ // A pass of the plan's improve loop (Hold-Plan d12): `- pass K[ (cycle C)]: blocking N, polish M[,
16
+ // by codex]`, one line each in its `## Passes` section, written by `gcf plan pass`.
17
+ const PASS_LINE =
18
+ /^- pass (\d+)(?: \(cycle (\d+)\))?: blocking (\d+), polish (\d+)(?:, by (\w+))?$/;
19
+ const PASSES = /^## Passes\s*$/;
20
+ const PASSES_ANY = /^## Passes\s*$/m;
21
+
22
+ /** The passes a plan's `## Passes` section records, of every cycle, in order. */
23
+ export function planPasses(text) {
24
+ const out = [];
25
+ let inside = false;
26
+ for (const line of text.split(/\r?\n/)) {
27
+ if (/^## /.test(line)) inside = PASSES.test(line);
28
+ const m = inside ? PASS_LINE.exec(line.trim()) : null;
29
+ if (!m) continue;
30
+ const by = m[5] ? { by: m[5] } : {};
31
+ out.push({ pass: +m[1], cycle: +(m[2] ?? 0), blocking: +m[3], polish: +m[4], ...by });
32
+ }
33
+
34
+ return out;
35
+ }
36
+
37
+ /**
38
+ * The verdict on one cycle's passes (Hold-Plan d12): `converged` when the last found nothing
39
+ * blocking; `continue` while the count is within `plan_passes` (raised by the operator's `more`);
40
+ * `stalled` past it: one technical pass over the limit, then the operator decides.
41
+ */
42
+ export function passVerdict(passes, planPasses, more = 0) {
43
+ if (planPasses === 0) return 'converged';
44
+ const last = passes.at(-1);
45
+ if (!last) return 'continue';
46
+ if (last.blocking === 0) return 'converged';
47
+
48
+ return passes.length > planPasses + more ? 'stalled' : 'continue';
49
+ }
50
+
51
+ /**
52
+ * Where the item's planning stands: the passes of its current cycle (`plan_cycle`), and the
53
+ * verdict on them. The operator's `stall.accepted` converges it; a plan written before this
54
+ * version (no `plan_cycle`, no `## Passes`, a task ticked) counts as converged: work in flight
55
+ * does not stop at an upgrade.
56
+ */
57
+ async function passesOf(projectDir, data, text, done) {
58
+ const cycle = Number(data.plan_cycle ?? 0);
59
+ const passes = planPasses(text).filter((x) => x.cycle === cycle);
60
+ const stall = data.stall ?? null;
61
+ let limit = 3;
62
+ try {
63
+ limit = Number((await readConfig(path.join(path.resolve(projectDir), 'gcf'))).plan_passes);
64
+ } catch {
65
+ // A broken config.yml: the default limit; the config's own check says what is wrong.
66
+ }
67
+
68
+ const legacy = data.plan_cycle === undefined && !PASSES_ANY.test(text) && done > 0;
69
+ const verdict =
70
+ stall?.accepted || legacy ? 'converged' : passVerdict(passes, limit, Number(stall?.more ?? 0));
71
+
72
+ return { cycle, passes, verdict };
73
+ }
13
74
 
14
75
  /**
15
76
  * An item's plan and its task checkboxes: `{plan, tasks, done}`, `tasks` null when there is no
@@ -81,10 +142,60 @@ export async function planCheck(projectDir, id) {
81
142
  const ctx = await buildContext(projectDir, id);
82
143
  if (ctx.over_budget)
83
144
  problems.push(`readset ~${ctx.total_tokens_est} tokens is over the budget ${ctx.budget}`);
84
- const format =
85
- found === null
86
- ? []
87
- : await planFormat(projectDir, readFileSync(path.resolve(projectDir, plan), 'utf8'));
145
+ const text = found === null ? '' : readFileSync(path.resolve(projectDir, plan), 'utf8');
146
+ const format = found === null ? [] : await planFormat(projectDir, text);
147
+ const { cycle, passes, verdict } = await passesOf(projectDir, item.data, text, done);
148
+
149
+ return {
150
+ ...{ id, plan, tasks, done, problems, format, ok: problems.length === 0 },
151
+ ...{ cycle, passes, converged: verdict === 'converged', stalled: verdict === 'stalled' },
152
+ };
153
+ }
154
+
155
+ /**
156
+ * `gcf plan pass` (Hold-Plan d12): record pass K of the current cycle in the plan's `## Passes`
157
+ * section, K the next number, and answer the verdict on the cycle's passes so far.
158
+ * @param {string} projectDir
159
+ * @param {string} id
160
+ * @param {{blocking: number, polish: number, by?: string}} pass
161
+ */
162
+ export async function recordPass(projectDir, id, { blocking, polish, by }) {
163
+ passArgs(blocking, polish, by);
164
+ const item = (await buildLedger(projectDir)).item(id);
165
+ if (!item) throw new Error(`${id} is not a work item of gcf/work/`);
166
+ const { plan } = planTicks(projectDir, { ...item.data, id });
167
+ const file = path.resolve(projectDir, plan);
168
+ if (!existsSync(file)) throw new Error(`no plan at ${plan}: write it first`);
169
+ const text = readFileSync(file, 'utf8');
170
+ const cycle = Number(item.data.plan_cycle ?? 0);
171
+ const k = planPasses(text).filter((x) => x.cycle === cycle).length + 1;
172
+ const tag = cycle ? ` (cycle ${cycle})` : '';
173
+ const line = `- pass ${k}${tag}: blocking ${blocking}, polish ${polish}${by ? `, by ${by}` : ''}`;
174
+ writeFileSync(file, withPass(text, line));
175
+ const r = await planCheck(projectDir, id);
176
+ const verdict = r.converged ? 'converged' : r.stalled ? 'stalled' : 'continue';
177
+
178
+ return { id, pass: k, cycle, verdict, passes: r.passes.map((x) => x.blocking) };
179
+ }
180
+
181
+ /** The refusal of a pass's counts and reviewer, if any. */
182
+ function passArgs(blocking, polish, by) {
183
+ for (const [k, v] of Object.entries({ blocking, polish }))
184
+ if (!Number.isInteger(v) || v < 0) throw new Error(`--${k} is a count of findings, 0 or more`);
185
+ if (by !== undefined && !/^\w+$/.test(by)) throw new Error('--by is one word: the reviewer');
186
+ }
187
+
188
+ /** The plan with `line` at the end of its `## Passes` section, the section added when none. */
189
+ function withPass(text, line) {
190
+ const eol = text.includes('\r\n') ? '\r\n' : '\n';
191
+ const lines = text.split(/\r?\n/);
192
+ const at = lines.findIndex((l) => PASSES.test(l));
193
+ if (at < 0) return `${text.replace(/\s*$/, '')}${eol}${eol}## Passes${eol}${eol}${line}${eol}`;
194
+ let end = lines.findIndex((l, n) => n > at && /^## /.test(l));
195
+ if (end < 0) end = lines.length;
196
+ // After the section's last line with words: its blank lines stay below it.
197
+ while (end > at + 1 && !lines[end - 1].trim()) end--;
198
+ lines.splice(end, 0, line);
88
199
 
89
- return { id, plan, tasks, done, problems, format, ok: problems.length === 0 };
200
+ return lines.join(eol);
90
201
  }