Preserve OpenCode session across command completion, control SessionStart emission (#14866)

* Preserve OpenCode session across command completion

- Add session start events and launch token tracking to establish session boundaries
- Defer retiring launch authority until OpenCode process actually exits, not just when a command finishes
- Fence previous tokens after restarts to prevent status updates from stale sessions
- Maps SessionStart as a session boundary for proper turn/state management

* Emit SessionStart only from OpenCode, not mimo-code

Restrict SessionStart lifecycle events to OpenCode exclusively. Mimo-code no longer emits SessionStart, as it should rely on OpenCode for session boundary signals. This prevents duplicate lifecycle events that could interfere with pane authority tracking and session state management. Also tighten foreground process result validation to reject stale results after title observation changes, fixing a race where a delayed foreground read from a previous cycle would incorrectly retire authority.
This commit is contained in:
Jinjing
2026-08-16 10:01:27 -07:00
committed by GitHub
parent e4e54a17d0
commit 1da1bdc01c
10 changed files with 411 additions and 20 deletions
+32 -2
View File
@@ -11119,7 +11119,7 @@ export class OrcaRuntimeService {
this.recordTerminalSideEffectFact(ptyId, { kind: 'bell' })
return
case 'command-finished':
this.retirePtyAgentLaunchAuthority(ptyId)
this.retirePtyAgentLaunchAuthorityAfterCommandFinished(ptyId)
this.recordTerminalSideEffectFact(ptyId, {
kind: 'command-finished',
exitCode: fact.exitCode
@@ -11408,7 +11408,7 @@ export class OrcaRuntimeService {
this.confirmPtyAgentExit(ptyId)
},
onCommandFinished: (exitCode: number | null) => {
this.retirePtyAgentLaunchAuthority(ptyId)
this.retirePtyAgentLaunchAuthorityAfterCommandFinished(ptyId)
this.recordTerminalSideEffectFact(ptyId, { kind: 'command-finished', exitCode })
},
onBell: () => {
@@ -13536,6 +13536,36 @@ export class OrcaRuntimeService {
}
}
private retirePtyAgentLaunchAuthorityAfterCommandFinished(ptyId: string): void {
const pty = this.ptysById.get(ptyId)
if (pty?.launchAgent !== 'opencode') {
this.retirePtyAgentLaunchAuthority(ptyId)
return
}
const titleObservedAt = pty.lastOscTitleAt ?? null
const foregroundRead = this.readPtyForegroundProcessFromController(ptyId, titleObservedAt ?? 0)
if (!foregroundRead) {
this.retirePtyAgentLaunchAuthority(ptyId)
return
}
const incarnationId = pty.incarnationId
void foregroundRead.then((result) => {
const current = this.ptysById.get(ptyId)
if (
current !== pty ||
current.incarnationId !== incarnationId ||
current.lastOscTitleAt !== titleObservedAt ||
result.controller !== this.ptyController
) {
return
}
if (result.available && recognizeAgentProcess(result.process)?.agent === 'opencode') {
return
}
this.retirePtyAgentLaunchAuthority(ptyId)
})
}
async resolveTerminalCwd(handle: string): Promise<string | null> {
const ptyId = this.resolveLeafForHandle(handle)?.ptyId
if (!ptyId) {