diff --git a/internal/sync/api/errors.go b/internal/sync/api/errors.go index 8b937c2d3..c0ccb4026 100644 --- a/internal/sync/api/errors.go +++ b/internal/sync/api/errors.go @@ -92,3 +92,20 @@ func responseError(err error) (map[string]any, bool) { } return nil, false } + +// IsForbidden reports whether a LaunchDarkly API error has a 403 status. +func IsForbidden(err error) bool { + status, ok := responseStatusCode(err) + return ok && status == http.StatusForbidden +} + +// ResponseCode returns the machine-readable code of a LaunchDarkly API error +// body, or "" when the body has none. +func ResponseCode(err error) string { + response, ok := responseError(err) + if !ok { + return "" + } + code, _ := response["code"].(string) + return code +} diff --git a/internal/sync/api/settings.go b/internal/sync/api/settings.go index 773e78ef0..833b8c761 100644 --- a/internal/sync/api/settings.go +++ b/internal/sync/api/settings.go @@ -19,6 +19,9 @@ type resolvedSetting struct { ResourceKind syncdomain.Kind `json:"resourceKind"` ResourceLookupKey string `json:"resourceLookupKey"` Direction syncdomain.SyncDirection `json:"direction"` + // MatchedResourcePattern is the stored rule pattern that produced the + // direction. It is absent or empty when no rule matched. + MatchedResourcePattern string `json:"matchedResourcePattern,omitempty"` } // SyncDirections returns the effective LaunchDarkly setting for each @@ -28,11 +31,27 @@ func (client Client) SyncDirections( projectKey string, ids []syncdomain.ResourceID, ) (map[syncdomain.ResourceID]syncdomain.SyncDirection, error) { - if len(ids) == 0 { - return map[syncdomain.ResourceID]syncdomain.SyncDirection{}, nil + ownership, err := client.ResolveOwnership(projectKey, ids) + if err != nil { + return nil, err + } + result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ownership)) + for id, owner := range ownership { + result[id] = owner.Direction } - if len(ids) > 100 { - return nil, fmt.Errorf("read sync settings: at most 100 resources are allowed") + return result, nil +} + +// ResolveOwnership returns the effective LaunchDarkly setting of each +// resource and the stored rule pattern that matched it. A resource of a kind +// that LaunchDarkly has no setting for is left out. Any number of resources +// can be read; they are asked for one page at a time. +func (client Client) ResolveOwnership( + projectKey string, + ids []syncdomain.ResourceID, +) (map[syncdomain.ResourceID]syncdomain.Ownership, error) { + if len(ids) == 0 { + return map[syncdomain.ResourceID]syncdomain.Ownership{}, nil } byKind := map[syncdomain.Kind][]syncdomain.ResourceID{} for _, id := range ids { @@ -41,31 +60,34 @@ func (client Client) SyncDirections( } byKind[id.Kind] = append(byKind[id.Kind], id) } - result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ids)) + result := make(map[syncdomain.ResourceID]syncdomain.Ownership, len(ids)) // The endpoint rejects a request that names a kind it has no setting for, // and the rejection fails every resource of the request. Each kind travels // alone, so a tool or a skill cannot take the variations down with it. + // The endpoint accepts at most one page of resources per request. for _, kind := range slices.Sorted(maps.Keys(byKind)) { - directions, err := client.syncDirectionsOfKind(projectKey, byKind[kind]) - if err != nil { - return nil, err + for batch := range slices.Chunk(byKind[kind], syncSettingsPageLimit) { + ownership, err := client.ownershipOfKind(projectKey, batch) + if err != nil { + return nil, err + } + maps.Copy(result, ownership) } - maps.Copy(result, directions) } return result, nil } -// syncDirectionsOfKind reads the settings of resources of one kind. A kind -// that LaunchDarkly rejects as unsupported has no settings. -func (client Client) syncDirectionsOfKind( +// ownershipOfKind reads the settings of resources of one kind. A kind that +// LaunchDarkly rejects as unsupported has no settings. +func (client Client) ownershipOfKind( projectKey string, ids []syncdomain.ResourceID, -) (map[syncdomain.ResourceID]syncdomain.SyncDirection, error) { +) (map[syncdomain.ResourceID]syncdomain.Ownership, error) { query := url.Values{} for _, id := range ids { query.Add("resources", string(id.Kind)+":"+id.LookupKey) } - result := make(map[syncdomain.ResourceID]syncdomain.SyncDirection, len(ids)) + result := make(map[syncdomain.ResourceID]syncdomain.Ownership, len(ids)) // The endpoint pages its items. Read every page, so that a batch of more // than one page of resources does not look like missing settings. for offset := 0; ; { @@ -87,7 +109,7 @@ func (client Client) syncDirectionsOfKind( "Configure a supported sync setting in LaunchDarkly.", ).WithResource(id) } - result[id] = item.Direction + result[id] = syncdomain.Ownership{Direction: item.Direction, MatchedPattern: item.MatchedResourcePattern} } offset += len(page.Items) // The total count ends the paging when the response has one. Without diff --git a/internal/sync/api/settings_test.go b/internal/sync/api/settings_test.go index eb5a655f4..eedd8dcd1 100644 --- a/internal/sync/api/settings_test.go +++ b/internal/sync/api/settings_test.go @@ -130,3 +130,53 @@ func TestSyncDirectionsPagesWithoutATotalCount(t *testing.T) { require.Len(t, transport.Requests, 2, "a full page without a total count is followed by a request for the next page") assert.Equal(t, []string{"100"}, transport.Requests[1].Query["offset"]) } + +// ResolveOwnership keeps the stored rule pattern that matched each resource. +// A missing or empty pattern means that no rule is stored. +func TestResolveOwnershipReportsTheMatchedRule(t *testing.T) { + transport := &recordingClient{Responses: [][]byte{ + []byte(`{"items": [ + {"resourceKind":"variation","resourceLookupKey":"support/wild","direction":"code_canonical","matchedResourcePattern":"support/*"}, + {"resourceKind":"variation","resourceLookupKey":"support/absent","direction":"both"}, + {"resourceKind":"variation","resourceLookupKey":"support/empty","direction":"server_canonical","matchedResourcePattern":""} + ], "totalCount": 3}`), + }} + client := NewClient(transport, "token", "https://example.com") + wild := syncdomain.VariationID("project", "support", "wild") + absent := syncdomain.VariationID("project", "support", "absent") + empty := syncdomain.VariationID("project", "support", "empty") + + ownership, err := client.ResolveOwnership("project", []syncdomain.ResourceID{wild, absent, empty}) + + require.NoError(t, err) + assert.Equal(t, map[syncdomain.ResourceID]syncdomain.Ownership{ + wild: {Direction: syncdomain.SyncDirectionCodeCanonical, MatchedPattern: "support/*"}, + absent: {Direction: syncdomain.SyncDirectionBoth}, + empty: {Direction: syncdomain.SyncDirectionServerCanonical}, + }, ownership) +} + +// The endpoint accepts at most 100 resources per request, so a read of more +// asks for them 100 at a time and answers for all of them. +func TestResolveOwnershipOfMoreThan100ResourcesReadsThemInBatches(t *testing.T) { + ids := make([]syncdomain.ResourceID, 0, 101) + items := make([]string, 0, 101) + for index := range 101 { + key := fmt.Sprintf("support/v%03d", index) + ids = append(ids, syncdomain.VariationID("project", "support", fmt.Sprintf("v%03d", index))) + items = append(items, fmt.Sprintf(`{"resourceKind":"variation","resourceLookupKey":%q,"direction":"server_canonical"}`, key)) + } + transport := &recordingClient{Responses: [][]byte{ + []byte(`{"items": [` + strings.Join(items[:100], ",") + `], "totalCount": 100}`), + []byte(`{"items": [` + items[100] + `], "totalCount": 1}`), + }} + client := NewClient(transport, "token", "https://example.com") + + ownership, err := client.ResolveOwnership("project", ids) + + require.NoError(t, err) + assert.Len(t, ownership, 101) + require.Len(t, transport.Requests, 2) + assert.Len(t, transport.Requests[0].Query["resources"], 100) + assert.Equal(t, []string{"variation:support/v100"}, transport.Requests[1].Query["resources"]) +} diff --git a/internal/sync/error.go b/internal/sync/error.go index a256da44f..c1e2d310e 100644 --- a/internal/sync/error.go +++ b/internal/sync/error.go @@ -26,6 +26,8 @@ const ( ErrorLinkedSourceMissing ErrorCode = "linked_source_missing" ErrorAttachmentDefinitionMissing ErrorCode = "attachment_definition_missing" ErrorSyncSettingUnavailable ErrorCode = "sync_setting_unavailable" + ErrorSyncVariationForbidden ErrorCode = "sync_variation_forbidden" + ErrorVariationManagedInCode ErrorCode = "variation_managed_in_code" ErrorBindingStale ErrorCode = "binding_stale" ErrorStateChanged ErrorCode = "state_changed" ErrorSourceNotWritable ErrorCode = "source_not_writable" diff --git a/internal/sync/ownership.go b/internal/sync/ownership.go new file mode 100644 index 000000000..50a064918 --- /dev/null +++ b/internal/sync/ownership.go @@ -0,0 +1,7 @@ +package sync + +// ManagedInLaunchDarklyMessage tells the user that LaunchDarkly owns a +// variation and how to move it to code. +func ManagedInLaunchDarklyMessage(id ResourceID) string { + return id.LookupKey + " is managed in LaunchDarkly; pulled, not pushed. To manage it in code, switch it to Code in LaunchDarkly." +} diff --git a/internal/sync/ownership_test.go b/internal/sync/ownership_test.go new file mode 100644 index 000000000..00ea55158 --- /dev/null +++ b/internal/sync/ownership_test.go @@ -0,0 +1,34 @@ +package sync + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestOwnershipManagedInLaunchDarkly(t *testing.T) { + tests := map[string]struct { + ownership Ownership + want bool + }{ + "an explicit LaunchDarkly rule": { + ownership: Ownership{Direction: SyncDirectionServerCanonical, MatchedPattern: "config/variation"}, + want: true, + }, + "a wildcard LaunchDarkly rule": { + ownership: Ownership{Direction: SyncDirectionServerCanonical, MatchedPattern: "config/*"}, + want: true, + }, + "the default with no rule": { + ownership: Ownership{Direction: SyncDirectionServerCanonical}, + }, + "a code rule": { + ownership: Ownership{Direction: SyncDirectionCodeCanonical, MatchedPattern: "config/variation"}, + }, + } + for name, test := range tests { + t.Run(name, func(t *testing.T) { + assert.Equal(t, test.want, test.ownership.ManagedInLaunchDarkly()) + }) + } +} diff --git a/internal/sync/prompt/acceptance_test.go b/internal/sync/prompt/acceptance_test.go index babd464bf..1f577d85b 100644 --- a/internal/sync/prompt/acceptance_test.go +++ b/internal/sync/prompt/acceptance_test.go @@ -43,7 +43,12 @@ type directAPI struct { skills map[string]versionedSkill manifest *syncapi.SyncManifest directions map[string]syncdomain.SyncDirection - requests []string + // patterns is the matchedResourcePattern of each resource, such as + // "variation:support/default". A resource without one matched no rule. + patterns map[string]string + // settingsReadErr fails each read of the sync settings. + settingsReadErr error + requests []string // expandTools makes each read of the variation add the content of its // tools to model.parameters.tools, as LaunchDarkly does. expandTools bool @@ -59,6 +64,8 @@ type directAPI struct { manifestWriteErr error // toolWriteErr fails each update of a tool. toolWriteErr error + // variationWriteErr fails each update of a variation. + variationWriteErr error } type notFoundAPI struct{} @@ -104,6 +111,9 @@ func (api *directAPI) MakeRequest( if api.onRequest != nil { api.onRequest(method, path) } + if api.variationWriteErr != nil && method == http.MethodPatch && strings.Contains(path, "/ai-configs/support/variations/") { + return nil, api.variationWriteErr + } if strings.HasSuffix(path, "/configs/sync/manifests") { if api.manifestWriteErr != nil && method != http.MethodGet { return nil, api.manifestWriteErr @@ -111,6 +121,9 @@ func (api *directAPI) MakeRequest( return handleManifestRequest(api.manifest, method, query, body) } if strings.HasSuffix(path, "/configs/sync/settings") && method == http.MethodGet { + if api.settingsReadErr != nil { + return nil, api.settingsReadErr + } items := make([]map[string]any, 0, len(query["resources"])) for _, resource := range query["resources"] { kind, lookupKey, _ := strings.Cut(resource, ":") @@ -118,9 +131,13 @@ func (api *directAPI) MakeRequest( if configured := api.directions[resource]; configured != "" { direction = configured } - items = append(items, map[string]any{ + item := map[string]any{ "resourceKind": kind, "resourceLookupKey": lookupKey, "direction": direction, - }) + } + if pattern := api.patterns[resource]; pattern != "" { + item["matchedResourcePattern"] = pattern + } + items = append(items, item) } return json.Marshal(map[string]any{"items": items}) } @@ -2027,7 +2044,7 @@ support = Agent(instructions="Local instructions.") variation: &server, directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionCodeCanonical, - }, + }, patterns: map[string]string{"variation:support/default": "support/default"}, } id := syncdomain.VariationID("production", "support", "default") @@ -2296,6 +2313,7 @@ func TestCompletionSourcePullsOneSystemMessage(t *testing.T) { directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } id := syncdomain.VariationID("production", "support", "default") @@ -2338,6 +2356,7 @@ completion = Agent( directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } id := syncdomain.VariationID("production", "support", "default") action := syncprompt.SourceLinkAction{ @@ -2537,6 +2556,7 @@ func TestStructuredSourceLinksPushAndPull(t *testing.T) { changeServer: func(api *directAPI) { api.variation.Instructions = "Pulled YAML prompt" api.directions["variation:support/default"] = syncdomain.SyncDirectionServerCanonical + api.patterns = map[string]string{"variation:support/default": "support/default"} }, wantPush: func(t *testing.T, api *directAPI) { assert.Equal(t, "Local YAML prompt", api.variation.Instructions) @@ -2559,6 +2579,7 @@ func TestStructuredSourceLinksPushAndPull(t *testing.T) { changeServer: func(api *directAPI) { api.variation.Instructions = "Pulled TOML prompt" api.directions["variation:support/default"] = syncdomain.SyncDirectionServerCanonical + api.patterns = map[string]string{"variation:support/default": "support/default"} }, wantPush: func(t *testing.T, api *directAPI) { assert.Equal(t, "Local TOML prompt", api.variation.Instructions) @@ -2718,6 +2739,7 @@ func TestMarkdownProviderProfilesPullAndPreserveMetadata(t *testing.T) { directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } id := syncdomain.VariationID("production", "support", "default") @@ -2936,7 +2958,9 @@ func (api *ambiguousWriteAPI) MakeRequest( beta bool, ) ([]byte, error) { response, err := api.directAPI.MakeRequest(accessToken, method, path, contentType, query, body, beta) - if err == nil && (method == "POST" || method == "PATCH") { + // The ambiguity is about the write of the variation, not about the claim + // of its ownership. + if err == nil && (method == "POST" || method == "PATCH") && !strings.HasSuffix(path, "/configs/sync/settings") { return nil, fmt.Errorf("connection closed after write") } return response, err @@ -2956,7 +2980,8 @@ func (api *changingReadAPI) MakeRequest( body []byte, beta bool, ) ([]byte, error) { - if method == "GET" && !strings.Contains(path, "/configs/sync/manifests") { + if method == "GET" && !strings.Contains(path, "/configs/sync/manifests") && + !strings.Contains(path, "/configs/sync/settings") { api.reads++ if api.reads == 2 { api.variation = pointer(variation("Concurrent")) @@ -3017,6 +3042,20 @@ func (api *multiDirectAPI) MakeRequest( if strings.HasSuffix(path, "/configs/sync/manifests") { return handleManifestRequest(api.manifest, method, query, body) } + if strings.HasSuffix(path, "/configs/sync/settings") { + // No resource matches a rule, and a claim succeeds. + if method != http.MethodGet { + return nil, nil + } + items := make([]map[string]any, 0, len(query["resources"])) + for _, resource := range query["resources"] { + kind, lookupKey, _ := strings.Cut(resource, ":") + items = append(items, map[string]any{ + "resourceKind": kind, "resourceLookupKey": lookupKey, "direction": syncdomain.SyncDirectionBoth, + }) + } + return json.Marshal(map[string]any{"items": items}) + } if method == "GET" { variations := make([]syncdomain.Variation, 0, len(api.variations)) for _, variation := range api.variations { @@ -3378,6 +3417,7 @@ func TestServerCanonicalSourcePullRepinsStaleToolVersions(t *testing.T) { directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } stdout, _, err := runPrompt(t, root, api, "--dry-run", "--output", "json") @@ -3412,6 +3452,7 @@ func TestLinkedPullRepinsOnTheLatestLaunchDarklyCopy(t *testing.T) { directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } onNthRead(api, "/ai-configs/support", 3, func() { api.variation.Name = "Renamed after review" }) @@ -3443,6 +3484,7 @@ func TestLinkedPullRecordsTheLockBeforeAFailedPinRefresh(t *testing.T) { directions: map[string]syncdomain.SyncDirection{ "variation:support/default": syncdomain.SyncDirectionServerCanonical, }, + patterns: map[string]string{"variation:support/default": "support/default"}, } pulled := server.Instructions // The mapped field changes after the review, so the pin update stops. diff --git a/internal/sync/prompt/conflict_test.go b/internal/sync/prompt/conflict_test.go index cc6a7c7ba..37885f125 100644 --- a/internal/sync/prompt/conflict_test.go +++ b/internal/sync/prompt/conflict_test.go @@ -504,11 +504,20 @@ func (api *conflictAPI) MakeRequest( method string, path string, _ string, - _ url.Values, + query url.Values, body []byte, _ bool, ) ([]byte, error) { api.requests = append(api.requests, method) + if strings.HasSuffix(path, "/configs/sync/settings") && method == "GET" { + // Every resource resolves to the default setting, with no rule. + items := make([]map[string]any, 0, len(query["resources"])) + for _, resource := range query["resources"] { + kind, lookupKey, _ := strings.Cut(resource, ":") + items = append(items, map[string]any{"resourceKind": kind, "resourceLookupKey": lookupKey, "direction": "both"}) + } + return json.Marshal(map[string]any{"items": items}) + } if strings.Contains(path, "/ai-tools/") { if method == "PATCH" { var tool syncdomain.Tool diff --git a/internal/sync/prompt/create.go b/internal/sync/prompt/create.go index a15c96988..d0d41f394 100644 --- a/internal/sync/prompt/create.go +++ b/internal/sync/prompt/create.go @@ -177,6 +177,10 @@ func (runner Runner) createResource( return err } reviewed := workspaceState{projectKeys: []string{row.ID.ProjectKey}, plan: Plan{Scope: options.Scope, Resources: []PlannedResource{row}}} + // The review sees the ownership that the apply reads again. + if err := applySourceDirections(client, reviewed.plan.Resources); err != nil { + return err + } if options.dryRun() { if err := writePlanOutput(options.Output, options.OutputKind, reviewed.plan, options.Diff); err != nil { return err diff --git a/internal/sync/prompt/execute.go b/internal/sync/prompt/execute.go index cd29c04da..5c463485a 100644 --- a/internal/sync/prompt/execute.go +++ b/internal/sync/prompt/execute.go @@ -755,6 +755,26 @@ func applyServerChange( if writeErr == nil { return pinsOf(variation), nil } + if syncapi.IsForbidden(writeErr) { + // The sync lock rejects a caller who holds the permission, so the + // message from LaunchDarkly is the cause, not the role. + if syncapi.ResponseCode(writeErr) == "managed_in_code" { + problem := syncdomain.NewProblem( + syncdomain.ErrorVariationManagedInCode, + fmt.Sprintf("LaunchDarkly rejected the write of variation %q of AI Config %q because it is managed in code", variationKey, configKey), + "Run sync from the repository that LaunchDarkly records as the source of this variation.", + ).WithResource(resource.ID) + problem.Cause = writeErr + return nil, problem + } + problem := syncdomain.NewProblem( + syncdomain.ErrorSyncVariationForbidden, + fmt.Sprintf("your role cannot write variation %q of AI Config %q in project %q", variationKey, configKey, projectKey), + fmt.Sprintf("Ask a LaunchDarkly admin to grant your role the syncAIConfigVariation action on the AI Config proj/%s:env/*:aiconfig/%s.", projectKey, configKey), + ).WithResource(resource.ID) + problem.Cause = writeErr + return nil, problem + } createConflict := resource.Action == ActionCreateServer && syncapi.IsConflict(writeErr) if !syncapi.MutationMayHaveSucceeded(writeErr) && !createConflict { return nil, writeErr diff --git a/internal/sync/prompt/execute_test.go b/internal/sync/prompt/execute_test.go index 4ab64c8fa..03f890e69 100644 --- a/internal/sync/prompt/execute_test.go +++ b/internal/sync/prompt/execute_test.go @@ -998,3 +998,35 @@ func TestSettledRowKeepsTheProjectionOfALinkedTool(t *testing.T) { assert.NotNil(t, result.Local.Attachments[1].Projection, "the linked tool keeps its projection for the publish") assert.Nil(t, result.Local.Attachments[1].Tool.CustomParameters) } + +// lockedMutationAPI answers every write with the 403 that the sync lock sends +// for a variation that code owns, with the given message. +type lockedMutationAPI struct{ message string } + +func (api lockedMutationAPI) MakeRequest(_ string, method string, _ string, _ string, _ url.Values, _ []byte, _ bool) ([]byte, error) { + if method == "PATCH" { + body, _ := json.Marshal(map[string]any{"code": "managed_in_code", "message": api.message, "statusCode": 403}) + return nil, errors.New(string(body)) + } + return []byte(`{}`), nil +} + +func (lockedMutationAPI) MakeUnauthenticatedRequest(string, string, []byte) ([]byte, error) { + return nil, nil +} + +// A write that the sync lock rejects is a variation_managed_in_code problem +// about that variation, not a missing permission. +func TestApplyServerChangeReportsALockRejectionForTheVariation(t *testing.T) { + variation := testVariation("local") + resource := PlannedResource{ID: testResourceID(), Action: ActionUpdateServer, Local: &variation} + client := syncapi.NewClient(lockedMutationAPI{message: "X is managed in code."}, "token", "https://example.com") + + _, err := applyServerChange(client, newAttachmentResolver(client), resource) + + problem, ok := syncdomain.ProblemFrom(err) + require.True(t, ok, "err: %v", err) + assert.Equal(t, syncdomain.ErrorVariationManagedInCode, problem.Code) + assert.Contains(t, problem.Message, "because it is managed in code") + assert.Equal(t, resource.ID.Label(), problem.Resource) +} diff --git a/internal/sync/prompt/output.go b/internal/sync/prompt/output.go index 5f1627faf..1780d0d6a 100644 --- a/internal/sync/prompt/output.go +++ b/internal/sync/prompt/output.go @@ -123,7 +123,23 @@ func writePlanOutput(out io.Writer, outputKind string, plan Plan, showDiffs bool } resources = append(resources, item) } - return writeJSON(out, map[string]any{"resources": resources}) + return writeJSON(out, jsonDocument(resources, plan)) +} + +// jsonDocument is the top-level JSON document: the resources, plus notes such +// as a push skipped because the variation is managed in LaunchDarkly. +func jsonDocument(resources any, plan Plan) map[string]any { + document := map[string]any{"resources": resources} + var notes []string + for _, resource := range plan.Resources { + if resource.showsManagedNote() { + notes = append(notes, syncdomain.ManagedInLaunchDarklyMessage(resource.ID)) + } + } + if len(notes) > 0 { + document["notes"] = notes + } + return document } // writeOutcomeOutput writes the result of each action, which includes @@ -152,7 +168,7 @@ func writeOutcomeOutput(out io.Writer, outputKind string, plan Plan, outcomes [] Create: skillCreate(planned[outcome.ID]), Source: plannedSource(planned[outcome.ID]), }) } - return writeJSON(out, map[string]any{"resources": resources}) + return writeJSON(out, jsonDocument(resources, plan)) } var rows []reviewRow @@ -277,6 +293,9 @@ func writePlanNotes(out io.Writer, plan Plan) { "Note: Another working copy synced %s after your sync.lock. %s\n", resource.ID.Label(), staleSuggestion(resource), ) } + if resource.showsManagedNote() { + _ = console.Printf("Note: %s\n", syncdomain.ManagedInLaunchDarklyMessage(resource.ID)) + } } var counts []string diff --git a/internal/sync/prompt/ownership_test.go b/internal/sync/prompt/ownership_test.go new file mode 100644 index 000000000..2a7fc23ff --- /dev/null +++ b/internal/sync/prompt/ownership_test.go @@ -0,0 +1,302 @@ +package prompt_test + +import ( + "encoding/json" + "errors" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + syncdomain "github.com/launchdarkly/ldcli/internal/sync" + synclocal "github.com/launchdarkly/ldcli/internal/sync/local" + syncprompt "github.com/launchdarkly/ldcli/internal/sync/prompt" +) + +// requireNoSettingsWrite checks that sync never wrote the sync settings: +// LaunchDarkly records code as the owner from the synced manifest. +func requireNoSettingsWrite(t *testing.T, requests []string) { + t.Helper() + assert.Zero(t, countRequests(requests, http.MethodPatch, "/configs/sync/settings"), "no sync settings write") +} + +// managedVariationWithLocalChange returns a working copy that tracks the +// managed variation support/default, with a local change to push. +func managedVariationWithLocalChange(t *testing.T) (string, *directAPI) { + t.Helper() + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, variation("Local"), true) + writeManifest(t, root, baseline) + return root, &directAPI{variation: pointer(baseline)} +} + +// linkPrompt links prompt.md to support/default and pushes the local copy. +func linkPrompt(t *testing.T, root string, api *directAPI) (string, error) { + t.Helper() + _, notes, err := runPrompt(t, root, api, "link", "--file", "prompt.md", "--format", "markdown", + "--project", "production", "--config-key", "support", "--key", "default", + "--conflict", "local", "--yes", "--no-input") + return notes, err +} + +const ( + supportDefaultSetting = "variation:support/default" + managedMessage = "support/default is managed in LaunchDarkly; pulled, not pushed. " + + "To manage it in code, switch it to Code in LaunchDarkly." +) + +func managedInLaunchDarkly(api *directAPI) { + api.directions = map[string]syncdomain.SyncDirection{supportDefaultSetting: syncdomain.SyncDirectionServerCanonical} + api.patterns = map[string]string{supportDefaultSetting: "support/default"} +} + +// A variation that no rule matches resolves to the server_canonical default, +// which does not make it pull: the push writes it, and writes no rule. +func TestPushOfAVariationWithoutARuleWritesNoSyncSettings(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + + _, _, err := runPrompt(t, root, api, "--yes") + + require.NoError(t, err) + requireNoSettingsWrite(t, api.requests) + assert.Equal(t, 1, countRequests(api.requests, http.MethodPatch, "/variations/default")) + assert.Equal(t, "Local", api.variation.Name) +} + +// A variation that code owns is pushed. +func TestPushOfACodeOwnedVariation(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.directions = map[string]syncdomain.SyncDirection{supportDefaultSetting: syncdomain.SyncDirectionCodeCanonical} + api.patterns = map[string]string{supportDefaultSetting: "support/default"} + + _, _, err := runPrompt(t, root, api, "--yes") + + require.NoError(t, err) + requireNoSettingsWrite(t, api.requests) + assert.Equal(t, 1, countRequests(api.requests, http.MethodPatch, "/variations/default")) + assert.Equal(t, "Local", api.variation.Name) +} + +// A variation with an explicit LaunchDarkly rule is never pushed, +// and the user learns why: on stderr, and as a notes entry in --output json. +func TestPushSkipsAVariationManagedInLaunchDarkly(t *testing.T) { + for _, arguments := range [][]string{{"--yes"}, {"--yes", "--output", "json"}} { + t.Run(strings.Join(arguments, " "), func(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + managedInLaunchDarkly(api) + + stdout, stderr, err := runPrompt(t, root, api, arguments...) + + require.NoError(t, err) + assert.Contains(t, stderr, managedMessage) + if len(arguments) > 1 { + assert.Contains(t, jsonNotes(t, stdout), managedMessage, "stdout: %s", stdout) + } + assert.Zero(t, countRequests(api.requests, http.MethodPatch, "/variations/default"), "no push") + assert.Equal(t, "Baseline", api.variation.Name) + }) + } +} + +// jsonNotes returns the notes of every JSON document in stdout. +func jsonNotes(t *testing.T, stdout string) []string { + t.Helper() + var notes []string + decoder := json.NewDecoder(strings.NewReader(stdout)) + for { + var document struct { + Notes []string `json:"notes"` + } + err := decoder.Decode(&document) + if errors.Is(err, io.EOF) { + return notes + } + require.NoError(t, err, "stdout is JSON: %s", stdout) + notes = append(notes, document.Notes...) + } +} + +// A variation with an explicit LaunchDarkly rule still pulls the changes made +// in LaunchDarkly. +func TestPullOfAVariationManagedInLaunchDarkly(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, baseline, false) + writeManifest(t, root, baseline) + api := &directAPI{variation: pointer(variation("Server"))} + managedInLaunchDarkly(api) + + _, _, err := runPrompt(t, root, api, "--yes") + + require.NoError(t, err) + compiled, err := synclocal.Compile(os.DirFS(root)) + require.NoError(t, err) + require.Len(t, compiled, 1) + assert.Equal(t, "Server", compiled[0].Variation.Name) + requireNoSettingsWrite(t, api.requests) +} + +// D2: linked variations follow ownership too. A server_canonical direction +// that no rule produced is not a LaunchDarkly owner, so a linked variation is +// pushed. +func TestPushOfALinkedVariationWithoutARule(t *testing.T) { + root, api := linkedVariationForPush(t) + api.directions[supportDefaultSetting] = syncdomain.SyncDirectionServerCanonical + + _, _, err := runPrompt(t, root, api, "--yes") + + require.NoError(t, err) + requireNoSettingsWrite(t, api.requests) + assert.Equal(t, "Local text", api.variation.Instructions) +} + +// A read of the sync settings that fails with an error other than 404 stops +// the push before any variation write. +func TestPushStopsWhenTheSyncSettingsReadFails(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.settingsReadErr = errors.New(`{"code":"internal","statusCode":500}`) + + _, _, err := runPrompt(t, root, api, "--yes") + + require.Error(t, err) + assert.Zero(t, countRequests(api.requests, http.MethodPatch, "/variations/default"), "no variation write") + assert.Equal(t, "Baseline", api.variation.Name) +} + +// A sync settings read that answers without an entry for a variation (here +// the 400 for an unsupported kind) fails closed: no push. +func TestPushFailsClosedWhenTheSyncSettingIsMissing(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.settingsReadErr = errors.New(`{"code":"invalid_request","message":"unsupported resource kind","statusCode":400}`) + + _, _, err := runPrompt(t, root, api, "--yes") + + require.Error(t, err) + assert.Contains(t, err.Error(), "LaunchDarkly returned no sync setting") + assert.Contains(t, err.Error(), string(syncdomain.ErrorSyncSettingUnavailable)) + assert.Zero(t, countRequests(api.requests, http.MethodPatch, "/variations/default"), "no push") + assert.Equal(t, "Baseline", api.variation.Name) +} + +// Without the sync settings API, there is no ownership to read, so the push +// goes ahead as before, with no warning. +func TestPushWithoutTheSettingsAPIPushes(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.settingsReadErr = errors.New(`{"code":"not_found","message":"Not found","statusCode":404}`) + + _, notes, err := runScoped(t, root, api, syncprompt.Options{ + Scope: syncdomain.ScopePrompts, Action: syncprompt.SyncAction{}, Yes: true, + }) + + require.NoError(t, err) + assert.NotContains(t, notes, "ownership") + assert.Equal(t, 1, countRequests(api.requests, http.MethodPatch, "/variations/default")) + assert.Equal(t, "Local", api.variation.Name) +} + +// A push of a code-owned variation that the server lock rejects because the +// role lacks syncAIConfigVariation names that action, not a generic error. +func TestPromptPushNamesSyncAIConfigVariationWhenTheVariationWriteIsForbidden(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.directions = map[string]syncdomain.SyncDirection{supportDefaultSetting: syncdomain.SyncDirectionCodeCanonical} + api.patterns = map[string]string{supportDefaultSetting: "support/default"} + api.variationWriteErr = errors.New(`{"code":"forbidden","statusCode":403}`) + + _, _, err := runPrompt(t, root, api, "--yes", "--output=plaintext") + + require.Error(t, err) + assert.Equal(t, 1, countRequests(api.requests, http.MethodPatch, "/ai-configs/support/variations/default"), "the variation write is tried") + requireNoSettingsWrite(t, api.requests) + assert.Contains(t, err.Error(), "syncAIConfigVariation") + assert.Contains(t, err.Error(), "proj/production:env/*:aiconfig/support", "names the AI Config resource as roles use it") +} + +// A link of a LaunchDarkly variation that no rule matches pushes it and +// writes no rule. +func TestLinkOfAVariationWithoutARule(t *testing.T) { + root := initRepository(t) + require.NoError(t, os.WriteFile(filepath.Join(root, "prompt.md"), []byte("Local prompt.\n"), 0o644)) + api := &directAPI{variation: pointer(variation("Server"))} + + _, err := linkPrompt(t, root, api) + + require.NoError(t, err) + requireNoSettingsWrite(t, api.requests) + assert.Equal(t, "Local prompt.", api.variation.Instructions) +} + +// A link of a variation with an explicit LaunchDarkly rule does not push it, +// and the user learns why. +func TestLinkLeavesAVariationManagedInLaunchDarkly(t *testing.T) { + root := initRepository(t) + require.NoError(t, os.WriteFile(filepath.Join(root, "prompt.md"), []byte("Local prompt.\n"), 0o644)) + api := &directAPI{variation: pointer(variation("Server"))} + managedInLaunchDarkly(api) + + notes, err := linkPrompt(t, root, api) + + require.NoError(t, err) + assert.Contains(t, notes, "support/default is managed in LaunchDarkly") + assert.Zero(t, countRequests(api.requests, http.MethodPatch, "/variations/default"), "no push") + assert.Equal(t, "Help", api.variation.Instructions) +} + +// A 403 that the server lock sends because code owns the variation is not a +// missing permission: the error passes on what LaunchDarkly said instead of +// asking for syncAIConfigVariation. +func TestPromptPushDoesNotBlameTheRoleWhenTheLockRejectsTheWrite(t *testing.T) { + root, api := managedVariationWithLocalChange(t) + api.directions = map[string]syncdomain.SyncDirection{supportDefaultSetting: syncdomain.SyncDirectionCodeCanonical} + api.patterns = map[string]string{supportDefaultSetting: "support/default"} + api.variationWriteErr = errors.New(`{"code":"managed_in_code","message":"Variation default in AI Config support is managed in code, so LaunchDarkly rejected this change.","statusCode":403}`) + + _, _, err := runPrompt(t, root, api, "--yes", "--output=plaintext") + + require.Error(t, err) + assert.NotContains(t, err.Error(), "syncAIConfigVariation", "the caller holds the permission; the lock is the cause") + assert.Contains(t, err.Error(), "is managed in code") +} + +// A create that an explicit LaunchDarkly rule blocks is an error, not a pull, +// so it gets no "pulled, not pushed" note. +func TestCreateBlockedByALaunchDarklyRuleHasNoManagedNote(t *testing.T) { + root := initRepository(t) + writeVariation(t, root, variation("New"), true) + api := &directAPI{} + api.directions = map[string]syncdomain.SyncDirection{supportDefaultSetting: syncdomain.SyncDirectionServerCanonical} + api.patterns = map[string]string{supportDefaultSetting: "support/*"} + + _, stderr, err := runPrompt(t, root, api, "--yes") + + require.Error(t, err) + assert.Contains(t, err.Error(), "server-canonical resource does not exist in LaunchDarkly") + assert.NotContains(t, stderr, "pulled, not pushed") +} + +// A pull of a variation that LaunchDarkly owns, which a changed tool then +// turns into a conflict, is not pulled, so it gets no "pulled, not pushed" +// note. +func TestManagedVariationTurnedIntoAConflictHasNoManagedNote(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeManifest(t, root, baseline) + trackTool(t, root, searchTool("old")) + local := baseline + local.Tools = []syncdomain.AttachmentRef{{Key: "search"}} + local.Attachments = []syncdomain.Attachment{toolAttachment(searchTool("edited"), 0)} + writeVariation(t, root, local, true) + api := &directAPI{variation: pointer(baseline), tools: map[string]versionedTool{"search": {Tool: searchTool("new"), Version: 2}}} + managedInLaunchDarkly(api) + + stdout, stderr, _ := runPrompt(t, root, api, "--dry-run", "--output", "json") + + assert.Contains(t, planRows(t, stdout), "variation support/default conflict") + assert.NotContains(t, stdout, "pulled, not pushed") + assert.NotContains(t, stderr, "pulled, not pushed") +} diff --git a/internal/sync/prompt/plan.go b/internal/sync/prompt/plan.go index 3c3e19bea..db2a8d6e7 100644 --- a/internal/sync/prompt/plan.go +++ b/internal/sync/prompt/plan.go @@ -101,6 +101,17 @@ type PlannedResource struct { // createsConfig is true when the create of the variation also creates // its config, because LaunchDarkly has none. createsConfig bool + // managedInLaunchDarkly is true when an explicit LaunchDarkly rule turned + // a push of the variation into a pull or a skip. Use showsManagedNote to + // know whether the final plan row still deserves the note. + managedInLaunchDarkly bool +} + +// showsManagedNote reports whether the row ends up pulled or skipped because +// of a LaunchDarkly rule. A row that later planning turned into an error or a +// conflict has its own message instead. +func (r PlannedResource) showsManagedNote() bool { + return r.managedInLaunchDarkly && r.Action != ActionError && r.Action != ActionConflict } func setProblem(resource *PlannedResource, problem *syncdomain.Problem) { diff --git a/internal/sync/prompt/state.go b/internal/sync/prompt/state.go index 9130e531e..7e06dba1b 100644 --- a/internal/sync/prompt/state.go +++ b/internal/sync/prompt/state.go @@ -251,10 +251,18 @@ func allowRelink(resources []PlannedResource, id ResourceID) { } } +// applySourceDirections applies the LaunchDarkly sync settings to the rows. +// A variation that an explicit LaunchDarkly rule owns pulls, and any other +// variation may push: LaunchDarkly records code as its owner once the +// variation is in a synced manifest. A linked tool or skill follows its +// direction. func applySourceDirections(client syncapi.Client, resources []PlannedResource) error { + if err := applyVariationOwnership(client, resources); err != nil { + return err + } byProject := make(map[string][]ResourceID) for _, resource := range resources { - if resource.Source != nil { + if resource.Source != nil && resource.ID.Kind != syncdomain.KindVariation { byProject[resource.ID.ProjectKey] = append(byProject[resource.ID.ProjectKey], resource.ID) } } @@ -278,17 +286,13 @@ func applySourceDirections(client syncapi.Client, resources []PlannedResource) e continue } if !ok { - suggestion := "Configure the resource sync setting in LaunchDarkly, then run link again." - if resource.ID.Kind != syncdomain.KindVariation { - suggestion = fmt.Sprintf( - "LaunchDarkly does not provide a %s sync setting. Configure one when support is available, or run detach.", - resource.ID.Kind, - ) - } return syncdomain.NewProblem( syncdomain.ErrorSyncSettingUnavailable, "LaunchDarkly returned no sync setting", - suggestion, + fmt.Sprintf( + "LaunchDarkly does not provide a %s sync setting. Configure one when support is available, or run detach.", + resource.ID.Kind, + ), ).WithResource(resource.ID) } enforceDirection(resource, direction) @@ -298,6 +302,65 @@ func applySourceDirections(client syncapi.Client, resources []PlannedResource) e return nil } +// applyVariationOwnership reads the ownership of every variation row and +// applies it. Without the sync settings API, the rows keep their actions. A +// variation that the API answers without an entry for fails closed, before +// anything is written, unless it is a create of a variation LaunchDarkly does +// not have yet. +func applyVariationOwnership(client syncapi.Client, resources []PlannedResource) error { + byProject := make(map[string][]ResourceID) + for _, resource := range resources { + if resource.ID.Kind == syncdomain.KindVariation { + byProject[resource.ID.ProjectKey] = append(byProject[resource.ID.ProjectKey], resource.ID) + } + } + for projectKey, ids := range byProject { + ownership, err := client.ResolveOwnership(projectKey, ids) + unavailable := syncapi.IsNotFound(err) + if err != nil && !unavailable { + return err + } + for index := range resources { + resource := &resources[index] + if resource.ID.Kind != syncdomain.KindVariation || resource.ID.ProjectKey != projectKey { + continue + } + if unavailable { + enforceDirection(resource, syncdomain.SyncDirectionBoth) + continue + } + owner, ok := ownership[resource.ID] + // A create with nothing in LaunchDarkly yet has no rule to read. + if !ok && !(resource.Action == ActionCreateServer && !resource.hasServer()) { + return syncdomain.NewProblem( + syncdomain.ErrorSyncSettingUnavailable, + "LaunchDarkly returned no sync setting", + "Configure the resource sync setting in LaunchDarkly, then run sync again.", + ).WithResource(resource.ID) + } + enforceOwnership(resource, owner) + } + } + return nil +} + +// enforceOwnership applies the ownership of one variation. An explicit +// LaunchDarkly rule makes it server-canonical, and code_canonical makes it +// code-canonical, whether a rule or a synced manifest produced it. A +// variation without a rule may move either way: the server_canonical default +// of the settings API does not make it pull. +func enforceOwnership(resource *PlannedResource, ownership syncdomain.Ownership) { + switch { + case ownership.ManagedInLaunchDarkly(): + resource.managedInLaunchDarkly = resource.Action.changesServer() || resource.Action == ActionConflict + enforceDirection(resource, syncdomain.SyncDirectionServerCanonical) + case ownership.Direction == syncdomain.SyncDirectionCodeCanonical: + enforceDirection(resource, syncdomain.SyncDirectionCodeCanonical) + default: + enforceDirection(resource, syncdomain.SyncDirectionBoth) + } +} + func enforceDirection(resource *PlannedResource, direction syncdomain.SyncDirection) { resource.Direction = direction // A conflict over a shared tool or skill is about that attachment, not diff --git a/internal/sync/resource.go b/internal/sync/resource.go index b80743366..1c3e1f6ce 100644 --- a/internal/sync/resource.go +++ b/internal/sync/resource.go @@ -38,6 +38,27 @@ func (direction SyncDirection) Valid() bool { direction == SyncDirectionBoth } +// Ownership is the effective LaunchDarkly sync setting of one resource, and +// the stored rule pattern that produced it. MatchedPattern is empty when no +// stored rule matched. +type Ownership struct { + Direction SyncDirection + MatchedPattern string +} + +// HasRule reports whether a stored rule matched the resource. +func (o Ownership) HasRule() bool { + return o.MatchedPattern != "" +} + +// ManagedInLaunchDarkly reports whether an explicit LaunchDarkly rule owns +// the resource. The server_canonical default of a resource without a rule +// does not count: LaunchDarkly makes that resource code-owned once it is in a +// synced manifest. +func (o Ownership) ManagedInLaunchDarkly() bool { + return o.HasRule() && o.Direction == SyncDirectionServerCanonical +} + // ResourceID identifies one synchronized resource. // // For a variation, LookupKey is "config-key/variation-key". For a tool or a