Repository navigation
Expand file tree
/
Copy pathcodex_config.go
More file actions
349 lines (336 loc) · 13.3 KB
/
Copy pathcodex_config.go
File metadata and controls
349 lines (336 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package workflow
import (
"encoding/json"
"errors"
"fmt"
"math"
"os"
"path"
"strings"
"time"
"github.com/github/gh-aw/pkg/console"
"github.com/github/gh-aw/pkg/constants"
"github.com/github/gh-aw/pkg/sliceutil"
"github.com/pelletier/go-toml/v2"
)
type codexNativeConfig struct {
Defaults map[string]any `json:"defaults"`
Overrides map[string]any `json:"overrides"`
DisablePlugins bool `json:"disablePlugins"`
}
func parseCodexConfig(config string) (map[string]any, error) {
result := make(map[string]any)
if strings.TrimSpace(config) == "" {
return result, nil
}
if strings.Contains(config, "${{") {
return nil, errors.New("engine.config: GitHub Actions expressions are not supported in Codex TOML; set the value in engine.env and reference it as ${ENV_VAR} instead")
}
if err := toml.Unmarshal([]byte(config), &result); err != nil {
return nil, fmt.Errorf("engine.config: invalid Codex TOML configuration: %w", err)
}
if err := validateCodexConfigValues(result); err != nil {
return nil, err
}
if policy, ok := result["shell_environment_policy"].(map[string]any); ok {
if _, canonical := policy["filters"]; canonical {
for _, legacy := range []string{"exclude", "include_only"} {
if _, exists := policy[legacy]; exists {
return nil, fmt.Errorf("engine.config: shell_environment_policy mixes filters with %s; use either a filters table or legacy exclude/include_only arrays, not both", legacy)
}
}
}
}
return result, nil
}
func validateCodexConfigValues(value any) error {
switch typed := value.(type) {
case map[string]any:
for _, entry := range typed {
if err := validateCodexConfigValues(entry); err != nil {
return err
}
}
case []any:
for _, entry := range typed {
if err := validateCodexConfigValues(entry); err != nil {
return err
}
}
case string, bool:
case float64:
if math.IsNaN(typed) || math.IsInf(typed, 0) {
return errors.New("engine.config: Codex numeric values must be finite")
}
case int64:
if typed < -9007199254740991 || typed > 9007199254740991 {
return errors.New("engine.config: Codex integer values must be within the runtime's safe integer range")
}
default:
return fmt.Errorf("engine.config: unsupported Codex TOML value type %T; use strings, booleans, numbers, arrays, or tables", value)
}
return nil
}
func (e *CodexEngine) buildNativeConfig(workflowData *WorkflowData, mcpTools []string) (*codexNativeConfig, error) {
config := &codexNativeConfig{
Defaults: map[string]any{
"history": map[string]any{"persistence": "none"},
"otel": map[string]any{"metrics_exporter": "none"},
"shell_environment_policy": map[string]any{
"inherit": "all",
"ignore_default_excludes": false,
"include_only": codexShellEnvironmentVars(workflowData),
},
},
Overrides: make(map[string]any),
DisablePlugins: workflowData == nil || len(workflowData.Plugins) == 0,
}
features := map[string]any{"plugins": !config.DisablePlugins}
if e.ResolveLLMProvider(workflowData) == LLMProviderGitHub {
features["shell_tool"] = false
features["code_mode"] = false
features["code_mode_only"] = false
config.Defaults["model_catalog_json"] = path.Join(codexHome(workflowData), "models.json")
}
config.Defaults["features"] = features
if isFirewallEnabled(workflowData) {
config.Defaults["model_provider"] = codexOpenAIProxyProviderID
config.Defaults["model_providers"] = map[string]any{
codexOpenAIProxyProviderID: map[string]any{
"name": codexOpenAIProxyProviderName,
"base_url": e.getOpenAIProxyProviderBaseURL(workflowData),
"env_key": "CODEX_API_KEY",
"wire_api": "responses",
"requires_openai_auth": false,
"supports_websockets": false,
},
}
}
config.Defaults["mcp_servers"] = codexNativeServerDefaults(workflowData, mcpTools)
if workflowData != nil && workflowData.EngineConfig != nil {
var err error
config.Overrides, err = parseCodexConfig(workflowData.EngineConfig.Config)
if err != nil {
return nil, err
}
if workflowData.IsDetectionRun {
// Detection inherits inference settings, not the agent's MCP tool configuration.
if _, exists := config.Overrides["mcp_servers"]; exists {
codexMCPLog.Print("Skipping inherited agent MCP settings for Codex detection")
delete(config.Overrides, "mcp_servers")
}
}
if err := validateCodexManagedConfig(config, isFirewallEnabled(workflowData), e.ResolveLLMProvider(workflowData) == LLMProviderGitHub); err != nil {
return nil, err
}
}
return config, nil
}
func codexNativeServerDefaults(workflowData *WorkflowData, mcpTools []string) map[string]any {
servers := make(map[string]any)
for _, name := range mcpTools {
switch name {
case "safe-outputs":
name = constants.SafeOutputsMCPServerID.String()
case "agentic-workflows":
name = constants.AgenticWorkflowsMCPServerID.String()
case "mcp-scripts":
name = constants.MCPScriptsMCPServerID.String()
case "cache-memory":
continue
}
server := map[string]any{
"startup_timeout_sec": int(constants.DefaultMCPStartupTimeout / time.Second),
"tool_timeout_sec": int(constants.DefaultToolTimeout / time.Second),
}
if workflowData != nil {
if timeout := templatableIntValue(&workflowData.ToolsStartupTimeout); timeout > 0 {
server["startup_timeout_sec"] = timeout
}
if timeout := templatableIntValue(&workflowData.ToolsTimeout); timeout > 0 {
server["tool_timeout_sec"] = timeout
}
}
if name == "github" && workflowData != nil {
userAgent := "github-agentic-workflow"
if workflowData.Name != "" {
userAgent = SanitizeArtifactIdentifier(workflowData.Name)
}
if workflowData.EngineConfig != nil && workflowData.EngineConfig.UserAgent != "" {
userAgent = workflowData.EngineConfig.UserAgent
}
server["http_headers"] = map[string]any{"User-Agent": userAgent}
}
servers[name] = server
}
return servers
}
func validateCodexManagedConfig(config *codexNativeConfig, firewallEnabled, githubProvider bool) error {
if githubProvider {
if _, exists := config.Overrides["model_catalog_json"]; exists {
return errors.New("engine.config: model_catalog_json is managed for GitHub inference to disable the unsupported Codex exec custom tool")
}
if features, ok := config.Overrides["features"].(map[string]any); ok {
for _, name := range []string{"shell_tool", "code_mode", "code_mode_only"} {
if value, exists := features[name]; exists && value != false {
return fmt.Errorf("engine.config: features.%s is disabled for GitHub inference because the Copilot compatibility adapter does not support the Codex exec custom tool", name)
}
}
}
}
if rawServers, exists := config.Overrides["mcp_servers"]; exists {
servers, ok := rawServers.(map[string]any)
if !ok {
return errors.New("engine.config: mcp_servers must be a TOML table")
}
declared, ok := config.Defaults["mcp_servers"].(map[string]any)
if !ok {
return errors.New("invalid compiler-generated Codex MCP defaults: expected a table")
}
for name, raw := range servers {
if _, exists := declared[name]; !exists {
return fmt.Errorf("engine.config: MCP server %q is not declared; configure servers through mcp-servers so the MCP gateway can enforce their policies", name)
}
options, ok := raw.(map[string]any)
if !ok {
return fmt.Errorf("engine.config: mcp_servers.%s must be a TOML table", name)
}
for _, key := range []string{"url", "command", "args", "env", "env_vars", "bearer_token_env_var"} {
if _, exists := options[key]; exists {
return fmt.Errorf("engine.config: mcp_servers.%s.%s is managed by the MCP gateway; configure connection settings through mcp-servers instead", name, key)
}
for _, key := range []string{"http_headers", "env_http_headers"} {
if headers, ok := options[key].(map[string]any); ok {
for name := range headers {
if strings.EqualFold(name, "Authorization") {
return errors.New("engine.config: MCP gateway authorization is managed; configure upstream credentials through mcp-servers instead")
}
}
}
}
}
}
}
if firewallEnabled {
if provider, exists := config.Overrides["model_provider"]; exists && provider != codexOpenAIProxyProviderID {
return errors.New("engine.config: model_provider is managed by AWF; configure inference with engine.model-provider or engine.env.OPENAI_BASE_URL instead")
}
if providers, ok := config.Overrides["model_providers"].(map[string]any); ok {
if proxy, ok := providers[codexOpenAIProxyProviderID].(map[string]any); ok {
for _, key := range []string{"base_url", "env_key", "wire_api", "requires_openai_auth", "supports_websockets"} {
if _, exists := proxy[key]; exists {
return fmt.Errorf("engine.config: model_providers.%s.%s is managed by AWF; configure the upstream endpoint through engine.env.OPENAI_BASE_URL instead", codexOpenAIProxyProviderID, key)
}
}
}
}
}
return nil
}
func codexHome(workflowData *WorkflowData) string {
if home := getEngineEnvOverrides(workflowData)["CODEX_HOME"]; home != "" {
return home
}
return constants.TmpMcpConfigDir
}
func codexConfigEnv(workflowData *WorkflowData) map[string]string {
result := make(map[string]string)
if workflowData == nil || workflowData.EngineConfig == nil {
return result
}
for name, value := range getEngineEnvOverrides(workflowData) {
if strings.Contains(workflowData.EngineConfig.Config, "${"+name+"}") {
result[name] = value
}
}
return result
}
func codexShellEnvironmentVars(workflowData *WorkflowData) []string {
names := map[string]struct{}{}
for _, name := range []string{
"PATH", "HOME", "SHELL", "TMPDIR", "TMP", "TEMP", "LANG", "LC_ALL", "LC_CTYPE", "USER", "LOGNAME",
"GITHUB_WORKSPACE", "RUNNER_TEMP", "RUNNER_TOOL_CACHE", "PLAYWRIGHT_BROWSERS_PATH",
"GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL",
"HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "all_proxy", "no_proxy",
"SSL_CERT_FILE", "SSL_CERT_DIR", "CURL_CA_BUNDLE", "REQUESTS_CA_BUNDLE", "NODE_EXTRA_CA_CERTS",
} {
names[name] = struct{}{}
}
if workflowData != nil {
env := getEngineEnvOverrides(workflowData)
for name, value := range env {
if !strings.Contains(value, "${{ secrets.") && !ContainsJobOutputExpr(value) {
names[name] = struct{}{}
} else {
delete(names, name)
}
}
for _, name := range workflowData.ExcludedEnv {
delete(names, name)
}
}
return sliceutil.SortedKeys(names)
}
func (e *CodexEngine) renderConfigurationStep(workflowData *WorkflowData, mcpTools []string, configOnly bool) (GitHubActionStep, error) {
config, err := e.buildNativeConfig(workflowData, mcpTools)
if err != nil {
return nil, err
}
payload, err := json.Marshal(config)
if err != nil {
return nil, fmt.Errorf("failed to serialize Codex configuration: %w", err)
}
env := codexConfigEnv(workflowData)
env["CODEX_HOME"] = codexHome(workflowData)
env["GH_AW_CODEX_CONFIG_JSON"] = string(payload)
command := `"$(command -v node)" "${RUNNER_TEMP}/gh-aw/actions/convert_gateway_config_codex.cjs" --bootstrap`
if configOnly {
command += "\n" + `"$(command -v node)" "${RUNNER_TEMP}/gh-aw/actions/convert_gateway_config_codex.cjs" --config-only`
}
if e.ResolveLLMProvider(workflowData) == LLMProviderGitHub {
// Model metadata can force code mode even when its feature flags are off.
command = "set -o pipefail\n" + e.codexCommandName(workflowData) + ` debug models --bundled | "$(command -v node)" "${RUNNER_TEMP}/gh-aw/actions/convert_gateway_config_codex.cjs" --direct-tools` + "\n" + command
}
return FormatStepWithCommandAndEnv([]string{" - name: Configure Codex"}, command, env), nil
}
func (c *Compiler) validateCodexCompatibility(workflowData *WorkflowData) error {
if workflowData == nil || workflowData.EngineConfig == nil {
return nil
}
config := workflowData.EngineConfig
engineID := workflowData.AI
if engineID == "" {
engineID = config.ID
}
engine, err := c.getAgenticEngine(engineID)
if err != nil {
return err
}
if _, ok := engine.(*CodexEngine); !ok {
return nil
}
if _, err := parseCodexConfig(config.Config); err != nil {
return err
}
if config.Command != "" {
return nil
}
provider := NewCodexEngine().ResolveLLMProvider(workflowData)
if provider != LLMProviderOpenAI && provider != LLMProviderGitHub {
return NewValidationError("engine.model-provider", string(provider),
"Codex requires an OpenAI Responses-compatible provider",
"Use openai or github, or configure a Responses-compatible bridge with engine.env.OPENAI_BASE_URL.\n\nExample:\nengine:\n id: codex\n model-provider: openai")
}
if provider == LLMProviderGitHub && !isFirewallEnabled(workflowData) {
return NewValidationError("sandbox.agent", "false",
"Codex with the GitHub provider requires the AWF sandbox",
"Remove sandbox.agent: false to enable authenticated GitHub inference.\n\nExample:\nengine:\n id: codex\n model: copilot/gpt-5.3-codex")
}
if workflowData.Tools["web-fetch"] == false {
if search, enabled := workflowData.Tools["web-search"]; enabled && search != false {
fmt.Fprintln(os.Stderr, console.FormatWarningMessage("Codex web-search includes page fetching; tools.web-fetch: false cannot disable browsing independently. Use network.hosted-web to restrict hosted retrieval."))
c.IncrementWarningCount()
}
}
return nil
}