zrb 1.13.1__py3-none-any.whl → 1.21.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. zrb/__init__.py +2 -6
  2. zrb/attr/type.py +8 -8
  3. zrb/builtin/__init__.py +2 -0
  4. zrb/builtin/group.py +31 -15
  5. zrb/builtin/http.py +7 -8
  6. zrb/builtin/llm/attachment.py +40 -0
  7. zrb/builtin/llm/chat_session.py +130 -144
  8. zrb/builtin/llm/chat_session_cmd.py +226 -0
  9. zrb/builtin/llm/chat_trigger.py +73 -0
  10. zrb/builtin/llm/history.py +4 -4
  11. zrb/builtin/llm/llm_ask.py +218 -110
  12. zrb/builtin/llm/tool/api.py +74 -62
  13. zrb/builtin/llm/tool/cli.py +35 -16
  14. zrb/builtin/llm/tool/code.py +49 -47
  15. zrb/builtin/llm/tool/file.py +262 -251
  16. zrb/builtin/llm/tool/note.py +84 -0
  17. zrb/builtin/llm/tool/rag.py +25 -18
  18. zrb/builtin/llm/tool/sub_agent.py +29 -22
  19. zrb/builtin/llm/tool/web.py +135 -143
  20. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/entity/add_entity_util.py +7 -7
  21. zrb/builtin/project/add/fastapp/fastapp_template/my_app_name/_zrb/module/add_module_util.py +5 -5
  22. zrb/builtin/project/add/fastapp/fastapp_util.py +1 -1
  23. zrb/builtin/searxng/config/settings.yml +5671 -0
  24. zrb/builtin/searxng/start.py +21 -0
  25. zrb/builtin/setup/latex/ubuntu.py +1 -0
  26. zrb/builtin/setup/ubuntu.py +1 -1
  27. zrb/builtin/shell/autocomplete/bash.py +4 -3
  28. zrb/builtin/shell/autocomplete/zsh.py +4 -3
  29. zrb/config/config.py +255 -78
  30. zrb/config/default_prompt/file_extractor_system_prompt.md +109 -9
  31. zrb/config/default_prompt/interactive_system_prompt.md +24 -30
  32. zrb/config/default_prompt/persona.md +1 -1
  33. zrb/config/default_prompt/repo_extractor_system_prompt.md +31 -31
  34. zrb/config/default_prompt/repo_summarizer_system_prompt.md +27 -8
  35. zrb/config/default_prompt/summarization_prompt.md +8 -13
  36. zrb/config/default_prompt/system_prompt.md +36 -30
  37. zrb/config/llm_config.py +129 -24
  38. zrb/config/llm_context/config.py +127 -90
  39. zrb/config/llm_context/config_parser.py +1 -7
  40. zrb/config/llm_context/workflow.py +81 -0
  41. zrb/config/llm_rate_limitter.py +89 -45
  42. zrb/context/any_shared_context.py +7 -1
  43. zrb/context/context.py +8 -2
  44. zrb/context/shared_context.py +6 -8
  45. zrb/group/any_group.py +12 -5
  46. zrb/group/group.py +67 -3
  47. zrb/input/any_input.py +5 -1
  48. zrb/input/base_input.py +18 -6
  49. zrb/input/text_input.py +7 -24
  50. zrb/runner/cli.py +21 -20
  51. zrb/runner/common_util.py +24 -19
  52. zrb/runner/web_route/task_input_api_route.py +5 -5
  53. zrb/runner/web_route/task_session_api_route.py +1 -4
  54. zrb/runner/web_util/user.py +7 -3
  55. zrb/session/any_session.py +12 -6
  56. zrb/session/session.py +39 -18
  57. zrb/task/any_task.py +24 -3
  58. zrb/task/base/context.py +17 -9
  59. zrb/task/base/execution.py +15 -8
  60. zrb/task/base/lifecycle.py +8 -4
  61. zrb/task/base/monitoring.py +12 -7
  62. zrb/task/base_task.py +69 -5
  63. zrb/task/base_trigger.py +12 -5
  64. zrb/task/llm/agent.py +138 -52
  65. zrb/task/llm/config.py +45 -13
  66. zrb/task/llm/conversation_history.py +76 -6
  67. zrb/task/llm/conversation_history_model.py +0 -168
  68. zrb/task/llm/default_workflow/coding/workflow.md +41 -0
  69. zrb/task/llm/default_workflow/copywriting/workflow.md +68 -0
  70. zrb/task/llm/default_workflow/git/workflow.md +118 -0
  71. zrb/task/llm/default_workflow/golang/workflow.md +128 -0
  72. zrb/task/llm/default_workflow/html-css/workflow.md +135 -0
  73. zrb/task/llm/default_workflow/java/workflow.md +146 -0
  74. zrb/task/llm/default_workflow/javascript/workflow.md +158 -0
  75. zrb/task/llm/default_workflow/python/workflow.md +160 -0
  76. zrb/task/llm/default_workflow/researching/workflow.md +153 -0
  77. zrb/task/llm/default_workflow/rust/workflow.md +162 -0
  78. zrb/task/llm/default_workflow/shell/workflow.md +299 -0
  79. zrb/task/llm/file_replacement.py +206 -0
  80. zrb/task/llm/file_tool_model.py +57 -0
  81. zrb/task/llm/history_summarization.py +22 -35
  82. zrb/task/llm/history_summarization_tool.py +24 -0
  83. zrb/task/llm/print_node.py +182 -63
  84. zrb/task/llm/prompt.py +213 -153
  85. zrb/task/llm/tool_wrapper.py +210 -53
  86. zrb/task/llm/workflow.py +76 -0
  87. zrb/task/llm_task.py +98 -47
  88. zrb/task/make_task.py +2 -3
  89. zrb/task/rsync_task.py +25 -10
  90. zrb/task/scheduler.py +4 -4
  91. zrb/util/attr.py +50 -40
  92. zrb/util/cli/markdown.py +12 -0
  93. zrb/util/cli/text.py +30 -0
  94. zrb/util/file.py +27 -11
  95. zrb/util/{llm/prompt.py → markdown.py} +2 -3
  96. zrb/util/string/conversion.py +1 -1
  97. zrb/util/truncate.py +23 -0
  98. zrb/util/yaml.py +204 -0
  99. {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/METADATA +40 -20
  100. {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/RECORD +102 -79
  101. {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/WHEEL +1 -1
  102. zrb/task/llm/default_workflow/coding.md +0 -24
  103. zrb/task/llm/default_workflow/copywriting.md +0 -17
  104. zrb/task/llm/default_workflow/researching.md +0 -18
  105. {zrb-1.13.1.dist-info → zrb-1.21.17.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,153 @@
1
+ ---
2
+ description: "A workflow for finding, synthesizing, and analyzing information from various sources."
3
+ ---
4
+ Follow this workflow to deliver accurate, well-sourced, and synthesized research findings.
5
+
6
+ # Core Mandates
7
+
8
+ - **Unyielding Objectivity:** Report facts, identify biases, never inject opinions
9
+ - **Source Hierarchy:** Prioritize authoritative sources over secondary ones
10
+ - **Synthesis Excellence:** Connect information into coherent narratives
11
+ - **Comprehensive Attribution:** Cite all significant claims and data points
12
+
13
+ # Tool Usage Guideline
14
+ - Use `search_internet` for web research and information gathering
15
+ - Use `open_web_page` to read and analyze web content
16
+ - Use `read_from_file` to analyze provided documents and materials
17
+ - Use `write_to_file` to organize research findings and notes
18
+
19
+ # Step 1: Deconstruct and Strategize
20
+
21
+ 1. **Isolate Core Questions:** Break down the user's request into specific, answerable questions
22
+ 2. **Develop Search Strategy:** Create precise keywords, phrases, and boolean operators
23
+ 3. **Identify Source Types:** Determine what types of sources will be most valuable
24
+ 4. **Plan Research Approach:** Outline the sequence of research activities
25
+
26
+ # Step 2: Source Evaluation and Collection
27
+
28
+ ## Source Hierarchy (The 3 Tiers)
29
+
30
+ ### Tier 1 (Ground Truth)
31
+ - Official documentation and specifications
32
+ - Peer-reviewed academic papers
33
+ - Direct source code and technical specifications
34
+ - Primary legal/government documents
35
+ - Statistical data from authoritative sources
36
+
37
+ ### Tier 2 (Reputable Reporting)
38
+ - Established news organizations
39
+ - Respected industry publications
40
+ - Books by credible authors and experts
41
+ - Conference proceedings from reputable organizations
42
+
43
+ ### Tier 3 (Qualified Sources)
44
+ - Blog posts by known experts
45
+ - Conference talks and presentations
46
+ - Forum answers from high-reputation users
47
+ - **Always qualify these sources** (e.g., "According to a senior engineer at Google...")
48
+
49
+ ## Research Execution
50
+ 1. **Iterative Searching:** Start with best queries, refine based on results
51
+ 2. **Cross-Reference:** Verify information across multiple sources
52
+ 3. **Source Vetting:** Quickly assess source credibility and potential biases
53
+ 4. **Information Extraction:** Pull out relevant facts, figures, and arguments
54
+
55
+ # Step 3: Information Synthesis
56
+
57
+ 1. **Identify Patterns:** Look for consensus, debates, or evolving understanding
58
+ 2. **Connect Dots:** Find relationships between different pieces of information
59
+ 3. **Identify Gaps:** Note where information is missing or contradictory
60
+ 4. **Structure Findings:** Organize information logically for presentation
61
+
62
+ # Step 4: Analysis and Reporting
63
+
64
+ ## Report Structure
65
+ - **Bottom Line Up Front (BLUF):** Start with direct, concise answer to main question
66
+ - **Supporting Evidence:** Present synthesized facts and data supporting the BLUF
67
+ - **Methodology:** Explain research approach and sources used
68
+ - **Nuance and Limitations:** Acknowledge conflicting information or data gaps
69
+ - **Citations:** Provide clear attribution for all significant claims
70
+
71
+ ## Quality Standards
72
+ - **Accuracy:** Verify all facts and figures before inclusion
73
+ - **Clarity:** Present complex information in accessible language
74
+ - **Completeness:** Address all aspects of the original question
75
+ - **Objectivity:** Present balanced view without personal bias
76
+
77
+ # Step 5: Task-Specific Execution
78
+
79
+ ## Comparative Analysis
80
+ - **Structured Comparison:** Use tables or detailed bullet points
81
+ - **Like-for-Like Basis:** Compare items on equivalent criteria
82
+ - **Implication Analysis:** Explain what differences mean in practice
83
+ - **Objective Assessment:** Avoid subjective preferences
84
+
85
+ ## Technical Research
86
+ - **Documentation Analysis:** Read and interpret technical specifications
87
+ - **Code Examination:** Analyze source code when available
88
+ - **Performance Data:** Include benchmarks and performance characteristics
89
+ - **Compatibility Analysis:** Consider integration and compatibility factors
90
+
91
+ ## Market/Industry Research
92
+ - **Market Size:** Include relevant statistics and growth projections
93
+ - **Competitive Landscape:** Analyze key players and their positions
94
+ - **Trend Analysis:** Identify emerging patterns and future directions
95
+ - **Regulatory Environment:** Consider legal and compliance factors
96
+
97
+ # Step 6: Verification and Quality Control
98
+
99
+ 1. **Fact Checking:** Verify all critical information across multiple sources
100
+ 2. **Bias Assessment:** Identify and acknowledge potential biases in sources
101
+ 3. **Logical Consistency:** Ensure conclusions follow logically from evidence
102
+ 4. **Citation Verification:** Confirm all citations are accurate and accessible
103
+
104
+ # Step 7: Final Presentation
105
+
106
+ ## Formatting Standards
107
+ - **Clear Headings:** Use descriptive section headings
108
+ - **Bulleted Lists:** Present information in scannable format
109
+ - **Citations:** Use consistent format (e.g., `[Source Name](URL)`)
110
+ - **Visual Organization:** Use tables, charts, or diagrams when helpful
111
+
112
+ ## Delivery Considerations
113
+ - **Executive Summary:** Include high-level findings for quick understanding
114
+ - **Detailed Analysis:** Provide comprehensive information for deep dives
115
+ - **Actionable Insights:** Highlight implications and recommended actions
116
+ - **Further Reading:** Suggest additional resources for interested readers
117
+
118
+ # Risk Assessment Guidelines
119
+
120
+ ## Low Risk (Proceed Directly)
121
+ - Researching publicly available information
122
+ - Synthesizing information from authoritative sources
123
+ - Creating comparative analyses of established products
124
+
125
+ ## Moderate Risk (Explain and Confirm)
126
+ - Research involving proprietary or sensitive information
127
+ - Analysis that could have legal or compliance implications
128
+ - Research requiring access to paid or restricted sources
129
+
130
+ ## High Risk (Refuse and Explain)
131
+ - Research involving personal or private information
132
+ - Analysis that could violate terms of service
133
+ - Activities that could be considered hacking or unauthorized access
134
+
135
+ # Best Practices
136
+
137
+ ## Research Ethics
138
+ - **Respect Copyright:** Properly attribute all sources
139
+ - **Avoid Plagiarism:** Always cite and paraphrase appropriately
140
+ - **Respect Privacy:** Never research personal or private information
141
+ - **Maintain Integrity:** Report findings honestly, even if unexpected
142
+
143
+ ## Efficiency Techniques
144
+ - **Source Tracking:** Keep organized notes of sources and findings
145
+ - **Template Usage:** Use consistent formats for similar research tasks
146
+ - **Tool Proficiency:** Master research tools and techniques
147
+ - **Time Management:** Allocate research time based on question complexity
148
+
149
+ ## Continuous Improvement
150
+ - **Learn from Results:** Analyze what research approaches work best
151
+ - **Update Methods:** Stay current with new research tools and techniques
152
+ - **Expand Knowledge:** Build expertise in relevant domains
153
+ - **Share Insights:** Document successful research strategies
@@ -0,0 +1,162 @@
1
+ ---
2
+ description: "A workflow for developing with Rust, including project analysis and best practices."
3
+ ---
4
+ Follow this workflow to deliver safe, efficient, and idiomatic Rust code that respects project conventions.
5
+
6
+ # Core Mandates
7
+
8
+ - **Memory Safety:** Leverage Rust's ownership system for safe code
9
+ - **Zero-Cost Abstractions:** Use Rust's features without runtime overhead
10
+ - **Idiomatic Patterns:** Follow established Rust conventions and practices
11
+ - **Tool Integration:** Use Cargo and Rust tooling effectively
12
+
13
+ # Tool Usage Guideline
14
+ - Use `read_from_file` to analyze Cargo.toml and source code
15
+ - Use `search_files` to find Rust patterns and conventions
16
+ - Use `run_shell_command` for Cargo operations
17
+ - Use `list_files` to understand project structure
18
+
19
+ # Step 1: Project Analysis
20
+
21
+ 1. **Crate Information:** Examine `Cargo.toml` for dependencies, features, and workspace members
22
+ 2. **Toolchain Version:** Check `rust-toolchain.toml` for Rust version and components
23
+ 3. **Formatting Configuration:** Look for `rustfmt.toml` or `.rustfmt.toml`
24
+ 4. **Linting Configuration:** Check `clippy.toml` or `[lints]` section in `Cargo.toml`
25
+ 5. **Module Structure:** Analyze `src/lib.rs`, `src/main.rs`, and `src/bin/` directory
26
+ 6. **Workspace Configuration:** Check for workspace members in `Cargo.toml`
27
+
28
+ # Step 2: Understand Conventions
29
+
30
+ 1. **Formatting:** `cargo fmt` is mandatory for all code
31
+ 2. **Linting:** `cargo clippy` must pass with project configuration
32
+ 3. **Safety:** Avoid `unsafe` blocks unless absolutely necessary and documented
33
+ 4. **Error Handling:** Follow project's error handling strategy (anyhow, thiserror, etc.)
34
+ 5. **Testing:** Use established test patterns and organization
35
+
36
+ # Step 3: Implementation Planning
37
+
38
+ 1. **Type Design:** Plan structs, enums, and traits based on project patterns
39
+ 2. **Ownership Strategy:** Consider ownership, borrowing, and lifetime requirements
40
+ 3. **Error Handling:** Plan appropriate error types and handling
41
+ 4. **Concurrency:** Consider async/await or threading requirements
42
+ 5. **Testing Strategy:** Plan comprehensive unit and integration tests
43
+
44
+ # Step 4: Write Code
45
+
46
+ ## Code Quality Standards
47
+ - **Formatting:** All code must be `cargo fmt` compliant
48
+ - **Linting:** Address all `cargo clippy` warnings
49
+ - **Documentation:** Add rustdoc comments for public APIs
50
+ - **Naming:** Follow Rust naming conventions (snake_case for variables/functions, PascalCase for types)
51
+ - **Safety:** Write safe Rust without unnecessary `unsafe` blocks
52
+
53
+ ## Rust Idioms
54
+ - **Result and Option:** Use for error and optional value handling
55
+ - **Ownership System:** Leverage borrowing and lifetimes correctly
56
+ - **Iterators:** Prefer iterator methods over manual loops
57
+ - **Pattern Matching:** Use `match` and `if let` for control flow
58
+ - **Traits:** Use for polymorphism and code organization
59
+
60
+ # Step 5: Testing and Verification
61
+
62
+ 1. **Write Tests:** Create comprehensive tests using `#[cfg(test)]` modules
63
+ 2. **Run Tests:** Execute `cargo test` to verify functionality
64
+ 3. **Format Code:** Run `cargo fmt` to ensure proper formatting
65
+ 4. **Lint Code:** Run `cargo clippy -- -D warnings` to catch issues
66
+ 5. **Build Verification:** Run `cargo check` for fast compilation checking
67
+
68
+ # Step 6: Quality Assurance
69
+
70
+ ## Testing Standards
71
+ - **Unit Tests:** Place in `#[cfg(test)]` modules within source files
72
+ - **Integration Tests:** Create in `tests/` directory for crate-level testing
73
+ - **Documentation Tests:** Include examples in rustdoc comments
74
+ - **Property Testing:** Use proptest or quickcheck for comprehensive testing
75
+
76
+ ## Code Review Checklist
77
+ - [ ] Code follows project formatting standards
78
+ - [ ] All tests pass with good coverage
79
+ - [ ] No clippy warnings
80
+ - [ ] Error handling is appropriate and idiomatic
81
+ - [ ] Documentation is complete for public APIs
82
+ - [ ] Performance considerations addressed
83
+ - [ ] Memory safety verified
84
+
85
+ # Step 7: Cargo Operations
86
+
87
+ ## Development Commands
88
+ - `cargo check`: Fast compilation checking
89
+ - `cargo build`: Build the project
90
+ - `cargo run`: Build and run the project
91
+ - `cargo test`: Run all tests
92
+ - `cargo fmt`: Format code
93
+ - `cargo clippy`: Run linter
94
+
95
+ ## Dependency Management
96
+ - `cargo add <dependency>`: Add a new dependency
97
+ - `cargo update`: Update dependencies
98
+ - `cargo tree`: Show dependency tree
99
+ - `cargo audit`: Check for security vulnerabilities
100
+
101
+ # Step 8: Advanced Rust Features
102
+
103
+ ## Async/Await
104
+ - **Async Runtime:** Use tokio, async-std, or smol based on project
105
+ - **Futures:** Understand and use futures appropriately
106
+ - **Concurrency:** Use channels and synchronization primitives
107
+
108
+ ## Macros
109
+ - **Declarative Macros:** Use for code generation when appropriate
110
+ - **Procedural Macros:** Use for advanced metaprogramming
111
+ - **Attribute Macros:** Use for annotations and code transformation
112
+
113
+ ## Unsafe Code (Use Sparingly)
114
+ - **FFI:** For foreign function interface
115
+ - **Memory Management:** For low-level memory operations
116
+ - **Performance Optimization:** Only when measurements justify
117
+ - **Documentation:** Must document safety invariants
118
+
119
+ # Step 9: Finalize and Deliver
120
+
121
+ 1. **Verify Dependencies:** Ensure dependency versions are appropriate
122
+ 2. **Run Full Test Suite:** Verify all existing tests still pass
123
+ 3. **Security Audit:** Run `cargo audit` for security vulnerabilities
124
+ 4. **Performance Testing:** Benchmark critical code paths if applicable
125
+ 5. **Documentation:** Generate and verify rustdoc documentation
126
+
127
+ # Common Patterns
128
+
129
+ ## Error Handling
130
+ - **thiserror:** For defining custom error types
131
+ - **anyhow:** For application-level error handling
132
+ - **Result Propagation:** Use `?` operator appropriately
133
+
134
+ ## Concurrency
135
+ - **std::thread:** For CPU-bound parallelism
136
+ - **tokio::spawn:** For async task spawning
137
+ - **std::sync:** For synchronization primitives
138
+ - **crossbeam:** For advanced concurrency patterns
139
+
140
+ ## Performance
141
+ - **Profiling:** Use perf, flamegraph, or cargo instruments
142
+ - **Benchmarking:** Use criterion for reliable benchmarks
143
+ - **Optimization:** Focus on measured bottlenecks
144
+
145
+ # Risk Assessment Guidelines
146
+
147
+ ## Low Risk (Proceed Directly)
148
+ - Adding tests to existing test modules
149
+ - Implementing utility functions following established patterns
150
+ - Creating new modules in established patterns
151
+
152
+ ## Moderate Risk (Explain and Confirm)
153
+ - Modifying core data structures
154
+ - Changing public trait implementations
155
+ - Adding new dependencies
156
+ - Using `unsafe` blocks
157
+
158
+ ## High Risk (Refuse and Explain)
159
+ - Breaking memory safety guarantees
160
+ - Modifying critical system components
161
+ - Changes affecting multiple crates in workspace
162
+ - Operations that could introduce security vulnerabilities
@@ -0,0 +1,299 @@
1
+ ---
2
+ description: "A workflow for writing robust, safe, and maintainable shell scripts."
3
+ ---
4
+ Follow this workflow to create reliable, secure, and well-structured shell scripts.
5
+
6
+ # Core Mandates
7
+
8
+ - **Safety First:** Prevent common shell scripting pitfalls and security issues
9
+ - **Robustness:** Handle errors gracefully and predictably
10
+ - **Portability:** Write scripts that work across different environments
11
+ - **Maintainability:** Create readable, well-documented scripts
12
+
13
+ # Tool Usage Guideline
14
+ - Use `read_from_file` to analyze existing shell scripts and configurations
15
+ - Use `run_shell_command` to test shell scripts and commands
16
+ - Use `write_to_file` to create new shell scripts
17
+ - Use `search_files` to find shell patterns and conventions
18
+
19
+ # Step 1: Script Foundation
20
+
21
+ ## Non-Negotiable Boilerplate
22
+ Every script MUST start with this header. No exceptions.
23
+
24
+ ```bash
25
+ #!/usr/bin/env bash
26
+
27
+ # Exit on error, exit on unset variables, and exit on pipe fails.
28
+ set -euo pipefail
29
+
30
+ # Set IFS to default and save for restoration
31
+ OLD_IFS="$IFS"
32
+ IFS=$'\n\t'
33
+
34
+ # Ensure proper cleanup on exit
35
+ trap 'IFS="$OLD_IFS"' EXIT
36
+ ```
37
+
38
+ ## Additional Safety Options
39
+ ```bash
40
+ # Treat unset variables as errors
41
+ set -u
42
+
43
+ # Exit on any error
44
+ set -e
45
+
46
+ # Exit on pipe failure
47
+ set -o pipefail
48
+
49
+ # Make sure to fail on command substitution
50
+ shopt -s inherit_errexit
51
+ ```
52
+
53
+ # Step 2: Script Structure Planning
54
+
55
+ 1. **Define Purpose:** Clearly understand what the script should accomplish
56
+ 2. **Identify Dependencies:** Determine required commands and tools
57
+ 3. **Plan Error Handling:** Design comprehensive error handling strategy
58
+ 4. **Structure Functions:** Plan modular function organization
59
+ 5. **Consider Portability:** Ensure compatibility with target environments
60
+
61
+ # Step 3: Implementation Standards
62
+
63
+ ## Variable Safety
64
+ - **Quote Everything:** Always use `"$variable"` instead of `$variable`
65
+ - **Local Variables:** Use `local` in functions to avoid global scope pollution
66
+ - **Constants:** Use `readonly` for values that shouldn't change
67
+ - **Arrays:** Use arrays for lists instead of strings with spaces
68
+
69
+ ## Function Definitions
70
+ ```bash
71
+ # Standard function template
72
+ function my_function() {
73
+ local arg1="$1"
74
+ local arg2="$2"
75
+
76
+ # Function logic here
77
+ echo "Processing: $arg1, $arg2"
78
+ }
79
+ ```
80
+
81
+ ## Error Handling Patterns
82
+ ```bash
83
+ # Check command existence
84
+ if ! command -v required_command &> /dev/null; then
85
+ echo "Error: required_command not found" >&2
86
+ exit 1
87
+ fi
88
+
89
+ # Handle command failures
90
+ important_command || {
91
+ echo "Error: important_command failed" >&2
92
+ exit 1
93
+ }
94
+
95
+ # Temporary file handling
96
+ tmp_file=$(mktemp)
97
+ trap 'rm -f "$tmp_file"' EXIT
98
+ ```
99
+
100
+ # Step 4: Write Script Components
101
+
102
+ ## Standard Script Structure
103
+ ```bash
104
+ #!/usr/bin/env bash
105
+ set -euo pipefail
106
+
107
+ # Configuration section
108
+ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
109
+ readonly SCRIPT_NAME="$(basename "$0")"
110
+
111
+ # Function definitions
112
+ function usage() {
113
+ cat << EOF
114
+ Usage: $SCRIPT_NAME [OPTIONS]
115
+
116
+ Description of what the script does.
117
+
118
+ OPTIONS:
119
+ -h, --help Show this help message
120
+ -v, --verbose Enable verbose output
121
+ -f, --file FILE Specify input file
122
+
123
+ EXAMPLES:
124
+ $SCRIPT_NAME -f input.txt
125
+ $SCRIPT_NAME --verbose
126
+
127
+ EOF
128
+ }
129
+
130
+ function log() {
131
+ echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >&2
132
+ }
133
+
134
+ function error() {
135
+ echo "[ERROR] $*" >&2
136
+ exit 1
137
+ }
138
+
139
+ function main() {
140
+ local verbose=false
141
+ local input_file=""
142
+
143
+ # Parse command line arguments
144
+ while [[ $# -gt 0 ]]; do
145
+ case $1 in
146
+ -h|--help)
147
+ usage
148
+ exit 0
149
+ ;;
150
+ -v|--verbose)
151
+ verbose=true
152
+ ;;
153
+ -f|--file)
154
+ input_file="$2"
155
+ shift
156
+ ;;
157
+ *)
158
+ error "Unknown option: $1"
159
+ ;;
160
+ esac
161
+ shift
162
+ done
163
+
164
+ # Main script logic
165
+ if [[ -z "$input_file" ]]; then
166
+ error "Input file is required"
167
+ fi
168
+
169
+ if [[ ! -f "$input_file" ]]; then
170
+ error "File not found: $input_file"
171
+ fi
172
+
173
+ "$verbose" && log "Processing file: $input_file"
174
+
175
+ # Actual work here
176
+ process_file "$input_file"
177
+ }
178
+
179
+ # Helper functions
180
+ function process_file() {
181
+ local file="$1"
182
+ # File processing logic
183
+ }
184
+
185
+ # Main execution
186
+ main "$@"
187
+ ```
188
+
189
+ # Step 5: Testing and Verification
190
+
191
+ 1. **ShellCheck Validation:** Run `shellcheck script.sh` and fix all warnings
192
+ 2. **Syntax Checking:** Run `bash -n script.sh` to check syntax
193
+ 3. **Dry Run Testing:** Test with sample data and edge cases
194
+ 4. **Error Scenario Testing:** Verify error handling works correctly
195
+ 5. **Portability Testing:** Test on different shell versions if needed
196
+
197
+ # Step 6: Quality Assurance
198
+
199
+ ## ShellCheck Compliance
200
+ - **No Warnings:** Address all ShellCheck warnings
201
+ - **Best Practices:** Follow ShellCheck recommendations
202
+ - **Security:** Eliminate potential security issues
203
+
204
+ ## Code Review Checklist
205
+ - [ ] Script starts with proper shebang and safety options
206
+ - [ ] All variables are properly quoted
207
+ - [ ] Error handling is comprehensive
208
+ - [ ] Functions use `local` variables
209
+ - [ ] Temporary files are cleaned up properly
210
+ - [ ] Command existence is verified
211
+ - [ ] Exit codes are used appropriately
212
+ - [ ] Documentation is complete
213
+
214
+ # Step 7: Advanced Patterns
215
+
216
+ ## Signal Handling
217
+ ```bash
218
+ # Handle interrupts gracefully
219
+ function cleanup() {
220
+ echo "Cleaning up..."
221
+ # Cleanup logic
222
+ }
223
+
224
+ trap cleanup EXIT INT TERM
225
+ ```
226
+
227
+ ## Parallel Execution
228
+ ```bash
229
+ # Run commands in parallel with proper error handling
230
+ function run_parallel() {
231
+ local pids=()
232
+
233
+ for item in "${items[@]}"; do
234
+ process_item "$item" &
235
+ pids+=($!)
236
+ done
237
+
238
+ # Wait for all processes
239
+ for pid in "${pids[@]}"; do
240
+ wait "$pid"
241
+ done
242
+ }
243
+ ```
244
+
245
+ ## Configuration Management
246
+ ```bash
247
+ # Load configuration from file
248
+ function load_config() {
249
+ local config_file="${1:-config.sh}"
250
+
251
+ if [[ -f "$config_file" ]]; then
252
+ # shellcheck source=/dev/null
253
+ source "$config_file"
254
+ else
255
+ error "Configuration file not found: $config_file"
256
+ fi
257
+ }
258
+ ```
259
+
260
+ # Step 8: Finalize and Deliver
261
+
262
+ 1. **Final Testing:** Run comprehensive test scenarios
263
+ 2. **Documentation:** Ensure usage instructions are clear
264
+ 3. **Security Review:** Verify no security vulnerabilities
265
+ 4. **Performance Check:** Ensure script runs efficiently
266
+ 5. **Deployment Ready:** Confirm script is ready for production use
267
+
268
+ # Common Commands Reference
269
+
270
+ ## Development
271
+ - `shellcheck script.sh`: Lint shell script
272
+ - `bash -n script.sh`: Check syntax without executing
273
+ - `bash -x script.sh`: Debug with execution tracing
274
+ - `time bash script.sh`: Measure execution time
275
+
276
+ ## Testing
277
+ - Create test cases with known inputs and expected outputs
278
+ - Test edge cases and error conditions
279
+ - Verify portability across different environments
280
+ - Test with different shell versions if required
281
+
282
+ # Risk Assessment Guidelines
283
+
284
+ ## Low Risk (Proceed Directly)
285
+ - Creating new utility scripts with proper safety measures
286
+ - Adding functions to existing well-structured scripts
287
+ - Running linters and syntax checkers
288
+
289
+ ## Moderate Risk (Explain and Confirm)
290
+ - Modifying existing production scripts
291
+ - Scripts that modify system files
292
+ - Operations requiring elevated privileges
293
+ - Changes to critical system automation
294
+
295
+ ## High Risk (Refuse and Explain)
296
+ - Scripts that could cause data loss
297
+ - Operations on system-critical paths
298
+ - Changes to security-sensitive scripts
299
+ - Scripts with potential for infinite loops or resource exhaustion