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:
1 parent
814a44f0b9
commit
5e953c9fcb
123 files changed
+26816
-3505
No files matched your search
@@ -0,0 +1,14 @@
|
||||
"""Security, redaction, audit and budget enforcement for report collaboration."""
|
||||
|
||||
from app.report_collaboration.security.budget import RunBudgetEnforcer, freeze_run_budget
|
||||
from app.report_collaboration.security.redaction import RedactionRecord, SanitizationResult, redact_mapping, redact_text, sanitize_for_user
|
||||
|
||||
__all__ = [
|
||||
"RedactionRecord",
|
||||
"RunBudgetEnforcer",
|
||||
"SanitizationResult",
|
||||
"freeze_run_budget",
|
||||
"redact_mapping",
|
||||
"redact_text",
|
||||
"sanitize_for_user",
|
||||
]
|
||||
@@ -0,0 +1,105 @@
|
||||
"""Frozen per-run budgets and fail-closed durable enforcement."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.config.report_collaboration_config import ReportCollaborationConfig
|
||||
from deerflow.persistence.report_collaboration.base import ReportCollaborationLimitError, ReportCollaborationStore
|
||||
from deerflow.persistence.report_collaboration.codec import parse_lease_until
|
||||
|
||||
|
||||
def freeze_run_budget(config: ReportCollaborationConfig) -> dict[str, Any]:
|
||||
return {
|
||||
"max_team_members": config.max_team_members,
|
||||
"max_parallel_tasks": config.max_parallel_tasks,
|
||||
"max_model_calls": config.max_model_calls,
|
||||
"max_retrieval_calls": config.max_retrieval_calls,
|
||||
"max_source_bytes": config.max_source_bytes,
|
||||
"token_budget": config.token_budget,
|
||||
"cost_budget": config.cost_budget,
|
||||
"session_timeout_seconds": config.session_timeout_seconds,
|
||||
"node_timeout_seconds": config.node_timeout_seconds,
|
||||
"max_node_attempts": config.max_node_attempts,
|
||||
"max_repair_rounds": config.max_repair_rounds,
|
||||
}
|
||||
|
||||
|
||||
class RunBudgetEnforcer:
|
||||
def __init__(self, store: ReportCollaborationStore, run_id: str) -> None:
|
||||
self._store = store
|
||||
self.run_id = run_id
|
||||
|
||||
async def consume(self, deltas: dict[str, float | int], *, action: str, resource_id: str | None = None) -> dict[str, Any]:
|
||||
try:
|
||||
result = await self._store.consume_run_budget(self.run_id, deltas=deltas)
|
||||
except ReportCollaborationLimitError as exc:
|
||||
await self.fail(exc.code, str(exc), detail={"attempted_usage": exc.usage, "limits": exc.limits})
|
||||
raise
|
||||
run = await self._store.get_run_record(self.run_id)
|
||||
if run is not None:
|
||||
await self._store.append_audit(
|
||||
session_id=str(run["session_id"]),
|
||||
run_id=self.run_id,
|
||||
category="budget",
|
||||
action=action,
|
||||
resource_type="run",
|
||||
resource_id=resource_id or self.run_id,
|
||||
detail={"delta": dict(deltas), "usage": result["usage"]},
|
||||
)
|
||||
return result
|
||||
|
||||
async def remaining_seconds(self) -> float | None:
|
||||
run = await self._store.get_run_record(self.run_id)
|
||||
if run is None:
|
||||
return None
|
||||
deadline = parse_lease_until(run.get("deadline_at"))
|
||||
if deadline is None:
|
||||
return None
|
||||
return max(0.0, (deadline - datetime.now(UTC)).total_seconds())
|
||||
|
||||
async def assert_time_available(self) -> None:
|
||||
remaining = await self.remaining_seconds()
|
||||
if remaining is not None and remaining <= 0:
|
||||
await self.fail("RUN_TIME_BUDGET_EXCEEDED", "运行总时长已超过预算")
|
||||
raise ReportCollaborationLimitError("RUN_TIME_BUDGET_EXCEEDED", "运行总时长已超过预算")
|
||||
|
||||
async def fail(self, code: str, message: str, *, detail: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
before = await self._store.get_run_record(self.run_id)
|
||||
if before is None:
|
||||
return {}
|
||||
if str(before.get("status") or "") in {"completed", "cancelled"}:
|
||||
return await self._store.get_run(self.run_id) or {}
|
||||
already_failed = bool(before.get("budget_exhausted_reason"))
|
||||
run = await self._store.fail_run_for_limit(self.run_id, reason=code)
|
||||
if already_failed:
|
||||
return run
|
||||
session_id = str(before["session_id"])
|
||||
await self._store.append_audit(
|
||||
session_id=session_id,
|
||||
run_id=self.run_id,
|
||||
category="budget",
|
||||
action="run.limit_exceeded",
|
||||
resource_type="run",
|
||||
resource_id=self.run_id,
|
||||
outcome="denied",
|
||||
reason=code,
|
||||
detail={"message": message, **(detail or {})},
|
||||
)
|
||||
await self._store.append_event(
|
||||
session_id=session_id,
|
||||
run_id=self.run_id,
|
||||
event_type="run.error",
|
||||
data={"code": code, "message": message},
|
||||
)
|
||||
await self._store.append_event(
|
||||
session_id=session_id,
|
||||
run_id=self.run_id,
|
||||
event_type="run.status.changed",
|
||||
data={"status": "failed", "reason": code},
|
||||
)
|
||||
return run
|
||||
|
||||
|
||||
__all__ = ["RunBudgetEnforcer", "freeze_run_budget"]
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Policy-based redaction without summarising or size-truncating visible data."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
_SECRET_KEYS = frozenset(
|
||||
{
|
||||
"api_key",
|
||||
"openai_api_key",
|
||||
"authorization",
|
||||
"access_token",
|
||||
"refresh_token",
|
||||
"token",
|
||||
"secret",
|
||||
"password",
|
||||
"cookie",
|
||||
"set-cookie",
|
||||
"base_url",
|
||||
"openai_api_base",
|
||||
}
|
||||
)
|
||||
_PRIVACY_KEYS = frozenset({"email", "phone", "mobile", "id_card", "identity_number", "身份证", "手机号"})
|
||||
_DROP_KEYS = frozenset(
|
||||
{
|
||||
"traceback",
|
||||
"stack",
|
||||
"stack_trace",
|
||||
"stacktrace",
|
||||
"exc_info",
|
||||
"exception_tb",
|
||||
"system_prompt",
|
||||
"internal_prompt",
|
||||
"hidden_prompt",
|
||||
"soul",
|
||||
"soul_md",
|
||||
"chain_of_thought",
|
||||
"thinking",
|
||||
"thinking_delta",
|
||||
}
|
||||
)
|
||||
_SECRET_PATTERNS = (
|
||||
re.compile(r"sk-[A-Za-z0-9_\-]{3,}"),
|
||||
re.compile(r"(?:Bearer\s+)[A-Za-z0-9\-._~+/]+=*", re.IGNORECASE),
|
||||
re.compile(r"(?:api[_-]?key\s*[:=]\s*)[^\s,;]+", re.IGNORECASE),
|
||||
re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----"),
|
||||
)
|
||||
_PRIVACY_PATTERNS = (
|
||||
re.compile(r"(?<![\w.+-])[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}(?![\w.-])"),
|
||||
re.compile(r"(?<!\d)1[3-9]\d{9}(?!\d)"),
|
||||
re.compile(r"(?<!\d)\d{17}[\dXx](?!\d)"),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RedactionRecord:
|
||||
path: str
|
||||
reason: str
|
||||
action: str = "replaced"
|
||||
|
||||
def as_dict(self) -> dict[str, str]:
|
||||
return {"path": self.path, "reason": self.reason, "action": self.action}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SanitizationResult:
|
||||
value: Any
|
||||
redactions: tuple[RedactionRecord, ...] = ()
|
||||
|
||||
|
||||
def redact_text(text: str, *, path: str = "$", include_privacy: bool = True) -> SanitizationResult:
|
||||
current = text
|
||||
reasons: list[RedactionRecord] = []
|
||||
for pattern in _SECRET_PATTERNS:
|
||||
replaced = pattern.sub("[REDACTED]", current)
|
||||
if replaced != current:
|
||||
reasons.append(RedactionRecord(path=path, reason="secret"))
|
||||
current = replaced
|
||||
if include_privacy:
|
||||
for pattern in _PRIVACY_PATTERNS:
|
||||
replaced = pattern.sub("[REDACTED]", current)
|
||||
if replaced != current:
|
||||
reasons.append(RedactionRecord(path=path, reason="privacy"))
|
||||
current = replaced
|
||||
return SanitizationResult(current, tuple(reasons))
|
||||
|
||||
|
||||
def sanitize_for_user(value: Any, *, allow_public_reasoning: bool = False) -> SanitizationResult:
|
||||
records: list[RedactionRecord] = []
|
||||
|
||||
def walk(item: Any, path: str, *, public_reasoning: bool) -> Any:
|
||||
if isinstance(item, dict):
|
||||
explicit_public = bool(item.get("public_reasoning")) or str(item.get("reasoning_visibility") or "").lower() == "public"
|
||||
out: dict[Any, Any] = {}
|
||||
for key, child in item.items():
|
||||
key_text = str(key)
|
||||
lowered = key_text.lower()
|
||||
child_path = f"{path}.{key_text}"
|
||||
if lowered in _DROP_KEYS:
|
||||
records.append(RedactionRecord(path=child_path, reason="internal_or_stack", action="removed"))
|
||||
continue
|
||||
if lowered in {"reasoning", "reasoning_delta"} and not (allow_public_reasoning and explicit_public):
|
||||
records.append(RedactionRecord(path=child_path, reason="hidden_reasoning", action="removed"))
|
||||
continue
|
||||
if lowered in _SECRET_KEYS:
|
||||
out[key] = "[REDACTED]"
|
||||
records.append(RedactionRecord(path=child_path, reason="secret"))
|
||||
continue
|
||||
if lowered in _PRIVACY_KEYS:
|
||||
out[key] = "[REDACTED]"
|
||||
records.append(RedactionRecord(path=child_path, reason="privacy"))
|
||||
continue
|
||||
out[key] = walk(child, child_path, public_reasoning=public_reasoning or explicit_public)
|
||||
return out
|
||||
if isinstance(item, list):
|
||||
return [walk(child, f"{path}[{index}]", public_reasoning=public_reasoning) for index, child in enumerate(item)]
|
||||
if isinstance(item, tuple):
|
||||
return [walk(child, f"{path}[{index}]", public_reasoning=public_reasoning) for index, child in enumerate(item)]
|
||||
if isinstance(item, str):
|
||||
result = redact_text(item, path=path)
|
||||
records.extend(result.redactions)
|
||||
return result.value
|
||||
return item
|
||||
|
||||
return SanitizationResult(walk(value, "$", public_reasoning=False), tuple(records))
|
||||
|
||||
|
||||
def redact_mapping(value: Any) -> Any:
|
||||
"""Compatibility helper for model/tool adapters."""
|
||||
|
||||
return sanitize_for_user(value).value
|
||||
|
||||
|
||||
__all__ = ["RedactionRecord", "SanitizationResult", "redact_mapping", "redact_text", "sanitize_for_user"]
|
||||
Reference in new issue
Block a user