executeForeground starts a process and waits for its completion, enforcing the configured timeout.
( ctx context.Context, conn workspacesdk.AgentConn, args ExecuteArgs, optTimeout time.Duration, workDir string, env map[string]string, )
| 179 | // executeForeground starts a process and waits for its |
| 180 | // completion, enforcing the configured timeout. |
| 181 | func executeForeground( |
| 182 | ctx context.Context, |
| 183 | conn workspacesdk.AgentConn, |
| 184 | args ExecuteArgs, |
| 185 | optTimeout time.Duration, |
| 186 | workDir string, |
| 187 | env map[string]string, |
| 188 | ) fantasy.ToolResponse { |
| 189 | timeout := optTimeout |
| 190 | if timeout <= 0 { |
| 191 | timeout = defaultTimeout |
| 192 | } |
| 193 | if args.Timeout != nil { |
| 194 | parsed, err := time.ParseDuration(*args.Timeout) |
| 195 | if err != nil { |
| 196 | return fantasy.NewTextErrorResponse( |
| 197 | fmt.Sprintf("invalid timeout %q: %v", *args.Timeout, err), |
| 198 | ) |
| 199 | } |
| 200 | timeout = parsed |
| 201 | } |
| 202 | |
| 203 | cmdCtx, cancel := context.WithTimeout(ctx, timeout) |
| 204 | defer cancel() |
| 205 | |
| 206 | start := time.Now() |
| 207 | |
| 208 | resp, err := conn.StartProcess(cmdCtx, workspacesdk.StartProcessRequest{ |
| 209 | Command: args.Command, |
| 210 | WorkDir: workDir, |
| 211 | Env: env, |
| 212 | Background: false, |
| 213 | }) |
| 214 | if err != nil { |
| 215 | return errorResult(fmt.Sprintf("start process: %v", err)) |
| 216 | } |
| 217 | |
| 218 | result := waitForProcess(cmdCtx, ctx, conn, resp.ID, timeout) |
| 219 | result.WallDurationMs = time.Since(start).Milliseconds() |
| 220 | |
| 221 | // Add an advisory note for file-dump commands. |
| 222 | if note := detectFileDump(args.Command); note != "" { |
| 223 | result.Note = note |
| 224 | } |
| 225 | |
| 226 | data, err := json.Marshal(result) |
| 227 | if err != nil { |
| 228 | return fantasy.NewTextErrorResponse(err.Error()) |
| 229 | } |
| 230 | return fantasy.NewTextResponse(string(data)) |
| 231 | } |
| 232 | |
| 233 | // truncateOutput safely truncates output to maxOutputToModel, |
| 234 | // ensuring the result is valid UTF-8 even if the cut falls in |
no test coverage detected