From 8640ea3374bf85520b216460fa5212bc8e5bc159 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 14 Aug 2026 18:39:27 -0500 Subject: [PATCH] minimize system prompt (#42638) --- packages/core/src/session/generate-node.ts | 9 +- packages/core/src/session/model-request.ts | 9 +- .../core/src/session/runner/prompt/base.txt | 95 ------------------- .../core/src/session/runner/prompt/system.txt | 14 +++ packages/core/src/session/system-prompt.ts | 24 +++++ .../core/test/plugin/system-prompt.test.ts | 6 +- packages/core/test/session-runner.test.ts | 4 +- .../core/test/session-system-prompt.test.ts | 8 ++ 8 files changed, 65 insertions(+), 104 deletions(-) delete mode 100644 packages/core/src/session/runner/prompt/base.txt create mode 100644 packages/core/src/session/runner/prompt/system.txt create mode 100644 packages/core/src/session/system-prompt.ts create mode 100644 packages/core/test/session-system-prompt.test.ts diff --git a/packages/core/src/session/generate-node.ts b/packages/core/src/session/generate-node.ts index 2ecf94d44b3..1aa1afd85fd 100644 --- a/packages/core/src/session/generate-node.ts +++ b/packages/core/src/session/generate-node.ts @@ -13,7 +13,7 @@ import { SessionHistory } from "./history.js" import { SessionModelHeaders } from "./model-headers.js" import { SessionPromptCacheKey } from "./prompt-cache-key.js" import { SessionRunnerModel } from "./runner/model.js" -import PROMPT_DEFAULT from "./runner/prompt/base.txt" +import { SessionSystemPrompt } from "./system-prompt.js" import { toLLMMessages } from "./runner/to-llm-message.js" export const layer = Layer.effect( @@ -39,7 +39,12 @@ export const layer = Layer.effect( sessionID: selection.session.id, agent: selection.agent.id, model: model.ref, - system: [selection.agent.info.system ? selection.agent.info.system : PROMPT_DEFAULT, history.initial] + system: [ + selection.agent.info.system + ? selection.agent.info.system + : SessionSystemPrompt.make(toolDefinitions.map((tool) => tool.name)), + history.initial, + ] .filter((part) => part.length > 0) .map(SystemPart.make), messages: [ diff --git a/packages/core/src/session/model-request.ts b/packages/core/src/session/model-request.ts index 06174bc73d6..71b86fb73b0 100644 --- a/packages/core/src/session/model-request.ts +++ b/packages/core/src/session/model-request.ts @@ -19,7 +19,7 @@ import { SessionModelTransport } from "./model-transport.js" import { SessionPromptCacheKey } from "./prompt-cache-key.js" import { PromptCacheDiagnostics } from "./prompt-cache-diagnostics.js" import { MAX_STEPS_PROMPT } from "./runner/max-steps.js" -import PROMPT_DEFAULT from "./runner/prompt/base.txt" +import { SessionSystemPrompt } from "./system-prompt.js" import { toLLMMessages } from "./runner/to-llm-message.js" const IMAGE_BYTES_TRIGGER = 25 * 1024 * 1024 // 25 MiB @@ -190,7 +190,12 @@ export const layer = Layer.effect( // The final Step keeps definitions available to protocols with native "none", // preserving their prompt cache prefix. Calls are still rejected at execution. const tools = input.context.tools - const system = [agent.info.system ? agent.info.system : PROMPT_DEFAULT, input.context.initial] + const system = [ + agent.info.system + ? agent.info.system + : SessionSystemPrompt.make(tools.definitions.map((tool) => tool.name)), + input.context.initial, + ] .filter((part) => part.length > 0) .map(SystemPart.make) const history = toLLMMessages(input.context.messages, resolved.ref, providerMetadataKey) diff --git a/packages/core/src/session/runner/prompt/base.txt b/packages/core/src/session/runner/prompt/base.txt deleted file mode 100644 index 754d040166d..00000000000 --- a/packages/core/src/session/runner/prompt/base.txt +++ /dev/null @@ -1,95 +0,0 @@ -You are opencode, an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user. - -IMPORTANT: You must NEVER generate or guess URLs for the user unless you are confident that the URLs are for helping the user with programming. You may use URLs provided by the user in their messages or local files. - -If the user asks for help or wants to give feedback inform them of the following: -- /help: Get help with using opencode -- To give feedback, users should report the issue at https://github.com/anomalyco/opencode/issues - -When the user directly asks about opencode (eg 'can opencode do...', 'does opencode have...') or asks in second person (eg 'are you able...', 'can you do...'), first use the webfetch tool to gather information to answer the question from opencode docs at https://opencode.ai/v2/docs/ - -# Tone and style -You should be concise, direct, and to the point. When you run a non-trivial shell 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). -Remember that your output will be displayed on a command line interface. Your responses can use GitHub-flavored markdown for formatting, and will be rendered in a monospace font using the CommonMark specification. -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 the shell tool or code comments as means to communicate with the user during the session. -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. -Only use emojis if the user explicitly requests it. Avoid using emojis in all communication unless asked. -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. -IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to. -IMPORTANT: Keep your responses short, since they will be displayed on a command line interface. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is .", "Here is the content of the file..." 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: - -user: what is 2+2? -assistant: 4 - - - -user: is 11 a prime number? -assistant: Yes - - - -user: what command should I run to list files in the current directory? -assistant: ls - - - -user: what command should I run to watch files in the current directory? -assistant: [use the read tool to list the files in the current directory, then read docs/commands in the relevant file to find out how to watch files] -npm run dev - - - -user: what files are in the directory src/? -assistant: [uses read and sees foo.c, bar.c, baz.c] -user: which file contains the implementation of foo? -assistant: src/foo.c - - - -user: write tests for new feature -assistant: [uses grep and glob search tools to find where similar tests are defined, uses concurrent read file tool use blocks in one tool call to read relevant files at the same time, uses edit file tool to write new tests] - - -# Proactiveness -You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between: -1. Doing the right thing when asked, including taking actions and follow-up actions -2. Not surprising the user with actions you take without asking -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. -3. Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did. - -# Following conventions -When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns. -- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language). -- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions. -- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic. -- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository. - -# Code style -- IMPORTANT: DO NOT ADD ***ANY*** COMMENTS unless asked - -# Doing tasks -The user will primarily request you perform software engineering tasks. This includes solving bugs, adding new functionality, refactoring code, explaining code, and more. For these tasks the following steps are recommended: -- Use the available search tools to understand the codebase and the user's query. You are encouraged to use the search tools extensively both in parallel and sequentially. -- Implement the solution using all tools available to you -- Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the README or search codebase to determine the testing approach. -- VERY IMPORTANT: When you have completed a task, you MUST run the lint and typecheck commands (e.g. npm run lint, npm run typecheck, ruff, etc.) with the shell tool if they were provided to you to ensure your code is correct. If you are unable to find the correct command, ask the user for the command to run and if they supply it, proactively suggest writing it to AGENTS.md so that you will know to run it next time. -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. - -- Tool results and user messages may include tags. tags contain useful information and reminders. They are NOT part of the user's provided input or the tool result. - -# Tool usage policy -- When doing file search, prefer to use the subagent tool in order to reduce context usage. -- You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. When making multiple shell tool calls, you MUST send a single message with multiple tools calls to run the calls in parallel. For example, if you need to run "git status" and "git diff", send a single message with two tool calls to run the calls in parallel. - -You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail. - -IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure. - -# Code References - -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 code location. - - -user: Where are errors from the client handled? -assistant: Clients are marked as failed in the `connectToServer` function in src/services/process.ts:712. - diff --git a/packages/core/src/session/runner/prompt/system.txt b/packages/core/src/session/runner/prompt/system.txt new file mode 100644 index 00000000000..4590fcbad43 --- /dev/null +++ b/packages/core/src/session/runner/prompt/system.txt @@ -0,0 +1,14 @@ +You are an AI agent powered by OpenCode, a coding agent harness. Help the user accomplish their goals using the tools you have available. + +# Harness +- Responses are rendered as GitHub-flavored Markdown. +- `` blocks are harness instructions, not user-authored content. Read and follow them. +${OPENCODE_TOOL_GUIDANCE} + +# Communication +- Use clear file paths when referring to files. +- Keep responses clear and concise, and avoid unnecessary technical jargon. + +# Working in codebases +- Keep changes consistent with the structure, naming, style, and patterns of the surrounding code. +- Treat unfamiliar files or changes as potential user work and investigate before deleting or overwriting them. diff --git a/packages/core/src/session/system-prompt.ts b/packages/core/src/session/system-prompt.ts new file mode 100644 index 00000000000..a480ed2ca1d --- /dev/null +++ b/packages/core/src/session/system-prompt.ts @@ -0,0 +1,24 @@ +export * as SessionSystemPrompt from "./system-prompt.js" + +import PROMPT from "./runner/prompt/system.txt" + +export function make(tools: string[]) { + const instructions: string[] = [] + if (tools.includes("write")) { + instructions.push( + "- Use the write tool to create files or completely replace their content. Prefer using the edit tool for targeted changes.", + ) + } + if (tools.includes("edit")) { + instructions.push( + "- Use the edit tool for targeted changes to existing text files. It replaces the exact text in `oldString` with `newString`, and the values must differ. By default, `oldString` must occur exactly once. If it occurs multiple times, include more surrounding context to make it unique or set `replaceAll` to true to replace every occurrence.", + ) + } + // if (tools.includes("patch")) { + // // instructions.push(...) + // } + if (tools.includes("read")) { + instructions.push("- Prefer using the read tool rather than shell commands like `cat`.") + } + return PROMPT.replace("${OPENCODE_TOOL_GUIDANCE}", instructions.join("\n")) +} diff --git a/packages/core/test/plugin/system-prompt.test.ts b/packages/core/test/plugin/system-prompt.test.ts index c3246752919..028b8414bf6 100644 --- a/packages/core/test/plugin/system-prompt.test.ts +++ b/packages/core/test/plugin/system-prompt.test.ts @@ -7,6 +7,7 @@ import { PluginHooks } from "@opencode-ai/core/plugin/hooks" import { PluginHost } from "@opencode-ai/core/plugin/host" import { SystemPromptPlugin } from "@opencode-ai/core/plugin/system-prompt" import { Session } from "@opencode-ai/core/session" +import { SessionSystemPrompt } from "@opencode-ai/core/session/system-prompt" import type { SessionHooks } from "@opencode-ai/plugin/effect/session" import { Model } from "@opencode-ai/schema/model" import { Provider } from "@opencode-ai/schema/provider" @@ -14,10 +15,9 @@ import { Effect } from "effect" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "./fixture" import PROMPT_META from "../../src/plugin/system-prompt/meta.txt" -import PROMPT_DEFAULT from "../../src/session/runner/prompt/base.txt" const it = testEffect(PluginTestLayer) -const fallback = PROMPT_DEFAULT +const fallback = SessionSystemPrompt.make([]) const makeHost = Effect.gen(function* () { const agents = yield* Agent.Service const plugins = yield* Plugin.Service @@ -74,7 +74,7 @@ describe("SystemPromptPlugin", () => { ["kimi-k2", "# Prompt and Tool Use"], ["trinity", "what command should I run to list files"], ["meta/muse-spark-1.1", "powered by Muse Spark"], - ["llama-3.3", "You are opencode, an interactive CLI tool"], + ["llama-3.3", fallback], ] as const yield* Effect.forEach( diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 1e6d502de8a..773bc8558ce 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -71,6 +71,7 @@ import { InstructionDiscovery } from "@opencode-ai/core/instruction-discovery" import { SkillInstructions } from "@opencode-ai/core/skill/instructions" import { ReferenceInstructions } from "@opencode-ai/core/reference/instructions" import { McpInstructions } from "@opencode-ai/core/mcp/instructions" +import { SessionSystemPrompt } from "@opencode-ai/core/session/system-prompt" import { ID } from "@opencode-ai/core/model" import { Location } from "@opencode-ai/core/location" import { Provider } from "@opencode-ai/core/provider" @@ -81,7 +82,6 @@ import { asc, desc, eq } from "drizzle-orm" import { testEffect } from "./lib/effect" import { permissionLayer } from "./lib/permission" import { agentHost, catalogHost, host } from "./plugin/host" -import PROMPT_DEFAULT from "../src/session/runner/prompt/base.txt" import { CodeModeInstructions } from "@opencode-ai/core/codemode/instructions" let requests: LLMRequest[] = [] @@ -147,7 +147,7 @@ const modelTransport = Layer.succeed( }), ) const model = LanguageModel.make({ id: "fake-model", provider: "fake", route: OpenAIChat.route }) -const defaultSystem = PROMPT_DEFAULT +const defaultSystem = SessionSystemPrompt.make([]) const replacementModel = LanguageModel.make({ id: "replacement", provider: "fake", route: OpenAIChat.route }) const compactModel = LanguageModel.make({ id: "compact", diff --git a/packages/core/test/session-system-prompt.test.ts b/packages/core/test/session-system-prompt.test.ts new file mode 100644 index 00000000000..cfa29702b35 --- /dev/null +++ b/packages/core/test/session-system-prompt.test.ts @@ -0,0 +1,8 @@ +import { expect, test } from "bun:test" +import { SessionSystemPrompt } from "@opencode-ai/core/session/system-prompt" + +test("renders the default system prompt instructions", () => { + const prompt = SessionSystemPrompt.make(["edit", "read", "shell"]) + expect(prompt).not.toContain("${OPENCODE_TOOL_GUIDANCE}") + expect(prompt).toContain("Use the edit tool for targeted changes to existing text files") +})