-
-
Notifications
You must be signed in to change notification settings - Fork 706
fix(vt): prevent pending scan starvation and retry unresolved results #468
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
+252
−16
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { getPendingScanSkillsInternal } from './skills' | ||
|
|
||
| type PendingScanResult = Array<{ | ||
| skillId: string | ||
| versionId: string | null | ||
| sha256hash: string | null | ||
| checkCount: number | ||
| }> | ||
|
|
||
| type WrappedHandler<TArgs, TResult> = { | ||
| _handler: (ctx: unknown, args: TArgs) => Promise<TResult> | ||
| } | ||
|
|
||
| const getPendingScanSkillsHandler = ( | ||
| getPendingScanSkillsInternal as unknown as WrappedHandler<Record<string, unknown>, PendingScanResult> | ||
| )._handler | ||
|
|
||
| describe('skills.getPendingScanSkillsInternal', () => { | ||
| it('includes unresolved VT records from the oldest slice and skips finalized ones', async () => { | ||
| const recentSkills = [ | ||
| makeSkill('skills:recent-clean', 'skillVersions:recent-clean', 'scanner.llm.clean'), | ||
| makeSkill('skills:recent-malicious', 'skillVersions:recent-malicious', 'scanner.vt.pending'), | ||
| ] | ||
| const oldestSkills = [ | ||
| makeSkill('skills:old-pending', 'skillVersions:old-pending', 'scanner.vt.pending'), | ||
| makeSkill('skills:old-stale', 'skillVersions:old-stale', 'scanner.llm.clean'), | ||
| makeSkill('skills:old-no-hash', 'skillVersions:old-no-hash', 'scanner.vt.pending'), | ||
| ] | ||
|
|
||
| const versions = new Map<string, unknown>([ | ||
| [ | ||
| 'skillVersions:recent-clean', | ||
| { _id: 'skillVersions:recent-clean', sha256hash: 'a'.repeat(64), vtAnalysis: { status: 'clean' } }, | ||
| ], | ||
| [ | ||
| 'skillVersions:recent-malicious', | ||
| { | ||
| _id: 'skillVersions:recent-malicious', | ||
| sha256hash: 'b'.repeat(64), | ||
| vtAnalysis: { status: 'malicious' }, | ||
| }, | ||
| ], | ||
| [ | ||
| 'skillVersions:old-pending', | ||
| { _id: 'skillVersions:old-pending', sha256hash: 'c'.repeat(64), vtAnalysis: { status: 'pending' } }, | ||
| ], | ||
| [ | ||
| 'skillVersions:old-stale', | ||
| { _id: 'skillVersions:old-stale', sha256hash: 'd'.repeat(64), vtAnalysis: { status: 'stale' } }, | ||
| ], | ||
| ['skillVersions:old-no-hash', { _id: 'skillVersions:old-no-hash' }], | ||
| ]) | ||
|
|
||
| const ctx = { | ||
| db: { | ||
| query: vi.fn((table: string) => { | ||
| if (table !== 'skills') throw new Error(`unexpected table ${table}`) | ||
| return { | ||
| withIndex: ( | ||
| indexName: string, | ||
| builder: (q: { eq: (field: string, value: unknown) => unknown }) => unknown, | ||
| ) => { | ||
| builder({ eq: () => ({}) }) | ||
| if (indexName === 'by_active_updated') { | ||
| return { | ||
| order: () => ({ | ||
| take: async () => recentSkills, | ||
| }), | ||
| } | ||
| } | ||
| if (indexName === 'by_active_created') { | ||
| return { | ||
| order: () => ({ | ||
| take: async () => oldestSkills, | ||
| }), | ||
| } | ||
| } | ||
| throw new Error(`unexpected index ${indexName}`) | ||
| }, | ||
| } | ||
| }), | ||
| get: vi.fn(async (id: string) => versions.get(id) ?? null), | ||
| }, | ||
| } | ||
|
|
||
| const result = await getPendingScanSkillsHandler(ctx, { | ||
| limit: 25, | ||
| skipRecentMinutes: 0, | ||
| }) | ||
|
|
||
| const ids = new Set(result.map((entry) => entry.skillId)) | ||
| expect(ids.has('skills:old-pending')).toBe(true) | ||
| expect(ids.has('skills:old-stale')).toBe(true) | ||
| expect(ids.has('skills:recent-clean')).toBe(false) | ||
| expect(ids.has('skills:recent-malicious')).toBe(false) | ||
| expect(ids.has('skills:old-no-hash')).toBe(false) | ||
| }) | ||
|
|
||
| it('exhaustive mode ignores recent-check suppression for manual backfills', async () => { | ||
| const now = Date.now() | ||
| const allSkills = [ | ||
| makeSkill('skills:recently-checked', 'skillVersions:recently-checked', 'scanner.vt.pending', now), | ||
| ] | ||
| const versions = new Map<string, unknown>([ | ||
| [ | ||
| 'skillVersions:recently-checked', | ||
| { _id: 'skillVersions:recently-checked', sha256hash: 'e'.repeat(64) }, | ||
| ], | ||
| ]) | ||
|
|
||
| const withIndex = vi.fn( | ||
| ( | ||
| indexName: string, | ||
| builder: (q: { eq: (field: string, value: unknown) => unknown }) => unknown, | ||
| ) => { | ||
| builder({ eq: () => ({}) }) | ||
| if (indexName !== 'by_active_updated') throw new Error(`unexpected index ${indexName}`) | ||
| return { | ||
| collect: async () => allSkills, | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| const ctx = { | ||
| db: { | ||
| query: vi.fn((table: string) => { | ||
| if (table !== 'skills') throw new Error(`unexpected table ${table}`) | ||
| return { withIndex } | ||
| }), | ||
| get: vi.fn(async (id: string) => versions.get(id) ?? null), | ||
| }, | ||
| } | ||
|
|
||
| const result = await getPendingScanSkillsHandler(ctx, { | ||
| limit: 25, | ||
| skipRecentMinutes: 60, | ||
| exhaustive: true, | ||
| }) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(result[0]?.skillId).toBe('skills:recently-checked') | ||
| }) | ||
|
|
||
| it('does not clamp exhaustive mode to 100 records', async () => { | ||
| const allSkills = Array.from({ length: 150 }, (_, i) => | ||
| makeSkill(`skills:bulk-${i}`, `skillVersions:bulk-${i}`, 'scanner.vt.pending'), | ||
| ) | ||
| const versions = new Map<string, unknown>( | ||
| allSkills.map((skill) => { | ||
| const versionId = skill.latestVersionId as string | ||
| return [versionId, { _id: versionId, sha256hash: `${String(versionId).slice(-8)}${'f'.repeat(56)}` }] | ||
| }), | ||
| ) | ||
|
|
||
| const withIndex = vi.fn( | ||
| ( | ||
| indexName: string, | ||
| builder: (q: { eq: (field: string, value: unknown) => unknown }) => unknown, | ||
| ) => { | ||
| builder({ eq: () => ({}) }) | ||
| if (indexName !== 'by_active_updated') throw new Error(`unexpected index ${indexName}`) | ||
| return { | ||
| collect: async () => allSkills, | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| const ctx = { | ||
| db: { | ||
| query: vi.fn((table: string) => { | ||
| if (table !== 'skills') throw new Error(`unexpected table ${table}`) | ||
| return { withIndex } | ||
| }), | ||
| get: vi.fn(async (id: string) => versions.get(id) ?? null), | ||
| }, | ||
| } | ||
|
|
||
| const result = await getPendingScanSkillsHandler(ctx, { | ||
| limit: 10000, | ||
| exhaustive: true, | ||
| skipRecentMinutes: 0, | ||
| }) | ||
|
|
||
| expect(result).toHaveLength(150) | ||
| }) | ||
| }) | ||
|
|
||
| function makeSkill( | ||
| id: string, | ||
| versionId: string, | ||
| moderationReason: string, | ||
| scanLastCheckedAt?: number, | ||
| ) { | ||
| return { | ||
| _id: id, | ||
| moderationStatus: 'active', | ||
| moderationReason, | ||
| latestVersionId: versionId, | ||
| scanLastCheckedAt, | ||
| } | ||
| } |
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
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.