Skip to content
Open
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
17 changes: 17 additions & 0 deletions internal/sync/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
52 changes: 37 additions & 15 deletions internal/sync/api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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; ; {
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions internal/sync/api/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
2 changes: 2 additions & 0 deletions internal/sync/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions internal/sync/ownership.go
Original file line number Diff line number Diff line change
@@ -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."
}
34 changes: 34 additions & 0 deletions internal/sync/ownership_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}
54 changes: 48 additions & 6 deletions internal/sync/prompt/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -104,23 +111,33 @@ 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
}
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, ":")
direction := syncdomain.SyncDirectionBoth
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})
}
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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"))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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" })

Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading