synth-code 0.0.1__py3-none-macosx_15_0_arm64.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.
- synth_code/__init__.py +0 -0
- synth_code/account.py +551 -0
- synth_code/account_http.py +86 -0
- synth_code/account_refusal.py +80 -0
- synth_code/accounting.py +148 -0
- synth_code/activity_grouping.py +149 -0
- synth_code/agent_panels.py +127 -0
- synth_code/approvals.py +397 -0
- synth_code/blobs.py +31 -0
- synth_code/cli.py +734 -0
- synth_code/client.py +413 -0
- synth_code/composer_delivery.py +139 -0
- synth_code/conversation.py +949 -0
- synth_code/daemon.py +34 -0
- synth_code/edition.py +306 -0
- synth_code/enrollment.py +304 -0
- synth_code/file_edit_facts.py +10 -0
- synth_code/freeze.py +234 -0
- synth_code/grant.py +59 -0
- synth_code/history.py +140 -0
- synth_code/history_generated.py +219 -0
- synth_code/history_wire.py +166 -0
- synth_code/image_input.py +109 -0
- synth_code/image_preview.py +105 -0
- synth_code/legacy_history.py +161 -0
- synth_code/live_turns.py +171 -0
- synth_code/model_picker.py +311 -0
- synth_code/native/manifest.json +1 -0
- synth_code/native/synth-code-runtime +0 -0
- synth_code/native.py +95 -0
- synth_code/oauth.py +284 -0
- synth_code/operator.py +116 -0
- synth_code/pack_source.py +65 -0
- synth_code/panel_request.py +291 -0
- synth_code/panels.py +40 -0
- synth_code/plugin_host.py +459 -0
- synth_code/protocol.py +35 -0
- synth_code/provider.py +313 -0
- synth_code/provider_fault.py +173 -0
- synth_code/provider_frames.py +571 -0
- synth_code/recommendation.py +324 -0
- synth_code/report.py +72 -0
- synth_code/research_events.py +44 -0
- synth_code/research_trace.py +449 -0
- synth_code/rex_view.py +58 -0
- synth_code/runtime_refusal.py +108 -0
- synth_code/schema/budgets.json +17 -0
- synth_code/schema/history-v2.json +1002 -0
- synth_code/session.py +291 -0
- synth_code/shoal.py +564 -0
- synth_code/side_panel.py +309 -0
- synth_code/skills.py +269 -0
- synth_code/slash.py +425 -0
- synth_code/source_view.py +135 -0
- synth_code/starter.json +13 -0
- synth_code/stdio.py +75 -0
- synth_code/stream.py +142 -0
- synth_code/subscriptions.py +267 -0
- synth_code/synth_authority.py +362 -0
- synth_code/synth_extra.py +32 -0
- synth_code/terminal.py +817 -0
- synth_code/terminal_journal.py +284 -0
- synth_code/terminal_layout.py +643 -0
- synth_code/terminal_surfaces.py +470 -0
- synth_code/terminal_viewport.py +125 -0
- synth_code/tool_render.py +86 -0
- synth_code/transcript_frames.py +212 -0
- synth_code/transcript_placement.py +63 -0
- synth_code/transcript_rows.py +456 -0
- synth_code/transport.py +243 -0
- synth_code/v001_history.py +468 -0
- synth_code/view.py +854 -0
- synth_code/views/__init__.py +93 -0
- synth_code/views/charts.py +196 -0
- synth_code/views/curses_adapter.py +116 -0
- synth_code/views/engine.py +170 -0
- synth_code/views/model.py +226 -0
- synth_code/views/primitives.py +270 -0
- synth_code/views/tiling.py +124 -0
- synth_code-0.0.1.dist-info/METADATA +50 -0
- synth_code-0.0.1.dist-info/RECORD +110 -0
- synth_code-0.0.1.dist-info/WHEEL +5 -0
- synth_code-0.0.1.dist-info/entry_points.txt +6 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-APACHE +190 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-MIT +21 -0
- synth_code-0.0.1.dist-info/licenses/temper/SOURCE.json +433 -0
- synth_code-0.0.1.dist-info/top_level.txt +2 -0
- synth_code_plugins/_sdk/__init__.py +19 -0
- synth_code_plugins/_sdk/callback_wire.py +240 -0
- synth_code_plugins/_sdk/callbacks.py +317 -0
- synth_code_plugins/_sdk/compliance.py +51 -0
- synth_code_plugins/_sdk/contracts.py +236 -0
- synth_code_plugins/_sdk/discovery.py +75 -0
- synth_code_plugins/_sdk/job_binding.py +71 -0
- synth_code_plugins/_sdk/lifecycle.py +50 -0
- synth_code_plugins/_sdk/lifecycle_generated.py +15 -0
- synth_code_plugins/_sdk/manifest.py +269 -0
- synth_code_plugins/_sdk/pack.py +332 -0
- synth_code_plugins/_sdk/push.py +98 -0
- synth_code_plugins/_sdk/records.py +26 -0
- synth_code_plugins/_sdk/registry.py +491 -0
- synth_code_plugins/_sdk/schemas.py +210 -0
- synth_code_plugins/_sdk/sources.py +230 -0
- synth_code_plugins/_sdk/surfaces.py +54 -0
- synth_code_plugins/_sdk/tools.py +172 -0
- synth_code_plugins/_sdk/worker_memory.py +49 -0
- synth_code_plugins/core/SKILL.md +6 -0
- synth_code_plugins/core/__init__.py +21 -0
- synth_code_plugins/core/plugin.toml +44 -0
- synth_code_plugins/core/source.py +18 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
"""Incremental display rows with immutable prefixes published to the painter."""
|
|
2
|
+
|
|
3
|
+
from bisect import bisect_right
|
|
4
|
+
from collections import defaultdict
|
|
5
|
+
from collections.abc import Mapping, Sequence
|
|
6
|
+
from copy import copy
|
|
7
|
+
from dataclasses import replace
|
|
8
|
+
|
|
9
|
+
from .activity_grouping import ToolRows, group_activity
|
|
10
|
+
|
|
11
|
+
ROW_CHUNK = 256
|
|
12
|
+
CHUNK_BRANCH = 16
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _chunk_at(node, stride, index):
|
|
16
|
+
while stride > 1:
|
|
17
|
+
branch, index = divmod(index, stride)
|
|
18
|
+
node = node[branch]
|
|
19
|
+
stride //= CHUNK_BRANCH
|
|
20
|
+
return node[index]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _put_chunk(node, stride, index, chunk):
|
|
24
|
+
branch, rest = divmod(index, stride)
|
|
25
|
+
if stride > 1:
|
|
26
|
+
child = node[branch] if branch < len(node) else ()
|
|
27
|
+
chunk = _put_chunk(child, stride // CHUNK_BRANCH, rest, chunk)
|
|
28
|
+
return (*node[:branch], chunk, *node[branch + 1:])
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _take_chunks(node, stride, count):
|
|
32
|
+
if count >= stride * len(node):
|
|
33
|
+
return node
|
|
34
|
+
branch, rest = divmod(count, stride)
|
|
35
|
+
head = node[:branch]
|
|
36
|
+
if not rest:
|
|
37
|
+
return head
|
|
38
|
+
return (*head, _take_chunks(node[branch], stride // CHUNK_BRANCH, rest))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class RowBuffer:
|
|
42
|
+
"""Keep complete row chunks immutable and fork only the changed path and tail."""
|
|
43
|
+
|
|
44
|
+
def __init__(self):
|
|
45
|
+
self._root = ()
|
|
46
|
+
self._stride = 1
|
|
47
|
+
self._tail = []
|
|
48
|
+
self.length = 0
|
|
49
|
+
|
|
50
|
+
def __len__(self):
|
|
51
|
+
return self.length
|
|
52
|
+
|
|
53
|
+
def __getitem__(self, index):
|
|
54
|
+
if isinstance(index, slice):
|
|
55
|
+
return [self[i] for i in range(*index.indices(self.length))]
|
|
56
|
+
chunk, offset = divmod(index, ROW_CHUNK)
|
|
57
|
+
tail = (self.length - len(self._tail)) // ROW_CHUNK
|
|
58
|
+
if chunk == tail:
|
|
59
|
+
return self._tail[offset]
|
|
60
|
+
return _chunk_at(self._root, self._stride, chunk)[offset]
|
|
61
|
+
|
|
62
|
+
def append(self, row):
|
|
63
|
+
if len(self._tail) == ROW_CHUNK:
|
|
64
|
+
index = self.length // ROW_CHUNK - 1
|
|
65
|
+
if index == CHUNK_BRANCH * self._stride:
|
|
66
|
+
self._root = (self._root,)
|
|
67
|
+
self._stride *= CHUNK_BRANCH
|
|
68
|
+
self._root = _put_chunk(self._root, self._stride, index, tuple(self._tail))
|
|
69
|
+
self._tail = []
|
|
70
|
+
self._tail.append(row)
|
|
71
|
+
self.length += 1
|
|
72
|
+
|
|
73
|
+
def extend(self, rows):
|
|
74
|
+
# Fill the tail a chunk-sized slice at a time: one C-level extend per chunk,
|
|
75
|
+
# not a Python call per row (catch-up re-emits thousands of rows per frame).
|
|
76
|
+
rows = rows if isinstance(rows, (list, tuple)) else list(rows)
|
|
77
|
+
taken = 0
|
|
78
|
+
count = len(rows)
|
|
79
|
+
while taken < count:
|
|
80
|
+
if len(self._tail) == ROW_CHUNK:
|
|
81
|
+
self.append(rows[taken])
|
|
82
|
+
taken += 1
|
|
83
|
+
continue
|
|
84
|
+
room = ROW_CHUNK - len(self._tail)
|
|
85
|
+
piece = rows[taken:taken + room]
|
|
86
|
+
self._tail.extend(piece)
|
|
87
|
+
self.length += len(piece)
|
|
88
|
+
taken += len(piece)
|
|
89
|
+
|
|
90
|
+
def span(self, start, stop):
|
|
91
|
+
"""Rows `[start, stop)` as a list, copied a chunk slice at a time."""
|
|
92
|
+
out = []
|
|
93
|
+
root_rows = self.length - len(self._tail)
|
|
94
|
+
while start < stop:
|
|
95
|
+
if start >= root_rows:
|
|
96
|
+
piece = self._tail[start - root_rows:stop - root_rows]
|
|
97
|
+
else:
|
|
98
|
+
chunk, offset = divmod(start, ROW_CHUNK)
|
|
99
|
+
piece = _chunk_at(self._root, self._stride, chunk)[offset:offset + stop - start]
|
|
100
|
+
out.extend(piece)
|
|
101
|
+
start += len(piece)
|
|
102
|
+
return out
|
|
103
|
+
|
|
104
|
+
def prefix(self, length):
|
|
105
|
+
result = RowBuffer()
|
|
106
|
+
full = (length - 1) // ROW_CHUNK if length else 0
|
|
107
|
+
remainder = length - full * ROW_CHUNK
|
|
108
|
+
root_count = (self.length - len(self._tail)) // ROW_CHUNK
|
|
109
|
+
if remainder:
|
|
110
|
+
chunk = self._tail if full == root_count else _chunk_at(self._root, self._stride, full)
|
|
111
|
+
result._tail = list(chunk[:remainder])
|
|
112
|
+
result._stride = self._stride
|
|
113
|
+
result._root = _take_chunks(self._root, self._stride, full)
|
|
114
|
+
result.length = length
|
|
115
|
+
return result
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class RowPrefix(Sequence):
|
|
119
|
+
"""Keep a returned row span's length as later frames append to its backing rows."""
|
|
120
|
+
|
|
121
|
+
def __init__(self, rows):
|
|
122
|
+
self._rows = rows
|
|
123
|
+
self._length = len(rows)
|
|
124
|
+
|
|
125
|
+
def __len__(self):
|
|
126
|
+
return self._length
|
|
127
|
+
|
|
128
|
+
def __getitem__(self, index):
|
|
129
|
+
if isinstance(index, slice):
|
|
130
|
+
start, stop, step = index.indices(self._length)
|
|
131
|
+
if step == 1:
|
|
132
|
+
rows = self._rows
|
|
133
|
+
return rows.span(start, stop) if isinstance(rows, RowBuffer) else rows[start:stop]
|
|
134
|
+
return [self._rows[i] for i in range(start, stop, step)]
|
|
135
|
+
if index < 0:
|
|
136
|
+
index += self._length
|
|
137
|
+
if not 0 <= index < self._length:
|
|
138
|
+
raise IndexError(index)
|
|
139
|
+
return self._rows[index]
|
|
140
|
+
|
|
141
|
+
def __iter__(self):
|
|
142
|
+
if isinstance(self._rows, RowBuffer):
|
|
143
|
+
# A chunk slice at a time, not an indexed lookup per row.
|
|
144
|
+
return iter(self._rows.span(0, self._length))
|
|
145
|
+
return (self._rows[index] for index in range(self._length))
|
|
146
|
+
|
|
147
|
+
def __eq__(self, other):
|
|
148
|
+
if not isinstance(other, Sequence):
|
|
149
|
+
return NotImplemented
|
|
150
|
+
return len(self) == len(other) and all(a == b for a, b in zip(self, other, strict=True))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class Frame:
|
|
154
|
+
"""Checkpoints at block boundaries let a redraw reuse its unchanged prefix."""
|
|
155
|
+
|
|
156
|
+
def __init__(self):
|
|
157
|
+
self.width = None
|
|
158
|
+
self.rows = RowBuffer()
|
|
159
|
+
self.checkpoints = [(0, None)]
|
|
160
|
+
self.blocks = []
|
|
161
|
+
self.block_rows = {}
|
|
162
|
+
self._anchors = []
|
|
163
|
+
self.tool_rows = {}
|
|
164
|
+
self.tools = {}
|
|
165
|
+
self.children = defaultdict(set)
|
|
166
|
+
self._tool_blocks = {}
|
|
167
|
+
self.tool_revision = 0
|
|
168
|
+
self._group_key = None
|
|
169
|
+
self._group_overlay = None
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def count(self):
|
|
173
|
+
return len(self.blocks)
|
|
174
|
+
|
|
175
|
+
def rewind(self, start, row):
|
|
176
|
+
if start == 0:
|
|
177
|
+
# A full rebuild drops every record at once instead of popping per block.
|
|
178
|
+
self.rows = RowBuffer()
|
|
179
|
+
if self.tools:
|
|
180
|
+
self.tool_revision += 1
|
|
181
|
+
self.blocks.clear()
|
|
182
|
+
self.block_rows.clear()
|
|
183
|
+
self._anchors.clear()
|
|
184
|
+
self.tool_rows.clear()
|
|
185
|
+
self.tools.clear()
|
|
186
|
+
self.children.clear()
|
|
187
|
+
self._tool_blocks.clear()
|
|
188
|
+
del self.checkpoints[1:]
|
|
189
|
+
return
|
|
190
|
+
if row < len(self.rows):
|
|
191
|
+
self.rows = self.rows.prefix(row)
|
|
192
|
+
for index in range(self.count - 1, start - 1, -1):
|
|
193
|
+
sequence, key = self.blocks[index]
|
|
194
|
+
span = self.block_rows.get(sequence)
|
|
195
|
+
if span is not None and span[0] >= row:
|
|
196
|
+
self.block_rows.pop(sequence)
|
|
197
|
+
self._anchors.pop()
|
|
198
|
+
if key is not None:
|
|
199
|
+
self.tool_rows.pop(key, None)
|
|
200
|
+
anchor = self._tool_blocks.pop(index, None)
|
|
201
|
+
tool = self.tools.pop(anchor, None)
|
|
202
|
+
if tool is not None:
|
|
203
|
+
self.tool_revision += 1
|
|
204
|
+
if tool is not None and tool.parent is not None:
|
|
205
|
+
children = self.children[tool.parent]
|
|
206
|
+
children.remove(tool.anchor)
|
|
207
|
+
if not children:
|
|
208
|
+
self.children.pop(tool.parent)
|
|
209
|
+
del self.blocks[start:]
|
|
210
|
+
del self.checkpoints[start + 1:]
|
|
211
|
+
|
|
212
|
+
def detach(self):
|
|
213
|
+
"""Start an empty frame; return the earlier one, whose records stay intact."""
|
|
214
|
+
earlier = copy(self)
|
|
215
|
+
self.rows = RowBuffer()
|
|
216
|
+
if self.tools:
|
|
217
|
+
self.tool_revision += 1
|
|
218
|
+
self.checkpoints = self.checkpoints[:1]
|
|
219
|
+
self.blocks = []
|
|
220
|
+
self.block_rows = {}
|
|
221
|
+
self._anchors = []
|
|
222
|
+
self.tool_rows = {}
|
|
223
|
+
self.tools = {}
|
|
224
|
+
self.children = defaultdict(set)
|
|
225
|
+
self._tool_blocks = {}
|
|
226
|
+
return earlier
|
|
227
|
+
|
|
228
|
+
def splice(self, earlier, first, stop):
|
|
229
|
+
"""Append blocks `[first, stop)` of `earlier`, whose rows are unchanged, shifted."""
|
|
230
|
+
row = earlier.checkpoints[first][0]
|
|
231
|
+
shift = len(self.rows) - row
|
|
232
|
+
self.rows.extend(earlier.rows.span(row, earlier.checkpoints[stop][0]))
|
|
233
|
+
block_rows, tool_rows = earlier.block_rows, earlier.tool_rows
|
|
234
|
+
tool_blocks, tools = earlier._tool_blocks, earlier.tools
|
|
235
|
+
entries = earlier.blocks[first:stop]
|
|
236
|
+
# Bulk records: per-block method calls dominate a long splice.
|
|
237
|
+
own = self.block_rows
|
|
238
|
+
spans = [
|
|
239
|
+
(sequence, (span[0] + shift, span[1] + shift))
|
|
240
|
+
for sequence, _ in entries
|
|
241
|
+
if (span := block_rows.get(sequence)) is not None and span[0] >= row and sequence not in own
|
|
242
|
+
]
|
|
243
|
+
own.update(spans)
|
|
244
|
+
self._anchors.extend((span[0], sequence) for sequence, span in spans)
|
|
245
|
+
own_tool_rows, own_tool_blocks, own_tools, children = (
|
|
246
|
+
self.tool_rows, self._tool_blocks, self.tools, self.children,
|
|
247
|
+
)
|
|
248
|
+
shifted = False
|
|
249
|
+
for index, (_, key) in enumerate(entries, first):
|
|
250
|
+
if key is None:
|
|
251
|
+
continue
|
|
252
|
+
if (tool_row := tool_rows.get(key)) is not None:
|
|
253
|
+
own_tool_rows[key] = tool_row + shift
|
|
254
|
+
if (tool := tools.get(tool_blocks.get(index))) is not None:
|
|
255
|
+
# Inline `set_tool`: one revision bump for the whole splice.
|
|
256
|
+
anchor = tool.anchor
|
|
257
|
+
own_tool_blocks[index] = anchor
|
|
258
|
+
own_tools[anchor] = tool if not shift else ToolRows(
|
|
259
|
+
anchor, tool.parent, tool.kind, tool.handled, tool.start + shift, tool.stop + shift,
|
|
260
|
+
)
|
|
261
|
+
if tool.parent is not None:
|
|
262
|
+
children[tool.parent].add(anchor)
|
|
263
|
+
shifted = True
|
|
264
|
+
if shifted:
|
|
265
|
+
self.tool_revision += 1
|
|
266
|
+
self.blocks.extend(entries)
|
|
267
|
+
self.checkpoints.extend(
|
|
268
|
+
(end + shift, style) for end, style in earlier.checkpoints[first + 1 : stop + 1]
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
def set_block(self, sequence, start, stop):
|
|
272
|
+
if sequence not in self.block_rows:
|
|
273
|
+
self.block_rows[sequence] = (start, stop)
|
|
274
|
+
self._anchors.append((start, sequence))
|
|
275
|
+
|
|
276
|
+
def anchor_at(self, row):
|
|
277
|
+
index = bisect_right(self._anchors, row, key=lambda anchor: anchor[0]) - 1
|
|
278
|
+
if index >= 0:
|
|
279
|
+
return self._anchors[index][1]
|
|
280
|
+
return None
|
|
281
|
+
|
|
282
|
+
def set_tool(self, index, tool):
|
|
283
|
+
self.tool_revision += 1
|
|
284
|
+
self._tool_blocks[index] = tool.anchor
|
|
285
|
+
self.tools[tool.anchor] = tool
|
|
286
|
+
if tool.parent is not None:
|
|
287
|
+
self.children[tool.parent].add(tool.anchor)
|
|
288
|
+
|
|
289
|
+
def group_overlay(self, groups, width):
|
|
290
|
+
key = groups, width, self.tool_revision
|
|
291
|
+
rows = RowPrefix(self.rows)
|
|
292
|
+
if key != self._group_key:
|
|
293
|
+
hidden, insertions = group_activity(rows, self.group_tools(groups), groups, width)
|
|
294
|
+
self._group_overlay = RowOverlay(rows, hidden, insertions)
|
|
295
|
+
self._group_key = key
|
|
296
|
+
return self._group_overlay.with_rows(rows)
|
|
297
|
+
|
|
298
|
+
def group_tools(self, groups):
|
|
299
|
+
anchors = {anchor for group in groups for anchor in group.selected}
|
|
300
|
+
pending = set()
|
|
301
|
+
for group in groups:
|
|
302
|
+
if group.anchor not in self.tools:
|
|
303
|
+
continue
|
|
304
|
+
anchors.add(group.anchor)
|
|
305
|
+
children = self.children.get(group.anchor, ())
|
|
306
|
+
if group.include_descendants:
|
|
307
|
+
pending.update(children)
|
|
308
|
+
else:
|
|
309
|
+
anchors.update(children)
|
|
310
|
+
descendants = set()
|
|
311
|
+
while pending:
|
|
312
|
+
anchor = pending.pop()
|
|
313
|
+
descendants.add(anchor)
|
|
314
|
+
children = self.children.get(anchor)
|
|
315
|
+
if children:
|
|
316
|
+
pending.update(children - descendants)
|
|
317
|
+
anchors.update(descendants)
|
|
318
|
+
return sorted(
|
|
319
|
+
(self.tools[anchor] for anchor in anchors if anchor in self.tools),
|
|
320
|
+
key=lambda tool: tool.start,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
class RowOverlay(RowPrefix):
|
|
325
|
+
"""Map sparse row insertions and hidden intervals onto an existing row view."""
|
|
326
|
+
|
|
327
|
+
def __init__(self, rows, hidden, insertions):
|
|
328
|
+
self._rows = rows
|
|
329
|
+
self._length = 0
|
|
330
|
+
self.inserted = {}
|
|
331
|
+
self._parts = []
|
|
332
|
+
self._starts = []
|
|
333
|
+
self._base = []
|
|
334
|
+
self._base_starts = []
|
|
335
|
+
counts = defaultdict(int)
|
|
336
|
+
entries = defaultdict(list)
|
|
337
|
+
for start, stop in hidden:
|
|
338
|
+
counts[start] += 1
|
|
339
|
+
counts[stop] -= 1
|
|
340
|
+
for offset, key, row in insertions:
|
|
341
|
+
entries[offset].append((key, row))
|
|
342
|
+
previous = 0
|
|
343
|
+
depth = 0
|
|
344
|
+
for offset in sorted(counts.keys() | entries.keys()):
|
|
345
|
+
if depth == 0 and previous < offset:
|
|
346
|
+
self._base_starts.append(previous)
|
|
347
|
+
self._base.append((offset, self._length))
|
|
348
|
+
self._starts.append(self._length)
|
|
349
|
+
self._parts.append((previous, None))
|
|
350
|
+
self._length += offset - previous
|
|
351
|
+
for key, row in entries.get(offset, ()):
|
|
352
|
+
self.inserted[key] = self._length
|
|
353
|
+
self._starts.append(self._length)
|
|
354
|
+
self._parts.append((offset, row))
|
|
355
|
+
self._length += 1
|
|
356
|
+
previous = offset
|
|
357
|
+
depth += counts.get(offset, 0)
|
|
358
|
+
|
|
359
|
+
assert depth == 0
|
|
360
|
+
self._tail_base = previous
|
|
361
|
+
self._tail_visible = self._length
|
|
362
|
+
self._length += len(rows) - previous
|
|
363
|
+
|
|
364
|
+
def with_rows(self, rows):
|
|
365
|
+
view = copy(self)
|
|
366
|
+
view._rows = rows
|
|
367
|
+
view._length = self._tail_visible + len(rows) - self._tail_base
|
|
368
|
+
return view
|
|
369
|
+
|
|
370
|
+
def __getitem__(self, index):
|
|
371
|
+
if isinstance(index, slice):
|
|
372
|
+
return [self[i] for i in range(*index.indices(len(self)))]
|
|
373
|
+
if index < 0:
|
|
374
|
+
index += len(self)
|
|
375
|
+
if not 0 <= index < len(self):
|
|
376
|
+
raise IndexError(index)
|
|
377
|
+
if index >= self._tail_visible:
|
|
378
|
+
return self._rows[self._tail_base + index - self._tail_visible]
|
|
379
|
+
part = bisect_right(self._starts, index) - 1
|
|
380
|
+
start, inserted = self._parts[part]
|
|
381
|
+
return inserted if inserted is not None else self._rows[start + index - self._starts[part]]
|
|
382
|
+
|
|
383
|
+
def __iter__(self):
|
|
384
|
+
return (self[index] for index in range(len(self)))
|
|
385
|
+
|
|
386
|
+
def base_index(self, index):
|
|
387
|
+
if index >= self._tail_visible:
|
|
388
|
+
return self._tail_base + index - self._tail_visible
|
|
389
|
+
part = bisect_right(self._starts, index) - 1
|
|
390
|
+
if part < 0:
|
|
391
|
+
return -1
|
|
392
|
+
start, inserted = self._parts[part]
|
|
393
|
+
if inserted is not None:
|
|
394
|
+
return self.previous_visible_base(start - 1)
|
|
395
|
+
return start + index - self._starts[part]
|
|
396
|
+
|
|
397
|
+
def previous_visible_base(self, index):
|
|
398
|
+
if index >= self._tail_base:
|
|
399
|
+
return min(index, len(self._rows) - 1)
|
|
400
|
+
part = bisect_right(self._base_starts, index) - 1
|
|
401
|
+
if part >= 0:
|
|
402
|
+
stop, _ = self._base[part]
|
|
403
|
+
return min(index, stop - 1)
|
|
404
|
+
return -1
|
|
405
|
+
|
|
406
|
+
def position(self, index):
|
|
407
|
+
if index >= self._tail_base:
|
|
408
|
+
return self._tail_visible + index - self._tail_base
|
|
409
|
+
part = bisect_right(self._base_starts, index) - 1
|
|
410
|
+
if part >= 0:
|
|
411
|
+
stop, position = self._base[part]
|
|
412
|
+
if index < stop:
|
|
413
|
+
return position + index - self._base_starts[part]
|
|
414
|
+
return None
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
class AdjustedAnchors(Mapping):
|
|
418
|
+
"""Translate only the anchors looked up by the painter."""
|
|
419
|
+
|
|
420
|
+
def __init__(self, anchors, overlay, *, span=False):
|
|
421
|
+
self._anchors = anchors
|
|
422
|
+
self._overlay = overlay
|
|
423
|
+
self._span = span
|
|
424
|
+
|
|
425
|
+
def __getitem__(self, key):
|
|
426
|
+
anchor = self._anchors[key]
|
|
427
|
+
if self._span:
|
|
428
|
+
start, stop = anchor
|
|
429
|
+
first = self._overlay.position(start)
|
|
430
|
+
last = self._overlay.position(stop - 1)
|
|
431
|
+
if first is not None and last is not None:
|
|
432
|
+
return first, last + 1
|
|
433
|
+
else:
|
|
434
|
+
offset = self._overlay.position(anchor)
|
|
435
|
+
if offset is not None:
|
|
436
|
+
return offset
|
|
437
|
+
raise KeyError(key)
|
|
438
|
+
|
|
439
|
+
def __iter__(self):
|
|
440
|
+
return (key for key in self._anchors if key in self)
|
|
441
|
+
|
|
442
|
+
def __len__(self):
|
|
443
|
+
return sum(1 for _ in self)
|
|
444
|
+
|
|
445
|
+
def __bool__(self):
|
|
446
|
+
return bool(self._anchors)
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def grouped_frame(frame, groups, width):
|
|
450
|
+
overlay = frame.group_overlay(groups, width)
|
|
451
|
+
return (
|
|
452
|
+
overlay,
|
|
453
|
+
AdjustedAnchors(frame.block_rows, overlay, span=True),
|
|
454
|
+
AdjustedAnchors(frame.tool_rows, overlay),
|
|
455
|
+
overlay.inserted,
|
|
456
|
+
)
|
synth_code/transport.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"""Persistent, bounded NDJSON transport over the local engine socket."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import array
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import socket
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
from collections import deque
|
|
12
|
+
from concurrent.futures import Future
|
|
13
|
+
from select import select
|
|
14
|
+
from threading import Condition, Event, Lock, Thread
|
|
15
|
+
from time import monotonic
|
|
16
|
+
|
|
17
|
+
from .runtime_refusal import runtime_unreachable
|
|
18
|
+
|
|
19
|
+
REQUEST_BYTES = 65_536
|
|
20
|
+
RESPONSE_BYTES = 2_000_000
|
|
21
|
+
PENDING_LIMIT = 8
|
|
22
|
+
PUSH_LIMIT = 256
|
|
23
|
+
PUSH_BYTES = 8_000_000
|
|
24
|
+
|
|
25
|
+
# The helper's cwd is private. Transfer the connected descriptor to the
|
|
26
|
+
# parent once so the helper never reads a command or a viewer credential.
|
|
27
|
+
_CONNECT = """
|
|
28
|
+
import array, socket, sys
|
|
29
|
+
with socket.socket(fileno=int(sys.argv[1])) as control, socket.socket(socket.AF_UNIX) as engine:
|
|
30
|
+
engine.settimeout(3)
|
|
31
|
+
try:
|
|
32
|
+
engine.connect(sys.argv[2])
|
|
33
|
+
except OSError as error:
|
|
34
|
+
control.send(str(error.errno or 5).encode())
|
|
35
|
+
else:
|
|
36
|
+
control.sendmsg([b'0'], [(socket.SOL_SOCKET, socket.SCM_RIGHTS,
|
|
37
|
+
array.array('i', [engine.fileno()]))])
|
|
38
|
+
control.settimeout(4)
|
|
39
|
+
control.recv(1)
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def connect(address):
|
|
44
|
+
"""Connect to the runtime socket, or raise its typed ``RuntimeUnreachable`` refusal."""
|
|
45
|
+
try:
|
|
46
|
+
return _connect(address)
|
|
47
|
+
except OSError as error:
|
|
48
|
+
refusal = runtime_unreachable(address, error)
|
|
49
|
+
if refusal is error:
|
|
50
|
+
raise
|
|
51
|
+
raise refusal from error
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _connect(address):
|
|
55
|
+
path = os.fspath(address)
|
|
56
|
+
# Use the absolute path: concurrent clients may attach to different states.
|
|
57
|
+
if len(os.fsencode(path)) < 103:
|
|
58
|
+
channel = socket.socket(socket.AF_UNIX)
|
|
59
|
+
try:
|
|
60
|
+
channel.settimeout(3)
|
|
61
|
+
channel.connect(path)
|
|
62
|
+
return channel
|
|
63
|
+
except OSError:
|
|
64
|
+
channel.close()
|
|
65
|
+
raise
|
|
66
|
+
|
|
67
|
+
parent, child = socket.socketpair()
|
|
68
|
+
with parent, child:
|
|
69
|
+
process = subprocess.Popen(
|
|
70
|
+
[sys.executable, "-I", "-c", _CONNECT, str(child.fileno()), address.name],
|
|
71
|
+
cwd=address.parent,
|
|
72
|
+
pass_fds=(child.fileno(),),
|
|
73
|
+
)
|
|
74
|
+
child.close()
|
|
75
|
+
descriptors = array.array("i")
|
|
76
|
+
parent.settimeout(4)
|
|
77
|
+
try:
|
|
78
|
+
status, ancillary, _, _ = parent.recvmsg(32, socket.CMSG_SPACE(descriptors.itemsize))
|
|
79
|
+
for level, kind, data in ancillary:
|
|
80
|
+
if level == socket.SOL_SOCKET and kind == socket.SCM_RIGHTS:
|
|
81
|
+
descriptors.frombytes(data[: len(data) - len(data) % descriptors.itemsize])
|
|
82
|
+
if status == b"0" and len(descriptors) == 1:
|
|
83
|
+
# On Darwin the helper must retain the original descriptor until
|
|
84
|
+
# recvmsg has installed the socket in the receiving process.
|
|
85
|
+
parent.sendall(b"a")
|
|
86
|
+
except OSError:
|
|
87
|
+
for descriptor in descriptors:
|
|
88
|
+
os.close(descriptor)
|
|
89
|
+
raise
|
|
90
|
+
finally:
|
|
91
|
+
parent.close()
|
|
92
|
+
try:
|
|
93
|
+
process.wait(timeout=1)
|
|
94
|
+
except subprocess.TimeoutExpired:
|
|
95
|
+
process.kill()
|
|
96
|
+
process.wait()
|
|
97
|
+
if status == b"0" and len(descriptors) == 1:
|
|
98
|
+
os.set_inheritable(descriptors[0], False)
|
|
99
|
+
return socket.socket(fileno=descriptors[0])
|
|
100
|
+
for descriptor in descriptors:
|
|
101
|
+
os.close(descriptor)
|
|
102
|
+
if status and status.isdigit() and status != b"0":
|
|
103
|
+
error = int(status)
|
|
104
|
+
raise OSError(error, os.strerror(error), path)
|
|
105
|
+
raise ConnectionError("Could not connect to the local engine socket")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class CommandCapacity(ValueError):
|
|
109
|
+
"""The local pending-command table is full."""
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class Connection:
|
|
113
|
+
def __init__(self, address, run, viewer, version):
|
|
114
|
+
self._socket = connect(address)
|
|
115
|
+
self._socket.settimeout(None)
|
|
116
|
+
self._frame = {"version": version, "run": run}
|
|
117
|
+
if viewer is not None:
|
|
118
|
+
self._frame["viewer"] = viewer
|
|
119
|
+
self._condition = Condition()
|
|
120
|
+
self._send_lock = Lock()
|
|
121
|
+
self._pending = {}
|
|
122
|
+
self._next_id = 1
|
|
123
|
+
self.pending_limit = PENDING_LIMIT
|
|
124
|
+
self._push = deque()
|
|
125
|
+
self._push_bytes = 0
|
|
126
|
+
self._error = None
|
|
127
|
+
Thread(target=self._read, daemon=True, name="synth-socket-reader").start()
|
|
128
|
+
|
|
129
|
+
@property
|
|
130
|
+
def closed(self):
|
|
131
|
+
with self._condition:
|
|
132
|
+
return self._error is not None
|
|
133
|
+
|
|
134
|
+
def close(self, error=None):
|
|
135
|
+
with self._condition:
|
|
136
|
+
if self._error is not None:
|
|
137
|
+
return
|
|
138
|
+
self._error = error or ConnectionError("Local engine connection closed")
|
|
139
|
+
try:
|
|
140
|
+
self._socket.shutdown(socket.SHUT_RDWR)
|
|
141
|
+
except OSError:
|
|
142
|
+
pass
|
|
143
|
+
self._socket.close()
|
|
144
|
+
for future in self._pending.values():
|
|
145
|
+
future.set_exception(self._error)
|
|
146
|
+
self._pending.clear()
|
|
147
|
+
self._condition.notify_all()
|
|
148
|
+
|
|
149
|
+
def request(self, op, fields, timeout):
|
|
150
|
+
future = Future()
|
|
151
|
+
with self._condition:
|
|
152
|
+
if self._error is not None:
|
|
153
|
+
raise self._error
|
|
154
|
+
if len(self._pending) >= self.pending_limit + (op == "subscribe"):
|
|
155
|
+
raise CommandCapacity(
|
|
156
|
+
f"Local command limit reached ({self.pending_limit})"
|
|
157
|
+
)
|
|
158
|
+
request_id = self._next_id
|
|
159
|
+
self._next_id += 1
|
|
160
|
+
data = json.dumps(
|
|
161
|
+
{**self._frame, "id": request_id, "command": {"op": op, **fields}},
|
|
162
|
+
ensure_ascii=False,
|
|
163
|
+
).encode() + b"\n"
|
|
164
|
+
if len(data) > REQUEST_BYTES:
|
|
165
|
+
raise ValueError(f"Request exceeds the local frame limit ({REQUEST_BYTES} bytes)")
|
|
166
|
+
self._pending[request_id] = future
|
|
167
|
+
|
|
168
|
+
try:
|
|
169
|
+
# The read side stays blocking while each write has a finite deadline.
|
|
170
|
+
with self._send_lock:
|
|
171
|
+
_send(self._socket, data)
|
|
172
|
+
return future.result(timeout=timeout)
|
|
173
|
+
except TimeoutError as error:
|
|
174
|
+
failure = TimeoutError(f"Local command {op} timed out; result unknown")
|
|
175
|
+
self.close(failure)
|
|
176
|
+
raise failure from error
|
|
177
|
+
except OSError as error:
|
|
178
|
+
self.close(error)
|
|
179
|
+
raise
|
|
180
|
+
|
|
181
|
+
def next_push(self, stopped: Event):
|
|
182
|
+
with self._condition:
|
|
183
|
+
self._condition.wait_for(lambda: stopped.is_set() or self._push or self._error)
|
|
184
|
+
if stopped.is_set():
|
|
185
|
+
return None
|
|
186
|
+
if self._push:
|
|
187
|
+
size, frame = self._push.popleft()
|
|
188
|
+
self._push_bytes -= size
|
|
189
|
+
return frame
|
|
190
|
+
raise self._error
|
|
191
|
+
|
|
192
|
+
def _read(self):
|
|
193
|
+
try:
|
|
194
|
+
with self._socket.makefile("rb") as source:
|
|
195
|
+
while line := source.readline(RESPONSE_BYTES + 1):
|
|
196
|
+
if len(line) > RESPONSE_BYTES or not line.endswith(b"\n"):
|
|
197
|
+
raise ConnectionError("Invalid frame from the local engine")
|
|
198
|
+
frame = json.loads(line)
|
|
199
|
+
if not isinstance(frame, dict):
|
|
200
|
+
raise TypeError("Expected a local engine frame object")
|
|
201
|
+
with self._condition:
|
|
202
|
+
if self._error is not None:
|
|
203
|
+
return
|
|
204
|
+
kind = frame.get("type")
|
|
205
|
+
if kind == "reply":
|
|
206
|
+
identity = frame.get("id")
|
|
207
|
+
future = self._pending.pop(identity, None) if type(identity) is int else None
|
|
208
|
+
if future is None:
|
|
209
|
+
raise ValueError("Invalid local engine reply ID")
|
|
210
|
+
future.set_result(frame)
|
|
211
|
+
elif kind in {"event", "caught_up", "lagged", "live", "refused"}:
|
|
212
|
+
if (
|
|
213
|
+
len(self._push) >= PUSH_LIMIT
|
|
214
|
+
or self._push_bytes + len(line) > PUSH_BYTES
|
|
215
|
+
):
|
|
216
|
+
raise ConnectionError("Local subscription buffer limit reached")
|
|
217
|
+
self._push.append((len(line), frame))
|
|
218
|
+
self._push_bytes += len(line)
|
|
219
|
+
self._condition.notify_all()
|
|
220
|
+
else:
|
|
221
|
+
raise ValueError("Invalid local engine frame type")
|
|
222
|
+
self.close()
|
|
223
|
+
except (OSError, TypeError, ValueError, RecursionError) as error:
|
|
224
|
+
self.close(ConnectionError(str(error)))
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _send(channel, data):
|
|
228
|
+
remaining = memoryview(data)
|
|
229
|
+
deadline = monotonic() + 3
|
|
230
|
+
while remaining:
|
|
231
|
+
try:
|
|
232
|
+
_, ready, _ = select([], [channel], [], max(0, deadline - monotonic()))
|
|
233
|
+
except ValueError as error:
|
|
234
|
+
raise ConnectionError("Local engine socket closed during write") from error
|
|
235
|
+
if not ready or monotonic() >= deadline:
|
|
236
|
+
raise TimeoutError("Local engine socket write timed out")
|
|
237
|
+
try:
|
|
238
|
+
sent = channel.send(remaining, socket.MSG_DONTWAIT)
|
|
239
|
+
except BlockingIOError:
|
|
240
|
+
continue
|
|
241
|
+
if sent == 0:
|
|
242
|
+
raise ConnectionError("Local engine socket closed during write")
|
|
243
|
+
remaining = remaining[sent:]
|