A doctor check is a function in packages/studyloop/src/studyloop/doctor/ that takes no arguments,
touches nothing, and returns a list of CheckResult rows. It is wired into the one registry built
by _get_registry() in packages/studyloop/src/studyloop/cli/_doctor.py, and its proof is a unit
test that fakes the thing being checked — a missing import, an absent binary — not the real
machine.
- Write
check_<thing>() -> list[CheckResult]in thedoctor/module for its category. - Register it in
_get_registry()under one of the categoriesCheckResultaccepts. - Say what is true and what to do; set
fix_auto=Trueonly if--fixcan actually do it. - Test the check with the lookup faked, and the command with the registry faked.
- Run
studyloop doctor --helpand the doctor test files; the rows are the JSON contract.
packages/studyloop/src/studyloop/doctor/models.py—CheckResult(category, name, status, message, *, fix_command=None, fix_note=None, fix_auto=False), a frozen dataclass.fix_commandis an exact copy-pasteable command and nothing else;fix_noteis prose; both are keyword-only, andfix_hintis a read-only property joining them (<command> (<note>)) for readers that predate the split.statusmust be one ofpass warn fail info;categoryone ofcore database config agents deps voice updates harness(VALID_STATUSES,VALID_CATEGORIES); anything else raises at construction.to_dict()is the shapestudyloop doctor --jsonprints (it carriesfix_command,fix_noteand the derivedfix_hint), so renaming a field is a contract change.packages/studyloop/src/studyloop/doctor/__init__.py—CheckerRegistry:register(category)andrun_all()/run_category(). A checker that raises becomes afailrow named after the function with the messageChecker crashed: …, so one bad check never hides the others.- One module per category:
core.py,database.py,config.py,agents.py,deps.py,harness.py,voice.py,updates.py,exporter.py,query_encoder.py, all underpackages/studyloop/src/studyloop/doctor/. packages/studyloop/src/studyloop/cli/_doctor.py—_get_registry()imports every check and registers it;_compute_exit_code()maps rows to0,1(anyfail, or awarnwithfix_auto=True) or2(acorefailthat cannot be auto-fixed);_apply_fixes()is the only code--fixruns, dispatched by category and name.updatesis a valid category but hidden from--categoryuntil a release exists.
Read check_optional_deps() in packages/studyloop/src/studyloop/doctor/deps.py (40 lines).
It walks a dict of optional imports, asks importlib.util.find_spec() and emits one row per
dependency: pass with no fix when present, info with the exact install command as
fix_command when absent, fix_auto=False both ways because installing is the learner's decision. It reads
no file, runs no process and has no branch that cannot be reached by faking one function.
uv run studyloop doctor --helpThree layers, each faking exactly what it needs and nothing more. The pattern is the cookbook's unit template: arrange the fake, call the function, assert the rows.
- The check:
packages/studyloop/tests/test_doctor_deps.pypatchesimportlib.util.find_specthree ways (all present, none, one) and asserts statuses byname.packages/studyloop/tests/test_doctor_models.pypins the dataclass contract. - The lookup seam: a check that needs a binary is tested by patching the lookup where the check
calls it —
patch("studyloop.doctor.harness.shutil.which", …)inpackages/studyloop/tests/test_harness_export.py,is_tmux_availableinpackages/studyloop/tests/test_doctor_core.py— or by writing an executable shim undertmp_pathand handing the check its path, as_fake_exporter()does inpackages/studyloop/tests/test_doctor_exporter.py. Never rely on what is on the developer'sPATH. - The command:
packages/studyloop/tests/test_cli_doctor.pyinvokesdoctorwithCliRunnerwhile_get_registryis patched to return canned rows, so the exit-code and--jsoncontracts are tested without running a single real check;packages/studyloop/tests/test_doctor_exit_code.pypins_compute_exit_codeand the exit-code row indocs/cli-reference.md.
uv run --group dev pytest packages/studyloop/tests/test_doctor_deps.py packages/studyloop/tests/test_doctor_models.py -q
uv run --group dev pytest packages/studyloop/tests/test_cli_doctor.py packages/studyloop/tests/test_doctor_exit_code.py -qpackages/studyloop/tests/test_doctor_integration.py runs the whole registry through the CLI
with STUDYLOOP_CONFIG pointed at a temp file and urllib.request.urlopen patched to fail, so
nothing reaches the network or the real configuration.
uv run --group dev pytest packages/studyloop/tests/test_doctor_*.py -qEvery doctor test runs in the default selection, so just test and just preflight include them
(F7). uv run studyloop doctor --help is executed by
packages/studyloop/tests/test_contributor_guide_drift.py, and every fenced studyloop doctor …
line on the site is resolved against the Click tree by packages/studyloop/tests/test_docs_drift.py
(F11), so a new --category value or option must be real before it is documented.
- No side effects: the check reads, probes and returns. Repairs live in
_apply_fixes()behind--fix, and a row withfix_auto=Truehas a matching branch there — otherwise the summary line promises a fix that does not exist. - Honest wording:
messagestates what was observed (session-export: not found on PATH);fix_commandis one command the learner can paste,fix_notethe sentence that says what it does or what to do when there is no command — never prose insidefix_command;warnis for degraded,failfor broken,infofor optional-and-absent — neverfailfor a missing optional extra. - One row per fact, named stably (
dep_fastapi,session_memory_executable_session-export): tests and the JSON consumers key onname. - The unit test fakes the lookup at the seam the check uses and covers present, absent and broken; the CLI test fakes the registry, not the world.
- The exit-code table in
docs/cli-reference.mdstill matches_compute_exit_code()if you changed when a row isfailorfix_auto.