Skip to content

feat(go): router sample on the presets package - #1395

Merged
NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
keemsisi:feat/go-sample-presets
Oct 28, 2026
Merged

NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
keemsisi:feat/go-sample-presets

Conversation

@keemsisi

Copy link
Copy Markdown
Contributor

laya-go: router sample on the presets package

Adds laya-go/samples/router, a runnable port of examples/29_presets_model_router.py: three requests go through presets.Router(), then a tier policy (sensitive or tools overrides difficulty) prints the side-by-side table, the policy lines and the closing notes. Same nested module as quickstart and benchmark; the root stays cgo-free.

Differences from Python

Python example 29 Go
Questions laya.router_questions() presets.Router()
State {"request": ...} dict lang.Object{{Key: "request", ...}} (a map would sort keys)
Answers dict lookups typed: *ScoreAnswer, *ChoiceAnswer, *NoulAnswer; a wrong type is an error, not a zero
Domain argmax max(items, key=value) first label with the highest probability, the same tie rule
Output banner, per-request describe, table, policy table, policy, notes (no banner or describe)
Empty probabilities raises domain "" (unreachable with the router preset)
Loader load("english") onnx.Open(graph) + agent.Open(os.DirFS(checkpoint), sess)

Tests (internal/gateway, no weights)

  • TestPolicyAndRowMatchPython: 10 cases against testdata/policy.json. The golden is recorded by samples/scripts/gen_router_policy_golden.py [--check], which extracts route_tier and the table-row expression from examples/29_presets_model_router.py with ast (asserting both are present), so a change to the example fails --check. Three thresholds: difficulty 1.5 (at-edge and just-under), needs_tools 0.5 (at-edge and just-under 0.4999), is_sensitive 0.5 (at-edge, inside sensitive-overrides-tools; no just-under case). Branch order: tools-overrides-hard, sensitive-overrides-hard, sensitive-overrides-tools, plus a probability tie in the domain argmax.
  • TestReadRejectsWrongAnswerTypes, TestReportShape.
  • Mutants, each compiling and failing TestPolicyAndRowMatchPython: >= 1.5 to > 1.5; is_sensitive >= 0.5 to >; domain tie takes the last label; sensitive and tools branches swapped; tools and difficulty branches swapped; difficulty branch moved first.
  • CI: the samples module has no test step upstream (only staticcheck and govulncheck, both cover the new files), so no --min-tests floor moves and the root lane is untouched.

Real-weights e2e (english checkpoint, onnx/english graph, ORT 1.30.0, arm64 CPU)

The Python side runs the example's own source (loader swapped for ONNXAgent on the same graph, banner and describe dropped). ref.py rewrites Python's headings into the Go form; diff of the two outputs after that reformat: empty (16 lines).

request difficulty domain needs_tools is_sensitive tier
trivial lookup 1.25 / 3 factual_lookup (0.88) 0.124 0.000 small
hard refactor 2.08 / 3 code (0.98) 0.162 0.053 frontier
sensitive legal 1.43 / 3 writing (0.44) 0.028 0.853 frontier

Identical in Go and Python at the printed precision.

Related: router lifecycle #1374 (the router preset's lifecycle half is separate); siblings merged: #1306 to #1311.

Port of examples/29_presets_model_router.py: presets.Router() over three
requests, then the tier policy. internal/gateway holds the policy and the
printing; its test compares 7 cases with a Python-recorded golden
(scripts/gen_router_policy_golden.py). Real-weights output (english graph)
is identical to the Python example's.
…mple

Golden gains tools-overrides-hard, sensitive-overrides-hard and
tools-just-under; swapping the tools/difficulty branches or moving difficulty
first now fails TestPolicyAndRowMatchPython. The golden generator takes
route_tier and the row expression from examples/29 with ast instead of a copy.
@NandhaKishorM
NandhaKishorM merged commit 3de567d into NandhaKishorM:main Oct 28, 2026
50 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 28, 2026
24 PRs. Vietnamese kept off the English checkpoint (#1392), deciding-window
attribution preserved through end hooks (#1369), RemoteRouter identifying
itself with a configurable User-Agent (#1396), ten more inputs refused where
they enter (#1372, #1394, #1397, #1398, #1400, #1401, #1402, #1403, #1404,
#1405, #1406), per-language temperatures and per-call budgets in .NET and
Rust (#1370, #1371), .NET calibration files (#1384), the Go router lifecycle,
SHA-256 pins and routed inference (#1374, #1379, #1395), a C++ foundation
(#1399, #1407), and parity-lane sharding that replaces the budget headroom
added for 0.4.16 (#1373, #1375).

Verified before tagging: all 92 suites pass, 640 tests, up from 612. ruff
clean on both lists. laya-ts 1251 tests across 44 files, tsc --noEmit clean.
Every generated-artifact gate clean on CPython 3.12 / unicodedata 15.0.0:
Java 20 families match laya (9 unverified), UnicodeTables matches CPython
15.0.0, LanguageTables matches laya/lang.py, three Go tables_gen.go match,
both laya-cpp .inc files match. Version agrees across pyproject.toml,
laya.__version__ and nix/package.nix. README carries 0.4.17 only.

Security audit of the merge diff, 362766 added lines over 480 files: zero
obfuscation, zero secrets, zero boundary hits, zero binaries, no mode or
symlink changes, 20 of 20 actions SHA-pinned including the new cpp.yml, npm
install hooks unchanged, and no third-party host in any added line
(example.com 90, github.com 2, download.pytorch.org 1, docs.rs 1). The 12
dynamic-exec hits are the C++ test harness invoking its own checker through
sys.executable, the Go sample generator reading laya's own example by AST at
development time, and one comment in rust.yml.

Held: #1364 (unchanged, it moves a cross-port contract), #1381 (a #[doc(hidden)]
but pub Rust hook that would ship as public API able to replace the forward
pass), #1386 (route_batch switched from self.route to self._route, silently
bypassing subclass overrides the docstring promises), and nine stacked port
PRs that conflict with each other and need linearizing (#1377, #1378, #1380,
#1382, #1383, #1385, #1387, #1388, #1389, #1390).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged. Building the router sample on the presets package rather than hand-rolling its configuration is the correct dependency direction for a sample: it demonstrates the API people should reach for, and it breaks when that API changes, which is exactly what you want from example code. A sample with its own private setup drifts into being wrong documentation.

Generating the policy golden from a script, with the fixture committed next to the test, keeps the sample's expectations checkable instead of decorative. testdata/policy.json sitting beside gateway_test.go is the idiomatic Go placement for it.

Shipped in 0.4.17.

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