fwdloop 0.4.1 → 0.5.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.
@@ -0,0 +1,375 @@
1
+ /**
2
+ * Strip an `hf:` routing prefix and look up `suffix` with `Object.hasOwn` —
3
+ * never plain truthiness/`in`, which would let a suffix like `"constructor"`
4
+ * resolve to an inherited `Object.prototype` member.
5
+ */
6
+ export function lookupRate(modelId: any, ratesTable?: {
7
+ 'zai-org/GLM-5.2': {
8
+ in: number;
9
+ out: number;
10
+ source: string;
11
+ };
12
+ 'moonshotai/Kimi-K3': {
13
+ in: number;
14
+ out: number;
15
+ source: string;
16
+ };
17
+ 'zai-org/GLM-5.3-Flash': {
18
+ in: number;
19
+ out: number;
20
+ source: string;
21
+ };
22
+ 'zai-org/GLM-4.7-Flash': {
23
+ in: number;
24
+ out: number;
25
+ source: string;
26
+ };
27
+ 'Qwen/Qwen3.8-27B': {
28
+ in: number;
29
+ out: number;
30
+ source: string;
31
+ };
32
+ 'openai/gpt-oss-120b': {
33
+ in: number;
34
+ out: number;
35
+ source: string;
36
+ };
37
+ 'nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4': {
38
+ in: number;
39
+ out: number;
40
+ source: string;
41
+ };
42
+ 'syn:large:text': {
43
+ in: number;
44
+ out: number;
45
+ source: string;
46
+ };
47
+ 'syn:small:text': {
48
+ in: number;
49
+ out: number;
50
+ source: string;
51
+ };
52
+ 'deepseek-v4-flash': {
53
+ in: number;
54
+ out: number;
55
+ source: string;
56
+ };
57
+ 'deepseek-flash': {
58
+ in: number;
59
+ out: number;
60
+ source: string;
61
+ };
62
+ 'deepseek-v4-pro': {
63
+ in: number;
64
+ out: number;
65
+ source: string;
66
+ };
67
+ }): {
68
+ suffix: string;
69
+ rates: any;
70
+ } | null;
71
+ /** Resolve a modelId to its hand-entered rate row, or throw — a caller here
72
+ * must never carry forward a 0 rate. */
73
+ export function resolveModelRate(modelId: any, ratesTable?: {
74
+ 'zai-org/GLM-5.2': {
75
+ in: number;
76
+ out: number;
77
+ source: string;
78
+ };
79
+ 'moonshotai/Kimi-K3': {
80
+ in: number;
81
+ out: number;
82
+ source: string;
83
+ };
84
+ 'zai-org/GLM-5.3-Flash': {
85
+ in: number;
86
+ out: number;
87
+ source: string;
88
+ };
89
+ 'zai-org/GLM-4.7-Flash': {
90
+ in: number;
91
+ out: number;
92
+ source: string;
93
+ };
94
+ 'Qwen/Qwen3.8-27B': {
95
+ in: number;
96
+ out: number;
97
+ source: string;
98
+ };
99
+ 'openai/gpt-oss-120b': {
100
+ in: number;
101
+ out: number;
102
+ source: string;
103
+ };
104
+ 'nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4': {
105
+ in: number;
106
+ out: number;
107
+ source: string;
108
+ };
109
+ 'syn:large:text': {
110
+ in: number;
111
+ out: number;
112
+ source: string;
113
+ };
114
+ 'syn:small:text': {
115
+ in: number;
116
+ out: number;
117
+ source: string;
118
+ };
119
+ 'deepseek-v4-flash': {
120
+ in: number;
121
+ out: number;
122
+ source: string;
123
+ };
124
+ 'deepseek-flash': {
125
+ in: number;
126
+ out: number;
127
+ source: string;
128
+ };
129
+ 'deepseek-v4-pro': {
130
+ in: number;
131
+ out: number;
132
+ source: string;
133
+ };
134
+ }): {
135
+ suffix: string;
136
+ rates: any;
137
+ };
138
+ /**
139
+ * The ceiling cost (USD) of one round of `modelId` — never 0, never null.
140
+ * A known model prices at its own rate; an unknown/missing model prices at
141
+ * the HIGHEST in/out rate anywhere in `ratesTable`.
142
+ */
143
+ export function ceilingCostUsd(modelId: any, ratesTable?: {
144
+ 'zai-org/GLM-5.2': {
145
+ in: number;
146
+ out: number;
147
+ source: string;
148
+ };
149
+ 'moonshotai/Kimi-K3': {
150
+ in: number;
151
+ out: number;
152
+ source: string;
153
+ };
154
+ 'zai-org/GLM-5.3-Flash': {
155
+ in: number;
156
+ out: number;
157
+ source: string;
158
+ };
159
+ 'zai-org/GLM-4.7-Flash': {
160
+ in: number;
161
+ out: number;
162
+ source: string;
163
+ };
164
+ 'Qwen/Qwen3.8-27B': {
165
+ in: number;
166
+ out: number;
167
+ source: string;
168
+ };
169
+ 'openai/gpt-oss-120b': {
170
+ in: number;
171
+ out: number;
172
+ source: string;
173
+ };
174
+ 'nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4': {
175
+ in: number;
176
+ out: number;
177
+ source: string;
178
+ };
179
+ 'syn:large:text': {
180
+ in: number;
181
+ out: number;
182
+ source: string;
183
+ };
184
+ 'syn:small:text': {
185
+ in: number;
186
+ out: number;
187
+ source: string;
188
+ };
189
+ 'deepseek-v4-flash': {
190
+ in: number;
191
+ out: number;
192
+ source: string;
193
+ };
194
+ 'deepseek-flash': {
195
+ in: number;
196
+ out: number;
197
+ source: string;
198
+ };
199
+ 'deepseek-v4-pro': {
200
+ in: number;
201
+ out: number;
202
+ source: string;
203
+ };
204
+ }): number;
205
+ /**
206
+ * Compare the model asked for against the model actually served.
207
+ * `match` identical; `prefix` identical once a routing prefix is stripped;
208
+ * `alias` a declared `syn:` alias resolving to a concrete model (expected);
209
+ * `substituted` a concrete request served by a DIFFERENT concrete model
210
+ * (the one that matters — the signed hash records the request, not what
211
+ * ran); `unreported` the provider told us nothing.
212
+ */
213
+ export function classifyModelId(requested: any, returned: any): "unreported" | "match" | "prefix" | "alias" | "substituted";
214
+ /**
215
+ * Sum every round of one model attempt into one meter. Money honesty in the
216
+ * strict direction: if ANY round has no priced cost, `costUsd` is `null` —
217
+ * never 0, never a partial sum passed off as complete. `rounds` records how
218
+ * many rounds folded in.
219
+ */
220
+ export function sumMeterings(events: any): {
221
+ rounds: number;
222
+ tokens: null;
223
+ costUsd: null;
224
+ model: null;
225
+ rateSource: null;
226
+ } | {
227
+ rounds: number;
228
+ tokens: {
229
+ inputTokens: number;
230
+ outputTokens: number;
231
+ cacheReadTokens: number;
232
+ cacheCreationTokens: number;
233
+ };
234
+ costUsd: number | null;
235
+ model: any;
236
+ rateSource: any;
237
+ };
238
+ /** Append one spend row, stamping `modelMatch` — the one writer for
239
+ * spend.jsonl. Warns (never throws) on a `substituted` model. */
240
+ export function appendSpendRow(path: any, row: any): void;
241
+ /** Throws once total spend (every null row repriced at its ceiling) is at
242
+ * or over `capUsd`. */
243
+ export function assertUnderGlobalCap(path: any, capUsd: any): number;
244
+ /**
245
+ * $0 preflight before any draft round and before any ledger write: the key
246
+ * env var for `slotName` must be set, non-empty, and free of any
247
+ * whitespace/control character (F26, 2026-09-21: a two-line `pass` entry
248
+ * exported a key with a trailing newline — still truthy, so a bare
249
+ * `if (!apiKey)` never caught it). Never throws — `{ ok:true }` or
250
+ * `{ ok:false, message }`; `message` never includes the key's value.
251
+ *
252
+ * @param {string} slotName
253
+ * @param {Record<string,string|undefined>} [env]
254
+ */
255
+ export function checkKeyPreflight(slotName: string, env?: Record<string, string | undefined>): {
256
+ ok: boolean;
257
+ message: string;
258
+ } | {
259
+ ok: boolean;
260
+ message?: undefined;
261
+ };
262
+ /**
263
+ * Build a provider for the given slot. Throws (never defaults) on an
264
+ * unknown slot, a missing/bad env key, or a model with no hand-entered
265
+ * rate. Returns `{ provider, rates, modelId, suffix, slot }`.
266
+ *
267
+ * `timeoutMs`/`deadlineMs` bound a silent socket (BA-18 idle) and a
268
+ * zombie-stream total wall clock (BA-19), respectively — see
269
+ * `LIVE_PROVIDER_OPTIONS` in src/model-step.js, the one writer of the live
270
+ * values this project uses.
271
+ *
272
+ * @param {string} slotName
273
+ * @param {{ model?: string, timeoutMs?: number, deadlineMs?: number }} [options]
274
+ * @returns {{ provider: any, rates: {in:number, out:number}, modelId: string, suffix: string, slot: string }}
275
+ */
276
+ export function makeProvider(slotName: string, options?: {
277
+ model?: string;
278
+ timeoutMs?: number;
279
+ deadlineMs?: number;
280
+ }): {
281
+ provider: any;
282
+ rates: {
283
+ in: number;
284
+ out: number;
285
+ };
286
+ modelId: string;
287
+ suffix: string;
288
+ slot: string;
289
+ };
290
+ /** Provider slots. Frozen — add a new slot here, never inline a baseUrl/env
291
+ * var elsewhere. `deepseek` is the baseline (F12); `synthetic` is second. */
292
+ export const PROVIDER_SLOTS: Readonly<{
293
+ synthetic: Readonly<{
294
+ baseUrl: "https://api.synthetic.new/openai/v1";
295
+ envVar: "SYNTHETIC_API_KEY";
296
+ defaultModel: "hf:Qwen/Qwen3.8-27B";
297
+ legacyMaxTokens: false;
298
+ }>;
299
+ deepseek: Readonly<{
300
+ baseUrl: "https://api.deepseek.com";
301
+ envVar: "DEEPSEEK_API_KEY";
302
+ defaultModel: "deepseek-flash";
303
+ legacyMaxTokens: true;
304
+ }>;
305
+ }>;
306
+ /**
307
+ * List-rate ceilings (USD/1K tokens), hand-entered — see
308
+ * poc/m0/spend.mjs@76a3607 for sourcing notes per row. A rate changes by
309
+ * editing a row here, never a second table or a runtime flag (hamr's
310
+ * ruling, 2026-09-21).
311
+ */
312
+ export const RATES_BY_SUFFIX: {
313
+ 'zai-org/GLM-5.2': {
314
+ in: number;
315
+ out: number;
316
+ source: string;
317
+ };
318
+ 'moonshotai/Kimi-K3': {
319
+ in: number;
320
+ out: number;
321
+ source: string;
322
+ };
323
+ 'zai-org/GLM-5.3-Flash': {
324
+ in: number;
325
+ out: number;
326
+ source: string;
327
+ };
328
+ 'zai-org/GLM-4.7-Flash': {
329
+ in: number;
330
+ out: number;
331
+ source: string;
332
+ };
333
+ 'Qwen/Qwen3.8-27B': {
334
+ in: number;
335
+ out: number;
336
+ source: string;
337
+ };
338
+ 'openai/gpt-oss-120b': {
339
+ in: number;
340
+ out: number;
341
+ source: string;
342
+ };
343
+ 'nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4': {
344
+ in: number;
345
+ out: number;
346
+ source: string;
347
+ };
348
+ 'syn:large:text': {
349
+ in: number;
350
+ out: number;
351
+ source: string;
352
+ };
353
+ 'syn:small:text': {
354
+ in: number;
355
+ out: number;
356
+ source: string;
357
+ };
358
+ 'deepseek-v4-flash': {
359
+ in: number;
360
+ out: number;
361
+ source: string;
362
+ };
363
+ 'deepseek-flash': {
364
+ in: number;
365
+ out: number;
366
+ source: string;
367
+ };
368
+ 'deepseek-v4-pro': {
369
+ in: number;
370
+ out: number;
371
+ source: string;
372
+ };
373
+ };
374
+ export const CEILING_INPUT_TOKENS: 32000;
375
+ export const CEILING_OUTPUT_TOKENS: 16000;
@@ -0,0 +1,161 @@
1
+ /** @param {string|null|undefined} modelId */
2
+ export function resolveCeilingUsd(modelId: string | null | undefined): any;
3
+ /** @param {{goal:string, primitives?:string[], reads?:Record<string,any>, gap?:string|null}} opts */
4
+ export function buildExecutorContext(opts: {
5
+ goal: string;
6
+ primitives?: string[];
7
+ reads?: Record<string, any>;
8
+ gap?: string | null;
9
+ }): Readonly<{
10
+ goal: string;
11
+ primitives: readonly string[];
12
+ reads: Readonly<{
13
+ [x: string]: any;
14
+ }>;
15
+ gap: string | null;
16
+ }>;
17
+ /**
18
+ * @param {unknown} value
19
+ * @param {string} [path]
20
+ * @returns {string|null} a description of the first violation found, or null when clean.
21
+ */
22
+ export function findForbiddenInContext(value: unknown, path?: string): string | null;
23
+ /** Also checks the executor's serialised (JSON round-tripped) form for the
24
+ * declared shape's own heading strings — a leak that survived key-name
25
+ * scanning (e.g. a heading baked into `goal` text) still needs to be
26
+ * physically absent, per M2 scope item 3. */
27
+ export function findForbiddenHeadingText(context: any, headingStrings: any): string | null;
28
+ export function normaliseGap(red: any): string;
29
+ /** A run dir that already holds `ask.json`/`answer.json` from an earlier run
30
+ * is a stale-answer hazard — refuse, never silently proceed or delete. */
31
+ export function checkFreshRunDir(runDir: any): {
32
+ ok: boolean;
33
+ red: string;
34
+ } | {
35
+ ok: boolean;
36
+ red?: undefined;
37
+ };
38
+ /** Copy each named source into `<runDir>/inputs/`, hash the FROZEN copy
39
+ * (never the original again after this), write `inputs.json`. An
40
+ * unreadable source refuses by name. */
41
+ export function freezeInputs(runDir: any, sources: any): {
42
+ ok: boolean;
43
+ red: string;
44
+ manifest?: undefined;
45
+ } | {
46
+ ok: boolean;
47
+ manifest: {
48
+ id: any;
49
+ source: any;
50
+ frozen: string;
51
+ sha256: string;
52
+ bytes: number;
53
+ }[];
54
+ red?: undefined;
55
+ };
56
+ /** The send target's directory must resolve (lexically AND after
57
+ * realpath — a symlink inside the repo pointing outside it must not pass)
58
+ * inside the repo, and be writable. Re-checked at write time, not just at
59
+ * preflight (time-of-check vs time-of-use, M0's own fix).
60
+ * @param {string} target
61
+ * @returns {{ok:true, dir:string} | {ok:false, red:string}} */
62
+ export function checkSendDestination(target: string): {
63
+ ok: true;
64
+ dir: string;
65
+ } | {
66
+ ok: false;
67
+ red: string;
68
+ };
69
+ /** The one writer for a run's artifact files. Refuses (throws) to overwrite
70
+ * an existing artifact id within a run UNLESS the caller explicitly asks
71
+ * for it (`overwrite: true` — the ask-redo path's own deliberate
72
+ * replacement of a step it is re-running; every other call site accepts
73
+ * the default and gets the safety refusal). */
74
+ export function writeArtifact(runDir: any, id: any, artifact: any, { overwrite }?: {
75
+ overwrite?: boolean | undefined;
76
+ }): void;
77
+ /** Reads one artifact back off disk; `undefined` when it was never written
78
+ * (mirrors the in-memory map's own "absent" shape for a not-yet-produced
79
+ * read). */
80
+ export function readArtifact(runDir: any, id: any): any;
81
+ /**
82
+ * @param {object} opts
83
+ * @param {string} opts.root - the flows root directory (M1's `readFlow` root).
84
+ * @param {string} opts.name - the flow name.
85
+ * @param {string} opts.runId
86
+ * @param {Array<{id:string, path:string}>} opts.sources - the real files to freeze for this run.
87
+ * @param {unknown} opts.catalogue - passed straight through to `readFlow`.
88
+ * @param {(executorContext:object, grantedTools:Record<string,any>, stepMeta?:{class:string|null}) => Promise<{ok:boolean, artifact?:unknown, costUsd:number|null, red?:string, transport?:boolean, model?:string, modelMatch?:boolean}>} opts.modelStep
89
+ * @param {(opts:{question:string, evidence:unknown, runDir:string}) => Promise<{decision:'accept'|'reject'|'rerun'|'timeout', reason?:string}>} opts.askStep
90
+ * @param {(target:string, filename:string, content:unknown) => Promise<{ok:boolean, red?:string, bytes?:number}>} opts.sendStep
91
+ * @param {Record<string, any>} [opts.primitives] - injected primitive implementations, keyed by catalogue verb.
92
+ * @param {() => string} [opts.clock] - returns the current ISO timestamp; defaults to the wall clock.
93
+ * @param {string} opts.businessDate - the run's explicit "as of today", never the wall clock.
94
+ * @param {number} [opts.ceilingUsd] - per-attempt ceiling override; defaults to `resolveCeilingUsd(null)`.
95
+ */
96
+ export function runFlow({ root, name, runId, sources, catalogue, modelStep, askStep, sendStep, primitives, clock, businessDate, ceilingUsd, }: {
97
+ root: string;
98
+ name: string;
99
+ runId: string;
100
+ sources: Array<{
101
+ id: string;
102
+ path: string;
103
+ }>;
104
+ catalogue: unknown;
105
+ modelStep: (executorContext: object, grantedTools: Record<string, any>, stepMeta?: {
106
+ class: string | null;
107
+ }) => Promise<{
108
+ ok: boolean;
109
+ artifact?: unknown;
110
+ costUsd: number | null;
111
+ red?: string;
112
+ transport?: boolean;
113
+ model?: string;
114
+ modelMatch?: boolean;
115
+ }>;
116
+ askStep: (opts: {
117
+ question: string;
118
+ evidence: unknown;
119
+ runDir: string;
120
+ }) => Promise<{
121
+ decision: "accept" | "reject" | "rerun" | "timeout";
122
+ reason?: string;
123
+ }>;
124
+ sendStep: (target: string, filename: string, content: unknown) => Promise<{
125
+ ok: boolean;
126
+ red?: string;
127
+ bytes?: number;
128
+ }>;
129
+ primitives?: Record<string, any> | undefined;
130
+ clock?: (() => string) | undefined;
131
+ businessDate: string;
132
+ ceilingUsd?: number | undefined;
133
+ }): Promise<{
134
+ outcome: string;
135
+ red: string | undefined;
136
+ spentUsd: number;
137
+ } | {
138
+ outcome: string;
139
+ red: string;
140
+ reds: string[];
141
+ runDir?: undefined;
142
+ artifacts?: undefined;
143
+ spentUsd?: undefined;
144
+ auditRows?: undefined;
145
+ } | {
146
+ outcome: string;
147
+ runDir: string;
148
+ artifacts: {};
149
+ spentUsd: number;
150
+ auditRows: any[];
151
+ red?: undefined;
152
+ reds?: undefined;
153
+ }>;
154
+ /** The strike governor — a run-owned constant, never raised by a caller,
155
+ * never carried in the arbiter block or the declaration (M2 scope item 5,
156
+ * signed 2026-09-24 "2A"). */
157
+ export const STRIKE_LIMIT: 2;
158
+ /** The hard attempt fallback per step (bareloop's rule: "the count is a
159
+ * fallback, never the governor" — it never raises the strike bound, it only
160
+ * bounds the case the strike governor doesn't catch). */
161
+ export const MAX_ATTEMPTS: 4;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @param {string} target - a signed arbiter target, e.g. `file:poc/m0/out`.
3
+ * @param {string} filename
4
+ * @param {unknown} content - JSON-serialised before writing.
5
+ * @returns {Promise<{ok:true, path:string, bytes:number} | {ok:false, red:string}>}
6
+ */
7
+ export function sendViaPrimitive(target: string, filename: string, content: unknown): Promise<{
8
+ ok: true;
9
+ path: string;
10
+ bytes: number;
11
+ } | {
12
+ ok: false;
13
+ red: string;
14
+ }>;
package/types/types.d.ts CHANGED
@@ -235,3 +235,76 @@ export type ReadFlowRed = {
235
235
  reds: string[];
236
236
  };
237
237
  export type ReadFlowResult = ReadFlowOk | ReadFlowRed;
238
+ /**
239
+ * M2 piece 1 — a step's fresh executor context: "goal in, gap back". No
240
+ * other field may ever appear here (src/runner.js's construction test
241
+ * proves it) — in particular never the step's close, its shape, the cap, or
242
+ * the strike count.
243
+ */
244
+ export type ExecutorContext = {
245
+ goal: string;
246
+ primitives: string[];
247
+ reads: Record<string, any>;
248
+ gap: string | null;
249
+ };
250
+ /**
251
+ * A green-class field value: `value` is the number/ISO-date/text claimed;
252
+ * `cite` resolves it (src/closers.js's own grammar).
253
+ */
254
+ export type GreenField = {
255
+ value: string | number;
256
+ cite: string;
257
+ };
258
+ export type GreenArtifact = {
259
+ fields: Record<string, GreenField>;
260
+ };
261
+ export type SoftgreenArtifact = {
262
+ text: string;
263
+ lines?: string[];
264
+ };
265
+ /**
266
+ * The result of any of src/closers.js's close functions.
267
+ */
268
+ export type CloseVerdict = {
269
+ verdict: "green" | "red" | "hitl" | "unparseable" | "crash";
270
+ red?: string | null | undefined;
271
+ reds?: string[] | undefined;
272
+ };
273
+ /**
274
+ * One row appended to `runs/<run-id>/audit.jsonl` (M2 scope item 9).
275
+ */
276
+ export type AuditRow = {
277
+ step: string | null;
278
+ attempt: number;
279
+ class: string | null;
280
+ verdict: string;
281
+ gap: string | null;
282
+ usd: number | null;
283
+ spendComplete: boolean;
284
+ wallMs: number;
285
+ model: string | null;
286
+ modelMatch: boolean | null;
287
+ strike: boolean;
288
+ };
289
+ /**
290
+ * One row appended to `flows/<name>/history.jsonl` (M2 scope item 9).
291
+ */
292
+ export type HistoryRow = {
293
+ runId: string;
294
+ at: string;
295
+ outcome: string;
296
+ spentUsd: number | null;
297
+ spendComplete: boolean;
298
+ capUsd: number | null;
299
+ wallMs: number;
300
+ signatureHash: string | null;
301
+ };
302
+ export type RunFlowResult = {
303
+ outcome: string;
304
+ red?: string | undefined;
305
+ reds?: string[] | undefined;
306
+ runDir?: string | undefined;
307
+ artifacts?: Record<string, any> | undefined;
308
+ spentUsd?: number | undefined;
309
+ auditRows?: AuditRow[] | undefined;
310
+ };