|
| 1 | +// Copyright 2026, Pulumi Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package neo |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/pulumi/pulumi/pkg/v3/backend" |
| 24 | + "github.com/pulumi/pulumi/pkg/v3/backend/httpstate" |
| 25 | +) |
| 26 | + |
| 27 | +// debugKind identifies which failed operation a debug session targets. The values double as the |
| 28 | +// noun used in the seed prompt, so callers can format a debugKind directly. |
| 29 | +type debugKind string |
| 30 | + |
| 31 | +const ( |
| 32 | + debugNone debugKind = "" |
| 33 | + debugUpdate debugKind = "update" |
| 34 | + debugPreview debugKind = "preview" |
| 35 | +) |
| 36 | + |
| 37 | +// latestID returns the id of the stack's most recent operation of this kind, or "" when none is |
| 38 | +// available. Updates and previews are looked up separately (previews never appear in GetHistory): |
| 39 | +// an update resolves to its history version, a preview to its opaque UpdateID. Best-effort. |
| 40 | +func (k debugKind) latestID(ctx context.Context, be httpstate.Backend, stackRef backend.StackReference) string { |
| 41 | + if stackRef == nil { |
| 42 | + return "" |
| 43 | + } |
| 44 | + switch k { |
| 45 | + case debugUpdate: |
| 46 | + if updates, err := be.GetHistory(ctx, stackRef, 1, 1); err == nil && len(updates) > 0 { |
| 47 | + return strconv.Itoa(updates[0].Version) |
| 48 | + } |
| 49 | + case debugPreview: |
| 50 | + if p, err := be.GetLatestStackPreview(ctx, stackRef); err == nil && p != nil { |
| 51 | + return p.UpdateID |
| 52 | + } |
| 53 | + case debugNone: |
| 54 | + // Not a debug session; nothing to look up. |
| 55 | + } |
| 56 | + return "" |
| 57 | +} |
| 58 | + |
| 59 | +// buildDebugPrompt assembles the full initial prompt for a debug session: the seed trigger line |
| 60 | +// (so Neo's skill evaluator loads the debugging skill) followed by the stack context. An empty id |
| 61 | +// is resolved to the latest operation of kind; any userPrompt sits between the two as extra |
| 62 | +// guidance. Every lookup is best-effort, so the prompt is always well-formed. |
| 63 | +func buildDebugPrompt( |
| 64 | + ctx context.Context, be httpstate.Backend, |
| 65 | + target taskTarget, kind debugKind, id, userPrompt string, |
| 66 | +) string { |
| 67 | + if id == "" { |
| 68 | + id = kind.latestID(ctx, be, target.ref) |
| 69 | + } |
| 70 | + seed := debugSeedPrompt(kind, id) |
| 71 | + if userPrompt != "" { |
| 72 | + seed += "\n\n" + userPrompt |
| 73 | + } |
| 74 | + return seed + "\n\n" + debugStackContext(be, target, kind, id) |
| 75 | +} |
| 76 | + |
| 77 | +// debugSeedPrompt builds Neo's initial prompt for a debug session. It is a short trigger line, not |
| 78 | +// a procedure: Neo's skill evaluator matches it and loads the debugging skill. With no id it targets |
| 79 | +// the most recent operation of that kind; with an id, that specific run. |
| 80 | +func debugSeedPrompt(kind debugKind, id string) string { |
| 81 | + if id == "" { |
| 82 | + return fmt.Sprintf( |
| 83 | + "Debug my most recent Pulumi %s on this stack and fix it directly in this working directory.\n", |
| 84 | + kind) |
| 85 | + } |
| 86 | + return fmt.Sprintf( |
| 87 | + "Debug the failed Pulumi %s %s of this stack and fix it directly in this working directory.\n", |
| 88 | + kind, id) |
| 89 | +} |
| 90 | + |
| 91 | +// debugStackContext builds a block describing the debug session's org, user, project, stack, and |
| 92 | +// resolved operation, appended to the seed prompt so Neo starts with the failure in context. Every |
| 93 | +// field is best-effort: anything empty or unavailable is omitted. |
| 94 | +func debugStackContext(be httpstate.Backend, target taskTarget, kind debugKind, id string) string { |
| 95 | + var b strings.Builder |
| 96 | + b.WriteString("Context for this debug session:\n") |
| 97 | + if target.org != "" { |
| 98 | + fmt.Fprintf(&b, "- Organization: %s\n", target.org) |
| 99 | + } |
| 100 | + if user, _, _, err := be.CurrentUser(); err == nil && user != "" { |
| 101 | + fmt.Fprintf(&b, "- User: %s\n", user) |
| 102 | + } |
| 103 | + if target.project != "" { |
| 104 | + fmt.Fprintf(&b, "- Project: %s\n", target.project) |
| 105 | + } |
| 106 | + if name := target.stackName(); name != "" { |
| 107 | + fmt.Fprintf(&b, "- Stack: %s\n", name) |
| 108 | + } |
| 109 | + if id != "" { |
| 110 | + fmt.Fprintf(&b, "- Debugging: %s %s\n", kind, id) |
| 111 | + } |
| 112 | + return b.String() |
| 113 | +} |
0 commit comments