11import { OpenCode } from "@opencode/client" ;
22import { Service } from "@opencode/client/service" ;
3+ import { createHash } from "node:crypto" ;
34
45// Cleanup must reach the lock release even when an evicted SDK scope can no
56// longer dispose its RPC registration. Keep the lock until work has settled.
@@ -13,7 +14,10 @@ export async function cleanup(...steps: (() => void | Promise<void>)[]) {
1314
1415export interface OwnerClient {
1516 health : { get ( options : { signal : AbortSignal } ) : Promise < { pid : number } > } ;
16- plugin : { list ( input : { location : { directory : string } } , options : { signal : AbortSignal } ) : Promise < unknown > } ;
17+ session : {
18+ create ( input : { id : string ; title : string ; location : { directory : string } ; metadata : Record < string , string > } , options : { signal : AbortSignal } ) : Promise < { id : string ; location : { directory : string } ; metadata ?: Record < string , unknown > } > ;
19+ rename ( input : { sessionID : string ; title : string } , options : { signal : AbortSignal } ) : Promise < unknown > ;
20+ } ;
1721}
1822
1923export async function touchOwner ( directory : string , signal : AbortSignal , connect : ( ) => Promise < OwnerClient | undefined > = async ( ) => {
@@ -25,22 +29,57 @@ export async function touchOwner(directory: string, signal: AbortSignal, connect
2529 // A standalone server must never activate a second owner in a different
2630 // background service. The public request must return to this exact process.
2731 if ( ( await client . health . get ( { signal } ) ) . pid !== process . pid ) return false ;
28- await client . plugin . list ( { location : { directory } } , { signal } ) ;
32+ // OpenCode's inactivity sweep observes durable session events, NOT HTTP
33+ // requests or plugin RPC. Reuse one empty maintenance session; never prompt
34+ // a model, touch a user's session, or create a new session on every tick.
35+ const id = `ses_${ createHash ( "sha256" ) . update ( `opencode2-automation-owner:${ directory } ` ) . digest ( "hex" ) . slice ( 0 , 32 ) } ` ;
36+ const title = "Automation owner keepalive" ;
37+ const session = await client . session . create ( { id, title, location : { directory } , metadata : { automation : "owner-keepalive" } } , { signal } ) ;
38+ if ( session . location . directory !== directory || session . metadata ?. automation !== "owner-keepalive" ) throw new Error ( "Automation keepalive session identity mismatch" ) ;
39+ // Rename emits Session.Renamed even when the title is unchanged.
40+ await client . session . rename ( { sessionID : session . id , title } , { signal } ) ;
2941 return true ;
3042}
3143
32- export function heartbeat ( touch : ( signal : AbortSignal ) => Promise < unknown > , report : ( error : unknown ) => void , everyMs = 30_000 ) {
44+ export function heartbeat ( touch : ( signal : AbortSignal ) => Promise < unknown > , report : ( error : unknown ) => void , everyMs = 10 * 60_000 ) {
3345 const controller = new AbortController ( ) ;
3446 let active : Promise < void > | undefined ;
35- const timer = setInterval ( ( ) => {
47+ const tick = ( ) => {
3648 if ( active || controller . signal . aborted ) return ;
3749 active = Promise . resolve ( ) . then ( ( ) => touch ( AbortSignal . any ( [ controller . signal , AbortSignal . timeout ( 15_000 ) ] ) ) )
3850 . then ( ( ) => { } , error => { if ( ! controller . signal . aborted ) report ( error ) ; } )
3951 . finally ( ( ) => { active = undefined ; } ) ;
40- } , everyMs ) ;
52+ } ;
53+ const timer = setInterval ( tick , everyMs ) ;
54+ tick ( ) ;
4155 return async ( ) => {
4256 clearInterval ( timer ) ;
4357 controller . abort ( ) ;
4458 await active ;
4559 } ;
4660}
61+
62+ // Some OpenCode Promise plugin adapters ignore the request's AbortSignal.
63+ // Bound our wait independently. Late SDK results are observed but cannot resume
64+ // the retired dispatcher; durable session identity allows the next owner to
65+ // reconcile work that continues in the server.
66+ export function abortable < T > ( action : ( ) => Promise < T > , signal ?: AbortSignal ) : Promise < T > {
67+ if ( ! signal ) return action ( ) ;
68+ return new Promise < T > ( ( resolve , reject ) => {
69+ if ( signal . aborted ) { reject ( signal . reason ) ; return ; }
70+ const abort = ( ) => reject ( signal . reason ) ;
71+ signal . addEventListener ( "abort" , abort , { once : true } ) ;
72+ Promise . resolve ( ) . then ( ( ) => { signal . throwIfAborted ( ) ; return action ( ) ; } )
73+ . then ( resolve , reject ) . finally ( ( ) => signal . removeEventListener ( "abort" , abort ) ) ;
74+ } ) ;
75+ }
76+
77+ export function cancellable < T extends object > ( api : T ) : T {
78+ return new Proxy ( api , {
79+ get ( target , property , receiver ) {
80+ const method : unknown = Reflect . get ( target , property , receiver ) ;
81+ if ( typeof method !== "function" ) return method ;
82+ return ( input : unknown , request ?: { signal ?: AbortSignal } ) => abortable ( ( ) => Reflect . apply ( method , target , [ input , request ] ) , request ?. signal ) ;
83+ } ,
84+ } ) ;
85+ }
0 commit comments