-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: refactor parent-child task coordination to event-driven architecture #6958
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,6 +130,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| readonly parentTask: Task | undefined = undefined | ||
| readonly taskNumber: number | ||
| readonly workspacePath: string | ||
| childTaskIds: string[] = [] | ||
| taskStatus: "active" | "paused" | "completed" = "active" | ||
|
|
||
| /** | ||
| * The mode associated with this task. Persisted across sessions | ||
|
|
@@ -184,7 +186,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| isInitialized = false | ||
| isPaused: boolean = false | ||
| pausedModeSlug: string = defaultModeSlug | ||
| private pauseInterval: NodeJS.Timeout | undefined | ||
|
|
||
| // API | ||
| readonly apiConfiguration: ProviderSettings | ||
|
|
@@ -574,7 +575,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| } | ||
| } | ||
|
|
||
| private async saveClineMessages() { | ||
| public async saveClineMessages() { | ||
| try { | ||
| await saveTaskMessages({ | ||
| messages: this.clineMessages, | ||
|
|
@@ -589,6 +590,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| globalStoragePath: this.globalStoragePath, | ||
| workspace: this.cwd, | ||
| mode: this._taskMode || defaultModeSlug, // Use the task's own mode, not the current provider mode | ||
| parentTaskId: this.parentTask?.taskId, | ||
| childTaskIds: this.childTaskIds, | ||
| taskStatus: this.taskStatus, | ||
| }) | ||
|
|
||
| this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage) | ||
|
|
@@ -1002,6 +1006,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| public async resumePausedTask(lastMessage: string) { | ||
| // Release this Cline instance from paused state. | ||
| this.isPaused = false | ||
| this.taskStatus = "active" | ||
| this.emit(RooCodeEventName.TaskUnpaused) | ||
|
|
||
| // Fake an answer from the subtask that it has completed running and | ||
|
|
@@ -1014,6 +1019,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| role: "user", | ||
| content: [{ type: "text", text: `[new_task completed] Result: ${lastMessage}` }], | ||
| }) | ||
|
|
||
| // Update task status in persistent storage | ||
| await this.saveClineMessages() | ||
| } catch (error) { | ||
| this.providerRef | ||
| .deref() | ||
|
|
@@ -1263,12 +1271,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| public dispose(): void { | ||
| console.log(`[Task] disposing task ${this.taskId}.${this.instanceId}`) | ||
|
|
||
| // Stop waiting for child task completion. | ||
| if (this.pauseInterval) { | ||
| clearInterval(this.pauseInterval) | ||
| this.pauseInterval = undefined | ||
| } | ||
|
|
||
| // Release any terminals associated with this task. | ||
| try { | ||
| // Release any terminals associated with this task. | ||
|
|
@@ -1341,21 +1343,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| } | ||
| } | ||
|
|
||
| // Used when a sub-task is launched and the parent task is waiting for it to | ||
| // finish. | ||
| // TBD: The 1s should be added to the settings, also should add a timeout to | ||
| // prevent infinite waiting. | ||
| public async waitForResume() { | ||
| await new Promise<void>((resolve) => { | ||
| this.pauseInterval = setInterval(() => { | ||
| if (!this.isPaused) { | ||
| clearInterval(this.pauseInterval) | ||
| this.pauseInterval = undefined | ||
| resolve() | ||
| } | ||
| }, 1000) | ||
| }) | ||
| } | ||
| // Direct resumption - no polling needed | ||
| // The child task will directly call resumePausedTask() on the parent | ||
|
|
||
| // Task Loop | ||
|
|
||
|
|
@@ -1425,27 +1414,17 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike { | |
| this.consecutiveMistakeCount = 0 | ||
| } | ||
|
|
||
| // In this Cline request loop, we need to check if this task instance | ||
| // has been asked to wait for a subtask to finish before continuing. | ||
| // Check if this task is paused (waiting for a subtask) | ||
| // The child task will directly resume this task when it completes | ||
| const provider = this.providerRef.deref() | ||
|
|
||
| if (this.isPaused && provider) { | ||
| provider.log(`[subtasks] paused ${this.taskId}.${this.instanceId}`) | ||
| await this.waitForResume() | ||
| provider.log(`[subtasks] resumed ${this.taskId}.${this.instanceId}`) | ||
| const currentMode = (await provider.getState())?.mode ?? defaultModeSlug | ||
|
|
||
| if (currentMode !== this.pausedModeSlug) { | ||
| // The mode has changed, we need to switch back to the paused mode. | ||
| await provider.handleModeSwitch(this.pausedModeSlug) | ||
|
|
||
| // Delay to allow mode change to take effect before next tool is executed. | ||
| await delay(500) | ||
|
|
||
| provider.log( | ||
| `[subtasks] task ${this.taskId}.${this.instanceId} has switched back to '${this.pausedModeSlug}' from '${currentMode}'`, | ||
| ) | ||
| } | ||
| provider.log( | ||
| `[subtasks] task ${this.taskId}.${this.instanceId} is paused, waiting for child task to complete`, | ||
| ) | ||
| // The task will be resumed directly by the child task calling resumePausedTask() | ||
| // No polling needed - execution will continue when resumed | ||
| return true // Exit the loop while paused | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the early return intentional here? When a task is paused, returning |
||
| } | ||
|
|
||
| // Getting verbose details is an expensive operation, it uses ripgrep to | ||
|
|
||
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.
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.
Race condition:
taskStatusis set to "active" butsaveClineMessages()is called without awaiting. If the system crashes between these operations, the status change is lost. Consider awaiting the save operation.