-
Notifications
You must be signed in to change notification settings - Fork 10.5k
refactor(core): extract SchedulerStateManager and modularize state transitions #16133
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
base: main
Are you sure you want to change the base?
Conversation
…ansitions - Extracted scheduler state (active calls, queue, completed batch) into a dedicated SchedulerStateManager class. - Simplified CoreToolScheduler by delegating state manipulations and status transitions to the manager. - Standardized terminal call finalization and archiving logic. - Updated CoreToolScheduler and useReactToolScheduler tests to be more resilient to granular state updates. - Refactored a2a-server E2E tests to use robust event matching instead of fragile array indices. - Added comprehensive unit tests for SchedulerStateManager.
Summary of ChangesHello @abhipatel12, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant and valuable refactoring by extracting the scheduler's state management logic from CoreToolScheduler into a new, dedicated SchedulerStateManager class. This change greatly improves modularity, separation of concerns, and testability. The new SchedulerStateManager is well-designed and is accompanied by a comprehensive test suite. The updates to existing tests make them more robust and resilient to the more granular, asynchronous state updates from the new architecture. Overall, this is an excellent improvement.
I've identified two high-severity issues where errors during tool modification are silently ignored, which could lead to unexpected behavior. These comments have been retained as they do not contradict any existing rules.
| if (!(invocationOrError instanceof Error)) { | ||
| this.state.updateArgs( | ||
| callId, | ||
| result.updatedParams, | ||
| invocationOrError, | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential issue here where an error from buildInvocation is silently ignored. If result.updatedParams are invalid, buildInvocation will return an Error. The current logic will not update the invocation but will proceed to update the status to awaiting_approval with a new diff. This can lead to a confusing state where the user sees a diff for a change that is invalid, and if they approve, the tool might execute with the old, stale parameters. The tool call should be transitioned to an 'error' state instead.
if (invocationOrError instanceof Error) {
this.state.updateStatus(
callId,
'error',
createErrorResponse(
waitingToolCall.request,
invocationOrError,
ToolErrorType.INVALID_TOOL_PARAMS,
),
);
await this.checkAndNotifyCompletion(signal);
return;
}
this.state.updateArgs(
callId,
result.updatedParams,
invocationOrError,
);| if (!(invocationOrError instanceof Error)) { | ||
| this.state.updateArgs( | ||
| callId, | ||
| result.updatedParams, | ||
| invocationOrError, | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to a previous comment, an error from buildInvocation is silently ignored here. If the newContent from the payload results in invalid parameters, buildInvocation returns an Error, but the tool call is not transitioned to an error state. This could lead to the tool executing with stale parameters if the user re-confirms. The tool call should be moved to an 'error' state to reflect the invalid modification.
if (invocationOrError instanceof Error) {
this.state.updateStatus(
callId,
'error',
createErrorResponse(
(toolCall as WaitingToolCall).request,
invocationOrError,
ToolErrorType.INVALID_TOOL_PARAMS,
),
);
await this.checkAndNotifyCompletion(signal);
return;
}
this.state.updateArgs(
callId,
result.updatedParams,
invocationOrError,
);
Summary
Extracts the scheduler state (active calls, queue, completed batch) from
CoreToolSchedulerinto a dedicatedSchedulerStateManager. This modularizes state management and simplifies the scheduler's logic, serving as a foundation for the Phase 2 asynchronous orchestrator.Details
SchedulerStateManagerto encapsulate all tool call data structures and status transitions, removing the complexsetStatusInternalandsetArgsInternalmethods from the scheduler.executingstate to prevent data loss for live output streaming and PID updates (discovered during internal review).finalizeCallstep to archive terminal calls into the completed batch after post-processing (like logging).SchedulerStateManagerand updated existing scheduler tests to be resilient to more granular state updates.Related Issues
Works towards resolving issue #14306.
How to Validate
npm run buildto ensure type safety.npx vitest packages/core/src/scheduler/state-manager.test.ts.npx vitest packages/core/src/core/coreToolScheduler.test.ts.npx vitest packages/cli/src/ui/hooks/useToolScheduler.test.ts.npx vitest packages/a2a-server/src/http/app.test.ts.Pre-Merge Checklist