fwdloop 0.16.0 → 0.18.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 (64) hide show
  1. package/README.md +2 -0
  2. package/bin/fwdloop +10 -7
  3. package/package.json +1 -1
  4. package/src/authoring.js +6 -6
  5. package/src/books.js +6 -1
  6. package/src/catalogue.json +9 -3
  7. package/src/ctx-tools.js +245 -0
  8. package/src/draft-steps.js +89 -30
  9. package/src/drafter.js +28 -4
  10. package/src/draftspend.js +109 -3
  11. package/src/liveness.js +34 -10
  12. package/src/panel/author.js +125 -37
  13. package/src/panel/authorcard.js +15 -10
  14. package/src/panel/authorflows.js +9 -3
  15. package/src/panel/authorresume.js +5 -4
  16. package/src/panel/authorstart.js +21 -9
  17. package/src/panel/authortrigger.js +384 -0
  18. package/src/panel/authorvalues.js +5 -3
  19. package/src/panel/data.js +104 -16
  20. package/src/panel/index.html +257 -44
  21. package/src/panel/scheduler.js +125 -0
  22. package/src/panel/server.js +24 -10
  23. package/src/panel/settings.js +2 -2
  24. package/src/plain-checks.js +51 -0
  25. package/src/primitives.js +34 -3
  26. package/src/provider.js +17 -8
  27. package/src/run-store.js +47 -0
  28. package/src/runner.js +2 -0
  29. package/src/runvalues.js +5 -3
  30. package/src/setup.js +24 -0
  31. package/src/signed-text.js +26 -0
  32. package/src/summary-words.js +155 -0
  33. package/src/trigger-clock.js +142 -0
  34. package/src/trigger-files.js +212 -0
  35. package/src/trigger-line.js +86 -0
  36. package/src/types.js +1 -0
  37. package/src/version.js +13 -0
  38. package/types/authoring.d.ts +1 -3
  39. package/types/ctx-tools.d.ts +18 -0
  40. package/types/draft-steps.d.ts +38 -5
  41. package/types/drafter.d.ts +3 -3
  42. package/types/draftspend.d.ts +56 -0
  43. package/types/liveness.d.ts +7 -1
  44. package/types/panel/author.d.ts +149 -146
  45. package/types/panel/authorcard.d.ts +12 -2
  46. package/types/panel/authorflows.d.ts +11 -3
  47. package/types/panel/authorstart.d.ts +7 -4
  48. package/types/panel/authortrigger.d.ts +210 -0
  49. package/types/panel/authorvalues.d.ts +3 -1
  50. package/types/panel/data.d.ts +14 -7
  51. package/types/panel/scheduler.d.ts +15 -0
  52. package/types/panel/server.d.ts +11 -3
  53. package/types/plain-checks.d.ts +22 -0
  54. package/types/primitives.d.ts +0 -6
  55. package/types/provider.d.ts +7 -1
  56. package/types/run-store.d.ts +11 -0
  57. package/types/runvalues.d.ts +1 -1
  58. package/types/setup.d.ts +27 -0
  59. package/types/summary-words.d.ts +53 -0
  60. package/types/trigger-clock.d.ts +60 -0
  61. package/types/trigger-files.d.ts +90 -0
  62. package/types/trigger-line.d.ts +44 -0
  63. package/types/types.d.ts +4 -0
  64. package/types/version.d.ts +2 -0
@@ -0,0 +1,86 @@
1
+ // M9 piece 3 (docs/wiki/the-module-ladder.md, "M9" item 1, am1): the trigger as typed data in the signed arbiter block.
2
+ // Pure text: the grammar of the one `guardrail: trigger ...` line, and the ONE place a prose text gains, changes or loses it.
3
+ //
4
+ // guardrail: trigger daily 09:00
5
+ // guardrail: trigger weekdays 09:00
6
+ // guardrail: trigger weekly mon 09:00
7
+ // guardrail: trigger file-updated <role> = file:<path>
8
+ // guardrail: trigger new-file <role> = file:<folder>
9
+ //
10
+ // No trigger line = By hand (`trigger: null`), so a job signed before M9 parses and hashes exactly as it did. Never cron text.
11
+ // Every regex is a fixed literal, anchored, with no nested quantifiers (signed-text.js's ReDoS rule).
12
+ import { createHash } from 'node:crypto';
13
+
14
+ import { validateSchedule } from './trigger-clock.js';
15
+
16
+ /** @typedef {import('./trigger-clock.js').Schedule} Schedule */
17
+ /** @typedef {Schedule | {kind:'file-updated', role:string, path:string} | {kind:'new-file', role:string, folder:string}} Trigger */
18
+
19
+ const LINE_RE = /^\s*guardrail:\s*trigger\b/i;
20
+ const CLOCK_RE = /^(daily|weekdays) (\d{2}):(\d{2})$/;
21
+ const WEEKLY_RE = /^weekly ([a-z]{3}) (\d{2}):(\d{2})$/;
22
+ const FILE_RE = /^(file-updated|new-file) ([a-z][a-z0-9_-]*) = file:(.*)$/;
23
+
24
+ /**
25
+ * The text after `trigger ` on a guardrail line, as a typed trigger. Never throws.
26
+ * @param {string} text
27
+ * @returns {{ok: true, trigger: Trigger} | {ok: false, error: string}}
28
+ */
29
+ export function parseTrigger(text) {
30
+ const t = String(text).trim();
31
+ const clock = CLOCK_RE.exec(t);
32
+ const weekly = clock ? null : WEEKLY_RE.exec(t);
33
+ const file = clock || weekly ? null : FILE_RE.exec(t);
34
+ try {
35
+ if (clock) return { ok: true, trigger: validateSchedule({ kind: clock[1], hour: Number(clock[2]), minute: Number(clock[3]) }) };
36
+ if (weekly) return { ok: true, trigger: validateSchedule({ kind: 'weekly', day: weekly[1], hour: Number(weekly[2]), minute: Number(weekly[3]) }) };
37
+ } catch (e) {
38
+ return { ok: false, error: /** @type {Error} */ (e).message };
39
+ }
40
+ if (file) {
41
+ const where = file[3].trim();
42
+ if (where === '') return { ok: false, error: 'has an empty path' };
43
+ return { ok: true, trigger: file[1] === 'new-file' ? { kind: 'new-file', role: file[2], folder: where } : { kind: 'file-updated', role: file[2], path: where } };
44
+ }
45
+ return { ok: false, error: `is not "daily HH:MM", "weekdays HH:MM", "weekly <day> HH:MM", "file-updated <role> = file:<path>" or "new-file <role> = file:<folder>": "${t}"` };
46
+ }
47
+
48
+ /** The signed-text line for a trigger. @param {Trigger} t */
49
+ export function triggerLine(t) {
50
+ const hhmm = (/** @type {Schedule} */ s) => `${String(s.hour).padStart(2, '0')}:${String(s.minute).padStart(2, '0')}`;
51
+ if (t.kind === 'daily' || t.kind === 'weekdays') return `guardrail: trigger ${t.kind} ${hhmm(t)}`;
52
+ if (t.kind === 'weekly') return `guardrail: trigger weekly ${t.day} ${hhmm(t)}`;
53
+ if (t.kind === 'file-updated') return `guardrail: trigger file-updated ${t.role} = file:${t.path}`;
54
+ const f = /** @type {{role: string, folder: string}} */ (t);
55
+ return `guardrail: trigger new-file ${f.role} = file:${f.folder}`;
56
+ }
57
+
58
+ /** Is this signed-text line a trigger line (so the grammar sees it, and `stripTrigger` removes it)? @param {string} line */
59
+ export const isTriggerLine = (line) => LINE_RE.test(line);
60
+
61
+ /** The prose without any trigger line. Everything else is byte-for-byte what it was. @param {string} prose */
62
+ export function stripTrigger(prose) {
63
+ return prose.split('\n').filter((l) => !LINE_RE.test(l)).join('\n');
64
+ }
65
+
66
+ /**
67
+ * The ONE writer of the trigger line: the prose with its trigger line replaced by this trigger's (or removed for null). With no trigger
68
+ * in and none asked for, the text is returned unchanged. The arbiter block is last in a signed text, so the line goes at the end.
69
+ * @param {string} prose @param {Trigger|null} trigger
70
+ */
71
+ export function proseWithTrigger(prose, trigger) {
72
+ const stripped = stripTrigger(prose);
73
+ if (trigger === null) return stripped;
74
+ return `${stripped.replace(/\s*$/, '')}\n${triggerLine(trigger)}\n`;
75
+ }
76
+
77
+ /**
78
+ * What the human's second click carries: binds the job, the exact signature it was shown at and the trigger.
79
+ * @param {{flow: string, flowSignatureHash: string, trigger: Trigger|null}} a
80
+ */
81
+ export function triggerHash({ flow, flowSignatureHash, trigger }) {
82
+ return createHash('sha256').update(JSON.stringify({ m9: 'trigger', flow, flowSignatureHash, trigger })).digest('hex');
83
+ }
84
+
85
+ /** M9: the file a triggered start writes (once, before the spawn) into the run's own folder: why this run started. `appendHistory` copies it into the run's history row. */
86
+ export const TRIGGER_RECORD_FILE = 'trigger.json';
package/src/types.js CHANGED
@@ -49,6 +49,7 @@
49
49
  * @property {string[]} skills - `skills <name>[, <name>...]`. Default ['core'].
50
50
  * @property {SourceSlot[]} sources - zero or more `source <role> = file:<path>`.
51
51
  * @property {number} roundBudgetMs - `round budget <int><s|m|h>`. Default 120000.
52
+ * @property {import("./trigger-line.js").Trigger|null} trigger - `trigger <kind> ...` (M9). Default null = By hand.
52
53
  */
53
54
 
54
55
  /**
package/src/version.js ADDED
@@ -0,0 +1,13 @@
1
+ // M4d amendment 17: fwdloop's own version, for the panel header. The ONE reader: the server calls it when it serves the page, so the number is always the one in
2
+ // the package.json of the code that is running (never typed into the page). An unreadable file is "unknown", never a made-up number.
3
+ import { readFileSync } from 'node:fs';
4
+
5
+ const OWN_PACKAGE = new URL('../package.json', import.meta.url);
6
+
7
+ /** @param {string|URL} [file] a package.json (a test passes a copy) @returns {string|null} */
8
+ export function readOwnVersion(file = OWN_PACKAGE) {
9
+ try {
10
+ const v = JSON.parse(readFileSync(file, 'utf8'))?.version;
11
+ return typeof v === 'string' && /^[0-9A-Za-z.+-]{1,40}$/.test(v) ? v : null;
12
+ } catch { return null; }
13
+ }
@@ -86,7 +86,7 @@ export function draftKeyRefusal(slot: string, env: Record<string, string | undef
86
86
  *
87
87
  * Test seam: `provider`/`rates`/`modelId` are injected; without them a live provider is built.
88
88
  */
89
- export function draftToDir({ proseFile, dir, root, name, slot, model, draftThinking, keyNames, budgetUsd, env, provider, rates, modelId, noQuestions, }: {
89
+ export function draftToDir({ proseFile, dir, root, name, slot, model, draftThinking, keyNames, env, provider, rates, modelId, noQuestions, }: {
90
90
  proseFile: any;
91
91
  dir: any;
92
92
  root: any;
@@ -95,7 +95,6 @@ export function draftToDir({ proseFile, dir, root, name, slot, model, draftThink
95
95
  model: any;
96
96
  draftThinking?: "on" | "off" | undefined;
97
97
  keyNames: any;
98
- budgetUsd?: number | undefined;
99
98
  env?: NodeJS.ProcessEnv | undefined;
100
99
  provider: any;
101
100
  rates: any;
@@ -183,7 +182,6 @@ export function signDraft({ dir, approve, signedBy, signedAt, env, keyNames, ses
183
182
  recorded: boolean;
184
183
  };
185
184
  };
186
- export const DRAFT_BUDGET_USD: 0.1;
187
185
  export const SPEC_HASH_FILE: "spec.hash";
188
186
  /** The one folder under the flows root where the panel keeps its draft folders (M4e). Starts with a dot, so it is never a flow name. */
189
187
  export const PANEL_DRAFTS_DIR: ".drafts";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @param {object} o
3
+ * @param {string} o.runDir
4
+ * @param {(verb: string, rawPath: unknown) => Promise<string>} o.gatePath - the read gate; resolves a model-given path to the absolute path it judged, or throws GateRefusal.
5
+ * @returns {Record<string, {name: string, description: string, parameters: object, execute: (args: any) => Promise<string>}>}
6
+ */
7
+ export function makeCtxTools({ runDir, gatePath }: {
8
+ runDir: string;
9
+ gatePath: (verb: string, rawPath: unknown) => Promise<string>;
10
+ }): Record<string, {
11
+ name: string;
12
+ description: string;
13
+ parameters: object;
14
+ execute: (args: any) => Promise<string>;
15
+ }>;
16
+ /** One stashed item holds at most this many bytes (measured after secrets are masked,
17
+ * because that is what enters the store). Bareloop's number, signed in M7 item 3. */
18
+ export const ISOLATE_MAX_BYTES: 65536;
@@ -1,14 +1,47 @@
1
1
  /**
2
- * @param {{ log: any, declaration: any }} p `log` = the draft's log.json (parsed), `declaration` = declaration.json or declaration.rejected.json (parsed)
3
- * @returns {Array<{ id: string, label: string, status: 'done'|'failed', details: string[], reds?: string[] }> | null}
2
+ * The tries of a draft, one per round, oldest first. The free retry after a broken reply (a `structure-retry` round) keeps the number of the try it
3
+ * retries: "try N again", never a new number. `reds` are the checker's raw refusals; `outcome` is a non-validator round's plain words, `rawOutcome` its own raw words.
4
+ * @param {any[]} rounds the draft's log.json `log`
5
+ * @returns {Array<{ label: string, passed: boolean, reds: string[], outcome: string, rawOutcome: string, again: boolean }>}
4
6
  */
5
- export function draftStepList({ log, declaration }: {
7
+ export function triesOf(rounds: any[]): Array<{
8
+ label: string;
9
+ passed: boolean;
10
+ reds: string[];
11
+ outcome: string;
12
+ rawOutcome: string;
13
+ again: boolean;
14
+ }>;
15
+ /**
16
+ * `questionOpen` (am15 item 3): the plan asked a question and the draft has NOT ended: its plan-checks line is not red and has no Fix ("red appears only on a draft that ended red").
17
+ * @param {{ log: any, declaration: any, questionOpen?: boolean }} p `log` = the draft's log.json (parsed), `declaration` = declaration.json or declaration.rejected.json (parsed)
18
+ * @returns {Array<{ id: string, label: string, status: 'done'|'failed'|'waiting', details: string[], note?: boolean }> | null}
19
+ */
20
+ export function draftStepList({ log, declaration, questionOpen }: {
6
21
  log: any;
7
22
  declaration: any;
23
+ questionOpen?: boolean;
24
+ }): Array<{
25
+ id: string;
26
+ label: string;
27
+ status: "done" | "failed" | "waiting";
28
+ details: string[];
29
+ note?: boolean;
30
+ }> | null;
31
+ /**
32
+ * M4d amendment 15, every line of the list: the line of ONE revise (or the redraft after answers), from that plan folder's own log.json. Same rules as `draftStepList`:
33
+ * one line, `<name> · <model> · <n rounds> · <cost>` and, for a plan that passed after refused tries, `fixed by the AI after N tries`; a red one carries one plain
34
+ * reason (and the Fix line under it), never details, folds or raw checker text. The try-by-try history is `triesOf` (the Audit Draft group). `null` = no log.
35
+ * @param {{ log: any, name: string }} p
36
+ * @returns {Array<{ id: string, label: string, status: 'done'|'failed'|'waiting', details: string[], note?: boolean }> | null}
37
+ */
38
+ export function reviseStepList({ log, name }: {
39
+ log: any;
40
+ name: string;
8
41
  }): Array<{
9
42
  id: string;
10
43
  label: string;
11
- status: "done" | "failed";
44
+ status: "done" | "failed" | "waiting";
12
45
  details: string[];
13
- reds?: string[];
46
+ note?: boolean;
14
47
  }> | null;
@@ -244,7 +244,7 @@ export function goalForLine(fromLine: any, lines: any): any;
244
244
  /**
245
245
  * Draft one declaration. `provider`/`rates`/`modelId` may be injected (tests,
246
246
  * the batch); otherwise a live provider is built from `slot` (throws on a bad
247
- * key — the batch preflights first). `budgetUsd` is this draft's hard cap.
247
+ * key — the batch preflights first). The draft's hard cap is the cap signed in the prose (M4d am13); `priorSpend(ceilingUsd)` (am16) is what this job's earlier drafting already spent, so every round must fit what is LEFT of the cap.
248
248
  * `makeProviderFn` is a test seam: the live-provider factory (default `makeProvider`).
249
249
  * `onBook` (M4e piece 2a, scope 13): called right BEFORE each provider call (`inFlight: true`, the call counted in
250
250
  * `calls`) and right AFTER it (`inFlight: false`), with what is known so far, so the caller can book spend per call
@@ -255,7 +255,7 @@ export function goalForLine(fromLine: any, lines: any): any;
255
255
  * costUsd:number|null, modelReturned:string|null, modelId?:string, price?:object|null, tokens?:object|null, structureRetries:number, revisions:number, stop:string|null, log:object[],
256
256
  * questions?:{line:number, question:string}[], droppedQuestions?:object[], notChecked?:string[], thinking?:{asked:string, status:string|null, say:string|null}, inputs?:{role:string, detail:string}[]}>}
257
257
  */
258
- export function draft({ proseText, slot, model, provider: injected, rates: injectedRates, modelId: injectedModelId, env, budgetUsd, skills, makeProviderFn, onBook, offerQuestions, draftThinking, }: {
258
+ export function draft({ proseText, slot, model, provider: injected, rates: injectedRates, modelId: injectedModelId, env, priorSpend, skills, makeProviderFn, onBook, offerQuestions, draftThinking, }: {
259
259
  proseText: any;
260
260
  slot: any;
261
261
  model: any;
@@ -263,7 +263,7 @@ export function draft({ proseText, slot, model, provider: injected, rates: injec
263
263
  rates: any;
264
264
  modelId: any;
265
265
  env: any;
266
- budgetUsd?: number | undefined;
266
+ priorSpend?: ((ceilingUsd: number) => number) | null | undefined;
267
267
  skills?: readonly string[] | undefined;
268
268
  makeProviderFn?: typeof makeProvider | undefined;
269
269
  onBook: any;
@@ -18,4 +18,60 @@ export function readDraftSpend(dir: string): Record<string, any> | null;
18
18
  * @returns {Record<string, any>[]}
19
19
  */
20
20
  export function readDirSpendRows(dir: string): Record<string, any>[];
21
+ /**
22
+ * M4d amendment 16 item 1: what ALL of a job's drafting spent — the one writer of that total. A panel draft's session folder holds one plan
23
+ * folder per draft (`draft/`, `draft-<n>/` for each revise and the redraft after answers); each is read by `readDirSpendRows` and priced by
24
+ * `spendRowCost`, the one cost rule. A round whose cost is not known counts at its WORST CASE (item 5): its own booked price's ceiling, else
25
+ * `ceilingUsd` (Settings prices x the fixed token limits), never $0; `notPriced` counts those rounds and `complete` is false.
26
+ * @param {string} sessionDir the panel's draft folder
27
+ * @param {{ ceilingUsd?: number, exclude?: string }} [o] `exclude` = a plan folder name to leave out (the one being written now)
28
+ * @returns {{ usd: number, notPriced: number, complete: boolean, missingEarlier: boolean }}
29
+ */
30
+ export function draftingSpent(sessionDir: string, { ceilingUsd, exclude }?: {
31
+ ceilingUsd?: number;
32
+ exclude?: string;
33
+ }, seen?: Set<any>): {
34
+ usd: number;
35
+ notPriced: number;
36
+ complete: boolean;
37
+ missingEarlier: boolean;
38
+ };
39
+ /**
40
+ * M4d amendment 16 items 1, 3, 5: the split of the card's Cap $ at Sign & run. `leftUsd` is what the first run may spend, rounded DOWN to 4 places (never
41
+ * more than is left). `line` is the sentence the page shows.
42
+ * @param {number} capUsd @param {{ usd: number, notPriced: number }} spent
43
+ */
44
+ export function potSplit(capUsd: number, spent: {
45
+ usd: number;
46
+ notPriced: number;
47
+ }): {
48
+ capUsd: number;
49
+ spentUsd: number;
50
+ notPriced: number;
51
+ leftUsd: number;
52
+ line: string;
53
+ };
54
+ /**
55
+ * The drafter's `priorSpend` for the plan folder `dir`: when it sits in a panel session folder (a `card.json` beside it), what the session's earlier plans spent;
56
+ * null for a plain CLI draft (no session, nothing earlier). The folder being written is left out.
57
+ * @param {string} dir @returns {((ceilingUsd: number) => number)|null}
58
+ */
59
+ export function sessionPriorSpend(dir: string): ((ceilingUsd: number) => number) | null;
60
+ /**
61
+ * M4d amendment 19 item 1: what a draft reserves against the monthly limit: what is LEFT of the cap in its prose (cap minus what this job's drafting already
62
+ * spent, `sessionPriorSpend`), not one round. A prose that does not parse falls back to one round: the draft itself refuses it at $0.
63
+ * @param {string} proseText @param {string} outDir the plan folder the draft will write @param {number} ceilingUsd one round at its worst case
64
+ */
65
+ export function draftHoldUsd(proseText: string, outDir: string, ceilingUsd: number): number;
66
+ /**
67
+ * M4d amendment 19 item 5 / amendment 20: the ONE sentence for a cap that cannot pay for one round. `kind` is "drafting" (the Chat card, the drafter) or "run"
68
+ * (Run a signed flow, Resume); nothing else differs. `modelId` is the model the minimum was computed for, `minUsd` its worst-case round.
69
+ * @param {'drafting'|'run'} kind @param {number} capUsd @param {number} minUsd @param {string} modelId
70
+ */
71
+ export function capTooSmallText(kind: "drafting" | "run", capUsd: number, minUsd: number, modelId: string): string;
21
72
  export const DRAFT_SPEND_FILE: "draft-spend.json";
73
+ /** M4d am21 item 2: the one sentence for a Start over (or revise, or sign) whose chain names an earlier draft that is gone. */
74
+ export const EARLIER_MISSING_SAY: "An earlier draft of this job is missing, so its spend can't be counted.";
75
+ /** The file a Start over's session folder carries to name the session it started over from (written once by src/panel/author.js). */
76
+ export const STARTED_OVER_FILE: "started-over-from.json";
77
+ export function usdText(n: number): string;
@@ -14,9 +14,10 @@ export function procStartOf(pid: number | "self"): string | null;
14
14
  * null - cannot tell (no /proc, or not ours to read): caller falls back to the 10-minute rule
15
15
  * @param {number} pid
16
16
  * @param {string|null} [procStart] the starttime recorded by the process itself; absent/null = not compared
17
+ * @param {typeof REAL_PROC} [proc] where /proc is read from (tests pass a snapshot)
17
18
  * @returns {boolean|null}
18
19
  */
19
- export function isFwdloopAlive(pid: number, procStart?: string | null): boolean | null;
20
+ export function isFwdloopAlive(pid: number, procStart?: string | null, proc?: typeof REAL_PROC): boolean | null;
20
21
  /**
21
22
  * The one writer call: this process records itself in the run dir's
22
23
  * `pids.jsonl`, before its first step.
@@ -68,3 +69,8 @@ export function readResumeLock(runDir: string): {
68
69
  export function booksFresh(runDir: string, nowMs?: number): boolean;
69
70
  /** The words the runner's refusal uses for a lock it cannot clear; the panel reads the same constant (M4c amendment 3 (e)). */
70
71
  export const LOCK_NO_HOLDER: "with no recorded holder";
72
+ declare namespace REAL_PROC {
73
+ function cmdline(pid: number): string;
74
+ function stat(pid: number): string;
75
+ }
76
+ export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @param {{ root: string, home?: string, skipMonthly?: boolean, loadEnv: () => { ok: boolean, env: Record<string, string|undefined>, refusal: string|null, keyNames?: string[] }, bin?: string, childRunning?: (dir: string) => boolean, thinkingSupport?: typeof bareagentThinkingSupport }} opts
2
+ * @param {{ root: string, home?: string, skipMonthly?: boolean, loadEnv: () => { ok: boolean, env: Record<string, string|undefined>, refusal: string|null, keyNames?: string[] }, bin?: string, childRunning?: (dir: string) => boolean, thinkingSupport?: typeof bareagentThinkingSupport, now?: () => number }} opts
3
3
  * `loadEnv` is the keys-file door (`keysForDoor`): called before EVERY spawn and every reply that quotes a log, so editing
4
4
  * the file needs no restart. Its `env` is BOTH the child's spawn env AND the scrub list (POC (a), M4d wiring rule).
5
5
  */
@@ -16,75 +16,17 @@ export function createAuthor(opts: {
16
16
  bin?: string;
17
17
  childRunning?: (dir: string) => boolean;
18
18
  thinkingSupport?: typeof bareagentThinkingSupport;
19
+ now?: () => number;
19
20
  }): {
20
- flows: () => {
21
- status: number;
21
+ /** `GET /api/author/flows`, each listed job carrying its trigger card (M9 piece 5): the typed trigger, its words, and why it cannot be set (no good run yet). */
22
+ flows(): {
22
23
  body: {
24
+ flows: any[];
23
25
  ok: boolean;
24
- flows: never[];
25
26
  leftThisMonth?: undefined;
26
- };
27
- } | {
28
- status: number;
29
- body: {
27
+ } | {
28
+ flows: any[];
30
29
  ok: boolean;
31
- flows: {
32
- edit: {
33
- ok: boolean;
34
- say: string;
35
- card?: undefined;
36
- flowHash?: undefined;
37
- } | {
38
- ok: boolean;
39
- card: {
40
- job: string;
41
- inputs: string;
42
- destination: string;
43
- capUsd: string;
44
- askWait: string;
45
- };
46
- flowHash: any;
47
- say?: undefined;
48
- };
49
- values: {
50
- capUsd: number;
51
- destination: string | null;
52
- askWaits: Record<string, string>;
53
- };
54
- jobLines: any;
55
- hasSend: boolean;
56
- capFloorUsd: number;
57
- floorText: string;
58
- model: {
59
- slot: string;
60
- model: string;
61
- label: string;
62
- } | null;
63
- flow: string;
64
- capUsd: any;
65
- roles: any;
66
- nextRunId: string;
67
- lastRunId: string | null;
68
- lastSources: Record<string, string>;
69
- leftThisMonth: any;
70
- runs: {
71
- sources: Record<string, string>;
72
- values: {
73
- capUsd: number;
74
- destination: string | null;
75
- askWaits: Record<string, string>;
76
- };
77
- runId: string;
78
- }[];
79
- stats: {
80
- steps: any;
81
- asks: number;
82
- green: number;
83
- notGreen: number;
84
- avgSpendUsd: number | null;
85
- avgWallMs: number | null;
86
- };
87
- }[];
88
30
  leftThisMonth: {
89
31
  limitUsd: null;
90
32
  leftUsd: null;
@@ -97,86 +39,10 @@ export function createAuthor(opts: {
97
39
  say: string;
98
40
  };
99
41
  };
100
- };
101
- runAgain: (flow: string | null, runId: string | null) => {
102
- status: any;
103
- body: {
104
- ok: boolean;
105
- refused: string;
106
- say: any;
107
- };
108
- } | {
109
42
  status: number;
110
- body: {
111
- ok: boolean;
112
- flow: {
113
- edit: {
114
- ok: boolean;
115
- say: string;
116
- card?: undefined;
117
- flowHash?: undefined;
118
- } | {
119
- ok: boolean;
120
- card: {
121
- job: string;
122
- inputs: string;
123
- destination: string;
124
- capUsd: string;
125
- askWait: string;
126
- };
127
- flowHash: any;
128
- say?: undefined;
129
- };
130
- values: {
131
- capUsd: number;
132
- destination: string | null;
133
- askWaits: Record<string, string>;
134
- };
135
- jobLines: any;
136
- hasSend: boolean;
137
- capFloorUsd: number;
138
- floorText: string;
139
- model: {
140
- slot: string;
141
- model: string;
142
- label: string;
143
- } | null;
144
- flow: string;
145
- capUsd: any;
146
- roles: any;
147
- nextRunId: string;
148
- lastRunId: string | null;
149
- lastSources: Record<string, string>;
150
- leftThisMonth: any;
151
- runs: {
152
- sources: Record<string, string>;
153
- values: {
154
- capUsd: number;
155
- destination: string | null;
156
- askWaits: Record<string, string>;
157
- };
158
- runId: string;
159
- }[];
160
- stats: {
161
- steps: any;
162
- asks: number;
163
- green: number;
164
- notGreen: number;
165
- avgSpendUsd: number | null;
166
- avgWallMs: number | null;
167
- };
168
- };
169
- pick: {
170
- sources: Record<string, string>;
171
- values: {
172
- capUsd: number;
173
- destination: string | null;
174
- askWaits: Record<string, string>;
175
- };
176
- runId: string;
177
- };
178
- };
179
43
  };
44
+ /** `GET /api/author/run-again`, its flow entry carrying the trigger card like the list's entries do. @param {string|null} flow @param {string|null} runId */
45
+ runAgain(flow: string | null, runId: string | null): any;
180
46
  run: (body: any) => {
181
47
  status: any;
182
48
  body: any;
@@ -185,6 +51,141 @@ export function createAuthor(opts: {
185
51
  status: any;
186
52
  body: any;
187
53
  };
54
+ trigger: {
55
+ inspect: (body: any) => {
56
+ ok: false;
57
+ reply: {
58
+ status: number;
59
+ body: {
60
+ ok: boolean;
61
+ };
62
+ };
63
+ } | {
64
+ ok: true;
65
+ flow: any;
66
+ read: import("../types.js").ReadFlowOk;
67
+ trigger: import("../trigger-line.js").Trigger | null;
68
+ flowDir: string;
69
+ realRoot: string;
70
+ proseText: string;
71
+ declaration: any;
72
+ hash: string;
73
+ };
74
+ now: () => number;
75
+ prepare(body: any): {
76
+ status: number;
77
+ body: {
78
+ ok: boolean;
79
+ };
80
+ } | {
81
+ status: number;
82
+ body: {
83
+ ok: boolean;
84
+ flow: any;
85
+ trigger: import("../trigger-line.js").Trigger | null;
86
+ words: string | null;
87
+ hash: string;
88
+ };
89
+ };
90
+ set(body: any): {
91
+ status: number;
92
+ body: {
93
+ ok: boolean;
94
+ };
95
+ } | {
96
+ status: number;
97
+ body: {
98
+ ok: boolean;
99
+ refused: string;
100
+ say: string;
101
+ reds?: undefined;
102
+ };
103
+ } | {
104
+ status: number;
105
+ body: {
106
+ ok: boolean;
107
+ refused: string;
108
+ say: string;
109
+ reds: any[];
110
+ };
111
+ } | {
112
+ status: number;
113
+ body: {
114
+ say?: string | undefined;
115
+ ok: boolean;
116
+ flow: any;
117
+ trigger: import("../trigger-line.js").Trigger | null;
118
+ words: string | null;
119
+ flowHash: string;
120
+ armed: boolean;
121
+ refused?: undefined;
122
+ reds?: undefined;
123
+ };
124
+ };
125
+ scan(): {
126
+ flow: string;
127
+ flowDir: string;
128
+ read: import("../types.js").ReadFlowOk;
129
+ trigger: import("./authortrigger.js").Trigger;
130
+ }[];
131
+ ensureArmed(job: {
132
+ flowDir: string;
133
+ read: any;
134
+ trigger: Trigger;
135
+ }): {
136
+ armed: boolean;
137
+ already: boolean;
138
+ };
139
+ busy(job: {
140
+ flow: string;
141
+ }): boolean;
142
+ note(job: {
143
+ flow: string;
144
+ flowDir: string;
145
+ trigger: Trigger;
146
+ }, row: Record<string, any>): void;
147
+ refuse(job: {
148
+ flow: string;
149
+ flowDir: string;
150
+ trigger: Trigger;
151
+ }, why: string): void;
152
+ fire(job: {
153
+ flow: string;
154
+ flowDir: string;
155
+ read: any;
156
+ trigger: Trigger;
157
+ }, cause: {
158
+ kind: "clock";
159
+ slot: number;
160
+ catchUp: boolean;
161
+ missed: number;
162
+ } | {
163
+ kind: "file";
164
+ path: string;
165
+ sha: string;
166
+ }, hooks?: {
167
+ told?: (outcome: string) => boolean;
168
+ beforeStart?: () => void;
169
+ }): Record<string, any>;
170
+ read(flow: string): {
171
+ flow: string;
172
+ trigger: import("../trigger-line.js").Trigger | null;
173
+ flowHash: string;
174
+ armed: boolean;
175
+ armedAt: number | null;
176
+ nextSlotMs: number | null;
177
+ lastOutcome: any;
178
+ hasGreenRun: boolean;
179
+ } | null;
180
+ card(flow: string): {
181
+ trigger: import("../trigger-line.js").Trigger | null;
182
+ words: string | null;
183
+ armed: boolean;
184
+ hasGreenRun: boolean;
185
+ refusal: string | null;
186
+ editNote: string | null;
187
+ } | null;
188
+ };
188
189
  resumePrepare: (body: any) => any;
189
190
  resumeForm: (flow: string | null, runId: string | null) => {
190
191
  status: any;
@@ -194,14 +195,14 @@ export function createAuthor(opts: {
194
195
  /**
195
196
  * `GET /api/author/monthly-check?cap=<usd>`: the note under Cap, one read-only check. `{ text, red }` is what to show (red =
196
197
  * the cap does not fit this month); `floorUsd` is the smallest cap that funds one round of a model step, for the page's
197
- * `needs at least $X per run` line. A cap that is not a number above 0 gets no note. Never spends, never writes.
198
- * @param {unknown} cap @param {string|null} [spent] what a resumed run has already spent (amendment 4 item 4) @param {{ provider?: string, model?: string, flow?: string }|null} [pick] the card's picked model, or (a Resume) the flow whose recorded model it is: its own prices are the floor (M4d am6)
198
+ * floor line (am20: `Cap $X is below one drafting|run round's worst case for <model>, $Y. …`, `kind` picks the word). A cap that is not a number above 0 gets no note. Never spends, never writes.
199
+ * @param {unknown} cap @param {string|null} [spent] what a resumed run has already spent (amendment 4 item 4) @param {{ provider?: string, model?: string, flow?: string }|null} [pick] the card's picked model, or (a Run / Resume) the flow whose recorded model it @param {string} [kind] 'run' for Run a signed flow and Resume, else the Chat card's 'drafting' is: its own prices are the floor (M4d am6)
199
200
  */
200
201
  monthlyCheck(cap: unknown, spent?: string | null, pick?: {
201
202
  provider?: string;
202
203
  model?: string;
203
204
  flow?: string;
204
- } | null): {
205
+ } | null, kind?: string, draftId?: string | null): {
205
206
  status: number;
206
207
  body: {
207
208
  ok: boolean;
@@ -232,6 +233,8 @@ export function createAuthor(opts: {
232
233
  say: string;
233
234
  };
234
235
  };
236
+ /** The starter's "this run's start is in phase `starting`" for the Runs list and a run's page (`startingFor`, the ONE rule). @param {string} flow @param {string} runId */
237
+ startingRun(flow: string, runId: string): boolean;
235
238
  /** `GET /api/author/start/:startId`: the start's phase, read from its files. @param {string} id */
236
239
  startGet(id: string): {
237
240
  status: number;