Skip to content

[AAASM-1183] ✨ (crewai): Add team_id, delegation_reason, and hierarchical Crew.kickoff patch#40

Merged
Chisanan232 merged 4 commits into
masterfrom
v0.0.1/AAASM-1183/feat/crewai_team_id_delegation
May 9, 2026
Merged

[AAASM-1183] ✨ (crewai): Add team_id, delegation_reason, and hierarchical Crew.kickoff patch#40
Chisanan232 merged 4 commits into
masterfrom
v0.0.1/AAASM-1183/feat/crewai_team_id_delegation

Conversation

@Chisanan232

Copy link
Copy Markdown
Contributor

What changed

Extends the CrewAI adapter with three spawn lineage enrichments:

  1. team_id — added to SpawnContext; set to str(crew.id) when a task is delegated within a Crew (resolved via task.agent.crew.id).
  2. delegation_reason — set to task.description[:256] (None when empty) on every delegated task execution.
  3. Crew.kickoff hierarchical hook — patches Crew.kickoff to wrap the entire hierarchical execution in a spawn_context_scope carrying the manager agent's ID, spawned_by_tool="crewai_kickoff_hierarchical", and team_id. Tasks executed within the kickoff inherit the outer context and increment depth correctly.

Also fixes pre-existing UP035, B010 violations in crewai/patch.py.

Why it changed

AAASM-1183: the flat-process task.execute_sync path was already intercepted (AAASM-1181), but team_id, delegation_reason, and the hierarchical manager delegation loop were missing.

Changes

  • agent_assembly/core/spawn.pyteam_id: str | None = None field on SpawnContext
  • agent_assembly/adapters/crewai/patch.py
    • _load_crewai_crew_class, _extract_crew_team_id, _extract_manager_agent_id, _is_hierarchical_process helpers
    • patched_execute_sync enriched with team_id and delegation_reason
    • _apply_crew_kickoff_patch / _revert_crew_kickoff_patch for hierarchical process
    • CrewAIPatch.apply/revert wired to include crew kickoff lifecycle
  • test/unit/adapters/crewai/test_crewai_spawn_context.py — 23 new cases across 5 new test classes
  • test/integration/test_spawn_lineage_integration.py — Test 8: hierarchical kickoff end-to-end

How to verify

uv sync
.venv/bin/python -m pytest test/unit/adapters/crewai/test_crewai_spawn_context.py -v
.venv/bin/python -m pytest test/integration/test_spawn_lineage_integration.py::test_crewai_hierarchical_kickoff_sets_team_id_and_spawn_ctx -v
.venv/bin/python -m pytest test/ -q  # 302 passed, 9 skipped
.venv/bin/ruff check agent_assembly/adapters/crewai/patch.py
.venv/bin/mypy agent_assembly/adapters/crewai/patch.py

Related

Closes AAASM-1183
Refs AAASM-212 (parent story)

Adds team_id: str | None = None to carry the crew/team identifier
through the spawn lineage chain. Required by AAASM-1183 for CrewAI.

Closes AAASM-1183
…ical patch

- team_id: str(crew.id) extracted via task.agent.crew path in patched_execute_sync
- delegation_reason: task.description[:256] (None when empty) on every delegated task
- _apply_crew_kickoff_patch / _revert_crew_kickoff_patch: wraps Crew.kickoff with
  spawn_context_scope when Process.hierarchical detected; sets team_id and
  spawned_by_tool="crewai_kickoff_hierarchical"
- CrewAIPatch.apply/revert wired to include crew kickoff lifecycle
- Fix pre-existing UP035, B010 violations in crewai/patch.py
- TestTaskExecuteSyncSpawnContext: +5 cases for team_id from agent.crew,
  team_id=None when no crew, delegation_reason propagation, 256-char truncation,
  None when description empty
- TestExtractCrewTeamId: 4 cases (str conversion, None crew, no id attr, non-str id)
- TestExtractManagerAgentId: 3 cases
- TestIsHierarchicalProcess: 2 cases (sequential bypass, ImportError guard)
- TestApplyCrewKickoffPatch: 5 cases (non-hierarchical bypass, hierarchical sets ctx,
  ctx reset after kickoff, idempotent apply, revert restores original)
Test 8: Crew.kickoff (hierarchical) sets _SPAWN_CTX with team_id="crew-team-42",
parent_agent_id from manager_agent, spawned_by_tool="crewai_kickoff_hierarchical"
at depth=1. Delegated tasks within the kickoff see depth=2, team_id propagated,
delegation_reason from task.description. _SPAWN_CTX reset to None on exit.
@codecov

codecov Bot commented May 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.01299% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
agent_assembly/adapters/crewai/patch.py 86.84% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud

sonarqubecloud Bot commented May 9, 2026

Copy link
Copy Markdown

@Chisanan232 Chisanan232 merged commit a4bda2a into master May 9, 2026
24 checks passed
@Chisanan232 Chisanan232 deleted the v0.0.1/AAASM-1183/feat/crewai_team_id_delegation branch May 9, 2026 04:21
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.

1 participant