fwdloop 0.8.1 → 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.
@@ -1,7 +1,7 @@
1
1
  <!DOCTYPE html>
2
2
  <!-- borrowed-from: bareloop src/panel/index.html@a30bbef — ADAPTED for fwdloop.
3
3
  M4a piece 3+ (docs/wiki/the-module-ladder.md, "M4a — read-only panel —
4
- SIGNED"): the Tokyo Night box-drawing visual design (CSS tokens, tab
4
+ SIGNED"; M4b adds the answer doors): the Tokyo Night box-drawing visual design (CSS tokens, tab
5
5
  layout, glyph/badge styling, responsive 390px breakpoint) is kept from
6
6
  the borrowed page; every data-bearing script (fetches, run rendering,
7
7
  step map, audit, job, run grouping) is rewired for fwdloop's own book
@@ -153,6 +153,26 @@
153
153
  arrow [▶], which this reserves for "running" alone. Its own class so
154
154
  the two states never share a ::before rule. */
155
155
  .dot.expired::before{content:"[!]";color:var(--amber);}
156
+ /* M4c amendment 2: stuck — answer saved, nobody carrying the run on. Its own glyph,
157
+ amber like the other "needs you" signs; it pulses (server-decided, glyphPulses). */
158
+ .dot.stuck::before{content:"[II]";color:var(--amber);}
159
+ /* M4c: only the two "alive" signs pulse — [▶] running and [·] waiting on
160
+ you — keyed to `.pulse`, which the server decides (glyphPulses), never to
161
+ a colour class (.grey is also the placeholder dot). A gentle opacity wave on the existing colors;
162
+ reduced-motion keeps the sign and drops only the animation. */
163
+ @keyframes signPulse{0%,100%{opacity:1;}50%{opacity:.35;}}
164
+ .dot.pulse::before{animation:signPulse 1.6s ease-in-out infinite;}
165
+ @media (prefers-reduced-motion: reduce){
166
+ .dot.pulse::before{animation:none;}
167
+ }
168
+
169
+ /* M4c inbox: section 1 (waiting on you) carries the page's amber accent
170
+ treatment; section 3 (answered/expired) is dimmed. Tokens only. */
171
+ .inbox-h{font-size:12px;color:var(--text-dim);font-weight:700;margin:12px 0 6px;}
172
+ .inbox-h:first-child{margin-top:0;}
173
+ .inbox-row.s1{border-left:4px solid var(--amber);background:var(--amber-bg);}
174
+ .inbox-row.s3{opacity:.6;}
175
+ .inbox-row .wf-line1{min-width:0;}
156
176
 
157
177
  .badge{display:inline-block;font-size:11px;padding:1px 4px;color:var(--text-dim);background:none;font-weight:700;}
158
178
  .badge::before{content:"[";color:var(--text-faint);}
@@ -171,6 +191,20 @@
171
191
  .btn:disabled{color:var(--text-faint);border-color:var(--border);cursor:not-allowed;}
172
192
  .btn:disabled::before,.btn:disabled::after{color:var(--text-faint);opacity:0.4;}
173
193
  .btn.small{padding:0 9px;height:32px;min-height:0;font-size:12px;}
194
+ /* M4b piece 4: the answer doors. Shape/placement from bareloop's mockup Run-tab
195
+ action bar (a row of `.btn`s with a hint line under it), redo is ours. */
196
+ .answer-doors{margin-top:12px;padding-top:8px;border-top:1px solid var(--border);}
197
+ .answer-doors label{display:block;font-size:11px;font-weight:700;color:var(--text-dim);text-transform:uppercase;letter-spacing:0.04em;margin-bottom:3px;}
198
+ .answer-doors textarea{display:block;width:100%;max-width:100%;box-sizing:border-box;min-height:64px;padding:6px 8px;font:inherit;color:var(--text);background:var(--bg);border:1px solid var(--border-strong);resize:vertical;}
199
+ .answer-doors .actions-row{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px;}
200
+ .answer-doors .btn{min-width:44px;}
201
+ .working-line{margin-top:4px;font-size:13px;color:var(--text);overflow-wrap:anywhere;}
202
+ .answer-line{font-size:13px;color:var(--text-dim);margin:2px 0;white-space:pre-wrap;overflow-wrap:anywhere;word-break:break-word;}
203
+ .answer-msg{margin-top:8px;padding:6px 8px;border:1px solid var(--border-strong);font-size:13px;color:var(--text);white-space:pre-wrap;word-break:break-word;overflow-wrap:anywhere;}
204
+ .answer-msg.refused{border-color:var(--red);}
205
+ .answer-msg.refused::before{content:"refused: ";color:var(--red);font-weight:700;}
206
+ .answer-msg.stuck{border-color:var(--amber);}
207
+ .answer-msg.info::before{content:"[·] ";color:var(--amber);font-weight:700;}
174
208
 
175
209
  .wf-row,.hist-row{
176
210
  display:flex;flex-direction:column;row-gap:4px;min-width:0;
@@ -330,8 +364,8 @@
330
364
  ("resume-text"/"jd-text" etc.), never the faint `.hint` style. */
331
365
  .evidence-heading{font-size:11px;font-weight:700;color:var(--text-dim);margin:8px 0 3px;text-transform:uppercase;letter-spacing:0.04em;}
332
366
  .evidence-heading:first-child{margin-top:0;}
333
- /* review #3: the ask's own question, a real heading under the "Ask i of
334
- n · step · asked · status" meta line — reuses the box-drawing h4 look
367
+ /* review #3: the ask's own question, a real heading under the "step ·
368
+ draft N · asked · status" meta line — reuses the box-drawing h4 look
335
369
  the Audit/Job group headers already use, but inline (no absolute
336
370
  ┤ ├ pips — those are reserved for a whole-box title, not an in-flow
337
371
  line) so it reads as a clear, bold heading without a second border. */
@@ -413,29 +447,30 @@
413
447
  <div class="tabrow" role="tablist" aria-label="Left panel views">
414
448
  <button role="tab" id="tab-chat" aria-controls="panel-chat" aria-selected="true" data-testid="left-tab-chat">Chat</button>
415
449
  <button role="tab" id="tab-runs" aria-controls="panel-runs" aria-selected="false" data-testid="left-tab-runs">Runs</button>
416
- <button role="tab" id="tab-inbox" aria-controls="panel-inbox" aria-selected="false" data-testid="left-tab-inbox">Inbox<span id="inbox-count-label"></span></button>
450
+ <button role="tab" id="tab-inbox" aria-controls="panel-inbox" aria-selected="false" data-testid="left-tab-inbox">Inbox<span id="inbox-count-label"> (0)</span></button>
417
451
  </div>
418
452
 
419
- <div class="pane-body">
453
+ <div class="pane-body" id="left-pane-body">
420
454
 
421
455
  <section id="panel-chat" class="tabpanel active" role="tabpanel" aria-labelledby="tab-chat">
422
456
  <div class="chat-thread">
423
457
  Authoring a flow from this page isn't built yet — the drafter is a POC. This
424
- panel is read-only: it shows flows and runs already started from the
425
- <code>fwdloop</code> CLI. Use the Runs tab to open one, or the Inbox tab for
426
- anything waiting on you.
458
+ panel shows flows and runs already started from the
459
+ <code>fwdloop</code> CLI, and lets you answer an ask that is waiting on you
460
+ (accept, redo or rerun, with a reason). It cannot start a run. Use the Runs
461
+ tab to open one, or the Inbox tab for anything waiting on you.
427
462
  </div>
428
463
  </section>
429
464
 
430
465
  <section id="panel-inbox" class="tabpanel" role="tabpanel" aria-labelledby="tab-inbox" hidden>
431
466
  <!-- M4a-1: a list of STOPS, like History — open ones first with
432
467
  [.] and time left, then past ones newest first with a short
433
- status word ([accepted]/[rejected: reason]/[reran: reason]/
468
+ status word ([accepted]/[redo: reason]/[reran: reason]/
434
469
  [expired]/[unanswered]). No evidence is rendered here any
435
470
  more (M4a-1 review): clicking a stop selects its run, switches
436
471
  the right pane to the Ask tab, and expands that ONE ask there
437
- — the wide right pane is where the evidence lives now. No
438
- answer controls — those are M4b. -->
472
+ — the wide right pane is where the evidence lives now. The
473
+ answer doors are in the Ask tab, not here (M4b). -->
439
474
  <div id="inbox-list" data-testid="inbox-list"></div>
440
475
  </section>
441
476
 
@@ -470,7 +505,7 @@
470
505
  <button role="tab" id="tab-ask" aria-controls="panel-ask" aria-selected="false" data-testid="right-tab-ask">Ask</button>
471
506
  </div>
472
507
 
473
- <div class="pane-body">
508
+ <div class="pane-body" id="right-pane-body">
474
509
 
475
510
  <section id="panel-run" class="tabpanel active" role="tabpanel" aria-labelledby="tab-run">
476
511
  <div id="run-empty" class="hint" style="font-size:13px;padding:24px 0;">select a run from Workflows or History</div>
@@ -560,13 +595,12 @@
560
595
  (past), the draft under review (scrollable), then each
561
596
  unjudged input labelled by step (goal as subtitle). A
562
597
  pre-M4a-1 ask shows "draft not kept (before M4a-1)" in place
563
- of the draft. Read-only: no answer controls here (M4b) — a
564
- clearly marked EMPTY slot stands in their place. Nothing in
565
- this panel is a real <button>/<input> — even the
566
- collapse/expand toggle is a div with role="button"
567
- tabindex="0" (the SAME pattern the step-map's own SVG nodes
568
- already use elsewhere on this page), so "no controls yet"
569
- holds by construction. -->
598
+ of the draft. M4b: an OPEN ask also carries the answer doors
599
+ (a labelled reason textarea + Accept / Redo / Rerun
600
+ buttons), or a stated reason there are none; a saved answer
601
+ whose resume did not start carries ONE "Try the resume
602
+ again" button. The collapse/expand toggle stays a div with
603
+ role="button" tabindex="0". -->
570
604
  <div id="ask-empty" class="hint" style="font-size:13px;padding:24px 0;">select a run first</div>
571
605
  <div id="ask-list" data-testid="ask-list" hidden></div>
572
606
  </section>
@@ -583,6 +617,9 @@
583
617
  "use strict";
584
618
 
585
619
  var PORT = "__FWDLOOP_PANEL_PORT__";
620
+ // M4b piece 1: per-process answer token, replaced at serve time; sent only as
621
+ // the x-fwdloop-token header of the two POSTs (postJSON below).
622
+ var TOKEN = "__FWDLOOP_PANEL_TOKEN__";
586
623
  document.getElementById("host-line").textContent = "127.0.0.1:" + PORT;
587
624
 
588
625
  function wireTabs(tabButtons){
@@ -603,15 +640,32 @@
603
640
  wireTabs([document.getElementById("tab-chat"), document.getElementById("tab-runs"), document.getElementById("tab-inbox")]);
604
641
  wireTabs([document.getElementById("tab-run"), document.getElementById("tab-audit"), document.getElementById("tab-details"), document.getElementById("tab-ask")]);
605
642
 
606
- // ---- fetch helpers — GET only, this panel is read-only by construction
607
- // (src/panel/server.js is GET/HEAD-only too; nothing on this page ever
608
- // sends a non-GET request or a request body). ----
643
+ // ---- fetch helpers. Reads are GET. The ONLY non-GET requests on this page are
644
+ // the two POSTs below (M4b): /api/answer (a human clicking a door) and
645
+ // /api/resume ("try the resume again"). The token goes in the request header
646
+ // and nowhere else — never the DOM, a URL, storage or a log. Same-origin. ----
609
647
  function getJSON(path){
610
648
  return fetch(path, { method: "GET", headers: { accept: "application/json" } }).then(function(r){
611
649
  if(!r.ok) throw new Error(path + ": HTTP " + r.status);
612
650
  return r.json();
613
651
  });
614
652
  }
653
+ // Resolves { status, body } for ANY HTTP status (a refusal is data to show by
654
+ // name, never an exception); rejects only on a network failure.
655
+ function postJSON(path, payload){
656
+ return fetch(path, {
657
+ method: "POST",
658
+ headers: { "content-type": "application/json", accept: "application/json", "x-fwdloop-token": TOKEN },
659
+ body: JSON.stringify(payload),
660
+ credentials: "same-origin"
661
+ }).then(function(r){
662
+ return r.text().then(function(t){
663
+ var body = null;
664
+ try{ body = JSON.parse(t); }catch(e){}
665
+ return { status: r.status, body: body, text: t };
666
+ });
667
+ });
668
+ }
615
669
 
616
670
  function escapeXml(s){
617
671
  return String(s).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");
@@ -737,17 +791,17 @@
737
791
  // rendering the raw words green/red/softgreen anywhere (coordinator's
738
792
  // browser-walk fix #2). `verdictWord`/`verdictGlyph` are the ONE place
739
793
  // that turns a verdict (plus the step's own close class, for telling a
740
- // human's reject apart from a mechanical red) into plain words and a
794
+ // human's redo apart from a mechanical red) into plain words and a
741
795
  // pass/fail glyph — used by the step cards, the map's inline attempt
742
796
  // text, and the flat Audit table alike, so they can never say something
743
797
  // different for the same row.
744
798
  // - `paused`/`refused`/an ask timeout/expiry are NOT failures (fix #3) —
745
799
  // no ✗, a plain word instead.
746
- // - a `red` verdict on a `hitl`-classed step is the human's own reject
747
- // (src/runner.js's redo path), not a mechanical stop — "rejected by
800
+ // - a `red` verdict on a `hitl`-classed step is the human's own redo
801
+ // (src/runner.js's redo path), not a mechanical stop — "redo by
748
802
  // you: <reason>", never "verdict red" / a fail glyph (fix #3).
749
803
  function verdictWord(verdict, closeClass, gap){
750
- if(closeClass === "hitl" && verdict === "red") return "rejected by you" + (gap ? ": " + gap : "");
804
+ if(closeClass === "hitl" && verdict === "red") return "redo by you" + (gap ? ": " + gap : "");
751
805
  if(verdict === "paused") return "paused for you";
752
806
  if(verdict === "refused") return "refused — no reason given";
753
807
  if(verdict === "ask-timeout" || verdict === "ask-expired") return "expired waiting for you";
@@ -759,7 +813,7 @@
759
813
  }
760
814
  function verdictPassed(verdict){ return verdict === "green" || verdict === "hitl"; }
761
815
  // `null` — no glyph at all — for a verdict that is neither a pass nor a
762
- // fail (paused/refused/expired/a human reject); a bare attempt glyph
816
+ // fail (paused/refused/expired/a human redo); a bare attempt glyph
763
817
  // would misread a pause as a failure (fix #3).
764
818
  function verdictGlyph(verdict, closeClass){
765
819
  if(closeClass === "hitl" && verdict === "red") return null;
@@ -767,8 +821,11 @@
767
821
  return verdictPassed(verdict) ? "✓" : "✗";
768
822
  }
769
823
  // The run-row glyph strings src/panel/data.js's computeGlyph emits are
770
- // already bracketed ("[✓]", "[✗]", "[·]", "[?]") — this only ever picks a
824
+ // already bracketed ("[✓]", "[✗]", "[·]", "[▶]", "[?]") — this only ever picks a
771
825
  // CSS color class off them, never re-derives the glyph itself.
826
+ // the dot's class list: colour from the glyph, `pulse` only when the server
827
+ // says this sign is [▶] running or [·] waiting on you (data.js glyphPulses).
828
+ function dotClass(g, pulse){ return "dot " + glyphClass(g) + (pulse ? " pulse" : ""); }
772
829
  function glyphClass(g){
773
830
  if(g === "[✓]") return "green";
774
831
  if(g === "[✗]") return "red";
@@ -781,6 +838,7 @@
781
838
  // "running", but a different glyph, so it gets its own dot class
782
839
  // (never "amber", which draws the reserved running arrow [▶]).
783
840
  if(g === "[!]") return "expired";
841
+ if(g === "[II]") return "stuck";
784
842
  return "grey"; // "[·]" — waiting on a human, or answered-not-resumed
785
843
  }
786
844
 
@@ -803,7 +861,7 @@
803
861
  var ROW_GAP = 70;
804
862
  // hamr review #2: "map cards missing try 2 if multiple attempts/iteration"
805
863
  // — a plain "try N" count whenever a step has been attempted more than
806
- // once, plus a mark when the LATEST attempt followed a human reject (the
864
+ // once, plus a mark when the LATEST attempt followed a human redo (the
807
865
  // same `afterReject` boundary src/panel/data.js already worked out —
808
866
  // never re-derived here). One attempt (the common case) shows no "try"
809
867
  // text at all, matching bareloop's own "only say it when it's true" shape.
@@ -816,7 +874,7 @@
816
874
  if(!tryCount || tryCount <= 1) return "";
817
875
  var last = attempts && attempts.length ? attempts[attempts.length - 1] : null;
818
876
  var txt = "try " + tryCount;
819
- if(last && last.afterReject) txt += " (re-attempt after reject)";
877
+ if(last && last.afterReject) txt += " (re-attempt after redo)";
820
878
  return txt;
821
879
  }
822
880
  // hamr review (browser re-walk #1): the per-attempt list ("attempt 1
@@ -1105,18 +1163,35 @@
1105
1163
  return row;
1106
1164
  }
1107
1165
  row.innerHTML =
1108
- '<span class="wf-line1"><span class="dot ' + glyphClass(r.glyph) + '"></span>' +
1166
+ '<span class="wf-line1"><span class="' + dotClass(r.glyph, r.pulse) + '"></span>' +
1109
1167
  '<span class="wf-name" title="' + escapeXml(fullName) + '">' + escapeXml(fullName) + '</span></span>' +
1110
1168
  '<span class="wf-meta-line">' +
1111
1169
  '<span class="wf-meta">' + escapeXml(r.label || "unknown") + '</span>' +
1112
- '<span class="wf-meta">' + escapeXml(r.spend || r.spendWhy || "unknown") + '</span>' +
1113
- '<span class="wf-meta">' + escapeXml(r.at ? r.at.slice(0,10) : (r.askedAt ? "asked " + readableDateTime(r.askedAt) : (r.atWhy || "unknown"))) + '</span>' +
1170
+ '<span class="wf-meta">' + escapeXml(runSpendText(r)) + '</span>' +
1171
+ '<span class="wf-meta">' + escapeXml(runAtText(r)) + '</span>' +
1114
1172
  '</span>';
1115
- row.addEventListener("click", function(){ selectRun(r.flow, r.runId, row, ".hist-row"); document.getElementById("tab-run").click(); scrollRunIntoViewMobile(); });
1173
+ row.addEventListener("click", function(){ openRunFromRuns(r, row); });
1116
1174
  row.addEventListener("keydown", function(e){ if(e.key === "Enter" || e.key === " "){ e.preventDefault(); row.click(); } });
1117
1175
  return row;
1118
1176
  }
1119
1177
 
1178
+ // M4c amendment 1 (a): the ONE place a click on a run in Runs decides where it lands.
1179
+ // Right side: that run, on the Run tab. A run with an ask waiting on the human also
1180
+ // moves the left side to the Inbox with that ask's row selected; any other run leaves
1181
+ // the left side on Runs.
1182
+ function openRunFromRuns(r, rowEl){
1183
+ if(r.waiting){
1184
+ var inboxId = "inbox-row-" + r.flow + "-" + r.runId + "-" + r.waitingAskId;
1185
+ var inboxRow = Array.prototype.find.call(document.querySelectorAll(".inbox-row"), function(el){ return el.getAttribute("data-testid") === inboxId; }) || null;
1186
+ selectRun(r.flow, r.runId, inboxRow, ".inbox-row", r.waitingAskId);
1187
+ document.getElementById("tab-inbox").click();
1188
+ } else {
1189
+ selectRun(r.flow, r.runId, rowEl, ".hist-row");
1190
+ }
1191
+ document.getElementById("tab-run").click();
1192
+ scrollRunIntoViewMobile();
1193
+ }
1194
+
1120
1195
  // ---- History view: every row exactly as /api/runs returned it. ----
1121
1196
  function renderHistory(runs){
1122
1197
  var el = document.getElementById("history-list");
@@ -1131,6 +1206,14 @@
1131
1206
  // ---- Workflows view: groups the ONE /api/runs payload by flow name
1132
1207
  // client-side. Runs arrive newest-first within a flow (server contract,
1133
1208
  // listRuns), so the first run seen for a flow is its latest.
1209
+ // The words for a run row's spend and time cells — the Workflows card (a flow's latest run) and
1210
+ // every History/expanded row say the same thing, from the same server fields.
1211
+ function runSpendText(r){
1212
+ return r.spend || r.spendWhy || "unknown";
1213
+ }
1214
+ function runAtText(r){
1215
+ return r.at ? r.at.slice(0,10) : (r.askedAt ? "asked " + readableDateTime(r.askedAt) : (r.startedAt ? "started " + readableDateTime(r.startedAt) : (r.atWhy || "unknown")));
1216
+ }
1134
1217
  function groupRunsByFlow(runs){
1135
1218
  var byFlow = {};
1136
1219
  var order = [];
@@ -1146,10 +1229,8 @@
1146
1229
  runs: flowRuns,
1147
1230
  runCount: last.runId === null ? 0 : flowRuns.length,
1148
1231
  lastRunId: last.runId,
1149
- lastAt: last.at,
1150
1232
  lastGlyph: last.glyph,
1151
1233
  lastLabel: last.label,
1152
- lastSpend: last.spend,
1153
1234
  lastRow: last,
1154
1235
  };
1155
1236
  });
@@ -1253,21 +1334,19 @@
1253
1334
  row.setAttribute("aria-pressed", isSelected ? "true" : "false");
1254
1335
  var wfFullName = g.flow + ' (' + g.lastRunId + ')';
1255
1336
  row.innerHTML =
1256
- '<span class="wf-line1"><span class="wf-caret">' + (hasOtherRuns ? (expanded ? "▼" : "▶") : "") + '</span><span class="dot ' + glyphClass(g.lastGlyph) + '"></span>' +
1337
+ '<span class="wf-line1"><span class="wf-caret">' + (hasOtherRuns ? (expanded ? "▼" : "▶") : "") + '</span><span class="' + dotClass(g.lastGlyph, g.lastRow.pulse) + '"></span>' +
1257
1338
  '<span class="wf-name" title="' + escapeXml(wfFullName) + '">' + escapeXml(wfFullName) + '</span></span>' +
1258
1339
  '<span class="wf-meta-line">' +
1259
1340
  '<span class="wf-meta">' + escapeXml(g.lastLabel || "unknown") + '</span>' +
1260
- '<span class="wf-meta">' + escapeXml(g.lastSpend || "unknown") + '</span>' +
1261
- '<span class="wf-meta">' + escapeXml(g.lastAt ? g.lastAt.slice(0,10) : "unknown") + '</span>' +
1341
+ '<span class="wf-meta">' + escapeXml(runSpendText(g.lastRow)) + '</span>' +
1342
+ '<span class="wf-meta">' + escapeXml(runAtText(g.lastRow)) + '</span>' +
1262
1343
  '<span class="wf-meta">' + g.runCount + ' run' + (g.runCount === 1 ? '' : 's') + '</span>' +
1263
1344
  '</span>';
1264
1345
  row.addEventListener("click", function(){
1265
1346
  // a single-run flow has nothing to expand — the click still opens
1266
1347
  // the run, it just never toggles `wfExpanded` for it.
1267
1348
  if(hasOtherRuns) wfExpanded[g.flow] = !expanded;
1268
- selectRun(g.flow, targetRunId, null, ".hist-row");
1269
- document.getElementById("tab-run").click();
1270
- scrollRunIntoViewMobile();
1349
+ openRunFromRuns(g.runs.filter(function(x){ return x.runId === targetRunId; })[0] || g.lastRow, null);
1271
1350
  renderWorkflows(flows);
1272
1351
  });
1273
1352
  row.addEventListener("keydown", function(e){ if(e.key === "Enter" || e.key === " "){ e.preventDefault(); row.click(); } });
@@ -1288,9 +1367,8 @@
1288
1367
  // the Workflows tab (filters by each flow's OWN latest-run fields).
1289
1368
  // bareloop's checkType (deterministic/rubric) chips are dropped — fwdloop
1290
1369
  // has no such concept; the result chips use fwdloop's own glyph
1291
- // vocabulary ([✓]/[✗]/[·]/[?] — `[▶]` never appears from listRuns today,
1292
- // see docs/wiki/the-module-ladder.md's M4a open POC question, so no chip
1293
- // for a state the books can't produce).
1370
+ // vocabulary ([✓]/[✗]/[·]/[▶]/[?] — `[▶]` running is known from M4c's pid
1371
+ // rows, src/liveness.js).
1294
1372
  function filterBarHTML(scope){
1295
1373
  return (
1296
1374
  '<div class="filterbar" data-scope="' + scope + '" data-testid="' + scope + '-filters">' +
@@ -1298,9 +1376,10 @@
1298
1376
  'id="' + scope + '-filter-search" data-testid="' + scope + '-filter-search" autocomplete="off">' +
1299
1377
  '<div class="chips" role="group" aria-label="Filter">' +
1300
1378
  '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[✓]" title="passed" data-testid="chip-' + scope + '-res-pass">✓</button>' +
1301
- '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[✗]" title="failed" data-testid="chip-' + scope + '-res-fail">✗</button>' +
1379
+ '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[✗]" title="stopped" data-testid="chip-' + scope + '-res-fail">✗</button>' +
1302
1380
  '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[·]" title="waiting on a human" data-testid="chip-' + scope + '-res-waiting">&middot;</button>' +
1303
- '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[?]" title="running or died: unknown" data-testid="chip-' + scope + '-res-unknown">?</button>' +
1381
+ '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[▶]" title="running" data-testid="chip-' + scope + '-res-running">▶</button>' +
1382
+ '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="result" data-filter-value="[?]" title="died or unknown" data-testid="chip-' + scope + '-res-unknown">?</button>' +
1304
1383
  '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="time" data-filter-value="7d" title="last 7 days" data-testid="chip-' + scope + '-time-7d">7d</button>' +
1305
1384
  '<button class="chip fchip" type="button" aria-pressed="false" data-filter-group="time" data-filter-value="30d" title="last 30 days" data-testid="chip-' + scope + '-time-30d">30d</button>' +
1306
1385
  '<button class="chip fchip" type="button" aria-pressed="true" data-filter-group="time" data-filter-value="all" title="all time" data-testid="chip-' + scope + '-time-all">all</button>' +
@@ -1523,14 +1602,14 @@
1523
1602
  var date = document.getElementById("active-wf-date");
1524
1603
 
1525
1604
  name.textContent = detail.flow + " (" + detail.runId + ")";
1526
- dot.className = "dot " + glyphClass(detail.glyph);
1605
+ dot.className = dotClass(detail.glyph, detail.pulse);
1527
1606
  verdict.textContent = detail.label || "unknown";
1528
1607
  // browser re-walk bug #4: this is the DATE field — it must use its own
1529
1608
  // why (`detail.atWhy`), never `detail.outcomeWhy` (a different book
1530
1609
  // fact borrowed by mistake). A parked run's ask.json `askedAt` is a
1531
1610
  // real timestamp the books hold, shown ahead of any generic why text.
1532
1611
  date.textContent = detail.at ? readableDateTime(detail.at)
1533
- : (detail.askedAt ? "asked " + readableDateTime(detail.askedAt) : (detail.atWhy || "unknown"));
1612
+ : (detail.askedAt ? "asked " + readableDateTime(detail.askedAt) : (detail.startedAt ? "started " + readableDateTime(detail.startedAt) : (detail.atWhy || "unknown")));
1534
1613
 
1535
1614
  document.getElementById("run-empty").hidden = true;
1536
1615
  document.getElementById("run-content").hidden = false;
@@ -1539,13 +1618,13 @@
1539
1618
  // wallMs (never derived/guessed) — a died/parked run or one whose
1540
1619
  // history row predates the field both say why instead (fix #5).
1541
1620
  var capText = typeof detail.capUsd === "number" ? ("$" + detail.capUsd.toFixed(2)) : "unknown";
1542
- var spendText = detail.spend || "unknown — no spend recorded yet";
1621
+ var spendText = detail.spend || (detail.glyph === "[▶]" ? "no priced spend yet" : "unknown — no spend recorded yet");
1543
1622
  var wallText = typeof detail.wallMs === "number" ? ("took " + duration(detail.wallMs)) : (detail.wallMsWhy || "wall time unknown");
1544
1623
  document.getElementById("run-counters").innerHTML =
1545
1624
  escapeXml(spendText) + " of " + escapeXml(capText) + " cap &middot; " + escapeXml(wallText);
1546
1625
 
1547
1626
  var box = document.getElementById("run-summary");
1548
- box.className = "summary-box " + (detail.glyph === "[✗]" ? "red" : (detail.glyph === "[·]" ? "amber" : (detail.glyph === "[?]" ? "magenta" : "")));
1627
+ box.className = "summary-box " + (detail.glyph === "[✗]" ? "red" : ((detail.glyph === "[·]" || detail.glyph === "[▶]" || detail.glyph === "[II]") ? "amber" : (detail.glyph === "[?]" ? "magenta" : "")));
1549
1628
  var stepBoxes = buildStepBoxes(detail.steps);
1550
1629
  var stepsDone = stepBoxes.filter(function(s){ return s.state === "done"; }).length;
1551
1630
  var stepsText = stepBoxes.length === 0
@@ -1561,7 +1640,7 @@
1561
1640
  var modelText = detail.model || (detail.modelWhy || "no model call recorded yet");
1562
1641
  var closeText = closeClassSummaryText(detail.steps) || (detail.stepsWhy || "no steps recorded");
1563
1642
  box.innerHTML =
1564
- '<div class="summary-headline"><span class="dot ' + glyphClass(detail.glyph) + '"></span><b>' + escapeXml(detail.label || "unknown") + '</b> &middot; <span class="num">' + escapeXml(spendText) + '</span></div>' +
1643
+ '<div class="summary-headline"><span class="' + dotClass(detail.glyph, detail.pulse) + '"></span><b>' + escapeXml(detail.label || "unknown") + '</b> &middot; <span class="num">' + escapeXml(spendText) + '</span></div>' +
1565
1644
  '<div class="summary-row" data-testid="summary-model"><span class="sum-label">model:</span><span class="sum-val">' + escapeXml(modelText) + '</span></div>' +
1566
1645
  '<div class="summary-row"><span class="sum-label">steps:</span><span class="sum-val">' + stepsText + '</span></div>' +
1567
1646
  '<div class="summary-row" data-testid="summary-why"><span class="sum-label">why:</span><span class="sum-val">' + escapeXml(detail.stopReason || detail.stopReasonWhy || "not recorded") + '</span></div>' +
@@ -2043,6 +2122,39 @@
2043
2122
  // ---- selection + loading ----
2044
2123
  var currentFlow = null;
2045
2124
  var currentRunId = null;
2125
+ // The ask the last click named (null = none): an Inbox row is "selected" only when its own
2126
+ // flow, run AND ask match, so two stops of one run never both light up.
2127
+ var currentAskId = null;
2128
+ // borrowed-from: bareloop src/panel/index.html@0a45f40 (sig / withScrollPreserved), adjusted to fwdloop's shape
2129
+ // One last-payload signature per render. Equal payload = the DOM is not touched. `selectRun` resets
2130
+ // the ones a different selection invalidates.
2131
+ var lastSig = { inbox: null, runs: null, detail: null, asks: null, audit: null };
2132
+ function sig(obj){
2133
+ try { return JSON.stringify(obj); } catch(e){ return null; }
2134
+ }
2135
+ // Saves/restores scrollTop of the given element ids around a re-render.
2136
+ function withScrollPreserved(ids, fn){
2137
+ var saved = ids.map(function(id){
2138
+ var el = document.getElementById(id);
2139
+ return el ? el.scrollTop : null;
2140
+ });
2141
+ fn();
2142
+ ids.forEach(function(id, i){
2143
+ if(saved[i] === null) return;
2144
+ var el = document.getElementById(id);
2145
+ if(el) el.scrollTop = saved[i];
2146
+ });
2147
+ }
2148
+ // If keyboard focus sits on a row inside `listEl`, put it back on the rebuilt row with the same
2149
+ // data-testid. Lookup is by attribute equality (never a selector built from ids).
2150
+ function withFocusPreserved(listEl, fn){
2151
+ var a = document.activeElement;
2152
+ var id = (a && a !== listEl && listEl.contains && listEl.contains(a) && a.getAttribute) ? a.getAttribute("data-testid") : null;
2153
+ fn();
2154
+ if(!id) return;
2155
+ var again = Array.prototype.find.call(listEl.querySelectorAll("[data-testid]"), function(el){ return el.getAttribute("data-testid") === id; });
2156
+ if(again && again.focus) again.focus();
2157
+ }
2046
2158
  // `focusAskId` (M4a-1): when a click on an Inbox stop calls `selectRun`,
2047
2159
  // it names the ONE ask that stop is about — `renderAsk` expands exactly
2048
2160
  // that ask and scrolls it into view, rather than whatever this run's
@@ -2053,6 +2165,10 @@
2053
2165
  if(rowEl){ rowEl.classList.add("selected"); rowEl.setAttribute("aria-pressed","true"); }
2054
2166
  currentFlow = flow;
2055
2167
  currentRunId = runId;
2168
+ currentAskId = focusAskId || null;
2169
+ openRunLive = true; // unknown until this run's own detail says it is finished
2170
+ // a different selection: every payload-equality skip for the selection's own panes is void
2171
+ lastSig.runs = lastSig.detail = lastSig.asks = lastSig.audit = null;
2056
2172
  var base = "/api/runs/" + encodeURIComponent(flow) + "/" + encodeURIComponent(runId);
2057
2173
  // detail, audit, job, and asks each load independently — the Run tab's
2058
2174
  // step cards get everything they need (including a step's own attempt-
@@ -2060,7 +2176,7 @@
2060
2176
  // src/panel/data.js works out the boundary server-side, off the run's
2061
2177
  // full audit-row order, so the client no longer needs a second,
2062
2178
  // separately-fetched copy of the audit rows to cross-reference (fix #6).
2063
- getJSON(base).then(renderRun).catch(function(e){
2179
+ getJSON(base).then(paintRun).catch(function(e){
2064
2180
  document.getElementById("run-empty").hidden = false;
2065
2181
  document.getElementById("run-empty").textContent = "failed to load run: " + e.message;
2066
2182
  document.getElementById("run-content").hidden = true;
@@ -2072,6 +2188,142 @@
2072
2188
  });
2073
2189
  }
2074
2190
 
2191
+ // ---- The ONE refresh loop (M4b piece 4, widened by the M4c exit walk). The page used to load
2192
+ // once, so a run that parked after load kept `[▶]`, Inbox stayed (0) and the run header kept its
2193
+ // first words. Now `pageTick` runs every LIVE_POLL_MS for the whole life of the page: it re-reads
2194
+ // the Runs list, History/Workflows, the Inbox list + (N), and the selected run's detail, asks and
2195
+ // audit — all through the SAME render functions, so the same guards hold (`holdAskRender` never
2196
+ // redraws over a typed reason; a refusal lives in `answerMsg` and is repainted). `startLive` no
2197
+ // longer owns a timer: it only names the answer being watched, and the tick judges it from the
2198
+ // books it just read (`liveOutcome`), up to LIVE_MAX_MS. `reloadRun` is also called straight
2199
+ // after an answer POST. Success is only ever said from what this reads. ----
2200
+ var LIVE_POLL_MS = 2000;
2201
+ // borrowed-from: bareloop src/panel/index.html@0a45f40 (RUNS_LIST_POLL_MS throttle inside the one tick), adjusted:
2202
+ // the open run's own panes keep the 2 s tick (only while that run is live); the Inbox and Runs lists
2203
+ // refresh at most every LISTS_POLL_MS. ONE timer: the throttle lives in `pageTick`, never a second timer.
2204
+ var LISTS_POLL_MS = 10000;
2205
+ var LIVE_MAX_MS = 5 * 60 * 1000;
2206
+ var live = null; // { flow, runId, askId, until } — the answer being watched, if any
2207
+ var pollTimer = null;
2208
+ var lastListsAt = 0; // when the lists were last asked for (any caller)
2209
+ var openRunLive = true; // is the open run still changing? (true until its detail says otherwise)
2210
+ var wasHidden = false;
2211
+ // One writer of `openRunLive`: every paint of the open run's detail goes through here.
2212
+ function paintRun(d){
2213
+ openRunLive = !(d && (d.glyph === "[\u2713]" || d.glyph === "[\u2717]"));
2214
+ var next = sig(d);
2215
+ if(next !== null && next === lastSig.detail) return;
2216
+ lastSig.detail = next;
2217
+ withScrollPreserved(["right-pane-body"], function(){ renderRun(d); });
2218
+ }
2219
+ // Immediate (every user action that re-reads calls this); the tick uses `listsDue` to throttle.
2220
+ function reloadLists(){
2221
+ lastListsAt = Date.now();
2222
+ getJSON("/api/inbox").then(function(r){ renderInbox(r.rows || [], r.openCount); }).catch(function(){});
2223
+ getJSON("/api/runs").then(function(r){
2224
+ var rows = r.rows || [];
2225
+ var next = sig(rows);
2226
+ if(next !== null && next === lastSig.runs) return;
2227
+ lastSig.runs = next;
2228
+ withScrollPreserved(["left-pane-body"], function(){ runsFilterBar.setItems(rows); runsFilterBar.refresh(); });
2229
+ }).catch(function(){});
2230
+ }
2231
+ function reloadRun(flow, runId, skipLists){
2232
+ var base = "/api/runs/" + encodeURIComponent(flow) + "/" + encodeURIComponent(runId);
2233
+ var still = function(){ return currentFlow === flow && currentRunId === runId; };
2234
+ var detailP = getJSON(base).then(function(d){ if(still()) paintRun(d); return d; }).catch(function(){ return null; });
2235
+ var asksP = getJSON(base + "/asks").then(function(a){
2236
+ if(still()){
2237
+ // the Ask tab is drawn from the books AND the page's own pending answer ("working…" block)
2238
+ var next = sig({ asks: a, pending: pendingAnswer });
2239
+ // the signature is stamped only when renderAsk REALLY drew (it returns false when held over a typed
2240
+ // reason): a held redraw leaves the stamp unset, so the next tick retries it against the same payload
2241
+ if(next === null || next !== lastSig.asks){ lastSig.asks = renderAsk(a, null) ? next : null; }
2242
+ }
2243
+ return a;
2244
+ }).catch(function(e){ if(still()){ lastSig.asks = null; renderAsk(null, null, e.message); } return null; });
2245
+ getJSON(base + "/audit").then(function(a){
2246
+ if(!still()) return;
2247
+ var next = sig(a);
2248
+ if(next !== null && next === lastSig.audit) return;
2249
+ lastSig.audit = next;
2250
+ renderAudit(a);
2251
+ }).catch(function(){});
2252
+ if(!skipLists) reloadLists();
2253
+ return Promise.all([detailP, asksP]);
2254
+ }
2255
+ function schedulePoll(ms){ clearTimeout(pollTimer); pollTimer = setTimeout(pageTick, ms); }
2256
+ function pageTick(){
2257
+ pollTimer = null;
2258
+ if(document.hidden){ wasHidden = true; schedulePoll(LIVE_POLL_MS); return; }
2259
+ var catchUp = wasHidden; // first tick after the tab came back: force both
2260
+ wasHidden = false;
2261
+ var watch = live;
2262
+ var sel = currentFlow !== null && currentRunId !== null;
2263
+ var watchIsSelected = watch && sel && watch.flow === currentFlow && watch.runId === currentRunId;
2264
+ var listsDue = catchUp || (Date.now() - lastListsAt) >= LISTS_POLL_MS;
2265
+ var wasLive = openRunLive;
2266
+ var selP;
2267
+ // the open run's own panes: every tick, but only while it is live (or being watched), or on catch-up
2268
+ if(sel && (openRunLive || watchIsSelected || catchUp)) selP = reloadRun(currentFlow, currentRunId, true);
2269
+ else selP = Promise.resolve([null, null]);
2270
+ if(listsDue) reloadLists();
2271
+ // the watched run, when it is not the one on screen: read it for its outcome only (no paint — `still()` is false)
2272
+ var watchP = (watch && !watchIsSelected) ? reloadRun(watch.flow, watch.runId, true) : selP;
2273
+ watchP.then(function(res){
2274
+ if(watch && live === watch) watchStep(watch, res);
2275
+ }).catch(function(){}).then(function(){
2276
+ // the open run just stopped being live: its list glyph must not wait up to LISTS_POLL_MS
2277
+ if(sel && wasLive && !openRunLive && !listsDue) reloadLists();
2278
+ if(!pollTimer) schedulePoll(LIVE_POLL_MS);
2279
+ });
2280
+ }
2281
+ function stopLive(){ live = null; }
2282
+ // What the books say now, in words (pure; `null` = keep waiting).
2283
+ function liveOutcome(askId, detail, asks){
2284
+ if(asks && asks.resume && asks.resume.state === "not-started") return { done: true, cls: null, text: null };
2285
+ var list = (asks && asks.asks) || [];
2286
+ var fresh = list.find(function(a){ return a.waiting && a.askId !== askId; });
2287
+ if(fresh) return { done: true, cls: "info", text: "the run is parked at a new ask — the doors above are for it" };
2288
+ if(detail && detail.glyph === "[✓]") return { done: true, cls: "info", text: "run finished [✓] — the sent artifact is in the Run tab" };
2289
+ if(detail && detail.outcome === "rerun") return { done: true, cls: "info", text: "rerun: this run ended and a fresh run was started — find it in Runs" };
2290
+ if(detail && detail.glyph === "[✗]") return { done: true, cls: "info", text: "run ended [✗] " + (detail.label || "") };
2291
+ var st = asks && asks.resume ? asks.resume.label : null;
2292
+ return { done: false, cls: "info", text: (st || "answer applied") + " — run: " + ((detail && detail.label) || "state unknown") };
2293
+ }
2294
+ // A watch outcome never overwrites a refusal the human has not yet clicked past (M4c item 7).
2295
+ function sayLive(flow, runId, o){
2296
+ if(answerMsg && answerMsg.cls === "refused") return;
2297
+ if(o.text) say(flow, runId, o.cls, o.text);
2298
+ else if(o.done){ answerMsg = null; paintAnswerMsg(); }
2299
+ }
2300
+ // The books showed the next state (or the watch gave up): the "working…" block ends and the Ask tab
2301
+ // is drawn from what the books say now. Uses the SAME renderAsk guard as every refresh.
2302
+ function endPending(flow, runId, asks){
2303
+ if(!pendingAnswer) return;
2304
+ pendingAnswer = null;
2305
+ if(currentFlow === flow && currentRunId === runId && asks) renderAsk(asks, null);
2306
+ }
2307
+ function startLive(flow, runId, askId){
2308
+ live = { flow: flow, runId: runId, askId: askId, until: Date.now() + LIVE_MAX_MS };
2309
+ schedulePoll(600);
2310
+ }
2311
+ function watchStep(mine, res){
2312
+ var o = liveOutcome(mine.askId, res[0], res[1]);
2313
+ sayLive(mine.flow, mine.runId, o);
2314
+ if(o.done){ endPending(mine.flow, mine.runId, res[1]); stopLive(); return; }
2315
+ if(Date.now() > mine.until){
2316
+ say(mine.flow, mine.runId, "info", "no outcome after " + Math.round(LIVE_MAX_MS / 60000) + " minutes of watching — reload the page to check the run's books");
2317
+ endPending(mine.flow, mine.runId, res[1]);
2318
+ stopLive();
2319
+ }
2320
+ }
2321
+ schedulePoll(LIVE_POLL_MS);
2322
+ // Back on the tab: one catch-up tick now (forcing both rates) instead of waiting out the 2 s beat.
2323
+ document.addEventListener("visibilitychange", function(){
2324
+ if(!document.hidden){ wasHidden = true; schedulePoll(0); }
2325
+ });
2326
+
2075
2327
  getJSON("/api/runs").then(function(result){
2076
2328
  var runs = result.rows || [];
2077
2329
  runsFilterBar.setItems(runs);
@@ -2106,13 +2358,16 @@
2106
2358
  // first with a short status word, off GET /api/inbox
2107
2359
  // (src/panel/data.js's listStops). Status vocabulary is exactly the
2108
2360
  // words the books' own decision field maps to (never a second, made-up
2109
- // vocabulary): "accepted" / "rejected" / "reran" / "expired" /
2361
+ // vocabulary): "accepted" / "redo" / "reran" / "expired" /
2110
2362
  // "unanswered" (this IS the open case) / the rare "open" fallback (an
2111
2363
  // archived ask.json itself failed to parse). No evidence renders here any
2112
2364
  // more — clicking a stop selects its run and opens the wide right pane's
2113
- // Ask tab, focused on that one ask. No answer controls — those are M4b. --
2365
+ // Ask tab, focused on that one ask. The answer doors live in the Ask tab. --
2114
2366
  function stopStatusLine(row){
2115
- if(row.open) return "time left: " + duration(row.timeLeftMs);
2367
+ if(row.waiting) return "time left: " + duration(row.timeLeftMs);
2368
+ if(row.stuck) return row.stuckLabel;
2369
+ if(row.working) return "working on your " + row.working + "…";
2370
+ if(row.open && row.resume) return row.resume.label;
2116
2371
  var bits = [row.status];
2117
2372
  if(row.reason) bits.push("reason: " + row.reason);
2118
2373
  if(row.answeredAt) bits.push("answered " + (readableDateTime(row.answeredAt) || row.answeredAt));
@@ -2121,17 +2376,29 @@
2121
2376
  }
2122
2377
  function renderStopRow(row){
2123
2378
  var wrap = document.createElement("div");
2124
- wrap.className = "hist-row inbox-row";
2379
+ wrap.className = "hist-row inbox-row s" + row.section;
2125
2380
  wrap.setAttribute("role", "button");
2126
2381
  wrap.setAttribute("tabindex", "0");
2127
- wrap.setAttribute("aria-pressed", "false");
2382
+ // Selected is derived here, off the page's own selection (flow + run + ask), so a rebuild keeps it.
2383
+ var selected = row.flow === currentFlow && row.runId === currentRunId && (row.askId || null) === currentAskId;
2384
+ if(selected) wrap.className += " selected";
2385
+ wrap.setAttribute("aria-pressed", selected ? "true" : "false");
2128
2386
  wrap.setAttribute("data-testid", "inbox-row-" + row.flow + "-" + row.runId + (row.askId ? "-" + row.askId : ""));
2129
2387
  var head = document.createElement("div");
2130
2388
  head.className = "wf-line1";
2131
- var badge = document.createElement("span");
2132
- badge.className = "badge " + (row.open ? "amber" : (row.status === "accepted" ? "green" : (row.status === "rejected" ? "red" : "grey")));
2133
- badge.textContent = row.open ? "·" : row.status;
2134
- head.appendChild(badge);
2389
+ // section 1: [·] waiting on you (pulses); section 2 working: [▶] (pulses);
2390
+ // a stuck run (answer saved, nobody carrying it on): [II] pulses, in section 1;
2391
+ // resume starting: [·] still; section 3: the status word.
2392
+ if(row.section === 1 || row.working || row.resume){
2393
+ var sign = document.createElement("span");
2394
+ sign.className = "dot " + (row.working ? "amber pulse" : (row.stuck ? "stuck pulse" : (row.waiting ? "grey pulse" : "grey")));
2395
+ head.appendChild(sign);
2396
+ } else {
2397
+ var badge = document.createElement("span");
2398
+ badge.className = "badge " + (row.status === "accepted" ? "green" : (row.status === "redo" ? "red" : "grey"));
2399
+ badge.textContent = row.status;
2400
+ head.appendChild(badge);
2401
+ }
2135
2402
  var flowName = textDiv(null, row.flow + " (" + row.runId + ")");
2136
2403
  flowName.className = "wf-name";
2137
2404
  head.appendChild(flowName);
@@ -2152,19 +2419,33 @@
2152
2419
  });
2153
2420
  return wrap;
2154
2421
  }
2155
- function renderInbox(rows){
2422
+ var INBOX_HEADS = { 1: "waiting on you", 2: "working on your answer", 3: "answered and expired" };
2423
+ // N comes from the server (data.js inboxOpenCount); the page only shows it.
2424
+ function renderInbox(rows, openCount){
2156
2425
  var el = document.getElementById("inbox-list");
2426
+ var next = sig({ rows: rows, openCount: openCount });
2427
+ if(next !== null && next === lastSig.inbox) return; // unchanged payload — never rebuild the DOM for nothing
2428
+ lastSig.inbox = next;
2429
+ withScrollPreserved(["left-pane-body"], function(){ withFocusPreserved(el, function(){ buildInbox(el, rows, openCount); }); });
2430
+ }
2431
+ function buildInbox(el, rows, openCount){
2157
2432
  el.innerHTML = "";
2158
- var openCount = 0;
2159
- (rows || []).forEach(function(r){ if(r.open) openCount += 1; });
2160
- document.getElementById("inbox-count-label").textContent = openCount > 0 ? (" (" + openCount + ")") : "";
2433
+ document.getElementById("inbox-count-label").textContent = " (" + (typeof openCount === "number" ? openCount : 0) + ")";
2161
2434
  if(!rows || !rows.length){
2162
2435
  el.innerHTML = '<div class="hint">nothing waiting on you — no stops recorded under this root</div>';
2163
2436
  return;
2164
2437
  }
2165
- rows.forEach(function(row){ el.appendChild(renderStopRow(row)); });
2438
+ var lastSection = 0;
2439
+ rows.forEach(function(row){
2440
+ if(row.section !== lastSection){
2441
+ lastSection = row.section;
2442
+ el.appendChild(textDiv("inbox-h", INBOX_HEADS[row.section] || ""));
2443
+ }
2444
+ el.appendChild(renderStopRow(row));
2445
+ });
2166
2446
  }
2167
- getJSON("/api/inbox").then(function(result){ renderInbox(result.rows || []); }).catch(function(e){
2447
+ getJSON("/api/inbox").then(function(result){ renderInbox(result.rows || [], result.openCount); }).catch(function(e){
2448
+ lastSig.inbox = null;
2168
2449
  document.getElementById("inbox-list").innerHTML = '<div class="hint">failed to load: ' + escapeXml(e.message) + '</div>';
2169
2450
  });
2170
2451
 
@@ -2173,19 +2454,195 @@
2173
2454
  // getRunAsks). Clicking a stop in the left Inbox calls `selectRun` with
2174
2455
  // that ask's id, which reaches here as `focusAskId` and expands exactly
2175
2456
  // that one. With no `focusAskId` (a run selected any other way), this
2176
- // run's own open ask (if it has one) expands by default. No answer
2177
- // controls anywhere in this panel (M4b) — a clearly marked EMPTY slot
2178
- // stands in their place; the collapse/expand toggle is a div with
2457
+ // run's own open ask (if it has one) expands by default. M4b: an open ask
2458
+ // carries the answer doors (renderAnswerBlock); the collapse/expand toggle is a div with
2179
2459
  // role="button" (never a real <button>), same posture as the step-map's
2180
2460
  // own SVG nodes elsewhere on this page. ----
2181
2461
  function askStatusBlock(ask){
2182
- if(ask.open) return textDiv("wf-meta", "time left: " + duration(ask.timeLeftMs));
2462
+ if(ask.waiting) return textDiv("wf-meta", "time left: " + duration(ask.timeLeftMs));
2463
+ // an open ask with a saved answer: the header meta line already says the resume label (stuck or starting) — once.
2464
+ if(ask.open && ask.resume) return null;
2183
2465
  var bits = [ask.status];
2184
2466
  if(ask.reason) bits.push("reason: " + ask.reason);
2185
2467
  if(ask.answeredAt) bits.push("answered " + (readableDateTime(ask.answeredAt) || ask.answeredAt));
2186
2468
  return textDiv("wf-meta", bits.join(" · "));
2187
2469
  }
2188
- function renderAskEvidenceBlock(ask){
2470
+ // ---- M4b piece 4: answer doors. The panel is a CLIENT of the arbiter: it sends
2471
+ // what the human typed and shows the server's/library's refusal by name; it
2472
+ // never decides that a reason is "good enough". ----
2473
+ // Pure: which controls does this ask get? `resume` is the run's own
2474
+ // `resume` field (src/panel/data.js deriveResumeState), never guessed here.
2475
+ // `pending` (M4c item 6) = the answer this page just sent and got a 202 for,
2476
+ // for THIS ask: until the books show the next state there are no doors, only
2477
+ // what is happening. Null when nothing was sent (or the send was refused).
2478
+ function pendingText(p){
2479
+ if(p.decision === "redo") return "working on your redo… draft " + (p.draftNo + 1) + " is coming";
2480
+ if(p.decision === "accept") return "shipping draft " + p.draftNo + "…";
2481
+ return "ending this run, starting a fresh one…";
2482
+ }
2483
+ function answerControls(ask, resume, pending){
2484
+ if(pending && ask && pending.askId === ask.askId){
2485
+ return { kind: "working", text: pendingText(pending), why: "no answer doors: the answer was sent" };
2486
+ }
2487
+ if(!ask || !ask.open){
2488
+ return { kind: "none", why: "no answer doors: this ask is " + ((ask && ask.status) || "closed") + " — there is nothing left to answer" };
2489
+ }
2490
+ if(resume && resume.askId === ask.askId){
2491
+ if(resume.state === "starting"){
2492
+ return { kind: "starting", label: resume.label, why: "no answer doors: this ask already has a saved answer" };
2493
+ }
2494
+ if(resume.state === "not-started"){
2495
+ return { kind: "stuck", label: resume.label, lockPath: resume.lockPath || null, reason: resume.reason || "no reason recorded", why: "no answer doors: this ask already has a saved answer" };
2496
+ }
2497
+ return { kind: "none", why: "no answer doors: " + resume.label };
2498
+ }
2499
+ return { kind: "doors" };
2500
+ }
2501
+ // One message per run, shown in the answer block of that run until the next
2502
+ // action. Set by `say`, never by a guess that the answer worked.
2503
+ var answerMsg = null; // { flow, runId, cls: "refused"|"info", text }
2504
+ var reasonDrafts = {}; // askId -> what the human has typed so far
2505
+ var pendingAnswer = null; // { flow, runId, askId, decision, draftNo } — set on a 202, cleared by the live watch
2506
+ // M4c item 7: should a periodic refresh skip re-drawing the Ask tab? Only while the
2507
+ // human is mid-answer (text typed, or the box focused) on an ask that is STILL
2508
+ // waiting in the new books — a dead ask's box never pins the page. A refusal needs no
2509
+ // hold: it lives in `answerMsg` and is repainted after every render, until the next click.
2510
+ function holdAskRender(box, result){
2511
+ if(!box || !(box.value !== "" || box.focused)) return false;
2512
+ return ((result && result.asks) || []).some(function(a){ return a.waiting && a.askId === box.askId; });
2513
+ }
2514
+ function readReasonBox(){
2515
+ var ta = document.querySelector("#ask-list textarea[data-testid='answer-reason']");
2516
+ return ta ? { askId: ta.getAttribute("data-askid"), value: ta.value, focused: document.activeElement === ta } : null;
2517
+ }
2518
+ function say(flow, runId, cls, text){ answerMsg = { flow: flow, runId: runId, cls: cls, text: text }; paintAnswerMsg(); }
2519
+ function paintAnswerMsg(){
2520
+ document.querySelectorAll("[data-answer-msg]").forEach(function(el){
2521
+ var m = answerMsg;
2522
+ var mine = m && el.getAttribute("data-flow") === m.flow && el.getAttribute("data-run") === m.runId;
2523
+ el.hidden = !mine;
2524
+ el.className = "answer-msg" + (mine ? " " + m.cls : "");
2525
+ el.textContent = mine ? m.text : "";
2526
+ });
2527
+ }
2528
+ function refusalText(r){
2529
+ var b = r.body;
2530
+ var name = b && typeof b.red === "string" ? b.red : (r.text || "no detail");
2531
+ return "HTTP " + r.status + " — " + name;
2532
+ }
2533
+ // While a POST is out the buttons are disabled (stops a double click) — nothing else is ever disabled.
2534
+ function setBusy(root, busy){
2535
+ root.querySelectorAll("button").forEach(function(b){ b.disabled = busy; });
2536
+ }
2537
+ // Every POST names the ask THIS block was rendered for (closure, never
2538
+ // re-read at click time), plus flow/runId captured the same way.
2539
+ function sendAnswer(root, ask, ctx, decision){
2540
+ var ta = root.querySelector("textarea");
2541
+ var payload = { flow: ctx.flow, runId: ctx.runId, askId: ask.askId, decision: decision, reason: ta ? ta.value : "" };
2542
+ say(ctx.flow, ctx.runId, "info", "sending " + decision + "…");
2543
+ setBusy(root, true);
2544
+ postJSON("/api/answer", payload).then(function(r){
2545
+ if(r.status === 202 && r.body && r.body.ok){
2546
+ delete reasonDrafts[ask.askId];
2547
+ pendingAnswer = { flow: ctx.flow, runId: ctx.runId, askId: ask.askId, decision: decision, draftNo: ctx.draftNo };
2548
+ answerMsg = null; paintAnswerMsg();
2549
+ startLive(ctx.flow, ctx.runId, ask.askId);
2550
+ } else {
2551
+ say(ctx.flow, ctx.runId, "refused", refusalText(r));
2552
+ }
2553
+ return reloadRun(ctx.flow, ctx.runId);
2554
+ }).catch(function(e){
2555
+ say(ctx.flow, ctx.runId, "refused", "network failure: " + e.message + " — nothing is confirmed; what the books say is shown in the ask header above");
2556
+ return reloadRun(ctx.flow, ctx.runId);
2557
+ }).then(function(){ if(root.isConnected) setBusy(root, false); });
2558
+ }
2559
+ function sendResumeAgain(root, ctx, askId){
2560
+ say(ctx.flow, ctx.runId, "info", "asking for the resume again…");
2561
+ setBusy(root, true);
2562
+ postJSON("/api/resume", { flow: ctx.flow, runId: ctx.runId }).then(function(r){
2563
+ if(r.status === 202 && r.body && r.body.ok){
2564
+ say(ctx.flow, ctx.runId, "info", "resume requested — waiting for the run's books to show whether it took over");
2565
+ startLive(ctx.flow, ctx.runId, askId);
2566
+ } else {
2567
+ say(ctx.flow, ctx.runId, "refused", refusalText(r));
2568
+ }
2569
+ return reloadRun(ctx.flow, ctx.runId);
2570
+ }).catch(function(e){
2571
+ say(ctx.flow, ctx.runId, "refused", "network failure: " + e.message + " — nothing is confirmed; what the books say is shown in the ask header above");
2572
+ return reloadRun(ctx.flow, ctx.runId);
2573
+ }).then(function(){ if(root.isConnected) setBusy(root, false); });
2574
+ }
2575
+ function makeButton(label, testid, onClick){
2576
+ var b = document.createElement("button");
2577
+ b.type = "button";
2578
+ b.className = "btn";
2579
+ b.textContent = label;
2580
+ b.setAttribute("data-testid", testid);
2581
+ b.addEventListener("click", onClick);
2582
+ return b;
2583
+ }
2584
+ function renderAnswerBlock(ask, ctx){
2585
+ var model = answerControls(ask, ctx.resume, pendingAnswer);
2586
+ var root = document.createElement("div");
2587
+ root.className = "answer-doors";
2588
+ root.setAttribute("data-testid", "answer-doors");
2589
+ if(model.kind === "doors"){
2590
+ var idSafe = "reason-" + String(ask.askId).replace(/[^A-Za-z0-9_-]/g, "_");
2591
+ var lab = document.createElement("label");
2592
+ lab.setAttribute("for", idSafe);
2593
+ lab.textContent = "reason (needed for redo and rerun)";
2594
+ var ta = document.createElement("textarea");
2595
+ ta.id = idSafe;
2596
+ ta.rows = 3;
2597
+ ta.setAttribute("data-testid", "answer-reason");
2598
+ ta.setAttribute("data-askid", ask.askId);
2599
+ ta.value = reasonDrafts[ask.askId] || "";
2600
+ ta.addEventListener("input", function(){ reasonDrafts[ask.askId] = ta.value; });
2601
+ root.appendChild(lab);
2602
+ root.appendChild(ta);
2603
+ var bar = document.createElement("div");
2604
+ bar.className = "actions-row";
2605
+ bar.setAttribute("data-testid", "action-bar");
2606
+ bar.appendChild(makeButton("Accept", "btn-accept", function(){ sendAnswer(root, ask, ctx, "accept"); }));
2607
+ bar.appendChild(makeButton("Redo", "btn-redo", function(){ sendAnswer(root, ask, ctx, "redo"); }));
2608
+ bar.appendChild(makeButton("Rerun", "btn-rerun", function(){ sendAnswer(root, ask, ctx, "rerun"); }));
2609
+ root.appendChild(bar);
2610
+ root.appendChild(textDiv("hint", "accept ships this draft; redo: redo the last step with your reason; rerun: end this run and start a fresh one from the top"));
2611
+ } else if(model.kind === "working"){
2612
+ var wk = document.createElement("div");
2613
+ wk.className = "working-line";
2614
+ wk.setAttribute("data-testid", "answer-working");
2615
+ var wsign = document.createElement("span");
2616
+ wsign.className = "dot amber pulse";
2617
+ wk.appendChild(wsign);
2618
+ wk.appendChild(document.createTextNode(model.text));
2619
+ root.appendChild(wk);
2620
+ } else if(model.kind === "starting"){
2621
+ root.appendChild(textDiv("hint", model.label + " — " + model.why));
2622
+ } else if(model.kind === "stuck"){
2623
+ root.appendChild(textDiv("answer-msg stuck", model.label + ": " + model.reason));
2624
+ if(model.lockPath) root.appendChild(textDiv("hint", "lock file: " + model.lockPath));
2625
+ root.appendChild(textDiv("hint", model.why + ". Trying again starts a resume only; it cannot answer."));
2626
+ var bar2 = document.createElement("div");
2627
+ bar2.className = "actions-row";
2628
+ bar2.appendChild(makeButton("Try the resume again", "btn-resume-again", function(){ sendResumeAgain(root, ctx, ask.askId); }));
2629
+ root.appendChild(bar2);
2630
+ } else {
2631
+ root.appendChild(textDiv("hint", model.why));
2632
+ }
2633
+ var msg = document.createElement("div");
2634
+ msg.setAttribute("data-answer-msg", "1");
2635
+ msg.setAttribute("data-flow", ctx.flow);
2636
+ msg.setAttribute("data-run", ctx.runId);
2637
+ msg.setAttribute("role", "status");
2638
+ msg.setAttribute("aria-live", "polite");
2639
+ msg.setAttribute("data-testid", "answer-msg");
2640
+ msg.hidden = true;
2641
+ root.appendChild(msg);
2642
+ setTimeout(paintAnswerMsg, 0);
2643
+ return root;
2644
+ }
2645
+ function renderAskEvidenceBlock(ask, ctx){
2189
2646
  var ev = document.createElement("div");
2190
2647
  ev.className = "attempts-list";
2191
2648
  var evidence = ask.evidence || {};
@@ -2197,7 +2654,7 @@
2197
2654
  // bolder" — a real heading class (same weight/size/uppercase posture
2198
2655
  // as the Job tab's own field `<label>`s), never the plain `.hint`
2199
2656
  // style a footnote/why-text uses.
2200
- ev.appendChild(textDiv("evidence-heading", "draft under review"));
2657
+ ev.appendChild(textDiv("evidence-heading", "draft " + ctx.draftNo));
2201
2658
  ev.appendChild(draftEl);
2202
2659
  } else {
2203
2660
  ev.appendChild(textDiv("hint", evidence.why || "no draft on file"));
@@ -2221,38 +2678,50 @@
2221
2678
  art.textContent = typeof u.text === "string" ? u.text : "no artifact text on file";
2222
2679
  ev.appendChild(art);
2223
2680
  });
2224
- // M4b's answer doors (accept/reject/rerun) land here — this panel is
2225
- // read-only by construction (M4a-1 scope), so the slot is empty and
2226
- // clearly marked rather than silently absent.
2227
- var answerSlot = document.createElement("div");
2228
- answerSlot.className = "hint";
2229
- answerSlot.setAttribute("data-testid", "ask-answer-slot");
2230
- answerSlot.textContent = "answer controls are not built yet (M4b)";
2231
- ev.appendChild(answerSlot);
2232
2681
  return ev;
2233
2682
  }
2683
+ // M4c item 5: "your answers so far" — one line per answered draft, every field from
2684
+ // the server's block (data.js groupAskBlocks); the page only lays it out.
2685
+ function answerLineText(a){
2686
+ return "draft " + a.draft + " → " + a.decision + (a.reason ? ' "' + a.reason + '"' : "")
2687
+ + (a.answeredAt ? " " + (readableDateTime(a.answeredAt) || a.answeredAt) : "");
2688
+ }
2689
+ function renderAnswersSoFar(block){
2690
+ var box = document.createElement("div");
2691
+ box.setAttribute("data-testid", "answers-so-far");
2692
+ if(!block.answers.length) return box;
2693
+ box.appendChild(textDiv("evidence-heading", "your answers so far"));
2694
+ block.answers.forEach(function(a){ box.appendChild(textDiv("answer-line", answerLineText(a))); });
2695
+ return box;
2696
+ }
2234
2697
  // hamr's 2026-09-27 exit-check review #3: "when opened from an Inbox
2235
2698
  // click it's not clear which ask is shown" — one line naming exactly
2236
- // which ask this is (`Ask i of n`, from src/panel/data.js's own
2237
- // index/total), which declared step it belongs to (`stepName`, at its
2699
+ // which ask line this block is (M4c: one block per signed ask line, its
2700
+ // draft number from src/panel/data.js's groupAskBlocks), which declared step it belongs to (`stepName`, at its
2238
2701
  // signed prose `stepLine` — src/panel/data.js's `deriveAskStepInfo`,
2239
2702
  // never re-derived here), when it was asked, and its status. `stepWhy` is
2240
2703
  // shown verbatim (never a guessed step name) when the books can't name
2241
2704
  // one — the honest word "before" a real value never invents.
2242
- function askHeaderMetaText(ask){
2243
- var bits = ["Ask " + ask.index + " of " + ask.total];
2244
- var stepBit = typeof ask.stepName === "string" && ask.stepName.length > 0
2245
- ? ask.stepName + (typeof ask.stepLine === "number" ? " (line " + ask.stepLine + ")" : "")
2705
+ function blockMetaText(block){
2706
+ var ask = block.current;
2707
+ var bits = [];
2708
+ var stepBit = typeof block.stepName === "string" && block.stepName.length > 0
2709
+ ? block.stepName + (typeof block.stepLine === "number" ? " (line " + block.stepLine + ")" : "")
2246
2710
  : (ask.stepWhy || "step not determinable");
2247
2711
  bits.push(stepBit);
2712
+ bits.push("draft " + block.draftNo);
2248
2713
  bits.push(typeof ask.askedAt === "string" ? "asked " + (readableDateTime(ask.askedAt) || ask.askedAt) : "asked: unknown");
2249
- bits.push(ask.open ? "waiting on you" : ask.status);
2714
+ bits.push(ask.waiting ? "waiting on you" : (ask.open && ask.resume ? ask.resume.label : ask.status));
2250
2715
  return bits.join(" · ");
2251
2716
  }
2252
- function renderAskRow(ask, expanded){
2717
+ // One block per signed ask line (M4c item 5), drawn from the server's `blocks`.
2718
+ function renderAskRow(block, expanded, ctx){
2719
+ var ask = block.current;
2720
+ ctx = { flow: ctx.flow, runId: ctx.runId, resume: ctx.resume, draftNo: block.draftNo };
2253
2721
  var wrap = document.createElement("div");
2254
2722
  wrap.className = "audit-group";
2255
- wrap.setAttribute("data-testid", "ask-row-" + (ask.askId || "no-id"));
2723
+ wrap.setAttribute("data-testid", "ask-block-" + (block.stepName || "no-step"));
2724
+ wrap.setAttribute("data-open", String(!!ask.open));
2256
2725
  var head = document.createElement("div");
2257
2726
  head.setAttribute("role", "button");
2258
2727
  head.setAttribute("tabindex", "0");
@@ -2261,14 +2730,19 @@
2261
2730
  var metaLine = document.createElement("div");
2262
2731
  metaLine.className = "wf-line1";
2263
2732
  var badge = document.createElement("span");
2264
- badge.className = "badge " + (ask.open ? "amber" : (ask.status === "accepted" ? "green" : (ask.status === "rejected" ? "red" : "grey")));
2265
- badge.textContent = ask.open ? "·" : ask.status;
2733
+ if(ask.stuck){
2734
+ // the same sign as the Inbox card and run header: [II], pulsing (server-decided `stuck`, data.js getRunAsks)
2735
+ badge.className = "dot stuck pulse";
2736
+ } else {
2737
+ badge.className = "badge " + (ask.open ? "amber" : (ask.status === "accepted" ? "green" : (ask.status === "redo" ? "red" : "grey")));
2738
+ badge.textContent = ask.open ? "·" : ask.status;
2739
+ }
2266
2740
  metaLine.appendChild(badge);
2267
- metaLine.appendChild(textDiv("wf-meta", askHeaderMetaText(ask)));
2741
+ metaLine.appendChild(textDiv("wf-meta", blockMetaText(block)));
2268
2742
  head.appendChild(metaLine);
2269
2743
  // The question itself — a real, bold heading (review #3: "the question
2270
2744
  // as a clear heading"), never folded into the same small meta line.
2271
- var qText = typeof ask.question === "string" ? ask.question : (ask.questionWhy || "question not recorded");
2745
+ var qText = typeof block.question === "string" ? block.question : (block.questionWhy || "question not recorded");
2272
2746
  var qHeading = document.createElement("h4");
2273
2747
  qHeading.className = "ask-question-heading";
2274
2748
  qHeading.textContent = qText;
@@ -2276,8 +2750,12 @@
2276
2750
  wrap.appendChild(head);
2277
2751
  var body = document.createElement("div");
2278
2752
  body.hidden = !expanded;
2279
- body.appendChild(askStatusBlock(ask));
2280
- body.appendChild(renderAskEvidenceBlock(ask));
2753
+ var statusBlock = askStatusBlock(ask);
2754
+ if(statusBlock) body.appendChild(statusBlock);
2755
+ body.appendChild(renderAskEvidenceBlock(ask, ctx));
2756
+ body.appendChild(renderAnswersSoFar(block));
2757
+ // M4b piece 4: the answer doors (or the reason there are none) — for the CURRENT draft.
2758
+ body.appendChild(renderAnswerBlock(ask, ctx));
2281
2759
  wrap.appendChild(body);
2282
2760
  function toggle(){
2283
2761
  var next = body.hidden;
@@ -2290,33 +2768,44 @@
2290
2768
  });
2291
2769
  return wrap;
2292
2770
  }
2771
+ var askDrawnFor = null; // "flow/runId" whose blocks the Ask tab last drew
2293
2772
  function renderAsk(result, focusAskId, loadError){
2294
2773
  var empty = document.getElementById("ask-empty");
2295
2774
  var list = document.getElementById("ask-list");
2296
- if(loadError){ empty.hidden = false; empty.textContent = "failed to load asks: " + loadError; list.hidden = true; list.innerHTML = ""; return; }
2297
- if(!result){ empty.hidden = false; empty.textContent = "select a run first"; list.hidden = true; list.innerHTML = ""; return; }
2298
- var asks = result.asks || [];
2299
- if(!asks.length){
2775
+ if(loadError){ empty.hidden = false; empty.textContent = "failed to load asks: " + loadError; list.hidden = true; list.innerHTML = ""; return true; }
2776
+ if(!result){ empty.hidden = false; empty.textContent = "select a run first"; list.hidden = true; list.innerHTML = ""; return true; }
2777
+ var blocks = result.blocks || [];
2778
+ if(!blocks.length){
2300
2779
  empty.hidden = false;
2301
2780
  empty.textContent = "this run has never parked on a human — no asks recorded";
2302
2781
  list.hidden = true;
2303
2782
  list.innerHTML = "";
2304
- return;
2783
+ return true;
2305
2784
  }
2785
+ // M4c item 7: never redraw over a reason being typed (every periodic refresh comes through here).
2786
+ // Returns false when held (nothing drawn), true after every real draw — the one signal `reloadRun` stamps on.
2787
+ if(!focusAskId && holdAskRender(readReasonBox(), result)) return false;
2306
2788
  empty.hidden = true;
2307
2789
  list.hidden = false;
2308
- list.innerHTML = "";
2309
- // Default expansion: the ask named by a click from the Inbox
2310
- // (`focusAskId`) wins; otherwise this run's own open ask (if it has
2311
- // one) expands; otherwise every ask stays collapsed.
2312
- var defaultOpenId = focusAskId;
2313
- if(!defaultOpenId){
2314
- var openAsk = asks.find(function(a){ return a.open; });
2315
- if(openAsk) defaultOpenId = openAsk.askId;
2790
+ // The refresh redraws this tab every tick: a block the human opened or closed by hand keeps that
2791
+ // state while its own openness is unchanged (a Inbox click, `focusAskId`, resets to its default).
2792
+ var kept = {};
2793
+ if(!focusAskId && askDrawnFor === result.flow + "/" + result.runId){
2794
+ list.querySelectorAll("[data-testid^='ask-block-']").forEach(function(w){
2795
+ kept[w.getAttribute("data-testid")] = { open: w.getAttribute("data-open"), expanded: w.firstChild.getAttribute("aria-expanded") === "true" };
2796
+ });
2316
2797
  }
2317
- asks.forEach(function(ask){
2318
- var expanded = defaultOpenId != null && ask.askId === defaultOpenId;
2319
- var row = renderAskRow(ask, expanded);
2798
+ askDrawnFor = result.flow + "/" + result.runId;
2799
+ list.innerHTML = "";
2800
+ // Default expansion: the block holding the ask a click from the Inbox
2801
+ // (`focusAskId`) named wins; otherwise a block with an open ask expands;
2802
+ // a run with a single ask line expands it; otherwise blocks stay collapsed.
2803
+ blocks.forEach(function(block){
2804
+ var ask = block.current;
2805
+ var expanded = focusAskId ? block.askIds.indexOf(focusAskId) !== -1 : (ask.open || blocks.length === 1);
2806
+ var prev = kept["ask-block-" + (block.stepName || "no-step")];
2807
+ if(prev && prev.open === String(!!ask.open)) expanded = prev.expanded;
2808
+ var row = renderAskRow(block, expanded, { flow: result.flow, runId: result.runId, resume: result.resume || null });
2320
2809
  // hamr's 2026-09-27 exit-check review #3: "the ask opened from the
2321
2810
  // Inbox gets a visible highlight" — a REAL Inbox click (`focusAskId`
2322
2811
  // set) on THIS ask, never a run's own default open-ask expansion
@@ -2325,7 +2814,7 @@
2325
2814
  // highlight`, this row already carries the `audit-group` class) —
2326
2815
  // persistent here, not momentary, since the human navigated here
2327
2816
  // specifically to see this one.
2328
- if(focusAskId && ask.askId === focusAskId){
2817
+ if(focusAskId && block.askIds.indexOf(focusAskId) !== -1){
2329
2818
  row.classList.add("audit-group-highlight");
2330
2819
  }
2331
2820
  list.appendChild(row);
@@ -2338,6 +2827,7 @@
2338
2827
  }
2339
2828
  }
2340
2829
  });
2830
+ return true;
2341
2831
  }
2342
2832
 
2343
2833
  // theme toggle