fwdloop 0.19.1 → 0.21.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 +22 -13
- package/package.json +1 -1
- package/src/audit-fold.js +177 -0
- package/src/authoring.js +24 -4
- package/src/books.js +4 -3
- package/src/closers.js +49 -2
- package/src/ctx-tools.js +1 -1
- package/src/diary.js +49 -0
- package/src/flow.js +218 -2
- package/src/flowmodel.js +42 -5
- package/src/jobkey.js +25 -0
- package/src/jsonl-append.js +29 -0
- package/src/model-step.js +103 -5
- package/src/monthly.js +3 -2
- package/src/panel/author.js +44 -18
- package/src/panel/authorcard.js +7 -13
- package/src/panel/authorflows.js +118 -24
- package/src/panel/authorresume.js +8 -4
- package/src/panel/authorstart.js +63 -18
- package/src/panel/authortrigger.js +250 -125
- package/src/panel/authorvalues.js +13 -2
- package/src/panel/data.js +279 -43
- package/src/panel/index.html +547 -150
- package/src/panel/needsyou.js +29 -0
- package/src/panel/scheduler.js +21 -22
- package/src/panel/server.js +30 -6
- package/src/primitives.js +44 -2
- package/src/provider.js +3 -2
- package/src/runner.js +58 -22
- package/src/runvalues.js +10 -2
- package/src/schedule.js +135 -0
- package/src/setup.js +0 -21
- package/src/summary-words.js +180 -7
- package/src/thinking.js +39 -0
- package/src/trigger-clock.js +18 -16
- package/src/trigger-files.js +37 -24
- package/src/trigger-line.js +6 -15
- package/src/try-checks.js +62 -0
- package/src/types.js +2 -1
- package/types/audit-fold.d.ts +54 -0
- package/types/authoring.d.ts +33 -2
- package/types/books.d.ts +17 -0
- package/types/ctx-tools.d.ts +1 -0
- package/types/diary.d.ts +57 -0
- package/types/flow.d.ts +106 -0
- package/types/flowmodel.d.ts +25 -3
- package/types/jobkey.d.ts +11 -0
- package/types/jsonl-append.d.ts +6 -0
- package/types/model-step.d.ts +4 -1
- package/types/panel/author.d.ts +47 -17
- package/types/panel/authorcard.d.ts +3 -6
- package/types/panel/authorflows.d.ts +38 -10
- package/types/panel/authorstart.d.ts +15 -2
- package/types/panel/authortrigger.d.ts +73 -41
- package/types/panel/authorvalues.d.ts +6 -0
- package/types/panel/data.d.ts +101 -19
- package/types/panel/needsyou.d.ts +20 -0
- package/types/runvalues.d.ts +5 -0
- package/types/schedule.d.ts +69 -0
- package/types/setup.d.ts +0 -19
- package/types/summary-words.d.ts +105 -2
- package/types/thinking.d.ts +31 -0
- package/types/trigger-clock.d.ts +8 -7
- package/types/trigger-files.d.ts +17 -10
- package/types/trigger-line.d.ts +16 -8
- package/types/try-checks.d.ts +55 -0
- package/types/types.d.ts +10 -1
package/src/panel/author.js
CHANGED
|
@@ -57,8 +57,9 @@ import { draftStepList, reviseStepList } from '../draft-steps.js';
|
|
|
57
57
|
import {
|
|
58
58
|
capTooSmallText, draftingSpent, EARLIER_MISSING_SAY, potSplit, STARTED_OVER_FILE, usdText,
|
|
59
59
|
} from '../draftspend.js';
|
|
60
|
+
import { jobKeyOfCard } from '../jobkey.js';
|
|
60
61
|
import { flowValues, valuesHash } from '../runvalues.js';
|
|
61
|
-
import {
|
|
62
|
+
import { capAboveSpentText, capNotAboveSpent } from './authorvalues.js';
|
|
62
63
|
import { createFlowsDoor } from './authorflows.js';
|
|
63
64
|
import { createTriggerDoor } from './authortrigger.js';
|
|
64
65
|
import { createResumeDoor } from './authorresume.js';
|
|
@@ -77,6 +78,8 @@ const newId = () => `d-${Date.now().toString(36).padStart(10, '0')}-${randomByte
|
|
|
77
78
|
const FINISHED = new Set(['abandoned', 'signed']);
|
|
78
79
|
const LOG_TAIL_CHARS = 1500;
|
|
79
80
|
const STOPPED_SAY = 'The draft process ended before it finished (it was stopped from outside). Nothing was signed and nothing was sent. Draft again.';
|
|
81
|
+
/** M6 am9: written once into a draft folder when the draft is a red run's redraft of an UNCHANGED job: `{flow, fromRun}`. Sign reads it; nothing else decides. */
|
|
82
|
+
const RUN_OF_FILE = 'run-of.json';
|
|
80
83
|
const RED_SAY = 'The drafter\'s plan did not pass the checks. Nothing was signed and nothing was sent. Fix the job and draft again; this draft is kept on disk and is never reused.';
|
|
81
84
|
/** Up to 2 revises per draft (amendment 14 item 3); a revise is used once its `draft-<n>/` exists (a refusal at $0 makes none). */
|
|
82
85
|
export const MAX_REVISES = 2;
|
|
@@ -369,11 +372,15 @@ export function createAuthor(opts) {
|
|
|
369
372
|
* @param {any} body @param {{ ok: boolean, env: Record<string, string|undefined>, keyNames?: string[] }} loaded @param {number} [priorUsd]
|
|
370
373
|
* @returns {{ ok: true, card: any, prose: string }|{ ok: false, reply: { status: number, body: any } }}
|
|
371
374
|
*/
|
|
372
|
-
function vetCard(body, loaded, priorUsd = 0) {
|
|
375
|
+
function vetCard(body, loaded, priorUsd = 0, runOf = /** @type {string|undefined} */ (undefined)) {
|
|
373
376
|
const card = cardFields(body);
|
|
374
|
-
const checked = checkCard(card, { root: realpathSync(root), keys: providerKeys(loaded), home });
|
|
377
|
+
const checked = checkCard(card, { root: realpathSync(root), keys: providerKeys(loaded), home, runOf });
|
|
375
378
|
const modelSays = modelRefusal(card, loaded);
|
|
376
379
|
if (!checked.ok || modelSays.length > 0) return { ok: false, reply: { status: 400, body: { ok: false, refused: 'card', refusals: [...(checked.ok ? [] : checked.refusals), ...modelSays] } } };
|
|
380
|
+
// M4d am24: a Cap at or below what the earlier draft already spent is refused with the Cap note's words, before anything is made
|
|
381
|
+
if (capNotAboveSpent(Number(card.capUsd), priorUsd)) {
|
|
382
|
+
return { ok: false, reply: { status: 400, body: { ok: false, refused: 'card', refusals: [{ field: 'capUsd', say: capAboveSpentText(priorUsd, capFloorFor(true, home, card)) }] } } };
|
|
383
|
+
}
|
|
377
384
|
// amendment 4 item 1: a cap that does not fit this month refuses here too, at $0, with the one refusal text (the page's red note is only a courtesy)
|
|
378
385
|
const claim = monthlyClaim(Math.max(0, Number(card.capUsd) - priorUsd));
|
|
379
386
|
if (claim && 'problem' in claim) return { ok: false, reply: { status: 409, body: { ok: false, refused: 'monthly', say: `${claim.problem} — refusing rather than guess the monthly limit. Nothing spent.` } } };
|
|
@@ -395,6 +402,7 @@ export function createAuthor(opts) {
|
|
|
395
402
|
return { ...r, body: { ...r.body, flow: { ...r.body.flow, trigger: triggerDoor.card(r.body.flow.flow) } } };
|
|
396
403
|
},
|
|
397
404
|
run: flowsDoor.run,
|
|
405
|
+
reuseCheck: (b) => flowsDoor.reuseCheck(b),
|
|
398
406
|
runPrepare: flowsDoor.runPrepare,
|
|
399
407
|
trigger: triggerDoor,
|
|
400
408
|
resumePrepare: resumeDoor.prepare,
|
|
@@ -419,14 +427,16 @@ export function createAuthor(opts) {
|
|
|
419
427
|
// a Resume (amendment 4 item 1): only what the run still has to spend is held, so the note asks for `cap - spent`; a cap not above
|
|
420
428
|
// what is spent says so in the door's own words (red)
|
|
421
429
|
const spentUsd = typeof spent === 'string' && spent.trim() !== '' && Number.isFinite(Number(spent)) && Number(spent) > 0 ? Number(spent) : 0;
|
|
422
|
-
if (spentUsd > 0 && n <= spentUsd + 1e-9) {
|
|
423
|
-
return { status: 200, body: { ok: true, text: `The cap must be above what is already spent (at least $${leastResumeCapUsd(spentUsd, floorUsd).toFixed(2)}).`, red: true, floorUsd, floorText } };
|
|
424
|
-
}
|
|
425
430
|
// M4d am21 item 1: a card that belongs to a draft (a revise, or the Start over about to be made) is checked for the pot LEFT, as the doors do
|
|
426
431
|
const dd = draftsDir();
|
|
427
432
|
const priorDir = typeof draftId === 'string' && ID_RE.test(draftId) && dd !== null && listIds().includes(draftId) ? join(dd, draftId) : null;
|
|
428
433
|
const priorUsd = priorDir === null ? 0 : draftingSpent(priorDir, { ceilingUsd: floorUsd }).usd;
|
|
429
|
-
|
|
434
|
+
// M4d am23: a cap at or below what is already spent (a Resume's run, or the earlier draft's drafting) is red in the one sentence
|
|
435
|
+
const alreadyUsd = spentUsd + priorUsd;
|
|
436
|
+
if (capNotAboveSpent(n, alreadyUsd)) {
|
|
437
|
+
return { status: 200, body: { ok: true, text: capAboveSpentText(alreadyUsd, floorUsd), red: true, floorUsd, floorText } };
|
|
438
|
+
}
|
|
439
|
+
const claim = monthlyClaim(Math.max(0, n - alreadyUsd));
|
|
430
440
|
if (claim === null || 'problem' in claim) return { status: 200, body: { ok: true, text: '', red: false, floorUsd, floorText } };
|
|
431
441
|
return { status: 200, body: { ok: true, ...monthlyNote(claim), floorUsd, floorText } };
|
|
432
442
|
},
|
|
@@ -463,6 +473,19 @@ export function createAuthor(opts) {
|
|
|
463
473
|
* @returns {{ status: number, body: any }}
|
|
464
474
|
*/
|
|
465
475
|
start(body) {
|
|
476
|
+
/** @type {{flow: string, fromRun: string}|null} */
|
|
477
|
+
let runOf = null;
|
|
478
|
+
// M6 am6: a Reuse card drafts only when a job field changed; the server compares the card's job with the signed one, here, before any money or folder
|
|
479
|
+
if (body?.reuseOf && typeof body.reuseOf.flow === 'string') {
|
|
480
|
+
const signedKey = flowsDoor.signedJobKey(body.reuseOf.flow);
|
|
481
|
+
if (signedKey === null) return { status: 400, body: { ok: false, refused: 'reuse-flow', say: `"${body.reuseOf.flow}" is not a signed flow that reads, so it cannot be reused. Nothing was spent.` } };
|
|
482
|
+
if (jobKeyOfCard(String(body.job ?? '')) === signedKey) {
|
|
483
|
+
// M6 am9: a RED run (`[✗]`, by the panel's own glyph rule, read here) always drafts, job changed or not; the unchanged job's new plan is a run of the SAME workflow
|
|
484
|
+
if (!flowsDoor.runIsRed(body.reuseOf.flow, body.reuseOf.fromRun)) return { status: 409, body: { ok: false, refused: 'job-unchanged', say: 'No job field changed, so nothing needs drafting. Use Run or Sign & run. Nothing was spent.' } };
|
|
485
|
+
runOf = { flow: body.reuseOf.flow, fromRun: body.reuseOf.fromRun };
|
|
486
|
+
body = { ...body, flowName: runOf.flow };
|
|
487
|
+
}
|
|
488
|
+
}
|
|
466
489
|
const { loaded, keys } = keysNow();
|
|
467
490
|
if (!loaded.ok) return { status: 409, body: { ok: false, refused: 'keys-file', say: String(loaded.refusal) } };
|
|
468
491
|
const dd = draftsDir();
|
|
@@ -471,13 +494,16 @@ export function createAuthor(opts) {
|
|
|
471
494
|
// an earlier draft that is gone is never counted as $0: refused here, at $0, in words
|
|
472
495
|
const startedFrom = body?.startedOverFrom;
|
|
473
496
|
let priorUsd = 0;
|
|
497
|
+
if (startedFrom !== undefined && startedFrom !== null && startedFrom !== '' && typeof startedFrom !== 'string') {
|
|
498
|
+
return { status: 400, body: { ok: false, refused: 'started-over-from', say: 'startedOverFrom must be the id of an earlier draft (a string).' } };
|
|
499
|
+
}
|
|
474
500
|
if (typeof startedFrom === 'string' && startedFrom !== '') {
|
|
475
501
|
if (!ID_RE.test(startedFrom) || !listIds().includes(startedFrom)) return { status: 400, body: { ok: false, refused: 'earlier-missing', say: EARLIER_MISSING_SAY } };
|
|
476
502
|
const spent = draftingSpent(join(dd, startedFrom), { ceilingUsd: capFloorFor(true, home, cardFields(body)) });
|
|
477
503
|
if (spent.missingEarlier) return { status: 400, body: { ok: false, refused: 'earlier-missing', say: EARLIER_MISSING_SAY } };
|
|
478
504
|
priorUsd = spent.usd;
|
|
479
505
|
}
|
|
480
|
-
const vet = vetCard(body, loaded, priorUsd);
|
|
506
|
+
const vet = vetCard(body, loaded, priorUsd, runOf?.flow);
|
|
481
507
|
if (!vet.ok) return vet.reply;
|
|
482
508
|
const { card } = vet;
|
|
483
509
|
for (const id of listIds()) {
|
|
@@ -497,6 +523,7 @@ export function createAuthor(opts) {
|
|
|
497
523
|
if (typeof from === 'string' && ID_RE.test(from) && listIds().includes(from)) {
|
|
498
524
|
writeFileSync(join(dir, STARTED_OVER_FILE), `${JSON.stringify({ startedOverFrom: from })}\n`, { mode: 0o600, flag: 'wx' });
|
|
499
525
|
}
|
|
526
|
+
if (runOf !== null) writeFileSync(join(dir, RUN_OF_FILE), `${JSON.stringify(runOf)}\n`, { mode: 0o600, flag: 'wx' });
|
|
500
527
|
writeFileSync(proseFile, vet.prose, { mode: 0o600 });
|
|
501
528
|
let child;
|
|
502
529
|
try {
|
|
@@ -557,25 +584,24 @@ export function createAuthor(opts) {
|
|
|
557
584
|
const bad = checkInputRows(inputs);
|
|
558
585
|
if (bad.length > 0) return { status: 400, body: { ok: false, refused: 'inputs', refusals: bad } };
|
|
559
586
|
// '' = no typed id: the start claims the next `run-<n>` (M4e amendment 3); a typed one is checked here, before signing
|
|
560
|
-
|
|
587
|
+
let runId = typeof b.runId === 'string' ? b.runId : '';
|
|
561
588
|
if (runId !== '') {
|
|
562
589
|
const run = starter.checkRun(name, runId);
|
|
563
590
|
if (!run.ok) return { status: 400, body: { ok: false, refused: 'run-id', say: run.say } };
|
|
564
591
|
}
|
|
565
592
|
const signedBy = userInfo().username;
|
|
566
|
-
// M6: a
|
|
567
|
-
const
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
const editBlock = editOf !== null ? flowsDoor.editBlock(name) : null;
|
|
571
|
-
if (editBlock !== null) return { status: 409, body: { ok: false, refused: 'run-live', say: editBlock } };
|
|
593
|
+
// M6 am9: a red run's redraft of an unchanged job is signed as the plan of a NEW run of the same workflow (the workflow's files are not written)
|
|
594
|
+
const runOfRec = readJson(dir, RUN_OF_FILE);
|
|
595
|
+
const asRunOf = typeof runOfRec?.flow === 'string' ? { flow: runOfRec.flow } : undefined;
|
|
596
|
+
if (asRunOf !== undefined && runId !== '') return { status: 400, body: { ok: false, refused: 'run-id', say: 'This plan becomes the next run of its workflow by itself; a run name cannot be typed. Nothing was signed.' } };
|
|
572
597
|
const result = signDraft({
|
|
573
|
-
dir: join(dir, v.plan), approve: v.hash, signedBy, env: loaded.env, keyNames: loaded.keyNames, sessionDir: dir,
|
|
598
|
+
dir: join(dir, v.plan), approve: v.hash, signedBy, env: loaded.env, keyNames: loaded.keyNames, sessionDir: dir, ...(asRunOf ? { asRunOf } : {}),
|
|
574
599
|
});
|
|
575
600
|
if (!result.ok) {
|
|
576
601
|
const keys = providerKeys(loaded);
|
|
577
602
|
return { status: 409, body: { ok: false, refused: 'sign-refused', say: 'The draft did not pass the signing checks. Nothing was signed.', reds: result.reds.map((r) => scrub(String(r), keys)) } };
|
|
578
603
|
}
|
|
604
|
+
if (asRunOf !== undefined) runId = result.runId;
|
|
579
605
|
writeFileSync(join(dir, 'signed.json'), `${JSON.stringify({
|
|
580
606
|
at: new Date().toISOString(), signedBy, hash: v.hash, flow: name, runId: runId === '' ? null : runId,
|
|
581
607
|
})}\n`, { mode: 0o600 });
|
|
@@ -591,7 +617,7 @@ export function createAuthor(opts) {
|
|
|
591
617
|
}
|
|
592
618
|
}
|
|
593
619
|
const started = starter.start({
|
|
594
|
-
kind: 'sign', flow: name, runId, sources: inputs.map((r) => ({ role: r.role, path: r.path })), values,
|
|
620
|
+
kind: 'sign', flow: name, runId, sources: inputs.map((r) => ({ role: r.role, path: r.path })), values, ...(asRunOf ? { planRun: true, pick: result.modelPick ?? null } : {}),
|
|
595
621
|
}, loaded);
|
|
596
622
|
if (started.status !== 202) return { status: started.status, body: { ...started.body, signed: true, flow: name, say: `${started.body.say ?? 'The run did not start.'} The flow is signed; start it from "Run a signed flow".` } };
|
|
597
623
|
return { status: 202, body: { ...started.body, signed: true } };
|
|
@@ -623,7 +649,7 @@ export function createAuthor(opts) {
|
|
|
623
649
|
if (v.revisesLeft <= 0) return { status: 409, body: { ok: false, refused: 'no-revises-left', say: `This draft has used its ${MAX_REVISES} revises. Start over: it keeps the fields and gives ${MAX_REVISES} new revises.` } };
|
|
624
650
|
if (childRunning(dir)) return { status: 409, body: { ok: false, refused: 'draft-live', say: 'The draft process is still finishing. Try again in a moment.' } };
|
|
625
651
|
if (draftingSpent(dir).missingEarlier) return { status: 400, body: { ok: false, refused: 'earlier-missing', say: EARLIER_MISSING_SAY } };
|
|
626
|
-
const vet = vetCard(body, loaded, draftedSoFar(dir, v.card));
|
|
652
|
+
const vet = vetCard(body, loaded, draftedSoFar(dir, v.card), readJson(dir, RUN_OF_FILE)?.flow);
|
|
627
653
|
if (!vet.ok) return vet.reply;
|
|
628
654
|
for (const other of listIds()) {
|
|
629
655
|
if (other === id) continue;
|
package/src/panel/authorcard.js
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
} from 'node:fs';
|
|
15
15
|
import { isAbsolute, join } from 'node:path';
|
|
16
16
|
|
|
17
|
-
import { checkFlowName
|
|
17
|
+
import { checkFlowName } from '../flow.js';
|
|
18
18
|
import { checkSendDestination } from '../runner.js';
|
|
19
19
|
import { parseSignedText } from '../signed-text.js';
|
|
20
20
|
import { capTooSmallText } from '../draftspend.js';
|
|
@@ -174,8 +174,6 @@ export function cardFields(body) {
|
|
|
174
174
|
// M4d amendment 6: the picked model (a key name and a model id from Chat's menu) and the per-job "Thinking on" tick; present only when sent
|
|
175
175
|
...(str(b.provider).trim() !== '' || str(b.model).trim() !== '' ? { provider: str(b.provider).trim(), model: str(b.model).trim() } : {}),
|
|
176
176
|
...(b.thinking === true ? { thinking: true } : {}),
|
|
177
|
-
// M6: present only when this card EDITS a signed job: which job, and the signature hash it was opened at
|
|
178
|
-
...(typeof b.editOf?.flow === 'string' && typeof b.editOf?.flowHash === 'string' && b.editOf.flowHash !== '' ? { editOf: { flow: b.editOf.flow, flowHash: b.editOf.flowHash } } : {}),
|
|
179
177
|
};
|
|
180
178
|
}
|
|
181
179
|
|
|
@@ -332,26 +330,22 @@ export function destinationRefusal(dest, root) {
|
|
|
332
330
|
/**
|
|
333
331
|
* Every $0 check on the card, before any spawn (scope 6). Collects all refusals so the page can name every box.
|
|
334
332
|
* @param {ReturnType<typeof cardFields>} card
|
|
335
|
-
* @param {{ root: string, keys?: string[], home?: string }} ctx `keys` = the provider key values (the keys load's scrub list): a key value typed into any box is refused; `home` = the config home Settings is read from (the cap floor)
|
|
333
|
+
* @param {{ root: string, keys?: string[], home?: string, runOf?: string }} ctx `keys` = the provider key values (the keys load's scrub list): a key value typed into any box is refused; `home` = the config home Settings is read from (the cap floor)
|
|
336
334
|
* @returns {{ ok: true, prose: string }|{ ok: false, refusals: {field: string, say: string}[] }}
|
|
337
335
|
*/
|
|
338
|
-
export function checkCard(card, { root, keys = [], home }) {
|
|
336
|
+
export function checkCard(card, { root, keys = [], home, runOf }) {
|
|
339
337
|
/** @type {{field: string, say: string}[]} */
|
|
340
338
|
const refusals = [];
|
|
341
339
|
const no = (field, say) => refusals.push({ field, say });
|
|
342
340
|
|
|
343
|
-
// flow name: the CLI's own check, and not an existing flow (
|
|
341
|
+
// flow name: the CLI's own check, and not an existing flow (a changed job is a NEW workflow, M6 am5, so a taken name is always refused)
|
|
344
342
|
const name = checkFlowName(card.flowName);
|
|
345
343
|
if (!name.ok) no('flowName', `${name.red}. Use lowercase letters, digits, "-" or "_".`);
|
|
346
|
-
else
|
|
347
|
-
const was = signedHashOf(join(root, card.editOf.flow));
|
|
348
|
-
if (card.flowName !== card.editOf.flow) no('flowName', `The name of a job being edited cannot change. It stays "${card.editOf.flow}".`);
|
|
349
|
-
else if (was === null) no('flowName', `The job "${card.editOf.flow}" is no longer here, so it cannot be edited. Clear the card and start a new job.`);
|
|
350
|
-
else if (was !== card.editOf.flowHash) no('flowName', `"${card.editOf.flow}" was signed again since you opened it for editing. Open it again to edit the new version. Nothing was changed.`);
|
|
351
|
-
} else {
|
|
344
|
+
else {
|
|
352
345
|
let there = true;
|
|
353
346
|
try { lstatSync(join(root, card.flowName)); } catch { there = false; }
|
|
354
|
-
|
|
347
|
+
// M6 am9: a red run's redraft of an unchanged job is a run of THIS workflow, so its own name is the one name that is allowed to be taken
|
|
348
|
+
if (there && card.flowName !== runOf) no('flowName', `There is already a flow named "${card.flowName}" here. Pick a name that is not used yet.`);
|
|
355
349
|
}
|
|
356
350
|
|
|
357
351
|
// job
|
package/src/panel/authorflows.js
CHANGED
|
@@ -19,18 +19,20 @@ import { canFlowRun, willNotRunSay } from '../canrun.js';
|
|
|
19
19
|
import { loadCatalogue } from '../catalogue.js';
|
|
20
20
|
import { ConfigError, readConfig } from '../config.js';
|
|
21
21
|
import {
|
|
22
|
-
changeSentence, checkFlowName, listFlowNames, listRunIds, nextRunId, readFileInside, readFlow, resolveRunDir,
|
|
22
|
+
changeSentence, checkFlowName, listFlowNames, listRunIds, nextRunId, planForReuse, readFileInside, readFlow, resolveRunDir, suggestFlowName,
|
|
23
23
|
} from '../flow.js';
|
|
24
|
+
import { jobKeyOfCard, jobKeyOfSigned } from '../jobkey.js';
|
|
24
25
|
import { spendSummary } from '../monthly.js';
|
|
25
26
|
import { userInfo } from 'node:os';
|
|
26
27
|
import {
|
|
27
|
-
applyRunValues, flowValues, pickRunValues, valuesHash,
|
|
28
|
+
applyRunValues, flowValues, pickRunValues, runModelOf, valuesHash,
|
|
28
29
|
} from '../runvalues.js';
|
|
29
30
|
import {
|
|
30
31
|
capFloorOf, cardFromSigned, checkInputRows, runInputRows,
|
|
31
32
|
} from './authorcard.js';
|
|
32
33
|
import { checkValues } from './authorvalues.js';
|
|
33
|
-
import { getRunControls } from './data.js';
|
|
34
|
+
import { getRunControls, getRunGlyph } from './data.js';
|
|
35
|
+
import { SCHEDULE_BUSY_SAY } from '../summary-words.js';
|
|
34
36
|
import { providerKeys } from './spawn.js';
|
|
35
37
|
import { readModelRecord, writeModelRecord } from '../flowmodel.js';
|
|
36
38
|
|
|
@@ -40,7 +42,7 @@ export const editBlockedSay = (runId) => `finish or stop ${runId} first`;
|
|
|
40
42
|
export const EDIT_STARTING_SAY = 'a run is starting';
|
|
41
43
|
const NO_LIMIT = 'no monthly limit set';
|
|
42
44
|
/** The body keys a run start takes (amendment 5 item 2): the open boxes plus the hash of the second click. Any other key is refused by name. */
|
|
43
|
-
const RUN_KEYS = ['flow', 'inputs', 'runId', 'destination', 'capUsd', 'askWaits', 'hash', 'provider', 'model'];
|
|
45
|
+
const RUN_KEYS = ['flow', 'inputs', 'runId', 'destination', 'capUsd', 'askWaits', 'hash', 'provider', 'model', 'planOf'];
|
|
44
46
|
|
|
45
47
|
/**
|
|
46
48
|
* What the Run / Resume forms show of a signed flow (read from its own files, never from a model): the job lines as the signed text has them
|
|
@@ -118,7 +120,7 @@ export function createFlowsDoor(opts) {
|
|
|
118
120
|
/** The values one run ran with: the flow's own, with the run's newest signed version laid over (`applyRunValues`). Unreadable = the flow's own. @param {any} read @param {string} name @param {string} runDir */
|
|
119
121
|
function runValuesOf(read, name, runDir) {
|
|
120
122
|
const applied = applyRunValues(read.arbiter, name, read.signature.flow, pickRunValues(runDir));
|
|
121
|
-
return { values: flowValues(applied.ok ? applied.arbiter : read.arbiter) };
|
|
123
|
+
return { values: flowValues(applied.ok ? applied.arbiter : read.arbiter), model: runModelOf(pickRunValues(runDir)) };
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
/**
|
|
@@ -178,21 +180,41 @@ export function createFlowsDoor(opts) {
|
|
|
178
180
|
const flow = typeof body?.flow === 'string' ? body.flow : '';
|
|
179
181
|
const named = checkFlowName(flow);
|
|
180
182
|
if (!named.ok) return reply(400, { refused: 'flow', refusals: [{ field: 'flow', say: `${named.red}.` }] });
|
|
181
|
-
const
|
|
182
|
-
if (!
|
|
183
|
-
return reply(400, { refused: 'flow', refusals: [{ field: 'flow', say: `"${flow}" is not a signed flow that passes its checks, so it will not run.${changeSentence(
|
|
183
|
+
const read0 = readSigned(realRoot, flow);
|
|
184
|
+
if (!read0.ok) {
|
|
185
|
+
return reply(400, { refused: 'flow', refusals: [{ field: 'flow', say: `"${flow}" is not a signed flow that passes its checks, so it will not run.${changeSentence(read0.reds.map((r) => scrub(String(r), keys)))}` }], reds: read0.reds.map((r) => scrub(String(r), keys)) });
|
|
184
186
|
}
|
|
187
|
+
// M6 am9: a reuse of a run runs THAT run's plan. A plan that is the workflow's own changes nothing; a different kept plan is read (verified) and everything below
|
|
188
|
+
// (the roles, the values, the hash the second click carries) is that plan's; the start then gives the new run a verified copy of it. One that does not verify refuses.
|
|
189
|
+
let planFrom = null;
|
|
190
|
+
let planRead = null;
|
|
191
|
+
const planOf = typeof body?.planOf === 'string' ? body.planOf.trim() : '';
|
|
192
|
+
if (planOf !== '') {
|
|
193
|
+
const rr = resolveRunDir(join(realRoot, flow), planOf);
|
|
194
|
+
if (!rr.ok) return reply(400, { refused: 'plan', say: `"${planOf}" is not a run name. Nothing was started.` });
|
|
195
|
+
const cat = loadCatalogue();
|
|
196
|
+
if (!cat.ok) return reply(500, { refused: 'catalogue', say: 'The catalogue could not be read. Nothing was started.' });
|
|
197
|
+
const pf = planForReuse({ root: realRoot, name: flow, runDir: rr.runDir, catalogue: cat.primitives });
|
|
198
|
+
if (!pf.ok) return reply(409, { refused: 'plan', say: scrub(pf.red, keys) });
|
|
199
|
+
if (!pf.same) { planRead = pf.read; planFrom = planOf; }
|
|
200
|
+
}
|
|
201
|
+
const read = planRead ?? read0;
|
|
185
202
|
const can = canFlowRun(read); // amendment 21 item 1: a flow the list shows refused is refused here too, in the same sentence
|
|
186
203
|
if (!can.ok) {
|
|
187
204
|
const red = scrub(String(can.red), keys);
|
|
188
205
|
return reply(409, { refused: 'flow', say: willNotRunSay(flow, red), refusals: [{ field: 'flow', say: willNotRunSay(flow, red) }] });
|
|
189
206
|
}
|
|
190
|
-
// M4d amendment 7b: a flow with no recorded model takes the one picked in Chat (checked now, at the click)
|
|
191
|
-
|
|
192
|
-
const
|
|
207
|
+
// M4d amendment 7b: a flow with no recorded model takes the one picked in Chat (checked now, at the click) and records it.
|
|
208
|
+
// M6 am5/am6: a flow WITH a record keeps it unless the body names a different model: that is this run's own model (a run field, Sign & run).
|
|
209
|
+
const record = readModelRecord(join(realRoot, flow));
|
|
210
|
+
const noRecord = record === null;
|
|
211
|
+
const namedModel = typeof body?.provider === 'string' && body.provider !== '' && typeof body?.model === 'string' && body.model !== '';
|
|
212
|
+
const modelReds = noRecord || namedModel ? modelRefusal({ provider: body?.provider, model: body?.model }, loaded) : [];
|
|
193
213
|
const newPick = noRecord && modelReds.length === 0 ? { slot: body.provider, model: body.model } : null;
|
|
214
|
+
const runModel = !noRecord && namedModel && modelReds.length === 0 && (body.provider !== record.slot || body.model !== record.model) ? { slot: body.provider, model: body.model } : null;
|
|
215
|
+
const floorFacts = formFacts(read, home, runModel === null ? join(realRoot, flow) : undefined, runModel ?? newPick);
|
|
194
216
|
const checked = checkValues({
|
|
195
|
-
allowedKeys: RUN_KEYS, body, base: flowValues(read.arbiter), realRoot, capOnly: false, floorUsd:
|
|
217
|
+
allowedKeys: RUN_KEYS, body, base: flowValues(read.arbiter), realRoot, capOnly: false, floorUsd: floorFacts.capFloorUsd, floorModel: floorFacts.floorModel, monthlyClaim,
|
|
196
218
|
});
|
|
197
219
|
if (!checked.ok && checked.refused) return reply(checked.status, { refused: checked.refused, say: checked.say, refusals: checked.refusals });
|
|
198
220
|
/** @type {{field: string, say: string}[]} */
|
|
@@ -213,11 +235,12 @@ export function createFlowsDoor(opts) {
|
|
|
213
235
|
if (!run.ok) refusals.push({ field: 'runId', say: String(run.say) });
|
|
214
236
|
}
|
|
215
237
|
if (refusals.length > 0 || !checked.ok) return reply(400, { refused: 'run', refusals });
|
|
238
|
+
const values = runModel === null ? checked.values : { ...checked.values, model: runModel }; // the run's own model is part of what the second click signs
|
|
216
239
|
const hash = valuesHash({
|
|
217
|
-
flow, flowSignatureHash: read.signature.flow, runId: null, version: 0, values
|
|
240
|
+
flow, flowSignatureHash: read.signature.flow, runId: null, version: 0, values,
|
|
218
241
|
});
|
|
219
242
|
return {
|
|
220
|
-
ok: /** @type {true} */ (true), flow, read, declared, rows, runId, values
|
|
243
|
+
ok: /** @type {true} */ (true), flow, read, declared, rows, runId, values, differs: checked.differs || runModel !== null, hash, newPick, flowDir: join(realRoot, flow), planFrom,
|
|
221
244
|
};
|
|
222
245
|
}
|
|
223
246
|
|
|
@@ -229,13 +252,35 @@ export function createFlowsDoor(opts) {
|
|
|
229
252
|
* @param {string} realRoot @param {string} name @returns {string|null}
|
|
230
253
|
*/
|
|
231
254
|
function editBlockOf(realRoot, name) {
|
|
255
|
+
return liveRunBlock(realRoot, name, EDIT_STARTING_SAY, editBlockedSay, (c) => c?.canStop === true);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* M9 am8 item 1: is a run of this job starting or still working? The SCHEDULE's own one decision (clock fires and the file queue), NOT Edit's: a run
|
|
260
|
+
* waiting at its ask (`atAsk`) is not busy here, where `editBlockOf` still counts it (M6). Returns the sentence the busy row carries, or null.
|
|
261
|
+
* @param {string} realRoot @param {string} name @returns {string|null}
|
|
262
|
+
*/
|
|
263
|
+
function scheduleBusyOf(realRoot, name) {
|
|
264
|
+
return liveRunBlock(realRoot, name, SCHEDULE_BUSY_SAY, () => SCHEDULE_BUSY_SAY, (c) => c?.canStop === true && c.atAsk !== true);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* The one walk both questions share: a start folder still `starting` for this flow answers `startingSay`; else the newest run whose controls satisfy `counts`
|
|
269
|
+
* answers `sayFor(runId)`; else null. The callers differ only in what counts as live (the schedule does not count a run waiting at its ask).
|
|
270
|
+
* @param {string} realRoot @param {string} name @param {string} startingSay @param {(id: string) => string} sayFor @param {(c: any) => boolean} counts @returns {string|null}
|
|
271
|
+
*/
|
|
272
|
+
function liveRunBlock(realRoot, name, startingSay, sayFor, counts) {
|
|
232
273
|
const cat = loadCatalogue();
|
|
233
274
|
if (!cat.ok) return null;
|
|
234
|
-
if (starter.startingFor(name, []) !== null) return
|
|
275
|
+
if (starter.startingFor(name, []) !== null) return startingSay;
|
|
235
276
|
const ids = listRunIds(join(realRoot, name)).filter((id) => /^run-\d+$/.test(id)).sort((a, b) => Number(b.slice(4)) - Number(a.slice(4)));
|
|
277
|
+
// `canStop` is false for any run with an end row (`runControls`: running and atAsk both need none), so a run that has ended is skipped by the
|
|
278
|
+
// same `endRow` rule `loadRunContext` uses, read fresh each call (no cache), before a whole run context is built for it.
|
|
279
|
+
const flowDir = join(realRoot, name);
|
|
280
|
+
const history = readHistory(flowDir);
|
|
236
281
|
for (const id of ids) {
|
|
237
|
-
|
|
238
|
-
if (
|
|
282
|
+
if (endRow(history, join(flowDir, 'runs', id), id) !== null) continue;
|
|
283
|
+
if (counts(getRunControls({ root: realRoot, flow: name, runId: id, catalogue: cat.primitives }))) return sayFor(id);
|
|
239
284
|
}
|
|
240
285
|
return null;
|
|
241
286
|
}
|
|
@@ -250,12 +295,8 @@ export function createFlowsDoor(opts) {
|
|
|
250
295
|
flow: name, capUsd: read.arbiter.capUsd, roles, nextRunId: nextRunId(flowDir), lastRunId: last.runId, lastSources: last.sources, leftThisMonth: left,
|
|
251
296
|
runs: passed, stats,
|
|
252
297
|
...formFacts(read, home, flowDir),
|
|
253
|
-
// M6: the job as
|
|
254
|
-
edit: (() => {
|
|
255
|
-
const blocked = editBlockOf(realRoot, name);
|
|
256
|
-
if (blocked !== null) return { ok: false, say: blocked };
|
|
257
|
-
const c = cardFromSigned(read); return c.ok ? { ok: true, card: c.card, flowHash: read.signature.flow } : c;
|
|
258
|
-
})(),
|
|
298
|
+
// M6 am5/am6: the job as the card Reuse workflow opens (`ok:true, card`), or why the card cannot hold it (`ok:false, say`). Nothing blocks it: a reuse never changes the job it is opened from.
|
|
299
|
+
edit: (() => { const c = cardFromSigned(read); return c.ok ? { ok: true, card: c.card } : c; })(),
|
|
259
300
|
};
|
|
260
301
|
}
|
|
261
302
|
|
|
@@ -266,6 +307,59 @@ export function createFlowsDoor(opts) {
|
|
|
266
307
|
return realRoot === null ? null : editBlockOf(realRoot, name);
|
|
267
308
|
},
|
|
268
309
|
|
|
310
|
+
/** M9 am8 item 1: the sentence that says why the SCHEDULE cannot start a run now (one is starting or still working; a run waiting at its ask does not count), or null. @param {string} name */
|
|
311
|
+
scheduleBusy(name) {
|
|
312
|
+
const realRoot = realRootOrNull();
|
|
313
|
+
return realRoot === null ? null : scheduleBusyOf(realRoot, name);
|
|
314
|
+
},
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* M6 am9: is this run red, as the panel's glyph rule reads it (`[✗]` and nothing else; stopped `[■]` and waiting are not)? The server decides this itself,
|
|
318
|
+
* never from the page's word. False for a run that does not resolve.
|
|
319
|
+
* @param {string} flow @param {unknown} runId
|
|
320
|
+
*/
|
|
321
|
+
runIsRed(flow, runId) {
|
|
322
|
+
const realRoot = realRootOrNull();
|
|
323
|
+
if (realRoot === null || !checkFlowName(flow).ok || typeof runId !== 'string' || runId === '') return false;
|
|
324
|
+
const cat = loadCatalogue();
|
|
325
|
+
if (!cat.ok) return false;
|
|
326
|
+
return getRunGlyph({ root: realRoot, flow, runId, catalogue: cat.primitives })?.glyph === '[✗]';
|
|
327
|
+
},
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* M6 am5/am6: the job key of the signed flow `name` (see src/jobkey.js), or null when it is not a signed flow that reads. The draft door compares a Reuse card with it.
|
|
331
|
+
* @param {string} name
|
|
332
|
+
*/
|
|
333
|
+
signedJobKey(name) {
|
|
334
|
+
const realRoot = realRootOrNull();
|
|
335
|
+
if (realRoot === null || !checkFlowName(name).ok) return null;
|
|
336
|
+
const read = readSigned(realRoot, name);
|
|
337
|
+
return read.ok ? jobKeyOfSigned(read) : null;
|
|
338
|
+
},
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* `POST /api/author/reuse-check` (M6 am6 item 2): what the ONE Reuse card's button is for the fields as typed, decided here, at $0, writes nothing.
|
|
342
|
+
* Any job field changed (lines, guardrails, asks, steps) = `draft` (with the next free name); else a RED source run (`[✗]`, M6 am9) = `draft` with `runOf: true`; else a destination, cap or ask wait different from the
|
|
343
|
+
* signed value = `sign-run` (with the hash the second click carries); else `run`. Only a red run's colour enters, and the server reads it. `fromRun` (the run the card was
|
|
344
|
+
* opened from) is dropped before the run checks; `job` is read here and never reaches the run door.
|
|
345
|
+
* @param {any} body
|
|
346
|
+
*/
|
|
347
|
+
reuseCheck(body) {
|
|
348
|
+
const { job, fromRun, ...rest } = body && typeof body === 'object' ? body : /** @type {any} */ ({});
|
|
349
|
+
const realRoot = realRootOrNull();
|
|
350
|
+
if (realRoot !== null && typeof job === 'string' && typeof rest.flow === 'string' && checkFlowName(rest.flow).ok) {
|
|
351
|
+
const read = readSigned(realRoot, rest.flow);
|
|
352
|
+
if (read.ok && jobKeyOfCard(job) !== jobKeyOfSigned(read)) {
|
|
353
|
+
return { status: 200, body: { ok: true, kind: 'draft', suggestedName: suggestFlowName(realRoot, rest.flow) } };
|
|
354
|
+
}
|
|
355
|
+
// M6 am9: a RED run always drafts. The job is unchanged here, so the new plan is the next run of the SAME workflow (no new name). Only `[✗]` is red.
|
|
356
|
+
if (read.ok && this.runIsRed(rest.flow, fromRun)) return { status: 200, body: { ok: true, kind: 'draft', runOf: true } };
|
|
357
|
+
}
|
|
358
|
+
const r = this.runPrepare(rest);
|
|
359
|
+
if (r.status !== 200) return r;
|
|
360
|
+
return { status: 200, body: { ...r.body, kind: r.body.needsSign ? 'sign-run' : 'run' } };
|
|
361
|
+
},
|
|
362
|
+
|
|
269
363
|
/** M9: the source paths the newest run of this job used, role -> path (blank = none), for a start a trigger fires. @param {string} name @param {string[]} roles */
|
|
270
364
|
lastSourcesOf(name, roles) {
|
|
271
365
|
const realRoot = realRootOrNull();
|
|
@@ -362,7 +456,7 @@ export function createFlowsDoor(opts) {
|
|
|
362
456
|
};
|
|
363
457
|
}
|
|
364
458
|
const started = starter.start({
|
|
365
|
-
kind: 'run', flow: s.flow, runId: s.runId, sources: s.declared.map((role) => ({ role, path: /** @type {{path: string}} */ (s.rows.find((r) => r.role === role)).path })), values, pick: s.newPick,
|
|
459
|
+
kind: 'run', flow: s.flow, runId: s.runId, sources: s.declared.map((role) => ({ role, path: /** @type {{path: string}} */ (s.rows.find((r) => r.role === role)).path })), values, pick: s.newPick, planOf: s.planFrom,
|
|
366
460
|
}, loaded);
|
|
367
461
|
// M4d amendment 7b: the pick of a flow that had no record is written only once the start succeeded (a refused start leaves none), beside the signed files, never in them
|
|
368
462
|
if (started.status === 202 && s.newPick !== null) {
|
|
@@ -16,7 +16,7 @@ import { userInfo } from 'node:os';
|
|
|
16
16
|
|
|
17
17
|
import { scrub } from '../authoring.js';
|
|
18
18
|
import { loadCatalogue } from '../catalogue.js';
|
|
19
|
-
import { changeSentence, checkFlowName, readFileInside,
|
|
19
|
+
import { changeSentence, checkFlowName, readFileInside, readPlan, readRunPlan, resolveRunDir } from '../flow.js';
|
|
20
20
|
import { readResumeLock } from '../liveness.js';
|
|
21
21
|
import { auditSpend } from '../runner.js';
|
|
22
22
|
import {
|
|
@@ -56,7 +56,9 @@ export function createResumeDoor(opts) {
|
|
|
56
56
|
if (!rd.ok) return no(400, { refused: 'run-id', say: `${rd.red.replace(/^run: /, '')}.` });
|
|
57
57
|
const cat = loadCatalogue();
|
|
58
58
|
if (!cat.ok) return no(500, { refused: 'catalogue', say: 'The catalogue could not be loaded. Nothing was signed.' });
|
|
59
|
-
|
|
59
|
+
// am12: the run's own saved copy. A run with none (before M6, or not there yet) reads the flow here only for the refusal words or the 404 below; its controls say canResume false
|
|
60
|
+
const own = readRunPlan({ root: realRoot, name: flow, catalogue: cat.primitives, runDir: rd.runDir });
|
|
61
|
+
const read = !own.ok && 'noCopy' in own ? readPlan({ root: realRoot, name: flow, catalogue: cat.primitives, runDir: rd.runDir }) : own;
|
|
60
62
|
if (!read.ok) return no(400, { refused: 'flow', say: `"${flow}" is not a signed flow that passes its checks, so a run of it will not resume.${changeSentence(read.reds)}` });
|
|
61
63
|
const controls = getRunControls({
|
|
62
64
|
root: realRoot, flow, runId, catalogue: cat.primitives,
|
|
@@ -80,11 +82,13 @@ export function createResumeDoor(opts) {
|
|
|
80
82
|
if (!checked.ok) {
|
|
81
83
|
return no(checked.status, checked.refused ? { refused: checked.refused, say: checked.say, refusals: checked.refusals } : { refused: 'resume', refusals: checked.refusals, say: checked.refusals[0]?.say ?? 'Not resumed. Fix the marked box.' });
|
|
82
84
|
}
|
|
85
|
+
// M6 am5/am6: a resume carries the run's own model forward (the record is the FULL set in force, never a patch)
|
|
86
|
+
const values = base.model ? { ...checked.values, model: base.model } : checked.values;
|
|
83
87
|
const hash = valuesHash({
|
|
84
|
-
flow, flowSignatureHash: read.signature.flow, runId, version, values
|
|
88
|
+
flow, flowSignatureHash: read.signature.flow, runId, version, values,
|
|
85
89
|
});
|
|
86
90
|
return {
|
|
87
|
-
ok: /** @type {true} */ (true), flow, runId, runDir: rd.runDir, version, values
|
|
91
|
+
ok: /** @type {true} */ (true), flow, runId, runDir: rd.runDir, version, values, hash, spentUsd: spent.total, remainingUsd: checked.remainingUsd, signatureHash: read.signature.flow, hashGiven: b.hash,
|
|
88
92
|
};
|
|
89
93
|
}
|
|
90
94
|
|