fwdloop 0.13.0 → 0.15.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/bin/fwdloop +25 -12
- package/package.json +2 -2
- package/src/authoring.js +73 -39
- package/src/canrun.js +71 -1
- package/src/catalogue.json +2 -2
- package/src/checked.js +54 -0
- package/src/closers.js +112 -91
- package/src/declaration.js +115 -11
- package/src/drafter.js +139 -39
- package/src/flow.js +220 -5
- package/src/keysfile.js +4 -2
- package/src/model-step.js +1 -1
- package/src/monthly.js +139 -17
- package/src/panel/author.js +226 -64
- package/src/panel/authorcard.js +99 -4
- package/src/panel/authorflows.js +51 -10
- package/src/panel/authorresume.js +2 -2
- package/src/panel/authorstart.js +18 -10
- package/src/panel/data.js +147 -81
- package/src/panel/index.html +497 -338
- package/src/panel/server.js +5 -2
- package/src/panel/settings.js +1 -1
- package/src/runner.js +105 -48
- package/src/setup.js +21 -14
- package/src/signed-diff.js +71 -0
- package/types/authoring.d.ts +52 -14
- package/types/canrun.d.ts +33 -1
- package/types/checked.d.ts +25 -0
- package/types/closers.d.ts +47 -28
- package/types/declaration.d.ts +16 -2
- package/types/drafter.d.ts +146 -11
- package/types/flow.d.ts +42 -2
- package/types/model-step.d.ts +1 -6
- package/types/monthly.d.ts +25 -1
- package/types/panel/author.d.ts +55 -40
- package/types/panel/authorcard.d.ts +54 -0
- package/types/panel/authorflows.d.ts +39 -0
- package/types/panel/authorstart.d.ts +1 -0
- package/types/panel/data.d.ts +11 -56
- package/types/runner.d.ts +11 -3
- package/types/setup.d.ts +6 -12
- package/types/signed-diff.d.ts +10 -0
package/src/monthly.js
CHANGED
|
@@ -4,8 +4,12 @@
|
|
|
4
4
|
// ONE record: `<configHome>/runs.jsonl`, append-only, mode 0600, one row per hold and per terminal note.
|
|
5
5
|
// hold: { kind:'hold', holdId, what:'run'|'resume'|'draft', flow, runId, runDir, pid, procStart, holdUsd, spentAtHold, at }
|
|
6
6
|
// terminal: { kind:'settled'|'released'|'refused', holdId, at, why [, alsoRunDirs] }
|
|
7
|
-
//
|
|
8
|
-
//
|
|
7
|
+
// named: { kind:'named', holdId, at, alsoRunDirs } — a hold's process starts spending into one more run dir (a rerun's new run)
|
|
8
|
+
// rolled: { kind:'rolled', at, seen, dirs } — the reduced spend rows of dirs whose holds all ended on their own (never "process
|
|
9
|
+
// gone"), so a read opens one record, not one file per run; `seen` = how many rows the record had when it was read
|
|
10
|
+
// Rows are only ever appended. Month spend is read from each named run/draft dir's OWN `spend.jsonl` (so the total is the same
|
|
11
|
+
// whichever `--root` a run used); only a `rolled` row stands in for a dir's file (its copy of that dir's spend, kept if the dir is
|
|
12
|
+
// deleted). hold, terminal and named rows carry no money.
|
|
9
13
|
//
|
|
10
14
|
// The claim (bareloop `claimRun`): the door APPENDS its hold FIRST, then reads the file; only live,
|
|
11
15
|
// unsettled holds that come BEFORE its own row count against it, so whoever is first holds. A hold whose
|
|
@@ -61,7 +65,7 @@ export function readRuns(home) {
|
|
|
61
65
|
try { text = readFileSync(runsPath(home), 'utf8'); } catch { return []; }
|
|
62
66
|
const rows = [];
|
|
63
67
|
for (const line of text.split('\n')) {
|
|
64
|
-
if (
|
|
68
|
+
if (line === '') continue; // a blank or torn line is skipped by the parse below
|
|
65
69
|
try { const r = JSON.parse(line); if (r && typeof r === 'object') rows.push(r); } catch { /* skip */ }
|
|
66
70
|
}
|
|
67
71
|
return rows;
|
|
@@ -75,10 +79,7 @@ const sameMonth = (a, b) => a.getFullYear() === b.getFullYear() && a.getMonth()
|
|
|
75
79
|
/** Every run/draft dir the record names (deduped by realpath), including dirs a settle note adds. */
|
|
76
80
|
function namedDirs(rows) {
|
|
77
81
|
const dirs = new Set();
|
|
78
|
-
for (const r of rows)
|
|
79
|
-
if (r.kind === 'hold' && typeof r.runDir === 'string') dirs.add(r.runDir);
|
|
80
|
-
if (Array.isArray(r.alsoRunDirs)) for (const d of r.alsoRunDirs) if (typeof d === 'string') dirs.add(d);
|
|
81
|
-
}
|
|
82
|
+
for (const r of rows) for (const d of dirsNamedBy(r)) dirs.add(d);
|
|
82
83
|
return [...dirs];
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -87,9 +88,79 @@ function dirSpend(dir) {
|
|
|
87
88
|
return readDirSpendRows(dir).reduce((n, r) => n + spendRowCost(r).usd, 0);
|
|
88
89
|
}
|
|
89
90
|
|
|
91
|
+
/**
|
|
92
|
+
* One spend row reduced to exactly what `spendSummary` reads of it (I1, amendment 16): `u` cost (spendRowCost), `c` complete, `t` tokens,
|
|
93
|
+
* `w` wall ms or null, `p` the provider key, `a` the row's `at` as written (absent when it had none). Nothing else of a row is used.
|
|
94
|
+
*/
|
|
95
|
+
function reduceRow(row) {
|
|
96
|
+
const { usd, complete } = spendRowCost(row);
|
|
97
|
+
return {
|
|
98
|
+
u: usd,
|
|
99
|
+
c: complete,
|
|
100
|
+
t: tokensOf(row.tokens),
|
|
101
|
+
w: Number.isFinite(row.wallMs) && row.wallMs >= 0 ? row.wallMs : null,
|
|
102
|
+
p: typeof row.provider === 'string' && row.provider !== '' ? row.provider : NOT_RECORDED,
|
|
103
|
+
...(row.at === undefined ? {} : { a: row.at }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const diskRows = (dir) => readDirSpendRows(dir).map(reduceRow);
|
|
108
|
+
|
|
109
|
+
/** The run dirs a row names: a hold's `runDir`, and any `alsoRunDirs` (a settle or named note). */
|
|
110
|
+
function dirsNamedBy(r) {
|
|
111
|
+
const out = [];
|
|
112
|
+
if (r.kind === 'hold' && typeof r.runDir === 'string') out.push(r.runDir);
|
|
113
|
+
if (Array.isArray(r.alsoRunDirs)) for (const d of r.alsoRunDirs) if (typeof d === 'string') out.push(d);
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The dirs of every hold the checker settled as "process gone" (amendment 17 3A): that run may have been misjudged and still spend,
|
|
119
|
+
* so such a dir is never rolled (rollSettled) and an older rolled row for it is never used (rolledEntries, amendment 19 1).
|
|
120
|
+
* @param {any[]} rows
|
|
121
|
+
* @returns {Set<string>}
|
|
122
|
+
*/
|
|
123
|
+
function goneDirs(rows) {
|
|
124
|
+
const goneHolds = new Set(rows.filter((r) => r.kind === 'settled' && r.why === 'process gone').map((r) => r.holdId));
|
|
125
|
+
const out = new Set();
|
|
126
|
+
for (const r of rows) {
|
|
127
|
+
if (r.kind !== 'rolled' && typeof r.holdId === 'string' && goneHolds.has(r.holdId)) for (const d of dirsNamedBy(r)) out.add(d);
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The rolled spend of settled dirs (I1, amendment 16). A `rolled` row in runs.jsonl carries, for dirs whose every hold had ended,
|
|
134
|
+
* their reduced spend rows, and `seen` = how many rows the record had when the snapshot was read. An entry is used only if NO row
|
|
135
|
+
* at index >= `seen` names that dir (a resume or a rerun names it again, so its live spend is read from the dir instead). Anything
|
|
136
|
+
* else — no entry, a newer naming row — reads the dir as before, so a total can never differ from reading every dir.
|
|
137
|
+
* @param {any[]} rows
|
|
138
|
+
* @returns {Map<string, ReturnType<typeof reduceRow>[]>}
|
|
139
|
+
*/
|
|
140
|
+
function rolledEntries(rows) {
|
|
141
|
+
/** @type {Map<string, {seen: number, rows: any[]}>} */
|
|
142
|
+
const best = new Map();
|
|
143
|
+
/** @type {Map<string, number>} */
|
|
144
|
+
const lastNamed = new Map();
|
|
145
|
+
rows.forEach((r, i) => {
|
|
146
|
+
for (const d of dirsNamedBy(r)) lastNamed.set(d, i);
|
|
147
|
+
if (r.kind === 'rolled' && Number.isInteger(r.seen) && r.dirs && typeof r.dirs === 'object') {
|
|
148
|
+
for (const [d, rs] of Object.entries(r.dirs)) {
|
|
149
|
+
const prev = best.get(d);
|
|
150
|
+
if (Array.isArray(rs) && !(prev && prev.seen > r.seen)) best.set(d, { seen: r.seen, rows: rs });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
const gone = goneDirs(rows);
|
|
155
|
+
const out = new Map();
|
|
156
|
+
for (const [d, e] of best) if (!gone.has(d) && (lastNamed.get(d) ?? -1) < e.seen) out.set(d, e.rows);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
90
160
|
/**
|
|
91
161
|
* The Money tab's figures (piece 4 reads this) and the check's month total.
|
|
92
|
-
*
|
|
162
|
+
* `rows` (a caller that has just read the record, like the claim) saves a second parse of the same file.
|
|
163
|
+
* @param {{ home?: string, now?: () => number, rows?: any[] }} [opts]
|
|
93
164
|
*/
|
|
94
165
|
export function spendSummary(opts = {}) {
|
|
95
166
|
const nowDate = new Date((opts.now ?? Date.now)());
|
|
@@ -107,17 +178,18 @@ export function spendSummary(opts = {}) {
|
|
|
107
178
|
if (wall === null) b.modelMsAtLeast = true;
|
|
108
179
|
else b.modelMs = (b.modelMs ?? 0) + wall;
|
|
109
180
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
181
|
+
const record = opts.rows ?? readRuns(opts.home);
|
|
182
|
+
const rolled = rolledEntries(record);
|
|
183
|
+
for (const dir of namedDirs(record)) {
|
|
184
|
+
for (const row of rolled.get(dir) ?? diskRows(dir)) {
|
|
185
|
+
const {
|
|
186
|
+
u: usd, c: complete, t: tokens, w: wall, p: key,
|
|
187
|
+
} = row;
|
|
116
188
|
const p = (byProvider[key] ??= { label: key, month: newBucket(), total: newBucket() });
|
|
117
189
|
add(total, usd, complete, tokens, wall);
|
|
118
190
|
add(p.total, usd, complete, tokens, wall);
|
|
119
|
-
if (row.
|
|
120
|
-
const at = new Date(row.
|
|
191
|
+
if (row.a === undefined || row.a === null) { undatedRows += 1; continue; }
|
|
192
|
+
const at = new Date(row.a);
|
|
121
193
|
if (Number.isNaN(at.getTime())) { month.atLeast = true; p.month.atLeast = true; continue; } // could be this month: unknown, never dropped
|
|
122
194
|
if (sameMonth(at, nowDate)) {
|
|
123
195
|
add(month, usd, complete, tokens, wall);
|
|
@@ -169,7 +241,7 @@ function liveHeldBefore(rows, idx, home, nowIso, settle = true) {
|
|
|
169
241
|
*/
|
|
170
242
|
function roomFor({ limit, holdUsd, rows, idx, home, now, nowIso, settle = true }) {
|
|
171
243
|
const { heldUsd, heldRuns } = liveHeldBefore(rows, idx, home, nowIso, settle);
|
|
172
|
-
const month = spendSummary({ home, now }).month;
|
|
244
|
+
const month = spendSummary({ home, now, rows }).month;
|
|
173
245
|
const leftCents = Math.max(0, Math.floor((limit - month.usd - heldUsd) * 100 + 1e-6));
|
|
174
246
|
const needCents = Math.ceil(holdUsd * 100 - 1e-6); // a non-finite hold never fits — never read as $0
|
|
175
247
|
return {
|
|
@@ -250,6 +322,55 @@ export function claimHold({
|
|
|
250
322
|
return { ok: true, holdId, room };
|
|
251
323
|
}
|
|
252
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Amendment 16 C2: a hold's process starts spending into one more run dir (a rerun's new run): name it NOW, so a process killed hard
|
|
327
|
+
* still has that dir's spend counted. Append-only; `namedDirs` dedupes, so the settle note naming it again never counts it twice.
|
|
328
|
+
* Never throws (a failed note is closed by the settle note's `alsoRunDirs` on a normal exit).
|
|
329
|
+
* @param {{ holdId: string|null, runDir: string, home?: string, now?: () => number }} a
|
|
330
|
+
*/
|
|
331
|
+
export function nameRunDir({
|
|
332
|
+
holdId, runDir, home, now = Date.now,
|
|
333
|
+
}) {
|
|
334
|
+
if (!holdId) return;
|
|
335
|
+
try {
|
|
336
|
+
appendRow(home, {
|
|
337
|
+
kind: 'named', holdId, at: new Date(now()).toISOString(), alsoRunDirs: [realpathLoose(runDir)],
|
|
338
|
+
});
|
|
339
|
+
} catch { /* see above */ }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** How many dirs one `rolled` row carries (keeps each appended line small enough to be one whole write). */
|
|
343
|
+
const ROLL_CHUNK = 200;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* I1 (amendment 16): fold the spend of every run/draft dir whose holds have ALL ended (settled, released or refused) into `rolled`
|
|
347
|
+
* rows, so the next month figure or Money load reads one record instead of one file per run. Append-only; a dir already rolled and not
|
|
348
|
+
* named since is left alone. Reads the dirs it folds exactly as `spendSummary` would, so no total changes. Never throws.
|
|
349
|
+
* @param {{ home?: string, now?: () => number }} [a]
|
|
350
|
+
*/
|
|
351
|
+
export function rollSettled({ home, now = Date.now } = {}) {
|
|
352
|
+
try {
|
|
353
|
+
const rows = readRuns(home);
|
|
354
|
+
const seen = rows.length;
|
|
355
|
+
const ended = new Set(rows.filter((r) => r.kind === 'settled' || r.kind === 'released' || r.kind === 'refused').map((r) => r.holdId));
|
|
356
|
+
/** @type {Set<string>} */
|
|
357
|
+
const open = new Set();
|
|
358
|
+
for (const r of rows) {
|
|
359
|
+
if (r.kind === 'rolled') continue;
|
|
360
|
+
const named = dirsNamedBy(r);
|
|
361
|
+
const unended = typeof r.holdId !== 'string' || !ended.has(r.holdId);
|
|
362
|
+
if (unended) for (const d of named) open.add(d);
|
|
363
|
+
}
|
|
364
|
+
for (const d of goneDirs(rows)) open.add(d);
|
|
365
|
+
const have = rolledEntries(rows);
|
|
366
|
+
const todo = namedDirs(rows).filter((d) => !open.has(d) && !have.has(d));
|
|
367
|
+
for (let i = 0; i < todo.length; i += ROLL_CHUNK) {
|
|
368
|
+
const dirs = Object.fromEntries(todo.slice(i, i + ROLL_CHUNK).map((d) => [d, diskRows(d)]));
|
|
369
|
+
appendRow(home, { kind: 'rolled', at: new Date(now()).toISOString(), seen, dirs });
|
|
370
|
+
}
|
|
371
|
+
} catch { /* a failed roll only means the next read opens the dirs, as before */ }
|
|
372
|
+
}
|
|
373
|
+
|
|
253
374
|
/**
|
|
254
375
|
* The run's own process ends or parks: give the hold back. Never throws (a failed settle is closed by the
|
|
255
376
|
* next check as "process gone"). `alsoRunDirs` names dirs this process also spent into (a rerun's new run).
|
|
@@ -264,6 +385,7 @@ export function settleHold({
|
|
|
264
385
|
kind: 'settled', holdId, at: new Date(now()).toISOString(), why, ...(alsoRunDirs?.length ? { alsoRunDirs: alsoRunDirs.map(realpathLoose) } : {}),
|
|
265
386
|
});
|
|
266
387
|
} catch { /* see above */ }
|
|
388
|
+
rollSettled({ home, now });
|
|
267
389
|
}
|
|
268
390
|
|
|
269
391
|
/**
|