fwdloop 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/bin/fwdloop +136 -21
  2. package/package.json +1 -1
  3. package/src/ask.js +3 -0
  4. package/src/authoring.js +101 -13
  5. package/src/books.js +23 -2
  6. package/src/canrun.js +36 -0
  7. package/src/config.js +2 -1
  8. package/src/declaration.js +72 -2
  9. package/src/drafter.js +41 -3
  10. package/src/draftspend.js +70 -0
  11. package/src/flow.js +43 -0
  12. package/src/liveness.js +1 -1
  13. package/src/model-step.js +30 -4
  14. package/src/monthly.js +58 -13
  15. package/src/panel/author.js +482 -0
  16. package/src/panel/authorcard.js +297 -0
  17. package/src/panel/authorflows.js +312 -0
  18. package/src/panel/authorresume.js +149 -0
  19. package/src/panel/authorstart.js +257 -0
  20. package/src/panel/authorvalues.js +110 -0
  21. package/src/panel/data.js +228 -34
  22. package/src/panel/index.html +1745 -127
  23. package/src/panel/resume.js +6 -8
  24. package/src/panel/server.js +119 -119
  25. package/src/panel/settings.js +7 -5
  26. package/src/panel/spawn.js +77 -0
  27. package/src/provider.js +12 -17
  28. package/src/runner.js +639 -85
  29. package/src/runvalues.js +178 -0
  30. package/src/send.js +10 -3
  31. package/src/setup.js +120 -0
  32. package/types/authoring.d.ts +24 -2
  33. package/types/books.d.ts +14 -2
  34. package/types/canrun.d.ts +32 -0
  35. package/types/config.d.ts +1 -0
  36. package/types/declaration.d.ts +9 -1
  37. package/types/drafter.d.ts +15 -1
  38. package/types/draftspend.d.ts +21 -0
  39. package/types/flow.d.ts +23 -0
  40. package/types/liveness.d.ts +2 -2
  41. package/types/model-step.d.ts +3 -1
  42. package/types/monthly.d.ts +21 -0
  43. package/types/panel/author.d.ts +335 -0
  44. package/types/panel/authorcard.d.ts +120 -0
  45. package/types/panel/authorflows.d.ts +175 -0
  46. package/types/panel/authorresume.d.ts +32 -0
  47. package/types/panel/authorstart.d.ts +115 -0
  48. package/types/panel/authorvalues.d.ts +62 -0
  49. package/types/panel/data.d.ts +103 -12
  50. package/types/panel/server.d.ts +11 -33
  51. package/types/panel/spawn.d.ts +30 -0
  52. package/types/provider.d.ts +6 -9
  53. package/types/runner.d.ts +120 -5
  54. package/types/runvalues.d.ts +97 -0
  55. package/types/send.d.ts +5 -1
  56. package/types/setup.d.ts +83 -0
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @param {{ root: string, bin?: string }} opts
3
+ */
4
+ export function createStarter(opts: {
5
+ root: string;
6
+ bin?: string;
7
+ }): {
8
+ checkRun: (flow: string, runId: unknown) => {
9
+ ok: boolean;
10
+ say: string;
11
+ runDir?: undefined;
12
+ } | {
13
+ ok: boolean;
14
+ runDir: string;
15
+ say?: undefined;
16
+ };
17
+ /**
18
+ * Start `fwdloop run` for a flow whose every $0 check already passed at the calling door. Re-checks the run id here (nothing
19
+ * awaits between this check and the spawn). 202 { startId, runId } or a refusal; a refusal creates nothing.
20
+ * `runId` '' = no typed id: the start CLAIMS the next `run-<n>` itself (claimRunId, folder created exclusively; amendment 3).
21
+ * `values` (M4e amendment 5 item 3): the human's signed values for THIS run (the page's second click), written once into the run's own
22
+ * folder as `signed-values.json` BEFORE the spawn; the run then uses them (`pickRunValues`). None = the flow's own.
23
+ * @param {{ kind: 'run'|'sign', flow: string, runId: string, sources: {role: string, path: string}[], values?: any }} a
24
+ * @param {Record<string, string|undefined>} env the merged keys env: the child's env AND the scrub list
25
+ * @returns {{ status: number, body: any }}
26
+ */
27
+ start({ kind, flow, runId, sources, values, }: {
28
+ kind: "run" | "sign";
29
+ flow: string;
30
+ runId: string;
31
+ sources: {
32
+ role: string;
33
+ path: string;
34
+ }[];
35
+ values?: any;
36
+ }, env: Record<string, string | undefined>): {
37
+ status: number;
38
+ body: any;
39
+ };
40
+ /**
41
+ * Start `fwdloop continue <runId>` (M4e amendment 4 item 4): Resume of a stopped or cap-halted run, after the door wrote the run's new
42
+ * signed values version. The same detached-child shape as `start`; the start folder reads as `starting` until the child consumes
43
+ * `halt.json`, then `started`; a child that died with `halt.json` still there is `refused` with its own sentence. One start at a time per flow.
44
+ * @param {{ flow: string, runId: string, version: number }} a @param {Record<string, string|undefined>} env the merged keys env
45
+ * @returns {{ status: number, body: any }}
46
+ */
47
+ startContinue({ flow, runId, version }: {
48
+ flow: string;
49
+ runId: string;
50
+ version: number;
51
+ }, env: Record<string, string | undefined>): {
52
+ status: number;
53
+ body: any;
54
+ };
55
+ /**
56
+ * The newest start the human has not cleared, for a page that was reloaded: `starting` (re-attach the poll) or `refused` (show the
57
+ * CLI's sentence). Null when that start began (`started`: the run is in Runs) or there is none.
58
+ * @param {string[]} keys
59
+ */
60
+ newestOpen(keys: string[]): {
61
+ phase: string;
62
+ startId: string;
63
+ kind: any;
64
+ flow: any;
65
+ runId: any;
66
+ } | null;
67
+ /**
68
+ * `POST /api/author/start/:startId/clear`: the human dismisses a REFUSED start. Writes `cleared.json` once; any other phase is refused.
69
+ * @param {string} id @param {string[]} keys
70
+ */
71
+ clear(id: string, keys: string[]): {
72
+ status: number;
73
+ body: {
74
+ ok: boolean;
75
+ refused: string;
76
+ say?: undefined;
77
+ startId?: undefined;
78
+ };
79
+ } | {
80
+ status: number;
81
+ body: {
82
+ ok: boolean;
83
+ refused: string;
84
+ say: string;
85
+ startId?: undefined;
86
+ };
87
+ } | {
88
+ status: number;
89
+ body: {
90
+ ok: boolean;
91
+ startId: string;
92
+ refused?: undefined;
93
+ say?: undefined;
94
+ };
95
+ };
96
+ /** `GET /api/author/start/:startId`. @param {string} id @param {string[]} keys */
97
+ get(id: string, keys: string[]): {
98
+ status: number;
99
+ body: {
100
+ ok: boolean;
101
+ refused: string;
102
+ };
103
+ } | {
104
+ status: number;
105
+ body: {
106
+ phase: string;
107
+ startId: string;
108
+ kind: any;
109
+ flow: any;
110
+ runId: any;
111
+ ok: boolean;
112
+ refused?: undefined;
113
+ };
114
+ };
115
+ };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The smallest cap a Resume accepts: the next whole cent above what is spent, but never under the cap floor. ONE function for the
3
+ * door's refusal and the page's note, so the "at least $X" it names is itself accepted.
4
+ * @param {number} spentUsd @param {number} floorUsd
5
+ */
6
+ export function leastResumeCapUsd(spentUsd: number, floorUsd: number): number;
7
+ /**
8
+ * @param {object} a
9
+ * @param {string[]} a.allowedKeys the body keys this door takes; any other key is refused by name
10
+ * @param {any} a.body
11
+ * @param {{capUsd: number, destination: string|null, askWaits: Record<string,string>}} a.base the values in force now (the flow's own, or the run's newest signed version)
12
+ * @param {string} a.realRoot the flows root (real path)
13
+ * @param {boolean} a.capOnly Resume: destination and waits are never read from the body
14
+ * @param {number} a.floorUsd the smallest cap that funds one round of a step (amendment 4 item 2); 0 = none
15
+ * @param {number} [a.spentUsd] what the run has already spent (Resume): the cap must be above it
16
+ * @param {boolean} [a.always] Resume: the floor and the monthly check always apply. A Run applies them only when its cap differs from the
17
+ * flow's own (a plain Run keeps the CLI's own gate, as it always did), so an old flow signed below today's floor still runs plain.
18
+ * @param {(remainingUsd: number) => null | { problem: string } | { ok: boolean, room: any }} a.monthlyClaim the one monthly check
19
+ * @returns {{ ok: true, values: {capUsd: number, destination: string|null, askWaits: Record<string,string>}, differs: boolean, remainingUsd: number } | { ok: false, status: number, refusals: {field: string, say: string}[], refused?: string, say?: string }}
20
+ */
21
+ export function checkValues({ allowedKeys, body, base, realRoot, capOnly, floorUsd, spentUsd, monthlyClaim, always, }: {
22
+ allowedKeys: string[];
23
+ body: any;
24
+ base: {
25
+ capUsd: number;
26
+ destination: string | null;
27
+ askWaits: Record<string, string>;
28
+ };
29
+ realRoot: string;
30
+ capOnly: boolean;
31
+ floorUsd: number;
32
+ spentUsd?: number | undefined;
33
+ always?: boolean | undefined;
34
+ monthlyClaim: (remainingUsd: number) => null | {
35
+ problem: string;
36
+ } | {
37
+ ok: boolean;
38
+ room: any;
39
+ };
40
+ }): {
41
+ ok: true;
42
+ values: {
43
+ capUsd: number;
44
+ destination: string | null;
45
+ askWaits: Record<string, string>;
46
+ };
47
+ differs: boolean;
48
+ remainingUsd: number;
49
+ } | {
50
+ ok: false;
51
+ status: number;
52
+ refusals: {
53
+ field: string;
54
+ say: string;
55
+ }[];
56
+ refused?: string;
57
+ say?: string;
58
+ };
59
+ export const NEW_FLOW_SAY: "Only the files, the destination, the money cap and an ask's wait can change from one run to the next. A change to a job line, an ask's words or the steps is a new flow: use New job.";
60
+ export const RESUME_ONLY_CAP_SAY: "A resume changes only the cap. New files or a new destination or ask wait is a new run of this flow (Run a signed flow); a change to a job line or an ask is a new flow (New job).";
61
+ export { valuesHash };
62
+ import { valuesHash } from '../runvalues.js';
@@ -65,7 +65,7 @@ export function readSavedAnswer(runDir: string): {
65
65
  * @param {{savedAnswer: {askId:string, decision:string|null, answeredAt?:string|null}|null, attempt: any, ask?: any}} ctx
66
66
  * @returns {{state: 'starting'|'not-started'|'broken'|'took-over', askId: string|null, tries: number|null, maxTries: number|null, reason: string|null, label: string}|null}
67
67
  */
68
- export function deriveResumeState({ savedAnswer: saved, attempt, ask }: {
68
+ export function deriveResumeState({ savedAnswer: savedOnDisk, attempt, ask }: {
69
69
  savedAnswer: {
70
70
  askId: string;
71
71
  decision: string | null;
@@ -143,6 +143,7 @@ export function glyphPulses(g: {
143
143
  * `computeGlyph`, the derivation the M4a POC proved against every real run
144
144
  * on disk. Ladder wording (M4a scope item 4):
145
145
  * - `[✓]` passed — a history row with `outcome:'complete'`.
146
+ * - `[■]` stopped (M4e amendment 4) — a history row `stopped`, or `cap-halt` with a halt record to continue from.
146
147
  * - `[✗]` failed — a history row with any other outcome (never `[?]`).
147
148
  * - `[·]` waiting on you — parked (`ask.json` present), no consumed answer,
148
149
  * NOT past its own `expiresAt`.
@@ -174,10 +175,10 @@ export function glyphPulses(g: {
174
175
  * A saved answer with a live process is `[▶]` working on your answer.
175
176
  * - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
176
177
  * consumed, the process is gone, no end row: cannot be carried on.
177
- * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string}} ctx
178
- * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]', label: string}}
178
+ * @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string, resumable?: boolean}} ctx
179
+ * @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]'|'[■]', label: string}}
179
180
  */
180
- export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, }: {
181
+ export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock, resumable, }: {
181
182
  historyRow: any;
182
183
  askJson: any;
183
184
  consumedAnswerExists: boolean;
@@ -186,8 +187,9 @@ export function computeGlyph({ historyRow, askJson, consumedAnswerExists, hasSta
186
187
  liveness?: "running" | "gone" | "unknown";
187
188
  booksFresh?: boolean;
188
189
  lock?: string;
190
+ resumable?: boolean;
189
191
  }): {
190
- glyph: "[\u2713]" | "[\u2717]" | "[\u00B7]" | "[!]" | "[\u25B6]" | "[?]" | "[II]";
192
+ glyph: "[\u2713]" | "[\u2717]" | "[\u00B7]" | "[!]" | "[\u25B6]" | "[?]" | "[II]" | "[\u25A0]";
191
193
  label: string;
192
194
  };
193
195
  /**
@@ -327,20 +329,20 @@ export function deriveStepTryMarks(closeClass: string | null, rows: Array<{
327
329
  }>): Array<"\u2713" | "\u2717" | "\u00B7">;
328
330
  /**
329
331
  * A step group's own state word for the Audit tab's collapsed header —
330
- * the SAME rule `src/panel/index.html`'s client-side `stepBoxState` already
331
- * applies to the Run tab's map/cards (ported here so the Audit header can
332
- * be computed server-side too, off the SAME last-row-wins logic): `waiting`
332
+ * the ONE decider of a step's state: the Run tab's map and cards read it too
333
+ * (`groupState` on each step; the page keeps no rule of its own), last-row-wins: `waiting`
333
334
  * (the run is currently parked on this step — its last attempt's verdict is
334
335
  * `paused`/`refused`), `done` (the last attempt passed — `green`/`hitl`),
335
- * `stopped` (the last attempt failed for any other reason). A group only
336
+ * `user-stopped` (M4e amendment 10: the step's own last row is `stopped`, a Stop
337
+ * the human asked), `stopped` (the last attempt failed for any other reason). A group only
336
338
  * ever exists for a step with at least one row, so `pending` never appears
337
339
  * here (unlike the Run tab's map, which also covers never-attempted steps).
338
340
  * @param {Array<{verdict:string}>} rows one step's own audit rows, book order
339
- * @returns {'done'|'waiting'|'stopped'}
341
+ * @returns {'done'|'waiting'|'stopped'|'user-stopped'}
340
342
  */
341
343
  export function deriveStepGroupState(rows: Array<{
342
344
  verdict: string;
343
- }>): "done" | "waiting" | "stopped";
345
+ }>): "done" | "waiting" | "stopped" | "user-stopped";
344
346
  /**
345
347
  * hamr's 2026-09-27 exit-check review #1: the Audit tab's grouped-by-step
346
348
  * header pieces, computed HERE (server-side) so `src/panel/index.html` only
@@ -394,6 +396,44 @@ export function summarizeSpendRows(spendRows: any[]): {
394
396
  cacheReadTokens: number | null;
395
397
  cacheReadTokensWhy: string | null;
396
398
  };
399
+ /**
400
+ * M4e amendment 4 item 4: the ONE decision for the Run tab's Stop and Resume buttons (the Stop and Resume doors read the
401
+ * same object, so the page never offers what the door would refuse). `running` is the glyph's own rule (`isRunningNow`);
402
+ * a run with no process record yet is `starting`; a parked run (`[·]`/`[!]`, an open ask) is neither.
403
+ * @param {any} ctx `loadRunContext`'s result @param {string} glyph `computeGlyph`'s sign
404
+ * @returns {{canStop: boolean, atAsk: boolean, stopRequested: boolean, starting: boolean, canResume: boolean, resumeOutcome: string|null, spentUsd: number|null, spendComplete: boolean|null, capUsd: number|null}}
405
+ */
406
+ export function runControls(ctx: any, glyph: string, label?: string): {
407
+ canStop: boolean;
408
+ atAsk: boolean;
409
+ stopRequested: boolean;
410
+ starting: boolean;
411
+ canResume: boolean;
412
+ resumeOutcome: string | null;
413
+ spentUsd: number | null;
414
+ spendComplete: boolean | null;
415
+ capUsd: number | null;
416
+ };
417
+ /**
418
+ * `runControls` for one run read straight from the books (the Stop door's own read). `null` when the run does not resolve.
419
+ * @param {{root: string, flow: string, runId: string, catalogue: any}} opts
420
+ */
421
+ export function getRunControls({ root, flow, runId, catalogue, }: {
422
+ root: string;
423
+ flow: string;
424
+ runId: string;
425
+ catalogue: any;
426
+ }): {
427
+ canStop: boolean;
428
+ atAsk: boolean;
429
+ stopRequested: boolean;
430
+ starting: boolean;
431
+ canResume: boolean;
432
+ resumeOutcome: string | null;
433
+ spentUsd: number | null;
434
+ spendComplete: boolean | null;
435
+ capUsd: number | null;
436
+ } | null;
397
437
  /**
398
438
  * `GET /api/runs` — every flow under `root`, every run under each flow
399
439
  * (M4a scope item 2). A flow with zero runs still gets one row (`runId:
@@ -439,6 +479,54 @@ export function getRunDetail({ root, flow, runId, catalogue, resumeAttempt, }: {
439
479
  catalogue: any;
440
480
  resumeAttempt?: (flow: string, runId: string) => any;
441
481
  }): any | null;
482
+ /**
483
+ * What the Draft adds up to, ONE place (the Map box, the first step card and the Audit group all read it, so the three can never disagree).
484
+ * M4e amendment 9: `timeMs` is the card row's `at` to the sign row's `at` (the whole drafting, human time included), `null` when either has no
485
+ * usable `at`; never a sum of the model rows' `wallMs`. `calls` sums the rows' `calls`, a row with none counts 1 and sets `callsAtLeast`, so an
486
+ * unknown never shows as a smaller exact number; no model rows = `null`. `humanChecks` = the note rows + the sign row + the run's signed-values
487
+ * rows (the card is not a check), given by the caller as `ctx.humanChecks`. Money follows `costDisplay` ("at least $X" for a floor).
488
+ * @param {any[]} modelRows the draft and change rows (`kind` draft | change) of `setup.jsonl`
489
+ * @param {{cardAt?: any, signAt?: any, humanChecks?: number|null}} [ctx]
490
+ * @returns {{calls: number|null, callsAtLeast: boolean, timeMs: number|null, humanChecks: number|null, usd: number|null, spendComplete: boolean, cost: string|null, modelRows: number}}
491
+ */
492
+ export function draftTotals(modelRows: any[], ctx?: {
493
+ cardAt?: any;
494
+ signAt?: any;
495
+ humanChecks?: number | null;
496
+ }): {
497
+ calls: number | null;
498
+ callsAtLeast: boolean;
499
+ timeMs: number | null;
500
+ humanChecks: number | null;
501
+ usd: number | null;
502
+ spendComplete: boolean;
503
+ cost: string | null;
504
+ modelRows: number;
505
+ };
506
+ /**
507
+ * M4e amendment 6 item 4 / amendment 7 item 2: the Draft of a run — the flow's own `setup.jsonl` (card, drafts, changes, notes, sign), then this
508
+ * run's own signed-values rows (amendment 5: "Sign & run" for version 0, "Sign & resume" for a later version), every row in the SAME shape
509
+ * as the run's audit rows so the page draws both with one row builder. Reads only through the safe gateways; never throws.
510
+ * @param {string} flowDir @param {string} runDir
511
+ * @returns {{present: boolean, why: string|null, rows: any[], summary: {present: boolean, why?: string, calls?: number|null, callsAtLeast?: boolean, humanChecks?: number|null, timeMs?: number|null, usd?: number|null, spendComplete?: boolean, cost?: string|null, modelRows?: number}}}
512
+ */
513
+ export function getDraftBlock(flowDir: string, runDir: string): {
514
+ present: boolean;
515
+ why: string | null;
516
+ rows: any[];
517
+ summary: {
518
+ present: boolean;
519
+ why?: string;
520
+ calls?: number | null;
521
+ callsAtLeast?: boolean;
522
+ humanChecks?: number | null;
523
+ timeMs?: number | null;
524
+ usd?: number | null;
525
+ spendComplete?: boolean;
526
+ cost?: string | null;
527
+ modelRows?: number;
528
+ };
529
+ };
442
530
  /**
443
531
  * `GET /api/runs/:flow/:runId/audit` — the Audit/logs tab (M4a scope item
444
532
  * 2): the raw `audit.jsonl` rows, scoped strictly to this run (each run's
@@ -446,7 +534,7 @@ export function getRunDetail({ root, flow, runId, catalogue, resumeAttempt, }: {
446
534
  * shared-sidecar contamination risk here the way bareloop's gate-audit
447
535
  * sidecar had). `null` when the flow/runId doesn't resolve (404).
448
536
  * @param {{root: string, flow: string, runId: string}} opts
449
- * @returns {{flow:string, runId:string, rows:any[], groups:any[], empty:boolean, why:string|null}|null}
537
+ * @returns {{flow:string, runId:string, rows:any[], groups:any[], draft:ReturnType<typeof getDraftBlock>, empty:boolean, why:string|null}|null}
450
538
  */
451
539
  export function getRunAudit({ root, flow, runId }: {
452
540
  root: string;
@@ -457,6 +545,7 @@ export function getRunAudit({ root, flow, runId }: {
457
545
  runId: string;
458
546
  rows: any[];
459
547
  groups: any[];
548
+ draft: ReturnType<typeof getDraftBlock>;
460
549
  empty: boolean;
461
550
  why: string | null;
462
551
  } | null;
@@ -672,3 +761,5 @@ export const STUCK_REASONS: {
672
761
  why: string;
673
762
  };
674
763
  };
764
+ /** The words the Draft says for a flow with no `setup.jsonl` (M4e amendment 6 item 4; renamed Draft by amendment 7 item 2). */
765
+ export const NO_DRAFT_WORDS: "no draft record";
@@ -1,43 +1,17 @@
1
- /**
2
- * hamr's 2026-10-03 ruling "1A": the panel token is REUSED across restarts, so a bookmark of
3
- * `http://127.0.0.1:<port>/` keeps working once the link was opened. Reads the token file (same path
4
- * `writeTokenFile` writes); a new token only when the file is missing, unreadable, not a regular file,
5
- * not mode 0600, not ours, or not 64 hex chars (the caller then rewrites it 0600).
6
- * @param {{ port: number, dir?: string, env?: Record<string, string|undefined> }} a
7
- * @returns {string}
8
- */
9
- export function loadOrMakeToken({ port, dir, env }: {
10
- port: number;
11
- dir?: string;
12
- env?: Record<string, string | undefined>;
13
- }): string;
14
- /**
15
- * M4c-fix item 2: the token's one on-disk home — `<dir>/panel-<port>.token`, dir 0700, file 0600,
16
- * `dir` = `$XDG_RUNTIME_DIR/fwdloop`, else `~/.cache/fwdloop`. Refuses a dir that is a symlink or
17
- * not ours. The file is replaced (unlink, then exclusive create at 0600), never rewritten in place,
18
- * so it never exists with a looser mode. Returns the file's path.
19
- * @param {{ port: number, token: string, dir?: string, env?: Record<string, string|undefined> }} a
20
- */
21
- export function writeTokenFile({ port, token, dir, env, }: {
22
- port: number;
23
- token: string;
24
- dir?: string;
25
- env?: Record<string, string | undefined>;
26
- }): string;
27
1
  /**
28
2
  * Handle one request against the API + the page. Exported
29
3
  * separately from {@link createPanelServer} so tests can drive it without a
30
4
  * real listening socket where that is simpler.
31
5
  * @param {import('node:http').IncomingMessage} req
32
6
  * @param {import('node:http').ServerResponse} res
33
- * @param {{ root: string, port: number, token: string, resumer: ReturnType<typeof createResumer>, settings: ReturnType<typeof createSettings> }} opts
7
+ * @param {{ root: string, port: number, resumer: ReturnType<typeof createResumer>, settings: ReturnType<typeof createSettings>, author: ReturnType<typeof createAuthor> }} opts
34
8
  */
35
9
  export function handleRequest(req: import("node:http").IncomingMessage, res: import("node:http").ServerResponse, opts: {
36
10
  root: string;
37
11
  port: number;
38
- token: string;
39
12
  resumer: ReturnType<typeof createResumer>;
40
13
  settings: ReturnType<typeof createSettings>;
14
+ author: ReturnType<typeof createAuthor>;
41
15
  }): void;
42
16
  /**
43
17
  * Start the panel server. Binds `127.0.0.1` ONLY. A taken port is a LOUD,
@@ -45,14 +19,13 @@ export function handleRequest(req: import("node:http").IncomingMessage, res: imp
45
19
  * header). Resolves once actually listening; rejects on a bind error
46
20
  * (including `EADDRINUSE`) with a `.port` field on the error for the
47
21
  * caller's message.
48
- * @param {{ port?: number, root: string, token?: string, settings?: { home?: string, env?: Record<string,string|undefined>, fetch?: typeof fetch, now?: () => number }, resume?: { env?: Record<string,string|undefined>, bin?: string, logDir?: string, maxTries?: number, windowMs?: number, slotMs?: number } }} opts
49
- * `resume` is for tests only (a fake env/bin, a short retry window); the CLI passes none.
50
- * @returns {Promise<{ server: import('node:http').Server, port: number, token: string, close: () => Promise<void> }>}
22
+ * @param {{ port?: number, root: string, settings?: { home?: string, env?: Record<string,string|undefined>, fetch?: typeof fetch, now?: () => number }, resume?: { env?: Record<string,string|undefined>, bin?: string, logDir?: string, maxTries?: number, windowMs?: number, slotMs?: number }, author?: { env?: Record<string,string|undefined>, loadEnv?: () => any, bin?: string } }} opts
23
+ * `resume` and `author` are for tests only (a fake env/bin, a short retry window); the CLI passes none.
24
+ * @returns {Promise<{ server: import('node:http').Server, port: number, close: () => Promise<void> }>}
51
25
  */
52
26
  export function createPanelServer(opts: {
53
27
  port?: number;
54
28
  root: string;
55
- token?: string;
56
29
  settings?: {
57
30
  home?: string;
58
31
  env?: Record<string, string | undefined>;
@@ -67,10 +40,14 @@ export function createPanelServer(opts: {
67
40
  windowMs?: number;
68
41
  slotMs?: number;
69
42
  };
43
+ author?: {
44
+ env?: Record<string, string | undefined>;
45
+ loadEnv?: () => any;
46
+ bin?: string;
47
+ };
70
48
  }): Promise<{
71
49
  server: import("node:http").Server;
72
50
  port: number;
73
- token: string;
74
51
  close: () => Promise<void>;
75
52
  }>;
76
53
  /**
@@ -91,3 +68,4 @@ export function panelMain(argv: string[], ctx: {
91
68
  export const DEFAULT_PORT: 4800;
92
69
  import { createResumer } from './resume.js';
93
70
  import { createSettings } from './settings.js';
71
+ import { createAuthor } from './author.js';
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @param {{ bin: string, argv: string[], env: Record<string, string|undefined>, logPath: string }} a
3
+ * @returns {import('node:child_process').ChildProcess} the child (already `unref()`ed; the log fd is closed here)
4
+ */
5
+ export function spawnDetached({ bin, argv, env, logPath, }: {
6
+ bin: string;
7
+ argv: string[];
8
+ env: Record<string, string | undefined>;
9
+ logPath: string;
10
+ }): import("node:child_process").ChildProcess;
11
+ /**
12
+ * The ONE writer of a panel child's `pid.json` (`<dir>/pid.json`): the pid, its /proc starttime and when it was started.
13
+ * Called right after the spawn by the draft door and the start door. A child with no pid writes nothing.
14
+ * @param {string} dir @param {import('node:child_process').ChildProcess} child
15
+ */
16
+ export function writePidFile(dir: string, child: import("node:child_process").ChildProcess): void;
17
+ /** One JSON file of a panel folder through the one safe gateway; null when missing, unreadable or not an object. @param {string} dir @param {string} rel */
18
+ export function readJsonFile(dir: string, rel: string): any;
19
+ /**
20
+ * Is the child recorded in `<dir>/pid.json` running? true / false, never null: no pid.json = nothing known to be running =
21
+ * false; a liveness that cannot be told (no /proc) reads as running only while pid.json is fresh (M4c's rule).
22
+ * @param {string} dir
23
+ */
24
+ export function childRunning(dir: string): boolean;
25
+ /**
26
+ * The provider key VALUES in the merged env — the scrub list. The env that holds them is the SAME object that is the child's
27
+ * spawn env (POC (a), M4d wiring rule), so the list can never differ from what the child was given.
28
+ * @param {Record<string, string|undefined>} env
29
+ */
30
+ export function providerKeys(env: Record<string, string | undefined>): string[];
@@ -92,7 +92,6 @@ export function resolveModelRate(modelId: any, ratesTable?: {
92
92
  };
93
93
  'deepseek-flash': {
94
94
  in: number;
95
- cacheIn: number;
96
95
  out: number;
97
96
  source: string;
98
97
  };
@@ -109,15 +108,15 @@ export function resolveModelRate(modelId: any, ratesTable?: {
109
108
  * THE one price lookup (M4d scope item 6): every model call — run, resume, model step, drafter —
110
109
  * is booked and cap-checked from this. Per field: a Settings price (`config.prices[slot]`, USD per
111
110
  * 1M -> per 1K) wins, else the code table's row, else the table's HIGHEST entry for that field
112
- * (an unknown model; unknown cost is never 0). A cache-read price is known only from Settings or a
113
- * row's `cacheIn`; otherwise it is left out (`cacheReadMult` omitted -> bare-agent's 0.1x of `in`).
111
+ * (an unknown model; unknown cost is never 0). M4d amendment 2: cached input is priced AS input —
112
+ * `cacheReadMult` is always 1; a saved `cachedInPerM` and a row's `cacheIn` are never read here.
114
113
  *
115
114
  * @param {string} modelId
116
115
  * @param {{ slot?: string, config?: Record<string, any>, ratesTable?: Record<string, any> }} [opts]
117
116
  * @returns {{
118
117
  * rates: { in: number, out: number, cacheReadMult?: number },
119
118
  * perM: { inPerM: number, cachedInPerM: number, outPerM: number },
120
- * source: { in: 'settings'|'table'|'ceiling', cachedIn: 'settings'|'table'|'default-multiplier', out: 'settings'|'table'|'ceiling' },
119
+ * source: { in: 'settings'|'table'|'ceiling', cachedIn: 'input', out: 'settings'|'table'|'ceiling' },
121
120
  * suffix: string|null,
122
121
  * }}
123
122
  */
@@ -138,7 +137,7 @@ export function resolvePrices(modelId: string, { slot, config, ratesTable }?: {
138
137
  };
139
138
  source: {
140
139
  in: "settings" | "table" | "ceiling";
141
- cachedIn: "settings" | "table" | "default-multiplier";
140
+ cachedIn: "input";
142
141
  out: "settings" | "table" | "ceiling";
143
142
  };
144
143
  suffix: string | null;
@@ -224,7 +223,6 @@ export function ceilingCostUsd(modelId: any, ratesTable?: {
224
223
  };
225
224
  'deepseek-flash': {
226
225
  in: number;
227
- cacheIn: number;
228
226
  out: number;
229
227
  source: string;
230
228
  };
@@ -374,8 +372,8 @@ export const PROVIDER_SLOTS: Readonly<{
374
372
  * provider slot) wins over a row here, per field, through `resolvePrices` below —
375
373
  * the one lookup every model call is booked and cap-checked by (M4d, signed
376
374
  * 2026-10-05; that REPLACES the 2026-09-21 "edit a row here, never a runtime
377
- * flag" ruling). `cacheIn` (optional) is the cache-READ price per 1K; a row
378
- * without it books cache reads at bare-agent's default 0.1x of `in`.
375
+ * flag" ruling). Cached input is priced as input
376
+ * (M4d amendment 2): a row has no cache rate.
379
377
  */
380
378
  export const RATES_BY_SUFFIX: {
381
379
  'zai-org/GLM-5.2': {
@@ -430,7 +428,6 @@ export const RATES_BY_SUFFIX: {
430
428
  };
431
429
  'deepseek-flash': {
432
430
  in: number;
433
- cacheIn: number;
434
431
  out: number;
435
432
  source: string;
436
433
  };