fix: persist temporary computer-use ownership
This commit is contained in:
1 parent
93a12d3111
commit
3a82dd393a
5 files changed
+110
-3
No files matched your search
@@ -2,6 +2,6 @@
|
|||||||
"group": "new",
|
"group": "new",
|
||||||
"order": 0,
|
"order": 0,
|
||||||
"authors": ["bvzrays"],
|
"authors": ["bvzrays"],
|
||||||
"zh": "新增默认启用的「虚拟机交互」扩展,统一处理虚拟机截图、鼠标、键盘与文本输入;新增按虚拟机授权的插件控制通道和 MCP 服务,支持客体 SSH、SFTP、隔离测试运行及临时 CoW 会话。",
|
"zh": "新增默认启用的「虚拟机交互」扩展,统一处理虚拟机截图、鼠标、键盘与文本输入;新增按虚拟机授权的插件控制通道和 MCP 服务。",
|
||||||
"en": "Add the enabled-by-default Computer Use extension for VM screenshots, pointer, keyboard, and text input, plus per-VM authorization, guest SSH/SFTP, isolated test runs, and temporary CoW sessions for the MCP service."
|
"en": "Add the enabled-by-default Computer Use extension for VM screenshots, pointer, keyboard, and text input, plus per-VM authorization and the MCP service."
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ module github.com/Nwflower/reims-go
|
|||||||
go 1.26.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/Microsoft/go-winio v0.6.2
|
||||||
github.com/klauspost/compress v1.20.5
|
github.com/klauspost/compress v1.20.5
|
||||||
github.com/pelletier/go-toml/v2 v2.4.3
|
github.com/pelletier/go-toml/v2 v2.4.3
|
||||||
github.com/ulikunitz/xz v0.5.17
|
github.com/ulikunitz/xz v0.5.17
|
||||||
@@ -13,7 +14,6 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3 // indirect
|
git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3 // indirect
|
||||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
|
||||||
github.com/bep/debounce v1.2.1 // indirect
|
github.com/bep/debounce v1.2.1 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.16 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.16 // indirect
|
||||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||||
|
|||||||
@@ -459,6 +459,7 @@ func (a *App) pollVMs(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
for key := range removedVMs {
|
for key := range removedVMs {
|
||||||
name := vmNameFromKey(key)
|
name := vmNameFromKey(key)
|
||||||
|
a.forgetComputerUseOwnedVM(key)
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
delete(a.transitions, key)
|
delete(a.transitions, key)
|
||||||
delete(a.pendingChanges, key)
|
delete(a.pendingChanges, key)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -21,6 +22,14 @@ const computerUsePreviewWidth = 1280
|
|||||||
const computerUseMaxTextRunes = 4000
|
const computerUseMaxTextRunes = 4000
|
||||||
const computerUseSecretPluginID = "reims.agent"
|
const computerUseSecretPluginID = "reims.agent"
|
||||||
|
|
||||||
|
const computerUseOwnershipFile = ".reims-go-computer-use"
|
||||||
|
|
||||||
|
type computerUseOwnership struct {
|
||||||
|
Version int `json:"version"`
|
||||||
|
PluginID string `json:"plugin_id"`
|
||||||
|
VMKey string `json:"vm_key"`
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) computerUseHostHandlers(mgr *plugin.Manager, pluginID string) map[string]plugin.RPCRequestHandler {
|
func (a *App) computerUseHostHandlers(mgr *plugin.Manager, pluginID string) map[string]plugin.RPCRequestHandler {
|
||||||
if mgr == nil || mgr.Broker() == nil {
|
if mgr == nil || mgr.Broker() == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -237,6 +246,19 @@ func (a *App) handleComputerUseVMClone(ctx context.Context, raw json.RawMessage,
|
|||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
cloneVM, err := a.resolveComputerUseVMInfo(cloneRef.Key())
|
||||||
|
if err != nil {
|
||||||
|
if cleanupErr := a.DeleteVMWithDriver(vm.DriverID, cloneName, false); cleanupErr != nil {
|
||||||
|
return nil, errors.Join(err, fmt.Errorf("clean up unregistered clone %q: %w", cloneName, cleanupErr))
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := writeComputerUseOwnership(cloneVM.Directory, pluginID, cloneRef.Key()); err != nil {
|
||||||
|
if cleanupErr := a.DeleteVMWithDriver(vm.DriverID, cloneName, false); cleanupErr != nil {
|
||||||
|
return nil, errors.Join(err, fmt.Errorf("clean up unregistered clone %q: %w", cloneName, cleanupErr))
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
// The lifecycle service may not expose a newly-created VM until the next
|
// The lifecycle service may not expose a newly-created VM until the next
|
||||||
// inventory poll. Return the canonical reference immediately so callers can
|
// inventory poll. Return the canonical reference immediately so callers can
|
||||||
// start the clone without turning a successful clone into a false failure.
|
// start the clone without turning a successful clone into a false failure.
|
||||||
@@ -266,6 +288,7 @@ func (a *App) handleComputerUseVMDelete(ctx context.Context, raw json.RawMessage
|
|||||||
if err := a.DeleteVMWithDriver(vm.DriverID, vm.Name, false); err != nil {
|
if err := a.DeleteVMWithDriver(vm.DriverID, vm.Name, false); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
removeComputerUseOwnership(vm.Directory)
|
||||||
a.markComputerUseOwnedVM(pluginID, vm.Ref().Key(), false)
|
a.markComputerUseOwnedVM(pluginID, vm.Ref().Key(), false)
|
||||||
return map[string]any{"ok": true}, nil
|
return map[string]any{"ok": true}, nil
|
||||||
}
|
}
|
||||||
@@ -410,6 +433,86 @@ func (a *App) markComputerUseOwnedVM(pluginID, vmKey string, owned bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) forgetComputerUseOwnedVM(vmKey string) {
|
||||||
|
vmKey = strings.TrimSpace(vmKey)
|
||||||
|
if vmKey == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
a.mu.Lock()
|
||||||
|
defer a.mu.Unlock()
|
||||||
|
for pluginID, owned := range a.computerUseOwnedVMs {
|
||||||
|
delete(owned, vmKey)
|
||||||
|
if len(owned) == 0 {
|
||||||
|
delete(a.computerUseOwnedVMs, pluginID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeComputerUseOwnership(vmDir, pluginID, vmKey string) error {
|
||||||
|
vmDir = strings.TrimSpace(vmDir)
|
||||||
|
pluginID = strings.TrimSpace(pluginID)
|
||||||
|
vmKey = strings.TrimSpace(vmKey)
|
||||||
|
if vmDir == "" || pluginID == "" || vmKey == "" {
|
||||||
|
return errors.New("temporary VM ownership metadata is incomplete")
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(computerUseOwnership{Version: 1, PluginID: pluginID, VMKey: vmKey})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path := filepath.Join(vmDir, computerUseOwnershipFile)
|
||||||
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create temporary VM ownership marker: %w", err)
|
||||||
|
}
|
||||||
|
if _, writeErr := file.Write(append(data, '\n')); writeErr != nil {
|
||||||
|
_ = file.Close()
|
||||||
|
_ = os.Remove(path)
|
||||||
|
return fmt.Errorf("write temporary VM ownership marker: %w", writeErr)
|
||||||
|
}
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
_ = os.Remove(path)
|
||||||
|
return fmt.Errorf("close temporary VM ownership marker: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeComputerUseOwnership(vmDir string) {
|
||||||
|
if strings.TrimSpace(vmDir) == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = os.Remove(filepath.Join(vmDir, computerUseOwnershipFile))
|
||||||
|
}
|
||||||
|
|
||||||
|
func readComputerUseOwnership(vm driver.VMInfo) (computerUseOwnership, bool) {
|
||||||
|
if vm.Directory == "" || !strings.HasPrefix(vm.Name, "__agent-") {
|
||||||
|
return computerUseOwnership{}, false
|
||||||
|
}
|
||||||
|
raw, err := os.ReadFile(filepath.Join(vm.Directory, computerUseOwnershipFile))
|
||||||
|
if err != nil {
|
||||||
|
return computerUseOwnership{}, false
|
||||||
|
}
|
||||||
|
var marker computerUseOwnership
|
||||||
|
if json.Unmarshal(raw, &marker) != nil || marker.Version != 1 || marker.PluginID == "" || marker.VMKey != vm.Ref().Key() {
|
||||||
|
return computerUseOwnership{}, false
|
||||||
|
}
|
||||||
|
return marker, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) restoreComputerUseOwnedVMs(ctx context.Context) error {
|
||||||
|
vms, err := a.listVMsInternal(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, item := range vms {
|
||||||
|
vm := driver.VMInfo{Name: item.Name, DriverID: item.DriverID, Directory: item.Dir}
|
||||||
|
marker, ok := readComputerUseOwnership(vm)
|
||||||
|
if ok {
|
||||||
|
a.markComputerUseOwnedVM(marker.PluginID, marker.VMKey, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func computerUseVMPhase(vm driver.VMInfo) string {
|
func computerUseVMPhase(vm driver.VMInfo) string {
|
||||||
if vmInfoBool(vm, "has_installer") {
|
if vmInfoBool(vm, "has_installer") {
|
||||||
return "installer"
|
return "installer"
|
||||||
|
|||||||
@@ -226,6 +226,9 @@ func (a *App) initPlugins() {
|
|||||||
fetch.SetDefaultDownloader(a.activeDownloader)
|
fetch.SetDefaultDownloader(a.activeDownloader)
|
||||||
|
|
||||||
mgr.Registry().Reconcile(context.Background())
|
mgr.Registry().Reconcile(context.Background())
|
||||||
|
if err := a.restoreComputerUseOwnedVMs(context.Background()); err != nil {
|
||||||
|
a.emitLog("plugin", "Could not restore temporary computer-use VM ownership: "+err.Error())
|
||||||
|
}
|
||||||
mgr.MarkBootComplete()
|
mgr.MarkBootComplete()
|
||||||
a.syncInputFilters()
|
a.syncInputFilters()
|
||||||
a.syncKeymapRules()
|
a.syncKeymapRules()
|
||||||
|
|||||||
Reference in new issue
Block a user