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,323 @@
1
+ """Cross-document move reference reconciliation (concept C-020, {p039}/{p080}/{p081}).
2
+
3
+ Scope, narrowed exactly to what this atomic step owns: given the subtree(s)
4
+ already structurally relocated by the sibling ``move.py`` orchestrator, this
5
+ module computes -- and, once computed, mechanically applies -- the reference
6
+ rewrites a cross-document move requires:
7
+
8
+ * every reference between two moved nodes ("subtree-internal") is rewritten
9
+ to carry the target document's ``document_id`` explicitly;
10
+ * every outgoing reference from a moved node to a node left behind in the
11
+ source document is rewritten to an explicit external ``NodeAddress``
12
+ naming the source document;
13
+ * every incoming reference that names a moved node -- found on a node still
14
+ resident in the source document, or on a node in any other *loaded*
15
+ document -- is rewritten to an explicit external ``NodeAddress`` naming
16
+ the target document.
17
+
18
+ This satisfies {p080}/{p081} in the context of the move defined by {p039}.
19
+ ``NodeAddress`` (imported unchanged from ``core.identity``) stays the sole
20
+ canonical representation; this module never introduces a second one and
21
+ never touches any object-reference cache -- there is none here to touch,
22
+ since ``core.nodes.Node.references`` already stores canonical ``NodeAddress``
23
+ values directly, per {p080}.
24
+
25
+ This file computes a :class:`ReferenceDelta` and applies it -- nothing else.
26
+ It never decides transaction boundaries, never acquires a lock, and never
27
+ commits or rolls back a transaction: the sibling ``move.py`` orchestrator
28
+ calls ``reconcile_references(...)`` once, inside its own rollback scope,
29
+ immediately after the structural relocation, and then calls the returned
30
+ delta's ``.apply(source_document, target_document, loaded_documents)``,
31
+ still inside that same scope. Likewise, refreshing either document's index
32
+ maps (``nodes_by_id``, ``short_id``, ``parent``/``child``, the
33
+ incoming/outgoing reference indices such as ``core.reference_cache
34
+ .ReferenceIndex``) is *not* done by this file -- per this step's own
35
+ contract, that synchronous refresh happens elsewhere, as part of the same
36
+ application ``move.py`` performs, not here directly.
37
+
38
+ Duck-typed contracts reused unchanged from the already-merged sibling
39
+ modules (never redefined here):
40
+
41
+ * ``NodeAddress(document_id, node_id)`` -- ``core.identity``. ``document_id
42
+ is None`` is the implicit-current-document (local) form; an explicit
43
+ value names a document unambiguously, per {p080}.
44
+ * ``is_local(address)`` -- ``core.references``, the exact local/external
45
+ classification helper this module reuses instead of re-implementing the
46
+ ``document_id is None`` check inline.
47
+ * ``document`` -- duck-typed exactly as ``move.py``'s own contract:
48
+ ``document.nodes_by_id`` (node_id -> node) and an optional
49
+ ``document.document_id``.
50
+ * ``node`` -- duck-typed with an optional ``node_id``, an optional
51
+ ``children`` sequence (for subtree traversal), and an optional
52
+ ``references`` sequence of ``NodeAddress`` values -- mirroring the
53
+ reserved extension-point attribute on ``core.nodes.Node`` -- that this
54
+ module reads and, in ``ReferenceDelta.apply``, rewrites in place via
55
+ ``setattr``, exactly the convention ``move.py`` itself already uses for
56
+ ``parent`` and ``children``.
57
+
58
+ ``short_id_remap`` is accepted only because ``move.py`` always supplies it
59
+ as part of its fixed call shape; references are addressed by ``node_id``,
60
+ never by ``short_id``, so this module never reads it.
61
+
62
+ An intra-document move (``source_document`` and ``target_document`` sharing
63
+ the same ``document_id``, including both being ``None``) has nothing to
64
+ reconcile -- no document boundary is actually crossed -- so
65
+ :func:`reconcile_references` returns an empty, no-op delta for it rather
66
+ than rewriting addresses that were already correct.
67
+ """
68
+
69
+ from __future__ import annotations
70
+
71
+ from dataclasses import dataclass
72
+ from typing import Any, Dict, List, Optional, Sequence, Tuple
73
+
74
+ from tree_engine.core.identity import NodeAddress
75
+ from tree_engine.core.references import is_local
76
+
77
+ __all__ = ["ReferenceChange", "ReferenceDelta", "reconcile_references"]
78
+
79
+
80
+ @dataclass(frozen=True)
81
+ class ReferenceChange:
82
+ """One node's outgoing ``references`` tuple, before and after rewrite."""
83
+
84
+ node: Any
85
+ old_references: Tuple[Any, ...]
86
+ new_references: Tuple[Any, ...]
87
+
88
+
89
+ @dataclass(frozen=True)
90
+ class ReferenceDelta:
91
+ """The complete, immutable plan produced by :func:`reconcile_references`.
92
+
93
+ ``changes`` is fully computed before this object is constructed, so
94
+ there is no lazy or partial state to observe between construction and
95
+ :meth:`apply` -- either every planned rewrite is here already, or (for
96
+ a node untouched by the move) it is simply absent from ``changes``.
97
+ Applying is a purely mechanical replay of that already-decided plan:
98
+ for each recorded :class:`ReferenceChange`, ``change.node.references``
99
+ is set to ``change.new_references``. Nothing else is touched -- no
100
+ lock, no transaction, no index map, no document attribute other than
101
+ the node's own ``references``.
102
+ """
103
+
104
+ changes: Tuple[ReferenceChange, ...] = ()
105
+
106
+ def apply(
107
+ self,
108
+ source_document: Any = None,
109
+ target_document: Any = None,
110
+ loaded_documents: Optional[Dict[Any, Any]] = None,
111
+ ) -> "ReferenceDelta":
112
+ """Write every planned rewrite back onto its owning node.
113
+
114
+ Accepts the same three arguments ``move.py`` passes positionally
115
+ (``source_document``, ``target_document``, ``loaded_documents``)
116
+ purely to satisfy the injected ``.apply(...)`` contract it already
117
+ calls; none of them is otherwise required here, since every node
118
+ this delta touches is already reachable directly through
119
+ ``self.changes``.
120
+ """
121
+
122
+ for change in self.changes:
123
+ setattr(change.node, "references", change.new_references)
124
+ return self
125
+
126
+
127
+ def _document_id(document: Any) -> Any:
128
+ return getattr(document, "document_id", None)
129
+
130
+
131
+ def _fragment_nodes(root: Any) -> List[Any]:
132
+ """Every node in ``root``'s subtree, ``root`` included (order-agnostic).
133
+
134
+ Mirrors ``move.py``'s own ``_fragment_nodes`` helper in shape (this
135
+ file never imports it, per the sibling-duplication precedent already
136
+ established between ``operations.py`` and ``updates.py``)."""
137
+
138
+ ordered: List[Any] = []
139
+ stack = [root]
140
+ while stack:
141
+ node = stack.pop(0)
142
+ ordered.append(node)
143
+ stack = list(getattr(node, "children", None) or ()) + stack
144
+ return ordered
145
+
146
+
147
+ def _collect_moved(moved_roots: Sequence[Any]) -> Tuple[List[Any], set]:
148
+ """Flatten every moved root's subtree into a node list and an id set."""
149
+
150
+ nodes: List[Any] = []
151
+ ids: set = set()
152
+ for root in moved_roots:
153
+ for node in _fragment_nodes(root):
154
+ nodes.append(node)
155
+ node_id = getattr(node, "node_id", None)
156
+ if node_id is not None:
157
+ ids.add(node_id)
158
+ return nodes, ids
159
+
160
+
161
+ def _references_of(node: Any) -> Tuple[Any, ...]:
162
+ return tuple(getattr(node, "references", None) or ())
163
+
164
+
165
+ def _address_parts(address: Any) -> Tuple[Any, Any]:
166
+ """Duck-typed ``(node_id, document_id)`` read off ``address``.
167
+
168
+ Mirrors ``validate_mutation``'s own ``getattr(reference, "node_id",
169
+ reference)`` convention: a bare id is accepted as-is, alongside a full
170
+ ``NodeAddress``-shaped value."""
171
+
172
+ node_id = getattr(address, "node_id", address)
173
+ document_id = getattr(address, "document_id", None)
174
+ return node_id, document_id
175
+
176
+
177
+ def _rewrite_outgoing(
178
+ moved_nodes: Sequence[Any],
179
+ moved_ids: set,
180
+ src_id: Any,
181
+ tgt_id: Any,
182
+ ) -> List[ReferenceChange]:
183
+ """Rewrite each moved node's own outgoing references -- {p039}'s first
184
+ two clauses: a subtree-internal reference adopts the target
185
+ document_id; a reference to a node left behind in the source document
186
+ becomes external and explicitly names the source document. A
187
+ reference that, even before the move, already named some third
188
+ document is left exactly as it was -- this move never touches it."""
189
+
190
+ changes: List[ReferenceChange] = []
191
+ for node in moved_nodes:
192
+ old_refs = _references_of(node)
193
+ if not old_refs:
194
+ continue
195
+ new_refs: List[Any] = []
196
+ changed = False
197
+ for address in old_refs:
198
+ node_id, document_id = _address_parts(address)
199
+ effective_doc = src_id if is_local(address) else document_id
200
+ if effective_doc == src_id and node_id in moved_ids:
201
+ new_address: Any = NodeAddress(node_id=node_id, document_id=tgt_id)
202
+ changed = True
203
+ elif effective_doc == src_id and node_id not in moved_ids:
204
+ new_address = NodeAddress(node_id=node_id, document_id=src_id)
205
+ changed = True
206
+ else:
207
+ new_address = address
208
+ new_refs.append(new_address)
209
+ if changed:
210
+ changes.append(
211
+ ReferenceChange(node=node, old_references=old_refs, new_references=tuple(new_refs))
212
+ )
213
+ return changes
214
+
215
+
216
+ def _rewrite_incoming(
217
+ scanned_docs: Dict[Any, Any],
218
+ moved_object_ids: set,
219
+ moved_ids: set,
220
+ src_id: Any,
221
+ tgt_id: Any,
222
+ ) -> List[ReferenceChange]:
223
+ """Rewrite every reference, in every *loaded* document, that names a
224
+ moved node -- {p039}'s third clause, generalized from "the source
225
+ document's remaining nodes" to any loaded document, since a third
226
+ document can hold an external reference into the source document just
227
+ as validly as the source document's own remaining nodes can. A
228
+ document the caller never loaded is, correctly, never visited here:
229
+ nothing in it is touched, since this module has no way to reach it."""
230
+
231
+ changes: List[ReferenceChange] = []
232
+ for doc_id, document in scanned_docs.items():
233
+ nodes_by_id = getattr(document, "nodes_by_id", None) or {}
234
+ for node in nodes_by_id.values():
235
+ if id(node) in moved_object_ids:
236
+ continue # the moved side is already handled by _rewrite_outgoing
237
+ old_refs = _references_of(node)
238
+ if not old_refs:
239
+ continue
240
+ new_refs: List[Any] = []
241
+ changed = False
242
+ for address in old_refs:
243
+ node_id, document_id = _address_parts(address)
244
+ effective_doc = doc_id if is_local(address) else document_id
245
+ if effective_doc == src_id and node_id in moved_ids:
246
+ new_refs.append(NodeAddress(node_id=node_id, document_id=tgt_id))
247
+ changed = True
248
+ else:
249
+ new_refs.append(address)
250
+ if changed:
251
+ changes.append(
252
+ ReferenceChange(node=node, old_references=old_refs, new_references=tuple(new_refs))
253
+ )
254
+ return changes
255
+
256
+
257
+ def reconcile_references(
258
+ *,
259
+ source_document: Any,
260
+ target_document: Any,
261
+ moved_roots: Sequence[Any],
262
+ short_id_remap: Sequence[Any] = (),
263
+ loaded_documents: Optional[Dict[Any, Any]] = None,
264
+ ) -> ReferenceDelta:
265
+ """Compute the reference-conversion plan for one cross-document move.
266
+
267
+ Called once by ``move.py``, after the structural relocation but before
268
+ either document is considered final, with ``moved_roots`` already
269
+ attached under ``target_document`` (so ``target_document.nodes_by_id``
270
+ already contains them and ``source_document.nodes_by_id`` no longer
271
+ does). Returns a fully-computed, immutable :class:`ReferenceDelta`;
272
+ nothing is mutated by this function itself -- only :meth:`ReferenceDelta
273
+ .apply` writes anything.
274
+
275
+ Three conversions are computed, per {p039}/{p080}/{p081}:
276
+
277
+ 1. A reference between two moved nodes stays addressable, now inside
278
+ the target document: its ``NodeAddress`` is rewritten with an
279
+ explicit ``document_id`` equal to ``target_document``'s.
280
+ 2. An outgoing reference from a moved node to a node left behind in
281
+ ``source_document`` becomes external: its ``NodeAddress`` is
282
+ rewritten with an explicit ``document_id`` equal to
283
+ ``source_document``'s.
284
+ 3. An incoming reference that names a moved node, found on any node
285
+ still resident in ``source_document`` or in any document supplied
286
+ through ``loaded_documents``, is rewritten with an explicit
287
+ ``document_id`` equal to ``target_document``'s. A document not
288
+ present in ``loaded_documents`` is never inspected, so a reference
289
+ living there is left exactly as it was.
290
+
291
+ A reference that, before this move, already named some other document
292
+ entirely (neither ``source_document`` nor ``target_document``) is left
293
+ untouched in every case above -- this move never rewrites a reference
294
+ it did not make stale.
295
+
296
+ ``short_id_remap`` is accepted only to match the fixed call shape
297
+ ``move.py`` always uses; references are addressed by ``node_id``, so
298
+ it is never read here.
299
+ """
300
+
301
+ src_id = _document_id(source_document)
302
+ tgt_id = _document_id(target_document)
303
+
304
+ if src_id == tgt_id:
305
+ # No document boundary is actually crossed (including the
306
+ # both-None implicit-document case): nothing needs reconciling.
307
+ return ReferenceDelta(changes=())
308
+
309
+ moved_nodes, moved_ids = _collect_moved(moved_roots)
310
+ moved_object_ids = {id(n) for n in moved_nodes}
311
+
312
+ scanned_docs: Dict[Any, Any] = {}
313
+ if src_id is not None:
314
+ scanned_docs[src_id] = source_document
315
+ if tgt_id is not None:
316
+ scanned_docs.setdefault(tgt_id, target_document)
317
+ for doc_id, document in (loaded_documents or {}).items():
318
+ scanned_docs.setdefault(doc_id, document)
319
+
320
+ outgoing = _rewrite_outgoing(moved_nodes, moved_ids, src_id, tgt_id)
321
+ incoming = _rewrite_incoming(scanned_docs, moved_object_ids, moved_ids, src_id, tgt_id)
322
+
323
+ return ReferenceDelta(changes=tuple(outgoing) + tuple(incoming))