From fcbeed1734d634ce1f660f9f0e7b70ae4c9eb9d1 Mon Sep 17 00:00:00 2001 From: Clifford Tawiah Date: Wed, 18 Nov 2026 15:13:41 -0500 Subject: [PATCH] fix(sync): stop an attach early when the project has no tools or skills (AIC-3646) An interactive attach asked for the project and the variation, and only then searched LaunchDarkly for tools or skills. When the project had none, the user learned that at the last step. The attach now reads one page of the catalog right after the project step. When the project has no tool or skill of the kind, it stops with an error that names the project, before the variation prompt. An attach with a named key skips the check, because the read of that key gives its own error. Co-Authored-By: Claude Fable 5.1 --- internal/sync/prompt/acceptance_test.go | 47 +++++++++++++++++++++++++ internal/sync/prompt/attach.go | 20 +++++++++++ 2 files changed, 67 insertions(+) diff --git a/internal/sync/prompt/acceptance_test.go b/internal/sync/prompt/acceptance_test.go index ed5267d0c..7c9e9272c 100644 --- a/internal/sync/prompt/acceptance_test.go +++ b/internal/sync/prompt/acceptance_test.go @@ -167,6 +167,20 @@ func (api *directAPI) MakeRequest( } return nil, api.createErr } + if strings.HasSuffix(path, "/ai-tools") && method == http.MethodGet { + items := make([]versionedTool, 0, len(api.tools)) + for _, key := range slices.Sorted(maps.Keys(api.tools)) { + items = append(items, api.tools[key]) + } + return json.Marshal(map[string]any{"items": items, "totalCount": len(items)}) + } + if strings.HasSuffix(path, "/ai-configs/skills") && method == http.MethodGet { + items := make([]versionedSkill, 0, len(api.skills)) + for _, key := range slices.Sorted(maps.Keys(api.skills)) { + items = append(items, api.skills[key]) + } + return json.Marshal(map[string]any{"items": items, "totalCount": len(items)}) + } if strings.HasSuffix(path, "/ai-tools") && method == http.MethodPost { var tool versionedTool if err := json.Unmarshal(body, &tool.Tool); err != nil { @@ -652,6 +666,39 @@ func TestPromptAttachesLatestSkillAsMarkdownFile(t *testing.T) { assert.Equal(t, "---\nkey: support\ndescription: Customer support guidance\n---\n\nFollow the support process.\n", string(content)) } +// An attach stops before the variation step when the project has no tool, +// so the user does not answer every prompt only to find an empty list. +func TestPromptAttachToolStopsWhenTheProjectHasNoTools(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, baseline, true) + writeManifest(t, root, baseline) + api := &directAPI{variation: pointer(baseline), tools: map[string]versionedTool{}} + + _, _, err := runPrompt(t, root, api, "attach", "tool", "--yes", "--no-input") + + require.Error(t, err) + assert.Contains(t, err.Error(), `project "production" has no tools to attach. Create one in LaunchDarkly first.`) + assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-tools"), "one read of the tool list") + assert.Empty(t, api.variation.Tools, "nothing was attached") +} + +// The same stop applies to skills. +func TestPromptAttachSkillStopsWhenTheProjectHasNoSkills(t *testing.T) { + root := initRepository(t) + baseline := variation("Baseline") + writeVariation(t, root, baseline, true) + writeManifest(t, root, baseline) + api := &directAPI{variation: pointer(baseline), skills: map[string]versionedSkill{}} + + _, _, err := runPrompt(t, root, api, "attach", "skill", "--yes", "--no-input") + + require.Error(t, err) + assert.Contains(t, err.Error(), `project "production" has no skills to attach. Create one in LaunchDarkly first.`) + assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-configs/skills"), "one read of the skill list") + assert.Empty(t, api.variation.Skills, "nothing was attached") +} + func TestPromptRejectsSkillAttachmentForCompletionVariation(t *testing.T) { root := initRepository(t) completion := variation("Completion") diff --git a/internal/sync/prompt/attach.go b/internal/sync/prompt/attach.go index 491183e62..e10bfa666 100644 --- a/internal/sync/prompt/attach.go +++ b/internal/sync/prompt/attach.go @@ -39,6 +39,12 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att if err != nil { return err } + // A named key reads one resource, and a missing one gets its own error. + if options.Key == "" { + if err := requireAttachments(client, projectKey, options.Kind); err != nil { + return err + } + } target, err := selectManagedVariation(variations, projectKey, options) if err != nil { return err @@ -75,6 +81,20 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att return err } +// requireAttachments stops an attach that has no key when the project has no +// tool or skill of the kind. It runs before the variation step, so that the +// user does not answer every prompt only to find an empty list at the end. +func requireAttachments(client syncapi.Client, projectKey string, kind syncdomain.AttachmentKind) error { + page, err := client.SearchAttachments(projectKey, kind, "", 1, 0) + if err != nil { + return err + } + if len(page.Items) == 0 && page.TotalCount == 0 { + return fmt.Errorf("project %q has no %ss to attach. Create one in LaunchDarkly first.", projectKey, kind) + } + return nil +} + // localAttachment reads the local file of an attachment. It returns nil when // the attachment has no local file. func localAttachment(repositoryRoot string, id ResourceID) (*syncdomain.Attachment, error) {