Skip to content

feat(go): presets and embedding shortlist - #1307

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
keemsisi:feat/go-presets-shortlist
Oct 23, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
keemsisi:feat/go-presets-shortlist

Conversation

@keemsisi

Copy link
Copy Markdown
Contributor

Based on main.

What and why

Part What
presets Triage, Email, EmailWith, Guard, Moderation, Router, All, StateField: laya/presets.py
shortlist Rank, RankWithInstructions, Choice, Predict, QueryText: shortlist_choice and predict_shortlist
shortlist Cached / CachedEmbedder (Embed, Info, Clear): cached_embed_fn
schema Question.Subset(indices): _subset_criteria for a typed question; drops the option order
CI goldens presets.json and shortlist.json regenerated per commit; floor 1603 -> 1667

Out of scope: predict_tournament, embed_fn_from_agent (the ONNX session exposes only the decision head), the router, calibration.

Differences from Python

  • A question is a schema.Question and a call's keywords are agent.CallOption; Predict takes any Predictor (*agent.Agent).
  • An Embedder is func(ctx, []string) ([][]float64, error). Python's "not callable", "not an array" and "wrong ndim" refusals reduce to a nil embedder (only when a ranking is needed), a wrong row count, an empty row, or rows of different widths (ErrEmbedding).
  • k must be an int >= 1 (ErrInvalidArgument); Python's bool and non-int refusals cannot occur. Empty, duplicated or wrongly typed criteria are refused when the Question is built.
  • Python's RuntimeWarning for a dropped option_order is Shortlisted.Warnings, same wording.
  • Ranking.Labels are schema.Label (the original key type); Indices adds their positions.
  • EmailWith takes []schema.Option (a Go map has no order); empty or nil is the defaults, as Python's categories or {...}.
  • StateField takes typed questions, which always have instructions; Python raises on a definition without them.
  • predict_tournament is not ported.
  • Cached stores float32 rows as Python does; a drift in dimension is refused as Python refuses it.

Evidence

Check Result
Lane (1.27.1 race, 1.22.12, committed goldens) 1667 run, 1667 pass, 0 skipped, 0 failed; floor+1 fails
gofmt, vet, staticcheck clean
Mutants 24 compiled, 24 killed
Scanners, review pending

Goldens (all from Python)

Golden Covers
presets.json every preset equal after canonicalization (email is_phishing criteria order, guard topic criteria list vs dict; the model input is identical), custom and empty email categories, state_field per preset and 9 hostile specs
shortlist.json 15 ranking cases (passthrough, ties, structured/empty/None/non-Latin state, list criteria, instructions), NaN ranking, 10 cosine vectors incl. underflow, 4 non-finite cases, cache counters, 12 refusals (each mapped or marked n/a), 4 predict_shortlist cases
go_extra.json shortlist_order option_order kept on passthrough and non-choice, dropped and warned on a narrowed choice, one warning per question

Real-weights e2e (multilingual fused graph, hashing embedder laya-rust/tools/hashing_embedder.py)

Check Result
Go shortlist.Predict vs Python predict_shortlist on ONNXAgent: 5 states x k in {1,3,5,8,17,99} x {plain, cached} = 60 calls, 6 choice questions, one with option_order, plus noul and score 2220 labels equal; scores within ~2 ulp of Python in general (summation order vs numpy pairwise/BLAS) and exactly equal on this rig; 2820 answer numbers max dev 0; 30 warnings
Cache counters after the sweep size 44, hits 661, misses 44, equal to Python
StateField on triage and router equal
Tamper (k + 1 in Go) 747 mismatches, FAIL

Mutants

Behaviour Mutant Killed by
stable, descending order tie-break reversed TestNaNRankingMatchesPython
NaN ranks last NaN first TestNaNRankingMatchesPython
signed cosine rank by magnitude TestRankingCasesMatchPython
cosine clip clip removed TestCosineIsClipped
non-finite read as 0 kept TestNonFiniteRowsBecomeZero
passthrough at k >= n k > n TestRankingCasesMatchPython
passthrough indices reversed TestRankingCasesMatchPython
passthrough needs no embedder embedder called TestRankingCasesMatchPython
k >= 1 k < 0 TestRefusalsMatchPython
query first, then options order swapped TestRankingCasesMatchPython
instructions + newline + state space TestRankingCasesMatchPython
Predict sends the narrowed question full question TestPredictMatchesPython
option_order warning removed TestOptionOrderDroppedLikePython
Subset drops the order kept TestSubsetKeepsOptionsAndDropsOrder
cache counters on one basis misses per distinct TestCacheCountersMatchPython
LRU refresh on hit removed TestCacheRowsAndEviction
cache dimension check removed TestCacheRefusals
no fused multiply-add in cosine float64() conversion removed TestCosineBlocksFusedMultiplyAdd (fails on arm64 only; on amd64 it is equivalent; the case values make it deterministic there)
cache float32 overflow is 0 overflow kept as Inf TestCacheFloat32OverflowIsZero
preset wording one word TestPresetsMatchPython
empty categories = defaults == nil TestEmailCategories
findall resumes after match resumes inside TestStateFieldScansLikeFindall
Python \w ASCII only TestStateFieldScansLikeFindall
exactly one field count < 1 TestStateFieldMatchesPython

Scanners and review

pending

Ports laya/presets.py and the embedding shortlist of laya/shortlist.py
(Rank, Predict, Cached, StateField, presets) with Python goldens, a
real-weights e2e against ONNXAgent, and CI floor 1603 -> 1667. One commit so
the regenerated goldens and the tests that read them land together.
@NandhaKishorM
NandhaKishorM merged commit 694527f into NandhaKishorM:main Oct 23, 2026
50 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 23, 2026
Three things my conflict resolution got wrong across #1306, #1307, #1308, #1309,
#1310 and #1311, which all touch go.yml, README.md, gen_go_fixtures.py and
go_extra.json:

1. `shortlist_order_family` lost its `return {"cases": cases}`. The conflict
   boundary fell on that line, so the function built its cases and returned None,
   and go_extra.json carried `"shortlist_order": null`. Found by parsing the
   merged file and checking all 98 top-level names against the PR each came from;
   only this one and `build` failed, and `build` is the validated union of all
   23 keys.

2. go.yml lost four of #1309's parity steps (the samples module's golden check,
   build, test and no-cgo build). #1310 conflicted over the same hunk and my
   resolver kept one side. Restored; the merged file now holds every step name
   from every PR in the series and none that is not in one of them.

3. `build()` had `email_fuzz` ahead of `router`. They are the only two of the
   new families that draw from the shared `rng`, so that order re-drew the
   corpus #1311 recorded `router.signatures` from, and
   `router/golden_test.go` pins how many of those 107 signatures come out
   byte-identical to Python's: 42 instead of 50. Nothing was wrong with that
   constant, or with Go. Putting `router` first gives it the stream #1311
   measured, and `email_fuzz` is the one re-drawn, which it can be: it has no
   such constant, only a 1500-case count, and it still holds 1500.

With that, `gen_go_fixtures.py --check` passes and each PR's own key reproduces
verbatim from this tree: calibration (#1306), shortlist_order (#1307),
email_fold (#1308) and router (#1311). `go build`, `go vet` and `go test` are
green across all 12 packages.

`--min-tests` is left at 3869 as a floor rather than an exact count, because the
figure a full lane reports cannot be measured here: several goldens need ONNX
exports. The comment says so and asks for it to be tightened on the next green
run.

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

Copy link
Copy Markdown
Owner

Merged into 0.4.14.

Presets and the embedding shortlist together is a sensible pair: the shortlist is the thing the docs tell people to reach for past roughly 20 options, and a port that has presets but no way to narrow a large label space leaves the user at exactly the point where the model degrades.

shortlist/cached.go existing as its own file matters. Re-embedding a fixed label set on every call is the obvious way to write this and the wrong one, and having the cache be a visible component rather than a hidden optimisation means the next person can see the contract.

You should know that my resolution of the gen_go_fixtures.py conflict dropped the return {"cases": cases} from the end of shortlist_order_family. The conflict boundary fell on that exact line. The function went on building its five cases and returning None, go_extra.json carried "shortlist_order": null, and nothing failed, because the Go side reads that key only through a fixture accessor. I found it by parsing the merged file and checking all 98 top-level names against the PR each came from. It is fixed, your fixture reproduces verbatim, and I will not resolve a Python conflict mechanically again without that check.

Green: go build, go vet, go test across all 12 packages.

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