Repository navigation
feat(cloud-agent): emit flat agent and code-review health events #6906
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
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a98ffe4
feat(db): record cloud agent product origin and index review outcomes
eshurakov f3e2e41
feat(cloud-agent): emit flat five-minute health events
eshurakov ed3a119
feat(web): emit flat code review health events
eshurakov ca135ca
docs: define agent and review health event contract
eshurakov be3292d
chore: keep agent health plan out of PR
eshurakov da8f980
refactor(web): compute review pending age in SQL and drop dead wait c…
eshurakov 0508e90
test: make health-event assertions falsifiable
eshurakov 6fe54a0
refactor(db): drop cloud agent session product_origin column
eshurakov e9e1b12
refactor(cloud-agent): bucket health origin from cli_sessions_v2 crea…
eshurakov 853a894
test(cloud-agent): cover created_on_platform origin bucketing
eshurakov 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
91 changes: 91 additions & 0 deletions
91
apps/web/src/app/api/cron/code-review-outcome-aggregate/route.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,91 @@ | ||
| import { NextRequest } from 'next/server'; | ||
|
|
||
| jest.mock('@/lib/config.server', () => ({ | ||
| CRON_SECRET: 'cron-secret', | ||
| })); | ||
|
|
||
| jest.mock('@/lib/code-reviews/telemetry/review-health-aggregate', () => ({ | ||
| collectCodeReviewOutcome: jest.fn(), | ||
| collectCodeReviewOpenStock: jest.fn(), | ||
| })); | ||
|
|
||
| import { | ||
| collectCodeReviewOpenStock, | ||
| collectCodeReviewOutcome, | ||
| } from '@/lib/code-reviews/telemetry/review-health-aggregate'; | ||
| import { GET } from './route'; | ||
|
|
||
| const outcomeMock = jest.mocked(collectCodeReviewOutcome); | ||
| const openStockMock = jest.mocked(collectCodeReviewOpenStock); | ||
|
|
||
| const request = (authorization?: string) => | ||
| new NextRequest('http://localhost:3000/api/cron/code-review-outcome-aggregate', { | ||
| method: 'GET', | ||
| headers: authorization ? { authorization } : undefined, | ||
| }); | ||
|
|
||
| describe('GET /api/cron/code-review-outcome-aggregate', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| outcomeMock.mockResolvedValue('complete'); | ||
| openStockMock.mockResolvedValue('complete'); | ||
| }); | ||
|
|
||
| it('returns 401 without collecting when authorization is missing', async () => { | ||
| const response = await GET(request()); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(outcomeMock).not.toHaveBeenCalled(); | ||
| expect(openStockMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns 401 without collecting when authorization is wrong', async () => { | ||
| const response = await GET(request('Bearer wrong-secret')); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(outcomeMock).not.toHaveBeenCalled(); | ||
| expect(openStockMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('collects the outcome then the open stock and returns only collectionStatus', async () => { | ||
| const response = await GET(request('Bearer cron-secret')); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| await expect(response.json()).resolves.toEqual({ collectionStatus: 'complete' }); | ||
| expect(outcomeMock).toHaveBeenCalledTimes(1); | ||
| expect(openStockMock).toHaveBeenCalledTimes(1); | ||
| expect(outcomeMock.mock.invocationCallOrder[0]).toBeLessThan( | ||
| openStockMock.mock.invocationCallOrder[0] | ||
| ); | ||
| }); | ||
|
|
||
| it('reports failed when either collection fails', async () => { | ||
| outcomeMock.mockResolvedValue('failed'); | ||
|
|
||
| const response = await GET(request('Bearer cron-secret')); | ||
|
|
||
| await expect(response.json()).resolves.toEqual({ collectionStatus: 'failed' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('route module without CRON_SECRET', () => { | ||
| const isolatedOutcomeMock = jest.fn(); | ||
| const isolatedOpenStockMock = jest.fn(); | ||
|
|
||
| it('imports and returns 401 without collecting', async () => { | ||
| jest.resetModules(); | ||
| jest.doMock('@/lib/config.server', () => ({ CRON_SECRET: '' })); | ||
| jest.doMock('@/lib/code-reviews/telemetry/review-health-aggregate', () => ({ | ||
| collectCodeReviewOutcome: isolatedOutcomeMock, | ||
| collectCodeReviewOpenStock: isolatedOpenStockMock, | ||
| })); | ||
|
|
||
| const { GET: isolatedGet } = (await import('./route')) as { GET: typeof GET }; | ||
| const response = await isolatedGet(request('Bearer anything')); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| await expect(response.json()).resolves.toEqual({ error: 'Unauthorized' }); | ||
| expect(isolatedOutcomeMock).not.toHaveBeenCalled(); | ||
| expect(isolatedOpenStockMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
24 changes: 24 additions & 0 deletions
24
apps/web/src/app/api/cron/code-review-outcome-aggregate/route.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,24 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { CRON_SECRET } from '@/lib/config.server'; | ||
| import { isCronAuthorizationValid } from '@/lib/cron-auth'; | ||
| import { | ||
| collectCodeReviewOpenStock, | ||
| collectCodeReviewOutcome, | ||
| } from '@/lib/code-reviews/telemetry/review-health-aggregate'; | ||
|
|
||
| export async function GET(request: Request) { | ||
| if ( | ||
| !CRON_SECRET || | ||
| !isCronAuthorizationValid(request.headers.get('authorization'), CRON_SECRET) | ||
| ) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
| } | ||
|
|
||
| const outcomeStatus = await collectCodeReviewOutcome(); | ||
| const openStockStatus = await collectCodeReviewOpenStock(); | ||
|
|
||
| return NextResponse.json({ | ||
| collectionStatus: | ||
| outcomeStatus === 'complete' && openStockStatus === 'complete' ? 'complete' : 'failed', | ||
| }); | ||
| } |
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.