Skip to content

fix(workforce): handle empty and whitespace strings in is_generic_role_name - #4282

Open
Xayar145 wants to merge 3 commits into
camel-ai:masterfrom
Xayar145:fix/workforce-generic-role-name-whitespace
Open

Xayar145 wants to merge 3 commits into
camel-ai:masterfrom
Xayar145:fix/workforce-generic-role-name-whitespace

Conversation

@Xayar145

Copy link
Copy Markdown

Summary

Improves role name validation in camel.societies.workforce.utils.is_generic_role_name():

  • Treats empty strings ("") and whitespace-only strings as generic role names so that fallback logic to agent title/description is correctly triggered.
  • Strips leading and trailing whitespace before checking against GENERIC_ROLE_NAMES to handle inputs like " worker ".
  • Added unit test test_is_generic_role_name to test/workforce/test_workforce.py.

Motivation

When initializing workforce workers without an explicit role or with whitespace strings, is_generic_role_name("") previously returned False because "" was not in GENERIC_ROLE_NAMES. This prevented the fallback identifier resolution from executing and caused empty folder paths during workflow organization.

Changes

  • camel/societies/workforce/utils.py: Strip whitespace and return True for empty/whitespace inputs in is_generic_role_name().
  • test/workforce/test_workforce.py: Added test cases for standard, whitespace-padded, and empty role name handling.

Tests

  • Added test_is_generic_role_name covering case variations and edge cases.

@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

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.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 229e8880-4fb1-4ec6-ab52-557a892dee33

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

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

True
"""
return role_name.lower() in GENERIC_ROLE_NAMES
if not role_name or not str(role_name).strip():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran this against b5d93ed in a clean python:3.10-slim container. Neither new branch is reachable from the three call sites in the package: all three pass a name that ContextUtility.sanitize_workflow_filename has already rewritten.

role_name        sanitize()      is_generic_role_name()   same on master 13dc7a7d
''            -> 'agent'      -> True                     True
'   '         -> '___'        -> False                    False
'  worker  '  -> '__worker__' -> False                    False

sanitize_workflow_filename returns agent when the sanitized string is empty (context_utils.py:308-309), and agent is already in GENERIC_ROLE_NAMES, so the empty case fell back correctly before this change. Spaces become underscores before the function is called, so .strip() finds no whitespace to remove and " " still resolves to a folder named ___, which is the symptom in your motivation.

The call sites are workforce.py:738 and :747 (both sanitize_workflow_filename output) and workflow_memory_manager.py:1368 (_get_sanitized_role_name, same function). I enumerated them with an ast pass over the package rather than a grep; the only other calls are the seven in your new test, which pass raw strings.

Moving the strip one level down, into sanitize_workflow_filename at line 301, gets both of your examples through the real path:

'   '        -> 'agent'  -> True
'  worker  ' -> 'worker' -> True

That was name.strip().lower().replace(" ", "_"), run at your head with the rest of the PR in place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirmed at 0b3f6fbb. Both of your motivating cases now reach the guard through the real call path, and the normal case is unchanged:

''             -> 'agent'        -> True
'   '          -> 'agent'        -> True
'  worker  '   -> 'worker'       -> True
'\t\n'         -> 'agent'        -> True
'Data Analyst' -> 'data_analyst' -> False

Ran in a clean python:3.10-slim container against sanitize_workflow_filename and is_generic_role_name as they stand at that SHA.

@Xayar145

Copy link
Copy Markdown
Author

Thanks @ebarkhordar for the detailed trace! You're completely right that sanitize_workflow_filename runs before is_generic_role_name at those call sites and turned " " into "___".

I've updated ContextUtility.sanitize_workflow_filename with name.strip().lower().replace(" ", "_") so that leading/trailing and all-whitespace strings properly strip down to empty and fall back to "agent".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants