sangkyoonnam opened a new pull request, #1444: URL: https://github.com/apache/flink-agents/pull/1444
Linked issue: #1438 ### Purpose of change `ModelRouter.of(...).build()` keeps working and routed requests behave as before. The api `ModelRouter` becomes a declaration only, and the router resource moves to `flink_agents.plan.routing.model_router`, the split the Java side already made. #### Runtime flow The builder checks its arguments through `RouterDeclaration` and writes a descriptor whose class is the `ResourceName.ModelRouter.MODEL_ROUTER` placeholder. `validate_routing` runs when a plan is built (`from_agent`) and when one is restored from JSON (a new `AgentPlan` after-validator). It applies the declaration rules to each Python router and replaces the placeholder with the plan-side `ModelRouter`, which only decodes its arguments. In the chat action, `_chat_resolved` hands the candidate loop to `plan/routing/executor.py`: `execute` tries the route's candidates in order, sends the fallback `ModelRoutingEvent` and stores the routing metadata; `ModelRoutingExecutionException` carries every candidate's cause back to the action, which builds the failed `ChatResponseEvent` as before. Retry stats move to `plan/actions/chat_retry_stats.py`. #### Key decisions - The placeholder keeps its old value, the api class path, so existing descriptors, YAML and serialized plans resolve without a migration step. - Python replaces only the placeholder class. Java replaces any class a Java descriptor names; Python keeps the narrower rule so a router declared with a custom `clazz:` loads as before. - The restore validator passes Java routers through (`allow_java=True`); the Java plan validates them and Python never provides them. - `decision_model_action` keeps its own copy of the retry-metrics helper; folding it into the new one is a follow-up. ### Behavioral Semantics #### Interaction decisions | Descriptor | Build (`from_agent`) | Restore (JSON) | | --- | --- | --- | | Python, placeholder class | Rules checked, provided as the plan resource | Same | | Python, other class | Rules checked, class kept | Same | | Java | `TypeError` | Kept unchanged | | Invalid arguments | `ValueError` | `ValueError` | #### Behavioral contracts and failure behavior 1. `ModelRouter.of(...).build()` fails at the call site on an invalid declaration, as before. 2. A restored plan with an invalid Python router fails at `AgentPlan.model_validate`. 3. The plan-side resource does not repeat the declaration checks. 4. A routed request emits the same fallback event, response metadata and final failure message as before. Metadata and retry stats stay under the same sensory-memory keys, so checkpointed state restores. 5. Constructing the api `ModelRouter` directly raises `TypeError`. ### Tests | Contract | Tests | | --- | --- | | Placeholder provided as the plan resource, on build and on restore | `test_plan_provides_declarations_as_the_plan_resource`, `test_restored_plan_naming_the_declaration_is_provided_as_the_resource` | | Java router kept on restore | `test_restored_plan_keeps_a_java_router` | | Invalid restored router | `test_restored_plan_with_an_invalid_router_fails` | | Resource decodes only | `test_resource_decodes_without_repeating_declaration_checks` | | Declaration not instantiable | `test_declaration_is_not_a_resource` | | Executor: selected answer, fallback event, direct route, every candidate failed | `test_routing_executor.py` | | Routed chat requests, YAML routers, recovery | Existing suites, unchanged apart from importing the plan-side resource where they built or typed it; `AgentPlanCrossLanguageTest` reads the updated Python plan snapshot | `pytest flink_agents/api flink_agents/plan flink_agents/runtime/tests`: 2459 passed. The 4 failures are `test_resource_name_python_classes_exist[DecisionModel.*]` and fail on main in this environment too (optional packages not installed). Ruff 0.11.13 passed. The Python plan snapshot now names the plan module. Not verified: a Python router declared with a custom `clazz:` (no test on main either). No live provider or Flink cluster was used. ### API `ModelRouter.of(...).build()` is unchanged. `ModelRouter` is no longer a pydantic `Resource` to subclass, and constructing it directly raises `TypeError`. `RouterDeclaration` in the same module holds the declaration rules the builder and the plan share; it is framework-facing. Serialized Python plans now name `flink_agents.plan.routing.model_router` as the router module; plans that name the old class still restore. ### Documentation - [x] `doc-not-needed` ### Was this patch authored or co-authored using generative AI tooling? - [x] Yes - [ ] No Generated-by: Claude Code 2.1.331 (Claude Opus 5.5) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
