Skip to content

Conversation

@Saedbhati
Copy link
Collaborator

Description

Describe your changes in detail (optional if the linked issue already contains a detailed description of the changes).
fixes #3332

Checklist

Go over all the following points, and put an x in all the boxes that apply.

  • I have read the CONTRIBUTION guide (required)
  • I have linked this PR to an issue using the Development section on the right sidebar or by adding Fixes #issue-number in the PR description (required)
  • I have checked if any dependencies need to be added or updated in pyproject.toml and uv lock
  • I have updated the tests accordingly (required for a bug fix or a new feature)
  • I have updated the documentation if needed:
  • I have added examples if this is a new feature

If you are unsure about any of these, don't hesitate to ask. We are here to help!

@Saedbhati Saedbhati requested a review from Wendong-Fan November 7, 2025 12:32
@github-actions github-actions bot added the Review Required PR need to be reviewed label Nov 7, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 7, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refinement

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Saedbhati , left some comments below, i think the design could be more tidy and now there's some redundant code/design, also could you add test code and example file for this? example task for workforce could be: search 10 different papers related to llm agent and write a html report about them

Comment on lines +224 to +231
max_refinement_iterations (int, optional): Maximum number of
iterative refinement attempts for parallel task results. When
parallel subtasks produce duplicate content or don't meet
requirements, the workforce will automatically validate and
generate additional targeted subtasks to fill gaps. This
parameter limits the refinement loop to prevent excessive
iterations. Set to 0 to disable refinement validation.
(default: :obj:`2`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better not to set default max value? since if still have duplicated content that means the task hasn't been success, we should continue refine it

we also need to add this argument to def clone

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The agent might get stuck in a refinement loop without a max limit, so keeping this parameter helps control and reduce the overall task completion cost

@Wendong-Fan Wendong-Fan added Waiting for Update PR has been reviewed, need to be updated based on review comment and removed Review Required PR need to be reviewed labels Nov 10, 2025
@Wendong-Fan Wendong-Fan added this to the Sprint 41 milestone Nov 10, 2025
@fengju0213 fengju0213 modified the milestones: Sprint 41, Sprint 42 Nov 10, 2025
Copy link
Collaborator

@hesamsheikh hesamsheikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR @Saedbhati
I added a few comments.

Comment on lines 1066 to 1217
def _validate_aggregated_result(
self, task: Task, aggregated_result: str
) -> ValidationResult:
r"""Validate aggregated results from parallel subtasks.
This method uses the Task Planner Agent to:
1. Deduplicate content across parallel subtask results
2. Verify requirements are met (e.g., "5 unique papers")
3. Provide guidance for additional tasks if needed
Args:
task (Task): The parent task containing subtasks
aggregated_result (str): The concatenated results from all subtasks
Returns:
ValidationResult: Validation result with deduplication and
requirement checking
"""
num_subtasks = len(task.subtasks) if task.subtasks else 0

subtask_ids = (
[sub.id for sub in task.subtasks] if task.subtasks else []
)
subtask_ids_str = ", ".join(subtask_ids) if subtask_ids else "None"

validation_prompt = str(
VALIDATION_PROMPT.format(
task_id=task.id,
task_content=task.content,
num_subtasks=num_subtasks,
subtask_ids=subtask_ids_str,
aggregated_result=aggregated_result,
response_format=VALIDATION_RESPONSE_FORMAT,
)
)

# Fallback values if parsing fails - fail-safe approach
fallback_values = {
"requirements_met": False,
"unique_count": 0,
"duplicate_count": 0,
"missing_count": num_subtasks,
"deduplicated_result": aggregated_result,
"reasoning": (
"Validation failed - could not parse response. "
"Failing safe to prevent accepting potentially "
"invalid results."
),
"additional_task_guidance": None,
"duplicate_subtask_ids": None,
}

examples = [
{
"requirements_met": True,
"unique_count": 5,
"duplicate_count": 2,
"missing_count": 0,
"deduplicated_result": "Deduplicated content here...",
"reasoning": (
"Found 5 unique papers after removing 2 duplicates. "
"Requirements fully met."
),
"additional_task_guidance": None,
"duplicate_subtask_ids": ["task_1.3", "task_1.5"],
},
{
"requirements_met": False,
"unique_count": 3,
"duplicate_count": 4,
"missing_count": 2,
"deduplicated_result": "3 unique papers found...",
"reasoning": (
"Only 3 unique papers found after deduplication. "
"Need 2 more to meet requirement of 5."
),
"additional_task_guidance": (
"Find ONE unique research paper on the topic, avoiding: "
"Paper A, Paper B, Paper C. To ensure diversity across "
"parallel retries, consider exploring different "
"publication years, research methodologies, or "
"application domains."
),
"duplicate_subtask_ids": [
"task_1.2",
"task_1.4",
"task_1.5",
"task_1.6",
],
},
]

try:
if self.use_structured_output_handler:
enhanced_prompt = (
self.structured_handler.generate_structured_prompt(
base_prompt=validation_prompt,
schema=ValidationResult,
examples=examples,
)
)
response = self.task_agent.step(enhanced_prompt)

result = self.structured_handler.parse_structured_response(
response.msg.content if response.msg else "",
schema=ValidationResult,
fallback_values=fallback_values,
)

if isinstance(result, ValidationResult):
return result
elif isinstance(result, dict):
return ValidationResult.model_validate(result)
else:
return ValidationResult.model_validate(fallback_values)
else:
response = self.task_agent.step(
validation_prompt, response_format=ValidationResult
)
return response.msg.parsed

except Exception as e:
logger.warning(
f"Error during validation for task {task.id}: {e}, "
f"using fallback"
)
return ValidationResult.model_validate(fallback_values)

def _build_task_content_with_refinement(
self,
base_content: str,
refinement_guidance: str,
refinement_iteration: int,
) -> str:
r"""Build task content with refinement guidance.
Args:
base_content (str): The base task content
refinement_guidance (str): The refinement guidance from validation
refinement_iteration (int): The current refinement iteration number
Returns:
str: The complete task content including refinement guidance
"""
return (
f"{base_content}\n\n"
f"IMPORTANT - REFINEMENT ITERATION {refinement_iteration}:\n"
f"{refinement_guidance}\n\n"
f"Your previous result was a duplicate. You MUST find a "
f"completely DIFFERENT and UNIQUE item this time."
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any possibility to put these methods in the utils.py?

Comment on lines 1340 to 1341
batch_result = await self._find_assignee([task])
assignment = batch_result.assignments[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few comments here are removed, which were kind of helpful.

@Saedbhati Saedbhati added Review Required PR need to be reviewed and removed Waiting for Update PR has been reviewed, need to be updated based on review comment labels Nov 12, 2025
@hesamsheikh
Copy link
Collaborator

Hey @Saedbhati
Are the review comments addressed or need more work?

Copy link
Collaborator

@waleedalzarooni waleedalzarooni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR @Saedbhati, just a couple of comments

task = Task(
content=(
"Find 5 unique research papers on NLP systems. "
"For each paper, provide: title, authors, year, and a brief "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line may be incomplete

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should do the job! Do you have any suggestions on how I can improve the task prompt?

)
iteration_num = refinement_iteration + 1
subtask.content = (
f"{subtask.additional_info['base_content'],}\n\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checking whether the comma after ['base_content'] is on purpose as it will create a tuple formatting ('content',) instead of just 'content'

@fengju0213 fengju0213 removed this from the Sprint 42 milestone Nov 24, 2025
@fengju0213 fengju0213 added this to the Sprint 43 milestone Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review Required PR need to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Enhance Workforce for duplicated task handling

6 participants