@devries-cameron20 / composio Mirror · read-only / commits / 2c4339a85
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 13:30 UTC

fix(python): preserve mixed multi-execute result order (#4310)

## Summary `ToolRouterSession._route_multi_execute` currently concatenates remote results before local results and assigns new indexes from that concatenated list. For a request such as `[LOCAL_TOOL, REMOTE_TOOL]`, callers receive `[REMOTE_TOOL, LOCAL_TOOL]`, so code that correlates `results[index]` with the requested tools can use the wrong result. This brings the Python implementation in line with the merged TypeScript behavior in [#3800](https://github.com/ComposioHQ/composio/pull/3800): preserve each tool's original request index, restore that order after local/remote execution, and then assign contiguous result indexes. Fixes # ## Changes - Preserve the original index on locally executed result entries. - Map remote sub-batch results back to their original request indexes before merging. - Sort the merged results by original index and re-index them sequentially. - Update the mixed local/remote regression test to assert request order and indexes. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? - `uv run --locked --group dev pytest tests/test_custom_tools.py -q` — 69 passed. - `uv run --locked --group dev nox -s tst -- tests/test_custom_tools.py` — 69 passed. - `uv run --locked --group dev ruff --config config/ruff.toml check composio/core/models/tool_router_session.py tests/test_custom_tools.py` — passed. - Ruff format check on both changed files — passed. - Verified the regression test fails on the pre-fix implementation and passes after the fix. ## Screenshots (if applicable) Not applicable. ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [ ] I updated documentation as needed - [x] I added tests or explain why not applicable - [ ] I added a changeset if this change affects published packages ## Additional context This change is limited to Python multi-execute result ordering. All-local and all-remote fast paths remain unchanged. No changeset is needed because this repository does not use Changesets for Python package changes. Signed-off-by: CoralGarden52 <2193436736@qq.com>

CoralGarden52 committed Sep 8, 2026 at 22:28 UTC 2c4339a859dcf516bbeb83669955186df51108d5
2 files changed +19 -10
python/composio/core/models/tool_router_session.py
+14 -7
@@ -415,8 +415,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
415 """Route a COMPOSIO_MULTI_EXECUTE_TOOL call.
416
417 Splits the tools[] array into local and remote, executes each
418 - appropriately, and merges results: remotes first, locals appended
419 - (matches TS — remote results may have workbench index references).
418 + appropriately, and merges results in the original request order.
419
420 Modifiers are NOT applied here — the caller (routing_execute)
421 handles before_execute/after_execute to avoid double application.
@@ -506,14 +505,14 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
505 "data": result["data"],
506 },
507 "tool_slug": parsed[idx]["tool_slug"],
508 + "index": idx,
509 }
510 if result.get("error"):
511 local_entry["response"]["error"] = result["error"]
512 local_entry["error"] = result["error"]
513 local_entries.append(local_entry)
514
515 - # Merge: remotes first, locals appended (matches TS behavior —
516 - # remote results may have workbench index references)
515 + # Restore original request order, then re-index sequentially.
516 remote_data_raw = (remote_result or {}).get("data")
517 remote_data = remote_data_raw if isinstance(remote_data_raw, dict) else {}
518 remote_results_list = (
@@ -521,10 +520,18 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
520 if isinstance(remote_data.get("results"), list)
521 else []
522 )
524 - all_results = [
525 - {**entry, "index": i}
526 - for i, entry in enumerate([*remote_results_list, *local_entries])
523 + merged_results = [
524 + {
525 + **entry,
526 + "index": remote_indices[position]
527 + if position < len(remote_indices)
528 + else position,
529 + }
530 + for position, entry in enumerate(remote_results_list)
531 ]
532 + merged_results.extend(local_entries)
533 + merged_results.sort(key=lambda entry: int(entry["index"]))
534 + all_results = [{**entry, "index": i} for i, entry in enumerate(merged_results)]
535 failed = sum(1 for r in all_results if r.get("error"))
536 merged_data = {**remote_data, "results": all_results}
537 if local_entries and any(
python/tests/test_custom_tools.py
+5 -3
@@ -1294,9 +1294,11 @@ class TestMultiExecuteRouting:
1294 )
1295 results = result["data"]["results"]
1296 assert len(results) == 2
1297 - # Remotes first, locals appended (matches TS)
1298 - assert results[0]["tool_slug"] == "R"
1299 - assert results[1]["tool_slug"] == "GREP"
1297 + # Results preserve the original request order.
1298 + assert results[0]["tool_slug"] == "GREP"
1299 + assert results[1]["tool_slug"] == "R"
1300 + assert results[0]["index"] == 0
1301 + assert results[1]["index"] == 1
1302 assert result["data"]["total_count"] == 2
1303 assert result["data"]["success_count"] == 2
1304 assert result["data"]["error_count"] == 0