python-module-refactor
Analyze top-level Python symbol dependencies and perform explicitly selected, AST-validated extractions or function replacements. Use for mechanical refactors after domain boundaries and compatibility requirements are decided.
- License 许可
- AGPL-3.0
- 所属仓库
- ismigar/Gnosi
- 最近更新
- 2026-11-15
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- python-module-refactor
- description
- Analyze top-level Python symbol dependencies and perform explicitly selected, AST-validated extractions or function replacements. Use for mechanical refactors after domain boundaries and compatibility requirements are decided.
02 资源文件 4
- 脚本scripts/analyze_symbol_graph.py7.1 KB
- 脚本scripts/extract_top_level_symbols.py6.0 KB
- 脚本scripts/replace_top_level_function.py2.4 KB
- 脚本tests/test_tools.py5.7 KB
03 SKILL.md 正文
SKILL: Python module refactor
ID: GNOSI-PYTHON-MODULE-REFACTOR-20260827 Status: ACTIVE
Purpose
Split large Python modules safely by first producing a deterministic top-level symbol dependency graph and then replacing selected top-level functions with AST-validated compatibility adapters. The tools support mechanical movement; they do not decide domain ownership or change runtime contracts.
Tools
scripts/analyze_symbol_graph.pyreads one Python module and writes a JSON graph ingnosi-python-symbol-graph-v1format.scripts/replace_top_level_function.pyreplaces exactly one named top-level sync or async function, including all its decorators.scripts/extract_top_level_symbols.pymoves explicitly named top-level functions, classes or simple assignments to a new module and inserts an explicit compatibility import at the first removed extent.
These tools require explicit source and output or replacement paths. Generated
reports and temporary replacement snippets belong under .tmp/ and must not be
committed.
Commands
Run from the Gnosi repository root:
uv run python pipeline/skills/python_module_refactor/scripts/analyze_symbol_graph.py \
backend/api/vault_routes.py --output .tmp/vault-symbol-graph.json
uv run python pipeline/skills/python_module_refactor/scripts/replace_top_level_function.py \
path/to/module.py selected_function .tmp/function-wrapper.py
Procedure
- Parse the original source successfully and create the symbol graph.
- Validate the report format and inspect
symbols, dependency edges, route decorators, and cyclic components before choosing a boundary. - Move state behind a single explicit owner and inject narrow ports for legacy collaborators before moving handlers.
- Prepare a replacement file that defines exactly the original function name.
- Run the replacer. It validates both the replacement and final module with Python's AST before writing.
- Run the tool a second time when useful; an identical replacement must be a no-op.
- Validate Ruff, strict mypy, focused tests, source guardrails, and the exact OpenAPI artifact. Mechanical equivalence is not sufficient without runtime contract tests.
For a multi-symbol extraction, prepare a destination preamble and source-import
snippet under .tmp/, then pass both files explicitly to
extract_top_level_symbols.py. Run it twice and require the second call to be a
no-op before deleting the temporary snippets.
Restrictions and edge cases
- The graph contains top-level definitions only. Dynamic imports, attribute calls, closures, runtime registration, and cross-module dependencies need separate inspection.
- The report key is
symbols, notcomponents. Cycles are reported only incyclic_components; an empty list is valid. - The replacement must contain exactly one top-level function with the requested name. Helper definitions belong in their destination module.
- Multi-symbol extraction accepts only a new destination. A mixture of present and already-moved symbols is rejected as an unsafe partial state.
- Decorators are part of the replacement extent. Preserve their order because repeated FastAPI decorators are applied bottom-up and can affect route order.
- Never infer safety from
__globals__identity tests. Verify public exports, route registrations, signatures, callbacks, and observable behavior instead. - Preserve lazy callback resolution when tests or integrations monkeypatch a legacy facade symbol after import.
- Note: do not remove an otherwise-unused
typingname from a compatibility facade when its historical__all__exported that name. Downstream star imports observe the export set even when production annotations no longer use it. Preserve the legacy symbol only at the facade boundary and verify the exact__all__; do not use it to weaken the typed domain implementation. - Pydantic response annotations and docstrings can change OpenAPI. Compare the complete generated artifact, not only route counts.
- When extracting an existing Pydantic response model, preserve its class docstring verbatim. Pydantic publishes that text as the schema description, so omitting it creates contract drift even when every field is unchanged.
- Do not move mutable module state by copying aliases. Introduce one canonical owner and keep compatibility accessors thin.
- Keep cleanup recoverable. Remove temporary worktrees and files only after all reports and contract tests pass.
- In zsh validation wrappers, do not assign an exit code to
status, because it is a read-only shell parameter and prevents the intended command from running. Use a task-specific name such astest_exitand propagate it only after printing the bounded log tail. - In restricted worktrees, do not let
uvfall back to the user-global cache, because even an offline check may inspect paths outside the permitted workspace. SetUV_CACHE_DIRto a worktree-local ignored directory together withUV_NO_SYNC=1andUV_OFFLINE=1. - If the packaged
uvruntime panics in macOS sandboxing while opening the System Configuration Dynamic Store, do not retry dependency resolution or enable network access. Reuse the repository's already-locked virtual environment executables directly and keep all validation offline. - When replacing an untyped JSON route family with forward-compatible Pydantic
responses, apply
response_model_exclude_unset=Trueconsistently to every route in the family. Do not rely on today's required fields for selected endpoints, because later optional compatibility fields must not appear as syntheticnullkeys. - A successful
ruff checkdoes not prove formatting compliance. Runruff format --checkover the exact changed Python paths before wider gates, and format only those reviewed paths when it reports drift. - Do not use
mypy --strict backendas shorthand whenbackend/testsis inside that tree and the repository's production gate intentionally excludes legacy tests. Resolve and run the canonical production-source file set, then check any newly added typed contract tests explicitly; otherwise thousands of unrelated test annotations obscure the changed-source result. - When a source guardrail invokes Ruff as a subprocess, selecting the locked
virtual environment's Python executable is not enough. Prepend that virtual
environment's
bindirectory toPATH; otherwise the guardrail reports that Ruff is unavailable even though direct Ruff checks pass. - Do not invoke a pnpm workspace script from a fresh linked worktree before
checking for its local
node_modules. Pnpm may start a complete workspace install and attempt registry access. For deterministic generated-client work, execute the already-installed generator binary from the canonical checkout against the reviewed worktree's OpenAPI input and output paths.
Verification
uv run ruff check pipeline/skills/python_module_refactoruv run mypy --strict pipeline/skills/python_module_refactor/scriptsuv run pytest -q pipeline/skills/python_module_refactor/tests- Run the project-specific tests, architecture guardrails, and deterministic OpenAPI comparison documented by the active refactor directive.
04 同仓库更多 Skills
zotero-sync-compatibility
Preserve the historical Zotero reference-table configuration path after Gnosi replaced bidirectional Zotero synchronization with its native reference manager.
zotero-schema
Generate Gnosi's Python and TypeScript item-type constants from the repository-pinned Zotero schema, or perform an explicitly requested pinned schema refresh. Use for mapping or generated-constant maintenance, not runtime downloads.
vault-core
Preserve Gnosi vault identifiers, internal links and active-vault context during file or metadata changes. Use for vault integrity work, not blanket normalization or cleanup of user documents.
translate-row
Translate configured table-row fields and Markdown into language children, updating existing translations in place. Use for the explicit translate-row action or maintenance of its provider adapters.
translate-page
Translate a Vault page title and Markdown body into language children, updating existing translations and preserving Gnosi directives. Use for the explicit translate-page action or Markdown segmentation maintenance.
technical-documentation
Generate and validate Gnosi's engineering portal from reviewed guides and deterministic source catalogs. Use for source-traceable documentation updates and pre-PR gates; never translate generated catalogs with a model.