feat(report-collaboration): complete RC-BE-015–018 backend, eval suite, and MySQL sync

Ship report rewrite/version APIs, security budget and audit persistence, AgentScope worker wiring, and the 20-task quality eval harness so the frontend can integrate against a stable contract once enabled in config.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-09-06 17:32:15 +08:00
1 parent 814a44f0b9
commit 5e953c9fcb
123 files changed
+26816 -3505

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,53 @@
"""Report-collaboration workbench configuration.
Master switch and server-enforced limits for the AgentScope-backed
multi-agent report runtime. Default **off**: APIs, workers and model
calls must not run until an operator opts in.
"""
from __future__ import annotations
from pydantic import BaseModel, Field
class ReportCollaborationConfig(BaseModel):
"""``config.yaml -> report_collaboration`` limits and feature flags."""
enabled: bool = Field(default=False, description="Master switch for /api/report-collaboration")
worker_enabled: bool = Field(
default=False,
description="When true, a dispatcher/worker may claim run leases. Gateway-only processes keep this false.",
)
planner_model: str | None = Field(default=None, description="Model name from AppConfig.models for planning")
coordinator_model: str | None = Field(default=None, description="Model name from AppConfig.models for the constrained coordinator")
research_model: str | None = Field(default=None, description="Model name from AppConfig.models for research members")
writer_model: str | None = Field(default=None, description="Model name from AppConfig.models for writing")
reviewer_model: str | None = Field(default=None, description="Model name from AppConfig.models for independent review")
max_team_members: int = Field(default=8, ge=2, le=16)
max_parallel_tasks: int = Field(default=4, ge=1, le=16)
max_repair_rounds: int = Field(default=3, ge=0, le=8)
max_node_attempts: int = Field(default=3, ge=1, le=8)
max_react_iterations: int = Field(default=8, ge=1, le=32, description="Per-member AgentScope ReAct loop cap")
session_timeout_seconds: int = Field(default=7200, ge=60)
node_timeout_seconds: int = Field(default=900, ge=30)
lease_seconds: int = Field(default=60, ge=10)
heartbeat_seconds: int = Field(default=15, ge=5)
event_retention_days: int = Field(default=30, ge=1)
allowed_tools: list[str] = Field(
default_factory=lambda: ["web_search", "web_fetch", "knowledge_search"],
description="Server allowlist; members may only receive a subset of these names",
)
source_policy: str = Field(default="cite_required", description="Default source policy frozen onto node contracts")
token_budget: int = Field(default=250_000, ge=1000, description="Per-run token budget across all members")
cost_budget: float = Field(default=0.0, ge=0, description="Optional per-run cost cap; 0 means unset")
max_model_calls: int = Field(default=200, ge=1, le=10_000, description="Per-run model-call cap across all members")
max_retrieval_calls: int = Field(default=40, ge=1, le=1000, description="Per-run web/knowledge retrieval call cap")
max_source_bytes: int = Field(default=2_000_000, ge=1000, description="Per-run external source body budget in bytes")
max_concurrent_runs_per_user: int = Field(default=2, ge=1, le=8)
max_clarification_rounds: int = Field(default=3, ge=1, le=8)
intent_confirmation_threshold: float = Field(
default=0.8,
ge=0.5,
le=1.0,
description="Runtime intent confidence below this value requires user confirmation",
)
@@ -0,0 +1,293 @@
"""Create report-collaboration workbench tables.
Revision ID: 20260905_01
Revises: 20260831_01
Create Date: 2026-09-05
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.mysql import LONGTEXT
revision = "20260905_01"
down_revision = "20260831_01"
branch_labels = None
depends_on = None
def _large_text_type() -> sa.types.TypeEngine:
bind = op.get_bind()
return LONGTEXT() if bind.dialect.name == "mysql" else sa.Text()
def _json_type() -> sa.types.TypeEngine:
bind = op.get_bind()
return sa.Text() if bind.dialect.name == "mysql" else sa.JSON()
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
text_type = _large_text_type()
json_type = _json_type()
false_default = sa.false()
if not inspector.has_table("report_collaboration_sessions"):
op.create_table(
"report_collaboration_sessions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("owner_id", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="empty"),
sa.Column("requirement_revision", sa.Integer(), nullable=False, server_default="0"),
sa.Column("selected_plan_id", sa.String(length=64), nullable=True),
sa.Column("active_run_id", sa.String(length=64), nullable=True),
sa.Column("current_report_version_id", sa.String(length=64), nullable=True),
sa.Column("requirement_json", json_type, nullable=True),
sa.Column("create_idempotency_key", sa.String(length=128), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("create_idempotency_key", name="uq_rc_sessions_create_idempotency"),
)
op.create_index("ix_rc_sessions_owner_id", "report_collaboration_sessions", ["owner_id"])
op.create_index("ix_rc_sessions_status", "report_collaboration_sessions", ["status"])
op.create_index("ix_rc_sessions_active_run_id", "report_collaboration_sessions", ["active_run_id"])
if not inspector.has_table("report_collaboration_messages"):
op.create_table(
"report_collaboration_messages",
sa.Column("message_id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("idempotency_key", sa.String(length=128), nullable=True),
sa.Column("role", sa.String(length=16), nullable=False),
sa.Column("content", text_type, nullable=False),
sa.Column("name", sa.String(length=128), nullable=True),
sa.Column("tool_calls_json", json_type, nullable=True),
sa.Column("tool_call_id", sa.String(length=128), nullable=True),
sa.Column("agent_run_id", sa.String(length=64), nullable=True),
sa.Column("node_run_id", sa.String(length=191), nullable=True),
sa.Column("phase_id", sa.String(length=64), nullable=True),
sa.Column("visibility", sa.String(length=16), nullable=False, server_default="visible"),
sa.Column("metadata_json", json_type, nullable=True),
sa.Column("command_id", sa.String(length=64), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
sa.UniqueConstraint("idempotency_key", name="uq_rc_messages_idempotency"),
)
op.create_index("ix_rc_messages_session_id", "report_collaboration_messages", ["session_id"])
op.create_index("ix_rc_messages_session_created", "report_collaboration_messages", ["session_id", "created_at"])
op.create_index("ix_rc_messages_agent_run_id", "report_collaboration_messages", ["agent_run_id"])
op.create_index("ix_rc_messages_node_run_id", "report_collaboration_messages", ["node_run_id"])
if not inspector.has_table("report_collaboration_plans"):
op.create_table(
"report_collaboration_plans",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("proposal_group_id", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("strategy", sa.String(length=64), nullable=False),
sa.Column("summary", text_type, nullable=False),
sa.Column("rationale", text_type, nullable=False),
sa.Column("recommended", sa.Boolean(), nullable=False, server_default=false_default),
sa.Column("estimated_duration_seconds", sa.Integer(), nullable=False, server_default="0"),
sa.Column("estimated_cost_level", sa.String(length=16), nullable=False, server_default="medium"),
sa.Column("requirement_revision", sa.Integer(), nullable=False, server_default="0"),
sa.Column("roles_json", text_type, nullable=False),
sa.Column("nodes_json", text_type, nullable=False),
sa.Column("edges_json", text_type, nullable=False),
sa.Column("quality_gates_json", text_type, nullable=False),
sa.Column("report_structure_id", sa.String(length=64), nullable=True),
sa.Column("validation_json", text_type, nullable=False),
sa.Column("revision", sa.Integer(), nullable=False, server_default="1"),
sa.Column("status", sa.String(length=32), nullable=False, server_default="proposed"),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_rc_plans_session_id", "report_collaboration_plans", ["session_id"])
op.create_index("ix_rc_plans_proposal_group_id", "report_collaboration_plans", ["proposal_group_id"])
op.create_index("ix_rc_plans_status", "report_collaboration_plans", ["status"])
op.create_index("ix_rc_plans_session_status", "report_collaboration_plans", ["session_id", "status"])
if not inspector.has_table("report_collaboration_runs"):
op.create_table(
"report_collaboration_runs",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("plan_id", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="queued"),
sa.Column("cancel_requested", sa.Boolean(), nullable=False, server_default=false_default),
sa.Column("next_event_seq", sa.Integer(), nullable=False, server_default="0"),
sa.Column("current_phase", sa.String(length=64), nullable=True),
sa.Column("active_dedupe_key", sa.String(length=64), nullable=True),
sa.Column("idempotency_key", sa.String(length=128), nullable=True),
sa.Column("lease_owner", sa.String(length=64), nullable=True),
sa.Column("lease_until", sa.DateTime(timezone=True), nullable=True),
sa.Column("attempt", sa.Integer(), nullable=False, server_default="0"),
sa.Column("version", sa.Integer(), nullable=False, server_default="0"),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("active_dedupe_key", name="uq_rc_runs_active_dedupe"),
sa.UniqueConstraint("idempotency_key", name="uq_rc_runs_idempotency"),
)
op.create_index("ix_rc_runs_session_id", "report_collaboration_runs", ["session_id"])
op.create_index("ix_rc_runs_status", "report_collaboration_runs", ["status"])
if not inspector.has_table("report_collaboration_node_runs"):
op.create_table(
"report_collaboration_node_runs",
sa.Column("node_run_id", sa.String(length=191), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("node_id", sa.String(length=128), nullable=False),
sa.Column("attempt", sa.Integer(), nullable=False, server_default="1"),
sa.Column("status", sa.String(length=32), nullable=False, server_default="planned"),
sa.Column("label", sa.String(length=255), nullable=False),
sa.Column("role_display_name", sa.String(length=128), nullable=True),
sa.Column("angle", sa.String(length=255), nullable=True),
sa.Column("progress", sa.Text(), nullable=True),
sa.Column("awaiting_input_json", json_type, nullable=True),
sa.Column("contract_json", text_type, nullable=True),
sa.Column("input_artifact_ids_json", json_type, nullable=True),
sa.Column("command_overlay_revision", sa.Integer(), nullable=False, server_default="0"),
sa.Column("agent_snapshot_json", json_type, nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("ended_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("run_id", "node_id", "attempt", name="uq_rc_node_runs_key"),
)
op.create_index("ix_rc_node_runs_session_id", "report_collaboration_node_runs", ["session_id"])
op.create_index("ix_rc_node_runs_run_id", "report_collaboration_node_runs", ["run_id"])
op.create_index("ix_rc_node_runs_status", "report_collaboration_node_runs", ["status"])
if not inspector.has_table("report_collaboration_agent_runs"):
op.create_table(
"report_collaboration_agent_runs",
sa.Column("agent_run_id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("node_run_id", sa.String(length=191), nullable=True),
sa.Column("phase_id", sa.String(length=64), nullable=True),
sa.Column("role", sa.String(length=64), nullable=False),
sa.Column("display_name", sa.String(length=128), nullable=False),
sa.Column("avatar_key", sa.String(length=64), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("agent_snapshot_json", json_type, nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("ended_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_rc_agent_runs_session_id", "report_collaboration_agent_runs", ["session_id"])
op.create_index("ix_rc_agent_runs_run_id", "report_collaboration_agent_runs", ["run_id"])
op.create_index("ix_rc_agent_runs_node_run_id", "report_collaboration_agent_runs", ["node_run_id"])
op.create_index("ix_rc_agent_runs_status", "report_collaboration_agent_runs", ["status"])
op.create_index("ix_rc_agent_runs_run_phase", "report_collaboration_agent_runs", ["run_id", "phase_id"])
if not inspector.has_table("report_collaboration_artifacts"):
op.create_table(
"report_collaboration_artifacts",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("node_id", sa.String(length=128), nullable=False),
sa.Column("attempt", sa.Integer(), nullable=False, server_default="1"),
sa.Column("producer_node_run_id", sa.String(length=191), nullable=True),
sa.Column("artifact_type", sa.String(length=64), nullable=False),
sa.Column("schema_version", sa.Integer(), nullable=False, server_default="1"),
sa.Column("requirement_revision", sa.Integer(), nullable=False, server_default="0"),
sa.Column("content_json", text_type, nullable=True),
sa.Column("summary", sa.Text(), nullable=True),
sa.Column("validation_status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("superseded_by", sa.String(length=64), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_rc_artifacts_session_id", "report_collaboration_artifacts", ["session_id"])
op.create_index("ix_rc_artifacts_run_id", "report_collaboration_artifacts", ["run_id"])
op.create_index("ix_rc_artifacts_run_node", "report_collaboration_artifacts", ["run_id", "node_id"])
if not inspector.has_table("report_collaboration_events"):
op.create_table(
"report_collaboration_events",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("event_id", sa.String(length=64), nullable=False),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("seq", sa.Integer(), nullable=False),
sa.Column("event_type", sa.String(length=64), nullable=False),
sa.Column("payload_json", text_type, nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("run_id", "seq", name="uq_rc_events_run_seq"),
sa.UniqueConstraint("event_id", name="uq_rc_events_event_id"),
)
op.create_index("ix_rc_events_session_id", "report_collaboration_events", ["session_id"])
op.create_index("ix_rc_events_run_seq", "report_collaboration_events", ["run_id", "seq"])
if not inspector.has_table("report_collaboration_commands"):
op.create_table(
"report_collaboration_commands",
sa.Column("command_id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=True),
sa.Column("idempotency_key", sa.String(length=128), nullable=False),
sa.Column("operation", sa.String(length=64), nullable=False, server_default="command"),
sa.Column("text", text_type, nullable=False),
sa.Column("intent", sa.String(length=64), nullable=True),
sa.Column("confidence", sa.Float(), nullable=True),
sa.Column("target_type", sa.String(length=64), nullable=True),
sa.Column("target_ids_json", json_type, nullable=True),
sa.Column("normalized_instruction", sa.Text(), nullable=True),
sa.Column("requires_confirmation", sa.Boolean(), nullable=False, server_default=false_default),
sa.Column("cost_level", sa.String(length=16), nullable=True),
sa.Column("reason", sa.Text(), nullable=True),
sa.Column("impact_nodes_json", json_type, nullable=True),
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("expected_revision", sa.Integer(), nullable=True),
sa.Column("error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
sa.UniqueConstraint("idempotency_key", name="uq_rc_commands_idempotency"),
)
op.create_index("ix_rc_commands_session_id", "report_collaboration_commands", ["session_id"])
op.create_index("ix_rc_commands_run_id", "report_collaboration_commands", ["run_id"])
op.create_index("ix_rc_commands_status", "report_collaboration_commands", ["status"])
if not inspector.has_table("report_collaboration_report_versions"):
op.create_table(
"report_collaboration_report_versions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("version", sa.Integer(), nullable=False),
sa.Column("parent_version_id", sa.String(length=64), nullable=True),
sa.Column("change_type", sa.String(length=32), nullable=False, server_default="initial"),
sa.Column("change_summary", sa.Text(), nullable=True),
sa.Column("markdown", text_type, nullable=False),
sa.Column("source_index_json", text_type, nullable=False),
sa.Column("is_head", sa.Boolean(), nullable=False, server_default=false_default),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_rc_report_versions_session_id", "report_collaboration_report_versions", ["session_id"])
op.create_index("ix_rc_report_versions_session_version", "report_collaboration_report_versions", ["session_id", "version"])
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
for table in (
"report_collaboration_report_versions",
"report_collaboration_commands",
"report_collaboration_events",
"report_collaboration_artifacts",
"report_collaboration_agent_runs",
"report_collaboration_node_runs",
"report_collaboration_runs",
"report_collaboration_plans",
"report_collaboration_messages",
"report_collaboration_sessions",
):
if inspector.has_table(table):
op.drop_table(table)
@@ -0,0 +1,104 @@
"""Add report template snapshots, rewrite proposals and atomic version keys.
Revision ID: 20260906_01
Revises: 20260905_01
Create Date: 2026-09-06
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.mysql import LONGTEXT
revision = "20260906_01"
down_revision = "20260905_01"
branch_labels = None
depends_on = None
def _large_text_type() -> sa.types.TypeEngine:
return LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
def _json_type() -> sa.types.TypeEngine:
return sa.Text() if op.get_bind().dialect.name == "mysql" else sa.JSON()
def _columns(table: str) -> set[str]:
return {str(item["name"]) for item in sa.inspect(op.get_bind()).get_columns(table)}
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("report_collaboration_runs"):
columns = _columns("report_collaboration_runs")
if "template_snapshot_json" not in columns:
op.add_column("report_collaboration_runs", sa.Column("template_snapshot_json", _json_type(), nullable=True))
if inspector.has_table("report_collaboration_report_versions"):
columns = _columns("report_collaboration_report_versions")
for name, column_type in (
("template_snapshot_json", _json_type()),
("quality_result_json", _json_type()),
("origin_run_id", sa.String(length=64)),
("idempotency_key", sa.String(length=128)),
):
if name not in columns:
op.add_column("report_collaboration_report_versions", sa.Column(name, column_type, nullable=True))
# Old rows predate idempotency. Give each a stable unique key before enforcing NOT NULL.
versions = sa.table(
"report_collaboration_report_versions",
sa.column("id", sa.String(length=64)),
sa.column("idempotency_key", sa.String(length=128)),
)
conn = op.get_bind()
rows = conn.execute(sa.select(versions.c.id).where(versions.c.idempotency_key.is_(None))).all()
for (version_id,) in rows:
conn.execute(versions.update().where(versions.c.id == version_id).values(idempotency_key=f"legacy:{version_id}"))
indexes = {item["name"] for item in sa.inspect(op.get_bind()).get_indexes("report_collaboration_report_versions")}
if "uq_rc_report_versions_idempotency" not in indexes:
op.create_index("uq_rc_report_versions_idempotency", "report_collaboration_report_versions", ["idempotency_key"], unique=True)
if "uq_rc_report_versions_session_version" not in indexes:
op.create_index("uq_rc_report_versions_session_version", "report_collaboration_report_versions", ["session_id", "version"], unique=True)
if "ix_rc_report_versions_origin_run_id" not in indexes:
op.create_index("ix_rc_report_versions_origin_run_id", "report_collaboration_report_versions", ["origin_run_id"])
if not inspector.has_table("report_collaboration_rewrite_proposals"):
op.create_table(
"report_collaboration_rewrite_proposals",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("parent_version_id", sa.String(length=64), nullable=False),
sa.Column("section", sa.String(length=255), nullable=True),
sa.Column("instruction", _large_text_type(), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="proposed"),
sa.Column("markdown", _large_text_type(), nullable=False),
sa.Column("source_index_json", _large_text_type(), nullable=False),
sa.Column("quality_result_json", _json_type(), nullable=True),
sa.Column("applied_version_id", sa.String(length=64), nullable=True),
sa.Column("idempotency_key", sa.String(length=128), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("idempotency_key", name="uq_rc_rewrites_idempotency"),
)
op.create_index("ix_rc_rewrites_session_id", "report_collaboration_rewrite_proposals", ["session_id"])
op.create_index("ix_rc_rewrites_status", "report_collaboration_rewrite_proposals", ["status"])
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("report_collaboration_rewrite_proposals"):
op.drop_table("report_collaboration_rewrite_proposals")
if inspector.has_table("report_collaboration_report_versions"):
indexes = {item["name"] for item in sa.inspect(op.get_bind()).get_indexes("report_collaboration_report_versions")}
if "ix_rc_report_versions_origin_run_id" in indexes:
op.drop_index("ix_rc_report_versions_origin_run_id", table_name="report_collaboration_report_versions")
for name in ("uq_rc_report_versions_session_version", "uq_rc_report_versions_idempotency"):
if name in indexes:
op.drop_index(name, table_name="report_collaboration_report_versions")
for name in ("idempotency_key", "origin_run_id", "quality_result_json", "template_snapshot_json"):
if name in _columns("report_collaboration_report_versions"):
op.drop_column("report_collaboration_report_versions", name)
if inspector.has_table("report_collaboration_runs") and "template_snapshot_json" in _columns("report_collaboration_runs"):
op.drop_column("report_collaboration_runs", "template_snapshot_json")
@@ -0,0 +1,72 @@
"""Add report-collaboration audit log and durable run budgets.
Revision ID: 20260907_01
Revises: 20260906_01
Create Date: 2026-09-07
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260907_01"
down_revision = "20260906_01"
branch_labels = None
depends_on = None
def _json_type() -> sa.types.TypeEngine:
return sa.Text() if op.get_bind().dialect.name == "mysql" else sa.JSON()
def _columns(table: str) -> set[str]:
return {str(item["name"]) for item in sa.inspect(op.get_bind()).get_columns(table)}
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("report_collaboration_runs"):
columns = _columns("report_collaboration_runs")
additions = (
("budget_snapshot_json", _json_type()),
("usage_json", _json_type()),
("deadline_at", sa.DateTime(timezone=True)),
("budget_exhausted_reason", sa.Text()),
)
for name, column_type in additions:
if name not in columns:
op.add_column("report_collaboration_runs", sa.Column(name, column_type, nullable=True))
if not inspector.has_table("report_collaboration_audits"):
op.create_table(
"report_collaboration_audits",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("session_id", sa.String(length=64), nullable=False),
sa.Column("run_id", sa.String(length=64), nullable=True),
sa.Column("category", sa.String(length=64), nullable=False),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("actor_type", sa.String(length=32), nullable=False, server_default="system"),
sa.Column("actor_id", sa.String(length=128), nullable=True),
sa.Column("resource_type", sa.String(length=64), nullable=True),
sa.Column("resource_id", sa.String(length=191), nullable=True),
sa.Column("outcome", sa.String(length=32), nullable=False, server_default="success"),
sa.Column("reason", sa.Text(), nullable=True),
sa.Column("detail_json", _json_type(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_rc_audits_session_id", "report_collaboration_audits", ["session_id"])
op.create_index("ix_rc_audits_run_id", "report_collaboration_audits", ["run_id"])
op.create_index("ix_rc_audits_category", "report_collaboration_audits", ["category"])
op.create_index("ix_rc_audits_session_created", "report_collaboration_audits", ["session_id", "created_at"])
op.create_index("ix_rc_audits_run_created", "report_collaboration_audits", ["run_id", "created_at"])
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table("report_collaboration_audits"):
op.drop_table("report_collaboration_audits")
if inspector.has_table("report_collaboration_runs"):
for name in ("budget_exhausted_reason", "deadline_at", "usage_json", "budget_snapshot_json"):
if name in _columns("report_collaboration_runs"):
op.drop_column("report_collaboration_runs", name)
@@ -50,18 +50,6 @@ from deerflow.persistence.feedback.model import FeedbackRow
from deerflow.persistence.fixed_questions.model import FixedQuestionRow
from deerflow.persistence.intent_turns.model import IntentTurnRow
from deerflow.persistence.light_apps.model import LightAppRow
from deerflow.persistence.workflow_data_sources.model import WorkflowDataSourceRow
from deerflow.persistence.workflow_events.model import WorkflowRunEventRow
from deerflow.persistence.workflow_runs.model import (
WorkflowNodeRunRow,
WorkflowRunArtifactRow,
WorkflowRunRow,
)
from deerflow.persistence.workflow_planning.model import (
WorkflowPlanningProposalRow,
WorkflowPlanningSessionRow,
)
from deerflow.persistence.workflows.model import WorkflowDefinitionRow, WorkflowVersionRow
from deerflow.persistence.llm_metrics.model import LlmCallMetricRow
from deerflow.persistence.llmwiki.model import LlmWikiConversationDepositRow, LlmWikiKnowledgeBaseRow
from deerflow.persistence.llmwiki_index.model import LlmWikiWikiPageRow, LlmWikiWikiSyncStateRow, LlmWikiWikiVectorRow
@@ -77,6 +65,20 @@ from deerflow.persistence.position_roundtable.model import (
)
from deerflow.persistence.positions.model import PositionRow, PositionTagBindingRow
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
from deerflow.persistence.report_collaboration.model import (
ReportCollaborationAgentRunRow,
ReportCollaborationArtifactRow,
ReportCollaborationAuditRow,
ReportCollaborationCommandRow,
ReportCollaborationEventRow,
ReportCollaborationMessageRow,
ReportCollaborationNodeRunRow,
ReportCollaborationPlanRow,
ReportCollaborationReportVersionRow,
ReportCollaborationRewriteProposalRow,
ReportCollaborationRunRow,
ReportCollaborationSessionRow,
)
from deerflow.persistence.report_structures.model import ReportStructureRow
from deerflow.persistence.research_templates.model import ResearchTemplateRow
from deerflow.persistence.roundtable_artifact_submissions.model import RoundtableArtifactSubmissionRow
@@ -109,6 +111,18 @@ from deerflow.persistence.tool_metrics.model import ToolCallMetricRow
from deerflow.persistence.user.model import UserRow
from deerflow.persistence.user_preferences.model import UserPreferencesRow
from deerflow.persistence.user_prompts.model import UserCustomPromptRow
from deerflow.persistence.workflow_data_sources.model import WorkflowDataSourceRow
from deerflow.persistence.workflow_events.model import WorkflowRunEventRow
from deerflow.persistence.workflow_planning.model import (
WorkflowPlanningProposalRow,
WorkflowPlanningSessionRow,
)
from deerflow.persistence.workflow_runs.model import (
WorkflowNodeRunRow,
WorkflowRunArtifactRow,
WorkflowRunRow,
)
from deerflow.persistence.workflows.model import WorkflowDefinitionRow, WorkflowVersionRow
__all__ = [
"FeedbackRow",
@@ -163,6 +177,18 @@ __all__ = [
"PositionRow",
"PositionTagBindingRow",
"RecommendedQuestionRow",
"ReportCollaborationAgentRunRow",
"ReportCollaborationArtifactRow",
"ReportCollaborationCommandRow",
"ReportCollaborationEventRow",
"ReportCollaborationMessageRow",
"ReportCollaborationNodeRunRow",
"ReportCollaborationPlanRow",
"ReportCollaborationReportVersionRow",
"ReportCollaborationAuditRow",
"ReportCollaborationRewriteProposalRow",
"ReportCollaborationRunRow",
"ReportCollaborationSessionRow",
"ReportStructureRow",
"ResearchTemplateRow",
"RoundtableChainRow",
@@ -0,0 +1,58 @@
"""Report-collaboration persistence public API."""
from __future__ import annotations
from deerflow.persistence.report_collaboration.base import (
UNSET,
ReportCollaborationConflictError,
ReportCollaborationLimitError,
ReportCollaborationNotFoundError,
ReportCollaborationStore,
ReportCollaborationValidationError,
)
from deerflow.persistence.report_collaboration.memory import MemoryReportCollaborationStore
from deerflow.persistence.report_collaboration.model import (
ReportCollaborationAgentRunRow,
ReportCollaborationArtifactRow,
ReportCollaborationCommandRow,
ReportCollaborationEventRow,
ReportCollaborationMessageRow,
ReportCollaborationNodeRunRow,
ReportCollaborationPlanRow,
ReportCollaborationAuditRow,
ReportCollaborationReportVersionRow,
ReportCollaborationRewriteProposalRow,
ReportCollaborationRunRow,
ReportCollaborationSessionRow,
)
from deerflow.persistence.report_collaboration.sql import SqlReportCollaborationStore
__all__ = [
"MemoryReportCollaborationStore",
"ReportCollaborationAgentRunRow",
"ReportCollaborationArtifactRow",
"ReportCollaborationCommandRow",
"ReportCollaborationConflictError",
"ReportCollaborationAuditRow",
"ReportCollaborationEventRow",
"ReportCollaborationLimitError",
"ReportCollaborationMessageRow",
"ReportCollaborationNodeRunRow",
"ReportCollaborationNotFoundError",
"ReportCollaborationPlanRow",
"ReportCollaborationReportVersionRow",
"ReportCollaborationRewriteProposalRow",
"ReportCollaborationRunRow",
"ReportCollaborationSessionRow",
"ReportCollaborationStore",
"ReportCollaborationValidationError",
"UNSET",
"SqlReportCollaborationStore",
"make_report_collaboration_store",
]
def make_report_collaboration_store(session_factory):
if session_factory is None:
return MemoryReportCollaborationStore()
return SqlReportCollaborationStore(session_factory)
@@ -0,0 +1,380 @@
"""Report-collaboration persistence interface and errors."""
from __future__ import annotations
from abc import ABC, abstractmethod
from datetime import datetime
from typing import Any
UNSET = object()
class ReportCollaborationNotFoundError(Exception):
def __init__(self, kind: str, ident: str) -> None:
super().__init__(f"report collaboration {kind} not found: {ident}")
self.kind = kind
self.ident = ident
class ReportCollaborationConflictError(Exception):
def __init__(self, code: str, message: str, *, current_revision: int | None = None) -> None:
super().__init__(message)
self.code = code
self.current_revision = current_revision
class ReportCollaborationValidationError(Exception):
def __init__(self, code: str, message: str) -> None:
super().__init__(message)
self.code = code
class ReportCollaborationLimitError(Exception):
def __init__(
self,
code: str,
message: str,
*,
usage: dict[str, Any] | None = None,
limits: dict[str, Any] | None = None,
) -> None:
super().__init__(message)
self.code = code
self.usage = usage or {}
self.limits = limits or {}
class ReportCollaborationStore(ABC):
"""SQL and memory implementations share this surface."""
@abstractmethod
async def create_session(self, *, owner_id: str, title: str, idempotency_key: str) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_sessions(self, owner_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def get_session_record(self, session_id: str) -> dict[str, Any] | None:
"""Internal session row including ``owner_id``."""
raise NotImplementedError
@abstractmethod
async def update_session(
self,
session_id: str,
*,
title: str | None,
expected_revision: int | None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def delete_session(self, session_id: str) -> None:
raise NotImplementedError
@abstractmethod
async def get_snapshot(self, session_id: str) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_messages(self, session_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def create_message(
self,
session_id: str,
*,
role: str,
content: str,
idempotency_key: str,
name: str | None = None,
metadata: dict[str, Any] | None = None,
command_id: str | None = None,
agent_run_id: str | None = None,
node_run_id: str | None = None,
phase_id: str | None = None,
visibility: str = "visible",
tool_calls: list[dict[str, Any]] | None = None,
tool_call_id: str | None = None,
completed: bool | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def get_message_by_idempotency(self, idempotency_key: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def save_requirement_state(
self,
session_id: str,
*,
requirement_json: dict[str, Any],
requirement_revision: int,
status: str | None = None,
title: str | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def enqueue_plan_request(
self,
session_id: str,
*,
idempotency_key: str,
expected_revision: int | None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_plans(self, session_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def insert_plan(self, session_id: str, plan: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def supersede_proposed_plans(self, session_id: str) -> int:
raise NotImplementedError
@abstractmethod
async def select_plan(
self,
session_id: str,
plan_id: str,
*,
idempotency_key: str,
expected_revision: int | None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def create_run(
self,
session_id: str,
*,
plan_id: str,
idempotency_key: str,
expected_revision: int | None,
role_snapshots: dict[str, dict[str, Any]] | None = None,
template_snapshot: dict[str, Any] | None = None,
budget_snapshot: dict[str, Any] | None = None,
max_active_runs_per_owner: int | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def get_run(self, run_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def request_cancel_run(self, run_id: str, *, idempotency_key: str) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def claim_run(self, run_id: str, *, lease_owner: str, lease_until: datetime) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def list_claimable(self, *, now: datetime, limit: int = 10) -> list[str]:
raise NotImplementedError
@abstractmethod
async def list_cancel_pending(self, *, limit: int = 20) -> list[str]:
raise NotImplementedError
@abstractmethod
async def renew_lease(self, run_id: str, *, lease_owner: str, lease_until: datetime) -> bool:
raise NotImplementedError
@abstractmethod
async def finalize_cancel(self, run_id: str, *, lease_owner: str | None = None) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def get_run_record(self, run_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def list_events(self, run_id: str, *, after_seq: int = 0, limit: int = 500) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def append_event(
self,
*,
session_id: str,
run_id: str,
event_type: str,
data: dict[str, Any] | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def create_command(
self,
session_id: str,
*,
idempotency_key: str,
text: str,
run_id: str | None = None,
operation: str = "command",
target_type: str | None = None,
target_ids: list[str] | None = None,
expected_revision: int | None = None,
status: str = "pending",
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def get_command(self, command_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def patch_command(
self,
command_id: str,
*,
patch: dict[str, Any],
expected_statuses: set[str] | None = None,
) -> dict[str, Any]:
"""Atomically patch a command, optionally comparing its current status."""
raise NotImplementedError
@abstractmethod
async def update_command_status(self, command_id: str, *, status: str, error: str | None = None) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_commands(self, session_id: str, *, run_id: str | None = None) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def get_node_contract(self, run_id: str, node_id: str) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_report_versions(self, session_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def get_report_version_record(self, session_id: str, version_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def create_report_version(
self,
session_id: str,
*,
idempotency_key: str,
markdown: str,
source_index: list[dict[str, Any]],
change_type: str,
change_summary: str | None,
parent_version_id: str | None = None,
template_snapshot: dict[str, Any] | None = None,
quality_result: dict[str, Any] | None = None,
origin_run_id: str | None = None,
) -> tuple[dict[str, Any], bool]:
raise NotImplementedError
@abstractmethod
async def create_rewrite_proposal(
self,
session_id: str,
*,
idempotency_key: str,
parent_version_id: str,
section: str | None,
instruction: str,
markdown: str,
source_index: list[dict[str, Any]],
quality_result: dict[str, Any] | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def get_rewrite_proposal(self, session_id: str, proposal_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def apply_rewrite_proposal(self, session_id: str, proposal_id: str, *, idempotency_key: str) -> tuple[dict[str, Any], bool]:
raise NotImplementedError
@abstractmethod
async def consume_run_budget(self, run_id: str, *, deltas: dict[str, float | int]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def fail_run_for_limit(self, run_id: str, *, reason: str) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def append_audit(
self,
*,
session_id: str,
run_id: str | None = None,
category: str,
action: str,
actor_type: str = "system",
actor_id: str | None = None,
resource_type: str | None = None,
resource_id: str | None = None,
outcome: str = "success",
reason: str | None = None,
detail: dict[str, Any] | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_audits(self, session_id: str, *, run_id: str | None = None) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def list_artifacts(self, run_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def get_plan(self, plan_id: str) -> dict[str, Any] | None:
raise NotImplementedError
@abstractmethod
async def list_node_runs(self, run_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def save_node_run(self, record: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_agent_runs(self, run_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def save_agent_run(self, record: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def list_artifacts_full(self, run_id: str) -> list[dict[str, Any]]:
raise NotImplementedError
@abstractmethod
async def save_artifact(self, record: dict[str, Any]) -> dict[str, Any]:
raise NotImplementedError
@abstractmethod
async def patch_run(
self,
run_id: str,
*,
status: str | None = None,
current_phase: str | None = None,
active_dedupe_key: Any = UNSET,
session_status: str | None = None,
) -> dict[str, Any]:
raise NotImplementedError
@@ -0,0 +1,66 @@
"""Durable per-run usage math shared by memory and SQL stores."""
from __future__ import annotations
from typing import Any
from deerflow.persistence.report_collaboration.base import ReportCollaborationLimitError
_USAGE_KEYS = ("model_calls", "retrieval_calls", "source_bytes", "input_tokens", "output_tokens", "cost")
def empty_usage() -> dict[str, float | int]:
return {
"model_calls": 0,
"retrieval_calls": 0,
"source_bytes": 0,
"input_tokens": 0,
"output_tokens": 0,
"total_tokens": 0,
"cost": 0.0,
}
def normalize_usage(raw: dict[str, Any] | None) -> dict[str, float | int]:
usage = empty_usage()
for key in _USAGE_KEYS:
if raw and raw.get(key) is not None:
usage[key] = float(raw[key]) if key == "cost" else int(raw[key])
usage["total_tokens"] = int(usage["input_tokens"]) + int(usage["output_tokens"])
return usage
def apply_budget_deltas(usage: dict[str, Any] | None, deltas: dict[str, float | int], limits: dict[str, Any] | None) -> dict[str, float | int]:
current = normalize_usage(usage)
proposed = dict(current)
for key, value in dict(deltas or {}).items():
if key not in _USAGE_KEYS:
continue
if key == "cost":
proposed[key] = float(proposed[key]) + float(value)
else:
proposed[key] = int(proposed[key]) + int(value)
proposed["total_tokens"] = int(proposed["input_tokens"]) + int(proposed["output_tokens"])
caps = dict(limits or {})
checks = (
("max_model_calls", "model_calls", "RUN_BUDGET_EXCEEDED"),
("max_retrieval_calls", "retrieval_calls", "RUN_BUDGET_EXCEEDED"),
("max_source_bytes", "source_bytes", "RUN_BUDGET_EXCEEDED"),
("token_budget", "total_tokens", "RUN_BUDGET_EXCEEDED"),
("cost_budget", "cost", "RUN_BUDGET_EXCEEDED"),
)
for limit_key, usage_key, code in checks:
cap = caps.get(limit_key)
if cap in (None, "", 0):
continue
if float(proposed[usage_key]) > float(cap):
raise ReportCollaborationLimitError(
code,
"运行预算已用尽",
usage=current,
limits=caps,
)
return proposed
__all__ = ["apply_budget_deltas", "empty_usage", "normalize_usage"]
@@ -0,0 +1,313 @@
"""Dict codecs shared by SQL and memory report-collaboration stores."""
from __future__ import annotations
import json
from datetime import UTC, datetime
from typing import Any
_TERMINAL_RUN_STATUSES = frozenset({"completed", "failed", "cancelled"})
def utc_now() -> datetime:
return datetime.now(UTC)
def iso(value: datetime | str | None) -> str | None:
if value is None:
return None
if isinstance(value, str):
return value
if value.tzinfo is None:
value = value.replace(tzinfo=UTC)
return value.astimezone(UTC).isoformat().replace("+00:00", "Z")
def iso_now() -> str:
return iso(utc_now()) or ""
def dumps(value: Any) -> str:
if value is None:
return "null"
return json.dumps(value, ensure_ascii=False, separators=(",", ":"), default=str)
def loads_list(value: Any) -> list[Any]:
if value is None:
return []
if isinstance(value, list):
return value
if isinstance(value, str) and value:
parsed = json.loads(value)
return parsed if isinstance(parsed, list) else []
return []
def loads_dict(value: Any) -> dict[str, Any]:
if value is None:
return {}
if isinstance(value, dict):
return value
if isinstance(value, str) and value:
parsed = json.loads(value)
return parsed if isinstance(parsed, dict) else {}
return {}
def make_node_run_id(node_id: str, attempt: int) -> str:
return f"{node_id}-attempt{attempt}"
def is_terminal_run(status: str) -> bool:
return status in _TERMINAL_RUN_STATUSES
def parse_lease_until(value: datetime | str | None) -> datetime | None:
if value is None:
return None
if isinstance(value, datetime):
return value if value.tzinfo else value.replace(tzinfo=UTC)
try:
parsed = datetime.fromisoformat(str(value).replace("Z", "+00:00"))
except ValueError:
return None
return parsed if parsed.tzinfo else parsed.replace(tzinfo=UTC)
def lease_is_expired(record: dict[str, Any], *, now: datetime | None = None) -> bool:
"""A missing lease on a non-queued run is treated as expired so a crash can reclaim it."""
deadline = parse_lease_until(record.get("lease_until"))
if deadline is None:
return True
current = now or utc_now()
if current.tzinfo is None:
current = current.replace(tzinfo=UTC)
return deadline < current
def session_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"title": record.get("title") or "",
"status": record["status"],
"requirement_revision": int(record.get("requirement_revision") or 0),
"selected_plan_id": record.get("selected_plan_id"),
"active_run_id": record.get("active_run_id"),
"current_report_version_id": record.get("current_report_version_id"),
"created_at": iso(record["created_at"]) or "",
"updated_at": iso(record["updated_at"]) or "",
}
def run_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"session_id": record["session_id"],
"plan_id": record["plan_id"],
"status": record["status"],
"cancel_requested": bool(record.get("cancel_requested")),
"next_event_seq": int(record.get("next_event_seq") or 0),
"current_phase": record.get("current_phase"),
"usage": dict(record.get("usage") or {}),
"budget_exhausted_reason": record.get("budget_exhausted_reason"),
"created_at": iso(record["created_at"]) or "",
"updated_at": iso(record["updated_at"]) or "",
}
def message_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"message_id": record["message_id"],
"role": record["role"],
"content": record.get("content") or "",
"name": record.get("name"),
"tool_calls": list(record.get("tool_calls") or []),
"tool_call_id": record.get("tool_call_id"),
"agent_run_id": record.get("agent_run_id"),
"node_run_id": record.get("node_run_id"),
"phase_id": record.get("phase_id"),
"visibility": record.get("visibility") or "visible",
"metadata": record.get("metadata"),
"command_id": record.get("command_id"),
"created_at": iso(record["created_at"]) or "",
"completed_at": iso(record.get("completed_at")),
}
def plan_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"proposal_group_id": record["proposal_group_id"],
"title": record["title"],
"strategy": record.get("strategy") or "",
"summary": record.get("summary") or "",
"rationale": record.get("rationale") or "",
"recommended": bool(record.get("recommended")),
"estimated_duration_seconds": int(record.get("estimated_duration_seconds") or 0),
"estimated_cost_level": record.get("estimated_cost_level") or "medium",
"requirement_revision": int(record.get("requirement_revision") or 0),
"roles": list(record.get("roles") or []),
"nodes": list(record.get("nodes") or []),
"edges": list(record.get("edges") or []),
"quality_gates": list(record.get("quality_gates") or []),
"report_structure_id": record.get("report_structure_id"),
"validation": record.get("validation") or {"ok": True, "errors": []},
"revision": int(record.get("revision") or 1),
"status": record.get("status") or "proposed",
}
def command_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"command_id": record["command_id"],
"idempotency_key": record["idempotency_key"],
"text": record.get("text") or "",
"intent": record.get("intent"),
"confidence": record.get("confidence"),
"target_type": record.get("target_type"),
"target_ids": list(record.get("target_ids") or []),
"normalized_instruction": record.get("normalized_instruction"),
"requires_confirmation": bool(record.get("requires_confirmation")),
"cost_level": record.get("cost_level"),
"reason": record.get("reason"),
"impact_nodes": list(record.get("impact_nodes") or []),
"status": record["status"],
"expected_revision": record.get("expected_revision"),
"error": record.get("error"),
"created_at": iso(record["created_at"]) or "",
"updated_at": iso(record.get("updated_at")),
}
def node_state_public(record: dict[str, Any], attempts: list[dict[str, Any]] | None = None) -> dict[str, Any]:
awaiting = record.get("awaiting_input")
return {
"node_id": record["node_id"],
"label": record.get("label") or record["node_id"],
"role_display_name": record.get("role_display_name"),
"angle": record.get("angle"),
"status": record.get("status") or "planned",
"attempt": int(record.get("attempt") or 1),
"progress": record.get("progress"),
"awaiting_input": awaiting,
"error": record.get("error"),
"attempts": attempts or [],
}
def agent_run_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"agent_run_id": record["agent_run_id"],
"node_run_id": record.get("node_run_id"),
"phase_id": record.get("phase_id"),
"role": record["role"],
"display_name": record["display_name"],
"avatar_key": record.get("avatar_key"),
"status": record.get("status") or "pending",
"started_at": iso(record.get("started_at")),
"ended_at": iso(record.get("ended_at")),
}
def artifact_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"run_id": record["run_id"],
"node_id": record["node_id"],
"attempt": int(record.get("attempt") or 1),
"artifact_type": record["artifact_type"],
"schema_version": int(record.get("schema_version") or 1),
"validation_status": record.get("validation_status") or "pending",
"summary": record.get("summary"),
"created_at": iso(record["created_at"]) or "",
}
def report_version_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"session_id": record["session_id"],
"version": int(record["version"]),
"parent_version_id": record.get("parent_version_id"),
"change_type": record.get("change_type") or "initial",
"change_summary": record.get("change_summary"),
"markdown": record.get("markdown") or "",
"source_index": list(record.get("source_index") or []),
"created_at": iso(record["created_at"]) or "",
"is_head": bool(record.get("is_head")),
}
def rewrite_proposal_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"session_id": record["session_id"],
"section": record.get("section"),
"instruction": record.get("instruction") or "",
"status": record.get("status") or "proposed",
"markdown": record.get("markdown"),
"created_at": iso(record["created_at"]) or "",
}
def audit_public(record: dict[str, Any]) -> dict[str, Any]:
return {
"id": record["id"],
"session_id": record["session_id"],
"run_id": record.get("run_id"),
"category": record.get("category") or "",
"action": record.get("action") or "",
"actor_type": record.get("actor_type") or "system",
"actor_id": record.get("actor_id"),
"resource_type": record.get("resource_type"),
"resource_id": record.get("resource_id"),
"outcome": record.get("outcome") or "success",
"reason": record.get("reason"),
"detail": record.get("detail") if isinstance(record.get("detail"), dict) else {},
"created_at": iso(record["created_at"]) or "",
}
def event_envelope(record: dict[str, Any]) -> dict[str, Any]:
return {
"eventId": record["event_id"],
"sessionId": record["session_id"],
"runId": record.get("run_id"),
"seq": int(record["seq"]),
"type": record["event_type"],
"timestamp": iso(record.get("timestamp") or record.get("created_at")) or "",
"data": record.get("data") if isinstance(record.get("data"), dict) else loads_dict(record.get("payload_json")),
}
def node_contract_from_plan(plan: dict[str, Any], node_id: str, *, status: str, attempt: int) -> dict[str, Any]:
nodes = list(plan.get("nodes") or [])
edges = list(plan.get("edges") or [])
node = next((item for item in nodes if item.get("id") == node_id), None)
if node is None:
return {}
upstream = [str(edge.get("source")) for edge in edges if edge.get("target") == node_id]
downstream = [str(edge.get("target")) for edge in edges if edge.get("source") == node_id]
depends = [str(item) for item in (node.get("depends_on") or [])]
return {
"node_id": node_id,
"label": node.get("label") or node_id,
"role": node.get("role_display_name") or node.get("role_key"),
"angle": node.get("angle"),
"status": status,
"attempt": attempt,
"mission": node.get("mission") or "",
"input_artifact_types": list(node.get("input_artifact_types") or []),
"source_policy": None,
"output_artifact_type": node.get("output_artifact_type") or "",
"output_structure": None,
"acceptance_criteria": list(node.get("acceptance_criteria") or []),
"allowed_tools": list(node.get("allowed_tools") or []),
"forbidden": [],
"time_budget_seconds": None,
"token_budget": None,
"max_attempts": 3,
"upstream": upstream or depends,
"downstream": downstream,
}
@@ -0,0 +1,298 @@
"""ORM models for the AgentScope report-collaboration workbench.
Independent of ``workflow_*`` and roundtable tables. Child rows carry
``session_id`` so session delete can remove them explicitly (no SQLite cascade).
"""
from __future__ import annotations
from datetime import UTC, datetime
import sqlalchemy as sa
from sqlalchemy import Boolean, Integer, String, Text, UniqueConstraint, false
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
class ReportCollaborationSessionRow(Base):
__tablename__ = "report_collaboration_sessions"
__table_args__ = (UniqueConstraint("create_idempotency_key", name="uq_rc_sessions_create_idempotency"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
owner_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(255), nullable=False, default="")
status: Mapped[str] = mapped_column(String(32), nullable=False, default="empty", index=True)
requirement_revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
selected_plan_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
active_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
current_report_version_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
requirement_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
create_idempotency_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationMessageRow(Base):
__tablename__ = "report_collaboration_messages"
__table_args__ = (
UniqueConstraint("idempotency_key", name="uq_rc_messages_idempotency"),
sa.Index("ix_rc_messages_session_created", "session_id", "created_at"),
)
message_id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
idempotency_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
role: Mapped[str] = mapped_column(String(16), nullable=False)
content: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
name: Mapped[str | None] = mapped_column(String(128), nullable=True)
tool_calls_json: Mapped[list | None] = mapped_column(PortableJSON(), nullable=True)
tool_call_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
agent_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
node_run_id: Mapped[str | None] = mapped_column(String(191), nullable=True, index=True)
phase_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
visibility: Mapped[str] = mapped_column(String(16), nullable=False, default="visible")
metadata_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
command_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
completed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
class ReportCollaborationPlanRow(Base):
__tablename__ = "report_collaboration_plans"
__table_args__ = (sa.Index("ix_rc_plans_session_status", "session_id", "status"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
proposal_group_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(255), nullable=False)
strategy: Mapped[str] = mapped_column(String(64), nullable=False, default="")
summary: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
rationale: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
recommended: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
estimated_duration_seconds: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
estimated_cost_level: Mapped[str] = mapped_column(String(16), nullable=False, default="medium")
requirement_revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
roles_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
nodes_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
edges_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
quality_gates_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
report_structure_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
validation_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
revision: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=sa.text("1"))
status: Mapped[str] = mapped_column(String(32), nullable=False, default="proposed", index=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationRunRow(Base):
__tablename__ = "report_collaboration_runs"
__table_args__ = (
UniqueConstraint("active_dedupe_key", name="uq_rc_runs_active_dedupe"),
UniqueConstraint("idempotency_key", name="uq_rc_runs_idempotency"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
plan_id: Mapped[str] = mapped_column(String(64), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="queued", index=True)
cancel_requested: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
next_event_seq: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
current_phase: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Active unique key = session_id while the run is non-terminal; NULL in a terminal state (MySQL/SQLite allow many NULLs).
active_dedupe_key: Mapped[str | None] = mapped_column(String(64), nullable=True)
idempotency_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
lease_owner: Mapped[str | None] = mapped_column(String(64), nullable=True)
lease_until: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
attempt: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
version: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
template_snapshot_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
budget_snapshot_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
usage_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
deadline_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
budget_exhausted_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationNodeRunRow(Base):
__tablename__ = "report_collaboration_node_runs"
__table_args__ = (UniqueConstraint("run_id", "node_id", "attempt", name="uq_rc_node_runs_key"),)
# Canonical id: ``${node_id}-attempt{attempt}``.
node_run_id: Mapped[str] = mapped_column(String(191), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
run_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
node_id: Mapped[str] = mapped_column(String(128), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=sa.text("1"))
status: Mapped[str] = mapped_column(String(32), nullable=False, default="planned", index=True)
label: Mapped[str] = mapped_column(String(255), nullable=False, default="")
role_display_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
angle: Mapped[str | None] = mapped_column(String(255), nullable=True)
progress: Mapped[str | None] = mapped_column(Text, nullable=True)
awaiting_input_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
contract_json: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
input_artifact_ids_json: Mapped[list | None] = mapped_column(PortableJSON(), nullable=True)
command_overlay_revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
agent_snapshot_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
started_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
ended_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationAgentRunRow(Base):
__tablename__ = "report_collaboration_agent_runs"
__table_args__ = (sa.Index("ix_rc_agent_runs_run_phase", "run_id", "phase_id"),)
agent_run_id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
run_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
node_run_id: Mapped[str | None] = mapped_column(String(191), nullable=True, index=True)
phase_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
role: Mapped[str] = mapped_column(String(64), nullable=False)
display_name: Mapped[str] = mapped_column(String(128), nullable=False)
avatar_key: Mapped[str | None] = mapped_column(String(64), nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending", index=True)
agent_snapshot_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
started_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
ended_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationArtifactRow(Base):
__tablename__ = "report_collaboration_artifacts"
__table_args__ = (sa.Index("ix_rc_artifacts_run_node", "run_id", "node_id"),)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
run_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
node_id: Mapped[str] = mapped_column(String(128), nullable=False)
attempt: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=sa.text("1"))
producer_node_run_id: Mapped[str | None] = mapped_column(String(191), nullable=True)
artifact_type: Mapped[str] = mapped_column(String(64), nullable=False)
schema_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1, server_default=sa.text("1"))
requirement_revision: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default=sa.text("0"))
content_json: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
summary: Mapped[str | None] = mapped_column(Text, nullable=True)
validation_status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending")
superseded_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationEventRow(Base):
__tablename__ = "report_collaboration_events"
__table_args__ = (
UniqueConstraint("run_id", "seq", name="uq_rc_events_run_seq"),
sa.Index("ix_rc_events_run_seq", "run_id", "seq"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
event_id: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
run_id: Mapped[str] = mapped_column(String(64), nullable=False)
seq: Mapped[int] = mapped_column(Integer, nullable=False)
event_type: Mapped[str] = mapped_column(String(64), nullable=False)
payload_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationCommandRow(Base):
__tablename__ = "report_collaboration_commands"
__table_args__ = (UniqueConstraint("idempotency_key", name="uq_rc_commands_idempotency"),)
command_id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False)
operation: Mapped[str] = mapped_column(String(64), nullable=False, default="command")
text: Mapped[str] = mapped_column(PortableLongText(), nullable=False, default="")
intent: Mapped[str | None] = mapped_column(String(64), nullable=True)
confidence: Mapped[float | None] = mapped_column(sa.Float(), nullable=True)
target_type: Mapped[str | None] = mapped_column(String(64), nullable=True)
target_ids_json: Mapped[list | None] = mapped_column(PortableJSON(), nullable=True)
normalized_instruction: Mapped[str | None] = mapped_column(Text, nullable=True)
requires_confirmation: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
cost_level: Mapped[str | None] = mapped_column(String(16), nullable=True)
reason: Mapped[str | None] = mapped_column(Text, nullable=True)
impact_nodes_json: Mapped[list | None] = mapped_column(PortableJSON(), nullable=True)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending", index=True)
expected_revision: Mapped[int | None] = mapped_column(Integer, nullable=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
class ReportCollaborationReportVersionRow(Base):
__tablename__ = "report_collaboration_report_versions"
__table_args__ = (
UniqueConstraint("session_id", "version", name="uq_rc_report_versions_session_version"),
UniqueConstraint("idempotency_key", name="uq_rc_report_versions_idempotency"),
sa.Index("ix_rc_report_versions_origin_run_id", "origin_run_id"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
version: Mapped[int] = mapped_column(Integer, nullable=False)
parent_version_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
change_type: Mapped[str] = mapped_column(String(32), nullable=False, default="initial")
change_summary: Mapped[str | None] = mapped_column(Text, nullable=True)
markdown: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
source_index_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
is_head: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=false())
idempotency_key: Mapped[str | None] = mapped_column(String(128), nullable=True)
origin_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
quality_result_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
template_snapshot_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationRewriteProposalRow(Base):
__tablename__ = "report_collaboration_rewrite_proposals"
__table_args__ = (
UniqueConstraint("idempotency_key", name="uq_rc_rewrites_idempotency"),
sa.Index("ix_rc_rewrites_session_id", "session_id"),
sa.Index("ix_rc_rewrites_status", "status"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
parent_version_id: Mapped[str] = mapped_column(String(64), nullable=False)
section: Mapped[str | None] = mapped_column(String(255), nullable=True)
instruction: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
status: Mapped[str] = mapped_column(String(32), nullable=False, default="proposed", server_default="proposed")
markdown: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
source_index_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
quality_result_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
applied_version_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
class ReportCollaborationAuditRow(Base):
__tablename__ = "report_collaboration_audits"
__table_args__ = (
sa.Index("ix_rc_audits_session_id", "session_id"),
sa.Index("ix_rc_audits_run_id", "run_id"),
sa.Index("ix_rc_audits_category", "category"),
sa.Index("ix_rc_audits_session_created", "session_id", "created_at"),
sa.Index("ix_rc_audits_run_created", "run_id", "created_at"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
session_id: Mapped[str] = mapped_column(String(64), nullable=False)
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
category: Mapped[str] = mapped_column(String(64), nullable=False)
action: Mapped[str] = mapped_column(String(128), nullable=False)
actor_type: Mapped[str] = mapped_column(String(32), nullable=False, default="system", server_default="system")
actor_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
resource_type: Mapped[str | None] = mapped_column(String(64), nullable=True)
resource_id: Mapped[str | None] = mapped_column(String(191), nullable=True)
outcome: Mapped[str] = mapped_column(String(32), nullable=False, default="success", server_default="success")
reason: Mapped[str | None] = mapped_column(Text, nullable=True)
detail_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
@@ -967,6 +967,16 @@ def validate_local_bash_command_paths(command: str, thread_data: ThreadDataState
raise PermissionError(f"Unsafe absolute paths in command: {unsafe}. Use paths under {VIRTUAL_PATH_PREFIX}")
def _sh_safe_host_path(path: str) -> str:
"""Normalise a resolved host path for embedding in a local bash command.
Windows backslash paths break when substituted unquoted into a POSIX sh
command — the shell eats the backslashes (``cd: F:react01...``). Windows
APIs accept forward slashes, so normalise and quote for shell safety.
"""
return shlex.quote(path.replace("\\", "/"))
def replace_virtual_paths_in_command(command: str, thread_data: ThreadDataState | None) -> str:
"""Replace all virtual paths (/mnt/user-data, /mnt/skills, /mnt/acp-workspace) in a command string.
@@ -986,7 +996,7 @@ def replace_virtual_paths_in_command(command: str, thread_data: ThreadDataState
skills_pattern = re.compile(rf"{re.escape(skills_container)}(/[^\s\"';&|<>()]*)?")
def replace_skills_match(match: re.Match) -> str:
return _resolve_skills_path(match.group(0))
return _sh_safe_host_path(_resolve_skills_path(match.group(0)))
result = skills_pattern.sub(replace_skills_match, result)
@@ -997,7 +1007,7 @@ def replace_virtual_paths_in_command(command: str, thread_data: ThreadDataState
acp_pattern = re.compile(rf"{re.escape(_ACP_WORKSPACE_VIRTUAL_PATH)}(/[^\s\"';&|<>()]*)?")
def replace_acp_match(match: re.Match, _tid: str | None = _thread_id) -> str:
return _resolve_acp_workspace_path(match.group(0), _tid)
return _sh_safe_host_path(_resolve_acp_workspace_path(match.group(0), _tid))
result = acp_pattern.sub(replace_acp_match, result)
@@ -1008,7 +1018,7 @@ def replace_virtual_paths_in_command(command: str, thread_data: ThreadDataState
pattern = re.compile(rf"{re.escape(VIRTUAL_PATH_PREFIX)}(/[^\s\"';&|<>()]*)?")
def replace_user_data_match(match: re.Match) -> str:
return replace_virtual_path(match.group(0), thread_data)
return _sh_safe_host_path(replace_virtual_path(match.group(0), thread_data))
result = pattern.sub(replace_user_data_match, result)
@@ -1474,7 +1484,7 @@ def grep_tool(
@tool("read_file", parse_docstring=True)
def read_file_tool(
def read_file_tool(
runtime: ToolRuntime[ContextT, ThreadState],
description: str,
path: str,
@@ -1488,21 +1498,21 @@ def read_file_tool(
path: The **absolute** path to the file to read.
start_line: Optional starting line number (1-indexed, inclusive). Use with end_line to read a specific range.
end_line: Optional ending line number (1-indexed, inclusive). Use with start_line to read a specific range.
"""
try:
requested_path = path
if _is_skills_path(requested_path):
from deerflow.tools.builtins.skill_tools import (
_resolve_active_agent_id,
is_coordinator_only_skill_path,
)
if is_coordinator_only_skill_path(requested_path) and _resolve_active_agent_id(runtime) != "roundtable-coordinator":
# Match skill_view's non-leaking behavior: ordinary agents must not discover whether it exists.
return f"Error: File not found: {requested_path}"
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
if is_local_sandbox(runtime):
"""
try:
requested_path = path
if _is_skills_path(requested_path):
from deerflow.tools.builtins.skill_tools import (
_resolve_active_agent_id,
is_coordinator_only_skill_path,
)
if is_coordinator_only_skill_path(requested_path) and _resolve_active_agent_id(runtime) != "roundtable-coordinator":
# Match skill_view's non-leaking behavior: ordinary agents must not discover whether it exists.
return f"Error: File not found: {requested_path}"
sandbox = ensure_sandbox_initialized(runtime)
ensure_thread_directories_exist(runtime)
if is_local_sandbox(runtime):
thread_data = get_thread_data(runtime)
validate_local_tool_path(path, thread_data, read_only=True)
if _is_skills_path(path):