fwdloop 0.11.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.
- package/bin/fwdloop +248 -27
- package/package.json +1 -1
- package/src/ask.js +3 -0
- package/src/authoring.js +103 -13
- package/src/books.js +23 -2
- package/src/canrun.js +36 -0
- package/src/config.js +256 -0
- package/src/declaration.js +72 -2
- package/src/drafter.js +53 -11
- package/src/draftspend.js +70 -0
- package/src/flow.js +43 -0
- package/src/keysfile.js +180 -0
- package/src/liveness.js +1 -1
- package/src/model-step.js +43 -8
- package/src/monthly.js +276 -0
- package/src/panel/author.js +482 -0
- package/src/panel/authorcard.js +297 -0
- package/src/panel/authorflows.js +312 -0
- package/src/panel/authorresume.js +149 -0
- package/src/panel/authorstart.js +257 -0
- package/src/panel/authorvalues.js +110 -0
- package/src/panel/data.js +228 -34
- package/src/panel/index.html +2073 -124
- package/src/panel/resume.js +14 -11
- package/src/panel/server.js +150 -117
- package/src/panel/settings.js +270 -0
- package/src/panel/spawn.js +77 -0
- package/src/provider.js +189 -43
- package/src/providershapes.js +19 -0
- package/src/runner.js +639 -85
- package/src/runvalues.js +178 -0
- package/src/send.js +10 -3
- package/src/setup.js +120 -0
- package/types/authoring.d.ts +24 -2
- package/types/books.d.ts +14 -2
- package/types/canrun.d.ts +32 -0
- package/types/config.d.ts +52 -0
- package/types/declaration.d.ts +9 -1
- package/types/drafter.d.ts +18 -2
- package/types/draftspend.d.ts +21 -0
- package/types/flow.d.ts +23 -0
- package/types/keysfile.d.ts +90 -0
- package/types/liveness.d.ts +2 -2
- package/types/model-step.d.ts +6 -2
- package/types/monthly.d.ts +130 -0
- package/types/panel/author.d.ts +335 -0
- package/types/panel/authorcard.d.ts +120 -0
- package/types/panel/authorflows.d.ts +175 -0
- package/types/panel/authorresume.d.ts +32 -0
- package/types/panel/authorstart.d.ts +115 -0
- package/types/panel/authorvalues.d.ts +62 -0
- package/types/panel/data.d.ts +103 -12
- package/types/panel/resume.d.ts +8 -1
- package/types/panel/server.d.ts +19 -33
- package/types/panel/settings.d.ts +23 -0
- package/types/panel/spawn.d.ts +30 -0
- package/types/provider.d.ts +123 -70
- package/types/providershapes.d.ts +11 -0
- package/types/runner.d.ts +120 -5
- package/types/runvalues.d.ts +97 -0
- package/types/send.d.ts +5 -1
- package/types/setup.d.ts +83 -0
package/src/runvalues.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// M4e amendment 5 item 3 and amendment 4 item 6 (docs/wiki/the-module-ladder.md): the signed values ONE RUN uses when they differ from
|
|
2
|
+
// its flow's own, and the signed version a resumed run continues under.
|
|
3
|
+
//
|
|
4
|
+
// A run's folder may hold `signed-values.json` (version 0: written once by the human's two-click "Sign & run", before the run starts) and
|
|
5
|
+
// `signed-values-r<k>.json` (k >= 1: written once by the human's two-click "Sign & resume", one per Resume). Each file is the FULL set
|
|
6
|
+
// in force from then on (cap, destination, each ask's wait), never a patch, so the highest version wins whole. Write-once ('wx'): no
|
|
7
|
+
// version is ever overwritten, and the flow's own prose.txt / signature.json are never touched; the next plain run starts from the flow's
|
|
8
|
+
// signed values again. The agent, the chat notes and the drafter have no path to these files (the runner reads them; the panel's two
|
|
9
|
+
// doors are the only writers, and only on a hash the human's click carried).
|
|
10
|
+
//
|
|
11
|
+
// hash sha256 over the canonical {flow, flowSignatureHash, runId|null, version, values}: the page shows it at the first click and
|
|
12
|
+
// sends it back at the second; the record carries it and every reader recomputes it (a hand-edited file reads as a refusal).
|
|
13
|
+
//
|
|
14
|
+
// ONE function decides which version a run uses (`pickRunValues`) and ONE applies it (`applyRunValues`): the runner (fresh run,
|
|
15
|
+
// answer-resume, continue) and the CLI's monthly hold all call these, never the files directly.
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { unlinkSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
|
|
20
|
+
import { readFileInside, readdirInside } from './flow.js';
|
|
21
|
+
|
|
22
|
+
export const VALUES_FILE_RE = /^signed-values(?:-r(\d+))?\.json$/;
|
|
23
|
+
|
|
24
|
+
const WAIT_RE = /^(\d+)\s*([smh])$/i;
|
|
25
|
+
const UNIT_MS = { s: 1000, m: 60_000, h: 3_600_000 };
|
|
26
|
+
|
|
27
|
+
/** 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 */
|
|
28
|
+
export function parseWaitMs(text) {
|
|
29
|
+
const m = typeof text === 'string' ? WAIT_RE.exec(text.trim()) : null;
|
|
30
|
+
if (m === null) return null;
|
|
31
|
+
const n = Number(m[1]);
|
|
32
|
+
return n > 0 && Number.isSafeInteger(n) ? n * UNIT_MS[m[2].toLowerCase()] : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The wait text for a ttl in ms (`2h`, `30m`, `45s`). @param {number} ms */
|
|
36
|
+
export function waitText(ms) {
|
|
37
|
+
if (ms % 3_600_000 === 0 && ms > 0) return `${ms / 3_600_000}h`;
|
|
38
|
+
if (ms % 60_000 === 0 && ms > 0) return `${ms / 60_000}m`;
|
|
39
|
+
return `${Math.round(ms / 1000)}s`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 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
|
|
44
|
+
* destination cannot be added or changed), and each ask's wait by its line number.
|
|
45
|
+
* @param {any} arbiter `readFlow`'s arbiter
|
|
46
|
+
* @returns {{ capUsd: number, destination: string|null, askWaits: Record<string, string> }}
|
|
47
|
+
*/
|
|
48
|
+
export function flowValues(arbiter) {
|
|
49
|
+
const sends = arbiter.sends ?? [];
|
|
50
|
+
const destination = sends.length === 1 && sends[0].target?.kind === 'file' ? String(sends[0].target.path) : null;
|
|
51
|
+
const askWaits = Object.fromEntries((arbiter.asks ?? []).map((a) => [String(a.line), waitText(a.ttlMs)]));
|
|
52
|
+
return { capUsd: arbiter.capUsd, destination, askWaits };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @param {{capUsd: number, destination: string|null, askWaits: Record<string,string>}} v the values in canonical key order */
|
|
56
|
+
function canonValues(v) {
|
|
57
|
+
return {
|
|
58
|
+
capUsd: v.capUsd,
|
|
59
|
+
destination: v.destination ?? null,
|
|
60
|
+
askWaits: Object.fromEntries(Object.entries(v.askWaits ?? {}).sort(([a], [b]) => Number(a) - Number(b))),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The hash the human's click is bound to.
|
|
66
|
+
* @param {{ flow: string, flowSignatureHash: string, runId?: string|null, version: number, values: {capUsd: number, destination: string|null, askWaits: Record<string,string>} }} a
|
|
67
|
+
*/
|
|
68
|
+
export function valuesHash({
|
|
69
|
+
flow, flowSignatureHash, runId = null, version, values,
|
|
70
|
+
}) {
|
|
71
|
+
return createHash('sha256').update(JSON.stringify({
|
|
72
|
+
flow, flowSignatureHash, runId, version, values: canonValues(values),
|
|
73
|
+
})).digest('hex');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** True when `a` and `b` are the same values (cap, destination, every wait). */
|
|
77
|
+
export function sameValues(a, b) {
|
|
78
|
+
return JSON.stringify(canonValues(a)) === JSON.stringify(canonValues(b));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** File name of a version. @param {number} version */
|
|
82
|
+
export const valuesFileName = (version) => (version === 0 ? 'signed-values.json' : `signed-values-r${version}.json`);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The ONE writer of a version file: write-once (`wx`), mode 0600. Refuses a record whose hash does not match its own fields.
|
|
86
|
+
* @param {string} runDir
|
|
87
|
+
* @param {{ flow: string, flowSignatureHash: string, runId: string, version: number, values: any, hash: string, signedBy: string, at: string }} record
|
|
88
|
+
* @returns {{ok: true, file: string} | {ok: false, red: string}}
|
|
89
|
+
*/
|
|
90
|
+
export function writeRunValues(runDir, record) {
|
|
91
|
+
const want = valuesHash({
|
|
92
|
+
flow: record.flow, flowSignatureHash: record.flowSignatureHash, runId: record.version === 0 ? null : record.runId, version: record.version, values: record.values,
|
|
93
|
+
});
|
|
94
|
+
if (want !== record.hash) return { ok: false, red: 'signed values: the hash does not match the values — nothing written' };
|
|
95
|
+
const file = join(runDir, valuesFileName(record.version));
|
|
96
|
+
try {
|
|
97
|
+
writeFileSync(file, `${JSON.stringify({ ...record, values: canonValues(record.values) }, null, 2)}\n`, { flag: 'wx', mode: 0o600 });
|
|
98
|
+
} catch (err) {
|
|
99
|
+
return { ok: false, red: err.code === 'EEXIST' ? `signed values: version ${record.version} of this run already exists — never overwritten` : `signed values: could not write ${valuesFileName(record.version)} (${err.code ?? err.message})` };
|
|
100
|
+
}
|
|
101
|
+
return { ok: true, file };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The ONE decision "which signed version does this run use": the highest version file in the run folder.
|
|
106
|
+
* `{ok:true, none:true}` = the flow's own values; `{ok:true, version, record}`; `{ok:false, red}` = a file that cannot be trusted
|
|
107
|
+
* (unreadable, not JSON, wrong shape, a hash that does not match its fields, a name that disagrees with its version).
|
|
108
|
+
* @param {string} runDir
|
|
109
|
+
* @returns {{ok: true, none: true} | {ok: true, none?: false, version: number, record: any} | {ok: false, red: string}}
|
|
110
|
+
*/
|
|
111
|
+
export function pickRunValues(runDir) {
|
|
112
|
+
let best = null;
|
|
113
|
+
for (const name of readdirInside(runDir, '.')) {
|
|
114
|
+
const m = VALUES_FILE_RE.exec(name);
|
|
115
|
+
if (m === null) continue;
|
|
116
|
+
const version = m[1] === undefined ? 0 : Number(m[1]);
|
|
117
|
+
if (best === null || version > best.version) best = { version, name };
|
|
118
|
+
}
|
|
119
|
+
if (best === null) return { ok: true, none: true };
|
|
120
|
+
const f = readFileInside(runDir, best.name);
|
|
121
|
+
if (!f.ok) return { ok: false, red: `signed values: ${best.name} — ${f.missing ? 'unreadable' : f.red}` };
|
|
122
|
+
let record;
|
|
123
|
+
try { record = JSON.parse(f.text); } catch (err) { return { ok: false, red: `signed values: ${best.name} is not valid JSON — ${err.message}` }; }
|
|
124
|
+
const v = record?.values;
|
|
125
|
+
const shaped = record && typeof record === 'object' && record.version === best.version && typeof record.flow === 'string' && typeof record.flowSignatureHash === 'string'
|
|
126
|
+
&& typeof record.hash === 'string' && v && typeof v === 'object' && Number.isFinite(v.capUsd) && v.capUsd > 0
|
|
127
|
+
&& (v.destination === null || (typeof v.destination === 'string' && v.destination !== ''))
|
|
128
|
+
&& v.askWaits && typeof v.askWaits === 'object' && Object.values(v.askWaits).every((w) => parseWaitMs(w) !== null);
|
|
129
|
+
if (!shaped) return { ok: false, red: `signed values: ${best.name} does not have the shape of a signed values record` };
|
|
130
|
+
const want = valuesHash({
|
|
131
|
+
flow: record.flow, flowSignatureHash: record.flowSignatureHash, runId: best.version === 0 ? null : (record.runId ?? null), version: best.version, values: v,
|
|
132
|
+
});
|
|
133
|
+
if (want !== record.hash) return { ok: false, red: `signed values: ${best.name} does not match its own hash — it was changed after it was signed` };
|
|
134
|
+
return { ok: true, version: best.version, record };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 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
|
|
139
|
+
* 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
|
|
140
|
+
* or an ask the flow does not have.
|
|
141
|
+
* @param {any} arbiter `readFlow`'s arbiter @param {string} flow @param {string} flowSignatureHash
|
|
142
|
+
* @param {ReturnType<typeof pickRunValues>} picked
|
|
143
|
+
* @returns {{ok: true, arbiter: any, version: number|null} | {ok: false, red: string}}
|
|
144
|
+
*/
|
|
145
|
+
export function applyRunValues(arbiter, flow, flowSignatureHash, picked) {
|
|
146
|
+
if (!picked.ok) return { ok: false, red: picked.red };
|
|
147
|
+
if (picked.none) return { ok: true, arbiter, version: null };
|
|
148
|
+
const { record } = picked;
|
|
149
|
+
if (record.flow !== flow || record.flowSignatureHash !== flowSignatureHash) {
|
|
150
|
+
return { ok: false, red: `signed values: ${valuesFileName(picked.version)} was signed for another flow or another signature of this flow — refusing` };
|
|
151
|
+
}
|
|
152
|
+
const own = flowValues(arbiter);
|
|
153
|
+
const v = record.values;
|
|
154
|
+
if ((v.destination === null) !== (own.destination === null)) return { ok: false, red: 'signed values: the destination does not fit this flow (it has no single send folder to change) — refusing' };
|
|
155
|
+
const lines = Object.keys(own.askWaits).sort().join(',');
|
|
156
|
+
if (Object.keys(v.askWaits).sort().join(',') !== lines) return { ok: false, red: 'signed values: the ask waits do not name this flow\'s asks — refusing' };
|
|
157
|
+
return {
|
|
158
|
+
ok: true,
|
|
159
|
+
version: picked.version,
|
|
160
|
+
arbiter: {
|
|
161
|
+
...arbiter,
|
|
162
|
+
capUsd: v.capUsd,
|
|
163
|
+
sends: (arbiter.sends ?? []).map((s) => (v.destination === null ? s : { ...s, target: { ...s.target, path: v.destination } })),
|
|
164
|
+
asks: (arbiter.asks ?? []).map((a) => ({ ...a, ttlMs: parseWaitMs(v.askWaits[String(a.line)]), ttlSigned: true })),
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A start that refused before the run began (monthly limit, empty key ...) leaves a run folder holding nothing but its signed values
|
|
171
|
+
* 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.
|
|
172
|
+
* @param {string} runDir @returns {boolean} true when the file was removed
|
|
173
|
+
*/
|
|
174
|
+
export function dropUnstartedValues(runDir) {
|
|
175
|
+
const names = readdirInside(runDir, '.');
|
|
176
|
+
if (names.length !== 1 || names[0] !== valuesFileName(0)) return false;
|
|
177
|
+
try { unlinkSync(join(runDir, names[0])); return true; } catch { return false; }
|
|
178
|
+
}
|
package/src/send.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// in-memory content handed in — so a write that silently truncates to 0
|
|
12
12
|
// bytes reds here, not upstream).
|
|
13
13
|
|
|
14
|
-
import { readFileSync } from 'node:fs';
|
|
14
|
+
import { lstatSync, readFileSync } from 'node:fs';
|
|
15
15
|
import { join } from 'node:path';
|
|
16
16
|
import { createShellTools } from 'bare-agent/tools';
|
|
17
17
|
|
|
@@ -30,10 +30,11 @@ const WRITE_TOOL = FOUND_WRITE_TOOL;
|
|
|
30
30
|
* @param {string|null} [acceptedSha256] - the sha256 the human's accept recorded
|
|
31
31
|
* (`answerAsk`, M4b piece 3). Required: none recorded (an answer written by an
|
|
32
32
|
* older version, or no accept in this process) refuses by name.
|
|
33
|
+
* @param {{root?: string, runDir?: string}} [ctx] the flow root and this run's folder, for the destination refusals
|
|
33
34
|
* @returns {Promise<{ok:true, path:string, bytes:number} | {ok:false, red:string}>}
|
|
34
35
|
*/
|
|
35
|
-
export async function sendViaPrimitive(target, filename, content, acceptedSha256) {
|
|
36
|
-
const destination = checkSendDestination(target);
|
|
36
|
+
export async function sendViaPrimitive(target, filename, content, acceptedSha256, ctx = {}) {
|
|
37
|
+
const destination = checkSendDestination(target, ctx);
|
|
37
38
|
if (!destination.ok) return { ok: false, red: destination.red };
|
|
38
39
|
|
|
39
40
|
// F48 round 4 (redesign) defense in depth: `src/runner.js`'s send slot
|
|
@@ -70,6 +71,12 @@ export async function sendViaPrimitive(target, filename, content, acceptedSha256
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
const path = join(destination.dir, filename);
|
|
74
|
+
// M4e amendment 1: never overwrite. `shell_write` has no exclusive-create mode, so this is an
|
|
75
|
+
// existence check immediately before the write (lstat: a dangling symlink at the name counts as
|
|
76
|
+
// present). A file created in the instants between this check and the write is still overwritten.
|
|
77
|
+
let present = true;
|
|
78
|
+
try { lstatSync(path); } catch (err) { if (err.code === 'ENOENT') present = false; }
|
|
79
|
+
if (present) return { ok: false, red: `send: "${path}" already exists — never overwritten (nothing shipped)` };
|
|
73
80
|
await WRITE_TOOL.execute({ path, content: serialised });
|
|
74
81
|
|
|
75
82
|
const bytes = readFileSync(path);
|
package/src/setup.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// M4e amendment 6 item 4 (docs/wiki/the-module-ladder.md, "M4e", "Amendment 6"): the SETUP record. At sign, the draft folder's own
|
|
2
|
+
// record (the card, each draft and change with its model, cost and verdict, each note in the human's words, and the sign) is copied once
|
|
3
|
+
// into the flow folder as `setup.jsonl`. It sits outside the three signed files (`FLOW_FILES`), so `readFlow` and the signature never see
|
|
4
|
+
// it. ONE writer (`writeSetup`, called from `signDraft`, which the CLI and the panel both use) and ONE reader (`readSetup`).
|
|
5
|
+
//
|
|
6
|
+
// What each row throws away: a draft/change row keeps model, cost, calls, time, verdict, plan hash and its first red, NOT the plan itself (the signed
|
|
7
|
+
// declaration is the plan) nor the model's tokens/rounds; a card row keeps the card's own typed fields; a note keeps its text verbatim.
|
|
8
|
+
// Third outcomes: a draft with no result is `stopped`; a cost nobody booked is `null` (shown "unknown", never $0).
|
|
9
|
+
import { lstatSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
|
|
12
|
+
import { scrub } from './authoring.js';
|
|
13
|
+
import { readDraftSpend } from './draftspend.js';
|
|
14
|
+
import { readFileInside, readdirInside } from './flow.js';
|
|
15
|
+
import { readSpendRows } from './provider.js';
|
|
16
|
+
|
|
17
|
+
export const SETUP_FILE = 'setup.jsonl';
|
|
18
|
+
const NOTE_RE = /^note-(\d+)\.txt$/;
|
|
19
|
+
|
|
20
|
+
const readJson = (dir, rel) => {
|
|
21
|
+
const r = readFileInside(dir, rel);
|
|
22
|
+
if (!r.ok) return null;
|
|
23
|
+
try { return JSON.parse(r.text); } catch { return null; }
|
|
24
|
+
};
|
|
25
|
+
const mtimeIso = (dir, rel) => {
|
|
26
|
+
try { return lstatSync(join(dir, rel)).mtime.toISOString(); } catch { return null; }
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** One plan folder's row: model, cost, verdict (green / red / stopped), plan hash, first red. @param {string} sessionDir @param {string} rel `draft` or `draft-<n>` */
|
|
30
|
+
function planRow(sessionDir, rel, kind, n) {
|
|
31
|
+
const hashFile = readFileInside(sessionDir, `${rel}/spec.hash`);
|
|
32
|
+
const hash = hashFile.ok && hashFile.text.trim() !== '' ? hashFile.text.trim() : null;
|
|
33
|
+
const log = readJson(sessionDir, `${rel}/log.json`);
|
|
34
|
+
const leak = readFileInside(sessionDir, `${rel}/scrub-leak.red`);
|
|
35
|
+
const spent = readSpendRows(join(sessionDir, rel, 'spend.jsonl')).filter((r) => r && r.kind === 'draft').pop() ?? readDraftSpend(join(sessionDir, rel));
|
|
36
|
+
let verdict = 'stopped';
|
|
37
|
+
/** @type {string|null} */
|
|
38
|
+
let gap = 'no result was recorded';
|
|
39
|
+
if (hash) { verdict = 'green'; gap = null; } else if (leak.ok || (log && log.ok === false)) {
|
|
40
|
+
verdict = 'red';
|
|
41
|
+
const reds = Array.isArray(log?.reds) ? log.reds.map(String) : [];
|
|
42
|
+
if (leak.ok) reds.push(leak.text.trim());
|
|
43
|
+
gap = reds.join('; ') || 'the plan did not pass the checks';
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
kind, n, at: typeof spent?.at === 'string' ? spent.at : null, model: spent?.model ?? log?.modelId ?? null,
|
|
47
|
+
costUsd: typeof spent?.costUsd === 'number' ? spent.costUsd : null, spendComplete: spent ? spent.spendComplete !== false && typeof spent.costUsd === 'number' : false,
|
|
48
|
+
// calls and time as the draft booked them; null = not recorded (never 0)
|
|
49
|
+
calls: Number.isInteger(spent?.calls) ? spent.calls : null, wallMs: typeof spent?.wallMs === 'number' ? spent.wallMs : null,
|
|
50
|
+
// retries the drafter spent on structure (log.json); null = not recorded (a plan from before it), never 0
|
|
51
|
+
structureRetries: Number.isInteger(log?.structureRetries) ? log.structureRetries : null,
|
|
52
|
+
verdict, hash, gap,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 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
|
|
58
|
+
* draft has none, so its record is its one plan folder (`planDir`, read as `draft`) plus the sign.
|
|
59
|
+
* @param {{ sessionDir?: string, planDir: string, hash: string, signedBy: string, signedAt: string, flowHash?: string }} a
|
|
60
|
+
*/
|
|
61
|
+
export function buildSetupRows({
|
|
62
|
+
sessionDir, planDir, hash, signedBy, signedAt, flowHash,
|
|
63
|
+
}) {
|
|
64
|
+
const rows = [];
|
|
65
|
+
if (sessionDir) {
|
|
66
|
+
const card = readJson(sessionDir, 'card.json');
|
|
67
|
+
if (card) rows.push({ kind: 'card', n: 0, at: mtimeIso(sessionDir, 'card.json'), card });
|
|
68
|
+
rows.push(planRow(sessionDir, 'draft', 'draft', 0));
|
|
69
|
+
const notes = readdirInside(sessionDir, '.').map((f) => NOTE_RE.exec(f)?.[1]).filter((x) => x !== undefined).map(Number).sort((a, b) => a - b);
|
|
70
|
+
for (const n of notes) {
|
|
71
|
+
const t = readFileInside(sessionDir, `note-${n}.txt`);
|
|
72
|
+
rows.push({
|
|
73
|
+
kind: 'note', n, at: mtimeIso(sessionDir, `note-${n}.txt`), text: t.ok ? t.text.replace(/\n$/, '') : '',
|
|
74
|
+
});
|
|
75
|
+
rows.push(planRow(sessionDir, `draft-${n}`, 'change', n));
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
const r = planRow(planDir, '.', 'draft', 0);
|
|
79
|
+
rows.push(r);
|
|
80
|
+
}
|
|
81
|
+
rows.push({
|
|
82
|
+
kind: 'sign', n: 0, at: signedAt, signedBy, hash, flowHash: flowHash ?? null,
|
|
83
|
+
});
|
|
84
|
+
return rows;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Write `<flowDir>/setup.jsonl` ONCE ('wx': a second write is refused, never merged or replaced), mode 0600, every row scrubbed of the
|
|
89
|
+
* given key values. Never throws.
|
|
90
|
+
* @param {{ flowDir: string, secrets?: string[] } & Parameters<typeof buildSetupRows>[0]} a
|
|
91
|
+
* @returns {{ ok: true, file: string, rows: number } | { ok: false, red: string }}
|
|
92
|
+
*/
|
|
93
|
+
export function writeSetup({ flowDir, secrets = [], ...rest }) {
|
|
94
|
+
const file = join(flowDir, SETUP_FILE);
|
|
95
|
+
try {
|
|
96
|
+
const rows = buildSetupRows(rest);
|
|
97
|
+
const text = rows.map((r) => scrub(JSON.stringify(r), secrets)).join('\n');
|
|
98
|
+
writeFileSync(file, `${text}\n`, { flag: 'wx', mode: 0o600 });
|
|
99
|
+
return { ok: true, file, rows: rows.length };
|
|
100
|
+
} catch (err) {
|
|
101
|
+
return { ok: false, red: err.code === 'EEXIST' ? `setup: ${SETUP_FILE} already exists — never overwritten` : `setup: could not write ${SETUP_FILE} (${err.code ?? err.message})` };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 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).
|
|
107
|
+
* @param {string} flowDir
|
|
108
|
+
* @returns {{ present: false } | { present: true, rows: any[], unreadable: number }}
|
|
109
|
+
*/
|
|
110
|
+
export function readSetup(flowDir) {
|
|
111
|
+
const r = readFileInside(flowDir, SETUP_FILE);
|
|
112
|
+
if (!r.ok) return { present: false };
|
|
113
|
+
const rows = [];
|
|
114
|
+
let unreadable = 0;
|
|
115
|
+
for (const line of r.text.split('\n')) {
|
|
116
|
+
if (line.trim() === '') continue;
|
|
117
|
+
try { const row = JSON.parse(line); if (row && typeof row === 'object' && typeof row.kind === 'string') rows.push(row); else unreadable += 1; } catch { unreadable += 1; }
|
|
118
|
+
}
|
|
119
|
+
return { present: true, rows, unreadable };
|
|
120
|
+
}
|
package/types/authoring.d.ts
CHANGED
|
@@ -19,10 +19,18 @@ export function specHash({ proseText, declarationText, inputFactsText, readoutTe
|
|
|
19
19
|
};
|
|
20
20
|
/** Literal (never RegExp) key scrub; a secret under 8 chars is skipped so it can't blank ordinary text. */
|
|
21
21
|
export function scrub(text: any, secrets: any): any;
|
|
22
|
+
/**
|
|
23
|
+
* Does `text` carry any provider key VALUE in `env` (every slot, 8+ chars, literal)? The one test for a human's free text
|
|
24
|
+
* (the revise note) — the same literal rule `scrub` and `sweepForSecrets` use.
|
|
25
|
+
* @param {string} text @param {Record<string, string|undefined>} env
|
|
26
|
+
*/
|
|
27
|
+
export function textHasKey(text: string, env: Record<string, string | undefined>): boolean;
|
|
22
28
|
/** Count files directly inside `dir` that contain a secret literally. */
|
|
23
29
|
export function sweepForSecrets(dir: any, secrets: any): number;
|
|
24
30
|
/** Exact signed TTL with its unit, never rounded: 2h / 30m / 20s / 1500ms (the largest unit that divides it exactly). */
|
|
25
31
|
export function fmtTtl(ms: any): string;
|
|
32
|
+
/** Zero-based indexes of the heading lines in a readout's text (split on "\n"): the panel renders those bold. */
|
|
33
|
+
export function readoutHeadIndexes(text: any): any[];
|
|
26
34
|
/** Plain-text readout of what the human is about to sign. Pure. */
|
|
27
35
|
export function buildReadout({ declaration, arbiter, lines, name, modelId, costUsd, rounds, spendComplete, root, }: {
|
|
28
36
|
declaration: any;
|
|
@@ -54,7 +62,7 @@ export function readProseFile(file: any): {
|
|
|
54
62
|
*
|
|
55
63
|
* Test seam: `provider`/`rates`/`modelId` are injected; without them a live provider is built.
|
|
56
64
|
*/
|
|
57
|
-
export function draftToDir({ proseFile, dir, root, name, slot, model, budgetUsd, env, provider, rates, modelId, }: {
|
|
65
|
+
export function draftToDir({ proseFile, dir, root, name, slot, model, budgetUsd, env, provider, rates, modelId, reviseFrom, noteFile, }: {
|
|
58
66
|
proseFile: any;
|
|
59
67
|
dir: any;
|
|
60
68
|
root: any;
|
|
@@ -66,6 +74,8 @@ export function draftToDir({ proseFile, dir, root, name, slot, model, budgetUsd,
|
|
|
66
74
|
provider: any;
|
|
67
75
|
rates: any;
|
|
68
76
|
modelId: any;
|
|
77
|
+
reviseFrom: any;
|
|
78
|
+
noteFile: any;
|
|
69
79
|
}): Promise<{
|
|
70
80
|
ok: boolean;
|
|
71
81
|
wrote: boolean;
|
|
@@ -110,13 +120,15 @@ export function draftToDir({ proseFile, dir, root, name, slot, model, budgetUsd,
|
|
|
110
120
|
/**
|
|
111
121
|
* The human step, $0. Every refusal returns reds and writes NO flow.
|
|
112
122
|
* `signedBy` must come from the human's own invocation (the CLI's --signed-by/username).
|
|
123
|
+
* `sessionDir` (the panel's draft folder: card, notes, every change) widens the setup record; the CLI has none, so its record is the one plan folder.
|
|
113
124
|
*/
|
|
114
|
-
export function signDraft({ dir, approve, signedBy, signedAt, env, }: {
|
|
125
|
+
export function signDraft({ dir, approve, signedBy, signedAt, env, sessionDir, }: {
|
|
115
126
|
dir: any;
|
|
116
127
|
approve: any;
|
|
117
128
|
signedBy: any;
|
|
118
129
|
signedAt?: string | undefined;
|
|
119
130
|
env?: NodeJS.ProcessEnv | undefined;
|
|
131
|
+
sessionDir: any;
|
|
120
132
|
}): {
|
|
121
133
|
ok: boolean;
|
|
122
134
|
reds: any[];
|
|
@@ -124,9 +136,19 @@ export function signDraft({ dir, approve, signedBy, signedAt, env, }: {
|
|
|
124
136
|
ok: boolean;
|
|
125
137
|
flowDir: string;
|
|
126
138
|
signature: import("./types.js").Signature;
|
|
139
|
+
setup: {
|
|
140
|
+
ok: true;
|
|
141
|
+
file: string;
|
|
142
|
+
rows: number;
|
|
143
|
+
} | {
|
|
144
|
+
ok: false;
|
|
145
|
+
red: string;
|
|
146
|
+
};
|
|
127
147
|
};
|
|
128
148
|
export const DRAFT_BUDGET_USD: 0.1;
|
|
129
149
|
export const SPEC_HASH_FILE: "spec.hash";
|
|
150
|
+
/** 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. */
|
|
151
|
+
export const PANEL_DRAFTS_DIR: ".drafts";
|
|
130
152
|
/** Written when the end sweep finds a key in the draft dir; sign refuses a dir that has it. */
|
|
131
153
|
export const LEAK_MARKER_FILE: "scrub-leak.red";
|
|
132
154
|
export function SIGN_LINE(dir: any, hash: any): string;
|
package/types/books.d.ts
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* rewritten, not a book the arbiter reads. One row per process that works on
|
|
4
4
|
* the run (`src/liveness.js` `recordPid` builds the row).
|
|
5
5
|
* @param {string} runDir
|
|
6
|
-
* @param {{pid: number, startedAt: string, procStart: string|null, leg: 'run'|'resume'}} row
|
|
6
|
+
* @param {{pid: number, startedAt: string, procStart: string|null, leg: 'run'|'resume'|'continue'}} row
|
|
7
7
|
*/
|
|
8
8
|
export function appendPidRow(runDir: string, row: {
|
|
9
9
|
pid: number;
|
|
10
10
|
startedAt: string;
|
|
11
11
|
procStart: string | null;
|
|
12
|
-
leg: "run" | "resume";
|
|
12
|
+
leg: "run" | "resume" | "continue";
|
|
13
13
|
}): void;
|
|
14
14
|
/**
|
|
15
15
|
* The one reader of `pids.jsonl` (same tolerant `readLines` every book uses).
|
|
@@ -103,3 +103,15 @@ export function appendHistory(flowDir: string, row: Record<string, any>): void;
|
|
|
103
103
|
* @returns {any[]}
|
|
104
104
|
*/
|
|
105
105
|
export function readHistory(flowDir: string): any[];
|
|
106
|
+
/**
|
|
107
|
+
* The ONE rule for "how did this run end": the LAST history row of the run (a run that was stopped or cap-halted and
|
|
108
|
+
* continued has one row per leg; history stays append-only). `null` when the run has no row yet, and also while a
|
|
109
|
+
* continue is in flight: the last row is a halt row, `halt.json` is gone (the continue consumed it by rename to
|
|
110
|
+
* `halt.<n>.consumed.json`) — an older halt row is then not the run's end. A run halted before halt records existed has
|
|
111
|
+
* no consumed file, so its halt row stays final.
|
|
112
|
+
* @param {any[]} historyRows `readHistory`'s rows @param {string} runDir @param {string} runId
|
|
113
|
+
* @returns {any|null}
|
|
114
|
+
*/
|
|
115
|
+
export function endRow(historyRows: any[], runDir: string, runId: string): any | null;
|
|
116
|
+
/** The two outcomes a run can be continued from (M4e amendment 4 item 4): the runner leaves `halt.json` for them. */
|
|
117
|
+
export const HALT_OUTCOMES: readonly string[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first step granting a verb that has no wired implementation, or null. (A verb absent from the catalogue entirely is caught earlier,
|
|
3
|
+
* at declaration validation inside `readFlow`.)
|
|
4
|
+
* @param {Record<string, any>} declaration `readFlow`'s declaration
|
|
5
|
+
* @returns {{step: {emits: string, fromLine: number}, verb: string}|null}
|
|
6
|
+
*/
|
|
7
|
+
export function findUnwiredVerbStep(declaration: Record<string, any>): {
|
|
8
|
+
step: {
|
|
9
|
+
emits: string;
|
|
10
|
+
fromLine: number;
|
|
11
|
+
};
|
|
12
|
+
verb: string;
|
|
13
|
+
} | null;
|
|
14
|
+
/** The preflight refusal sentence for an unwired verb, in the words the run's own preflight uses. @param {{step: {emits: string, fromLine: number}, verb: string}} found */
|
|
15
|
+
export function unwiredRed(found: {
|
|
16
|
+
step: {
|
|
17
|
+
emits: string;
|
|
18
|
+
fromLine: number;
|
|
19
|
+
};
|
|
20
|
+
verb: string;
|
|
21
|
+
}): string;
|
|
22
|
+
/**
|
|
23
|
+
* Can this flow run? `read` is `readFlow`'s result (a failed read — signature, tampering, missing files — is a refusal with its own reds).
|
|
24
|
+
* @param {any} read
|
|
25
|
+
* @returns {{ok: true} | {ok: false, red: string}}
|
|
26
|
+
*/
|
|
27
|
+
export function canFlowRun(read: any): {
|
|
28
|
+
ok: true;
|
|
29
|
+
} | {
|
|
30
|
+
ok: false;
|
|
31
|
+
red: string;
|
|
32
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `~/.config/fwdloop`, or an injected override — the ONE definition of fwdloop's config home
|
|
3
|
+
* (the keys file lives beside config.json; `src/keysfile.js` calls this, never its own copy).
|
|
4
|
+
* @param {string} [home] test seam; production never passes it
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
export function configHome(home?: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} [home]
|
|
10
|
+
* @returns {string} `<home>/config.json`
|
|
11
|
+
*/
|
|
12
|
+
export function configPath(home?: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* The home a model-call price lookup reads, resolved at the door. Under a test process
|
|
15
|
+
* (`NODE_ENV=test` or `node --test`) only an explicit `FWDLOOP_CONFIG_HOME` is read; with none, NO
|
|
16
|
+
* config is read at all — a test never touches the real `~/.config/fwdloop`. Outside tests: the real home.
|
|
17
|
+
* @returns {{ skip: boolean, home: string|undefined }}
|
|
18
|
+
*/
|
|
19
|
+
export function configDoorHome(): {
|
|
20
|
+
skip: boolean;
|
|
21
|
+
home: string | undefined;
|
|
22
|
+
};
|
|
23
|
+
/** One provider field's value problem in plain words, or null when fine. Shared by validateDoc and the route. */
|
|
24
|
+
export function providerFieldProblem(field: any, v: any): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* The parsed, validated document. `{}` when the file is missing. THROWS ConfigError when the file is
|
|
27
|
+
* there but unreadable, not a JSON object, or holds an invalid known field — never `{}` for those.
|
|
28
|
+
* @param {{ home?: string }} [opts]
|
|
29
|
+
* @returns {Record<string, any>}
|
|
30
|
+
*/
|
|
31
|
+
export function readConfig(opts?: {
|
|
32
|
+
home?: string;
|
|
33
|
+
}): Record<string, any>;
|
|
34
|
+
/**
|
|
35
|
+
* Apply a patch and write. `null` deletes a field. Top-level fields shallow-merge; `prices` merges
|
|
36
|
+
* per slot, then per field (`prices.<slot> = null` clears the slot, `prices.<slot>.<field> = null`
|
|
37
|
+
* clears the field). Everything is validated before anything is written; a refusal leaves the file
|
|
38
|
+
* untouched.
|
|
39
|
+
* @param {Record<string, any>} patch
|
|
40
|
+
* @param {{ home?: string }} [opts]
|
|
41
|
+
* @returns {Record<string, any>} the document as written
|
|
42
|
+
*/
|
|
43
|
+
export function updateConfig(patch: Record<string, any>, opts?: {
|
|
44
|
+
home?: string;
|
|
45
|
+
}): Record<string, any>;
|
|
46
|
+
/** A plain-words config failure (bad value, secret-shaped string, unreadable file). */
|
|
47
|
+
export class ConfigError extends Error {
|
|
48
|
+
}
|
|
49
|
+
/** Fields a config file may hold; `cachedInPerM` stays so an older file still loads, but nothing prices from it. */
|
|
50
|
+
export const PRICE_FIELDS: readonly string[];
|
|
51
|
+
export const PROVIDER_FIELDS: readonly string[];
|
|
52
|
+
export function cleanBaseUrl(v: any): any;
|
package/types/declaration.d.ts
CHANGED
|
@@ -19,6 +19,11 @@ export function checkShapes(declaration: any): {
|
|
|
19
19
|
reds: any[];
|
|
20
20
|
shapedSteps: number;
|
|
21
21
|
};
|
|
22
|
+
/** M4e amendment 6 item 1 (+ amendment 7 item 1): a step's check cannot fight its own job line. Sections must be found in the line's words
|
|
23
|
+
* (case-insensitive substring of the goal, first occurrence) and in the line's order; `maxWords` must equal the line's
|
|
24
|
+
* own guardrail limit when that guardrail states one. A shape key a note may still change (mustCarry, linesPerInvoice)
|
|
25
|
+
* is untouched. Pushes reds naming the step and both orders/values. */
|
|
26
|
+
export function checkShapeFitsJobLine(step: any, i: any, lines: any, reds: any): void;
|
|
22
27
|
/**
|
|
23
28
|
* Validate a drafter declaration against the M1 closed schema. `arbiter`
|
|
24
29
|
* and `lines` are `parseSignedText`'s output; `catalogue` is a plain array
|
|
@@ -31,7 +36,9 @@ export function checkShapes(declaration: any): {
|
|
|
31
36
|
* by name, even if the catalogue lists it. Omitted = no wired check (existing callers).
|
|
32
37
|
* `verbatimGoals` (optional boolean; M6a amendment 1): when true, a step naming a signed line must carry that line's
|
|
33
38
|
* text as its goal, exactly. Off by default so already-signed flows keep running; draft and sign turn it on.
|
|
34
|
-
*
|
|
39
|
+
* `fitJobLine` (optional boolean; M4e amendment 6): when true, a `close.shape` whose sections/maxWords fight the step's job
|
|
40
|
+
* line or its guardrail is a red. Draft, change and sign turn it on; readFlow does not (signed flows keep running).
|
|
41
|
+
* @param {{ arbiter?: unknown, lines?: unknown, catalogue?: unknown, wired?: Set<string>, verbatimGoals?: boolean, fitJobLine?: boolean }} [context]
|
|
35
42
|
* @returns {ValidateDeclarationResult}
|
|
36
43
|
*/
|
|
37
44
|
export function validateDeclaration(declaration: unknown, context?: {
|
|
@@ -40,6 +47,7 @@ export function validateDeclaration(declaration: unknown, context?: {
|
|
|
40
47
|
catalogue?: unknown;
|
|
41
48
|
wired?: Set<string>;
|
|
42
49
|
verbatimGoals?: boolean;
|
|
50
|
+
fitJobLine?: boolean;
|
|
43
51
|
}): ValidateDeclarationResult;
|
|
44
52
|
/** @typedef {import('./types.js').Arbiter} Arbiter */
|
|
45
53
|
/** @typedef {import('./types.js').SignedLine} SignedLine */
|
package/types/drafter.d.ts
CHANGED
|
@@ -114,25 +114,40 @@ export function buildSystemPrompt({ menu, lines, arbiter, factsInfo }: {
|
|
|
114
114
|
* lines do not exist in the grammar. A step naming no signed line gets a fixed placeholder, never model prose.
|
|
115
115
|
*/
|
|
116
116
|
export function goalForLine(fromLine: any, lines: any): any;
|
|
117
|
+
/**
|
|
118
|
+
* M4e amendment 3 item 3 (revise the plan): the first round's user text when the human asked for a change. The model sees the
|
|
119
|
+
* current plan (its own fields only: no machine-set `goal`, no `inputFacts`) and the note; the rules above still hold, and the
|
|
120
|
+
* validator below is the ONE check that refuses a plan that touches what only the card may set.
|
|
121
|
+
*/
|
|
122
|
+
export function reviseOpening(plan: any, note: any): string;
|
|
117
123
|
/**
|
|
118
124
|
* Draft one declaration. `provider`/`rates`/`modelId` may be injected (tests,
|
|
119
125
|
* the batch); otherwise a live provider is built from `slot` (throws on a bad
|
|
120
126
|
* key — the batch preflights first). `budgetUsd` is this draft's hard cap.
|
|
121
127
|
* `makeProviderFn` is a test seam: the live-provider factory (default `makeProvider`).
|
|
128
|
+
* `onBook` (M4e piece 2a, scope 13): called right BEFORE each provider call (`inFlight: true`, the call counted in
|
|
129
|
+
* `calls`) and right AFTER it (`inFlight: false`), with what is known so far, so the caller can book spend per call
|
|
130
|
+
* and a draft killed between rounds still has its paid rounds on disk. It may throw before a call (nothing is
|
|
131
|
+
* spent then and the draft stops); after a call it is the caller's to keep from throwing.
|
|
132
|
+
* `revise` (M4e amendment 3 item 3): `{ plan, note }` — the current green plan and the human's note; the first round then asks for the
|
|
133
|
+
* revised plan. Everything else (the $0 gates, the validator, the budget, the metering) is the same code path as a first draft.
|
|
122
134
|
*
|
|
123
135
|
* @returns {Promise<{ok:boolean, declaration:object|null, reds:string[], rounds:number, calls:number, spendComplete:boolean,
|
|
124
|
-
* costUsd:number|null, modelReturned:string|null, modelId?:string, tokens?:object|null, structureRetries:number, revisions:number, stop:string|null, log:object[]}>}
|
|
136
|
+
* costUsd:number|null, modelReturned:string|null, modelId?:string, price?:object|null, tokens?:object|null, structureRetries:number, revisions:number, stop:string|null, log:object[]}>}
|
|
125
137
|
*/
|
|
126
|
-
export function draft({ proseText, slot, model, provider: injected, rates: injectedRates, modelId: injectedModelId, budgetUsd, skills, makeProviderFn, }: {
|
|
138
|
+
export function draft({ proseText, slot, model, provider: injected, rates: injectedRates, modelId: injectedModelId, env, budgetUsd, skills, makeProviderFn, onBook, revise, }: {
|
|
127
139
|
proseText: any;
|
|
128
140
|
slot?: string | undefined;
|
|
129
141
|
model: any;
|
|
130
142
|
provider: any;
|
|
131
143
|
rates: any;
|
|
132
144
|
modelId: any;
|
|
145
|
+
env: any;
|
|
133
146
|
budgetUsd?: number | undefined;
|
|
134
147
|
skills?: readonly string[] | undefined;
|
|
135
148
|
makeProviderFn?: typeof makeProvider | undefined;
|
|
149
|
+
onBook: any;
|
|
150
|
+
revise: any;
|
|
136
151
|
}): Promise<{
|
|
137
152
|
ok: boolean;
|
|
138
153
|
declaration: object | null;
|
|
@@ -143,6 +158,7 @@ export function draft({ proseText, slot, model, provider: injected, rates: injec
|
|
|
143
158
|
costUsd: number | null;
|
|
144
159
|
modelReturned: string | null;
|
|
145
160
|
modelId?: string;
|
|
161
|
+
price?: object | null;
|
|
146
162
|
tokens?: object | null;
|
|
147
163
|
structureRetries: number;
|
|
148
164
|
revisions: number;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* (Re)write `<dir>/draft-spend.json` atomically (tmp + rename), so a reader never sees a torn file.
|
|
3
|
+
* @param {string} dir the draft dir
|
|
4
|
+
* @param {Record<string, any>} rec
|
|
5
|
+
*/
|
|
6
|
+
export function writeDraftSpend(dir: string, rec: Record<string, any>): void;
|
|
7
|
+
/**
|
|
8
|
+
* The draft dir's live spend record as a spend row, or null when the file is not there. A file that is there but
|
|
9
|
+
* cannot be read is unknown spend: one unmetered call (priced at the ceiling), never $0 and never skipped.
|
|
10
|
+
* @param {string} dir
|
|
11
|
+
* @returns {Record<string, any>|null}
|
|
12
|
+
*/
|
|
13
|
+
export function readDraftSpend(dir: string): Record<string, any> | null;
|
|
14
|
+
/**
|
|
15
|
+
* Every spend row of one run/draft dir: its `spend.jsonl` rows; and, only when there are none, the draft dir's
|
|
16
|
+
* `draft-spend.json` record (see the file header: a finished draft's spend.jsonl supersedes it).
|
|
17
|
+
* @param {string} dir
|
|
18
|
+
* @returns {Record<string, any>[]}
|
|
19
|
+
*/
|
|
20
|
+
export function readDirSpendRows(dir: string): Record<string, any>[];
|
|
21
|
+
export const DRAFT_SPEND_FILE: "draft-spend.json";
|