-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Core: Agentic observability for vitest and ghost stories #34537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yannbf
merged 27 commits into
project/sb-agentic-setup
from
sidnioulz/agentic-telemetry-ws2
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
993868a
docs: add agent vitest telemetry implementation plan
Sidnioulz 473bd11
refactor: extract shared test result types from ghost-stories
Sidnioulz ff2b625
refactor: extract shared test analysis logic from ghost-stories
Sidnioulz 2acb09d
refactor: rename render analysis gate to support both ghost stories a…
Sidnioulz 16beee4
feat: add agent-test-run telemetry event type
Sidnioulz 983c0d1
feat: add AgentTelemetryReporter for vitest CLI agent runs
Sidnioulz 60762e8
feat: inject AgentTelemetryReporter when agent detected in vitest CLI
Sidnioulz c46a39a
fix error message extraction logic
yannbf fbfdfdf
improve error categorization
yannbf a3e829f
add isWithinInitialSession utility to easily check session windows
yannbf ca96d8d
remove ghostStories global
yannbf 0834932
remove duplicated tsconfig json key
yannbf 656cdea
Merge branch 'sidnioulz/agentic-telemetry-ws1' into sidnioulz/agentic…
yannbf cd66a6a
Merge remote-tracking branch 'origin/sidnioulz/agentic-telemetry-ws1'…
yannbf bacd3ce
fix initial session code
yannbf 6714b03
Revert "remove ghostStories global"
yannbf f0d0c94
Merge branch 'project/sb-agentic-setup' into sidnioulz/agentic-teleme…
yannbf 175b59c
dangling changes
yannbf 7cac0a4
fix reporting time
yannbf f694ac3
refactor ghost stories test result parsing
yannbf d437782
fix error message extraction
yannbf a8ab777
add --include and --exclude log flags to event log collector
yannbf 2089cea
add --no-metadata flag in event log collector
yannbf 054986e
Merge branch 'project/sb-agentic-setup' into sidnioulz/agentic-teleme…
yannbf 6d29099
account for PR feedback
yannbf 8a008b2
refactor ghost run logic
yannbf e81a5db
fix tests
yannbf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
code/addons/vitest/src/vitest-plugin/agent-telemetry-reporter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { AgentTelemetryReporter } from './agent-telemetry-reporter.ts'; | ||
|
|
||
| vi.mock('storybook/internal/telemetry', () => ({ | ||
| telemetry: vi.fn(), | ||
| isExampleStoryId: vi.fn( | ||
| (id: string) => | ||
| id.startsWith('example-button--') || | ||
| id.startsWith('example-header--') || | ||
| id.startsWith('example-page--') | ||
| ), | ||
| })); | ||
|
|
||
| const { telemetry } = await import('storybook/internal/telemetry'); | ||
|
|
||
| function createMockTestCase({ | ||
| storyId, | ||
| status, | ||
| reports = [], | ||
| errors = [], | ||
| }: { | ||
| storyId?: string; | ||
| status: 'passed' | 'failed' | 'pending'; | ||
| reports?: Array<{ type: string; result?: Record<string, unknown> }>; | ||
| errors?: Array<{ message: string; stack?: string }>; | ||
| }) { | ||
| return { | ||
| meta: () => ({ storyId, reports }), | ||
| result: () => ({ | ||
| state: status, | ||
| errors: status === 'failed' ? errors : [], | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| function createMockTestModules(testCounts: { passed: number; failed: number }) { | ||
| const tests: Array<{ result: () => { state: string } }> = []; | ||
| for (let i = 0; i < testCounts.passed; i++) { | ||
| tests.push({ result: () => ({ state: 'passed' }) }); | ||
| } | ||
| for (let i = 0; i < testCounts.failed; i++) { | ||
| tests.push({ result: () => ({ state: 'failed' }) }); | ||
| } | ||
| return [ | ||
| { | ||
| children: { | ||
| allTests: function* (filter?: string) { | ||
| for (const t of tests) { | ||
| if (!filter || t.result().state === filter) { | ||
| yield t; | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| errors: () => [], | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| describe('AgentTelemetryReporter', () => { | ||
| let reporter: AgentTelemetryReporter; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| reporter = new AgentTelemetryReporter({ | ||
| configDir: '.storybook', | ||
| agent: { name: 'claude' }, | ||
| }); | ||
| }); | ||
|
|
||
| describe('onTestCaseResult', () => { | ||
| it('should collect story test results', () => { | ||
| const testCase = createMockTestCase({ | ||
| storyId: 'my-story--primary', | ||
| status: 'passed', | ||
| }); | ||
| reporter.onTestCaseResult(testCase as any); | ||
| }); | ||
|
|
||
| it('should skip tests without storyId', () => { | ||
| const testCase = createMockTestCase({ | ||
| storyId: undefined, | ||
| status: 'passed', | ||
| }); | ||
| reporter.onTestCaseResult(testCase as any); | ||
| }); | ||
|
|
||
| it('should skip example story IDs', () => { | ||
| const testCase = createMockTestCase({ | ||
| storyId: 'example-button--primary', | ||
| status: 'passed', | ||
| }); | ||
| reporter.onTestCaseResult(testCase as any); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onTestRunEnd', () => { | ||
| it('should send telemetry with analysis of collected results', async () => { | ||
| reporter.onInit({ config: { watch: false } } as any); | ||
|
|
||
| reporter.onTestCaseResult(createMockTestCase({ storyId: 's1', status: 'passed' }) as any); | ||
| reporter.onTestCaseResult( | ||
| createMockTestCase({ | ||
| storyId: 's2', | ||
| status: 'failed', | ||
| errors: [{ message: 'Error: Module not found: foo' }], | ||
| }) as any | ||
| ); | ||
| reporter.onTestCaseResult( | ||
| createMockTestCase({ | ||
| storyId: 's3', | ||
| status: 'passed', | ||
| reports: [{ type: 'render-analysis', result: { emptyRender: true } }], | ||
| }) as any | ||
| ); | ||
|
|
||
| await reporter.onTestRunEnd(createMockTestModules({ passed: 2, failed: 1 }) as any, []); | ||
|
|
||
| expect(telemetry).toHaveBeenCalledWith( | ||
| 'ai-setup-self-healing-scoring', | ||
| expect.objectContaining({ | ||
| agent: { name: 'claude' }, | ||
| analysis: expect.objectContaining({ | ||
| total: 3, | ||
| passed: 2, | ||
| passedButEmptyRender: 1, | ||
| successRate: 0.67, | ||
| successRateWithoutEmptyRender: 0.33, | ||
| uniqueErrorCount: 1, | ||
| }), | ||
| unhandledErrorCount: 0, | ||
| watch: false, | ||
| }), | ||
| { configDir: '.storybook', stripMetadata: true } | ||
| ); | ||
| }); | ||
|
|
||
| it('should filter out example stories from analysis', async () => { | ||
| reporter.onInit({ config: { watch: false } } as any); | ||
|
|
||
| reporter.onTestCaseResult( | ||
| createMockTestCase({ storyId: 'my-story--primary', status: 'passed' }) as any | ||
| ); | ||
| reporter.onTestCaseResult( | ||
| createMockTestCase({ storyId: 'example-button--primary', status: 'passed' }) as any | ||
| ); | ||
|
|
||
| await reporter.onTestRunEnd(createMockTestModules({ passed: 2, failed: 0 }) as any, []); | ||
|
|
||
| expect(telemetry).toHaveBeenCalledWith( | ||
| 'ai-setup-self-healing-scoring', | ||
| expect.objectContaining({ | ||
| analysis: expect.objectContaining({ | ||
| total: 1, | ||
| passed: 1, | ||
| }), | ||
| }), | ||
| expect.anything() | ||
| ); | ||
| }); | ||
|
|
||
| it('should count unhandled errors', async () => { | ||
| reporter.onInit({ config: { watch: false } } as any); | ||
|
|
||
| await reporter.onTestRunEnd( | ||
| createMockTestModules({ passed: 0, failed: 0 }) as any, | ||
| [{ message: 'unhandled' }, { message: 'another' }] as any | ||
| ); | ||
|
|
||
| expect(telemetry).toHaveBeenCalledWith( | ||
| 'ai-setup-self-healing-scoring', | ||
| expect.objectContaining({ | ||
| unhandledErrorCount: 2, | ||
| }), | ||
| expect.anything() | ||
| ); | ||
| }); | ||
|
|
||
| it('should reset collected results after each run', async () => { | ||
| reporter.onInit({ config: { watch: false } } as any); | ||
|
|
||
| reporter.onTestCaseResult(createMockTestCase({ storyId: 's1', status: 'passed' }) as any); | ||
| await reporter.onTestRunEnd(createMockTestModules({ passed: 1, failed: 0 }) as any, []); | ||
|
|
||
| reporter.onTestCaseResult( | ||
| createMockTestCase({ | ||
| storyId: 's2', | ||
| status: 'failed', | ||
| errors: [{ message: 'err' }], | ||
| }) as any | ||
| ); | ||
| await reporter.onTestRunEnd(createMockTestModules({ passed: 0, failed: 1 }) as any, []); | ||
|
|
||
| const secondCall = vi.mocked(telemetry).mock.calls[1]; | ||
| expect(secondCall[1]).toEqual( | ||
| expect.objectContaining({ | ||
| analysis: expect.objectContaining({ | ||
| total: 1, | ||
| passed: 0, | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
88 changes: 88 additions & 0 deletions
88
code/addons/vitest/src/vitest-plugin/agent-telemetry-reporter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import type { SerializedError } from 'vitest'; | ||
| import type { TestCase, TestModule, Vitest } from 'vitest/node'; | ||
| import type { Reporter } from 'vitest/reporters'; | ||
|
|
||
| import type { TaskMeta } from '@vitest/runner'; | ||
| import type { Report } from 'storybook/preview-api'; | ||
| import { analyzeTestResults, toStoryTestResult } from 'storybook/internal/core-server'; | ||
| import type { StoryTestResult } from 'storybook/internal/core-server'; | ||
| import { isExampleStoryId, telemetry } from 'storybook/internal/telemetry'; | ||
| import type { AgentInfo } from 'storybook/internal/telemetry'; | ||
|
|
||
| interface AgentTelemetryReporterOptions { | ||
| configDir: string; | ||
| agent: AgentInfo; | ||
| } | ||
|
|
||
| export class AgentTelemetryReporter implements Reporter { | ||
| private ctx!: Vitest; | ||
|
|
||
| private testResults: StoryTestResult[] = []; | ||
|
|
||
| private startTime = Date.now(); | ||
|
|
||
| private configDir: string; | ||
|
|
||
| private agent: AgentInfo; | ||
|
|
||
| constructor(options: AgentTelemetryReporterOptions) { | ||
| this.configDir = options.configDir; | ||
| this.agent = options.agent; | ||
| } | ||
|
|
||
| onInit(ctx: Vitest) { | ||
| this.ctx = ctx; | ||
| } | ||
|
|
||
| onTestRunStart() { | ||
| this.startTime = Date.now(); | ||
| } | ||
|
|
||
| onTestCaseResult(testCase: TestCase) { | ||
| const { storyId, reports } = testCase.meta() as TaskMeta & | ||
| Partial<{ storyId: string; reports: Report[] }>; | ||
|
|
||
| if (!storyId || isExampleStoryId(storyId)) { | ||
| return; | ||
| } | ||
|
|
||
| const testResult = testCase.result(); | ||
| const result = toStoryTestResult({ | ||
| storyId, | ||
| statusRaw: testResult.state, | ||
| reports, | ||
| errors: testResult.errors, | ||
| }); | ||
|
|
||
| if (result) { | ||
| this.testResults.push(result); | ||
| } | ||
| } | ||
|
|
||
| async onTestRunEnd( | ||
| testModules: readonly TestModule[], | ||
| unhandledErrors: readonly SerializedError[] | ||
| ) { | ||
| const analysis = analyzeTestResults(this.testResults); | ||
| const duration = Date.now() - this.startTime; | ||
|
|
||
| const testModulesErrors = testModules.flatMap((t) => t.errors()); | ||
| const unhandledErrorCount = unhandledErrors.length + testModulesErrors.length; | ||
|
|
||
| // Fire and forget — same pattern as the existing test-run telemetry | ||
| telemetry( | ||
| 'ai-setup-self-healing-scoring', | ||
| { | ||
| agent: this.agent, | ||
| analysis, | ||
| unhandledErrorCount, | ||
| duration, | ||
| watch: this.ctx.config.watch, | ||
| }, | ||
| { configDir: this.configDir, stripMetadata: true } | ||
| ); | ||
|
|
||
| // Reset for next run (watch mode) | ||
| this.testResults = []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.