合入 W2-a 的复核修复(nr8/a:导入的 NOT NULL 补值与孤儿行、收尾 dispose、sslmode 换算、PostgreSQL 写入去 NUL、不分大小写搜索 contains_ci、json_eq 两库同语义、JSON 守卫、用户可见列表补决胜键、bubblewrap --clearenv、部署脚本与原子写的几处、README 连接数预算)

# Conflicts:
#	backend/app/api/schedules.py
#	backend/app/main.py
This commit is contained in:
ThearchyHelios
2026-10-19 12:21:38 +08:00
80 changed files with 995 additions and 201 deletions
+2 -2
View File
@@ -151,7 +151,7 @@ async def reference() -> dict[str, Any]:
async def list_service_accounts(session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
_require_admin()
rows = list((await session.execute(select(User).where(User.kind == keys.SERVICE_KIND)
.order_by(User.created_at))).scalars())
.order_by(User.created_at, User.id))).scalars())
counts: dict[str, int] = {}
for owner_id in (await session.execute(select(AccessKey.owner_user_id).where(
AccessKey.owner_user_id.in_([r.id for r in rows] or [""]), AccessKey.revoked_at.is_(None)))).scalars():
@@ -232,7 +232,7 @@ async def list_keys(session: AsyncSession = Depends(get_session)) -> dict[str, A
principal = current_principal()
if principal.mode == "off" or not principal.user_id:
return {"items": []}
query = select(AccessKey).order_by(AccessKey.created_at.desc())
query = select(AccessKey).order_by(AccessKey.created_at.desc(), AccessKey.id.desc())
if not _is_admin():
query = query.where(AccessKey.owner_user_id == principal.user_id)
rows = list((await session.execute(query)).scalars())
+1 -1
View File
@@ -25,7 +25,7 @@ async def list_actions(
limit: int = Query(default=100, ge=1, le=500),
) -> dict[str, Any]:
ensure_role("builder")
stmt = select(ActionRecord).order_by(ActionRecord.created_at.desc()).limit(limit)
stmt = select(ActionRecord).order_by(ActionRecord.created_at.desc(), ActionRecord.id.desc()).limit(limit)
if run_id:
stmt = stmt.where(ActionRecord.run_id == run_id)
if app_id:
+3 -3
View File
@@ -249,7 +249,7 @@ async def list_apps(include_archived: bool = False, scope: str | None = None,
from app.blueprints.personal import allowed
principal = _principal(x_actor)
query = select(App).order_by(App.updated_at.desc())
query = select(App).order_by(App.updated_at.desc(), App.id.desc())
if not include_archived:
query = query.where(App.status != "archived")
if scope == "personal":
@@ -434,7 +434,7 @@ async def assistant_conversation(app_id: str, session: AsyncSession = Depends(ge
mine = Conversation.created_by == who if who else Conversation.created_by.is_(None)
lookup = (select(Conversation).where(Conversation.kind == "app", Conversation.app_id == app.id,
Conversation.archived.is_(False), mine)
.order_by(Conversation.last_active_at.desc()).limit(1))
.order_by(Conversation.last_active_at.desc(), Conversation.id.desc()).limit(1))
# 取或建在进程里按 (应用, 人) 串行:编辑器开发模式下 effect 执行两次、同一个人开两个标签页,都会同时来取。库里有
# (app_id, created_by) 的部分唯一索引(ux_conversations_app_owner):别处(另一个进程)抢先建了的,这次插入在保存点里
# 被拒,改用那一段——历史才不会被拆开。没署名(关登录又没填)的不受索引约束,靠这把进程内的锁
@@ -692,7 +692,7 @@ async def _hook_state(session: AsyncSession, app: App) -> tuple[dict[str, Any],
version = await _previous(session, app)
rows: dict[str, AppHook] = {}
for row in (await session.execute(select(AppHook).where(AppHook.app_id == app.id)
.order_by(AppHook.created_at))).scalars():
.order_by(AppHook.created_at, AppHook.id))).scalars():
rows.setdefault(row.name, row)
return declarations(app.draft or {}), declarations(version.spec if version is not None else {}), rows
+3 -2
View File
@@ -264,13 +264,14 @@ async def list_blueprints(scope: Literal["shared", "personal"] | None = None,
# 候选只有自己的、发布成了应用的(要不要列看应用的管理权,shared_listed 逐个应用核对)
query = query.where(or_(Blueprint.owner == (principal.username or ""), Blueprint.app_id.is_not(None)))
else:
query = query.order_by(Blueprint.updated_at.desc()).limit(_LIST_MAX)
query = query.order_by(Blueprint.updated_at.desc(), Blueprint.id.desc()).limit(_LIST_MAX)
rows += await shared_listed(session, principal, list((await session.execute(query)).scalars()))
if scope in (None, "personal") and await allowed(session, principal):
mine = Blueprint.scope == "personal"
if principal.mode != "off":
mine = mine & (Blueprint.owner == (principal.username or ""))
rows += list((await session.execute(select(Blueprint).where(mine).order_by(Blueprint.updated_at.desc())
rows += list((await session.execute(select(Blueprint).where(mine)
.order_by(Blueprint.updated_at.desc(), Blueprint.id.desc())
.limit(_LIST_MAX))).scalars())
rows.sort(key=lambda bp: bp.updated_at or bp.created_at, reverse=True)
return {"items": [await blueprint_out(session, bp) for bp in rows[:_LIST_MAX]]}
+1 -1
View File
@@ -120,7 +120,7 @@ async def pick_base(session: Any, target: Run, *, schedule_only: bool = False) -
Run.created_at < target.created_at)
base = (await session.execute(
select(Run).where(*earlier, Run.status == "succeeded", Run.manifest_hash.is_not(None))
.order_by(Run.created_at.desc()).limit(1))).scalars().first()
.order_by(Run.created_at.desc(), Run.id.desc()).limit(1))).scalars().first()
if base is None:
continue
between = await session.scalar(select(func.count()).select_from(Run).where(
+1 -1
View File
@@ -390,7 +390,7 @@ async def list_conversations(
# 开登录后的 user 角色:只列自己建的;建立人为空的老对话不列
query = query.where(Conversation.created_by == mine)
rows = list((await session.execute(
query.order_by(Conversation.last_active_at.desc())
query.order_by(Conversation.last_active_at.desc(), Conversation.id.desc())
)).scalars())
# 应用的助手对话只列能管理的应用的(一个应用一次判断)
managed: dict[str, bool] = {}
+1 -1
View File
@@ -280,7 +280,7 @@ async def _recipe_infos(session: AsyncSession, rows: list[DataSource]) -> dict[s
for st in (await session.execute(
select(ImportStaging).where(
ImportStaging.source_id.in_(ids), ImportStaging.status.in_(table_versions.STAGING_OPEN),
).order_by(ImportStaging.created_at)
).order_by(ImportStaging.created_at, ImportStaging.id)
)).scalars():
# 按创建时间升序,后面的覆盖前面的:留下最近的那一个
out.setdefault(st.source_id, {})["open_staging"] = {
+9 -7
View File
@@ -285,7 +285,7 @@ async def _overview(session: AsyncSession, suite_ids: list[str]) -> dict[str, di
for r in reversed(same)]
schedules = (await session.execute(select(Schedule).where(
Schedule.target_kind == "eval_suite", Schedule.target_id.in_(suite_ids))
.order_by(Schedule.created_at))).scalars()
.order_by(Schedule.created_at, Schedule.id))).scalars()
for row in schedules:
try:
text = cron.describe(row.cron, row.timezone)
@@ -299,7 +299,7 @@ async def _overview(session: AsyncSession, suite_ids: list[str]) -> dict[str, di
@router.get("/suites")
async def list_suites(archived: bool = False, session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
"""题集(归档的不列,archived=true 时一起列)。每个题集另带 latest、trend、schedules(_overview)。"""
query = select(EvalSuite).order_by(EvalSuite.updated_at.desc())
query = select(EvalSuite).order_by(EvalSuite.updated_at.desc(), EvalSuite.id.desc())
if not archived:
query = query.where(EvalSuite.archived.is_(False))
rows = list((await session.execute(query)).scalars())
@@ -457,7 +457,7 @@ async def preview_sql(suite_id: str, request: Request, session: AsyncSession = D
async with SessionLocal() as read:
refs = (await read.execute(select(Artifact.id).where(
Artifact.run_id == outcome.run_ids[0], Artifact.kind == "query_snapshot",
Artifact.node_id == gold_rules.QUERY_NODE).order_by(Artifact.created_at.desc()))).scalars().all()
Artifact.node_id == gold_rules.QUERY_NODE).order_by(Artifact.created_at.desc(), Artifact.id.desc()))).scalars().all()
snapshot = None
for ref in refs:
try:
@@ -537,7 +537,7 @@ async def _run_queries(session: AsyncSession, run_id: str) -> list[dict[str, Any
from app.db.models import Artifact
rows = (await session.execute(select(Artifact).where(Artifact.run_id == run_id, Artifact.kind == QUERY_SNAPSHOT_KIND)
.order_by(Artifact.created_at))).scalars().all()
.order_by(Artifact.created_at, Artifact.id))).scalars().all()
out = []
for ref in rows:
try:
@@ -780,7 +780,8 @@ def _snapshot_error(e: snapshots.SnapshotInvalid) -> CodedHTTPException:
@router.get("/catalog-snapshots")
async def list_snapshots(session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
rows = list((await session.execute(select(EvalCatalogSnapshot).order_by(EvalCatalogSnapshot.created_at.desc())
rows = list((await session.execute(select(EvalCatalogSnapshot)
.order_by(EvalCatalogSnapshot.created_at.desc(), EvalCatalogSnapshot.id.desc())
.limit(200))).scalars())
await snapshots.reconcile_stale(session, rows)
return {"snapshots": [snapshots.snapshot_out(r) for r in rows]}
@@ -976,7 +977,8 @@ async def list_evaluations(suite_id: str, limit: int = 50, session: AsyncSession
await _suite(session, suite_id)
rows = list((await session.execute(select(EvalRun).where(EvalRun.suite_id == suite_id)
.order_by(EvalRun.created_at.desc()).limit(max(1, min(limit, 200))))).scalars())
.order_by(EvalRun.created_at.desc(), EvalRun.id.desc())
.limit(max(1, min(limit, 200))))).scalars())
await runner.reconcile_stale(session, rows)
return {"evaluations": [_evaluation_out(r) for r in rows]}
@@ -1185,7 +1187,7 @@ async def get_pair(pair_id: str, session: AsyncSession = Depends(get_session)) -
from app.eval import compare, runner
rows = list((await session.execute(select(EvalRun).where(EvalRun.pair_id == pair_id)
.order_by(EvalRun.created_at))).scalars())
.order_by(EvalRun.created_at, EvalRun.id))).scalars())
if not rows:
raise CodedHTTPException(404, "这次 A/B 不存在", PAIR_NOT_FOUND)
await runner.reconcile_stale(session, rows)
+1 -1
View File
@@ -52,7 +52,7 @@ async def tool_usage(
for (rid,) in await session.execute(
select(Run.id)
.where(Run.workflow_id == workflow_id, counted_runs())
.order_by(Run.created_at.desc())
.order_by(Run.created_at.desc(), Run.id.desc())
.limit(limit_runs)
)
]
+2 -2
View File
@@ -49,7 +49,7 @@ def user_out(row: User) -> dict[str, Any]:
@router.get("/users")
async def list_users(session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
rows = (await session.execute(select(User).order_by(User.created_at))).scalars()
rows = (await session.execute(select(User).order_by(User.created_at, User.id))).scalars()
return {"items": [user_out(r) for r in rows]}
@@ -294,7 +294,7 @@ async def candidates(purpose: str, app_id: str | None = None, entry: str | None
@router.get("/audit")
async def list_audit(subject_kind: str | None = None, subject_id: str | None = None, limit: int = 200,
session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
query = select(AuditEvent).order_by(AuditEvent.at.desc()).limit(max(1, min(limit, 1000)))
query = select(AuditEvent).order_by(AuditEvent.at.desc(), AuditEvent.id.desc()).limit(max(1, min(limit, 1000)))
if subject_kind:
query = query.where(AuditEvent.subject_kind == subject_kind)
if subject_id:
+2 -2
View File
@@ -221,7 +221,7 @@ async def list_documents(
collection: str | None = None,
session: AsyncSession = Depends(get_session),
) -> list[Document]:
stmt = select(Document).order_by(Document.created_at.desc()).limit(300)
stmt = select(Document).order_by(Document.created_at.desc(), Document.id.desc()).limit(300)
if collection:
stmt = stmt.where(Document.collection == collection)
return list((await session.execute(stmt)).scalars())
@@ -710,7 +710,7 @@ class SkillOut(SkillIn):
@skills_router.get("", response_model=list[SkillOut])
async def list_skills(session: AsyncSession = Depends(get_session)) -> list[Skill]:
return list((await session.execute(select(Skill).order_by(Skill.created_at.desc()))).scalars())
return list((await session.execute(select(Skill).order_by(Skill.created_at.desc(), Skill.id.desc()))).scalars())
@skills_router.post("", response_model=SkillOut, status_code=201)
+1 -1
View File
@@ -86,7 +86,7 @@ async def effects(workflow_id: str | None = None, version: int | None = None, ap
query = query.where(ActionRecord.state == state)
if capability:
query = query.where(ActionRecord.capability == capability)
rows = (await session.execute(query.order_by(ActionRecord.created_at.desc()).limit(limit))).scalars()
rows = (await session.execute(query.order_by(ActionRecord.created_at.desc(), ActionRecord.id.desc()).limit(limit))).scalars()
return {"items": [effect_row(r) for r in rows]}
+2 -2
View File
@@ -127,7 +127,7 @@ async def list_channels() -> dict[str, Any]:
from app.db.models import User
principal = current_principal()
stmt = select(NotifyChannel).order_by(NotifyChannel.created_at)
stmt = select(NotifyChannel).order_by(NotifyChannel.created_at, NotifyChannel.id)
if principal.mode != "off":
# 个人渠道只给本人看
stmt = stmt.where(or_(NotifyChannel.scope != "user", NotifyChannel.owner_id == principal.user_id))
@@ -295,7 +295,7 @@ async def list_deliveries(
format 是渠道的格式:邮件渠道成功的那条界面上写「已交给邮件服务器」,不写「已送达」(退信不追踪)。"""
stmt = (select(NotifyDelivery, NotifyChannel.format)
.join(NotifyChannel, NotifyChannel.id == NotifyDelivery.channel_id)
.order_by(NotifyDelivery.created_at.desc()).limit(limit))
.order_by(NotifyDelivery.created_at.desc(), NotifyDelivery.id.desc()).limit(limit))
principal = current_principal()
if principal.mode != "off" and not principal.at_least("builder"):
stmt = stmt.where(NotifyChannel.scope == "user", NotifyChannel.owner_id == principal.user_id)
+7 -9
View File
@@ -213,7 +213,7 @@ async def list_runs(
"""
stmt = _filtered(select(Run), workflow_id=workflow_id, status=status, run_class=run_class, q=q,
schedule_id=schedule_id, origin_kind=origin_kind)
stmt = stmt.order_by(Run.created_at.desc()).limit(limit)
stmt = stmt.order_by(Run.created_at.desc(), Run.id.desc()).limit(limit)
if before is not None:
stmt = stmt.where(Run.created_at < before)
return list((await session.execute(stmt)).scalars())
@@ -265,7 +265,10 @@ def _filtered(stmt: Any, *, workflow_id: str | None, status: str | None, run_cla
if classes := _csv(run_class):
stmt = stmt.where(Run.run_class.in_(classes))
if q and q.strip():
stmt = stmt.where(Run.workflow_name.ilike(f"%{_like(q.strip())}%", escape="\\"))
# 不分大小写(db/portable.contains_ci):PostgreSQL 的文本列按字节排序(COLLATE "C"),列上的 ILIKE 只认 ASCII 的大小写
from app.db.portable import contains_ci
stmt = stmt.where(contains_ci(Run.workflow_name, q.strip()))
if schedule_id:
stmt = stmt.where(Run.schedule_id == schedule_id)
if origins := _csv(origin_kind):
@@ -281,11 +284,6 @@ def _csv(value: str | None) -> list[str]:
return [v.strip() for v in (value or "").split(",") if v.strip()]
def _like(text: str) -> str:
"""用户输入里的 % 和 _ 按字面匹配,不当通配符。"""
return text.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
@router.get("/{run_id}", response_model=RunOut)
async def get_run(run_id: str, session: AsyncSession = Depends(get_session)) -> RunOut:
run = await session.get(Run, run_id)
@@ -343,7 +341,7 @@ async def list_artifacts(
) -> list[dict[str, Any]]:
"""这次运行产出的全部工件引用。审阅出具物时从这里下钻到完整证据。"""
rows = await session.execute(
select(Artifact).where(Artifact.run_id == run_id).order_by(Artifact.created_at)
select(Artifact).where(Artifact.run_id == run_id).order_by(Artifact.created_at, Artifact.id)
)
return [
{
@@ -757,7 +755,7 @@ async def list_approvals(
.join(Task, Task.approval_id == Approval.id, isouter=True)
# 别人的个人应用刷新、个人蓝图试运行上的审批不列(只给拥有者,同运行列表)
.where(or_(Run.id.is_(None), not_(hidden_runs(current_principal()))))
.order_by(Approval.created_at.desc())
.order_by(Approval.created_at.desc(), Approval.id.desc())
.limit(limit)
)
if status != "all" and (statuses := _csv(status)):
+6 -6
View File
@@ -467,7 +467,7 @@ async def _fire_outs(session: AsyncSession, fires: list[ScheduleFire]) -> list[d
if fires:
rows = (await session.execute(
select(WebhookDelivery).where(WebhookDelivery.fire_id.in_([f.id for f in fires]))
.order_by(WebhookDelivery.created_at)
.order_by(WebhookDelivery.created_at, WebhookDelivery.id)
)).scalars()
for d in rows:
notified.setdefault(d.fire_id, []).append({"event": d.event, "status": d.status, "at": _iso(d.created_at)})
@@ -618,10 +618,10 @@ async def trigger_options(event: str | None = None, q: str | None = None, select
async def page(model: Any, *conds: Any) -> tuple[list[Any], bool]:
query = select(model).where(*conds)
if words:
# 名字里含这几个字,不分大小写:PostgreSQL 上 LIKE 分 ASCII 大小写、SQLite 不分,一律 ilike(转义同 runs._like)
from app.api.runs import _like
# 名字里含这几个字,不分大小写:两种库同一个说法(db/portable.contains_ci,PostgreSQL 上按库的区域设置折大小写)
from app.db.portable import contains_ci
query = query.where(model.name.ilike(f"%{_like(words)}%", escape="\\"))
query = query.where(contains_ci(model.name, words))
rows = list((await session.execute(query.order_by(model.name, model.id).limit(limit + 1))).scalars())
more = len(rows) > limit
rows = rows[:limit]
@@ -656,7 +656,7 @@ async def trigger_options(event: str | None = None, q: str | None = None, select
objects, more = await makers[e]()
entries.append({"event": e, "label": triggers.EVENT_LABEL[e], "subject_label": triggers.SUBJECT_LABEL[e],
"objects": objects, "more": more})
apps = (await session.execute(select(App.id, App.name).where(App.status != "archived").order_by(App.name))).all()
apps = (await session.execute(select(App.id, App.name).where(App.status != "archived").order_by(App.name, App.id))).all()
return {
"events": entries,
"origins": [{"kind": k, "label": triggers.ORIGIN_LABEL[k]} for k in triggers.FORMAL_ORIGINS],
@@ -737,7 +737,7 @@ async def _preview_event(session: AsyncSession, body: dict[str, Any]) -> dict[st
@router.get("")
async def list_schedules(session: AsyncSession = Depends(get_session)) -> dict[str, Any]:
rows = list((await session.execute(select(Schedule).order_by(Schedule.created_at.desc()))).scalars())
rows = list((await session.execute(select(Schedule).order_by(Schedule.created_at.desc(), Schedule.id.desc()))).scalars())
return {"items": await _schedule_outs(session, rows)}
+1 -1
View File
@@ -134,7 +134,7 @@ def _to_out(row: Provider) -> ProviderOut:
@router.get("/providers", response_model=list[ProviderOut])
async def list_providers(session: AsyncSession = Depends(get_session)) -> list[ProviderOut]:
rows = (await session.execute(select(Provider).order_by(Provider.created_at))).scalars()
rows = (await session.execute(select(Provider).order_by(Provider.created_at, Provider.id))).scalars()
return [_to_out(r) for r in rows]
+1 -1
View File
@@ -206,7 +206,7 @@ async def list_shares(app_id: str, session: AsyncSession = Depends(get_session),
app, _ = await _managed(session, app_id, x_actor)
rows = list((await session.execute(select(ViewShare).where(ViewShare.app_id == app.id)
.order_by(ViewShare.created_at.desc()))).scalars())
.order_by(ViewShare.created_at.desc(), ViewShare.id.desc()))).scalars())
version = await _previous(session, app)
titles = _titles(version.spec if version is not None else None)
return {"enabled": await _enabled(session), "embed_origins": bool(settings.embed_origins),
+2 -2
View File
@@ -79,7 +79,7 @@ async def upkeep(session: AsyncSession = Depends(get_session)) -> dict[str, Any]
counts = await sc.counts_by_source(session, ids)
schedules: dict[str, list[dict[str, Any]]] = {}
for r in (await session.execute(select(Schedule).where(Schedule.target_kind == "source_maintenance")
.order_by(Schedule.created_at))).scalars():
.order_by(Schedule.created_at, Schedule.id))).scalars():
schedules.setdefault(r.target_id or "", []).append({
"id": r.id, "enabled": r.enabled, "cron": r.cron, "timezone": r.timezone,
"description_text": _describe(r.cron, r.timezone),
@@ -249,7 +249,7 @@ async def get_maintenance(source_id: str, session: AsyncSession = Depends(get_se
row = await _source(session, source_id)
rows = list((await session.execute(select(Schedule).where(
Schedule.target_kind == "source_maintenance", Schedule.target_id == row.id)
.order_by(Schedule.created_at))).scalars())
.order_by(Schedule.created_at, Schedule.id))).scalars())
outs = await _schedule_outs(session, rows)
fire_ids = [o["last_fire"]["id"] for o in outs if o.get("last_fire")]
inputs = dict((await session.execute(select(ScheduleFire.id, ScheduleFire.inputs).where(
+1 -1
View File
@@ -131,7 +131,7 @@ async def _workflow_out(session: AsyncSession, workflow: Workflow) -> WorkflowOu
@router.get("", response_model=list[WorkflowOut])
async def list_workflows(session: AsyncSession = Depends(get_session)) -> list[WorkflowOut]:
rows = list(
(await session.execute(select(Workflow).order_by(Workflow.updated_at.desc()))).scalars()
(await session.execute(select(Workflow).order_by(Workflow.updated_at.desc(), Workflow.id.desc()))).scalars()
)
counts = await _run_counts(session)
levels = await _published_levels(session)