Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
plans,
plugins,
presets,
project_source_bindings,
providers,
records,
schedules,
Expand All @@ -37,6 +38,7 @@
webhooks,
workers,
workflows,
workspace_sources,
workspaces,
)

Expand Down Expand Up @@ -77,3 +79,5 @@
v1_router.include_router(system.router)
v1_router.include_router(identity.router)
v1_router.include_router(workspaces.router)
v1_router.include_router(workspace_sources.router)
v1_router.include_router(project_source_bindings.router)
292 changes: 143 additions & 149 deletions backend/api/v1/chat.py

Large diffs are not rendered by default.

226 changes: 226 additions & 0 deletions backend/api/v1/project_source_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from backend.database import get_db
from backend.models.source_binding import (
Source,
SourceBinding,
SourceBindingRevision,
SourceRevision,
)
from backend.models.workflow import Project
from backend.schemas.common import ApiResponse
from backend.schemas.source_binding import (
SourceBindingCreate,
SourceBindingRead,
SourceBindingRevisionCreate,
SourceBindingRevisionRead,
SourceBindingUpdate,
)
from backend.security.identity import RequestIdentity, get_request_identity
from backend.security.workspace_rbac import (
WorkspacePermission,
get_workspace_access,
require_permission,
)

router = APIRouter(
prefix="/workspaces/{workspace_id}/projects/{project_id}/source-bindings",
tags=["source-bindings"],
)


async def _get_project(db: AsyncSession, workspace_id: str, project_id: str) -> Project:
project = await db.scalar(
select(Project).where(Project.workspace_id == workspace_id, Project.id == project_id)
)
if project is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Project not found")
return project


async def _get_source_in_workspace(db: AsyncSession, workspace_id: str, source_id: str) -> Source:
# Scoping the lookup to workspace_id (taken from THIS project's own path,
# never from the caller-supplied source_id) is the cross-workspace guard
# from ADR-0041: a Project can only bind Sources owned by its own Workspace.
source = await db.scalar(
select(Source).where(Source.workspace_id == workspace_id, Source.id == source_id)
)
if source is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Source not found")
return source


async def _get_source_revision(
db: AsyncSession,
source_id: str,
revision_number: int,
) -> SourceRevision:
revision = await db.scalar(
select(SourceRevision).where(
SourceRevision.source_id == source_id,
SourceRevision.revision_number == revision_number,
)
)
if revision is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Source revision not found")
return revision


async def _get_binding(
db: AsyncSession, project_id: str, binding_id: str, for_update: bool = False
) -> SourceBinding:
query = select(SourceBinding).where(
SourceBinding.project_id == project_id, SourceBinding.id == binding_id
)
if for_update:
query = query.with_for_update()
Comment on lines +77 to +78

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

python -m pip install --quiet "SQLAlchemy==2.0.0"

python - <<'PY'
from sqlalchemy import column, select, table
from sqlalchemy.dialects import postgresql, sqlite

stmt = select(table("source_bindings", column("id"))).with_for_update()
sqlite_sql = str(stmt.compile(dialect=sqlite.dialect()))
postgres_sql = str(stmt.compile(dialect=postgresql.dialect()))

print(f"SQLite:     {sqlite_sql}")
print(f"PostgreSQL: {postgres_sql}")
assert "FOR UPDATE" not in sqlite_sql.upper()
assert "FOR UPDATE" in postgres_sql.upper()
PY

Repository: 2233admin/opencli-admin

Length of output: 292


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Repository files matching project_source_bindings.py:\n'
fd -a 'project_source_bindings\.py$' . | sed 's#^\./##'

printf '\nFile excerpt:\n'
file="$(fd 'project_source_bindings\.py$' . | head -n1)"
if [ -n "$file" ]; then
  nl -ba "$file" | sed -n '1,260p'
fi

printf '\nSearch for supported DB docs / sqlite references:\n'
rg -n "sqlite|SQLite|PostgreSQL|postgres|FOR UPDATE|with_for_update|revision" -S . \
  -g '!*.lock' -g '!**/*.pyc' -g '!__pycache__' -g '!node_modules' -g '!dist' -g '!build' | head -n 200

printf '\nSQLAlchemy dependency/version references:\n'
rg -n "sqlalchemy|SQLAlchemy|dependency|DATABASE|DATABASE_URL" -S . \
  -g 'pyproject.toml' -g 'requirements*.txt' -g 'poetry.lock' -g 'Pipfile*' -g 'environment.yml' -g '!node_modules' -g '!dist' -g '!build' | head -n 200

Repository: 2233admin/opencli-admin

Length of output: 332


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'File excerpt:\n'
file="$(fd 'project_source_bindings\.py$' . | head -n1)"
python3 - <<'PY' "$file"
import sys
path = sys.argv[1]
for i, line in enumerate(open(path, encoding="utf-8"), 1):
    if 1 <= i <= 260:
        print(f"{i:4d}\t{line}", end="")
PY

printf '\nSQLite references and relevant docs/code:\n'
rg -n "sqlite|SQLite|DATABASE_URL|DATABASE|PostgreSQL|postgres|FOR UPDATE|with_for_update|update_sequence|revision" -S . \
  -g '!*.lock' -g '!**/*.pyc' -g '!__pycache__' -g '!node_modules' -g '!dist' -g '!build' | head -n 240

printf '\nDependency references:\n'
rg -n "sqlalchemy|SQLAlchemy|Django|FastAPI|uvicorn|dependency" -S . \
  -g 'pyproject.toml' -g 'requirements*.txt' -g 'poetry.lock' -g 'Pipfile*' -g 'environment.yml' -g '!node_modules' -g '!dist' -g '!build' | head -n 200

Repository: 2233admin/opencli-admin

Length of output: 40890


Make revision allocation dialect-safe for SQLite.

with_for_update() is only an exclusive row lock on PostgreSQL; on SQLite it drops to no locking, so the following update_sequence + 1 write can race and hit the unique revision constraint. Use a SQLite transaction/retry-safe allocation pattern with the existing tests, or restrict the single-lock revision path to a dialect-safe backend.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/api/v1/project_source_bindings.py` around lines 77 - 78, Update the
revision allocation flow around the for_update branch to be safe on SQLite: use
a transaction/retry-based allocation pattern that prevents concurrent
update_sequence + 1 races, or gate the single-lock path to dialects where row
locking is supported. Preserve unique revision allocation and extend the
existing tests to cover concurrent SQLite behavior.

binding = await db.scalar(query)
if binding is None:
raise HTTPException(status.HTTP_404_NOT_FOUND, "Source binding not found")
return binding


@router.get("", response_model=ApiResponse[list[SourceBindingRead]])
async def list_source_bindings(
workspace_id: str,
project_id: str,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.READ)
await _get_project(db, workspace_id, project_id)
rows = (await db.execute(
select(SourceBinding)
.where(SourceBinding.project_id == project_id)
.order_by(SourceBinding.created_at)
)).scalars().all()
return ApiResponse.ok([SourceBindingRead.model_validate(row) for row in rows])


@router.post("", response_model=ApiResponse[SourceBindingRead], status_code=201)
async def create_source_binding(
workspace_id: str,
project_id: str,
body: SourceBindingCreate,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.MANAGE_CONFIGURATION)
await _get_project(db, workspace_id, project_id)
source = await _get_source_in_workspace(db, workspace_id, body.source_id)
pinned_revision = await _get_source_revision(db, source.id, body.source_revision_number)

binding = SourceBinding(
project_id=project_id,
source_id=source.id,
name=body.name,
slug=body.slug,
current_revision_number=1,
created_by_user_id=access.user_id,
)
db.add(binding)
await db.flush()
db.add(
SourceBindingRevision(
source_binding_id=binding.id,
revision_number=1,
pinned_source_revision_id=pinned_revision.id,
scope_config=body.scope_config,
created_by_user_id=access.user_id,
)
)
await db.flush()
return ApiResponse.ok(SourceBindingRead.model_validate(binding))


@router.get("/{binding_id}", response_model=ApiResponse[SourceBindingRead])
async def get_source_binding(
workspace_id: str,
project_id: str,
binding_id: str,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.READ)
await _get_project(db, workspace_id, project_id)
binding = await _get_binding(db, project_id, binding_id)
return ApiResponse.ok(SourceBindingRead.model_validate(binding))


@router.patch("/{binding_id}", response_model=ApiResponse[SourceBindingRead])
async def update_source_binding(
workspace_id: str,
project_id: str,
binding_id: str,
body: SourceBindingUpdate,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.MANAGE_CONFIGURATION)
await _get_project(db, workspace_id, project_id)
binding = await _get_binding(db, project_id, binding_id, for_update=True)
for field, value in body.model_dump(exclude_unset=True).items():
setattr(binding, field, value)
await db.flush()
return ApiResponse.ok(SourceBindingRead.model_validate(binding))


@router.get(
"/{binding_id}/revisions", response_model=ApiResponse[list[SourceBindingRevisionRead]]
)
async def list_source_binding_revisions(
workspace_id: str,
project_id: str,
binding_id: str,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.READ)
await _get_project(db, workspace_id, project_id)
await _get_binding(db, project_id, binding_id)
rows = (await db.execute(
select(SourceBindingRevision)
.where(SourceBindingRevision.source_binding_id == binding_id)
.order_by(SourceBindingRevision.revision_number)
)).scalars().all()
return ApiResponse.ok([SourceBindingRevisionRead.model_validate(row) for row in rows])


@router.post(
"/{binding_id}/revisions",
response_model=ApiResponse[SourceBindingRevisionRead],
status_code=201,
)
async def create_source_binding_revision(
workspace_id: str,
project_id: str,
binding_id: str,
body: SourceBindingRevisionCreate,
identity: RequestIdentity = Depends(get_request_identity),
db: AsyncSession = Depends(get_db),
) -> ApiResponse:
access = await get_workspace_access(db, workspace_id, identity)
require_permission(access, WorkspacePermission.MANAGE_CONFIGURATION)
await _get_project(db, workspace_id, project_id)
binding = await _get_binding(db, project_id, binding_id, for_update=True)
pinned_revision = await _get_source_revision(db, binding.source_id, body.source_revision_number)

next_revision = binding.current_revision_number + 1
revision = SourceBindingRevision(
source_binding_id=binding.id,
revision_number=next_revision,
pinned_source_revision_id=pinned_revision.id,
scope_config=body.scope_config,
created_by_user_id=access.user_id,
)
db.add(revision)
binding.current_revision_number = next_revision
await db.flush()
return ApiResponse.ok(SourceBindingRevisionRead.model_validate(revision))
Loading