Patchflow
Anchor-based atomic batch edits for OpenCode: one auditable tool call instead of opaque python heredocs or N edit calls.
0
584
近 7 天 584
37.0
生态多维模型
7 天前
2026-10-13
快速安装与配置
opencode.json写入当前项目的 opencode.json,只对这个仓库生效。
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-patchflow@0.1.3"]
}写入 ~/.config/opencode/opencode.json,对所有项目生效。
~/.config/opencode/opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-patchflow@0.1.3"]
}若你要在本地改造这个插件,先装到项目里再从本地路径引用。
shell
pnpm add -D opencode-patchflowOpenCode 启动时会通过内嵌运行时自动加载 npm 依赖并缓存至本地目录,无需手动在全局环境执行安装。
Anchor-based atomic batch edits for OpenCode: one auditable tool call where
the agent would otherwise smuggle writes through shell or chain N fragile
edit calls.
Requires OpenCode 2.x. Built against
@opencode/plugin2.x and the V2 plugin API only (Plugin.define,toolandpermissionhooks). There is no 1.x support.
Agents under pressure edit files the way that costs the fewest round-trips,
which usually means a Python heredoc through shell:
python3 - <<'EOF'
s = open(p).read()
s = s.replace(anchor, anchor + add)
open(p, 'w').write(s)
EOF
One call, but opaque to permissions, review, and diff logging — and silent
about partial failure. The honest alternative, chaining N edit calls,
restores the audit trail at N times the latency and N exact-match
rollbacks. patchflow_apply keeps the single-call ergonomics with the
trail intact: each edit names a file, an anchor, and the text to insert
after it or replace it with, asserting the anchor occurs exactly
expectCount times (default 1). Every edit in the call validates before
anything is written, so a batch lands atomically per file group instead of
halfway. dryRun: true previews the whole batch without touching disk, and
one call can span as many files as the change needs.
// One tool call, two files, asserted anchors:
patchflow_apply({
edits: [
{ path: "src/a.rs", anchor: "assert!(x.is_none());\n", text: "// checked\n" },
{ path: "src/b.rs", anchor: "v=1", text: "v=2", mode: "replace", expectCount: 2 },
],
dryRun: true,
});
The plugin also watches shell for the heredoc habit itself. When a
command hides a file write — open(p, 'w') in Python, sed -i, perl -pi, tee, a bare >/>> redirect — it logs a [patchflow] covert-write (…) observed line, and when that request already resolves to
ask, it appends a pointer to patchflow_apply. It never allows what your
rules deny and never denies what they allow; it is a nudge, not a gate, so
pair it with a real policy engine where enforcement matters.
// opencode.jsonc
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["opencode-patchflow"]
}
Two options tune the monitor,
{ "monitorOnly": true, "suggestOnAsk": true }. The first keeps the plugin
to observation, never touching a permission verdict (static deny rules
win regardless). The second controls the pointer appended to
already-asking shell requests. A third option, { "metrics": true },
keeps session-aggregated counters — successful applies and dry runs with
edit and file totals, errors, observed covert writes, and built-in edit
calls as the comparison baseline — in
~/.local/share/opencode/log/patchflow-metrics.json. Only counts, never
commands or contents; at most twenty sessions are kept, oldest first out,
with cumulative totals beside them. It defaults to off. Local development points at the source
directly:
{ "plugins": [{ "package": "/home/mateo/opencode-patchflow/src/index.ts", "options": {} }] }
Development
npm install
npm test # node --test, no build step
npm run typecheck
Needs Node 22 or newer: the test files run straight through type stripping, with no compilation involved.
License
MIT
同类生态推荐
Better Hashline
opencode-better-hashline
Fail-closed, snapshot-bound line editing for OpenCode
Scriptbox
opencode-scriptbox
Closed-world registry of reusable agent utility scripts for OpenCode: descriptive names plus one-line purpose headers, listed on demand.
Startup Commands
opencode-startup-commands
Launch trusted background commands when OpenCode initializes a project or directory.