Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 2 additions & 9 deletions cmd/surla/management_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,14 @@ func TestUnexpectedManagementResponseDoesNotEchoCredential(t *testing.T) {
// TestManagementErrorsUseOnlyCLIText covers declared errors, malformed bodies and
// malformed HTTP itself. The peer deliberately copies the sent credential into each reply.
func TestManagementErrorsUseOnlyCLIText(t *testing.T) {
for _, scenario := range []string{"forbidden", "window_busy", "unprocessable", "unknown name", "invalid JSON", "malformed status", "malformed header"} {
for _, scenario := range []string{"forbidden", "unprocessable", "unknown name", "invalid JSON", "malformed status", "malformed header"} {
t.Run(scenario, func(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState("")
require.NoError(t, err)
key := strings.Repeat("c", 64)
marker := "peer-must-never-print"
status := http.StatusForbidden
if scenario == "window_busy" {
status = http.StatusConflict
}
if scenario == "unprocessable" {
status = http.StatusUnprocessableEntity
}
Expand Down Expand Up @@ -199,11 +196,7 @@ func TestManagementErrorsUseOnlyCLIText(t *testing.T) {
require.NoError(t, state.SaveRunning(project.Running{ManagementCredential: key}))
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
defer cancel()
if scenario == "window_busy" {
_, err = projectClient(state).EnsureTeam(ctx)
} else {
_, err = projectClient(state).Identity(ctx)
}
_, err = projectClient(state).Identity(ctx)
require.Error(t, err)
assert.False(t, strings.Contains(err.Error(), key), "the CLI must never echo the sent credential")
assert.False(t, strings.Contains(err.Error(), marker), "the CLI must never print peer-authored text")
Expand Down
4 changes: 2 additions & 2 deletions cmd/surla/model_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func handOverModelSettings(ctx context.Context, state *project.State, sel models
}
result, ok := raw.(*gensurla.ModelSettingsResult)
if !ok {
return errors.New("project server returned no model settings result; run surla again")
return errors.New("project server returned no model settings result; try again")
}
line := modelSettingsLine(sel, result)
if line == "" {
Expand Down Expand Up @@ -123,5 +123,5 @@ func liveModelSentence(choice gensurla.Connection) string {
if gateway, ok := choice.AsGateway(); ok {
return "Live targets use the model gateway at " + printableTerminalText(gateway.GatewayAddr) + "; change it on the views page."
}
return "Live targets have no model: export a provider key in a shell and run surla there, or choose a model gateway on the views page."
return "Live targets have no model: export a provider key in the shell your coding agent starts from and restart the agent, or in a shell where you run surla, or choose a model gateway on the views page."
}
6 changes: 3 additions & 3 deletions cmd/surla/model_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestModelSettingsLineNamesWhatIsHeld(t *testing.T) {
sel: modelsettings.Selection{Send: map[string]map[string]string{}, NotSent: []modelsettings.Unsent{{Name: "ANTHROPIC_BASE_URL", MissingKey: "ANTHROPIC_API_KEY"}}},
result: &gensurla.ModelSettingsResult{Held: map[string][]string{}, Connection: noChoice()},
want: "No model settings are held by the project server; not sent: ANTHROPIC_BASE_URL (ANTHROPIC_API_KEY is not set). " +
"Live targets have no model: export a provider key in a shell and run surla there, or choose a model gateway on the views page.",
"Live targets have no model: export a provider key in the shell your coding agent starts from and restart the agent, or in a shell where you run surla, or choose a model gateway on the views page.",
},
{
name: "nothing held, nothing unsent, a gateway",
Expand Down Expand Up @@ -180,12 +180,12 @@ func TestHandOverRepliesNeverPrintAValue(t *testing.T) {
output string
}{
{name: "success", replies: []int{http.StatusOK}, output: "Model settings held by the project server: ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL (from this shell)"},
{name: "refused with an unchanged credential", replies: []int{http.StatusUnauthorized}, want: "project server response could not be used (HTTP 401); run surla again"},
{name: "refused with an unchanged credential", replies: []int{http.StatusUnauthorized}, want: "project server response could not be used (HTTP 401); try again"},
{name: "refused, then accepted with the changed credential", replies: []int{http.StatusUnauthorized, http.StatusOK}, rotate: true, output: "(from this shell)"},
{name: "refused twice with a changed credential", replies: []int{http.StatusUnauthorized, http.StatusUnauthorized}, rotate: true, want: "[surla hand_over_model_settings]: invalid response code 401"},
{name: "value too long", replies: []int{http.StatusBadRequest}, want: "the project server refused the model settings from this shell (HTTP 400); each ANTHROPIC_ and OPENAI_ request setting must be at most 4096 characters"},
{name: "server fault", replies: []int{http.StatusInternalServerError}, want: "[surla hand_over_model_settings]: invalid response code 500"},
{name: "connection refused", closed: true, want: "project server connection failed; run surla again"},
{name: "connection refused", closed: true, want: "project server connection failed; try again"},
{name: "deadline", hang: true, want: "project server request did not finish"},
} {
t.Run(tc.name, func(t *testing.T) {
Expand Down
53 changes: 19 additions & 34 deletions cmd/surla/project_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ func projectClient(state *project.State) *genproject.Client {
teamClient := teamHTTPClient()
return genproject.NewClient(
managementEndpoint(state, client, (*genprojectclient.Client).Identity),
teamManagementEndpoint(state, teamClient),
managementEndpoint(state, client, (*genprojectclient.Client).EnsureTeam),
managementEndpoint(state, teamClient, (*genprojectclient.Client).AttachGuide),
managementEndpoint(state, client, (*genprojectclient.Client).Status),
managementEndpoint(state, client, (*genprojectclient.Client).Stop),
Expand All @@ -665,18 +665,6 @@ func projectClient(state *project.State) *genproject.Client {
)
}

// teamManagementEndpoint gives only the team request's deadline its own concrete recovery step.
func teamManagementEndpoint(state *project.State, client *http.Client) goa.Endpoint {
next := managementEndpoint(state, client, (*genprojectclient.Client).EnsureTeam)
return func(ctx context.Context, payload any) (any, error) {
result, err := next(ctx, payload)
if errors.Is(err, context.DeadlineExceeded) {
return nil, fmt.Errorf("project team request did not finish because Herdr did not answer in time; open the Herdr app, then click Start agents on the views page: %w", context.DeadlineExceeded)
}
return result, err
}
}

// managementEndpoint preserves generated transport behavior without printing peer-authored
// error text for one project service operation (see managementCall).
func managementEndpoint(
Expand All @@ -700,7 +688,9 @@ func modelSettingsEndpoint(state *project.State) goa.Endpoint {

// managementCall preserves generated transport behavior without printing peer-authored
// error text. endpoint builds the generated operation on a fresh Doer for each invocation.
// Only declared error names select fixed CLI wording; other replies name status.
// Only declared error names select fixed wording; other replies name status. The CLI
// and surla stdio's guide attachment share these errors, so a retry is worded "try
// again", which is running surla again for one and calling the tool again for the other.
// Response status belongs to this invocation's Doer, including a changed-key retry.
func managementCall(
state *project.State,
Expand Down Expand Up @@ -730,51 +720,46 @@ func managementCall(
}
return nil, goahttp.ErrInvalidResponse(responseErr.Service, responseErr.Method, doer.lastStatus, "")
}
var spaceConflict *genproject.TeamSpaceConflict
if errors.As(err, &spaceConflict) {
return nil, fmt.Errorf("%d Herdr spaces match this project; close duplicate spaces and run surla again (HTTP %d)", spaceConflict.Count, doer.lastStatus)
}
var unavailable *genproject.TeamUnavailable
if errors.As(err, &unavailable) {
return nil, fmt.Errorf("%s (HTTP %d)", teamUnavailableStep(state, unavailable.Cause), doer.lastStatus)
}
var serviceErr *goa.ServiceError
if errors.As(err, &serviceErr) {
switch serviceErr.Name {
case "window_busy":
return nil, fmt.Errorf("a dialog is open in Herdr; close it, then run surla again (HTTP %d)", doer.lastStatus)
case "forbidden":
return nil, fmt.Errorf("project management operation is not permitted (HTTP %d); run surla again", doer.lastStatus)
return nil, fmt.Errorf("project management operation is not permitted (HTTP %d); try again", doer.lastStatus)
case "unprocessable":
return nil, fmt.Errorf("project management request could not be applied (HTTP %d)", doer.lastStatus)
}
}
return nil, fmt.Errorf("project server response could not be used (HTTP %d); run surla again", doer.lastStatus)
return nil, fmt.Errorf("project server response could not be used (HTTP %d); try again", doer.lastStatus)
}
}

// teamUnavailableStep is the CLI's fixed text for one declared team failure cause. It names
// what could not be used and the step that repairs it; the retry is the views page's
// Start agents, which reaches the same team operation, because the page opens regardless.
// teamUnavailableStep is the fixed text for one team failure cause of attach_guide,
// the only management operation that returns one. surla stdio calls it when a
// guide's first Surla tool call attaches the agent, so the text names what could
// not be used, its repair, and the retry: calling the Surla tool again.
func teamUnavailableStep(state *project.State, cause string) string {
const retry = "then click Start agents on the views page"
const retry = "then call the Surla tool again"
switch cause {
case "stopping":
return "the project server is stopping; run surla again"
return "the project server is stopping; call the Surla tool again to start it"
case "chorus":
return "Chorus is not running or its settings are invalid. Run chorus up (or chorus status to see the error), then start the agents again (Start agents on the page, or run surla)."
return "Chorus is not running or its settings are invalid. Run chorus up (or chorus status to see the error), " + retry
case "herdr":
return "Herdr did not complete a team command; open the Herdr app, " + retry
case "saved_space":
return "This project's saved Herdr space has an ID this version of Surla no longer accepts. Close that space in Herdr (its agents stop with it), then start the agents again; Surla will prepare a replacement space."
case "roster_folder":
return "Herdr's launcher roster folder could not be written; make data/chorus-launcher/rosters in Herdr's app folder (~/.config/herdr-app unless HERDR_APP_HOME is set) writable by this account, " + retry
case "project_state":
return fmt.Sprintf("this project's private state in %s could not be read or written; make that folder and the folders in it private to this account (chmod 700) and its files too (chmod 600), %s", state.Dir, retry)
return fmt.Sprintf("a file in this project's private state %s could not be used: its record (project.json), its agents folder or the guide's credential in that folder. If that file lost its permissions or owner, make it this account's and usable only by it (chmod 600 on that file, or chmod 700 on the agents folder); if it is missing or damaged, restore it from a backup; %s", state.Dir, retry)
case "role_folder":
return fmt.Sprintf("a team role's working folder could not be read; make sure this account can open the project folder %s and its build working folders, %s", state.Record.Directory, retry)
}
return "project team could not be prepared; run surla again"
return "project team could not be prepared; call the Surla tool again"
}

// projectHTTPClient keeps management requests off proxies and refuses redirects,
Expand Down Expand Up @@ -833,9 +818,9 @@ func (d *managementDoer) Do(r *http.Request) (*http.Response, error) {
current, readErr := d.state.ReadRunning()
if readErr != nil || current.ManagementCredential == running.ManagementCredential {
if readErr != nil {
d.localErr = fmt.Errorf("project management access was refused; run surla again: %w", readErr)
d.localErr = fmt.Errorf("project management access was refused; try again: %w", readErr)
}
return nil, errors.Join(errors.New("project management access was refused; run surla again"), d.localErr, managementConnectionError(r.Context(), response.Body.Close()))
return nil, errors.Join(errors.New("project management access was refused; try again"), d.localErr, managementConnectionError(r.Context(), response.Body.Close()))
}
if err := response.Body.Close(); err != nil {
return nil, managementConnectionError(r.Context(), err)
Expand Down Expand Up @@ -877,7 +862,7 @@ func managementConnectionError(ctx context.Context, err error) error {
if errors.Is(err, context.Canceled) {
return fmt.Errorf("project server request did not finish: %w", context.Canceled)
}
return errors.New("project server connection failed; run surla again")
return errors.New("project server connection failed; try again")
}

// waitForRunning waits without network traffic while startup has not published access.
Expand All @@ -893,7 +878,7 @@ func waitForRunning(ctx context.Context, state *project.State) (project.Running,
}
select {
case <-ctx.Done():
return project.Running{}, fmt.Errorf("project server has not published management access; run surla again: %w", ctx.Err())
return project.Running{}, fmt.Errorf("project server has not published management access; try again: %w", ctx.Err())
case <-time.After(50 * time.Millisecond):
}
}
Expand Down
44 changes: 19 additions & 25 deletions cmd/surla/project_team_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,23 @@ func TestCommandErrorsNeverIncludeArguments(t *testing.T) {
assert.Equal(t, "no-such-program command failed", err.Error())
}

func TestTeamTimeoutHasRecoveryStep(t *testing.T) {
for _, operation := range []string{"team", "identity"} {
t.Run(operation, func(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState(t.TempDir())
require.NoError(t, err)
release := make(chan struct{})
api := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { <-release }))
defer api.Close()
defer close(release)
state.Record.Port = api.Listener.Addr().(*net.TCPAddr).Port
require.NoError(t, state.SaveRunning(project.Running{ManagementCredential: strings.Repeat("a", 64)}))
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Millisecond)
defer cancel()
if operation == "team" {
_, err = projectClient(state).EnsureTeam(ctx)
require.ErrorContains(t, err, "open the Herdr app, then click Start agents on the views page")
} else {
_, err = projectClient(state).Identity(ctx)
require.ErrorContains(t, err, "project server request did not finish")
assert.NotContains(t, err.Error(), "check Herdr")
}
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
}
// TestManagementTimeoutHasRecoveryStep checks that a management request that
// does not finish names its own recovery and no Herdr step: no management
// read waits on Herdr.
func TestManagementTimeoutHasRecoveryStep(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState(t.TempDir())
require.NoError(t, err)
release := make(chan struct{})
api := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { <-release }))
defer api.Close()
defer close(release)
state.Record.Port = api.Listener.Addr().(*net.TCPAddr).Port
require.NoError(t, state.SaveRunning(project.Running{ManagementCredential: strings.Repeat("a", 64)}))
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Millisecond)
defer cancel()
_, err = projectClient(state).Identity(ctx)
require.ErrorContains(t, err, "project server request did not finish")
assert.NotContains(t, err.Error(), "check Herdr")
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
70 changes: 43 additions & 27 deletions cmd/surla/project_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -16,6 +15,7 @@ import (
"testing"
"time"

genproject "github.com/goadesign/surla/gen/project"
gensurla "github.com/goadesign/surla/gen/surla"
"github.com/goadesign/surla/internal/project"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -178,32 +178,48 @@ func TestTeamCommandProtocol(t *testing.T) {
assert.Equal(t, 50*time.Second, teamHTTPClient().Timeout)
}

// TestEnsureTeamDeclaredErrorsAreFixed drives the generated CLI decoder with hostile messages.
func TestEnsureTeamDeclaredErrorsAreFixed(t *testing.T) {
for _, tc := range []struct {
name, body, want string
status int
}{{"ambiguous", `{"name":"ambiguous_space","count":3}`, "3 Herdr spaces match", 409}, {"busy", `{"name":"window_busy","id":"fixture","message":"private peer output","temporary":false,"timeout":false,"fault":false}`, "a dialog is open in Herdr", 409}, {"unavailable", `{"name":"team_unavailable","cause":"herdr"}`, "Herdr did not complete a team command", 503}} {
t.Run(tc.name, func(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState(t.TempDir())
require.NoError(t, err)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("goa-error", strings.Split(tc.body, `"`)[3])
w.WriteHeader(tc.status)
_, err := io.WriteString(w, tc.body)
assert.NoError(t, err)
}))
defer api.Close()
state.Record.Port = api.Listener.Addr().(*net.TCPAddr).Port
require.NoError(t, state.SaveRunning(project.Running{ManagementCredential: strings.Repeat("a", 64)}))
_, err = projectClient(state).EnsureTeam(t.Context())
require.ErrorContains(t, err, tc.want)
assert.NotContains(t, err.Error(), "private peer output")
assert.Contains(t, err.Error(), fmt.Sprintf("HTTP %d", tc.status))
})
}
// TestAttachGuideTeamFailureIsFixed drives the CLI decoder with the team
// failures attach_guide's owner returns, as surla stdio meets them when a guide
// attaches. Each names its repair and the retry is calling the Surla tool
// again, never typing surla. project_state comes from a real project server
// whose registered guide credential is damaged; stopping comes from a stand-in
// reply with the owner's exact body.
func TestAttachGuideTeamFailureIsFixed(t *testing.T) {
t.Run("project_state", func(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState(t.TempDir())
require.NoError(t, err)
management := startStdioProjectServer(t, state)
guide, err := attachFixtureGuide(t.Context(), state, management)
require.NoError(t, err)
const garbage = "not-a-credential-value"
require.NoError(t, os.WriteFile(state.AgentFile(guide.Handle), []byte(garbage), 0o600))

_, err = projectClient(state).AttachGuide(t.Context(), &genproject.AttachGuidePayload{})
require.Error(t, err)
assert.Contains(t, err.Error(), "a file in this project's private state "+state.Dir+" could not be used")
assert.Contains(t, err.Error(), "chmod 600 on that file")
assert.Contains(t, err.Error(), "then call the Surla tool again")
assert.NotContains(t, err.Error(), garbage)
assert.NotContains(t, strings.ToLower(err.Error()), "run surla")
})
t.Run("stopping", func(t *testing.T) {
isolateServeWorkspace(t)
state, err := fixtureProjectState(t.TempDir())
require.NoError(t, err)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("goa-error", "team_unavailable")
w.WriteHeader(http.StatusServiceUnavailable)
_, err := io.WriteString(w, `{"name":"team_unavailable","cause":"stopping"}`)
assert.NoError(t, err)
}))
defer api.Close()
state.Record.Port = api.Listener.Addr().(*net.TCPAddr).Port
require.NoError(t, state.SaveRunning(project.Running{ManagementCredential: strings.Repeat("a", 64)}))
_, err = projectClient(state).AttachGuide(t.Context(), &genproject.AttachGuidePayload{})
require.ErrorContains(t, err, "the project server is stopping; call the Surla tool again to start it (HTTP 503)")
})
}

// TestInstalledTeamOptions proves argument discovery against local programs
Expand Down
Loading
Loading