Commit df220e8
authored
feat: fingerprint-guarded session reuse as default (session: "auto") (#18)
* feat: fingerprint-guarded session reuse as default (session: "auto")
Previously the provider created a fresh Cursor agent every turn and
re-sent the whole transcript: robust but cache-hostile, increasingly
costly as a conversation grows, and it paid opencode's system prompt on
top of Cursor's own. Opt-in `session: true` resumed one agent per session
but blindly: it could drift from opencode's view of history (message
edits, reverts, opencode-side compaction) and was polluted by non-chat
side calls (e.g. title generation).
Add `session: "auto"` (new default) which hashes only the parts opencode
replays verbatim — the system prompt and the user-message sequence — and
classifies each turn:
- new -> fresh agent, full transcript, pool it
- side-call -> system prompt differs; fresh ephemeral agent, pool untouched
- continuation -> prior user seq is an exact prefix + exactly one new user
message; resume the pooled agent, send only the new message
- divergence -> edit/revert/compaction/queued msgs (or failed resume);
fresh agent + full transcript, re-pool
Worst case on any misclassification is one self-healing full replay —
never worse than the old default. `session: true` is now an alias for
"auto"; `session: false` keeps the always-fresh behavior.
Also include tool outputs (truncated: 2000 chars/result, 500/args) in the
flattened transcript so fresh/divergence/`session: false` replays stay
faithful instead of dropping prior tool results to `[result of X]`
placeholders.
OPENCODE_CURSOR_DEBUG=1 logs per-turn classification and cache usage.
Adds transcript-fingerprint unit tests, rewrites session-pool tests for
the decision-driven acquireAgent contract, and adds a live
session-reuse smoke script.
* feat: re-forward live MCP servers per turn with OAuth mapping
The config hook only snapshots opencode's MCP set once at startup, so
mid-session enable/disable never reached the Cursor agent. Re-forward the
live set from chat.params using client.mcp.status() (runtime truth) +
client.config.get() (launch specs), and force a fresh Agent.create when the
forwarded set changes between turns (a resumed agent keeps its original
servers).
Map remote OAuth client registration (clientId/clientSecret/scope) onto the
Cursor SDK's auth block so the agent runs its own OAuth flow. opencode's
access token never lands in config.mcp, so servers needing OAuth without a
shareable clientId (dynamic registration / needs_auth) are skipped and the
user is notified via a one-time toast instead of forwarding a spec that 401s.
* feat: persist session pool records so reuse survives restarts
The pool was in-memory only: an opencode restart lost the fingerprint
records, so the first turn of every resumed conversation classified as
"new" and paid a cache-cold full-transcript replay even though the
Cursor agent (and its conversation, in Cursor's checkpoint store) was
still resumable.
Persist the records best-effort to
~/.cache/opencode-cursor/session-pool.json following the model-cache
pattern (never throws, optimization-only). Records carry updatedAt and
are pruned to a 7-day TTL and a 200-entry most-recently-used cap. The
in-memory pool lazily hydrates from disk (memory wins on conflict);
concurrent opencode processes are last-write-wins on the whole file,
where a lost record costs exactly one self-healing full replay.
clearAgentPool() now wipes the disk store too; a new
resetSessionPoolMemory() test hook simulates process restarts.
* fix(stream): close open text/reasoning parts before tool blocks
The earlier ordering fix (328aecc) closed parts on text<->reasoning
transitions, but blocks-mode tool parts were enqueued while the
narration part stayed open. Hosts position a part where it STARTED, so
text streamed after a tool call appended to the pre-tool part and
rendered ABOVE the tool block.
Close the open text/reasoning part before emitting tool parts — but
only when parts are actually emitted: edit calls buffer until their
result (no parts at call time), so the narration part stays alive
across that gap instead of splitting needlessly.
Adds three regression tests: text/tool/text ordering, reasoning/tool/
reasoning ordering, and the buffered-edit no-split case.
* docs: catch README/CHANGELOG up to MCP re-forwarding and ordering fix
- MCP section: live per-turn forwarding via chat.params (mcp.status +
config.get), OAuth client mapping to the Cursor auth block, the
unshareable-OAuth skip + one-time toast, and the session-reuse
interaction (changed set -> fresh agent; tools sit atop the
cache-prefix hierarchy).
- Session reuse table: add the MCP-set-changed row.
- Cache implications: MCP changes listed among prefix re-seeders.
- CHANGELOG: entries for MCP re-forwarding and the tool-block part
ordering fix.
* style: normalise indentation to tabs (formatter)1 parent 39dd3e7 commit df220e8
19 files changed
Lines changed: 1885 additions & 367 deletions
File tree
- scripts
- src
- plugin
- provider
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
7 | 53 | | |
8 | 54 | | |
9 | 55 | | |
| |||
47 | 93 | | |
48 | 94 | | |
49 | 95 | | |
50 | | - | |
| 96 | + | |
| 97 | + | |
51 | 98 | | |
52 | 99 | | |
53 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
| 155 | + | |
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
169 | 202 | | |
170 | 203 | | |
171 | 204 | | |
| |||
205 | 238 | | |
206 | 239 | | |
207 | 240 | | |
208 | | - | |
209 | | - | |
210 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
211 | 248 | | |
212 | 249 | | |
213 | 250 | | |
214 | 251 | | |
215 | 252 | | |
| 253 | + | |
216 | 254 | | |
217 | 255 | | |
218 | 256 | | |
219 | 257 | | |
220 | 258 | | |
221 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
222 | 271 | | |
223 | 272 | | |
224 | 273 | | |
| |||
297 | 346 | | |
298 | 347 | | |
299 | 348 | | |
300 | | - | |
301 | | - | |
302 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
303 | 353 | | |
304 | 354 | | |
305 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
0 commit comments