: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
fix(tasks): show artifacts in the web task page artifacts tab (#109691)
Shy Alter73167120953c
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsxindex 154b8c835167..52f0fda89b4a 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx+++ b/products/posthog_ai/frontend/scenes/TaskTracker/TaskRunArtifacts.stories.tsx@@ -23,6 +23,7 @@ import { taskRunArtifactsLogic } from './taskRunArtifactsLogic'const TASK_ID = 'task-trials'const RUN_ID = 'run-trials'+const EARLIER_RUN_ID = 'run-trials-earlier'const REPORT_MARKDOWN = `# Trial starts dropped at the plan picker@@ -155,9 +156,9 @@ const CONTENT_BY_PATH: Record<string, string> = {'tasks/artifacts/artifact-csv': WEEKS_CSV,}-function mockRun(artifacts: TaskRunArtifactResponseApi[]): TaskRun {+function mockRun(artifacts: TaskRunArtifactResponseApi[], id = RUN_ID): TaskRun {return {- id: RUN_ID,+ id,task: TASK_ID,stage: null,branch: null,@@ -241,11 +242,17 @@ const RUN_LOGS = [),].join('\n')-function taskMocks(artifacts: TaskRunArtifactResponseApi[]): {+/** With `resumed`, the files sit on an earlier run and the latest run has none, as after a resume. */+function taskMocks(+ artifacts: TaskRunArtifactResponseApi[],+ { resumed = false }: { resumed?: boolean } = {}+): {get: Record<string, MockSignature>post: Record<string, MockSignature>} {- const run = mockRun(artifacts)+ const run = mockRun(resumed ? [] : artifacts)+ const earlierRun = mockRun(resumed ? artifacts : [], EARLIER_RUN_ID)+ const runs = resumed ? [run, earlierRun] : [run]const task = mockTask(run)return {get: {@@ -255,17 +262,24 @@ function taskMocks(artifacts: TaskRunArtifactResponseApi[]): {return [200, { results, count: results.length, next: null, previous: null }]},[`/api/projects/:team_id/tasks/${TASK_ID}/`]: task,- [`/api/projects/:team_id/tasks/${TASK_ID}/runs/`]: { count: 1, next: null, previous: null, results: [run] },+ [`/api/projects/:team_id/tasks/${TASK_ID}/runs/`]: {+ count: runs.length,+ next: null,+ previous: null,+ // The real runs list carries no artifact manifests.+ results: runs.map(({ artifacts: _artifacts, ...rest }) => rest),+ },[`/api/projects/:team_id/tasks/${TASK_ID}/runs/${RUN_ID}/`]: run,+ [`/api/projects/:team_id/tasks/${TASK_ID}/runs/${EARLIER_RUN_ID}/`]: earlierRun,[`/api/projects/:team_id/tasks/${TASK_ID}/runs/${RUN_ID}/logs`]: () => new HttpResponse(RUN_LOGS),- [`/api/projects/:team_id/tasks/${TASK_ID}/runs/${RUN_ID}/artifacts/artifact-chart/download/`]: () =>+ [`/api/projects/:team_id/tasks/${TASK_ID}/runs/:run_id/artifacts/artifact-chart/download/`]: () =>new HttpResponse(CHART_SVG, { headers: { 'Content-Type': 'image/svg+xml' } }),'/api/projects/:team_id/task_channels/': [],'/api/projects/:team_id/integrations/': { results: [] },'/api/environments/:team_id/conversations/': { results: [], next: null },},post: {- [`/api/projects/:team_id/tasks/${TASK_ID}/runs/${RUN_ID}/artifacts/download/`]: async ({+ [`/api/projects/:team_id/tasks/${TASK_ID}/runs/:run_id/artifacts/download/`]: async ({request,}: {request: Request@@ -350,3 +364,8 @@ export const FlagOff: Story = {parameters: { featureFlags: [FEATURE_FLAGS.TASKS] },render: () => <StoryPage tab="conversation" />,}++export const ResumedTask: Story = {+ decorators: [mswDecorator(taskMocks(ARTIFACTS, { resumed: true }))],+ render: () => <StoryPage artifactId="artifact-report" />,+}diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsxindex 48be70a63924..7c6c105fe8b9 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunArtifacts.tsx@@ -17,12 +17,11 @@ import { dayjs } from 'lib/dayjs'import { LemonMarkdown } from 'lib/lemon-ui/LemonMarkdown'import { cn } from 'lib/utils/css-classes'-import type { TaskRunArtifactResponseApi } from 'products/tasks/frontend/generated/api.schemas'-import { withStrictCsp } from '../artifactHtml'import {ARTIFACT_KIND_LABEL,ArtifactPreviewKind,+ RunArtifact,TaskRunTab,artifactPreviewKind,formatArtifactSize,@@ -96,7 +95,7 @@ function ArtifactPreview({ taskId }: { taskId: string }): JSX.Element | null {return null}if (selectedKind === 'image') {- const src = artifactDownloadUrl(currentProjectId, selectedRun, selectedArtifact)+ const src = artifactDownloadUrl(currentProjectId, taskId, selectedArtifact)return (<div className="flex min-h-full items-center justify-center p-8">{src && (@@ -191,11 +190,11 @@ function ArtifactNav({ taskId }: { taskId: string }): JSX.Element {)}-function ArtifactToolbar({ taskId, artifact }: { taskId: string; artifact: TaskRunArtifactResponseApi }): JSX.Element {- const { artifacts, selectedIndex, selectedRun, currentProjectId } = useValues(taskRunArtifactsLogic({ taskId }))+function ArtifactToolbar({ taskId, artifact }: { taskId: string; artifact: RunArtifact }): JSX.Element {+ const { artifacts, selectedIndex, currentProjectId } = useValues(taskRunArtifactsLogic({ taskId }))const { stepArtifact, downloadArtifact } = useActions(taskRunArtifactsLogic({ taskId }))const kind = artifactPreviewKind(artifact)- const downloadUrl = artifactDownloadUrl(currentProjectId, selectedRun, artifact)+ const downloadUrl = artifactDownloadUrl(currentProjectId, taskId, artifact)const single = artifacts.length < 2return (<div className="flex shrink-0 flex-wrap items-center gap-2 border-b border-primary px-3 py-1.5">diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.tsindex 41603f6b714c..7670699b2488 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.test.ts@@ -1,6 +1,6 @@import type { TaskRunArtifactResponseApi } from 'products/tasks/frontend/generated/api.schemas'-import { artifactPreviewKind, parseCsv, visibleRunArtifacts } from './taskRunArtifacts'+import { artifactPreviewKind, collectRunArtifacts, parseCsv, visibleRunArtifacts } from './taskRunArtifacts'function artifact(overrides: Partial<TaskRunArtifactResponseApi>): TaskRunArtifactResponseApi {return {@@ -62,4 +62,25 @@ describe('taskRunArtifacts', () => {]expect(visibleRunArtifacts(artifacts)).toEqual([kept])})++ it('collectRunArtifacts keeps files from earlier runs of a resumed task', () => {+ const latest = { id: 'run-3', artifacts: [artifact({ id: 'chart', uploaded_at: '2026-09-30T19:00:00Z' })] }+ const earlier = {+ id: 'run-1',+ artifacts: [+ artifact({ id: 'report', uploaded_at: '2026-09-30T17:00:00Z' }),+ artifact({ id: 'chart', uploaded_at: '2026-09-30T18:00:00Z' }),+ ],+ }+ expect(+ collectRunArtifacts([latest, null, earlier]).map(({ id, runId, uploaded_at }) => ({+ id,+ runId,+ uploaded_at,+ }))+ ).toEqual([+ { id: 'chart', runId: 'run-3', uploaded_at: '2026-09-30T19:00:00Z' },+ { id: 'report', runId: 'run-1', uploaded_at: '2026-09-30T17:00:00Z' },+ ])+ })})diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.tsindex e14c6dfeba80..4792822b0225 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifacts.ts@@ -103,3 +103,32 @@ export function parseCsv(text: string): string[][] {}return rows.filter((cells) => cells.some((cell) => cell !== ''))}++/** A file the agent wrote, with the run that holds it. Downloads must name that run, not the open one. */+export interface RunArtifact extends TaskRunArtifactResponseApi {+ runId: string+}++interface RunWithArtifacts {+ id: string+ artifacts?: readonly TaskRunArtifactResponseApi[] | null+}++/**+ * Agent files from every run of a task. A resumed task keeps writing new runs, so the files from earlier+ * runs in the chain are only on those runs. The first run that lists an id wins, so pass the live run first.+ */+export function collectRunArtifacts(runs: readonly (RunWithArtifacts | null | undefined)[]): RunArtifact[] {+ const byId = new Map<string, RunArtifact>()+ for (const run of runs) {+ if (!run) {+ continue+ }+ for (const artifact of visibleRunArtifacts(run.artifacts ?? [])) {+ if (artifact.id && !byId.has(artifact.id)) {+ byId.set(artifact.id, { ...artifact, runId: run.id })+ }+ }+ }+ return [...byId.values()].sort((a, b) => b.uploaded_at.localeCompare(a.uploaded_at))+}diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.tsindex a4ebb71bfdbb..94f12fa2d763 100644--- a/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts+++ b/products/posthog_ai/frontend/scenes/TaskTracker/taskRunArtifactsLogic.ts@@ -1,4 +1,4 @@-import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'+import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'import posthog from 'posthog-js'@@ -8,16 +8,19 @@ import { projectLogic } from 'scenes/projectLogic'import {getTasksRunsArtifactsDownloadCreateUrl,getTasksRunsArtifactsDownloadRetrieveUrl,+ tasksRunsRetrieve,} from 'products/tasks/frontend/generated/api'-import type { TaskRunArtifactResponseApi, TaskRunDetailDTOApi } from 'products/tasks/frontend/generated/api.schemas'+import type { TaskRunDetailDTOApi } from 'products/tasks/frontend/generated/api.schemas'+import type { TaskRun } from '../../types/taskTypes'import { taskDetailSceneLogic } from './taskDetailSceneLogic'import {ArtifactPreviewKind,+ RunArtifact,TaskRunTab,artifactPreviewKind,+ collectRunArtifacts,isTextPreview,- visibleRunArtifacts,} from './taskRunArtifacts'export interface TaskRunArtifactsLogicProps {@@ -33,12 +36,15 @@ export interface ArtifactText {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface taskRunArtifactsLogicValues {currentProjectId: number | null // projectLogic+ runs: TaskRun[] // taskDetailSceneLogicselectedRun: TaskRunDetailDTOApi | null // taskDetailSceneLogicactiveTab: TaskRunTabartifactText: ArtifactText | nullartifactTextLoading: boolean- artifacts: TaskRunArtifactResponseApi[]- selectedArtifact: TaskRunArtifactResponseApi | null+ artifacts: RunArtifact[]+ chainRuns: TaskRunDetailDTOApi[]+ chainRunsLoading: boolean+ selectedArtifact: RunArtifact | nullselectedArtifactId: string | nullselectedIndex: numberselectedKind: ArtifactPreviewKind | null@@ -48,13 +54,20 @@ export interface taskRunArtifactsLogicValues {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface taskRunArtifactsLogicActions {- downloadArtifact: (artifact: TaskRunArtifactResponseApi) => {- artifact: TaskRunArtifactResponseApi+ loadTaskRunsSuccess: (+ runs: TaskRun[],+ payload?: any+ ) => {+ payload?: any+ runs: TaskRun[]+ } // taskDetailSceneLogic+ downloadArtifact: (artifact: RunArtifact) => {+ artifact: RunArtifact}ensureSelectedText: () => {value: true}- loadArtifactText: (artifact: TaskRunArtifactResponseApi) => TaskRunArtifactResponseApi+ loadArtifactText: (artifact: RunArtifact) => RunArtifactloadArtifactTextFailure: (error: string,errorObject?: any@@ -64,10 +77,25 @@ export interface taskRunArtifactsLogicActions {}loadArtifactTextSuccess: (artifactText: ArtifactText | null,- payload?: TaskRunArtifactResponseApi+ payload?: RunArtifact) => {artifactText: ArtifactText | null- payload?: TaskRunArtifactResponseApi+ payload?: RunArtifact+ }+ loadChainRuns: (runIds: string[]) => string[]+ loadChainRunsFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ loadChainRunsSuccess: (+ chainRuns: TaskRunDetailDTOApi[],+ payload?: string[]+ ) => {+ chainRuns: TaskRunDetailDTOApi[]+ payload?: string[]}selectArtifact: (artifactId: string) => {artifactId: string@@ -84,15 +112,12 @@ export interface taskRunArtifactsLogicActions {export interface taskRunArtifactsLogicMeta {key: string__keaTypeGenInternalSelectorTypes: {- artifacts: (selectedRun: TaskRunDetailDTOApi | null) => TaskRunArtifactResponseApi[]- selectedIndex: (artifacts: TaskRunArtifactResponseApi[], selectedArtifactId: string | null) => number- selectedArtifact: (- artifacts: TaskRunArtifactResponseApi[],- selectedIndex: number- ) => TaskRunArtifactResponseApi | null- selectedKind: (selectedArtifact: TaskRunArtifactResponseApi | null) => ArtifactPreviewKind | null+ artifacts: (selectedRun: TaskRunDetailDTOApi | null, chainRuns: TaskRunDetailDTOApi[]) => RunArtifact[]+ selectedIndex: (artifacts: RunArtifact[], selectedArtifactId: string | null) => number+ selectedArtifact: (artifacts: RunArtifact[], selectedIndex: number) => RunArtifact | null+ selectedKind: (selectedArtifact: RunArtifact | null) => ArtifactPreviewKind | nullselectedText: (- selectedArtifact: TaskRunArtifactResponseApi | null,+ selectedArtifact: RunArtifact | null,textsById: Record<string, ArtifactText>) => ArtifactText | null}@@ -105,16 +130,15 @@ export type taskRunArtifactsLogicType = MakeLogicType<taskRunArtifactsLogicMeta>+// A long-lived task can have many runs. Older runs past this cap keep their files out of the list.+const MAX_CHAIN_RUNS = 20+/** The download-by-id URL redirects to a fresh presigned link, so an `img` or an `a` can use it directly. */-export function artifactDownloadUrl(- projectId: number | null,- run: TaskRunDetailDTOApi | null,- artifact: TaskRunArtifactResponseApi-): string | null {- if (projectId === null || !run || !artifact.id) {+export function artifactDownloadUrl(projectId: number | null, taskId: string, artifact: RunArtifact): string | null {+ if (projectId === null || !artifact.id) {return null}- return getTasksRunsArtifactsDownloadRetrieveUrl(String(projectId), run.task, run.id, artifact.id)+ return getTasksRunsArtifactsDownloadRetrieveUrl(String(projectId), taskId, artifact.runId, artifact.id)}export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([@@ -122,22 +146,45 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([key((props) => props.taskId),path((key) => ['products', 'posthog_ai', 'frontend', 'scenes', 'TaskTracker', 'taskRunArtifactsLogic', key]),connect((props: TaskRunArtifactsLogicProps) => ({- values: [taskDetailSceneLogic({ taskId: props.taskId }), ['selectedRun'], projectLogic, ['currentProjectId']],+ values: [+ taskDetailSceneLogic({ taskId: props.taskId }),+ ['selectedRun', 'runs'],+ projectLogic,+ ['currentProjectId'],+ ],+ actions: [taskDetailSceneLogic({ taskId: props.taskId }), ['loadTaskRunsSuccess']],})),actions({setActiveTab: (tab: TaskRunTab) => ({ tab }),selectArtifact: (artifactId: string) => ({ artifactId }),stepArtifact: (delta: number) => ({ delta }),- downloadArtifact: (artifact: TaskRunArtifactResponseApi) => ({ artifact }),+ downloadArtifact: (artifact: RunArtifact) => ({ artifact }),ensureSelectedText: true,}),- loaders(({ values }) => ({+ loaders(({ props, values }) => ({+ chainRuns: [+ [] as TaskRunDetailDTOApi[],+ {+ // The runs list carries no artifact manifests, so each run's detail is read to find its files.+ loadChainRuns: async (runIds: string[]): Promise<TaskRunDetailDTOApi[]> => {+ if (values.currentProjectId === null) {+ return values.chainRuns+ }+ const projectId = String(values.currentProjectId)+ const runs = await Promise.all(+ runIds+ .slice(0, MAX_CHAIN_RUNS)+ .map((runId) => tasksRunsRetrieve(projectId, props.taskId, runId).catch(() => null))+ )+ return runs.filter((run): run is TaskRunDetailDTOApi => run !== null)+ },+ },+ ],artifactText: [null as ArtifactText | null,{- loadArtifactText: async (artifact: TaskRunArtifactResponseApi): Promise<ArtifactText | null> => {- const run = values.selectedRun- if (values.currentProjectId === null || !run || !artifact.id || !artifact.storage_path) {+ loadArtifactText: async (artifact: RunArtifact): Promise<ArtifactText | null> => {+ if (values.currentProjectId === null || !artifact.id || !artifact.storage_path) {return null}// The POST download streams the bytes from the app origin, so reading them needs no CORS@@ -145,7 +192,11 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([try {// nosemgrep: prefer-codegen-api -- A file download: the generated function parses the body as JSON.const response = await api.createResponse(- getTasksRunsArtifactsDownloadCreateUrl(String(values.currentProjectId), run.task, run.id),+ getTasksRunsArtifactsDownloadCreateUrl(+ String(values.currentProjectId),+ props.taskId,+ artifact.runId+ ),{ storage_path: artifact.storage_path })return { artifactId: artifact.id, text: await response.text(), error: null }@@ -173,13 +224,13 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([}),selectors({artifacts: [- (s) => [s.selectedRun],- (selectedRun: TaskRunDetailDTOApi | null): TaskRunArtifactResponseApi[] =>- visibleRunArtifacts(selectedRun?.artifacts ?? []),+ (s) => [s.selectedRun, s.chainRuns],+ (selectedRun: TaskRunDetailDTOApi | null, chainRuns: TaskRunDetailDTOApi[]): RunArtifact[] =>+ collectRunArtifacts([selectedRun, ...chainRuns]),],selectedIndex: [(s) => [s.artifacts, s.selectedArtifactId],- (artifacts: TaskRunArtifactResponseApi[], selectedArtifactId: string | null): number =>+ (artifacts: RunArtifact[], selectedArtifactId: string | null): number =>Math.max(0,artifacts.findIndex((artifact) => artifact.id === selectedArtifactId)@@ -187,20 +238,17 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([],selectedArtifact: [(s) => [s.artifacts, s.selectedIndex],- (artifacts: TaskRunArtifactResponseApi[], selectedIndex: number): TaskRunArtifactResponseApi | null =>- artifacts[selectedIndex] ?? null,+ (artifacts: RunArtifact[], selectedIndex: number): RunArtifact | null => artifacts[selectedIndex] ?? null,],selectedKind: [(s) => [s.selectedArtifact],- (selectedArtifact: TaskRunArtifactResponseApi | null): ArtifactPreviewKind | null =>+ (selectedArtifact: RunArtifact | null): ArtifactPreviewKind | null =>selectedArtifact ? artifactPreviewKind(selectedArtifact) : null,],selectedText: [(s) => [s.selectedArtifact, s.textsById],- (- selectedArtifact: TaskRunArtifactResponseApi | null,- textsById: Record<string, ArtifactText>- ): ArtifactText | null => (selectedArtifact?.id && textsById[selectedArtifact.id]) || null,+ (selectedArtifact: RunArtifact | null, textsById: Record<string, ArtifactText>): ArtifactText | null =>+ (selectedArtifact?.id && textsById[selectedArtifact.id]) || null,],}),listeners(({ actions, values }) => {@@ -226,11 +274,16 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([setActiveTab: ({ tab }) => {if (tab === 'artifacts') {posthog.capture('task artifacts tab opened', { artifact_count: values.artifacts.length })+ // The agent can upload a file after the page loads, so read the manifests again.+ actions.loadChainRuns(values.runs.map((run) => run.id))}previewSelected()},selectArtifact: previewSelected,ensureSelectedText: loadSelectedText,+ loadTaskRunsSuccess: ({ runs }) => {+ actions.loadChainRuns(runs.map((run) => run.id))+ },stepArtifact: ({ delta }) => {const { artifacts, selectedIndex } = valuesconst next = artifacts[(selectedIndex + delta + artifacts.length) % artifacts.length]@@ -243,4 +296,9 @@ export const taskRunArtifactsLogic = kea<taskRunArtifactsLogicType>([},}}),+ afterMount(({ actions, values }) => {+ if (values.runs.length > 0) {+ actions.loadChainRuns(values.runs.map((run) => run.id))+ }+ }),])