fwdloop 0.12.0 → 0.13.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 (56) hide show
  1. package/bin/fwdloop +136 -21
  2. package/package.json +1 -1
  3. package/src/ask.js +3 -0
  4. package/src/authoring.js +101 -13
  5. package/src/books.js +23 -2
  6. package/src/canrun.js +36 -0
  7. package/src/config.js +2 -1
  8. package/src/declaration.js +72 -2
  9. package/src/drafter.js +41 -3
  10. package/src/draftspend.js +70 -0
  11. package/src/flow.js +43 -0
  12. package/src/liveness.js +1 -1
  13. package/src/model-step.js +30 -4
  14. package/src/monthly.js +58 -13
  15. package/src/panel/author.js +482 -0
  16. package/src/panel/authorcard.js +297 -0
  17. package/src/panel/authorflows.js +312 -0
  18. package/src/panel/authorresume.js +149 -0
  19. package/src/panel/authorstart.js +257 -0
  20. package/src/panel/authorvalues.js +110 -0
  21. package/src/panel/data.js +228 -34
  22. package/src/panel/index.html +1745 -127
  23. package/src/panel/resume.js +6 -8
  24. package/src/panel/server.js +119 -119
  25. package/src/panel/settings.js +7 -5
  26. package/src/panel/spawn.js +77 -0
  27. package/src/provider.js +12 -17
  28. package/src/runner.js +639 -85
  29. package/src/runvalues.js +178 -0
  30. package/src/send.js +10 -3
  31. package/src/setup.js +120 -0
  32. package/types/authoring.d.ts +24 -2
  33. package/types/books.d.ts +14 -2
  34. package/types/canrun.d.ts +32 -0
  35. package/types/config.d.ts +1 -0
  36. package/types/declaration.d.ts +9 -1
  37. package/types/drafter.d.ts +15 -1
  38. package/types/draftspend.d.ts +21 -0
  39. package/types/flow.d.ts +23 -0
  40. package/types/liveness.d.ts +2 -2
  41. package/types/model-step.d.ts +3 -1
  42. package/types/monthly.d.ts +21 -0
  43. package/types/panel/author.d.ts +335 -0
  44. package/types/panel/authorcard.d.ts +120 -0
  45. package/types/panel/authorflows.d.ts +175 -0
  46. package/types/panel/authorresume.d.ts +32 -0
  47. package/types/panel/authorstart.d.ts +115 -0
  48. package/types/panel/authorvalues.d.ts +62 -0
  49. package/types/panel/data.d.ts +103 -12
  50. package/types/panel/server.d.ts +11 -33
  51. package/types/panel/spawn.d.ts +30 -0
  52. package/types/provider.d.ts +6 -9
  53. package/types/runner.d.ts +120 -5
  54. package/types/runvalues.d.ts +97 -0
  55. package/types/send.d.ts +5 -1
  56. package/types/setup.d.ts +83 -0
package/types/runner.d.ts CHANGED
@@ -1,3 +1,28 @@
1
+ /** The panel's one write of a stop request. `'exists'` = already asked, nothing more to do. @param {string} runDir */
2
+ export function requestStop(runDir: string): "written" | "exists";
3
+ /** The ONE reader of the stop request: `{at}` (when it was asked; null if unreadable) or `null` when none is pending. @param {string} runDir */
4
+ export function readStopRequest(runDir: string): {
5
+ at: string | null;
6
+ } | null;
7
+ /** Is a stop pending for this run? (read-only) @param {string} runDir */
8
+ export function stopPending(runDir: string): boolean;
9
+ /** The one reader of halt.json: `{ok:true, halt}`, `{ok:false, missing:true}` (not resumable), or `{ok:false, red}`. @param {string} runDir */
10
+ export function readHaltRecord(runDir: string): {
11
+ ok: boolean;
12
+ missing: boolean;
13
+ red?: undefined;
14
+ halt?: undefined;
15
+ } | {
16
+ ok: boolean;
17
+ red: string;
18
+ missing?: undefined;
19
+ halt?: undefined;
20
+ } | {
21
+ ok: boolean;
22
+ halt: any;
23
+ missing?: undefined;
24
+ red?: undefined;
25
+ };
1
26
  /** @param {string|null|undefined} modelId */
2
27
  export function resolveCeilingUsd(modelId: string | null | undefined): any;
3
28
  /** @param {{goal:string, primitives?:string[], reads?:Record<string,any>, gap?:string|null}} opts */
@@ -53,13 +78,23 @@ export function freezeInputs(runDir: any, sources: any): {
53
78
  }[];
54
79
  red?: undefined;
55
80
  };
56
- /** The send target's directory must resolve (lexically AND after
57
- * realpath — a symlink inside the repo pointing outside it must not pass)
58
- * inside the repo, and be writable. Re-checked at write time, not just at
59
- * preflight (time-of-check vs time-of-use, M0's own fix).
81
+ /** The send target's folder (M4e amendment 1, "Destination is any folder").
82
+ * An absolute `file:/abs/path` is that folder, anywhere on the machine. A
83
+ * relative path keeps its M0/M2 meaning: joined onto the install folder and
84
+ * fenced inside it (lexically AND after realpath — a symlink inside the repo
85
+ * pointing outside must not pass). Either way, with `realpathSync` (symlinks
86
+ * followed), the folder must exist, be a folder, be writable, and must NOT be
87
+ * the run's own folder (its records and `inputs/`), the panel/CLI flow root, any flow
88
+ * folder under it (one holding a FLOW_FILES file) or `<root>/.drafts` or `<root>/.starts`, or the fwdloop config folder (holds the keys).
89
+ * Called at sign time and re-called at write time (time-of-check vs
90
+ * time-of-use, M0's own fix) — the one rule for both.
60
91
  * @param {string} target
92
+ * @param {{root?: string, runDir?: string}} [ctx] `root` = the flow root, `runDir` = this run's folder
61
93
  * @returns {{ok:true, dir:string} | {ok:false, red:string}} */
62
- export function checkSendDestination(target: string): {
94
+ export function checkSendDestination(target: string, ctx?: {
95
+ root?: string;
96
+ runDir?: string;
97
+ }): {
63
98
  ok: true;
64
99
  dir: string;
65
100
  } | {
@@ -226,6 +261,84 @@ export function resumeRun({ root, name, runId, catalogue, modelStep, sendStep, p
226
261
  ceilingUsd?: number | undefined;
227
262
  nowMs?: (() => number) | undefined;
228
263
  }): Promise<any>;
264
+ /**
265
+ * What a run has spent so far, from its own books (audit.jsonl is the source of truth): the sum of every priced row (a cap-halt
266
+ * row cost nothing and carries no number), and whether any row left the total a floor. Used by `continueRun` and the CLI's hold.
267
+ * @param {string} runDir
268
+ * @returns {{ ok: true, total: number, complete: boolean } | { ok: false, red: string }}
269
+ */
270
+ export function auditSpend(runDir: string): {
271
+ ok: true;
272
+ total: number;
273
+ complete: boolean;
274
+ } | {
275
+ ok: false;
276
+ red: string;
277
+ };
278
+ /**
279
+ * M4e amendment 4 items 4 and 6 (Resume of a stopped or cap-halted run): the SAME run id continues, never a new run. Takes the
280
+ * same exclusive `resume.lock` as an answer-resume (one resume at a time per run), reads `halt.json` (the runner's own record of
281
+ * where it stopped), re-verifies the flow's signature, the frozen inputs and every finished step's artifact, lays the run's
282
+ * highest signed values version over the flow's (`pickRunValues`: the human's new cap lives there, write-once, never in the flow's
283
+ * own files), refuses by name at $0 when that cap is not above what is already spent, consumes `halt.json` by rename (the
284
+ * mutex, like an answer), then re-enters the ONE fold at the first step with no artifact. Done steps are not re-run, spend so
285
+ * far (re-summed from the books) counts against the new cap, the plan is never redrawn.
286
+ *
287
+ * @param {object} opts same shape as `resumeRun`'s
288
+ * @param {string} opts.root
289
+ * @param {string} opts.name
290
+ * @param {string} opts.runId
291
+ * @param {unknown} opts.catalogue
292
+ * @param {(executorContext:object, grantedTools:Record<string,any>, stepMeta?:{class:string|null}) => Promise<any>} opts.modelStep
293
+ * @param {(target:string, filename:string, content:unknown) => Promise<{ok:boolean, red?:string, bytes?:number}>} opts.sendStep
294
+ * @param {Record<string, any>} [opts.primitives]
295
+ * @param {string[]} [opts.primitiveReds]
296
+ * @param {() => string} [opts.clock]
297
+ * @param {string} opts.businessDate
298
+ * @param {number} [opts.ceilingUsd]
299
+ * @param {() => number} [opts.nowMs]
300
+ */
301
+ export function continueRun({ root, name, runId, catalogue, modelStep, sendStep, primitives, primitiveReds, clock, businessDate, ceilingUsd, nowMs, }: {
302
+ root: string;
303
+ name: string;
304
+ runId: string;
305
+ catalogue: unknown;
306
+ modelStep: (executorContext: object, grantedTools: Record<string, any>, stepMeta?: {
307
+ class: string | null;
308
+ }) => Promise<any>;
309
+ sendStep: (target: string, filename: string, content: unknown) => Promise<{
310
+ ok: boolean;
311
+ red?: string;
312
+ bytes?: number;
313
+ }>;
314
+ primitives?: Record<string, any> | undefined;
315
+ primitiveReds?: string[] | undefined;
316
+ clock?: (() => string) | undefined;
317
+ businessDate: string;
318
+ ceilingUsd?: number | undefined;
319
+ nowMs?: (() => number) | undefined;
320
+ }): Promise<any>;
321
+ /**
322
+ * M4e amendment 13: the Stop on a run that already waits at its ask (the panel's Stop door; also the fallback for a request that
323
+ * landed right after the park). Writes the request if none is pending (`requestStop`, its one writer), takes the same exclusive
324
+ * `resume.lock` as a resume, checks the run really is parked on its open, unanswered ask, and stops it at the ask (`stopAtAsk`).
325
+ * Refused by name, writing nothing, for a run that is not waiting at an ask. $0: no model call, no spend.
326
+ * @param {object} opts
327
+ * @param {string} opts.root
328
+ * @param {string} opts.name
329
+ * @param {string} opts.runId
330
+ * @param {unknown} opts.catalogue
331
+ * @param {() => string} [opts.clock]
332
+ * @param {() => number} [opts.nowMs]
333
+ */
334
+ export function stopParkedRun({ root, name, runId, catalogue, clock, nowMs, }: {
335
+ root: string;
336
+ name: string;
337
+ runId: string;
338
+ catalogue: unknown;
339
+ clock?: (() => string) | undefined;
340
+ nowMs?: (() => number) | undefined;
341
+ }): Promise<any>;
229
342
  /**
230
343
  * `writeLog`'s own sibling reader (M4a piece 2, docs/wiki/the-module-
231
344
  * ladder.md M4a scope item 2: "one reader per book") — `null` when the run
@@ -270,3 +383,5 @@ export const MAX_ATTEMPTS: 4;
270
383
  * orders of magnitude below the cheapest real round (audit rows here run
271
384
  * $0.0006+), so nothing a real tamper produces can hide inside it. */
272
385
  export const SPEND_TOLERANCE_USD: 1e-9;
386
+ export const STOP_FILE: "stop.request";
387
+ export const HALT_FILE: "halt.json";
@@ -0,0 +1,97 @@
1
+ /** An ask wait as the signed text spells it (`<int>` then `s`, `m` or `h`), in ms; null when it is not that or not above 0. @param {unknown} text */
2
+ export function parseWaitMs(text: unknown): number | null;
3
+ /** The wait text for a ttl in ms (`2h`, `30m`, `45s`). @param {number} ms */
4
+ export function waitText(ms: number): string;
5
+ /**
6
+ * The values a flow's own signature gives: the cap, the ONE send's destination folder (null when the flow has no single file send, so a
7
+ * destination cannot be added or changed), and each ask's wait by its line number.
8
+ * @param {any} arbiter `readFlow`'s arbiter
9
+ * @returns {{ capUsd: number, destination: string|null, askWaits: Record<string, string> }}
10
+ */
11
+ export function flowValues(arbiter: any): {
12
+ capUsd: number;
13
+ destination: string | null;
14
+ askWaits: Record<string, string>;
15
+ };
16
+ /**
17
+ * The hash the human's click is bound to.
18
+ * @param {{ flow: string, flowSignatureHash: string, runId?: string|null, version: number, values: {capUsd: number, destination: string|null, askWaits: Record<string,string>} }} a
19
+ */
20
+ export function valuesHash({ flow, flowSignatureHash, runId, version, values, }: {
21
+ flow: string;
22
+ flowSignatureHash: string;
23
+ runId?: string | null;
24
+ version: number;
25
+ values: {
26
+ capUsd: number;
27
+ destination: string | null;
28
+ askWaits: Record<string, string>;
29
+ };
30
+ }): string;
31
+ /** True when `a` and `b` are the same values (cap, destination, every wait). */
32
+ export function sameValues(a: any, b: any): boolean;
33
+ /**
34
+ * The ONE writer of a version file: write-once (`wx`), mode 0600. Refuses a record whose hash does not match its own fields.
35
+ * @param {string} runDir
36
+ * @param {{ flow: string, flowSignatureHash: string, runId: string, version: number, values: any, hash: string, signedBy: string, at: string }} record
37
+ * @returns {{ok: true, file: string} | {ok: false, red: string}}
38
+ */
39
+ export function writeRunValues(runDir: string, record: {
40
+ flow: string;
41
+ flowSignatureHash: string;
42
+ runId: string;
43
+ version: number;
44
+ values: any;
45
+ hash: string;
46
+ signedBy: string;
47
+ at: string;
48
+ }): {
49
+ ok: true;
50
+ file: string;
51
+ } | {
52
+ ok: false;
53
+ red: string;
54
+ };
55
+ /**
56
+ * The ONE decision "which signed version does this run use": the highest version file in the run folder.
57
+ * `{ok:true, none:true}` = the flow's own values; `{ok:true, version, record}`; `{ok:false, red}` = a file that cannot be trusted
58
+ * (unreadable, not JSON, wrong shape, a hash that does not match its fields, a name that disagrees with its version).
59
+ * @param {string} runDir
60
+ * @returns {{ok: true, none: true} | {ok: true, none?: false, version: number, record: any} | {ok: false, red: string}}
61
+ */
62
+ export function pickRunValues(runDir: string): {
63
+ ok: true;
64
+ none: true;
65
+ } | {
66
+ ok: true;
67
+ none?: false;
68
+ version: number;
69
+ record: any;
70
+ } | {
71
+ ok: false;
72
+ red: string;
73
+ };
74
+ /**
75
+ * The arbiter a run executes under: the flow's own, with the picked record's cap, send folder and ask waits laid over it. The flow's
76
+ * arbiter object is never mutated. A record for another flow or a changed flow signature is refused; so is a record that names a send
77
+ * or an ask the flow does not have.
78
+ * @param {any} arbiter `readFlow`'s arbiter @param {string} flow @param {string} flowSignatureHash
79
+ * @param {ReturnType<typeof pickRunValues>} picked
80
+ * @returns {{ok: true, arbiter: any, version: number|null} | {ok: false, red: string}}
81
+ */
82
+ export function applyRunValues(arbiter: any, flow: string, flowSignatureHash: string, picked: ReturnType<typeof pickRunValues>): {
83
+ ok: true;
84
+ arbiter: any;
85
+ version: number | null;
86
+ } | {
87
+ ok: false;
88
+ red: string;
89
+ };
90
+ /**
91
+ * A start that refused before the run began (monthly limit, empty key ...) leaves a run folder holding nothing but its signed values
92
+ * file: remove that one file so the folder can be removed too and the run name is not burned. Never touches a folder with anything else.
93
+ * @param {string} runDir @returns {boolean} true when the file was removed
94
+ */
95
+ export function dropUnstartedValues(runDir: string): boolean;
96
+ export const VALUES_FILE_RE: RegExp;
97
+ export function valuesFileName(version: number): string;
package/types/send.d.ts CHANGED
@@ -5,9 +5,13 @@
5
5
  * @param {string|null} [acceptedSha256] - the sha256 the human's accept recorded
6
6
  * (`answerAsk`, M4b piece 3). Required: none recorded (an answer written by an
7
7
  * older version, or no accept in this process) refuses by name.
8
+ * @param {{root?: string, runDir?: string}} [ctx] the flow root and this run's folder, for the destination refusals
8
9
  * @returns {Promise<{ok:true, path:string, bytes:number} | {ok:false, red:string}>}
9
10
  */
10
- export function sendViaPrimitive(target: string, filename: string, content: unknown, acceptedSha256?: string | null): Promise<{
11
+ export function sendViaPrimitive(target: string, filename: string, content: unknown, acceptedSha256?: string | null, ctx?: {
12
+ root?: string;
13
+ runDir?: string;
14
+ }): Promise<{
11
15
  ok: true;
12
16
  path: string;
13
17
  bytes: number;
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The rows of a draft's record, oldest first. `sessionDir` is the panel's draft folder (card.json, draft/, note-<n>.txt, draft-<n>/); a CLI
3
+ * draft has none, so its record is its one plan folder (`planDir`, read as `draft`) plus the sign.
4
+ * @param {{ sessionDir?: string, planDir: string, hash: string, signedBy: string, signedAt: string, flowHash?: string }} a
5
+ */
6
+ export function buildSetupRows({ sessionDir, planDir, hash, signedBy, signedAt, flowHash, }: {
7
+ sessionDir?: string;
8
+ planDir: string;
9
+ hash: string;
10
+ signedBy: string;
11
+ signedAt: string;
12
+ flowHash?: string;
13
+ }): ({
14
+ kind: any;
15
+ n: any;
16
+ at: any;
17
+ model: any;
18
+ costUsd: any;
19
+ spendComplete: boolean;
20
+ calls: any;
21
+ wallMs: any;
22
+ structureRetries: any;
23
+ verdict: string;
24
+ hash: string | null;
25
+ gap: string | null;
26
+ } | {
27
+ kind: string;
28
+ n: number;
29
+ at: string | null;
30
+ card: any;
31
+ text?: undefined;
32
+ signedBy?: undefined;
33
+ hash?: undefined;
34
+ flowHash?: undefined;
35
+ } | {
36
+ kind: string;
37
+ n: number;
38
+ at: string | null;
39
+ text: string;
40
+ card?: undefined;
41
+ signedBy?: undefined;
42
+ hash?: undefined;
43
+ flowHash?: undefined;
44
+ } | {
45
+ kind: string;
46
+ n: number;
47
+ at: string;
48
+ signedBy: string;
49
+ hash: string;
50
+ flowHash: string | null;
51
+ card?: undefined;
52
+ text?: undefined;
53
+ })[];
54
+ /**
55
+ * Write `<flowDir>/setup.jsonl` ONCE ('wx': a second write is refused, never merged or replaced), mode 0600, every row scrubbed of the
56
+ * given key values. Never throws.
57
+ * @param {{ flowDir: string, secrets?: string[] } & Parameters<typeof buildSetupRows>[0]} a
58
+ * @returns {{ ok: true, file: string, rows: number } | { ok: false, red: string }}
59
+ */
60
+ export function writeSetup({ flowDir, secrets, ...rest }: {
61
+ flowDir: string;
62
+ secrets?: string[];
63
+ } & Parameters<typeof buildSetupRows>[0]): {
64
+ ok: true;
65
+ file: string;
66
+ rows: number;
67
+ } | {
68
+ ok: false;
69
+ red: string;
70
+ };
71
+ /**
72
+ * The flow's setup rows, through the one safe gateway. `present:false` = a flow signed before amendment 6 (or a file that cannot be read).
73
+ * @param {string} flowDir
74
+ * @returns {{ present: false } | { present: true, rows: any[], unreadable: number }}
75
+ */
76
+ export function readSetup(flowDir: string): {
77
+ present: false;
78
+ } | {
79
+ present: true;
80
+ rows: any[];
81
+ unreadable: number;
82
+ };
83
+ export const SETUP_FILE: "setup.jsonl";