fwdloop 0.15.0 → 0.17.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/README.md +2 -0
- package/bin/fwdloop +113 -36
- package/package.json +2 -2
- package/src/authoring.js +27 -15
- package/src/catalogue.json +9 -3
- package/src/closers.js +5 -0
- package/src/config.js +66 -4
- package/src/ctx-tools.js +245 -0
- package/src/declaration.js +25 -1
- package/src/draft-steps.js +138 -0
- package/src/drafter.js +81 -18
- package/src/draftspend.js +109 -3
- package/src/flowmodel.js +30 -0
- package/src/input-facts.js +12 -3
- package/src/keysfile.js +51 -16
- package/src/liveness.js +34 -10
- package/src/model-step.js +13 -11
- package/src/monthly.js +9 -1
- package/src/panel/author.js +170 -43
- package/src/panel/authorcard.js +32 -20
- package/src/panel/authorflows.js +34 -19
- package/src/panel/authorresume.js +11 -9
- package/src/panel/authorstart.js +11 -9
- package/src/panel/authorvalues.js +5 -3
- package/src/panel/data.js +20 -8
- package/src/panel/index.html +584 -182
- package/src/panel/resume.js +3 -3
- package/src/panel/server.js +12 -6
- package/src/panel/settings.js +138 -80
- package/src/panel/spawn.js +15 -9
- package/src/plain-checks.js +51 -0
- package/src/primitives.js +34 -3
- package/src/provider.js +148 -181
- package/src/providershapes.js +7 -7
- package/src/run-store.js +47 -0
- package/src/runner.js +40 -22
- package/src/setup.js +3 -0
- package/src/thinking.js +55 -0
- package/src/version.js +13 -0
- package/types/authoring.d.ts +11 -6
- package/types/closers.d.ts +2 -0
- package/types/config.d.ts +9 -1
- package/types/ctx-tools.d.ts +18 -0
- package/types/draft-steps.d.ts +47 -0
- package/types/drafter.d.ts +15 -12
- package/types/draftspend.d.ts +56 -0
- package/types/flowmodel.d.ts +16 -0
- package/types/input-facts.d.ts +6 -1
- package/types/keysfile.d.ts +23 -6
- package/types/liveness.d.ts +7 -1
- package/types/model-step.d.ts +1 -1
- package/types/monthly.d.ts +1 -0
- package/types/panel/author.d.ts +47 -9
- package/types/panel/authorcard.d.ts +33 -10
- package/types/panel/authorflows.d.ts +32 -9
- package/types/panel/authorresume.d.ts +2 -1
- package/types/panel/authorstart.d.ts +16 -6
- package/types/panel/authorvalues.d.ts +3 -1
- package/types/panel/data.d.ts +2 -2
- package/types/panel/resume.d.ts +2 -1
- package/types/panel/server.d.ts +6 -2
- package/types/panel/settings.d.ts +3 -1
- package/types/panel/spawn.d.ts +13 -4
- package/types/plain-checks.d.ts +22 -0
- package/types/primitives.d.ts +0 -6
- package/types/provider.d.ts +100 -284
- package/types/providershapes.d.ts +3 -4
- package/types/run-store.d.ts +11 -0
- package/types/runner.d.ts +2 -4
- package/types/setup.d.ts +8 -0
- package/types/thinking.d.ts +25 -0
- package/types/version.d.ts +2 -0
package/README.md
CHANGED
|
@@ -62,6 +62,8 @@ standalone.
|
|
|
62
62
|
|
|
63
63
|
Work in progress. Nothing here is ready to install yet.
|
|
64
64
|
|
|
65
|
+
The in-run `impact` tool needs ripgrep (`rg`) on PATH; without it the tool is refused with a message, never a wrong answer.
|
|
66
|
+
|
|
65
67
|
## License
|
|
66
68
|
|
|
67
69
|
Apache-2.0. See `LICENSE` and `NOTICE`.
|
package/bin/fwdloop
CHANGED
|
@@ -18,7 +18,8 @@ import { parseArgs } from 'node:util';
|
|
|
18
18
|
import {
|
|
19
19
|
existsSync, readdirSync, realpathSync, rmdirSync,
|
|
20
20
|
} from 'node:fs';
|
|
21
|
-
import { extname, join, resolve } from 'node:path';
|
|
21
|
+
import { basename, dirname, extname, join, resolve } from 'node:path';
|
|
22
|
+
import { draftHoldUsd } from '../src/draftspend.js';
|
|
22
23
|
import { pathToFileURL } from 'node:url';
|
|
23
24
|
|
|
24
25
|
import {
|
|
@@ -33,8 +34,9 @@ import { makeLiveModelStep } from '../src/model-step.js';
|
|
|
33
34
|
import { sendViaPrimitive } from '../src/send.js';
|
|
34
35
|
import { resolvePrimitives } from '../src/primitives.js';
|
|
35
36
|
import {
|
|
36
|
-
checkKeyPreflight,
|
|
37
|
+
checkKeyPreflight, resolveUsableModel, pickTicked, readPriceConfig, ceilingCostUsd,
|
|
37
38
|
} from '../src/provider.js';
|
|
39
|
+
import { readModelRecord } from '../src/flowmodel.js';
|
|
38
40
|
import { configDoorHome } from '../src/config.js';
|
|
39
41
|
import {
|
|
40
42
|
claimHold, settleHold, nameRunDir, monthlyRefusalText, ConfigError,
|
|
@@ -48,11 +50,9 @@ import { isInteractive, confirmTypedName, NEEDS_TTY } from '../src/sign-confirm.
|
|
|
48
50
|
import { draftToDir, draftKeyRefusal, signDraft, SIGN_LINE, costText, questionsRefusal } from '../src/authoring.js';
|
|
49
51
|
import { userInfo } from 'node:os';
|
|
50
52
|
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
//
|
|
54
|
-
// never reads a key any other way and never prints it.
|
|
55
|
-
const MODEL_SLOT = 'deepseek';
|
|
53
|
+
// This CLI drives no fixed provider (M4d amendment 6). A job runs with the model recorded beside it (`model.json`, written when it was drafted:
|
|
54
|
+
// the provider is the name of a key, its key is read from the env under that name and never printed); `draft` takes `--model` (+ `--provider`).
|
|
55
|
+
// Whether that model may be used is `resolveUsableModel` (ticked in Settings > Providers with both prices): this file names no model and no price.
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* M4d: the ONE place a keyed command gets its env — the shell env with the keys file
|
|
@@ -60,11 +60,12 @@ const MODEL_SLOT = 'deepseek';
|
|
|
60
60
|
* start. A file other users can read is refused here (ruling 1A), before any dir or ledger
|
|
61
61
|
* write, at $0. process.env is never mutated: the merged env is handed down by hand.
|
|
62
62
|
*/
|
|
63
|
-
function
|
|
63
|
+
function keyedLoad() {
|
|
64
64
|
const loaded = keysForDoor();
|
|
65
65
|
if (!loaded.ok) fail(String(loaded.refusal));
|
|
66
|
-
return loaded
|
|
66
|
+
return loaded;
|
|
67
67
|
}
|
|
68
|
+
const keyedEnv = () => keyedLoad().env;
|
|
68
69
|
|
|
69
70
|
/**
|
|
70
71
|
* M4d piece 3: the monthly check, at the ONE seam all three money doors (`run`, `resume`, `draft`) call,
|
|
@@ -75,6 +76,8 @@ function keyedEnv() {
|
|
|
75
76
|
* which settles it as "process gone").
|
|
76
77
|
* @returns {{ addRunDir: (d: string) => void, nameRunDir: (d: string) => void }}
|
|
77
78
|
*/
|
|
79
|
+
const NO_CONFIG_HOME_REFUSAL = 'NODE_ENV=test is set without FWDLOOP_CONFIG_HOME, so the keys file, prices and monthly limit would be skipped. Nothing spent. Unset NODE_ENV, or set FWDLOOP_CONFIG_HOME.';
|
|
80
|
+
|
|
78
81
|
function takeMonthlyHold({ what, flow, runId, runDir, holdUsd, spentAtHold = 0 }) {
|
|
79
82
|
const door = configDoorHome();
|
|
80
83
|
const home = door.home;
|
|
@@ -84,7 +87,7 @@ function takeMonthlyHold({ what, flow, runId, runDir, holdUsd, spentAtHold = 0 }
|
|
|
84
87
|
// a real provider call here would spend with no keys file, prices, limit or record.
|
|
85
88
|
const hook = what === 'draft' ? 'FWDLOOP_TEST_DRAFT_PROVIDER' : 'FWDLOOP_TEST_MODEL_STEP';
|
|
86
89
|
if (!(process.env.NODE_ENV === 'test' && process.env[hook])) {
|
|
87
|
-
fail(
|
|
90
|
+
fail(NO_CONFIG_HOME_REFUSAL);
|
|
88
91
|
}
|
|
89
92
|
return { addRunDir: () => {}, nameRunDir: () => {} };
|
|
90
93
|
}
|
|
@@ -142,8 +145,8 @@ function today() {
|
|
|
142
145
|
}
|
|
143
146
|
|
|
144
147
|
/**
|
|
145
|
-
* Build the run's `modelStep
|
|
146
|
-
*
|
|
148
|
+
* Build the run's `modelStep` and its per-round hold (`{ modelStep, ceilingUsd }`). Live by default (`makeLiveModelStep` against
|
|
149
|
+
* the job's recorded provider, refusing an unset/empty/whitespace key via
|
|
147
150
|
* `checkKeyPreflight` BEFORE this function returns — i.e. before the caller
|
|
148
151
|
* has created a run dir or written any book row).
|
|
149
152
|
*
|
|
@@ -156,18 +159,57 @@ function today() {
|
|
|
156
159
|
* be hit by a user typing `--something`. This is how the test suite drives
|
|
157
160
|
* every CLI scenario at $0 with zero chance of a real network call.
|
|
158
161
|
*/
|
|
159
|
-
async function resolveModelStep(spendPath, env) {
|
|
162
|
+
async function resolveModelStep(spendPath, env, flowDir, named = {}) {
|
|
160
163
|
if (process.env.NODE_ENV === 'test' && process.env.FWDLOOP_TEST_MODEL_STEP) {
|
|
161
164
|
const modulePath = resolve(process.env.FWDLOOP_TEST_MODEL_STEP);
|
|
162
165
|
const mod = await import(pathToFileURL(modulePath).href);
|
|
163
166
|
if (typeof mod.default !== 'function') {
|
|
164
167
|
throw new Error(`FWDLOOP_TEST_MODEL_STEP module "${modulePath}" has no default export function`);
|
|
165
168
|
}
|
|
166
|
-
|
|
169
|
+
// A test double has no model of its own. When Settings has one model ticked with both prices its hold applies (so a cap-halt
|
|
170
|
+
// is exercised the way a live run's is); with none, the double holds nothing. It is never refused here: it spends nothing.
|
|
171
|
+
const modelStep = await mod.default({ spendPath });
|
|
172
|
+
let usable = null;
|
|
173
|
+
try {
|
|
174
|
+
const rec = configDoorHome().skip ? null : readModelRecord(flowDir);
|
|
175
|
+
usable = rec === null ? null : resolveUsableModel(rec.slot, { config: readPriceConfig(), model: rec.model });
|
|
176
|
+
} catch (err) {
|
|
177
|
+
if (!(err instanceof ConfigError)) throw err; // a broken config is refused by name at the monthly hold, right after this
|
|
178
|
+
}
|
|
179
|
+
return { modelStep, ...(usable?.ok ? { ceilingUsd: ceilingCostUsd(usable.rates) } : {}) };
|
|
180
|
+
}
|
|
181
|
+
// A test process with no config home reads no Settings: say why, not "tick a model".
|
|
182
|
+
if (configDoorHome().skip) throw new Error(NO_CONFIG_HOME_REFUSAL);
|
|
183
|
+
// The ONE decision of which model runs and at what price (M4d amendments 5 and 6): the model recorded with the job (a job drafted before the record
|
|
184
|
+
// existed takes `--model`), refused by name, at $0, before a run dir or a book row exists. The per-round hold is that model's own Settings price times the token bounds.
|
|
185
|
+
let usable;
|
|
186
|
+
let slot;
|
|
187
|
+
try {
|
|
188
|
+
const config = readPriceConfig();
|
|
189
|
+
const rec = readModelRecord(flowDir);
|
|
190
|
+
if (rec !== null && named.model !== undefined && (named.model !== rec.model || (named.provider !== undefined && named.provider !== rec.slot))) {
|
|
191
|
+
throw new Error(`This job already has its model recorded (${rec.slot} · ${rec.model}); --model cannot change it.`);
|
|
192
|
+
}
|
|
193
|
+
let pick = rec;
|
|
194
|
+
if (pick === null) {
|
|
195
|
+
if (named.model === undefined) throw new Error('This job has no model recorded. Name one with --model <id>, a model ticked in Settings > Providers with both prices.');
|
|
196
|
+
const p = pickTicked(config, named);
|
|
197
|
+
if (!p.ok) throw new Error(p.say);
|
|
198
|
+
pick = p;
|
|
199
|
+
}
|
|
200
|
+
slot = pick.slot;
|
|
201
|
+
const keyCheck = checkKeyPreflight(slot, env);
|
|
202
|
+
if (!keyCheck.ok) throw new Error(keyCheck.message);
|
|
203
|
+
usable = resolveUsableModel(slot, { config, model: pick.model });
|
|
204
|
+
} catch (err) {
|
|
205
|
+
if (!(err instanceof ConfigError)) throw err;
|
|
206
|
+
throw new Error(`${err.message} — refusing to start rather than guess the price. Nothing spent.`);
|
|
167
207
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
208
|
+
if (!usable.ok) throw new Error(usable.say);
|
|
209
|
+
return {
|
|
210
|
+
modelStep: makeLiveModelStep({ slot, model: usable.model, spendPath, env }),
|
|
211
|
+
ceilingUsd: ceilingCostUsd(usable.rates),
|
|
212
|
+
};
|
|
171
213
|
}
|
|
172
214
|
|
|
173
215
|
function parseSourceFlags(list) {
|
|
@@ -249,6 +291,8 @@ async function cmdRun(rest) {
|
|
|
249
291
|
root: { type: 'string' },
|
|
250
292
|
source: { type: 'string', multiple: true, default: [] },
|
|
251
293
|
'run-id': { type: 'string' },
|
|
294
|
+
// a job drafted before its model was recorded: the model it runs with (a ticked line); --provider only if two keys tick the same id
|
|
295
|
+
model: { type: 'string' }, provider: { type: 'string' },
|
|
252
296
|
},
|
|
253
297
|
allowPositionals: true,
|
|
254
298
|
});
|
|
@@ -321,8 +365,9 @@ async function cmdRun(rest) {
|
|
|
321
365
|
// here — before ANY directory for this run is created.
|
|
322
366
|
const spendPath = join(runDir, 'spend.jsonl');
|
|
323
367
|
let modelStep;
|
|
368
|
+
let ceilingUsd;
|
|
324
369
|
try {
|
|
325
|
-
modelStep = await resolveModelStep(spendPath, env);
|
|
370
|
+
({ modelStep, ceilingUsd } = await resolveModelStep(spendPath, env, join(root, flowName), values));
|
|
326
371
|
} catch (err) {
|
|
327
372
|
fail(err.message);
|
|
328
373
|
return;
|
|
@@ -354,6 +399,7 @@ async function cmdRun(rest) {
|
|
|
354
399
|
sources,
|
|
355
400
|
catalogue,
|
|
356
401
|
modelStep,
|
|
402
|
+
ceilingUsd,
|
|
357
403
|
askStep: makeParkingAskStep(),
|
|
358
404
|
sendStep: sendViaPrimitive,
|
|
359
405
|
primitives,
|
|
@@ -543,6 +589,7 @@ async function cmdResume(rest) {
|
|
|
543
589
|
options: {
|
|
544
590
|
flow: { type: 'string' },
|
|
545
591
|
root: { type: 'string' },
|
|
592
|
+
model: { type: 'string' }, provider: { type: 'string' },
|
|
546
593
|
},
|
|
547
594
|
allowPositionals: true,
|
|
548
595
|
});
|
|
@@ -574,8 +621,9 @@ async function cmdResume(rest) {
|
|
|
574
621
|
|
|
575
622
|
const spendPath = join(runDir, 'spend.jsonl');
|
|
576
623
|
let modelStep;
|
|
624
|
+
let ceilingUsd;
|
|
577
625
|
try {
|
|
578
|
-
modelStep = await resolveModelStep(spendPath, env);
|
|
626
|
+
({ modelStep, ceilingUsd } = await resolveModelStep(spendPath, env, join(root, flowName), values));
|
|
579
627
|
} catch (err) {
|
|
580
628
|
fail(err.message);
|
|
581
629
|
return;
|
|
@@ -619,6 +667,7 @@ async function cmdResume(rest) {
|
|
|
619
667
|
runId,
|
|
620
668
|
catalogue,
|
|
621
669
|
modelStep,
|
|
670
|
+
ceilingUsd,
|
|
622
671
|
sendStep: sendViaPrimitive,
|
|
623
672
|
primitives,
|
|
624
673
|
primitiveReds,
|
|
@@ -642,6 +691,7 @@ async function cmdContinue(rest) {
|
|
|
642
691
|
options: {
|
|
643
692
|
flow: { type: 'string' },
|
|
644
693
|
root: { type: 'string' },
|
|
694
|
+
model: { type: 'string' }, provider: { type: 'string' },
|
|
645
695
|
},
|
|
646
696
|
allowPositionals: true,
|
|
647
697
|
});
|
|
@@ -671,8 +721,9 @@ async function cmdContinue(rest) {
|
|
|
671
721
|
|
|
672
722
|
const spendPath = join(runDir, 'spend.jsonl');
|
|
673
723
|
let modelStep;
|
|
724
|
+
let ceilingUsd;
|
|
674
725
|
try {
|
|
675
|
-
modelStep = await resolveModelStep(spendPath, env);
|
|
726
|
+
({ modelStep, ceilingUsd } = await resolveModelStep(spendPath, env, join(root, flowName), values));
|
|
676
727
|
} catch (err) {
|
|
677
728
|
fail(err.message);
|
|
678
729
|
return;
|
|
@@ -706,6 +757,7 @@ async function cmdContinue(rest) {
|
|
|
706
757
|
runId,
|
|
707
758
|
catalogue,
|
|
708
759
|
modelStep,
|
|
760
|
+
ceilingUsd,
|
|
709
761
|
sendStep: sendViaPrimitive,
|
|
710
762
|
primitives,
|
|
711
763
|
primitiveReds,
|
|
@@ -759,15 +811,19 @@ async function cmdDraft(rest) {
|
|
|
759
811
|
const { values, positionals } = parseArgs({
|
|
760
812
|
args: rest,
|
|
761
813
|
options: {
|
|
762
|
-
out: { type: 'string' }, root: { type: 'string' }, name: { type: 'string' },
|
|
814
|
+
out: { type: 'string' }, root: { type: 'string' }, name: { type: 'string' },
|
|
763
815
|
// M4e amendment 24 item 4: the redraft after the human's answers never asks (the panel passes this; a plain draft may ask on try 3)
|
|
764
816
|
'no-questions': { type: 'boolean' },
|
|
817
|
+
// M4d amendment 6: the model that drafts (and then runs) the job - a line ticked in Settings > Providers; --provider only if two keys tick the same id.
|
|
818
|
+
// --thinking is the Chat "Thinking on" tick (drafting only; off unless given).
|
|
819
|
+
model: { type: 'string' }, provider: { type: 'string' }, thinking: { type: 'boolean' },
|
|
765
820
|
},
|
|
766
821
|
allowPositionals: true,
|
|
767
822
|
});
|
|
768
|
-
const
|
|
823
|
+
const keysLoad = keyedLoad();
|
|
824
|
+
const { env } = keysLoad;
|
|
769
825
|
const proseFile = positionals[0];
|
|
770
|
-
if (!proseFile) fail('draft: a prose file is required — fwdloop draft <prose.txt> --out <draftDir> --root <flowsDir> --name <flow>
|
|
826
|
+
if (!proseFile) fail('draft: a prose file is required — fwdloop draft <prose.txt> --out <draftDir> --root <flowsDir> --name <flow>');
|
|
771
827
|
for (const req of ['out', 'root', 'name']) if (!values[req]) fail(`draft: --${req} is required`);
|
|
772
828
|
|
|
773
829
|
let injected = {};
|
|
@@ -775,34 +831,54 @@ async function cmdDraft(rest) {
|
|
|
775
831
|
const mod = await import(pathToFileURL(resolve(process.env.FWDLOOP_TEST_DRAFT_PROVIDER)).href);
|
|
776
832
|
injected = await mod.default();
|
|
777
833
|
}
|
|
778
|
-
// The
|
|
834
|
+
// The model is named by the caller (a ticked line): refused by name at $0 when it is not. An injected test provider carries its own.
|
|
835
|
+
let pick = {};
|
|
779
836
|
if (injected.provider == null) {
|
|
780
|
-
|
|
837
|
+
if (configDoorHome().skip) fail(NO_CONFIG_HOME_REFUSAL);
|
|
838
|
+
try {
|
|
839
|
+
const p = pickTicked(readPriceConfig(), values);
|
|
840
|
+
if (!p.ok) fail(`draft refused at $0 — ${p.say}`);
|
|
841
|
+
pick = { slot: p.slot, model: p.model };
|
|
842
|
+
} catch (err) {
|
|
843
|
+
if (!(err instanceof ConfigError)) throw err;
|
|
844
|
+
fail(`${err.message} — refusing to draft rather than guess the price. Nothing spent.`);
|
|
845
|
+
}
|
|
846
|
+
// The key preflight (draftToDir repeats it).
|
|
847
|
+
const refusal = draftKeyRefusal(pick.slot, env);
|
|
781
848
|
if (refusal !== null) fail(`draft refused at $0 — ${refusal}`);
|
|
782
|
-
}
|
|
783
|
-
// M4d piece 3: the monthly check —
|
|
784
|
-
//
|
|
849
|
+
} else if (values.model !== undefined) pick = { slot: values.provider, model: values.model };
|
|
850
|
+
// M4d piece 3: the monthly check — one ceiling round, priced from the
|
|
851
|
+
// picked model's own Settings prices (an injected test provider carries its own rates). A model that is not
|
|
852
|
+
// ticked with both prices is refused here by name, at $0, before the monthly hold or any draft folder (M4d amendment 5).
|
|
785
853
|
let ceiling;
|
|
786
854
|
try {
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
855
|
+
let rates = injected.rates;
|
|
856
|
+
if (!rates) {
|
|
857
|
+
if (configDoorHome().skip) fail(NO_CONFIG_HOME_REFUSAL);
|
|
858
|
+
const usable = resolveUsableModel(pick.slot, { config: readPriceConfig(), model: pick.model });
|
|
859
|
+
if (!usable.ok) fail(`draft refused at $0 — ${usable.say}`);
|
|
860
|
+
rates = usable.rates;
|
|
861
|
+
}
|
|
862
|
+
ceiling = ceilingCostUsd(rates);
|
|
791
863
|
} catch (err) {
|
|
792
864
|
if (!(err instanceof ConfigError)) throw err;
|
|
793
865
|
fail(`${err.message} — refusing to draft rather than guess the price. Nothing spent.`);
|
|
794
866
|
}
|
|
867
|
+
// M4d am19 item 1: the hold is what is left of the cap (cap minus this job's drafting so far), the most the draft may spend, not one round
|
|
868
|
+
const proseRead = readFileInside(dirname(resolve(proseFile)), basename(resolve(proseFile)));
|
|
795
869
|
takeMonthlyHold({
|
|
796
|
-
what: 'draft', flow: values.name, runId: null, runDir: resolve(values.out), holdUsd: ceiling,
|
|
870
|
+
what: 'draft', flow: values.name, runId: null, runDir: resolve(values.out), holdUsd: draftHoldUsd(proseRead.ok ? proseRead.text : '', resolve(values.out), ceiling),
|
|
797
871
|
});
|
|
798
872
|
const result = await draftToDir({
|
|
799
873
|
proseFile: resolve(proseFile),
|
|
800
874
|
dir: resolve(values.out),
|
|
801
875
|
root: resolve(values.root),
|
|
802
876
|
name: values.name,
|
|
803
|
-
budgetUsd: values.budget === undefined ? undefined : Number(values.budget),
|
|
804
877
|
noQuestions: values['no-questions'] === true,
|
|
878
|
+
draftThinking: values.thinking === true ? 'on' : 'off',
|
|
805
879
|
env,
|
|
880
|
+
keyNames: keysLoad.keyNames,
|
|
881
|
+
...pick,
|
|
806
882
|
...injected,
|
|
807
883
|
});
|
|
808
884
|
const cost = result.costUsd === null ? 'UNKNOWN' : costText(Number(result.costUsd), result.spendComplete);
|
|
@@ -830,7 +906,8 @@ async function cmdSign(rest) {
|
|
|
830
906
|
options: { approve: { type: 'string' }, 'signed-by': { type: 'string' } },
|
|
831
907
|
allowPositionals: true,
|
|
832
908
|
});
|
|
833
|
-
const
|
|
909
|
+
const keysLoad = keyedLoad(); // sign takes no key, but its leak sweep must know the file's keys too
|
|
910
|
+
const env = keysLoad.env;
|
|
834
911
|
const dir = positionals[0];
|
|
835
912
|
if (!dir) fail('sign: a draft dir is required — fwdloop sign <dir> --approve <hash>');
|
|
836
913
|
// M4e amendments 24/25: a plan with a question is not signed from here — the answers are taken in the panel. One check, also run again inside signDraft.
|
|
@@ -846,7 +923,7 @@ async function cmdSign(rest) {
|
|
|
846
923
|
const typed = await confirmTypedName({ name: flowName });
|
|
847
924
|
if (!typed.ok) fail(`sign refused — ${typed.red}`);
|
|
848
925
|
const result = signDraft({
|
|
849
|
-
dir: resolve(dir), approve: values.approve, signedBy: values['signed-by'] ?? userInfo().username, env,
|
|
926
|
+
dir: resolve(dir), approve: values.approve, signedBy: values['signed-by'] ?? userInfo().username, env, keyNames: keysLoad.keyNames,
|
|
850
927
|
});
|
|
851
928
|
if (!result.ok) fail(`sign refused — ${result.reds.join('; ')}`);
|
|
852
929
|
process.stdout.write(`signed: ${result.flowDir} (by ${result.signature.signedBy})\n`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fwdloop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Describe a job as steps and guardrails; an agent builds the workflow, pauses for humans where you said, checks every step happened, and reruns it under budgets it cannot touch.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"NOTICE"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"bare-agent": "^0.
|
|
52
|
+
"bare-agent": "^0.52.0",
|
|
53
53
|
"bareguard": "^0.21.2",
|
|
54
54
|
"litectx": "^0.32.0"
|
|
55
55
|
},
|
package/src/authoring.js
CHANGED
|
@@ -27,11 +27,12 @@ import { WIRED_VERBS } from './primitives.js';
|
|
|
27
27
|
import { canonicalBytes } from './signature.js';
|
|
28
28
|
import { writeFlow, readFileInside, readdirInside, checkFlowName } from './flow.js';
|
|
29
29
|
import { checkSendDestination } from './runner.js';
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
30
|
+
import { checkKeyPreflight, appendSpendRow } from './provider.js';
|
|
31
|
+
import { keyValues } from './keysfile.js';
|
|
32
|
+
import { readModelRecord, writeModelRecord } from './flowmodel.js';
|
|
33
|
+
import { sessionPriorSpend, writeDraftSpend } from './draftspend.js';
|
|
32
34
|
import { writeSetup } from './setup.js';
|
|
33
35
|
|
|
34
|
-
export const DRAFT_BUDGET_USD = 0.10;
|
|
35
36
|
export const SPEC_HASH_FILE = 'spec.hash';
|
|
36
37
|
/** 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. */
|
|
37
38
|
export const PANEL_DRAFTS_DIR = '.drafts';
|
|
@@ -246,13 +247,12 @@ export function draftKeyRefusal(slot, env) {
|
|
|
246
247
|
* Test seam: `provider`/`rates`/`modelId` are injected; without them a live provider is built.
|
|
247
248
|
*/
|
|
248
249
|
export async function draftToDir({
|
|
249
|
-
proseFile, dir, root, name, slot
|
|
250
|
+
proseFile, dir, root, name, slot, model, draftThinking = /** @type {'on'|'off'} */ ('off'), keyNames, env = process.env, provider, rates, modelId, noQuestions = false,
|
|
250
251
|
}) {
|
|
251
252
|
const refuse = (reds) => ({ ok: false, wrote: false, reds, costUsd: 0 });
|
|
252
253
|
const nameCheck = checkFlowName(name);
|
|
253
254
|
if (!nameCheck.ok) return refuse([nameCheck.red]);
|
|
254
255
|
if (typeof root !== 'string' || root === '') return refuse(['draft: --root <flows dir> is required']);
|
|
255
|
-
if (!(budgetUsd > 0)) return refuse(['draft: budget must be a positive number']);
|
|
256
256
|
// An unsigned draft folder inside the flows root would list as a flow in the panel and Inbox (checked by realpath, at use time).
|
|
257
257
|
const realRoot = realpathOfNew(root);
|
|
258
258
|
const realDir = realpathOfNew(dir);
|
|
@@ -267,12 +267,11 @@ export async function draftToDir({
|
|
|
267
267
|
if (!read.ok) return refuse([String(read.red)]);
|
|
268
268
|
const prose = { text: String(read.text) };
|
|
269
269
|
const injected = provider != null;
|
|
270
|
-
const secretVar = PROVIDER_SLOTS[slot]?.envVar;
|
|
271
270
|
if (!injected) {
|
|
272
271
|
const refusal = draftKeyRefusal(slot, env);
|
|
273
272
|
if (refusal !== null) return refuse([refusal]);
|
|
274
273
|
}
|
|
275
|
-
const secrets =
|
|
274
|
+
const secrets = keyValues(env, [...new Set([...(keyNames ?? []), ...(typeof slot === 'string' ? [slot] : [])])]);
|
|
276
275
|
if (secrets.some((s) => s.length >= 8 && prose.text.includes(s))) return refuse(['prose: contains an API key value — refused']);
|
|
277
276
|
// Red messages can echo what a provider's error body echoed (a key): scrubbed like every file, before anything prints them.
|
|
278
277
|
const cleanReds = (reds) => (reds ?? []).map((r) => scrub(String(r), secrets));
|
|
@@ -304,8 +303,10 @@ export async function draftToDir({
|
|
|
304
303
|
// after a call the cost is already in the file as "one call in flight" (a ceiling): the honest direction, so no throw
|
|
305
304
|
}
|
|
306
305
|
};
|
|
306
|
+
// M4d am16: a panel draft folder (card.json beside this plan folder) is one job's session: what its earlier plans spent comes off the cap first.
|
|
307
|
+
const priorSpend = sessionPriorSpend(dir);
|
|
307
308
|
const result = await draft({
|
|
308
|
-
proseText: prose.text, slot, model,
|
|
309
|
+
proseText: prose.text, slot, model, provider, rates, modelId, env, onBook, priorSpend, offerQuestions: !noQuestions, draftThinking,
|
|
309
310
|
});
|
|
310
311
|
if (result.stop === 'pre-flight') {
|
|
311
312
|
try { rmdirSync(dir); } catch { /* the claimed dir is still empty; leave it rather than mask the refusal */ }
|
|
@@ -323,6 +324,9 @@ export async function draftToDir({
|
|
|
323
324
|
files['prose.txt'] = prose.text; // verbatim
|
|
324
325
|
const targetText = dj({ root, name });
|
|
325
326
|
files['target.json'] = targetText;
|
|
327
|
+
// M4d am6 item 6: the pick is remembered beside the draft (written below as model.json, outside the signed files) so the signed job runs with it.
|
|
328
|
+
const recordModel = model ?? result.modelId ?? modelId;
|
|
329
|
+
const pick = typeof slot === 'string' && typeof recordModel === 'string' && recordModel !== '' ? { slot, model: recordModel } : null;
|
|
326
330
|
// Amendments 24/25: the questions and the drafter's own "not checked" reading are written BEFORE log.json/spec.hash (the panel reads those
|
|
327
331
|
// two as "the draft finished"), as their own files: never in the readout, so never in the hash.
|
|
328
332
|
if (result.questions?.length) files[QUESTIONS_FILE] = scrub(dj({ questions: result.questions }), secrets);
|
|
@@ -330,7 +334,7 @@ export async function draftToDir({
|
|
|
330
334
|
files['log.json'] = scrub(dj({
|
|
331
335
|
ok: result.ok, stop: result.stop, reds: result.reds, rounds: result.rounds, calls: result.calls, costUsd: result.costUsd, spendComplete: result.spendComplete,
|
|
332
336
|
modelId: result.modelId, modelReturned: result.modelReturned, structureRetries: result.structureRetries,
|
|
333
|
-
revisions: result.revisions, log: result.log,
|
|
337
|
+
revisions: result.revisions, thinking: result.thinking, inputs: result.inputs, log: result.log,
|
|
334
338
|
}), secrets);
|
|
335
339
|
let hash = null;
|
|
336
340
|
if (result.ok) {
|
|
@@ -366,12 +370,13 @@ export async function draftToDir({
|
|
|
366
370
|
calls: result.calls,
|
|
367
371
|
spendComplete: result.spendComplete,
|
|
368
372
|
stop: result.stop,
|
|
369
|
-
budgetUsd,
|
|
373
|
+
budgetUsd: parsed.arbiter.capUsd, // the cap signed in the prose is the draft's limit too (M4d am13)
|
|
370
374
|
// the draft's own time (amendment 7 item 2: the Draft's total time adds up from these); null when no call was booked
|
|
371
375
|
startedAt,
|
|
372
376
|
wallMs: startedAt === null ? null : Math.max(0, Date.now() - Date.parse(startedAt)),
|
|
373
377
|
});
|
|
374
378
|
for (const [f, text] of Object.entries(files)) writeFileSync(path.join(dir, f), text);
|
|
379
|
+
if (pick !== null) writeModelRecord(dir, pick);
|
|
375
380
|
leaks = sweepForSecrets(dir, secrets);
|
|
376
381
|
if (leaks > 0) {
|
|
377
382
|
const red = `scrub: ${leaks} file(s) in the draft dir contain a key value`;
|
|
@@ -393,18 +398,18 @@ export async function draftToDir({
|
|
|
393
398
|
* `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.
|
|
394
399
|
* M6: `replaces: {flowHash}` (a draft opened from an existing signed job, carrying that job's signature hash) makes this sign REPLACE that job
|
|
395
400
|
* (`writeFlow` checks the hash is still the job's); without it a taken name is refused as ever.
|
|
396
|
-
* @param {{dir: any, approve?: any, signedBy: any, signedAt?: string, env?: Record<string, string|undefined>, sessionDir?: string, replaces?: {flowHash: string}}} a
|
|
401
|
+
* @param {{dir: any, approve?: any, signedBy: any, signedAt?: string, env?: Record<string, string|undefined>, keyNames?: string[], sessionDir?: string, replaces?: {flowHash: string}}} a
|
|
397
402
|
*/
|
|
398
403
|
export function signDraft({
|
|
399
|
-
dir, approve, signedBy, signedAt = new Date().toISOString(), env = process.env, sessionDir, replaces,
|
|
404
|
+
dir, approve, signedBy, signedAt = new Date().toISOString(), env = process.env, keyNames, sessionDir, replaces,
|
|
400
405
|
}) {
|
|
401
406
|
const refuse = (...reds) => ({ ok: false, reds });
|
|
402
407
|
if (readFileInside(dir, LEAK_MARKER_FILE).ok) return refuse(`sign: "${dir}" carries a key-leak marker (${LEAK_MARKER_FILE}) — never signed`);
|
|
403
408
|
const openQuestion = questionsRefusal(dir);
|
|
404
409
|
if (openQuestion !== null) return refuse(`sign: ${openQuestion}`);
|
|
405
|
-
// Sign is $0 and takes no key, so it can only sweep for keys
|
|
410
|
+
// Sign is $0 and takes no key, so it can only sweep for the keys it is told of (`keyNames`, the rows set up in Settings).
|
|
406
411
|
// With none set the sweep is skipped: the marker above and the missing spec.hash are then the guard.
|
|
407
|
-
const keys =
|
|
412
|
+
const keys = keyValues(env, keyNames);
|
|
408
413
|
if (sweepForSecrets(dir, keys) > 0) return refuse(`sign: "${dir}" contains a key value — nothing signed`);
|
|
409
414
|
if (typeof approve !== 'string' || approve === '') return refuse('sign: --approve <hash> is required');
|
|
410
415
|
const read = (f) => readFileInside(dir, f);
|
|
@@ -459,5 +464,12 @@ export function signDraft({
|
|
|
459
464
|
const setup = writeSetup({
|
|
460
465
|
flowDir: written.dir, sessionDir, planDir: dir, hash: h.hash, signedBy, signedAt, flowHash: written.signature?.flow, secrets: /** @type {string[]} */ (keys.filter((k) => typeof k === 'string' && k.length >= 8)),
|
|
461
466
|
});
|
|
462
|
-
|
|
467
|
+
// M4d am6 item 6: the draft's model pick goes into the flow folder beside the signed files; a run reads it from there. The flow IS signed by now,
|
|
468
|
+
// so a failure to write it never un-signs: `modelRecord.ok:false` is returned for the caller to say (the run is then refused by name).
|
|
469
|
+
const pickRecord = readModelRecord(dir);
|
|
470
|
+
let modelRecord = { ok: true, recorded: pickRecord !== null };
|
|
471
|
+
if (pickRecord !== null) {
|
|
472
|
+
try { writeModelRecord(written.dir, pickRecord); } catch (e) { modelRecord = { ok: false, recorded: false, why: /** @type {any} */ (e)?.code ?? 'error' }; }
|
|
473
|
+
}
|
|
474
|
+
return { ok: true, flowDir: written.dir, signature: written.signature, setup, modelRecord };
|
|
463
475
|
}
|
package/src/catalogue.json
CHANGED
|
@@ -14,13 +14,19 @@
|
|
|
14
14
|
"verb": "grep", "component": "select", "package": "bare-agent/tools", "symbol": "createShellTools", "tool": "shell_grep", "class": "read", "skill": "core", "desc": "Find lines matching a pattern across files."
|
|
15
15
|
},
|
|
16
16
|
{
|
|
17
|
-
"verb": "recall", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "recall", "class": "read", "skill": "core", "desc": "Search
|
|
17
|
+
"verb": "recall", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "recall", "class": "read", "skill": "core", "desc": "Search this run's code and markdown files only (its inputs and out folder, .md .js .py .ts); a CSV or Word file is not searched. Returns pointers to the best matches, not their full text."
|
|
18
18
|
},
|
|
19
19
|
{
|
|
20
|
-
"verb": "get", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "get", "class": "read", "skill": "core", "desc": "Fetch the full text of one
|
|
20
|
+
"verb": "get", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "get", "class": "read", "skill": "core", "desc": "Fetch the full text of one parked item by its id, or one part of a file by the pointer recall gave."
|
|
21
21
|
},
|
|
22
22
|
{
|
|
23
|
-
"verb": "recent", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "
|
|
23
|
+
"verb": "recent", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "recentActivity", "class": "read", "skill": "core", "desc": "List the functions most recently changed in this run's code files, newest first. This is not a git repository, so it finds nothing inside a run."
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"verb": "related", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "related", "class": "read", "skill": "core", "desc": "List the files a given code or markdown file imports and the files that import it, in code and markdown files only; a CSV or Word file has none."
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"verb": "impact", "component": "select", "package": "litectx", "symbol": "LiteCtx", "method": "impact", "class": "read", "skill": "core", "desc": "Name the callers and importers of one function or symbol, in code and markdown files only; a CSV or Word file has none, so it finds nothing there."
|
|
24
30
|
},
|
|
25
31
|
{
|
|
26
32
|
"verb": "compress", "component": "compress", "package": "litectx", "symbol": "compress", "class": "read", "skill": "core", "desc": "Shrink source code: keep each function header and its doc comment, drop the body (or reduce it to a name). Code only — prose and markdown come back unchanged."
|
package/src/closers.js
CHANGED
|
@@ -74,6 +74,11 @@ const FORMULAS = Object.freeze({
|
|
|
74
74
|
sub: (vals) => vals[0] - vals[1],
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
+
/** An input with cells (M4d amendment 8): a file whose name ends `.csv`. The one decider for "this input can be cited". */
|
|
78
|
+
export function isCellsPath(p) {
|
|
79
|
+
return typeof p === 'string' && p.toLowerCase().endsWith('.csv');
|
|
80
|
+
}
|
|
81
|
+
|
|
77
82
|
const CELL_RE = /^([a-zA-Z0-9_-]+)!([A-Za-z]+)(\d+)$/;
|
|
78
83
|
const FORMULA_RE = /^(sum|count|min|max|sub|daysBetween)\((.*)\)$/;
|
|
79
84
|
const FIELD_REF_RE = /^#([A-Za-z0-9_]+)$/;
|