Skip to content

Set up code-shape-kit and fix its findings - #4

Open
emanuilo wants to merge 2 commits into
mainfrom
code-shape-kit
Open

emanuilo wants to merge 2 commits into
mainfrom
code-shape-kit

Conversation

@emanuilo

@emanuilo emanuilo commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Sets up code-shape-kit v0.3.0 and fixes everything it found. code-shape check passes with no baseline file.

Setup

  • code-shape-kit[tools] @ git+…@v0.3.0 is in the dev group, with the marker ; python_full_version >= '3.11.4'. The kit needs 3.11.4+ and the project still supports 3.10, so on 3.10 uv sync skips the kit (the 3.10 test job still passes).
  • code-shape init added [tool.shape] and [tool.importlinter] to pyproject.toml, plus .github/workflows/code-shape.yml (on Python 3.11) and .github/CODEOWNERS (owner: @emanuilo).
  • AGENTS.md (new) has the "Code shape" rules for agents.

Rules: init's guesses and what I changed

Setting init's guess Final
packages hometabs (src layout) kept
config_modules src/hometabs/config.py kept. __init__.py and auth.py also read the environment; that code now goes through config
layers bot -> images -> agent | models -> __main__ | auth | config | formatting | skills __main__ moved to the top (see below). Now __main__ -> bot -> images -> agent | models -> auth | config | formatting | skills
ignore_imports none none
banned_api only the default os.environ rules added adapters: claude_agent_sdk (agent.py, models.py), httpx (models.py), telegram (bot/*), PIL and pillow_heif (images.py), dotenv (config.py)
output_modules default (cli.py, __main__.py) kept: nothing prints
logger_objects none (loguru is built in) kept
limits, gates, skip, exclude, error_boundaries, whitelist defaults / none untouched

Why:

  • __main__ moved up. init put it at the bottom because it only imports the package root. It's the entry point, though, and hometabs/__init__.py imports bot, so __main__ -> hometabs -> bot broke the contract. Per designing-the-shape.md, entry points go on top. No ignore_imports were needed.
  • claude_agent_sdk is allowed in models.py as well as agent.py. models.py reads the SDK's EffortLevel literal. Moving that into agent.py would make models import agent, which breaks the agent | models independence, so I left both allowed.
  • Not part of the kit config: in the project's own [tool.ruff.lint] I added logger-objects = ["loguru.logger"]. With it, ruff accepts except Exception: logger.exception(...) (BLE001) the same way the kit does. Without it, the existing CI lint job would need noqa comments again.

Worth a look (not changed):

  • images sits above agent only because images.py imports the Image dataclass from agent.py. Image is plain domain data inside the SDK adapter, so it could move to a small domain module.

Findings

Before: 56 (all NEW, no baseline). After: 0.

The 56, by check:

Check Count
type-errors 12
inline-suppression 10
swallowed-errors 7
dead-code 5
cognitive-complexity 4
function-complexity 3
banned-api 3
boolean-flag-args 3
too-many-positional 2
file-lines 1
class-lines 1
too-many-returns 1
too-many-branches 1
nesting 1
too-many-locals 1
import-contracts 1

Code changes

  • bot.py (853 lines, class 616) is now the hometabs.bot package:
    • app.py: handlers and the turn queue. A TurnRequest replaces the 6-argument calls.
    • chats.py: per-chat agents, locks and saved sessions.
    • model_menu.py: /model and /effort texts, buttons and the choice itself.
    • media.py: image refs, album collection, downloads.
    • replies.py: sending text and images, typing, the "Working… step N" message.
    • stores.py: sessions.json and settings.json, with one shared JSON read/write.
  • Environment. Only config.py touches it now (log_level(), unset_env_vars(), Settings.anthropic_base_url). build_cli_env puts only the chosen credential in options.env. Before, it also copied the whole scrubbed os.environ, which added nothing: the SDK merges options.env over os.environ anyway. Telegram's InvalidToken is now handled in HometabsBot.run(), which raises ConfigError.
  • Agent.
    • summarize_tool_input uses a dispatch table and small helpers.
    • BrowsingAgent.ask hands each message to a _Turn collector.
    • effort is now passed as the SDK's EffortLevel type instead of using type: ignore. An unknown level is dropped with a warning.
  • Errors. Broad excepts are narrowed to the errors each place expects (EXIF reads, model cache, model fetch, SDK effort levels). Where a catch-all is deliberate (disconnect, progress callback, /stop, a failed turn), it now logs the traceback with logger.exception. All noqa comments and the one type: ignore are gone.
  • Types.
    • Telegram's optional effective_message / effective_chat are guarded.
    • Fixed the extract_images narrowing and the exif_transpose variable.
    • refresh(force=) and busy= are now keyword-only.
  • Dead code. Removed REPL_TOOL (used only in tests), BrowsingAgent.init_info and .connected (nothing reads them, including at runtime), and the "optional" pillow_heif import. pillow-heif is a required dependency, so HEIF_SUPPORTED and its pragma: no cover went too. models_cache.json now uses CACHE_FILENAME.
  • Tests are updated for the new modules. I added tests for unset_env_vars and anthropic_base_url. The build_cli_env tests now check that the env holds exactly the credential.
  • README layout, the architecture diagram labels and the CHANGELOG are updated.

Tests: 141 passed on Python 3.12 and on 3.10. ruff check and ruff format --check are clean, and uv lock --check is OK.

Needs a decision

Nothing is escalated: every finding was fixed in the code.

code-shape check --base origin/main reports one "rule loosened": config.py may now use os.environ. That's expected, since main had no config.

Before merging

  • In branch protection for main, turn on "Require review from Code Owners".
  • Make the Code shape / check status check required, so a rule change can't merge without a code owner.

🤖 Generated with Claude Code

Add code-shape-kit v0.3.0 as a dev dependency (Python 3.11.4+ only, as the
kit needs it), the [tool.shape] and import-linter config from code-shape
init, the CI gate, CODEOWNERS for the rule files and the agent rules in
AGENTS.md.

Changes to init's guesses: __main__ is the top layer (it is the entry
point), and adapters are declared for claude_agent_sdk, httpx, telegram,
PIL, pillow_heif and dotenv. ruff now treats loguru's logger like the
kit does, so a catch-all that logs the traceback passes BLE001.
- Split bot.py into the hometabs.bot package: app (handlers, turn queue),
  chats (per-chat agents and sessions), model_menu (/model, /effort),
  media (images and albums), replies (sending), stores (JSON state).
- Only config.py reads the environment: LOG_LEVEL, ANTHROPIC_BASE_URL and
  the credential scrub go through it, and options.env now carries only the
  chosen credential (the SDK merges it over the scrubbed process env).
- Telegram's InvalidToken is handled in the bot package.
- Break up summarize_tool_input and BrowsingAgent.ask; narrow the broad
  excepts, or log the traceback where a catch-all is deliberate.
- Fix type errors (effort level, image source, optional Telegram fields)
  without type: ignore; make boolean flags keyword-only.
- Remove dead code (REPL_TOOL, init_info, connected) and the optional
  pillow-heif import, which is a required dependency; use CACHE_FILENAME.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant