<!-- Generated by scripts/generate_ai_collab_docs.py. Edit metadata in that script. -->

# Backend Service: learn

This file adds local guidance for `src/api/flaskr/service/learn`. Read the
repository root and `src/api/AGENTS.md` first, then use this file for
module-specific ownership and risk points.

## Scope

- This service owns learning-session orchestration, SSE streaming,
  ask-provider adapters, listen-mode assembly, and runtime conversation state.

- Primary checked-in entry files today:
  `src/api/flaskr/service/learn/routes.py`,
  `src/api/flaskr/service/learn/learn_funcs.py`,
  `src/api/flaskr/service/learn/runscript_v2.py`,
  `src/api/flaskr/service/learn/listen_element_run_stream.py`,
  `src/api/flaskr/service/learn/ask_provider_adapters/registry.py`

- Keep behavior in this module unless the concern is clearly cross-service and
  shared by multiple backend domains.

- Preferred pytest location: `src/api/tests/service/learn/` already exists and
  should remain the first stop.

## Do

- Start from the local entry files and preserve how this service already
  structures helpers, models, routes, and DTO-like helpers.

- Preserve the SSE event flow and element-oriented runtime state used by
  study, preview, and listen-mode frontends.

- Keep ask-provider integration behind adapter and registry layers so
  provider-specific branches do not leak into routes.

- Treat listen-mode and preview helpers as part of the same runtime state
  machine, and update them together when payload contracts move.

## Avoid

- Do not introduce a second streaming protocol or event naming scheme without
  updating every consumer and compatibility path.

- Do not bypass adapter normalization when wiring new provider or workflow
  integrations into ask flows.

- Do not change runtime DTO enums or element semantics without coordinated
  frontend and test updates.

- Avoid scattering service-specific rules upward into root docs when the
  behavior only matters inside this module.

## Commands

- `rg -n "learn" src/api/flaskr/service src/api/tests` to locate all backend
  call sites and tests before editing.

- `cd src/api && pytest tests/service/learn/ -q` for the narrowest focused
  regression pass while iterating on this module.

- `cd src/api && FLASK_APP=app.py flask db migrate -m "message"` and review
  the generated revision if schema or indexed business-key changes are made.

- `cd src/api && pytest -q` when a change touches shared DTOs, auth, streaming
  contracts, or other services in addition to this module.

## Tests

- Add or update focused tests around `src/api/tests/service/learn/` for the
  happy path and the highest-risk validation, permission, or failure case.

- Expand coverage when payload shapes, error codes, or shared helper contracts
  used outside this module change.

- Review persistence-related changes manually for indexing, business-key
  semantics, and migration output whenever models move.

- Document any skipped provider, network, or credential-dependent checks in
  the final task summary.

## Related Skills

- `src/api/SKILL.md`

- `SKILL.md`

- Create a new backend skill under `src/api/skills/<topic>/SKILL.md` when this
  module grows a repeatable multi-step debugging workflow.
