Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The agent binary runs inside the (Linux) workspace, so it is a Linux binary (`de

## Testing and Headless Verification

When testing local external providers with `--disable-daemon`, verify that startup and workspace logs pass configured options to the runtime subprocess and that the serialized agent payload excludes them. Include a host secret with the same name to check the local SSH session handoff. Provider options can contain host credentials.

### Headless / Xvfb Requirements

Desktop tests run inside an Electron browser environment. In headless or container environments, Electron commands require an `xvfb-run` prefix to emulate a display server:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

Devsy gives teams and their AI coding agents separate workspaces built from one standard definition. Workspaces run on Docker, Kubernetes, cloud providers, and SSH remote hosts.

Local external providers keep their configured runtime options when you use `devsy workspace up --disable-daemon`, including when a host secret has the same environment name. Provider options stay out of the workspace agent payload in this mode. Workspace logs use the configured runtime options as well.

<table align="center">
<tr>
<td><b>Website</b></td>
Expand Down
54 changes: 46 additions & 8 deletions cmd/workspace/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
clientpkg "github.com/devsy-org/devsy/pkg/client"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/log"
"github.com/devsy-org/devsy/pkg/provider"
"github.com/devsy-org/devsy/pkg/ssh"
"github.com/devsy-org/devsy/pkg/tunnel"
"github.com/devsy-org/devsy/pkg/workspace"
Expand Down Expand Up @@ -133,6 +134,15 @@ func (cmd *LogsCmd) getWorkspaceClient(
// injectLogsAgent injects the devsy agent binary over stdin/stdout and runs the
// remote ssh-server that runLogsSession then connects to.
func injectLogsAgent(ctx context.Context, params injectLogsAgentParams) error {
info, err := localLogsAgentInfo(params.client)
if err != nil {
return err
}
var environment []string
if info != nil {
environment = provider.ToEnvironment(info.Workspace, info.Machine, info.Options, nil)
}

streamer := log.NewJSONLogStreamer(log.StreamerOptions{
FallbackLevel: log.LevelDebug,
DetectLevelPrefixes: true,
Expand All @@ -149,17 +159,34 @@ func injectLogsAgent(ctx context.Context, params injectLogsAgentParams) error {
RawStdout: true,
})
},
IsLocal: params.client.AgentLocal(),
RemoteAgentPath: params.client.AgentPath(),
DownloadURL: params.client.AgentURL(),
Command: params.sshServerCmd,
Stdin: params.stdin,
Stdout: params.stdout,
Stderr: streamer,
Timeout: params.timeout,
LocalEnvironment: environment,
IsLocal: params.client.AgentLocal(),
RemoteAgentPath: params.client.AgentPath(),
DownloadURL: params.client.AgentURL(),
Command: params.sshServerCmd,
Stdin: params.stdin,
Stdout: params.stdout,
Stderr: streamer,
Timeout: params.timeout,
})
}

func localLogsAgentInfo(
workspaceClient clientpkg.WorkspaceClient,
) (*provider.AgentWorkspaceInfo, error) {
if !workspaceClient.AgentLocal() {
return nil, nil
}
_, info, err := workspaceClient.AgentInfo(provider.CLIOptions{})
if err != nil {
return nil, fmt.Errorf("get agent info for logs: %w", err)
}
if info.Agent.Driver != provider.ExternalDriver {
return nil, nil
}
return info, nil
}

type injectLogsAgentParams struct {
client clientpkg.WorkspaceClient
sshServerCmd string
Expand All @@ -169,6 +196,10 @@ type injectLogsAgentParams struct {
}

func runLogsSession(stdout, stdin *os.File, client clientpkg.WorkspaceClient) error {
info, err := localLogsAgentInfo(client)
if err != nil {
return err
}
sshClient, err := ssh.StdioClientWithUser(stdout, stdin, "")
if err != nil {
return err
Expand All @@ -180,6 +211,13 @@ func runLogsSession(stdout, stdin *os.File, client clientpkg.WorkspaceClient) er
return err
}
defer func() { _ = session.Close() }()
if info != nil {
for name, value := range provider.ToOptions(info.Workspace, info.Machine, info.Options) {
if err := session.Setenv(name, value); err != nil {
return fmt.Errorf("set provider environment %q: %w", name, err)
}
}
}

agentCommand := fmt.Sprintf(
"%s internal agent workspace logs --context %s --id %s",
Expand Down
102 changes: 102 additions & 0 deletions cmd/workspace/logs_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package workspace

import (
"errors"
"io"
"os"
"testing"

"github.com/devsy-org/devsy/pkg/client"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/provider"
"github.com/stretchr/testify/suite"
)

type logsEnvironmentSuite struct{ suite.Suite }

func TestLogsEnvironment(t *testing.T) { suite.Run(t, new(logsEnvironmentSuite)) }

func (s *logsEnvironmentSuite) TestLocalInjection() {
s.T().Setenv(config.EnvHome, s.T().TempDir())
s.T().Setenv("MSB_PATH", "ambient-runtime")
s.T().Setenv("MSB_LIBKRUNFW_PATH", "")
s.T().Setenv("MSB_HOME", "")
for _, tc := range []struct{ name, driver, want string }{
{name: "external", driver: provider.ExternalDriver, want: "configured runtime|configured firmware|workspace home"},
{name: "docker", driver: provider.DockerDriver, want: "ambient-runtime||"},
} {
s.Run(tc.name, func() {
c := newLogsEnvironmentClient(tc.driver)
reader, writer, err := os.Pipe()
s.Require().NoError(err)
defer func() { s.NoError(reader.Close()) }()
defer func() { _ = writer.Close() }()
err = injectLogsAgent(s.T().Context(), injectLogsAgentParams{
client: c, stdout: writer,
sshServerCmd: `printf '%s|%s|%s' "$MSB_PATH" "$MSB_LIBKRUNFW_PATH" "$MSB_HOME"`,
})
s.Require().NoError(err)
s.Require().NoError(writer.Close())
output, err := io.ReadAll(reader)
s.Require().NoError(err)
s.Equal(tc.want, string(output))
s.Equal("ambient-runtime", os.Getenv("MSB_PATH"))
})
}
}

func (s *logsEnvironmentSuite) TestResolutionError() {
c := newLogsEnvironmentClient(provider.ExternalDriver)
c.infoErr = errors.New("option resolution failed")
err := injectLogsAgent(s.T().Context(), injectLogsAgentParams{client: c})
s.ErrorIs(err, c.infoErr)
}

func (s *logsEnvironmentSuite) TestRemoteInjectionDoesNotResolveOptions() {
c := newLogsEnvironmentClient(provider.ExternalDriver)
c.local = false
c.infoErr = errors.New("remote options must not be requested")
info, err := localLogsAgentInfo(c)
s.Require().NoError(err)
s.Nil(info)
s.Zero(c.infoCalls)
}

type logsEnvironmentClient struct {
client.WorkspaceClient
info *provider.AgentWorkspaceInfo
infoErr error
local bool
infoCalls int
}

func (c *logsEnvironmentClient) AgentLocal() bool { return c.local }
func (*logsEnvironmentClient) AgentPath() string { return "" }
func (*logsEnvironmentClient) AgentURL() string { return "" }

func (c *logsEnvironmentClient) AgentInfo(
provider.CLIOptions,
) (string, *provider.AgentWorkspaceInfo, error) {
c.infoCalls++
return "", c.info, c.infoErr
}

func newLogsEnvironmentClient(driver string) *logsEnvironmentClient {
return &logsEnvironmentClient{
local: true,
info: &provider.AgentWorkspaceInfo{
Agent: provider.ProviderAgentConfig{Driver: driver, Local: config.BoolTrue},
Options: map[string]config.OptionValue{
"MSB_PATH": {Value: "configured runtime"},
"MSB_LIBKRUNFW_PATH": {Value: "configured firmware"},
},
Workspace: &provider.Workspace{
ID: "logs-environment", Context: config.DefaultContext,
Provider: provider.WorkspaceProviderConfig{
Name: "logs-external",
Options: map[string]config.OptionValue{"MSB_HOME": {Value: "workspace home"}},
},
},
},
}
}
31 changes: 23 additions & 8 deletions cmd/workspace/up/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func (cmd *UpCmd) devsyUpMachineSSH(
if lifecycleOption != nil {
options = append(options, lifecycleOption)
}
var environment map[string]string
if wInfo.Agent.Driver == provider2.ExternalDriver && wInfo.Agent.Local == config.BoolTrue {
environment = provider2.ToOptions(wInfo.Workspace, wInfo.Machine, wInfo.Options)
}
return sshtunnel.ExecuteCommand(ctx, sshtunnel.ExecuteCommandOptions{
Client: client,
AddPrivateKeys: devsyConfig.ContextOption(
Expand All @@ -280,6 +284,7 @@ func (cmd *UpCmd) devsyUpMachineSSH(
AgentInject: newAgentInjectFunc(client, wInfo),
SSHCommand: sshTunnelCmd,
Command: agentCommand,
EnvVars: environment,
TunnelServerFunc: func(ctx context.Context, stdin io.WriteCloser, stdout io.Reader) (*config2.Result, error) {
return tunnelserver.RunUpServer(ctx, tunnelserver.UpSpec{
Reader: stdout,
Expand Down Expand Up @@ -318,6 +323,15 @@ func newAgentInjectFunc(
client client2.WorkspaceClient,
wInfo *provider2.AgentWorkspaceInfo,
) sshtunnel.AgentInjectFunc {
var localEnvironment []string
if wInfo.Agent.Driver == provider2.ExternalDriver && wInfo.Agent.Local == config.BoolTrue {
localEnvironment = provider2.ToEnvironment(
wInfo.Workspace,
wInfo.Machine,
wInfo.Options,
nil,
)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return func(
cancelCtx context.Context,
sshCmd string,
Expand All @@ -334,14 +348,15 @@ func newAgentInjectFunc(
RawStdout: true,
})
},
IsLocal: client.AgentLocal(),
RemoteAgentPath: client.AgentPath(),
DownloadURL: client.AgentURL(),
Command: sshCmd,
Stdin: sshTunnelStdinReader,
Stdout: sshTunnelStdoutWriter,
Stderr: writer,
Timeout: wInfo.InjectTimeout,
LocalEnvironment: localEnvironment,
IsLocal: client.AgentLocal(),
RemoteAgentPath: client.AgentPath(),
DownloadURL: client.AgentURL(),
Command: sshCmd,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Stdin: sshTunnelStdinReader,
Stdout: sshTunnelStdoutWriter,
Stderr: writer,
Timeout: wInfo.InjectTimeout,
})
}
}
99 changes: 99 additions & 0 deletions cmd/workspace/up/external_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package up

import (
"bytes"
"os"
"runtime"
"testing"

"github.com/devsy-org/devsy/pkg/agent"
"github.com/devsy-org/devsy/pkg/client"
"github.com/devsy-org/devsy/pkg/client/clientimplementation"
"github.com/devsy-org/devsy/pkg/config"
"github.com/devsy-org/devsy/pkg/hash"
"github.com/devsy-org/devsy/pkg/provider"
"github.com/stretchr/testify/suite"
)

type externalEnvironmentSuite struct{ suite.Suite }

func TestExternalEnvironment(t *testing.T) { suite.Run(t, new(externalEnvironmentSuite)) }

func (s *externalEnvironmentSuite) TestLocalInjection() {
s.T().Setenv(config.EnvHome, s.T().TempDir())
s.T().Setenv("MSB_PATH", "ambient-runtime")
for _, disabled := range []bool{false, true} {
name := "daemon"
if disabled {
name = "disabled"
}
s.Run(name, func() {
client := s.newClient()
compressed, info, err := client.AgentInfo(provider.CLIOptions{DisableDaemon: disabled})
s.Require().NoError(err)
payload, _, err := agent.DecodeWorkspaceInfo(compressed)
s.Require().NoError(err)
s.Equal(!disabled, len(payload.Options) > 0)
s.Equal(!disabled, len(payload.Workspace.Provider.Options) > 0)
var stdout bytes.Buffer
err = newAgentInjectFunc(
client,
info,
)(
s.T().Context(),
`printf '%s|%s|%s' "$MSB_PATH" "$MSB_LIBKRUNFW_PATH" "$MSB_HOME"`,
nil,
&stdout,
nil,
)
s.Require().NoError(err)
s.Equal("configured runtime|configured firmware|workspace home", stdout.String())
s.Equal("ambient-runtime", os.Getenv("MSB_PATH"))
})
}
}

const environmentProvider = "external-test"

func (s *externalEnvironmentSuite) newClient() client.WorkspaceClient {
cfg := &config.Config{
DefaultContext: config.DefaultContext,
Contexts: map[string]*config.ContextConfig{
config.DefaultContext: {
Providers: map[string]*config.ProviderConfig{
environmentProvider: {
Options: map[string]config.OptionValue{
"MSB_PATH": {Value: "configured runtime"},
"MSB_LIBKRUNFW_PATH": {Value: "configured firmware"},
},
},
},
},
},
}
workspace := &provider.Workspace{
ID: "environment",
Context: config.DefaultContext,
Provider: provider.WorkspaceProviderConfig{
Name: environmentProvider,
Options: map[string]config.OptionValue{"MSB_HOME": {Value: "workspace home"}},
},
}
providerConfig := &provider.ProviderConfig{Name: environmentProvider}
providerConfig.Agent.Driver = provider.ExternalDriver
providerConfig.Agent.Local = config.BoolTrue
providerConfig.Agent.External.Binary = "RUNTIME"
providerConfig.Agent.Binaries = map[string][]*provider.ProviderBinary{
"RUNTIME": {
{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Path: "https://example.invalid/runtime",
Checksum: hash.String("runtime"),
},
},
}
client, err := clientimplementation.NewWorkspaceClient(cfg, providerConfig, workspace, nil)
s.Require().NoError(err)
return client
}
3 changes: 2 additions & 1 deletion pkg/agent/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (

// InjectOptions defines the parameters for injecting the Devsy agent into a remote environment.
type InjectOptions struct {
LocalEnvironment []string
// Exec is the function used to execute commands on the remote machine. Required.
// nolint:staticcheck
Exec inject.ExecFunc
Expand Down Expand Up @@ -271,7 +272,7 @@ func injectLocally(ctx context.Context, opts *InjectOptions) error {
Stdin: opts.Stdin,
Stdout: opts.Stdout,
Stderr: opts.Stderr,
Environ: nil,
Environ: opts.LocalEnvironment,
})
}

Expand Down
Loading
Loading