opencode-agent-loop-breaker 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +201 -2
- package/docs/analysis.md +458 -0
- package/package.json +29 -4
- package/src/actions.ts +321 -0
- package/src/detectors.ts +57 -0
- package/src/index.ts +102 -0
- package/src/normalize.ts +122 -0
- package/src/options.ts +176 -0
- package/src/runtime.ts +449 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 opencode-agent-loop-breaker contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,202 @@
|
|
|
1
|
-
#
|
|
1
|
+
# opencode-agent-loop-breaker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An OpenCode V2 plugin that stops agent loops. It watches tool calls and model
|
|
4
|
+
steps. When the same work repeats without a successful change, it blocks the
|
|
5
|
+
next call and gives the model a short instruction.
|
|
6
|
+
|
|
7
|
+
It does not compact the session. It does not roll back the conversation. It
|
|
8
|
+
does not change subagent prompts.
|
|
9
|
+
|
|
10
|
+
## Why this plugin exists
|
|
11
|
+
|
|
12
|
+
The plugin `joeyism/opencode-anti-loop` is a V1 plugin. OpenCode V2 uses a new
|
|
13
|
+
plugin API. A V1 plugin does not run in V2. This package uses the native V2 API:
|
|
14
|
+
|
|
15
|
+
- `ctx.tool.hook("execute.before")` to inspect and block a tool call.
|
|
16
|
+
- `ctx.tool.hook("execute.after")` to record the result.
|
|
17
|
+
- `ctx.event.subscribe()` to see `session.step.ended` and `session.idle`.
|
|
18
|
+
- `ctx.session.synthetic()` to send one short notice after truncated output.
|
|
19
|
+
|
|
20
|
+
The analysis of the original plugin is in [docs/analysis.md](docs/analysis.md).
|
|
21
|
+
|
|
22
|
+
## Detectors
|
|
23
|
+
|
|
24
|
+
| Detector | Default | What it sees | State reset |
|
|
25
|
+
|---|---:|---|---|
|
|
26
|
+
| Identical action | 4 | One action signature repeats | Successful change |
|
|
27
|
+
| Action cycle | 3 repeats | A block of 2 to 4 actions repeats | Successful change |
|
|
28
|
+
| Identical output | 3 | One action returns the same output | Successful change |
|
|
29
|
+
| Repeated output | 6 | Different commands return one output | Successful change |
|
|
30
|
+
| Failure streak | 3 | Execution commands fail in a row | Successful execution or change |
|
|
31
|
+
| Test repeat | 3 | One test command runs again | Successful change |
|
|
32
|
+
| File investigation | 10 | One file or target is inspected | Successful change |
|
|
33
|
+
| Total investigation | 30 | All targets are inspected | Successful change |
|
|
34
|
+
| Truncation | 2 steps | Model output hits the token limit | Short notice, then cooldown |
|
|
35
|
+
|
|
36
|
+
A successful `write`, `edit`, or `apply_patch` clears the loop counters. A
|
|
37
|
+
successful mutation through `bash` does too. This is the main rule: progress
|
|
38
|
+
breaks the loop.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
### Local plugin
|
|
43
|
+
|
|
44
|
+
Clone this repository. Install the development dependencies:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
npm install
|
|
48
|
+
npm test
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Link the package into the project plugin directory:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
mkdir -p .opencode/plugins
|
|
55
|
+
ln -s /absolute/path/to/opencode-agent-loop-breaker .opencode/plugins/agent-loop-breaker
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
OpenCode loads the directory from its `package.json` exports. Restart is not
|
|
59
|
+
necessary after a config save. Use `/reload` if the plugin does not appear.
|
|
60
|
+
To use the default options globally, link the same directory into
|
|
61
|
+
`~/.config/opencode/plugins/`. For custom options, use the config entry
|
|
62
|
+
instead of the auto-loaded directory.
|
|
63
|
+
|
|
64
|
+
### Configure options
|
|
65
|
+
|
|
66
|
+
Copy or link the package outside the auto-loaded directory. Then add a config
|
|
67
|
+
entry:
|
|
68
|
+
|
|
69
|
+
```jsonc
|
|
70
|
+
{
|
|
71
|
+
"$schema": "https://opencode.ai/config.json",
|
|
72
|
+
"plugins": [
|
|
73
|
+
{
|
|
74
|
+
"package": "./vendor/opencode-agent-loop-breaker",
|
|
75
|
+
"options": {
|
|
76
|
+
"maxIdenticalActions": 5,
|
|
77
|
+
"maxFailureStreak": 4,
|
|
78
|
+
"debug": false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Do not add the same directory to `.opencode/plugins/` and to `plugins` at the
|
|
86
|
+
same time. The plugin can load two times.
|
|
87
|
+
|
|
88
|
+
## Options
|
|
89
|
+
|
|
90
|
+
| Option | Type | Default | Meaning |
|
|
91
|
+
|---|---|---:|---|
|
|
92
|
+
| `enabled` | boolean | `true` | Turn all detectors off |
|
|
93
|
+
| `maxIdenticalActions` | number | `4` | Repeat limit. `0` disables |
|
|
94
|
+
| `maxCycleRepeats` | number | `3` | Cycle repeat limit. `0` disables |
|
|
95
|
+
| `maxCycleLength` | number | `4` | Longest cycle. `0` disables |
|
|
96
|
+
| `maxIdenticalOutputs` | number | `3` | Same output from one action. `0` disables |
|
|
97
|
+
| `maxRepeatedOutputs` | number | `6` | Same output from many commands. `0` disables |
|
|
98
|
+
| `maxFailureStreak` | number | `3` | Failed commands in a row. `0` disables |
|
|
99
|
+
| `maxTestRuns` | number | `3` | Runs of one test command. `0` disables |
|
|
100
|
+
| `maxFileInvestigations` | number | `10` | Inspections of one target. `0` disables |
|
|
101
|
+
| `maxTotalInvestigations` | number | `30` | All inspections. `0` disables |
|
|
102
|
+
| `onTruncation` | `"notify"` or `"off"` | `"notify"` | Send a notice after truncation |
|
|
103
|
+
| `maxTruncatedSteps` | number | `2` | Truncated steps before a notice |
|
|
104
|
+
| `truncationCooldownMs` | number | `60000` | Minimum time between notices |
|
|
105
|
+
| `ignoreTools` | string array | `["skill","todowrite","todoread","todo_write","todo_read","write_todos","read_todos","question"]` | Tools to ignore |
|
|
106
|
+
| `resetOnIdle` | boolean | `true` | Clear state when the session is idle |
|
|
107
|
+
| `debug` | boolean | `false` | Log blocked calls to stderr |
|
|
108
|
+
|
|
109
|
+
Set a detector limit to `0` to disable that detector. The minimum active limit
|
|
110
|
+
for `maxIdenticalActions`, `maxCycleLength`, `maxCycleRepeats`, and
|
|
111
|
+
`maxIdenticalOutputs` is `2`.
|
|
112
|
+
|
|
113
|
+
## How it works
|
|
114
|
+
|
|
115
|
+
### Tool call before
|
|
116
|
+
|
|
117
|
+
1. The plugin ignores configured tools.
|
|
118
|
+
2. It makes an action signature. The signature uses the tool name and the
|
|
119
|
+
normalized input. A write signature includes the path and a hash of the
|
|
120
|
+
content. A bash signature includes the normalized command.
|
|
121
|
+
3. It classifies the action as `mutate`, `test`, `build`, `investigate`, or
|
|
122
|
+
`execute`.
|
|
123
|
+
4. It checks the failure streak, repeated output, investigation caps, test
|
|
124
|
+
runs, identical output, identical actions, and action cycles.
|
|
125
|
+
5. If a limit is hit, it throws an error. OpenCode blocks the call.
|
|
126
|
+
6. After four blocks, the message adds a request to stop and explain the
|
|
127
|
+
blocker to the user.
|
|
128
|
+
|
|
129
|
+
### Tool call after
|
|
130
|
+
|
|
131
|
+
The plugin records the action. It normalizes the output and stores its hash.
|
|
132
|
+
Line numbers, temporary paths, durations, hex addresses, and the workspace
|
|
133
|
+
prefix do not change the hash.
|
|
134
|
+
|
|
135
|
+
A successful mutation clears the counters. A failed execution command adds one
|
|
136
|
+
to the failure streak.
|
|
137
|
+
|
|
138
|
+
### Model step ended
|
|
139
|
+
|
|
140
|
+
When `finish` is `"length"`, the plugin counts the step. After two steps, it
|
|
141
|
+
sends one synthetic message:
|
|
142
|
+
|
|
143
|
+
> Loop Breaker: the last model steps hit the output limit. Stop reasoning now.
|
|
144
|
+
> Make one small tool call, or give a short answer.
|
|
145
|
+
|
|
146
|
+
The default cooldown is 60 seconds. The message does not run through the prompt
|
|
147
|
+
hook, so it does not start another loop.
|
|
148
|
+
|
|
149
|
+
### Session idle
|
|
150
|
+
|
|
151
|
+
The plugin deletes the session state. Memory stays bounded. At most 200
|
|
152
|
+
sessions are kept.
|
|
153
|
+
|
|
154
|
+
## Difference from the original plugin
|
|
155
|
+
|
|
156
|
+
| Area | `opencode-anti-loop` | This plugin |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| API | V1. Does not run in V2 | Native V2 |
|
|
159
|
+
| Blocking | Throws from V1 before hook | Throws from V2 `execute.before` |
|
|
160
|
+
| Compaction | Automatic on every block | None |
|
|
161
|
+
| Rollback | Optional conversation revert at 10 blocks | None |
|
|
162
|
+
| Subagent prompt | Always injects a TDD prompt | No prompt change |
|
|
163
|
+
| Detectors | 13 documented, 12 in code, all active | 8 loop detectors plus 1 notice |
|
|
164
|
+
| State | One global state | One state per session |
|
|
165
|
+
| Truncation | Hardcoded 16K token threshold | V2 `finish === "length"` |
|
|
166
|
+
| Phase awareness | `AGENT_PHASE` environment variable | Not needed |
|
|
167
|
+
| Logging | None | Optional debug log |
|
|
168
|
+
|
|
169
|
+
The original plugin tries to repair the loop. This plugin tries to stop the
|
|
170
|
+
next harmful call and to return control to the model. It is smaller and less
|
|
171
|
+
intrusive.
|
|
172
|
+
|
|
173
|
+
## Limitations
|
|
174
|
+
|
|
175
|
+
- The detectors are heuristics. They can block valid repeated work. Tune the
|
|
176
|
+
limits for your project.
|
|
177
|
+
- Bash classification uses regular expressions. Wrappers, aliases, and unusual
|
|
178
|
+
scripts can get the wrong class.
|
|
179
|
+
- Output normalization can merge outputs that differ only in numbers, paths, or
|
|
180
|
+
durations.
|
|
181
|
+
- State is in memory. A restart clears it.
|
|
182
|
+
- The plugin does not revert files. It does not undo a bad change.
|
|
183
|
+
- The plugin needs OpenCode `2.4.3` or newer.
|
|
184
|
+
- The plugin is not a security boundary. A model can avoid a detector by
|
|
185
|
+
changing the command. A changed command often means progress.
|
|
186
|
+
- The plugin does not read reasoning text. It uses finish reason and tool
|
|
187
|
+
activity only.
|
|
188
|
+
|
|
189
|
+
## Development
|
|
190
|
+
|
|
191
|
+
```sh
|
|
192
|
+
npm run typecheck
|
|
193
|
+
npm test
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
The core runtime is independent of OpenCode. Tests cover normalization, action
|
|
197
|
+
classification, cycle detection, each detector, state reset, idle cleanup, and
|
|
198
|
+
the truncation cooldown.
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT
|
package/docs/analysis.md
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
# Analysis of `joeyism/opencode-anti-loop`
|
|
2
|
+
|
|
3
|
+
## Scope
|
|
4
|
+
|
|
5
|
+
This document examines the repository at
|
|
6
|
+
<https://github.com/joeyism/opencode-anti-loop>. The clone has package version
|
|
7
|
+
`0.2.2`. The license is MIT. The package has 3,299 lines of source and tests.
|
|
8
|
+
|
|
9
|
+
The package is a plugin for OpenCode V1. It does not run in OpenCode V2. The
|
|
10
|
+
V1 and V2 plugin APIs are different.
|
|
11
|
+
|
|
12
|
+
## Package shape
|
|
13
|
+
|
|
14
|
+
The package has these source files:
|
|
15
|
+
|
|
16
|
+
| File | Lines | Role |
|
|
17
|
+
|---|---:|---|
|
|
18
|
+
| `src/index.ts` | 14 | Entry point. Resolves options and creates hooks |
|
|
19
|
+
| `src/config.ts` | 56 | Defaults and option resolution |
|
|
20
|
+
| `src/types.ts` | 109 | Options, state, and record types |
|
|
21
|
+
| `src/state.ts` | 305 | State updates, caps, action cycle |
|
|
22
|
+
| `src/hooks.ts` | 522 | Event and tool hooks, escalation |
|
|
23
|
+
| `src/command.ts` | 255 | Command intent, targets, normalization |
|
|
24
|
+
| `src/hash.ts` | 24 | SHA-256 and output normalization |
|
|
25
|
+
| `src/errors.ts` | 202 | Block messages |
|
|
26
|
+
| `tests/*.test.ts` | 1,812 | Unit tests for V1 behavior |
|
|
27
|
+
|
|
28
|
+
The entry point is small:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const antiLoop = async (ctx: PluginInput, options?: AntiLoopOptions) => {
|
|
32
|
+
const resolved = resolveOptions(options)
|
|
33
|
+
const state = createState(resolved)
|
|
34
|
+
return createHooks(ctx, state)
|
|
35
|
+
}
|
|
36
|
+
export { antiLoop }
|
|
37
|
+
export default antiLoop
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
One state object serves the whole plugin instance. The state is not keyed by
|
|
41
|
+
session.
|
|
42
|
+
|
|
43
|
+
## V1 hooks
|
|
44
|
+
|
|
45
|
+
The plugin uses three V1 hooks.
|
|
46
|
+
|
|
47
|
+
### `event`
|
|
48
|
+
|
|
49
|
+
The hook looks only at `message.part.updated` with a `step-finish` part. It
|
|
50
|
+
reads:
|
|
51
|
+
|
|
52
|
+
- `part.tokens.output`
|
|
53
|
+
- `part.reason`
|
|
54
|
+
- `part.messageID`
|
|
55
|
+
- `part.sessionID`
|
|
56
|
+
|
|
57
|
+
If `reason === "length"` or output tokens are above 16,000, it sends a V1
|
|
58
|
+
`session.prompt` message. The message tells the model to make a tool call and
|
|
59
|
+
to limit reasoning to 3 to 5 lines.
|
|
60
|
+
|
|
61
|
+
If `allowRollback` is true, the hook also records message IDs for a later
|
|
62
|
+
revert. The revert moves the conversation back two messages. It does not
|
|
63
|
+
restore files.
|
|
64
|
+
|
|
65
|
+
### `tool.execute.before`
|
|
66
|
+
|
|
67
|
+
This is the main detector. The V1 signature is `(input, output)`. The tool
|
|
68
|
+
arguments are in `output.args`. The plugin can change them. It can throw to
|
|
69
|
+
block the call.
|
|
70
|
+
|
|
71
|
+
The hook does this work:
|
|
72
|
+
|
|
73
|
+
1. It skips the `skill` tool.
|
|
74
|
+
2. It reads `AGENT_PHASE` from the environment.
|
|
75
|
+
3. It increments the exploration counter for every other tool.
|
|
76
|
+
4. It builds an action signature.
|
|
77
|
+
5. For bash, it classifies intent, extracts file targets, builds a semantic
|
|
78
|
+
group, and checks investigation caps.
|
|
79
|
+
6. It checks hard loop, timeout, command streak, duplicate test, and
|
|
80
|
+
exploration sprawl.
|
|
81
|
+
7. For write and edit, it hashes the new content and stages a pending write.
|
|
82
|
+
8. For task, it changes the subagent prompt and checks subagent frequency.
|
|
83
|
+
9. It records the action and runs exact and semantic cycle detection.
|
|
84
|
+
|
|
85
|
+
The block path is not uniform:
|
|
86
|
+
|
|
87
|
+
- `handleManualThrow` increments the blocked counter and throws.
|
|
88
|
+
- `throwAfterCompact` increments the blocked counter, compacts the session,
|
|
89
|
+
and throws.
|
|
90
|
+
- Above 5 blocks, it also sends a `SYSTEM OVERRIDE` prompt and resets some
|
|
91
|
+
investigation counters.
|
|
92
|
+
- At 10 blocks, if `allowRollback` is true, it tries a conversation revert.
|
|
93
|
+
|
|
94
|
+
One important detail: `throwAfterCompact` calls `session.summarize` on every
|
|
95
|
+
block, not only after 5 blocks. At 5 to 9 blocks it calls `session.summarize`
|
|
96
|
+
two times. The name suggests a conditional action. The code is unconditional.
|
|
97
|
+
|
|
98
|
+
### `tool.execute.after`
|
|
99
|
+
|
|
100
|
+
This hook records results. It has these duties:
|
|
101
|
+
|
|
102
|
+
- A successful call resets the blocked-call counter.
|
|
103
|
+
- A successful write or edit commits the file hash and sets the first-write
|
|
104
|
+
flag.
|
|
105
|
+
- A failed bash call updates the hard loop streak.
|
|
106
|
+
- A successful or timed-out bash call updates command streaks and test runs.
|
|
107
|
+
- A successful write mutation increments the mutation epoch.
|
|
108
|
+
- Output hashes are normalized and tracked.
|
|
109
|
+
- A global output streak can call the block path after the command already ran.
|
|
110
|
+
|
|
111
|
+
The last point is a design problem. A throw from `tool.execute.after` cannot
|
|
112
|
+
prevent the command that already ran. The hook can only report an error.
|
|
113
|
+
|
|
114
|
+
## Detector inventory
|
|
115
|
+
|
|
116
|
+
The README lists 13 detectors. The source has 12. The advertised zombie-loop
|
|
117
|
+
detector is not in the source. The `maxZombieSteps` option is in the README but
|
|
118
|
+
not in `src/types.ts` or `src/config.ts`.
|
|
119
|
+
|
|
120
|
+
| Detector | Default | Source location | Effect |
|
|
121
|
+
|---|---:|---|---|
|
|
122
|
+
| Duplicate test | 2 runs | `state.ts`, `hooks.ts` | Blocks before run 3 |
|
|
123
|
+
| Command streak | 3 per epoch | `state.ts` | Blocks before run 4 |
|
|
124
|
+
| Identical output per command | 3 | `state.ts` | Blocks before run 4 |
|
|
125
|
+
| Identical output global | 5 | `hooks.ts` after | Throws after the command |
|
|
126
|
+
| Timeout loop | 2 timeouts | `state.ts` | Blocks before next run |
|
|
127
|
+
| Exact action cycle | 6, advisory at 5 | `state.ts` | Blocks before execution |
|
|
128
|
+
| Semantic action cycle | 8, advisory at 7 | `state.ts` | Blocks before execution |
|
|
129
|
+
| Zombie loop | 3 steps | Not present | Documented only |
|
|
130
|
+
| Hard loop | 3 failures | `hooks.ts` | Broken by an off-by-one |
|
|
131
|
+
| Exploration sprawl | 8 before first write, 20 after | `hooks.ts` | Blocks |
|
|
132
|
+
| File investigation | 12 per group, 24 global | `hooks.ts` | Blocks |
|
|
133
|
+
| Subagent loop | 3 spawns, novelty below 0.3 | `hooks.ts` | Blocks spawn 4 |
|
|
134
|
+
| Output truncation | Length or above 16K tokens | `hooks.ts` event | Injects a prompt |
|
|
135
|
+
|
|
136
|
+
### The hard-loop defect
|
|
137
|
+
|
|
138
|
+
The hard-loop detector does not reach its block condition.
|
|
139
|
+
|
|
140
|
+
- A failed bash call checks `hardLoopStreak >= maxHardLoops - 1`.
|
|
141
|
+
- With the default of 3, the third failure sees `2 >= 2`.
|
|
142
|
+
- The hook compacts the session and returns before it calls
|
|
143
|
+
`recordHardLoopViolation`.
|
|
144
|
+
- The streak stays at 2.
|
|
145
|
+
- The before hook checks `hardLoopStreak >= 3`. The condition is false forever.
|
|
146
|
+
|
|
147
|
+
The result is repeated compaction on failures, but no hard-loop block. This is
|
|
148
|
+
an off-by-one defect, not a tuning choice.
|
|
149
|
+
|
|
150
|
+
### The "zombie loop" gap
|
|
151
|
+
|
|
152
|
+
The README says the plugin blocks after 3 steps with zero reasoning tokens. The
|
|
153
|
+
source has no such code. A search for `zombie`, `reasoning`, or
|
|
154
|
+
`maxZombieSteps` finds only the truncation message and README text.
|
|
155
|
+
|
|
156
|
+
This is important for two reasons:
|
|
157
|
+
|
|
158
|
+
1. The advertised behavior is not present.
|
|
159
|
+
2. Zero reasoning tokens are not a reliable loop signal. Some models and
|
|
160
|
+
providers do not report reasoning tokens separately. A detector that uses
|
|
161
|
+
this value can produce false positives.
|
|
162
|
+
|
|
163
|
+
## Normalization
|
|
164
|
+
|
|
165
|
+
### Commands
|
|
166
|
+
|
|
167
|
+
`normalizeCommand` does these steps:
|
|
168
|
+
|
|
169
|
+
- It removes incrementing numbers from bare names. `file12.c` becomes
|
|
170
|
+
`file.c`.
|
|
171
|
+
- It replaces heredoc bodies with a short hash.
|
|
172
|
+
- It collapses whitespace.
|
|
173
|
+
|
|
174
|
+
### Action signatures
|
|
175
|
+
|
|
176
|
+
`normalizeActionSignature` removes numbers from file paths and identifiers.
|
|
177
|
+
This makes `check_weights1.py` and `check_weights2.py` look equal in the
|
|
178
|
+
signature.
|
|
179
|
+
|
|
180
|
+
### Output
|
|
181
|
+
|
|
182
|
+
`normalizeOutputForHashing` removes:
|
|
183
|
+
|
|
184
|
+
- File, line, and column numbers.
|
|
185
|
+
- Compiler gutters.
|
|
186
|
+
- Incrementing filenames.
|
|
187
|
+
- Durations.
|
|
188
|
+
- Temp paths.
|
|
189
|
+
- Hex addresses.
|
|
190
|
+
|
|
191
|
+
The hash is SHA-256.
|
|
192
|
+
|
|
193
|
+
These normalizations are the core of the anti-evasion design. They are also a
|
|
194
|
+
source of false positives. Two outputs that differ only in a count or a path
|
|
195
|
+
can become one key.
|
|
196
|
+
|
|
197
|
+
## Action cycle algorithm
|
|
198
|
+
|
|
199
|
+
`detectActionCycle` runs after each action is recorded. It tries every cycle
|
|
200
|
+
length `L` from 1 to `floor(n / 2)`. The last `L` actions are the pattern. It
|
|
201
|
+
compares that pattern with the blocks before it.
|
|
202
|
+
|
|
203
|
+
A cycle is exact when all of these fields match:
|
|
204
|
+
|
|
205
|
+
- `normalizedSignature`
|
|
206
|
+
- `contentHash`, when both values exist
|
|
207
|
+
- `outputHash`, when both values exist
|
|
208
|
+
- `isTimeout`, when both values exist
|
|
209
|
+
|
|
210
|
+
A cycle is semantic when the `semanticGroup` values match and the same hash
|
|
211
|
+
rules hold. Exact cycles use a lower threshold than semantic cycles. The
|
|
212
|
+
function returns a warning at the configured count and an advisory at count
|
|
213
|
+
minus one. An advisory is appended to the tool output. A warning throws.
|
|
214
|
+
|
|
215
|
+
The action history is capped at 100 records. Recent output hashes are capped at
|
|
216
|
+
20.
|
|
217
|
+
|
|
218
|
+
## Escalation
|
|
219
|
+
|
|
220
|
+
The escalation path is aggressive:
|
|
221
|
+
|
|
222
|
+
| Blocked calls | Action |
|
|
223
|
+
|---:|---|
|
|
224
|
+
| 1 | Compact, then throw |
|
|
225
|
+
| 2 to 4 | Compact, then throw |
|
|
226
|
+
| 5 to 9 | Compact twice, send a `SYSTEM OVERRIDE` prompt, reset investigation counters, then throw |
|
|
227
|
+
| 10 and `allowRollback` off | Same as 5 to 9 |
|
|
228
|
+
| 10 and `allowRollback` on | Revert the conversation two messages, send a note, allow the call |
|
|
229
|
+
|
|
230
|
+
The block counter resets when any tool call succeeds. It is not a lifetime
|
|
231
|
+
counter.
|
|
232
|
+
|
|
233
|
+
The `SYSTEM OVERRIDE` prompt orders a write as the next action. It is a strong
|
|
234
|
+
change to model behavior. It can be correct for a build pipeline, but it is not
|
|
235
|
+
a neutral guardrail.
|
|
236
|
+
|
|
237
|
+
## State
|
|
238
|
+
|
|
239
|
+
`PluginState` holds these maps and counters:
|
|
240
|
+
|
|
241
|
+
- File hashes and sources.
|
|
242
|
+
- Test runs by target.
|
|
243
|
+
- Command streaks by normalized command.
|
|
244
|
+
- Subagent frequencies.
|
|
245
|
+
- File target investigations.
|
|
246
|
+
- Global file investigations.
|
|
247
|
+
- Mutation epoch.
|
|
248
|
+
- Known tracked files.
|
|
249
|
+
- Action history.
|
|
250
|
+
- Hard loop streak.
|
|
251
|
+
- Exploration counters.
|
|
252
|
+
- Blocked-call counter.
|
|
253
|
+
- First-write flag.
|
|
254
|
+
- Agent-written files.
|
|
255
|
+
- Recent output hashes.
|
|
256
|
+
- Message history.
|
|
257
|
+
- First message ID.
|
|
258
|
+
- Rollback flag.
|
|
259
|
+
|
|
260
|
+
The state is in memory. It is not session-scoped. It is not persisted. A
|
|
261
|
+
restart clears it. A long-lived server with many sessions shares one state.
|
|
262
|
+
|
|
263
|
+
## Why the package does not run in V2
|
|
264
|
+
|
|
265
|
+
The package depends on V1 types and V1 runtime behavior:
|
|
266
|
+
|
|
267
|
+
- It imports `@opencode-ai/plugin` and uses `Hooks` and `PluginInput`.
|
|
268
|
+
- It uses `tool.execute.before` and `tool.execute.after` as V1 hooks.
|
|
269
|
+
- It uses `event` as a V1 hook and reads `message.part.updated`.
|
|
270
|
+
- It calls `ctx.client.session.summarize`, `session.prompt`, and
|
|
271
|
+
`session.revert`.
|
|
272
|
+
- It returns a V1 hook object.
|
|
273
|
+
|
|
274
|
+
V2 changes all of these points:
|
|
275
|
+
|
|
276
|
+
- Tool hooks are `ctx.tool.hook("execute.before")` and
|
|
277
|
+
`ctx.tool.hook("execute.after")`.
|
|
278
|
+
- Events come from `ctx.event.subscribe()`.
|
|
279
|
+
- The `message.part.updated` event does not exist. `session.step.ended` has
|
|
280
|
+
the finish reason and token usage.
|
|
281
|
+
- `session.summarize` is replaced by `ctx.session.compact`.
|
|
282
|
+
- `session.prompt` uses a different input shape.
|
|
283
|
+
- `session.revert` is not part of the V2 session API used by this plugin.
|
|
284
|
+
- A V1 hook object is not a V2 server part.
|
|
285
|
+
|
|
286
|
+
A V1 implementation therefore does nothing in V2. It is not a performance
|
|
287
|
+
problem. It is an API mismatch.
|
|
288
|
+
|
|
289
|
+
## Design lessons
|
|
290
|
+
|
|
291
|
+
### What the original does well
|
|
292
|
+
|
|
293
|
+
- It treats loops as a first-class agent failure.
|
|
294
|
+
- It uses more than one signal: actions, outputs, tests, failures, and timing.
|
|
295
|
+
- It normalizes volatile data before it hashes.
|
|
296
|
+
- It separates action identity from semantic intent.
|
|
297
|
+
- It records progress and resets some counters after a write.
|
|
298
|
+
- It has a broad unit test suite for V1 behavior.
|
|
299
|
+
|
|
300
|
+
### What the original does poorly
|
|
301
|
+
|
|
302
|
+
- It targets V1 and cannot run in V2.
|
|
303
|
+
- It compacts on every block.
|
|
304
|
+
- It mixes blocking with prompt injection and conversation rollback.
|
|
305
|
+
- It changes every subagent prompt without consent.
|
|
306
|
+
- It has one global state for all sessions.
|
|
307
|
+
- It has no per-detector off switch.
|
|
308
|
+
- It has no debug log.
|
|
309
|
+
- It has a hard-coded truncation threshold.
|
|
310
|
+
- It depends on an environment variable for phase awareness.
|
|
311
|
+
- It has a broken hard-loop check.
|
|
312
|
+
- It documents a zombie detector that does not exist.
|
|
313
|
+
- It throws from a post-execution hook, where a block cannot work.
|
|
314
|
+
- It uses aggressive messages with uppercase commands and emoji.
|
|
315
|
+
|
|
316
|
+
## Reimplementation choices
|
|
317
|
+
|
|
318
|
+
The new package keeps the useful detection ideas and removes the risky parts.
|
|
319
|
+
|
|
320
|
+
### Kept
|
|
321
|
+
|
|
322
|
+
- Action signatures from tool name and normalized input.
|
|
323
|
+
- Command intent classes.
|
|
324
|
+
- Output hashing with volatile-value removal.
|
|
325
|
+
- Exact action cycles.
|
|
326
|
+
- Identical output checks.
|
|
327
|
+
- Duplicate test checks.
|
|
328
|
+
- Failure streaks.
|
|
329
|
+
- Per-target and total investigation caps.
|
|
330
|
+
- A short truncation notice.
|
|
331
|
+
- A bounded action history.
|
|
332
|
+
|
|
333
|
+
### Removed
|
|
334
|
+
|
|
335
|
+
- Automatic compaction.
|
|
336
|
+
- Conversation rollback.
|
|
337
|
+
- Subagent prompt injection.
|
|
338
|
+
- The `SYSTEM OVERRIDE` prompt.
|
|
339
|
+
- Phase detection through `AGENT_PHASE`.
|
|
340
|
+
- The zombie detector.
|
|
341
|
+
- Global state across sessions.
|
|
342
|
+
- Throwing from `execute.after`.
|
|
343
|
+
|
|
344
|
+
### Added
|
|
345
|
+
|
|
346
|
+
- Per-session state with idle cleanup.
|
|
347
|
+
- A real V2 event subscription.
|
|
348
|
+
- V2 tool hooks.
|
|
349
|
+
- A detector threshold of 0 to disable a detector.
|
|
350
|
+
- A maximum of 200 sessions.
|
|
351
|
+
- An optional debug log.
|
|
352
|
+
- A truncation cooldown.
|
|
353
|
+
- Tests for the new runtime.
|
|
354
|
+
|
|
355
|
+
## V2 runtime sequence
|
|
356
|
+
|
|
357
|
+
```text
|
|
358
|
+
User prompt
|
|
359
|
+
|
|
|
360
|
+
v
|
|
361
|
+
Model step
|
|
362
|
+
|
|
|
363
|
+
+--> session.step.ended (finish = "length")
|
|
364
|
+
| |
|
|
365
|
+
| +--> count truncated steps
|
|
366
|
+
| +--> after 2 steps, send one synthetic notice
|
|
367
|
+
|
|
|
368
|
+
+--> tool.execute.before
|
|
369
|
+
|
|
|
370
|
+
+--> ignore configured tools
|
|
371
|
+
+--> build action descriptor
|
|
372
|
+
+--> check failure streak
|
|
373
|
+
+--> check repeated output
|
|
374
|
+
+--> check investigation caps
|
|
375
|
+
+--> check test runs
|
|
376
|
+
+--> check identical action
|
|
377
|
+
+--> check action cycle
|
|
378
|
+
+--> check identical output
|
|
379
|
+
|
|
|
380
|
+
+--> allow: store pending action
|
|
381
|
+
+--> block: throw Error with a short reason
|
|
382
|
+
|
|
|
383
|
+
+--> tool.execute.after
|
|
384
|
+
|
|
|
385
|
+
+--> record action and output hash
|
|
386
|
+
+--> failed execution: failure streak + 1
|
|
387
|
+
+--> successful execution: failure streak = 0
|
|
388
|
+
+--> successful mutation: clear loop counters
|
|
389
|
+
|
|
|
390
|
+
v
|
|
391
|
+
session.idle
|
|
392
|
+
|
|
|
393
|
+
+--> delete session state
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## New detector semantics
|
|
397
|
+
|
|
398
|
+
| Detector | Condition | Reset |
|
|
399
|
+
|---|---|---|
|
|
400
|
+
| Identical action | Same signature `N` times | Mutation |
|
|
401
|
+
| Action cycle | A block of 1 to 4 actions repeats `N` times | Mutation |
|
|
402
|
+
| Identical output | One action returns one output hash `N` times | Mutation |
|
|
403
|
+
| Repeated output | `N` commands return one output hash | Mutation |
|
|
404
|
+
| Failure streak | `N` failed execution commands in a row | Execution success or mutation |
|
|
405
|
+
| Test repeat | One test key runs `N` times | Mutation |
|
|
406
|
+
| File investigation | One target reaches `N` inspections | Mutation |
|
|
407
|
+
| Total investigation | All targets reach `N` inspections | Mutation |
|
|
408
|
+
| Truncation | `N` steps end with `finish: "length"` | Cooldown |
|
|
409
|
+
|
|
410
|
+
A mutation is a successful `write`, `edit`, or `apply_patch`. A successful bash
|
|
411
|
+
mutation is also a mutation. The bash classifier sees redirection, `tee`,
|
|
412
|
+
`sed -i`, file operations, package installs, and git write commands.
|
|
413
|
+
|
|
414
|
+
## Block message
|
|
415
|
+
|
|
416
|
+
A block uses this shape:
|
|
417
|
+
|
|
418
|
+
```text
|
|
419
|
+
Loop Breaker blocked this tool call.
|
|
420
|
+
Reason: <what repeated>
|
|
421
|
+
<one action to take>
|
|
422
|
+
<escalation after 4 blocks>
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
The escalation at 4 blocks tells the model to stop and explain the blocker to
|
|
426
|
+
the user. It does not compact, revert, or change the prompt.
|
|
427
|
+
|
|
428
|
+
## Test strategy
|
|
429
|
+
|
|
430
|
+
The new package has tests for:
|
|
431
|
+
|
|
432
|
+
- Option defaults and clamping.
|
|
433
|
+
- Disabling a detector with 0.
|
|
434
|
+
- Command normalization.
|
|
435
|
+
- Output normalization and fingerprints.
|
|
436
|
+
- Deterministic JSON.
|
|
437
|
+
- Bash intent classification.
|
|
438
|
+
- Action signatures for write, edit, read, grep, and task.
|
|
439
|
+
- Cycle detection for one-step and multi-step cycles.
|
|
440
|
+
- Each detector and its reason.
|
|
441
|
+
- Mutation reset.
|
|
442
|
+
- Failure streak reset.
|
|
443
|
+
- Idle cleanup.
|
|
444
|
+
- Ignored tools.
|
|
445
|
+
- Truncation notice and cooldown.
|
|
446
|
+
|
|
447
|
+
The runtime is independent of OpenCode. The plugin file is a thin adapter.
|
|
448
|
+
|
|
449
|
+
## Conclusion
|
|
450
|
+
|
|
451
|
+
`opencode-anti-loop` is a serious V1 attempt at a hard problem. Its detection
|
|
452
|
+
ideas are useful. Its execution model is not compatible with V2, and several
|
|
453
|
+
behaviors are too invasive: compaction on every block, prompt injection,
|
|
454
|
+
rollback, global state, and subagent prompt changes.
|
|
455
|
+
|
|
456
|
+
The new plugin keeps the detection core, uses the V2 API, and makes the
|
|
457
|
+
guardrail smaller. It blocks the next call. It records progress. It resets on
|
|
458
|
+
progress. It does not repair the conversation.
|