localarena 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.md +211 -0
- package/javascript/src/index.d.ts +90 -0
- package/javascript/src/index.js +522 -0
- package/package.json +54 -0
- package/schema/localarena-v1.schema.json +97 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to LocalArena are documented here.
|
|
4
|
+
|
|
5
|
+
The project follows [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-07-25
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Dependency-free Python and JavaScript packages under the shared
|
|
12
|
+
`localarena` name.
|
|
13
|
+
- Pairwise match recording with standard Elo updates.
|
|
14
|
+
- Deterministic round-robin and least-played pair scheduling.
|
|
15
|
+
- Rating-sorted standings with win, loss, draw, and match counts.
|
|
16
|
+
- Schema-v1 JSON snapshots that replay across both runtimes.
|
|
17
|
+
- JSON-safe contestant and match metadata.
|
|
18
|
+
- Python and Node.js behavioral suites, package build checks, and release
|
|
19
|
+
automation for PyPI and npm.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# localarena
|
|
2
|
+
|
|
3
|
+
Local-first pairwise evaluation, deterministic scheduling, and Elo ranking for
|
|
4
|
+
models, agents, prompts, algorithms, or anything else you want to compare.
|
|
5
|
+
|
|
6
|
+
[](https://pypi.org/project/localarena/)
|
|
7
|
+
[](https://www.npmjs.com/package/localarena)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
`localarena` is the small, dependency-free engine underneath a head-to-head
|
|
11
|
+
arena. You generate two outputs and decide which side won; LocalArena keeps the
|
|
12
|
+
match log, updates ratings, builds a fair next pairing, and gives you a portable
|
|
13
|
+
leaderboard.
|
|
14
|
+
|
|
15
|
+
- Same package name and concepts in Python and JavaScript
|
|
16
|
+
- No network calls, telemetry, model SDK, database, or implicit file writes
|
|
17
|
+
- Deterministic round-robin and least-played scheduling
|
|
18
|
+
- Standard Elo updates with wins, losses, and draws
|
|
19
|
+
- Versioned JSON snapshots that can move between both runtimes
|
|
20
|
+
- Detached metadata so callers cannot accidentally mutate arena state
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
Python:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install localarena
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
JavaScript or TypeScript (Node.js 18+):
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install localarena
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
37
|
+
|
|
38
|
+
### Python
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from localarena import Arena, Result
|
|
42
|
+
|
|
43
|
+
arena = Arena(["ollama/qwen3", "lm-studio/gemma"])
|
|
44
|
+
|
|
45
|
+
# Generate both answers locally, ask a human or judge, then record the verdict.
|
|
46
|
+
arena.record(
|
|
47
|
+
"ollama/qwen3",
|
|
48
|
+
"lm-studio/gemma",
|
|
49
|
+
Result.LEFT,
|
|
50
|
+
metadata={"prompt_id": "explain-rag"},
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
for standing in arena.standings():
|
|
54
|
+
print(standing.name, round(standing.rating, 2))
|
|
55
|
+
|
|
56
|
+
print("Compare next:", arena.next_pair())
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### JavaScript
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { Arena, Result } from "localarena";
|
|
63
|
+
|
|
64
|
+
const arena = new Arena(["ollama/qwen3", "lm-studio/gemma"]);
|
|
65
|
+
|
|
66
|
+
arena.record("ollama/qwen3", "lm-studio/gemma", Result.LEFT, {
|
|
67
|
+
prompt_id: "explain-rag",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
for (const standing of arena.standings()) {
|
|
71
|
+
console.log(standing.name, standing.rating.toFixed(2));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
console.log("Compare next:", arena.nextPair());
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The library intentionally does not decide how outputs are generated or judged.
|
|
78
|
+
That keeps private prompts and responses on your machine and lets the same
|
|
79
|
+
engine work with Ollama, LM Studio, vLLM, llama.cpp, local functions, human
|
|
80
|
+
votes, or any provider SDK you explicitly choose.
|
|
81
|
+
|
|
82
|
+
## Schedule a full arena
|
|
83
|
+
|
|
84
|
+
`round_robin` / `roundRobin` returns each unordered pair once per round.
|
|
85
|
+
Pair orientation flips every second round to reduce persistent left/right bias.
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from localarena import Arena, Result, round_robin
|
|
89
|
+
|
|
90
|
+
models = ["a", "b", "c"]
|
|
91
|
+
arena = Arena(models)
|
|
92
|
+
|
|
93
|
+
for left, right in round_robin(models, rounds=2):
|
|
94
|
+
# Replace this deterministic example with your own comparison.
|
|
95
|
+
winner = Result.LEFT if len(left) >= len(right) else Result.RIGHT
|
|
96
|
+
arena.record(left, right, winner)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
import { Arena, Result, roundRobin } from "localarena";
|
|
101
|
+
|
|
102
|
+
const models = ["a", "b", "c"];
|
|
103
|
+
const arena = new Arena(models);
|
|
104
|
+
|
|
105
|
+
for (const [left, right] of roundRobin(models, 2)) {
|
|
106
|
+
const winner = left.length >= right.length ? Result.LEFT : Result.RIGHT;
|
|
107
|
+
arena.record(left, right, winner);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Portable snapshots
|
|
112
|
+
|
|
113
|
+
Standings are derived from an append-only match history. A snapshot contains
|
|
114
|
+
only the arena settings, contestant metadata, and that history:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import json
|
|
118
|
+
from localarena import Arena
|
|
119
|
+
|
|
120
|
+
payload = json.dumps(arena.snapshot(), ensure_ascii=False, indent=2)
|
|
121
|
+
restored = Arena.from_json(payload)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
const payload = JSON.stringify(arena, null, 2);
|
|
126
|
+
const restored = Arena.fromJSON(payload);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The schema-v1 keys are deliberately shared:
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"schema_version": 1,
|
|
134
|
+
"initial_rating": 1000,
|
|
135
|
+
"k_factor": 32,
|
|
136
|
+
"contestants": [
|
|
137
|
+
{"name": "a", "metadata": {}},
|
|
138
|
+
{"name": "b", "metadata": {}}
|
|
139
|
+
],
|
|
140
|
+
"matches": [
|
|
141
|
+
{
|
|
142
|
+
"id": 1,
|
|
143
|
+
"left": "a",
|
|
144
|
+
"right": "b",
|
|
145
|
+
"result": "left",
|
|
146
|
+
"metadata": {}
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
See [the JSON Schema](schema/localarena-v1.schema.json) for the serialized
|
|
153
|
+
contract. Restoring a snapshot replays the history instead of trusting stored
|
|
154
|
+
ratings or counters.
|
|
155
|
+
|
|
156
|
+
## Core API
|
|
157
|
+
|
|
158
|
+
| Python | JavaScript | Purpose |
|
|
159
|
+
| --- | --- | --- |
|
|
160
|
+
| `Arena(names, initial_rating=..., k_factor=...)` | `new Arena(names, { initialRating, kFactor })` | Create an arena |
|
|
161
|
+
| `add(name, metadata)` | `add(name, metadata)` | Register a contestant |
|
|
162
|
+
| `rating(name)` | `rating(name)` | Read one current rating |
|
|
163
|
+
| `record(left, right, result, metadata)` | `record(left, right, result, metadata)` | Record one match |
|
|
164
|
+
| `standings()` / `leaderboard()` | `standings()` / `leaderboard()` | Return rating-sorted rows |
|
|
165
|
+
| `next_pair()` | `nextPair()` | Choose the least-played pair |
|
|
166
|
+
| `snapshot()` / `to_json()` | `snapshot()` / `JSON.stringify(arena)` | Export schema v1 |
|
|
167
|
+
| `Arena.from_snapshot()` / `from_json()` | `Arena.fromSnapshot()` / `fromJSON()` | Validate and replay |
|
|
168
|
+
| `expected_score(a, b)` | `expectedScore(a, b)` | Compute Elo expectation |
|
|
169
|
+
| `round_robin(names, rounds)` | `roundRobin(names, rounds)` | Build all-pairs schedule |
|
|
170
|
+
|
|
171
|
+
Results are `"left"`, `"right"`, or `"draw"`. Python also exports the
|
|
172
|
+
`Result` string enum; JavaScript exports a frozen `Result` object.
|
|
173
|
+
|
|
174
|
+
## Rating behavior
|
|
175
|
+
|
|
176
|
+
LocalArena uses the conventional Elo expectation:
|
|
177
|
+
|
|
178
|
+
```text
|
|
179
|
+
E(left) = 1 / (1 + 10 ^ ((right_rating - left_rating) / 400))
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
A win scores `1`, a draw `0.5`, and a loss `0`. Both contestants receive equal
|
|
183
|
+
and opposite rating adjustments, so total rating is conserved apart from
|
|
184
|
+
normal floating-point precision.
|
|
185
|
+
|
|
186
|
+
Elo is an online rating system: changing match order can change the final
|
|
187
|
+
ratings. Keep and share the match log when reproducibility matters.
|
|
188
|
+
|
|
189
|
+
## Development
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
# Python
|
|
193
|
+
python3 -m pip install -e python
|
|
194
|
+
python3 -m unittest discover -s python/tests
|
|
195
|
+
python3 -m build python
|
|
196
|
+
python3 -m twine check python/dist/*
|
|
197
|
+
|
|
198
|
+
# JavaScript
|
|
199
|
+
npm ci
|
|
200
|
+
npm test
|
|
201
|
+
npm audit --audit-level=low
|
|
202
|
+
npm pack --dry-run
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
CI tests all supported runtimes. Pushing an immutable `v*` tag runs the release
|
|
206
|
+
workflow, verifies that the tag, Python package, and npm package versions agree,
|
|
207
|
+
then publishes the exact tested artifacts to PyPI and npm.
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
export const SCHEMA_VERSION: 1;
|
|
2
|
+
|
|
3
|
+
export const Result: Readonly<{
|
|
4
|
+
LEFT: "left";
|
|
5
|
+
RIGHT: "right";
|
|
6
|
+
DRAW: "draw";
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
export type ResultValue = (typeof Result)[keyof typeof Result];
|
|
10
|
+
|
|
11
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
12
|
+
export type JsonValue =
|
|
13
|
+
| JsonPrimitive
|
|
14
|
+
| JsonValue[]
|
|
15
|
+
| { [key: string]: JsonValue };
|
|
16
|
+
export type Metadata = Record<string, JsonValue>;
|
|
17
|
+
|
|
18
|
+
export interface Match {
|
|
19
|
+
id: number;
|
|
20
|
+
left: string;
|
|
21
|
+
right: string;
|
|
22
|
+
result: ResultValue;
|
|
23
|
+
metadata: Metadata;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Standing {
|
|
27
|
+
rank: number;
|
|
28
|
+
name: string;
|
|
29
|
+
rating: number;
|
|
30
|
+
wins: number;
|
|
31
|
+
losses: number;
|
|
32
|
+
draws: number;
|
|
33
|
+
matches: number;
|
|
34
|
+
metadata: Metadata;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ContestantSnapshot {
|
|
38
|
+
name: string;
|
|
39
|
+
metadata: Metadata;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ArenaSnapshot {
|
|
43
|
+
schema_version: 1;
|
|
44
|
+
initial_rating: number;
|
|
45
|
+
k_factor: number;
|
|
46
|
+
contestants: ContestantSnapshot[];
|
|
47
|
+
matches: Match[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ArenaOptions {
|
|
51
|
+
initialRating?: number;
|
|
52
|
+
kFactor?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function expectedScore(
|
|
56
|
+
leftRating: number,
|
|
57
|
+
rightRating: number,
|
|
58
|
+
): number;
|
|
59
|
+
|
|
60
|
+
export function roundRobin(
|
|
61
|
+
contestants: readonly string[],
|
|
62
|
+
rounds?: number,
|
|
63
|
+
): [string, string][];
|
|
64
|
+
|
|
65
|
+
export class Arena {
|
|
66
|
+
constructor(contestants?: readonly string[], options?: ArenaOptions);
|
|
67
|
+
|
|
68
|
+
readonly initialRating: number;
|
|
69
|
+
readonly kFactor: number;
|
|
70
|
+
readonly contestants: string[];
|
|
71
|
+
readonly matches: Match[];
|
|
72
|
+
|
|
73
|
+
history(): Match[];
|
|
74
|
+
add(name: string, metadata?: Metadata): this;
|
|
75
|
+
rating(name: string): number;
|
|
76
|
+
record(
|
|
77
|
+
left: string,
|
|
78
|
+
right: string,
|
|
79
|
+
result: ResultValue,
|
|
80
|
+
metadata?: Metadata,
|
|
81
|
+
): Match;
|
|
82
|
+
standings(): Standing[];
|
|
83
|
+
leaderboard(): Standing[];
|
|
84
|
+
nextPair(): [string, string] | null;
|
|
85
|
+
snapshot(): ArenaSnapshot;
|
|
86
|
+
toJSON(): ArenaSnapshot;
|
|
87
|
+
|
|
88
|
+
static fromSnapshot(snapshot: ArenaSnapshot): Arena;
|
|
89
|
+
static fromJSON(value: string): Arena;
|
|
90
|
+
}
|
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalArena's dependency-free pairwise evaluation and Elo ranking engine.
|
|
3
|
+
*
|
|
4
|
+
* Snapshots are intentionally small and portable between the JavaScript and
|
|
5
|
+
* Python packages. Standings are always derived by replaying the match log.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const SCHEMA_VERSION = 1;
|
|
9
|
+
|
|
10
|
+
export const Result = Object.freeze({
|
|
11
|
+
LEFT: "left",
|
|
12
|
+
RIGHT: "right",
|
|
13
|
+
DRAW: "draw",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const VALID_RESULTS = new Set(Object.values(Result));
|
|
17
|
+
|
|
18
|
+
function compareCodePoints(left, right) {
|
|
19
|
+
const leftPoints = Array.from(left, (value) => value.codePointAt(0));
|
|
20
|
+
const rightPoints = Array.from(right, (value) => value.codePointAt(0));
|
|
21
|
+
const length = Math.min(leftPoints.length, rightPoints.length);
|
|
22
|
+
|
|
23
|
+
for (let index = 0; index < length; index += 1) {
|
|
24
|
+
if (leftPoints[index] !== rightPoints[index]) {
|
|
25
|
+
return leftPoints[index] - rightPoints[index];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return leftPoints.length - rightPoints.length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function assertFiniteNumber(value, field) {
|
|
32
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
33
|
+
throw new TypeError(`${field} must be a finite number`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function assertName(name, field = "name") {
|
|
38
|
+
if (typeof name !== "string") {
|
|
39
|
+
throw new TypeError(`${field} must be a string`);
|
|
40
|
+
}
|
|
41
|
+
if (name.trim().length === 0) {
|
|
42
|
+
throw new RangeError(`${field} must not be empty`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function assertExactKeys(value, expected, field) {
|
|
47
|
+
const actual = Object.keys(value);
|
|
48
|
+
const missing = expected.filter((key) => !Object.hasOwn(value, key));
|
|
49
|
+
const extra = actual.filter((key) => !expected.includes(key));
|
|
50
|
+
if (missing.length === 0 && extra.length === 0) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const details = [];
|
|
55
|
+
if (missing.length > 0) {
|
|
56
|
+
details.push(`missing ${missing.sort().join(", ")}`);
|
|
57
|
+
}
|
|
58
|
+
if (extra.length > 0) {
|
|
59
|
+
details.push(`unexpected ${extra.sort().join(", ")}`);
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`${field} has invalid fields: ${details.join("; ")}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function cloneMetadata(metadata = {}, field = "metadata") {
|
|
65
|
+
if (
|
|
66
|
+
metadata === null ||
|
|
67
|
+
typeof metadata !== "object" ||
|
|
68
|
+
Array.isArray(metadata)
|
|
69
|
+
) {
|
|
70
|
+
throw new TypeError(`${field} must be a JSON object`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let serialized;
|
|
74
|
+
try {
|
|
75
|
+
serialized = JSON.stringify(metadata, (_key, value) => {
|
|
76
|
+
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
77
|
+
throw new TypeError(`${field} must contain only finite numbers`);
|
|
78
|
+
}
|
|
79
|
+
if (
|
|
80
|
+
typeof value === "number" &&
|
|
81
|
+
Number.isInteger(value) &&
|
|
82
|
+
!Number.isSafeInteger(value)
|
|
83
|
+
) {
|
|
84
|
+
throw new TypeError(
|
|
85
|
+
`${field} integers must be within JavaScript's safe range`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (
|
|
89
|
+
typeof value === "undefined" ||
|
|
90
|
+
typeof value === "function" ||
|
|
91
|
+
typeof value === "symbol" ||
|
|
92
|
+
typeof value === "bigint"
|
|
93
|
+
) {
|
|
94
|
+
throw new TypeError(`${field} must contain only JSON values`);
|
|
95
|
+
}
|
|
96
|
+
return value;
|
|
97
|
+
});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error instanceof TypeError && error.message.startsWith(field)) {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
throw new TypeError(`${field} must be JSON-serializable`, {
|
|
103
|
+
cause: error,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (serialized === undefined) {
|
|
108
|
+
throw new TypeError(`${field} must be JSON-serializable`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const clone = JSON.parse(serialized);
|
|
112
|
+
if (clone === null || typeof clone !== "object" || Array.isArray(clone)) {
|
|
113
|
+
throw new TypeError(`${field} must be a JSON object`);
|
|
114
|
+
}
|
|
115
|
+
return clone;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function cloneMatch(match) {
|
|
119
|
+
return {
|
|
120
|
+
id: match.id,
|
|
121
|
+
left: match.left,
|
|
122
|
+
right: match.right,
|
|
123
|
+
result: match.result,
|
|
124
|
+
metadata: cloneMetadata(match.metadata),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function compareCandidateScores(left, right) {
|
|
129
|
+
for (let index = 0; index < 3; index += 1) {
|
|
130
|
+
if (left[index] !== right[index]) {
|
|
131
|
+
return left[index] - right[index];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const leftName = compareCodePoints(left[3], right[3]);
|
|
135
|
+
if (leftName !== 0) {
|
|
136
|
+
return leftName;
|
|
137
|
+
}
|
|
138
|
+
return compareCodePoints(left[4], right[4]);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function pairKey(left, right) {
|
|
142
|
+
return compareCodePoints(left, right) <= 0
|
|
143
|
+
? JSON.stringify([left, right])
|
|
144
|
+
: JSON.stringify([right, left]);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Return the expected score for the left contestant under the Elo model.
|
|
149
|
+
*/
|
|
150
|
+
export function expectedScore(leftRating, rightRating) {
|
|
151
|
+
assertFiniteNumber(leftRating, "leftRating");
|
|
152
|
+
assertFiniteNumber(rightRating, "rightRating");
|
|
153
|
+
|
|
154
|
+
const exponent = (rightRating - leftRating) / 400;
|
|
155
|
+
if (exponent >= 308) {
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
if (exponent <= -308) {
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
return 1 / (1 + 10 ** exponent);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Build a deterministic all-pairs schedule.
|
|
166
|
+
*
|
|
167
|
+
* Caller order is preserved. On every second repetition, pair orientation is
|
|
168
|
+
* flipped to reduce systematic left/right bias.
|
|
169
|
+
*/
|
|
170
|
+
export function roundRobin(contestants, rounds = 1) {
|
|
171
|
+
if (!Array.isArray(contestants)) {
|
|
172
|
+
throw new TypeError("contestants must be an array");
|
|
173
|
+
}
|
|
174
|
+
if (!Number.isSafeInteger(rounds) || rounds < 1) {
|
|
175
|
+
throw new RangeError("rounds must be a positive safe integer");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const names = contestants.map((name, index) => {
|
|
179
|
+
assertName(name, `contestants[${index}]`);
|
|
180
|
+
return name;
|
|
181
|
+
});
|
|
182
|
+
if (new Set(names).size !== names.length) {
|
|
183
|
+
throw new Error("contestants must contain unique names");
|
|
184
|
+
}
|
|
185
|
+
if (names.length < 2) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const basePairs = [];
|
|
190
|
+
for (let left = 0; left < names.length - 1; left += 1) {
|
|
191
|
+
for (let right = left + 1; right < names.length; right += 1) {
|
|
192
|
+
basePairs.push([names[left], names[right]]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const schedule = [];
|
|
197
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
198
|
+
for (const [left, right] of basePairs) {
|
|
199
|
+
schedule.push(round % 2 === 0 ? [left, right] : [right, left]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return schedule;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export class Arena {
|
|
206
|
+
constructor(contestants = [], options = {}) {
|
|
207
|
+
if (!Array.isArray(contestants)) {
|
|
208
|
+
throw new TypeError("contestants must be an array");
|
|
209
|
+
}
|
|
210
|
+
if (
|
|
211
|
+
options === null ||
|
|
212
|
+
typeof options !== "object" ||
|
|
213
|
+
Array.isArray(options)
|
|
214
|
+
) {
|
|
215
|
+
throw new TypeError("options must be an object");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const initialRating = options.initialRating ?? 1000;
|
|
219
|
+
const kFactor = options.kFactor ?? 32;
|
|
220
|
+
assertFiniteNumber(initialRating, "initialRating");
|
|
221
|
+
assertFiniteNumber(kFactor, "kFactor");
|
|
222
|
+
if (kFactor <= 0) {
|
|
223
|
+
throw new RangeError("kFactor must be greater than zero");
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
this._initialRating = initialRating;
|
|
227
|
+
this._kFactor = kFactor;
|
|
228
|
+
this._contestants = new Map();
|
|
229
|
+
this._matches = [];
|
|
230
|
+
|
|
231
|
+
for (const name of contestants) {
|
|
232
|
+
this.add(name);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
get initialRating() {
|
|
237
|
+
return this._initialRating;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
get kFactor() {
|
|
241
|
+
return this._kFactor;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
get contestants() {
|
|
245
|
+
return Array.from(this._contestants.keys());
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get matches() {
|
|
249
|
+
return this._matches.map(cloneMatch);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
history() {
|
|
253
|
+
return this.matches;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
add(name, metadata = {}) {
|
|
257
|
+
assertName(name);
|
|
258
|
+
if (this._contestants.has(name)) {
|
|
259
|
+
throw new Error(`contestant already exists: ${name}`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this._contestants.set(name, {
|
|
263
|
+
name,
|
|
264
|
+
rating: this._initialRating,
|
|
265
|
+
wins: 0,
|
|
266
|
+
losses: 0,
|
|
267
|
+
draws: 0,
|
|
268
|
+
metadata: cloneMetadata(metadata),
|
|
269
|
+
});
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
rating(name) {
|
|
274
|
+
assertName(name);
|
|
275
|
+
const contestant = this._contestants.get(name);
|
|
276
|
+
if (contestant === undefined) {
|
|
277
|
+
throw new Error(`unknown contestant: ${name}`);
|
|
278
|
+
}
|
|
279
|
+
return contestant.rating;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
record(left, right, result, metadata = {}) {
|
|
283
|
+
assertName(left, "left");
|
|
284
|
+
assertName(right, "right");
|
|
285
|
+
if (left === right) {
|
|
286
|
+
throw new Error("left and right contestants must be different");
|
|
287
|
+
}
|
|
288
|
+
if (!this._contestants.has(left)) {
|
|
289
|
+
throw new Error(`unknown contestant: ${left}`);
|
|
290
|
+
}
|
|
291
|
+
if (!this._contestants.has(right)) {
|
|
292
|
+
throw new Error(`unknown contestant: ${right}`);
|
|
293
|
+
}
|
|
294
|
+
if (!VALID_RESULTS.has(result)) {
|
|
295
|
+
throw new RangeError('result must be "left", "right", or "draw"');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const safeMetadata = cloneMetadata(metadata);
|
|
299
|
+
const leftState = this._contestants.get(left);
|
|
300
|
+
const rightState = this._contestants.get(right);
|
|
301
|
+
const expectedLeft = expectedScore(leftState.rating, rightState.rating);
|
|
302
|
+
const actualLeft =
|
|
303
|
+
result === Result.LEFT ? 1 : result === Result.DRAW ? 0.5 : 0;
|
|
304
|
+
const adjustment = this._kFactor * (actualLeft - expectedLeft);
|
|
305
|
+
const nextLeftRating = leftState.rating + adjustment;
|
|
306
|
+
const nextRightRating = rightState.rating - adjustment;
|
|
307
|
+
if (!Number.isFinite(nextLeftRating) || !Number.isFinite(nextRightRating)) {
|
|
308
|
+
throw new RangeError(
|
|
309
|
+
"rating update overflowed; use smaller ratings or kFactor",
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
leftState.rating = nextLeftRating;
|
|
314
|
+
rightState.rating = nextRightRating;
|
|
315
|
+
if (result === Result.LEFT) {
|
|
316
|
+
leftState.wins += 1;
|
|
317
|
+
rightState.losses += 1;
|
|
318
|
+
} else if (result === Result.RIGHT) {
|
|
319
|
+
leftState.losses += 1;
|
|
320
|
+
rightState.wins += 1;
|
|
321
|
+
} else {
|
|
322
|
+
leftState.draws += 1;
|
|
323
|
+
rightState.draws += 1;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const match = {
|
|
327
|
+
id: this._matches.length + 1,
|
|
328
|
+
left,
|
|
329
|
+
right,
|
|
330
|
+
result,
|
|
331
|
+
metadata: safeMetadata,
|
|
332
|
+
};
|
|
333
|
+
this._matches.push(match);
|
|
334
|
+
return cloneMatch(match);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
standings() {
|
|
338
|
+
const standings = Array.from(this._contestants.values(), (contestant) => ({
|
|
339
|
+
name: contestant.name,
|
|
340
|
+
rating: contestant.rating,
|
|
341
|
+
wins: contestant.wins,
|
|
342
|
+
losses: contestant.losses,
|
|
343
|
+
draws: contestant.draws,
|
|
344
|
+
matches: contestant.wins + contestant.losses + contestant.draws,
|
|
345
|
+
metadata: cloneMetadata(contestant.metadata),
|
|
346
|
+
})).sort((left, right) => {
|
|
347
|
+
if (left.rating !== right.rating) {
|
|
348
|
+
return right.rating - left.rating;
|
|
349
|
+
}
|
|
350
|
+
return compareCodePoints(left.name, right.name);
|
|
351
|
+
});
|
|
352
|
+
return standings.map((standing, index) => ({
|
|
353
|
+
rank: index + 1,
|
|
354
|
+
...standing,
|
|
355
|
+
}));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
leaderboard() {
|
|
359
|
+
return this.standings();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
nextPair() {
|
|
363
|
+
if (this._contestants.size < 2) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const names = Array.from(this._contestants.keys()).sort(compareCodePoints);
|
|
368
|
+
const totals = new Map(
|
|
369
|
+
this.standings().map((standing) => [standing.name, standing.matches]),
|
|
370
|
+
);
|
|
371
|
+
const pairCounts = new Map();
|
|
372
|
+
for (const match of this._matches) {
|
|
373
|
+
const key = pairKey(match.left, match.right);
|
|
374
|
+
pairCounts.set(key, (pairCounts.get(key) ?? 0) + 1);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
let bestPair;
|
|
378
|
+
let bestScore;
|
|
379
|
+
for (let leftIndex = 0; leftIndex < names.length - 1; leftIndex += 1) {
|
|
380
|
+
for (
|
|
381
|
+
let rightIndex = leftIndex + 1;
|
|
382
|
+
rightIndex < names.length;
|
|
383
|
+
rightIndex += 1
|
|
384
|
+
) {
|
|
385
|
+
const left = names[leftIndex];
|
|
386
|
+
const right = names[rightIndex];
|
|
387
|
+
const leftMatches = totals.get(left);
|
|
388
|
+
const rightMatches = totals.get(right);
|
|
389
|
+
const score = [
|
|
390
|
+
pairCounts.get(pairKey(left, right)) ?? 0,
|
|
391
|
+
leftMatches + rightMatches,
|
|
392
|
+
Math.max(leftMatches, rightMatches),
|
|
393
|
+
left,
|
|
394
|
+
right,
|
|
395
|
+
];
|
|
396
|
+
if (
|
|
397
|
+
bestScore === undefined ||
|
|
398
|
+
compareCandidateScores(score, bestScore) < 0
|
|
399
|
+
) {
|
|
400
|
+
bestPair = [left, right];
|
|
401
|
+
bestScore = score;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return bestPair;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
snapshot() {
|
|
409
|
+
const contestants = Array.from(this._contestants.values())
|
|
410
|
+
.sort((left, right) => compareCodePoints(left.name, right.name))
|
|
411
|
+
.map((contestant) => ({
|
|
412
|
+
name: contestant.name,
|
|
413
|
+
metadata: cloneMetadata(contestant.metadata),
|
|
414
|
+
}));
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
schema_version: SCHEMA_VERSION,
|
|
418
|
+
initial_rating: this._initialRating,
|
|
419
|
+
k_factor: this._kFactor,
|
|
420
|
+
contestants,
|
|
421
|
+
matches: this.matches,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
toJSON() {
|
|
426
|
+
return this.snapshot();
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
static fromSnapshot(snapshot) {
|
|
430
|
+
if (
|
|
431
|
+
snapshot === null ||
|
|
432
|
+
typeof snapshot !== "object" ||
|
|
433
|
+
Array.isArray(snapshot)
|
|
434
|
+
) {
|
|
435
|
+
throw new TypeError("snapshot must be an object");
|
|
436
|
+
}
|
|
437
|
+
if (snapshot.schema_version !== SCHEMA_VERSION) {
|
|
438
|
+
throw new Error(
|
|
439
|
+
`unsupported schema_version: ${String(snapshot.schema_version)}`,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
if (!Array.isArray(snapshot.contestants)) {
|
|
443
|
+
throw new TypeError("snapshot.contestants must be an array");
|
|
444
|
+
}
|
|
445
|
+
if (!Array.isArray(snapshot.matches)) {
|
|
446
|
+
throw new TypeError("snapshot.matches must be an array");
|
|
447
|
+
}
|
|
448
|
+
assertExactKeys(
|
|
449
|
+
snapshot,
|
|
450
|
+
[
|
|
451
|
+
"schema_version",
|
|
452
|
+
"initial_rating",
|
|
453
|
+
"k_factor",
|
|
454
|
+
"contestants",
|
|
455
|
+
"matches",
|
|
456
|
+
],
|
|
457
|
+
"snapshot",
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
const arena = new Arena([], {
|
|
461
|
+
initialRating: snapshot.initial_rating,
|
|
462
|
+
kFactor: snapshot.k_factor,
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
snapshot.contestants.forEach((contestant, index) => {
|
|
466
|
+
if (
|
|
467
|
+
contestant === null ||
|
|
468
|
+
typeof contestant !== "object" ||
|
|
469
|
+
Array.isArray(contestant)
|
|
470
|
+
) {
|
|
471
|
+
throw new TypeError(`snapshot.contestants[${index}] must be an object`);
|
|
472
|
+
}
|
|
473
|
+
assertExactKeys(
|
|
474
|
+
contestant,
|
|
475
|
+
["name", "metadata"],
|
|
476
|
+
`snapshot.contestants[${index}]`,
|
|
477
|
+
);
|
|
478
|
+
arena.add(contestant.name, contestant.metadata);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
snapshot.matches.forEach((match, index) => {
|
|
482
|
+
if (
|
|
483
|
+
match === null ||
|
|
484
|
+
typeof match !== "object" ||
|
|
485
|
+
Array.isArray(match)
|
|
486
|
+
) {
|
|
487
|
+
throw new TypeError(`snapshot.matches[${index}] must be an object`);
|
|
488
|
+
}
|
|
489
|
+
assertExactKeys(
|
|
490
|
+
match,
|
|
491
|
+
["id", "left", "right", "result", "metadata"],
|
|
492
|
+
`snapshot.matches[${index}]`,
|
|
493
|
+
);
|
|
494
|
+
const expectedId = index + 1;
|
|
495
|
+
if (!Number.isSafeInteger(match.id) || match.id !== expectedId) {
|
|
496
|
+
throw new Error(
|
|
497
|
+
`snapshot.matches[${index}].id must be ${expectedId}`,
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
arena.record(
|
|
501
|
+
match.left,
|
|
502
|
+
match.right,
|
|
503
|
+
match.result,
|
|
504
|
+
match.metadata,
|
|
505
|
+
);
|
|
506
|
+
});
|
|
507
|
+
return arena;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
static fromJSON(value) {
|
|
511
|
+
if (typeof value !== "string") {
|
|
512
|
+
throw new TypeError("value must be a JSON string");
|
|
513
|
+
}
|
|
514
|
+
let snapshot;
|
|
515
|
+
try {
|
|
516
|
+
snapshot = JSON.parse(value);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
throw new SyntaxError("value must contain valid JSON", { cause: error });
|
|
519
|
+
}
|
|
520
|
+
return Arena.fromSnapshot(snapshot);
|
|
521
|
+
}
|
|
522
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "localarena",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local-first pairwise evaluation, deterministic scheduling, and Elo ranking.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"evaluation",
|
|
7
|
+
"elo",
|
|
8
|
+
"llm",
|
|
9
|
+
"local-first",
|
|
10
|
+
"pairwise",
|
|
11
|
+
"ranking"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/maziyarpanahi/localarena#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/maziyarpanahi/localarena/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/maziyarpanahi/localarena.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "Maziyar Panahi",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"main": "./javascript/src/index.js",
|
|
26
|
+
"types": "./javascript/src/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./javascript/src/index.d.ts",
|
|
30
|
+
"import": "./javascript/src/index.js",
|
|
31
|
+
"default": "./javascript/src/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./schema/localarena-v1.schema.json": "./schema/localarena-v1.schema.json",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"javascript/src",
|
|
38
|
+
"schema",
|
|
39
|
+
"README.md",
|
|
40
|
+
"CHANGELOG.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"check": "node --check javascript/src/index.js",
|
|
45
|
+
"test": "node --test",
|
|
46
|
+
"pack:check": "npm pack --dry-run"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://github.com/maziyarpanahi/localarena/blob/main/schema/localarena-v1.schema.json",
|
|
4
|
+
"title": "LocalArena snapshot v1",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"required": [
|
|
8
|
+
"schema_version",
|
|
9
|
+
"initial_rating",
|
|
10
|
+
"k_factor",
|
|
11
|
+
"contestants",
|
|
12
|
+
"matches"
|
|
13
|
+
],
|
|
14
|
+
"properties": {
|
|
15
|
+
"schema_version": {
|
|
16
|
+
"const": 1
|
|
17
|
+
},
|
|
18
|
+
"initial_rating": {
|
|
19
|
+
"type": "number"
|
|
20
|
+
},
|
|
21
|
+
"k_factor": {
|
|
22
|
+
"type": "number",
|
|
23
|
+
"exclusiveMinimum": 0
|
|
24
|
+
},
|
|
25
|
+
"contestants": {
|
|
26
|
+
"type": "array",
|
|
27
|
+
"items": {
|
|
28
|
+
"$ref": "#/$defs/contestant"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"matches": {
|
|
32
|
+
"type": "array",
|
|
33
|
+
"items": {
|
|
34
|
+
"$ref": "#/$defs/match"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"$defs": {
|
|
39
|
+
"name": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"minLength": 1,
|
|
42
|
+
"pattern": "\\S"
|
|
43
|
+
},
|
|
44
|
+
"metadata": {
|
|
45
|
+
"type": "object"
|
|
46
|
+
},
|
|
47
|
+
"contestant": {
|
|
48
|
+
"type": "object",
|
|
49
|
+
"additionalProperties": false,
|
|
50
|
+
"required": [
|
|
51
|
+
"name",
|
|
52
|
+
"metadata"
|
|
53
|
+
],
|
|
54
|
+
"properties": {
|
|
55
|
+
"name": {
|
|
56
|
+
"$ref": "#/$defs/name"
|
|
57
|
+
},
|
|
58
|
+
"metadata": {
|
|
59
|
+
"$ref": "#/$defs/metadata"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"match": {
|
|
64
|
+
"type": "object",
|
|
65
|
+
"additionalProperties": false,
|
|
66
|
+
"required": [
|
|
67
|
+
"id",
|
|
68
|
+
"left",
|
|
69
|
+
"right",
|
|
70
|
+
"result",
|
|
71
|
+
"metadata"
|
|
72
|
+
],
|
|
73
|
+
"properties": {
|
|
74
|
+
"id": {
|
|
75
|
+
"type": "integer",
|
|
76
|
+
"minimum": 1
|
|
77
|
+
},
|
|
78
|
+
"left": {
|
|
79
|
+
"$ref": "#/$defs/name"
|
|
80
|
+
},
|
|
81
|
+
"right": {
|
|
82
|
+
"$ref": "#/$defs/name"
|
|
83
|
+
},
|
|
84
|
+
"result": {
|
|
85
|
+
"enum": [
|
|
86
|
+
"left",
|
|
87
|
+
"right",
|
|
88
|
+
"draw"
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
"metadata": {
|
|
92
|
+
"$ref": "#/$defs/metadata"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|