Skip to content

Commit 7ce23cb

Browse files
feat(web): Labs entry, Labs start and the practice-check loopback hop (W2)
Why: proposal §7's Labs row and council M0 Q2 (option b): the drill runs in `studyloop drill`, in another process; its result reaches the mentor through ONE loopback route in the web server, which re-renders and bounds (lane T), never trusting CLI-rendered text. `web/routes/modules.py`: GET /api/modules/labs lists installed modules' decks and tasks by id and prompt (+ successCriteria, phases and the exact drill commands) -- never hint/solution/mentorNotes (§5.6, C7; a tripwire asserts it). POST /api/modules/labs/start reuses the one session-start path (201 start payload + drill) or binds the drill to the live session (200, attached); it records `labs_drill` and the capability (token + web_port) in the state file. Nothing executes here: the browser has no consent surface in v1 (§5.5, `drill` has no --yes; §8 browser execution is v1.1). POST /api/modules/labs/cancel clears the drill and touches nothing else. POST /api/modules/practice-check: loopback peer only (403), token header (401) compared in constant time to the state file (403), the token's session must be the active one (403), lane T's validator (422 naming the field, never the value), replay dedup on (attempt_id, phase) (409), burst 5/min per session (429), then deliver_to_active_session -> 200 with the outcome's status/reason -- a non-landing delivery is a 200 that says so, because the hop maps any non-2xx to its own `error`. `_options.py`: `labs` on GET /session/options only when non-empty, read fresh (the lockfile is not in the index fingerprint). `app.py`: the one include line (moved here from W3: W1 cannot go green without it). `orchestrator.py`: `mint_practice_check_token()` and its one call beside the web_pid/web_port write (W2 brief, owned block #2). `labs.js`: the store (one entry, hidden with no module), the view (start -> Study Session view, reload re-adopts through the tested re-attach path; attached start needs none; cancel also drops an in-flight start), and the client-side scrub of mentor-only fields. index.html/style.css: the sidebar entry and the view; `.labs-error`, not `.picker-error`, keeps the picker's strict single-selector contract. `lane_ownership.yaml`: the two W2 grants (orchestrator.py, _drill.py) and the S3 lease (mkdocs.yml) recorded as dual ownership so the guard enforces the brief instead of refusing it. Evidence: evidence/W/w1-web-modules.green.txt (21 passed), w1-module-labs-js.green.txt (17 pass), w2-gate-g.txt (lint, pyright 0, test-js 184, docs strict, release-consistency, spec-check green; just test 7885 passed / 3 failed: the pre-existing tmux red and the two coverage-ratchet tests that W4's browser journey satisfies).
1 parent bfd156b commit 7ce23cb

12 files changed

Lines changed: 1269 additions & 13 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
node --test packages/studyloop/tests/js/module-labs.test.js
2+
# worktree: module-web-labs HEAD: bfd156bb+W2 2026-10-04T23:25:23Z
3+
✔ the mentor-only field list is exactly the three §5.6 fields (plus the snake_case twin) (0.560041ms)
4+
✔ containsMentorOnlyFields finds a forbidden key anywhere in a payload (0.101708ms)
5+
✔ labsEntries is empty for an options payload with no labs key, and for a non-array (0.054458ms)
6+
✔ labsEntries keeps ids, prompts and success criteria and drops anything mentor-side (0.106958ms)
7+
✔ drillCommands prefers the server commands and otherwise derives them from the phases (0.067417ms)
8+
✔ the store is unavailable until a refresh finds an installed module (0.117375ms)
9+
✔ the store becomes available with one installed module and exposes it scrubbed (0.094209ms)
10+
✔ a failed refresh hides Labs rather than showing a stale list (0.080458ms)
11+
✔ starting a drill posts the drill identity (and the chosen agent) to the labs start route (0.2385ms)
12+
✔ a fresh start (201) lands the learner in the Study Session view and re-reads the live session (0.099625ms)
13+
✔ an attached start (200) does not reload: the session was already the learner's (0.083792ms)
14+
✔ the active drill never carries mentor-side fields, whatever the server sent (0.06675ms)
15+
✔ a refused start (409 from another view, 503 without an agent) is shown, not swallowed (0.333333ms)
16+
✔ start without an agent is refused locally with the picker's own hint (0.069292ms)
17+
✔ cancel posts to the cancel route, clears the drill and leaves the session alone (0.110583ms)
18+
✔ a start in flight can be cancelled: the late 201 is dropped and nothing is adopted (0.109041ms)
19+
✔ the view restores a live drill from storage on init and reads the agents from the store (0.116542ms)
20+
ℹ tests 17
21+
ℹ suites 0
22+
ℹ pass 17
23+
ℹ fail 0
24+
ℹ cancelled 0
25+
ℹ skipped 0
26+
ℹ todo 0
27+
ℹ duration_ms 34.478042
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
env -u VIRTUAL_ENV uv run pytest packages/studyloop/tests/test_web_modules.py -q -p no:cacheprovider
2+
# worktree: module-web-labs HEAD: bfd156bb+W2 (routes/modules.py, _options.py labs entry, app.py include, orchestrator token, labs.js, index.html, style.css) 2026-10-04T23:25:22Z
3+
..................... [100%]
4+
=============================== warnings summary ===============================
5+
.venv/lib/python3.12/site-packages/starlette/testclient.py:53
6+
/Users/ataylor/code/personal/tools/studyloop-wt/module-web-labs/.venv/lib/python3.12/site-packages/starlette/testclient.py:53: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
7+
_PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]
8+
9+
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
10+
21 passed, 1 warning in 1.35s
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
env -u VIRTUAL_ENV just lint typecheck test-js docs release-consistency spec-check (then: just test, below)
2+
# worktree: module-web-labs bfd156bb+W2 uncommitted 2026-10-04T23:49:00Z
3+
uv run --group dev ruff check .
4+
All checks passed!
5+
uv run --group dev ruff format --check .
6+
uv run --group dev pyright
7+
0 errors, 0 warnings, 0 informations
8+
✔ submit: non-JSON HTTP error falls back to "<status> <statusText>" (0.173291ms)
9+
✔ submit: network failure sets formError and never opens a WS (0.15775ms)
10+
✔ WS: transport_error is the failure terminal — error summary, running=false, socket closed (0.145334ms)
11+
✔ WS: onerror surfaces formError; onclose is the running=false safety net (0.190083ms)
12+
✔ a failed refresh hides Labs rather than showing a stale list (0.123083ms)
13+
✔ load: a failing list surfaces an error and still finishes loading (0.154834ms)
14+
✔ recordCheckpoint: both sinks failed is "not recorded", never "partially recorded" (0.233791ms)
15+
ℹ tests 184
16+
ℹ pass 184
17+
ℹ fail 0
18+
INFO - Documentation built in 2.25 seconds
19+
uv run python scripts/check-release-consistency.py --skip-wheel
20+
release consistency passed for studyloop 0.5.0
21+
if command -v openspec >/dev/null 2>&1; then openspec validate --specs --all; else echo "openspec CLI not found — skipping spec validation (see the OpenSpec section of docs/contributing.md)"; fi
22+
ℹ [INFO] file: skip_specs is set in .openspec.yaml: change declares no spec-level behavior changes, zero deltas accepted
23+
⚠ [WARNING] overview: Purpose section is still a placeholder rather than a Purpose anyone wrote (the sentence `openspec archive` writes for a new capability, or a `TBD`/`TODO` marker left in its place). Replace it with what this capability is for, editing the main spec directly: a `## Purpose` in a delta is read only when the capability is created, so it cannot replace this one.
24+
Totals: 25 passed, 0 failed (25 items)
25+
fast-recipes exit=0
26+
27+
env -u VIRTUAL_ENV just test # 2026-10-04T23:49:39Z
28+
FAILED packages/studyloop/tests/test_e2e_coverage_gate.py::test_every_route_has_a_full_stack_test
29+
FAILED packages/studyloop/tests/test_e2e_coverage_gate.py::test_every_nav_view_has_a_browser_test
30+
FAILED packages/studyloop/tests/test_fresh_install_scope.py::test_studyloop_study_exits_2_with_the_diagnostic_on_a_virgin_home
31+
==== 3 failed, 7885 passed, 15 skipped, 823 deselected in 966.49s (0:16:06) ====
32+
just test exit=1
33+
34+
# Reading (lane W): 3 red.
35+
# - test_fresh_install_scope::test_studyloop_study_exits_2_... -- pre-existing baseline red
36+
# (00-common.md §0: tmux is a mise shim that cannot resolve under the virgin HOME).
37+
# - test_e2e_coverage_gate::test_every_route_has_a_full_stack_test and
38+
# ::test_every_nav_view_has_a_browser_test -- the repo's coverage ratchet: the four new
39+
# routes and the `labs` nav view must be walked by a browser/full-stack test under
40+
# tests/e2e/. That test is W4 (tests/e2e/test_module_labs_journey.py); the ratchet is
41+
# structurally red for W2 and W3 and is shown green in the W4 gate transcript.

‎packages/studyloop/src/studyloop/session/orchestrator.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,33 @@ def start_web_background(session_name: str, *, lan: bool = False, password: str
273273
stderr=subprocess.DEVNULL,
274274
env=child_env,
275275
)
276+
from studyloop.modules.hop import TOKEN_KEY
276277
from studyloop.session_state import write_session_state
277278

278-
write_session_state({"web_pid": proc.pid, "web_port": port})
279+
# The per-session capability the loopback `practice_check` hop presents (council
280+
# M0 Q2.2): minted beside the port it will be presented to, in the owner-only
281+
# state file `studyloop drill` trusts. The web route re-mints only when absent.
282+
write_session_state(
283+
{"web_pid": proc.pid, "web_port": port, TOKEN_KEY: mint_practice_check_token()}
284+
)
279285
_open_browser(f"http://127.0.0.1:{port}/session")
280286
except Exception:
281287
console.print("[yellow]Could not start web dashboard.[/yellow]")
282288

283289

290+
def mint_practice_check_token() -> str:
291+
"""A fresh per-session capability token for the ``practice_check`` loopback route.
292+
293+
32 random bytes, URL-safe (43 characters): unguessable, printable, and safe to carry in
294+
the ``X-StudyLoop-Session-Token`` header. Its only copy is the session state file
295+
(``0o600`` in a ``0o700`` directory), which is what makes it a capability rather than
296+
a password -- same user, same machine, same lifetime as the session.
297+
"""
298+
import secrets
299+
300+
return secrets.token_urlsafe(32)
301+
302+
284303
def _kill_port_occupant(port: int, expected_cmd: str = "") -> None:
285304
"""Kill any process listening on *port*.
286305

‎packages/studyloop/src/studyloop/web/app.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ async def context_scope_error(_request: Request, exc: ScopeError) -> JSONRespons
289289
explorer,
290290
history,
291291
mastery,
292+
modules,
292293
notes,
293294
now,
294295
parking,
@@ -310,6 +311,9 @@ async def context_scope_error(_request: Request, exc: ScopeError) -> JSONRespons
310311
app.include_router(explorer.router, prefix="/api")
311312
app.include_router(artefacts.router)
312313
app.include_router(body_double.router, prefix="/api")
314+
app.include_router(
315+
modules.router, prefix="/api"
316+
) # module-contract v1: Labs + practice-check hop
313317
# Topic exercises are a developer preview. Keep the router absent (404),
314318
# rather than registered-and-refusing, unless the web process was started
315319
# explicitly with `studyloop web --dev`; otherwise the API would remain a

0 commit comments

Comments
 (0)