ai-editor-tree-engine-dev 1.0.176__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. ai_editor_tree_engine_dev-1.0.176.dist-info/METADATA +35 -0
  2. ai_editor_tree_engine_dev-1.0.176.dist-info/RECORD +121 -0
  3. ai_editor_tree_engine_dev-1.0.176.dist-info/WHEEL +5 -0
  4. ai_editor_tree_engine_dev-1.0.176.dist-info/licenses/LICENSE +21 -0
  5. ai_editor_tree_engine_dev-1.0.176.dist-info/top_level.txt +1 -0
  6. tree_engine/PUBLIC_API.md +161 -0
  7. tree_engine/__init__.py +327 -0
  8. tree_engine/checks/__init__.py +60 -0
  9. tree_engine/checks/builtin/__init__.py +5 -0
  10. tree_engine/checks/builtin/compile_check.py +53 -0
  11. tree_engine/checks/builtin/csv_syntax.py +51 -0
  12. tree_engine/checks/builtin/docstrings.py +211 -0
  13. tree_engine/checks/builtin/encoding.py +55 -0
  14. tree_engine/checks/builtin/fleet_rules.py +227 -0
  15. tree_engine/checks/builtin/format_syntax.py +82 -0
  16. tree_engine/checks/builtin/jsonl_syntax.py +49 -0
  17. tree_engine/checks/builtin/shipped.py +52 -0
  18. tree_engine/checks/builtin/syntax.py +118 -0
  19. tree_engine/checks/builtin/xml_wellformed.py +51 -0
  20. tree_engine/checks/chain.py +332 -0
  21. tree_engine/checks/external/__init__.py +5 -0
  22. tree_engine/checks/external/_base.py +61 -0
  23. tree_engine/checks/external/bandit.py +53 -0
  24. tree_engine/checks/external/black.py +48 -0
  25. tree_engine/checks/external/flake8.py +38 -0
  26. tree_engine/checks/external/isort.py +38 -0
  27. tree_engine/checks/external/mypy.py +45 -0
  28. tree_engine/checks/external/ruff.py +45 -0
  29. tree_engine/checks/external/runner.py +197 -0
  30. tree_engine/checks/model.py +110 -0
  31. tree_engine/checks/options.py +73 -0
  32. tree_engine/checks/plugin.py +171 -0
  33. tree_engine/checks/registry.py +349 -0
  34. tree_engine/core/address.py +376 -0
  35. tree_engine/core/identifier_map.py +266 -0
  36. tree_engine/core/identity.py +226 -0
  37. tree_engine/core/instrumentation.py +399 -0
  38. tree_engine/core/integrity.py +341 -0
  39. tree_engine/core/integrity_full.py +382 -0
  40. tree_engine/core/journal.py +260 -0
  41. tree_engine/core/journal_compose.py +571 -0
  42. tree_engine/core/live_tree.py +785 -0
  43. tree_engine/core/locking.py +188 -0
  44. tree_engine/core/move.py +585 -0
  45. tree_engine/core/move_references.py +323 -0
  46. tree_engine/core/node_types.py +408 -0
  47. tree_engine/core/nodes.py +348 -0
  48. tree_engine/core/offsets.py +24 -0
  49. tree_engine/core/operations.py +825 -0
  50. tree_engine/core/plugin_boundary.py +160 -0
  51. tree_engine/core/position_map.py +263 -0
  52. tree_engine/core/position_transfer.py +512 -0
  53. tree_engine/core/range_refresh.py +366 -0
  54. tree_engine/core/reference_cache.py +360 -0
  55. tree_engine/core/references.py +223 -0
  56. tree_engine/core/reparse.py +378 -0
  57. tree_engine/core/short_id.py +296 -0
  58. tree_engine/core/subtree_apply.py +397 -0
  59. tree_engine/core/subtree_copy.py +399 -0
  60. tree_engine/core/transactions.py +397 -0
  61. tree_engine/core/trivia.py +396 -0
  62. tree_engine/core/updates.py +398 -0
  63. tree_engine/core/validation.py +377 -0
  64. tree_engine/errors.py +104 -0
  65. tree_engine/exceptions.py +376 -0
  66. tree_engine/facade.py +4524 -0
  67. tree_engine/plugins/_statements.py +369 -0
  68. tree_engine/plugins/bsl/block_edit.py +449 -0
  69. tree_engine/plugins/bsl/blocks.py +127 -0
  70. tree_engine/plugins/bsl/checker.py +297 -0
  71. tree_engine/plugins/bsl/dialects.py +303 -0
  72. tree_engine/plugins/bsl/generator.py +481 -0
  73. tree_engine/plugins/bsl/import_map.py +995 -0
  74. tree_engine/plugins/bsl/plugin.py +452 -0
  75. tree_engine/plugins/contract.py +355 -0
  76. tree_engine/plugins/cpp/import_map.py +505 -0
  77. tree_engine/plugins/cpp/languages.py +99 -0
  78. tree_engine/plugins/cpp/plugin.py +501 -0
  79. tree_engine/plugins/csv_format.py +676 -0
  80. tree_engine/plugins/detachable.py +156 -0
  81. tree_engine/plugins/fallback.py +421 -0
  82. tree_engine/plugins/ini_format.py +330 -0
  83. tree_engine/plugins/json_format.py +813 -0
  84. tree_engine/plugins/json_pointer.py +203 -0
  85. tree_engine/plugins/jsonl_format.py +392 -0
  86. tree_engine/plugins/markdown_format.py +748 -0
  87. tree_engine/plugins/onec_query_format.py +61 -0
  88. tree_engine/plugins/parse_position.py +54 -0
  89. tree_engine/plugins/plain_text.py +1543 -0
  90. tree_engine/plugins/python/export_map.py +306 -0
  91. tree_engine/plugins/python/import_map.py +429 -0
  92. tree_engine/plugins/python/plugin.py +1710 -0
  93. tree_engine/plugins/registry.py +497 -0
  94. tree_engine/plugins/scan.py +87 -0
  95. tree_engine/plugins/selection.py +228 -0
  96. tree_engine/plugins/sql_format.py +61 -0
  97. tree_engine/plugins/toml_format.py +1072 -0
  98. tree_engine/plugins/xml_format.py +911 -0
  99. tree_engine/plugins/yaml/builder.py +49 -0
  100. tree_engine/plugins/yaml/emitter.py +225 -0
  101. tree_engine/plugins/yaml/flow.py +227 -0
  102. tree_engine/plugins/yaml/plugin.py +458 -0
  103. tree_engine/plugins/yaml/ranges.py +52 -0
  104. tree_engine/plugins/yaml/reader.py +359 -0
  105. tree_engine/plugins/yaml/scanner.py +298 -0
  106. tree_engine/query/adapter.py +283 -0
  107. tree_engine/query/engine.py +853 -0
  108. tree_engine/query/inspection.py +498 -0
  109. tree_engine/query/outline.py +288 -0
  110. tree_engine/query/predicates.py +263 -0
  111. tree_engine/query/rendered_lines.py +46 -0
  112. tree_engine/query/results.py +206 -0
  113. tree_engine/query/selector.py +400 -0
  114. tree_engine/query/xml_query.py +747 -0
  115. tree_engine/storage/codec.py +400 -0
  116. tree_engine/storage/document_bridge.py +579 -0
  117. tree_engine/storage/file_txn.py +459 -0
  118. tree_engine/storage/history.py +106 -0
  119. tree_engine/storage/lifecycle.py +622 -0
  120. tree_engine/storage/schema.py +403 -0
  121. tree_engine/storage/session_guard.py +398 -0
@@ -0,0 +1,283 @@
1
+ """Traversal adapter binding the ported cst_query engine to the common tree
2
+ (concept C-009, atomic step G-018/T-001/A-002).
3
+
4
+ Scope, per {p005} and {p018}: this is the *only* piece of the cst_query
5
+ port that changes. The legacy engine in ``ai_editor/cst_query`` walked a
6
+ LibCST tree via ``ai_editor/cst_query/index_builder.py`` (``build_index``,
7
+ producing one ``NodeInfo`` per visited node with its LibCST parent, a
8
+ ``kind`` alias -- ``module``/``class``/``function``/``method``/``stmt``/
9
+ ``smallstmt``/``import``/``node`` -- resolved from a class/function name
10
+ stack, plus position/name/qualname) and matched selector steps against
11
+ that flat list in ``ai_editor/cst_query/executor.py`` (``_matches_node_type``:
12
+ a known alias compares against ``kind``, anything else compares against the
13
+ literal LibCST class name). This module reproduces exactly that traversal
14
+ contract -- "walk every node, tag it with a role if one applies, keep every
15
+ node reachable by its own literal type name regardless" -- over the common
16
+ tree model instead of LibCST, so the selector language, predicates,
17
+ combinators, pseudo-selectors, result models and exceptions living in
18
+ sibling files (this same tactical step, T-001) need no functional
19
+ redesign, only a new traversal source.
20
+
21
+ Two differences from the legacy adapter, both required by the common
22
+ model: (1) the four legacy alias buckets ``function``/``method``/``class``/
23
+ ``module`` are no longer a hand-written class/function name stack -- they
24
+ are the standard semantic roles a loaded format plugin declares via
25
+ :meth:`~tree_engine.plugins.contract.FormatPluginContract.semantic_role_mapping`
26
+ (and, where a plugin exposes it, its own context-sensitive
27
+ ``role_for(node, ancestors)`` resolver -- see ``tree_engine.plugins.python
28
+ .plugin.PythonFormatPlugin.role_for``, whose docstring explains why a flat
29
+ kind -> role table alone cannot distinguish a method from a top-level
30
+ function); (2) every produced match is a sibling ``QueryMatch`` (see
31
+ ``tree_engine.query.results``), carrying the {p021} ``node_id``/
32
+ ``short_id_hex`` pair for the node itself and for every ancestor on its
33
+ ``parent_path``, instead of the legacy ``NodeInfo``/``Match`` shape.
34
+
35
+ This module reads only already-loaded ``Document``/``Node`` state (see
36
+ ``tree_engine.core.nodes``): it never parses or reparses source text, and
37
+ it never mutates the tree, a document version, or any index. Predicate
38
+ evaluation, combinators, pseudo-selectors, and full selector orchestration
39
+ are sibling concerns under this same tactical step and are out of scope
40
+ here -- this module only walks the tree, resolves each node's role, and
41
+ assembles the compact result record for a node the caller already decided
42
+ is a match by type/role.
43
+
44
+ Source-of-truth requirement labels honored here: {p005}, {p018}, {p021},
45
+ {p040}.
46
+ """
47
+
48
+ from __future__ import annotations
49
+
50
+ from typing import Any, Iterator, Optional, Sequence, Tuple
51
+
52
+ from tree_engine.core.address import NodeAddressView, build_node_address_view
53
+ from tree_engine.core.nodes import Document, Node, iter_children
54
+ from tree_engine.core.short_id import to_hex
55
+ from tree_engine.query.results import QueryMatch, build_query_match
56
+
57
+ __all__ = [
58
+ "TreeTraversalAdapter",
59
+ "iter_matching_nodes",
60
+ "resolve_semantic_role",
61
+ ]
62
+
63
+
64
+ def resolve_semantic_role(
65
+ plugin: Any, node: Node, ancestors: Sequence[Node] = ()
66
+ ) -> str:
67
+ """Ask ``plugin`` which standard semantic role (if any) ``node`` plays.
68
+
69
+ ``plugin`` is duck-typed against
70
+ ``tree_engine.plugins.contract.FormatPluginContract``: this function
71
+ never imports a concrete plugin or a registry, it only calls whatever
72
+ the caller already resolved and injected.
73
+
74
+ Preference order, mirroring
75
+ ``tree_engine.plugins.python.plugin.PythonFormatPlugin``'s own
76
+ documented reasoning: if ``plugin`` exposes a callable ``role_for``
77
+ attribute, it is called as ``role_for(node, ancestors)`` -- the only
78
+ way to resolve a role that depends on structural context, such as
79
+ promoting a function-shaped node to ``method`` when its nearest
80
+ enclosing construct is a class. Otherwise this falls back to the
81
+ mandatory, context-free ``plugin.semantic_role_mapping()`` declarative
82
+ table and looks up ``node.kind`` in it directly.
83
+
84
+ Returns the resolved role as a plain lowercase string (``"function"``,
85
+ ``"method"``, ``"class"``, or ``"module"``), or ``""`` when no role
86
+ applies -- never raises for an inapplicable role, per {p018}: "for a
87
+ format where the role is inapplicable, a query correctly returns an
88
+ empty result".
89
+ """
90
+
91
+ role_for = getattr(plugin, "role_for", None)
92
+ if callable(role_for):
93
+ return _role_to_str(role_for(node, tuple(ancestors)))
94
+
95
+ mapping = plugin.semantic_role_mapping()
96
+ node_type_to_role = getattr(mapping, "node_type_to_role", mapping)
97
+ role = node_type_to_role.get(str(node.kind))
98
+ return _role_to_str(role)
99
+
100
+
101
+ def _role_to_str(role: Any) -> str:
102
+ """Normalize a resolved role to a plain string, ``""`` when absent.
103
+
104
+ Accepts ``None`` (no role), a plain ``str``, or an enum member such as
105
+ ``tree_engine.plugins.contract.SemanticRole`` (read via its ``.value``)
106
+ -- whatever shape a duck-typed plugin returns.
107
+ """
108
+
109
+ if role is None:
110
+ return ""
111
+ return str(getattr(role, "value", role))
112
+
113
+
114
+ def _matches_node_type(node: Node, role: str, node_type: Optional[str]) -> bool:
115
+ """True when ``node`` (already tagged with ``role``) satisfies ``node_type``.
116
+
117
+ D1 of S6a (wish 8564a2ba): this module keeps no independent
118
+ node-type-matching rule of its own any more -- it delegates to
119
+ ``tree_engine.query.engine._node_type_matches``, the one canonical
120
+ implementation for the whole ``tree_engine.query`` package (imported
121
+ lazily, here, to avoid a circular import at module load time:
122
+ ``engine.py`` itself imports :class:`TreeTraversalAdapter` from this
123
+ module). ``role`` is this module's own narrower, four-value semantic
124
+ role (see :func:`resolve_semantic_role`); the canonical function widens
125
+ its alias vocabulary to eight words for a format that supplies a
126
+ richer classification, but a four-value ``role`` is structurally never
127
+ one of the four extra words, so this call site's own observable
128
+ behavior -- role-alias matching plus the literal/bare-type fallback --
129
+ is unchanged.
130
+ """
131
+
132
+ from tree_engine.query.engine import (
133
+ _node_type_matches as _canonical_node_type_matches,
134
+ )
135
+
136
+ return _canonical_node_type_matches(node, role, node_type)
137
+
138
+
139
+ def _build_parent_path(ancestors: Sequence[Node]) -> Tuple[NodeAddressView, ...]:
140
+ """Build the ordered {p021} ``parent_path`` for ``ancestors``.
141
+
142
+ ``ancestors`` must already be in root-to-immediate-parent order (the
143
+ order :func:`_walk` below produces). Each entry is turned into a
144
+ ``NodeAddressView`` pairing that ancestor's ``node_id`` with its
145
+ compact ``short_id_hex``, via ``tree_engine.core.address
146
+ .build_node_address_view`` -- the sibling helper
147
+ ``tree_engine.query.results`` itself documents as how a caller is
148
+ expected to produce this pair, so this function never formats hex or
149
+ assembles the pair by hand.
150
+ """
151
+
152
+ return tuple(
153
+ build_node_address_view(ancestor.node_id, ancestor.short_id)
154
+ for ancestor in ancestors
155
+ )
156
+
157
+
158
+ def _children_for(node: Node, plugin: Any) -> Iterator[Node]:
159
+ """``node``'s children in the order this package's traversal should
160
+ see them (S6a of wish 8564a2ba, verifier findings #4/#6): a plugin
161
+ declaring a callable ``ordered_children_for(node)`` (only the python
162
+ plugin, at the time of writing -- see ``PythonFormatPlugin
163
+ .ordered_children_for``) gets to reorder/filter what ``node.children``
164
+ itself carries for query-traversal purposes only (e.g. reconstructing
165
+ real document order, or dropping synthetic marker nodes with no
166
+ legacy counterpart); any other plugin is completely unaffected and
167
+ gets exactly ``tree_engine.core.nodes.iter_children(node)``, unchanged.
168
+ """
169
+
170
+ ordered_children_for = getattr(plugin, "ordered_children_for", None)
171
+ if callable(ordered_children_for):
172
+ return iter(ordered_children_for(node))
173
+ return iter_children(node)
174
+
175
+
176
+ def _walk(
177
+ node: Node, ancestors: Tuple[Node, ...], plugin: Any
178
+ ) -> Iterator[Tuple[Node, Tuple[Node, ...], str]]:
179
+ """Depth-first, pre-order walk yielding ``(node, ancestors, role)``.
180
+
181
+ ``ancestors`` is ``node``'s own ancestor path, root first, immediate
182
+ parent last -- exactly the order :func:`_build_parent_path` expects.
183
+ Recurses through :func:`_children_for` (``tree_engine.core.nodes
184
+ .iter_children`` for a plugin with no query-traversal opinion of its
185
+ own); nothing here parses, reparses, or mutates anything.
186
+ """
187
+
188
+ role = resolve_semantic_role(plugin, node, ancestors)
189
+ yield node, ancestors, role
190
+ child_ancestors = ancestors + (node,)
191
+ for child in _children_for(node, plugin):
192
+ yield from _walk(child, child_ancestors, plugin)
193
+
194
+
195
+ def iter_matching_nodes(
196
+ document: Document, plugin: Any, node_type: Optional[str] = None
197
+ ) -> Iterator[QueryMatch]:
198
+ """Yield a :class:`~tree_engine.query.results.QueryMatch` for every node
199
+ of ``document.root``'s already-loaded subtree that satisfies ``node_type``.
200
+
201
+ ``node_type`` is either one of the four standard semantic roles
202
+ (matched via :func:`resolve_semantic_role`, per plugin, with an
203
+ inapplicable role simply producing no matches -- never an error) or a
204
+ literal format-specific type name (matched against the node's own
205
+ ``kind``, independent of role, per {p018}); ``None``/``"*"`` matches
206
+ every node. Every yielded ``QueryMatch`` carries the node's own
207
+ ``node_id``/``short_id_hex`` and its full root-to-parent ``parent_path``
208
+ (see :func:`_build_parent_path`), assembled solely through the sibling
209
+ ``tree_engine.query.results.build_query_match`` constructor -- this
210
+ function never builds a competing result shape.
211
+
212
+ Reads only ``document``/``plugin`` state already given to it: no
213
+ source text is parsed or reparsed, and no node, document version, or
214
+ index is mutated.
215
+ """
216
+
217
+ for node, ancestors, role in _walk(document.root, (), plugin):
218
+ if not _matches_node_type(node, role, node_type):
219
+ continue
220
+ parent_path = _build_parent_path(ancestors)
221
+ short_id_hex = to_hex(node.short_id)
222
+ yield build_query_match(node, role, short_id_hex, parent_path)
223
+
224
+
225
+ class TreeTraversalAdapter:
226
+ """Binds one already-loaded ``Document`` and its format plugin to the
227
+ ported cst_query engine's traversal needs (concept C-009).
228
+
229
+ Thin, stateless-beyond-construction wrapper around the module-level
230
+ :func:`iter_matching_nodes`/:func:`resolve_semantic_role` functions:
231
+ it exists so sibling selector-orchestration code (predicate
232
+ evaluation, combinators, pseudo-selectors -- out of scope here) can
233
+ hold one adapter instance per open document/plugin pair instead of
234
+ re-threading both through every call.
235
+
236
+ ``document`` and ``plugin`` are stored exactly as given -- this class
237
+ performs no validation, resolution, or lookup of its own: the caller
238
+ is expected to have already loaded the document's tree and resolved
239
+ its format plugin (e.g. via ``tree_engine.plugins.selection``)
240
+ beforehand.
241
+ """
242
+
243
+ def __init__(self, document: Document, plugin: Any) -> None:
244
+ self._document = document
245
+ self._plugin = plugin
246
+
247
+ @property
248
+ def document(self) -> Document:
249
+ """The already-loaded ``Document`` this adapter walks."""
250
+
251
+ return self._document
252
+
253
+ @property
254
+ def plugin(self) -> Any:
255
+ """The already-loaded format plugin used for role resolution."""
256
+
257
+ return self._plugin
258
+
259
+ def iter_nodes(self) -> Iterator[Tuple[Node, Tuple[Node, ...], str]]:
260
+ """Depth-first pre-order traversal of the whole document tree.
261
+
262
+ Yields ``(node, ancestors, role)`` for every node, ``ancestors``
263
+ in root-to-parent order and ``role`` the standard semantic role
264
+ (``""`` when none applies) -- the raw traversal entry point
265
+ sibling selector-orchestration code builds on top of. Equivalent
266
+ to the module-level :func:`_walk` seeded from ``self.document.root``.
267
+ """
268
+
269
+ return _walk(self._document.root, (), self._plugin)
270
+
271
+ def iter_matching_nodes(
272
+ self, node_type: Optional[str] = None
273
+ ) -> Iterator[QueryMatch]:
274
+ """Bound form of the module-level :func:`iter_matching_nodes`,
275
+ using this adapter's own ``document`` and ``plugin``."""
276
+
277
+ return iter_matching_nodes(self._document, self._plugin, node_type)
278
+
279
+ def resolve_semantic_role(self, node: Node, ancestors: Sequence[Node] = ()) -> str:
280
+ """Bound form of the module-level :func:`resolve_semantic_role`,
281
+ using this adapter's own ``plugin``."""
282
+
283
+ return resolve_semantic_role(self._plugin, node, ancestors)