fwdloop 0.10.0 → 0.12.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/bin/fwdloop +130 -20
- package/package.json +1 -1
- package/src/ask.js +200 -8
- package/src/authoring.js +34 -6
- package/src/config.js +255 -0
- package/src/drafter.js +12 -8
- package/src/keysfile.js +180 -0
- package/src/liveness.js +7 -7
- package/src/model-step.js +16 -6
- package/src/monthly.js +231 -0
- package/src/panel/data.js +200 -73
- package/src/panel/index.html +750 -120
- package/src/panel/lock.js +42 -0
- package/src/panel/resume.js +43 -7
- package/src/panel/server.js +295 -47
- package/src/panel/settings.js +268 -0
- package/src/provider.js +194 -35
- package/src/providershapes.js +19 -0
- package/src/runner.js +47 -10
- package/types/ask.d.ts +70 -1
- package/types/authoring.d.ts +3 -2
- package/types/config.d.ts +51 -0
- package/types/drafter.d.ts +4 -2
- package/types/keysfile.d.ts +90 -0
- package/types/liveness.d.ts +2 -1
- package/types/model-step.d.ts +3 -1
- package/types/monthly.d.ts +109 -0
- package/types/panel/data.d.ts +108 -29
- package/types/panel/lock.d.ts +21 -0
- package/types/panel/resume.d.ts +12 -1
- package/types/panel/server.d.ts +39 -3
- package/types/panel/settings.d.ts +23 -0
- package/types/provider.d.ts +126 -70
- package/types/providershapes.d.ts +11 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// M4c-fix amendment 2 (h): the human's "Remove the old lock". The ONE remover of a resume lock, and it lives in the
|
|
2
|
+
// panel: only `POST /api/remove-lock` (src/panel/server.js) calls it. No runner, resume or agent path imports this
|
|
3
|
+
// file — the machine never removes a lock it cannot judge (test: m4c-fix-remove-lock.test.js greps every src file).
|
|
4
|
+
import { realpathSync, unlinkSync } from 'node:fs';
|
|
5
|
+
import { join, sep } from 'node:path';
|
|
6
|
+
import { appendAudit } from '../books.js';
|
|
7
|
+
import { readResumeLock } from '../liveness.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Remove `<runDir>/resume.lock`, only if at THIS moment it has no recorded holder (`readResumeLock` -> `empty`;
|
|
11
|
+
* a live, dead or unknowable holder is never touched), and only if the lock file really sits inside this run dir
|
|
12
|
+
* (realpath of the run dir against realpath of `--root` at use time; a symlinked lock is unlinked as the link itself,
|
|
13
|
+
* never followed).
|
|
14
|
+
* Then one audit row through the one audit writer, shaped like the `ask-reopened` row (a pure note: cost 0, no model).
|
|
15
|
+
* @param {{root: string, runDir: string, nowIso?: string}} a
|
|
16
|
+
* @returns {{ok: true, at: string}|{ok: false, refused: 'no-lock'|'lock-has-holder'|'lock-outside-run', red: string}}
|
|
17
|
+
*/
|
|
18
|
+
export function removeOldLock({ root, runDir, nowIso = new Date().toISOString() }) {
|
|
19
|
+
const lock = readResumeLock(runDir);
|
|
20
|
+
if (lock.state === 'none') return { ok: false, refused: 'no-lock', red: 'there is no resume lock to remove' };
|
|
21
|
+
if (lock.state !== 'empty') return { ok: false, refused: 'lock-has-holder', red: `the resume lock has a recorded holder (${lock.state}) — not removed` };
|
|
22
|
+
let inside = false;
|
|
23
|
+
try {
|
|
24
|
+
const real = realpathSync(runDir);
|
|
25
|
+
const realRoot = realpathSync(root);
|
|
26
|
+
inside = (real + sep).startsWith(realRoot + sep) && real === realpathSync(join(lock.path, '..'));
|
|
27
|
+
} catch { inside = false; }
|
|
28
|
+
if (!inside) return { ok: false, refused: 'lock-outside-run', red: 'the resume lock is not inside this run — not removed' };
|
|
29
|
+
// Amendment 3 (d): look once more right before the unlink. This shrinks the window, it cannot close it: a resume
|
|
30
|
+
// can still take the lock between this read and the unlink — the written known limit of (d).
|
|
31
|
+
if (readResumeLock(runDir).state !== 'empty') return { ok: false, refused: 'lock-has-holder', red: 'the resume lock has a recorded holder — not removed' };
|
|
32
|
+
try { unlinkSync(lock.path); } catch (err) {
|
|
33
|
+
if (err.code === 'ENOENT') return { ok: false, refused: 'no-lock', red: 'there is no resume lock to remove' };
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
appendAudit(runDir, {
|
|
37
|
+
step: 'ask', attempt: null, class: null, verdict: 'lock-removed',
|
|
38
|
+
gap: `removed by human via panel at ${nowIso}`,
|
|
39
|
+
usd: 0, spendComplete: true, wallMs: 0, model: null, modelMatch: null, strike: false, at: nowIso, tokens: null, tools: null, refused: [],
|
|
40
|
+
});
|
|
41
|
+
return { ok: true, at: nowIso };
|
|
42
|
+
}
|
package/src/panel/resume.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
// numbers). Only the lock refusal is retried.
|
|
18
18
|
// 3. Still unconsumed after the last try (or a non-lock refusal): the attempt
|
|
19
19
|
// record says so, with the child's own refusal text verbatim; `data.js`
|
|
20
|
-
// turns that into "answer saved
|
|
20
|
+
// turns that into "your answer is saved; the run stopped before using it".
|
|
21
21
|
//
|
|
22
22
|
// No second mutex: exactly one resume applies the answer because the resume's
|
|
23
23
|
// own rename-to-consume is atomic (F44). This module only decides whether to
|
|
@@ -35,13 +35,15 @@
|
|
|
35
35
|
// could reach the log only through a provider error body that echoes it.
|
|
36
36
|
|
|
37
37
|
import { spawn } from 'node:child_process';
|
|
38
|
-
import { createHash } from 'node:crypto';
|
|
38
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
39
39
|
import {
|
|
40
40
|
closeSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, statSync, unlinkSync,
|
|
41
41
|
} from 'node:fs';
|
|
42
42
|
import { tmpdir } from 'node:os';
|
|
43
43
|
import { dirname, join } from 'node:path';
|
|
44
44
|
import { fileURLToPath } from 'node:url';
|
|
45
|
+
import { scrub } from '../authoring.js';
|
|
46
|
+
import { PROVIDER_SLOTS } from '../provider.js';
|
|
45
47
|
|
|
46
48
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
47
49
|
const BIN = join(HERE, '..', '..', 'bin', 'fwdloop');
|
|
@@ -74,8 +76,10 @@ function ensureLogDir(dir) {
|
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
|
-
* @param {{ root: string, env?: Record<string, string|undefined>, bin?: string, logDir?: string,
|
|
79
|
+
* @param {{ root: string, env?: Record<string, string|undefined>, loadEnv?: () => { ok: boolean, env: Record<string, string|undefined>, refusal: string|null }, bin?: string, logDir?: string,
|
|
78
80
|
* maxTries?: number, windowMs?: number, slotMs?: number }} opts
|
|
81
|
+
* `loadEnv` (M4d: the keys-file door, called before EVERY spawn so editing the file needs no panel restart) returns the merged env
|
|
82
|
+
* that is BOTH the child's spawn env AND its scrub list, or a refusal sentence that becomes the attempt's refusal (nothing spawns).
|
|
79
83
|
* `env`/`bin`/`logDir`/`maxTries`/`windowMs`/`slotMs` are injectable so tests neither sleep 10 s
|
|
80
84
|
* nor need a live key; production passes none of them. `slotMs` (default `windowMs / maxTries`,
|
|
81
85
|
* i.e. 2000 ms) is only the spacing between tries: a test sets it small and `windowMs` (the
|
|
@@ -84,26 +88,46 @@ function ensureLogDir(dir) {
|
|
|
84
88
|
*/
|
|
85
89
|
export function createResumer(opts) {
|
|
86
90
|
const {
|
|
87
|
-
root, env = process.env, bin = BIN, maxTries = RESUME_MAX_TRIES, windowMs = RESUME_WINDOW_MS, slotMs = windowMs / maxTries,
|
|
91
|
+
root, env = process.env, loadEnv, bin = BIN, maxTries = RESUME_MAX_TRIES, windowMs = RESUME_WINDOW_MS, slotMs = windowMs / maxTries,
|
|
88
92
|
} = opts;
|
|
89
93
|
const rootTag = createHash('sha256').update(root).digest('hex').slice(0, 8);
|
|
90
94
|
/** @type {Map<string, any>} */
|
|
91
95
|
const attempts = new Map();
|
|
96
|
+
/** Run key -> how many resume CHILD processes of this panel are alive for it. The one writer is
|
|
97
|
+
* `tryOnce` (+1 at spawn, -1 once, at the child's exit/error); `busy` only reads it. */
|
|
98
|
+
const live = new Map();
|
|
92
99
|
const keyOf = (flow, runId) => `${flow}/${runId}`;
|
|
93
100
|
|
|
94
101
|
/** One resume process. Resolves as soon as the books show the answer consumed,
|
|
95
102
|
* or the child exited, or `deadline` passed — never waits for a long resume. */
|
|
96
103
|
function tryOnce({ flow, runId, runDir, askId, logPath, deadline }) {
|
|
97
104
|
const consumedPath = join(runDir, `answer.${askId}.consumed.json`);
|
|
105
|
+
const loaded = loadEnv ? loadEnv() : { ok: true, env, refusal: null };
|
|
106
|
+
if (!loaded.ok) return Promise.resolve({ kind: 'exited', code: null, sig: null, text: String(loaded.refusal) });
|
|
107
|
+
const runEnv = loaded.env; // ONE object: the spawn env below AND the scrub list further down (POC (a) wiring rule)
|
|
98
108
|
const fd = openSync(logPath, 'a', 0o600);
|
|
99
109
|
const offset = statSync(logPath).size;
|
|
100
110
|
const child = spawn(process.execPath, [bin, 'resume', runId, '--flow', flow, '--root', root], {
|
|
101
|
-
detached: true, stdio: ['ignore', fd, fd], env,
|
|
111
|
+
detached: true, stdio: ['ignore', fd, fd], env: runEnv,
|
|
102
112
|
});
|
|
103
113
|
child.unref();
|
|
104
114
|
closeSync(fd);
|
|
115
|
+
const runKey = keyOf(flow, runId);
|
|
116
|
+
live.set(runKey, (live.get(runKey) ?? 0) + 1);
|
|
117
|
+
let released = false;
|
|
118
|
+
const release = () => {
|
|
119
|
+
if (released) return;
|
|
120
|
+
released = true;
|
|
121
|
+
const n = (live.get(runKey) ?? 1) - 1;
|
|
122
|
+
if (n <= 0) live.delete(runKey); else live.set(runKey, n);
|
|
123
|
+
};
|
|
124
|
+
child.once('exit', release);
|
|
125
|
+
child.once('error', release);
|
|
126
|
+
// The child's output is quoted in a refusal (`reason`) that reaches HTTP responses and the books: the
|
|
127
|
+
// provider key(s) of this panel's env are scrubbed here, the one place that reads the log slice (amendment 1 (e)(3)).
|
|
128
|
+
const keys = Object.values(PROVIDER_SLOTS).map((p) => runEnv[p.envVar]).filter(Boolean);
|
|
105
129
|
const sliceOfLog = () => {
|
|
106
|
-
try { return readFileSync(logPath).subarray(offset).toString('utf8').trim(); } catch { return ''; } // offset is in BYTES
|
|
130
|
+
try { return scrub(readFileSync(logPath).subarray(offset).toString('utf8').trim(), keys); } catch { return ''; } // offset is in BYTES
|
|
107
131
|
};
|
|
108
132
|
const settled = new Promise((resolve) => {
|
|
109
133
|
let done = false;
|
|
@@ -137,7 +161,9 @@ export function createResumer(opts) {
|
|
|
137
161
|
|
|
138
162
|
async function loop(rec, runDir) {
|
|
139
163
|
const t0 = Date.now();
|
|
140
|
-
|
|
164
|
+
// One log file per attempt record (never per run): a second resume of the same run must not
|
|
165
|
+
// append to, nor have its log deleted by, the first one's clean exit (M4c-fix item 7).
|
|
166
|
+
const logPath = join(ensureLogDir(opts.logDir), `resume-${rootTag}-${rec.flow}-${rec.runId}-${randomBytes(4).toString('hex')}.log`);
|
|
141
167
|
rec.logPath = logPath;
|
|
142
168
|
const consumed = () => existsSync(join(runDir, `answer.${rec.askId}.consumed.json`));
|
|
143
169
|
for (let n = 1; n <= maxTries; n += 1) {
|
|
@@ -176,6 +202,9 @@ export function createResumer(opts) {
|
|
|
176
202
|
const key = keyOf(flow, runId);
|
|
177
203
|
const old = attempts.get(key);
|
|
178
204
|
if (old) old.superseded = true;
|
|
205
|
+
// The previous attempt's reason is stale once a newer one starts (routes never start one while a
|
|
206
|
+
// child is alive): drop its log so refusal logs do not pile up, one per attempt (item 7).
|
|
207
|
+
if (old && old.state !== 'in-flight' && old.logPath) { try { unlinkSync(old.logPath); } catch { /* already gone */ } }
|
|
179
208
|
const rec = {
|
|
180
209
|
flow, runId, askId, state: 'in-flight', tries: 0, maxTries, windowMs, refusal: null, startedAt: Date.now(), endedAt: null, logPath: null, superseded: false,
|
|
181
210
|
};
|
|
@@ -183,6 +212,13 @@ export function createResumer(opts) {
|
|
|
183
212
|
loop(rec, runDir).catch((e) => finish(rec, 'stuck', `the panel could not start the resume: ${e.message}`));
|
|
184
213
|
return { ...rec };
|
|
185
214
|
},
|
|
215
|
+
/** True while this run is already resuming: a resume child of this panel is alive for it, or its
|
|
216
|
+
* latest attempt is still starting (between tries). Routes refuse a second resume while true.
|
|
217
|
+
* @param {string} flow @param {string} runId */
|
|
218
|
+
busy(flow, runId) {
|
|
219
|
+
const k = keyOf(flow, runId);
|
|
220
|
+
return (live.get(k) ?? 0) > 0 || attempts.get(k)?.state === 'in-flight';
|
|
221
|
+
},
|
|
186
222
|
/** A copy of the run's attempt record, or null (none yet, or the panel restarted).
|
|
187
223
|
* @param {string} flow @param {string} runId */
|
|
188
224
|
get(flow, runId) {
|