|
| 1 | +--- |
| 2 | +title: "studyctl Web UI: Four-Bug Fix (Pomodoro Config, ttyd Auth, Port Cleanup, Terminal Resilience)" |
| 3 | +date: 2026-04-07 |
| 4 | +category: integration-issues |
| 5 | +tags: |
| 6 | + - web-ui |
| 7 | + - pomodoro |
| 8 | + - ttyd |
| 9 | + - security |
| 10 | + - process-management |
| 11 | + - terminal-health |
| 12 | + - fastapi |
| 13 | + - websocket |
| 14 | + - alpine-js |
| 15 | +affected_modules: |
| 16 | + - studyctl.settings |
| 17 | + - studyctl.session.orchestrator |
| 18 | + - studyctl.session.cleanup |
| 19 | + - studyctl.web.routes.session |
| 20 | + - studyctl.web.routes.terminal_proxy |
| 21 | + - studyctl.web.static (components.js, index.html, style.css) |
| 22 | + - studyctl.tui.sidebar |
| 23 | + - studyctl.cli._study |
| 24 | +severity: high |
| 25 | +resolution_time: "~2 hours (fixes only; split layout rewrite is separate)" |
| 26 | +--- |
| 27 | + |
| 28 | +# Web UI: Four-Bug Fix Session |
| 29 | + |
| 30 | +## Problem Summary |
| 31 | + |
| 32 | +Four user-reported issues with the studyctl web UI during v2.2.0 usage: |
| 33 | + |
| 34 | +1. **Pomodoro timer hardcoded to 25 minutes** — no way to adjust focus/break durations |
| 35 | +2. **ttyd terminal accessible without auth** — `-c` flag not passed, WebSocket proxy didn't forward credentials |
| 36 | +3. **Stale ttyd/web server processes blocked new sessions** — orphaned processes on ports 7681/8567 prevented startup |
| 37 | +4. **Terminal panel showed raw tmux error** — "can't find session: study-harness-matrix-1d96f243" when ttyd attached to dead session |
| 38 | + |
| 39 | +## Root Causes |
| 40 | + |
| 41 | +### 1. Pomodoro Hardcoded |
| 42 | +Module-level constants in `sidebar.py` (`POMODORO_FOCUS = 25 * 60`) and `components.js` (`STUDY: 25 * 60`). No config path existed — `Settings` dataclass had no pomodoro fields. |
| 43 | + |
| 44 | +### 2. ttyd No Auth |
| 45 | +`start_ttyd_background()` built the ttyd command without `-c` credentials. The WebSocket proxy in `terminal_proxy.py` connected to upstream ttyd without forwarding the browser's `Authorization` header — HTTP proxy forwarded it but WS didn't. |
| 46 | + |
| 47 | +### 3. Stale Processes |
| 48 | +`end_session_common()` killed processes by PID only (from session state). When cleanup didn't run (abrupt exit, test harness crash), PIDs were lost. New session's `start_web_background()` / `start_ttyd_background()` silently failed to bind the occupied port. |
| 49 | + |
| 50 | +### 4. Terminal Panel Error |
| 51 | +The `terminalPanel()` Alpine component checked `state.ttyd_port` to set `available=true` but never verified ttyd was actually reachable. ttyd stayed running after tmux session died (no `--once` flag), serving the raw tmux "can't find session" error to every new client. |
| 52 | + |
| 53 | +## Solutions |
| 54 | + |
| 55 | +### Fix 1: Configurable Pomodoro Timer |
| 56 | + |
| 57 | +**Settings layer** (`settings.py`): |
| 58 | +```python |
| 59 | +@dataclass |
| 60 | +class PomodoroConfig: |
| 61 | + focus: int = 25 # minutes |
| 62 | + short_break: int = 5 |
| 63 | + long_break: int = 15 |
| 64 | + cycles: int = 4 |
| 65 | +``` |
| 66 | +Added to `Settings`, loaded from YAML `pomodoro:` section. |
| 67 | + |
| 68 | +**API endpoint** (`web/routes/session.py`): |
| 69 | +```python |
| 70 | +@router.get("/settings/pomodoro") |
| 71 | +def get_pomodoro_settings() -> dict: |
| 72 | + pomo = load_settings().pomodoro |
| 73 | + return {"focus": pomo.focus, "short_break": pomo.short_break, ...} |
| 74 | +``` |
| 75 | + |
| 76 | +**Web UI** (`components.js`): Alpine store loads `localStorage > API > defaults`. `saveDurations()` persists to localStorage. Editable number inputs in header overlay and body-double panel. |
| 77 | + |
| 78 | +**TUI sidebar** (`sidebar.py`): Reads from `load_settings().pomodoro`. Keybindings: `s` start/stop, `+/-` adjust focus, `p` pause, `r` reset. |
| 79 | + |
| 80 | +### Fix 2: ttyd Authentication |
| 81 | + |
| 82 | +**Credential passthrough** (`orchestrator.py`): |
| 83 | +```python |
| 84 | +def start_ttyd_background(session_name, *, lan=False, username="", password=""): |
| 85 | + if username and password: |
| 86 | + cmd.extend(["-c", f"{username}:{password}"]) |
| 87 | +``` |
| 88 | + |
| 89 | +**WebSocket auth** (`terminal_proxy.py`): |
| 90 | +```python |
| 91 | +auth_header = ws.headers.get("authorization") |
| 92 | +if auth_header: |
| 93 | + upstream_kwargs["additional_headers"] = {"Authorization": auth_header} |
| 94 | +``` |
| 95 | + |
| 96 | +Both CLI (`_study.py`) and web route (`session.py`) read config credentials and pass them through. |
| 97 | + |
| 98 | +### Fix 3: Stale Process Cleanup |
| 99 | + |
| 100 | +**Port-based kill** (`orchestrator.py`): |
| 101 | +```python |
| 102 | +def _kill_port_occupant(port: int, expected_cmd: str = "") -> None: |
| 103 | + """Kill any process listening on port. Safety guard: only kills if command matches.""" |
| 104 | + # Uses lsof -ti :port, ps -p PID -o command=, os.kill(SIGTERM) |
| 105 | + # 0.5s grace period after kill for port release |
| 106 | +``` |
| 107 | + |
| 108 | +Called before `start_ttyd_background()` and `start_web_background()`. Port-based fallback also added to `end_session_common()` in `cleanup.py`. `web_port` now stored in session state. |
| 109 | + |
| 110 | +### Fix 4: Terminal Panel Resilience |
| 111 | + |
| 112 | +**Zombie ttyd killer** (`session.py`): |
| 113 | +```python |
| 114 | +def _kill_stale_ttyd(state: dict) -> None: |
| 115 | + """Kill stale ttyd process if the tmux session it attaches to is gone.""" |
| 116 | +``` |
| 117 | +Called from `_get_full_state()` during zombie detection. |
| 118 | + |
| 119 | +**Health check** (`index.html`): `terminalPanel()` component split into `available` (state has ttyd_port) vs `connected` (probe `/terminal/` succeeds). 10s periodic health check. `.terminal-unavailable` overlay shows "Terminal session ended" when connected=false. |
| 120 | + |
| 121 | +## Investigation Steps That Failed |
| 122 | + |
| 123 | +- **Split-pane resizable layout**: Attempted CSS Flexbox, CSS Grid, absolute positioning (Alpine), absolute positioning (vanilla JS). ALL produced correct DOM values in Playwright but didn't render correctly in real browsers (Chrome/Brave/Firefox on macOS). Deferred to next session — see handoff document. |
| 124 | + |
| 125 | +## Prevention Strategies |
| 126 | + |
| 127 | +1. **Always pass auth credentials through the full chain** — HTTP proxy, WebSocket proxy, and the underlying service. Test with `curl -u` against both the proxy and direct endpoints. |
| 128 | +2. **Kill by port, not just PID** — PIDs are fragile (recycling, lost state). Port-based cleanup is reliable. Always call `_kill_port_occupant()` before binding. |
| 129 | +3. **Health-check backing services** — don't assume a port number in state means the service is actually running. Probe the endpoint before showing UI. |
| 130 | +4. **Config should flow to ALL consumers** — if a value is configurable, wire it through to every place that uses it (API, web UI, TUI, CLI). |
| 131 | + |
| 132 | +## Test Coverage |
| 133 | + |
| 134 | +20 new tests in `tests/test_fixes_pomodoro_auth_terminal.py`: |
| 135 | +- `TestPomodoroConfig` (4 tests): defaults, custom values, YAML loading |
| 136 | +- `TestPomodoroAPI` (3 tests): endpoint returns config/defaults/fallback |
| 137 | +- `TestTtydAuth` (3 tests): `-c` flag present/absent based on credentials |
| 138 | +- `TestTtydLifecycle` (4 tests): kill stale ttyd, zombie detector integration |
| 139 | +- `TestPomodoroWebUI` (4 Playwright E2E): settings row visible/hidden, custom duration, API |
| 140 | +- `TestTerminalResilience` (2 Playwright E2E): unavailable message, hidden when no ttyd |
| 141 | + |
| 142 | +## Related Documentation |
| 143 | + |
| 144 | +- `docs/plans/2026-04-04-ttyd-integration.md` — original ttyd integration plan |
| 145 | +- `docs/plans/2026-04-05-web-ui-alpine-sidebar-plan.md` — Alpine sidebar plan |
| 146 | +- `docs/internal/solutions/tmux-session-management-and-ci-issues.md` — prior session cleanup patterns |
| 147 | +- `docs/internal/solutions/parallel-research-catches-pre-implementation-assumptions.md` — ttyd auth research (Note: `-c` flag IS supported despite this doc's finding about `TTYD_CREDENTIAL`) |
| 148 | +- `.claude/handoffs/2026-04-07-184001-web-ui-split-layout-rewrite.md` — handoff for the unfinished split layout rewrite |
0 commit comments