From 8638a6fd309dbff72941c5f0c2d2321114691a60 Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Tue, 20 Oct 2026 12:13:11 -0700 Subject: [PATCH 1/6] feat: read agent space ownership and operator app state AgentSpaces lists the agent spaces in the region. A space counts as made by setup (ManagedByLDCLI) only if it has the tag ldcli:managed=aws-devops-agent. A user can change the description, so setup does not read it. Each space costs one ListTagsForResource call. An agent space does not report its ARN, so setup builds it. If setup cannot read a tag, it stops with an error that names aidevops:ListTagsForResource. AgentSpace reads one space with the same check. appState reads the operator app with GetOperatorApp. GetStatus uses it. If the app is off, the status has operatorAppEnabled false and no URL, and the plaintext output says "off". Co-Authored-By: Claude Opus 5.5 --- cmd/aws_devops_agent/status.go | 6 +- cmd/aws_devops_agent/status_fixture_test.go | 7 +- cmd/aws_devops_agent/status_test.go | 12 +++ cmd/aws_devops_agent/testdata/status.json | 1 + internal/awsdevops/awsdevops.go | 2 + internal/awsdevops/awsdevops_test.go | 39 ++++++- internal/awsdevops/lookup.go | 82 +++++++++++++++ internal/awsdevops/lookup_test.go | 107 ++++++++++++++++++++ internal/awsdevops/setup.go | 40 ++++++++ internal/awsdevops/status.go | 23 +++-- 10 files changed, 304 insertions(+), 15 deletions(-) diff --git a/cmd/aws_devops_agent/status.go b/cmd/aws_devops_agent/status.go index 342e6db58..617c22e33 100644 --- a/cmd/aws_devops_agent/status.go +++ b/cmd/aws_devops_agent/status.go @@ -92,7 +92,11 @@ func printStatus(out io.Writer, status awsdevops.Status) { } for _, space := range status.AgentSpaces { _, _ = fmt.Fprintf(out, "\nAgent space %s (%s)\n", space.Name, space.AgentSpaceID) - _, _ = fmt.Fprintf(out, " Operator app: %s\n", space.OperatorAppURL) + app := space.OperatorAppURL + if !space.OperatorAppEnabled { + app = "off" + } + _, _ = fmt.Fprintf(out, " Operator app: %s\n", app) if len(space.Associations) > 0 { _, _ = fmt.Fprintln(out, " Connected to:") } diff --git a/cmd/aws_devops_agent/status_fixture_test.go b/cmd/aws_devops_agent/status_fixture_test.go index 747249a27..f02c25d5c 100644 --- a/cmd/aws_devops_agent/status_fixture_test.go +++ b/cmd/aws_devops_agent/status_fixture_test.go @@ -14,9 +14,10 @@ func realisticStatus() awsdevops.Status { {ServiceID: "s-ld01", ServiceType: "mcpserver", Name: "LaunchDarkly"}, }, AgentSpaces: []awsdevops.AgentSpaceStatus{{ - AgentSpaceID: "as-prod01", - Name: "Production", - OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"), + AgentSpaceID: "as-prod01", + Name: "Production", + OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"), + OperatorAppEnabled: true, Associations: []awsdevops.AssociationStatus{ {AssociationID: "a-001", ServiceID: "aws", Status: "valid", AccountID: "123456789012", AccountType: "monitor"}, {AssociationID: "a-002", ServiceID: "s-ld01", Status: "valid"}, diff --git a/cmd/aws_devops_agent/status_test.go b/cmd/aws_devops_agent/status_test.go index 299739c52..271517e55 100644 --- a/cmd/aws_devops_agent/status_test.go +++ b/cmd/aws_devops_agent/status_test.go @@ -285,3 +285,15 @@ func TestStatusServicesListLabelsBlankService(t *testing.T) { } } } + +// An app that is off has no working URL. +func TestStatusShowsAnOperatorAppThatIsOff(t *testing.T) { + status := realisticStatus() + status.AgentSpaces[0].OperatorAppEnabled = false + status.AgentSpaces[0].OperatorAppURL = "" + + out := renderStatus(status) + + assert.Contains(t, out, "Operator app: off") + assert.NotContains(t, out, "aidevops.global.app.aws") +} diff --git a/cmd/aws_devops_agent/testdata/status.json b/cmd/aws_devops_agent/testdata/status.json index 46b9c9cbd..ad232fdd5 100644 --- a/cmd/aws_devops_agent/testdata/status.json +++ b/cmd/aws_devops_agent/testdata/status.json @@ -25,6 +25,7 @@ "agentSpaceId": "as-prod01", "name": "Production", "operatorAppUrl": "https://as-prod01.aidevops.global.app.aws", + "operatorAppEnabled": true, "associations": [ { "associationId": "a-001", diff --git a/internal/awsdevops/awsdevops.go b/internal/awsdevops/awsdevops.go index 79aab94bd..72155518a 100644 --- a/internal/awsdevops/awsdevops.go +++ b/internal/awsdevops/awsdevops.go @@ -54,6 +54,8 @@ type AgentAPI interface { DeregisterService(context.Context, *devopsagent.DeregisterServiceInput, ...func(*devopsagent.Options)) (*devopsagent.DeregisterServiceOutput, error) DisassociateService(context.Context, *devopsagent.DisassociateServiceInput, ...func(*devopsagent.Options)) (*devopsagent.DisassociateServiceOutput, error) EnableOperatorApp(context.Context, *devopsagent.EnableOperatorAppInput, ...func(*devopsagent.Options)) (*devopsagent.EnableOperatorAppOutput, error) + GetOperatorApp(context.Context, *devopsagent.GetOperatorAppInput, ...func(*devopsagent.Options)) (*devopsagent.GetOperatorAppOutput, error) + ListTagsForResource(context.Context, *devopsagent.ListTagsForResourceInput, ...func(*devopsagent.Options)) (*devopsagent.ListTagsForResourceOutput, error) GetAgentSpace(context.Context, *devopsagent.GetAgentSpaceInput, ...func(*devopsagent.Options)) (*devopsagent.GetAgentSpaceOutput, error) ListAgentSpaces(context.Context, *devopsagent.ListAgentSpacesInput, ...func(*devopsagent.Options)) (*devopsagent.ListAgentSpacesOutput, error) ListAssociations(context.Context, *devopsagent.ListAssociationsInput, ...func(*devopsagent.Options)) (*devopsagent.ListAssociationsOutput, error) diff --git a/internal/awsdevops/awsdevops_test.go b/internal/awsdevops/awsdevops_test.go index d889f05b2..948fd8b82 100644 --- a/internal/awsdevops/awsdevops_test.go +++ b/internal/awsdevops/awsdevops_test.go @@ -27,6 +27,11 @@ import ( const testAccountID = "123456789012" type fakeAgent struct { + // tags holds tags by resource ARN. + tags map[string]map[string]string + tagsErr error + // operatorApps holds enabled apps by space. + operatorApps map[string]*devopsagent.GetOperatorAppOutput calls []string services []agenttypes.RegisteredService associationInputs []*devopsagent.AssociateServiceInput @@ -101,6 +106,25 @@ func (f *fakeAgent) DisassociateService(_ context.Context, in *devopsagent.Disas return &devopsagent.DisassociateServiceOutput{}, nil } +func (f *fakeAgent) GetOperatorApp(_ context.Context, in *devopsagent.GetOperatorAppInput, _ ...func(*devopsagent.Options)) (*devopsagent.GetOperatorAppOutput, error) { + f.calls = append(f.calls, "GetOperatorApp") + app, ok := f.operatorApps[aws.ToString(in.AgentSpaceId)] + if !ok { + return nil, &agenttypes.ResourceNotFoundException{} + } + + return app, nil +} + +func (f *fakeAgent) ListTagsForResource(_ context.Context, in *devopsagent.ListTagsForResourceInput, _ ...func(*devopsagent.Options)) (*devopsagent.ListTagsForResourceOutput, error) { + f.calls = append(f.calls, "ListTagsForResource") + if f.tagsErr != nil { + return nil, f.tagsErr + } + + return &devopsagent.ListTagsForResourceOutput{Tags: f.tags[aws.ToString(in.ResourceArn)]}, nil +} + func (f *fakeAgent) EnableOperatorApp(_ context.Context, _ *devopsagent.EnableOperatorAppInput, _ ...func(*devopsagent.Options)) (*devopsagent.EnableOperatorAppOutput, error) { f.calls = append(f.calls, "EnableOperatorApp") @@ -110,11 +134,18 @@ func (f *fakeAgent) EnableOperatorApp(_ context.Context, _ *devopsagent.EnableOp } func (f *fakeAgent) GetAgentSpace(_ context.Context, in *devopsagent.GetAgentSpaceInput, _ ...func(*devopsagent.Options)) (*devopsagent.GetAgentSpaceOutput, error) { + id := aws.ToString(in.AgentSpaceId) + name := "launchdarkly" + var description *string + for _, space := range f.agentSpaces { + if aws.ToString(space.AgentSpaceId) == id && space.Name != nil { + name = aws.ToString(space.Name) + description = space.Description + } + } + return &devopsagent.GetAgentSpaceOutput{ - AgentSpace: &agenttypes.AgentSpace{ - AgentSpaceId: in.AgentSpaceId, - Name: aws.String("launchdarkly"), - }, + AgentSpace: &agenttypes.AgentSpace{AgentSpaceId: in.AgentSpaceId, Name: aws.String(name), Description: description}, }, nil } diff --git a/internal/awsdevops/lookup.go b/internal/awsdevops/lookup.go index 7d6baa2d3..9b4b1f00a 100644 --- a/internal/awsdevops/lookup.go +++ b/internal/awsdevops/lookup.go @@ -70,6 +70,88 @@ func listAssociations(ctx context.Context, clients Clients, agentSpaceID string) } } +// AgentSpaceSummary identifies an agent space the account already has, for a +// caller that has to pick one. +type AgentSpaceSummary struct { + AgentSpaceID string `json:"agentSpaceId"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + // ManagedByLDCLI means that setup made the space. + ManagedByLDCLI bool `json:"managedByLdcli"` +} + +// AgentSpaces lists the agent spaces in the account and region. A space that +// setup made has the ldcli:managed tag. A user can change the description, so +// only the tag counts. Each space costs one tag lookup. +func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, error) { + spaces, err := listAgentSpaces(ctx, clients) + if err != nil || len(spaces) == 0 { + return nil, err + } + accountID, err := clients.AccountID(ctx) + if err != nil { + return nil, err + } + + summaries := make([]AgentSpaceSummary, 0, len(spaces)) + for _, space := range spaces { + summary := AgentSpaceSummary{ + AgentSpaceID: aws.ToString(space.AgentSpaceId), + Name: aws.ToString(space.Name), + Description: aws.ToString(space.Description), + } + if summary.ManagedByLDCLI, err = hasManagedTag(ctx, clients, accountID, summary.AgentSpaceID); err != nil { + return nil, err + } + summaries = append(summaries, summary) + } + + return summaries, nil +} + +// AgentSpace reads one agent space, with the same ownership check as +// AgentSpaces, for a caller that already has the space ID. +func AgentSpace(ctx context.Context, clients Clients, spaceID string) (AgentSpaceSummary, error) { + out, err := clients.Agent.GetAgentSpace(ctx, &devopsagent.GetAgentSpaceInput{AgentSpaceId: aws.String(spaceID)}) + if err != nil { + return AgentSpaceSummary{}, fmt.Errorf("unable to read agent space %s: %w", spaceID, err) + } + accountID, err := clients.AccountID(ctx) + if err != nil { + return AgentSpaceSummary{}, err + } + summary := AgentSpaceSummary{ + AgentSpaceID: spaceID, + Name: aws.ToString(out.AgentSpace.Name), + Description: aws.ToString(out.AgentSpace.Description), + } + summary.ManagedByLDCLI, err = hasManagedTag(ctx, clients, accountID, spaceID) + if err != nil { + return AgentSpaceSummary{}, err + } + + return summary, nil +} + +// hasManagedTag reports the ldcli:managed tag on an agent space. +func hasManagedTag(ctx context.Context, clients Clients, accountID, spaceID string) (bool, error) { + out, err := clients.Agent.ListTagsForResource(ctx, &devopsagent.ListTagsForResourceInput{ + ResourceArn: aws.String(agentSpaceARN(clients.Region, accountID, spaceID)), + }) + if err != nil { + return false, fmt.Errorf( + "unable to read the tags of agent space %s. Setup needs aidevops:ListTagsForResource: %w", spaceID, err) + } + + return out.Tags[ManagedTagKey] == ManagedTagValue, nil +} + +// agentSpaceARN builds the ARN that the tag API needs. An agent space does not +// report its ARN. +func agentSpaceARN(region, accountID, spaceID string) string { + return fmt.Sprintf("arn:aws:aidevops:%s:%s:agentspace/%s", region, accountID, spaceID) +} + // FindMCPServer returns the ID of the MCP server registered at endpoint, if the // account has one. AWS keeps MCP servers at the account level, so a second // agent space reuses the existing registration. diff --git a/internal/awsdevops/lookup_test.go b/internal/awsdevops/lookup_test.go index 8457ef052..2632d95ff 100644 --- a/internal/awsdevops/lookup_test.go +++ b/internal/awsdevops/lookup_test.go @@ -2,10 +2,12 @@ package awsdevops_test import ( "context" + "errors" "net/http" "testing" "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/devopsagent" agenttypes "github.com/aws/aws-sdk-go-v2/service/devopsagent/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -73,3 +75,108 @@ func TestFindMCPServerMatchesOnTheEndpointInTheServiceDetails(t *testing.T) { assert.Equal(t, "mcp-2", serviceID) } + +const space1ARN = "arn:aws:aidevops:us-east-1:" + testAccountID + ":agentspace/space-1" + +var spaceTags = map[string]string{awsdevops.ManagedTagKey: awsdevops.ManagedTagValue} + +func TestAgentSpacesListsEveryPage(t *testing.T) { + agent := &fakeAgent{ + paginate: true, + agentSpaces: []agenttypes.AgentSpace{ + {AgentSpaceId: aws.String("space-1"), Name: aws.String("launchdarkly"), Description: aws.String("Managed by the LaunchDarkly CLI")}, + {AgentSpaceId: aws.String("space-2"), Name: aws.String("platform"), Description: aws.String("Team space")}, + }, + tags: map[string]map[string]string{space1ARN: spaceTags}, + } + + spaces, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) + require.NoError(t, err) + + assert.Equal(t, []awsdevops.AgentSpaceSummary{ + {AgentSpaceID: "space-1", Name: "launchdarkly", Description: "Managed by the LaunchDarkly CLI", ManagedByLDCLI: true}, + {AgentSpaceID: "space-2", Name: "platform", Description: "Team space"}, + }, spaces) +} + +// A user can change the description, so only the tag counts. +func TestAgentSpacesCountsOnlyTheTag(t *testing.T) { + agent := &fakeAgent{agentSpaces: []agenttypes.AgentSpace{ + {AgentSpaceId: aws.String("space-1"), Name: aws.String("copy"), Description: aws.String("Managed by the LaunchDarkly CLI")}, + {AgentSpaceId: aws.String("space-2"), Name: aws.String("tagged"), Description: aws.String("Renamed by a user")}, + }} + agent.tags = map[string]map[string]string{ + "arn:aws:aidevops:us-east-1:" + testAccountID + ":agentspace/space-2": spaceTags, + } + + spaces, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) + require.NoError(t, err) + + assert.False(t, spaces[0].ManagedByLDCLI) + assert.True(t, spaces[1].ManagedByLDCLI) + assert.Equal(t, 2, countCalls(agent.calls, "ListTagsForResource")) +} + +func TestAgentSpacesReportsATagError(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{ + {AgentSpaceId: aws.String("space-1"), Name: aws.String("launchdarkly"), Description: aws.String("Managed by the LaunchDarkly CLI")}, + }, + tagsErr: errors.New("AccessDeniedException"), + } + + _, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) + + require.Error(t, err) + assert.Contains(t, err.Error(), "tags of agent space space-1") + assert.Contains(t, err.Error(), "aidevops:ListTagsForResource") +} + +func countCalls(calls []string, name string) int { + n := 0 + for _, call := range calls { + if call == name { + n++ + } + } + + return n +} + +func TestStatusReadsTheOperatorApp(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "on"), namedSpace("space-2", "off")}, + operatorApps: map[string]*devopsagent.GetOperatorAppOutput{ + "space-1": {Iam: &agenttypes.IamAuthConfiguration{}}, + }, + } + + status, err := awsdevops.GetStatus(context.Background(), newTestClients(agent, newFakeIAM()), "") + require.NoError(t, err) + + require.Len(t, status.AgentSpaces, 2) + assert.True(t, status.AgentSpaces[0].OperatorAppEnabled) + assert.Equal(t, awsdevops.OperatorAppURL("space-1"), status.AgentSpaces[0].OperatorAppURL) + assert.False(t, status.AgentSpaces[1].OperatorAppEnabled) + assert.Empty(t, status.AgentSpaces[1].OperatorAppURL) +} + +func TestAgentSpaceReadsTheTagOfOneSpace(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{ + {AgentSpaceId: aws.String("space-1"), Name: aws.String("launchdarkly"), Description: aws.String("Managed by the LaunchDarkly CLI")}, + }, + tags: map[string]map[string]string{space1ARN: spaceTags}, + } + + space, err := awsdevops.AgentSpace(context.Background(), newTestClients(agent, newFakeIAM()), "space-1") + require.NoError(t, err) + + assert.True(t, space.ManagedByLDCLI) + assert.Equal(t, "launchdarkly", space.Name) + assert.NotContains(t, agent.calls, "ListAgentSpaces") +} + +func namedSpace(id, name string) agenttypes.AgentSpace { + return agenttypes.AgentSpace{AgentSpaceId: aws.String(id), Name: aws.String(name)} +} diff --git a/internal/awsdevops/setup.go b/internal/awsdevops/setup.go index a270a39d9..e2f9b6616 100644 --- a/internal/awsdevops/setup.go +++ b/internal/awsdevops/setup.go @@ -92,6 +92,46 @@ type SetupResult struct { MCPAssociationID string `json:"mcpAssociationId,omitempty"` } +// AppAction is the planned operator app action. +type AppAction int + +const ( + AppEnable AppAction = iota + AppKeep + AppChange +) + +// AppStep is the planned operator app step. +type AppStep struct { + Action AppAction + URL string + // Flow is the sign-in at plan time. + Flow string +} + +// appState reads the app. Flow "" means off. +func appState(ctx context.Context, clients Clients, spaceID string) (AppStep, error) { + app, err := clients.Agent.GetOperatorApp(ctx, &devopsagent.GetOperatorAppInput{AgentSpaceId: aws.String(spaceID)}) + var missing *agenttypes.ResourceNotFoundException + switch { + case errors.As(err, &missing): + return AppStep{}, nil + case err != nil: + return AppStep{}, fmt.Errorf("unable to read the operator app of agent space %s: %w", spaceID, err) + } + state := AppStep{URL: aws.ToString(app.OperatorAppUrl)} + switch { + case app.Idc != nil: + state.Flow = "idc" + case app.Idp != nil: + state.Flow = "idp" + case app.Iam != nil: + state.Flow = "iam" + } + + return state, nil +} + // Setup provisions the IAM roles, agent space, service associations and the // MCP server the AWS DevOps Agent needs to manage LaunchDarkly flags. func Setup(ctx context.Context, clients Clients, opts SetupOptions) (SetupResult, error) { diff --git a/internal/awsdevops/status.go b/internal/awsdevops/status.go index 755a08af2..c3370d2a9 100644 --- a/internal/awsdevops/status.go +++ b/internal/awsdevops/status.go @@ -36,10 +36,12 @@ type ServiceStatus struct { } type AgentSpaceStatus struct { - AgentSpaceID string `json:"agentSpaceId"` - Name string `json:"name"` - OperatorAppURL string `json:"operatorAppUrl"` - Associations []AssociationStatus `json:"associations,omitempty"` + AgentSpaceID string `json:"agentSpaceId"` + Name string `json:"name"` + OperatorAppURL string `json:"operatorAppUrl"` + // OperatorAppEnabled is false if the app is off. The URL is then empty. + OperatorAppEnabled bool `json:"operatorAppEnabled"` + Associations []AssociationStatus `json:"associations,omitempty"` } type AssociationStatus struct { @@ -107,9 +109,16 @@ func GetStatus(ctx context.Context, clients Clients, agentSpaceID string) (Statu return status, fmt.Errorf("unable to read agent space %s: %w", id, err) } spaceStatus := AgentSpaceStatus{ - AgentSpaceID: id, - Name: aws.ToString(space.AgentSpace.Name), - OperatorAppURL: OperatorAppURL(id), + AgentSpaceID: id, + Name: aws.ToString(space.AgentSpace.Name), + } + app, err := appState(ctx, clients, id) + if err != nil { + return status, err + } + if app.Flow != "" { + spaceStatus.OperatorAppEnabled = true + spaceStatus.OperatorAppURL = OperatorAppURL(id) } associations, err := listAssociations(ctx, clients, id) From 6cdc078fb59400cf8425f4550159b3850993a226 Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Wed, 21 Oct 2026 11:17:30 -0700 Subject: [PATCH 2/6] test: add operatorAppEnabled to the partial status fixture Co-Authored-By: Claude Opus 5.5 --- cmd/aws_devops_agent/testdata/status-partial.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/aws_devops_agent/testdata/status-partial.json b/cmd/aws_devops_agent/testdata/status-partial.json index 3a6fc91a7..061078c75 100644 --- a/cmd/aws_devops_agent/testdata/status-partial.json +++ b/cmd/aws_devops_agent/testdata/status-partial.json @@ -16,7 +16,8 @@ { "agentSpaceId": "as-prod01", "name": "Production", - "operatorAppUrl": "https://as-prod01.aidevops.global.app.aws" + "operatorAppUrl": "https://as-prod01.aidevops.global.app.aws", + "operatorAppEnabled": false } ], "missingSetup": [ From 6f69c6f6d9eab886245c3ad0f1d147950dc6fbc3 Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Wed, 21 Oct 2026 11:47:31 -0700 Subject: [PATCH 3/6] fix: show an operator app that status cannot read as unknown status failed if GetOperatorApp failed for a reason other than an app that is off, for example without aidevops:GetOperatorApp. The whole report was then lost. status now reports the app as unknown with the AWS error code, and shows the rest of the space. status also uses the operator app URL that AWS sends, like setup. It builds the URL only if AWS sends none. Doc comments now say that appState needs a space that exists, and that AgentSpaces fails on purpose if it cannot read the tag of a space. Co-Authored-By: Claude Opus 5.5 --- cmd/aws_devops_agent/status.go | 5 +- cmd/aws_devops_agent/status_fixture_test.go | 6 +-- cmd/aws_devops_agent/status_test.go | 12 +++++ .../testdata/status-partial.json | 5 +- internal/awsdevops/awsdevops_test.go | 7 ++- internal/awsdevops/lookup.go | 4 ++ internal/awsdevops/lookup_test.go | 53 +++++++++++++++++++ internal/awsdevops/setup.go | 4 +- internal/awsdevops/status.go | 35 +++++++++--- 9 files changed, 116 insertions(+), 15 deletions(-) diff --git a/cmd/aws_devops_agent/status.go b/cmd/aws_devops_agent/status.go index 617c22e33..be31b1a46 100644 --- a/cmd/aws_devops_agent/status.go +++ b/cmd/aws_devops_agent/status.go @@ -93,7 +93,10 @@ func printStatus(out io.Writer, status awsdevops.Status) { for _, space := range status.AgentSpaces { _, _ = fmt.Fprintf(out, "\nAgent space %s (%s)\n", space.Name, space.AgentSpaceID) app := space.OperatorAppURL - if !space.OperatorAppEnabled { + switch { + case space.OperatorAppError != "": + app = "unknown (" + space.OperatorAppError + ")" + case !space.OperatorAppEnabled: app = "off" } _, _ = fmt.Fprintf(out, " Operator app: %s\n", app) diff --git a/cmd/aws_devops_agent/status_fixture_test.go b/cmd/aws_devops_agent/status_fixture_test.go index f02c25d5c..3e727c7bb 100644 --- a/cmd/aws_devops_agent/status_fixture_test.go +++ b/cmd/aws_devops_agent/status_fixture_test.go @@ -37,9 +37,9 @@ func partialStatus() awsdevops.Status { {Name: awsdevops.OperatorAppRoleName}, }, AgentSpaces: []awsdevops.AgentSpaceStatus{{ - AgentSpaceID: "as-prod01", - Name: "Production", - OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"), + AgentSpaceID: "as-prod01", + Name: "Production", + OperatorAppError: "AccessDeniedException", }}, MissingSetup: []string{"IAM role " + awsdevops.OperatorAppRoleName + " does not exist"}, } diff --git a/cmd/aws_devops_agent/status_test.go b/cmd/aws_devops_agent/status_test.go index 271517e55..276ac5647 100644 --- a/cmd/aws_devops_agent/status_test.go +++ b/cmd/aws_devops_agent/status_test.go @@ -253,6 +253,18 @@ func TestStatusFullSetupOmitsSetupHint(t *testing.T) { assert.NotContains(t, renderStatus(realisticStatus()), "ldcli aws-devops-agent setup") } +func TestStatusShowsAnUnknownOperatorApp(t *testing.T) { + status := realisticStatus() + space := &status.AgentSpaces[0] + space.OperatorAppEnabled = false + space.OperatorAppURL = "" + space.OperatorAppError = "AccessDeniedException" + section := spaceSection(t, renderStatus(status), "Production") + + assert.Equal(t, " Operator app: unknown (AccessDeniedException)", lineWith(t, section, "Operator app")) + assert.Contains(t, strings.Join(section, "\n"), "LaunchDarkly MCP server: ready") +} + func TestStatusJSONUnchanged(t *testing.T) { cases := map[string]awsdevops.Status{ "testdata/status.json": realisticStatus(), diff --git a/cmd/aws_devops_agent/testdata/status-partial.json b/cmd/aws_devops_agent/testdata/status-partial.json index 061078c75..d54be8ce6 100644 --- a/cmd/aws_devops_agent/testdata/status-partial.json +++ b/cmd/aws_devops_agent/testdata/status-partial.json @@ -16,8 +16,9 @@ { "agentSpaceId": "as-prod01", "name": "Production", - "operatorAppUrl": "https://as-prod01.aidevops.global.app.aws", - "operatorAppEnabled": false + "operatorAppUrl": "", + "operatorAppEnabled": false, + "operatorAppError": "AccessDeniedException" } ], "missingSetup": [ diff --git a/internal/awsdevops/awsdevops_test.go b/internal/awsdevops/awsdevops_test.go index 948fd8b82..703d5b3ec 100644 --- a/internal/awsdevops/awsdevops_test.go +++ b/internal/awsdevops/awsdevops_test.go @@ -31,7 +31,9 @@ type fakeAgent struct { tags map[string]map[string]string tagsErr error // operatorApps holds enabled apps by space. - operatorApps map[string]*devopsagent.GetOperatorAppOutput + operatorApps map[string]*devopsagent.GetOperatorAppOutput + // operatorAppErr fails every GetOperatorApp call. + operatorAppErr error calls []string services []agenttypes.RegisteredService associationInputs []*devopsagent.AssociateServiceInput @@ -108,6 +110,9 @@ func (f *fakeAgent) DisassociateService(_ context.Context, in *devopsagent.Disas func (f *fakeAgent) GetOperatorApp(_ context.Context, in *devopsagent.GetOperatorAppInput, _ ...func(*devopsagent.Options)) (*devopsagent.GetOperatorAppOutput, error) { f.calls = append(f.calls, "GetOperatorApp") + if f.operatorAppErr != nil { + return nil, f.operatorAppErr + } app, ok := f.operatorApps[aws.ToString(in.AgentSpaceId)] if !ok { return nil, &agenttypes.ResourceNotFoundException{} diff --git a/internal/awsdevops/lookup.go b/internal/awsdevops/lookup.go index 9b4b1f00a..836b7784c 100644 --- a/internal/awsdevops/lookup.go +++ b/internal/awsdevops/lookup.go @@ -83,6 +83,10 @@ type AgentSpaceSummary struct { // AgentSpaces lists the agent spaces in the account and region. A space that // setup made has the ldcli:managed tag. A user can change the description, so // only the tag counts. Each space costs one tag lookup. +// +// If one tag lookup fails, AgentSpaces fails. Setup uses the tag to decide if +// it can change the roles and the operator app of a space, so it must not +// guess the owner of a space. The status command does not call AgentSpaces. func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, error) { spaces, err := listAgentSpaces(ctx, clients) if err != nil || len(spaces) == 0 { diff --git a/internal/awsdevops/lookup_test.go b/internal/awsdevops/lookup_test.go index 2632d95ff..2f7e169c5 100644 --- a/internal/awsdevops/lookup_test.go +++ b/internal/awsdevops/lookup_test.go @@ -161,6 +161,59 @@ func TestStatusReadsTheOperatorApp(t *testing.T) { assert.Empty(t, status.AgentSpaces[1].OperatorAppURL) } +// apiError acts like an AWS API error with a code. +type apiError struct{ code string } + +func (e apiError) Error() string { return "api error " + e.code + ": not allowed" } +func (e apiError) ErrorCode() string { return e.code } + +// A policy without aidevops:GetOperatorApp must not hide the rest of status. +func TestStatusReportsAnUnreadableAppAsUnknown(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "on")}, + operatorAppErr: apiError{code: "AccessDeniedException"}, + associations: []agenttypes.Association{{AssociationId: aws.String("assoc-1"), ServiceId: aws.String("mcp-1")}}, + } + + status, err := awsdevops.GetStatus(context.Background(), newTestClients(agent, newFakeIAM()), "") + require.NoError(t, err) + + require.Len(t, status.AgentSpaces, 1) + space := status.AgentSpaces[0] + assert.Equal(t, "AccessDeniedException", space.OperatorAppError) + assert.False(t, space.OperatorAppEnabled) + assert.Empty(t, space.OperatorAppURL) + assert.Len(t, space.Associations, 1) + assert.Len(t, status.Roles, 2) +} + +func TestStatusReportsTheErrorTextWithoutACode(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "on")}, + operatorAppErr: errors.New("connection reset"), + } + + status, err := awsdevops.GetStatus(context.Background(), newTestClients(agent, newFakeIAM()), "") + require.NoError(t, err) + + assert.Contains(t, status.AgentSpaces[0].OperatorAppError, "connection reset") +} + +// status and setup show the URL that AWS sends. +func TestStatusUsesTheOperatorAppURLFromAWS(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "on")}, + operatorApps: map[string]*devopsagent.GetOperatorAppOutput{ + "space-1": {Iam: &agenttypes.IamAuthConfiguration{}, OperatorAppUrl: aws.String("https://app.example.aws")}, + }, + } + + status, err := awsdevops.GetStatus(context.Background(), newTestClients(agent, newFakeIAM()), "") + require.NoError(t, err) + + assert.Equal(t, "https://app.example.aws", status.AgentSpaces[0].OperatorAppURL) +} + func TestAgentSpaceReadsTheTagOfOneSpace(t *testing.T) { agent := &fakeAgent{ agentSpaces: []agenttypes.AgentSpace{ diff --git a/internal/awsdevops/setup.go b/internal/awsdevops/setup.go index e2f9b6616..8f379710c 100644 --- a/internal/awsdevops/setup.go +++ b/internal/awsdevops/setup.go @@ -109,7 +109,9 @@ type AppStep struct { Flow string } -// appState reads the app. Flow "" means off. +// appState reads the app. Flow "" means off. The space must exist. AWS sends +// ResourceNotFoundException for an app that is off and for a space that does +// not exist, so appState cannot tell the two apart. func appState(ctx context.Context, clients Clients, spaceID string) (AppStep, error) { app, err := clients.Agent.GetOperatorApp(ctx, &devopsagent.GetOperatorAppInput{AgentSpaceId: aws.String(spaceID)}) var missing *agenttypes.ResourceNotFoundException diff --git a/internal/awsdevops/status.go b/internal/awsdevops/status.go index c3370d2a9..c8265f0f6 100644 --- a/internal/awsdevops/status.go +++ b/internal/awsdevops/status.go @@ -2,6 +2,7 @@ package awsdevops import ( "context" + "errors" "fmt" "github.com/aws/aws-sdk-go-v2/aws" @@ -40,8 +41,12 @@ type AgentSpaceStatus struct { Name string `json:"name"` OperatorAppURL string `json:"operatorAppUrl"` // OperatorAppEnabled is false if the app is off. The URL is then empty. - OperatorAppEnabled bool `json:"operatorAppEnabled"` - Associations []AssociationStatus `json:"associations,omitempty"` + OperatorAppEnabled bool `json:"operatorAppEnabled"` + // OperatorAppError is set if status could not read the app, for example + // without aidevops:GetOperatorApp. The state of the app is then unknown, + // and OperatorAppEnabled is false. + OperatorAppError string `json:"operatorAppError,omitempty"` + Associations []AssociationStatus `json:"associations,omitempty"` } type AssociationStatus struct { @@ -112,13 +117,18 @@ func GetStatus(ctx context.Context, clients Clients, agentSpaceID string) (Statu AgentSpaceID: id, Name: aws.ToString(space.AgentSpace.Name), } + // status is a read command. If it cannot read the app, it reports the + // app as unknown and shows the rest of the space. app, err := appState(ctx, clients, id) - if err != nil { - return status, err - } - if app.Flow != "" { + switch { + case err != nil: + spaceStatus.OperatorAppError = errorCode(err) + case app.Flow != "": spaceStatus.OperatorAppEnabled = true - spaceStatus.OperatorAppURL = OperatorAppURL(id) + spaceStatus.OperatorAppURL = app.URL + if spaceStatus.OperatorAppURL == "" { + spaceStatus.OperatorAppURL = OperatorAppURL(id) + } } associations, err := listAssociations(ctx, clients, id) @@ -155,3 +165,14 @@ func associationAccount(configuration agenttypes.ServiceConfiguration) (accountI return "", "" } + +// errorCode gives the AWS error code, for example AccessDeniedException, or +// the error text if the error has no code. +func errorCode(err error) string { + var api interface{ ErrorCode() string } + if errors.As(err, &api) && api.ErrorCode() != "" { + return api.ErrorCode() + } + + return err.Error() +} From 377d1df692d713339441b4d6830caa0cc5a029e7 Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Wed, 21 Oct 2026 12:10:24 -0700 Subject: [PATCH 4/6] fix: name the tag permission only for an access error The tag error said that setup needs aidevops:ListTagsForResource for every failure. For another error, such as a server error that the SDK retried, the message now gives only the AWS error. Co-Authored-By: Claude Opus 5.5 --- internal/awsdevops/lookup.go | 7 ++++++- internal/awsdevops/lookup_test.go | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/awsdevops/lookup.go b/internal/awsdevops/lookup.go index 836b7784c..1b0bbb9e8 100644 --- a/internal/awsdevops/lookup.go +++ b/internal/awsdevops/lookup.go @@ -142,10 +142,15 @@ func hasManagedTag(ctx context.Context, clients Clients, accountID, spaceID stri out, err := clients.Agent.ListTagsForResource(ctx, &devopsagent.ListTagsForResourceInput{ ResourceArn: aws.String(agentSpaceARN(clients.Region, accountID, spaceID)), }) - if err != nil { + // The SDK retries a throttle or a server error before it returns. Only an + // access error points to the permission. + if err != nil && errorCode(err) == "AccessDeniedException" { return false, fmt.Errorf( "unable to read the tags of agent space %s. Setup needs aidevops:ListTagsForResource: %w", spaceID, err) } + if err != nil { + return false, fmt.Errorf("unable to read the tags of agent space %s: %w", spaceID, err) + } return out.Tags[ManagedTagKey] == ManagedTagValue, nil } diff --git a/internal/awsdevops/lookup_test.go b/internal/awsdevops/lookup_test.go index 2f7e169c5..f62090bfa 100644 --- a/internal/awsdevops/lookup_test.go +++ b/internal/awsdevops/lookup_test.go @@ -122,7 +122,7 @@ func TestAgentSpacesReportsATagError(t *testing.T) { agentSpaces: []agenttypes.AgentSpace{ {AgentSpaceId: aws.String("space-1"), Name: aws.String("launchdarkly"), Description: aws.String("Managed by the LaunchDarkly CLI")}, }, - tagsErr: errors.New("AccessDeniedException"), + tagsErr: apiError{code: "AccessDeniedException"}, } _, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) @@ -132,6 +132,19 @@ func TestAgentSpacesReportsATagError(t *testing.T) { assert.Contains(t, err.Error(), "aidevops:ListTagsForResource") } +// Another error does not blame a missing permission. +func TestAgentSpacesReportsAnotherTagErrorWithoutThePermission(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "launchdarkly")}, + tagsErr: apiError{code: "InternalServerException"}, + } + + _, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) + + require.Error(t, err) + assert.Equal(t, "unable to read the tags of agent space space-1: api error InternalServerException", err.Error()) +} + func countCalls(calls []string, name string) int { n := 0 for _, call := range calls { @@ -164,7 +177,7 @@ func TestStatusReadsTheOperatorApp(t *testing.T) { // apiError acts like an AWS API error with a code. type apiError struct{ code string } -func (e apiError) Error() string { return "api error " + e.code + ": not allowed" } +func (e apiError) Error() string { return "api error " + e.code } func (e apiError) ErrorCode() string { return e.code } // A policy without aidevops:GetOperatorApp must not hide the rest of status. From 04c516ef3f5c3fffc60cec40536514c2b06460ff Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Wed, 21 Oct 2026 12:16:13 -0700 Subject: [PATCH 5/6] feat: keep the agent spaces whose tags setup can read AgentSpaces failed if one tag lookup failed, so one space could hide every other space. Now the summary of that space has TagErr, and the other spaces stay in the list. If every lookup fails, AgentSpaces still returns the error. Co-Authored-By: Claude Opus 5.5 --- internal/awsdevops/awsdevops_test.go | 5 +++++ internal/awsdevops/lookup.go | 25 ++++++++++++++++++++----- internal/awsdevops/lookup_test.go | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/internal/awsdevops/awsdevops_test.go b/internal/awsdevops/awsdevops_test.go index 703d5b3ec..ff7193494 100644 --- a/internal/awsdevops/awsdevops_test.go +++ b/internal/awsdevops/awsdevops_test.go @@ -30,6 +30,8 @@ type fakeAgent struct { // tags holds tags by resource ARN. tags map[string]map[string]string tagsErr error + // tagsErrFor fails the tag lookup of one resource ARN. + tagsErrFor map[string]error // operatorApps holds enabled apps by space. operatorApps map[string]*devopsagent.GetOperatorAppOutput // operatorAppErr fails every GetOperatorApp call. @@ -126,6 +128,9 @@ func (f *fakeAgent) ListTagsForResource(_ context.Context, in *devopsagent.ListT if f.tagsErr != nil { return nil, f.tagsErr } + if err := f.tagsErrFor[aws.ToString(in.ResourceArn)]; err != nil { + return nil, err + } return &devopsagent.ListTagsForResourceOutput{Tags: f.tags[aws.ToString(in.ResourceArn)]}, nil } diff --git a/internal/awsdevops/lookup.go b/internal/awsdevops/lookup.go index 1b0bbb9e8..30f520c4d 100644 --- a/internal/awsdevops/lookup.go +++ b/internal/awsdevops/lookup.go @@ -78,15 +78,21 @@ type AgentSpaceSummary struct { Description string `json:"description,omitempty"` // ManagedByLDCLI means that setup made the space. ManagedByLDCLI bool `json:"managedByLdcli"` + // TagErr is set if setup could not read the tags of the space. The owner + // of the space is then unknown, and ManagedByLDCLI is false. + TagErr error `json:"-"` } // AgentSpaces lists the agent spaces in the account and region. A space that // setup made has the ldcli:managed tag. A user can change the description, so // only the tag counts. Each space costs one tag lookup. // -// If one tag lookup fails, AgentSpaces fails. Setup uses the tag to decide if -// it can change the roles and the operator app of a space, so it must not -// guess the owner of a space. The status command does not call AgentSpaces. +// If a tag lookup fails, the summary of that space has TagErr, and the other +// spaces stay in the list. Setup uses the tag to decide if it can change the +// roles and the operator app of a space, so a caller must not use a space with +// TagErr as if its owner is known. If every lookup fails, for example without +// aidevops:ListTagsForResource, AgentSpaces returns the first error. The +// status command does not call AgentSpaces. func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, error) { spaces, err := listAgentSpaces(ctx, clients) if err != nil || len(spaces) == 0 { @@ -98,17 +104,26 @@ func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, err } summaries := make([]AgentSpaceSummary, 0, len(spaces)) + var firstErr error + failed := 0 for _, space := range spaces { summary := AgentSpaceSummary{ AgentSpaceID: aws.ToString(space.AgentSpaceId), Name: aws.ToString(space.Name), Description: aws.ToString(space.Description), } - if summary.ManagedByLDCLI, err = hasManagedTag(ctx, clients, accountID, summary.AgentSpaceID); err != nil { - return nil, err + summary.ManagedByLDCLI, summary.TagErr = hasManagedTag(ctx, clients, accountID, summary.AgentSpaceID) + if summary.TagErr != nil { + failed++ + if firstErr == nil { + firstErr = summary.TagErr + } } summaries = append(summaries, summary) } + if failed == len(summaries) { + return nil, firstErr + } return summaries, nil } diff --git a/internal/awsdevops/lookup_test.go b/internal/awsdevops/lookup_test.go index f62090bfa..c69bb21de 100644 --- a/internal/awsdevops/lookup_test.go +++ b/internal/awsdevops/lookup_test.go @@ -117,6 +117,7 @@ func TestAgentSpacesCountsOnlyTheTag(t *testing.T) { assert.Equal(t, 2, countCalls(agent.calls, "ListTagsForResource")) } +// If every lookup fails, AgentSpaces fails. func TestAgentSpacesReportsATagError(t *testing.T) { agent := &fakeAgent{ agentSpaces: []agenttypes.AgentSpace{ @@ -132,6 +133,27 @@ func TestAgentSpacesReportsATagError(t *testing.T) { assert.Contains(t, err.Error(), "aidevops:ListTagsForResource") } +// One failed lookup does not hide the other spaces. +func TestAgentSpacesKeepsTheSpacesWithATag(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "launchdarkly"), namedSpace("space-2", "sandbox")}, + tags: map[string]map[string]string{space1ARN: spaceTags}, + tagsErrFor: map[string]error{ + "arn:aws:aidevops:us-east-1:" + testAccountID + ":agentspace/space-2": apiError{code: "InternalServerException"}, + }, + } + + spaces, err := awsdevops.AgentSpaces(context.Background(), newTestClients(agent, newFakeIAM())) + + require.NoError(t, err) + require.Len(t, spaces, 2) + assert.True(t, spaces[0].ManagedByLDCLI) + assert.NoError(t, spaces[0].TagErr) + assert.Equal(t, "sandbox", spaces[1].Name) + assert.False(t, spaces[1].ManagedByLDCLI) + assert.ErrorContains(t, spaces[1].TagErr, "tags of agent space space-2") +} + // Another error does not blame a missing permission. func TestAgentSpacesReportsAnotherTagErrorWithoutThePermission(t *testing.T) { agent := &fakeAgent{ From ff4e2fda751b608ab7d91a95442e5fd08313356e Mon Sep 17 00:00:00 2001 From: Christina Chu Date: Wed, 21 Oct 2026 12:56:08 -0700 Subject: [PATCH 6/6] feat: report a tag error of one agent space on its summary AgentSpace failed if it could not read the tags of the space. It now puts the error on the summary as TagErr, like AgentSpaces, so the caller decides what to do with a space whose owner is unknown. Co-Authored-By: Claude Opus 5.5 --- internal/awsdevops/lookup.go | 10 +++++----- internal/awsdevops/lookup_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/awsdevops/lookup.go b/internal/awsdevops/lookup.go index 30f520c4d..645b12d44 100644 --- a/internal/awsdevops/lookup.go +++ b/internal/awsdevops/lookup.go @@ -129,7 +129,8 @@ func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, err } // AgentSpace reads one agent space, with the same ownership check as -// AgentSpaces, for a caller that already has the space ID. +// AgentSpaces, for a caller that already has the space ID. If setup cannot +// read the tags, the summary has TagErr. func AgentSpace(ctx context.Context, clients Clients, spaceID string) (AgentSpaceSummary, error) { out, err := clients.Agent.GetAgentSpace(ctx, &devopsagent.GetAgentSpaceInput{AgentSpaceId: aws.String(spaceID)}) if err != nil { @@ -144,10 +145,9 @@ func AgentSpace(ctx context.Context, clients Clients, spaceID string) (AgentSpac Name: aws.ToString(out.AgentSpace.Name), Description: aws.ToString(out.AgentSpace.Description), } - summary.ManagedByLDCLI, err = hasManagedTag(ctx, clients, accountID, spaceID) - if err != nil { - return AgentSpaceSummary{}, err - } + // A tag error goes on the summary, as in AgentSpaces. The caller decides + // what to do with a space whose owner is unknown. + summary.ManagedByLDCLI, summary.TagErr = hasManagedTag(ctx, clients, accountID, spaceID) return summary, nil } diff --git a/internal/awsdevops/lookup_test.go b/internal/awsdevops/lookup_test.go index c69bb21de..3532bd102 100644 --- a/internal/awsdevops/lookup_test.go +++ b/internal/awsdevops/lookup_test.go @@ -154,6 +154,21 @@ func TestAgentSpacesKeepsTheSpacesWithATag(t *testing.T) { assert.ErrorContains(t, spaces[1].TagErr, "tags of agent space space-2") } +// A caller with a space ID gets the space even if its tags cannot be read. +func TestAgentSpaceReportsATagErrorOnTheSummary(t *testing.T) { + agent := &fakeAgent{ + agentSpaces: []agenttypes.AgentSpace{namedSpace("space-1", "launchdarkly")}, + tagsErr: apiError{code: "AccessDeniedException"}, + } + + space, err := awsdevops.AgentSpace(context.Background(), newTestClients(agent, newFakeIAM()), "space-1") + + require.NoError(t, err) + assert.Equal(t, "launchdarkly", space.Name) + assert.False(t, space.ManagedByLDCLI) + assert.ErrorContains(t, space.TagErr, "aidevops:ListTagsForResource") +} + // Another error does not blame a missing permission. func TestAgentSpacesReportsAnotherTagErrorWithoutThePermission(t *testing.T) { agent := &fakeAgent{