diff --git a/apps/api/src/opendirector_api/agent.py b/apps/api/src/opendirector_api/agent.py index 7ef36a60b..0f7b3431d 100644 --- a/apps/api/src/opendirector_api/agent.py +++ b/apps/api/src/opendirector_api/agent.py @@ -13,6 +13,8 @@ from pydantic import BaseModel, ConfigDict, Field, JsonValue from opendirector.agent.catalog import model_choices +from opendirector.agent.canvas_links import canvas_links_for_text +from opendirector.domain.creation.chat import ChatCanvasLink from opendirector.agent.context import RunContext, RuntimeId from opendirector.agent.citations import referenced_sources, with_trace_sources from opendirector.agent.runtime import AgentRuntimes, runtime_available, configured_runtimes, runtime_spec, runtime_catalog, runtime_status @@ -456,12 +458,24 @@ async def produce() -> None: for activity in activities: if activity['status'] == 'running': activity['status'] = 'interrupted' + canvas_links = [] + try: + final_snapshot = await asyncio.to_thread(projects.snapshot, project_id) + canvas_links = [ChatCanvasLink.model_validate(link).model_dump(mode='json') + for link in canvas_links_for_text(text, final_snapshot, value.id)] + except Exception: + # Link resolution is navigation metadata; it must not + # fail a turn whose accepted result is already saved. + logger.exception('agent.canvas_links.failed project_id=%s turn_id=%s', + project_id, value.id) saved = await asyncio.to_thread(conversations.update, project_id, finish={ 'id': str(uuid5(value.id, 'assistant')), 'role': 'assistant', 'text': text, 'status': status, 'model': model, 'runtime': value.runtime, 'thinking': value.thinking, 'tools': activities, 'sources': list(sources.values()), - **({'plan': plan} if plan is not None else {})}, conversation_id=value.conversationId) + **({'plan': plan} if plan is not None else {}), + **({'canvasLinks': canvas_links} if canvas_links else {})}, + conversation_id=value.conversationId) else: saved = None events.put({'type': 'end', 'conversation': saved, 'error': error}) diff --git a/packages/core/src/opendirector/agent/canvas_links.py b/packages/core/src/opendirector/agent/canvas_links.py new file mode 100644 index 000000000..3ac41abe6 --- /dev/null +++ b/packages/core/src/opendirector/agent/canvas_links.py @@ -0,0 +1,446 @@ +"""Resolve assistant Markdown links to stable canvas item identities. + +An assistant reply may link to canvas content with a relative destination such +as ``[Finished video](output.mp4)``. The file name is matched against project +assets and their recorded provenance, then mapped to the canvas items that +display the asset. The result stores only stable object identities and typed +status codes; current geometry is deliberately not stored, so moving, renaming +or regrouping an item never invalidates a saved link. + +Markdown links (including reference-style links) and plain @title mentions are +considered. Code, images and ordinary external URLs are left untouched. Item +titles resolve only through @title mentions; a bare link destination matches +asset names, not item titles. +""" +from __future__ import annotations + +from typing import Any, Mapping +import re +from urllib.parse import unquote, urlsplit +from uuid import UUID +from markdown_it import MarkdownIt + +LINK_STATUSES = ('focused', 'hidden', 'materialize', 'ambiguous', 'missing', + 'cross-project', 'external') + +def link_targets(text: str) -> list[str]: + """Extract renderer-compatible link destinations, excluding images/code.""" + targets: list[str] = [] + for token in MarkdownIt().parse(text): + if token.type != 'inline' or not token.children: + continue + for child in token.children: + if child.type == 'link_open' and child.attrs: + href = child.attrs.get('href') + if href is not None and str(href).strip(): + targets.append(str(href)) + return targets + + +# Full-width punctuation that always ends a mention, even mid-sentence: +# "\u53c2\u89c1@\u6807\u9898\u3001\u7136\u540e\u7ee7\u7eed", "\u201c@\u6807\u9898\u201d\u6548\u679c\u4e0d\u9519", "\uff08@\u6807\u9898\uff09\u8bf7\u7ee7\u7eed". +_MENTION_END_ANY = '\u3001\u201d\u2026\u2014\uff09\u300d\u300f\u3011\u300b\u3009' +# ASCII sentence punctuation only ends a mention at the end of the text or +# before whitespace, so a title such as ``Chapter 2.1`` keeps its punctuation. +_MENTION_END_AFTER_SPACE = '.:"\'\uff1a' + + +def _boundary_indexes(value: str) -> list[int]: + """Indexes where sentence punctuation ends a mention.""" + indexes = [] + for index, char in enumerate(value): + if char in _MENTION_END_ANY: + indexes.append(index) + elif char in _MENTION_END_AFTER_SPACE and ( + index + 1 == len(value) or value[index + 1].isspace()): + indexes.append(index) + return indexes + + +def _mention_base(value: str) -> str: + """Trim a mention at the first sentence punctuation that ends it. + + A period or colon followed by whitespace (or the end of the text) ends the + mention, so ``@Finished note. Next step`` resolves ``Finished note``. An + internal period such as ``Chapter 2.1`` stays part of the title because it + is not followed by whitespace. Full-width closing punctuation ends the + mention wherever it appears. + """ + value = value.strip() + indexes = _boundary_indexes(value) + return value[:indexes[0]].rstrip() if indexes else value.rstrip() + + +def _mention_targets(text: str) -> list[str]: + targets = [] + for token in MarkdownIt().parse(text): + if token.type != 'inline' or not token.children: + continue + in_link = False + for child in token.children: + if child.type == 'link_open': + in_link = True + elif child.type == 'link_close': + in_link = False + elif child.type == 'text' and not in_link: + # The word boundary is ASCII-only, matching JavaScript's \w: + # CJK prose directly before a mention is not glued to it. + # Hard sentence punctuation ends the run here; softer trailing + # punctuation (period/colon/full-width closers) is kept in the + # raw mention so an exact title such as "@Dr. Smith" can win + # before the resolver trims it. + for match in re.finditer( + r"(?`\u3002\uff1b\uff0c\uff01\uff1f\uff1a]+)", + child.content): + targets.append('@' + match.group(1).strip()) + return targets + + +def _uuid(value: str) -> str | None: + try: + return str(UUID(str(value).strip())) + except (ValueError, TypeError, AttributeError): + return None + + +def _identity(target: str) -> tuple[str, Any]: + """Classify a destination without touching project state or raising.""" + raw = target.strip() + if raw.startswith('<') and raw.endswith('>'): + raw = raw[1:-1].strip() + if raw.startswith(('#', '?')): + # In-page anchors and query-only destinations are not project objects. + return 'external', raw + if raw.startswith('@'): + return 'title', raw[1:].strip() + if raw.startswith('project:'): + project, separator, remainder = raw[len('project:'):].partition('/') + if not separator: + return 'missing', raw + for prefix in ('canvas:', 'node:'): + if remainder.startswith(prefix): + project_id = _uuid(project) + node_id = _uuid(remainder[len(prefix):]) + if project_id is None or node_id is None: + return 'missing', raw + return 'project', (project_id, node_id) + return 'missing', raw + for prefix in ('canvas:', 'node:'): + if raw.startswith(prefix): + node_id = _uuid(raw[len(prefix):]) + return ('node', node_id) if node_id is not None else ('missing', raw) + try: + parsed = urlsplit(raw) + except ValueError: + return 'missing', raw + if parsed.scheme or parsed.netloc: + # http(s) links keep the existing external-link policy; unsafe schemes + # (javascript:, data:, file:) are never treated as project links. + return 'external', raw + path = unquote(parsed.path).replace('\\', '/') + while path.startswith('./'): + path = path[2:] + path = path.lstrip('/') + if not path or path in {'.', '..'} or path.endswith('/'): + return 'missing', raw + name = path + if not name or name.rsplit('/', 1)[-1] in {'.', '..'}: + return 'missing', raw + return 'name', name + + +def format_canvas_link(node_id: str, project_id: str | None = None) -> str: + """Canonical forms consumed by the resolver and future tool guidance.""" + node = _uuid(node_id) + if node is None: + raise ValueError('Canvas item id must be a UUID') + if project_id is None: + return f'canvas:{node}' + project = _uuid(project_id) + if project is None: + raise ValueError('Project id must be a UUID') + return f'project:{project}/canvas:{node}' + + +def _items(snapshot: Any) -> tuple[Any, ...]: + return tuple(getattr(snapshot, 'canvasItems', ()) or ()) + + +def _assets(snapshot: Any) -> tuple[Any, ...]: + return tuple(getattr(snapshot, 'assets', ()) or ()) + + +def _project_id(snapshot: Any) -> str: + project = getattr(snapshot, 'project', None) + return str(getattr(project, 'id', '')) + + +def _base_name(value: str) -> str: + return str(value or '').replace('\\', '/').rsplit('/', 1)[-1] + + +def _asset_matches(name: str, asset: Any) -> bool: + if '/' in name: + # A directory-qualified destination must match the full name/path; a + # same-basename file in another directory is a different object. + return name in {str(getattr(asset, 'name', '') or '').replace('\\', '/'), + str(getattr(asset, 'path', '') or '').replace('\\', '/')} + return name in {_base_name(getattr(asset, 'name', '')), + _base_name(getattr(asset, 'path', ''))} + + +def _binding_asset_ids(item: Any) -> list[str]: + binding = getattr(item, 'binding', None) + role = getattr(binding, 'role', None) + reference = (getattr(binding, 'objectRef', None) if role == 'material' + else getattr(binding, 'materialOverride', None) if role == 'generation' + else None) + asset_id = getattr(reference, 'assetId', None) + return [str(asset_id)] if asset_id is not None else [] + + +def _run_id(asset: Any) -> str: + provenance = getattr(asset, 'provenance', None) + return str(getattr(provenance, 'agentRunId', '') or '') + + +def _node_result(item: Any, result: dict) -> dict: + visible = bool(getattr(item, 'visible', True)) + return {**result, + 'status': 'focused' if visible else 'hidden', + 'nodeId': str(getattr(item, 'id', '')), + 'kind': str(getattr(item, 'kind', '')), + **({} if visible else {'action': 'reveal'})} + + +def _node_candidate(item: Any) -> dict: + visible = bool(getattr(item, 'visible', True)) + return {'nodeId': str(getattr(item, 'id', '')), + 'kind': str(getattr(item, 'kind', '')), + 'title': str(getattr(item, 'title', '')), + 'hidden': not visible} + + +def _effective_visibility(item: Any, items: tuple[Any, ...]) -> bool: + by_id = {str(getattr(value, 'id', '')): value for value in items} + current = item + seen: set[str] = set() + while current is not None: + identity = str(getattr(current, 'id', '')) + if identity in seen or not bool(getattr(current, 'visible', True)): + return False + seen.add(identity) + parent = getattr(current, 'parentId', None) + current = by_id.get(str(parent)) if parent else None + return True + + +def _resolved_node(item: Any, snapshot: Any, result: dict) -> dict: + output = _node_result(item, result) + visible = _effective_visibility(item, _items(snapshot)) + output['status'] = 'focused' if visible else 'hidden' + if visible: + output.pop('action', None) + else: + output['action'] = 'reveal' + return output + + +def _asset_candidate(asset: Any) -> dict: + return {'assetId': str(getattr(asset, 'id', '')), + 'kind': str(getattr(asset, 'kind', '')), + 'title': str(getattr(asset, 'name', ''))} + + +def _unique(items: list[Any]) -> list[Any]: + seen: set[str] = set() + result = [] + for item in items: + identity = str(getattr(item, 'id', '')) + if identity not in seen: + seen.add(identity) + result.append(item) + return result + + +def _resolve_name(name: str, snapshot: Any, turn_id: str | None, result: dict) -> dict: + assets = [asset for asset in _assets(snapshot) if _asset_matches(name, asset)] + # An adopted generation node displays the asset selected by its current + # result even though its binding remains a generation spec. + results = {str(getattr(result_version, 'id', '')): result_version + for result_version in (getattr(snapshot, 'results', ()) or ())} + specs = {str(getattr(spec, 'id', '')): spec + for spec in (getattr(snapshot, 'generationSpecs', ()) or ())} + for item in _items(snapshot): + binding = getattr(item, 'binding', None) + if getattr(binding, 'role', None) != 'generation' or getattr(binding, 'materialOverride', None) is not None: + continue + spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) + or getattr(binding, 'objectRef', None)) + spec = specs.get(str(getattr(spec_ref, 'id', ''))) + selected = results.get(str(getattr(spec, 'currentResultId', ''))) + asset_ref = getattr(selected, 'asset', None) + asset_id = getattr(asset_ref, 'assetId', None) + asset = next((value for value in _assets(snapshot) if str(getattr(value, 'id', '')) == str(asset_id)), None) + if asset is not None and _asset_matches(name, asset) and asset not in assets: + assets.append(asset) + if turn_id is not None and len(assets) > 1: + # A filename shared by several assets in a turn identifies only that + # turn's output; a single asset (a user import or an earlier turn's + # result) still resolves by name. + assets = [asset for asset in assets if _run_id(asset) == str(turn_id)] + if not assets: + return {**result, 'status': 'missing'} + if assets: + asset_ids = {str(getattr(asset, 'id', '')) for asset in assets} + result_assets: dict[str, str] = {} + results = {str(getattr(value, 'id', '')): value + for value in (getattr(snapshot, 'results', ()) or ())} + specs = {str(getattr(value, 'id', '')): value + for value in (getattr(snapshot, 'generationSpecs', ()) or ())} + for item in _items(snapshot): + binding = getattr(item, 'binding', None) + if getattr(binding, 'role', None) != 'generation' or getattr(binding, 'materialOverride', None) is not None: + continue + spec_ref = (getattr(binding, 'spec', None) or getattr(binding, 'generationSpec', None) + or getattr(binding, 'objectRef', None)) + spec = specs.get(str(getattr(spec_ref, 'id', ''))) + selected = results.get(str(getattr(spec, 'currentResultId', ''))) + asset_ref = getattr(selected, 'asset', None) + selected_id = getattr(asset_ref, 'assetId', None) + if selected_id is not None: + result_assets[str(getattr(item, 'id', ''))] = str(selected_id) + nodes = _unique([item for item in _items(snapshot) + if asset_ids.intersection(_binding_asset_ids(item)) or + result_assets.get(str(getattr(item, 'id', ''))) in asset_ids]) + if len(assets) == 1 and len(nodes) == 1: + return _resolved_node(nodes[0], snapshot, result) + if len(assets) == 1 and not nodes: + asset = assets[0] + return {**result, 'status': 'materialize', + 'assetId': str(getattr(asset, 'id', '')), + 'kind': str(getattr(asset, 'kind', '')), + 'action': 'materialize'} + return {**result, 'status': 'ambiguous', + 'candidates': [*map(_asset_candidate, assets), *map(_node_candidate, nodes)]} + return {**result, 'status': 'missing'} + + +_PROSE_WORD = re.compile(r"[^\W\d_]+(?:['’-][^\W\d_]+)*\Z") +_CJK_CHARACTER = re.compile('[㐀-䶿一-鿿豈-﫿' + '぀-ヿ가-힯]') + + +def _known_titles(snapshot: Any) -> list[str]: + titles: list[str] = [] + for item in _items(snapshot): + title = str(getattr(item, 'title', '') or '') + if title and title not in titles: + titles.append(title) + return titles + + +def _title_prefix_accepts(remainder: str) -> bool: + """Whether the text after a known title still lets the mention end there. + + An exact match has no remainder. Sentence punctuation ends the mention; + an ASCII period/colon must be followed by whitespace or the end, so the + internal period of ``Chapter 2.1`` is not a boundary. Trailing prose words + ("Dr. Smith says hello") are allowed, but a number or label is not + ("@Scene 99" may be part of a longer title, so it must not reference the + unrelated shorter node "Scene"). Glued CJK prose ("@标题生成视频") is + allowed because Chinese has no word separator. + """ + if not remainder: + return True + first = remainder[0] + if first in _MENTION_END_ANY: + return True + if first in _MENTION_END_AFTER_SPACE: + return len(remainder) == 1 or remainder[1].isspace() + if first.isspace(): + rest = remainder.strip() + return bool(rest) and all( + _PROSE_WORD.fullmatch(word) for word in rest.split()) + return bool(_CJK_CHARACTER.match(remainder)) + + +def _resolve_title(name: str, snapshot: Any, result: dict) -> dict: + # Choose the longest known title that prefixes the mention, then require a + # clean mention boundary after it. A longer unusable prefix shadows every + # shorter one: resolving "Dr" for "@Dr. Smith 99" would reference a + # different object, so the safer outcome is "missing". + prefixes = [title for title in _known_titles(snapshot) if name.startswith(title)] + if prefixes: + title = max(prefixes, key=len) + if _title_prefix_accepts(name[len(title):]): + nodes = _unique([item for item in _items(snapshot) + if str(getattr(item, 'title', '')) == title]) + if len(nodes) == 1: + return _resolved_node(nodes[0], snapshot, {**result, 'target': '@' + title}) + if len(nodes) > 1: + return {**result, 'target': '@' + title, 'status': 'ambiguous', + 'candidates': [_node_candidate(item) for item in nodes]} + # Keep the persisted target on the mention itself so the renderer never + # turns the rest of the sentence ("@No such item. Next step") into one + # clickable reference. A raw mention with no boundary keeps its full text. + base = _mention_base(name) + return {**result, 'target': '@' + base if base else result['target'], + 'status': 'missing'} + + +def resolve_canvas_link(target: str, snapshot: Any, turn_id: str | None = None) -> dict: + """Resolve one destination to a typed, JSON-serializable outcome.""" + raw = str(target) + result = {'target': raw} + try: + kind, value = _identity(raw) + if kind == 'external': + return {**result, 'status': 'external'} + if kind == 'missing': + return {**result, 'status': 'missing'} + if kind == 'title': + return _resolve_title(value, snapshot, result) + if kind == 'project': + project_id, node_id = value + if project_id != _project_id(snapshot): + return {**result, 'status': 'cross-project', + 'projectId': project_id, 'nodeId': node_id} + item = next((item for item in _items(snapshot) + if str(getattr(item, 'id', '')) == node_id), None) + return _resolved_node(item, snapshot, result) if item is not None else {**result, 'status': 'missing'} + if kind == 'node': + item = next((item for item in _items(snapshot) + if str(getattr(item, 'id', '')) == value), None) + return _resolved_node(item, snapshot, result) if item is not None else {**result, 'status': 'missing'} + return _resolve_name(value, snapshot, turn_id, result) + except Exception: + # A malformed project fact or an unexpected snapshot shape must not + # break an assistant reply; the link simply stays unresolved. + return {**result, 'status': 'missing'} + + +def canvas_links_for_text(text: str, snapshot: Any, turn_id: str | None = None) -> list[dict]: + """Resolve each unique inline link, omitting ordinary external URLs.""" + result: list[dict] = [] + seen: set[str] = set() + for target in [*link_targets(text), *_mention_targets(text)]: + if target in seen: + continue + seen.add(target) + resolved = resolve_canvas_link(target, snapshot, turn_id) + if resolved['status'] != 'external': + result.append(resolved) + return result + + +def with_canvas_links(message: Mapping[str, Any], snapshot: Any, + turn_id: str | None = None) -> dict: + """Attach resolved references to an assistant message before persistence.""" + value = dict(message) + if value.get('role') != 'assistant': + return value + links = canvas_links_for_text(str(value.get('text', '')), snapshot, turn_id) + if links: + value['canvasLinks'] = links + return value diff --git a/packages/core/src/opendirector/agent/tools.py b/packages/core/src/opendirector/agent/tools.py index 64da5bd17..706b291c5 100644 --- a/packages/core/src/opendirector/agent/tools.py +++ b/packages/core/src/opendirector/agent/tools.py @@ -408,6 +408,9 @@ def narrow(value: JsonValue) -> None: raise ProjectError('未知业务工具。') command = parse_command({**arguments, 'projectId': str(project_id), 'commandId': str(uuid5(turn_id, call_id))}) + if command.type == 'generation.submit': + command = command.model_copy(update={ + 'payload': command.payload.model_copy(update={'agentRunId': turn_id})}) if command.type == 'creation.batch' and any(operation.type not in COMMANDS for operation in command.payload.operations): raise ProjectError('批量命令包含未开放的 Agent 操作。', 403) if command.type in {'generation.submit', 'job.cancel', 'job.recover'}: diff --git a/packages/core/src/opendirector/application/creation_generation.py b/packages/core/src/opendirector/application/creation_generation.py index fb5fb207e..a49448b3e 100644 --- a/packages/core/src/opendirector/application/creation_generation.py +++ b/packages/core/src/opendirector/application/creation_generation.py @@ -396,6 +396,8 @@ def accept(before): originItemId=origin, previousJobId=command.payload.previousJobId, adoptionPolicy=command.payload.adoptionPolicy, adoptionResultId=old.currentResultId, adoptionMaterial=material, batchId=command.payload.batchId, showAdditionalResult=command.payload.showAdditionalResult) + if source == 'agent' and command.payload.agentRunId is not None: + stub = stub.model_copy(update={'agentRunId': command.payload.agentRunId}) task = self.task(stub) records = {str(a.id): media_record(a) for a in before.assets} try: @@ -561,6 +563,7 @@ def apply(current, asset): 'canvasItems': (*tuple(adopted_item if item and i.id == item.id else i for i in current.canvasItems), *([additional] if additional else [])), 'generationSpecs': tuple(adopted_spec if s.id == spec.id else s for s in current.generationSpecs)}) provenance = AssetProvenance(source='generated', jobId=job.id, inputFingerprint=job.inputFingerprint, + agentRunId=job.agentRunId, sources=tuple({r.asset.assetId: r.asset for r in job.inputSnapshot.references}.values())) receipt = self.assets.register(command, source='background', provenance=provenance, apply=apply) if receipt.status == 'rejected': diff --git a/packages/core/src/opendirector/domain/creation/chat.py b/packages/core/src/opendirector/domain/creation/chat.py new file mode 100644 index 000000000..01f0ce79c --- /dev/null +++ b/packages/core/src/opendirector/domain/creation/chat.py @@ -0,0 +1,27 @@ +"""Typed navigation metadata persisted with assistant conversation replies.""" +from typing import Literal +from uuid import UUID + +from pydantic import Field + +from .base import Contract + + +class ChatCanvasLinkCandidate(Contract): + nodeId: UUID | None = Field(default=None, description='Canvas item candidate identity.') + assetId: UUID | None = Field(default=None, description='Project asset candidate identity.') + kind: str | None = Field(default=None, description='Candidate business kind.') + title: str | None = Field(default=None, description='Candidate display title.') + hidden: bool | None = Field(default=None, description='Whether the candidate is hidden.') + + +class ChatCanvasLink(Contract): + target: str = Field(description='Original Markdown destination.') + status: Literal['focused', 'hidden', 'materialize', 'ambiguous', 'missing', + 'cross-project', 'external'] = Field(description='Typed link resolution outcome.') + nodeId: UUID | None = Field(default=None, description='Resolved canvas item identity.') + assetId: UUID | None = Field(default=None, description='Resolved project asset identity.') + projectId: UUID | None = Field(default=None, description='Destination project identity.') + kind: str | None = Field(default=None, description='Resolved business kind.') + action: Literal['reveal', 'materialize'] | None = Field(default=None, description='Required presentation action.') + candidates: tuple[ChatCanvasLinkCandidate, ...] | None = Field(default=None, description='Candidates requiring user choice.') diff --git a/packages/core/src/opendirector/domain/creation/commands.py b/packages/core/src/opendirector/domain/creation/commands.py index 2e02ff8ca..cbe7992c7 100644 --- a/packages/core/src/opendirector/domain/creation/commands.py +++ b/packages/core/src/opendirector/domain/creation/commands.py @@ -213,6 +213,7 @@ class GenerationSubmit(Contract): adoptionPolicy: Literal['if_current', 'candidate_only'] = Field(default='if_current', description='Policy for adopting the completed result.') batchId: UUID | None = Field(default=None, description='Generation batch ID.') showAdditionalResult: bool = Field(default=False, description='Whether to display an additional result item.') + agentRunId: UUID | None = Field(default=None, description='Trusted Agent turn that initiated this execution.') @model_validator(mode='after') def presentation(self): diff --git a/packages/core/src/opendirector/domain/creation/generation.py b/packages/core/src/opendirector/domain/creation/generation.py index 91b6f0b40..5a1f6aefb 100644 --- a/packages/core/src/opendirector/domain/creation/generation.py +++ b/packages/core/src/opendirector/domain/creation/generation.py @@ -301,6 +301,7 @@ class Job(ProjectEntity): adoptionMaterial: AssetRef | None = Field(default=None, description='Material version reserved for adoption.') adopted: bool = Field(default=False, description='Whether the generated result was adopted.') adapterParameters: dict[str, JsonValue] = Field(default_factory=dict, description='Fixed provider-specific execution parameters.') + agentRunId: UUID | None = Field(default=None, description='Agent turn that initiated this execution, when applicable.') @model_validator(mode='after') def execution_identity(self): diff --git a/scripts/generate_contracts.py b/scripts/generate_contracts.py index 20426da25..1d9a4b333 100644 --- a/scripts/generate_contracts.py +++ b/scripts/generate_contracts.py @@ -17,7 +17,7 @@ from opendirector.domain.creation.schema import ALIASES, CreationContracts from opendirector.domain.rule_facts import typescript_module -MODULES = ('base', 'assets', 'models', 'generation', 'editors', 'canvas', 'project', 'commands', 'timeline_packages', 'schema') +MODULES = ('base', 'assets', 'models', 'generation', 'editors', 'canvas', 'chat', 'project', 'commands', 'timeline_packages', 'schema') def models(): diff --git a/scripts/verify_canvas_link_mutations.py b/scripts/verify_canvas_link_mutations.py new file mode 100644 index 000000000..b41800d24 --- /dev/null +++ b/scripts/verify_canvas_link_mutations.py @@ -0,0 +1,57 @@ +"""Run the same assertions against the repaired baseline or an in-memory mutant. + +PYTHONPATH=/tmp/odstubs:packages/core/src:apps/api/src python3 \ + scripts/verify_canvas_link_mutations.py R1 baseline|mutant +No tracked source is written. Each invocation starts a fresh Python process. +""" +import importlib +from pathlib import Path +import sys + +# scripts/opendirector.py must not shadow the installed/source package. +sys.path = [entry for entry in sys.path + if Path(entry).resolve() != Path(__file__).resolve().parent] + +import pytest + +CASES = { + 'R1': ('opendirector.agent.canvas_links', + "assets = [asset for asset in assets if _run_id(asset) == str(turn_id)]", + 'assets = assets', 'relative_link_uses_the_turn_provenance'), + 'R2': ('opendirector.agent.canvas_links', + " or getattr(binding, 'materialOverride', None) is not None", '', 'material_override_links'), + 'R3a': ('opendirector.agent.canvas_links', 'raw = target.strip()', + 'raw = unquote(target.strip())', 'url_path_is_decoded_once'), + 'R3b': ('opendirector.agent.canvas_links', 'path = unquote(parsed.path)', + 'path = parsed.path', 'url_path_is_decoded_once'), + 'R4': ('opendirector.agent.canvas_links', '*_mention_targets(text)', '', 'plain_mentions_resolve'), + 'R5': ('opendirector.agent.canvas_links', + "def _mention_targets(text: str) -> list[str]:\n targets = []", + "def _mention_targets(text: str) -> list[str]:\n targets = ['@Finished note']", + 'mentions_in_code'), + 'R6': ('opendirector.agent.canvas_links', 'for token in MarkdownIt().parse(text):', + "for token in MarkdownIt().parse(text.split('\\n\\n')[0]):", 'reference_style_markdown'), + 'R7': ('opendirector_api.agent', + "ChatCanvasLink.model_validate(link).model_dump(mode='json')", 'link', + 'finished_turn_persists'), + 'M2': ('opendirector.agent.canvas_links', + "visible = _effective_visibility(item, _items(snapshot))", 'visible = True', + 'hidden_materialize_ambiguous or child_under_hidden'), + 'M4': ('opendirector.agent.canvas_links', + "if token.type != 'inline' or not token.children:", + "if token.type == 'fence':\n targets.append('fenced.md')\n if token.type != 'inline' or not token.children:", + 'code_examples_are_not_navigable'), +} + +case, mode = sys.argv[1:] +assert mode in {'baseline', 'mutant'} +name, old, new, selector = CASES[case] +module = importlib.import_module(name) +if mode == 'mutant': + path = Path(module.__file__) + source = path.read_text() + assert old in source, f'{case}: mutation source absent' + exec(compile(source.replace(old, new), str(path), 'exec'), module.__dict__) +result = pytest.main(['tests/test_canvas_links.py', '-q', '-k', selector, '-p', 'no:cacheprovider']) +print(f'{case} {mode}: pytest exit={result}') +sys.exit(0 if result == (0 if mode == 'baseline' else 1) else 1) diff --git a/src/backend/chat.ts b/src/backend/chat.ts index f935df4c8..a162f54d4 100644 --- a/src/backend/chat.ts +++ b/src/backend/chat.ts @@ -2,6 +2,7 @@ import type { ReferenceContent } from "../canvas/chatReferenceContent.ts"; import { defaultTranslate } from "../canvas/assistantTranslate.ts"; import type { Translate } from "../i18n/I18nProvider.tsx"; import { headers, json, request, responseError } from "./api.ts"; +import type { ChatCanvasLink, ChatCanvasLinkCandidate } from "../contracts/generated.ts"; export type AgentRuntime = | "default" @@ -60,7 +61,10 @@ export interface ChatSource { url: string; title: string; } +/** A resolved canvas reference persisted with an assistant reply. */ +export type { ChatCanvasLink, ChatCanvasLinkCandidate }; export interface ChatMessage { + canvasLinks?: ChatCanvasLink[]; sources?: ChatSource[]; plan?: ChatPlanEntry[]; runtime?: AgentRuntime; diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 1da3e8e12..84e29a9eb 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -68,6 +68,7 @@ export function AssistantPanel({ onModelSelections, onOpenSettings, onPrepareReferences, + onFocusCanvasNode, }: { documentId: string; documentTitle: string; @@ -84,6 +85,7 @@ export function AssistantPanel({ onModelSelections?: (selections: ModelSelections) => void; onOpenSettings: () => void; onPrepareReferences: () => Promise; + onFocusCanvasNode?: (nodeId: string) => void; }) { const t = useT(); const chat = useAssistantChat(documentId, catalog, onTurnEnd, t, open); @@ -434,7 +436,7 @@ export function AssistantPanel({ )}
{message.role === "assistant" ? ( - + ) : message.role === "user" && (message.content || message.assets?.length) ? ( referenceContent( diff --git a/src/canvas/AssistantReply.tsx b/src/canvas/AssistantReply.tsx index 4c2981b5c..51f3c3089 100644 --- a/src/canvas/AssistantReply.tsx +++ b/src/canvas/AssistantReply.tsx @@ -1,14 +1,18 @@ import { memo, useMemo, useState } from "react"; -import { Streamdown } from "streamdown"; -import type { LinkSafetyModalProps } from "streamdown"; +import type { ComponentProps } from "react"; +import { Streamdown, defaultRehypePlugins } from "streamdown"; +import type { Components, LinkSafetyModalProps, StreamdownProps } from "streamdown"; import type { ChatMessage, ChatTool } from "../backend/chat"; import type { Translate } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; import { CanvasIcon } from "./CanvasIcon"; import { AssistantLinkInfo } from "./AssistantLinkInfo"; +import { applyCanvasLinkClick } from "./assistantCanvasLink"; +import { prepareCanvasReferences, renderCanvasReferences } from "./canvasReferences.ts"; import { activityProgress, replySections, toolLabel } from "./assistantActivity.ts"; import { assistantToolText } from "./assistantToolText.ts"; import { assistantCitationFormatter } from "./assistantCitations.ts"; +import { renderCanvasMentions } from "./assistantCanvasMentions.ts"; import { defaultTranslate } from "./assistantTranslate.ts"; const STATUS: Record = { @@ -94,14 +98,17 @@ function ToolGroup({ tools, t }: { tools: ChatTool[]; t: Translate }) { export const AssistantReply = memo(function AssistantReply({ message, thinking, + onCanvasLink, t = defaultTranslate, }: { message: ChatMessage; thinking: boolean; + onCanvasLink?: (nodeId: string) => void; /** Injected by the panel; standalone mounts fall back to the active locale. */ t?: Translate; }) { const [copyState, setCopyState] = useState<"idle" | "copied" | "failed">("idle"); + const [canvasHint, setCanvasHint] = useState(null); const formatText = useMemo(() => assistantCitationFormatter( message.text, message.sources, message.status === "streaming", t, ), [message.text, message.sources, message.status, t]); @@ -109,6 +116,30 @@ export const AssistantReply = memo(function AssistantReply({ enabled: true, renderModal: (props: LinkSafetyModalProps) => props.isOpen ? : null, }), [t]); + const projectLinks = useMemo(() => { + const links = new Map(message.canvasLinks?.map((link) => [link.target, link])); + // Give persisted project references their own renderer. Ordinary anchors + // keep Streamdown's sanitize and link-safety behavior. + const components: Components = { + "canvas-reference": ({ href, children }: ComponentProps<"a">) => { + const link = links.get(String(href)); + if (!link) return {children}; + return { + event.preventDefault(); + const hint = applyCanvasLinkClick(link, (nodeId) => onCanvasLink?.(nodeId)); + setCanvasHint(hint + ? t(hint.hintKey) + (hint.projectId ? ` · ${hint.projectId}` : "") + : null); + }}>{children}; + }, + }; + const targets = Array.from(links.keys()); + const rehypePlugins: StreamdownProps["rehypePlugins"] = [defaultRehypePlugins.raw, + [prepareCanvasReferences, { targets }], defaultRehypePlugins.sanitize, + defaultRehypePlugins.harden, [renderCanvasReferences, { targets }], + [renderCanvasMentions, { targets }]]; + return { components, rehypePlugins }; + }, [message.canvasLinks, onCanvasLink, t]); const copyLabel = copyState === "copied" ? t("assistant.reply.copied") @@ -124,6 +155,8 @@ export const AssistantReply = memo(function AssistantReply({ mode={message.status === "streaming" ? "streaming" : "static"} isAnimating={message.status === "streaming"} linkSafety={linkSafety} + components={projectLinks.components} + rehypePlugins={projectLinks.rehypePlugins} > {formatText(text)} @@ -134,6 +167,7 @@ export const AssistantReply = memo(function AssistantReply({ : ; return ( <> + {canvasHint &&

{canvasHint}

} {!!message.plan?.length &&
diff --git a/src/canvas/assistantCanvasLink.ts b/src/canvas/assistantCanvasLink.ts new file mode 100644 index 000000000..230e57234 --- /dev/null +++ b/src/canvas/assistantCanvasLink.ts @@ -0,0 +1,44 @@ +import type { ChatCanvasLink } from "../backend/chat"; +import type { MessageKey } from "../i18n/types"; + +/** The decision made when a resolved canvas link is clicked. */ +export type CanvasLinkClick = + | { focus: string } + | { hintKey: MessageKey; projectId?: string }; + +export type CanvasLinkHint = { hintKey: MessageKey; projectId?: string }; + +/** Map a resolved link to its click behavior, split out for logic-level tests. */ +export function canvasLinkClickAction(link: ChatCanvasLink): CanvasLinkClick { + if ((link.status === "focused" || link.status === "hidden") && link.nodeId) { + return { focus: link.nodeId }; + } + const keys: Record = { + "cross-project": "assistant.reply.canvasLink.crossProject", + materialize: "assistant.reply.canvasLink.materialize", + ambiguous: "assistant.reply.canvasLink.ambiguous", + missing: "assistant.reply.canvasLink.missing", + }; + return { + hintKey: keys[link.status] ?? "assistant.reply.canvasLink.missing", + ...(link.status === "cross-project" && link.projectId + ? { projectId: link.projectId } + : {}), + }; +} + +/** + * Apply a click: focus the referenced node, or return the hint to display. + * Split from the React handler so the rendered-reference path can be tested. + */ +export function applyCanvasLinkClick( + link: ChatCanvasLink, + focus: (nodeId: string) => void, +): CanvasLinkHint | null { + const action = canvasLinkClickAction(link); + if ("focus" in action) { + focus(action.focus); + return null; + } + return action; +} diff --git a/src/canvas/assistantCanvasMentions.ts b/src/canvas/assistantCanvasMentions.ts new file mode 100644 index 000000000..523839dca --- /dev/null +++ b/src/canvas/assistantCanvasMentions.ts @@ -0,0 +1,69 @@ +import type { Element, Root } from "hast"; + +const WORD_CHARACTER = /\w/; + +/** Turn plain @title mentions into the same clickable canvas-reference nodes. */ +export function renderCanvasMentions({ targets }: { targets: string[] }): (tree: Root) => void { + // Longer names first so a prefix mention never splits a longer one. + const mentions = targets.filter((target) => target.startsWith("@")) + .sort((a, b) => b.length - a.length); + return (tree: Root) => { + if (!mentions.length) return; + const split = (value: string): Element["children"] => { + const parts: Element["children"] = []; + let rest = value; + while (rest.length) { + let index = -1; + let mention = ""; + for (const target of mentions) { + let found = -1; + let from = 0; + while ((found = rest.indexOf(target, from)) !== -1) { + // Mirror the backend's (? 0) parts.push({ type: "text", value: rest.slice(0, index) }); + parts.push({ + type: "element", + tagName: "canvas-reference", + properties: { href: mention }, + children: [{ type: "text", value: mention }], + }); + rest = rest.slice(index + mention.length); + } + return parts; + }; + const visit = (parent: Root | Element) => { + const next: Element["children"] = []; + for (const child of parent.children) { + if (child.type === "element") { + if (child.tagName !== "code" && child.tagName !== "pre" && + child.tagName !== "a" && child.tagName !== "canvas-reference") { + visit(child); + } + next.push(child); + } else if (child.type === "text") { + next.push(...split(child.value)); + } else { + next.push(child as Element["children"][number]); + } + } + parent.children = next; + }; + visit(tree); + }; +} diff --git a/src/canvas/canvasReferences.ts b/src/canvas/canvasReferences.ts new file mode 100644 index 000000000..71f704daa --- /dev/null +++ b/src/canvas/canvasReferences.ts @@ -0,0 +1,40 @@ +import type { Element, Root } from "hast"; + +/** + * Rewrite persisted project-reference anchors into a private, sanitizable URL + * before the Markdown sanitizer runs, then restore the original target and + * tag after it. The placeholder is https so a sanitizer that keeps ordinary + * links cannot drop the reference; the reverse pass runs last so no sanitizer + * sees the custom `canvas-reference` element. + */ +function canvasReferencePlugin({ targets, prepare }: { targets: string[]; prepare: boolean }) { + const placeholders = new Map(targets.map((target, index) => + [target, `https://opendirector.invalid/canvas-reference/${index}`])); + const originals = new Map(Array.from(placeholders, ([target, placeholder]) => [placeholder, target])); + return (tree: Root) => { + const visit = (parent: Root | Element) => { + for (const child of parent.children) { + if (child.type !== "element") continue; + if (child.tagName === "a") { + const href = String(child.properties.href); + if (prepare && placeholders.has(href)) { + child.properties.href = placeholders.get(href); + } else if (!prepare && originals.has(href)) { + child.tagName = "canvas-reference"; + child.properties.href = originals.get(href); + } + } + visit(child); + } + }; + visit(tree); + }; +} + +export function prepareCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: true }); +} + +export function renderCanvasReferences(options: { targets: string[] }) { + return canvasReferencePlugin({ ...options, prepare: false }); +} diff --git a/src/canvas/workspace/WorkspaceView.tsx b/src/canvas/workspace/WorkspaceView.tsx index 1905161b0..32711da98 100644 --- a/src/canvas/workspace/WorkspaceView.tsx +++ b/src/canvas/workspace/WorkspaceView.tsx @@ -2248,6 +2248,31 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { ); }} onTurnEnd={() => void syncServiceResults()} + onFocusCanvasNode={(id) => { + void (async () => { + if ((doc.hiddenNodes ?? []).some((node) => node.id === id)) { + const client = creationClient(doc.id); + const item = client.snapshot.canvasItems.find((node) => node.id === id); + if (!item) return; + const versions = [objectVersion("canvas_item", item)]; + let parentId = item.parentId; + while (parentId) { + const parent = client.snapshot.canvasItems.find( + (item) => item.id === parentId, + )!; + versions.push(objectVersion("canvas_item", parent)); + parentId = parent.parentId; + } + await client.command("canvas.show", { item }, versions); + await client.refresh(); + await new Promise((resolve) => requestAnimationFrame(() => resolve())); + } else if (!doc.nodes.some((node) => node.id === id)) { + return; + } + setSelected([id]); + void flow.fitView({ nodes: [{ id }], padding: 0.35, duration: 250, maxZoom: 1.2 }); + })().catch((error) => notify(errorMessage(error))); + }} onModelSelections={setAssistantModels} onPrepareReferences={() => persist(historyRef.current.present)} onOpenSettings={() => { diff --git a/src/contracts/creation.schema.json b/src/contracts/creation.schema.json index 5eb935515..3e8a7affa 100644 --- a/src/contracts/creation.schema.json +++ b/src/contracts/creation.schema.json @@ -3291,6 +3291,198 @@ "title": "ChangedObject", "type": "object" }, + "ChatCanvasLink": { + "additionalProperties": false, + "properties": { + "target": { + "description": "Original Markdown destination.", + "title": "Target", + "type": "string" + }, + "status": { + "description": "Typed link resolution outcome.", + "enum": [ + "focused", + "hidden", + "materialize", + "ambiguous", + "missing", + "cross-project", + "external" + ], + "title": "Status", + "type": "string" + }, + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved canvas item identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved project asset identity.", + "title": "Assetid" + }, + "projectId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Destination project identity.", + "title": "Projectid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved business kind.", + "title": "Kind" + }, + "action": { + "anyOf": [ + { + "enum": [ + "reveal", + "materialize" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Required presentation action.", + "title": "Action" + }, + "candidates": { + "anyOf": [ + { + "items": { + "$ref": "#/$defs/ChatCanvasLinkCandidate" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidates requiring user choice.", + "title": "Candidates" + } + }, + "required": [ + "target", + "status" + ], + "title": "ChatCanvasLink", + "type": "object" + }, + "ChatCanvasLinkCandidate": { + "additionalProperties": false, + "properties": { + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Canvas item candidate identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Project asset candidate identity.", + "title": "Assetid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate business kind.", + "title": "Kind" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate display title.", + "title": "Title" + }, + "hidden": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Whether the candidate is hidden.", + "title": "Hidden" + } + }, + "title": "ChatCanvasLinkCandidate", + "type": "object" + }, "CommandBase": { "additionalProperties": false, "properties": { @@ -5473,6 +5665,20 @@ "description": "Whether to display an additional result item.", "title": "Showadditionalresult", "type": "boolean" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Trusted Agent turn that initiated this execution.", + "title": "Agentrunid" } }, "required": [ @@ -7182,6 +7388,20 @@ "description": "Fixed provider-specific execution parameters.", "title": "Adapterparameters", "type": "object" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Agent turn that initiated this execution, when applicable.", + "title": "Agentrunid" } }, "required": [ @@ -18508,6 +18728,211 @@ "title": "ChangedObject", "type": "object" }, + "ChatCanvasLink": { + "additionalProperties": false, + "properties": { + "target": { + "description": "Original Markdown destination.", + "title": "Target", + "type": "string" + }, + "status": { + "description": "Typed link resolution outcome.", + "enum": [ + "focused", + "hidden", + "materialize", + "ambiguous", + "missing", + "cross-project", + "external" + ], + "title": "Status", + "type": "string" + }, + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved canvas item identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved project asset identity.", + "title": "Assetid" + }, + "projectId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Destination project identity.", + "title": "Projectid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Resolved business kind.", + "title": "Kind" + }, + "action": { + "anyOf": [ + { + "enum": [ + "reveal", + "materialize" + ], + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Required presentation action.", + "title": "Action" + }, + "candidates": { + "anyOf": [ + { + "items": { + "$ref": "#/$defs/ChatCanvasLinkCandidate" + }, + "type": "array" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidates requiring user choice.", + "title": "Candidates" + } + }, + "required": [ + "target", + "status", + "nodeId", + "assetId", + "projectId", + "kind", + "action", + "candidates" + ], + "title": "ChatCanvasLink", + "type": "object" + }, + "ChatCanvasLinkCandidate": { + "additionalProperties": false, + "properties": { + "nodeId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Canvas item candidate identity.", + "title": "Nodeid" + }, + "assetId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Project asset candidate identity.", + "title": "Assetid" + }, + "kind": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate business kind.", + "title": "Kind" + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Candidate display title.", + "title": "Title" + }, + "hidden": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Whether the candidate is hidden.", + "title": "Hidden" + } + }, + "required": [ + "nodeId", + "assetId", + "kind", + "title", + "hidden" + ], + "title": "ChatCanvasLinkCandidate", + "type": "object" + }, "CommandBase": { "additionalProperties": false, "properties": { @@ -20761,6 +21186,20 @@ "description": "Whether to display an additional result item.", "title": "Showadditionalresult", "type": "boolean" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Trusted Agent turn that initiated this execution.", + "title": "Agentrunid" } }, "required": [ @@ -20770,7 +21209,8 @@ "previousJobId", "adoptionPolicy", "batchId", - "showAdditionalResult" + "showAdditionalResult", + "agentRunId" ], "title": "GenerationSubmit", "type": "object" @@ -22551,6 +22991,20 @@ "description": "Fixed provider-specific execution parameters.", "title": "Adapterparameters", "type": "object" + }, + "agentRunId": { + "anyOf": [ + { + "format": "uuid", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Agent turn that initiated this execution, when applicable.", + "title": "Agentrunid" } }, "required": [ @@ -22580,7 +23034,8 @@ "adoptionResultId", "adoptionMaterial", "adopted", - "adapterParameters" + "adapterParameters", + "agentRunId" ], "title": "Job", "type": "object" diff --git a/src/contracts/generated.ts b/src/contracts/generated.ts index 3e5a30968..939b76042 100644 --- a/src/contracts/generated.ts +++ b/src/contracts/generated.ts @@ -361,6 +361,25 @@ export interface ChangedObjectInput extends ContractInput { readonly change: "created" | "updated" | "hidden" | "deleted"; } +export interface ChatCanvasLinkInput extends ContractInput { + readonly target: string; + readonly status: "focused" | "hidden" | "materialize" | "ambiguous" | "missing" | "cross-project" | "external"; + readonly nodeId?: string | null; + readonly assetId?: string | null; + readonly projectId?: string | null; + readonly kind?: string | null; + readonly action?: "reveal" | "materialize" | null; + readonly candidates?: ReadonlyArray | null; +} + +export interface ChatCanvasLinkCandidateInput extends ContractInput { + readonly nodeId?: string | null; + readonly assetId?: string | null; + readonly kind?: string | null; + readonly title?: string | null; + readonly hidden?: boolean | null; +} + export interface CommandBaseInput extends ContractInput { readonly commandId: string; readonly projectId: string; @@ -557,6 +576,7 @@ export interface GenerationSubmitInput extends ContractInput { readonly adoptionPolicy?: "if_current" | "candidate_only"; readonly batchId?: string | null; readonly showAdditionalResult?: boolean; + readonly agentRunId?: string | null; } export interface GenerationSubmitCommandInput extends CommandBaseInput { @@ -720,6 +740,7 @@ export interface JobInput extends ProjectEntityInput { readonly adoptionMaterial?: AssetRefInput | null; readonly adopted?: boolean; readonly adapterParameters?: Readonly>; + readonly agentRunId?: string | null; } export interface JobCancelCommandInput extends CommandBaseInput { @@ -1936,6 +1957,25 @@ export interface ChangedObject extends Contract { readonly change: "created" | "updated" | "hidden" | "deleted"; } +export interface ChatCanvasLink extends Contract { + readonly target: string; + readonly status: "focused" | "hidden" | "materialize" | "ambiguous" | "missing" | "cross-project" | "external"; + readonly nodeId: string | null; + readonly assetId: string | null; + readonly projectId: string | null; + readonly kind: string | null; + readonly action: "reveal" | "materialize" | null; + readonly candidates: ReadonlyArray | null; +} + +export interface ChatCanvasLinkCandidate extends Contract { + readonly nodeId: string | null; + readonly assetId: string | null; + readonly kind: string | null; + readonly title: string | null; + readonly hidden: boolean | null; +} + export interface CommandBase extends Contract { readonly commandId: string; readonly projectId: string; @@ -2132,6 +2172,7 @@ export interface GenerationSubmit extends Contract { readonly adoptionPolicy: "if_current" | "candidate_only"; readonly batchId: string | null; readonly showAdditionalResult: boolean; + readonly agentRunId: string | null; } export interface GenerationSubmitCommand extends CommandBase { @@ -2295,6 +2336,7 @@ export interface Job extends ProjectEntity { readonly adoptionMaterial: AssetRef | null; readonly adopted: boolean; readonly adapterParameters: Readonly>; + readonly agentRunId: string | null; } export interface JobCancelCommand extends CommandBase { diff --git a/src/i18n/messages/en-US/assistant.ts b/src/i18n/messages/en-US/assistant.ts index 3e19abda4..f3d067c2e 100644 --- a/src/i18n/messages/en-US/assistant.ts +++ b/src/i18n/messages/en-US/assistant.ts @@ -1,6 +1,10 @@ /** English assistant copy. Keys must match `zh-CN/assistant.ts`. */ export const assistant = { "assistant.reply.sourceUnavailable": "Source unavailable", + "assistant.reply.canvasLink.materialize": "This item is not on the canvas yet.", + "assistant.reply.canvasLink.ambiguous": "This link matches several canvas items.", + "assistant.reply.canvasLink.missing": "This linked item is not in the project.", + "assistant.reply.canvasLink.crossProject": "This link points to another project.", "assistant.reply.copyLink": "Copy link", "assistant.reply.copyLinkFailed": "Copy failed; select the URL to copy it", "assistant.reply.openLink": "Open link", diff --git a/src/i18n/messages/zh-CN/assistant.ts b/src/i18n/messages/zh-CN/assistant.ts index 4c95240b8..828c4388a 100644 --- a/src/i18n/messages/zh-CN/assistant.ts +++ b/src/i18n/messages/zh-CN/assistant.ts @@ -1,6 +1,10 @@ /** Assistant panel, reply activity, pickers, references and chat transport. */ export const assistant = { "assistant.reply.sourceUnavailable": "来源不可用", + "assistant.reply.canvasLink.materialize": "该对象尚未放置在画布中。", + "assistant.reply.canvasLink.ambiguous": "该链接匹配多个画布对象。", + "assistant.reply.canvasLink.missing": "链接的对象不在当前工程中。", + "assistant.reply.canvasLink.crossProject": "该链接指向另一个工程。", "assistant.reply.copyLink": "复制链接", "assistant.reply.copyLinkFailed": "复制失败,请选择链接地址复制", "assistant.reply.openLink": "打开链接", diff --git a/tests/canvasLinkClick.test.ts b/tests/canvasLinkClick.test.ts new file mode 100644 index 000000000..3020501e4 --- /dev/null +++ b/tests/canvasLinkClick.test.ts @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { canvasLinkClickAction } from "../src/canvas/assistantCanvasLink.ts"; +import type { ChatCanvasLink } from "../src/backend/chat.ts"; + +const id = "11111111-1111-4111-8111-111111111111"; + +function link(overrides: Partial): ChatCanvasLink { + return { + target: "notes.md", + status: "focused", + nodeId: null, + assetId: null, + projectId: null, + kind: null, + action: null, + candidates: null, + ...overrides, + }; +} + +test("focused and hidden links focus their node id on click", () => { + assert.deepEqual(canvasLinkClickAction(link({ status: "focused", nodeId: id })), { focus: id }); + assert.deepEqual(canvasLinkClickAction(link({ status: "hidden", nodeId: id, action: "reveal" })), { focus: id }); +}); + +test("a focused link without a node id never focuses", () => { + assert.deepEqual(canvasLinkClickAction(link({ status: "focused", nodeId: null })), { + hintKey: "assistant.reply.canvasLink.missing", + }); +}); + +test("non-focusable outcomes show a translated hint and never focus a colliding id", () => { + const projectId = "22222222-2222-4222-8222-222222222222"; + assert.deepEqual(canvasLinkClickAction(link({ status: "cross-project", nodeId: id, projectId })), { + hintKey: "assistant.reply.canvasLink.crossProject", + projectId, + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "materialize", assetId: id })), { + hintKey: "assistant.reply.canvasLink.materialize", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "ambiguous" })), { + hintKey: "assistant.reply.canvasLink.ambiguous", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "missing" })), { + hintKey: "assistant.reply.canvasLink.missing", + }); + assert.deepEqual(canvasLinkClickAction(link({ status: "external" })), { + hintKey: "assistant.reply.canvasLink.missing", + }); +}); diff --git a/tests/canvasLinkMessages.test.ts b/tests/canvasLinkMessages.test.ts new file mode 100644 index 000000000..97c37de2d --- /dev/null +++ b/tests/canvasLinkMessages.test.ts @@ -0,0 +1,97 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { translate } from "../src/i18n/messages.ts"; +import { zhCN } from "../src/i18n/messages/zh-CN/index.ts"; +import { enUS } from "../src/i18n/messages/en-US/index.ts"; +import { renderCanvasMentions } from "../src/canvas/assistantCanvasMentions.ts"; + +type HastText = { type: "text"; value: string }; +type HastElement = { type: "element"; tagName: string; properties: Record; children: HastNode[] }; +type HastNode = HastText | HastElement | { type: string }; +type HastRoot = { type: "root"; children: HastNode[] }; + +function text(value: string): HastText { + return { type: "text", value }; +} + +function element(tagName: string, children: HastNode[], properties: Record = {}): HastElement { + return { type: "element", tagName, properties, children }; +} + +function rendered(value: string, targets: string[]): HastNode[] { + const tree: HastRoot = { type: "root", children: [element("p", [text(value)])] }; + renderCanvasMentions({ targets })(tree as unknown as Parameters>[0]); + return (tree.children[0] as HastElement).children; +} + +const keys = [ + "assistant.reply.canvasLink.materialize", + "assistant.reply.canvasLink.ambiguous", + "assistant.reply.canvasLink.missing", + "assistant.reply.canvasLink.crossProject", +] as const; + +test("canvas link messages exist in both catalogs", () => { + for (const key of keys) { + assert.ok(key in zhCN, `missing zh-CN key ${key}`); + assert.ok(key in enUS, `missing en-US key ${key}`); + } +}); + +test("canvas link messages resolve in both locales without leaking the other language", () => { + for (const key of keys) { + const zh = translate("zh-CN", key); + const en = translate("en-US", key); + assert.ok(zh.length > 0 && en.length > 0); + assert.match(zh, /[㐀-鿿]/, `${key} should be Chinese`); + assert.doesNotMatch(en, /[㐀-鿿]/, `${key} must not contain Chinese`); + assert.notEqual(zh, en); + } +}); + +test("canvas link outcome copy states the condition without promising an action", () => { + // Hidden links focus their node (which the workspace reveals); only + // outcomes that cannot focus have a hint, so there is no hidden hint key. + // Clicking does not materialize an item or open a candidate picker, so the + // copy must not instruct the user to "show" or "choose". + assert.equal(translate("en-US", "assistant.reply.canvasLink.materialize"), + "This item is not on the canvas yet."); + assert.equal(translate("zh-CN", "assistant.reply.canvasLink.materialize"), + "该对象尚未放置在画布中。"); + assert.doesNotMatch(translate("en-US", "assistant.reply.canvasLink.ambiguous"), /choose/i); + assert.doesNotMatch(translate("zh-CN", "assistant.reply.canvasLink.ambiguous"), /请选择/); +}); + +test("a standalone mention renders as a clickable canvas reference", () => { + assert.deepEqual(rendered("@Title", ["@Title"]), [ + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + +test("a mention glued to a word character stays plain prose", () => { + assert.deepEqual(rendered("foo@Title", ["@Title"]), [text("foo@Title")]); + assert.deepEqual(rendered("see foo@Title and @Title", ["@Title"]), [ + text("see foo@Title and "), + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + +test("CJK prose directly before a mention is a boundary, like the backend", () => { + // JavaScript \w is ASCII-only, so the Chinese character before @ is prose, + // not part of the mention; the backend now uses the same boundary. + assert.deepEqual(rendered("看这个@Title", ["@Title"]), [ + text("看这个"), + element("canvas-reference", [text("@Title")], { href: "@Title" }), + ]); +}); + +test("a mention inside an anchor (an autolinked URL) is left untouched", () => { + const tree: HastRoot = { + type: "root", + children: [element("a", [text("https://x/@Title")], { href: "https://x/@Title" })], + }; + renderCanvasMentions({ targets: ["@Title"] })(tree as unknown as Parameters>[0]); + const anchor = tree.children[0] as HastElement; + assert.equal(anchor.tagName, "a"); + assert.deepEqual(anchor.children, [text("https://x/@Title")]); +}); diff --git a/tests/canvasLinkReferences.test.ts b/tests/canvasLinkReferences.test.ts new file mode 100644 index 000000000..72fe59d71 --- /dev/null +++ b/tests/canvasLinkReferences.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { prepareCanvasReferences, renderCanvasReferences } from "../src/canvas/canvasReferences.ts"; +import { applyCanvasLinkClick } from "../src/canvas/assistantCanvasLink.ts"; +import type { ChatCanvasLink } from "../src/backend/chat.ts"; + +type HastText = { type: "text"; value: string }; +type HastElement = { + type: "element"; + tagName: string; + properties: Record; + children: HastNode[]; +}; +type HastNode = HastText | HastElement | { type: string }; +type HastRoot = { type: "root"; children: HastNode[] }; + +const nodeId = "11111111-1111-4111-8111-111111111111"; + +function link(overrides: Partial): ChatCanvasLink { + return { + target: `canvas:${nodeId}`, + status: "focused", + nodeId, + assetId: null, + projectId: null, + kind: "text", + action: null, + candidates: null, + ...overrides, + }; +} + +/** What Streamdown's Markdown renderer hands to the rehype chain. */ +function anchorTree(href: string, label = "Finished note"): HastRoot { + return { + type: "root", + children: [{ + type: "element", + tagName: "p", + properties: {}, + children: [{ + type: "element", + tagName: "a", + properties: { href }, + children: [{ type: "text", value: label }], + }], + }], + }; +} + +function anchor(tree: HastRoot): HastElement { + return (tree.children[0] as HastElement).children[0] as HastElement; +} + +function chain(tree: HastRoot, targets: string[]): void { + // Mirrors the production plugin order in AssistantReply: prepare, then the + // third-party sanitize/harden passes (which see only the https placeholder + // asserted below), then render. + prepareCanvasReferences({ targets })(tree as never); + renderCanvasReferences({ targets })(tree as never); +} + +test("a persisted markdown reference becomes a clickable canvas-reference", () => { + const persisted = link({}); + // Use the real message.canvasLinks map the component builds. + const links = new Map([[persisted.target, persisted]]); + const targets = Array.from(links.keys()); + const tree = anchorTree(persisted.target); + + prepareCanvasReferences({ targets })(tree as never); + // The placeholder must be an ordinary https URL so the sanitizer keeps it. + assert.equal(anchor(tree).tagName, "a"); + assert.match(String(anchor(tree).properties.href), + /^https:\/\/opendirector\.invalid\/canvas-reference\/0$/); + + renderCanvasReferences({ targets })(tree as never); + const reference = anchor(tree); + assert.equal(reference.tagName, "canvas-reference"); + assert.equal(reference.properties.href, persisted.target); + assert.deepEqual(reference.children, [{ type: "text", value: "Finished note" }]); + + // The component looks the rendered href back up in the persisted links and + // applies the click action; a focused link must reach the canvas callback. + const focused: string[] = []; + const hint = applyCanvasLinkClick(links.get(String(reference.properties.href))!, (id) => focused.push(id)); + assert.equal(hint, null); + assert.deepEqual(focused, [nodeId]); +}); + +test("a reference target that is not persisted stays an ordinary anchor", () => { + // The component only passes targets it has resolved links for; an anchor + // whose destination is not in message.canvasLinks must not become a + // canvas-reference with a guessed identity. + const links = new Map(); + const targets = Array.from(links.keys()); + const href = `canvas:${nodeId}`; + const tree = anchorTree(href); + + chain(tree, targets); + + const untouched = anchor(tree); + assert.equal(untouched.tagName, "a"); + assert.equal(untouched.properties.href, href); + assert.equal(links.get(String(untouched.properties.href)), undefined); +}); + +test("a materialize reference renders but reports a declarative hint", () => { + const persisted = link({ target: "notes.md", status: "materialize", nodeId: null, + assetId: "22222222-2222-4222-8222-222222222222", action: "materialize" }); + const links = new Map([[persisted.target, persisted]]); + const targets = Array.from(links.keys()); + const tree = anchorTree(persisted.target, "notes.md"); + + chain(tree, targets); + const reference = anchor(tree); + assert.equal(reference.tagName, "canvas-reference"); + + const focused: string[] = []; + const hint = applyCanvasLinkClick(links.get(String(reference.properties.href))!, (id) => focused.push(id)); + assert.deepEqual(focused, []); + assert.deepEqual(hint, { hintKey: "assistant.reply.canvasLink.materialize" }); +}); diff --git a/tests/fixtures/creation-project.json b/tests/fixtures/creation-project.json index a040daa0c..c928ec2b1 100644 --- a/tests/fixtures/creation-project.json +++ b/tests/fixtures/creation-project.json @@ -1034,6 +1034,7 @@ "cancelRequested": false, "remoteCancellation": "not_requested", "originItemId": null, + "agentRunId": null, "previousJobId": null, "technicalCheck": null, "adoptionPolicy": "if_current", diff --git a/tests/test_canvas_links.py b/tests/test_canvas_links.py new file mode 100644 index 000000000..99f40f15c --- /dev/null +++ b/tests/test_canvas_links.py @@ -0,0 +1,699 @@ +"""Canvas link resolution and persistence for assistant replies (#333, backend slice).""" +import json +from pathlib import Path +from types import SimpleNamespace as NS +from uuid import UUID, uuid4, uuid5 + +from fastapi.testclient import TestClient +import pytest + +from opendirector.agent.canvas_links import ( + canvas_links_for_text, + format_canvas_link, + link_targets, + resolve_canvas_link, + with_canvas_links, +) +from opendirector.infrastructure.local_projects import LocalProjects +from opendirector_api.main import create_app +from opendirector_api import agent + + +def asset(asset_id, name, *, kind='text', run=None): + return NS(id=asset_id, name=name, kind=kind, path=f'assets/{asset_id}', + provenance=NS(source='generated', agentRunId=run)) + + +def node(node_id, *, kind='text', title='', visible=True, asset_id=None, + parent_id=None): + binding = NS(role='material', objectRef=NS(assetId=asset_id)) if asset_id else NS(role='empty') + return NS(id=node_id, kind=kind, title=title, visible=visible, + parentId=parent_id, position=NS(x=0, y=0), binding=binding) + + +def snapshot(*, project_id=None, assets=(), nodes=()): + return NS(project=NS(id=project_id or uuid4()), assets=tuple(assets), canvasItems=tuple(nodes)) + + +def test_relative_link_uses_the_turn_provenance_and_never_picks_an_unrelated_duplicate(): + turn, other = uuid4(), uuid4() + current = asset(uuid4(), 'notes.md', run=turn) + unrelated = asset(uuid4(), 'notes.md', run=other) + item = node(uuid4(), title='Finished note', asset_id=current.id) + project = snapshot(assets=[current, unrelated], nodes=[item]) + + resolved = resolve_canvas_link('notes.md', project, turn) + assert resolved == {'target': 'notes.md', 'status': 'focused', + 'nodeId': str(item.id), 'kind': 'text'} + + # Without the turn context, the duplicate filename is ambiguous instead of + # silently selecting one of the two unrelated objects. + assert resolve_canvas_link('notes.md', project)['status'] == 'ambiguous' + + +def test_directory_qualified_link_focuses_its_directory_and_never_a_basename_duplicate(): + turn, other = uuid4(), uuid4() + first_asset = asset(uuid4(), 'folder1/notes.md', run=other) + second_asset = asset(uuid4(), 'folder2/notes.md', run=turn) + first = node(uuid4(), title='Folder 1 note', asset_id=first_asset.id) + second = node(uuid4(), title='Folder 2 note', asset_id=second_asset.id) + project = snapshot(assets=[first_asset, second_asset], nodes=[first, second]) + + # The link names its directory, so it resolves folder1's asset even though + # folder2's same-basename asset belongs to the current turn. + resolved = resolve_canvas_link('folder1/notes.md', project, turn) + assert resolved['nodeId'] == str(first.id) + + # A directory with no such asset must not silently fall back to a + # same-basename file elsewhere. + assert resolve_canvas_link('folder3/notes.md', project, turn)['status'] == 'missing' + + +def test_unique_asset_resolves_by_name_regardless_of_turn_provenance(): + turn = uuid4() + # A user-imported asset has no agentRunId, yet it is the only notes.md. + imported = asset(uuid4(), 'notes.md') + imported_item = node(uuid4(), title='Imported note', asset_id=imported.id) + project = snapshot(assets=[imported], nodes=[imported_item]) + assert resolve_canvas_link('notes.md', project, turn)['nodeId'] == str(imported_item.id) + + # A previous turn's output is equally unique and must not be reported as + # "not in this project" only because its run id differs. + earlier = asset(uuid4(), 'notes.md', run=uuid4()) + earlier_item = node(uuid4(), title='Earlier note', asset_id=earlier.id) + project = snapshot(assets=[earlier], nodes=[earlier_item]) + assert resolve_canvas_link('notes.md', project, turn)['nodeId'] == str(earlier_item.id) + + +def test_missing_malformed_and_external_targets_are_typed_and_never_raise(): + project = snapshot() + for target in ('', ' ', 'unknown.mp4', 'javascript:alert(1)', + 'data:text/plain,hello', 'file:///etc/passwd', + '#section', '?query', '../../etc/passwd', 'canvas:not-a-uuid', + 'project:not-a-uuid/canvas:also-bad', 'project:missing'): + result = resolve_canvas_link(target, project) + assert result['status'] in {'missing', 'external'} + assert set(result) >= {'target', 'status'} + assert resolve_canvas_link('https://example.test/a', project) == { + 'target': 'https://example.test/a', 'status': 'external'} + assert link_targets('plain text without links') == [] + + +def test_hidden_materialize_ambiguous_and_explicit_nodes(): + hidden = node(uuid4(), title='Old wording', visible=False) + unplaced = asset(uuid4(), 'draft.md') + shared = asset(uuid4(), 'shared.mp4', kind='video') + first = node(uuid4(), kind='video', title='Take 1', asset_id=shared.id) + second = node(uuid4(), kind='video', title='Take 2', asset_id=shared.id) + project = snapshot(assets=[unplaced, shared], nodes=[hidden, first, second]) + + revealed = resolve_canvas_link(f'canvas:{hidden.id}', project) + assert revealed['status'] == 'hidden' and revealed['action'] == 'reveal' + + materialize = resolve_canvas_link('draft.md', project) + assert materialize['status'] == 'materialize' + assert materialize['assetId'] == str(unplaced.id) + assert materialize['action'] == 'materialize' + + ambiguous = resolve_canvas_link('shared.mp4', project) + assert ambiguous['status'] == 'ambiguous' + assert {candidate['nodeId'] for candidate in ambiguous['candidates'] if 'nodeId' in candidate} == { + str(first.id), str(second.id)} + + explicit = resolve_canvas_link(f'node:{first.id}', project) + assert explicit['status'] == 'focused' and explicit['nodeId'] == str(first.id) + assert resolve_canvas_link(f'canvas:{uuid4()}', project)['status'] == 'missing' + + +def test_cross_project_targets_report_the_destination_project(): + foreign_project = uuid4() + item = node(uuid4()) + project = snapshot(nodes=[item]) + target = format_canvas_link(str(item.id), str(foreign_project)) + resolved = resolve_canvas_link(target, project) + assert resolved['status'] == 'cross-project' + assert resolved['projectId'] == str(foreign_project) + assert resolved['nodeId'] == str(item.id) + # A fully qualified link to this project resolves normally after reload. + own = format_canvas_link(str(item.id), str(project.project.id)) + assert resolve_canvas_link(own, project)['status'] == 'focused' + + +@pytest.mark.parametrize('kind', ['text', 'image', 'video', 'audio', 'subject', + 'timeline', 'group', 'stack', 'tool', 'external']) +def test_explicit_identity_resolves_every_canvas_kind(kind): + item = node(uuid4(), kind=kind, title='Item') + project = snapshot(nodes=[item]) + resolved = resolve_canvas_link(f'canvas:{item.id}', project) + assert resolved == {'target': f'canvas:{item.id}', 'status': 'focused', + 'nodeId': str(item.id), 'kind': kind} + + +def test_title_matching_covers_non_media_nodes_and_keeps_duplicates_ambiguous(): + group = node(uuid4(), kind='group', title='Scene 1') + text = node(uuid4(), kind='text', title='Finished note') + duplicate_a = node(uuid4(), kind='text', title='Same title') + duplicate_b = node(uuid4(), kind='text', title='Same title') + project = snapshot(nodes=[group, text, duplicate_a, duplicate_b]) + + assert resolve_canvas_link('@Scene 1', project)['nodeId'] == str(group.id) + assert resolve_canvas_link('@Finished note', project)['nodeId'] == str(text.id) + ambiguous = resolve_canvas_link('@Same title', project) + assert ambiguous['status'] == 'ambiguous' + assert len(ambiguous['candidates']) == 2 + + +def test_code_examples_are_not_navigable_links(): + fence = '```markdown\n[Example](fenced.md)\n```' + inline = 'Use `[Example](inline.md)` literally.' + text = f'{fence}\n{inline}\nReal [Finished note](notes.md)' + stored = asset(uuid4(), 'notes.md') + item = node(uuid4(), title='Finished note', asset_id=stored.id) + project = snapshot(assets=[stored], nodes=[item]) + assert link_targets(text) == ['notes.md'] + links = canvas_links_for_text(text, project) + assert [link['target'] for link in links] == ['notes.md'] + assert links[0]['nodeId'] == str(item.id) + + +def test_markdown_parser_ignores_nested_image_labels_and_malformed_destinations(): + text = ('````markdown\n```\n[example](notes.md)\n````\n' + '![alt [nested](notes.md)](image.png)\n' + '[bad](notes.md unquoted junk) [open](') + assert link_targets(text) == [] + + +def test_markdown_escaped_filename_resolves_to_real_asset(): + stored = asset(uuid4(), 'notes.md') + item = node(uuid4(), title='Finished note', asset_id=stored.id) + resolved, = canvas_links_for_text(r'[note](notes\.md)', snapshot(assets=[stored], nodes=[item])) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(item.id) + + +def test_child_under_hidden_group_is_hidden(): + group = node(uuid4(), kind='group', visible=False) + child = node(uuid4(), title='Child', parent_id=group.id) + resolved = resolve_canvas_link(f'canvas:{child.id}', snapshot(nodes=[group, child])) + assert resolved['status'] == 'hidden' + assert resolved['action'] == 'reveal' + + +def test_adopted_generation_asset_resolves_through_current_result(): + asset_id, spec_id, result_id, item_id = (uuid4() for _ in range(4)) + stored = asset(asset_id, 'adopted.png', kind='image') + item = node(item_id, kind='image', parent_id=None) + item.binding = NS(role='generation', objectRef=NS(id=spec_id)) + result = NS(id=result_id, asset=NS(assetId=asset_id)) + spec = NS(id=spec_id, currentResultId=result_id) + project = snapshot(assets=[stored], nodes=[item]) + project.results = (result,) + project.generationSpecs = (spec,) + resolved = resolve_canvas_link('adopted.png', project) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(item_id) + + +def test_stable_identity_does_not_depend_on_position_or_title(): + item = node(uuid4(), title='Before') + original = snapshot(nodes=[item]) + first = resolve_canvas_link('@Before', original) + moved = node(item.id, title='After', parent_id=uuid4()) + moved.position = NS(x=1234, y=-500) + after = snapshot(nodes=[moved]) + second = resolve_canvas_link(f'canvas:{item.id}', after) + assert first['nodeId'] == second['nodeId'] == str(item.id) + assert resolve_canvas_link('@Before', after)['status'] == 'missing' + assert resolve_canvas_link('@After', after)['nodeId'] == str(item.id) + + +def validated_example(): + from test_creation_contracts import project_example + return project_example().model_dump(mode='json') + + +def test_unique_other_turn_filename_resolves_and_explicit_history_identity_works(): + from opendirector.domain.creation import ProjectSnapshot + data = validated_example() + data['assets'][0]['name'] = 'notes.md' + data['assets'][0]['provenance']['agentRunId'] = str(uuid4()) + data['canvasItems'][0]['title'] = 'notes.md' + project = ProjectSnapshot.model_validate(data) + # A unique asset keeps resolving by filename even when it belongs to an + # earlier turn; the turn preference only disambiguates duplicate names. + assert resolve_canvas_link('notes.md', project, uuid4())['nodeId'] == str(project.canvasItems[0].id) + assert resolve_canvas_link(f'canvas:{project.canvasItems[0].id}', project, uuid4())['nodeId'] == str(project.canvasItems[0].id) + + +def test_material_override_links_only_to_the_displayed_asset(): + from opendirector.application.creation_canvas import current_asset + from opendirector.domain.creation import ProjectSnapshot + from test_creation_contracts import asset_ref + data = validated_example() + data['assets'][-1]['name'] = 'original.png' + data['assets'][1]['name'] = 'replacement.png' + data['assets'][1]['provenance'] = dict(source='imported', importedFrom='generated') + initial = ProjectSnapshot.model_validate(data) + data['canvasItems'][-3]['binding']['materialOverride'] = asset_ref(initial.assets[1]).model_dump(mode='json') + # Avoid an unrelated material representation of replacement.png. + data['canvasItems'][1]['binding'] = dict(role='empty') + project = ProjectSnapshot.model_validate(data) + item = project.canvasItems[-3] + assert current_asset(project, item).name == 'replacement.png' + assert resolve_canvas_link('original.png', project)['status'] == 'materialize' + assert resolve_canvas_link('replacement.png', project)['nodeId'] == str(item.id) + + +@pytest.mark.parametrize(('target', 'name'), [ + ('report%2520final.md', 'report%20final.md'), + ('folder%252Freport.md', 'folder%2Freport.md'), + ('folder%2Freport.md', 'folder/report.md'), + ('report%23final.md', 'report#final.md'), + ('report%3Ffinal.md', 'report?final.md'), + ('100%25.md', '100%.md'), +]) +def test_url_path_is_decoded_once_after_structure_parsing(target, name): + stored = asset(uuid4(), name) + wrong = asset(uuid4(), 'report final.md') + item = node(uuid4(), asset_id=stored.id) + other = node(uuid4(), asset_id=wrong.id) + links = canvas_links_for_text(f'[file]({target})', snapshot(assets=[stored, wrong], nodes=[item, other])) + assert links[0]['nodeId'] == str(item.id) + + +def test_plain_mentions_resolve_unique_missing_and_duplicate_titles(): + from opendirector.domain.creation import ProjectSnapshot + data = validated_example() + data['canvasItems'][0]['title'] = 'Finished note' + data['canvasItems'][1]['title'] = 'Same title' + data['canvasItems'][2]['title'] = 'Same title' + project = ProjectSnapshot.model_validate(data) + links = canvas_links_for_text('@Finished note\n@No such item\n@Same title', project, uuid4()) + assert [link['status'] for link in links] == ['focused', 'missing', 'ambiguous'] + assert links[0]['nodeId'] == str(project.canvasItems[0].id) + assert len(links[2]['candidates']) == 2 + + +def test_mentions_in_code_images_and_link_labels_are_ignored(): + item = node(uuid4(), title='Finished note') + text = ('```\n@Finished note\n```\n`@Finished note`\n' + '![\u0040Finished note](image.png)\n[label @Finished note](https://example.test)') + assert canvas_links_for_text(text, snapshot(nodes=[item])) == [] + + +@pytest.mark.parametrize('suffix', ['.', ':', '"', "'", '`', ')']) +def test_mention_trailing_punctuation_does_not_swallow_the_title(suffix): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(f'@Finished note{suffix}', snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['nodeId'] == str(item.id) + + +@pytest.mark.parametrize('text', [ + '@Finished note. Next step', + '@Finished note: Next step', + '@Finished note.', +]) +def test_mention_sentence_punctuation_ends_the_mention_before_running_prose(text): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['target'] == '@Finished note' + assert links[0]['nodeId'] == str(item.id) + + +def test_empty_link_destinations_are_not_persisted_as_missing_noise(): + item = node(uuid4(), title='Finished note') + project = snapshot(nodes=[item]) + links = canvas_links_for_text('[x]() [y]( ) [z](notes.md)', project) + assert [link['target'] for link in links] == ['notes.md'] + assert link_targets('[x]() [y]( )') == [] + + +def test_mentions_keep_internal_punctuation_and_strip_only_trailing(): + chapter = node(uuid4(), title='Chapter 2.1') + finished = node(uuid4(), title='Finished note') + project = snapshot(nodes=[chapter, finished]) + + links = canvas_links_for_text('See @Chapter 2.1 here', project) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['nodeId'] == str(chapter.id) + assert links[0]['target'] == '@Chapter 2.1' + + links = canvas_links_for_text('@Finished note.', project) + assert links[0]['target'] == '@Finished note' + assert links[0]['nodeId'] == str(finished.id) + + +def test_nonexistent_multiword_title_never_falls_back_to_a_shorter_node(): + shorter = node(uuid4(), title='Scene') + project = snapshot(nodes=[shorter]) + # "99 does not exist" is not running prose that may be stripped off a + # title; resolving it to the unrelated node "Scene" references the wrong + # object. Only an ordinary trailing word may be dropped. + for text in ('@Scene 99', '@Scene 99 does not exist'): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert link['target'] == text + + +@pytest.mark.parametrize(('text', 'expected'), [ + ('看这个@标题', '@标题'), + ('@标题:请看', '@标题'), + ('@标题:', '@标题'), +]) +def test_cjk_mention_boundaries_and_fullwidth_colon(text, expected): + item = node(uuid4(), title='标题') + links = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert [link['status'] for link in links] == ['focused'] + assert links[0]['target'] == expected + assert links[0]['nodeId'] == str(item.id) + + +def test_ascii_word_glued_to_a_cjk_mention_stays_prose(): + item = node(uuid4(), title='标题') + assert canvas_links_for_text('abc@标题', snapshot(nodes=[item]), uuid4()) == [] + + +def test_exact_title_wins_before_sentence_punctuation_is_trimmed(): + # "@Dr. Smith" must resolve the full title even when the shorter "Dr" + # exists; trimming first would persist the wrong node id. + short = node(uuid4(), title='Dr') + full = node(uuid4(), title='Dr. Smith') + note = node(uuid4(), title='Note') + intro = node(uuid4(), title='Note: Intro') + project = snapshot(nodes=[short, full, note, intro]) + for text, expected, target in ( + ('@Dr. Smith', full, '@Dr. Smith'), + ('@Dr. Smith.', full, '@Dr. Smith'), + ('@Dr. Smith says hello', full, '@Dr. Smith'), + ('@Note: Intro', intro, '@Note: Intro'), + ('@Note: Intro details', intro, '@Note: Intro')): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'focused' + assert link['target'] == target + assert link['nodeId'] == str(expected.id) + + +def test_glued_cjk_prose_after_a_full_title_resolves_the_full_title(): + # The CJK prefix fallback must prefer the longer known title even though + # the shorter one is also a prefix. + short = node(uuid4(), title='Dr') + full = node(uuid4(), title='Dr. Smith') + link, = canvas_links_for_text('(@Dr. Smith后续', + snapshot(nodes=[short, full]), uuid4()) + assert link['target'] == '@Dr. Smith' + assert link['nodeId'] == str(full.id) + + +def test_a_longer_known_title_with_an_unusable_boundary_shadows_shorter_nodes(): + # The longer title exists, but the mention continues with a number that + # may belong to a longer title. Resolving "Dr" / "标题" would reference a + # different object, so the mention stays missing. + nodes = [node(uuid4(), title='Dr'), node(uuid4(), title='Dr. Smith'), + node(uuid4(), title='标题'), node(uuid4(), title='标题生成')] + project = snapshot(nodes=nodes) + for text in ('@Dr. Smith 99', '@标题生成 99'): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert 'nodeId' not in link + + +def test_missing_mentions_keep_the_target_off_following_prose(): + # A missing mention must not wrap the rest of the sentence into the + # clickable reference; the persisted target stops at the mention boundary. + project = snapshot() + for text, target in ( + ('@No such item. Next step', '@No such item'), + ('@No such item.', '@No such item'), + ('@Unknown 标题、继续', '@Unknown 标题')): + link, = canvas_links_for_text(text, project, uuid4()) + assert link['status'] == 'missing' + assert link['target'] == target + assert 'nodeId' not in link + + +@pytest.mark.parametrize(('text', 'title', 'expected_target'), [ + ('参见@标题、然后继续', '标题', '@标题'), + ('“@标题”效果不错', '标题', '@标题'), + ('(@标题)请继续', '标题', '@标题'), + ('@标题…继续', '标题', '@标题'), + ('@标题—继续', '标题', '@标题'), + ('@标题」继续', '标题', '@标题'), + ('@标题】继续', '标题', '@标题'), + ('@Finished note)后续', 'Finished note', '@Finished note'), +]) +def test_fullwidth_punctuation_ends_a_mention_mid_sentence(text, title, expected_target): + item = node(uuid4(), title=title) + link, = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'focused' + assert link['target'] == expected_target + assert link['nodeId'] == str(item.id) + + +@pytest.mark.parametrize('text', ['用@标题生成视频', '把@标题改一下']) +def test_cjk_prose_after_a_mention_resolves_a_known_title_prefix(text): + # Chinese prose has no word separator, so the mention swallows the rest + # of the phrase; the longest known title prefix still resolves it. + item = node(uuid4(), title='标题') + link, = canvas_links_for_text(text, snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'focused' + assert link['target'] == '@标题' + assert link['nodeId'] == str(item.id) + + +def test_cjk_prefix_fallback_uses_the_longest_known_title(): + short = node(uuid4(), title='标题') + longer = node(uuid4(), title='标题生成') + link, = canvas_links_for_text('用@标题生成视频', snapshot(nodes=[short, longer]), uuid4()) + assert link['target'] == '@标题生成' + assert link['nodeId'] == str(longer.id) + + +def test_ascii_continuation_does_not_use_the_cjk_prefix_fallback(): + # "Scene" is a prefix of "Scene2", but the continuation is not CJK prose; + # silently referencing "Scene" would be the wrong object. + item = node(uuid4(), title='Scene') + link, = canvas_links_for_text('@Scene2', snapshot(nodes=[item]), uuid4()) + assert link['status'] == 'missing' + assert 'nodeId' not in link + + +def test_bare_title_name_is_missing_and_only_mentions_resolve_titles(): + item = node(uuid4(), kind='group', title='Scene 1') + project = snapshot(nodes=[item]) + # A bare filename-like destination matches assets, never item titles; the + # production path (agent.py) always supplies a turn id, so this fallback + # used to be dead code. + assert resolve_canvas_link('Scene 1', project, uuid4())['status'] == 'missing' + assert resolve_canvas_link('@Scene 1', project, uuid4())['nodeId'] == str(item.id) + + +def test_reference_style_markdown_links_are_supported(): + item = node(uuid4(), title='Finished note') + links = canvas_links_for_text(f'[Finished note][ref]\n\n[ref]: canvas:{item.id}', snapshot(nodes=[item])) + assert links[0]['nodeId'] == str(item.id) + + +def test_message_enrichment_only_attaches_project_references(): + item = node(uuid4(), title='Finished note') + project = snapshot(nodes=[item]) + message = {'id': 'm', 'role': 'assistant', + 'text': '[local](notes.md) [site](https://example.test) @Finished note'} + enriched = with_canvas_links(message, project) + assert [link['status'] for link in enriched['canvasLinks']] == ['missing', 'focused'] + assert 'canvasLinks' not in message + user = with_canvas_links({'id': 'u', 'role': 'user', 'text': '[x](notes.md)'}, project) + assert 'canvasLinks' not in user + + +def test_agent_generation_submit_stamps_provenance_and_relative_link_hits_it(tmp_path): + import asyncio + from opendirector.application.creation_generation import CreationGeneration + from opendirector.domain.creation.commands import parse_command + from opendirector.infrastructure.creation_projects import CreationProjects + from opendirector.infrastructure.model_configs import ModelConfigs + from opendirector.agent.tools import AgentTools + + store = CreationProjects(LocalProjects(tmp_path)) + pid, session = uuid4(), str(uuid4()) + + def send(kind, *, versions=(), **payload): + return store.execute(parse_command({ + 'type': kind, 'projectId': pid, 'commandId': uuid4(), + 'expectedVersions': list(versions), 'payload': payload}), session=session) + + def version(kind, obj): + return {'objectType': kind, 'id': str(obj.id), 'revision': obj.revision} + + assert send('project.create', name='Links').status == 'applied' + store.open(pid, session) + + spec_id, item_id = uuid4(), uuid4() + assert send('creation.batch', operations=[ + {'type': 'generationSpec.create', 'payload': {'spec': { + 'id': str(spec_id), 'projectId': str(pid), 'kind': 'text', 'purpose': 'generation'}}}, + {'type': 'canvas.show', 'payload': {'item': { + 'id': str(item_id), 'projectId': str(pid), 'kind': 'text', 'title': 'Result', + 'binding': {'role': 'generation', 'objectRef': {'id': str(spec_id)}}}}}]).status == 'applied' + + configs = ModelConfigs(store.local.repository) + config = configs.save('agent', 'http://127.0.0.1:12345/v1', 'test-agent', '', + connection_type='self_hosted', protocol='openai', auth_mode='none') + spec = next(s for s in store.snapshot(pid).generationSpecs if s.id == spec_id) + assert send('generationSpec.update', versions=[version('generation_spec', spec)], + spec=spec.model_copy(update={'prompt': 'result.md', + 'modelConfigId': UUID(config['id'])}).model_dump(mode='json')).status == 'applied' + spec = store.snapshot(pid).generationSpecs[-1] + target = next(item for item in store.snapshot(pid).canvasItems if item.id == item_id) + + # A same-named user import: only the stamped generation output carries the turn. + assert send('asset.writeText', name='result.md', text='v1').status == 'applied' + + class Provider: + def __init__(self): + self.finish = asyncio.Event() + + def validate(self, kind, config): + pass + + async def generate(self, config, task, submitted): + await self.finish.wait() + return 'md', b'Actual generated text' + + provider = Provider() + generation = CreationGeneration(store, configs, provider) + business = AgentTools(store, generation, configs) + turn_id = uuid4() + provider.finish.set() + + async def submit_and_wait(): + receipt = await business.execute(pid, turn_id, session, 'submit-call', 'execute_command', { + 'type': 'generation.submit', + 'expectedVersions': [version('canvas_item', target), version('generation_spec', spec)], + 'payload': {'specId': str(spec.id), 'expectedSpecRevision': spec.revision, + 'originItemId': str(target.id)}}, [], None) + assert receipt['status'] == 'accepted', receipt + await asyncio.gather(*(job.future for job in list(generation.jobs.values()))) + + try: + asyncio.run(submit_and_wait()) + finally: + asyncio.run(generation.close()) + + snapshot = store.snapshot(pid) + generated = next(asset for asset in snapshot.assets if asset.provenance.source == 'generated') + assert str(generated.provenance.agentRunId) == str(turn_id) + resolved = resolve_canvas_link('result.md', snapshot, turn_id) + assert resolved['status'] == 'focused' + assert resolved['nodeId'] == str(target.id) + + +def frames(response): + return [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')] + + +async def empty_skills(*args): + return [] + + +def test_finished_turn_persists_canvas_links_across_move_rename_and_reopen(tmp_path, monkeypatch): + async def stream(self, run): + yield {'type': 'delta', 'text': f'Done [Finished note](canvas:{item_id})'} + + monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + monkeypatch.setattr(agent, 'skills', empty_skills) + from opendirector.infrastructure.model_configs import ModelConfigs + monkeypatch.setattr(ModelConfigs, 'resolve', lambda *args: {'id': str(uuid4()), 'modelId': 'test-model'}) + + project_id = str(uuid4()) + assistant_path = f'/api/creation/projects/{project_id}/assistant' + root = f'/api/creation/projects/{project_id}' + headers = {'X-OpenDirector-Request': '1', 'X-OpenDirector-Session': str(uuid4())} + + with TestClient(create_app(LocalProjects(tmp_path)), headers=headers) as client: + created = client.post(root + '/commands', json={ + 'type': 'project.create', 'projectId': project_id, + 'commandId': str(uuid4()), 'payload': {'name': 'Links'}}) + assert created.status_code == 200, created.text + assert client.post(root + '/open').status_code == 200 + + item_id, spec_id = str(uuid4()), str(uuid4()) + batch = client.post(root + '/commands', json={ + 'type': 'creation.batch', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'operations': [ + {'type': 'generationSpec.create', 'payload': {'spec': { + 'id': spec_id, 'projectId': project_id, 'kind': 'text', 'purpose': 'draft'}}}, + {'type': 'canvas.show', 'payload': {'item': { + 'id': item_id, 'projectId': project_id, 'kind': 'text', + 'title': 'Finished note', + 'binding': {'role': 'empty', 'draftSpec': {'id': spec_id}}}}}, + ]}}) + assert batch.status_code == 200 and batch.json()['status'] == 'applied', batch.text + written = client.post(root + '/commands', json={ + 'type': 'asset.writeText', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'name': 'notes.md', 'text': 'v1'}}) + assert written.status_code == 200 and written.json()['status'] == 'applied', written.text + saved_asset = client.get(root).json()['assets'][0] + materialized = client.post(root + '/commands', json={ + 'type': 'canvas.materialize', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'itemId': item_id, 'asset': { + 'assetId': saved_asset['id'], 'revision': saved_asset['revision'], + 'fingerprint': saved_asset['fingerprint'], 'kind': saved_asset['kind']}}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 1}]}) + assert materialized.status_code == 200, materialized.text + assert materialized.json()['status'] == 'applied', materialized.text + + turn_id = uuid4() + response = client.post(assistant_path + '/turns', json={ + 'id': str(turn_id), 'runtime': 'default', 'text': 'Create the note'}) + assert response.status_code == 200, response.text + events = frames(response) + assert events[-1]['type'] == 'end' and events[-1]['error'] is None + saved = events[-1]['conversation']['messages'][-1] + assert saved['id'] == str(uuid5(turn_id, 'assistant')) + schema = json.loads((Path(__file__).resolve().parents[1] / 'src/contracts/creation.schema.json').read_text()) + assert set(saved['canvasLinks'][0]) >= set(schema['output']['$defs']['ChatCanvasLink']['required']) + from opendirector.domain.creation.chat import ChatCanvasLink + assert saved['canvasLinks'] == [ChatCanvasLink(target=f'canvas:{item_id}', status='focused', + nodeId=item_id, kind='text').model_dump(mode='json')] + + disk = next(tmp_path.glob('**/agent-runs/conversation.json')) + persisted = json.loads(disk.read_text(encoding='utf-8'))['messages'][-1] + assert persisted['canvasLinks'] == saved['canvasLinks'] + + moved = client.post(root + '/commands', json={ + 'type': 'canvas.move', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'items': [{'itemId': item_id, 'position': {'x': 640, 'y': 480}}]}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 2}]}) + assert moved.json()['status'] == 'applied', moved.text + renamed = client.post(root + '/commands', json={ + 'type': 'canvas.update', 'commandId': str(uuid4()), 'projectId': project_id, + 'payload': {'itemId': item_id, 'title': 'Renamed note'}, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 3}]}) + assert renamed.json()['status'] == 'applied', renamed.text + group_id = str(uuid4()) + grouped = client.post(root + '/commands', json={ + 'type': 'creation.batch', 'commandId': str(uuid4()), 'projectId': project_id, + 'expectedVersions': [{'objectType': 'canvas_item', 'id': item_id, 'revision': 4}], + 'payload': {'operations': [ + {'type': 'canvas.show', 'payload': {'item': { + 'id': group_id, 'projectId': project_id, 'kind': 'group'}}}, + {'type': 'canvas.update', 'payload': {'itemId': item_id, 'parentId': group_id}}, + ]}}) + assert grouped.status_code == 200 and grouped.json()['status'] == 'applied', grouped.text + reread = client.get(assistant_path).json() + assert reread['messages'][-1]['canvasLinks'] == saved['canvasLinks'] + + # A fresh application instance reads the same persisted identity. + with TestClient(create_app(LocalProjects(tmp_path)), headers=headers) as reopened: + restored = reopened.get(assistant_path).json() + assert restored['messages'][-1]['canvasLinks'] == saved['canvasLinks'] + opened = reopened.post(root + '/open-canvas') + assert opened.status_code == 200, opened.text + restored_item = next(item for item in opened.json()['snapshot']['canvasItems'] + if item['id'] == item_id) + assert restored_item['title'] == 'Renamed note' + assert restored_item['position'] == {'x': 640, 'y': 480} + assert restored_item['parentId'] == group_id