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.
- ai_editor_tree_engine_dev-1.0.176.dist-info/METADATA +35 -0
- ai_editor_tree_engine_dev-1.0.176.dist-info/RECORD +121 -0
- ai_editor_tree_engine_dev-1.0.176.dist-info/WHEEL +5 -0
- ai_editor_tree_engine_dev-1.0.176.dist-info/licenses/LICENSE +21 -0
- ai_editor_tree_engine_dev-1.0.176.dist-info/top_level.txt +1 -0
- tree_engine/PUBLIC_API.md +161 -0
- tree_engine/__init__.py +327 -0
- tree_engine/checks/__init__.py +60 -0
- tree_engine/checks/builtin/__init__.py +5 -0
- tree_engine/checks/builtin/compile_check.py +53 -0
- tree_engine/checks/builtin/csv_syntax.py +51 -0
- tree_engine/checks/builtin/docstrings.py +211 -0
- tree_engine/checks/builtin/encoding.py +55 -0
- tree_engine/checks/builtin/fleet_rules.py +227 -0
- tree_engine/checks/builtin/format_syntax.py +82 -0
- tree_engine/checks/builtin/jsonl_syntax.py +49 -0
- tree_engine/checks/builtin/shipped.py +52 -0
- tree_engine/checks/builtin/syntax.py +118 -0
- tree_engine/checks/builtin/xml_wellformed.py +51 -0
- tree_engine/checks/chain.py +332 -0
- tree_engine/checks/external/__init__.py +5 -0
- tree_engine/checks/external/_base.py +61 -0
- tree_engine/checks/external/bandit.py +53 -0
- tree_engine/checks/external/black.py +48 -0
- tree_engine/checks/external/flake8.py +38 -0
- tree_engine/checks/external/isort.py +38 -0
- tree_engine/checks/external/mypy.py +45 -0
- tree_engine/checks/external/ruff.py +45 -0
- tree_engine/checks/external/runner.py +197 -0
- tree_engine/checks/model.py +110 -0
- tree_engine/checks/options.py +73 -0
- tree_engine/checks/plugin.py +171 -0
- tree_engine/checks/registry.py +349 -0
- tree_engine/core/address.py +376 -0
- tree_engine/core/identifier_map.py +266 -0
- tree_engine/core/identity.py +226 -0
- tree_engine/core/instrumentation.py +399 -0
- tree_engine/core/integrity.py +341 -0
- tree_engine/core/integrity_full.py +382 -0
- tree_engine/core/journal.py +260 -0
- tree_engine/core/journal_compose.py +571 -0
- tree_engine/core/live_tree.py +785 -0
- tree_engine/core/locking.py +188 -0
- tree_engine/core/move.py +585 -0
- tree_engine/core/move_references.py +323 -0
- tree_engine/core/node_types.py +408 -0
- tree_engine/core/nodes.py +348 -0
- tree_engine/core/offsets.py +24 -0
- tree_engine/core/operations.py +825 -0
- tree_engine/core/plugin_boundary.py +160 -0
- tree_engine/core/position_map.py +263 -0
- tree_engine/core/position_transfer.py +512 -0
- tree_engine/core/range_refresh.py +366 -0
- tree_engine/core/reference_cache.py +360 -0
- tree_engine/core/references.py +223 -0
- tree_engine/core/reparse.py +378 -0
- tree_engine/core/short_id.py +296 -0
- tree_engine/core/subtree_apply.py +397 -0
- tree_engine/core/subtree_copy.py +399 -0
- tree_engine/core/transactions.py +397 -0
- tree_engine/core/trivia.py +396 -0
- tree_engine/core/updates.py +398 -0
- tree_engine/core/validation.py +377 -0
- tree_engine/errors.py +104 -0
- tree_engine/exceptions.py +376 -0
- tree_engine/facade.py +4524 -0
- tree_engine/plugins/_statements.py +369 -0
- tree_engine/plugins/bsl/block_edit.py +449 -0
- tree_engine/plugins/bsl/blocks.py +127 -0
- tree_engine/plugins/bsl/checker.py +297 -0
- tree_engine/plugins/bsl/dialects.py +303 -0
- tree_engine/plugins/bsl/generator.py +481 -0
- tree_engine/plugins/bsl/import_map.py +995 -0
- tree_engine/plugins/bsl/plugin.py +452 -0
- tree_engine/plugins/contract.py +355 -0
- tree_engine/plugins/cpp/import_map.py +505 -0
- tree_engine/plugins/cpp/languages.py +99 -0
- tree_engine/plugins/cpp/plugin.py +501 -0
- tree_engine/plugins/csv_format.py +676 -0
- tree_engine/plugins/detachable.py +156 -0
- tree_engine/plugins/fallback.py +421 -0
- tree_engine/plugins/ini_format.py +330 -0
- tree_engine/plugins/json_format.py +813 -0
- tree_engine/plugins/json_pointer.py +203 -0
- tree_engine/plugins/jsonl_format.py +392 -0
- tree_engine/plugins/markdown_format.py +748 -0
- tree_engine/plugins/onec_query_format.py +61 -0
- tree_engine/plugins/parse_position.py +54 -0
- tree_engine/plugins/plain_text.py +1543 -0
- tree_engine/plugins/python/export_map.py +306 -0
- tree_engine/plugins/python/import_map.py +429 -0
- tree_engine/plugins/python/plugin.py +1710 -0
- tree_engine/plugins/registry.py +497 -0
- tree_engine/plugins/scan.py +87 -0
- tree_engine/plugins/selection.py +228 -0
- tree_engine/plugins/sql_format.py +61 -0
- tree_engine/plugins/toml_format.py +1072 -0
- tree_engine/plugins/xml_format.py +911 -0
- tree_engine/plugins/yaml/builder.py +49 -0
- tree_engine/plugins/yaml/emitter.py +225 -0
- tree_engine/plugins/yaml/flow.py +227 -0
- tree_engine/plugins/yaml/plugin.py +458 -0
- tree_engine/plugins/yaml/ranges.py +52 -0
- tree_engine/plugins/yaml/reader.py +359 -0
- tree_engine/plugins/yaml/scanner.py +298 -0
- tree_engine/query/adapter.py +283 -0
- tree_engine/query/engine.py +853 -0
- tree_engine/query/inspection.py +498 -0
- tree_engine/query/outline.py +288 -0
- tree_engine/query/predicates.py +263 -0
- tree_engine/query/rendered_lines.py +46 -0
- tree_engine/query/results.py +206 -0
- tree_engine/query/selector.py +400 -0
- tree_engine/query/xml_query.py +747 -0
- tree_engine/storage/codec.py +400 -0
- tree_engine/storage/document_bridge.py +579 -0
- tree_engine/storage/file_txn.py +459 -0
- tree_engine/storage/history.py +106 -0
- tree_engine/storage/lifecycle.py +622 -0
- tree_engine/storage/schema.py +403 -0
- tree_engine/storage/session_guard.py +398 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Document lock coordinator for transactional concurrency (C-007).
|
|
2
|
+
|
|
3
|
+
Scope (concept C-007, narrowed to the internal mutex/transaction
|
|
4
|
+
coordinator only): this module owns exactly one thing — the
|
|
5
|
+
:class:`DocumentLockCoordinator` that serializes operations on a single
|
|
6
|
+
document and, for a transaction spanning several documents, acquires all
|
|
7
|
+
of their locks in a deterministic order before any of them proceed.
|
|
8
|
+
|
|
9
|
+
Per {p035}: operations on one document are serialized by an internal
|
|
10
|
+
mutex/transaction coordinator. A transaction spanning several documents,
|
|
11
|
+
including cross-document move and reference propagation, takes the locks
|
|
12
|
+
of every participating document in a deterministic order by document_id
|
|
13
|
+
(UUID4) and releases them only after commit or rollback completes, so
|
|
14
|
+
that deadlock and partial publication are impossible. Automatic merging
|
|
15
|
+
of concurrent changes is forbidden — this module never merges anything,
|
|
16
|
+
it only orders and holds locks.
|
|
17
|
+
|
|
18
|
+
Out of scope here, delivered by the sibling step G-015/T-001/A-002: batch
|
|
19
|
+
execution, the isolated transactional working state, expected_version
|
|
20
|
+
checks, and the commit/rollback protocol itself. This module performs no
|
|
21
|
+
validation and no version bookkeeping; it only answers the question
|
|
22
|
+
"whose turn is it to touch this document" and does so in an order that
|
|
23
|
+
can never deadlock.
|
|
24
|
+
|
|
25
|
+
Source-of-truth requirement labels honored here: {p035}.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import threading
|
|
31
|
+
from contextlib import contextmanager
|
|
32
|
+
from typing import Dict, Iterable, Iterator, List
|
|
33
|
+
from uuid import UUID
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"DocumentLockCoordinator",
|
|
37
|
+
"LockOrderError",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class LockOrderError(Exception):
|
|
42
|
+
"""Raised when lock usage would violate the coordinator's invariants.
|
|
43
|
+
|
|
44
|
+
Covers exactly two misuses that would otherwise silently break the
|
|
45
|
+
deadlock-freedom and no-partial-release guarantees from {p035}:
|
|
46
|
+
releasing a document that this coordinator did not report as locked,
|
|
47
|
+
and re-entering :meth:`DocumentLockCoordinator.acquire_locks` for a
|
|
48
|
+
document_id that is already held by the *same* acquire session before
|
|
49
|
+
that session has released it (double-acquire within one call).
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class DocumentLockCoordinator:
|
|
54
|
+
"""Serializes per-document operations and orders multi-document locks.
|
|
55
|
+
|
|
56
|
+
Holds one :class:`threading.Lock` per document_id, created lazily on
|
|
57
|
+
first use and kept for the coordinator's lifetime (locks are cheap and
|
|
58
|
+
reused, never removed, so that concurrent callers always synchronize
|
|
59
|
+
on the same lock object for a given document_id).
|
|
60
|
+
|
|
61
|
+
Thread-safety: the coordinator's own bookkeeping (the registry dict and
|
|
62
|
+
the "currently held" set) is itself protected by a private guard lock.
|
|
63
|
+
That guard is only ever held for the brief instant needed to look up or
|
|
64
|
+
create a per-document lock, or to flip a "locked" flag — never while
|
|
65
|
+
blocking on a document lock — so it cannot itself become a source of
|
|
66
|
+
contention or deadlock.
|
|
67
|
+
|
|
68
|
+
Deadlock freedom: :meth:`acquire_locks` always sorts the requested
|
|
69
|
+
document_ids into strictly ascending UUID4 order before acquiring them
|
|
70
|
+
one at a time in that order. Two transactions that both want documents
|
|
71
|
+
A and B — regardless of which order the caller requested them in — end
|
|
72
|
+
up trying to take A then B, never A-then-B in one thread and B-then-A
|
|
73
|
+
in the other. That per-document total order is exactly what rules out
|
|
74
|
+
the circular-wait condition required for deadlock.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(self) -> None:
|
|
78
|
+
self._locks: Dict[UUID, threading.Lock] = {}
|
|
79
|
+
self._held: set[UUID] = set()
|
|
80
|
+
self._owner: Dict[UUID, int] = {}
|
|
81
|
+
self._guard = threading.Lock()
|
|
82
|
+
|
|
83
|
+
def _lock_for(self, document_id: UUID) -> threading.Lock:
|
|
84
|
+
"""Return the per-document lock for document_id, creating it if new."""
|
|
85
|
+
with self._guard:
|
|
86
|
+
lock = self._locks.get(document_id)
|
|
87
|
+
if lock is None:
|
|
88
|
+
lock = threading.Lock()
|
|
89
|
+
self._locks[document_id] = lock
|
|
90
|
+
return lock
|
|
91
|
+
|
|
92
|
+
def is_locked(self, document_id: UUID) -> bool:
|
|
93
|
+
"""Return True if document_id currently holds a lock via this coordinator."""
|
|
94
|
+
with self._guard:
|
|
95
|
+
return document_id in self._held
|
|
96
|
+
|
|
97
|
+
@contextmanager
|
|
98
|
+
def acquire_locks(self, document_ids: Iterable[UUID]) -> Iterator[List[UUID]]:
|
|
99
|
+
"""Acquire locks for all document_ids in ascending UUID4 order.
|
|
100
|
+
|
|
101
|
+
Duplicate ids are acquired once. Locks are taken one at a time in
|
|
102
|
+
sorted order — never all-at-once and never in caller-supplied
|
|
103
|
+
order — which is what makes concurrent multi-document transactions
|
|
104
|
+
deadlock-free: every caller converges on the same acquisition
|
|
105
|
+
order for any given pair of documents.
|
|
106
|
+
|
|
107
|
+
The context manager enters (yields) only once every requested lock
|
|
108
|
+
is held. On normal exit *or* on an exception propagating out of
|
|
109
|
+
the ``with`` block, it releases exactly the locks it acquired, via
|
|
110
|
+
:meth:`release_locks`, satisfying "release only after commit or
|
|
111
|
+
rollback completes, never partially" for the common case where the
|
|
112
|
+
caller does commit/rollback work inside the ``with`` block.
|
|
113
|
+
|
|
114
|
+
Callers that need to hold locks across a longer-lived commit or
|
|
115
|
+
rollback sequence that does not fit a single ``with`` block may
|
|
116
|
+
instead call :meth:`acquire_locks` as a plain function... but the
|
|
117
|
+
supported, recommended shape is the context manager: do the
|
|
118
|
+
commit/rollback work inside the block, then let it release.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
document_ids: document_id values to lock for this transaction.
|
|
122
|
+
|
|
123
|
+
Yields:
|
|
124
|
+
The sorted, de-duplicated list of document_ids that were
|
|
125
|
+
locked, in the exact order they were acquired.
|
|
126
|
+
|
|
127
|
+
Raises:
|
|
128
|
+
LockOrderError: if the *same thread* re-enters
|
|
129
|
+
:meth:`acquire_locks` for a document_id it already holds
|
|
130
|
+
and has not yet released. Ordinary contention — a
|
|
131
|
+
different thread wanting the same document_id — is not an
|
|
132
|
+
error: that thread simply blocks in this call until the
|
|
133
|
+
holder releases it.
|
|
134
|
+
"""
|
|
135
|
+
ordered = sorted(set(document_ids), key=lambda doc_id: doc_id.int)
|
|
136
|
+
acquired: List[UUID] = []
|
|
137
|
+
current_thread = threading.get_ident()
|
|
138
|
+
try:
|
|
139
|
+
for document_id in ordered:
|
|
140
|
+
with self._guard:
|
|
141
|
+
if self._owner.get(document_id) == current_thread:
|
|
142
|
+
# This exact thread already holds this document's
|
|
143
|
+
# lock in an outer, unreleased session. A plain
|
|
144
|
+
# Lock.acquire() below would block forever on
|
|
145
|
+
# itself, so fail fast instead of self-deadlocking.
|
|
146
|
+
raise LockOrderError(
|
|
147
|
+
f"document {document_id} is already locked by "
|
|
148
|
+
"the current thread; release_locks() must be "
|
|
149
|
+
"called before it can be re-acquired"
|
|
150
|
+
)
|
|
151
|
+
lock = self._lock_for(document_id)
|
|
152
|
+
lock.acquire()
|
|
153
|
+
with self._guard:
|
|
154
|
+
self._held.add(document_id)
|
|
155
|
+
self._owner[document_id] = current_thread
|
|
156
|
+
acquired.append(document_id)
|
|
157
|
+
yield list(acquired)
|
|
158
|
+
finally:
|
|
159
|
+
if acquired:
|
|
160
|
+
self.release_locks(acquired)
|
|
161
|
+
|
|
162
|
+
def release_locks(self, document_ids: Iterable[UUID]) -> None:
|
|
163
|
+
"""Release locks for the given document_ids.
|
|
164
|
+
|
|
165
|
+
Must be called exactly once per :meth:`acquire_locks` session,
|
|
166
|
+
only after the caller's commit or rollback has fully completed —
|
|
167
|
+
never mid-transaction and never partially (all ids passed to one
|
|
168
|
+
call are released together, in this call, with no early return on
|
|
169
|
+
the first failure: every held id is unlocked even if one entry
|
|
170
|
+
turns out to already be free).
|
|
171
|
+
|
|
172
|
+
Releasing a document_id that this coordinator does not currently
|
|
173
|
+
report as held is a no-op for that id: it is not an error, so that
|
|
174
|
+
callers cleaning up in a ``finally`` block after a partial
|
|
175
|
+
acquisition failure can safely pass the full requested set.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
document_ids: document_id values to release.
|
|
179
|
+
"""
|
|
180
|
+
for document_id in document_ids:
|
|
181
|
+
with self._guard:
|
|
182
|
+
if document_id not in self._held:
|
|
183
|
+
continue
|
|
184
|
+
self._held.discard(document_id)
|
|
185
|
+
self._owner.pop(document_id, None)
|
|
186
|
+
lock = self._locks.get(document_id)
|
|
187
|
+
if lock is not None and lock.locked():
|
|
188
|
+
lock.release()
|