: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tasks): add data helpers for artifact comments (#110449)
Shy Alter865031c12394
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.test.ts b/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.test.tsnew file mode 100644index 000000000000..10a702c89090--- /dev/null+++ b/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.test.ts@@ -0,0 +1,109 @@+import type { CommentApi } from 'products/platform_features/frontend/generated/api.schemas'++import {+ TextCommentAnchor,+ buildArtifactCommentThreads,+ createTextCommentAnchor,+ resolveTextCommentAnchor,+} from './artifactComments'++function comment(overrides: Partial<CommentApi>): CommentApi {+ return {+ id: 'comment-1',+ created_by: null,+ completed_by: null,+ slack_thread: null,+ version: 0,+ created_at: '2026-09-30T18:00:00Z',+ completed_at: null,+ content: 'Looks good',+ scope: 'task_artifact',+ item_id: 'artifact-1',+ source_comment: null,+ ...overrides,+ }+}++describe('artifactComments', () => {+ it('reads thread state from Desktop resolve and reopen records and keeps them out of the replies', () => {+ const threads = buildArtifactCommentThreads([+ comment({+ id: 'root-pin',+ item_context: { anchor: { kind: 'region', x: 0.2, y: 0.3, width: 0.035, height: 0.035 } },+ }),+ comment({ id: 'reply', source_comment: 'root-pin', created_at: '2026-09-30T18:01:00Z' }),+ comment({+ id: 'resolve',+ source_comment: 'root-pin',+ content: 'Resolved this thread',+ created_at: '2026-09-30T18:02:00Z',+ item_context: { threadState: 'resolved' },+ }),+ comment({+ id: 'reopen',+ source_comment: 'root-pin',+ content: 'Reopened this thread',+ created_at: '2026-09-30T18:03:00Z',+ item_context: { threadState: 'open' },+ }),+ comment({+ id: 'root-doc',+ created_at: '2026-09-30T18:04:00Z',+ item_context: { anchor: { kind: 'document' } },+ completed_at: '2026-09-30T18:05:00Z',+ }),+ ])++ expect(+ threads.map(({ root, replies, resolved, pinNumber }) => ({+ id: root.id,+ replies: replies.map((reply) => reply.id),+ resolved,+ pinNumber,+ }))+ ).toEqual([+ { id: 'root-pin', replies: ['reply'], resolved: false, pinNumber: 1 },+ { id: 'root-doc', replies: [], resolved: true, pinNumber: null },+ ])+ })++ const DOCUMENT = 'Trial starts fell. The plan picker lost people. Fix the plan picker first.'+ const anchorFor = (quote: string, occurrence = 0): TextCommentAnchor => {+ let start = -1+ for (let index = 0; index <= occurrence; index++) {+ start = DOCUMENT.indexOf(quote, start + 1)+ }+ return createTextCommentAnchor(DOCUMENT, start, start + quote.length)!+ }++ it.each([+ {+ name: 'keeps the stored offsets when they still match',+ text: DOCUMENT,+ anchor: anchorFor('plan picker', 1),+ expected: { start: DOCUMENT.lastIndexOf('plan picker'), status: 'exact' },+ },+ {+ // Desktop renders markdown with a different component, so its offsets land elsewhere here.+ name: 'finds the quote again when the text before it changed',+ text: `# Report\n\n${DOCUMENT}`,+ anchor: anchorFor('Trial starts fell'),+ expected: { start: '# Report\n\n'.length, status: 'reanchored' },+ },+ {+ name: 'picks the repeat whose surrounding text matches',+ text: `Summary. ${DOCUMENT}`,+ anchor: anchorFor('plan picker', 1),+ expected: { start: `Summary. ${DOCUMENT}`.lastIndexOf('plan picker'), status: 'reanchored' },+ },+ {+ name: 'gives up on a repeat it cannot tell apart',+ text: 'plan picker and plan picker',+ anchor: { kind: 'text', quote: 'plan picker', prefix: 'x', suffix: 'y', start: 40, end: 51 },+ expected: null,+ },+ ] as const)('$name', ({ text, anchor, expected }) => {+ const resolved = resolveTextCommentAnchor(text, anchor as TextCommentAnchor)+ expect(resolved && { start: resolved.start, status: resolved.status }).toEqual(expected)+ })+})diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.ts b/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.tsnew file mode 100644index 000000000000..e046d7b82b8e--- /dev/null+++ b/products/posthog_ai/frontend/scenes/TaskTracker/artifactComments.ts@@ -0,0 +1,233 @@+import type { CommentApi } from 'products/platform_features/frontend/generated/api.schemas'++import type { ArtifactPreviewKind, RunArtifact } from './taskRunArtifacts'++// Artifact comments are ordinary PostHog comments with scope "task_artifact" and the artifact version id as+// item_id. PostHog Desktop writes the same rows with the same item_context shape, so both apps show each+// other's threads, pins and highlights.++// pinned: comment scope for task artifacts, shared with PostHog Desktop and the comments API+export const ARTIFACT_COMMENT_SCOPE = 'task_artifact'++// The same limits Desktop uses, so an anchor written here also passes Desktop's schema.+const CONTEXT_LENGTH = 32+const MAX_QUOTE_LENGTH = 10_000+// The side of a new pin's region, as a share of the image width and height.+const PIN_REGION_SIZE = 0.035++export interface TextCommentAnchor {+ kind: 'text'+ quote: string+ prefix: string+ suffix: string+ start: number+ end: number+}++/** A box on an image. Every value is a share of the image size, from 0 to 1. */+export interface RegionCommentAnchor {+ kind: 'region'+ x: number+ y: number+ width: number+ height: number+}++export interface DocumentCommentAnchor {+ kind: 'document'+}++export type ArtifactCommentAnchor = TextCommentAnchor | RegionCommentAnchor | DocumentCommentAnchor++/** The anchor names in analytics. They match the words the UI uses. */+export type ArtifactCommentAnchorKind = 'document' | 'image' | 'selection'++export type ThreadState = 'resolved' | 'open'++export interface ArtifactCommentContext {+ anchor: ArtifactCommentAnchor | null+ threadState: ThreadState | null+}++export interface ArtifactCommentThread {+ root: CommentApi+ /** Replies people wrote, oldest first. Resolve and reopen records stay out. */+ replies: CommentApi[]+ resolved: boolean+ anchor: ArtifactCommentAnchor | null+ /** Set on a region thread: its number among the image's pins, from 1, in the order people placed them. */+ pinNumber: number | null+}++/** Where a text range sits in a document. `status` is `reanchored` when the text moved since the comment. */+export interface ResolvedTextAnchor {+ start: number+ end: number+ status: 'exact' | 'reanchored'+}++export function anchorKindLabel(anchor: ArtifactCommentAnchor | null): ArtifactCommentAnchorKind {+ return anchor?.kind === 'region' ? 'image' : anchor?.kind === 'text' ? 'selection' : 'document'+}++/** Comments need a stable artifact id the server can find on the task. Cited objects and living documents have none. */+export function isCommentableArtifact(artifact: RunArtifact | null): artifact is RunArtifact & { id: string } {+ return !!artifact?.id && artifact.type !== 'reference' && !artifact.living+}++/** Kinds where a person can select text in the rendered view and comment on it. */+export function supportsSelectionComments(kind: ArtifactPreviewKind | null): boolean {+ return kind === 'markdown' || kind === 'text'+}++function isShare(value: unknown): value is number {+ return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 1+}++function readAnchor(value: unknown): ArtifactCommentAnchor | null {+ if (!value || typeof value !== 'object') {+ return null+ }+ const anchor = value as Record<string, unknown>+ if (anchor.kind === 'document') {+ return { kind: 'document' }+ }+ if (anchor.kind === 'region') {+ const { x, y, width, height } = anchor+ return isShare(x) && isShare(y) && isShare(width) && isShare(height)+ ? { kind: 'region', x, y, width, height }+ : null+ }+ if (anchor.kind === 'text') {+ const { quote, prefix, suffix, start, end } = anchor+ if (+ typeof quote !== 'string' ||+ !quote ||+ typeof prefix !== 'string' ||+ typeof suffix !== 'string' ||+ !Number.isInteger(start) ||+ !Number.isInteger(end) ||+ (start as number) < 0 ||+ (end as number) <= (start as number)+ ) {+ return null+ }+ return { kind: 'text', quote, prefix, suffix, start: start as number, end: end as number }+ }+ return null+}++/** The generated client types `item_context` as unknown, so each field is checked before use. */+export function readArtifactCommentContext(comment: Pick<CommentApi, 'item_context'>): ArtifactCommentContext {+ const context =+ comment.item_context && typeof comment.item_context === 'object'+ ? (comment.item_context as Record<string, unknown>)+ : {}+ const threadState =+ context.threadState === 'resolved' || context.threadState === 'open' ? context.threadState : null+ return { anchor: readAnchor(context.anchor), threadState }+}++function bySentAt(a: CommentApi, b: CommentApi): number {+ return a.created_at.localeCompare(b.created_at) || a.id.localeCompare(b.id)+}++/** Groups a flat comment list into threads, oldest root first. */+export function buildArtifactCommentThreads(comments: readonly CommentApi[]): ArtifactCommentThread[] {+ const roots: CommentApi[] = []+ const repliesByRoot = new Map<string, CommentApi[]>()+ for (const comment of comments) {+ if (comment.deleted) {+ continue+ }+ if (!comment.source_comment) {+ roots.push(comment)+ continue+ }+ const replies = repliesByRoot.get(comment.source_comment) ?? []+ replies.push(comment)+ repliesByRoot.set(comment.source_comment, replies)+ }+ let pins = 0+ return roots.sort(bySentAt).map((root) => {+ const all = (repliesByRoot.get(root.id) ?? []).sort(bySentAt)+ // The newest resolve or reopen record wins. A thread without one follows the root's completion.+ const latestState = all+ .map((reply) => readArtifactCommentContext(reply).threadState)+ .filter((state): state is ThreadState => state !== null)+ .at(-1)+ const anchor = readArtifactCommentContext(root).anchor+ return {+ root,+ replies: all.filter((reply) => readArtifactCommentContext(reply).threadState === null),+ resolved: latestState ? latestState === 'resolved' : !!root.completed_at,+ anchor,+ pinNumber: anchor?.kind === 'region' ? ++pins : null,+ }+ })+}++/** The anchor for a text range of `text`, or null when the range holds no visible characters. */+export function createTextCommentAnchor(text: string, start: number, end: number): TextCommentAnchor | null {+ const safeStart = Math.max(0, Math.min(start, text.length))+ const safeEnd = Math.max(safeStart, Math.min(end, text.length))+ const quote = text.slice(safeStart, safeEnd)+ if (!quote.trim() || quote.length > MAX_QUOTE_LENGTH) {+ return null+ }+ return {+ kind: 'text',+ quote,+ prefix: text.slice(Math.max(0, safeStart - CONTEXT_LENGTH), safeStart),+ suffix: text.slice(safeEnd, safeEnd + CONTEXT_LENGTH),+ start: safeStart,+ end: safeEnd,+ }+}++/**+ * Finds a stored quote in `text` without a guess. The stored offsets are checked first. Desktop renders+ * markdown with a different component, so its offsets can be off here, and the quote is searched for then.+ * The prefix and suffix pick between repeats of the quote. A tie returns null, so a highlight never lands+ * on the wrong words.+ */+export function resolveTextCommentAnchor(text: string, anchor: TextCommentAnchor): ResolvedTextAnchor | null {+ if (text.slice(anchor.start, anchor.end) === anchor.quote) {+ return { start: anchor.start, end: anchor.end, status: 'exact' }+ }+ const candidates: number[] = []+ // Matches do not overlap, the same as Desktop, so both apps pick the same repeat of a quote.+ for (+ let match = text.indexOf(anchor.quote);+ match >= 0;+ match = text.indexOf(anchor.quote, match + anchor.quote.length)+ ) {+ candidates.push(match)+ }+ if (candidates.length === 0) {+ return null+ }+ const ranked = candidates+ .map((start) => {+ const end = start + anchor.quote.length+ let score = 0+ if (anchor.prefix && text.slice(Math.max(0, start - anchor.prefix.length), start) === anchor.prefix) {+ score += 2+ }+ if (anchor.suffix && text.slice(end, end + anchor.suffix.length) === anchor.suffix) {+ score += 2+ }+ return { start, score }+ })+ .sort((a, b) => b.score - a.score)+ if (ranked.length > 1 && (ranked[0].score === 0 || ranked[0].score === ranked[1].score)) {+ return null+ }+ return { start: ranked[0].start, end: ranked[0].start + anchor.quote.length, status: 'reanchored' }+}++/** A pin region centered on a click, as shares of the image size. The region stays inside the image. */+export function regionAnchorAt(x: number, y: number): RegionCommentAnchor {+ const clamp = (value: number): number => Math.max(0, Math.min(1 - PIN_REGION_SIZE, value - PIN_REGION_SIZE / 2))+ return { kind: 'region', x: clamp(x), y: clamp(y), width: PIN_REGION_SIZE, height: PIN_REGION_SIZE }+}