Repository navigation
Commit a2fef89
fix(opencode): fork the session on a cwd change instead of dropping context
The directory-mismatch guard added while hardening this PR started a fresh, EMPTY
session whenever the resumed session's stored directory differed from the requested
cwd. Its stated rationale -- "OpenCode routes a prompt to the session's own stored
directory, so resuming under a different cwd would run in the wrong tree" -- does not
hold: OpenCode resolves tool execution, snapshots and file ops from the per-request
directory param (instance context), not from session.info.directory. So the guard
solved a non-problem and introduced a real one: any time a thread's cwd changes (most
commonly when it moves from the project root into a git worktree between turns) the
whole conversation was stranded in the old session and the follow-up landed in an
empty one -- the exact pingdotgg#3604 symptom this PR set out to fix.
Fix: when the persisted session exists but was created under a different directory,
fork it INTO the requested directory (client.session.fork({ sessionID, directory }))
instead of creating an empty session. OpenCode clones the full message history into a
new session bound to the requested worktree, so the follow-up keeps its context and
tools still run on the correct tree. The fork id becomes the durable resume cursor. A
genuinely missing (404) session still starts fresh, unchanged.
Verified against the OpenCode source that fork copies all messages/parts with fresh
ids and stamps the new session's directory/path from the request context.
Test: the former "starts fresh on directory mismatch" case now asserts the session is
forked with history (fork called, no session.create, cursor -> fork id) via a new fork
mock; the not-found path still starts fresh.
Co-authored-by: codex <[email protected]>1 parent fa26ab3 commit a2fef89
2 files changed
Lines changed: 81 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| |||
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| 93 | + | |
92 | 94 | | |
93 | 95 | | |
94 | 96 | | |
| |||
164 | 166 | | |
165 | 167 | | |
166 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
167 | 179 | | |
168 | 180 | | |
169 | 181 | | |
| |||
469 | 481 | | |
470 | 482 | | |
471 | 483 | | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
478 | 493 | | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
485 | 500 | | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
495 | 517 | | |
496 | | - | |
497 | | - | |
| 518 | + | |
| 519 | + | |
498 | 520 | | |
499 | 521 | | |
500 | 522 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1230 | 1230 | | |
1231 | 1231 | | |
1232 | 1232 | | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
1238 | 1237 | | |
1239 | 1238 | | |
1240 | 1239 | | |
| |||
1255 | 1254 | | |
1256 | 1255 | | |
1257 | 1256 | | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
1258 | 1288 | | |
1259 | 1289 | | |
1260 | | - | |
1261 | | - | |
1262 | | - | |
| 1290 | + | |
1263 | 1291 | | |
1264 | 1292 | | |
1265 | 1293 | | |
| |||
0 commit comments