Skip to content

Contributor Guide

Welcome to the Smart AutoSorter AI Pro project! This page covers local setup and the architectural rules contributors must follow. Checks, baselines and pull request conventions are described in CONTRIBUTING.md at the repository root.

Development Setup

To set up your local development environment and sync every dependency group (dev tools, docs, build), run:

Contributor Onboarding Workflow

uv sync --all-groups
uv run python scripts/install_git_hooks.py

scripts/install_git_hooks.py installs the repository's offline pre-commit hook into .git/hooks/pre-commit, or skips cleanly with guidance when Git is configured with a custom core.hooksPath.

Add --extra ml to also install the optional OCR, audio and embedding libraries.

Once the setup is complete, verify your environment by running the test suite:

uv run pytest -m "not slow"

CLI Demo Mode

To help new developers see the end-to-end data flow without reading the source, the app has a demo mode. It runs on its own and needs no input.

Running the Demo

The demo seeds sample documents into sandbox/demo_workspace whenever that folder is missing or empty (leaving any existing user files untouched), executes real file moves into topic subfolders, prints the resulting folder tree to stdout (or structured JSON when --json is passed), and records an undoable session in the app store so uv run smart-autosorter undo --latest restores the files.

To run the demo and undo it, use:

uv run smart-autosorter --demo
uv run smart-autosorter undo --latest

Automated Sample Corpus Utility

The demo builds its data with generate_sample_corpus(base_dir, overwrite=False) from app/core/sample_corpus.py, which app/demo.py, --preset demo, and the TUI's sample-dataset key all use. It writes categorized sample documents (finance, technology, health, legal, personal, and HR texts plus an empty file), exceeding the clustering engine's three-document minimum. Read it to see how test data is assembled.

Centralized Backend System Utilities

To prevent redundant patterns, platform-specific bugs, and visual/functional defects across application scopes, we consolidate all system packaging, path character validation, and session database directory setup / encryption key lookups.

1. Unified Helpers Overview

Shared path and packaging helpers live in app.core.path_utils, cryptography in app.core.crypto and secret scrubbing in app.core.text_utils. Don't write your own platform or frozen-bundle checks.

  • is_packaged() -> bool: Returns True if running inside a frozen bundle (e.g., PyInstaller).
  • get_base_path(caller_file_path: str | None = None) -> str: Standard application base path resolver. Always pass __file__ when calling from another module so that mocked environments are correctly handled.
  • validate_target_path(target_path: str, keyword: str | None = None) -> None: Standard target path validation for illegal characters, absolute paths, or traversal segments.
  • is_subpath_or_equal(child: str, parent: str) -> bool (in app.core.mover, not path_utils): Cross-platform check that a candidate path is inside a parent directory.
  • setup_session_directory(session_id: str | None = None) -> tuple[str, Path]: Creates the session directory under $AUTOSORTER_APP_DIR/sessions, or in the OS temp folder when that variable is unset.
  • resolve_db_crypto(db_path: Path | str) -> SessionCrypto: Standard cached lookup function for session encryption keys and database decrypters (reusing resolved keys across worker threads and honoring SORTIFY_KEYS_DIR for isolated sandboxes).
  • CryptoManager: Centralized facade (app.core.crypto) providing static methods for key derivation (derive_db_hash, derive_keyring_account, derive_isolated_key_path) and envelope encryption (encrypt_proxy_setting, decrypt_proxy_setting).
  • sanitize_secret_patterns(text: str, replacement: str = "") -> str: Centralized secret scrubbing delegate (app.core.text_utils) that redacts API keys, JWTs, Bearer tokens, private keys, and high-entropy secret payloads across text streams and logging filters.

2. Architectural Decisions & Rules

  • No Direct sys.frozen Checks: Never use getattr(sys, "frozen", False) in modules. Use is_packaged() instead.
  • Consolidated Illegal Characters: All paths and filenames must validate against ILLEGAL_PATH_CHARS_SET or ILLEGAL_NAME_CHARS_SET inside app/core/path_utils.py.
  • Standard Key Resolution: Any database connection or database initialization must obtain its SessionCrypto instance via resolve_db_crypto(db_path). Do not hardcode standard secret.key filenames or paths inside database modules.
  • Centralized Cryptography & Secret Scrubbing: Use CryptoManager static methods for key derivation and proxy envelope encryption. Route diagnostic and log scrubbing through sanitize_secret_patterns().

  • Schema Changes Are Numbered Migrations: Never change a table with ALTER TABLE inside try/except, or with CREATE TABLE IF NOT EXISTS on every open. Add a Migration(version, name, apply) to the end of that database's list (for the main database, MAIN_DB_MIGRATIONS in app/core/db.py). app.core.migrations.migrate runs each one once, in a transaction, and refuses a database newer than the code. Never edit a migration after it ships (ADR 0008).