@@ -18,6 +18,7 @@ import { Channel, invoke } from "@tauri-apps/api/core";
1818import { TRANSITION_SOURCE , type TransitionSource , harnessActivity } from "./harnessActivity.ts" ;
1919import { observedAgents } from "./harnessAgent.ts" ;
2020import { type PendingPhase , pendingPrompts } from "./pendingPrompts.ts" ;
21+ import { followedOpencodeSession } from "./sessionTracking.ts" ;
2122import { subagents } from "./subagents.ts" ;
2223
2324/// Mirror of the Rust enum. `kind` is the serde tag from
@@ -235,7 +236,14 @@ const translate = (harnessId: string, event: ClaudeEvent): void => {
235236/// the frontend catches up.
236237export type OpencodeEvent =
237238 | { kind : "connected" }
238- | { kind : "session_created" ; session_id : string }
239+ // #116: `parent_id` is non-null for a subagent child session — never
240+ // a conversation to capture or follow. `null` marks a root session.
241+ | { kind : "session_created" ; session_id : string ; parent_id : string | null }
242+ // #116: a user message landed in a ROOT session (backend-verified
243+ // not a child) — the only signal the `/sessions` picker gives when
244+ // it switches onto an EXISTING session, since it publishes nothing
245+ // of its own. See `followedOpencodeSession` in `sessionTracking.ts`.
246+ | { kind : "root_session_prompted" ; session_id : string }
239247 | { kind : "session_busy" }
240248 | { kind : "session_idle" }
241249 | { kind : "message_delta" }
@@ -265,6 +273,23 @@ export type OpencodeEvent =
265273/// poll stays as a fallback — see `captureOpencodeSessionId` for
266274/// the relationship.
267275///
276+ /// `getSessionId` reads the harness's CURRENT session id, live, at
277+ /// the moment each event is translated — not the value captured when
278+ /// this adapter attached. The adapter is attached once per PTY, but
279+ /// the harness's own sessionId can change under it (#116: `/new` or a
280+ /// `/sessions` pick), so a value closed over at attach time would go
281+ /// stale the first time that happens.
282+ ///
283+ /// `onSessionFollowed` fires when `followedOpencodeSession` decides
284+ /// the harness moved onto a different root session (`/new` or an
285+ /// existing session picked via `/sessions`) — see that function for
286+ /// the two signals this is built from. The caller wires it to
287+ /// `replaceHarnessSessionId`, same as Claude's `/clear`/`/resume`/fork
288+ /// follow. Deliberately NOT routed through `harnessActivity` here: a
289+ /// "switch" is detected by a user prompt arriving, and opencode's own
290+ /// busy/idle SSE (port-scoped, already authoritative) is what should
291+ /// keep driving the phase.
292+ ///
268293/// Soft-fail: any Rust-side rejection (port closed, IPC dropped)
269294/// detaches authoritative + warns. The harness keeps running on
270295/// L2a.
@@ -275,14 +300,16 @@ export function attachOpencodeEvents(
275300 port : number ,
276301 sessionId : string | undefined ,
277302 onSessionCaptured : ( ( sessionId : string ) => void ) | undefined ,
303+ getSessionId : ( ) => string | undefined ,
304+ onSessionFollowed : ( ( sessionId : string ) => void ) | undefined ,
278305) : ( ) => void {
279306 const channel = new Channel < OpencodeEvent > ( ) ;
280307 let closed = false ;
281308 channel . onmessage = ( event ) => {
282309 // #259: see attachClaudeEvents. `connected` arrives first, so a
283310 // stream that is up disarms the watchdog before any prompt.
284311 if ( ! closed ) harnessActivity . adapterDelivered ( harnessId ) ;
285- translateOpencode ( harnessId , event , onSessionCaptured ) ;
312+ translateOpencode ( harnessId , event , onSessionCaptured , getSessionId , onSessionFollowed ) ;
286313 } ;
287314
288315 // See attachClaudeEvents for why this happens synchronously.
@@ -343,6 +370,8 @@ const translateOpencode = (
343370 harnessId : string ,
344371 event : OpencodeEvent ,
345372 onSessionCaptured : ( ( sessionId : string ) => void ) | undefined ,
373+ getSessionId : ( ) => string | undefined ,
374+ onSessionFollowed : ( ( sessionId : string ) => void ) | undefined ,
346375) : void => {
347376 switch ( event . kind ) {
348377 case "connected" :
@@ -366,13 +395,58 @@ const translateOpencode = (
366395 pendingPrompts . forget ( harnessId ) ;
367396 harnessActivity . attachAuthoritativeSource ( harnessId ) ;
368397 return ;
369- case "session_created" :
370- // SSE-driven session-id capture (replaces chapter 5
371- // phase 2b's sqlite poll on the happy path). The
372- // callback short-circuits the sqlite fallback once
373- // invoked.
374- onSessionCaptured ?.( event . session_id ) ;
398+ case "session_created" : {
399+ // #116: a child session (subagent, non-null parent_id) is
400+ // never a conversation to capture OR follow — Skein has no
401+ // business tracking it as the harness's own session id.
402+ if ( event . parent_id !== null ) return ;
403+ const current = getSessionId ( ) ;
404+ if ( current === undefined ) {
405+ // SSE-driven session-id capture (replaces chapter 5
406+ // phase 2b's sqlite poll on the happy path). The
407+ // callback short-circuits the sqlite fallback once
408+ // invoked.
409+ onSessionCaptured ?.( event . session_id ) ;
410+ return ;
411+ }
412+ const followed = followedOpencodeSession ( current , {
413+ kind : "session_created" ,
414+ sessionId : event . session_id ,
415+ parentId : event . parent_id ,
416+ } ) ;
417+ if ( followed ) {
418+ console . info (
419+ `[skein] opencode ${ harnessId } followed onto ${ followed . sessionId } (${ followed . source } )` ,
420+ ) ;
421+ onSessionFollowed ?.( followed . sessionId ) ;
422+ }
375423 return ;
424+ }
425+ case "root_session_prompted" : {
426+ // #116: the `/sessions` picker publishes nothing when it
427+ // switches onto an EXISTING root session — the first user
428+ // prompt landing there is the earliest honest signal. See
429+ // `followedOpencodeSession`.
430+ const current = getSessionId ( ) ;
431+ if ( current === undefined ) {
432+ // No session captured yet: treat this as the initial
433+ // capture, not a follow — same reasoning as
434+ // `session_created`'s `current === undefined` arm.
435+ onSessionCaptured ?.( event . session_id ) ;
436+ return ;
437+ }
438+ const followed = followedOpencodeSession ( current , {
439+ kind : "root_session_prompted" ,
440+ sessionId : event . session_id ,
441+ } ) ;
442+ if ( followed ) {
443+ console . info (
444+ `[skein] opencode ${ harnessId } followed onto ${ followed . sessionId } (${ followed . source } )` ,
445+ ) ;
446+ onSessionFollowed ?.( followed . sessionId ) ;
447+ }
448+ return ;
449+ }
376450 case "session_busy" :
377451 // #86: "still working" — must not clear a pending
378452 // permission, so no `clearsPermission` (defaults to false).
0 commit comments