From 3a435d0120f1f9329727691080f133638399766d Mon Sep 17 00:00:00 2001 From: codeasier Date: Sat, 19 Sep 2026 20:51:39 +0800 Subject: [PATCH 1/2] fix(types): check snapshot runtime at the basedpyright error gate Phase 3 of #122 tightens first-party snapshot annotations so the snapshot-only executionEnvironments downgrade list can be removed instead of widened. Replay, import, and split behavior is unchanged. Fixes #122 --- AGENTS.md | 7 +- CHANGELOG.md | 4 ++ pyproject.toml | 15 ---- src/pt_snap_cli/snapshot/PROVENANCE.md | 9 ++- src/pt_snap_cli/snapshot/base/entities.py | 71 +++++++++++-------- .../snapshot/simulate/allocator_context.py | 2 +- src/pt_snap_cli/snapshot/simulate/simulate.py | 11 +-- .../simulate/simulated_caching_allocator.py | 56 +++++++++------ .../snapshot/simulate/snapshot_lookup.py | 2 +- .../tools/adaptors/database/callstack.py | 4 +- .../tools/adaptors/database/entity2record.py | 6 +- .../snapshot/tools/adaptors/snapshot2db.py | 9 +-- .../snapshot/tools/slice_dump/dump.py | 3 +- .../snapshot/tools/slice_dump/hooker.py | 9 ++- src/pt_snap_cli/snapshot/util/file_util.py | 10 +-- src/pt_snap_cli/snapshot/util/sqlite_meta.py | 4 +- src/pt_snap_cli/snapshot/util/timer.py | 3 +- tests/snapshot/test_entities.py | 24 +++++++ tests/snapshot/test_slice_dump_runtime.py | 7 ++ 19 files changed, 157 insertions(+), 99 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 5f3ca65..106a89b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,10 +22,9 @@ Run these commands from the repository root. - Check formatting: `black --check .` - Apply formatting: `black .` - Type check: `python -m basedpyright --pythonpath "$(python -c 'import sys; print(sys.executable)')"` - (covers all of `src/pt_snap_cli`; the gate is zero errors and warnings are - informational. `pyproject.toml` downgrades a listed set of rules to warnings - under `src/pt_snap_cli/snapshot/` until its annotations are fixed — do not - widen that list, shrink it) + (covers all of `src/pt_snap_cli`, including `snapshot/`; the gate is zero + errors and warnings are informational. Do not add a snapshot + `executionEnvironments` downgrade.) `tests/run_tests.sh` is tied to a developer-specific Conda path and writes coverage reports under `test_reports/`; use direct `pytest` commands unless diff --git a/CHANGELOG.md b/CHANGELOG.md index 86188ed..d5dbc6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ - 已有 `metadata --json`、`report peak-memory --json` 与 `skill list/install/upgrade/uninstall --json` 成功字段保持兼容,不包进新信封。它们在 `--json` 失败时改走 stderr 错误信封。新的 `capabilities --json` 与 `overview --json` 使用 #138 成功/失败信封。`capabilities` 在技能目录不可读时走 `SKILL_ERROR`;`overview` 把损坏的设备表/边界查询翻译为 `DATABASE_SCHEMA_INVALID`,并把损坏的 focus 文件翻译为 `FOCUS_FILE_INVALID`。 - 查询默认 `total` 不再在触达 `-n` 时自动变成匹配集合 `COUNT`。依赖该旧语义的调用方必须显式传 `--exact-total` / `exact_total=True`。 +### 稳定性与工程 + +- basedpyright 对 `src/pt_snap_cli/snapshot/` 与其余包路径同一套 error 门槛:补齐注解后移除 snapshot `executionEnvironments` 降级列表(#122 阶段 3)。 + ## [0.4.0] - Unreleased 相对 v0.3.0:关闭 MCP 产品面,Agent 集成入口收敛到 bundled skills 与 CLI;并补上 helper 引导技能、查询参数白名单,以及易误读字段的语义元数据。本段覆盖 `v0.3.0` 之后已合入 `main` 的全部用户可见变更。 diff --git a/pyproject.toml b/pyproject.toml index f5798ec..a95509a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,21 +103,6 @@ include = ["src/pt_snap_cli"] # to be, which is why the check passed in CI yet failed on developer machines. failOnWarnings = false -# The snapshot runtime was migrated from its upstream project with its original -# loose annotations (issue #122, phase 3). Only the rules it currently violates are -# downgraded, and only under this root; every other module is checked at error -# level. Remove entries here as the annotations are fixed. Changes under -# src/pt_snap_cli/snapshot/ also require a PROVENANCE.md entry. -[[tool.basedpyright.executionEnvironments]] -root = "src/pt_snap_cli/snapshot" -reportMissingTypeArgument = "warning" -reportAssignmentType = "warning" -reportAttributeAccessIssue = "warning" -reportUninitializedInstanceVariable = "warning" -reportArgumentType = "warning" -reportIndexIssue = "warning" -reportImportCycles = "warning" - [tool.coverage.run] source = ["pt_snap_cli"] branch = true diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index 98b4f47..ec63a8f 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -186,8 +186,15 @@ The upstream standalone split frontend is deliberately excluded, not deferred: follow-up also propagates `_raw_frames` through NPU workspace adaptation, maps legacy callstack-schema SQLite errors to an actionable re-import message, and uses a left join with a missing-callstack placeholder in - statistical analysis. These are local fixes to the issue #113 runtime; + statistical analysis. These are local fixes to the issue #113 runtime; audited upstream source mappings and license terms remain unchanged. +- On 2026-09-19, issue #122 phase 3 tightened first-party type annotations in + the snapshot runtime (`dict[str, Any]`, `int | None`, `Literal` defaults, + `DeviceSnapshot.__init__`, and import-cycle-safe submodule imports) so + basedpyright can check `src/pt_snap_cli/snapshot/` at the same error level + as the rest of the package. Call formatting, optional-field defaults, and + replay/import/split behavior are unchanged; audited upstream source mappings + and license terms remain unchanged. ## Migration toolchain diff --git a/src/pt_snap_cli/snapshot/base/entities.py b/src/pt_snap_cli/snapshot/base/entities.py index edc636f..c057f70 100644 --- a/src/pt_snap_cli/snapshot/base/entities.py +++ b/src/pt_snap_cli/snapshot/base/entities.py @@ -1,5 +1,5 @@ from dataclasses import dataclass, field -from typing import Any, Literal +from typing import Any, Literal, cast class Frame: @@ -7,10 +7,10 @@ class Frame: line: int = -1 name: str = "" - _origin: dict = None # Readonly + _origin: dict[str, Any] | None = None # Readonly @classmethod - def from_dict(cls, frame_dict: dict): + def from_dict(cls, frame_dict: dict[str, Any]): frame = cls() frame.filename = frame_dict["filename"] frame.line = frame_dict["line"] @@ -26,19 +26,19 @@ def to_dict(self): ) -def _format_callstack(frames: list[Frame] | list[dict]) -> str: +def _format_frame(frame: Frame | dict[str, Any]) -> str: + if isinstance(frame, Frame): + return f"{frame.filename}:{frame.line} {frame.name}" + return f"{frame['filename']}:{frame['line']} {frame['name']}" + + +def _format_callstack(frames: list[Frame] | list[dict[str, Any]]) -> str: if not frames: return "" - if isinstance(frames[0], Frame): - return "\n".join( - [f"{frame.filename}:{frame.line} {frame.name}" for frame in reversed(frames)] - ) - return "\n".join( - [f"{frame['filename']}:{frame['line']} {frame['name']}" for frame in reversed(frames)] - ) + return "\n".join(_format_frame(frame) for frame in reversed(frames)) -def _validate_raw_frames(frames: list[dict]) -> None: +def _validate_raw_frames(frames: list[dict[str, Any]]) -> None: for frame in frames: _ = frame["filename"] _ = frame["line"] @@ -76,12 +76,12 @@ class TraceEntry: device_free: int = -1 # only present for OOM, the amount of # memory npu still reports to be free - _origin: dict = None # Readonly - idx: int = -1 # 索引,全局唯一 - _raw_frames: list[dict] | None = field(default=None, repr=False) + _origin: dict[str, Any] | None = None # Readonly + idx: int | None = -1 # 索引,全局唯一 + _raw_frames: list[dict[str, Any]] | None = field(default=None, repr=False) @classmethod - def from_dict(cls, trace_dict: dict, _raw_frames: bool = False): + def from_dict(cls, trace_dict: dict[str, Any], _raw_frames: bool = False): frame_dicts = trace_dict.get("frames", []) if _raw_frames: _validate_raw_frames(frame_dicts) @@ -98,7 +98,7 @@ def from_dict(cls, trace_dict: dict, _raw_frames: bool = False): ) return trace_entry - def callstack_frames(self) -> list[Frame] | list[dict]: + def callstack_frames(self) -> list[Frame] | list[dict[str, Any]]: """Return the frame container that backs ``get_callstack()``. Callers that deduplicate callstacks key on this container's identity, @@ -112,7 +112,7 @@ def get_callstack(self): def to_dict(self, include_id: bool = False): if self._origin and not include_id: return self._origin - trace_dict = ( + trace_dict: dict[str, Any] = ( dict(self._origin) if self._origin else { @@ -157,12 +157,12 @@ class Block: # 指向持有该block的segment对象 segment_ptr: Any = None - free_event_idx: int = None - alloc_event_idx: int = None - _raw_frames: list[dict] | None = field(default=None, repr=False) + free_event_idx: int | None = None + alloc_event_idx: int | None = None + _raw_frames: list[dict[str, Any]] | None = field(default=None, repr=False) @classmethod - def from_dict(cls, block_dict: dict, _raw_frames: bool = False): + def from_dict(cls, block_dict: dict[str, Any], _raw_frames: bool = False): frame_dicts = block_dict.get("frames", []) if _raw_frames: _validate_raw_frames(frame_dicts) @@ -212,21 +212,24 @@ class Segment: address: int = -1 total_size: int = 0 # aclrtMalloc'd size of segment stream: int = 0 - segment_type: Literal["small", "large"] = "" # 'large' (>1MB) + segment_type: Literal["small", "large", ""] = "" # 'large' (>1MB) allocated_size: int = 0 # size of memory in use active_size: int = 0 # size of memory in use or in active_awaiting_free state blocks: list[Block] = field(default_factory=list) device: int = 0 frames: list[Frame] = field(default_factory=list) is_expandable: bool = False - _origin: dict = None # Readonly - free_or_unmap_event_idx: int = None - alloc_or_map_event_idx: int = None - _raw_frames: list[dict] | None = field(default=None, repr=False) + _origin: dict[str, Any] | None = None # Readonly + free_or_unmap_event_idx: int | None = None + alloc_or_map_event_idx: int | None = None + _raw_frames: list[dict[str, Any]] | None = field(default=None, repr=False) @classmethod def from_dict( - cls, segment_dict: dict, ignore_inactive_blocks: bool = False, _raw_frames: bool = False + cls, + segment_dict: dict[str, Any], + ignore_inactive_blocks: bool = False, + _raw_frames: bool = False, ): frame_dicts = segment_dict.get("frames", []) if _raw_frames: @@ -260,7 +263,7 @@ def build_from_event(cls, event: TraceEntry, with_inactive_block: bool = False): stream=event.stream, frames=event.frames, _raw_frames=event._raw_frames, - device=event.device if hasattr(event, "device") else 0, + device=cast(int, getattr(event, "device", 0)), allocated_size=0, active_size=0, is_expandable=event.action in ["segment_map", "segment_unmap"], @@ -309,10 +312,18 @@ class DeviceSnapshot: device: int + def __init__(self) -> None: + self.segments = [] + self.trace_entries = [] + self.total_allocated = 0 + self.total_reserved = 0 + self.total_activated = 0 + self.device = 0 + @classmethod def from_dict( cls, - snapshot_dict: dict, + snapshot_dict: dict[str, Any], device: int, ignore_inactive_blocks: bool = False, _raw_frames: bool = False, diff --git a/src/pt_snap_cli/snapshot/simulate/allocator_context.py b/src/pt_snap_cli/snapshot/simulate/allocator_context.py index 0a911d5..7d69d4d 100644 --- a/src/pt_snap_cli/snapshot/simulate/allocator_context.py +++ b/src/pt_snap_cli/snapshot/simulate/allocator_context.py @@ -4,7 +4,7 @@ class AllocatorContext: def __init__(self, snapshot: DeviceSnapshot): self.device_snapshot = snapshot - self.current_undo_event: TraceEntry = None + self.current_undo_event: TraceEntry | None = None self.workspace_flag = False def set_current_undo_event(self, undo_event: TraceEntry): diff --git a/src/pt_snap_cli/snapshot/simulate/simulate.py b/src/pt_snap_cli/snapshot/simulate/simulate.py index ed9e12c..7e8d942 100644 --- a/src/pt_snap_cli/snapshot/simulate/simulate.py +++ b/src/pt_snap_cli/snapshot/simulate/simulate.py @@ -1,12 +1,14 @@ from logging import Logger +from typing import Any from ..base import Block, BlockState, DeviceSnapshot from ..util import get_logger -from . import snapshot_lookup, snapshot_mutator from .allocator_context import AllocatorContext from .hooker_defs import AllocatorHooker, SimulateHooker from .replay_executor import ReplayExecutor from .simulated_caching_allocator import SimulatedCachingAllocator +from .snapshot_lookup import find_segment +from .snapshot_mutator import attach_block loading_logger = get_logger("LOAD") replay_logger = get_logger("REPLAY") @@ -20,12 +22,13 @@ class SimulateDeviceSnapshot: _loading_logger: Logger _replay_logger: Logger - def __init__(self, snapshot_dict: dict, device: int, *, _raw_frames: bool = False): + def __init__(self, snapshot_dict: dict[str, Any], device: int, *, _raw_frames: bool = False): # 基于device初始化logger self._loading_logger = loading_logger.getChild(f"{device}") self._replay_logger = replay_logger.getChild(f"{device}") if not snapshot_dict: raise RuntimeError("Cannot init snapshot from empty data.") + self.device = device self._loading_logger.info("Loading snapshot data...") self.device_snapshot = DeviceSnapshot.from_dict( snapshot_dict, @@ -89,7 +92,7 @@ def _adapt_workspace_snapshot(self): f"(stream {workspace_snapshot.stream}) is internally inconsistent" ) break - _, existed_seg = snapshot_lookup.find_segment( + _, existed_seg = find_segment( snapshot, workspace_snapshot.addr, workspace_snapshot.stream ) if existed_seg is None: @@ -118,7 +121,7 @@ def _adapt_workspace_snapshot(self): existed_seg.allocated_size = 0 existed_seg.active_size = 0 existed_seg.blocks = [] - snapshot_mutator.attach_block( + attach_block( snapshot, existed_seg, Block( diff --git a/src/pt_snap_cli/snapshot/simulate/simulated_caching_allocator.py b/src/pt_snap_cli/snapshot/simulate/simulated_caching_allocator.py index 637692d..234edea 100644 --- a/src/pt_snap_cli/snapshot/simulate/simulated_caching_allocator.py +++ b/src/pt_snap_cli/snapshot/simulate/simulated_caching_allocator.py @@ -3,9 +3,25 @@ from ..base import Block, BlockState, Segment, TraceEntry from ..util import get_logger -from . import snapshot_lookup, snapshot_mutator from .allocator_context import AllocatorContext from .allocator_hook_dispatcher import AllocatorHookDispatcher +from .snapshot_lookup import ( + find_block, + find_gap_for_alloc_block, + find_overlapping_segment, + find_segment, +) +from .snapshot_mutator import ( + attach_block, + decrease_reserved, + detach_block, + increase_reserved, + insert_segment, + merge_mapped_segment, + promote_pending_free_block, + remove_segment, + split_or_shrink_segment, +) allocator_logger = get_logger("ALLOCATOR") @@ -67,7 +83,7 @@ def alloc_block(self, new_block: Block) -> bool: :param new_block: 待分配的block """ _error = "Failed to simulate alloc block" - gap_result = snapshot_lookup.find_gap_for_alloc_block( + gap_result = find_gap_for_alloc_block( self.ctx.device_snapshot, new_block.address, new_block.size, @@ -86,7 +102,7 @@ def alloc_block(self, new_block: Block) -> bool: else: new_block.state = BlockState.ACTIVE_ALLOCATED self.dispatcher.pre_replay_alloc_block(new_block, self.ctx.device_snapshot) - snapshot_mutator.attach_block(self.ctx.device_snapshot, segment, new_block, insert_idx) + attach_block(self.ctx.device_snapshot, segment, new_block, insert_idx) self.dispatcher.post_replay_alloc_block(new_block, self.ctx.device_snapshot) return True @@ -96,7 +112,7 @@ def free_block(self, alloc_event: TraceEntry) -> bool: :param alloc_event: 待回滚的alloc事件 """ _error = "Failed to simulate free block" - seg_idx, segment = snapshot_lookup.find_overlapping_segment( + seg_idx, segment = find_overlapping_segment( self.ctx.device_snapshot, alloc_event.addr, alloc_event.stream ) if seg_idx == -1 or segment is None: @@ -104,7 +120,7 @@ def free_block(self, alloc_event: TraceEntry) -> bool: f"{_error}: cannot find segment for block (addr={alloc_event.addr})" ) return False - block_idx, exist_block = snapshot_lookup.find_block(segment, alloc_event.addr) + block_idx, exist_block = find_block(segment, alloc_event.addr) if block_idx == -1 or exist_block is None: # workspace场景容忍 if self.ctx.workspace_flag: @@ -121,7 +137,7 @@ def free_block(self, alloc_event: TraceEntry) -> bool: return False exist_block.alloc_event_idx = alloc_event.idx self.dispatcher.pre_replay_free_block(exist_block, self.ctx.device_snapshot) - if not snapshot_mutator.detach_block(self.ctx.device_snapshot, exist_block, block_idx): + if not detach_block(self.ctx.device_snapshot, exist_block, block_idx): allocator_logger.error(f"{_error}: block has no segment_ptr") return False self.dispatcher.post_replay_free_block(exist_block, self.ctx.device_snapshot, use_copy=True) @@ -133,7 +149,7 @@ def active_block(self, free_requested_event: TraceEntry) -> bool: :param free_requested_event: 待回放的free_requested请求 """ _error = "Failed to simulate active block" - seg_idx, segment = snapshot_lookup.find_overlapping_segment( + seg_idx, segment = find_overlapping_segment( self.ctx.device_snapshot, free_requested_event.addr, free_requested_event.stream, @@ -143,9 +159,7 @@ def active_block(self, free_requested_event: TraceEntry) -> bool: f"{_error}: cannot find segment for block (addr={free_requested_event.addr})" ) return False - block_idx, active_pending_free_block = snapshot_lookup.find_block( - segment, free_requested_event.addr - ) + block_idx, active_pending_free_block = find_block(segment, free_requested_event.addr) if block_idx == -1 or active_pending_free_block is None: allocator_logger.error( f"{_error}: cannot find block (addr={free_requested_event.addr})" @@ -164,9 +178,7 @@ def active_block(self, free_requested_event: TraceEntry) -> bool: f"current state: {active_pending_free_block.state}" ) return False - if not snapshot_mutator.promote_pending_free_block( - self.ctx.device_snapshot, active_pending_free_block - ): + if not promote_pending_free_block(self.ctx.device_snapshot, active_pending_free_block): allocator_logger.error(f"{_error}: the found active pending block's segment is none.") return False return True @@ -183,18 +195,18 @@ def alloc_or_map_segment(self, new_segment: Segment, merge: bool = False) -> boo if self.ctx.current_undo_event: new_segment.free_or_unmap_event_idx = self.ctx.current_undo_event.idx if not merge: - snapshot_mutator.insert_segment(self.ctx.device_snapshot, new_segment) + insert_segment(self.ctx.device_snapshot, new_segment) self.dispatcher.post_replay_map_or_alloc_segment(new_segment, self.ctx.device_snapshot) return True left_adjacent_idx, right_adjacent_idx = _find_adjacent_segment_indices( segments, new_segment ) if left_adjacent_idx == -1 and right_adjacent_idx == -1: - snapshot_mutator.insert_segment(self.ctx.device_snapshot, new_segment) + insert_segment(self.ctx.device_snapshot, new_segment) self.dispatcher.post_replay_map_or_alloc_segment(new_segment, self.ctx.device_snapshot) return True virtual_map_segment = copy.deepcopy(new_segment) - if not snapshot_mutator.merge_mapped_segment( + if not merge_mapped_segment( self.ctx.device_snapshot, new_segment, left_adjacent_idx, @@ -202,7 +214,7 @@ def alloc_or_map_segment(self, new_segment: Segment, merge: bool = False) -> boo ): allocator_logger.error(f"{_error}: failed to merge adjacent segments") return False - snapshot_mutator.increase_reserved(self.ctx.device_snapshot, virtual_map_segment.total_size) + increase_reserved(self.ctx.device_snapshot, virtual_map_segment.total_size) self.dispatcher.post_replay_map_or_alloc_segment( virtual_map_segment, self.ctx.device_snapshot ) @@ -215,7 +227,7 @@ def free_segment(self, alloc_seg_event: TraceEntry) -> bool: """ _error = "Free segment failed" seg_addr = alloc_seg_event.addr - exist_seg_idx, exist_seg = snapshot_lookup.find_segment( + exist_seg_idx, exist_seg = find_segment( self.ctx.device_snapshot, seg_addr, alloc_seg_event.stream ) if exist_seg_idx == -1 or exist_seg is None: @@ -237,7 +249,7 @@ def free_segment(self, alloc_seg_event: TraceEntry) -> bool: exist_seg.alloc_or_map_event_idx = alloc_seg_event.idx self.dispatcher.pre_replay_unmap_or_free_segment(exist_seg, self.ctx.device_snapshot) - snapshot_mutator.remove_segment(self.ctx.device_snapshot, exist_seg, exist_seg_idx) + remove_segment(self.ctx.device_snapshot, exist_seg, exist_seg_idx) self.dispatcher.post_replay_unmap_or_free_segment(exist_seg, self.ctx.device_snapshot) return True @@ -251,7 +263,7 @@ def unmap_segment(self, map_event): virtual_free_segment = Segment.build_from_event(map_event) seg_addr = virtual_free_segment.address unmap_size = virtual_free_segment.total_size - exist_seg_idx, exist_seg = snapshot_lookup.find_overlapping_segment( + exist_seg_idx, exist_seg = find_overlapping_segment( self.ctx.device_snapshot, seg_addr, map_event.stream ) if exist_seg_idx < 0 or exist_seg is None or exist_seg_idx >= len(segments): @@ -276,12 +288,12 @@ def unmap_segment(self, map_event): f"{_error}: stream mismatch (segment: {exist_seg.stream}, event: {map_event.stream})" ) return False - if not snapshot_mutator.split_or_shrink_segment( + if not split_or_shrink_segment( self.ctx.device_snapshot, exist_seg_idx, seg_addr, unmap_size ): allocator_logger.error(f"{_error}: failed to split or shrink segment") return False - snapshot_mutator.decrease_reserved(self.ctx.device_snapshot, unmap_size) + decrease_reserved(self.ctx.device_snapshot, unmap_size) self.dispatcher.post_replay_unmap_or_free_segment( virtual_free_segment, self.ctx.device_snapshot ) diff --git a/src/pt_snap_cli/snapshot/simulate/snapshot_lookup.py b/src/pt_snap_cli/snapshot/simulate/snapshot_lookup.py index 2ff359b..f7d6b7c 100644 --- a/src/pt_snap_cli/snapshot/simulate/snapshot_lookup.py +++ b/src/pt_snap_cli/snapshot/simulate/snapshot_lookup.py @@ -79,7 +79,7 @@ def find_segment(snapshot: DeviceSnapshot, addr: int, stream: int) -> tuple[int, def find_gap_for_alloc_block( - snapshot: DeviceSnapshot, event_addr: int, event_size: int, stream: int = None + snapshot: DeviceSnapshot, event_addr: int, event_size: int, stream: int | None = None ) -> tuple[Segment, int] | None: """Find the insertion gap for a block allocation inside a segment. diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/database/callstack.py b/src/pt_snap_cli/snapshot/tools/adaptors/database/callstack.py index 04599fb..df9c46e 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/database/callstack.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/database/callstack.py @@ -1,3 +1,5 @@ +from typing import Any + from ....base import TraceEntry from .defs import CallstackFieldDefs @@ -41,7 +43,7 @@ def intern(self, event: TraceEntry) -> int: return callstack_id - def records(self) -> list[dict]: + def records(self) -> list[dict[str, Any]]: """Return one record per distinct callstack, ordered by id.""" return [ {CallstackFieldDefs.ID: callstack_id, CallstackFieldDefs.CALLSTACK: text} diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/database/entity2record.py b/src/pt_snap_cli/snapshot/tools/adaptors/database/entity2record.py index 9014054..535360d 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/database/entity2record.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/database/entity2record.py @@ -1,3 +1,5 @@ +from typing import Any + from ....base import Block, TraceEntry from .callstack import CallstackInterner from .defs import BlockFieldDefs, EventFieldDefs @@ -30,7 +32,7 @@ def event2record( reserved: int = 0, *, callstacks: CallstackInterner, -) -> dict: +) -> dict[str, Any]: return { EventFieldDefs.ID: (event.idx if event.idx is not None else next_default_event_id()), EventFieldDefs.ACTION: event.action, @@ -44,7 +46,7 @@ def event2record( } -def block2record(block: Block) -> dict: +def block2record(block: Block) -> dict[str, Any]: return { BlockFieldDefs.ID: ( block.alloc_event_idx if block.alloc_event_idx is not None else next_default_block_id() diff --git a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py index 380d15c..67a757f 100644 --- a/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py +++ b/src/pt_snap_cli/snapshot/tools/adaptors/snapshot2db.py @@ -1,6 +1,7 @@ import os import tempfile from pathlib import Path +from typing import Any from ...base import Block, BlockState, DeviceSnapshot, TraceEntry from ...representation import UnsafePickleError, load_snapshot_representation, replay_snapshot @@ -33,14 +34,14 @@ def __init__( self.db.create_trace_entry_table(device) self.db.create_block_table(device) - def insert_event(self, event_record: dict, device: int = 0): + def insert_event(self, event_record: dict[str, Any], device: int = 0): if device not in self._device_event_cache: self._device_event_cache[device] = [] self._device_event_cache[device].append(event_record) if len(self._device_event_cache[device]) >= self._insert_cache_size: self._do_insert_events(device) - def insert_block(self, block_record: dict, device: int = 0): + def insert_block(self, block_record: dict[str, Any], device: int = 0): if device not in self._device_block_cache: self._device_block_cache[device] = [] self._device_block_cache[device].append(block_record) @@ -53,7 +54,7 @@ def flush(self, device: int = 0): if self._device_block_cache.get(device, None): self._do_insert_blocks(device) - def insert_callstacks(self, records: list[dict]): + def insert_callstacks(self, records: list[dict[str, Any]]): """Write the interned callstack table once every device has replayed.""" if not records: return @@ -159,7 +160,7 @@ def close(self, *, commit: bool = True): self.db_handler.close(commit=commit) -def dump(pickle_file: str, dump_file: str, device=None) -> bool: +def dump(pickle_file: str | Path, dump_file: str | Path, device: int | None = None) -> bool: try: data = load_snapshot_representation(Path(pickle_file)) except UnsafePickleError: diff --git a/src/pt_snap_cli/snapshot/tools/slice_dump/dump.py b/src/pt_snap_cli/snapshot/tools/slice_dump/dump.py index 6b5aeca..b30a425 100644 --- a/src/pt_snap_cli/snapshot/tools/slice_dump/dump.py +++ b/src/pt_snap_cli/snapshot/tools/slice_dump/dump.py @@ -1,5 +1,6 @@ import os.path from pathlib import Path +from typing import Literal from ...representation import load_snapshot_representation, replay_snapshot from ...util import get_logger @@ -14,7 +15,7 @@ def run_slice_dump( slices: int = 4, max_entries: int = 15000, dump_dir: str = "", - dump_type: str = "pkl", + dump_type: Literal["json", "pkl"] = "pkl", ): resolved_dump_dir = dump_dir or os.path.dirname(snapshot_file) dump_logger.info(f"Start to dump snapshot slice, reading pickle file '{snapshot_file}'.") diff --git a/src/pt_snap_cli/snapshot/tools/slice_dump/hooker.py b/src/pt_snap_cli/snapshot/tools/slice_dump/hooker.py index e107e1a..0c93050 100644 --- a/src/pt_snap_cli/snapshot/tools/slice_dump/hooker.py +++ b/src/pt_snap_cli/snapshot/tools/slice_dump/hooker.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import Literal -from ...base import DeviceSnapshot, TraceEntry +from ...base import DeviceSnapshot, Segment, TraceEntry from ...simulate import SimulateHooker from ...util import get_logger from ...util.file_util import save_dict_to_pickle @@ -42,7 +42,7 @@ def __init__( self.max_entries = max_entries self.num_of_events = -1 self.events_buffer = [] - self.prev_segments = None + self.prev_segments: list[Segment] | None = None self.dump_dir = dump_dir self.dump_count = 0 self.dump_type = dump_type @@ -94,9 +94,12 @@ def _is_need_dump(self, current_snapshot: DeviceSnapshot) -> bool: def dump(self, device: int): slice_snapshot_name = self._get_dump_filename() + prev_segments = self.prev_segments + if prev_segments is None: + raise RuntimeError("Cannot dump slice before capturing previous segments") slice_snapshot = DeviceSnapshot() slice_snapshot.device = device - slice_snapshot.segments = self.prev_segments + slice_snapshot.segments = prev_segments slice_snapshot.trace_entries = list(reversed(self.events_buffer)) slice_snapshot_dict = slice_snapshot.to_dict(include_trace_ids=True) dump_logger.info(f"Start to dump snapshot slice: {slice_snapshot_name}") diff --git a/src/pt_snap_cli/snapshot/util/file_util.py b/src/pt_snap_cli/snapshot/util/file_util.py index 0f24165..b34d6e7 100644 --- a/src/pt_snap_cli/snapshot/util/file_util.py +++ b/src/pt_snap_cli/snapshot/util/file_util.py @@ -40,7 +40,7 @@ def find_class(self, module: str, name: str): ) -def load_pickle_to_dict(pickle_file: Path) -> dict: +def load_pickle_to_dict(pickle_file: Path) -> dict[Any, Any]: """ 从指定路径加载 pickle 文件,并确保其内容为 dict 类型。 @@ -122,9 +122,7 @@ def check_dir_valid( >>> check_dir_valid('/nonexistent') False """ - _path = path - if not isinstance(path, Path): - _path = Path(_path) + _path = path if isinstance(path, Path) else Path(path) if not _path.is_dir(): return False if need_readable and not os.access(_path, os.R_OK): @@ -154,9 +152,7 @@ def check_file_valid( >>> check_file_valid('/nonexistent.txt') False """ - _path = path - if not isinstance(path, Path): - _path = Path(_path) + _path = path if isinstance(path, Path) else Path(path) if not _path.is_file(): return False if need_readable and not os.access(_path, os.R_OK): diff --git a/src/pt_snap_cli/snapshot/util/sqlite_meta.py b/src/pt_snap_cli/snapshot/util/sqlite_meta.py index e291d6d..f117ff0 100644 --- a/src/pt_snap_cli/snapshot/util/sqlite_meta.py +++ b/src/pt_snap_cli/snapshot/util/sqlite_meta.py @@ -119,7 +119,7 @@ def __init__( not_null: bool = False, # 是否不可为空 unique: bool = False, # 是否唯一 default: Any | None = None, # 缺省值, - value_map: dict[Any, Any] = None, + value_map: dict[Any, Any] | None = None, ): if autoincrement and not primary_key: raise ValueError("autoincrement requires primary_key=True") @@ -204,7 +204,7 @@ class SqliteTable: column_dict: dict[str, SqliteColumn] _column_value_map: dict[str, dict[Any, Any]] - def __init__(self, table_name: str, columns: Iterable[SqliteColumn] = None): + def __init__(self, table_name: str, columns: Iterable[SqliteColumn] | None = None): self.name = table_name self.column_dict = {} self._column_value_map = {} diff --git a/src/pt_snap_cli/snapshot/util/timer.py b/src/pt_snap_cli/snapshot/util/timer.py index 7d6d57c..be3a1c2 100644 --- a/src/pt_snap_cli/snapshot/util/timer.py +++ b/src/pt_snap_cli/snapshot/util/timer.py @@ -8,6 +8,7 @@ import time from collections.abc import Callable from functools import wraps +from typing import Any def timer(name: str | None = None, logger: logging.Logger | None = None): @@ -38,7 +39,7 @@ def timer(name: str | None = None, logger: logging.Logger | None = None): 2024-01-01 12:00:00 [ INFO ][ __main__ ]: 数据处理 took 0.5023 seconds """ - def decorator(func: Callable): + def decorator(func: Callable[..., Any]): _name = func.__name__ if not name else name @wraps(func) diff --git a/tests/snapshot/test_entities.py b/tests/snapshot/test_entities.py index 4b9ac9e..6069c29 100644 --- a/tests/snapshot/test_entities.py +++ b/tests/snapshot/test_entities.py @@ -40,6 +40,9 @@ def test_to_dict_without_origin(self): self.assertEqual(result["line"], 42) self.assertEqual(result["name"], "test_func") + def test_origin_default_is_none(self): + self.assertIsNone(Frame()._origin) + class TestTraceEntry(unittest.TestCase): def test_from_dict(self): @@ -176,6 +179,11 @@ def test_to_dict(self): class TestBlock(unittest.TestCase): + def test_event_index_defaults_are_none(self): + block = Block() + self.assertIsNone(block.free_event_idx) + self.assertIsNone(block.alloc_event_idx) + def test_from_dict(self): block = Block.from_dict( { @@ -233,6 +241,13 @@ def test_to_dict(self): class TestSegment(unittest.TestCase): + def test_optional_defaults(self): + segment = Segment() + self.assertEqual(segment.segment_type, "") + self.assertIsNone(segment._origin) + self.assertIsNone(segment.free_or_unmap_event_idx) + self.assertIsNone(segment.alloc_or_map_event_idx) + @staticmethod def _segment_dict(is_expandable=False, blocks=None): return { @@ -350,6 +365,15 @@ def test_to_dict(self): class TestDeviceSnapshot(unittest.TestCase): + def test_init_sets_empty_state(self): + snapshot = DeviceSnapshot() + self.assertEqual(snapshot.segments, []) + self.assertEqual(snapshot.trace_entries, []) + self.assertEqual(snapshot.total_allocated, 0) + self.assertEqual(snapshot.total_reserved, 0) + self.assertEqual(snapshot.total_activated, 0) + self.assertEqual(snapshot.device, 0) + def test_from_dict(self): snapshot = DeviceSnapshot.from_dict( { diff --git a/tests/snapshot/test_slice_dump_runtime.py b/tests/snapshot/test_slice_dump_runtime.py index 6700b5c..cf4b8e1 100644 --- a/tests/snapshot/test_slice_dump_runtime.py +++ b/tests/snapshot/test_slice_dump_runtime.py @@ -76,6 +76,13 @@ def test_slice_dump_hooker_rejects_strategy_init_before_event_count(tmp_path: Pa hooker._init_splitting_strategy() +def test_slice_dump_hooker_rejects_dump_before_previous_segments(tmp_path: Path) -> None: + hooker = SliceDumpHooker(str(tmp_path), num_of_slices=2, max_entries=3) + + with pytest.raises(RuntimeError, match="before capturing previous segments"): + hooker.dump(device=0) + + def test_slice_dump_hooker_json_dump_uses_utf8( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: From f63e17718d093adb1743b76bd50e5f7d9f37bf76 Mon Sep 17 00:00:00 2001 From: codeasier Date: Sat, 19 Sep 2026 22:51:37 +0800 Subject: [PATCH 2/2] fix(types): restore append-only provenance line for CI The snapshot-provenance guard rejects rewritten existing lines; this puts the accidental extra spaces back so the 2026-09-19 entry is the only insertion. --- src/pt_snap_cli/snapshot/PROVENANCE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pt_snap_cli/snapshot/PROVENANCE.md b/src/pt_snap_cli/snapshot/PROVENANCE.md index ec63a8f..63225c2 100644 --- a/src/pt_snap_cli/snapshot/PROVENANCE.md +++ b/src/pt_snap_cli/snapshot/PROVENANCE.md @@ -186,7 +186,7 @@ The upstream standalone split frontend is deliberately excluded, not deferred: follow-up also propagates `_raw_frames` through NPU workspace adaptation, maps legacy callstack-schema SQLite errors to an actionable re-import message, and uses a left join with a missing-callstack placeholder in - statistical analysis. These are local fixes to the issue #113 runtime; + statistical analysis. These are local fixes to the issue #113 runtime; audited upstream source mappings and license terms remain unchanged. - On 2026-09-19, issue #122 phase 3 tightened first-party type annotations in the snapshot runtime (`dict[str, Any]`, `int | None`, `Literal` defaults,