Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions contextual_orchestrator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,16 @@ def _auto_discover_runtime_agents(orchestrator: TaskOrchestrator) -> dict[str, l
return orchestrator.sync_discovered_agents(agents)


def main() -> None:
def main(argv: list[str] | None = None) -> None:
"""Parse CLI options and run bootstrap, prompt completion, or the HTTP server."""
if len(sys.argv) > 1 and sys.argv[1] == "register-credential":
_register_credential_command(sys.argv[2:])
arguments = list(sys.argv[1:] if argv is None else argv)
if arguments and arguments[0] == "register-credential":
_register_credential_command(arguments[1:])
return
if len(sys.argv) > 1 and sys.argv[1] == "discover-models":
_discover_models_command(sys.argv[2:])
if arguments and arguments[0] == "discover-models":
_discover_models_command(arguments[1:])
return
if len(sys.argv) > 1 and sys.argv[1] == "check-fast-mlsirm":
if arguments and arguments[0] == "check-fast-mlsirm":
_check_fast_mlsirm_command()
return

Expand Down Expand Up @@ -336,7 +337,7 @@ def main() -> None:
action="store_true",
help="discover provider models at startup and activate the discovered agents",
)
args = parser.parse_args()
args = parser.parse_args(arguments)

client = ModelClient(
ca_bundle=args.provider_ca_bundle,
Expand All @@ -357,6 +358,8 @@ def main() -> None:
)
if args.auto_discover_model_agents:
_auto_discover_runtime_agents(orchestrator)
if args.auto_discover_model_agents:
_auto_discover_runtime_agents(orchestrator)
Comment on lines +361 to +362

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.

🟡 Startup model discovery runs twice, doubling provider queries

When automatic model discovery is requested, the discovery routine is invoked twice back-to-back (_auto_discover_runtime_agents(orchestrator) at contextual_orchestrator/__main__.py:361-362) instead of once, so the server contacts every provider and re-syncs agents a second time for no reason.
Impact: Every server startup with auto-discovery enabled makes a redundant full round of provider network calls and agent syncing, wasting time and provider quota and doubling the chance of a startup failure.

Duplicated conditional block introduced by the PR

The original code had a single guarded call. This PR added an identical second copy:

    if args.auto_discover_model_agents:
        _auto_discover_runtime_agents(orchestrator)
    if args.auto_discover_model_agents:
        _auto_discover_runtime_agents(orchestrator)

_auto_discover_runtime_agents (contextual_orchestrator/__main__.py:254-262) calls discover_all_models() (provider queries) and orchestrator.sync_discovered_agents(...), both of which now execute twice. This duplication is unrelated to the PR's stated intent (passing explicit argv) and is almost certainly an accidental bad merge/edit.

Suggested change
if args.auto_discover_model_agents:
_auto_discover_runtime_agents(orchestrator)
if args.auto_discover_model_agents:
_auto_discover_runtime_agents(orchestrator)
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.


if args.conduct_hint_threshold is not None or args.route_text_length_threshold is not None:
overrides: dict[str, int] = {}
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def test_key_only_split_tokens_select_split_mode() -> None:
set_backend(None)


def test_main_accepts_explicit_argv_without_mutating_process_arguments() -> None:
original_argv = sys.argv[:]
with patch("contextual_orchestrator.__main__.serve") as serve:
main(["--serve", "--auth-token", "argv-token"])

assert sys.argv == original_argv
security = serve.call_args.kwargs["security"]
assert security.auth_token == "argv-token"


def test_invalid_local_provider_options_fail_at_parser_boundary() -> None:
invalid_options = (
(["--local-concurrency", "0"], "positive integer"),
Expand Down
Loading