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
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-427andktw.py:359-370. Both calloutbound.readwith the state cap andAccept: application/json, map the refusal through_REFUSALS, check the status,parse,digestand keep the file withRAW.keep_state. - Proposal: one
ktw.fetch_state(url) -> (data, Raw)(orread_statealso returning theRaw);ktw_readscalls it and keeps only the bodies branch. - Effort: S. Risk: low.
2. ktw_reads reaches into private names (design)
- Files:
ktw._nowatktw_reads.py:164,:168,:181,:186,:455,:476,:512;ktw._REFUSALSat:416;threading.BoundedSemaphore._valueat:258. - Proposal: make
nowand the refusal map public inktw(item 1 removes the_REFUSALSuse); count the reads in flight per project in_slotsitself 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) carriesstatus,code,message,detailsand is turned into anApiErrorone to one atroutes/keep_the_why.py:129-132. The 409ktw_not_configuredwith the same message:routes/keep_the_why.py:43,:85,ktw_reads.py:367. The 503ktw_unavailable:routes/keep_the_why.py:35,:52,:117, andktw_reads._unavailable(:322). - Proposal: raise
ApiErrorfromktw_reads(errors.pyis no route module), witherrors.ktw_not_configured()anderrors.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
walkordigest) leaves no trace; the project view just stays stale. - Proposal: catch the expected
Busyand forge errors quietly and log anything else withlog.warning(..., exc_info=True). - Effort: S. Risk: none.
5. Small copies in ktw (duplication)
- Files:
RawStore.mark(ktw.py:443-449) andRawStore.put(:470-474) write the sameread_atbookkeeping;ktw_reads.require_covered(ktw_reads.py:237-243) only callsreader.require_covered, and its one caller (routes/keep_the_why.py:103) can call that directly. - Proposal:
putcalls 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) runsmember()for the team,member()andwho()forby,who()forconfirmed_by, and two more fordeleted.by: four to six queries per entry._compact(:544-568) calls it for every entry of the packet's three stores untilPACKET_TEXTbytes each, so short entries cost thousands of queries perPOST /me/resume(also viaroutes/briefing_view.py:90).GET …/memory(routes/memory.py:137-187) does it for up to 200 rows, and withqreads every row of the selection first. - Proposal: a batch
entry_views(conn, rows)that loads the memberships and handles once, asviews.principal_idsdoes;_compactand 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) andsoul_versions(:195-212) each assemblemarkdown,version,by,confirmed_byand the time by hand (updated_atin two,atin one). - Proposal: one
_soul_fields(conn, row); the three add their own time field. - Effort: S. Risk: none.
Covered elsewhere
ktw_readscalling private forge methods, and_visibilityduplicatingForge.repository_public: tsk-e77g3nhqqnex (A3-2, A3-3).If-None-Matchinroutes/keep_the_why._matches: tsk-k25qqhygend8..replace("Z", "+00:00")atktw.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.