Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { pathToFileURL } from "node:url";
import Fastify from "fastify";
import {
healthPayload,
Expand Down Expand Up @@ -913,7 +914,10 @@ async function start() {
}
}

const isMain = import.meta.url === `file://${process.argv[1]?.replace(/\\/g, "/")}`;
// Use `pathToFileURL` to derive the comparison URL — Windows absolute paths produce
// `file:///D:/...` (three slashes) but a naive `file://${path}` produces `file://D:/...`,
// which never matches and silently prevents the server from starting.
const isMain = process.argv[1] ? import.meta.url === pathToFileURL(process.argv[1]).href : false;
if (isMain) {
void start();
}
41 changes: 41 additions & 0 deletions project/stories/STORY-064-windows-isMain-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
id: STORY-064
title: Fix Windows isMain check in apps/api/src/index.ts (silent server failure on Windows)
type: story
status: in-progress
priority: P0
estimate: XS
parent: EPIC-019
phase: scaffolding
tags: [bug, windows, dev-experience]
created: 2026-05-11
updated: 2026-05-11
---

## Description

The `start()` invocation at the bottom of `apps/api/src/index.ts` is gated on:

```ts
const isMain = import.meta.url === `file://${process.argv[1]?.replace(/\\/g, "/")}`;
```

On Windows this is ALWAYS false. `import.meta.url` for an absolute Windows path is `file:///D:/...` (three slashes) but the manual construction produces `file://D:/...` (two slashes). The dev server starts silently, never calls `app.listen`, never logs anything, and Chrome's localhost:4000 just refuses connections. Caught during the 2026-05-11 Chrome walkthrough; no automated test ever exercised the entrypoint because `start()` is only ever invoked via the running process.

## Acceptance criteria

- [x] Replace the hand-rolled URL comparison with Node's `url.pathToFileURL(process.argv[1]).href`. Both Windows and POSIX produce the canonical `file:///…` form, so the check works on both.
- [x] `pnpm --filter @learnpro/api dev` on a fresh Windows clone logs `Server listening at http://0.0.0.0:4000` within ~5 s of startup.
- [ ] (Optional follow-up) Add an apps/api e2e test that spawns the server via the dev script and waits for `/health` to return 200 — would catch this class of bug.

## Dependencies

None.

## Notes

Fixed inline during the Chrome walkthrough; lands via a chore PR. The optional e2e test is filed for a future session but not blocking this Story's close.

## Activity log

- 2026-05-11 — created + in-progress. Found during /option 1/ Chrome walkthrough — API server silently refused connections on Windows. Patched `apps/api/src/index.ts` to use `pathToFileURL`. Lands via chore PR.
42 changes: 42 additions & 0 deletions project/stories/STORY-065-piston-windows-docker-desktop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
id: STORY-065
title: Piston sandbox fundamentally broken on Docker Desktop for Windows (cgroup v2 unavailable)
type: story
status: backlog
priority: P1
estimate: M
parent: EPIC-003
phase: v1
tags: [bug, sandbox, piston, windows, self-host]
created: 2026-05-11
updated: 2026-05-11
---

## Description

The Piston sandbox container (`ghcr.io/engineer-man/piston`) requires a writable cgroup v2 filesystem at `/sys/fs/cgroup` — its `docker-entrypoint.sh` does `mkdir isolate/` directly under `/sys/fs/cgroup` to set up per-execution isolation. Docker Desktop on Windows does NOT expose a writable cgroup v2 mount to arbitrary containers (the WSL2 backend's cgroup behavior is restricted), so Piston enters a restart loop spamming `mkdir: cannot create directory 'isolate/': Read-only file system`.

Result: the Run/Submit button in /playground and /session hangs indefinitely, because `apps/api`'s SandboxProvider calls into a Piston that's perpetually restarting. This blocks **every** code-execution flow in the app for Windows self-hosters.

Caught during the 2026-05-11 Chrome walkthrough — clicking Run in /playground caused the page to hang for 30+ seconds and Chrome to drop the screenshot CDP call.

## Acceptance criteria

- [ ] Document Windows dev-stack limitations in `infra/docker/README.md` — Piston requires Linux or macOS Docker; Windows must use WSL2-native Docker (not Docker Desktop's WSL2-integrated daemon).
- [ ] Add a `health` endpoint check in apps/api's startup that pings Piston and logs a loud warning if it returns non-200, so an operator sees the problem before clicking Run.
- [ ] Decide on a fallback strategy: either (a) ship judge0 as an alternative sandbox provider for Windows-Docker-Desktop users, (b) document the WSL2-native Docker workaround in the self-host guide, or (c) document the limitation and leave Run disabled with a clear message.
- [ ] If we add a fallback provider, it should plug into the existing `SandboxProvider` interface so the rest of the app needs no changes.

## Dependencies

- ADR-0002 (sandbox decision) — re-read; consider whether judge0 belongs as a documented alternative for self-hosters with broken cgroup v2.

## Notes

This is a real blocker for the "self-host on any platform" positioning. Most Mac/Linux Docker setups work fine; Windows is the outlier. Worth documenting prominently in the self-host README so users don't burn time discovering this themselves.

Tested 2026-05-11 with Docker Desktop 29.3.0 on Windows 11 + WSL2 backend. Piston's `docker-entrypoint.sh` log: `mkdir: cannot create directory 'isolate/': Read-only file system` repeating ~50 times before container marked Restarting.

## Activity log

- 2026-05-11 — created. Found during /option 1/ Chrome walkthrough.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
id: STORY-066
title: Fix /dashboard ↔ /recommended infinite redirect loop for unrecognized target_role values
type: story
status: backlog
priority: P0
estimate: S
parent: EPIC-017
phase: v1
tags: [bug, auth, post-signin, redirect-loop]
created: 2026-05-11
updated: 2026-05-11
---

## Description

A signed-in user with `profiles.target_role` set to **any value not recognized by the role library** (any free-text answer from the deterministic onboarding fallback in `LEARNPRO_DISABLE_ONBOARDING_LLM=1` mode, plus any LLM answer that doesn't fuzzy-match a library slug) gets stuck in an infinite 307 loop:

1. Browser → `/dashboard` (after onboarding completes).
2. `/dashboard` calls `destinationForUser(user_id)` → returns `/recommended` because `episodes=0` and `target_role` is truthy (the check is just `if (target_role)`, not "is this a recognized role"). → 307 redirect.
3. `/recommended` calls `/api/recommendation`. Server returns `{ role: null, recommended_tracks: [], recommended_daily_minutes: null }` because `getRecommendation` does case-insensitive slug match and `"backend engineer"` ≠ `"backend-engineer"`. The page sees `!payload.role` and 307s to `/dashboard`.
4. GOTO 2.

Chrome shows `ERR_TOO_MANY_REDIRECTS`. Logs show 100+ alternating 307s per refresh.

Caught during the 2026-05-11 Chrome walkthrough.

## Acceptance criteria

- [ ] Fix `destinationFor` in `apps/web/src/auth/post-signin.ts` so it only returns `/recommended` when the user's `target_role` actually maps to a known role in the library. Two options, pick one:
- **A.** Wire `getRecommendation` into `destinationFor` so the routing decision uses the same truth source as the page. Costs an extra DB-less library lookup; no Postgres round-trip.
- **B.** Pass a sentinel cookie / query param when `/recommended` bails to `/dashboard`, and short-circuit the destinationFor check on the dashboard when the sentinel is present. Stateful, fragile, but no role-library coupling.
- [ ] Regression test: a Vitest + JSDOM unit test on `destinationFor` with `target_role: "Backend engineer"` (label, not slug) returns `/dashboard`, not `/recommended`.
- [ ] Regression test: a Vitest unit test on `/recommended/page.tsx`'s redirect path that asserts the bail-out path is exercised only when the API returns null role (no orchestration loop).
- [ ] Manual verification: complete onboarding with the deterministic fallback (`LEARNPRO_DISABLE_ONBOARDING_LLM=1`) and answer "Backend engineer" — landing page must be `/dashboard`, no redirect loop.

## Dependencies

- Related: STORY-067 (label-form acceptance in `getRecommendation`). Fixing 067 alone would mask 066 for common labels but not for genuinely unmappable text like "AI / ML researcher".

## Notes

This is a P0 because the deterministic onboarding fallback is the default path when `ANTHROPIC_API_KEY` is unset (i.e. every self-hoster who hasn't wired an API key), and the fallback produces label-form answers. Every such user currently lands in the redirect loop on first sign-in.

## Activity log

- 2026-05-11 — created. Found during /option 1/ Chrome walkthrough.
44 changes: 44 additions & 0 deletions project/stories/STORY-067-getrecommendation-label-form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
id: STORY-067
title: getRecommendation should accept label form, not just slug
type: story
status: backlog
priority: P1
estimate: XS
parent: EPIC-017
phase: v1
tags: [bug, recommendation, profile]
created: 2026-05-11
updated: 2026-05-11
---

## Description

`packages/profile/src/roleLibrary.ts::getRecommendation` currently does:

```ts
const needle = target_role.trim().toLowerCase();
return library.find((r) => r.slug.toLowerCase() === needle) ?? null;
```

It only matches slugs. But `profiles.target_role` stores whatever the onboarding agent (or its deterministic fallback) wrote — and the deterministic fallback writes the user's free-text reply like "Backend engineer" (the label, not "backend-engineer" the slug). Real-world result: every fallback-onboarded user gets `role: null` from `/api/recommendation`, which (via STORY-066) drives the redirect loop.

## Acceptance criteria

- [ ] Extend `getRecommendation` to match against `label` (case-insensitive, trimmed) in addition to `slug`. Slug match wins if both apply (defensive — labels can shadow slugs in pathological libraries).
- [ ] Add a Vitest case for label-form input: `getRecommendation(LIBRARY, "Backend engineer")` returns the `backend-engineer` role.
- [ ] Add a Vitest case for slug-form input that should still work: `getRecommendation(LIBRARY, "backend-engineer")` unchanged.
- [ ] Add a Vitest case for `"BACKEND ENGINEER"` (case-insensitive label).
- [ ] Add a Vitest case for unknown text: `getRecommendation(LIBRARY, "Quantum researcher")` still returns `null`.

## Dependencies

- Doesn't fix STORY-066 by itself (genuinely-unrecognized free text still loops), but reduces the population that hits the loop significantly.

## Notes

Optional stretch: add a fuzzy-match fallback (Levenshtein ≤ 2, or token-set similarity) so "Backend Eng." also resolves. Don't ship in this Story — keep the change tight.

## Activity log

- 2026-05-11 — created. Found during /option 1/ Chrome walkthrough.
55 changes: 55 additions & 0 deletions project/stories/STORY-068-db-seed-tracks-and-problems-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
id: STORY-068
title: Ship db:seed:tracks and db:seed:problems CLIs (fresh self-host gets no real content)
type: story
status: backlog
priority: P0
estimate: S
parent: EPIC-019
phase: v1
tags: [bug, seed, self-host, dev-experience]
created: 2026-05-11
updated: 2026-05-11
---

## Description

A fresh `pnpm --filter @learnpro/db db:migrate && db:seed && db:seed:concepts` cycle leaves the DB with:

- 206 concepts + 428 prerequisite edges (✓ from `db:seed:concepts`).
- 1 demo track (`python-arrays-101`) + 1 demo episode for a single demo user (✓ from `db:seed`).
- **Zero real tracks** (`python-fundamentals`, `typescript-fundamentals`) — the YAMLs in `packages/tracks/*.yaml` are never imported into the DB.
- **Zero real problems** — the 33 Python + 30 TypeScript YAMLs in `packages/problems/yaml/` are never imported.

`seedTrack` and `seedProblems` exist as exported functions in `@learnpro/tracks` and `@learnpro/problems`, but they're only called from Vitest integration tests. There's no production bin script + npm script wiring them.

Result: any fresh self-host setup lands the user in an empty app — onboarding completes, but `/api/recommendation` returns `recommended_tracks: []`, `/api/tutor/start` has no problems to assign, and the entire learner loop is unreachable.

Caught during the 2026-05-11 Chrome walkthrough.

## Acceptance criteria

- [ ] New `packages/db/bin/seed-tracks.ts`: loads both track YAMLs via `@learnpro/tracks` and calls `seedTrack` against the DB. Idempotent on re-run.
- [ ] New `packages/db/bin/seed-problems.ts`: loads all problems via `@learnpro/problems::loadProblems()` and calls `seedProblems`. Idempotent on re-run.
- [ ] Add `"db:seed:tracks": "tsx bin/seed-tracks.ts"` and `"db:seed:problems": "tsx bin/seed-problems.ts"` to `packages/db/package.json` scripts.
- [ ] Add `@learnpro/tracks` and `@learnpro/problems` as `workspace:*` dependencies of `@learnpro/db` if not already present. Mind the workspace dep cycle rules from the 2026-05-11 DECISIONS_LOG entry — `@learnpro/db` is upstream of both packages today, so adding back-edges would cycle. Probably the bin scripts belong in a NEW package or in `@learnpro/tracks`/`@learnpro/problems` instead.
- [ ] Add an `npm-run-all` style aggregated `db:bootstrap` script in the root package.json that runs migrate → seed → seed:concepts → seed:tracks → seed:problems in order. Document it in the self-host README.
- [ ] Vitest integration test that runs the full bootstrap chain against a transient Docker Postgres and asserts every expected row count is non-zero.
- [ ] Self-host README updated with the single-command bootstrap recipe.

## Dependencies

None. Pure additive work — existing seed scripts unchanged.

## Notes

The "bin scripts in `@learnpro/db` import `@learnpro/tracks`" approach creates a workspace dep cycle (because `@learnpro/tracks` already depends on `@learnpro/db`). Three options:
1. **Put the bin scripts in `@learnpro/tracks`** and `@learnpro/problems` themselves. Pro: no cycle. Con: split bootstrap UX across packages.
2. **Create a `@learnpro/bootstrap` package** that depends on all three (db, tracks, problems). Pro: clean. Con: new package overhead.
3. **Inline the seed logic** in `@learnpro/db/bin/` and don't import `@learnpro/tracks` — parse the YAMLs directly. Pro: no cycle, no new package. Con: duplicates parsing logic; drifts from `@learnpro/tracks` schema validation.

Pick (1) for v1 — least new infrastructure. `pnpm --filter @learnpro/tracks db:seed:tracks` matches the existing `pnpm --filter @learnpro/db db:seed:concepts` pattern.

## Activity log

- 2026-05-11 — created. Found during /option 1/ Chrome walkthrough.
Loading