mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 14:12:58 +00:00
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.
111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
"""Backfill ``tool_call_attempts.user_id`` / ``agent_id`` (migration 0018).
|
|
|
|
New rows are stamped at propose time by the tool executor. This script
|
|
fills historical rows from data we already trust; tiers run in one
|
|
transaction so each later tier sees only the rows still NULL.
|
|
|
|
Tiers
|
|
-----
|
|
1. Parent message (high confidence). Rows with a ``message_id`` copy the
|
|
message's ``user_id`` and the conversation's ``agent_id``.
|
|
|
|
Message-less rows (headless: scheduled / webhook runs, plus pre-0018
|
|
parse-failure rows) are left NULL on purpose: there is no FK linking an
|
|
attempt to its run, so any inference from a schedule-run *time window*
|
|
would also catch webhook attempts and misattribute them to an unrelated
|
|
tenant whose run happened to span the same instant. The analytics reader
|
|
treats unattributable rows as invisible rather than guessing an owner,
|
|
and new headless rows are stamped at propose time by the executor.
|
|
|
|
Usage::
|
|
|
|
# Dry-run (default): runs the fills in a rolled-back transaction and
|
|
# reports exactly how many rows each tier would touch.
|
|
python scripts/db/backfill_tool_attempts_attribution.py
|
|
|
|
# Commit the backfill.
|
|
python scripts/db/backfill_tool_attempts_attribution.py --apply
|
|
|
|
Exit codes:
|
|
0 — success (dry-run or apply)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
|
|
from sqlalchemy import text # noqa: E402
|
|
|
|
from docsgpt.storage.db.engine import get_engine # noqa: E402
|
|
|
|
|
|
# Tier 1: parent message → user, conversation → agent.
|
|
_TIER1 = text(
|
|
"""
|
|
UPDATE tool_call_attempts t
|
|
SET user_id = m.user_id,
|
|
agent_id = c.agent_id
|
|
FROM conversation_messages m
|
|
LEFT JOIN conversations c ON c.id = m.conversation_id
|
|
WHERE t.message_id = m.id
|
|
AND t.user_id IS NULL
|
|
"""
|
|
)
|
|
|
|
_COUNT_NULL = text(
|
|
"SELECT count(*) FROM tool_call_attempts WHERE user_id IS NULL"
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description=(
|
|
"Backfill tool_call_attempts.user_id/agent_id from existing data."
|
|
),
|
|
)
|
|
parser.add_argument(
|
|
"--apply",
|
|
action="store_true",
|
|
help="Commit the backfill. Default is a rolled-back dry-run.",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
engine = get_engine()
|
|
with engine.connect() as conn:
|
|
trans = conn.begin()
|
|
try:
|
|
# A one-shot maintenance UPDATE can run well past the engine's
|
|
# 30s per-statement guardrail; lift it for this transaction.
|
|
conn.execute(text("SET LOCAL statement_timeout = 0"))
|
|
|
|
before = conn.execute(_COUNT_NULL).scalar_one()
|
|
|
|
t1 = conn.execute(_TIER1).rowcount or 0
|
|
|
|
after = conn.execute(_COUNT_NULL).scalar_one()
|
|
|
|
print(f"NULL user_id rows before: {before}")
|
|
print(f" tier 1 (parent message): {t1}")
|
|
print(f"NULL user_id rows remaining: {after}")
|
|
print(" (message-less headless rows left NULL by design)")
|
|
|
|
if args.apply:
|
|
trans.commit()
|
|
print("\nCommitted.")
|
|
else:
|
|
trans.rollback()
|
|
print("\nDry run — rolled back. Re-run with --apply to commit.")
|
|
except Exception:
|
|
trans.rollback()
|
|
raise
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|