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
@@ -152,8 +152,18 @@ export function capFloorFor(hasRoundStep: boolean, home?: string, pick?: {
152
152
  provider?: string;
153
153
  model?: string;
154
154
  } | null): number;
155
- /** The one sentence for a cap under the floor (red under Cap, and the Draft / sign refusal). @param {number} floorUsd */
156
- export function capFloorText(floorUsd: number): string;
155
+ /** The floor and the model id it was computed for (M4d am20: the refusal sentence names it). `{ usd: 0, model: null }` when there is nothing to hold. @param {boolean} hasRoundStep @param {string} [home] @param {{ slot?: string, provider?: string, model?: string }|null} [pick] */
156
+ export function capFloorOf(hasRoundStep: boolean, home?: string, pick?: {
157
+ slot?: string;
158
+ provider?: string;
159
+ model?: string;
160
+ } | null): {
161
+ usd: number;
162
+ model: null;
163
+ } | {
164
+ usd: number;
165
+ model: string;
166
+ };
157
167
  /**
158
168
  * Amendment 1's destination checks, in ONE place: the New job card and the per-run destination of amendment 5 both ask it. `null` = fine,
159
169
  * else the sentence (a folder that is the run folder, a flow folder, the root, `.drafts`, `.starts`, the config folder, a file, a missing
@@ -16,7 +16,7 @@ export function formFacts(read: any, home?: string, flowDir?: string, picked?: {
16
16
  jobLines: any;
17
17
  hasSend: boolean;
18
18
  capFloorUsd: number;
19
- floorText: string;
19
+ floorModel: string | null;
20
20
  model: {
21
21
  slot: string;
22
22
  model: string;
@@ -44,6 +44,14 @@ export function createFlowsDoor(opts: {
44
44
  }): {
45
45
  /** M6 amendments 1 and 3: the sentence that says why this flow cannot be edited now, or null (the sign route's check). @param {string} name */
46
46
  editBlock(name: string): string | null;
47
+ /** M9: the source paths the newest run of this job used, role -> path (blank = none), for a start a trigger fires. @param {string} name @param {string[]} roles */
48
+ lastSourcesOf(name: string, roles: string[]): {
49
+ runId: null;
50
+ sources: Record<string, string>;
51
+ } | {
52
+ runId: string;
53
+ sources: Record<string, string>;
54
+ };
47
55
  /** `GET /api/author/flows`. */
48
56
  flows(): {
49
57
  status: number;
@@ -82,7 +90,7 @@ export function createFlowsDoor(opts: {
82
90
  jobLines: any;
83
91
  hasSend: boolean;
84
92
  capFloorUsd: number;
85
- floorText: string;
93
+ floorModel: string | null;
86
94
  model: {
87
95
  slot: string;
88
96
  model: string;
@@ -169,7 +177,7 @@ export function createFlowsDoor(opts: {
169
177
  jobLines: any;
170
178
  hasSend: boolean;
171
179
  capFloorUsd: number;
172
- floorText: string;
180
+ floorModel: string | null;
173
181
  model: {
174
182
  slot: string;
175
183
  model: string;
@@ -14,19 +14,21 @@ export function createStarter(opts: {
14
14
  runDir: string;
15
15
  say?: undefined;
16
16
  };
17
- startingFor: (flow: string, keys: string[]) => string | null;
17
+ startingFor: (flow: string, keys: string[], runId?: string) => string | null;
18
18
  /**
19
19
  * Start `fwdloop run` for a flow whose every $0 check already passed at the calling door. Re-checks the run id here (nothing
20
20
  * awaits between this check and the spawn). 202 { startId, runId } or a refusal; a refusal creates nothing.
21
21
  * `runId` '' = no typed id: the start CLAIMS the next `run-<n>` itself (claimRunId, folder created exclusively; amendment 3).
22
22
  * `values` (M4e amendment 5 item 3): the human's signed values for THIS run (the page's second click), written once into the run's own
23
23
  * folder as `signed-values.json` BEFORE the spawn; the run then uses them (`pickRunValues`). None = the flow's own.
24
- * @param {{ kind: 'run'|'sign', flow: string, runId: string, sources: {role: string, path: string}[], values?: any, pick?: {slot: string, model: string}|null }} a
24
+ * `trigger` (M9): `{kind, reason, catchUp, ...}` for a start a trigger fired (kind 'trigger'): written once into the run's own folder as `trigger.json`
25
+ * BEFORE the spawn; `appendHistory` copies it into the run's history row (the catch-up mark, am2 item 1).
26
+ * @param {{ kind: 'run'|'sign'|'trigger', flow: string, runId: string, sources: {role: string, path: string}[], values?: any, pick?: {slot: string, model: string}|null, trigger?: Record<string, any>|null }} a
25
27
  * @param {{ env: Record<string, string|undefined>, keyNames?: string[] }} loaded the keys load: its env is the child's env AND (with its key names) the scrub list
26
28
  * @returns {{ status: number, body: any }}
27
29
  */
28
- start({ kind, flow, runId, sources, values, pick, }: {
29
- kind: "run" | "sign";
30
+ start({ kind, flow, runId, sources, values, pick, trigger, }: {
31
+ kind: "run" | "sign" | "trigger";
30
32
  flow: string;
31
33
  runId: string;
32
34
  sources: {
@@ -38,6 +40,7 @@ export function createStarter(opts: {
38
40
  slot: string;
39
41
  model: string;
40
42
  } | null;
43
+ trigger?: Record<string, any> | null;
41
44
  }, loaded: {
42
45
  env: Record<string, string | undefined>;
43
46
  keyNames?: string[];
@@ -0,0 +1,210 @@
1
+ /** The folder holding the ledgers of ONE signed trigger. @param {string} flowDir @param {string} flowHash */
2
+ export function triggerLedgerDir(flowDir: string, flowHash: string): string;
3
+ /** Every fire outcome of a job, oldest first, through the safe gateway. @param {string} flowDir @returns {any[]} */
4
+ export function readOutcomes(flowDir: string): any[];
5
+ /** The ONE writer of `triggers.jsonl`: append-only, one line per fire outcome. @param {string} flowDir @param {Record<string, any>} row */
6
+ export function appendOutcome(flowDir: string, row: Record<string, any>): void;
7
+ /**
8
+ * M9 item 1 and 2: does this job have "one green run"? The ONE function that decides it. A passed run counts when the job it ran (its own
9
+ * `job/` copy) is the current job WITHOUT its trigger line: so changing only the trigger keeps the run (item 2), and an Edit that changes
10
+ * the steps, a guardrail or the cap does not (am1). A run from before runs kept a job copy has none to compare, so it counts only when its
11
+ * history row's signature is the job's current one (a job signed since then is not the job it ran, unless that signing only added or changed its trigger line: see signingsOf).
12
+ * @param {string} flowDir
13
+ */
14
+ export function hasGreenRunForJob(flowDir: string): boolean;
15
+ /**
16
+ * The ONE writer of "this trigger was set": the starting point (am2 item 2). Idempotent. A file kind records what is already there first
17
+ * (never runs), then the armed row, so a failure between the two leaves it unarmed and the next call finishes it. Throws, naming why, when
18
+ * the target is not usable now.
19
+ * @param {string} flowDir @param {string} flowHash @param {Trigger} trigger @param {number} now epoch ms
20
+ * @returns {{armed: boolean, already: boolean}}
21
+ */
22
+ export function armFor(flowDir: string, flowHash: string, trigger: Trigger, now: number): {
23
+ armed: boolean;
24
+ already: boolean;
25
+ };
26
+ /**
27
+ * @param {{ root: string, flowsDoor: ReturnType<typeof import('./authorflows.js').createFlowsDoor>, starter: ReturnType<typeof import('./authorstart.js').createStarter>, loadEnv: () => any, monthlyClaim: (usd: number) => any, modelRefusal: (card: any, loaded: any) => { field: string, say: string }[], now?: () => number }} opts
28
+ */
29
+ export function createTriggerDoor(opts: {
30
+ root: string;
31
+ flowsDoor: ReturnType<typeof import("./authorflows.js").createFlowsDoor>;
32
+ starter: ReturnType<typeof import("./authorstart.js").createStarter>;
33
+ loadEnv: () => any;
34
+ monthlyClaim: (usd: number) => any;
35
+ modelRefusal: (card: any, loaded: any) => {
36
+ field: string;
37
+ say: string;
38
+ }[];
39
+ now?: () => number;
40
+ }): {
41
+ inspect: (body: any) => {
42
+ ok: false;
43
+ reply: {
44
+ status: number;
45
+ body: {
46
+ ok: boolean;
47
+ };
48
+ };
49
+ } | {
50
+ ok: true;
51
+ flow: any;
52
+ read: import("../types.js").ReadFlowOk;
53
+ trigger: import("../trigger-line.js").Trigger | null;
54
+ flowDir: string;
55
+ realRoot: string;
56
+ proseText: string;
57
+ declaration: any;
58
+ hash: string;
59
+ };
60
+ now: () => number;
61
+ /** `POST /api/author/trigger-prepare`: the first click. Checks, signs and writes nothing. @param {any} body */
62
+ prepare(body: any): {
63
+ status: number;
64
+ body: {
65
+ ok: boolean;
66
+ };
67
+ } | {
68
+ status: number;
69
+ body: {
70
+ ok: boolean;
71
+ flow: any;
72
+ trigger: import("../trigger-line.js").Trigger | null;
73
+ words: string | null;
74
+ hash: string;
75
+ };
76
+ };
77
+ /**
78
+ * `POST /api/author/trigger`: the second click, carrying the hash it was shown. Re-signs the SAME plan (declaration.json byte for byte,
79
+ * prose.txt with only its trigger line changed) through the M6 replace path, then arms the trigger. $0: no drafter, no run.
80
+ * @param {any} body
81
+ */
82
+ set(body: any): {
83
+ status: number;
84
+ body: {
85
+ ok: boolean;
86
+ };
87
+ } | {
88
+ status: number;
89
+ body: {
90
+ ok: boolean;
91
+ refused: string;
92
+ say: string;
93
+ reds?: undefined;
94
+ };
95
+ } | {
96
+ status: number;
97
+ body: {
98
+ ok: boolean;
99
+ refused: string;
100
+ say: string;
101
+ reds: any[];
102
+ };
103
+ } | {
104
+ status: number;
105
+ body: {
106
+ say?: string | undefined;
107
+ ok: boolean;
108
+ flow: any;
109
+ trigger: import("../trigger-line.js").Trigger | null;
110
+ words: string | null;
111
+ flowHash: string;
112
+ armed: boolean;
113
+ refused?: undefined;
114
+ reds?: undefined;
115
+ };
116
+ };
117
+ /** Every signed job that has a trigger, as the scheduler needs it: `{flow, flowDir, read, trigger}`. Reads each job the way the CLI will (signature verified). */
118
+ scan(): {
119
+ flow: string;
120
+ flowDir: string;
121
+ read: import("../types.js").ReadFlowOk;
122
+ trigger: Trigger;
123
+ }[];
124
+ /** Arm this job's signed trigger if it is not armed yet (a crash between the sign and the arm). Idempotent; throws naming why. @param {{flowDir: string, read: any, trigger: Trigger}} job */
125
+ ensureArmed(job: {
126
+ flowDir: string;
127
+ read: any;
128
+ trigger: Trigger;
129
+ }): {
130
+ armed: boolean;
131
+ already: boolean;
132
+ };
133
+ /** M9 am3: is a run of this job starting, running or waiting at an ask? (the same check `fire` makes first) @param {{flow: string}} job */
134
+ busy(job: {
135
+ flow: string;
136
+ }): boolean;
137
+ /** M9 am3: one row for a queued file that is not a fire (deleted / changed before its turn). @param {{flow: string, flowDir: string, trigger: Trigger}} job @param {Record<string, any>} row */
138
+ note(job: {
139
+ flow: string;
140
+ flowDir: string;
141
+ trigger: Trigger;
142
+ }, row: Record<string, any>): void;
143
+ /**
144
+ * One row in `triggers.jsonl` that is not a fire (the trigger could not be watched). @param {{flow: string, flowDir: string, trigger: Trigger}} job @param {string} why
145
+ */
146
+ refuse(job: {
147
+ flow: string;
148
+ flowDir: string;
149
+ trigger: Trigger;
150
+ }, why: string): void;
151
+ /**
152
+ * M9 item 3-5: one fire, in this order, each outcome ONE row in `triggers.jsonl`: busy -> monthly-exhausted -> refused (an input or the keys
153
+ * are missing) -> started through the ONE start path (kind `trigger`; the reason and the catch-up mark go into the run's own folder first).
154
+ * The fire / ran ledger row was written by the caller BEFORE this, so a crash loses a run and never doubles one.
155
+ * @param {{flow: string, flowDir: string, read: any, trigger: Trigger}} job
156
+ * @param {{kind: 'clock', slot: number, catchUp: boolean, missed: number} | {kind: 'file', path: string, sha: string}} cause
157
+ * @param {{ told?: (outcome: string) => boolean, beforeStart?: () => void }} [hooks] M9 am3 (a queued file): `told(outcome)` true = this file
158
+ * already has a row for that outcome, so none is written again (the outcome is still returned); `beforeStart` runs right before the ONE
159
+ * start, once every check has passed: the scheduler writes the file's `ran` row there.
160
+ */
161
+ fire(job: {
162
+ flow: string;
163
+ flowDir: string;
164
+ read: any;
165
+ trigger: Trigger;
166
+ }, cause: {
167
+ kind: "clock";
168
+ slot: number;
169
+ catchUp: boolean;
170
+ missed: number;
171
+ } | {
172
+ kind: "file";
173
+ path: string;
174
+ sha: string;
175
+ }, hooks?: {
176
+ told?: (outcome: string) => boolean;
177
+ beforeStart?: () => void;
178
+ }): Record<string, any>;
179
+ /**
180
+ * What a card shows of a job's trigger, as data (no words): the signed trigger, whether it is armed, the next slot (clock kinds), the last
181
+ * fire outcome. `null` when the job is not a readable signed job.
182
+ * @param {string} flow
183
+ */
184
+ read(flow: string): {
185
+ flow: string;
186
+ trigger: import("../trigger-line.js").Trigger | null;
187
+ flowHash: string;
188
+ armed: boolean;
189
+ armedAt: number | null;
190
+ nextSlotMs: number | null;
191
+ lastOutcome: any;
192
+ hasGreenRun: boolean;
193
+ } | null;
194
+ /**
195
+ * What the Run card's dropdown needs of one job (M9 piece 5): the typed trigger, its words, whether it is armed, and, when the job has had
196
+ * no good run, the server's refusal sentence (the page shows it and disables the dropdown; it invents no words). `editNote` is am1's
197
+ * warning, set only when the job has a trigger. `null` when the job is not a readable signed job.
198
+ * @param {string} flow
199
+ */
200
+ card(flow: string): {
201
+ trigger: import("../trigger-line.js").Trigger | null;
202
+ words: string | null;
203
+ armed: boolean;
204
+ hasGreenRun: boolean;
205
+ refusal: string | null;
206
+ editNote: string | null;
207
+ } | null;
208
+ };
209
+ export const NO_GREEN_SAY: "This job has not had a good run yet. Run it once by hand and get it right first; then you can set when it runs. Nothing was changed.";
210
+ export type Trigger = import("../trigger-line.js").Trigger;
@@ -11,6 +11,7 @@ export function leastResumeCapUsd(spentUsd: number, floorUsd: number): number;
11
11
  * @param {{capUsd: number, destination: string|null, askWaits: Record<string,string>}} a.base the values in force now (the flow's own, or the run's newest signed version)
12
12
  * @param {string} a.realRoot the flows root (real path)
13
13
  * @param {boolean} a.capOnly Resume: destination and waits are never read from the body
14
+ * @param {string|null} [a.floorModel] the model id the floor was computed for (named in the refusal, M4d am20)
14
15
  * @param {number} a.floorUsd the smallest cap that funds one round of a step (amendment 4 item 2); 0 = none
15
16
  * @param {number} [a.spentUsd] what the run has already spent (Resume): the cap must be above it
16
17
  * @param {boolean} [a.always] Resume: the floor and the monthly check always apply. A Run applies them only when its cap differs from the
@@ -18,7 +19,7 @@ export function leastResumeCapUsd(spentUsd: number, floorUsd: number): number;
18
19
  * @param {(remainingUsd: number) => null | { problem: string } | { ok: boolean, room: any }} a.monthlyClaim the one monthly check
19
20
  * @returns {{ ok: true, values: {capUsd: number, destination: string|null, askWaits: Record<string,string>}, differs: boolean, remainingUsd: number } | { ok: false, status: number, refusals: {field: string, say: string}[], refused?: string, say?: string }}
20
21
  */
21
- export function checkValues({ allowedKeys, body, base, realRoot, capOnly, floorUsd, spentUsd, monthlyClaim, always, }: {
22
+ export function checkValues({ allowedKeys, body, base, realRoot, capOnly, floorUsd, floorModel, spentUsd, monthlyClaim, always, }: {
22
23
  allowedKeys: string[];
23
24
  body: any;
24
25
  base: {
@@ -28,6 +29,7 @@ export function checkValues({ allowedKeys, body, base, realRoot, capOnly, floorU
28
29
  };
29
30
  realRoot: string;
30
31
  capOnly: boolean;
32
+ floorModel?: string | null | undefined;
31
33
  floorUsd: number;
32
34
  spentUsd?: number | undefined;
33
35
  always?: boolean | undefined;
@@ -173,12 +173,13 @@ export function glyphPulses(g: {
173
173
  * - `[II]` stuck (M4c amendment 2, `isStuck`) — the answer is saved, not taken,
174
174
  * and no live process (pid row or resume lock holder) carries the run on.
175
175
  * A saved answer with a live process is `[▶]` working on your answer.
176
+ * - `[▶]` starting (live-walk fix) — no end row, no open ask, no pid row yet, and the starter reads this run's start folder as phase `starting` (`ctx.starting`).
176
177
  * - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
177
178
  * consumed, the process is gone, no end row: cannot be carried on.
178
- * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean, stoppedAtAsk?: string|null, jobEdited?: boolean}} ctx
179
+ * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean, stoppedAtAsk?: string|null, jobEdited?: boolean, starting?: boolean}} ctx
179
180
  * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]'|'[■]', label: string}}
180
181
  */
181
- export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk, jobEdited, }: {
182
+ export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, stoppedAtAsk, jobEdited, starting, }: {
182
183
  historyRow: any;
183
184
  askJson: any;
184
185
  consumedAnswerExists: boolean;
@@ -190,6 +191,7 @@ export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasSta
190
191
  resumable?: boolean;
191
192
  stoppedAtAsk?: string | null;
192
193
  jobEdited?: boolean;
194
+ starting?: boolean;
193
195
  }): {
194
196
  glyph: "[\u2713]" | "[\u2717]" | "[\u00B7]" | "[!]" | "[\u25B6]" | "[?]" | "[II]" | "[\u25A0]";
195
197
  label: string;
@@ -442,14 +444,15 @@ export function getRunControls({ root, flow, runId, catalogue, }: {
442
444
  * null`, an explicit `why`) — no flow is silently missing from the list.
443
445
  * Ordered by `orderRuns` (waiting on you, running, then newest `at` first; a row
444
446
  * with no `at` is never given a guessed one).
445
- * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
446
- * `resumeAttempt`: the panel's in-memory resume-attempt lookup (M4b piece 2); absent = none.
447
+ * @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean}} opts
448
+ * `startingRun`: the starter's phase-`starting` rule for one run (absent = no). `resumeAttempt`: the panel's in-memory resume-attempt lookup (M4b piece 2); absent = none.
447
449
  * @returns {any[]}
448
450
  */
449
- export function listRuns({ root, catalogue, resumeAttempt }: {
451
+ export function listRuns({ root, catalogue, resumeAttempt, startingRun }: {
450
452
  root: string;
451
453
  catalogue: any;
452
454
  resumeAttempt?: (flow: string, runId: string) => any;
455
+ startingRun?: (flow: string, runId: string) => boolean;
453
456
  }): any[];
454
457
  /**
455
458
  * The ONE ordering of Runs — History and Workflows both render the list in the
@@ -471,15 +474,19 @@ export function orderRuns(rows: any[]): any[];
471
474
  * `log.json`. `null` when the flow/runId doesn't resolve to a real run
472
475
  * directory at all (caller renders 404); a `red` field names why a
473
476
  * sub-derivation couldn't fill (never invented).
474
- * @param {{root: string, flow: string, runId: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
477
+ * @param {{root: string, flow: string, runId: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any, startingRun?: (flow: string, runId: string) => boolean, triggerRead?: (flow: string) => any, now?: () => number, home?: string}} opts
475
478
  * @returns {any|null}
476
479
  */
477
- export function getRunDetail({ root, flow, runId, catalogue, resumeAttempt, }: {
480
+ export function getRunDetail({ root, flow, runId, catalogue, resumeAttempt, startingRun, triggerRead, now, home, }: {
478
481
  root: string;
479
482
  flow: string;
480
483
  runId: string;
481
484
  catalogue: any;
482
485
  resumeAttempt?: (flow: string, runId: string) => any;
486
+ startingRun?: (flow: string, runId: string) => boolean;
487
+ triggerRead?: (flow: string) => any;
488
+ now?: () => number;
489
+ home?: string;
483
490
  }): any | null;
484
491
  /**
485
492
  * What the Draft adds up to, ONE place (the Map box, the first step card and the Audit group all read it, so the three can never disagree).
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param {{ door: ReturnType<typeof import('./authortrigger.js').createTriggerDoor>, now?: () => number, intervalMs?: number }} opts
3
+ * `intervalMs` 0 = no timer (a test drives `tick`).
4
+ */
5
+ export function createScheduler(opts: {
6
+ door: ReturnType<typeof import("./authortrigger.js").createTriggerDoor>;
7
+ now?: () => number;
8
+ intervalMs?: number;
9
+ }): {
10
+ tick: () => void;
11
+ /** Start the real timer (idempotent). */
12
+ start(): void;
13
+ /** Stop it (idempotent). The server's close calls this. */
14
+ stop(): void;
15
+ };
@@ -4,14 +4,16 @@
4
4
  * real listening socket where that is simpler.
5
5
  * @param {import('node:http').IncomingMessage} req
6
6
  * @param {import('node:http').ServerResponse} res
7
- * @param {{ root: string, port: number, resumer: ReturnType<typeof createResumer>, settings: ReturnType<typeof createSettings>, author: ReturnType<typeof createAuthor> }} opts
7
+ * @param {{ root: string, port: number, packagePath?: string, resumer: ReturnType<typeof createResumer>, settings: ReturnType<typeof createSettings>, author: ReturnType<typeof createAuthor>, now?: () => number }} opts
8
8
  */
9
9
  export function handleRequest(req: import("node:http").IncomingMessage, res: import("node:http").ServerResponse, opts: {
10
10
  root: string;
11
11
  port: number;
12
+ packagePath?: string;
12
13
  resumer: ReturnType<typeof createResumer>;
13
14
  settings: ReturnType<typeof createSettings>;
14
15
  author: ReturnType<typeof createAuthor>;
16
+ now?: () => number;
15
17
  }): void;
16
18
  /**
17
19
  * Start the panel server. Binds `127.0.0.1` ONLY. A taken port is a LOUD,
@@ -19,13 +21,14 @@ export function handleRequest(req: import("node:http").IncomingMessage, res: imp
19
21
  * header). Resolves once actually listening; rejects on a bind error
20
22
  * (including `EADDRINUSE`) with a `.port` field on the error for the
21
23
  * caller's message.
22
- * @param {{ port?: number, root: string, settings?: { home?: string, env?: Record<string,string|undefined>, fetch?: typeof fetch, now?: () => number, openFolderSpawn?: Function, platform?: string }, resume?: { env?: Record<string,string|undefined>, bin?: string, logDir?: string, maxTries?: number, windowMs?: number, slotMs?: number }, author?: { env?: Record<string,string|undefined>, loadEnv?: () => any, bin?: string } }} opts
24
+ * @param {{ port?: number, root: string, packagePath?: string, settings?: { home?: string, env?: Record<string,string|undefined>, fetch?: typeof fetch, now?: () => number, openFolderSpawn?: Function, platform?: string }, resume?: { env?: Record<string,string|undefined>, bin?: string, logDir?: string, maxTries?: number, windowMs?: number, slotMs?: number }, author?: { env?: Record<string,string|undefined>, loadEnv?: () => any, bin?: string, now?: () => number }, scheduler?: { intervalMs?: number } }} opts
23
25
  * `resume` and `author` are for tests only (a fake env/bin, a short retry window); the CLI passes none.
24
- * @returns {Promise<{ server: import('node:http').Server, port: number, close: () => Promise<void> }>}
26
+ * @returns {Promise<{ server: import('node:http').Server, port: number, tick: () => void, close: () => Promise<void> }>}
25
27
  */
26
28
  export function createPanelServer(opts: {
27
29
  port?: number;
28
30
  root: string;
31
+ packagePath?: string;
29
32
  settings?: {
30
33
  home?: string;
31
34
  env?: Record<string, string | undefined>;
@@ -46,10 +49,15 @@ export function createPanelServer(opts: {
46
49
  env?: Record<string, string | undefined>;
47
50
  loadEnv?: () => any;
48
51
  bin?: string;
52
+ now?: () => number;
53
+ };
54
+ scheduler?: {
55
+ intervalMs?: number;
49
56
  };
50
57
  }): Promise<{
51
58
  server: import("node:http").Server;
52
59
  port: number;
60
+ tick: () => void;
53
61
  close: () => Promise<void>;
54
62
  }>;
55
63
  /**
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @param {unknown} raw one raw refusal text
3
+ * @returns {{ say: string, reason: string, fix: string, raw: string }}
4
+ */
5
+ export function plainPlanCheck(raw: unknown): {
6
+ say: string;
7
+ reason: string;
8
+ fix: string;
9
+ raw: string;
10
+ };
11
+ /** @type {ReadonlyArray<{re: RegExp, say: string|null, fix: string}>} `say` null = the checker's own sentence, as it is without its "declaration: " prefix */
12
+ export const PLAN_CHECK_RULES: ReadonlyArray<{
13
+ re: RegExp;
14
+ say: string | null;
15
+ fix: string;
16
+ }>;
17
+ export const GENERIC_SAY: "One of the checks on the plan failed.";
18
+ export const GENERIC_FIX: "Draft again.";
19
+ export function pageRules(): {
20
+ re: string;
21
+ say: string | null;
22
+ }[];
@@ -50,10 +50,4 @@ export class GateRefusal extends Error {
50
50
  path: any;
51
51
  rule: any;
52
52
  }
53
- /** The verbs this piece actually implements (the switch below, as data) —
54
- * one writer for "is this verb wired": `resolvePrimitives`'s own reds use
55
- * it, and `src/runner.js`'s preflight (F46: a step granted a
56
- * catalogue-present-but-unwired verb, e.g. litectx's `compress`, must
57
- * refuse before any model call or spend) imports this SAME set rather than
58
- * keeping a second copy of the list. */
59
53
  export const WIRED_VERBS: Set<string>;
@@ -164,6 +164,9 @@ export function sumMeterings(events: any): {
164
164
  /** Append one spend row, stamping `modelMatch` — the one writer for
165
165
  * spend.jsonl. Warns (never throws) on a `substituted` model. */
166
166
  export function appendSpendRow(path: any, row: any): void;
167
+ export function rowCeilingUsd(row: any): number;
168
+ /** How many calls of an incomplete row were never metered (at least one). @param {any} row */
169
+ export function unmeteredCalls(row: any): number;
167
170
  /**
168
171
  * The one rule for what a spend row costs: `usd` and whether it is `complete`. An incomplete row (a call
169
172
  * died unmetered, or a round was left unpriced) counts its priced floor PLUS one ceiling per unmetered call
@@ -243,9 +246,12 @@ export function makeProvider(slotName: string, options?: {
243
246
  source: any;
244
247
  };
245
248
  };
246
- export function isKeyName(v: any): boolean;
249
+ export { isKeyName };
250
+ /** The sentence part every refusal of a non-key name carries, so the rule is named wherever it is refused. */
251
+ export const KEY_NAME_RULE_SAY: "a key name is written in capitals and ends in _KEY";
247
252
  export const CEILING_INPUT_TOKENS: 32000;
248
253
  export const CEILING_OUTPUT_TOKENS: 16000;
249
254
  /** A model that is not usable (`resolveUsableModel`): its message is the plain sentence, shown as it is (no "key:" or "config:" prefix). */
250
255
  export class ModelRefusal extends Error {
251
256
  }
257
+ import { isKeyName } from './keysfile.js';
@@ -0,0 +1,11 @@
1
+ /** The folder holding THIS run's store. A different run dir is a different folder.
2
+ * @param {string} runDir */
3
+ export function runStoreDir(runDir: string): string;
4
+ /** Open this run's store, run `fn(lc)`, close it. `root` is the run dir: litectx indexes
5
+ * only its own extensions (.md .js .py ...) there, never the run's json/jsonl records.
6
+ * @template T @param {string} runDir @param {(lc: any) => Promise<T>|T} fn @returns {Promise<T>} */
7
+ export function withRunStore<T>(runDir: string, fn: (lc: any) => Promise<T> | T): Promise<T>;
8
+ /** A run STARTS with an empty store, never an inherited one: a run id can be reused (the next id is max+1, so deleting the last run
9
+ * folder hands its id to the next run) and the store sits beside the run folder, not in it. Called by the one place a fresh run
10
+ * begins (`runFlow`); resume and continue keep the run's own store. @param {string} runDir */
11
+ export function resetRunStore(runDir: string): void;
@@ -89,7 +89,7 @@ export function applyRunValues(arbiter: any, flow: string, flowSignatureHash: st
89
89
  };
90
90
  /**
91
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.
92
+ * file and/or its trigger record: remove those so the folder can be removed too and the run name is not burned. Never touches a folder with anything else.
93
93
  * @param {string} runDir @returns {boolean} true when the file was removed
94
94
  */
95
95
  export function dropUnstartedValues(runDir: string): boolean;
package/types/setup.d.ts CHANGED
@@ -11,6 +11,14 @@ export function buildSetupRows({ sessionDir, planDir, hash, signedBy, signedAt,
11
11
  signedAt: string;
12
12
  flowHash?: string;
13
13
  }): ({
14
+ tries?: {
15
+ label: string;
16
+ passed: boolean;
17
+ reds: string[];
18
+ outcome: string;
19
+ rawOutcome: string;
20
+ again: boolean;
21
+ }[] | undefined;
14
22
  kind: any;
15
23
  n: any;
16
24
  at: any;
@@ -62,6 +70,25 @@ export function writeSetup({ flowDir, secrets, ...rest }: {
62
70
  ok: false;
63
71
  red: string;
64
72
  };
73
+ /**
74
+ * M9 item 2: a trigger-only re-sign signs the same plan under a new flow hash. The old `setup.jsonl` bytes are written back untouched (append-only)
75
+ * and ONE row is added: kind `trigger-resign`, the record of the signing it continues (its plan hash and "Not checked" list, copied) under the new
76
+ * `flowHash`, naming the old one in `resignOf`. A reader that wants this job's record looks for the row whose `flowHash` is the job's. Never throws.
77
+ * @param {{ flowDir: string, oldText: string, oldFlowHash: string, newFlowHash: string, at: string }} a
78
+ * @returns {{ ok: true } | { ok: false, red: string }}
79
+ */
80
+ export function carrySetup({ flowDir, oldText, oldFlowHash, newFlowHash, at }: {
81
+ flowDir: string;
82
+ oldText: string;
83
+ oldFlowHash: string;
84
+ newFlowHash: string;
85
+ at: string;
86
+ }): {
87
+ ok: true;
88
+ } | {
89
+ ok: false;
90
+ red: string;
91
+ };
65
92
  /**
66
93
  * 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).
67
94
  * @param {string} flowDir
@@ -0,0 +1,53 @@
1
+ /**
2
+ * SCHEDULED: what starts the job, in words. `null` (By hand) has no sentence.
3
+ * @param {Trigger|null} t @param {string} [home]
4
+ * @returns {string|null}
5
+ */
6
+ export function scheduledWords(t: Trigger | null, home?: string): string | null;
7
+ /** "Mon 13 Oct, 09:00" — local time, fixed English names (never the machine's locale). @param {number} ms */
8
+ export function slotWords(ms: number): string;
9
+ /** am1, said before a human signs an edit of a job that has a trigger. @param {Trigger|null} t @param {string} [home] */
10
+ export function editClearsWords(t: Trigger | null, home?: string): string | null;
11
+ /**
12
+ * NEXT of a job with a trigger. One sentence, first match wins:
13
+ * 1. a run of this job is waiting at its ask (the next run waits too);
14
+ * 2. the latest outcome this month is `monthly-exhausted` (the signed sentence, amounts from the row);
15
+ * 3. a clock kind with a next slot; 4. a file kind that is armed; 5. not armed yet.
16
+ * A last outcome of `refused` goes in front of 3-5, so a trigger that cannot start is never silent.
17
+ * `red` is the typed flag for "the human must act": monthly-exhausted and the refused lead. The page paints it; it never guesses from the text.
18
+ * @param {{trigger: Trigger, armed: boolean, nextSlotMs: number|null, lastOutcome: any, waitingLine: number|null, now: number}} f
19
+ * @returns {{text: string, red: boolean}}
20
+ */
21
+ export function scheduledNextFor(f: {
22
+ trigger: Trigger;
23
+ armed: boolean;
24
+ nextSlotMs: number | null;
25
+ lastOutcome: any;
26
+ waitingLine: number | null;
27
+ now: number;
28
+ }): {
29
+ text: string;
30
+ red: boolean;
31
+ };
32
+ /** The NEXT sentence alone. @param {Parameters<typeof scheduledNextFor>[0]} f */
33
+ export function scheduledNext(f: Parameters<typeof scheduledNextFor>[0]): string;
34
+ /** Fixed catch-up sentence (am2 item 1) from a run's `trigger.json`. @param {any} rec */
35
+ export function catchUpWords(rec: any): "This run was a catch-up: it started more than 2 minutes after its set time (the panel was off)." | null;
36
+ /**
37
+ * ENDED / NEXT of a finished run. Every outcome the history row can hold has a fixed sentence; an unknown one gets an honest fixed fallback
38
+ * that names the code and guesses nothing. `null` when there is no ending (still running, waiting, or no history row and not died).
39
+ * @param {{outcome: string|null, died?: boolean, line: number|null, destination: string|null, canResume: boolean, home?: string}} f
40
+ * @returns {{reason: string, next: string}|null}
41
+ */
42
+ export function endedWords(f: {
43
+ outcome: string | null;
44
+ died?: boolean;
45
+ line: number | null;
46
+ destination: string | null;
47
+ canResume: boolean;
48
+ home?: string;
49
+ }): {
50
+ reason: string;
51
+ next: string;
52
+ } | null;
53
+ export type Trigger = import("./trigger-line.js").Trigger;