Skip to content

fix: allocate new storage for env vars on each tool call #841

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

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
20 changes: 12 additions & 8 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,38 +217,42 @@ func appendInputAsEnv(env []string, input string) []string {
dec := json.NewDecoder(bytes.NewReader([]byte(input)))
dec.UseNumber()

env = appendEnv(env, "GPTSCRIPT_INPUT", input)
// If we don't create a new slice here, then parallel tool calls can end up getting messed up.
newEnv := make([]string, len(env), cap(env)+1+len(data))
copy(newEnv, env)

newEnv = appendEnv(newEnv, "GPTSCRIPT_INPUT", input)

if err := json.Unmarshal([]byte(input), &data); err != nil {
// ignore invalid JSON
return env
return newEnv
}

for k, v := range data {
switch val := v.(type) {
case string:
env = appendEnv(env, k, val)
newEnv = appendEnv(newEnv, k, val)
case json.Number:
env = appendEnv(env, k, string(val))
newEnv = appendEnv(newEnv, k, string(val))
case bool:
env = appendEnv(env, k, fmt.Sprint(val))
newEnv = appendEnv(newEnv, k, fmt.Sprint(val))
default:
data, err := json.Marshal(val)
if err == nil {
env = appendEnv(env, k, string(data))
newEnv = appendEnv(newEnv, k, string(data))
}
}
}

return env
return newEnv
}

func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.Tool, input string, useShell bool) (*exec.Cmd, func(), error) {
if runtime.GOOS == "windows" {
useShell = false
}

envvars := append(e.Env[:], extraEnv...)
envvars := append(e.Env, extraEnv...)
envvars = appendInputAsEnv(envvars, input)
if log.IsDebug() {
envvars = append(envvars, "GPTSCRIPT_DEBUG=true")
Expand Down