Oracle src/rune/model_profiles.py 438 lines / 15 fns: 5/15 -> 15/15 per scripts/dichotomy_audit.py (name-preserving match). The ten audit-named gaps land as Go functions: getProfilePath, loadProfile (suppressed-exception floor + unknown-key filtering), saveProfile (Python-shaped JSON writer), buildProfileFromOllama (knowledge merge + uncensored tier + injected clock), scanAllModels (Ollama sweep with banker's rounding + learned-data preservation), getOrCreateProfile, recordInteraction (learning accumulator), getPromptContext (operator string assembly), to_dict/from_dict (the dataclass lens via profileFromMap), plus the stdin-op serve CLI over the same code paths. Freeze rig scripts/mp_freeze_seams (HOME pinned before the oracle import, per-case hermetic store wipe+seed, canned Ollama server, frozen clock via the harness envelope): 36-case frozen truth regenerates byte-identical across runs; freeze_truth --verify OK against the live oracle. Battery 36/36 replay VERBATIM from the embedded truth (gen_mp_battery.py generator). Parity scripts/check_model_profiles_parity.py 143/143 LIVE (detection surface, sanitization matrix, lens, build assembly, record sequences, prompt contexts, save byte-parity, scan sweep incl. dead-port degrade). gofmt+vet+build clean, pytest 31/31 both pins. Divergences (documented, not bugs): non-string profile names are accepted by the oracle dataclass but rejected by the typed port (local_tool_runtime precedent); integral-float rendering is canonicalized in the sweep (context_broker precedent) while the frozen save_profile rows pin the file writer byte-exact.
419 lines
11 KiB
Python
419 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""gen_mp_battery.py — ONE-SHOT generator: emit
|
|
src/go/model-profiles/frozen_battery_test.go with the frozen truth
|
|
(frozen_mp_truth.json) embedded VERBATIM (never hand-transcribed) plus
|
|
the dispatch harness. Re-run after any truth regeneration."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
TRUTH = REPO / "src" / "go" / "model-profiles" / "frozen_mp_truth.json"
|
|
OUT = REPO / "src" / "go" / "model-profiles" / "frozen_battery_test.go"
|
|
|
|
HARNESS = r'''package main
|
|
|
|
// frozen_battery_test.go — gap-closing wave (#2479 dichotomy tranche):
|
|
// replays the shared-harness frozen oracle truth (generated by
|
|
// scripts/gen_model_profiles_truth.py via scripts/freeze_truth.py
|
|
// through the scripts/mp_freeze_seams rig, RUNE_FROZEN_NOW pinned)
|
|
// VERBATIM, in order, against the store seam repointed at a fresh temp
|
|
// dir. The case sequence is self-seeding: replaying it from an empty
|
|
// store regenerates every byte of expected state — exactly what
|
|
// PYTHONPATH=scripts/mp_freeze_seams:src python3 scripts/freeze_truth.py \
|
|
// --verify src/go/model-profiles/frozen_mp_truth.json \
|
|
// --module rune.model_profiles
|
|
// checks against the live oracle. (GENERATED by scripts/gen_mp_battery.py
|
|
// — do not hand-edit the truth const.)
|
|
//
|
|
// DIVERGENCE (documented, not a bug — the local_tool_runtime precedent):
|
|
// load_profile({"name": <non-string>}) is ACCEPTED by the Python oracle
|
|
// (dataclass __init__ performs no type checks) but REJECTED by the Go
|
|
// port (typed signature). No frozen row carries that shape.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const frozenMpTruthJSON = `__TRUTH__`
|
|
|
|
func decodeMPArg(t *testing.T, raw json.RawMessage, dst any) {
|
|
t.Helper()
|
|
var v any
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
t.Fatalf("arg decode: %v", err)
|
|
}
|
|
if v == nil {
|
|
return // null → leave the zero value (None default)
|
|
}
|
|
if err := json.Unmarshal(raw, dst); err != nil {
|
|
t.Fatalf("arg re-decode: %v", err)
|
|
}
|
|
}
|
|
|
|
func profileToMap(p ModelProfile) map[string]any {
|
|
raw, err := json.Marshal(p)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func seedStore(t *testing.T, seed map[string]json.RawMessage) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(profilesDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, raw := range seed {
|
|
var v any
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
t.Fatalf("seed decode: %v", err)
|
|
}
|
|
var data []byte
|
|
if s, ok := v.(string); ok {
|
|
data = []byte(s) // raw-text seed (corrupt-JSON floor)
|
|
} else {
|
|
data, _ = json.Marshal(v)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(profilesDir, name), data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func storeFiles() map[string]any {
|
|
out := map[string]any{}
|
|
entries, err := os.ReadDir(profilesDir)
|
|
if err != nil {
|
|
return out
|
|
}
|
|
for _, e := range entries {
|
|
if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") {
|
|
continue
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(profilesDir, e.Name()))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var v any
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
continue
|
|
}
|
|
out[e.Name()] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
// dispatchFrozen — one frozen case against the ported surface. Stateful
|
|
// ops mirror the rig seams: wipe the store, seed from the args, run,
|
|
// return the JSON-native projection.
|
|
func dispatchFrozen(t *testing.T, fn string, args []json.RawMessage) any {
|
|
t.Helper()
|
|
arg := func(i int, dst any) { decodeMPArg(t, args[i], dst) }
|
|
argOpt := func(i int, dst any) {
|
|
if i < len(args) {
|
|
decodeMPArg(t, args[i], dst)
|
|
}
|
|
}
|
|
switch fn {
|
|
case "seam_get_profile_path":
|
|
var name string
|
|
arg(0, &name)
|
|
got := getProfilePath(name)
|
|
rel, err := filepath.Rel(profilesDir, got)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The frozen expectation carries the rig's absolute path; the
|
|
// contract is the sanitized FILE NAME (store root is the seam).
|
|
return filepath.Join("$STORE", rel)
|
|
case "seam_load_profile":
|
|
var name string
|
|
arg(0, &name)
|
|
var seed map[string]json.RawMessage
|
|
argOpt(1, &seed)
|
|
os.RemoveAll(profilesDir)
|
|
if seed != nil {
|
|
seedStore(t, seed)
|
|
}
|
|
if p, ok := loadProfile(name); ok {
|
|
return profileToMap(*p)
|
|
}
|
|
return nil
|
|
case "seam_save_profile":
|
|
var state map[string]json.RawMessage
|
|
arg(0, &state)
|
|
p := profileFromState(t, state)
|
|
os.RemoveAll(profilesDir)
|
|
saveProfile(p)
|
|
raw, err := os.ReadFile(getProfilePath(p.Name))
|
|
if err != nil {
|
|
t.Fatalf("save wrote nothing: %v", err)
|
|
}
|
|
return map[string]any{"raw": string(raw), "parsed": storeFiles()}
|
|
case "seam_build_profile_from_ollama":
|
|
var name string
|
|
arg(0, &name)
|
|
var info map[string]any
|
|
arg(1, &info)
|
|
return profileToMap(buildProfileFromOllama(name, info))
|
|
case "seam_scan_all_models":
|
|
var fixture any
|
|
arg(0, &fixture)
|
|
var seed map[string]json.RawMessage
|
|
argOpt(1, &seed)
|
|
os.RemoveAll(profilesDir)
|
|
if seed != nil {
|
|
seedStore(t, seed)
|
|
}
|
|
saved := ollamaURL
|
|
var srv *httptest.Server
|
|
if s, ok := fixture.(string); ok && s == "dead" {
|
|
ollamaURL = "http://127.0.0.1:1"
|
|
} else {
|
|
payload, _ := json.Marshal(fixture)
|
|
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(payload)
|
|
}))
|
|
ollamaURL = srv.URL
|
|
}
|
|
profiles := scanAllModels()
|
|
ollamaURL = saved
|
|
if srv != nil {
|
|
srv.Close()
|
|
}
|
|
out := make([]any, len(profiles))
|
|
for i, p := range profiles {
|
|
out[i] = profileToMap(p)
|
|
}
|
|
return map[string]any{"profiles": out, "files": storeFiles()}
|
|
case "seam_get_or_create_profile":
|
|
var name string
|
|
arg(0, &name)
|
|
var seed map[string]json.RawMessage
|
|
argOpt(1, &seed)
|
|
os.RemoveAll(profilesDir)
|
|
if seed != nil {
|
|
seedStore(t, seed)
|
|
}
|
|
p := getOrCreateProfile(name)
|
|
return map[string]any{"profile": profileToMap(p), "files": storeFiles()}
|
|
case "seam_record_interaction":
|
|
var name string
|
|
arg(0, &name)
|
|
var outcomes [][]any
|
|
arg(1, &outcomes)
|
|
var seed map[string]json.RawMessage
|
|
argOpt(2, &seed)
|
|
os.RemoveAll(profilesDir)
|
|
if seed != nil {
|
|
seedStore(t, seed)
|
|
}
|
|
for _, oc := range outcomes {
|
|
success, _ := oc[0].(bool)
|
|
topic, _ := oc[1].(string)
|
|
recordInteraction(name, success, topic)
|
|
}
|
|
return storeFiles()
|
|
case "seam_get_prompt_context":
|
|
var name string
|
|
arg(0, &name)
|
|
var seed map[string]json.RawMessage
|
|
argOpt(1, &seed)
|
|
os.RemoveAll(profilesDir)
|
|
if seed != nil {
|
|
seedStore(t, seed)
|
|
}
|
|
return getPromptContext(name)
|
|
case "seam_to_dict":
|
|
var state map[string]json.RawMessage
|
|
arg(0, &state)
|
|
return profileToMap(profileFromState(t, state))
|
|
case "seam_from_dict":
|
|
var data map[string]json.RawMessage
|
|
arg(0, &data)
|
|
return profileToMap(profileFromState(t, data))
|
|
default:
|
|
t.Fatalf("frozen case names unknown fn %q", fn)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// profileFromState rebuilds a ModelProfile from a from_dict-shaped doc
|
|
// (unknown keys filtered, absent keys defaulted — the oracle lens).
|
|
func profileFromState(t *testing.T, state map[string]json.RawMessage) ModelProfile {
|
|
t.Helper()
|
|
p := defaultModelProfile("")
|
|
get := func(key string) (json.RawMessage, bool) {
|
|
raw, ok := state[key]
|
|
return raw, ok
|
|
}
|
|
if raw, ok := get("name"); ok {
|
|
if err := json.Unmarshal(raw, &p.Name); err != nil {
|
|
t.Fatalf("state name: %v", err)
|
|
}
|
|
}
|
|
strField := func(key string, dst *string) {
|
|
if raw, ok := get(key); ok {
|
|
if err := json.Unmarshal(raw, dst); err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
}
|
|
}
|
|
strField("display_name", &p.DisplayName)
|
|
strField("family", &p.Family)
|
|
strField("parameters", &p.Parameters)
|
|
strField("quantization", &p.Quantization)
|
|
strField("thinking_format", &p.ThinkingFormat)
|
|
strField("response_style", &p.ResponseStyle)
|
|
strField("last_used", &p.LastUsed)
|
|
strField("created", &p.Created)
|
|
numField := func(key string, dst *float64) {
|
|
if raw, ok := get(key); ok {
|
|
var n json.Number
|
|
if err := json.Unmarshal(raw, &n); err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
f, err := n.Float64()
|
|
if err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
*dst = f
|
|
}
|
|
}
|
|
intField := func(key string, dst *int) {
|
|
if raw, ok := get(key); ok {
|
|
var n json.Number
|
|
if err := json.Unmarshal(raw, &n); err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
i, err := n.Int64()
|
|
if err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
*dst = int(i)
|
|
}
|
|
}
|
|
var f float64
|
|
numField("size_gb", &f)
|
|
p.SizeGB = f
|
|
numField("optimal_temperature", &p.OptimalTemperature)
|
|
intField("safety_tier", &p.SafetyTier)
|
|
intField("context_window", &p.ContextWindow)
|
|
intField("interactions", &p.Interactions)
|
|
intField("successes", &p.Successes)
|
|
intField("failures", &p.Failures)
|
|
listField := func(key string, dst *[]string) {
|
|
if raw, ok := get(key); ok {
|
|
var l []string
|
|
if err := json.Unmarshal(raw, &l); err != nil {
|
|
t.Fatalf("state %s: %v", key, err)
|
|
}
|
|
if l == nil {
|
|
l = []string{}
|
|
}
|
|
*dst = l
|
|
}
|
|
}
|
|
listField("strengths", &p.Strengths)
|
|
listField("weaknesses", &p.Weaknesses)
|
|
listField("prompting_tips", &p.PromptingTips)
|
|
listField("known_refusal_topics", &p.KnownRefusalTopics)
|
|
return p
|
|
}
|
|
|
|
// normalizeExpected rewrites the rig-rooted absolute paths the frozen
|
|
// expectations carry into the $STORE placeholder (the store root is the
|
|
// seam — only the sanitized file name is contractual).
|
|
func normalizeExpected(v any) any {
|
|
switch x := v.(type) {
|
|
case string:
|
|
if strings.Contains(x, ".rune/model_profiles/") {
|
|
parts := strings.Split(x, ".rune/model_profiles/")
|
|
return filepath.Join("$STORE", parts[len(parts)-1])
|
|
}
|
|
return x
|
|
case map[string]any:
|
|
for k, e := range x {
|
|
x[k] = normalizeExpected(e)
|
|
}
|
|
return x
|
|
case []any:
|
|
for i, e := range x {
|
|
x[i] = normalizeExpected(e)
|
|
}
|
|
return x
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
func TestFrozenBattery(t *testing.T) {
|
|
setProfilesDir(filepath.Join(t.TempDir(), ".rune", "model_profiles"))
|
|
savedNow := nowUTC
|
|
nowUTC = func() time.Time {
|
|
return time.Date(2026, 10, 13, 9, 41, 23, 123456000, time.UTC)
|
|
}
|
|
defer func() { nowUTC = savedNow }()
|
|
var envelope struct {
|
|
FrozenNow string `json:"frozen_now"`
|
|
Cases []struct {
|
|
Fn string `json:"fn"`
|
|
Args []json.RawMessage `json:"args"`
|
|
Kwargs map[string]any `json:"kwargs"`
|
|
Expected any `json:"expected"`
|
|
} `json:"cases"`
|
|
}
|
|
if err := json.Unmarshal([]byte(frozenMpTruthJSON), &envelope); err != nil {
|
|
t.Fatalf("frozen truth malformed: %v", err)
|
|
}
|
|
for i, c := range envelope.Cases {
|
|
if len(c.Kwargs) != 0 {
|
|
t.Fatalf("case %d %s: unexpected kwargs %v", i, c.Fn, c.Kwargs)
|
|
}
|
|
got := dispatchFrozen(t, c.Fn, c.Args)
|
|
raw, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatalf("case %d %s: marshal got: %v", i, c.Fn, err)
|
|
}
|
|
var gotAny any
|
|
if err := json.Unmarshal(raw, &gotAny); err != nil {
|
|
t.Fatalf("case %d %s: reparse: %v", i, c.Fn, err)
|
|
}
|
|
wantAny := normalizeExpected(c.Expected)
|
|
if !reflect.DeepEqual(gotAny, wantAny) {
|
|
gj, _ := json.Marshal(gotAny)
|
|
ej, _ := json.Marshal(wantAny)
|
|
t.Errorf("case %d %s:\n got %s\n want %s", i, c.Fn, gj, ej)
|
|
}
|
|
}
|
|
fmt.Printf("replayed %d frozen cases\n", len(envelope.Cases))
|
|
}
|
|
'''
|
|
|
|
|
|
def main() -> int:
|
|
truth = TRUTH.read_text()
|
|
assert truth.endswith("\n"), "truth file must end with newline"
|
|
body = HARNESS.replace("__TRUTH__", truth.rstrip("\n"))
|
|
OUT.write_text(body)
|
|
print(f"wrote {OUT} ({len(body)} bytes)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|