oliver-zehentleitner / maity

clean-up: core platform: the app shell's per-request asset walk, CLI and config repetition, app start-up wiring, small copies

Sign in to report

tsk-581bcbm72g95 Open

From the clean-up audit tsk-hgnbapyw7hnh (uncovered files, G1 core platform), read at 9dfd4b65. Paths are under hub/src/maity_hub/.

1. Slow path on a hot route: every app-shell answer re-reads and re-versions the dashboard

  • Files: shell.py:393-408 (ShellRoute.handle) calls shell_response (411-417) on the event loop, not in the thread pool.
  • What is wrong:
  • It reads index.html from disk on every call.
  • versioned (315-335) calls asset_versions (165-178), which walks brand/, css/, js/ and vendor/ (about 170 files) with one os.stat per file.
  • _refs_current stats each stylesheet's references again.
  • _inside_shell (384-386) builds the same document a second time for a stored public page.
  • Every dashboard navigation that loads the shell pays this, and a slow disk stalls every other request on the loop. app.py:648-649 already keeps the hashing off the event loop at start-up.
  • Proposal: build the composed shell once and cache it, keyed by index.html's mtime and size plus a cheap signature of the asset directories, or once per process. Both paths share the cache. At the least, run it in the thread pool.
  • Effort: S
  • Risk: low. The version query and the headers stay; test_app covers the shell.

2. Duplication: cli.py opens and checks the database the same way six times

  • Files: make_engine, then require_current_schema, then except SchemaNotCurrentError and except (SQLAlchemyError, ImportError), at cli.py:167-184, 254-263, 280-286, 330-336, 406-412 and 432-438. Some paths skip engine.dispose() on failure: _owner_create returns at 334-336, before its try/finally.
  • Proposal: one _current_engine(settings) helper, or a context manager that disposes.
  • Effort: S
  • Risk: low.

3. Debt: a type: ignore on a global monkeypatch of uvicorn

  • Files:
  • cli.py:643 replaces uvicorn.supervisors.multiprocess.Process for the whole process (# type: ignore[misc]).
  • _WorkerProcess.server (695-703) relies on uvicorn's private _server.
  • A uvicorn upgrade can break both without a test failing.
  • Proposal: set the patch around supervisor.run() and restore it afterwards. Add a test that fails when uvicorn's Process no longer has _server.
  • Effort: S
  • Risk: low.

4. Duplication and a silent truncation in config.py

  • Files:
  • Whole-number settings go through parse_number (a float) and int() (config.py:221-241): MAITY_WORKERS, MAITY_REPLICAS, MAITY_DB_POOL_SIZE, MAITY_DB_MAX_OVERFLOW, MAITY_DB_ASYNC_POOL_SIZE, MAITY_DB_POOL_RECYCLE, MAITY_AVATAR_DECODE_MEMORY_MB and MAITY_AVATAR_DECODES.

- So MAITY_WORKERS=2.9 starts 2 workers and says nothing.
- The public-page settings use parse_whole (493-500), which refuses such a value.

  • parse_alias_limit (518-526) is parse_whole without a default, and parse_limit (503-515) repeats the same digit check.
  • parse_skill_source (294-296) is a wrapper of parse_skill_url with one caller.
  • The https-URL shape check is written three times: parse_skill_url (299-308), parse_s3_endpoint (365-401), parse_join_url (630-663).
  • The host normalisation .lower().rstrip('.') is at 181, 418, 659 and mail/environment.py:71.
  • Proposal: whole numbers through parse_whole, parse_alias_limit included; inline parse_skill_source; one _https_origin(name, value) for the shared URL checks.
  • Effort: S
  • Risk: low. A fractional value that starts the hub today would refuse to start; say so in the changelog.

5. Design: the app's start-up wiring is written by hand

  • Files:
  • The lifespan (app.py:338-448) starts 21 background objects one by one and stops them by hand in reverse (416-448).

- A new worker needs edits in both lists.
- A start that raises half-way leaves the started ones running.

  • app.py imports serving inside three functions (175, 192, 290), but serving.py imports no app module at load time (serving.py:12-22; it imports app only inside app(), at 184).
  • Proposal: keep the started workers in a list or a contextlib.ExitStack and stop them in reverse in a finally. Import serving at the top.
  • Effort: S
  • Risk: low.

6. Duplication: the load-test handle pattern twice

  • Files: loadtest.py:227 HANDLE = re.compile(r'lt-[0-9]+') repeats paths.py:35 LOADTEST_HANDLE; the docstrings say they mean the same accounts.
  • Proposal: use paths.LOADTEST_HANDLE.
  • Effort: S
  • Risk: none.

7. Test debt: a skip that can never run

  • Files: hub/tests/test_catalog.py:35-36 skips a language whose words file is missing. test_catalog.py:27-29 already requires every interface language to have one, so the skip never runs; if it did, it would hide a missing file.
  • Proposal: drop the skip.
  • Effort: S
  • Risk: none.

Covered elsewhere

  • The copied background Worker class that item 5 starts: tsk-dzpzpn5xrm5j item 1. That card says the start and stop order in app.py must stay the same; item 5 keeps it.

Effort sum: 7 x S = 7 points.

Notes

  1. Maity Coder Claude 003 · Nov 20, 2026, 17:04 UTC

    Audit round 3b (read at 80fef076): lines moved. ShellRoute.handle is hub/src/maity_hub/shell.py:463-486, shell_response :489-497. The CLI engine-open copies are now seven: hub/src/maity_hub/cli.py:180-197, 267-276, 293-299, 346-352, 420-426, 446-452, 532-545; _copy_to_s3, _loadtest_purge and _loadtest_seed never dispose their engine.