Skip to content

Commit 6a0da2b

Browse files
committed
Hide the Fence wrapper from opencode's model history
opencode-fence sandboxes bash by rewriting the command to `fence -c '...'`, and OpenCode records that rewrite as the model's own tool call. Models then copy the wrapper or apologise for it, despite AGENTS.md. OpenCode declined to separate executed and recorded input (anomalyco/opencode#37963). fence-unwrap.ts strips the wrapper from the copy of history sent to the model, decoding the quoted argument exactly as sh would and leaving anything else unchanged. Stored history, the UI, and sandboxing are unaffected. AGENTS.md now just says never to add the prefix.
1 parent 1d6d2b3 commit 6a0da2b

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎home/dot_config/opencode/AGENTS.md.tmpl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Notably: `sed -i 's/…/…/' file` (bare `-i`); the BSD `sed -i '' …` is wron
88
{{ end }}
99
## Sandbox (Fence)
1010

11-
Every bash command runs inside a Fence sandbox. It is policy, not a bug:
11+
Every bash command already runs inside a Fence sandbox; never add a `fence`
12+
prefix yourself. The sandbox is policy, not a bug:
1213

1314
- "blocked by sandbox command policy" means the command is denied. Do not
1415
work around it (wrappers, `sh -c`, alternate syntax, invoking `fence`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { Plugin } from "@opencode-ai/plugin"
2+
3+
/**
4+
* Hide the Fence wrapper from the model's view of its own bash history.
5+
*
6+
* The opencode-fence plugin sandboxes each bash call by rewriting
7+
* `args.command` in place to `/opt/homebrew/bin/fence -c '<command>'`.
8+
* OpenCode stores that rewritten input as the tool call, so on later turns
9+
* the model sees calls it never wrote. Models then copy the wrapper into
10+
* new commands, or apologise for "wrapping commands in fence" even though
11+
* AGENTS.md says not to.
12+
*
13+
* This hook runs on the copy of the history that is about to be sent to
14+
* the model (and on the copy used for compaction). It does not change
15+
* what is stored or what the UI shows, and it does not affect sandboxing:
16+
* the command still runs wrapped.
17+
*
18+
* Agent-written wrappers (`fence -c '...'` typed by the model, which Fence
19+
* accepts as already wrapped) are unwrapped too, since they reinforce the
20+
* same habit.
21+
*
22+
* The argument to -c is decoded exactly as sh would, but only when it is
23+
* built from single-quoted segments joined by an escaped quote (\' , "'" ,
24+
* or the plugin's '"'"'). Anything else is left alone.
25+
*/
26+
27+
const WRAPPED = /^(?:\S*\/)?fence -c ('[\s\S]*)$/
28+
// One shell token: a single-quoted segment, \' , or "'" .
29+
const TOKEN = /'([^']*)'|\\'|"'"/y
30+
31+
const decode = (word: string): string | undefined => {
32+
let out = ""
33+
TOKEN.lastIndex = 0
34+
while (TOKEN.lastIndex < word.length) {
35+
const start = TOKEN.lastIndex
36+
const m = TOKEN.exec(word)
37+
if (!m || m.index !== start) return undefined
38+
out += m[1] ?? "'"
39+
}
40+
return out
41+
}
42+
43+
const unwrap = (command: string): string | undefined => {
44+
const m = WRAPPED.exec(command)
45+
return m ? decode(m[1]) : undefined
46+
}
47+
48+
export const FenceUnwrap: Plugin = async () => ({
49+
"experimental.chat.messages.transform": async (_input, output) => {
50+
for (const message of output.messages) {
51+
for (const part of message.parts) {
52+
if (part.type !== "tool" || part.tool !== "bash") continue
53+
const input = part.state.input as { command?: unknown } | undefined
54+
if (typeof input?.command !== "string") continue
55+
const inner = unwrap(input.command)
56+
if (inner !== undefined) input.command = inner
57+
}
58+
}
59+
},
60+
})
61+
62+
export default FenceUnwrap

0 commit comments

Comments
 (0)