Skip to content

fix(api): manage analyzer connection lifetime explicitly - #171

Merged
codeasier merged 1 commit into
mainfrom
fix/issue-165-analyzer-lifecycle
Oct 3, 2026
Merged

codeasier merged 1 commit into
mainfrom
fix/issue-165-analyzer-lifecycle

Conversation

@codeasier

Copy link
Copy Markdown
Owner

📝 Description

Implement the highest-priority connection-lifecycle portion of the v0.4.0 tracking issue.

  • Add idempotent SnapshotAnalyzer.close() and context-manager support. Normal and exceptional exits clean up; exceptions propagate. Business/discovery methods and context re-entry fail with RuntimeError("SnapshotAnalyzer is closed.") after close.
  • Internally created caches are owned; explicitly injected caches are borrowed. Closing one borrower preserves connections used by other analyzers. The external owner closes the shared cache.
  • Preserve public context_cache and explicit invalidate_context_cache() cleanup, including after analyzer close. Cache invalidation/closure alone still permits an open analyzer to repopulate its cache.
  • Close a persistent Context immediately if constructor schema validation fails before cache publication.
  • Add ownership-aware service cleanup and six lines of CLI finally cleanup for query, overview and peak-memory report.
  • Add real SQLite handle-lifetime regressions and targeted fixture cleanup; document the API contract in English and Chinese.

🔗 Related Issue

Refs #165. Partially addresses #165; this PR must not close the umbrella issue.

🧪 Testing

  • Tests added/updated
  • Manual testing completed (verification is automated against real SQLite connections and CLI invocations)

Local environment: macOS, Python 3.13.11 via /Users/test1/liuyekang/miniconda3/bin/python; isolated temporary HOME, worktree src first in PYTHONPATH with existing site packages retained. Confirmed package import resolves to this worktree; no editable reinstall.

Check Result
python -m pytest tests/test_fixture_provenance.py -q before deserializing suites 7 passed
Focused lifecycle/cache/performance tests (initial run) 39 passed
API/context/CLI-API contract and context-cache/query/overview/report service suites 97 passed
python -m pytest -q 1060 passed, 1 existing xfailed, 22.29s
python -m pytest tests/test_analyzer_lifecycle.py tests/test_snapshot_analyzer_cache.py tests/test_query_cache_perf.py tests/test_api.py -q -W error::ResourceWarning -W error::pytest.PytestUnraisableExceptionWarning 67 passed
python -m ruff check . passed
python -m black --check . passed, 156 files unchanged
python -m basedpyright --pythonpath /Users/test1/liuyekang/miniconda3/bin/python --outputjson 0 errors, 831 informational warnings
git diff --check passed

Regression coverage includes real handles becoming unusable after close, multi-database cleanup, query/overview reuse, idempotency, no connection reopening after close, body/template/SQL/schema/overview failures, external shared caches on normal and exceptional exit, public cache compatibility, service reuse, CLI success/error cleanup, and Python 3.13 garbage-collection ResourceWarning checks.

✅ Checklist

  • Code follows project guidelines
  • Documentation updated (if applicable)
  • No new warnings introduced (basedpyright remains at the issue baseline of 831; installed pytest-asyncio emits its existing unset-loop-scope deprecation warning)
  • Changes tested locally

Snapshot Runtime Provenance

Not applicable: no files under src/pt_snap_cli/snapshot/** changed.

  • Snapshot provenance evaluation completed when snapshot runtime files change
    Snapshot provenance decision: no-update
    Snapshot provenance no-update reason: Changes are limited to analyzer/context/service lifetime, CLI cleanup, tests and API documentation; the snapshot runtime is unchanged.

📸 Screenshots (if applicable)

Not applicable.

📋 Additional Notes

  • Base: origin/main at c85a357; isolated worktree .worktrees/issue-165-resolve.
  • Explicit lifetime management is required; there is no destructor-based cleanup guarantee. Borrowed caches remain the caller’s responsibility.
  • ResourceWarning-as-error validation is targeted to the changed lifecycle/API/cache suites, not a claim that all historical fixtures across the repository are warning-free. Local testing covered Python 3.13; CI supplies the supported-version matrix.
  • Remaining 项目分析:v0.4.0 架构、质量验证与改进优先级 #165 work stays separate: uncovered replay/runtime boundary cases and broader fixture cleanup, coverage regression thresholds, PEP 604 SQLite mapping (existing xfail), gradual Any/Unknown type-warning reduction, and CLI module/output-adapter refactoring.
  • No skill content, query templates, coverage settings or snapshot-runtime files are changed.

@codeasier
codeasier merged commit 989b7ac into main Oct 3, 2026
6 checks passed
@codeasier
codeasier deleted the fix/issue-165-analyzer-lifecycle branch October 3, 2026 19:18
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