diff --git a/backend/app/gateway/share_fork.py b/backend/app/gateway/share_fork.py index 9d57cd218c..0ee5148c01 100644 --- a/backend/app/gateway/share_fork.py +++ b/backend/app/gateway/share_fork.py @@ -6,6 +6,7 @@ import os import uuid from contextlib import asynccontextmanager +from deerflow_extension_api import AgentRunError from fastapi import HTTPException, Request from langchain_core.messages import AIMessage, HumanMessage from langgraph.types import Overwrite @@ -38,6 +39,11 @@ logger = logging.getLogger(__name__) SHARE_IMPORT_COMPLETE_KEY = "share_import_complete" SHARE_IMPORT_HISTORY_MARKED_KEY = "share_import_history_marked" +# The descriptor-confined writer below is POSIX-only. Capture already refuses +# the same artifacts off POSIX, so import answers with that contract instead of +# surfacing a 500 from a partial write. +_ARTIFACT_IMPORT_SUPPORTED = os.name == "posix" + def _write_fork_files(owner: str, thread_id: str, artifacts: list[dict]) -> None: """A fresh UUID directory, descriptor-confined descendants and exclusive files.""" @@ -88,6 +94,8 @@ async def fork_share(request: Request, repo: ThreadShareRepository, token: str, share = await repo.get_public(token) if share is None: raise HTTPException(404, "Share unavailable") + if share["artifacts"] and not _ARTIFACT_IMPORT_SUPPORTED: + raise AgentRunError(422, "Importing a share with artifacts requires POSIX") owner = get_effective_user_id() thread_id = str(uuid.uuid5(uuid.NAMESPACE_URL, f"deerflow-share:{owner}:{share['share_id']}:{request_id}")) threads = get_thread_store(request) diff --git a/backend/tests/test_shares_router.py b/backend/tests/test_shares_router.py index b5f28fc9f0..db521c183d 100644 --- a/backend/tests/test_shares_router.py +++ b/backend/tests/test_shares_router.py @@ -612,6 +612,24 @@ async def test_imported_nested_files_have_sandbox_write_permissions(share_app): assert stat.S_IMODE(directory.stat().st_mode) == 0o777 +async def test_artifact_import_off_posix_is_refused_before_any_state(share_app, monkeypatch): + ctx = share_app + share = await make_share(ctx) + snapshot = await ctx.client.get(f"/api/shares/{share['token']}") + assert snapshot.json()["artifacts"] + url = f"/api/shares/{share['token']}/fork" + request_id = str(uuid4()) + monkeypatch.setattr(share_fork, "_ARTIFACT_IMPORT_SUPPORTED", False) + refused = await ctx.client.post(url, json={"request_id": request_id}, headers=headers("bob")) + assert refused.status_code == 422, refused.text + assert refused.json()["detail"] == "Importing a share with artifacts requires POSIX" + # The refusal precedes any workspace state, so the same request can succeed + # once the descriptor-confined writer is available again. + monkeypatch.setattr(share_fork, "_ARTIFACT_IMPORT_SUPPORTED", True) + retried = await ctx.client.post(url, json={"request_id": request_id}, headers=headers("bob")) + assert retried.status_code == 200, retried.text + + async def test_unsafe_artifact_fails_without_publishing_partial_share(share_app): ctx = share_app ctx.output.unlink() diff --git a/docs/guide/sharing.md b/docs/guide/sharing.md index 29ab57f74b..d3e7fe0467 100644 --- a/docs/guide/sharing.md +++ b/docs/guide/sharing.md @@ -68,7 +68,7 @@ All paths below are relative to `/api`. | `DELETE /threads/{id}/shares/{share_id}` | Owner with `threads:write`; revokes access and removes stored snapshot bytes. | | `GET /shares/{token}` | Anonymous capability read; returns visible messages, artifact manifest, checkpoint and sequence. | | `GET`, `HEAD /shares/{token}/artifacts/{path}` | Reads only a copied manifest entry. Supports byte ranges and `?download=true`. | -| `POST /shares/{token}/fork` | Signed-in visitor with `threads:write`; body contains a UUID `request_id`. Returns the new `thread_id`. Retrying the same request reuses a completed import. | +| `POST /shares/{token}/fork` | Signed-in visitor with `threads:write`; body contains a UUID `request_id`. Returns the new `thread_id`. Retrying the same request reuses a completed import. Importing a share that carries artifacts requires a POSIX host; other hosts answer `422` before creating any state. | Creation captures the latest idle root checkpoint and its persisted visible history under exclusive thread admission. `checkpoint_id`, when supplied, is