-
Notifications
You must be signed in to change notification settings - Fork 36
fix(events): wait for watch event bursts #1831
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
10 commits
Select commit
Hold shift + click to select a range
0283a75
fix(events): debounce watch event wakes
sentry-junior[bot] 7954acb
Revert "fix(events): debounce watch event wakes"
sentry-junior[bot] c9e33c5
fix(events): renewable, capped debounce for watch event wakes
sentry-junior[bot] 13ea366
test(events): keep debounce coverage focused
sentry-junior[bot] 45eee11
refactor(events): simplify watch event delay
sentry-junior[bot] b200ebf
fix(events): delay the queue wake
sentry-junior[bot] f6b1729
fix(events): let user messages bypass event delay
sentry-junior[bot] 8af9a53
fix(events): let all human input bypass event wake delay
sentry-junior[bot] b622d60
fix(events): preserve active conversation leases
sentry-junior[bot] f8797a4
docs(events): explain the event debounce deferral
sentry-junior[bot] 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
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
181 changes: 181 additions & 0 deletions
181
packages/junior/tests/integration/event-wake-delay.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,181 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| createEventInboundMessage, | ||
| EVENT_WAIT_MS, | ||
| } from "@/chat/events/notification"; | ||
| import type { AgentRun } from "@/chat/agent/types"; | ||
| import { getConversationEventStore } from "@/chat/db"; | ||
| import { | ||
| appendAndEnqueueWebMessage, | ||
| createConversationId, | ||
| recordWebConversationActivity, | ||
| } from "@/chat/conversations/web-input"; | ||
| import { | ||
| resolveMailboxTurnWork, | ||
| type MailboxTurnWork, | ||
| } from "@/chat/task-execution/mailbox-turn"; | ||
| import { createConversationTurnWorker } from "@/chat/task-execution/conversation-turn"; | ||
| import { appendAndEnqueueInboundMessage } from "@/chat/task-execution/store"; | ||
| import { processConversationQueueMessage } from "@/chat/task-execution/vercel-callback"; | ||
| import type { | ||
| ConversationWorkerContext, | ||
| ConversationWorkerResult, | ||
| } from "@/chat/task-execution/worker"; | ||
| import { | ||
| closeConversationFixture, | ||
| createConversationFixture, | ||
| } from "../fixtures/conversation"; | ||
| import { createModelAgentRunnerForRun } from "../fixtures/agent-runner"; | ||
| import { createModelStream } from "../fixtures/model-stream"; | ||
|
|
||
| function requireConversationTurn( | ||
| worker: ( | ||
| context: ConversationWorkerContext, | ||
| resolved: MailboxTurnWork, | ||
| ) => Promise<ConversationWorkerResult>, | ||
| ) { | ||
| return async ( | ||
| context: ConversationWorkerContext, | ||
| ): Promise<ConversationWorkerResult> => { | ||
| const resolved = await resolveMailboxTurnWork(context); | ||
| if (!resolved) throw new Error("Expected Conversation mailbox work"); | ||
| return await worker(context, resolved); | ||
| }; | ||
| } | ||
|
|
||
| describe("event wake delay", () => { | ||
| afterEach(async () => { | ||
| await closeConversationFixture(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("waits for a burst of events before running one Turn", async () => { | ||
| const { actor, conversationStore, queue, state } = | ||
| await createConversationFixture(); | ||
| const conversationId = createConversationId({ | ||
| actorEmail: actor.email, | ||
| idempotencyKey: "event-burst-1", | ||
| }); | ||
| await recordWebConversationActivity({ | ||
| actor, | ||
| conversationId, | ||
| conversationStore, | ||
| nowMs: 1, | ||
| }); | ||
| await conversationStore.recordActivity({ | ||
| activityAtMs: 1, | ||
| conversationId, | ||
| nowMs: 1, | ||
| title: "Events", | ||
| }); | ||
|
|
||
| const baseMs = 1_700_000_000_000; | ||
| const nowSpy = vi.spyOn(Date, "now").mockReturnValue(baseMs); | ||
| const subscription = { | ||
| conversationId, | ||
| id: "resource-subscription-burst", | ||
| }; | ||
| const eventMessage = (sequence: number, receivedAtMs: number) => | ||
| createEventInboundMessage({ | ||
| event: { | ||
| eventKey: `check-${sequence}`, | ||
| eventType: "check_suite.completed", | ||
| identifier: "getsentry/junior#1563", | ||
| namespace: "github", | ||
| occurredAtMs: receivedAtMs, | ||
| trustedSummary: `Check ${sequence} failed`, | ||
| }, | ||
| receivedAtMs, | ||
| subscription, | ||
| text: `Check ${sequence} failed`, | ||
| }); | ||
|
|
||
| await appendAndEnqueueInboundMessage({ | ||
| conversationStore, | ||
| message: eventMessage(1, baseMs), | ||
| queue, | ||
| queueDelayMs: EVENT_WAIT_MS, | ||
| state, | ||
| }); | ||
| expect(queue.sentRecords()).toEqual([ | ||
| expect.objectContaining({ delayMs: 30_000 }), | ||
| ]); | ||
|
|
||
| const agentRuns: AgentRun[] = []; | ||
| const run = requireConversationTurn( | ||
| createConversationTurnWorker( | ||
| createModelAgentRunnerForRun((agentRun) => { | ||
| agentRuns.push(agentRun); | ||
| return createModelStream([ | ||
| { type: "text", text: "Handled both events." }, | ||
| ]); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| nowSpy.mockReturnValue(baseMs + 100); | ||
| await appendAndEnqueueWebMessage( | ||
| { | ||
| actor, | ||
| conversationId, | ||
| idempotencyKey: "human-message", | ||
| message: "Please check now", | ||
| }, | ||
| { conversationStore, queue, state }, | ||
| ); | ||
| expect(queue.sentRecords()).toHaveLength(2); | ||
| await expect( | ||
| processConversationQueueMessage(queue.takeMessage(), { | ||
| conversationStore, | ||
| queue, | ||
| run, | ||
| state, | ||
| }), | ||
| ).resolves.toEqual({ status: "pending_requeued" }); | ||
| expect(agentRuns).toHaveLength(1); | ||
|
|
||
| nowSpy.mockReturnValue(baseMs + 200); | ||
| await appendAndEnqueueInboundMessage({ | ||
| conversationStore, | ||
| message: eventMessage(2, baseMs + 200), | ||
| queue, | ||
| state, | ||
| }); | ||
| expect(queue.sentRecords()).toHaveLength(3); | ||
|
|
||
| nowSpy.mockReturnValue(baseMs + 30_000); | ||
| await expect( | ||
| processConversationQueueMessage(queue.takeMessage(), { | ||
| conversationStore, | ||
| queue, | ||
| run, | ||
| state, | ||
| }), | ||
| ).resolves.toEqual({ status: "pending_requeued" }); | ||
| expect(agentRuns).toHaveLength(1); | ||
| expect(queue.sentRecords().at(-1)).toMatchObject({ delayMs: 5_000 }); | ||
|
|
||
| nowSpy.mockReturnValue(baseMs + 35_000); | ||
| await expect( | ||
| processConversationQueueMessage(queue.takeMessage(), { | ||
| conversationStore, | ||
| queue, | ||
| run, | ||
| state, | ||
| }), | ||
| ).resolves.toEqual({ status: "completed" }); | ||
| expect(agentRuns).toHaveLength(2); | ||
|
|
||
| const userMessages = ( | ||
| await getConversationEventStore().loadHistory(conversationId) | ||
| ).flatMap((event) => | ||
| event.data.type === "message" && event.data.role === "user" | ||
| ? [event.data.text] | ||
| : [], | ||
| ); | ||
| expect(userMessages).toEqual([ | ||
| "Please check now", | ||
| "Check 1 failed\n\nCheck 2 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.