Skip to content

Commit 68017a0

Browse files
committed
feat(opencode): add isReadOnly and isDestructive flags to tool interface
Add optional isReadOnly and isDestructive metadata to Tool.Def interface. Applied to 11 built-in tools: Read-only: read, glob, grep, list, webfetch, codesearch Destructive: write, edit, apply_patch, multiedit Neutral: todowrite (isReadOnly: false) These flags enable permission systems and auto-approval classifiers to make smarter decisions without analyzing tool behavior at runtime. Closes anomalyco#20296
1 parent 85c1692 commit 68017a0

12 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎packages/opencode/src/tool/apply_patch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const PatchParams = z.object({
2121

2222
export const ApplyPatchTool = Tool.define("apply_patch", {
2323
description: DESCRIPTION,
24+
isDestructive: true,
2425
parameters: PatchParams,
2526
async execute(params, ctx) {
2627
if (!params.patchText) {

‎packages/opencode/src/tool/codesearch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface McpCodeResponse {
3535

3636
export const CodeSearchTool = Tool.define("codesearch", {
3737
description: DESCRIPTION,
38+
isReadOnly: true,
3839
parameters: z.object({
3940
query: z
4041
.string()

‎packages/opencode/src/tool/edit.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function convertToLineEnding(text: string, ending: "\n" | "\r\n"): string {
3636

3737
export const EditTool = Tool.define("edit", {
3838
description: DESCRIPTION,
39+
isDestructive: true,
3940
parameters: z.object({
4041
filePath: z.string().describe("The absolute path to the file to modify"),
4142
oldString: z.string().describe("The text to replace"),

‎packages/opencode/src/tool/glob.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { assertExternalDirectory } from "./external-directory"
99

1010
export const GlobTool = Tool.define("glob", {
1111
description: DESCRIPTION,
12+
isReadOnly: true,
1213
parameters: z.object({
1314
pattern: z.string().describe("The glob pattern to match files against"),
1415
path: z

‎packages/opencode/src/tool/grep.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const MAX_LINE_LENGTH = 2000
1414

1515
export const GrepTool = Tool.define("grep", {
1616
description: DESCRIPTION,
17+
isReadOnly: true,
1718
parameters: z.object({
1819
pattern: z.string().describe("The regex pattern to search for in file contents"),
1920
path: z.string().optional().describe("The directory to search in. Defaults to the current working directory."),

‎packages/opencode/src/tool/ls.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const LIMIT = 100
3737

3838
export const ListTool = Tool.define("list", {
3939
description: DESCRIPTION,
40+
isReadOnly: true,
4041
parameters: z.object({
4142
path: z.string().describe("The absolute path to the directory to list (must be absolute, not relative)").optional(),
4243
ignore: z.array(z.string()).describe("List of glob patterns to ignore").optional(),

‎packages/opencode/src/tool/multiedit.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Instance } from "../project/instance"
77

88
export const MultiEditTool = Tool.define("multiedit", {
99
description: DESCRIPTION,
10+
isDestructive: true,
1011
parameters: z.object({
1112
filePath: z.string().describe("The absolute path to the file to modify"),
1213
edits: z

‎packages/opencode/src/tool/read.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const MAX_BYTES_LABEL = `${MAX_BYTES / 1024} KB`
2020

2121
export const ReadTool = Tool.define("read", {
2222
description: DESCRIPTION,
23+
isReadOnly: true,
2324
parameters: z.object({
2425
filePath: z.string().describe("The absolute path to the file or directory to read"),
2526
offset: z.coerce.number().describe("The line number to start reading from (1-indexed)").optional(),

‎packages/opencode/src/tool/todo.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Todo } from "../session/todo"
55

66
export const TodoWriteTool = Tool.define("todowrite", {
77
description: DESCRIPTION_WRITE,
8+
isReadOnly: false,
89
parameters: z.object({
910
todos: z.array(z.object(Todo.Info.shape)).describe("The updated todo list"),
1011
}),

‎packages/opencode/src/tool/tool.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export namespace Tool {
2828
export interface Def<Parameters extends z.ZodType = z.ZodType, M extends Metadata = Metadata> {
2929
description: string
3030
parameters: Parameters
31+
/** Whether this tool only reads data (never modifies state). Used for permission auto-approval. */
32+
isReadOnly?: boolean
33+
/** Whether this tool can destruct or irreversibly modify state. Used for elevated permission prompts. */
34+
isDestructive?: boolean
3135
execute(
3236
args: z.infer<Parameters>,
3337
ctx: Context,

0 commit comments

Comments
 (0)