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,377 @@
1
+ """Shared preflight mutation-validation pipeline (concept C-006, {p009}).
2
+
3
+ This module implements the single entry point every mutation operation in
4
+ this task family (``insert``, ``replace_range``, ``replace_node_id``, an
5
+ incoming cross-document ``move``, a subtree ``apply``, or a
6
+ ``parse_fragment`` result) must call before any tree or index-map change is
7
+ applied. Per {p009}, :func:`validate_mutation` checks, in order:
8
+
9
+ 1. UUID4 format validity of every node_id carried by the incoming node
10
+ or fragment.
11
+ 2. node_id conflicts against the target document's existing identity
12
+ map.
13
+ 3. parent/position type compatibility.
14
+ 4. cycle formation.
15
+ 5. resolvability of references touched by the change.
16
+ 6. the remaining structural invariants of the tree (here: the
17
+ positional-addressing literal used by ``insert``, per {p107}).
18
+
19
+ Every failure is reported as a distinct :class:`ValidationError` carrying
20
+ the matching :class:`~tree_engine.errors.ErrorCode`, never a generic
21
+ exception. All six stages run to completion and accumulate failures; the
22
+ pipeline never stops at the first one, so independent and combined failure
23
+ scenarios are both fully reported by a single call.
24
+
25
+ For any node arriving without a ``short_id``, and for a fragment carried
26
+ over from another document, this module computes a *plan* describing which
27
+ short_id values must be reassigned -- it never allocates an identifier
28
+ itself. A free short_id (present, and not conflicting with the target
29
+ document) is left completely untouched: it never appears in the plan. A
30
+ missing or conflicting short_id becomes a plan entry; the fresh value is
31
+ obtained by invoking the caller-supplied ``next_short_id`` allocator (the
32
+ existing allocator contract owned by ``short_id.py``), never by any ad hoc
33
+ scheme invented here.
34
+
35
+ Sibling contracts that are not yet available as importable modules in this
36
+ worktree -- the parent/position compatibility rules of ``node_types.py``
37
+ and the ``next_short_id`` allocator of ``short_id.py`` -- are consumed
38
+ here only as caller-injected, duck-typed callables, and the incoming
39
+ node/fragment and the target document are consumed only through the
40
+ minimal structural (duck-typed) attributes documented on
41
+ :func:`validate_mutation`. This file imports neither sibling module, so it
42
+ can be exercised today and wired to the real modules later without any
43
+ change to its own code.
44
+
45
+ This module performs no mutation of any tree or index map, and allocates
46
+ no identifier of its own, in any code path: the atomic application of a
47
+ validated change belongs to the sibling atomic-tree-operations file.
48
+ """
49
+
50
+ from __future__ import annotations
51
+
52
+ from dataclasses import dataclass, field
53
+ from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence
54
+ from uuid import UUID
55
+
56
+ from tree_engine.errors import ErrorCode
57
+
58
+ __all__ = [
59
+ "ValidationError",
60
+ "ValidationResult",
61
+ "ShortIdReassignment",
62
+ "validate_mutation",
63
+ ]
64
+
65
+
66
+ _VALID_POSITIONS = frozenset(
67
+ {"first_child", "last_child", "child_index", "before", "after"}
68
+ )
69
+
70
+
71
+ # --------------------------------------------------------------------------
72
+ # Result types
73
+ # --------------------------------------------------------------------------
74
+
75
+
76
+ @dataclass(frozen=True)
77
+ class ValidationError:
78
+ """One distinct preflight validation failure.
79
+
80
+ ``code`` is the exact :class:`~tree_engine.errors.ErrorCode` naming the
81
+ failure category; a generic exception is never used in its place.
82
+ ``message`` is a human-readable detail for logs and diagnostics.
83
+ ``node_id`` optionally pins the failure to the offending node: a
84
+ ``uuid.UUID`` when the identifier parsed, or the raw offending value
85
+ when it did not (e.g. a malformed UUID4 string).
86
+ """
87
+
88
+ code: ErrorCode
89
+ message: str
90
+ node_id: Optional[Any] = None
91
+
92
+
93
+ @dataclass(frozen=True)
94
+ class ShortIdReassignment:
95
+ """One entry of the short_id reassignment plan.
96
+
97
+ ``node_id`` identifies the node this entry belongs to; it disambiguates
98
+ entries when several incoming nodes share ``old_short_id is None``, so
99
+ the plan cannot be a bare ``{old_short_id: new_short_id}`` dict.
100
+ ``old_short_id`` is the short_id the node arrived with, or ``None``
101
+ when it had none. ``new_short_id`` is the fresh value obtained from the
102
+ caller-supplied ``next_short_id`` allocator, or ``None`` when no
103
+ allocator was supplied to this call (the entry still records that a
104
+ reassignment is required; no value is invented locally).
105
+ """
106
+
107
+ node_id: Any
108
+ old_short_id: Optional[Any]
109
+ new_short_id: Optional[Any]
110
+
111
+
112
+ @dataclass(frozen=True)
113
+ class ValidationResult:
114
+ """Outcome of one :func:`validate_mutation` call.
115
+
116
+ ``success`` is ``True`` exactly when ``errors`` is empty. ``errors`` is
117
+ the ordered list of distinct failures found, in the order the checks
118
+ that produced them ran ({p009}'s six-stage order). ``short_id_plan`` is
119
+ the ordered short_id reassignment plan, populated only when ``success``
120
+ is ``True``: a free short_id is left out entirely, and only a true
121
+ conflict (or a node missing a short_id) appears as an entry.
122
+ """
123
+
124
+ success: bool
125
+ errors: List[ValidationError] = field(default_factory=list)
126
+ short_id_plan: List[ShortIdReassignment] = field(default_factory=list)
127
+
128
+
129
+ # --------------------------------------------------------------------------
130
+ # Internal helpers
131
+ # --------------------------------------------------------------------------
132
+
133
+
134
+ def _parse_uuid4(value: Any) -> Optional[UUID]:
135
+ """Return ``value`` as a UUID4 ``UUID``, or ``None`` if it is not one."""
136
+
137
+ candidate: Optional[UUID]
138
+ if isinstance(value, UUID):
139
+ candidate = value
140
+ else:
141
+ try:
142
+ candidate = UUID(str(value))
143
+ except (ValueError, AttributeError, TypeError):
144
+ candidate = None
145
+ if candidate is None or candidate.version != 4:
146
+ return None
147
+ return candidate
148
+
149
+
150
+ def _iter_fragment_nodes(root: Any) -> List[Any]:
151
+ """Return ``root`` and every descendant, pre-order.
152
+
153
+ ``children`` is read as a duck-typed, optional attribute: absent or
154
+ ``None`` means a leaf. This is the only shape assumed of the incoming
155
+ node/fragment object's internal structure.
156
+ """
157
+
158
+ ordered: List[Any] = []
159
+ stack = [root]
160
+ while stack:
161
+ node = stack.pop(0)
162
+ ordered.append(node)
163
+ children = list(getattr(node, "children", None) or ())
164
+ stack = children + stack
165
+ return ordered
166
+
167
+
168
+ def _node_id_of(node: Any) -> Any:
169
+ return getattr(node, "node_id", None)
170
+
171
+
172
+ def _short_id_of(node: Any) -> Any:
173
+ return getattr(node, "short_id", None)
174
+
175
+
176
+ # --------------------------------------------------------------------------
177
+ # Public entry point
178
+ # --------------------------------------------------------------------------
179
+
180
+
181
+ def validate_mutation(
182
+ document: Any,
183
+ incoming: Any,
184
+ *,
185
+ parent: Any = None,
186
+ position: Optional[str] = None,
187
+ index: Optional[int] = None,
188
+ is_type_compatible: Optional[Callable[[Any, Optional[str], Any], bool]] = None,
189
+ references: Optional[Iterable[Any]] = None,
190
+ loaded_documents: Optional[Dict[Any, Any]] = None,
191
+ used_short_ids: Optional[Sequence[Any]] = None,
192
+ next_short_id: Optional[Callable[[], Any]] = None,
193
+ ) -> ValidationResult:
194
+ """Run the shared {p009} preflight pipeline once and return its result.
195
+
196
+ Args:
197
+ document: the target document. Duck-typed: read via
198
+ ``document.nodes_by_id`` (a mapping of node_id -> node, the
199
+ same attribute name ``identity.replace_node_id`` already
200
+ relies on) and, optionally, ``document.document_id``. Never
201
+ mutated.
202
+ incoming: the node or fragment root being introduced. Duck-typed:
203
+ ``node_id`` (required), optional ``short_id``, optional
204
+ ``children`` (an iterable of same-shaped nodes, for a
205
+ fragment). Never mutated.
206
+ parent: the prospective parent node, when the change attaches
207
+ ``incoming`` under a parent (``insert``, a subtree apply, an
208
+ incoming move). Duck-typed: optional ``node_id`` and ``parent``
209
+ (its own parent, walked upward for cycle detection). ``None``
210
+ when the change has no parent/position dimension (e.g.
211
+ ``replace_node_id``).
212
+ position: one of ``"first_child"``, ``"last_child"``,
213
+ ``"child_index"``, ``"before"``, ``"after"``, or ``None`` when
214
+ not applicable, per the {p107} addressing modes.
215
+ index: the sibling index; required only when ``position ==
216
+ "child_index"``.
217
+ is_type_compatible: the node-type compatibility rules already
218
+ provided by ``node_types.py``, injected as a callable
219
+ ``(parent, position, incoming) -> bool`` because that module is
220
+ not yet importable from this file. When omitted, the
221
+ compatibility check is skipped (treated as compatible) rather
222
+ than guessed at.
223
+ references: the references touched by the change, each duck-typed
224
+ with a required ``node_id`` (or given directly as a bare
225
+ id/UUID) and an optional ``document_id`` (``None`` meaning the
226
+ target document). Resolved against ``document.nodes_by_id``
227
+ for a local reference, or against
228
+ ``loaded_documents[document_id].nodes_by_id`` for a
229
+ cross-document one.
230
+ loaded_documents: mapping of ``document_id -> document-like``
231
+ object (each exposing ``nodes_by_id``) for resolving a
232
+ cross-document reference. A cross-document reference whose
233
+ document is absent here is reported unresolved.
234
+ used_short_ids: the short_id values already allocated in the
235
+ target document, as known by the caller (typically read from
236
+ the document's short_id index maintained by ``short_id.py``).
237
+ next_short_id: the existing ``next_short_id`` allocator from
238
+ ``short_id.py``, injected as a zero-argument callable because
239
+ that module is not yet importable from this file. Called only
240
+ for a node that needs a fresh short_id (missing or
241
+ conflicting); never called for a free short_id, and never
242
+ called at all when validation fails.
243
+
244
+ Returns:
245
+ A :class:`ValidationResult` whose ``success`` is ``True`` iff
246
+ ``errors`` is empty, in which case ``short_id_plan`` carries the
247
+ reassignment plan. No tree, index map, or identifier is ever
248
+ allocated or mutated by this function, in any code path.
249
+ """
250
+
251
+ errors: List[ValidationError] = []
252
+ fragment_nodes = _iter_fragment_nodes(incoming)
253
+ nodes_by_id = getattr(document, "nodes_by_id", None)
254
+ if nodes_by_id is None:
255
+ nodes_by_id = {}
256
+
257
+ # 1. UUID4 format validity, for every node in the incoming node/fragment.
258
+ parsed_ids: List[Optional[UUID]] = []
259
+ for node in fragment_nodes:
260
+ raw_id = _node_id_of(node)
261
+ parsed = _parse_uuid4(raw_id) if raw_id is not None else None
262
+ parsed_ids.append(parsed)
263
+ if parsed is None:
264
+ errors.append(
265
+ ValidationError(
266
+ code=ErrorCode.INVALID_SELECTOR,
267
+ message=f"node_id is not a syntactically valid UUID4: {raw_id!r}",
268
+ node_id=raw_id,
269
+ )
270
+ )
271
+
272
+ # 2. node_id conflicts against the document's existing identity map.
273
+ for parsed in parsed_ids:
274
+ if parsed is not None and parsed in nodes_by_id:
275
+ errors.append(
276
+ ValidationError(
277
+ code=ErrorCode.NODE_ID_CONFLICT,
278
+ message=f"node_id already present in the document: {parsed}",
279
+ node_id=parsed,
280
+ )
281
+ )
282
+
283
+ # 3. parent/position type compatibility, using the node-type
284
+ # compatibility rules injected from node_types.py.
285
+ if is_type_compatible is not None and not is_type_compatible(parent, position, incoming):
286
+ errors.append(
287
+ ValidationError(
288
+ code=ErrorCode.INVALID_PARENT_TYPE,
289
+ message="incoming node type is not admissible under the chosen parent and position",
290
+ node_id=_node_id_of(incoming),
291
+ )
292
+ )
293
+
294
+ # 4. cycle formation: none of the incoming fragment's own node_ids may
295
+ # already sit among the ancestors of the target parent.
296
+ if parent is not None:
297
+ incoming_parsed_ids = {p for p in parsed_ids if p is not None}
298
+ incoming_raw_ids = {_node_id_of(node) for node in fragment_nodes}
299
+ ancestor = parent
300
+ visited_ancestor_object_ids: set = set()
301
+ while ancestor is not None and id(ancestor) not in visited_ancestor_object_ids:
302
+ visited_ancestor_object_ids.add(id(ancestor))
303
+ ancestor_raw_id = getattr(ancestor, "node_id", None)
304
+ ancestor_parsed_id = _parse_uuid4(ancestor_raw_id) if ancestor_raw_id is not None else None
305
+ if ancestor_raw_id in incoming_raw_ids or (
306
+ ancestor_parsed_id is not None and ancestor_parsed_id in incoming_parsed_ids
307
+ ):
308
+ errors.append(
309
+ ValidationError(
310
+ code=ErrorCode.CYCLE_DETECTED,
311
+ message="attaching the incoming node/fragment under this parent would introduce a cycle",
312
+ node_id=_node_id_of(incoming),
313
+ )
314
+ )
315
+ break
316
+ ancestor = getattr(ancestor, "parent", None)
317
+
318
+ # 5. resolvability of references touched by the change.
319
+ own_document_id = getattr(document, "document_id", None)
320
+ for reference in references or ():
321
+ ref_node_id = getattr(reference, "node_id", reference)
322
+ ref_document_id = getattr(reference, "document_id", None)
323
+ if ref_document_id is None or ref_document_id == own_document_id:
324
+ target_map = nodes_by_id
325
+ else:
326
+ target_document = (loaded_documents or {}).get(ref_document_id)
327
+ target_map = getattr(target_document, "nodes_by_id", None) if target_document is not None else None
328
+ if target_map is None or ref_node_id not in target_map:
329
+ errors.append(
330
+ ValidationError(
331
+ code=ErrorCode.UNRESOLVED_REFERENCE,
332
+ message=f"reference cannot be resolved: {ref_node_id!r}",
333
+ node_id=ref_node_id,
334
+ )
335
+ )
336
+
337
+ # 6. remaining structural invariants: the {p107} positional-addressing
338
+ # literal, when a parent/position dimension applies to this change.
339
+ if position is not None and position not in _VALID_POSITIONS:
340
+ errors.append(
341
+ ValidationError(
342
+ code=ErrorCode.INVALID_POSITION,
343
+ message=f"position is not one of {sorted(_VALID_POSITIONS)}: {position!r}",
344
+ )
345
+ )
346
+ elif position == "child_index" and (
347
+ index is None or isinstance(index, bool) or not isinstance(index, int) or index < 0
348
+ ):
349
+ errors.append(
350
+ ValidationError(
351
+ code=ErrorCode.INVALID_POSITION,
352
+ message=f"child_index position requires a non-negative integer index, got {index!r}",
353
+ )
354
+ )
355
+
356
+ if errors:
357
+ return ValidationResult(success=False, errors=errors, short_id_plan=[])
358
+
359
+ # Compute the short_id reassignment plan. Only reachable on success, and
360
+ # no identifier is generated here: a fresh value is only ever obtained
361
+ # by invoking the caller-supplied next_short_id allocator.
362
+ used = set(used_short_ids or ())
363
+ plan: List[ShortIdReassignment] = []
364
+ for node in fragment_nodes:
365
+ old_short_id = _short_id_of(node)
366
+ if old_short_id is not None and old_short_id not in used:
367
+ continue # free short_id: left completely untouched.
368
+ new_short_id = next_short_id() if next_short_id is not None else None
369
+ plan.append(
370
+ ShortIdReassignment(
371
+ node_id=_node_id_of(node),
372
+ old_short_id=old_short_id,
373
+ new_short_id=new_short_id,
374
+ )
375
+ )
376
+
377
+ return ValidationResult(success=True, errors=[], short_id_plan=plan)
tree_engine/errors.py ADDED
@@ -0,0 +1,104 @@
1
+ from __future__ import annotations
2
+
3
+ import enum
4
+ import types
5
+
6
+
7
+ class ErrorLayer(str, enum.Enum):
8
+ """Error layer enumeration per frozen HRS fragment {p023}."""
9
+ CORE = "CORE"
10
+ PLUGIN = "PLUGIN"
11
+ STORAGE = "STORAGE"
12
+
13
+
14
+ class ErrorCode(str, enum.Enum):
15
+ """
16
+ Stable error-code catalog per frozen HRS fragment {p023}.
17
+
18
+ This catalog defines all error codes produced by the tree engine.
19
+ FORMAT_CONTENT_PARSE_FAILED is the only code with a permitted plain-text fallback.
20
+ No exception classes are defined in this file; the typed exception hierarchy
21
+ built on this catalog is delivered later by the public-facade step tied to concept C-021.
22
+ """
23
+ NODE_NOT_FOUND = "NODE_NOT_FOUND"
24
+ NODE_ID_CONFLICT = "NODE_ID_CONFLICT"
25
+ SHORT_ID_CONFLICT = "SHORT_ID_CONFLICT"
26
+ DOCUMENT_VERSION_CONFLICT = "DOCUMENT_VERSION_CONFLICT"
27
+ INVALID_SELECTOR = "INVALID_SELECTOR"
28
+ INVALID_POSITION = "INVALID_POSITION"
29
+ INVALID_PARENT_TYPE = "INVALID_PARENT_TYPE"
30
+ CYCLE_DETECTED = "CYCLE_DETECTED"
31
+ UNRESOLVED_REFERENCE = "UNRESOLVED_REFERENCE"
32
+ FORMAT_UNKNOWN_EXTENSION = "FORMAT_UNKNOWN_EXTENSION"
33
+ FORMAT_EXTENSION_CONFLICT = "FORMAT_EXTENSION_CONFLICT"
34
+ FORMAT_PLUGIN_NOT_FOUND = "FORMAT_PLUGIN_NOT_FOUND"
35
+ PLUGIN_CAPABILITY_NOT_SUPPORTED = "PLUGIN_CAPABILITY_NOT_SUPPORTED"
36
+ FORMAT_CONTENT_PARSE_FAILED = "FORMAT_CONTENT_PARSE_FAILED"
37
+ FORMAT_FRAGMENT_PARSE_FAILED = "FORMAT_FRAGMENT_PARSE_FAILED"
38
+ FORMAT_PLUGIN_CONTRACT_ERROR = "FORMAT_PLUGIN_CONTRACT_ERROR"
39
+ UNSUPPORTED_TRANSLATION = "UNSUPPORTED_TRANSLATION"
40
+ TREE_PAYLOAD_INVALID = "TREE_PAYLOAD_INVALID"
41
+ TREE_SCHEMA_UNSUPPORTED = "TREE_SCHEMA_UNSUPPORTED"
42
+ CHECKSUM_MISMATCH = "CHECKSUM_MISMATCH"
43
+ CONCURRENT_SOURCE_MODIFICATION = "CONCURRENT_SOURCE_MODIFICATION"
44
+ CONCURRENT_TREE_MODIFICATION = "CONCURRENT_TREE_MODIFICATION"
45
+ STORAGE_IO_ERROR = "STORAGE_IO_ERROR"
46
+ # tree_engine.checks (TODO 15a86bcb, decision D-BZ): file-check run failures, PLUGIN layer.
47
+ CHECK_TOOL_CRASHED = "CHECK_TOOL_CRASHED"
48
+ CHECK_TOOL_TIMEOUT = "CHECK_TOOL_TIMEOUT"
49
+ CHECK_TOOL_MISSING = "CHECK_TOOL_MISSING"
50
+ CHECK_CONFIG_INVALID = "CHECK_CONFIG_INVALID"
51
+
52
+
53
+ _ERROR_CODE_LAYER_MAP = {
54
+ ErrorCode.NODE_NOT_FOUND: ErrorLayer.CORE,
55
+ ErrorCode.NODE_ID_CONFLICT: ErrorLayer.CORE,
56
+ ErrorCode.SHORT_ID_CONFLICT: ErrorLayer.CORE,
57
+ ErrorCode.DOCUMENT_VERSION_CONFLICT: ErrorLayer.CORE,
58
+ ErrorCode.INVALID_SELECTOR: ErrorLayer.CORE,
59
+ ErrorCode.INVALID_POSITION: ErrorLayer.CORE,
60
+ ErrorCode.INVALID_PARENT_TYPE: ErrorLayer.CORE,
61
+ ErrorCode.CYCLE_DETECTED: ErrorLayer.CORE,
62
+ ErrorCode.UNRESOLVED_REFERENCE: ErrorLayer.CORE,
63
+ ErrorCode.CONCURRENT_TREE_MODIFICATION: ErrorLayer.CORE,
64
+ ErrorCode.FORMAT_UNKNOWN_EXTENSION: ErrorLayer.PLUGIN,
65
+ ErrorCode.FORMAT_EXTENSION_CONFLICT: ErrorLayer.PLUGIN,
66
+ ErrorCode.FORMAT_PLUGIN_NOT_FOUND: ErrorLayer.PLUGIN,
67
+ ErrorCode.PLUGIN_CAPABILITY_NOT_SUPPORTED: ErrorLayer.PLUGIN,
68
+ ErrorCode.FORMAT_CONTENT_PARSE_FAILED: ErrorLayer.PLUGIN,
69
+ ErrorCode.FORMAT_FRAGMENT_PARSE_FAILED: ErrorLayer.PLUGIN,
70
+ ErrorCode.FORMAT_PLUGIN_CONTRACT_ERROR: ErrorLayer.PLUGIN,
71
+ ErrorCode.UNSUPPORTED_TRANSLATION: ErrorLayer.PLUGIN,
72
+ ErrorCode.TREE_PAYLOAD_INVALID: ErrorLayer.STORAGE,
73
+ ErrorCode.TREE_SCHEMA_UNSUPPORTED: ErrorLayer.STORAGE,
74
+ ErrorCode.CHECKSUM_MISMATCH: ErrorLayer.STORAGE,
75
+ ErrorCode.CONCURRENT_SOURCE_MODIFICATION: ErrorLayer.STORAGE,
76
+ ErrorCode.STORAGE_IO_ERROR: ErrorLayer.STORAGE,
77
+ ErrorCode.CHECK_TOOL_CRASHED: ErrorLayer.PLUGIN,
78
+ ErrorCode.CHECK_TOOL_TIMEOUT: ErrorLayer.PLUGIN,
79
+ ErrorCode.CHECK_TOOL_MISSING: ErrorLayer.PLUGIN,
80
+ ErrorCode.CHECK_CONFIG_INVALID: ErrorLayer.PLUGIN,
81
+ }
82
+
83
+ # Totality guard: verify that every ErrorCode is mapped exactly once
84
+ _all_codes = set(ErrorCode)
85
+ _mapped_codes = set(_ERROR_CODE_LAYER_MAP.keys())
86
+ assert _mapped_codes == _all_codes, (
87
+ f"ErrorCode mapping is not total: missing {_all_codes - _mapped_codes}, "
88
+ f"extra {_mapped_codes - _all_codes}"
89
+ )
90
+
91
+ ERROR_CODE_LAYER = types.MappingProxyType(_ERROR_CODE_LAYER_MAP)
92
+
93
+
94
+ def error_layer(code: ErrorCode) -> ErrorLayer:
95
+ """
96
+ Return the layer (CORE, PLUGIN, or STORAGE) that owns the given error code.
97
+
98
+ Args:
99
+ code: An ErrorCode member.
100
+
101
+ Returns:
102
+ The ErrorLayer that owns this code.
103
+ """
104
+ return ERROR_CODE_LAYER[code]