-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodexlaunch.go
More file actions
126 lines (117 loc) · 3 KB
/
Copy pathcodexlaunch.go
File metadata and controls
126 lines (117 loc) · 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
package agenthooks
import (
"crypto/sha256"
"encoding/hex"
"path/filepath"
"strconv"
"strings"
)
type codexLaunchContext struct {
CWD string `json:"cwd"`
Executable string `json:"executable,omitempty"`
Profile string `json:"profile,omitempty"`
Overrides []string `json:"overrides,omitempty"`
Unreplayable bool `json:"unreplayable,omitempty"`
}
func currentCodexLaunchContext(cwd string) (codexLaunchContext, bool) {
args, pid := findAncestorProcess(isCodexProcessArgs)
if args == nil {
return codexLaunchContext{}, false
}
ctx := parseCodexLaunchArgs(args, cwd)
if executable, err := procExecutable(pid); err == nil && executable != "" {
ctx.Executable = executable
}
return ctx, true
}
func isCodexProcessArgs(args []string) bool {
if len(args) == 0 {
return false
}
name := strings.ToLower(strings.TrimSuffix(filepath.Base(args[0]), filepath.Ext(args[0])))
return name == "codex" || strings.HasPrefix(name, "codex-")
}
func parseCodexLaunchArgs(args []string, cwd string) codexLaunchContext {
ctx := codexLaunchContext{CWD: cwd}
if len(args) > 0 {
ctx.Executable = args[0]
args = args[1:]
}
for len(args) > 0 {
arg := args[0]
args = args[1:]
if arg == "--" {
break
}
switch arg {
case "-c", "--config", "--enable", "--disable":
if len(args) > 0 {
ctx.Overrides = append(ctx.Overrides, canonicalCodexFlag(arg), args[0])
args = args[1:]
}
case "-p", "--profile":
if len(args) > 0 {
ctx.Profile = args[0]
args = args[1:]
}
case "--ignore-user-config":
ctx.Unreplayable = true
default:
name, value, ok := strings.Cut(arg, "=")
if ok {
switch name {
case "--config", "--enable", "--disable":
ctx.Overrides = append(ctx.Overrides, name, value)
continue
case "--profile":
ctx.Profile = value
continue
}
}
switch {
case strings.HasPrefix(arg, "-c") && len(arg) > 2:
ctx.Overrides = append(ctx.Overrides, "--config", arg[2:])
case strings.HasPrefix(arg, "-p") && len(arg) > 2:
ctx.Profile = arg[2:]
}
}
}
return ctx
}
func canonicalCodexFlag(flag string) string {
switch flag {
case "-c":
return "--config"
case "-p":
return "--profile"
default:
return flag
}
}
func (c codexLaunchContext) replayArgs() []string {
args := make([]string, 0, len(c.Overrides)+2)
if c.Profile != "" {
args = append(args, "--profile", c.Profile)
}
return append(args, c.Overrides...)
}
func (c codexLaunchContext) hasOverrides() bool {
return c.Profile != "" || len(c.Overrides) > 0 || c.Unreplayable
}
func (c codexLaunchContext) cacheKey() string {
h := sha256.New()
writeHashPart := func(value string) {
_, _ = h.Write([]byte(value))
_, _ = h.Write([]byte{0})
}
writeHashPart("agenthooks-codex-mcp-v1")
writeHashPart(c.CWD)
writeHashPart(c.Executable)
writeHashPart(c.Profile)
writeHashPart(strconv.FormatBool(c.Unreplayable))
for _, arg := range c.Overrides {
writeHashPart(arg)
}
hashLaunchEnvironment(writeHashPart)
return hex.EncodeToString(h.Sum(nil))[:32]
}