Skip to content
Merged
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
81 changes: 35 additions & 46 deletions pkg/cli/codemod_agent_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,60 +40,49 @@ func getAgentTaskToAgentSessionCodemod() Codemod {
return content, false, nil
}

// Parse frontmatter to get raw lines
frontmatterLines, markdown, err := parseFrontmatterLines(content)
if err != nil {
return content, false, err
}

// Find and replace create-agent-task with create-agent-session within the safe-outputs block
var modified bool
var inSafeOutputsBlock bool
var safeOutputsIndent string

result := make([]string, len(frontmatterLines))

for i, line := range frontmatterLines {
trimmedLine := strings.TrimSpace(line)

// Track if we're in the safe-outputs block
if strings.HasPrefix(trimmedLine, "safe-outputs:") {
inSafeOutputsBlock = true
safeOutputsIndent = getIndentation(line)
result[i] = line
continue
}
newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) {
var modified bool
var inSafeOutputsBlock bool
var safeOutputsIndent string
result := make([]string, len(lines))
for i, line := range lines {
trimmedLine := strings.TrimSpace(line)

// Track if we're in the safe-outputs block
if strings.HasPrefix(trimmedLine, "safe-outputs:") {
inSafeOutputsBlock = true
safeOutputsIndent = getIndentation(line)
result[i] = line
continue
}

// Check if we've left the safe-outputs block
if inSafeOutputsBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, safeOutputsIndent) {
inSafeOutputsBlock = false
// Check if we've left the safe-outputs block
if inSafeOutputsBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, safeOutputsIndent) {
inSafeOutputsBlock = false
}
}
}

// Replace create-agent-task with create-agent-session if in safe-outputs block
if inSafeOutputsBlock && strings.HasPrefix(trimmedLine, "create-agent-task:") {
replacedLine, didReplace := findAndReplaceInLine(line, "create-agent-task", "create-agent-session")
if didReplace {
result[i] = replacedLine
modified = true
agentSessionCodemodLog.Printf("Replaced safe-outputs.create-agent-task with safe-outputs.create-agent-session on line %d", i+1)
// Replace create-agent-task with create-agent-session if in safe-outputs block
if inSafeOutputsBlock && strings.HasPrefix(trimmedLine, "create-agent-task:") {
replacedLine, didReplace := findAndReplaceInLine(line, "create-agent-task", "create-agent-session")
if didReplace {
result[i] = replacedLine
modified = true
agentSessionCodemodLog.Printf("Replaced safe-outputs.create-agent-task with safe-outputs.create-agent-session on line %d", i+1)
} else {
result[i] = line
}
} else {
result[i] = line
}
} else {
result[i] = line
}
return result, modified
})
if applied {
agentSessionCodemodLog.Print("Applied create-agent-task to create-agent-session migration")
}

if !modified {
return content, false, nil
}

// Reconstruct the content
newContent := reconstructContent(result, markdown)
agentSessionCodemodLog.Print("Applied create-agent-task to create-agent-session migration")
return newContent, true, nil
return newContent, applied, err
},
}
}
109 changes: 50 additions & 59 deletions pkg/cli/codemod_assign_to_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,76 +51,67 @@ func getAssignToAgentDefaultAgentCodemod() Codemod {
return content, false, nil
}

// Parse frontmatter to get raw lines
frontmatterLines, markdown, err := parseFrontmatterLines(content)
if err != nil {
return content, false, err
}

var modified bool
var inSafeOutputsBlock bool
var safeOutputsIndent string
var inAssignToAgentBlock bool
var assignToAgentIndent string

result := make([]string, len(frontmatterLines))

for i, line := range frontmatterLines {
trimmedLine := strings.TrimSpace(line)

// Track if we're in the safe-outputs block
if strings.HasPrefix(trimmedLine, "safe-outputs:") {
inSafeOutputsBlock = true
safeOutputsIndent = getIndentation(line)
result[i] = line
continue
}
newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) {
var modified bool
var inSafeOutputsBlock bool
var safeOutputsIndent string
var inAssignToAgentBlock bool
var assignToAgentIndent string
result := make([]string, len(lines))
for i, line := range lines {
trimmedLine := strings.TrimSpace(line)

// Track if we're in the safe-outputs block
if strings.HasPrefix(trimmedLine, "safe-outputs:") {
inSafeOutputsBlock = true
safeOutputsIndent = getIndentation(line)
result[i] = line
continue
}

// Check if we've left the safe-outputs block
if inSafeOutputsBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, safeOutputsIndent) {
inSafeOutputsBlock = false
inAssignToAgentBlock = false
// Check if we've left the safe-outputs block
if inSafeOutputsBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, safeOutputsIndent) {
inSafeOutputsBlock = false
inAssignToAgentBlock = false
}
}
}

// Track if we're in the assign-to-agent block within safe-outputs
if inSafeOutputsBlock && strings.HasPrefix(trimmedLine, "assign-to-agent:") {
inAssignToAgentBlock = true
assignToAgentIndent = getIndentation(line)
result[i] = line
continue
}
// Track if we're in the assign-to-agent block within safe-outputs
if inSafeOutputsBlock && strings.HasPrefix(trimmedLine, "assign-to-agent:") {
inAssignToAgentBlock = true
assignToAgentIndent = getIndentation(line)
result[i] = line
continue
}

// Check if we've left the assign-to-agent block
if inAssignToAgentBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, assignToAgentIndent) {
inAssignToAgentBlock = false
// Check if we've left the assign-to-agent block
if inAssignToAgentBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
if hasExitedBlock(line, assignToAgentIndent) {
inAssignToAgentBlock = false
}
}
}

// Replace default-agent with name if in assign-to-agent block
if inAssignToAgentBlock && strings.HasPrefix(trimmedLine, "default-agent:") {
replacedLine, didReplace := findAndReplaceInLine(line, "default-agent", "name")
if didReplace {
result[i] = replacedLine
modified = true
assignToAgentCodemodLog.Printf("Replaced safe-outputs.assign-to-agent.default-agent with safe-outputs.assign-to-agent.name on line %d", i+1)
// Replace default-agent with name if in assign-to-agent block
if inAssignToAgentBlock && strings.HasPrefix(trimmedLine, "default-agent:") {
replacedLine, didReplace := findAndReplaceInLine(line, "default-agent", "name")
if didReplace {
result[i] = replacedLine
modified = true
assignToAgentCodemodLog.Printf("Replaced safe-outputs.assign-to-agent.default-agent with safe-outputs.assign-to-agent.name on line %d", i+1)
} else {
result[i] = line
}
} else {
result[i] = line
}
} else {
result[i] = line
}
return result, modified
})
if applied {
assignToAgentCodemodLog.Print("Applied assign-to-agent default-agent to name migration")
}

if !modified {
return content, false, nil
}

newContent := reconstructContent(result, markdown)
assignToAgentCodemodLog.Print("Applied assign-to-agent default-agent to name migration")
return newContent, true, nil
return newContent, applied, err
},
}
}
52 changes: 12 additions & 40 deletions pkg/cli/codemod_bash_anonymous.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cli

import "github.com/github/gh-aw/pkg/logger"
import (
"strings"

"github.com/github/gh-aw/pkg/logger"
)

var bashAnonymousCodemodLog = logger.New("cli:codemod_bash_anonymous")

Expand Down Expand Up @@ -34,22 +38,11 @@ func getBashAnonymousRemovalCodemod() Codemod {
return content, false, nil
}

// Parse frontmatter to get raw lines
frontmatterLines, markdown, err := parseFrontmatterLines(content)
if err != nil {
return content, false, err
}

// Replace the bash field from anonymous to explicit true
modifiedLines, modified := replaceBashAnonymousWithTrue(frontmatterLines)
if !modified {
return content, false, nil
newContent, applied, err := applyFrontmatterLineTransform(content, replaceBashAnonymousWithTrue)
if applied {
bashAnonymousCodemodLog.Print("Applied bash anonymous removal, replaced with 'bash: true'")
}

// Reconstruct the content
newContent := reconstructContent(modifiedLines, markdown)
bashAnonymousCodemodLog.Print("Applied bash anonymous removal, replaced with 'bash: true'")
return newContent, true, nil
return newContent, applied, err
},
}
}
Expand All @@ -62,10 +55,7 @@ func replaceBashAnonymousWithTrue(lines []string) ([]string, bool) {
var toolsIndent string

for _, line := range lines {
trimmedLine := line

// Trim to check content but preserve original spacing
trimmed := trimLine(trimmedLine)
trimmed := strings.TrimSpace(line)

// Track if we're in the tools block
if trimmed == "tools:" {
Expand All @@ -76,14 +66,14 @@ func replaceBashAnonymousWithTrue(lines []string) ([]string, bool) {
}

// Check if we've left the tools block
if inToolsBlock && len(trimmed) > 0 && !startsWith(trimmed, "#") {
if inToolsBlock && len(trimmed) > 0 && !strings.HasPrefix(trimmed, "#") {
if hasExitedBlock(line, toolsIndent) {
inToolsBlock = false
}
}

// Replace bash: with bash: true if in tools block
if inToolsBlock && (trimmed == "bash:" || startsWith(trimmed, "bash: ")) {
if inToolsBlock && (trimmed == "bash:" || strings.HasPrefix(trimmed, "bash: ")) {
// Check if it's just 'bash:' with nothing after the colon
if trimmed == "bash:" {
indent := getIndentation(line)
Expand All @@ -99,21 +89,3 @@ func replaceBashAnonymousWithTrue(lines []string) ([]string, bool) {

return result, modified
}

// Helper function to trim whitespace
func trimLine(s string) string {
start := 0
for start < len(s) && (s[start] == ' ' || s[start] == '\t') {
start++
}
end := len(s)
for end > start && (s[end-1] == ' ' || s[end-1] == '\t' || s[end-1] == '\n' || s[end-1] == '\r') {
end--
}
return s[start:end]
}

// Helper function to check if string starts with prefix
func startsWith(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
Loading
Loading