-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(files): add workspace content search #7289
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1132f2
feat(files): add workspace content search
icecrasher321 bc7e8ca
fix(files): address search review findings
icecrasher321 f681054
chore(helm): bump chart for search scheduler
icecrasher321 d770429
fix(db): sync file search migration snapshot
icecrasher321 8e9758a
fix(files): address second search review round
icecrasher321 2d3f46e
fix(files): harden search and content provenance
icecrasher321 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
68 changes: 68 additions & 0 deletions
68
apps/sim/app/api/cron/workspace-file-search-dispatch/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,68 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| enqueueDispatch: vi.fn(), | ||
| verifyCronAuth: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth/internal', () => ({ verifyCronAuth: mocks.verifyCronAuth })) | ||
| vi.mock('@/lib/workspace-files/search/enqueue-dispatch', () => ({ | ||
| enqueueWorkspaceFileSearchDispatch: mocks.enqueueDispatch, | ||
| })) | ||
|
|
||
| import { GET } from '@/app/api/cron/workspace-file-search-dispatch/route' | ||
|
|
||
| function request() { | ||
| return createMockRequest( | ||
| 'GET', | ||
| undefined, | ||
| {}, | ||
| 'http://localhost:3000/api/cron/workspace-file-search-dispatch' | ||
| ) | ||
| } | ||
|
|
||
| describe('workspace file search dispatch route', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mocks.verifyCronAuth.mockReturnValue(null) | ||
| }) | ||
|
|
||
| it('returns as soon as Trigger.dev accepts the dispatcher run', async () => { | ||
| mocks.enqueueDispatch.mockResolvedValue({ backend: 'trigger-dev', jobId: 'run-1' }) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(202) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| success: true, | ||
| triggered: true, | ||
| backend: 'trigger-dev', | ||
| jobId: 'run-1', | ||
| }) | ||
| }) | ||
|
|
||
| it('returns the cron auth refusal without dispatching', async () => { | ||
| mocks.verifyCronAuth.mockReturnValue(new Response(null, { status: 401 })) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mocks.enqueueDispatch).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('fails closed when Trigger.dev does not accept the dispatcher run', async () => { | ||
| mocks.enqueueDispatch.mockRejectedValue(new Error('trigger unavailable')) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(500) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| success: false, | ||
| error: 'Dispatcher enqueue failed', | ||
| }) | ||
| }) | ||
| }) |
30 changes: 30 additions & 0 deletions
30
apps/sim/app/api/cron/workspace-file-search-dispatch/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,30 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { verifyCronAuth } from '@/lib/auth/internal' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { enqueueWorkspaceFileSearchDispatch } from '@/lib/workspace-files/search/enqueue-dispatch' | ||
|
|
||
| const logger = createLogger('WorkspaceFileSearchDispatchRoute') | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
| export const maxDuration = 60 | ||
|
|
||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| const authError = verifyCronAuth(request, 'Workspace file search dispatcher') | ||
| if (authError) return authError | ||
|
|
||
| try { | ||
| const result = await enqueueWorkspaceFileSearchDispatch() | ||
| logger.info('Workspace file search dispatcher accepted', result) | ||
| return NextResponse.json({ success: true, triggered: true, ...result }, { status: 202 }) | ||
| } catch (error) { | ||
| logger.error('Workspace file search dispatcher enqueue failed', { | ||
| error: toError(error).message, | ||
| }) | ||
| return NextResponse.json( | ||
| { success: false, error: 'Dispatcher enqueue failed' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| }) | ||
45 changes: 45 additions & 0 deletions
45
apps/sim/background/workspace-file-search-dispatch.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,45 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| dispatch: vi.fn(), | ||
| task: vi.fn((config: unknown) => config), | ||
| })) | ||
|
|
||
| vi.mock('@trigger.dev/sdk', () => ({ task: mocks.task })) | ||
| vi.mock('@/lib/workspace-files/search/dispatcher', () => ({ | ||
| dispatchWorkspaceFileSearchIndexJobs: mocks.dispatch, | ||
| })) | ||
|
|
||
| import { FILE_SEARCH_DISPATCH_MAX_DURATION_SECONDS } from '@/lib/workspace-files/search/constants' | ||
| import { workspaceFileSearchDispatchTask } from '@/background/workspace-file-search-dispatch' | ||
|
|
||
| describe('workspace file search dispatch task', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('serializes bounded dispatcher runs outside the cron request', async () => { | ||
| expect(workspaceFileSearchDispatchTask).toMatchObject({ | ||
| id: 'workspace-file-search-dispatch', | ||
| machine: 'small-1x', | ||
| maxDuration: FILE_SEARCH_DISPATCH_MAX_DURATION_SECONDS, | ||
| retry: { maxAttempts: 3 }, | ||
| queue: { | ||
| name: 'workspace-file-search-dispatch', | ||
| concurrencyLimit: 1, | ||
| }, | ||
| }) | ||
|
|
||
| mocks.dispatch.mockResolvedValue({ | ||
| dispatchedFiles: 2, | ||
| backfilledFiles: 1000, | ||
| reapedClaims: 0, | ||
| lockAcquired: true, | ||
| }) | ||
| await workspaceFileSearchDispatchTask.run() | ||
| expect(mocks.dispatch).toHaveBeenCalledOnce() | ||
| }) | ||
| }) |
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,19 @@ | ||
| import { task } from '@trigger.dev/sdk' | ||
| import { FILE_SEARCH_DISPATCH_MAX_DURATION_SECONDS } from '@/lib/workspace-files/search/constants' | ||
| import { dispatchWorkspaceFileSearchIndexJobs } from '@/lib/workspace-files/search/dispatcher' | ||
|
|
||
| /** | ||
| * Runs the bounded search-index control plane outside the cron request. Per-file parsing remains | ||
| * isolated in `workspace-file-search-index`; this task only backfills, claims, and enqueues work. | ||
| */ | ||
| export const workspaceFileSearchDispatchTask = task({ | ||
| id: 'workspace-file-search-dispatch', | ||
| machine: 'small-1x', | ||
| maxDuration: FILE_SEARCH_DISPATCH_MAX_DURATION_SECONDS, | ||
| retry: { maxAttempts: 3 }, | ||
| queue: { | ||
| name: 'workspace-file-search-dispatch', | ||
| concurrencyLimit: 1, | ||
| }, | ||
| run: () => dispatchWorkspaceFileSearchIndexJobs(), | ||
| }) |
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,68 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| indexWorkspaceFile: vi.fn(), | ||
| markFailed: vi.fn(), | ||
| task: vi.fn((config: unknown) => config), | ||
| })) | ||
|
|
||
| vi.mock('@trigger.dev/sdk', () => ({ task: mocks.task })) | ||
| vi.mock('@/lib/workspace-files/search/indexing', () => ({ | ||
| indexWorkspaceFileForSearch: mocks.indexWorkspaceFile, | ||
| markWorkspaceFileSearchIndexFailed: mocks.markFailed, | ||
| })) | ||
|
|
||
| import { | ||
| FILE_SEARCH_INDEX_GLOBAL_CONCURRENCY, | ||
| FILE_SEARCH_INDEX_MAX_DURATION_SECONDS, | ||
| } from '@/lib/workspace-files/search/constants' | ||
| import { workspaceFileSearchIndexTask } from '@/background/workspace-file-search-index' | ||
|
|
||
| const payload = { | ||
| workspaceId: 'workspace-1', | ||
| fileId: 'file-1', | ||
| sourceContentUpdatedAt: '2026-08-29T12:00:00.000Z', | ||
| } | ||
|
|
||
| describe('workspace file search index task', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('uses isolated medium workers with a hard global concurrency and duration cap', () => { | ||
| expect(workspaceFileSearchIndexTask).toMatchObject({ | ||
| id: 'workspace-file-search-index', | ||
| machine: 'medium-1x', | ||
| maxDuration: FILE_SEARCH_INDEX_MAX_DURATION_SECONDS, | ||
| retry: { maxAttempts: 3 }, | ||
| queue: { | ||
| name: 'workspace-file-search-index', | ||
| concurrencyLimit: FILE_SEARCH_INDEX_GLOBAL_CONCURRENCY, | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('passes the Trigger.dev abort signal to the single-revision indexer', async () => { | ||
| const signal = new AbortController().signal | ||
| mocks.indexWorkspaceFile.mockResolvedValue(undefined) | ||
|
|
||
| await workspaceFileSearchIndexTask.run(payload, { signal }) | ||
|
|
||
| expect(mocks.indexWorkspaceFile).toHaveBeenCalledWith(payload, signal) | ||
| }) | ||
|
|
||
| it('marks the revision failed only from the terminal onFailure hook', async () => { | ||
| mocks.indexWorkspaceFile.mockRejectedValue(new Error('retryable parser failure')) | ||
|
|
||
| await expect( | ||
| workspaceFileSearchIndexTask.run(payload, { signal: new AbortController().signal }) | ||
| ).rejects.toThrow('retryable parser failure') | ||
| expect(mocks.markFailed).not.toHaveBeenCalled() | ||
|
|
||
| await workspaceFileSearchIndexTask.onFailure({ payload }) | ||
| expect(mocks.markFailed).toHaveBeenCalledWith(payload) | ||
| }) | ||
| }) |
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,30 @@ | ||
| import { task } from '@trigger.dev/sdk' | ||
| import { | ||
| FILE_SEARCH_INDEX_GLOBAL_CONCURRENCY, | ||
| FILE_SEARCH_INDEX_MAX_DURATION_SECONDS, | ||
| } from '@/lib/workspace-files/search/constants' | ||
| import { | ||
| indexWorkspaceFileForSearch, | ||
| markWorkspaceFileSearchIndexFailed, | ||
| type WorkspaceFileSearchIndexPayload, | ||
| } from '@/lib/workspace-files/search/indexing' | ||
|
|
||
| /** | ||
| * Builds one immutable workspace-file search revision. PostgreSQL owns the durable state; this | ||
| * task only supplies isolated compute, retries, and a hard global execution cap. | ||
| */ | ||
| export const workspaceFileSearchIndexTask = task({ | ||
| id: 'workspace-file-search-index', | ||
| machine: 'medium-1x', | ||
| maxDuration: FILE_SEARCH_INDEX_MAX_DURATION_SECONDS, | ||
| retry: { maxAttempts: 3 }, | ||
| queue: { | ||
| name: 'workspace-file-search-index', | ||
| concurrencyLimit: FILE_SEARCH_INDEX_GLOBAL_CONCURRENCY, | ||
| }, | ||
| run: (payload: WorkspaceFileSearchIndexPayload, { signal }) => | ||
| indexWorkspaceFileForSearch(payload, signal), | ||
| onFailure: async ({ payload }) => { | ||
| await markWorkspaceFileSearchIndexFailed(payload) | ||
| }, | ||
| }) |
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.