Zeren Wang a58ab484a6
feat(projects): Projects MVP Phase 2 — instructions, document shelf, promotion, trash (#5443)
* feat(projects): Projects MVP Phase 2 — instructions, document shelf, promotion, trash

Implements docs/superpowers/specs/2026-09-12-projects-mvp-phase2-design.md
(issue #5160, tracker #5129) in the slice order of the spec's §16.

Slices:
- A: ProjectsConfig + write-time 422 UTF-8 byte cap; PROJECT_CONTEXT_KEY
  admission pinning (both server-owned sets + worker hoist); latest-only
  request-scoped <project> block via DynamicContextMiddleware
  wrap_model_call/awrap_model_call (idempotent reassembly, reserved ID
  prefix + marker + provenance, never persisted); journal audit
  fingerprints; Instructions tab.
- B: ProjectDocumentRow + migration 0023; ProjectDocumentRepository with
  locked check-and-set; hash-qualified immutable shelf storage with
  Paths helpers; upload/list/content/delete-to-trash routes; project
  delete trashes the shelf in-transaction; request-scoped bounded
  <documents> index with honest count/shown + actionable overflow note;
  list_project_documents/read_project_document tools registered only on
  pinned runs; PAT allowlist + drift guards; blocking-IO anchors.
- C: shared thread-upload ingestion service (uploads router refactored to
  parity); POST from-thread with provenance; attach-to-thread with
  lock-staged copy (archived source allowed); read-only thread-files
  view with per-group truncation reporting.
- D: restore (restored/merged/not_found/no_target/content_missing; no
  file moves), purge (continuous row lock across unlink/delete/commit,
  retryable on FS errors), retention sweep (lazy + startup, 24h orphan
  guard, row-side reconciliation never deletes).
- E: Documents tab (shelf + conversation-files browser, provenance,
  archived banner, content-missing rows), /workspace/trash route,
  sidebar entry, composer attach handoff, i18n (en-US/zh-CN), e2e mocks
  + specs.

Review hardening folded in (10 rounds, all with tests):
- force active shelf content (HTML/XML family) to download; nosniff on
  artifact + content responses; unified unsandboxed-iframe PDF preview
  (fixes the pre-existing Chromium sandbox blank in the artifact viewer)
- scope document trash to the URL project under the document lock
- atomic no-overwrite filename reservation for ALL ingestion (seeded
  claims + os.link commit with suffix retry; same-name re-upload now
  unique-names instead of replacing); hidden staging only, no visible
  placeholders; lease cleanup on setup failure
- serialize conversion under the document lock with post-lock active
  revalidation; drain locked filesystem work on cancellation; preserve
  bytes when an insert's commit state is uncertain (including trashed
  rows)
- original-integrity checks before serving text or cached conversions;
  content_missing surfaced in list responses (UI reads the flag, no
  409-probe); downloads always serve original bytes
- bounded streaming document reads with cached char counts; shelf limits
  declared in middleware release identity
- thread-root confinement for from-thread sources; config fallback
  rejects fractional/infinite values; composer counts staged
  attachments; pending attachments persist until submission or removal;
  in-flight instruction/rename edits survive save refetches; shelf and
  trash pagination; conversation-file and thread-files pages stay
  subscribed to refetches

Docs: README/README_zh, backend API.md/ARCHITECTURE.md, AGENTS.md
contracts, config.example.yaml projects block.

Review follow-ups (head b4807477 → this revision):
- The trash retention sweep is split so repeated lazy triggers stay
  bounded: the indexed expiry purge still runs on every trigger
  (GET /api/trash/documents, POST /api/trash/purge) while the
  O(all rows + all files) reconciliation is throttled to one run per
  user per 15 minutes (process-local, per-user window). The startup
  sweep now runs as a background task instead of blocking gateway
  readiness, and shutdown awaits it (bounded).
- The export scrub (stripInternalMarkers) is fence- and indentation-aware
  like the render path, so a pasted, fenced <project>/<documents> snippet
  survives markdown export while real injected blocks (never fenced) are
  still removed. Fence regexes moved to a dependency-free leaf module to
  avoid the messages↔streamdown import cycle.
- The artifact viewer's PDF iframe no longer carries an added title
  attribute (the upstream e2e contract locates it via :not([title])), and
  the upstream artifact-preview spec now pins the new contract: PDFs
  render unsandboxed, images keep sandbox="".

* fix(projects): round-2 review — cancel an overrun trash sweep, restore the PDF frame title

- Shutdown cancelled only the shield around the background startup sweep,
  so an all-users reconciliation that outlived the 5s budget kept walking
  rows and files while the document repo and DB engine were disposed
  underneath it. The wait now lives in `_shutdown_startup_trash_sweep`,
  which cancels the task and drains it before worker exit: the shield
  keeps the wait bounded, the cancel makes it final (CancelledError lands
  at the sweep's next await, and `_run_startup_trash_sweep` only catches
  `Exception`, so nothing swallows it).
- The browser-preview iframe lost `title={getFileName(filepath)}` in the
  previous fix round, leaving the PDF frame without an accessible name
  while its siblings keep theirs. Restore it (WCAG frame titles), assert
  it in the DOM test, and anchor the e2e on `iframe[title="report.pdf"]`
  instead of `iframe:not([title])`.

* fix(projects): round-3 review — report the sweep's late finish, not a phantom cancel

`Task.cancel()` returns False when the sweep already finished inside the
window between the deadline firing and the cancel, so the shutdown log
claimed a cancellation that never happened. Branch on that outcome: the
warning stays for a real cancel, a late finish is logged at info, and both
paths still reap the task before worker exit.

* fix(projects): round-4 review — make Empty trash delete what it confirms

`POST /api/trash/purge` only ran the retention sweep, and the sweep's
candidate selection is age-gated, so a freshly trashed document survived
"Empty trash" even though the confirmation promises that every listed
document is permanently deleted. With one trashed row the route answered
`{"purged": 0}` and left it in place; `GET /api/trash/documents` sweeps
expired rows before listing, so the visible rows were normally ineligible
for the action by construction.

Empty trash now drives `purge_all_trashed`: the caller's trashed rows
(`list_all_trashed`, no age filter) each go through the same guarded,
row-locked `purge` as the single-document delete — bytes first, then the
row, in one transaction — so a row restored mid-flight is skipped instead of
force-deleted, and an unlink failure rolls that row back and answers 500 with
a retryable message. Retention expiry stays where it was: the sweep's
`purge_candidates` is now the only age-gated selection, and the lazy
retention sweep still runs on the listing and at startup.

Tests: the router suite replaces the retention-gated expectation with the
reviewer's repro (fresh row purged, bytes unlinked, shelf and other users'
trash untouched, a failing unlink stays retryable and 500); a blocking-I/O
anchor drives the new entry point through the offload; the mocked e2e covers
the action end to end; a new real-backend spec performs it against the real
gateway and re-reads `GET /api/trash/documents`. README, API, ARCHITECTURE
and the phase-2 design docs (en+zh) state the age-independent contract.
2026-09-16 18:46:18 +08:00

202 lines
9.3 KiB
Python

"""Project shelf tools: bounded live reads through the pinned project identity.
Two read-only tools (Phase-2 spec §7.3; the shelf is user-curated — there is
no agent-initiated shelf write). Both take ``project_id`` from the run's
admission-pinned context (``PROJECT_CONTEXT_KEY``) and ``user_id`` from
:func:`resolve_runtime_user_id`, then query **live** shelf rows: the pinned
snapshot fixes *which* project, never *what* the shelf currently holds. A
missing pin or a missing session factory is a tool error, never an empty
success; a document trashed after this run's index was rendered fails with a
"no longer on the shelf" error rather than serving stale content (§11).
Registration is conditional on the pinned key (§10.11) — see
``agents/lead_agent/agent.py``; subagents never receive these tools. Every
file read and conversion is offloaded via
:func:`deerflow.utils.file_io.run_file_io`.
"""
from __future__ import annotations
import json
import logging
from typing import Annotated, Any
from langchain.tools import tool
from deerflow.agents.middlewares.input_sanitization_middleware import neutralize_untrusted_tags
from deerflow.config.paths import Paths, get_paths
from deerflow.projects.context import pinned_project_snapshot
from deerflow.projects.documents import auto_convert_documents_enabled, document_char_count, read_document_text_window, read_text_serving_path
from deerflow.runtime.user_context import resolve_runtime_user_id
from deerflow.tools.types import Runtime
from deerflow.utils.file_io import run_file_io
logger = logging.getLogger(__name__)
_LIST_DEFAULT_LIMIT = 50
_LIST_MAX_LIMIT = 200
_READ_DEFAULT_LIMIT = 8000
_READ_MAX_LIMIT = 20000
_BINARY_DECLINE_MESSAGE = "This document is binary and cannot be read as text. To let the agent process it, attach it to a thread (attach-to-thread) instead."
_CONVERSION_DISABLED_MESSAGE = "This document is a convertible office/PDF file, but automatic document conversion is disabled (uploads.auto_convert_documents). Enable conversion, or attach the file to a thread (attach-to-thread) instead."
_NOT_ON_SHELF_MESSAGE = "This document is no longer on the shelf (it was trashed or removed after this run started). Call list_project_documents for the current shelf."
_CONTENT_MISSING_MESSAGE = "This document's content is missing from storage (content_missing); only its shelf row remains. Move it to trash from the project page."
_NO_PROJECT_CONTEXT_MESSAGE = "No project is pinned for this run; project document tools are only available in project member threads."
_NO_STORE_MESSAGE = "Project document store is unavailable."
def _error(message: str) -> str:
return json.dumps({"error": message}, ensure_ascii=False)
def _resolve_pin_and_repo(runtime: Runtime) -> tuple[str, str, Any] | str:
"""Resolve ``(project_id, user_id, repository)`` or a JSON error string.
Fail closed (§7.3): no pinned project context or no session factory is a
tool error with no data, never an empty success.
"""
snapshot = pinned_project_snapshot(runtime)
project_id = str(snapshot.get("project_id") or "") if snapshot is not None else ""
if not project_id:
return _error(_NO_PROJECT_CONTEXT_MESSAGE)
from deerflow.persistence import get_session_factory
from deerflow.persistence.projects import ProjectDocumentRepository
session_factory = get_session_factory()
if session_factory is None:
return _error(_NO_STORE_MESSAGE)
user_id = resolve_runtime_user_id(runtime)
return project_id, user_id, ProjectDocumentRepository(session_factory)
def _resolve_auto_convert() -> bool:
"""Worker-thread config read: the app config may cold-load from disk."""
try:
from deerflow.config.app_config import get_app_config
return auto_convert_documents_enabled(get_app_config())
except Exception:
return False
def _clamp(value: int, *, default: int, lower: int, upper: int) -> int:
try:
parsed = int(value)
except (TypeError, ValueError):
return default
return max(lower, min(parsed, upper))
def _shelf_entry_json(row: dict) -> dict[str, Any]:
return {
"id": row["id"],
"name": neutralize_untrusted_tags(str(row.get("name") or "")),
"size_bytes": int(row.get("size_bytes") or 0),
"updated_at": str(row.get("updated_at") or ""),
}
async def _list_project_documents_impl(runtime: Runtime, *, offset: int, limit: int) -> str:
resolved = _resolve_pin_and_repo(runtime)
if isinstance(resolved, str):
return resolved
project_id, user_id, repo = resolved
limit = _clamp(limit, default=_LIST_DEFAULT_LIMIT, lower=1, upper=_LIST_MAX_LIMIT)
offset = max(_clamp(offset, default=0, lower=0, upper=1 << 62), 0)
rows = await repo.list_active(project_id, limit=limit, offset=offset, user_id=user_id)
total = await repo.count_active(project_id, user_id=user_id)
next_offset: int | None = offset + len(rows) if offset + len(rows) < total else None
return json.dumps(
{
"total": total,
"offset": offset,
"next_offset": next_offset,
"documents": [_shelf_entry_json(row) for row in rows],
},
ensure_ascii=False,
)
async def _read_project_document_impl(runtime: Runtime, *, document_id: str, offset: int, limit: int, paths: Paths | None = None) -> str:
resolved = _resolve_pin_and_repo(runtime)
if isinstance(resolved, str):
return resolved
project_id, user_id, repo = resolved
row = await repo.get(document_id, user_id=user_id)
# Fail closed on every mismatch: trashed/purged after the index rendered,
# foreign, or belonging to a different project than the pinned one — one
# stale-entry error, never cross-project reads, never stale content (§11).
if row is None or row.get("project_id") != project_id:
return _error(_NOT_ON_SHELF_MESSAGE)
paths = paths or get_paths()
auto_convert = await run_file_io(_resolve_auto_convert)
serving_path, reason = await read_text_serving_path(repo, paths, user_id=user_id, row=row, auto_convert=auto_convert)
if serving_path is None:
if reason == "content_missing":
return _error(_CONTENT_MISSING_MESSAGE)
if reason == "conversion_disabled":
return _error(_CONVERSION_DISABLED_MESSAGE)
return _error(_BINARY_DECLINE_MESSAGE)
offset = _clamp(offset, default=0, lower=0, upper=1 << 62)
limit = _clamp(limit, default=_READ_DEFAULT_LIMIT, lower=1, upper=_READ_MAX_LIMIT)
# The character count is content-identity cached (immutable rows, §6.2);
# the windowed read decodes only what the page needs — and past-end pages
# read nothing at all.
total_chars = await document_char_count(document_id=row["id"], sha256=row["sha256"], path=serving_path)
content = await read_document_text_window(serving_path, offset=offset, limit=limit) if offset < total_chars else ""
return json.dumps(
{
"name": neutralize_untrusted_tags(str(row.get("name") or "")),
"total_chars": total_chars,
"offset": offset,
"returned_chars": len(content),
"truncated": offset + len(content) < total_chars,
"content": content,
},
ensure_ascii=False,
)
@tool
async def list_project_documents(
runtime: Runtime,
offset: Annotated[int, "Number of shelf entries to skip for pagination (default 0). Use next_offset from a previous call to walk the shelf."] = 0,
limit: Annotated[int, "Maximum entries to return (default 50, max 200)."] = _LIST_DEFAULT_LIMIT,
) -> str:
"""List documents on the current project's shelf (metadata only, no content).
Returns JSON: {"total", "offset", "next_offset", "documents": [{"id",
"name", "size_bytes", "updated_at"}, ...]} in the shelf's own order
(recently updated first). Use this when the <documents> index says the
shelf has more entries than it shows, or the user asks what documents the
project holds. Each entry's stable "id" is what read_project_document
takes. Live data: reflects the shelf as of this call.
"""
return await _list_project_documents_impl(runtime, offset=offset, limit=limit)
@tool
async def read_project_document(
runtime: Runtime,
document_id: Annotated[str, "Stable document ID from the <documents> index or list_project_documents."],
offset: Annotated[int, "Character offset into the document's text (default 0)."] = 0,
limit: Annotated[int, "Maximum characters to return (default 8000, max 20000)."] = _READ_DEFAULT_LIMIT,
) -> str:
"""Read a bounded text slice of one project shelf document.
Returns JSON: {"name", "total_chars", "offset", "returned_chars",
"truncated", "content"}. Long documents are served in slices: advance
"offset" by the returned character count while "truncated" is true.
Office/PDF documents are served as converted markdown when conversion is
enabled. Binary documents cannot be read — attach them to a thread
instead. A document trashed after this run started reports that it is no
longer on the shelf.
"""
return await _read_project_document_impl(runtime, document_id=document_id, offset=offset, limit=limit)
def get_project_document_tools() -> list:
"""The two shelf tools the lead agent gains in project runs (§7.3)."""
return [list_project_documents, read_project_document]