@@ -5,6 +5,7 @@ import { Bus } from "../bus.js"
55import { Database } from "../database/database.js"
66import { Job } from "../job.js"
77import { Instance } from "../instance/service.js"
8+ import { KV } from "../kv.js"
89import { makeGlobalNode } from "@opencode/util/effect/app-node"
910import { SessionEvent } from "./event.js"
1011import { SessionInbox } from "./inbox.js"
@@ -38,8 +39,22 @@ export interface Interface {
3839 ) => Effect . Effect < boolean >
3940 /** Resolves once this process owns no active execution for the Session. Returns immediately when idle and never starts work. */
4041 readonly awaitIdle : ( sessionID : SessionSchema . ID ) => Effect . Effect < void >
42+ /**
43+ * Stops the Session until it is given work again: marks it stopped (durably), cancels the background work it owns
44+ * (its shells, its own subagents' jobs; not its own subagent job), interrupts its active work and waits for that
45+ * to settle. A running subagent child settles as cancelled and reports that to its parent once. While stopped, a
46+ * notice admitted to it does not wake it, and a later period of it (its owner prompting it) reports nothing to its
47+ * parent. A `resume` (a parent's subagent tool giving it a new task) ends the stop. Returns whether active work was
48+ * interrupted. Queued input is the Session's to drop (`Session.stop`).
49+ */
50+ readonly stop : ( sessionID : SessionSchema . ID ) => Effect . Effect < boolean >
51+ /** Whether the Session was stopped and not resumed since. */
52+ readonly stopped : ( sessionID : SessionSchema . ID ) => Effect . Effect < boolean >
4153}
4254
55+ /** The KV key prefix of a stopped Session's mark. */
56+ const STOPPED = "session.stopped:"
57+
4358/** Routes execution from a Session ID to its selected instance's runner. */
4459export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/SessionExecution" ) { }
4560
@@ -70,6 +85,9 @@ export const layer = Layer.effect(
7085 const jobs = yield * Job . Service
7186 const inbox = yield * SessionInbox . Service
7287 const db = ( yield * Database . Service ) . db
88+ const kv = yield * KV . Service
89+ const stopped = ( sessionID : SessionSchema . ID ) =>
90+ kv . get ( STOPPED + sessionID ) . pipe ( Effect . map ( ( mark ) => mark !== undefined ) )
7391 const reportLifecycle = < A > ( sessionID : SessionSchema . ID , effect : Effect . Effect < A > ) =>
7492 effect . pipe (
7593 Effect . tapCause ( ( cause ) =>
@@ -103,6 +121,7 @@ export const layer = Layer.effect(
103121 draining : Effect . suspend < ReadonlySet < SessionSchema . ID > , never , never > ( ( ) =>
104122 holder . coordinator ? holder . coordinator . draining : Effect . succeed ( new Set < SessionSchema . ID > ( ) ) ,
105123 ) ,
124+ stopped,
106125 } )
107126 // Why a Session's execution is being interrupted, read from the coordinator once it exists.
108127 let interruption : ( sessionID : SessionSchema . ID ) => InterruptReason | undefined = ( ) => undefined
@@ -208,9 +227,20 @@ export const layer = Layer.effect(
208227 yield * coordinator . wake ( sessionID , "steer" )
209228 return interrupted
210229 } ) ,
211- resume : coordinator . run ,
230+ // New work ends a stop: the mark goes before the run starts, so its period reports as any other.
231+ resume : ( sessionID ) => kv . remove ( STOPPED + sessionID ) . pipe ( Effect . andThen ( coordinator . run ( sessionID ) ) ) ,
212232 wake : coordinator . wake ,
213233 awaitIdle : coordinator . awaitIdle ,
234+ stop : ( sessionID ) =>
235+ Effect . gen ( function * ( ) {
236+ // Marked first: a notice its cancelled work admits, or a report of its own subagents, must not wake it.
237+ yield * kv . set ( STOPPED + sessionID , true )
238+ yield * jobs . cancelOwned ( sessionID )
239+ const interrupted = yield * coordinator . interrupt ( sessionID , "user" )
240+ yield * coordinator . awaitIdle ( sessionID )
241+ return interrupted
242+ } ) ,
243+ stopped,
214244 } )
215245 } ) ,
216246)
@@ -232,7 +262,7 @@ function publishTerminal(
232262export const node = makeGlobalNode ( {
233263 service : Service ,
234264 layer,
235- deps : [ SessionStore . node , Instance . node , Bus . node , Database . node , Job . node , SessionInbox . node ] ,
265+ deps : [ SessionStore . node , Instance . node , Bus . node , Database . node , Job . node , SessionInbox . node , KV . node ] ,
236266} )
237267
238268/** Low-level compatibility layer for callers that only need durable Session recording. */
@@ -245,5 +275,7 @@ export const noopLayer = Layer.succeed(
245275 wake : ( ) => Effect . void ,
246276 interrupt : ( ) => Effect . succeed ( false ) ,
247277 awaitIdle : ( ) => Effect . void ,
278+ stop : ( ) => Effect . succeed ( false ) ,
279+ stopped : ( ) => Effect . succeed ( false ) ,
248280 } ) ,
249281)
0 commit comments