Skip to content

feat(router): add durable automatic model selection - #892

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
zuyu:codex/model-router-stage3-auto
Sep 28, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
zuyu:codex/model-router-stage3-auto

Conversation

@zuyu

@zuyu zuyu commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add opt-in automatic model selection for Server-catalog chat turns. A confidently easy, read-only task can use an operator-qualified economy Offering; difficult, uncertain, unsupported, or incompatible cases retain the strong Offering. This gives API and SDK users a conservative way to reduce inference cost without changing explicit model selection.

The shared runtime persists the selected model and its semantic admission before primary inference, reauthorizes execution, and propagates the choice to child runs. Auto selection starts or awaits the bounded assessment under the default auxiliary policy, so it does not silently pin every request to strong. Learned routing and mid-turn escalation are outside this PR.

Related issue

No linked issue. Follows the next-turn assessment work in #888.

Change type

  • Feature
  • Bug fix
  • Documentation
  • Refactor or performance improvement
  • Test
  • Build, CI, or maintenance

User and compatibility impact

  • Configure an operator-qualified economy/strong pair and revision in [model_routing] in the Server runtime configuration. Routing is disabled by default; this PR does not establish measured savings or qualify model pairs automatically.
  • HTTP chat clients opt in with execution_policy.model_routing = "auto" and omit model_selection. The TypeScript SDK accepts modelSelection: "auto".
  • Explicit model selection and Server-default Work selection keep their existing behavior and default request serialization. Auto rejects conflicting explicit selections, provider model gateways, bound Work requests, and fixed-default semantic admission.
  • Auto uses the existing judgment boundary under the default capacity-aware policy, boundary_only, and always. Disabled or unusable assessments retain strong. Ordinary non-Auto turns keep their existing admission timing.
  • No database schema migration is required. Deploy Server support before clients send the new opt-in field. CLI/Web selectors and WebSocket chat remain explicit.

Architecture and complexity delta

  • Canonical owner changed or extended: pure selection policy in astra-turn-core; typed routing configuration in astra-turn-types; existing Offering admission and shared agent loop in runtime; generation-fenced run-event transaction in services.
  • Existing implementations and callers searched: primary HTTP admission, semantic Work admission, model revalidation, shared turn preparation, dynamic-agent/forked-skill inheritance, in-memory/database run stores, SDK serialization, and their tests.
  • Superseded code, states, tables, shims, or self-only tests removed: none. No new table or parallel execution loop. Reuse the existing semantic judge, admission authority, event ledger, and recovery checks.
  • If parallel implementations remain, their boundary and retirement condition: none introduced. Explicit selection, Server-default selection, and opt-in Auto converge on the same admitted execution contract. Existing in-memory and database stores implement the same fenced transaction contract.

The immutable routing event records policy and algorithm versions, input reference, assessment, selected Offering/model, contract digest, and semantic Work decision. The transaction updates the effective run model alongside that event. Persistence failure, stale execution ownership, revoked access, or changed model contracts prevent dispatch. Credentials and endpoints are not stored in the routing decision.

Verification

  • Commands and results:
    • cargo test --locked --offline -p astra-runtime --lib --no-default-features model_routing -q — 8 passed. The entrypoint test covers default, explicit capacity-aware, boundary-only, always, disabled, and malformed-assessment cases, including no duplicate assessment on later preparation.
    • cargo test --locked --offline -p astra-runtime --lib --no-default-features ordinary_primary_turn_defers_work_admission_without_a_boundary -q — 1 passed.
    • cargo test --locked --offline -p astra-runtime --lib --no-default-features post_response_work_admission_is_projected_into_the_typed_graph -q — 1 passed.
    • cargo test --locked --offline -p astra-services --test runs_db_batch_it auto_model_routing_memory_commit_is_atomic_and_fenced -q — 1 passed.
    • From packages/sdk: npm test -- src/__tests__/client.test.ts — 85 passed.
    • cargo clippy --locked --offline -p astra-runtime --lib --tests -- -D warnings — passed on the final code.
    • make format-check and git diff --check — passed.
    • make check — passed on the rebased implementation before the final default-policy fix; that fix was subsequently verified by the targeted tests, Clippy, and formatting checks above.
    • The new default-policy regression failed before the fix with zero judgment calls instead of one, then passed after the fix.
    • cargo test --locked --offline -p astra-services --test runs_db_batch_it -- --skip auto_model_routing_database_commit_is_atomic_and_fenced — 2 passed: shared database-isolation contract and in-memory routing transaction; 21 database tests ignored.
    • With MATRIXONE_HOST=127.0.0.1, MATRIXONE_PORT=6001, local development credentials, ASTRA_DATABASE=ASTRA_TEST_DATABASE=astra_runtime_test_router_pr892_20260927_runtime_ignored (both variables set separately), ASTRA_DATABASE_PREFIX="", ASTRA_AUTO_CREATE_DATABASE=1, and ASTRA_TEST_DB_IT=1: cargo test --locked --offline -p astra-services --test runs_db_batch_it auto_model_routing_database_commit_is_atomic_and_fenced -- --ignored --exact --test-threads=1 — 1 passed against real MatrixOne in 0.90s.
  • Public entrypoint exercised: HTTP request DTO conversion and Auto admission; shared prepare_turn_iteration and execute_turn against a local mock provider; SDK request serialization. No live HTTP deployment or paid provider was exercised.
  • Unhappy paths exercised: missing policy, unauthorized Offering, conflicting explicit/provider/fixed selections, unavailable/incompatible/cross-billing economy candidates, unsupported input, disabled/malformed assessments, persistence fencing, missing resume decisions, changed contracts, revoked child inheritance, and credential rotation.
  • Database verification: no DDL or schema migration. The existing transaction atomically updates model identity with the immutable event. The shared atomicity/idempotency/fencing assertions pass against both the in-memory store and real MatrixOne 4.1.2, including rejected owner/session/generation/status, invalid baseline rollback, duplicate event replay, and conflicting decision rollback. The database test now uses the shared isolation guard, accepting CI lane database names while retaining explicit opt-in, designated/effective database matching, and protected-database rejection.

Final checklist

  • I added or updated tests at the layer that owns the behavior, or explained why no test is needed.
  • I updated public or design documentation for contract changes, or the change needs no documentation update.
  • I checked the diff for credentials, private URLs, customer data, generated files, and other sensitive information.
  • The PR title follows the repository's Conventional Commit format.

@zuyu
zuyu requested a review from XuPeng-SH as a code owner September 27, 2026 18:55

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deep review of 14dbe40: one correctness issue in the production child-inheritance wiring needs fixing. Auto admission and primary reauthorization support Genesis through ModelService, but the new dynamic-agent and forked-skill inheritance closures bypass that service and use the database-only resolver, so valid Genesis Auto runs cannot start these children.

Reviewed HTTP admission, shared-loop selection timing, semantic admission, durable decision/model updates, replay and contract checks, child execution, and SDK serialization. Five targeted source-executed SDK serialization/validation cases and git diff --check passed. Rust/Cargo and a MatrixOne test instance are unavailable in this environment, so I did not rerun the Rust or database suites. The inline finding is based on the concrete production call chain.

@zuyu
zuyu requested a review from XuPeng-SH September 28, 2026 02:50

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deep re-review of aa6dab5.

The previous Genesis/UC inheritance finding is fixed. Both dynamic-agent runtime-context resolution and forked-skill inheritance now use the injected ModelService, retaining the existing contract-digest check. The new regressions exercise those production resolver paths with a service-backed Offering and verify that revoked access is rejected.

I rechecked HTTP Auto admission, selection timing and semantic Work admission, candidate compatibility and billing identity, the atomic decision/model transaction, replay and contract revalidation, dynamic-agent/forked-skill/delegation inheritance, and SDK serialization. I found no additional actionable correctness issue.

Validation: local git diff --check passed. I inspected this head's CI logs: all 6,194 runtime tests passed, including both new Genesis inheritance tests and all eight model-routing tests; static checks and the turn-core/services/plan job also passed. Rust/Cargo and a MatrixOne instance are unavailable locally, so I did not rerun those suites here.

CI is not fully green: the online integration job reported 276 passed, one failure because partial-batch bootstrap exceeded its 10-second setup budget, and one session-artifact test timeout at 30 seconds. The logs do not establish a causal link to this PR. This code approval does not resolve those outstanding integration failures; they still need investigation or a successful rerun before merge.

@mergify mergify Bot added the queued label Sep 28, 2026
@mergify

mergify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-28 04:05 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-09-28 04:27 UTC · at c77012fd562cff4792ea2b39b23c4f4e11e46501 · squash

This pull request spent 21 minutes 29 seconds in the queue, including 20 minutes 57 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Check PR title
    • check-neutral = Check PR title
    • check-skipped = Check PR title
  • any of [🛡 GitHub branch protection]:
    • check-success = check
    • check-neutral = check
    • check-skipped = check
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: core crates + bridge hooks
    • check-neutral = Test: core crates + bridge hooks
    • check-skipped = Test: core crates + bridge hooks
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-runtime
    • check-neutral = Test: astra-runtime
    • check-skipped = Test: astra-runtime
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-fs-tools)
    • check-neutral = Test: astra-cli (edge-fs-tools)
    • check-skipped = Test: astra-cli (edge-fs-tools)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-git-gix)
    • check-neutral = Test: astra-cli (edge-git-gix)
    • check-skipped = Test: astra-cli (edge-git-gix)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-rest)
    • check-neutral = Test: astra-cli (edge-rest)
    • check-skipped = Test: astra-cli (edge-rest)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (edge-shell)
    • check-neutral = Test: astra-cli (edge-shell)
    • check-skipped = Test: astra-cli (edge-shell)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: astra-cli (non-edge)
    • check-neutral = Test: astra-cli (non-edge)
    • check-skipped = Test: astra-cli (non-edge)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: online (core)
    • check-neutral = Test: online (core)
    • check-skipped = Test: online (core)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: online (integration)
    • check-neutral = Test: online (integration)
    • check-skipped = Test: online (integration)
  • any of [🛡 GitHub branch protection]:
    • check-success = Test: turn-core + services + plan
    • check-neutral = Test: turn-core + services + plan
    • check-skipped = Test: turn-core + services + plan
  • any of [🛡 GitHub branch protection]:
    • check-success = web (typecheck, test, build)
    • check-neutral = web (typecheck, test, build)
    • check-skipped = web (typecheck, test, build)
  • any of [🛡 GitHub branch protection]:
    • check-success = Astra SDK (typecheck, test+coverage, build)
    • check-neutral = Astra SDK (typecheck, test+coverage, build)
    • check-skipped = Astra SDK (typecheck, test+coverage, build)

@mergify
mergify Bot merged commit c77012f into matrixorigin:main Sep 28, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Sep 28, 2026
@zuyu
zuyu deleted the codex/model-router-stage3-auto branch September 28, 2026 04:29
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