-
Notifications
You must be signed in to change notification settings - Fork 125
Add dmr sandbox config and launch command #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CoderHariswar
wants to merge
8
commits into
docker:main
Choose a base branch
from
CoderHariswar:fix-dmr-sandbox-config-launch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2be8943
Add dmr sandbox config and launch command
CoderHariswar 934299f
Potential fix for pull request finding
CoderHariswar e1e3fb1
Add dmr sandbox config and launch command
CoderHariswar 064d96b
Move sandbox config and launch to shared CLI commands
CoderHariswar 93c3726
Use static sandbox command for sbx launch
CoderHariswar 6334d03
Update CLI reference docs
CoderHariswar de714be
Refine persistent config command
CoderHariswar 03c6e25
Remove unrelated launch spacing change
CoderHariswar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,23 @@ Examples: | |
| appArgs = args[dashIdx:] | ||
| } | ||
|
|
||
| // If a sandbox tool is configured, launch host apps through it before | ||
| // resolving runner endpoints. This keeps sandbox launch independent of | ||
| // whether the host app binary itself is installed. | ||
| if _, ok := hostApps[app]; ok { | ||
| sandboxTool, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if sandboxTool != "" { | ||
| if err := validateSandboxTool(sandboxTool); err != nil { | ||
| return err | ||
| } | ||
| return launchSandboxedHostApp(cmd, sandboxTool, app, appArgs, dryRun) | ||
| } | ||
| } | ||
|
|
||
| runner, err := getStandaloneRunner(cmd.Context()) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to determine standalone runner endpoint: %w", err) | ||
|
|
@@ -155,9 +172,11 @@ Examples: | |
| if ca, ok := containerApps[app]; ok { | ||
| return launchContainerApp(cmd, ca, ep.container, image, port, detach, appArgs, dryRun) | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can remove these line space changes which i think is not part of changes |
||
| if cli, ok := hostApps[app]; ok { | ||
| return launchHostApp(cmd, app, ep.host, cli, model, runner, appArgs, dryRun) | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra change |
||
| return fmt.Errorf("unsupported app %q (supported: %s)", app, strings.Join(supportedApps, ", ")) | ||
| }, | ||
| } | ||
|
|
@@ -170,6 +189,31 @@ Examples: | |
| return c | ||
| } | ||
|
|
||
| func launchSandboxedHostApp(cmd *cobra.Command, sandboxTool, app string, appArgs []string, dryRun bool) error { | ||
| if err := validateSandboxTool(sandboxTool); err != nil { | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
| return err | ||
| } | ||
|
|
||
| args := append([]string{app}, appArgs...) | ||
|
|
||
| switch sandboxTool { | ||
| case "sbx": | ||
| if dryRun { | ||
| cmd.Printf("sbx %s\n", strings.Join(args, " ")) | ||
| return nil | ||
| } | ||
|
|
||
| launchCmd := exec.Command("sbx", args...) | ||
| launchCmd.Stdin = os.Stdin | ||
| launchCmd.Stdout = os.Stdout | ||
| launchCmd.Stderr = os.Stderr | ||
|
|
||
| return launchCmd.Run() | ||
| default: | ||
| return fmt.Errorf("unsupported sandbox tool %q", sandboxTool) | ||
| } | ||
| } | ||
|
|
||
| // listSupportedApps prints all supported apps with their descriptions and install status. | ||
| func listSupportedApps(cmd *cobra.Command) error { | ||
| cmd.Println("Supported apps:") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var allowedSandboxTools = map[string]struct{}{ | ||
| "sbx": {}, | ||
| } | ||
|
|
||
| func newSandboxConfigCmd() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "config <key> <value>", | ||
| Short: "Set model runner configuration values", | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(_ *cobra.Command, args []string) error { | ||
| key := args[0] | ||
| value := args[1] | ||
|
|
||
| if key != "sandbox.tool" { | ||
| return fmt.Errorf("unsupported config key %q", key) | ||
| } | ||
|
|
||
| if err := validateSandboxTool(value); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return writeSandboxToolConfig(value) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func validateSandboxTool(tool string) error { | ||
|
CoderHariswar marked this conversation as resolved.
Outdated
|
||
| if _, ok := allowedSandboxTools[tool]; !ok { | ||
| return fmt.Errorf("unsupported sandbox tool %q", tool) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func dmrConfigPath() (string, error) { | ||
| configDir, err := os.UserConfigDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("unable to determine config directory: %w", err) | ||
| } | ||
|
|
||
| return filepath.Join(configDir, "dmr", "config.toml"), nil | ||
| } | ||
|
|
||
| func writeSandboxToolConfig(tool string) error { | ||
| path, err := dmrConfigPath() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { | ||
| return fmt.Errorf("unable to create config directory: %w", err) | ||
| } | ||
|
|
||
| content := fmt.Sprintf("[sandbox]\ntool = %q\n", tool) | ||
|
|
||
| if err := os.WriteFile(path, []byte(content), 0o644); err != nil { | ||
| return fmt.Errorf("unable to write config: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func readSandboxToolConfig() (string, error) { | ||
| path, err := dmrConfigPath() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| file, err := os.Open(path) | ||
| if os.IsNotExist(err) { | ||
| return "", nil | ||
| } | ||
| if err != nil { | ||
| return "", fmt.Errorf("unable to read config: %w", err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| inSandboxSection := false | ||
| scanner := bufio.NewScanner(file) | ||
|
|
||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
|
|
||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
|
|
||
| if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { | ||
| inSandboxSection = line == "[sandbox]" | ||
| continue | ||
| } | ||
|
|
||
| if !inSandboxSection { | ||
| continue | ||
| } | ||
|
|
||
| key, value, ok := strings.Cut(line, "=") | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if strings.TrimSpace(key) != "tool" { | ||
| continue | ||
| } | ||
|
|
||
| return strings.Trim(strings.TrimSpace(value), `"`), nil | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return "", fmt.Errorf("unable to parse config: %w", err) | ||
| } | ||
|
|
||
| return "", nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteAndReadSandboxToolConfig(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| if err := writeSandboxToolConfig("sbx"); err != nil { | ||
| t.Fatalf("writeSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "sbx" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want %q", got, "sbx") | ||
| } | ||
|
|
||
| configPath, err := dmrConfigPath() | ||
| if err != nil { | ||
| t.Fatalf("dmrConfigPath() error = %v", err) | ||
| } | ||
|
|
||
| content, err := os.ReadFile(configPath) | ||
| if err != nil { | ||
| t.Fatalf("ReadFile(%q) error = %v", configPath, err) | ||
| } | ||
|
|
||
| want := "[sandbox]\ntool = \"sbx\"\n" | ||
| if string(content) != want { | ||
| t.Fatalf("config content = %q, want %q", string(content), want) | ||
| } | ||
| } | ||
|
|
||
| func TestReadSandboxToolConfigMissingFile(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want empty string", got) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateSandboxToolAllowsSbx(t *testing.T) { | ||
| if err := validateSandboxTool("sbx"); err != nil { | ||
| t.Fatalf("validateSandboxTool() error = %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateSandboxToolRejectsUnsupportedTool(t *testing.T) { | ||
| err := validateSandboxTool("firejail") | ||
| if err == nil { | ||
| t.Fatal("validateSandboxTool() error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandRejectsUnsupportedKey(t *testing.T) { | ||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"unsupported.key", "sbx"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("config command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandRejectsUnsupportedTool(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"sandbox.tool", "firejail"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("config command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestSandboxConfigCommandWritesConfig(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newSandboxConfigCmd() | ||
| cmd.SetArgs([]string{"sandbox.tool", "sbx"}) | ||
|
|
||
| if err := cmd.Execute(); err != nil { | ||
| t.Fatalf("config command error = %v", err) | ||
| } | ||
|
|
||
| got, err := readSandboxToolConfig() | ||
| if err != nil { | ||
| t.Fatalf("readSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| if got != "sbx" { | ||
| t.Fatalf("readSandboxToolConfig() = %q, want %q", got, "sbx") | ||
| } | ||
| } | ||
|
|
||
| func TestLaunchCommandRequiresConfiguredSandboxTool(t *testing.T) { | ||
| t.Setenv("XDG_CONFIG_HOME", t.TempDir()) | ||
|
|
||
| cmd := newLaunchCmd() | ||
| cmd.SetArgs([]string{"opencode"}) | ||
|
|
||
| if err := cmd.Execute(); err == nil { | ||
| t.Fatal("launch command error = nil, want error") | ||
| } | ||
| } | ||
|
|
||
| func TestLaunchCommandUsesConfiguredSandboxTool(t *testing.T) { | ||
| configDir := t.TempDir() | ||
| binDir := t.TempDir() | ||
| outputPath := filepath.Join(t.TempDir(), "output.txt") | ||
|
|
||
| t.Setenv("XDG_CONFIG_HOME", configDir) | ||
| t.Setenv("TEST_OUTPUT", outputPath) | ||
|
|
||
| sbxPath := filepath.Join(binDir, "sbx") | ||
| sbxScript := "#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$TEST_OUTPUT\"\n" | ||
|
|
||
| if err := os.WriteFile(sbxPath, []byte(sbxScript), 0o755); err != nil { | ||
| t.Fatalf("WriteFile(%q) error = %v", sbxPath, err) | ||
| } | ||
|
|
||
| oldPath := os.Getenv("PATH") | ||
| t.Setenv("PATH", binDir+string(os.PathListSeparator)+oldPath) | ||
|
|
||
| if err := writeSandboxToolConfig("sbx"); err != nil { | ||
| t.Fatalf("writeSandboxToolConfig() error = %v", err) | ||
| } | ||
|
|
||
| cmd := newLaunchCmd() | ||
| cmd.SetArgs([]string{"opencode", "--", "--help"}) | ||
|
|
||
| if err := cmd.Execute(); err != nil { | ||
| t.Fatalf("launch command error = %v", err) | ||
| } | ||
|
|
||
| content, err := os.ReadFile(outputPath) | ||
| if err != nil { | ||
| t.Fatalf("ReadFile(%q) error = %v", outputPath, err) | ||
| } | ||
|
|
||
| want := "opencode\n--help\n" | ||
| if string(content) != want { | ||
| t.Fatalf("sandbox output = %q, want %q", string(content), want) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.