-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(knowledge): project document ACL and chunk changes asynchronously #8199
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
waleedlatif1
merged 3 commits into
staging
from
feat/knowledge-projection-acl-projector
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
70f3dfb
feat(knowledge): project document ACL and chunk changes asynchronously
waleedlatif1 6309546
fix(knowledge): keep projection guards out of historical migrations
waleedlatif1 5b54733
test(knowledge): count projection requests per connector ACL case
waleedlatif1 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| enqueueSweep: vi.fn(), | ||
| verifyCronAuth: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth/internal', () => ({ verifyCronAuth: mocks.verifyCronAuth })) | ||
| vi.mock('@/lib/knowledge/projection/enqueue', () => ({ | ||
| enqueueKnowledgeProjectionSweep: mocks.enqueueSweep, | ||
| })) | ||
|
|
||
| import { GET } from '@/app/api/cron/knowledge-projection/route' | ||
|
|
||
| function request() { | ||
| return createMockRequest( | ||
| 'GET', | ||
| undefined, | ||
| {}, | ||
| 'http://localhost:3000/api/cron/knowledge-projection' | ||
| ) | ||
| } | ||
|
|
||
| describe('knowledge projection sweep route', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mocks.verifyCronAuth.mockReturnValue(null) | ||
| }) | ||
|
|
||
| it('returns as soon as Trigger.dev accepts the pass', async () => { | ||
| mocks.enqueueSweep.mockResolvedValue({ | ||
| triggered: true, | ||
| 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('answers 200 without a pass when the projector has nothing to do', async () => { | ||
| mocks.enqueueSweep.mockResolvedValue({ triggered: false, backend: null, jobId: null }) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(200) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| success: true, | ||
| triggered: false, | ||
| backend: null, | ||
| jobId: null, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns the cron auth refusal without enqueueing', async () => { | ||
| mocks.verifyCronAuth.mockReturnValue(new Response(null, { status: 401 })) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mocks.enqueueSweep).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('fails closed when Trigger.dev does not accept the pass', async () => { | ||
| mocks.enqueueSweep.mockRejectedValue(new Error('trigger unavailable')) | ||
|
|
||
| const response = await GET(request()) | ||
|
|
||
| expect(response.status).toBe(500) | ||
| await expect(response.json()).resolves.toEqual({ | ||
| success: false, | ||
| error: 'Sweep enqueue failed', | ||
| }) | ||
| }) | ||
| }) |
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,29 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } 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 { enqueueKnowledgeProjectionSweep } from '@/lib/knowledge/projection/enqueue' | ||
|
|
||
| const logger = createLogger('KnowledgeProjectionSweepRoute') | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
| export const maxDuration = 60 | ||
|
|
||
| /** | ||
| * The knowledge projector's periodic sweep: enqueues one pass per window while there is work, and | ||
| * returns once Trigger.dev accepts it. Writers ask for passes as they commit; this converges | ||
| * whatever those requests missed. | ||
| */ | ||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| const authError = verifyCronAuth(request, 'Knowledge projection sweep') | ||
| if (authError) return authError | ||
|
|
||
| try { | ||
| const result = await enqueueKnowledgeProjectionSweep() | ||
| return NextResponse.json({ success: true, ...result }, { status: result.triggered ? 202 : 200 }) | ||
| } catch (error) { | ||
| logger.error('Knowledge projection sweep enqueue failed', { error: getErrorMessage(error) }) | ||
| return NextResponse.json({ success: false, error: 'Sweep enqueue failed' }, { status: 500 }) | ||
| } | ||
| }) |
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 @@ | ||
| import { task } from '@trigger.dev/sdk' | ||
| import { | ||
| type BackgroundRetryPolicy, | ||
| backgroundRetryAttemptCeiling, | ||
| getBackgroundRetryDecision, | ||
| } from '@/lib/core/errors/background-retry' | ||
| import { | ||
| KNOWLEDGE_PROJECTION_PASS_BUDGET_MS, | ||
| KNOWLEDGE_PROJECTION_TASK_ID, | ||
| requestKnowledgeProjection, | ||
| } from '@/lib/knowledge/projection/enqueue' | ||
| import { runKnowledgeProjectionPass } from '@/lib/knowledge/projection/run' | ||
|
|
||
| /** | ||
| * A pass gives a single document up on a lock or statement timeout without failing, so a failed | ||
| * pass lost its connection or its database. Those back off for minutes; the sweep starts a fresh | ||
| * pass every minute regardless, so a few attempts are enough. | ||
| */ | ||
| export const KNOWLEDGE_PROJECTION_RETRY_POLICY: BackgroundRetryPolicy = { | ||
| maxAttempts: 2, | ||
| database: { maxAttempts: 3, baseDelayMs: 60 * 1000, maxDelayMs: 5 * 60 * 1000 }, | ||
| } | ||
|
|
||
| /** | ||
| * Runs one knowledge projector pass. One pass runs at a time and projects several documents at | ||
| * once itself; the prompt requests and the sweep collapse into whichever pass is queued. A pass | ||
| * that ran out of budget with marks left asks for the next one. Retry-safe: a pass writes only rows | ||
| * that differ from their source and removes a mark only on the generation it read. | ||
| */ | ||
| export const knowledgeProjectionTask = task({ | ||
| id: KNOWLEDGE_PROJECTION_TASK_ID, | ||
| machine: 'small-1x', | ||
| maxDuration: 15 * 60, | ||
| retry: { maxAttempts: backgroundRetryAttemptCeiling(KNOWLEDGE_PROJECTION_RETRY_POLICY) }, | ||
| queue: { name: KNOWLEDGE_PROJECTION_TASK_ID, concurrencyLimit: 1 }, | ||
| catchError: async ({ error, ctx }) => | ||
| getBackgroundRetryDecision(error, ctx.attempt.number, KNOWLEDGE_PROJECTION_RETRY_POLICY), | ||
| run: async () => { | ||
| const result = await runKnowledgeProjectionPass({ | ||
| budgetMs: KNOWLEDGE_PROJECTION_PASS_BUDGET_MS, | ||
| }) | ||
| if (result.remaining) await requestKnowledgeProjection() | ||
| return result | ||
| }, | ||
| }) |
This file was deleted.
Oops, something went wrong.
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
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.