fix: address PR review findings
This commit is contained in:
1 parent
26728abac0
commit
93a12d3111
4 files changed
+49
-16
No files matched your search
@@ -144,6 +144,7 @@ type App struct {
|
||||
// computerUseOwnedVMs records temporary CoW VMs created by an MCP session.
|
||||
// Ownership is process-scoped and disappears when the desktop process exits.
|
||||
computerUseOwnedVMs map[string]map[string]bool
|
||||
computerUseCloneMu sync.Mutex
|
||||
}
|
||||
|
||||
// Lifecycle exposes Wails lifecycle callbacks without exporting them as
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -18,12 +19,13 @@ import (
|
||||
|
||||
const computerUsePreviewWidth = 1280
|
||||
const computerUseMaxTextRunes = 4000
|
||||
const computerUseSecretPluginID = "reims.agent"
|
||||
|
||||
func (a *App) computerUseHostHandlers(mgr *plugin.Manager, pluginID string) map[string]plugin.RPCRequestHandler {
|
||||
if mgr == nil || mgr.Broker() == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]plugin.RPCRequestHandler{
|
||||
handlers := map[string]plugin.RPCRequestHandler{
|
||||
"host/vm.list": a.authorizedComputerUseVMHandler(mgr, pluginID, "vm:read", "host/vm.list", a.handleComputerUseVMList),
|
||||
"host/vm.get": a.authorizedComputerUseVMHandler(mgr, pluginID, "vm:read", "host/vm.get", a.handleComputerUseVMGet),
|
||||
"host/vm.start": a.authorizedComputerUseVMHandler(mgr, pluginID, "vm:control", "host/vm.start", a.handleComputerUseVMStart),
|
||||
@@ -37,8 +39,12 @@ func (a *App) computerUseHostHandlers(mgr *plugin.Manager, pluginID string) map[
|
||||
"host/console.pointer": a.authorizedComputerUseVMHandler(mgr, pluginID, "qmp:write", "host/console.pointer", a.handleComputerUsePointer),
|
||||
"host/console.key": a.authorizedComputerUseVMHandler(mgr, pluginID, "qmp:write", "host/console.key", a.handleComputerUseKey),
|
||||
"host/console.type": a.authorizedComputerUseVMHandler(mgr, pluginID, "qmp:write", "host/console.type", a.handleComputerUseType),
|
||||
"host/console.typeSecret": a.authorizedComputerUseVMHandler(mgr, pluginID, "qmp:write", "host/console.typeSecret", a.handleComputerUseTypeSecretForPlugin),
|
||||
}
|
||||
// The plugin ID is supplied by the manager while wiring the sidecar, never
|
||||
// by the channel payload. Keep the handler registered for a stable method
|
||||
// table, then reject every plugin except the official MCP service below.
|
||||
handlers["host/console.typeSecret"] = a.authorizedComputerUseVMHandler(mgr, pluginID, "qmp:write", "host/console.typeSecret", a.handleComputerUseTypeSecret)
|
||||
return handlers
|
||||
}
|
||||
|
||||
type computerUseVMHandler func(context.Context, json.RawMessage, *plugin.Manager, string) (any, error)
|
||||
@@ -49,14 +55,10 @@ func (a *App) authorizedComputerUseVMHandler(mgr *plugin.Manager, pluginID, perm
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) handleComputerUseTypeSecretForPlugin(ctx context.Context, raw json.RawMessage, mgr *plugin.Manager, pluginID string) (any, error) {
|
||||
if pluginID != "reims.agent" {
|
||||
func (a *App) handleComputerUseTypeSecret(ctx context.Context, raw json.RawMessage, mgr *plugin.Manager, pluginID string) (any, error) {
|
||||
if pluginID != computerUseSecretPluginID {
|
||||
return nil, errors.New("secret typing is reserved for reims.agent")
|
||||
}
|
||||
return a.handleComputerUseTypeSecret(ctx, raw, mgr, pluginID)
|
||||
}
|
||||
|
||||
func (a *App) handleComputerUseTypeSecret(ctx context.Context, raw json.RawMessage, mgr *plugin.Manager, pluginID string) (any, error) {
|
||||
var req struct {
|
||||
VM string `json:"vm"`
|
||||
Setting string `json:"setting"`
|
||||
@@ -74,7 +76,7 @@ func (a *App) handleComputerUseTypeSecret(ctx context.Context, raw json.RawMessa
|
||||
if mgr == nil {
|
||||
return nil, errors.New("plugin manager is unavailable")
|
||||
}
|
||||
secret, err := mgr.SecretSetting("reims.agent", vm.Ref().Key(), req.Setting)
|
||||
secret, err := mgr.SecretSetting(pluginID, vm.Ref().Key(), req.Setting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -194,7 +196,7 @@ func (a *App) handleComputerUseVMShutdown(_ context.Context, raw json.RawMessage
|
||||
return map[string]any{"ok": true}, nil
|
||||
}
|
||||
|
||||
func (a *App) handleComputerUseVMClone(_ context.Context, raw json.RawMessage, mgr *plugin.Manager, pluginID string) (any, error) {
|
||||
func (a *App) handleComputerUseVMClone(ctx context.Context, raw json.RawMessage, mgr *plugin.Manager, pluginID string) (any, error) {
|
||||
var req struct {
|
||||
VM string `json:"vm"`
|
||||
CloneName string `json:"clone_name"`
|
||||
@@ -213,13 +215,31 @@ func (a *App) handleComputerUseVMClone(_ context.Context, raw json.RawMessage, m
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cloneRef := driver.NewVMRef(vm.DriverID, cloneName)
|
||||
a.computerUseCloneMu.Lock()
|
||||
defer a.computerUseCloneMu.Unlock()
|
||||
if existing, listErr := a.listVMsInternal(ctx); listErr != nil {
|
||||
return nil, listErr
|
||||
} else if slices.ContainsFunc(existing, func(item driver.VMListItem) bool {
|
||||
return item.Name == cloneName && driver.CanonicalDriverID(item.DriverID) == driver.CanonicalDriverID(vm.DriverID)
|
||||
}) {
|
||||
return nil, fmt.Errorf("VM name %q is already taken", cloneName)
|
||||
}
|
||||
if err := a.CloneVMCoWWithDriver(vm.DriverID, vm.Name, cloneName); err != nil {
|
||||
// CloneCoW can fail after creating an overlay or VM record. The target
|
||||
// was absent during the preflight above, so clean up any partial target.
|
||||
if existing, listErr := a.listVMsInternal(ctx); listErr == nil && slices.ContainsFunc(existing, func(item driver.VMListItem) bool {
|
||||
return item.Name == cloneName && driver.CanonicalDriverID(item.DriverID) == driver.CanonicalDriverID(vm.DriverID)
|
||||
}) {
|
||||
if cleanupErr := a.DeleteVMWithDriver(vm.DriverID, cloneName, false); cleanupErr != nil {
|
||||
return nil, errors.Join(err, fmt.Errorf("clean up failed clone %q: %w", cloneName, cleanupErr))
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// The lifecycle service may not expose a newly-created VM until the next
|
||||
// inventory poll. Return the canonical reference immediately so callers can
|
||||
// start the clone without turning a successful clone into a false failure.
|
||||
cloneRef := driver.NewVMRef(vm.DriverID, cloneName)
|
||||
a.markComputerUseOwnedVM(pluginID, cloneRef.Key(), true)
|
||||
return map[string]any{"vm": cloneRef.Key(), "name": cloneName, "driver_id": vm.DriverID, "state": "stopped", "owned": true}, nil
|
||||
}
|
||||
@@ -335,11 +355,11 @@ func (a *App) resolveComputerUseVM(mgr *plugin.Manager, pluginID, ref string) (d
|
||||
return driver.VMInfo{}, err
|
||||
}
|
||||
if driver.CanonicalDriverID(vm.DriverID) != builtin.IDDriverReimsMacOS {
|
||||
return driver.VMInfo{}, fmt.Errorf("computer use is unavailable for driver %q", vm.DriverID)
|
||||
return driver.VMInfo{}, fmt.Errorf("computer_use_unavailable: driver %q does not support computer use", vm.DriverID)
|
||||
}
|
||||
current, ok := a.polledVMForDriver(vm.DriverID, vm.Name)
|
||||
if !ok || current.State != "running" || current.PID == nil || *current.PID <= 0 {
|
||||
return driver.VMInfo{}, fmt.Errorf("VM %q is not running", vm.Name)
|
||||
return driver.VMInfo{}, fmt.Errorf("vm_not_running: VM %q is not running", vm.Name)
|
||||
}
|
||||
if phase := computerUseVMPhase(vm); phase != "normal" {
|
||||
return driver.VMInfo{}, fmt.Errorf("confirmation_required: VM %q is in %s phase", vm.Name, phase)
|
||||
|
||||
@@ -83,7 +83,10 @@ func (a *App) initPlugins() {
|
||||
return driver.NewSidecarDriver(contrib, sup)
|
||||
},
|
||||
HostHandlers: func(pluginID string) map[string]plugin.RPCRequestHandler {
|
||||
return a.computerUseHostHandlers(mgr, pluginID)
|
||||
a.mu.Lock()
|
||||
current := a.pluginMgr
|
||||
a.mu.Unlock()
|
||||
return a.computerUseHostHandlers(current, pluginID)
|
||||
},
|
||||
OnSettingsChanged: func(pluginID, vmName string) {
|
||||
a.onPluginSettingsChanged(pluginID, vmName)
|
||||
|
||||
@@ -1044,8 +1044,8 @@ func (m *Manager) SecretSetting(pluginID, vmName, key string) (string, error) {
|
||||
return "", fmt.Errorf("setting %q is not a secret", key)
|
||||
}
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if m.state == nil {
|
||||
m.mu.RUnlock()
|
||||
return "", m.stateNotLoadedErr()
|
||||
}
|
||||
values := map[string]any(nil)
|
||||
@@ -1057,10 +1057,19 @@ func (m *Manager) SecretSetting(pluginID, vmName, key string) (string, error) {
|
||||
}
|
||||
}
|
||||
text, _ := values[key].(string)
|
||||
m.mu.RUnlock()
|
||||
if text == "" {
|
||||
return "", errors.New("secret setting is empty")
|
||||
}
|
||||
return unprotectSettingSecret(text)
|
||||
if strings.HasPrefix(text, protectedSecretPrefix) {
|
||||
return unprotectSettingSecret(text)
|
||||
}
|
||||
// Versions before DPAPI protection stored secret settings as plain text.
|
||||
// Re-save the legacy value through SetSettings so the next read is protected.
|
||||
if err := m.SetSettings(pluginID, vmName, map[string]any{key: text}); err != nil {
|
||||
return "", fmt.Errorf("migrate legacy secret setting %q: %w", key, err)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// SavedVMSettingsForPlugin returns a copy of all saved per-VM setting maps for pluginID.
|
||||
|
||||
Reference in new issue
Block a user