oliver-zehentleitner / maity

clean-up: CLI client: de-duplicate cli.py helpers (paging, cursor, dict, PR links, JSON input, paths)

Sign in to report

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 list in _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, then call(GET, path, query + cursor), then extend(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 in cmd_ticker copies it line for line, including the error text. The more: <command> --cursor line is built twice: cli.py:2360-2366 (cmd_messages) and 3623-3631 (cmd_ticker).
  • Proposal: cmd_ticker calls _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 as onboarding.get (onboarding.py:112-118). cli._obj (cli.py:3110-3111) is the same as onboarding._dict (onboarding.py:121-122). cli.py uses both spellings, sometimes in one function: _resume binds field = onboarding.get (cli.py:333), and _moved uses _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 walks card["links"], keeps kind == "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(), runs json.loads, maps the error to UsageError and (in two of them) checks for a dict.
  • Proposal: _answer_body and _load_review call _json_file, which gets an object_only flag 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_path exists (cli.py:4017), but cli.py:3757, 3856, 3915, 3970 and 3997 spell out project_path("jour-fixe", "rounds", …). _resume builds /projects/{owner}/{slug}/tasks by hand (cli.py:356-361) beside Session.project_path. _handle_map (cli.py:2279-2289) does its own GET …/members and filtering instead of _members (cli.py:3155-3160). hub.USER_AGENT = CLIENT (hub.py:25) and MESSAGE_TYPES = SAY_TYPES (cli.py:2276) are aliases with one meaning each.
  • Proposal: use the helpers and drop the two aliases. For _resume, a project_path variant 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

  1. 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 kind message and payload type chat, then reads payload.conversation.id. It accepts the id only if it starts with cnv- and matches FETCHABLE_ID.
    • cli.py:730-748 _chat_suffix(event) makes the same kind/type check. It reads payload.conversation_id first and falls back to payload.conversation.id.
    • cli.py:2142 in cmd_chat repeats the startswith("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, the message row of the event table, has both conversation_id? and conversation?) gets its suffix but no rules. A third copy would be one more chance to drift.
    • Proposal: add _chat_of(event) -> str | None, which reads conversation_id and then conversation.id, and _group_chat_id(value) -> bool, which does the cnv- and FETCHABLE_ID test. _chat_suffix, _group_chat_of and cmd_chat use them.
    • Effort S. Risk low. tests/test_chat_rules.py and the existing _chat_suffix tests cover both paths.

    Minor, same area: the half-second floor for reads made within a wait's budget is written as a bare 0.5 at cli.py:719 (_chat_rules) and cli.py:825 (_unread_note). The _chat_rules docstring already says "as for the unread-chats note". A MIN_READ_BUDGET = 0.5 next to NOTE_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.