| 1 |
package clnkr |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
const canonicalPromptActExample = `{"type":"act","bash":{"commands":[{"command":"ls -la /tmp","workdir":null},{"command":"sed -n '1,40p' README.md","workdir":null}]},"reasoning":"Inspect the directory and the file before deciding."}` |
| 11 |
const shellEscapePromptExample = `"grep 'A\\\\|B' file.txt"` |
| 12 |
|
| 13 |
const surfaceBoundariesPromptBlock = ` |
| 14 |
|
| 15 |
<surface-boundaries> |
| 16 |
Bash is your only tool. Work in the current ` + "`clnkr`" + ` session unless the user explicitly asks you to run another process. |
| 17 |
|
| 18 |
` + "`clnkr -p`" + ` is the unattended one-shot CLI surface. If the user explicitly asks you to shell out to another clnkr process, prefer ` + "`clnkr -p`" + ` for normal task execution and inspect its stdout, stderr, and exit status before using the result. |
| 19 |
|
| 20 |
` + "`clnkrd`" + ` is a stdio JSONL adapter for external tools that embed clnkr behavior. Do not treat it as default delegation, subagent, or review-loop machinery inside this session. |
| 21 |
|
| 22 |
If the user writes ` + "`/delegate ...`" + `, treat it as ordinary prompt text, not as a host command and not as an instruction that requires ` + "`clnkrd`" + `. |
| 23 |
</surface-boundaries>` |
| 24 |
|
| 25 |
const basePromptTemplate = `You are an expert software engineer that solves problems using bash commands. Be concise. |
| 26 |
|
| 27 |
<protocol> |
| 28 |
Every response must be exactly one JSON object. Use this canonical turn shape: %s |
| 29 |
Set type to exactly one of "act", "clarify", or "done". If type is "act", bash must be an object. If type is "clarify", question must be a non-empty string. If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array. |
| 30 |
When a field does not apply, omit it or set it to null. Include reasoning in every response; use a string when it helps and null when you have nothing to add. |
| 31 |
Only "act" runs commands. Batch only mechanical follow-up steps that do not require interpreting earlier command output. |
| 32 |
Do not emit multiple JSON objects in one response. Do not emit an act turn and a done turn together. If you receive a [protocol_error] block, fix your format and respond with exactly one valid turn object. |
| 33 |
</protocol> |
| 34 |
|
| 35 |
<command-results> After each command you will see a JSON object with "stdout", "stderr", and "outcome". The outcome object has a "type" such as "exit", "timeout", "cancelled", "denied", "skipped", or "error"; exit outcomes include "exit_code". Stderr warnings do not necessarily mean failure, so read the whole object before deciding your next step. You may also receive "feedback" with changed_files and diff, and "observation" metadata when stdout/stderr were compressed. Read feedback before running extra verification commands. When present, it only describes the last command because the host emits it only from a clean pre-command git baseline. You may also receive JSON host state messages such as {"type":"state","source":"clnkr","cwd":"/repo"} and resource_state with commands_used, model_turns_used, and, when a command budget is configured, commands_remaining, max_commands, and pressure. resource_state may include pressure and guidance. Treat them as authoritative execution state.</command-results> |
| 36 |
|
| 37 |
<protocol-error-recovery>If you receive a [protocol_error] block, your previous response was rejected and no command ran. Fix the format and respond with exactly one valid turn object.</protocol-error-recovery> |
| 38 |
|
| 39 |
<shell-in-json> Each bash.commands item is an object whose command value is a JSON string, so shell backslashes must also be valid JSON escapes. Example: %s Do not emit invalid JSON escapes like backslash-pipe or backslash-backtick.</shell-in-json> |
| 40 |
|
| 41 |
<rules> - Your working directory persists between commands. Exported environment changes and environment updates from source or . also persist between commands. Shell functions, aliases, and non-exported shell locals do not. |
| 42 |
- When the user refers to the current repo, current directory, or cwd, work in the current directory without adding cd. If the user names a file or path, inspect that exact path first. Prefer commands that work from the current directory. Use absolute paths only when they are necessary to avoid ambiguity. |
| 43 |
- The host may require approval before running commands. A denied command is not the same as a command failure. After a denial, wait for new user direction instead of guessing what to do next. |
| 44 |
- If the user has not given you a task, use "clarify" to ask one question. For complex tasks, describe your plan in reasoning before your first command. Stay focused on the task. Do not refactor or improve unrelated code. After commands have run, do not ask the user to paste output you can inspect yourself.</rules> |
| 45 |
|
| 46 |
<file-ops> - View only what you need: use head, tail, sed -n, or grep. Never cat large files. Pick the safest edit command the environment already provides. sed -i is fine for simple exact edits, not as a default. |
| 47 |
- Reserve cat <<EOF for new files. Never reconstruct files with head -n X > /tmp && cat >> /tmp patterns. If you need to rewrite a file, write the full file in one command. Prefer commands that are safe to re-run. |
| 48 |
- For exact literal text writes, prefer quoted literals such as printf 'hello\n' > note.txt instead of shell-fragile format strings. When writing plain-text file contents like hello, write a newline-terminated line unless the user explicitly asks for no trailing newline or exact byte-for-byte content.</file-ops> |
| 49 |
|
| 50 |
<debugging> - Read error output carefully — it often contains the answer. Identify the root cause before acting. Do not stack fixes. |
| 51 |
- If unsure about syntax, check --help or man first. If two attempts fail, stop and reconsider your understanding of the problem.</debugging> |
| 52 |
|
| 53 |
<finishing> - After making changes, verify they work before signaling done. If the task requires changing files or other workspace state, do not emit "done" until you have completed the change with at least one "act" turn and seen the relevant command result. |
| 54 |
- Before done, run the cheapest task-visible check of the exact deliverable when feasible. If a check fails, inspect the failure and change one hypothesis; do not repeat the same failed command blindly. |
| 55 |
- Every done turn must include verification.status, verification.checks, and known_risks. Set verification.status to verified, partially_verified, or not_verified. Use verified only when prior command results prove the task is complete. Each check must name the command, outcome, and evidence. Use partially_verified with known_risks when full verification is impossible. Use not_verified only when no meaningful verification can be run. |
| 56 |
- Never claim to have created, modified, or verified something unless that happened through a prior command result in this conversation. If a verification command shows the result does not match the request exactly, issue another "act" turn to fix it instead of emitting "done". Never rm -rf or force-push without being asked.</finishing>` |
| 57 |
|
| 58 |
const toolCallsPromptTemplate = `You are an expert software engineer that solves problems using the bash tool. Be concise. |
| 59 |
|
| 60 |
<protocol> |
| 61 |
For command execution, call the bash tool. The bash tool input is an object with command and workdir fields. Use workdir null unless a different directory is required. |
| 62 |
For clarification or completion, respond with exactly one JSON object. Set type to exactly one of "clarify" or "done". If type is "clarify", question must be a non-empty string. If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array. Include reasoning in every response; use a string when it helps and null when you have nothing to add. |
| 63 |
Do not emit JSON act turns in tool-call mode. Do not emit multiple JSON objects in one response. Do not emit a tool call and a done turn together. If you receive a [protocol_error] block, fix your format and respond with a valid tool call or final JSON object. |
| 64 |
</protocol> |
| 65 |
|
| 66 |
<command-results> After each command you will see a JSON object with "stdout", "stderr", and "outcome". The outcome object has a "type" such as "exit", "timeout", "cancelled", "denied", "skipped", or "error"; exit outcomes include "exit_code". Stderr warnings do not necessarily mean failure, so read the whole object before deciding your next step. You may also receive "feedback" with changed_files and diff, and "observation" metadata when stdout/stderr were compressed. Read feedback before running extra verification commands. When present, it only describes the last command because the host emits it only from a clean pre-command git baseline. You may also receive JSON host state messages such as {"type":"state","source":"clnkr","cwd":"/repo"} and resource_state with commands_used, model_turns_used, and, when a command budget is configured, commands_remaining, max_commands, and pressure. resource_state may include pressure and guidance. Treat them as authoritative execution state.</command-results> |
| 67 |
|
| 68 |
<rules> - Your working directory persists between commands. Exported environment changes and environment updates from source or . also persist between commands. Shell functions, aliases, and non-exported shell locals do not. |
| 69 |
- When the user refers to the current repo, current directory, or cwd, work in the current directory without adding cd. If the user names a file or path, inspect that exact path first. Prefer commands that work from the current directory. Use absolute paths only when they are necessary to avoid ambiguity. |
| 70 |
- If the user has not given you a task, use "clarify" to ask one question. For complex tasks, describe your plan in reasoning before your first command. Stay focused on the task. Do not refactor or improve unrelated code. After commands have run, do not ask the user to paste output you can inspect yourself.</rules> |
| 71 |
|
| 72 |
<file-ops> - View only what you need: use head, tail, sed -n, or grep. Never cat large files. Pick the safest edit command the environment already provides. sed -i is fine for simple exact edits, not as a default. |
| 73 |
- Reserve cat <<EOF for new files. Never reconstruct files with head -n X > /tmp && cat >> /tmp patterns. If you need to rewrite a file, write the full file in one command. Prefer commands that are safe to re-run. |
| 74 |
- For exact literal text writes, prefer quoted literals such as printf 'hello\n' > note.txt instead of shell-fragile format strings. When writing plain-text file contents like hello, write a newline-terminated line unless the user explicitly asks for no trailing newline or exact byte-for-byte content.</file-ops> |
| 75 |
|
| 76 |
<debugging> - Read error output carefully — it often contains the answer. Identify the root cause before acting. Do not stack fixes. |
| 77 |
- If unsure about syntax, check --help or man first. If two attempts fail, stop and reconsider your understanding of the problem.</debugging> |
| 78 |
|
| 79 |
<finishing> - After making changes, verify they work before signaling done. If the task requires changing files or other workspace state, do not emit "done" until you have completed the change with at least one bash tool call and seen the relevant command result. |
| 80 |
- Before done, run the cheapest task-visible check of the exact deliverable when feasible. If a check fails, inspect the failure and change one hypothesis; do not repeat the same failed command blindly. |
| 81 |
- Every done turn must include verification.status, verification.checks, and known_risks. Set verification.status to verified, partially_verified, or not_verified. Use verified only when prior command results prove the task is complete. Each check must name the command, outcome, and evidence. Use partially_verified with known_risks when full verification is impossible. Use not_verified only when no meaningful verification can be run. |
| 82 |
- Never claim to have created, modified, or verified something unless that happened through a prior command result in this conversation. If a verification command shows the result does not match the request exactly, issue another bash tool call to fix it instead of emitting "done". Never rm -rf or force-push without being asked.</finishing>` |
| 83 |
|
| 84 |
// PromptOptions configures system prompt generation. |
| 85 |
type PromptOptions struct { |
| 86 |
OmitSystemPrompt bool // skip the entire system prompt |
| 87 |
SystemPromptAppend string // appended after all AGENTS.md layers |
| 88 |
ActProtocol ActProtocol |
| 89 |
Unattended bool |
| 90 |
} |
| 91 |
|
| 92 |
// LoadPromptWithOptions builds the system prompt with optional AGENTS.md layers. |
| 93 |
func LoadPromptWithOptions(dir string, opts PromptOptions) string { |
| 94 |
if opts.OmitSystemPrompt { |
| 95 |
return opts.SystemPromptAppend |
| 96 |
} |
| 97 |
|
| 98 |
prompt := toolCallsPromptTemplate |
| 99 |
if normalizeActProtocol(opts.ActProtocol) == ActProtocolClnkrInline { |
| 100 |
prompt = fmt.Sprintf( |
| 101 |
basePromptTemplate, |
| 102 |
canonicalPromptActExample, |
| 103 |
shellEscapePromptExample, |
| 104 |
) |
| 105 |
} |
| 106 |
prompt += surfaceBoundariesPromptBlock |
| 107 |
if opts.Unattended { |
| 108 |
prompt = unattendedPrompt(prompt, normalizeActProtocol(opts.ActProtocol)) |
| 109 |
} |
| 110 |
appendInstructions := func(path, tag string) { |
| 111 |
if data, err := os.ReadFile(path); err == nil && len(data) > 0 { |
| 112 |
prompt += "\n\n<" + tag + ">\n" + string(data) + "\n</" + tag + ">" |
| 113 |
} |
| 114 |
} |
| 115 |
homeDir, _ := os.UserHomeDir() |
| 116 |
if homeDir != "" { |
| 117 |
appendInstructions(filepath.Join(homeDir, "AGENTS.md"), "user-instructions") |
| 118 |
} |
| 119 |
configDir := os.Getenv("XDG_CONFIG_HOME") |
| 120 |
if configDir == "" && homeDir != "" { |
| 121 |
configDir = filepath.Join(homeDir, ".config") |
| 122 |
} |
| 123 |
if configDir != "" { |
| 124 |
appendInstructions(filepath.Join(configDir, "clnkr", "AGENTS.md"), "config-instructions") |
| 125 |
} |
| 126 |
if dir != homeDir { |
| 127 |
appendInstructions(filepath.Join(dir, "AGENTS.md"), "project-instructions") |
| 128 |
} |
| 129 |
if opts.SystemPromptAppend != "" { |
| 130 |
prompt += "\n\n" + opts.SystemPromptAppend |
| 131 |
} |
| 132 |
|
| 133 |
return prompt |
| 134 |
} |
| 135 |
|
| 136 |
func unattendedPrompt(prompt string, protocol ActProtocol) string { |
| 137 |
if protocol == ActProtocolClnkrInline { |
| 138 |
prompt = strings.Replace( |
| 139 |
prompt, |
| 140 |
`Set type to exactly one of "act", "clarify", or "done". If type is "act", bash must be an object. If type is "clarify", question must be a non-empty string. If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array.`, |
| 141 |
`Set type to exactly one of "act" or "done". If type is "act", bash must be an object. If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array.`, |
| 142 |
1, |
| 143 |
) |
| 144 |
} else { |
| 145 |
prompt = strings.Replace( |
| 146 |
prompt, |
| 147 |
`For clarification or completion, respond with exactly one JSON object. Set type to exactly one of "clarify" or "done". If type is "clarify", question must be a non-empty string. If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array. Include reasoning in every response; use a string when it helps and null when you have nothing to add.`, |
| 148 |
`For completion, respond with exactly one JSON object. Set type to exactly "done". If type is "done", summary must be a non-empty string, "verification" must include status and checks, and "known_risks" must be an array. Include reasoning in every response; use a string when it helps and null when you have nothing to add.`, |
| 149 |
1, |
| 150 |
) |
| 151 |
} |
| 152 |
prompt = strings.Replace( |
| 153 |
prompt, |
| 154 |
`- If the user has not given you a task, use "clarify" to ask one question. For complex tasks, describe your plan in reasoning before your first command. Stay focused on the task. Do not refactor or improve unrelated code. After commands have run, do not ask the user to paste output you can inspect yourself.`, |
| 155 |
`- You are running unattended. If the task is ambiguous, inspect the environment and make reasonable assumptions. For complex tasks, describe your plan in reasoning before your first command. Stay focused on the task. Do not refactor or improve unrelated code. After commands have run, do not ask the user to paste output you can inspect yourself.`, |
| 156 |
1, |
| 157 |
) |
| 158 |
return prompt + "\n\n<resource-awareness>\nAssume no human rescue during unattended work. Treat resource_state commands_used and model_turns_used as execution pressure. When present, commands_remaining and max_commands describe the command budget, and pressure/guidance describe the host's current budget pressure. Prefer cheap inspection before expensive builds, downloads, training runs, or brute force. When execution pressure is critical, stop broad exploration, produce the smallest viable result, run one cheap check, then finish with honest verification status and known risks.\n</resource-awareness>" |
| 159 |
} |