Skip to content

Commit 96806fe

Browse files
fix(web): the session timer's init() runs once per page load
Alpine calls init() for an x-data object that defines one, and the markup also says x-init="init()", so the page called it twice. The earlier fix (#14 journey) made only the listener registration idempotent and left the fetches to run again as "idempotent". The RED before this commit shows they are not: two /api/session/state reads race, the picker settles on the first, and the second can land after a session was started elsewhere and adopt it — the CI artifact of run 35516418191, byte for byte. init() is now a once-per-instance gate over the former body (_initOnce): later calls return the first run's promise, so the settled picker is the one and only read, and the _listenersRegistered guard is subsumed. The e2e click-site comment records what the artifact showed and keeps the capture for any fourth occurrence, which would have to be a different mechanism. Verified on this branch: node --test 148/148 (147 + the RED, now green); the recovery journey e2e file 9 passed / 2 skipped in order; ruff clean.
1 parent 60d5f94 commit 96806fe

2 files changed

Lines changed: 37 additions & 20 deletions

File tree

‎packages/studyloop/src/studyloop/web/static/js/components/session-timer.js‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ export function sessionTimer() {
132132
user acts on the conflict, so an /api/session/state response that was
133133
already in flight cannot overwrite the newer state. */
134134
_conflictEpoch: 0,
135+
/* The one init() run of this page load (see init()). Null until the
136+
first call; every later call returns it instead of starting another. */
137+
_initPromise: null,
135138

136139
energyBandLabel() {
137140
if (this.energy >= 7) return 'High energy';
@@ -165,18 +168,27 @@ export function sessionTimer() {
165168
});
166169
},
167170

168-
async init() {
171+
init() {
169172
// Alpine calls init() itself for an x-data object that defines one, and
170-
// the markup ALSO says x-init="init()", so this runs twice per page
171-
// load. That was harmless while the listeners below only set picker
172-
// fields; it is not once one of them starts a session — two listeners
173-
// meant two POSTs per click and a 409 for the second (found by the #14
174-
// browser journey). Register the window listeners exactly once; the
175-
// fetches below are idempotent and may run again.
176-
if (!this._listenersRegistered) {
177-
this._listenersRegistered = true;
178-
this._registerWindowListeners();
179-
}
173+
// the markup ALSO says x-init="init()", so the page calls this twice per
174+
// load. Two listeners were the first cost of that (two POSTs per click
175+
// and a 409 for the second, found by the #14 browser journey); the
176+
// fetches were believed idempotent and left to run again. They are not:
177+
// each run issued its own /api/session/state read, the picker settled
178+
// on whichever landed first, and the OTHER read could land after a
179+
// session had since been started elsewhere -- a second tab, over plain
180+
// HTTP -- and adopt it into a tab that was sitting on the picker: no
181+
// click, no 409, Start hidden under the live layout. That is the state
182+
// CI run 35516418191 captured at the click (e2e, test_409_from_a_second
183+
// _tab, the third such timeout and the first with evidence). So init()
184+
// runs ONCE per page load, whoever calls it: later calls get the first
185+
// run's promise. The settled picker is then the one and only read.
186+
if (!this._initPromise) this._initPromise = this._initOnce();
187+
return this._initPromise;
188+
},
189+
190+
async _initOnce() {
191+
this._registerWindowListeners();
180192

181193
const optionsPromise = fetch('/api/session/options')
182194
.then((res) => res.ok ? res.json() : null)
@@ -293,7 +305,7 @@ export function sessionTimer() {
293305
/* A planning launch can arrive from the Plans view before init()'s
294306
options fetch has resolved; the agent is not missing, it is not yet
295307
known. Wait for the picker's own settlement before deciding.
296-
(init() sets _optionsReady on every run; a timer whose init never
308+
(init()'s one run sets _optionsReady; a timer whose init never
297309
ran has nothing to wait for and falls through to the check.) */
298310
if (purpose === 'planning' && !this.agent && this._optionsReady) {
299311
/* Bounded (council review 6): a fetch that never settles must not hold

‎packages/studyloop/tests/e2e/test_session_recovery_journey.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,19 @@ def test_409_from_a_second_tab_offers_reattach_that_adopts_the_session(
347347
d.agent = 'codex';
348348
}"""
349349
)
350-
# Twice on CI this click has timed out with "element is not visible"
351-
# (runs 35341660469 and 35380095596) after the settled wait above passed,
352-
# and neither run produced evidence of WHY the picker was hidden: the
353-
# timeout fires before this test's own _diag hook, so the artifact held
354-
# nothing for it. Every adopt path in the timer was read against the
355-
# second failure and none explains it without a click. Capture the
356-
# state at the click so a third occurrence names the mechanism instead
357-
# of the symptom -- the fix must rest on that, not on a guess.
350+
# Three times on CI this click timed out with "element is not visible"
351+
# (runs 35341660469, 35380095596, 35516418191) after the settled wait
352+
# above passed. The first two left no evidence -- the timeout fired
353+
# before this test's _diag hook -- so the capture below was added, and
354+
# the third run's artifact named the mechanism: sessionActive true,
355+
# topic "Study focus" (tab A's), conflict null, picker display none,
356+
# with startSession() never run. The timer's init() ran twice per page
357+
# load (Alpine's auto-init plus x-init="init()"), each run issued its own
358+
# /api/session/state read, the settled wait observed the first, and the
359+
# second landed after _start_session above and adopted tab A's session
360+
# into this tab. Fixed at the source -- init() is one run per page load,
361+
# so the settled picker IS the only read. The capture stays: a fourth
362+
# occurrence would mean a different mechanism, and it must name it.
358363
try:
359364
page.locator("[data-testid='study-start-session']").click()
360365
except Exception: # pragma: no cover - diagnostics only

0 commit comments

Comments
 (0)