Skip to content

Commit fe0677c

Browse files
committed
feat(agent): run process starts once per tool call
chatd needs to start at most one process per tool call and to stop that process on a user interrupt, across task retries and ownership changes. The start route now runs through the tool call store's middleware. With a tool call in the request context, the process ID is the tool call UUID, so chatd can address the process without the start response. The start registers a cancel hook that kills the process (the process group on Unix) and marks it canceled only when the kill succeeded; its channel closes when the process exits. A start whose body cannot be decoded abandons its run, so a retried request runs. timeout_ms stores the execute deadline at the first start. A replay returns the recorded response, so it never moves the deadline. The output wait ends at exit, at the deadline, or at the 5-minute cap, and the answer reports timed_out, canceled, and duration_ms. An exited process started by a tool call is reaped once the store no longer retains its record; other processes keep the 5-minute rule. A one-minute ticker on the agent clock sweeps the store and reaps, so reaping no longer depends on process_list. Close stops it. The agent holds one store per instance and mounts the cancel route at POST /api/v0/tool-calls/{id}/cancel.
1 parent 0e8c849 commit fe0677c

5 files changed

Lines changed: 788 additions & 61 deletions

File tree

‎agent/agent.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/coder/coder/v2/agent/agentscripts"
4949
"github.com/coder/coder/v2/agent/agentsocket"
5050
"github.com/coder/coder/v2/agent/agentssh"
51+
"github.com/coder/coder/v2/agent/agenttoolcall"
5152
"github.com/coder/coder/v2/agent/boundarylogproxy"
5253
"github.com/coder/coder/v2/agent/proto"
5354
"github.com/coder/coder/v2/agent/proto/resourcesmonitor"
@@ -344,6 +345,7 @@ type agent struct {
344345
filesAPI *agentfiles.API
345346
gitAPI *agentgit.API
346347
processAPI *agentproc.API
348+
toolCallStore *agenttoolcall.Store
347349
desktopAPI *agentdesktop.API
348350
mcpManager *agentmcp.Manager
349351
mcpAPI *agentmcp.API
@@ -473,7 +475,8 @@ func (a *agent) init() {
473475
}
474476
return ""
475477
}
476-
a.processAPI = agentproc.NewAPI(a.logger.Named("processes"), a.execer, a.filesystem, pathStore, a.envInfo, a.updateCommandEnv, workingDirFn)
478+
a.toolCallStore = agenttoolcall.NewStore(a.clock)
479+
a.processAPI = agentproc.NewAPI(a.logger.Named("processes"), a.execer, a.filesystem, pathStore, a.envInfo, a.updateCommandEnv, workingDirFn, agentproc.WithClock(a.clock), agentproc.WithToolCallStore(a.toolCallStore))
477480
gitOpts := append([]agentgit.Option{agentgit.WithClock(a.clock)}, a.gitAPIOptions...)
478481
a.gitAPI = agentgit.NewAPI(a.logger.Named("git"), pathStore, gitOpts...)
479482
desktop := agentdesktop.NewPortableDesktop(

‎agent/agentproc/api.go‎

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package agentproc
22

33
import (
4-
"context"
54
"encoding/json"
65
"errors"
76
"fmt"
@@ -17,10 +16,12 @@ import (
1716
"github.com/coder/coder/v2/agent/agentchat"
1817
"github.com/coder/coder/v2/agent/agentexec"
1918
"github.com/coder/coder/v2/agent/agentgit"
19+
"github.com/coder/coder/v2/agent/agenttoolcall"
2020
"github.com/coder/coder/v2/agent/usershell"
2121
"github.com/coder/coder/v2/coderd/httpapi"
2222
"github.com/coder/coder/v2/codersdk"
2323
"github.com/coder/coder/v2/codersdk/workspacesdk"
24+
"github.com/coder/quartz"
2425
)
2526

2627
const (
@@ -35,14 +36,48 @@ type API struct {
3536
logger slog.Logger
3637
manager *manager
3738
pathStore *agentgit.PathStore
39+
// toolCallStore runs the start route once per tool call. Nil
40+
// leaves tool call headers unhandled.
41+
toolCallStore *agenttoolcall.Store
42+
}
43+
44+
// Option configures an API.
45+
type Option func(*apiOptions)
46+
47+
type apiOptions struct {
48+
clock quartz.Clock
49+
toolCallStore *agenttoolcall.Store
50+
}
51+
52+
// WithClock sets the clock for process times, execute deadlines,
53+
// output waits, and the periodic sweep.
54+
func WithClock(clock quartz.Clock) Option {
55+
return func(o *apiOptions) {
56+
o.clock = clock
57+
}
58+
}
59+
60+
// WithToolCallStore runs the start route through store.Middleware, so
61+
// a start with tool call headers runs once and its process ID is the
62+
// tool call UUID. An exited tool call process is kept while the store
63+
// retains its record.
64+
func WithToolCallStore(store *agenttoolcall.Store) Option {
65+
return func(o *apiOptions) {
66+
o.toolCallStore = store
67+
}
3868
}
3969

4070
// NewAPI creates a new process API handler.
41-
func NewAPI(logger slog.Logger, execer agentexec.Execer, fs afero.Fs, pathStore *agentgit.PathStore, envInfo usershell.EnvInfoer, updateEnv func(current []string) (updated []string, err error), workingDir func() string) *API {
71+
func NewAPI(logger slog.Logger, execer agentexec.Execer, fs afero.Fs, pathStore *agentgit.PathStore, envInfo usershell.EnvInfoer, updateEnv func(current []string) (updated []string, err error), workingDir func() string, opts ...Option) *API {
72+
options := apiOptions{clock: quartz.NewReal()}
73+
for _, opt := range opts {
74+
opt(&options)
75+
}
4276
return &API{
43-
logger: logger,
44-
manager: newManager(logger, execer, fs, envInfo, updateEnv, workingDir),
45-
pathStore: pathStore,
77+
logger: logger,
78+
manager: newManager(logger, execer, fs, envInfo, updateEnv, workingDir, options.clock, options.toolCallStore),
79+
pathStore: pathStore,
80+
toolCallStore: options.toolCallStore,
4681
}
4782
}
4883

@@ -55,7 +90,11 @@ func (api *API) Close() error {
5590
// Routes returns the HTTP handler for process-related routes.
5691
func (api *API) Routes() http.Handler {
5792
r := chi.NewRouter()
58-
r.Post("/start", api.handleStartProcess)
93+
if api.toolCallStore != nil {
94+
r.With(api.toolCallStore.Middleware).Post("/start", api.handleStartProcess)
95+
} else {
96+
r.Post("/start", api.handleStartProcess)
97+
}
5998
r.Get("/list", api.handleListProcesses)
6099
r.Get("/{id}/output", api.handleProcessOutput)
61100
r.Post("/{id}/signal", api.handleSignalProcess)
@@ -65,9 +104,14 @@ func (api *API) Routes() http.Handler {
65104
// handleStartProcess starts a new process.
66105
func (api *API) handleStartProcess(rw http.ResponseWriter, r *http.Request) {
67106
ctx := r.Context()
107+
toolCall, isToolCall := agenttoolcall.FromContext(ctx)
68108

69109
var req workspacesdk.StartProcessRequest
70110
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
111+
// Nothing ran, so a repeated request may run the tool call.
112+
if isToolCall {
113+
toolCall.Abandon()
114+
}
71115
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
72116
Message: "Request body must be valid JSON.",
73117
Detail: err.Error(),
@@ -81,13 +125,19 @@ func (api *API) handleStartProcess(rw http.ResponseWriter, r *http.Request) {
81125
})
82126
return
83127
}
128+
if req.TimeoutMs < 0 {
129+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
130+
Message: "Timeout must not be negative.",
131+
})
132+
return
133+
}
84134

85135
var chatID string
86136
if chatContext, ok := agentchat.FromContext(ctx); ok {
87137
chatID = chatContext.ID.String()
88138
}
89139

90-
proc, err := api.manager.start(req, chatID)
140+
proc, err := api.manager.start(req, chatID, toolCall)
91141
if err != nil {
92142
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
93143
Message: "Failed to start process.",
@@ -194,11 +244,9 @@ func (api *API) handleProcessOutput(rw http.ResponseWriter, r *http.Request) {
194244
}
195245

196246
// Cap the wait at maxWaitDuration regardless of
197-
// client-supplied timeout.
198-
waitCtx, waitCancel := context.WithTimeout(ctx, maxWaitDuration)
199-
defer waitCancel()
200-
201-
_ = proc.waitForOutput(waitCtx)
247+
// client-supplied timeout, and end it at the execute
248+
// deadline.
249+
api.manager.waitForExit(ctx, proc, maxWaitDuration)
202250
// Fall through to read snapshot below.
203251
}
204252

@@ -209,13 +257,17 @@ func (api *API) handleProcessOutput(rw http.ResponseWriter, r *http.Request) {
209257
// the final buffer state.
210258
info := proc.info()
211259
output, truncated := proc.output()
260+
timedOut, canceled, duration := proc.runState(info, api.manager.clock.Now())
212261

213262
httpapi.Write(ctx, rw, http.StatusOK, workspacesdk.ProcessOutputResponse{
214-
Output: output,
215-
Truncated: truncated,
216-
Running: info.Running,
217-
ExitCode: info.ExitCode,
218-
Command: info.Command,
263+
Output: output,
264+
Truncated: truncated,
265+
Running: info.Running,
266+
ExitCode: info.ExitCode,
267+
Command: info.Command,
268+
TimedOut: timedOut,
269+
Canceled: canceled,
270+
DurationMs: duration.Milliseconds(),
219271
})
220272
}
221273

0 commit comments

Comments
 (0)