From 8270597fe2dbc3c99fbccc53108195fa4d865872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Mon, 30 Nov 2026 11:28:14 -0700 Subject: [PATCH] refactor(runtime): declare kind paths and capabilities Move control and utility executable names plus the QEMU environment key into runtime kind specs. Read capabilities only from manifest.json and declare them when packing reims runtimes. --- frontend/wailsjs/go/models.ts | 6 ++++ internal/runtime/env_test.go | 25 ++++++++++++++ internal/runtime/kinds.go | 40 ++++++---------------- internal/runtime/kinds_test.go | 62 +++++----------------------------- internal/runtime/runtime.go | 62 +++++++++++++++++++++++++--------- scripts/pack-runtime.ps1 | 15 ++++++++ 6 files changed, 110 insertions(+), 100 deletions(-) diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts index c0c37cd..f446852 100644 --- a/frontend/wailsjs/go/models.ts +++ b/frontend/wailsjs/go/models.ts @@ -2641,6 +2641,9 @@ export namespace runtime { name_en: string; required_files?: string[]; default_api_version?: string; + control_executable?: string; + utility_executable?: string; + control_qemu_env?: string; builtin?: boolean; plugin_id?: string; @@ -2656,6 +2659,9 @@ export namespace runtime { this.name_en = source["name_en"]; this.required_files = source["required_files"]; this.default_api_version = source["default_api_version"]; + this.control_executable = source["control_executable"]; + this.utility_executable = source["utility_executable"]; + this.control_qemu_env = source["control_qemu_env"]; this.builtin = source["builtin"]; this.plugin_id = source["plugin_id"]; } diff --git a/internal/runtime/env_test.go b/internal/runtime/env_test.go index f58adf8..ee7898d 100644 --- a/internal/runtime/env_test.go +++ b/internal/runtime/env_test.go @@ -51,3 +51,28 @@ func TestBuildChildEnvRedirectsTemp(t *testing.T) { } } } + +func TestKindSpecControlsExecutablesAndEnvironment(t *testing.T) { + root := t.TempDir() + inst := Installed{Dir: root, Kind: DefaultKind} + if got := inst.ControlPath(); !strings.Contains(strings.ToLower(got), "reims-ctl") { + t.Fatalf("ControlPath = %q", got) + } + if got := inst.UtilityPath(); !strings.Contains(strings.ToLower(got), "reims-util") { + t.Fatalf("UtilityPath = %q", got) + } + custom := Installed{Dir: root, Spec: KindSpec{Kind: "custom", ControlExecutable: "custom-ctl", UtilityExecutable: "custom-util", ControlQemuEnv: "CUSTOM_QEMU"}} + if got := custom.ControlPath(); !strings.Contains(got, "custom-ctl") { + t.Fatalf("custom ControlPath = %q", got) + } + env := BuildChildEnv(&config.Config{DataRoot: t.TempDir()}, custom) + found := false + for _, item := range env { + if strings.HasPrefix(item, "CUSTOM_QEMU=") { + found = true + } + } + if !found { + t.Fatal("custom QEMU environment declaration was not applied") + } +} diff --git a/internal/runtime/kinds.go b/internal/runtime/kinds.go index 97eeeb7..40d67d6 100644 --- a/internal/runtime/kinds.go +++ b/internal/runtime/kinds.go @@ -29,6 +29,9 @@ type KindSpec struct { NameEn string `json:"name_en"` RequiredFiles []string `json:"required_files,omitempty"` DefaultAPIVersion string `json:"default_api_version,omitempty"` + ControlExecutable string `json:"control_executable,omitempty"` + UtilityExecutable string `json:"utility_executable,omitempty"` + ControlQemuEnv string `json:"control_qemu_env,omitempty"` Builtin bool `json:"builtin,omitempty"` PluginID string `json:"plugin_id,omitempty"` } @@ -51,6 +54,9 @@ func NewKindRegistry() *KindRegistry { NameEn: "Reims vGPU Runtime", RequiredFiles: []string{"bin/reims-ctl.exe", "qemu/bin/qemu-system-x86_64.exe"}, DefaultAPIVersion: DefaultRuntimeAPI, + ControlExecutable: "reims-ctl", + UtilityExecutable: "reims-util", + ControlQemuEnv: "REIMS_CTL_QEMU", Builtin: true, PluginID: "builtin.runtime.reims-vgpu", } @@ -130,17 +136,10 @@ func (m Manifest) EffectiveRuntimeAPI() string { return DefaultRuntimeAPI } -// buggyWin32UnicodeInputPatchSHA256 is the SHA-256 of the initial -// reims-vgpu-win32-unicode-input.patch shipped in runtime-3731afd-p2/p3, -// which compared WM_CHAR.time against WM_KEYDOWN.time and duplicated -// physical keystrokes (issue #14). Runtimes built with this patch hash must -// NOT advertise the "win32-unicode-input" capability. -const buggyWin32UnicodeInputPatchSHA256 = "0b34f7c0ebdf3ca537c7f4523af97967a06924aedba7058dbc978870f561b366" - -// EffectiveCapabilities returns declared capabilities plus inferred legacy -// capabilities derived from Patches so callers no longer match raw patch filenames (ยง5.2). +// EffectiveCapabilities returns only capabilities declared by manifest.json. +// Runtime packages declare patch-derived capabilities at build time. func (m Manifest) EffectiveCapabilities() []string { - seen := make(map[string]bool, len(m.Capabilities)+len(m.Patches)) + seen := make(map[string]bool, len(m.Capabilities)) var out []string add := func(c string) { c = strings.TrimSpace(c) @@ -152,29 +151,10 @@ func (m Manifest) EffectiveCapabilities() []string { for _, c := range m.Capabilities { add(c) } - for _, p := range m.Patches { - low := strings.ToLower(p.Name) - switch { - case strings.Contains(low, "nested-virt-nonfatal"): - add("nested-virt-nonfatal") - case strings.Contains(low, "startup-ipis"): - add("win10-startup-ipis") - case strings.Contains(low, "win10-compat"): - add("win10-compat") - case strings.Contains(low, "coroutine-fiber"): - add("win32-coroutine-fiber") - case strings.Contains(low, "win32-unicode-input"): - if sha := strings.TrimSpace(p.SHA256); sha != "" && !strings.EqualFold(sha, buggyWin32UnicodeInputPatchSHA256) { - add("win32-unicode-input") - } - case strings.Contains(low, "display-fixed-mode"): - add("display-fixed-mode") - } - } return out } -// HasCapability reports whether the runtime declares or infers capability capName. +// HasCapability reports whether manifest.json declares capability capName. func (m Manifest) HasCapability(capName string) bool { for _, c := range m.EffectiveCapabilities() { if strings.EqualFold(c, capName) { diff --git a/internal/runtime/kinds_test.go b/internal/runtime/kinds_test.go index 9e5bffd..c96d9d3 100644 --- a/internal/runtime/kinds_test.go +++ b/internal/runtime/kinds_test.go @@ -25,65 +25,19 @@ func TestKindRegistryAndCapabilities(t *testing.T) { t.Fatal("expected qemu-ovmf to be unregistered") } - // Legacy manifest infers capabilities from patches. - m := Manifest{ - Commit: "abc1234", - Patches: []Patch{ - {Name: "qemu-whpx-nested-virt-nonfatal.patch"}, - {Name: "qemu-whpx-win10-startup-ipis.patch"}, - }, - } + m := Manifest{Commit: "abc1234", Patches: []Patch{{Name: "qemu-whpx-nested-virt-nonfatal.patch"}}} if m.EffectiveKind() != DefaultKind { t.Fatalf("EffectiveKind = %q", m.EffectiveKind()) } - if !m.HasCapability("nested-virt-nonfatal") || !m.HasCapability("win10-startup-ipis") { - t.Fatalf("expected inferred capabilities, got %v", m.EffectiveCapabilities()) + if len(m.EffectiveCapabilities()) != 0 { + t.Fatalf("patch names must not infer capabilities: %v", m.EffectiveCapabilities()) } - fixedModeM := Manifest{Patches: []Patch{{Name: "reims-vgpu-display-fixed-mode.patch"}}} - if !fixedModeM.HasCapability("display-fixed-mode") { - t.Fatalf("expected display-fixed-mode capability, got %v", fixedModeM.EffectiveCapabilities()) + declared := Manifest{Capabilities: []string{"nested-virt-nonfatal", "win32-unicode-input", "nested-virt-nonfatal"}} + if !declared.HasCapability("nested-virt-nonfatal") || !declared.HasCapability("win32-unicode-input") { + t.Fatalf("declared capabilities = %v", declared.EffectiveCapabilities()) } - - // Buggy p2/p3 win32-unicode-input patch hash (issue #14) and empty hash must - // not advertise the win32-unicode-input capability; fixed patch hashes and - // explicit capability declarations must advertise it. - for _, tc := range []struct { - name string - m Manifest - want bool - }{ - { - name: "buggy-p3-hash", - m: Manifest{Patches: []Patch{{ - Name: "reims-vgpu-win32-unicode-input.patch", - SHA256: buggyWin32UnicodeInputPatchSHA256, - }}}, - want: false, - }, - { - name: "missing-hash", - m: Manifest{Patches: []Patch{{ - Name: "reims-vgpu-win32-unicode-input.patch", - }}}, - want: false, - }, - { - name: "fixed-patch-hash", - m: Manifest{Patches: []Patch{{ - Name: "reims-vgpu-win32-unicode-input.patch", - SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - }}}, - want: true, - }, - { - name: "explicit-capability", - m: Manifest{Capabilities: []string{"win32-unicode-input"}}, - want: true, - }, - } { - if got := tc.m.HasCapability("win32-unicode-input"); got != tc.want { - t.Fatalf("%s: HasCapability(win32-unicode-input) = %v, want %v", tc.name, got, tc.want) - } + if got := len(declared.EffectiveCapabilities()); got != 2 { + t.Fatalf("capability dedup = %d", got) } } diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index bc470cf..5968957 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -79,24 +79,48 @@ type Installed struct { Dir string `json:"dir"` Active bool `json:"active"` Manifest Manifest `json:"manifest"` + Spec KindSpec `json:"-"` } -func (r Installed) ReimsCtlPath() string { - name := "reims-ctl" - if runtime.GOOS == "windows" { - name += ".exe" +func specForKind(kind string) KindSpec { + spec, _ := NewKindRegistry().Get(kind) + if spec.Kind == "" { + spec.Kind = kind } - return filepath.Join(r.Dir, "bin", name) + return spec } -func (r Installed) ReimsUtilPath() string { - name := "reims-util" - if runtime.GOOS == "windows" { +func executablePath(dir, subdir, name string) string { + if name == "" { + return "" + } + if runtime.GOOS == "windows" && !strings.HasSuffix(strings.ToLower(name), ".exe") { name += ".exe" } - return filepath.Join(r.Dir, "bin", name) + return filepath.Join(dir, subdir, name) } +func (r Installed) runtimeSpec() KindSpec { + if r.Spec.Kind != "" || r.Spec.ControlExecutable != "" { + return r.Spec + } + kind := r.Kind + if kind == "" { + kind = r.Manifest.EffectiveKind() + } + return specForKind(kind) +} +func (r Installed) ControlPath() string { + return executablePath(r.Dir, "bin", r.runtimeSpec().ControlExecutable) +} +func (r Installed) UtilityPath() string { + return executablePath(r.Dir, "bin", r.runtimeSpec().UtilityExecutable) +} + +// ReimsCtlPath and ReimsUtilPath are compatibility wrappers for the built-in driver. +func (r Installed) ReimsCtlPath() string { return r.ControlPath() } +func (r Installed) ReimsUtilPath() string { return r.UtilityPath() } + func (r Installed) QemuPath() string { name := "qemu-system-x86_64" if runtime.GOOS == "windows" { @@ -140,8 +164,9 @@ func List(cfg *config.Config) ([]Installed, error) { Kind: kind, Commit: commit, Dir: dir, - Active: kind == DefaultKind && commit == cfg.ActiveRuntime, + Active: specForKind(kind).Builtin && commit == cfg.ActiveRuntime, Manifest: m, + Spec: specForKind(kind), }) continue } @@ -172,8 +197,9 @@ func List(cfg *config.Config) ([]Installed, error) { Kind: kind, Commit: commit, Dir: subDir, - Active: (kind == DefaultKind && commit == cfg.ActiveRuntime) || (kind+"/"+commit) == cfg.ActiveRuntime, + Active: (specForKind(kind).Builtin && commit == cfg.ActiveRuntime) || (kind+"/"+commit) == cfg.ActiveRuntime, Manifest: m, + Spec: specForKind(kind), }) } } @@ -211,7 +237,7 @@ func Remove(cfg *config.Config, commit string) (*Installed, error) { for i := range list { item := list[i] if item.Commit == want || (item.Kind+"/"+item.Commit) == want { - if match == nil || item.Kind == DefaultKind { + if match == nil || item.Spec.Builtin { cp := item match = &cp } @@ -273,11 +299,13 @@ func BuildChildEnv(cfg *config.Config, rt Installed) []string { "XDG_CONFIG_HOME": cfg.XDGConfigDir(), "XDG_DATA_HOME": cfg.XDGDataDir(), "XDG_CACHE_HOME": cfg.XDGCacheDir(), - "REIMS_CTL_QEMU": rt.QemuPath(), "TEMP": tmpDir, "TMP": tmpDir, "TMPDIR": tmpDir, } + if envName := rt.runtimeSpec().ControlQemuEnv; envName != "" { + overrides[envName] = rt.QemuPath() + } env := os.Environ() out := make([]string, 0, len(env)+len(overrides)) seenPath := false @@ -371,13 +399,14 @@ func InstallFromArchive(cfg *config.Config, archivePath string) (*Installed, err if !isSafeRuntimeSegment(m.Commit) { return nil, fmt.Errorf("invalid runtime commit %q in manifest.json", m.Commit) } - if kind != DefaultKind || m.Origin == "third-party" { + spec := specForKind(kind) + if !spec.Builtin || m.Origin == "third-party" { if err := ValidateThirdPartyProvenance(m); err != nil { return nil, err } } finalDir := filepath.Join(cfg.RuntimesDir(), m.Commit) - if kind != DefaultKind { + if !spec.Builtin { kindDir := filepath.Join(cfg.RuntimesDir(), kind) if err := os.MkdirAll(kindDir, 0o755); err != nil { return nil, err @@ -392,8 +421,9 @@ func InstallFromArchive(cfg *config.Config, archivePath string) (*Installed, err Kind: kind, Commit: m.Commit, Dir: finalDir, - Active: kind == DefaultKind, + Active: spec.Builtin, Manifest: m, + Spec: spec, }, nil } diff --git a/scripts/pack-runtime.ps1 b/scripts/pack-runtime.ps1 index 29d7fa4..1ec8adf 100644 --- a/scripts/pack-runtime.ps1 +++ b/scripts/pack-runtime.ps1 @@ -426,6 +426,18 @@ foreach ($rel in $keyFiles) { $fp = Join-Path $bundleDir ($rel -replace '/', '\') $hashes[$rel] = Get-Sha256 $fp } +$capabilities = @() +foreach ($patch in $patchEntries) { + $name = $patch.name.ToLowerInvariant() + $cap = $null + if ($name.Contains('nested-virt-nonfatal')) { $cap = 'nested-virt-nonfatal' } + elseif ($name.Contains('startup-ipis')) { $cap = 'win10-startup-ipis' } + elseif ($name.Contains('win10-compat')) { $cap = 'win10-compat' } + elseif ($name.Contains('coroutine-fiber')) { $cap = 'win32-coroutine-fiber' } + elseif ($name.Contains('win32-unicode-input') -and $patch.sha256 -and $patch.sha256 -ne '0b34f7c0ebdf3ca537c7f4523af97967a06924aedba7058dbc978870f561b366') { $cap = 'win32-unicode-input' } + elseif ($name.Contains('display-fixed-mode')) { $cap = 'display-fixed-mode' } + if ($cap -and $capabilities -notcontains $cap) { $capabilities += $cap } +} $manifest = [ordered]@{ schema_version = 2 name = $bundleName @@ -437,6 +449,9 @@ $manifest = [ordered]@{ util_commit = $utilCommit metal2vulkan_commit = $m2vCommit patches = $patchEntries + kind = 'reims-vgpu' + runtime_api = '1.0.0' + capabilities = $capabilities channel = $Channel source_package = "$srcName.tar.zst" symbols_package = "$symbolsName.tar.zst"