Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

9.6 KB
Raw Blame
"""Forwarding of inbox discussion questions into the scout steering channel.

When someone hits "Discuss" on an inbox report and types a question, the kickoff spins up a fresh
agentic task whose prompt is the report link plus that question — but nothing about it ever reaches
the scout that authored the report. Yet those questions often carry exactly the context a scout could
use next run: a correction ("this is Apple's approval flow, not a bug"), a preference ("we never JOIN
in batch exports"), or a domain fact the scout couldn't know. This module closes that gap the same way
`dismissal_notes.py` does for dismissal feedback: it also leaves the question as a `SignalScoutNote`,
which every run reads by name at cold start (`scout-notes-list`).

Unlike a dismissal — one reviewer's verdict that a report wasn't worth surfacing — a discussion
question is not a judgement on the report at all, just something a user wanted to know. So the note is
framed as context to weigh, explicitly inviting the scout to fold anything durable into its scratchpad
or ignore it as noise; the scout's own judgement decides which. It is a derived convenience, never a
record of truth: the question lives on the discussion task regardless, so forwarding is best-effort and
never allowed to fail task creation.

Authorization is the notes API's, in full. Creating a discussion task needs only `task:write`, while
the notes table is gated to skill-authoring authorization (scouts read note content verbatim while
holding privileged sandbox tools), so forwarding re-checks that the caller could have left the note by
hand: `principal_may_steer_scouts` (the token's `scoped_teams` leg plus canonical-project access and
the `llm_skill` editor bar) *and* the `signal_scout:write` / `llm_skill:write` key scopes. The
dismissal path skips that scope leg because dismissal text reaches run context anyway through the
reports API; a question has no such second path, so this note is its only carrier and a `task:write`
credential must not open the channel on its own. Forwarding runs against the canonical project whose
scouts read the row, and only when the task was created on the canonical team itself, so a child
environment's question never reaches the parent project's readers.
"""

from __future__ import annotations

import logging
from collections.abc import Sequence

from django.utils import timezone

from posthog.models import Team, User

from products.signals.backend.dismissal_notes import DERIVED_NOTE_TTL, principal_may_steer_scouts
from products.signals.backend.models import SignalReport, SignalScoutNote
from products.signals.backend.scout_authorship import resolve_report_scout_skill
from products.signals.backend.scout_harness.tools.notes import leave_note
from products.signals.backend.scout_note_actor import note_actor

logger = logging.getLogger(__name__)

# Report titles are unbounded TextFields; a note references them for recognition only.
_MAX_TITLE_CHARS = 200
# The question is quoted into a note capped at MAX_NOTE_CONTENT_LENGTH. The Discuss box caps input
# well below this, so the truncation only bites on a hand-crafted create request — where losing the
# tail of a question beats losing the whole note to an oversize rejection.
_MAX_QUESTION_CHARS = 4_000

# The scopes the notes API demands of a token writing a note by hand, which this path forwards under.
_REQUIRED_NOTE_SCOPES = ("signal_scout:write", "llm_skill:write")

# The kickoff prompt (frontend `buildDiscussReportPrompt`) prefixes the report URL on its own line,
# then the user's question after a blank line. We forward just the question; matching is lenient so a
# format change degrades to forwarding the whole prompt rather than dropping the note.
_PROMPT_PREFIXES = (
    "let's discuss this posthog inbox report:",
    "answer this question about the posthog inbox report at ",
    "a user sent this about the posthog inbox report at ",
)
_DESKTOP_DESCRIPTION_PREFIX = "Discuss report: "
# Keep in sync with `buildDiscussDescription` in the Desktop inbox hook.
_DESKTOP_DESCRIPTION_MAX_CHARS = 200


def forward_discussion_note(
    *,
    team: Team,
    report_id: str,
    text: str,
    question: str | None,
    user_id: int | None,
    scoped_team_ids: Sequence[int] | None,
    api_scopes: Sequence[str] | None,
) -> str | None:
    """Leave a discussion question as a scout note. Returns the created note id, or None if nothing
    was forwarded.

    `scoped_team_ids` and `api_scopes` describe the calling credential (both None for session auth);
    the caller reads them off the request so this stays independent of HTTP.

    Best-effort by contract: nothing here may turn a successful task creation into a 5xx — the task
    (and the question on it) already exists, so a failure must not surface. Every step past this point
    runs inside one failure boundary, because authorization and target resolution both read the
    database.
    """
    if question is not None:
        if not question.strip():
            return None
    elif not text or not text.strip():
        return None
    try:
        return _forward(
            team=team,
            report_id=str(report_id),
            text=text,
            question=question,
            user_id=user_id,
            scoped_team_ids=scoped_team_ids,
            api_scopes=api_scopes,
        )
    except Exception:
        logger.exception(
            "Failed to forward a discussion question to a scout note",
            extra={"team_id": team.id, "report_id": report_id},
        )
        return None


def _forward(
    *,
    team: Team,
    report_id: str,
    text: str,
    question: str | None,
    user_id: int | None,
    scoped_team_ids: Sequence[int] | None,
    api_scopes: Sequence[str] | None,
) -> str | None:
    # Scout rows and their notes live under the canonical parent team; forwarding a child
    # environment's question would hand its report id, title, and text to an audience that may
    # have no access to that environment. Those discussions stay on the task only.
    canonical_team = team.parent_team or team
    if team.id != canonical_team.id:
        return None

    user = _note_writer(user_id=user_id, scoped_team_ids=scoped_team_ids, api_scopes=api_scopes, team=canonical_team)
    if user is None:
        return None

    report = SignalReport.objects.filter(team_id=canonical_team.id, id=report_id).first()
    if report is None:
        return None

    note_question = (
        question.strip()
        if question is not None
        else _extract_question(text, report_title=report.title, report_id=report_id)
    )
    if not note_question:
        return None

    skill_name = resolve_report_scout_skill(canonical_team.id, report_id)
    content = _build_note_content(report=report, actor=note_actor(canonical_team.id, user), question=note_question)
    created = leave_note(
        team_id=canonical_team.id,
        content=content,
        skill_name=skill_name,
        created_by_id=user_id,
        expires_at=timezone.now() + DERIVED_NOTE_TTL,
        origin=SignalScoutNote.Origin.REPORT_DISCUSSION,
    )
    return created.id


def _note_writer(
    *, user_id: int | None, scoped_team_ids: Sequence[int] | None, api_scopes: Sequence[str] | None, team: Team
) -> User | None:
    """The caller, if they could have left the same note by hand through the notes API."""
    if api_scopes is not None and not _scopes_allow_note_write(api_scopes):
        # A token that can create the discussion task but not write a note. Its question stays on
        # the task, which is where the user asked it; it just doesn't enter the steering channel.
        return None

    user = User.objects.filter(pk=user_id).first() if user_id else None
    if user is None:
        return None
    if not principal_may_steer_scouts(user=user, scoped_team_ids=scoped_team_ids, canonical_team=team):
        return None
    return user


def _scopes_allow_note_write(api_scopes: Sequence[str]) -> bool:
    scopes = set(api_scopes)
    return "*" in scopes or scopes.issuperset(_REQUIRED_NOTE_SCOPES)


def _extract_question(text: str, *, report_title: str | None, report_id: str | None) -> str:
    stripped = text.strip()
    if stripped.lower().startswith(_PROMPT_PREFIXES):
        _, _, question = stripped.partition("\n")
        return question.strip()

    if report_id is not None:
        report_label = (report_title or "").strip() or report_id
        desktop_base = f"{_DESKTOP_DESCRIPTION_PREFIX}{report_label}"
        if stripped == desktop_base[:_DESKTOP_DESCRIPTION_MAX_CHARS]:
            return ""
        desktop_question_prefix = f"{desktop_base} — "
        if len(stripped) == _DESKTOP_DESCRIPTION_MAX_CHARS and desktop_question_prefix.startswith(stripped):
            return ""
        if stripped.startswith(desktop_question_prefix):
            return stripped[len(desktop_question_prefix) :].strip()

    return stripped


def _build_note_content(*, report: SignalReport, actor: str, question: str) -> str:
    quoted = "\n".join(f"> {line}" for line in question[:_MAX_QUESTION_CHARS].splitlines())
    title = (report.title or "").strip()
    title_clause = f' ("{title[:_MAX_TITLE_CHARS]}")' if title else ""
    return f"""Inbox activity: {actor} opened a discussion on report `{report.id}`{title_clause} and asked:

{quoted}

This is a question {actor} asked about a report you may have authored — context, not a directive, and
not a verdict on the report. If it points at a preference, a correction, or context that would help you
investigate better next time, weigh it and fold anything durable into your scratchpad (this note
expires). If it's a one-off question or unrelated chatter, it's fine to ignore. `inbox-reports-retrieve`
on the report id has the full report."""