on main help sign in
chore(server): acknowledge accepted prompts with HTTP 202

Return as soon as a prompt is accepted instead of keeping the request open for the full agent turn. Workspace shutdown is reported as a conflict so clients can distinguish rejection from accepted work. Co-Authored-By: Charm Crush <crush@charm.land>

commitee94df068af1d5d029fe425dc80d337aed09f263 authorChristian Rocha <christian@rocha.is> dateSun 31 May 2026, 23:10 (5 months ago) parent8b242c5
2 files changed+7−4
internal/server/agent_cancel_test.go+2−2 internal/server/proto.go+5−2
internal/server/agent_cancel_test.go+2−2
@@ -139,7 +139,7 @@ func TestPostAgent_ReturnsOKOnContextCanceled(t *testing.T) {
139139 // The handler returns immediately, before the dispatched run is
140140 // released, because the run no longer owns the HTTP response.
141141 rec := postAgent(t, c, t.Context(), wsID, "S1")
142− require.Equal(t, http.StatusOK, rec.Code, "fire-and-forget SendMessage must return 200 without waiting for the run")
142+ require.Equal(t, http.StatusAccepted, rec.Code, "fire-and-forget SendMessage must return 202 without waiting for the run")
143143
144144 // The run is dispatched on a goroutine; let it return
145145 // context.Canceled. Nothing from that path reaches the (already
@@ -169,7 +169,7 @@ func TestPostAgent_DetachesRequestContext(t *testing.T) {
169169 // The handler returns immediately; the run keeps executing on its
170170 // own goroutine bound to the workspace context.
171171 rec := postAgent(t, c, reqCtx, wsID, "S1")
172− require.Equal(t, http.StatusOK, rec.Code)
172+ require.Equal(t, http.StatusAccepted, rec.Code)
173173
174174 select {
175175 case <-coord.entered:
internal/server/proto.go+5−2
@@ -740,9 +740,10 @@ func (c *controllerV1) handleGetWorkspaceAgent(w http.ResponseWriter, r *http.Re
740740// @Accept json
741741// @Param id path string true "Workspace ID"
742742// @Param request body proto.AgentMessage true "Agent message"
743−// @Success 200
743+// @Success 202
744744// @Failure 400 {object} proto.Error
745745// @Failure 404 {object} proto.Error
746+// @Failure 409 {object} proto.Error
746747// @Failure 500 {object} proto.Error
747748// @Router /workspaces/{id}/agent [post]
748749func (c *controllerV1) handlePostWorkspaceAgent(w http.ResponseWriter, r *http.Request) {
@@ -767,7 +768,7 @@ func (c *controllerV1) handlePostWorkspaceAgent(w http.ResponseWriter, r *http.R
767768 c.handleError(w, r, err)
768769 return
769770 }
770− w.WriteHeader(http.StatusOK)
771+ w.WriteHeader(http.StatusAccepted)
771772}
772773
773774// handlePostWorkspaceAgentInit initializes the agent for a workspace.
@@ -1061,6 +1062,8 @@ func (c *controllerV1) handleError(w http.ResponseWriter, r *http.Request, err e
10611062 status = http.StatusBadRequest
10621063 case errors.Is(err, backend.ErrClientNotAttached):
10631064 status = http.StatusNotFound
1065+ case errors.Is(err, backend.ErrWorkspaceClosing):
1066+ status = http.StatusConflict
10641067 }
10651068 c.server.logError(r, err.Error())
10661069 jsonError(w, status, err.Error())