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/src/panel/data.js
CHANGED
|
@@ -28,12 +28,15 @@ import {
|
|
|
28
28
|
readFlow, listFlowNames, listRunIds, resolveRunDir, checkFlowName, readFileInside, readdirInside,
|
|
29
29
|
} from '../flow.js';
|
|
30
30
|
import {
|
|
31
|
-
readAudit, readHistory, auditRowTokens, auditRowAt, auditRowTools,
|
|
31
|
+
readAudit, readHistory, readPidRows, auditRowTokens, auditRowAt, auditRowTools,
|
|
32
32
|
} from '../books.js';
|
|
33
33
|
import { readAsk, readRunState, readLog } from '../runner.js';
|
|
34
34
|
import { readSpendRows } from '../provider.js';
|
|
35
35
|
import {
|
|
36
|
-
|
|
36
|
+
runLiveness, booksFresh, readResumeLock, LOCK_NO_HOLDER,
|
|
37
|
+
} from '../liveness.js';
|
|
38
|
+
import {
|
|
39
|
+
readAskEvidence, listArchivedAsks, normalizeDecision, DECISION_STATUS, answerTiming,
|
|
37
40
|
} from '../ask.js';
|
|
38
41
|
|
|
39
42
|
/** `{ ok:false, red }` result shape every exported function here can return
|
|
@@ -130,7 +133,7 @@ function hasConsumedAnswer(runDir, askId) {
|
|
|
130
133
|
* through `readFileInside` (a symlinked file is "none"). `null` when there is
|
|
131
134
|
* none or it is unreadable/unparseable or names no askId.
|
|
132
135
|
* @param {string} runDir
|
|
133
|
-
* @returns {{askId: string, decision: string|null}|null}
|
|
136
|
+
* @returns {{askId: string, decision: string|null, answeredAt: string|null}|null}
|
|
134
137
|
*/
|
|
135
138
|
export function readSavedAnswer(runDir) {
|
|
136
139
|
const r = readFileInside(runDir, 'answer.json');
|
|
@@ -138,7 +141,11 @@ export function readSavedAnswer(runDir) {
|
|
|
138
141
|
try {
|
|
139
142
|
const a = JSON.parse(r.text);
|
|
140
143
|
if (a && typeof a.askId === 'string' && a.askId.length > 0) {
|
|
141
|
-
return {
|
|
144
|
+
return {
|
|
145
|
+
askId: a.askId,
|
|
146
|
+
decision: typeof a.decision === 'string' ? normalizeDecision(a.decision) : null,
|
|
147
|
+
answeredAt: typeof a.answeredAt === 'string' ? a.answeredAt : null,
|
|
148
|
+
};
|
|
142
149
|
}
|
|
143
150
|
} catch { /* unparseable: not a usable answer */ }
|
|
144
151
|
return null;
|
|
@@ -154,10 +161,15 @@ export const RESUME_REASON_UNKNOWN = 'reason unknown: the panel restarted, so it
|
|
|
154
161
|
* unconsumed. From the panel's in-memory attempt record (`attempt`, may be
|
|
155
162
|
* null): whether a resume is still being started, and the resume's own refusal.
|
|
156
163
|
* `null` when there is nothing to say (no saved answer and no attempt).
|
|
157
|
-
*
|
|
164
|
+
* M4c amendment 3: `ask` (the run's open `ask.json`, may be null). A saved answer for that ask that was
|
|
165
|
+
* NOT saved in time (`answerTiming`) once the deadline has passed is not a resumable answer: it reads as
|
|
166
|
+
* none, so the run is `[!]` expired, the same as an ask nobody answered.
|
|
167
|
+
* @param {{savedAnswer: {askId:string, decision:string|null, answeredAt?:string|null}|null, attempt: any, ask?: any}} ctx
|
|
158
168
|
* @returns {{state: 'starting'|'not-started'|'took-over', askId: string|null, tries: number|null, maxTries: number|null, reason: string|null, label: string}|null}
|
|
159
169
|
*/
|
|
160
|
-
export function deriveResumeState({ savedAnswer, attempt }) {
|
|
170
|
+
export function deriveResumeState({ savedAnswer: saved, attempt, ask = null }) {
|
|
171
|
+
const pastDeadline = ask && saved && ask.askId === saved.askId && Date.parse(ask.expiresAt) < Date.now();
|
|
172
|
+
const savedAnswer = pastDeadline && answerTiming(saved.answeredAt, ask.expiresAt) !== 'on-time' ? null : saved;
|
|
161
173
|
const tries = attempt ? attempt.tries : null;
|
|
162
174
|
const maxTries = attempt ? attempt.maxTries : null;
|
|
163
175
|
if (savedAnswer) {
|
|
@@ -184,6 +196,50 @@ export function deriveResumeState({ savedAnswer, attempt }) {
|
|
|
184
196
|
return null;
|
|
185
197
|
}
|
|
186
198
|
|
|
199
|
+
export const WAITING_LABEL = 'waiting on you (parked, unanswered)';
|
|
200
|
+
export const STUCK_LABEL = 'stuck — answer saved, click try the resume again';
|
|
201
|
+
export const STUCK_LOCK_LABEL = 'stuck — remove the old resume lock by hand, then try again';
|
|
202
|
+
export const CRASHED_LABEL = 'crashed after taking your answer — start a fresh run';
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* M4c amendment 2 (a): the ONE rule for "stuck". The answer is saved and not yet
|
|
206
|
+
* taken (`resume.state === 'not-started'`: `answer.json` is on disk; the panel's
|
|
207
|
+
* transient 'starting' is not stuck) and nothing is carrying the run on: the
|
|
208
|
+
* newest pid row's process is not alive AND the resume lock is not held by a
|
|
209
|
+
* live process (`readResumeLock`, src/liveness.js). computeGlyph, the Inbox
|
|
210
|
+
* (`listStops`) and Runs (`listRuns`) all read this, never a second copy.
|
|
211
|
+
* @param {{resume?: any, liveness?: 'running'|'gone'|'unknown', lock?: string}} ctx
|
|
212
|
+
* @returns {boolean}
|
|
213
|
+
*/
|
|
214
|
+
export function isStuck({ resume, liveness, lock }) {
|
|
215
|
+
return !!resume && resume.state === 'not-started' && liveness !== 'running' && lock !== 'live';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* M4c amendment 3 (e): the ONE choice of the stuck label. After a "Try the resume again" the runner
|
|
220
|
+
* refused for a lock with no recorded holder (`LOCK_NO_HOLDER`, src/liveness.js) and that lock is
|
|
221
|
+
* still there unreadable (`lock === 'empty'`), trying again can never work: the label says to remove
|
|
222
|
+
* the file by hand. Every other stuck run keeps `STUCK_LABEL`.
|
|
223
|
+
* @param {{resume?: any, lock?: string}} ctx
|
|
224
|
+
* @returns {string}
|
|
225
|
+
*/
|
|
226
|
+
export function stuckLabelFor({ resume, lock }) {
|
|
227
|
+
return lock === 'empty' && typeof resume?.reason === 'string' && resume.reason.includes(LOCK_NO_HOLDER)
|
|
228
|
+
? STUCK_LOCK_LABEL : STUCK_LABEL;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* M4c item 1: does this sign pulse? Only `[▶]` running and `[·]` WAITING ON YOU
|
|
233
|
+
* do, and `[II]` stuck (M4c amendment 2: it is not a final state). The other
|
|
234
|
+
* `[·]` states (answered not resumed, resume starting) are not waiting on the
|
|
235
|
+
* human, so they stay still. The one place that decides.
|
|
236
|
+
* @param {{glyph: string, label: string}} g a `computeGlyph` result
|
|
237
|
+
* @returns {boolean}
|
|
238
|
+
*/
|
|
239
|
+
export function glyphPulses(g) {
|
|
240
|
+
return g.glyph === '[▶]' || g.glyph === '[II]' || (g.glyph === '[·]' && g.label === WAITING_LABEL);
|
|
241
|
+
}
|
|
242
|
+
|
|
187
243
|
/**
|
|
188
244
|
* The glyph + human-words label for one run — ported verbatim (in spirit,
|
|
189
245
|
* from real derivation, not a shortcut) from `poc/m4/panel-data.mjs`'s
|
|
@@ -205,18 +261,27 @@ export function deriveResumeState({ savedAnswer, attempt }) {
|
|
|
205
261
|
* - `[·]` answered, not resumed yet — parked, a consumed answer exists, but
|
|
206
262
|
* no history row yet (resume hasn't finished) — always in words, never
|
|
207
263
|
* the same line as "waiting on you", never `[?]`.
|
|
208
|
-
* - `[?]` died / unknown — no history row, no open ask,
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* on disk. Never guessed into `[✗]` or `[✓]`.
|
|
264
|
+
* - `[?]` died / unknown — no history row, no open ask, and either the
|
|
265
|
+
* pid row's process is gone (M4c), or there is no pid row / no /proc and
|
|
266
|
+
* the books are older than 10 minutes. Never guessed into `[✗]` or `[✓]`.
|
|
212
267
|
* - `[·]` answer saved, resume not started / starting (M4b amendment 1) —
|
|
213
268
|
* `answer.json` still on disk (`resume` = `deriveResumeState`): never
|
|
214
269
|
* "waiting on you" (the human already answered) and never a success.
|
|
215
|
-
*
|
|
216
|
-
*
|
|
270
|
+
* - `[▶]` running (M4c) — no end row, no open unanswered ask, and either the
|
|
271
|
+
* newest pid row's process is alive and is fwdloop (`liveness`, src/liveness.js)
|
|
272
|
+
* or (`liveness` unknown) the run's books changed in the last 10 minutes
|
|
273
|
+
* (`booksFresh`). A gone process is `[?]`. Absent `liveness`/`booksFresh`
|
|
274
|
+
* (a direct caller) reads as unknown / not fresh.
|
|
275
|
+
* - `[II]` stuck (M4c amendment 2, `isStuck`) — the answer is saved, not taken,
|
|
276
|
+
* and no live process (pid row or resume lock holder) carries the run on.
|
|
277
|
+
* A saved answer with a live process is `[▶]` working on your answer.
|
|
278
|
+
* - `[?]` crashed after taking your answer (amendment 2 (f)) — the answer was
|
|
279
|
+
* consumed, the process is gone, no end row: cannot be carried on.
|
|
280
|
+
* @param {{historyRow: any, askJson: any, consumedAnswerExists: boolean, hasStateJson: boolean, resume?: any, liveness?: 'running'|'gone'|'unknown', booksFresh?: boolean, lock?: string}} ctx
|
|
281
|
+
* @returns {{glyph: '[✓]'|'[✗]'|'[·]'|'[!]'|'[▶]'|'[?]'|'[II]', label: string}}
|
|
217
282
|
*/
|
|
218
283
|
export function computeGlyph({
|
|
219
|
-
historyRow, askJson, consumedAnswerExists, hasStateJson, resume,
|
|
284
|
+
historyRow, askJson, consumedAnswerExists, hasStateJson, resume, liveness, booksFresh, lock,
|
|
220
285
|
}) {
|
|
221
286
|
if (historyRow) {
|
|
222
287
|
if (historyRow.outcome === 'complete') return { glyph: '[✓]', label: 'passed' };
|
|
@@ -224,9 +289,13 @@ export function computeGlyph({
|
|
|
224
289
|
if (historyRow.outcome === 'rerun') return { glyph: '[✗]', label: 'stopped by you (rerun), a fresh run was started' };
|
|
225
290
|
return { glyph: '[✗]', label: `failed (${historyRow.outcome ?? 'unknown outcome'})` };
|
|
226
291
|
}
|
|
227
|
-
if (askJson && resume &&
|
|
292
|
+
if (askJson && resume && resume.state === 'starting') {
|
|
228
293
|
return { glyph: '[·]', label: resume.label };
|
|
229
294
|
}
|
|
295
|
+
if (askJson && resume && resume.state === 'not-started') {
|
|
296
|
+
if (isStuck({ resume, liveness, lock })) return { glyph: '[II]', label: stuckLabelFor({ resume, lock }) };
|
|
297
|
+
return { glyph: '[▶]', label: 'working on your answer' };
|
|
298
|
+
}
|
|
230
299
|
// No history row. A park never writes one, and a consumed answer file
|
|
231
300
|
// stays on disk until resume writes its own history row — "no history
|
|
232
301
|
// row" alone never means died.
|
|
@@ -235,15 +304,24 @@ export function computeGlyph({
|
|
|
235
304
|
if (Number.isFinite(expiresMs) && Date.now() > expiresMs) {
|
|
236
305
|
return { glyph: '[!]', label: 'ask expired, not resumed yet' };
|
|
237
306
|
}
|
|
238
|
-
return { glyph: '[·]', label:
|
|
307
|
+
return { glyph: '[·]', label: WAITING_LABEL };
|
|
239
308
|
}
|
|
240
309
|
if (askJson && consumedAnswerExists) {
|
|
310
|
+
// M4c: the answer was consumed — a resume process took it. Alive = working on it.
|
|
311
|
+
if (liveness === 'running') return { glyph: '[▶]', label: 'working on your answer' };
|
|
312
|
+
if (liveness === 'gone') return { glyph: '[?]', label: CRASHED_LABEL };
|
|
241
313
|
return { glyph: '[·]', label: 'answered, not resumed yet' };
|
|
242
314
|
}
|
|
315
|
+
// M4c: no end row, no open ask — is a process still working? Pid row first
|
|
316
|
+
// (`liveness`, src/liveness.js); with no row or no /proc (`unknown`), the
|
|
317
|
+
// 10-minute books rule. Never `[▶]` off a parked ask (branches above).
|
|
318
|
+
if (liveness === 'running') return { glyph: '[▶]', label: 'running' };
|
|
319
|
+
if (liveness === 'gone') return { glyph: '[?]', label: 'the process working on this run is gone (no end row was written)' };
|
|
320
|
+
if (booksFresh) return { glyph: '[▶]', label: 'running (no pid record; its books changed in the last 10 minutes)' };
|
|
243
321
|
if (hasStateJson && !askJson) {
|
|
244
322
|
return { glyph: '[?]', label: 'running or died: unknown (parked state with no open ask and no history row)' };
|
|
245
323
|
}
|
|
246
|
-
return { glyph: '[?]', label: 'running or died: unknown (
|
|
324
|
+
return { glyph: '[?]', label: 'running or died: unknown (no pid record, books older than 10 minutes)' };
|
|
247
325
|
}
|
|
248
326
|
|
|
249
327
|
/**
|
|
@@ -697,7 +775,7 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
|
|
|
697
775
|
const stateJson = readRunState(runDir);
|
|
698
776
|
const consumedAnswerExists = hasConsumedAnswer(runDir, askJson && typeof askJson.askId === 'string' ? askJson.askId : null);
|
|
699
777
|
// A finished run has no resume to report (its history row says how it ended).
|
|
700
|
-
const resume = historyRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(runDir), attempt });
|
|
778
|
+
const resume = historyRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(runDir), attempt, ask: askJson });
|
|
701
779
|
|
|
702
780
|
return {
|
|
703
781
|
flowName,
|
|
@@ -714,16 +792,35 @@ function loadRunContext(root, flowDir, runDir, flowName, runId, catalogue, attem
|
|
|
714
792
|
consumedAnswerExists,
|
|
715
793
|
hasStateJson: stateJson !== null,
|
|
716
794
|
resume,
|
|
795
|
+
// M4c: only read for a run with no end row (an end row always wins).
|
|
796
|
+
liveness: historyRow ? 'unknown' : runLiveness(runDir),
|
|
797
|
+
lock: historyRow ? 'none' : readResumeLock(runDir).state,
|
|
798
|
+
booksFresh: historyRow ? false : booksFresh(runDir),
|
|
799
|
+
// M4c exit walk: when the process that is working on the run started (its newest pid row's
|
|
800
|
+
// own `startedAt`), so a `[▶]` run has a real time and never "parked or died". Not a book change.
|
|
801
|
+
startedAt: historyRow ? null : lastPidStartedAt(runDir),
|
|
717
802
|
};
|
|
718
803
|
}
|
|
719
804
|
|
|
805
|
+
/** @param {string} runDir @returns {string|null} */
|
|
806
|
+
function lastPidStartedAt(runDir) {
|
|
807
|
+
const rows = readPidRows(runDir);
|
|
808
|
+
const at = rows[rows.length - 1]?.startedAt;
|
|
809
|
+
return typeof at === 'string' && !Number.isNaN(Date.parse(at)) ? at : null;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/** The ONE running rule for wording is the glyph's own (`computeGlyph` -> `[▶]`, which already
|
|
813
|
+
* folds in `runLiveness`): a run with no end row whose glyph is `[▶]` is running. */
|
|
814
|
+
const RUNNING_WHY = 'still running — not finished yet';
|
|
815
|
+
/** @param {any} ctx @param {string} glyph */
|
|
816
|
+
const isRunningNow = (ctx, glyph) => !ctx.historyRow && glyph === '[▶]';
|
|
817
|
+
|
|
720
818
|
/**
|
|
721
819
|
* `GET /api/runs` — every flow under `root`, every run under each flow
|
|
722
820
|
* (M4a scope item 2). A flow with zero runs still gets one row (`runId:
|
|
723
821
|
* null`, an explicit `why`) — no flow is silently missing from the list.
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
* (a real ordering, never `Date.now()` guessed in for a missing `at`).
|
|
822
|
+
* Ordered by `orderRuns` (waiting on you, running, then newest `at` first; a row
|
|
823
|
+
* with no `at` is never given a guessed one).
|
|
727
824
|
* @param {{root: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
|
|
728
825
|
* `resumeAttempt`: the panel's in-memory resume-attempt lookup (M4b piece 2); absent = none.
|
|
729
826
|
* @returns {any[]}
|
|
@@ -750,6 +847,12 @@ export function listRuns({ root, catalogue, resumeAttempt }) {
|
|
|
750
847
|
}
|
|
751
848
|
const ctx = loadRunContext(root, run.flowDir, run.runDir, flowName, runId, catalogue, resumeAttempt?.(flowName, runId) ?? null);
|
|
752
849
|
const { glyph, label } = computeGlyph(ctx);
|
|
850
|
+
// M4c amendment 1 (c): the ask waiting on the human, by the same rule
|
|
851
|
+
// listStops uses (deriveAskOpenFields) — the least time left if several.
|
|
852
|
+
const waitingAsk = runAsksInOrder(run.runDir, !!ctx.historyRow)
|
|
853
|
+
.map((ask) => ({ ask, ...deriveAskOpenFields(ask, !!ctx.historyRow, ctx.resume) }))
|
|
854
|
+
.filter((r) => r.waiting)
|
|
855
|
+
.reduce((best, r) => (best === null || (r.timeLeftMs ?? Infinity) < (best.timeLeftMs ?? Infinity) ? r : best), /** @type {any} */ (null));
|
|
753
856
|
let spend;
|
|
754
857
|
if (ctx.historyRow) {
|
|
755
858
|
const cd = costDisplay(ctx.historyRow.spentUsd, ctx.historyRow.spendComplete);
|
|
@@ -771,25 +874,60 @@ export function listRuns({ root, catalogue, resumeAttempt }) {
|
|
|
771
874
|
runId,
|
|
772
875
|
glyph,
|
|
773
876
|
label,
|
|
877
|
+
pulse: glyphPulses({ glyph, label }),
|
|
878
|
+
stuck: glyph === '[II]',
|
|
774
879
|
resume: ctx.resume,
|
|
775
880
|
spend,
|
|
776
|
-
spendWhy: (!ctx.historyRow && spend === null)
|
|
881
|
+
spendWhy: (!ctx.historyRow && spend === null)
|
|
882
|
+
? (isRunningNow(ctx, glyph) ? 'running — no priced spend yet' : 'no history row yet and no priced spend.jsonl rows — nothing to floor')
|
|
883
|
+
: null,
|
|
884
|
+
waiting: waitingAsk !== null,
|
|
885
|
+
waitingAskId: waitingAsk ? waitingAsk.ask.askId : null,
|
|
886
|
+
timeLeftMs: waitingAsk ? waitingAsk.timeLeftMs : null,
|
|
777
887
|
at: ctx.historyRow ? ctx.historyRow.at : null,
|
|
778
888
|
askedAt,
|
|
779
|
-
|
|
889
|
+
startedAt: isRunningNow(ctx, glyph) ? ctx.startedAt : null,
|
|
890
|
+
atWhy: (ctx.historyRow || askedAt) ? null
|
|
891
|
+
: isRunningNow(ctx, glyph)
|
|
892
|
+
? (ctx.startedAt ? null : 'running — start time not recorded (no pid record)')
|
|
893
|
+
: 'no history row yet (parked or died before one was written)',
|
|
780
894
|
});
|
|
781
895
|
}
|
|
782
896
|
}
|
|
783
|
-
|
|
784
|
-
|
|
897
|
+
return orderRuns(rows);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* The ONE ordering of Runs — History and Workflows both render the list in the
|
|
902
|
+
* order given (M4c amendment 1 (c)); a flow's top row is therefore the first
|
|
903
|
+
* run of that flow here, so a waiting run is the parent with no second rule:
|
|
904
|
+
* 1 waiting on you — least time left first; a stuck run (`[II]`, amendment 2)
|
|
905
|
+
* after the ones with a timer;
|
|
906
|
+
* 2 running (`[▶]`, the glyph's own liveness rule);
|
|
907
|
+
* 3 everything else — finish time `at`, newest first.
|
|
908
|
+
* A row with no known time keeps input order at the end of its section.
|
|
909
|
+
* @param {any[]} rows
|
|
910
|
+
* @returns {any[]}
|
|
911
|
+
*/
|
|
912
|
+
export function orderRuns(rows) {
|
|
913
|
+
const sectionOf = (r) => ((r.waiting || r.stuck) ? 1 : r.glyph === '[▶]' ? 2 : 3);
|
|
785
914
|
return rows
|
|
786
915
|
.map((row, index) => ({ row, index }))
|
|
787
916
|
.sort((a, b) => {
|
|
917
|
+
const sa = sectionOf(a.row);
|
|
918
|
+
const sb = sectionOf(b.row);
|
|
919
|
+
if (sa !== sb) return sa - sb;
|
|
920
|
+
if (sa === 1) {
|
|
921
|
+
if (!!a.row.stuck !== !!b.row.stuck) return a.row.stuck ? 1 : -1;
|
|
922
|
+
const at = typeof a.row.timeLeftMs === 'number' ? a.row.timeLeftMs : Infinity;
|
|
923
|
+
const bt = typeof b.row.timeLeftMs === 'number' ? b.row.timeLeftMs : Infinity;
|
|
924
|
+
return at !== bt ? at - bt : a.index - b.index;
|
|
925
|
+
}
|
|
788
926
|
const am = typeof a.row.at === 'string' ? Date.parse(a.row.at) : NaN;
|
|
789
927
|
const bm = typeof b.row.at === 'string' ? Date.parse(b.row.at) : NaN;
|
|
790
928
|
const aKnown = Number.isFinite(am);
|
|
791
929
|
const bKnown = Number.isFinite(bm);
|
|
792
|
-
if (aKnown && bKnown) return bm - am;
|
|
930
|
+
if (aKnown && bKnown && am !== bm) return bm - am;
|
|
793
931
|
if (aKnown !== bKnown) return aKnown ? -1 : 1;
|
|
794
932
|
return a.index - b.index;
|
|
795
933
|
})
|
|
@@ -1046,7 +1184,8 @@ export function getRunDetail({
|
|
|
1046
1184
|
const wallMs = ctx.historyRow && typeof ctx.historyRow.wallMs === 'number' ? ctx.historyRow.wallMs : null;
|
|
1047
1185
|
const wallMsWhy = wallMs !== null
|
|
1048
1186
|
? null
|
|
1049
|
-
: (ctx.historyRow ? 'history row has no wallMs recorded'
|
|
1187
|
+
: (ctx.historyRow ? 'history row has no wallMs recorded'
|
|
1188
|
+
: isRunningNow(ctx, glyph) ? RUNNING_WHY : 'no history row yet (parked or died before completion)');
|
|
1050
1189
|
|
|
1051
1190
|
let spend;
|
|
1052
1191
|
if (ctx.historyRow) {
|
|
@@ -1069,9 +1208,10 @@ export function getRunDetail({
|
|
|
1069
1208
|
runId,
|
|
1070
1209
|
glyph,
|
|
1071
1210
|
label,
|
|
1211
|
+
pulse: glyphPulses({ glyph, label }),
|
|
1072
1212
|
resume: ctx.resume,
|
|
1073
1213
|
outcome: ctx.historyRow ? ctx.historyRow.outcome : null,
|
|
1074
|
-
outcomeWhy: ctx.historyRow ? null : 'no history row (parked or died before completion)',
|
|
1214
|
+
outcomeWhy: ctx.historyRow ? null : (isRunningNow(ctx, glyph) ? RUNNING_WHY : 'no history row (parked or died before completion)'),
|
|
1075
1215
|
capUsd: ctx.historyRow ? ctx.historyRow.capUsd : (ctx.flowRead.ok ? ctx.flowRead.arbiter.capUsd : null),
|
|
1076
1216
|
spend,
|
|
1077
1217
|
model,
|
|
@@ -1092,9 +1232,12 @@ export function getRunDetail({
|
|
|
1092
1232
|
askedAt: (!ctx.historyRow && ctx.askJson && typeof ctx.askJson.askedAt === 'string')
|
|
1093
1233
|
? ctx.askJson.askedAt
|
|
1094
1234
|
: null,
|
|
1235
|
+
startedAt: isRunningNow(ctx, glyph) ? ctx.startedAt : null,
|
|
1095
1236
|
atWhy: (ctx.historyRow || (ctx.askJson && typeof ctx.askJson.askedAt === 'string'))
|
|
1096
1237
|
? null
|
|
1097
|
-
:
|
|
1238
|
+
: isRunningNow(ctx, glyph)
|
|
1239
|
+
? (ctx.startedAt ? null : 'running — start time not recorded (no pid record)')
|
|
1240
|
+
: 'no history row yet (parked or died before completion)',
|
|
1098
1241
|
};
|
|
1099
1242
|
}
|
|
1100
1243
|
|
|
@@ -1333,7 +1476,11 @@ function isOpenStatus(status) {
|
|
|
1333
1476
|
* @returns {{open: boolean, waiting: boolean, resume: any, timeLeftMs: number|null}}
|
|
1334
1477
|
*/
|
|
1335
1478
|
function deriveAskOpenFields(ask, hasHistoryRow, resume = null) {
|
|
1336
|
-
|
|
1479
|
+
// M4c amendment 3: an ask past its deadline reads `expired`, but one whose answer was saved in time
|
|
1480
|
+
// (`resume` is null for a late/unreadable one, `deriveResumeState`) is still open: it can be carried on.
|
|
1481
|
+
const answerSaved = resume != null && resume.askId === ask.askId
|
|
1482
|
+
&& (resume.state === 'starting' || resume.state === 'not-started');
|
|
1483
|
+
const open = (isOpenStatus(ask.status) || (ask.status === 'expired' && answerSaved)) && !hasHistoryRow;
|
|
1337
1484
|
// M4b fix: the ONE decision "is this ask waiting on the human". An open ask
|
|
1338
1485
|
// whose answer is already saved (`resume` = `deriveResumeState` for THIS
|
|
1339
1486
|
// ask, the run's own label) is open but not waiting: the human answered.
|
|
@@ -1385,6 +1532,7 @@ function normalizeArchivedRow(a) {
|
|
|
1385
1532
|
reason: a.answer.reason ?? null,
|
|
1386
1533
|
answeredAt: a.answer.answeredAt ?? null,
|
|
1387
1534
|
archived: true,
|
|
1535
|
+
emits: typeof a.emits === 'string' ? a.emits : null,
|
|
1388
1536
|
evidence: flattenEvidence(a.evidence),
|
|
1389
1537
|
};
|
|
1390
1538
|
}
|
|
@@ -1584,6 +1732,54 @@ export function deriveAskStepInfo(orderedAsks, auditRows, declSteps) {
|
|
|
1584
1732
|
});
|
|
1585
1733
|
}
|
|
1586
1734
|
|
|
1735
|
+
/**
|
|
1736
|
+
* M4c item 5 — the Ask tab's ONE grouping function: one block per signed ask
|
|
1737
|
+
* line. Key = the archive's recorded `emits`, else the step the ask's own "paused" audit row names (`stepName`,
|
|
1738
|
+
* `deriveAskStepInfo`); when the books cannot name a step (count mismatch, a
|
|
1739
|
+
* pre-M4a-1 run) the key falls back to the question text, then to one shared
|
|
1740
|
+
* "unknown" key — never a guessed pairing. Draft numbers are the ask's 1-based
|
|
1741
|
+
* position within its own block (how many times this line has parked).
|
|
1742
|
+
* `current` is the newest draft; `answers` is one row per draft that has a
|
|
1743
|
+
* recorded decision (the newest draft only when it is not itself open),
|
|
1744
|
+
* decision word via `DECISION_STATUS` (old `reject` files already read `redo`).
|
|
1745
|
+
* @param {any[]} asks rows as built by `getRunAsks` (oldest first)
|
|
1746
|
+
* @returns {any[]}
|
|
1747
|
+
*/
|
|
1748
|
+
export function groupAskBlocks(asks) {
|
|
1749
|
+
const decisionWord = (status) => Object.keys(DECISION_STATUS).find((k) => DECISION_STATUS[k] === status) ?? status;
|
|
1750
|
+
const byKey = new Map();
|
|
1751
|
+
for (const ask of asks) {
|
|
1752
|
+
let key = 'unknown';
|
|
1753
|
+
if (typeof ask.emits === 'string') key = `step:${ask.emits}`;
|
|
1754
|
+
else if (typeof ask.stepName === 'string') key = `step:${ask.stepName}`;
|
|
1755
|
+
else if (typeof ask.question === 'string') key = `q:${ask.question}`;
|
|
1756
|
+
if (!byKey.has(key)) byKey.set(key, []);
|
|
1757
|
+
byKey.get(key).push(ask);
|
|
1758
|
+
}
|
|
1759
|
+
return [...byKey.entries()].map(([key, drafts]) => {
|
|
1760
|
+
const current = drafts[drafts.length - 1];
|
|
1761
|
+
const answers = [];
|
|
1762
|
+
drafts.forEach((d, i) => {
|
|
1763
|
+
if (d === current && d.open) return;
|
|
1764
|
+
if (d.status === 'unanswered' || d.status === 'open') return;
|
|
1765
|
+
answers.push({
|
|
1766
|
+
draft: i + 1, askId: d.askId, decision: decisionWord(d.status), reason: d.reason ?? null, answeredAt: d.answeredAt ?? null,
|
|
1767
|
+
});
|
|
1768
|
+
});
|
|
1769
|
+
return {
|
|
1770
|
+
key,
|
|
1771
|
+
stepName: current.emits ?? current.stepName ?? null,
|
|
1772
|
+
stepLine: current.stepLine ?? null,
|
|
1773
|
+
question: current.question ?? null,
|
|
1774
|
+
questionWhy: current.questionWhy ?? null,
|
|
1775
|
+
draftNo: drafts.length,
|
|
1776
|
+
askIds: drafts.map((d) => d.askId),
|
|
1777
|
+
current,
|
|
1778
|
+
answers,
|
|
1779
|
+
};
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1587
1783
|
/**
|
|
1588
1784
|
* `GET /api/runs/:flow/:runId/asks` — the Ask tab (M4a-1 scope item 1):
|
|
1589
1785
|
* every ask this run's books can name, in order, each carrying full
|
|
@@ -1599,7 +1795,7 @@ export function deriveAskStepInfo(orderedAsks, auditRows, declSteps) {
|
|
|
1599
1795
|
* detail carries, `null` for a finished run or nothing to say) so the Ask tab
|
|
1600
1796
|
* can tell "open, answerable" from "answer saved" without a second fetch.
|
|
1601
1797
|
* @param {{root: string, flow: string, runId: string, catalogue: any, resumeAttempt?: (flow: string, runId: string) => any}} opts
|
|
1602
|
-
* @returns {{flow:string, runId:string, asks:any[], resume: any}|null}
|
|
1798
|
+
* @returns {{flow:string, runId:string, asks:any[], resume: any, blocks: any[]}|null}
|
|
1603
1799
|
*/
|
|
1604
1800
|
export function getRunAsks({
|
|
1605
1801
|
root, flow, runId, catalogue, resumeAttempt,
|
|
@@ -1620,10 +1816,19 @@ export function getRunAsks({
|
|
|
1620
1816
|
});
|
|
1621
1817
|
const declSteps = flowRead.ok ? flowRead.declaration.steps : null;
|
|
1622
1818
|
const stepInfo = deriveAskStepInfo(ordered, auditRows, declSteps);
|
|
1623
|
-
|
|
1819
|
+
let resume = hasHistoryRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(run.runDir), attempt: resumeAttempt?.(flow, runId) ?? null, ask: readAsk(run.runDir) });
|
|
1820
|
+
// M4c amendment 2: the Ask tab says what the run list says — stuck, by the one rule (`isStuck`).
|
|
1821
|
+
let runStuck = false;
|
|
1822
|
+
const lockRead = readResumeLock(run.runDir);
|
|
1823
|
+
if (resume && isStuck({ resume, liveness: runLiveness(run.runDir), lock: lockRead.state })) {
|
|
1824
|
+
const label = stuckLabelFor({ resume, lock: lockRead.state });
|
|
1825
|
+
resume = { ...resume, label, ...(label === STUCK_LOCK_LABEL ? { lockPath: lockRead.path } : {}) };
|
|
1826
|
+
runStuck = true;
|
|
1827
|
+
}
|
|
1624
1828
|
const asks = ordered.map((ask, i) => ({
|
|
1625
1829
|
...ask,
|
|
1626
1830
|
...deriveAskOpenFields(ask, hasHistoryRow, resume),
|
|
1831
|
+
stuck: runStuck && resume?.askId === ask.askId,
|
|
1627
1832
|
index: i + 1,
|
|
1628
1833
|
total: ordered.length,
|
|
1629
1834
|
stepName: stepInfo[i].step,
|
|
@@ -1631,7 +1836,7 @@ export function getRunAsks({
|
|
|
1631
1836
|
stepWhy: stepInfo[i].why,
|
|
1632
1837
|
}));
|
|
1633
1838
|
return {
|
|
1634
|
-
flow, runId, asks, resume,
|
|
1839
|
+
flow, runId, asks, resume, blocks: groupAskBlocks(asks),
|
|
1635
1840
|
};
|
|
1636
1841
|
}
|
|
1637
1842
|
|
|
@@ -1662,34 +1867,94 @@ export function listStops({ root, resumeAttempt }) {
|
|
|
1662
1867
|
const run = resolveRunPath(root, flowName, runId);
|
|
1663
1868
|
if (!run.ok) continue;
|
|
1664
1869
|
const hasHistoryRow = historyRunIds.has(runId);
|
|
1665
|
-
const resume = hasHistoryRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(run.runDir), attempt: resumeAttempt?.(flowName, runId) ?? null });
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1870
|
+
const resume = hasHistoryRow ? null : deriveResumeState({ savedAnswer: readSavedAnswer(run.runDir), attempt: resumeAttempt?.(flowName, runId) ?? null, ask: readAsk(run.runDir) });
|
|
1871
|
+
// M4c amendment 2: stuck by the ONE rule (`isStuck`), marked on the ask whose answer is saved.
|
|
1872
|
+
const lockState = readResumeLock(run.runDir).state;
|
|
1873
|
+
const stuckAskId = !hasHistoryRow && isStuck({ resume, liveness: runLiveness(run.runDir), lock: lockState })
|
|
1874
|
+
? (resume?.askId ?? null) : null;
|
|
1875
|
+
const stuckLabel = stuckLabelFor({ resume, lock: lockState });
|
|
1876
|
+
const runAsks = runAsksInOrder(run.runDir, hasHistoryRow).map((ask) => ({
|
|
1877
|
+
flow: flowName,
|
|
1878
|
+
runId,
|
|
1879
|
+
...ask,
|
|
1880
|
+
...deriveAskOpenFields(ask, hasHistoryRow, resume),
|
|
1881
|
+
stuck: stuckAskId !== null && ask.askId === stuckAskId,
|
|
1882
|
+
stuckLabel: stuckAskId !== null && ask.askId === stuckAskId ? stuckLabel : null,
|
|
1883
|
+
}));
|
|
1884
|
+
// M4c item 4: a run with no end row, no ask waiting on the human and no
|
|
1885
|
+
// saved-but-unresumed answer, whose newest pid row is alive, is "working
|
|
1886
|
+
// on your <decision>" — marked on that run's newest answered ask (the
|
|
1887
|
+
// one the live process took). `runLiveness` is the ONE liveness rule.
|
|
1888
|
+
const nothingWaiting = !runAsks.some((r) => r.waiting || r.resume);
|
|
1889
|
+
if (!hasHistoryRow && nothingWaiting && runLiveness(run.runDir) === 'running') {
|
|
1890
|
+
const answered = runAsks.filter((r) => WORKING_WORD[r.status]);
|
|
1891
|
+
const newest = answered.reduce((best, r) => (best === null || sortMs(r.answeredAt) >= sortMs(best.answeredAt) ? r : best), null);
|
|
1892
|
+
if (newest) newest.working = WORKING_WORD[newest.status];
|
|
1673
1893
|
}
|
|
1894
|
+
for (const r of runAsks) rows.push(r);
|
|
1674
1895
|
}
|
|
1675
1896
|
}
|
|
1897
|
+
return orderStops(rows);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
/** ask status -> the answer's own word, for "working on your <word>…". */
|
|
1901
|
+
const WORKING_WORD = { accepted: 'accept', redo: 'redo', reran: 'rerun' };
|
|
1902
|
+
|
|
1903
|
+
/** ms for sorting; NaN when the stamp is absent/unparseable. */
|
|
1904
|
+
function sortMs(iso) {
|
|
1905
|
+
return typeof iso === 'string' ? Date.parse(iso) : NaN;
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
/**
|
|
1909
|
+
* The ONE ordering of the Inbox (M4c item 4, hamr 2026-09-30); the page renders
|
|
1910
|
+
* in the order given and stamps each row's `section`:
|
|
1911
|
+
* 1 waiting on you — least time left first ("runs out first"); a stuck run
|
|
1912
|
+
* (amendment 2: answer saved, nobody carrying it on) after the timed asks;
|
|
1913
|
+
* 2 working on an answer you gave (`working`), or answer saved and resume not
|
|
1914
|
+
* started (`resume`) — newest answer first;
|
|
1915
|
+
* 3 answered and expired asks — newest first (answeredAt, else expiresAt).
|
|
1916
|
+
* A row with no known time sorts last within its section, ties by input order.
|
|
1917
|
+
* @param {any[]} rows
|
|
1918
|
+
* @returns {any[]}
|
|
1919
|
+
*/
|
|
1920
|
+
export function orderStops(rows) {
|
|
1921
|
+
const sectionOf = (r) => ((r.waiting || r.stuck) ? 1 : (r.working || r.resume) ? 2 : 3);
|
|
1922
|
+
const key = (r) => {
|
|
1923
|
+
const t = sectionOf(r) === 3 ? (sortMs(r.answeredAt) || sortMs(r.expiresAt)) : sortMs(r.answeredAt);
|
|
1924
|
+
return Number.isFinite(t) ? t : NaN;
|
|
1925
|
+
};
|
|
1676
1926
|
return rows
|
|
1677
|
-
.map((row, index) => ({ row, index }))
|
|
1927
|
+
.map((row, index) => ({ row: { ...row, section: sectionOf(row) }, index }))
|
|
1678
1928
|
.sort((a, b) => {
|
|
1679
|
-
|
|
1680
|
-
|
|
1929
|
+
const sa = a.row.section;
|
|
1930
|
+
const sb = b.row.section;
|
|
1931
|
+
if (sa !== sb) return sa - sb;
|
|
1932
|
+
if (sa === 1) {
|
|
1933
|
+
if (!!a.row.stuck !== !!b.row.stuck) return a.row.stuck ? 1 : -1;
|
|
1681
1934
|
const at = typeof a.row.timeLeftMs === 'number' ? a.row.timeLeftMs : Infinity;
|
|
1682
1935
|
const bt = typeof b.row.timeLeftMs === 'number' ? b.row.timeLeftMs : Infinity;
|
|
1683
|
-
|
|
1684
|
-
return a.index - b.index;
|
|
1936
|
+
return at !== bt ? at - bt : a.index - b.index;
|
|
1685
1937
|
}
|
|
1686
|
-
const
|
|
1687
|
-
const
|
|
1688
|
-
const aKnown = Number.isFinite(
|
|
1689
|
-
const bKnown = Number.isFinite(
|
|
1690
|
-
if (aKnown && bKnown) return
|
|
1938
|
+
const ak = key(a.row);
|
|
1939
|
+
const bk = key(b.row);
|
|
1940
|
+
const aKnown = Number.isFinite(ak);
|
|
1941
|
+
const bKnown = Number.isFinite(bk);
|
|
1942
|
+
if (aKnown && bKnown && ak !== bk) return bk - ak;
|
|
1691
1943
|
if (aKnown !== bKnown) return aKnown ? -1 : 1;
|
|
1692
1944
|
return a.index - b.index;
|
|
1693
1945
|
})
|
|
1694
1946
|
.map(({ row }) => row);
|
|
1695
1947
|
}
|
|
1948
|
+
|
|
1949
|
+
/**
|
|
1950
|
+
* N for `Inbox (N)` (M4c item 3): asks open and not expired, across every flow
|
|
1951
|
+
* and run, plus stuck runs (amendment 2: the human must act). An answered ask
|
|
1952
|
+
* whose resume is carried on is not `waiting`, so not counted; a crashed one
|
|
1953
|
+
* (answer taken, process gone) is not stuck and not counted;
|
|
1954
|
+
* an expired ask is not `open`. The page only shows this number.
|
|
1955
|
+
* @param {any[]} rows `listStops` rows
|
|
1956
|
+
* @returns {number}
|
|
1957
|
+
*/
|
|
1958
|
+
export function inboxOpenCount(rows) {
|
|
1959
|
+
return rows.filter((r) => r.waiting || r.stuck).length;
|
|
1960
|
+
}
|