oliver-zehentleitner / maity

clean-up: G5 Keep the Why and memory: read_state twice, ktw_reads on ktw's privates, a silent except, and per-entry queries in the resume packet

Sign in to report

tsk-fm045j1zm5k0 Done Maity KTW Integrator

From the clean-up audit tsk-hgnbapyw7hnh (uncovered files, G5 Jour fixe, ticker, swarm, Keep the Why and memory), read at 9dfd4b65. Line numbers are at that SHA. Paths are under hub/src/maity_hub/.

1. ktw_reads._read repeats ktw.read_state (duplication)

  • Files: ktw_reads.py:411-427 and ktw.py:359-370. Both call outbound.read with the state cap and Accept: application/json, map the refusal through _REFUSALS, check the status, parse, digest and keep the file with RAW.keep_state.
  • Proposal: one ktw.fetch_state(url) -> (data, Raw) (or read_state also returning the Raw); ktw_reads calls it and keeps only the bodies branch.
  • Effort: S. Risk: low.

2. ktw_reads reaches into private names (design)

  • Files: ktw._now at ktw_reads.py:164, :168, :181, :186, :455, :476, :512; ktw._REFUSALS at :416; threading.BoundedSemaphore._value at :258.
  • Proposal: make now and the refusal map public in ktw (item 1 removes the _REFUSALS use); count the reads in flight per project in _slots itself instead of reading the semaphore's private counter.
  • Effort: S. Risk: none.

3. ktw_reads.Refused mirrors ApiError, and the KTW errors are written seven times (duplication)

  • Files: ktw_reads.Refused (ktw_reads.py, the class at the top) carries status, code, message, details and is turned into an ApiError one to one at routes/keep_the_why.py:129-132. The 409 ktw_not_configured with the same message: routes/keep_the_why.py:43, :85, ktw_reads.py:367. The 503 ktw_unavailable: routes/keep_the_why.py:35, :52, :117, and ktw_reads._unavailable (:322).
  • Proposal: raise ApiError from ktw_reads (errors.py is no route module), with errors.ktw_not_configured() and errors.ktw_unavailable(reason, **details).
  • Effort: S. Risk: low (the error bodies are pinned by tests).

4. A broad except that logs nothing (debt)

  • Files: ktw.py:966-967 (_refresh_in_background: except Exception: pass). _one_export (:638-640) logs the same kind of failure.
  • What is wrong: a background refresh that keeps failing (a bug in walk or digest) leaves no trace; the project view just stays stale.
  • Proposal: catch the expected Busy and forge errors quietly and log anything else with log.warning(..., exc_info=True).
  • Effort: S. Risk: none.

5. Small copies in ktw (duplication)

  • Files: RawStore.mark (ktw.py:443-449) and RawStore.put (:470-474) write the same read_at bookkeeping; ktw_reads.require_covered (ktw_reads.py:237-243) only calls reader.require_covered, and its one caller (routes/keep_the_why.py:103) can call that directly.
  • Proposal: put calls a shared _touch; drop the wrapper.
  • Effort: S. Risk: none.

6. Per-entry queries in the memory views (slow path on resume)

  • Files: memory.entry_view (memory.py:231-259) runs member() for the team, member() and who() for by, who() for confirmed_by, and two more for deleted.by: four to six queries per entry. _compact (:544-568) calls it for every entry of the packet's three stores until PACKET_TEXT bytes each, so short entries cost thousands of queries per POST /me/resume (also via routes/briefing_view.py:90). GET …/memory (routes/memory.py:137-187) does it for up to 200 rows, and with q reads every row of the selection first.
  • Proposal: a batch entry_views(conn, rows) that loads the memberships and handles once, as views.principal_ids does; _compact and the listing use it.
  • Effort: M. Risk: low.

7. The soul's shape is built three times (duplication)

  • Files: soul_view (memory.py:56-68), version_view (:71-87) and soul_versions (:195-212) each assemble markdown, version, by, confirmed_by and the time by hand (updated_at in two, at in one).
  • Proposal: one _soul_fields(conn, row); the three add their own time field.
  • Effort: S. Risk: none.

Covered elsewhere

  • ktw_reads calling private forge methods, and _visibility duplicating Forge.repository_public: tsk-e77g3nhqqnex (A3-2, A3-3).
  • If-None-Match in routes/keep_the_why._matches: tsk-k25qqhygend8.
  • .replace("Z", "+00:00") at ktw.py:260: tsk-kfpz2m8we8f1.
  • memory.who (handle lookup): tsk-ee3z4rakddq9. memory.owner_of (agent-to-owner): tsk-9tj3p6ca30m6.
  • Searched: ktw_reads, ktw.py, Keep the Why clean-up, entry_view, memory.py.

Effort sum: M + 6S, about 8 points.