Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

54 lines
2.3 KiB
Python

"""Task attribution for signals API writes, derived from the `X-Insights-Task-Id` header.
Sandbox agents authenticate with an OAuth token issued on behalf of the task's creator, so
`request.user` alone would mis-attribute agent writes to that human. The sandbox provisioning
bakes the agent's task id into an `X-Insights-Task-Id` header on its MCP config (and the
agent-server's direct API calls), and the MCP server forwards it — the LLM never handles its own
task id, so attribution is deterministic.
Trust model: the header is caller-supplied attribution metadata, not an authorization boundary —
the bearer token is already team-scoped, and the named task must belong to the same team, so a
caller can only attribute writes to tasks it could already see.
"""
from __future__ import annotations
import uuid
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request
from products.signals.backend.models import ArtefactAttribution
from products.tasks.backend.facade import api as tasks_api
TASK_ID_HEADER = "X-Insights-Task-Id"
def resolve_task_id_from_header(request: Request, team_id: int) -> str | None:
"""Return the validated task id from `X-Insights-Task-Id`, or None when the header is absent.
Raises a DRF `ValidationError` (→ 400) when the header value is not a UUID, or names a task
that doesn't exist on this team.
"""
raw = request.headers.get(TASK_ID_HEADER)
if not raw or not raw.strip():
return None
try:
task_uuid = uuid.UUID(raw.strip())
except ValueError:
raise ValidationError({TASK_ID_HEADER: "must be a task UUID."})
if not tasks_api.task_exists(task_uuid, team_id):
raise ValidationError({TASK_ID_HEADER: "unknown task for this project."})
return str(task_uuid)
def resolve_request_attribution(request: Request, team_id: int) -> ArtefactAttribution:
"""Attribution for an API write: the header task when present, else the requesting user."""
task_id = resolve_task_id_from_header(request, team_id)
if task_id is not None:
return ArtefactAttribution.from_task(task_id)
user_id = request.user.id
if user_id is None: # unreachable behind authentication, but keeps attribution honest
raise ValidationError("Cannot attribute a write to an anonymous user.")
return ArtefactAttribution.from_user(user_id)