Files
DocsGPT/docsgpt/alembic/versions/0018_tool_attempts_attribution.py
Alex 574f96341e refactor: rename the application package to docsgpt
The backend import package is now docsgpt, the name it will carry on PyPI;
application was far too generic to install into anyone's site-packages.
git mv plus a mechanical rewrite of every import, dotted string and path
reference: 734 Python files, the compose files, Dockerfile, workflows, docs,
setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage
config, .gitignore. Behaviour is unchanged.

Kept for one release:
- A top-level application package whose meta-path finder resolves
  application.x.y to the already-imported docsgpt.x.y object, so old imports
  and entry points (celery -A application.app.celery,
  uvicorn application.asgi:asgi_app) keep working with a FutureWarning.
- Celery registers every application.* task name as an alias of its
  docsgpt.* task on start-up, so messages queued by the previous release still
  run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries
  the previous release wrote are left unread instead of firing twice.

The backend image builds from the repository root (docker build -f
docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore
allow-lists docsgpt/ and application/ and keeps caches, local data, .env
files, the sample index files and the Dockerfile out. Compose and the image
workflows point at the new context.
2026-09-07 10:20:43 +01:00

57 lines
2.3 KiB
Python

"""0018 tool_call_attempts attribution — record user/agent on the journal.
Adds ``tool_call_attempts.user_id`` / ``agent_id`` so tool analytics can
attribute attempts that never get a ``message_id`` — headless runs
(scheduled / webhook) execute tools before any conversation message
exists, and parse-failure rows never reach one at all.
DDL only — a whole-table UPDATE here would hold the ALTERs' ACCESS
EXCLUSIVE lock across the rewrite and stall live tool journaling. The
backfill lives in ``scripts/db/backfill_tool_attempts_attribution.py``;
until it runs, the analytics reader falls back to the parent message's
user for unstamped rows.
The index is built ``CONCURRENTLY`` in an autocommit block: the
``ADD COLUMN`` ALTERs commit first (a fast metadata-only change), so the
O(table-size) index build never runs while their ACCESS EXCLUSIVE lock
is held and never blocks live journaling.
Revision ID: 0018_tool_attempts_attribution
Revises: 0017_oidc_scim
"""
from typing import Sequence, Union
from alembic import op
revision: str = "0018_tool_attempts_attribution"
down_revision: Union[str, None] = "0017_oidc_scim"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute("ALTER TABLE tool_call_attempts ADD COLUMN user_id TEXT;")
op.execute("ALTER TABLE tool_call_attempts ADD COLUMN agent_id UUID;")
# CONCURRENTLY can't run inside a transaction; the autocommit block
# commits the ALTERs above (releasing their lock) before building.
# DROP IF EXISTS first so a failed concurrent build (which leaves an
# INVALID index) is cleaned up on re-run.
with op.get_context().autocommit_block():
op.execute("DROP INDEX IF EXISTS tool_call_attempts_user_ts_idx;")
op.execute(
"CREATE INDEX CONCURRENTLY tool_call_attempts_user_ts_idx "
"ON tool_call_attempts (user_id, attempted_at DESC) "
"WHERE user_id IS NOT NULL;"
)
def downgrade() -> None:
with op.get_context().autocommit_block():
op.execute(
"DROP INDEX CONCURRENTLY IF EXISTS tool_call_attempts_user_ts_idx;"
)
op.execute("ALTER TABLE tool_call_attempts DROP COLUMN IF EXISTS agent_id;")
op.execute("ALTER TABLE tool_call_attempts DROP COLUMN IF EXISTS user_id;")