Files
Reims-Go/internal/vmstart/core.go
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

104 lines
2.4 KiB
Go

// Package vmstart holds driver-independent start helpers: commit-memory
// checks, process affinity and command-output line writing.
package vmstart
import (
"bufio"
"context"
"os"
"strconv"
"strings"
"time"
"github.com/Nwflower/reims-go/internal/host"
)
func parseSizeGB(raw string) (float64, bool) {
raw = strings.TrimSpace(raw)
if len(raw) < 2 {
return 0, false
}
v, err := strconv.ParseFloat(raw[:len(raw)-1], 64)
if err != nil || v <= 0 {
return 0, false
}
switch strings.ToUpper(raw[len(raw)-1:]) {
case "G":
return v, true
case "M":
return v / 1024, true
default:
return 0, false
}
}
// WaitForCommitMemory waits until guest RAM plus the host margin is available.
func WaitForCommitMemory(ctx context.Context, memory string, timeout time.Duration, onWait func(availGB, neededGB float64)) bool {
return waitForCommitMemory(ctx, memory, timeout, 250*time.Millisecond, host.CommitAvailGB, onWait)
}
func waitForCommitMemory(ctx context.Context, memory string, timeout, pollInterval time.Duration, probe func() (float64, bool), onWait func(availGB, neededGB float64)) bool {
memGB, ok := parseSizeGB(memory)
if !ok {
return true
}
needed := memGB + host.StartMarginGB
avail, ok := probe()
if !ok || needed <= avail {
return true
}
if onWait != nil {
onWait(avail, needed)
}
if timeout <= 0 {
return false
}
if pollInterval <= 0 {
pollInterval = 250 * time.Millisecond
}
timer := time.NewTimer(timeout)
defer timer.Stop()
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return false
case <-timer.C:
avail, ok := probe()
return !ok || needed <= avail
case <-ticker.C:
avail, ok := probe()
if !ok || needed <= avail {
return true
}
}
}
}
// LineWriter turns command output into one log call per non-empty line.
type LineWriter func(string)
func (w LineWriter) Write(b []byte) (int, error) {
sc := bufio.NewScanner(strings.NewReader(string(b)))
for sc.Scan() {
if line := strings.TrimSpace(sc.Text()); line != "" {
w(line)
}
}
return len(b), nil
}
// CloseRequestEnv returns the environment variables that let a VM display
// window delegate close confirmation back to this desktop client executable.
func CloseRequestEnv(name string) []string {
executable, err := os.Executable()
if err != nil {
return nil
}
return []string{
"REIMS_GO_VM_CLOSE_EXE=" + executable,
"REIMS_GO_VM_CLOSE_NAME=" + name,
}
}