Files
Reims-Go/internal/app/computer_use_clone_settings_test.go
T
Nwflower 33e28520b7 fix(mcp): read clone secrets and SSH user from the source VM
A temporary clone made by session_create keeps no vm settings of its own, so
vm_unlock on a clone always failed with "secret setting is empty" even though
the source VM had a saved login password and AI access enabled. The SSH user
was empty for the same reason: the clone has no settings and no copied
.reims-go-ssh-user file, while access is granted through the owns-clone path.

Record the source VM key in the temporary VM ownership marker and fall back to
it when a clone has no value of its own. The fallback reads the single stored
secret instead of copying it, so the password never multiplies across clones,
nothing is left behind when a clone is disposed, and revoking AI access on the
source removes the fallback immediately. Surface a clear localized error when
no VM has a saved password.

model: deepseek/deepseek-v4.1-flash
agent: lead (DeepSeek Harness)
2026-12-04 06:02:10 -07:00

174 lines
5.9 KiB
Go

package app
import (
"context"
"errors"
"io"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/lifecycle"
"github.com/Nwflower/reims-go/internal/plugin"
"github.com/Nwflower/reims-go/internal/testutil/fakedriver"
)
const cloneSourceSettingPlugin = "test.clone.agent"
// newCloneSettingsEnv builds a host whose MCP clone reads its settings from a
// source VM. The plugin declares the same login_password/username/
// allow_ai_access schema as reims.agent, scoped to the VM.
func newCloneSettingsEnv(t *testing.T) (*App, *plugin.Manager, driver.Driver) {
t.Helper()
cfg := config.Default()
cfg.DataRoot = t.TempDir()
mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: cfg.DataRoot, HostVersion: "0.0.0"})
if err != nil {
t.Fatal(err)
}
t.Cleanup(mgr.Close)
if err := mgr.Registry().Register(plugin.PluginDef{
Manifest: plugin.Manifest{
ID: cloneSourceSettingPlugin,
Version: "1.0.0",
Name: plugin.LocalizedText{EnUS: cloneSourceSettingPlugin},
Settings: &plugin.SettingsSchema{
Scope: "vm",
Properties: map[string]plugin.SettingProperty{
"allow_ai_access": {Type: "boolean"},
"username": {Type: "string"},
"login_password": {Type: "string", Secret: true},
},
},
},
Source: plugin.SourceBuiltin,
Desired: true,
Activate: func(_ context.Context, _ *plugin.Scope) error {
return nil
},
}); err != nil {
t.Fatal(err)
}
mgr.Registry().Reconcile(context.Background())
h := fakedriver.NewHarness(t)
drv := h.Add("hosted", "test.clone.driver", "test.driver.clone")
app := &App{
cfg: cfg,
pluginMgr: mgr,
transitions: map[string]string{},
pendingChanges: map[string][]PendingChange{},
pollWake: make(chan struct{}, 1),
diskPaths: map[string]cachedDiskPath{},
procSamples: map[string]host.ProcessSample{},
driverRouter: h.Router,
vmStateTable: h.States,
lifecycleSvc: lifecycle.New(h.Registry, h.Router, nil),
}
return app, mgr, drv
}
func createVMs(t *testing.T, drv driver.Driver, names ...string) {
t.Helper()
for _, name := range names {
if err := drv.Create(context.Background(), driver.CreateRequest{DriverID: drv.ID(), Name: name}, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
}
}
func TestCloneSecretFallsBackToAuthorizedSource(t *testing.T) {
app, mgr, drv := newCloneSettingsEnv(t)
createVMs(t, drv, "source", "__agent-clone")
sourceKey := driver.NewVMRef(drv.ID(), "source").Key()
cloneKey := driver.NewVMRef(drv.ID(), "__agent-clone").Key()
if err := mgr.SetSettings(cloneSourceSettingPlugin, sourceKey, map[string]any{
"allow_ai_access": true,
"username": "macuser",
"login_password": "secret-from-source",
}); err != nil {
t.Fatal(err)
}
cloneVM, err := app.resolveVMInfo(drv.ID(), "__agent-clone")
if err != nil {
t.Fatal(err)
}
if err := writeComputerUseOwnership(cloneVM.Directory, cloneSourceSettingPlugin, cloneKey, sourceKey); err != nil {
t.Fatal(err)
}
got, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password")
if err != nil {
t.Fatalf("clone should borrow the source password: %v", err)
}
if got != "secret-from-source" {
t.Fatalf("clone secret = %q, want the source value", got)
}
if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "macuser" {
t.Fatalf("clone SSH user = %q, want the source user", user)
}
// The clone's own value wins over the source.
if err := mgr.SetSettings(cloneSourceSettingPlugin, cloneKey, map[string]any{
"allow_ai_access": true,
"username": "cloneuser",
"login_password": "clone-own-password",
}); err != nil {
t.Fatal(err)
}
got, err = app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password")
if err != nil || got != "clone-own-password" {
t.Fatalf("clone own secret = %q (%v), want its own value", got, err)
}
if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "cloneuser" {
t.Fatalf("clone SSH user = %q, want its own value", user)
}
// Clearing the clone's own password and revoking access on the source
// removes the fallback again.
if err := mgr.SetSettings(cloneSourceSettingPlugin, cloneKey, map[string]any{"login_password": ""}); err != nil {
t.Fatal(err)
}
if err := mgr.SetSettings(cloneSourceSettingPlugin, sourceKey, map[string]any{"allow_ai_access": false}); err != nil {
t.Fatal(err)
}
if _, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password"); !errors.Is(err, plugin.ErrSecretEmpty) {
t.Fatalf("revoked source should leave no fallback, got %v", err)
}
if user := app.computerUseSSHUser(mgr, cloneSourceSettingPlugin, cloneVM); user != "cloneuser" {
t.Fatalf("clone keeps its own SSH user after source revoke, got %q", user)
}
}
func TestCloneSecretWithoutSourceStaysEmpty(t *testing.T) {
app, mgr, drv := newCloneSettingsEnv(t)
createVMs(t, drv, "__agent-orphan")
cloneVM, err := app.resolveVMInfo(drv.ID(), "__agent-orphan")
if err != nil {
t.Fatal(err)
}
// A marker with no source (the lost-marker repair path) has no fallback.
if err := writeComputerUseOwnership(cloneVM.Directory, cloneSourceSettingPlugin, cloneVM.Ref().Key(), ""); err != nil {
t.Fatal(err)
}
if _, err := app.computerUseSecretSetting(mgr, cloneSourceSettingPlugin, cloneVM, "login_password"); !errors.Is(err, plugin.ErrSecretEmpty) {
t.Fatalf("clone without a source should have no fallback, got %v", err)
}
}
func TestSecretMissingMessageLocalized(t *testing.T) {
const key = "err.computer_use.secret_missing"
for _, loc := range []i18n.Locale{i18n.ZhCN, i18n.EnUS} {
msg := strings.TrimSpace(i18n.M(loc, key, nil))
if msg == "" || msg == key {
t.Fatalf("%s %q did not resolve", loc, key)
}
}
}