-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpartial_rendering.py
More file actions
297 lines (245 loc) · 11.9 KB
/
Copy pathpartial_rendering.py
File metadata and controls
297 lines (245 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from dataclasses import dataclass
from typing import Literal
import plain_spec
from change_detection import PartialRenderStart, determine_partial_render_start
from plain2code_exceptions import ModuleDoesNotExistError
from plain_modules import PlainModule
@dataclass
class PlainModuleRenderState:
last_render_module: PlainModule
last_render_frid: str | None
change: PlainModule | None = None
change_type: Literal["spec_change", "code_change"] | None = None
@dataclass
class RenderChoice:
module: PlainModule | None = None
render_range: list[str] | None = None
wipe_later_modules: bool = False
is_destructive: bool = False
choice_type: str | None = None
def spec_change(plain_module: PlainModule) -> PlainModule | None:
all_modules = plain_module.all_required_modules + [plain_module]
for _module in all_modules:
module_metadata = _module.load_module_metadata()
if (
module_metadata
and "source_hash" in module_metadata
and module_metadata["source_hash"] != _module.get_module_source_hash()
):
return _module
return None
def code_change(plain_module: PlainModule) -> PlainModule | None:
all_modules = plain_module.all_required_modules + [plain_module]
for _module in all_modules:
if len(_module.required_modules) == 0:
continue
module_metadata = _module.load_module_metadata()
previous_module = _module.required_modules[-1]
if (
module_metadata
and "required_modules_code_hash" in module_metadata
and module_metadata["required_modules_code_hash"] != previous_module.get_module_code_hash()
):
return previous_module
return None
def module_comes_before_or_equal(
all_required_modules: list[PlainModule],
module1: PlainModule,
module2: PlainModule,
) -> bool:
for module in all_required_modules:
if module.module_name == module1.module_name:
return True
if module.module_name == module2.module_name:
return False
raise ValueError(f"Module {module1.module_name} and {module2.module_name} not found in {all_required_modules}")
def get_plain_module_render_state(plain_module: PlainModule) -> PlainModuleRenderState | None:
sc = spec_change(plain_module)
cc = code_change(plain_module)
all_required_modules = plain_module.all_required_modules
last_rendered_module_name, last_rendered_frid = plain_module.get_module_render_status()
if last_rendered_module_name is None and last_rendered_frid is None:
return None
if last_rendered_module_name == plain_module.module_name:
module = plain_module
else:
found_module: PlainModule | None = None
for required_module in all_required_modules:
if required_module.module_name == last_rendered_module_name:
found_module = required_module
if found_module is None:
raise ModuleDoesNotExistError(
f"Last rendered module {last_rendered_module_name} not found in {[rmodule.module_name for rmodule in all_required_modules]}"
)
module = found_module
pr = PlainModuleRenderState(
last_render_module=module,
last_render_frid=last_rendered_frid,
change=None,
change_type=None,
)
if sc is None and cc is None:
return pr
if sc is not None:
pr.change = sc
pr.change_type = "spec_change"
if cc is not None and (
pr.change is None
or (pr.change is not None and module_comes_before_or_equal(all_required_modules, cc, pr.change))
):
pr.change = cc
pr.change_type = "code_change"
return pr
def get_all_affected_modules_from_change(
plain_module: PlainModule,
plain_module_render_state: PlainModuleRenderState,
) -> list[PlainModule]:
all_affected_modules = dict[str, PlainModule]()
if plain_module_render_state.change_type == "spec_change":
start_module = plain_module_render_state.change
elif plain_module_render_state.change_type == "code_change":
if plain_module_render_state.change.is_module_fully_rendered():
start_module = plain_module.get_next_module(plain_module_render_state.change.module_name)
else:
start_module = plain_module_render_state.change
else:
raise ValueError(f"Unknown change type: {plain_module_render_state.change_type}")
affected_module = False
all_modules = plain_module.all_required_modules + [plain_module]
for module in all_modules:
if module.module_name == start_module.module_name:
affected_module = True
if affected_module and module.module_name not in all_affected_modules:
all_affected_modules[module.module_name] = module
return list(all_affected_modules.values())
def change_is_only_future_work(
plain_module: PlainModule,
plain_module_render_state: PlainModuleRenderState,
partial_start: "PartialRenderStart | None",
) -> bool:
"""Return True when a spec change only adds work *after* the last-rendered functionality.
Appending a new functionality past the render frontier (e.g. a new functionality at the
end of the last module) does not affect anything already rendered, so it needs no user
decision. The normal "continue" path renders the outstanding functionalities, starting
from the first one that was never rendered.
"""
if partial_start is None:
return False
all_modules = plain_module.all_required_modules + [plain_module]
order = {module.module_name: index for index, module in enumerate(all_modules)}
last_index = order[plain_module_render_state.last_render_module.module_name]
start_index = order[partial_start.module.module_name]
if start_index != last_index:
return start_index > last_index
if plain_module_render_state.last_render_frid is None:
return True
return int(partial_start.frid) > int(plain_module_render_state.last_render_frid)
def _resume_render_choice(
plain_module: PlainModule,
plain_module_render_state: PlainModuleRenderState,
force_render: bool,
) -> RenderChoice:
"""The natural next step from the last-rendered position when no decision is needed:
start an unrendered module, continue a partially-rendered one, or advance to the next.
"""
pr = plain_module_render_state
if pr.last_render_module.has_no_rendered_functionality():
return RenderChoice(
module=pr.last_render_module,
render_range=None,
choice_type="module_start",
wipe_later_modules=True,
is_destructive=False,
)
if not pr.last_render_module.is_module_fully_rendered():
if not pr.last_render_frid:
raise ValueError("Last render FRID is not set for a non-initial module")
next_frid, next_module = plain_module.get_next_frid(pr.last_render_frid, pr.last_render_module.module_name)
render_range = plain_spec.get_render_range_from(next_frid, next_module.plain_source)
return RenderChoice(
module=next_module,
render_range=None if force_render else render_range,
wipe_later_modules=force_render,
choice_type="continue_from_frid",
)
next_module = plain_module.get_next_module(pr.last_render_module.module_name) or pr.last_render_module
return RenderChoice(
module=next_module,
render_range=None,
choice_type="module_start",
is_destructive=pr.last_render_module.module_name == plain_module.module_name,
)
def get_render_choices(
plain_module: PlainModule,
plain_module_render_state: PlainModuleRenderState,
force_render: bool = False,
) -> dict[str, RenderChoice]:
render_choices = list[RenderChoice]()
module_start_points = list[str]()
# Decide whether the detected change actually requires a decision. A spec change that only
# adds work *after* the last-rendered functionality (e.g. a new functionality appended to
# the end of the last module) does not affect anything already rendered, so it is treated
# like a normal "continue" rather than a change that requires choosing where to restart.
partial_start = None
treat_as_continue = plain_module_render_state.change is None
if plain_module_render_state.change is not None and plain_module_render_state.change_type == "spec_change":
partial_start = determine_partial_render_start(plain_module)
if change_is_only_future_work(plain_module, plain_module_render_state, partial_start):
treat_as_continue = True
if treat_as_continue:
# Continue / resume from the last-rendered position. The change-driven blocks below decide
# the start otherwise — once a change touches already-rendered work, resuming from the old
# position would build on stale code.
render_choices.append(_resume_render_choice(plain_module, plain_module_render_state, force_render))
else:
# change is set here (otherwise treat_as_continue would be True), so change_type is always
# "spec_change" or "code_change" and get_all_affected_modules_from_change is well-defined.
all_affected_modules = get_all_affected_modules_from_change(plain_module, plain_module_render_state)
if plain_module_render_state.change_type == "spec_change" and partial_start is not None:
# Optimized partial render: render from the changed functionality, keeping the
# module's earlier (unchanged) functionalities.
render_range = plain_spec.get_render_range_from(partial_start.frid, partial_start.module.plain_source)
render_choices.append(
RenderChoice(
module=partial_start.module,
render_range=render_range,
choice_type="render_from_change",
wipe_later_modules=len(all_affected_modules) > 1,
is_destructive=True,
)
)
# Full re-render of the affected module(s) from scratch. For a code change this is always
# the action; for a spec change with no partial start (non-FR sections changed, or only
# trailing FR removals) it is the only safe option, and when a partial start does exist it
# is offered alongside it as the "rebuild this module cleanly" alternative. The "re-render
# from first module" choice below remains the full-chain reset.
if len(all_affected_modules) > 0 and all_affected_modules[0].module_name not in module_start_points:
render_choices.append(
RenderChoice(
module=all_affected_modules[0],
render_range=None,
choice_type="rerender_affected",
wipe_later_modules=True,
is_destructive=True,
)
)
module_start_points.append(all_affected_modules[0].module_name)
if len(plain_module.all_required_modules) > 0:
first_module = plain_module.all_required_modules[0]
if first_module.module_name != plain_module_render_state.last_render_module.module_name and (
plain_module_render_state.change is not None
and first_module.module_name != plain_module_render_state.change.module_name
and first_module.module_name not in module_start_points
):
render_choices.append(
RenderChoice(
module=first_module,
render_range=None,
choice_type="rerender_from_first",
wipe_later_modules=True,
is_destructive=True,
)
)
module_start_points.append(first_module.module_name)
render_choices.append(RenderChoice(module=None, render_range=None, choice_type="quit"))
return {str(idx): choice for idx, choice in enumerate(render_choices, start=1)}