Round 2 of adversarial review found a real bug in the fix the previous round's own review prompted: mutating the live project.variables array (and its row objects) in place before calling setVariables() meant projects.js's remember() -- which deep-clones the project for the undo stack BEFORE applying the new value -- captured the already-edited state as the "before" snapshot. Undo after any variable edit or removal was a silent no-op. Fixed by never mutating what project.variables already points at: every commit now reads project.variables fresh and hands setVariables a brand new array, which is also what closes the SAME stale-closure race the in-place version was built to fix in the first place (redraw-mid-edit still reads whatever the previous commit already saved, since it reads live rather than from a snapshot taken at render time). Also fixed core/prompt_build.py's docstring, which claimed the opposite of what resolve-variables-last actually does and what its own test already pinned.
31 lines
1.5 KiB
Python
31 lines
1.5 KiB
Python
"""The text actually sent as a scene's prompt at generation time.
|
|
|
|
`Scene.text` (core/projects.py) stays the user's literal typed words -- this
|
|
module never writes back to it. It only ever runs at the moment a prompt
|
|
value is about to leave the app for a real run, so editing a scene and
|
|
looking at what it produced always shows what was actually typed, not an
|
|
expanded copy nothing can trace back.
|
|
|
|
Order matches v4's: anchor and postfix are joined to the scene text FIRST,
|
|
then shortcuts expand across the whole combined string (a shortcut trigger
|
|
sitting right at the anchor/scene boundary still matches), then `$variables`
|
|
resolve last. Resolving last means a variable's OWN text is never re-scanned
|
|
for shortcut triggers -- only the anchor/scene/postfix text is -- but a
|
|
variable may still reference another variable, since resolve_variables()
|
|
recurses through the variable map itself (see its own docstring for the
|
|
cycle-safety that recursion needs).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from . import shortcuts as shortcuts_mod
|
|
|
|
|
|
def build(scene_text: str, *, anchor: str = "", postfix: str = "",
|
|
postfix_enabled: bool = True, variables=None, shortcuts=None, seed: int = 0) -> str:
|
|
anchor = (anchor or "").strip()
|
|
postfix = (postfix or "").strip() if postfix_enabled else ""
|
|
combined = " ".join(p for p in (anchor, str(scene_text or "").strip(), postfix) if p)
|
|
expanded = shortcuts_mod.expand(combined, shortcuts=shortcuts, seed=seed)
|
|
return shortcuts_mod.resolve_variables(expanded, variables)
|