fwdloop 0.9.0 → 0.10.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/package.json +1 -1
- package/src/ask.js +19 -0
- package/src/books.js +20 -0
- package/src/liveness.js +160 -0
- package/src/panel/data.js +315 -50
- package/src/panel/index.html +380 -110
- package/src/panel/server.js +3 -2
- package/src/runner.js +72 -18
- package/types/ask.d.ts +12 -0
- package/types/books.d.ts +19 -0
- package/types/liveness.d.ts +69 -0
- package/types/panel/data.d.ts +126 -15
- package/types/runner.d.ts +4 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fwdloop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|
package/src/ask.js
CHANGED
|
@@ -25,6 +25,24 @@ import { createHash, randomUUID } from 'node:crypto';
|
|
|
25
25
|
import { appendAudit } from './books.js';
|
|
26
26
|
import { readFileInside, resolveInside, readdirInside } from './flow.js';
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* M4c amendment 3: the ONE rule for "was this answer saved in time" — the
|
|
30
|
+
* answer's own saved time (`answeredAt`, written by `answerAsk`) against the
|
|
31
|
+
* ask's deadline, never the clock at the moment a resume restarts.
|
|
32
|
+
* on-time - answeredAt parses and is not after expiresAt
|
|
33
|
+
* late - answeredAt parses and is after expiresAt
|
|
34
|
+
* unreadable - answeredAt or expiresAt missing/unparseable: never on time
|
|
35
|
+
* @param {unknown} answeredAt
|
|
36
|
+
* @param {unknown} expiresAt
|
|
37
|
+
* @returns {'on-time'|'late'|'unreadable'}
|
|
38
|
+
*/
|
|
39
|
+
export function answerTiming(answeredAt, expiresAt) {
|
|
40
|
+
const a = typeof answeredAt === 'string' ? Date.parse(answeredAt) : NaN;
|
|
41
|
+
const e = typeof expiresAt === 'string' ? Date.parse(expiresAt) : NaN;
|
|
42
|
+
if (Number.isNaN(a) || Number.isNaN(e)) return 'unreadable';
|
|
43
|
+
return a > e ? 'late' : 'on-time';
|
|
44
|
+
}
|
|
45
|
+
|
|
28
46
|
/**
|
|
29
47
|
* M4b amendment 3 (SIGNED by hamr 2026-09-30): the ONE translation of a
|
|
30
48
|
* decision. The answer that was spelled `reject` is `redo`; `reject` is still
|
|
@@ -541,6 +559,7 @@ export function listArchivedAsks(runDir) {
|
|
|
541
559
|
expiresAt: ask.expiresAt,
|
|
542
560
|
evidence: readAskEvidence(ask),
|
|
543
561
|
answer,
|
|
562
|
+
...(typeof ask.emits === 'string' ? { emits: ask.emits } : {}),
|
|
544
563
|
});
|
|
545
564
|
}
|
|
546
565
|
|
package/src/books.js
CHANGED
|
@@ -28,6 +28,26 @@ function appendLine(filePath, row) {
|
|
|
28
28
|
appendFileSync(filePath, `${JSON.stringify(row)}\n`);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* M4c piece 1: the ONE writer of a run dir's `pids.jsonl` — append-only, never
|
|
33
|
+
* rewritten, not a book the arbiter reads. One row per process that works on
|
|
34
|
+
* the run (`src/liveness.js` `recordPid` builds the row).
|
|
35
|
+
* @param {string} runDir
|
|
36
|
+
* @param {{pid: number, startedAt: string, procStart: string|null, leg: 'run'|'resume'}} row
|
|
37
|
+
*/
|
|
38
|
+
export function appendPidRow(runDir, row) {
|
|
39
|
+
appendLine(join(runDir, 'pids.jsonl'), row);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The one reader of `pids.jsonl` (same tolerant `readLines` every book uses).
|
|
44
|
+
* @param {string} runDir
|
|
45
|
+
* @returns {any[]}
|
|
46
|
+
*/
|
|
47
|
+
export function readPidRows(runDir) {
|
|
48
|
+
return readLines(runDir, 'pids.jsonl');
|
|
49
|
+
}
|
|
50
|
+
|
|
31
51
|
/**
|
|
32
52
|
* Raw read-back of one JSONL book, one writer's own sibling reader (M4a
|
|
33
53
|
* piece 2, docs/wiki/the-module-ladder.md M4a scope item 2: "one reader per
|
package/src/liveness.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// M4c piece 1: "is this run's process alive?" — the ONE rule. The panel's `[▶]`
|
|
2
|
+
// and (M4d) the money hold both call `isFwdloopAlive`/`runLiveness`, so the two
|
|
3
|
+
// never disagree (bareloop's own open inconsistency). No panel imports here.
|
|
4
|
+
// borrowed-from: bareloop src/runlist.js@00b2b75
|
|
5
|
+
// fwdloop tightened bareloop's rule: bareloop matched `fwdloop` in ANY argv
|
|
6
|
+
// position after node; here only the SCRIPT (first non-option arg) may name it
|
|
7
|
+
// (the panel's own resume spawn passes `--root <dir>`, and a root whose last
|
|
8
|
+
// segment is `fwdloop` would otherwise read as a fwdloop process). And it adds
|
|
9
|
+
// `procStart` (field 22 of /proc/<pid>/stat) to close a recycled pid that is
|
|
10
|
+
// itself a DIFFERENT fwdloop process. Proof: poc/m4c/README.md.
|
|
11
|
+
import { lstatSync, readFileSync, writeSync } from 'node:fs';
|
|
12
|
+
import { basename, join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { appendPidRow, readPidRows } from './books.js';
|
|
15
|
+
|
|
16
|
+
const NAME = 'fwdloop';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Field 22 (`starttime`, clock ticks since boot) of /proc/<pid>/stat, as a
|
|
20
|
+
* string; `null` when unreadable. The comm field (2) may contain spaces and
|
|
21
|
+
* parens, so fields are counted from the LAST ')'.
|
|
22
|
+
* @param {number|'self'} pid
|
|
23
|
+
* @returns {string|null}
|
|
24
|
+
*/
|
|
25
|
+
export function procStartOf(pid) {
|
|
26
|
+
try {
|
|
27
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
28
|
+
const after = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
29
|
+
const v = after[19]; // field 3 is after[0], so field 22 is after[19]
|
|
30
|
+
return v !== undefined && /^\d+$/.test(v) ? v : null;
|
|
31
|
+
} catch { return null; }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Is `pid` a live fwdloop process — and, when `procStart` is given, the SAME
|
|
36
|
+
* process that recorded it?
|
|
37
|
+
* true - exists AND its command line is fwdloop (AND its starttime matches)
|
|
38
|
+
* false - gone, a zombie, alive but something else, or a different process on a recycled pid
|
|
39
|
+
* null - cannot tell (no /proc, or not ours to read): caller falls back to the 10-minute rule
|
|
40
|
+
* @param {number} pid
|
|
41
|
+
* @param {string|null} [procStart] the starttime recorded by the process itself; absent/null = not compared
|
|
42
|
+
* @returns {boolean|null}
|
|
43
|
+
*/
|
|
44
|
+
export function isFwdloopAlive(pid, procStart) {
|
|
45
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
46
|
+
try { process.kill(pid, 0); } catch (e) { if (e?.code !== 'EPERM') return false; /* EPERM: exists, not ours */ }
|
|
47
|
+
let cmdline;
|
|
48
|
+
try { cmdline = readFileSync(`/proc/${pid}/cmdline`, 'utf8'); } catch (e) {
|
|
49
|
+
return e?.code === 'ENOENT' && process.platform === 'linux' && hasProc() ? false : null; // died between the two calls vs no /proc
|
|
50
|
+
}
|
|
51
|
+
if (cmdline === '') return false; // zombie (or kernel thread): not a runner
|
|
52
|
+
const [argv0, ...rest] = cmdline.split('\0');
|
|
53
|
+
let isFwdloop;
|
|
54
|
+
if (basename(argv0) === NAME) isFwdloop = true; // direct exec of a renamed/compiled bin
|
|
55
|
+
else if (!basename(argv0).startsWith('node')) isFwdloop = false;
|
|
56
|
+
else {
|
|
57
|
+
// node <script> ...: only the SCRIPT (first non-option arg) may name fwdloop.
|
|
58
|
+
const script = rest.find((a) => a !== '' && !a.startsWith('-'));
|
|
59
|
+
isFwdloop = script !== undefined && basename(script) === NAME;
|
|
60
|
+
}
|
|
61
|
+
if (!isFwdloop) return false;
|
|
62
|
+
if (typeof procStart === 'string') {
|
|
63
|
+
const live = procStartOf(pid);
|
|
64
|
+
if (live === null) return null; // cannot compare: never a silent true
|
|
65
|
+
if (live !== procStart) return false; // a different fwdloop process reusing the pid
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function hasProc() {
|
|
71
|
+
try { readFileSync('/proc/self/cmdline'); return true; } catch { return false; }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The one writer call: this process records itself in the run dir's
|
|
76
|
+
* `pids.jsonl`, before its first step.
|
|
77
|
+
* @param {string} runDir
|
|
78
|
+
* @param {'run'|'resume'} leg
|
|
79
|
+
* @param {string} at ISO timestamp from the run's own clock
|
|
80
|
+
*/
|
|
81
|
+
export function recordPid(runDir, leg, at) {
|
|
82
|
+
appendPidRow(runDir, { pid: process.pid, startedAt: at, procStart: procStartOf('self'), leg });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Is the process that last worked on this run still alive? Reads the newest
|
|
87
|
+
* `pids.jsonl` row (the one reader, `readPidRows`).
|
|
88
|
+
* `unknown` = no row (a pre-M4c run), a malformed row, or the check said `null`.
|
|
89
|
+
* @param {string} runDir
|
|
90
|
+
* @returns {'running'|'gone'|'unknown'}
|
|
91
|
+
*/
|
|
92
|
+
export function runLiveness(runDir) {
|
|
93
|
+
const rows = readPidRows(runDir);
|
|
94
|
+
const row = rows[rows.length - 1];
|
|
95
|
+
if (!row || !Number.isInteger(row.pid)) return 'unknown';
|
|
96
|
+
const alive = isFwdloopAlive(row.pid, typeof row.procStart === 'string' ? row.procStart : null);
|
|
97
|
+
return alive === true ? 'running' : alive === false ? 'gone' : 'unknown';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* M4c amendment 2 (d): the resume lock records who holds it. The one writer:
|
|
102
|
+
* `resumeRun` calls this on the fd it just created with `wx`.
|
|
103
|
+
* @param {number} fd
|
|
104
|
+
*/
|
|
105
|
+
export function writeLockHolder(fd) {
|
|
106
|
+
writeSync(fd, JSON.stringify({ pid: process.pid, procStart: procStartOf('self') }));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The words the runner's refusal uses for a lock it cannot clear; the panel reads the same constant (M4c amendment 3 (e)). */
|
|
110
|
+
export const LOCK_NO_HOLDER = 'with no recorded holder';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The one reader of `resume.lock`. Through `lstat` (a symlinked lock is read as
|
|
114
|
+
* `empty`, never followed).
|
|
115
|
+
* none - no lock file
|
|
116
|
+
* live - the holder is a live fwdloop process (same rule as `[▶]`)
|
|
117
|
+
* dead - the holder is gone (or a different process on a recycled pid)
|
|
118
|
+
* unknown - liveness cannot be told (no /proc): never treated as dead
|
|
119
|
+
* empty - a lock with no readable holder (pre-amendment resumer, torn write)
|
|
120
|
+
* @param {string} runDir
|
|
121
|
+
* @returns {{state: 'none'|'live'|'dead'|'unknown'|'empty', pid: number|null, path: string}}
|
|
122
|
+
*/
|
|
123
|
+
export function readResumeLock(runDir) {
|
|
124
|
+
const path = join(runDir, 'resume.lock');
|
|
125
|
+
let st;
|
|
126
|
+
try { st = lstatSync(path); } catch { return { state: 'none', pid: null, path }; }
|
|
127
|
+
if (!st.isFile()) return { state: 'empty', pid: null, path };
|
|
128
|
+
let h;
|
|
129
|
+
try { h = JSON.parse(readFileSync(path, 'utf8')); } catch { return { state: 'empty', pid: null, path }; }
|
|
130
|
+
if (!h || !Number.isInteger(h.pid) || h.pid <= 0) return { state: 'empty', pid: null, path };
|
|
131
|
+
const procStart = typeof h.procStart === 'string' ? h.procStart : null;
|
|
132
|
+
// This very process holds it (two resumeRun calls in one process): alive by definition,
|
|
133
|
+
// whatever its command line says.
|
|
134
|
+
if (h.pid === process.pid && procStart === procStartOf('self')) return { state: 'live', pid: h.pid, path };
|
|
135
|
+
const alive = isFwdloopAlive(h.pid, procStart);
|
|
136
|
+
return { state: alive === true ? 'live' : alive === false ? 'dead' : 'unknown', pid: h.pid, path };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The run's books, for the 10-minute fallback: state, audit, model log, spend ledger, and the pid file itself. */
|
|
140
|
+
const BOOK_FILES = ['state.json', 'audit.jsonl', 'log.json', 'spend.jsonl', 'pids.jsonl'];
|
|
141
|
+
const FRESH_MS = 10 * 60 * 1000;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* bareloop's fallback (signed M4c scope item 2): when liveness is `unknown`
|
|
145
|
+
* (a pre-M4c run with no pid row, or /proc unreadable), a run with no end row
|
|
146
|
+
* whose books changed in the last 10 minutes is still working; older is not.
|
|
147
|
+
* Symlinked files are skipped (lstat), never followed.
|
|
148
|
+
* @param {string} runDir
|
|
149
|
+
* @param {number} [nowMs]
|
|
150
|
+
* @returns {boolean}
|
|
151
|
+
*/
|
|
152
|
+
export function booksFresh(runDir, nowMs = Date.now()) {
|
|
153
|
+
for (const f of BOOK_FILES) {
|
|
154
|
+
try {
|
|
155
|
+
const st = lstatSync(join(runDir, f));
|
|
156
|
+
if (st.isFile() && nowMs - st.mtimeMs <= FRESH_MS) return true;
|
|
157
|
+
} catch { /* missing file: not evidence of freshness */ }
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|