Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def version_family(projects, project_id, item_id, snapshot, *, records=None):
if item is None or item.kind not in {'text', 'image', 'video', 'audio'}:
raise ContractError('missing_object', '该媒体节点不存在。', item_id)
if records is None:
records = projects.history(project_id)
# A read-only scan: history() deep-copies every snapshot, which took
# seconds per call on projects with hundreds of commits.
records = projects.records(project_id)
family, spec_ids = {}, set()
for record in reversed(records):
previous = next((i for i in record.snapshot.canvasItems if i.id == item_id), None)
Expand Down Expand Up @@ -87,7 +89,10 @@ def content(values):


def node_versions(projects, project_id, item_id, *, assets=None):
records = projects.history(project_id)
# Scan the shared commit records instead of history(): copying every
# snapshot made opening a node's versions take ~7 s on a 500-commit project.
# The records must not be mutated, so only the small listing is copied out.
records = projects.records(project_id)
snapshot = records[0].snapshot
item, family = version_family(projects, project_id, item_id, snapshot, records=records)
service = assets or CreationAssets(projects)
Expand Down Expand Up @@ -116,10 +121,10 @@ def node_versions(projects, project_id, item_id, *, assets=None):
modelAvailable=models[key] if job else None,
currentReferencesDiffer=current_references_differ(snapshot, job.inputSnapshot) if job else False))
current = current_asset(snapshot, item)
return dict(item=item, versions=rows, currentAssetId=current.id if current else None,
return deepcopy(dict(item=item, versions=rows, currentAssetId=current.id if current else None,
eventCursor=snapshot.eventCursor,
expectedVersions=[dict(objectType=kind, id=obj.id, revision=obj.revision)
for (kind, _), obj in object_index(snapshot).items()])
for (kind, _), obj in object_index(snapshot).items()]))


def apply_node_version(history, before, command):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_creation_node_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,26 @@ async def test_follow_history_next_execution_uses_current_source_and_keeps_old_e
await runner.close()


def test_listing_scans_shared_records_without_copying_or_exposing_them(project, monkeypatch):
store, pid, session = project
node, first = text_material(project)
assert send(store, pid, 'asset.writeText', session=session, versions=versions(store.snapshot(pid)),
name='second.md', text='second', previousAsset=asset_ref(first)).status == 'applied'
second = store.snapshot(pid).assets[-1]
assert send(store, pid, 'canvas.materialize', session=session, versions=versions(store.snapshot(pid)),
itemId=node.id, asset=asset_ref(second)).status == 'applied'
before = [record.model_dump_json() for record in store.records(pid)]
# history() deep-copies every commit; long projects took seconds per dialog open.
monkeypatch.setattr(store, 'history', lambda *args: pytest.fail('listing must not copy every commit'))
listing = CreationHistory(store).node_versions(pid, node.id)
assert {row['asset'].id for row in listing['versions']} == {first.id, second.id}
shared = store.records(pid)[0].snapshot
shared_objects = {id(asset) for asset in shared.assets} | {id(item) for item in shared.canvasItems}
assert id(listing['item']) not in shared_objects
assert not {id(row['asset']) for row in listing['versions']} & shared_objects
assert [record.model_dump_json() for record in store.records(pid)] == before


def test_listing_and_counts_load_history_once_and_recheck_changed_files(project, monkeypatch):
store, pid, session = project
node, first = text_material(project)
Expand Down
Loading