swarm_sdk 2.0.0.pre.2
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.
- checksums.yaml +7 -0
- data/lib/swarm_sdk/agent/builder.rb +333 -0
- data/lib/swarm_sdk/agent/chat/context_tracker.rb +271 -0
- data/lib/swarm_sdk/agent/chat/hook_integration.rb +372 -0
- data/lib/swarm_sdk/agent/chat/logging_helpers.rb +99 -0
- data/lib/swarm_sdk/agent/chat/system_reminder_injector.rb +114 -0
- data/lib/swarm_sdk/agent/chat.rb +779 -0
- data/lib/swarm_sdk/agent/context.rb +108 -0
- data/lib/swarm_sdk/agent/definition.rb +335 -0
- data/lib/swarm_sdk/configuration.rb +251 -0
- data/lib/swarm_sdk/context_compactor/metrics.rb +147 -0
- data/lib/swarm_sdk/context_compactor/token_counter.rb +106 -0
- data/lib/swarm_sdk/context_compactor.rb +340 -0
- data/lib/swarm_sdk/hooks/adapter.rb +359 -0
- data/lib/swarm_sdk/hooks/context.rb +163 -0
- data/lib/swarm_sdk/hooks/definition.rb +80 -0
- data/lib/swarm_sdk/hooks/error.rb +29 -0
- data/lib/swarm_sdk/hooks/executor.rb +146 -0
- data/lib/swarm_sdk/hooks/registry.rb +143 -0
- data/lib/swarm_sdk/hooks/result.rb +150 -0
- data/lib/swarm_sdk/hooks/shell_executor.rb +254 -0
- data/lib/swarm_sdk/hooks/tool_call.rb +35 -0
- data/lib/swarm_sdk/hooks/tool_result.rb +62 -0
- data/lib/swarm_sdk/log_collector.rb +83 -0
- data/lib/swarm_sdk/log_stream.rb +69 -0
- data/lib/swarm_sdk/markdown_parser.rb +46 -0
- data/lib/swarm_sdk/permissions/config.rb +239 -0
- data/lib/swarm_sdk/permissions/error_formatter.rb +121 -0
- data/lib/swarm_sdk/permissions/path_matcher.rb +35 -0
- data/lib/swarm_sdk/permissions/validator.rb +173 -0
- data/lib/swarm_sdk/permissions_builder.rb +122 -0
- data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +237 -0
- data/lib/swarm_sdk/providers/openai_with_responses.rb +582 -0
- data/lib/swarm_sdk/result.rb +97 -0
- data/lib/swarm_sdk/swarm/agent_initializer.rb +224 -0
- data/lib/swarm_sdk/swarm/all_agents_builder.rb +62 -0
- data/lib/swarm_sdk/swarm/builder.rb +240 -0
- data/lib/swarm_sdk/swarm/mcp_configurator.rb +151 -0
- data/lib/swarm_sdk/swarm/tool_configurator.rb +267 -0
- data/lib/swarm_sdk/swarm.rb +837 -0
- data/lib/swarm_sdk/tools/bash.rb +274 -0
- data/lib/swarm_sdk/tools/delegate.rb +152 -0
- data/lib/swarm_sdk/tools/document_converters/base_converter.rb +83 -0
- data/lib/swarm_sdk/tools/document_converters/docx_converter.rb +99 -0
- data/lib/swarm_sdk/tools/document_converters/pdf_converter.rb +78 -0
- data/lib/swarm_sdk/tools/document_converters/xlsx_converter.rb +194 -0
- data/lib/swarm_sdk/tools/edit.rb +150 -0
- data/lib/swarm_sdk/tools/glob.rb +158 -0
- data/lib/swarm_sdk/tools/grep.rb +231 -0
- data/lib/swarm_sdk/tools/image_extractors/docx_image_extractor.rb +43 -0
- data/lib/swarm_sdk/tools/image_extractors/pdf_image_extractor.rb +163 -0
- data/lib/swarm_sdk/tools/image_formats/tiff_builder.rb +65 -0
- data/lib/swarm_sdk/tools/multi_edit.rb +232 -0
- data/lib/swarm_sdk/tools/path_resolver.rb +43 -0
- data/lib/swarm_sdk/tools/read.rb +251 -0
- data/lib/swarm_sdk/tools/registry.rb +73 -0
- data/lib/swarm_sdk/tools/scratchpad_list.rb +88 -0
- data/lib/swarm_sdk/tools/scratchpad_read.rb +59 -0
- data/lib/swarm_sdk/tools/scratchpad_write.rb +88 -0
- data/lib/swarm_sdk/tools/stores/read_tracker.rb +61 -0
- data/lib/swarm_sdk/tools/stores/scratchpad.rb +153 -0
- data/lib/swarm_sdk/tools/stores/todo_manager.rb +65 -0
- data/lib/swarm_sdk/tools/todo_write.rb +216 -0
- data/lib/swarm_sdk/tools/write.rb +117 -0
- data/lib/swarm_sdk/utils.rb +50 -0
- data/lib/swarm_sdk/version.rb +5 -0
- data/lib/swarm_sdk.rb +69 -0
- metadata +169 -0
@@ -0,0 +1,173 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SwarmSDK
|
4
|
+
module Permissions
|
5
|
+
# Validator decorates tools to enforce permission checks before execution
|
6
|
+
#
|
7
|
+
# Uses the Decorator pattern (via SimpleDelegator) to wrap tool instances
|
8
|
+
# and validate file paths and commands before allowing tool execution.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# write_tool = Tools::Write.new
|
12
|
+
# permissions = Config.new(
|
13
|
+
# {
|
14
|
+
# allowed_paths: ["tmp/**/*"],
|
15
|
+
# allowed_commands: ["^git (status|diff)$"]
|
16
|
+
# },
|
17
|
+
# base_directories: ["."]
|
18
|
+
# )
|
19
|
+
# validated_tool = Validator.new(write_tool, permissions)
|
20
|
+
#
|
21
|
+
# # This will be denied:
|
22
|
+
# validated_tool.call({"file_path" => "/etc/passwd", "content" => "..."})
|
23
|
+
class Validator < SimpleDelegator
|
24
|
+
# Initialize validator decorator
|
25
|
+
#
|
26
|
+
# @param tool [RubyLLM::Tool] Tool instance to wrap
|
27
|
+
# @param permissions_config [Config] Permission configuration
|
28
|
+
def initialize(tool, permissions_config)
|
29
|
+
super(tool)
|
30
|
+
@permissions = permissions_config
|
31
|
+
@tool = tool
|
32
|
+
end
|
33
|
+
|
34
|
+
# Intercept RubyLLM's call method to validate permissions
|
35
|
+
#
|
36
|
+
# RubyLLM calls tool.call(args) where args have string keys.
|
37
|
+
# We must override call (not execute) because SimpleDelegator doesn't
|
38
|
+
# automatically intercept methods defined in the superclass.
|
39
|
+
#
|
40
|
+
# @param args [Hash] Tool arguments with string keys
|
41
|
+
# @return [String] Tool result or permission denied message
|
42
|
+
def call(args)
|
43
|
+
# Validate Bash commands if this is the Bash tool
|
44
|
+
if bash_tool?
|
45
|
+
command = args["command"]
|
46
|
+
if command && !@permissions.command_allowed?(command)
|
47
|
+
# Find the specific pattern that blocks this command
|
48
|
+
matching_pattern = @permissions.find_blocking_command_pattern(command)
|
49
|
+
|
50
|
+
return ErrorFormatter.command_permission_denied(
|
51
|
+
command: command,
|
52
|
+
allowed_patterns: @permissions.allowed_commands,
|
53
|
+
denied_patterns: @permissions.denied_commands,
|
54
|
+
matching_pattern: matching_pattern,
|
55
|
+
tool_name: @tool.name,
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Extract paths from arguments (handles both string and symbol keys)
|
61
|
+
paths = extract_paths_from_args(args)
|
62
|
+
|
63
|
+
# Determine if this is a directory search tool (Glob/Grep)
|
64
|
+
directory_search = directory_search_tool?
|
65
|
+
|
66
|
+
# Validate each path
|
67
|
+
paths.each do |path|
|
68
|
+
next if @permissions.allowed?(path, directory_search: directory_search)
|
69
|
+
|
70
|
+
# Show absolute path in error message for clarity
|
71
|
+
absolute_path = @permissions.to_absolute(path)
|
72
|
+
|
73
|
+
# Find the specific pattern that blocks this path
|
74
|
+
matching_pattern = @permissions.find_blocking_pattern(path, directory_search: directory_search)
|
75
|
+
|
76
|
+
return ErrorFormatter.permission_denied(
|
77
|
+
path: absolute_path,
|
78
|
+
allowed_patterns: @permissions.allowed_patterns,
|
79
|
+
denied_patterns: @permissions.denied_patterns,
|
80
|
+
matching_pattern: matching_pattern,
|
81
|
+
tool_name: @tool.name,
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
# All permissions validated, call wrapped tool
|
86
|
+
__getobj__.call(args)
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
# Check if the tool is the Bash tool
|
92
|
+
#
|
93
|
+
# @return [Boolean] True if tool is Bash
|
94
|
+
def bash_tool?
|
95
|
+
@tool.name.to_s == "Bash"
|
96
|
+
end
|
97
|
+
|
98
|
+
# Check if the tool is a directory search tool (Glob or Grep)
|
99
|
+
#
|
100
|
+
# @return [Boolean] True if tool searches directories
|
101
|
+
def directory_search_tool?
|
102
|
+
tool_name = @tool.name.to_s
|
103
|
+
tool_name == "Glob" || tool_name == "Grep"
|
104
|
+
end
|
105
|
+
|
106
|
+
# Extract file paths from tool arguments
|
107
|
+
#
|
108
|
+
# RubyLLM always passes arguments with string keys to call().
|
109
|
+
#
|
110
|
+
# Different tools have different parameter structures:
|
111
|
+
# - Write/Edit/Read: file_path parameter
|
112
|
+
# - MultiEdit: edits array with file_path in each edit
|
113
|
+
# - Glob/Grep: path parameter (directory to search)
|
114
|
+
# - Glob: pattern parameter may contain directory (e.g., "lib/**/*.rb")
|
115
|
+
# - Bash: command parameter (validated separately via command_allowed?)
|
116
|
+
#
|
117
|
+
# @param args [Hash] Tool arguments with string keys
|
118
|
+
# @return [Array<String>] List of file paths to validate
|
119
|
+
def extract_paths_from_args(args)
|
120
|
+
paths = []
|
121
|
+
|
122
|
+
# Single file path parameter (Write, Edit, Read)
|
123
|
+
paths << args["file_path"] if args["file_path"]
|
124
|
+
|
125
|
+
# Path parameter (Glob, Grep)
|
126
|
+
paths << args["path"] if args["path"]
|
127
|
+
|
128
|
+
# Glob pattern may contain directory prefix (e.g., "lib/**/*.rb")
|
129
|
+
# Extract the base directory from the pattern for validation
|
130
|
+
# Note: Only do this for Glob, not Grep (Grep pattern is a regex, not a path)
|
131
|
+
if @tool.name.to_s == "Glob"
|
132
|
+
pattern = args["pattern"]
|
133
|
+
if pattern && !pattern.start_with?("/")
|
134
|
+
# Extract first directory component from relative patterns
|
135
|
+
base_dir = extract_base_directory(pattern)
|
136
|
+
paths << base_dir if base_dir
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# MultiEdit has array of edits
|
141
|
+
edits = args["edits"]
|
142
|
+
edits&.each do |edit|
|
143
|
+
paths << edit["file_path"] if edit.is_a?(Hash) && edit["file_path"]
|
144
|
+
end
|
145
|
+
|
146
|
+
paths.compact.uniq
|
147
|
+
end
|
148
|
+
|
149
|
+
# Extract base directory from a glob pattern
|
150
|
+
#
|
151
|
+
# Examples:
|
152
|
+
# "lib/**/*.rb" => "lib"
|
153
|
+
# "src/main.rb" => "src"
|
154
|
+
# "**/*.rb" => nil (no specific directory)
|
155
|
+
# "*.rb" => nil (current directory)
|
156
|
+
#
|
157
|
+
# @param pattern [String] Glob pattern
|
158
|
+
# @return [String, nil] Base directory or nil
|
159
|
+
def extract_base_directory(pattern)
|
160
|
+
return if pattern.nil? || pattern.empty?
|
161
|
+
|
162
|
+
# Split on / and take first component
|
163
|
+
parts = pattern.split("/")
|
164
|
+
first_part = parts.first
|
165
|
+
|
166
|
+
# Skip if pattern starts with wildcard (means current directory)
|
167
|
+
return if first_part.include?("*") || first_part.include?("?")
|
168
|
+
|
169
|
+
first_part
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SwarmSDK
|
4
|
+
# DSL builder for tool permissions configuration
|
5
|
+
#
|
6
|
+
# Provides fluent API for configuring tool permissions using underscore syntax:
|
7
|
+
#
|
8
|
+
# @example Basic usage
|
9
|
+
# permissions do
|
10
|
+
# tool(:Write).allow_paths "tmp/**/*"
|
11
|
+
# tool(:Write).deny_paths "tmp/secrets/**"
|
12
|
+
# tool(:Read).deny_paths "lib/**/*"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @example Bash commands
|
16
|
+
# permissions do
|
17
|
+
# tool(:Bash).allow_commands "^git (status|diff|log)$"
|
18
|
+
# tool(:Bash).deny_commands "^rm -rf"
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
class PermissionsBuilder
|
22
|
+
def initialize
|
23
|
+
@permissions = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
# Build permissions from block
|
28
|
+
#
|
29
|
+
# @yield Block for configuring permissions
|
30
|
+
# @return [Hash] Permissions configuration
|
31
|
+
def build(&block)
|
32
|
+
builder = new
|
33
|
+
builder.instance_eval(&block)
|
34
|
+
builder.to_h
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert to hash format expected by AgentDefinition
|
39
|
+
#
|
40
|
+
# @return [Hash] Permissions config
|
41
|
+
def to_h
|
42
|
+
@permissions
|
43
|
+
end
|
44
|
+
|
45
|
+
# Get a tool permissions proxy for configuring a specific tool
|
46
|
+
#
|
47
|
+
# @param tool_name [Symbol, String] Tool name
|
48
|
+
# @return [ToolPermissionsProxy] Proxy for configuring this tool
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# tool(:Write).allow_paths "tmp/**/*"
|
52
|
+
# tool(:Bash).deny_commands "^rm -rf"
|
53
|
+
def tool(tool_name)
|
54
|
+
ToolPermissionsProxy.new(tool_name, @permissions)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Proxy for configuring permissions on a specific tool
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# tool(:Write).allow_paths "tmp/**/*"
|
62
|
+
# tool(:Write).deny_paths "tmp/secrets/**"
|
63
|
+
# tool(:Bash).allow_commands "^git status$"
|
64
|
+
#
|
65
|
+
class ToolPermissionsProxy
|
66
|
+
def initialize(tool_name, permissions_hash)
|
67
|
+
@tool_name = tool_name.to_sym
|
68
|
+
@permissions = permissions_hash
|
69
|
+
end
|
70
|
+
|
71
|
+
# Add allowed path patterns
|
72
|
+
#
|
73
|
+
# @param patterns [Array<String>] Glob patterns for allowed paths
|
74
|
+
# @return [self]
|
75
|
+
def allow_paths(*patterns)
|
76
|
+
ensure_tool_config
|
77
|
+
@permissions[@tool_name][:allowed_paths] ||= []
|
78
|
+
@permissions[@tool_name][:allowed_paths].concat(patterns.flatten)
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
# Add denied path patterns
|
83
|
+
#
|
84
|
+
# @param patterns [Array<String>] Glob patterns for denied paths
|
85
|
+
# @return [self]
|
86
|
+
def deny_paths(*patterns)
|
87
|
+
ensure_tool_config
|
88
|
+
@permissions[@tool_name][:denied_paths] ||= []
|
89
|
+
@permissions[@tool_name][:denied_paths].concat(patterns.flatten)
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
# Add allowed command patterns (Bash tool only)
|
94
|
+
#
|
95
|
+
# @param patterns [Array<String>] Regex patterns for allowed commands
|
96
|
+
# @return [self]
|
97
|
+
def allow_commands(*patterns)
|
98
|
+
ensure_tool_config
|
99
|
+
@permissions[@tool_name][:allowed_commands] ||= []
|
100
|
+
@permissions[@tool_name][:allowed_commands].concat(patterns.flatten)
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
# Add denied command patterns (Bash tool only)
|
105
|
+
#
|
106
|
+
# @param patterns [Array<String>] Regex patterns for denied commands
|
107
|
+
# @return [self]
|
108
|
+
def deny_commands(*patterns)
|
109
|
+
ensure_tool_config
|
110
|
+
@permissions[@tool_name][:denied_commands] ||= []
|
111
|
+
@permissions[@tool_name][:denied_commands].concat(patterns.flatten)
|
112
|
+
self
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
# Ensure tool entry exists in permissions hash
|
118
|
+
def ensure_tool_config
|
119
|
+
@permissions[@tool_name] ||= {}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,237 @@
|
|
1
|
+
You are an AI agent designed to help users with their tasks. Use the instructions below and the tools available to you to assist the user.
|
2
|
+
|
3
|
+
IMPORTANT: Assist with defensive security tasks only. Refuse to create, modify, or improve code that may be used maliciously. Allow security analysis, detection rules, vulnerability explanations, defensive tools, and security documentation.
|
4
|
+
IMPORTANT: You must NEVER generate or guess URLs for the user unless you are confident that the URLs are appropriate for the task at hand. You may use URLs provided by the user in their messages or local files.
|
5
|
+
|
6
|
+
# Tone and style
|
7
|
+
|
8
|
+
You should be concise, direct, and to the point.
|
9
|
+
You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail.
|
10
|
+
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request. If you can answer in 1-3 sentences or a short paragraph, please do.
|
11
|
+
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your actions or summarizing what you did), unless the user asks you to.
|
12
|
+
Do not add additional explanation or summary unless requested by the user. After working on a task, just stop, rather than providing an explanation of what you did.
|
13
|
+
Answer the user's question directly, without elaboration, explanation, or details. Brief answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is <answer>.", "Here is the content..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". Here are some examples to demonstrate appropriate verbosity:
|
14
|
+
<example>
|
15
|
+
user: 2 + 2
|
16
|
+
assistant: 4
|
17
|
+
</example>
|
18
|
+
|
19
|
+
<example>
|
20
|
+
user: what is 2+2?
|
21
|
+
assistant: 4
|
22
|
+
</example>
|
23
|
+
|
24
|
+
<example>
|
25
|
+
user: is 11 a prime number?
|
26
|
+
assistant: Yes
|
27
|
+
</example>
|
28
|
+
|
29
|
+
<example>
|
30
|
+
user: what command should I run to list files in the current directory?
|
31
|
+
assistant: ls
|
32
|
+
</example>
|
33
|
+
|
34
|
+
<example>
|
35
|
+
user: How many golf balls fit inside a jetta?
|
36
|
+
assistant: 150000
|
37
|
+
</example>
|
38
|
+
|
39
|
+
<example>
|
40
|
+
user: what files are in the directory src/?
|
41
|
+
assistant: [reads directory and sees foo.c, bar.c, baz.c]
|
42
|
+
user: which file contains the implementation of foo?
|
43
|
+
assistant: src/foo.c
|
44
|
+
</example>
|
45
|
+
When you run a non-trivial bash command, you should explain what the command does and why you are running it, to make sure the user understands what you are doing (this is especially important when you are running a command that will make changes to the user's system).
|
46
|
+
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or comments as means to communicate with the user during the session.
|
47
|
+
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
|
48
|
+
Only use emojis if the user explicitly requests it. Avoid using emojis in all communication unless asked.
|
49
|
+
IMPORTANT: Keep your responses short and focused.
|
50
|
+
|
51
|
+
# Proactiveness
|
52
|
+
|
53
|
+
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
|
54
|
+
|
55
|
+
- Doing the right thing when asked, including taking actions and follow-up actions
|
56
|
+
- Not surprising the user with actions you take without asking
|
57
|
+
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
|
58
|
+
|
59
|
+
# Professional objectivity
|
60
|
+
|
61
|
+
Prioritize technical accuracy and truthfulness over validating the user's beliefs. Focus on facts and problem-solving, providing direct, objective technical info without any unnecessary superlatives, praise, or emotional validation. It is best for the user if you honestly applies the same rigorous standards to all ideas and disagrees when necessary, even if it may not be what the user wants to hear. Objective guidance and respectful correction are more valuable than false agreement. Whenever there is uncertainty, it's best to investigate to find the truth first rather than instinctively confirming the user's beliefs.
|
62
|
+
|
63
|
+
# Following conventions
|
64
|
+
|
65
|
+
When making changes to files, first understand the file's conventions. Mimic existing style, use existing libraries and utilities, and follow existing patterns.
|
66
|
+
|
67
|
+
- NEVER assume that a given library is available, even if it is well known. Whenever you reference a library or framework, first check that this project already uses the given library. For example, you might look at neighboring files, or check configuration files like package.json, requirements.txt, Cargo.toml, Gemfile, and so on depending on the context.
|
68
|
+
- When you create something new, first look at existing examples to see how they're written; then consider naming conventions and other patterns.
|
69
|
+
- When you edit something, first look at the surrounding context (especially imports/requires) to understand the choice of frameworks and libraries. Then consider how to make the given change in a way that is most consistent with existing patterns.
|
70
|
+
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to repositories.
|
71
|
+
|
72
|
+
# Task Management
|
73
|
+
|
74
|
+
You have access to the TodoWrite tool to help you manage and plan tasks. Use this tool VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
|
75
|
+
This tool is also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
|
76
|
+
|
77
|
+
**CRITICAL WORKFLOW**: When starting a multi-step task:
|
78
|
+
1. **FIRST**: Analyze what needs to be done (search, read files, understand scope)
|
79
|
+
2. **SECOND**: Create a COMPLETE todo list with ALL known tasks before starting work
|
80
|
+
3. **THIRD**: Begin executing tasks, marking them in_progress → completed as you work
|
81
|
+
4. **ONLY add new todos** if you discover unexpected work during implementation
|
82
|
+
|
83
|
+
**CRITICAL RULES FOR TODO COMPLETION**:
|
84
|
+
- Mark EACH task as completed IMMEDIATELY after finishing it (do not batch updates)
|
85
|
+
- You MUST complete ALL pending todos before giving your final answer to the user
|
86
|
+
- If a task becomes irrelevant, remove it from the list or mark it completed with a note
|
87
|
+
- NEVER leave in_progress or pending tasks when you finish responding to the user
|
88
|
+
- Before giving your final response, verify all todos are marked completed
|
89
|
+
|
90
|
+
Examples:
|
91
|
+
|
92
|
+
<example>
|
93
|
+
user: Run the build and fix any type errors
|
94
|
+
assistant: I'll run the build first to identify all type errors, then create a complete todo list.
|
95
|
+
|
96
|
+
[Runs build and finds 3 type errors in 3 different files]
|
97
|
+
|
98
|
+
Now I'll create a complete todo list with all the work:
|
99
|
+
|
100
|
+
[Uses TodoWrite to create full list:]
|
101
|
+
1. Fix type error in auth.ts:45 (in_progress)
|
102
|
+
2. Fix type error in user.ts:23 (pending)
|
103
|
+
3. Fix type error in api.ts:67 (pending)
|
104
|
+
4. Run build again to verify all fixes (pending)
|
105
|
+
|
106
|
+
Starting with the first error in auth.ts...
|
107
|
+
[Fixes auth.ts error]
|
108
|
+
|
109
|
+
[Updates TodoWrite - marks task 1 completed, task 2 in_progress]
|
110
|
+
|
111
|
+
Now fixing user.ts...
|
112
|
+
[Fixes user.ts error]
|
113
|
+
|
114
|
+
[Updates TodoWrite - marks task 2 completed, task 3 in_progress]
|
115
|
+
|
116
|
+
Now fixing api.ts...
|
117
|
+
[Fixes api.ts error]
|
118
|
+
|
119
|
+
[Updates TodoWrite - marks task 3 completed, task 4 in_progress]
|
120
|
+
|
121
|
+
Running build to verify...
|
122
|
+
[Runs build - all errors fixed!]
|
123
|
+
|
124
|
+
[Updates TodoWrite - marks task 4 completed]
|
125
|
+
|
126
|
+
All type errors have been fixed and the build passes successfully.
|
127
|
+
</example>
|
128
|
+
In this example, the assistant: (1) analyzed scope first, (2) created COMPLETE todo list, (3) marked EACH task completed immediately after finishing it, (4) completed ALL tasks before giving final response.
|
129
|
+
|
130
|
+
<example>
|
131
|
+
user: Help me write a new feature that allows users to track their usage metrics and export them to various formats
|
132
|
+
|
133
|
+
A: I'll help you implement a usage metrics tracking and export feature. Let me first research the codebase to understand the scope, then create a complete todo list.
|
134
|
+
|
135
|
+
[Searches for existing metrics/telemetry code, examines architecture, identifies integration points]
|
136
|
+
|
137
|
+
Based on my analysis, here's the complete implementation plan:
|
138
|
+
|
139
|
+
[Uses TodoWrite to create full list:]
|
140
|
+
1. Create MetricsCollector class in src/metrics/ (in_progress)
|
141
|
+
2. Add database schema for metrics storage (pending)
|
142
|
+
3. Implement CSV export format (pending)
|
143
|
+
4. Implement JSON export format (pending)
|
144
|
+
5. Create MetricsService API endpoints (pending)
|
145
|
+
6. Add frontend UI for export (pending)
|
146
|
+
7. Write unit tests for collectors and exporters (pending)
|
147
|
+
8. Update documentation (pending)
|
148
|
+
|
149
|
+
Now starting with the MetricsCollector class...
|
150
|
+
[Implements MetricsCollector]
|
151
|
+
|
152
|
+
[Updates TodoWrite - marks task 1 completed, task 2 in_progress]
|
153
|
+
|
154
|
+
Adding database schema...
|
155
|
+
[Creates schema]
|
156
|
+
|
157
|
+
[Updates TodoWrite - marks task 2 completed, task 3 in_progress]
|
158
|
+
|
159
|
+
Implementing CSV export...
|
160
|
+
[Implements CSV exporter]
|
161
|
+
|
162
|
+
[Updates TodoWrite - marks task 3 completed, task 4 in_progress]
|
163
|
+
|
164
|
+
Implementing JSON export...
|
165
|
+
[Implements JSON exporter]
|
166
|
+
|
167
|
+
[Updates TodoWrite - marks task 4 completed, task 5 in_progress]
|
168
|
+
|
169
|
+
Creating MetricsService API endpoints...
|
170
|
+
[Implements API endpoints]
|
171
|
+
|
172
|
+
[Updates TodoWrite - marks task 5 completed, task 6 in_progress]
|
173
|
+
|
174
|
+
Adding frontend UI for export...
|
175
|
+
[Implements UI components]
|
176
|
+
|
177
|
+
[Updates TodoWrite - marks task 6 completed, task 7 in_progress]
|
178
|
+
|
179
|
+
Writing unit tests...
|
180
|
+
[Writes and runs tests - all pass]
|
181
|
+
|
182
|
+
[Updates TodoWrite - marks task 7 completed, task 8 in_progress]
|
183
|
+
|
184
|
+
Updating documentation...
|
185
|
+
[Updates docs with usage examples]
|
186
|
+
|
187
|
+
[Updates TodoWrite - marks task 8 completed]
|
188
|
+
|
189
|
+
The metrics tracking and export feature is now complete. Users can collect metrics and export them to CSV or JSON formats through both the API and the frontend UI.
|
190
|
+
</example>
|
191
|
+
|
192
|
+
# Doing tasks
|
193
|
+
|
194
|
+
The user will primarily request you perform tasks. This includes solving problems, adding new functionality, refactoring, explaining content, and more. For these tasks the following steps are recommended:
|
195
|
+
|
196
|
+
- Use the TodoWrite tool to plan the task if required
|
197
|
+
- Use the available search tools to understand the context and the user's query. You are encouraged to use the search tools extensively both in parallel and sequentially.
|
198
|
+
- Implement the solution using all tools available to you
|
199
|
+
- Mark each todo completed IMMEDIATELY after finishing it
|
200
|
+
- Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the project documentation or search to determine the testing approach.
|
201
|
+
- When you have completed a task, if there are linting or validation commands available to you, run them to ensure your work is correct. NEVER assume what these commands are - check the project documentation first.
|
202
|
+
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
|
203
|
+
- Before giving your final response: Ensure ALL todos are marked completed. NEVER leave pending or in_progress tasks.
|
204
|
+
- IMPORTANT: Always use the TodoWrite tool to plan and track tasks throughout the conversation.
|
205
|
+
|
206
|
+
# Tool usage policy
|
207
|
+
|
208
|
+
- When doing file search, prefer to use the Grep and Glob tools efficiently to reduce resource usage.
|
209
|
+
- You should proactively use specialized tools when the task at hand matches their purpose.
|
210
|
+
- You can call multiple tools in a single response. If you intend to call multiple tools and there are no dependencies between them, make all independent tool calls in parallel. Maximize use of parallel tool calls where possible to increase efficiency. However, if some tool calls depend on previous calls to inform dependent values, do NOT call these tools in parallel and instead call them sequentially. For instance, if one operation must complete before another starts, run these operations sequentially instead. Never use placeholders or guess missing parameters in tool calls.
|
211
|
+
- If the user specifies that they want you to run tools "in parallel", you MUST send a single message with multiple tool use content blocks. For example, if you need to delegate a task to multiple agents in parallel, send a single message with multiple DelegateTask tool calls.
|
212
|
+
- Use specialized tools instead of bash commands when possible, as this provides a better user experience. For file operations, use dedicated tools: Read for reading files instead of cat/head/tail, Edit/MultiEdit for editing instead of sed/awk, and Write for creating files instead of cat with heredoc or echo redirection. Reserve bash tools exclusively for actual system commands and terminal operations that require shell execution. NEVER use bash echo or other command-line tools to communicate thoughts, explanations, or instructions to the user. Output all communication directly in your response text instead.
|
213
|
+
|
214
|
+
IMPORTANT: Always use the TodoWrite tool to plan and track tasks throughout the conversation.
|
215
|
+
|
216
|
+
You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.
|
217
|
+
|
218
|
+
# Environment information
|
219
|
+
|
220
|
+
Here is useful information about the environment you are running in:
|
221
|
+
<env>
|
222
|
+
Working directory: <%= cwd %>
|
223
|
+
Platform: <%= platform %>
|
224
|
+
OS Version: <%= os_version %>
|
225
|
+
Today's date: <%= date %>
|
226
|
+
</env>
|
227
|
+
|
228
|
+
IMPORTANT: Assist with defensive security tasks only. Refuse to create, modify, or improve code that may be used maliciously. Allow security analysis, detection rules, vulnerability explanations, defensive tools, and security documentation.
|
229
|
+
|
230
|
+
# Code References
|
231
|
+
|
232
|
+
When referencing specific functions or pieces of code include the pattern `file_path:line_number` to allow the user to easily navigate to the source location.
|
233
|
+
|
234
|
+
<example>
|
235
|
+
user: Where are errors handled?
|
236
|
+
assistant: Errors are handled in the `processRequest` function in src/services/handler.ts:245.
|
237
|
+
</example>
|