|
| 1 | +# Spec: Testable Living Docs poll state machine |
| 2 | + |
| 3 | +> Single-layer feature (attune-gui). Follow-on to |
| 4 | +> [`dashboard-js-testing`](../dashboard-js-testing/) — reuses its |
| 5 | +> approved no-build ES-module + Vitest pattern (build strategy and test |
| 6 | +> runner are already settled there; this spec does not re-litigate them). |
| 7 | +
|
| 8 | +--- |
| 9 | + |
| 10 | +## Phase 1: Requirements |
| 11 | + |
| 12 | +**Status**: approved (2026-06-14) |
| 13 | + |
| 14 | +### Problem statement |
| 15 | + |
| 16 | +The Living Docs page polls `/api/living-docs/rows` while any document is |
| 17 | +regenerating |
| 18 | +([`living_docs.html`](attune-gui/sidecar/attune_gui/templates/living_docs.html), |
| 19 | +`{% block scripts %}`). The poller — `_startPoll` / `_stopPoll` / |
| 20 | +`_scheduleNextPoll` / `_poll` — is a recursive-`setTimeout` state machine |
| 21 | +that must: |
| 22 | + |
| 23 | +- **stop** once no row is `regenerating` |
| 24 | + ([`:200`](attune-gui/sidecar/attune_gui/templates/living_docs.html)), |
| 25 | +- **pause** on a hidden tab and resume on `visibilitychange`, |
| 26 | +- keep going on a transient fetch error (don't give up mid-regeneration). |
| 27 | + |
| 28 | +This is exactly the kind of decision logic that breaks silently — polls |
| 29 | +forever, stops too early, or hammers a backgrounded tab — and it is |
| 30 | +**untested**. It's the next-richest untested inline block after the batch |
| 31 | +panel, and migrating it applies the pattern established in |
| 32 | +`dashboard-js-testing`. |
| 33 | + |
| 34 | +### Scope |
| 35 | + |
| 36 | +**In scope:** |
| 37 | + |
| 38 | +- **Extract the poll decision logic** into a `static_cw/*.js` ES module — |
| 39 | + the pure parts: `shouldKeepPolling(rows)` (stop when none regenerating) |
| 40 | + and any next-delay helper. The module is DOM-free / timer-free so |
| 41 | + Vitest imports it in Node. |
| 42 | +- **Vitest coverage** for the extracted logic, run by the existing |
| 43 | + `frontend (Vitest)` CI job (the include glob from |
| 44 | + `dashboard-js-testing`). |
| 45 | +- **Shrink the inline script** to a `<script type="module">` glue shim |
| 46 | + that keeps the DOM/`setTimeout`/`visibilitychange` wiring and calls the |
| 47 | + module. Behavior-preserving. |
| 48 | + |
| 49 | +**Out of scope:** |
| 50 | + |
| 51 | +- **The Jinja↔JS `computed_state` → badge/action mapping duplication.** |
| 52 | + The same mapping is hand-maintained in Jinja |
| 53 | + ([`:118-157`](attune-gui/sidecar/attune_gui/templates/living_docs.html)) |
| 54 | + and in JS `_renderBadge`/`_renderActions` |
| 55 | + ([`:228-256`](attune-gui/sidecar/attune_gui/templates/living_docs.html)). |
| 56 | + It's a real drift risk, but de-duplicating it (e.g. a server-provided |
| 57 | + presentational field) is a larger change with an API-payload impact — |
| 58 | + **deferred to its own follow-up spec**. This spec leaves both copies |
| 59 | + as-is; the glue shim still calls the existing `_renderBadge` / |
| 60 | + `_renderActions` unchanged. |
| 61 | +- Other inline blocks (`commands.html`, etc.) — separate follow-ups. |
| 62 | +- Re-deriving the build/test-runner choice — settled in |
| 63 | + `dashboard-js-testing`. |
| 64 | +- Any change to poll cadence, action behavior, or API semantics. |
| 65 | + |
| 66 | +### User stories |
| 67 | + |
| 68 | +1. **As a maintainer**, I want the poll stop/continue logic unit-tested |
| 69 | + so a change can't silently make the dashboard poll forever or stop |
| 70 | + mid-regeneration. |
| 71 | +2. **As a reviewer**, I want the extraction to be behavior-preserving and |
| 72 | + verified live before merge. |
| 73 | + |
| 74 | +### Affected layers |
| 75 | + |
| 76 | +- [x] attune-gui (frontend) — primary and only |
| 77 | +- [ ] attune-rag / help / author — no changes |
| 78 | + |
| 79 | +### Coverage areas |
| 80 | + |
| 81 | +| Area | Status | Notes | |
| 82 | +|------|--------|-------| |
| 83 | +| **Problem & scope** | addressed | Untested poll state machine; extract + test the pure decision. | |
| 84 | +| **Data & API contracts** | N/A | No API change — pure client refactor. | |
| 85 | +| **UI/UX & states** | addressed | No visual/behavior change; same polling cadence and badges. | |
| 86 | +| **Edge cases** | addressed | Empty rows, all-terminal rows (must stop), some-regenerating (must continue), hidden-tab pause, transient fetch error (keep polling). | |
| 87 | +| **Cross-layer impact** | N/A | Single layer. | |
| 88 | +| **Error handling** | addressed | Transient fetch error preserves current "keep polling" behavior; glue shim retains the try/catch. | |
| 89 | +| **Tradeoffs & alternatives** | addressed | Pure-logic extraction vs. leaving inline (defeats the purpose) vs. de-dup refactor (deferred — see Out of scope). | |
| 90 | +| **Rollback strategy** | addressed | Revert restores the inline poller; other blocks untouched. | |
| 91 | + |
| 92 | +### Edge cases & open questions |
| 93 | + |
| 94 | +| Question / Edge case | Resolution | |
| 95 | +|----------------------|------------| |
| 96 | +| Stop when no row regenerating | `shouldKeepPolling(rows)`: some `regenerating` → true; none → false; empty → false. Tested. | |
| 97 | +| Hidden-tab pause / resume | Stays in the DOM shim (`document.hidden`, `visibilitychange`); not in the pure module. | |
| 98 | +| Transient fetch error mid-poll | Shim keeps the existing try/catch + reschedule; `shouldKeepPolling` only decides from row data, not fetch outcome. | |
| 99 | +| Does `_applyRows` / `_renderBadge` / `_renderActions` move? | **No** — DOM rendering stays inline this spec (the badge/action de-dup is a separate deferred spec). Only the poll decision is extracted. | |
| 100 | +| What is `shouldKeepPolling`'s input shape? | The `rows` array from `/api/living-docs/rows` (objects with `computed_state`). Pure over that — no DOM. | |
| 101 | + |
| 102 | +### Success criteria |
| 103 | + |
| 104 | +- `shouldKeepPolling` (and any schedule helper) live in a tested |
| 105 | + `static_cw/*.js` module; Vitest covers stop / continue / empty. |
| 106 | +- Living Docs polls **identically** to today — starts when a row is |
| 107 | + regenerating, stops when none are, pauses on hidden tab (verified |
| 108 | + live). |
| 109 | +- The `frontend (Vitest)` CI job runs the new tests; no new infra. |
| 110 | + |
| 111 | +### Gaps |
| 112 | + |
| 113 | +- The Jinja↔JS badge/action duplication is a known drift risk left |
| 114 | + **explicitly deferred** to a follow-up spec (it needs an API-payload |
| 115 | + decision). Documented, not silently skipped. |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +## Phase 2: Design |
| 120 | + |
| 121 | +**Status**: approved (2026-06-14) — see [`design.md`](design.md) |
| 122 | + |
| 123 | +## Phase 3: Tasks |
| 124 | + |
| 125 | +**Status**: approved (2026-06-14) — see [`tasks.md`](tasks.md) |
0 commit comments