feat: harden workspace file APIs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
tao.chen
2026-07-02 17:22:26 +08:00
co-authored by Claude Fable 5
parent c1b8982e3b
commit 04b309d3fc
18 changed files with 1262 additions and 76 deletions
+18
View File
@@ -14,6 +14,7 @@ type Config struct {
Server ServerConfig `yaml:"server"`
Workspace WorkspaceConfig `yaml:"workspace"`
Process ProcessConfig `yaml:"process"`
File FileConfig `yaml:"file"`
Log LogConfig `yaml:"log"`
}
@@ -36,6 +37,11 @@ type ProcessConfig struct {
OpenCodeCommand string `yaml:"opencodeCommand"`
}
// FileConfig holds file operation limits.
type FileConfig struct {
MaxWriteBytes int64 `yaml:"maxWriteBytes"`
}
// LogConfig holds structured logging settings.
type LogConfig struct {
Level string `yaml:"level"`
@@ -48,6 +54,7 @@ type rawConfig struct {
Server rawServerConfig `yaml:"server"`
Workspace WorkspaceConfig `yaml:"workspace"`
Process ProcessConfig `yaml:"process"`
File FileConfig `yaml:"file"`
Log LogConfig `yaml:"log"`
}
@@ -71,6 +78,7 @@ func Default() Config {
},
Workspace: WorkspaceConfig{Root: "./workspaces"},
Process: ProcessConfig{OpenCodeCommand: "opencode"},
File: FileConfig{MaxWriteBytes: 1 << 20},
Log: LogConfig{Level: "info", Format: "json"},
}
}
@@ -121,6 +129,9 @@ func applyRaw(cfg *Config, raw rawConfig) error {
if raw.Process.OpenCodeCommand != "" {
cfg.Process.OpenCodeCommand = raw.Process.OpenCodeCommand
}
if raw.File.MaxWriteBytes != 0 {
cfg.File.MaxWriteBytes = raw.File.MaxWriteBytes
}
if raw.Log.Level != "" {
cfg.Log.Level = raw.Log.Level
}
@@ -174,6 +185,13 @@ func applyEnv(cfg *Config) error {
if v := os.Getenv("CODESPACE_OPENCODE_COMMAND"); v != "" {
cfg.Process.OpenCodeCommand = v
}
if v := os.Getenv("CODESPACE_FILE_MAX_WRITE_BYTES"); v != "" {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("invalid integer for CODESPACE_FILE_MAX_WRITE_BYTES: %w", err)
}
cfg.File.MaxWriteBytes = n
}
if v := os.Getenv("CODESPACE_LOG_LEVEL"); v != "" {
cfg.Log.Level = v
}
+36
View File
@@ -143,3 +143,39 @@ func TestLoadAppliesLogEnvironmentOverrides(t *testing.T) {
t.Fatalf("Log.Format = %q, want json", cfg.Log.Format)
}
}
func TestDefaultIncludesFileConfig(t *testing.T) {
cfg := Default()
if cfg.File.MaxWriteBytes != 1<<20 {
t.Fatalf("File.MaxWriteBytes = %d, want 1048576", cfg.File.MaxWriteBytes)
}
}
func TestLoadParsesFileConfigFromYAML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
data := []byte(`file:
maxWriteBytes: 128
`)
if err := os.WriteFile(path, data, 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.File.MaxWriteBytes != 128 {
t.Fatalf("got %d", cfg.File.MaxWriteBytes)
}
}
func TestLoadAppliesFileEnvironmentOverride(t *testing.T) {
t.Setenv("CODESPACE_FILE_MAX_WRITE_BYTES", "256")
cfg, err := Load(filepath.Join(t.TempDir(), "missing.yaml"))
if err != nil {
t.Fatal(err)
}
if cfg.File.MaxWriteBytes != 256 {
t.Fatalf("got %d", cfg.File.MaxWriteBytes)
}
}