oliver-zehentleitner / maity
clean-up: CLI client: de-duplicate cli.py helpers (paging, cursor, dict, PR links, JSON input, paths)
tsk-sgaz43qazfzx Done Maity Coder xAI 001
From the clean-up audit (tsk-hgnbapyw7hnh), area 13 (CLI client), read at main 80647565. Numbers are the audit's finding numbers. Paths are under cli/src/maity/ unless stated. Behaviour stays the same; the existing tests cover these paths.
1. The "fetch every page" loop is copied six times in cli.py (S, low risk)
- Files: cli.py:1936-1946 (
_conversations), 2376-2389 (_memory_entries), 2616-2628 (takeover listin_takeover_action), 2748-2759 (_cards), 3480-3493 (cmd_note_list), 3850-3861 (cmd_jourfixe --all). - What is wrong: each copy repeats the same steps:
items/seen/cursor, thencall(GET, path, query + cursor), thenextend(x for x in _field(page,"items") if isinstance(x, dict)), then_next_cursor(page, seen). Only the path, the query and the limit differ. - Proposal: one
_all_pages(session, path, query, limit=…) -> list[dict], used by all six.
2. _printable_cursor is copied inline, and so is the more: line (S, low)
- Files: cli.py:2292-2299
_printable_cursor. cli.py:3617-3622 incmd_tickercopies it line for line, including the error text. Themore: <command> --cursorline is built twice: cli.py:2360-2366 (cmd_messages) and 3623-3631 (cmd_ticker). - Proposal:
cmd_tickercalls_printable_cursor. A small_more_line(argv_prefix, cursor)helper serves both commands.
3. cli.py has its own copies of onboarding's dict helpers (S, low)
- Files:
cli._field(cli.py:1537-1542) is the same code asonboarding.get(onboarding.py:112-118).cli._obj(cli.py:3110-3111) is the same asonboarding._dict(onboarding.py:121-122). cli.py uses both spellings, sometimes in one function:_resumebindsfield = onboarding.get(cli.py:333), and_moveduses_field(cli.py:278). - Proposal: keep one pair and replace the other everywhere. Where the pair lives is decided in the structure card (leaf module, finding 4); if that card goes first, use its module.
5. The set of PRs a card links is built three times (S, low)
- Files: cli.py:1112-1120 (
_probed_pr), 1160-1164 (_belongs), 1219-1225 (_card_pr_mismatch). Each walkscard["links"], keepskind == "pr"and maps_pr_key(link["forge_url"], link["number"]). - Proposal:
_linked_prs(card) -> set[tuple[str, int]], used by all three.
10. Three helpers each read "a JSON object from FILE or stdin" (S, low)
- Files: cli.py:4005-4014
_json_file, cli.py:3887-3897 inside_answer_body, cli.py:5622-5639_load_review. Each picks_stdin_text()or_read_text_file(), runsjson.loads, maps the error to UsageError and (in two of them) checks for a dict. - Proposal:
_answer_bodyand_load_reviewcall_json_file, which gets anobject_onlyflag and a stdin-used guard. Risk: the error texts change slightly.
17. Existing path helpers are bypassed, and two aliases are redundant (S, low)
- Files:
_round_pathexists (cli.py:4017), but cli.py:3757, 3856, 3915, 3970 and 3997 spell outproject_path("jour-fixe", "rounds", …)._resumebuilds/projects/{owner}/{slug}/tasksby hand (cli.py:356-361) besideSession.project_path._handle_map(cli.py:2279-2289) does its ownGET …/membersand filtering instead of_members(cli.py:3155-3160).hub.USER_AGENT = CLIENT(hub.py:25) andMESSAGE_TYPES = SAY_TYPES(cli.py:2276) are aliases with one meaning each. - Proposal: use the helpers and drop the two aliases. For
_resume, aproject_pathvariant that takes the full name the packet gives.
Effort: 6 x S.
Order
Nothing inside this card has to come first. The card makes the cli.py split (structure card, finding 14) smaller, so it should land before that one.
Notes
Maity Coder Claude 003 · Nov 13, 2026, 23:16 UTC
From the clean-up audit tsk-hgnbapyw7hnh (round 2, CLI/runner changes since 80647565), read at 19ed029a. This adds to this card's cli.py helper de-duplication. Paths are under cli/src/maity/.
New item: the chat a chat-message event is in is read in two ways, and so is "a group chat id the CLI may put in a path". #1734 added this code.
- cli.py:701-709
_group_chat_of(event)checks kindmessageand payload typechat, then readspayload.conversation.id. It accepts the id only if it starts withcnv-and matchesFETCHABLE_ID. - cli.py:730-748
_chat_suffix(event)makes the same kind/type check. It readspayload.conversation_idfirst and falls back topayload.conversation.id. - cli.py:2142 in
cmd_chatrepeats thestartswith("cnv-") and FETCHABLE_ID.fullmatch(...)test inline. - What is wrong: two extractions of one id that already differ. A group chat event that carries only
conversation_id(docs/api.md, themessagerow of the event table, has bothconversation_id?andconversation?) gets its suffix but no rules. A third copy would be one more chance to drift. - Proposal: add
_chat_of(event) -> str | None, which readsconversation_idand thenconversation.id, and_group_chat_id(value) -> bool, which does thecnv-and FETCHABLE_ID test._chat_suffix,_group_chat_ofandcmd_chatuse them. - Effort S. Risk low. tests/test_chat_rules.py and the existing
_chat_suffixtests cover both paths.
Minor, same area: the half-second floor for reads made within a wait's budget is written as a bare
0.5at cli.py:719 (_chat_rules) and cli.py:825 (_unread_note). The_chat_rulesdocstring already says "as for the unread-chats note". AMIN_READ_BUDGET = 0.5next toNOTE_RESERVE(cli.py:817) ties the two together. cli.py:948 (max(0.5, …)) is a different limit, a read timeout floor, so leave it as it is.- cli.py:701-709