Skip to content

Conversation

amondnet
Copy link

@amondnet amondnet commented Sep 11, 2025

This pull request refines and improves the scripts/update-agent-context.sh script, focusing on better handling of special characters in placeholders, more robust environment variable passing to the embedded Python script, and improved maintainability. The changes also fix quoting and escaping issues and ensure that Bash variables are properly referenced in both shell and Python code.

Shell script improvements:

  • Improved placeholder replacement: All sed commands now properly escape square brackets in placeholders (e.g., [PROJECT NAME]), ensuring accurate and reliable text substitution. [1] [2]
  • More robust extraction of plan details: The grep patterns for extracting information from the plan file now use escaped asterisks to match the markdown syntax more reliably.

Python integration and variable handling:

  • Environment variable passing: The script now exports all necessary variables before invoking the embedded Python script, and the Python code reads these using os.environ for improved clarity and reliability.
  • Correct Bash-to-Python variable referencing: All Bash variables in the Python code are replaced with their Python equivalents, preventing accidental literal string insertion and improving maintainability. [1] [2] [3]

General maintainability and correctness:

  • Consistent use of raw string delimiters in Python's heredoc invocation to avoid accidental variable expansion.
  • Improved regex patterns and file handling in the Python section for greater compatibility and correctness.

These changes collectively make the script more robust and easier to maintain, while fixing subtle bugs related to string escaping and variable usage.

fix #47
fix #154

@amondnet amondnet requested a review from localden as a code owner September 11, 2025 01:28
@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 01:28
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes issues with variable handling and escaping in the update-agent-context.sh script to improve reliability and maintainability. The script updates agent context files based on feature branch changes.

  • Escapes square brackets in sed placeholder replacements to prevent regex interpretation
  • Fixes Bash variable references within embedded Python code by using environment variables
  • Improves regex patterns for extracting markdown metadata from plan files

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 02:45
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 03:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

if "$NEW_LANG" and f"# {NEW_LANG}" not in content:
commands_section = re.search(r'## Commands\n\`\`\`bash\n(.*?)\n\`\`\`', content, re.DOTALL)
if new_lang and new_lang not in content:
commands_section = re.search(r'## Commands\n```bash\n(.*?)\n```', content, re.DOTALL)
Copy link
Preview

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, the regex patterns with backticks in raw strings should be escaped as r'## Commands\\n```bash\\n(.*?)\\n```' to ensure proper matching of markdown code blocks.

Copilot uses AI. Check for mistakes.

@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 03:28
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

if "$NEW_PROJECT_TYPE" == "web" and "frontend/" not in content:
struct_section = re.search(r'## Project Structure\n\`\`\`\n(.*?)\n\`\`\`', content, re.DOTALL)
if new_project_type == "web" and "frontend/" not in content:
struct_section = re.search(r'## Project Structure\n```\n(.*?)\n```', content, re.DOTALL)
Copy link
Preview

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex patterns for matching markdown code blocks are duplicated across multiple sections. Consider extracting these into reusable functions or constants to improve maintainability.

Copilot uses AI. Check for mistakes.

value=$(grep "^\\*\\*${field}\\*\\*: " "$plan_file" 2>/dev/null | head -1 | sed "s|^\\*\\*${field}\\*\\*: ||")
# Filter out "NEEDS CLARIFICATION" for all except Project Type
if [[ "$field" == "Storage" ]]; then
value=$(echo "$value" | grep -v "N/A" | grep -v "NEEDS CLARIFICATION")
Copy link
Preview

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using multiple grep commands with pipes creates unnecessary subprocesses. Consider using a single grep with alternation pattern: grep -v -E '(N/A|NEEDS CLARIFICATION)'.

Suggested change
value=$(echo "$value" | grep -v "N/A" | grep -v "NEEDS CLARIFICATION")
value=$(echo "$value" | grep -v -E 'N/A|NEEDS CLARIFICATION')

Copilot uses AI. Check for mistakes.

value=$(grep "^\\*\\*${field}\\*\\*: " "$plan_file" 2>/dev/null | head -1 | sed "s|^\\*\\*${field}\\*\\*: ||")
# Filter out "NEEDS CLARIFICATION" for all except Project Type
if [[ "$field" == "Storage" ]]; then
value=$(echo "$value" | grep -v "N/A" | grep -v "NEEDS CLARIFICATION")
Copy link
Preview

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using multiple grep commands with pipes creates unnecessary subprocesses. Consider using a single grep with alternation pattern: grep -v -E '(N/A|NEEDS CLARIFICATION)'.

Suggested change
value=$(echo "$value" | grep -v "N/A" | grep -v "NEEDS CLARIFICATION")
value=$(echo "$value" | grep -v -E 'N/A|NEEDS CLARIFICATION')

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

update-agent-context.sh seems to be failing on macOS Bug noticed when running scripts/update-agent-context.sh
2 participants