Skip to content

Commit 47bfae5

Browse files
fix: permission checks for external_directory and doom_loop (#4433)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: AerionDyseti <AerionDyseti@users.noreply.github.com>
1 parent 52cf9e3 commit 47bfae5

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

packages/opencode/src/permission/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ export namespace Permission {
186186
public readonly permissionID: string,
187187
public readonly toolCallID?: string,
188188
public readonly metadata?: Record<string, any>,
189+
public readonly reason?: string,
189190
) {
190-
super(`The user rejected permission to use this specific tool call. You may try again with different parameters.`)
191+
super(
192+
reason !== undefined
193+
? reason
194+
: `The user rejected permission to use this specific tool call. You may try again with different parameters.`,
195+
)
191196
}
192197
}
193198
}

packages/opencode/src/session/processor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export namespace SessionProcessor {
136136

137137
const parts = await MessageV2.parts(input.assistantMessage.id)
138138
const lastThree = parts.slice(-DOOM_LOOP_THRESHOLD)
139+
139140
if (
140141
lastThree.length === DOOM_LOOP_THRESHOLD &&
141142
lastThree.every(
@@ -160,6 +161,17 @@ export namespace SessionProcessor {
160161
input: value.input,
161162
},
162163
})
164+
} else if (permission.doom_loop === "deny") {
165+
throw new Permission.RejectedError(
166+
input.assistantMessage.sessionID,
167+
"doom_loop",
168+
value.toolCallId,
169+
{
170+
tool: value.toolName,
171+
input: value.input,
172+
},
173+
`You seem to be stuck in a doom loop, please stop repeating the same action`,
174+
)
163175
}
164176
}
165177
}

packages/opencode/src/tool/edit.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export const EditTool = Tool.define("edit", {
5757
parentDir,
5858
},
5959
})
60+
} else if (agent.permission.external_directory === "deny") {
61+
throw new Permission.RejectedError(
62+
ctx.sessionID,
63+
"external_directory",
64+
ctx.callID,
65+
{
66+
filepath: filePath,
67+
parentDir,
68+
},
69+
`File ${filePath} is not in the current working directory`,
70+
)
6071
}
6172
}
6273

packages/opencode/src/tool/patch.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ export const PatchTool = Tool.define("patch", {
6868
parentDir,
6969
},
7070
})
71+
} else if (agent.permission.external_directory === "deny") {
72+
throw new Permission.RejectedError(
73+
ctx.sessionID,
74+
"external_directory",
75+
ctx.callID,
76+
{
77+
filepath: filePath,
78+
parentDir,
79+
},
80+
`File ${filePath} is not in the current working directory`,
81+
)
7182
}
7283
}
7384

packages/opencode/src/tool/read.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ export const ReadTool = Tool.define("read", {
4646
parentDir,
4747
},
4848
})
49+
} else if (agent.permission.external_directory === "deny") {
50+
throw new Permission.RejectedError(
51+
ctx.sessionID,
52+
"external_directory",
53+
ctx.callID,
54+
{
55+
filepath: filepath,
56+
parentDir,
57+
},
58+
`File ${filepath} is not in the current working directory`,
59+
)
4960
}
5061
}
5162

packages/opencode/src/tool/write.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ export const WriteTool = Tool.define("write", {
3636
parentDir,
3737
},
3838
})
39+
} else if (agent.permission.external_directory === "deny") {
40+
throw new Permission.RejectedError(
41+
ctx.sessionID,
42+
"external_directory",
43+
ctx.callID,
44+
{
45+
filepath: filepath,
46+
parentDir,
47+
},
48+
`File ${filepath} is not in the current working directory`,
49+
)
3950
}
4051
}
4152

0 commit comments

Comments
 (0)