@@ -8,6 +8,7 @@ import { describe, expect, it } from "@effect/vitest";
88import * as HostProcess from "@t3tools/shared/HostProcess" ;
99import * as Effect from "effect/Effect" ;
1010import * as Layer from "effect/Layer" ;
11+ import * as Logger from "effect/Logger" ;
1112import { ChildProcessSpawner } from "effect/process" ;
1213
1314import * as ServerConfig from "../config.ts" ;
@@ -77,7 +78,10 @@ const withFakeSystemd = <A, E>(
7778 readonly cgroupRoot : string ;
7879 } ) => Effect . Effect < A , E > ,
7980 // A user manager older than systemd 253 rejects OOMPolicy on scopes.
80- { oomPolicy = true } : { readonly oomPolicy ?: boolean } = { } ,
81+ {
82+ oomPolicy = true ,
83+ stopFails = false ,
84+ } : { readonly oomPolicy ?: boolean ; readonly stopFails ?: boolean } = { } ,
8185) =>
8286 Effect . gen ( function * ( ) {
8387 const bin = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-agent-scope-" ) ) ;
@@ -105,6 +109,13 @@ const withFakeSystemd = <A, E>(
105109 return { ...output ( "" ) , code : ChildProcessSpawner . ExitCode ( 1 ) } ;
106110 }
107111 const [ , verb , unit = "" ] = args ;
112+ if ( stopFails && verb === "stop" ) {
113+ return {
114+ ...output ( "" ) ,
115+ stderr : `Failed to stop ${ args . at ( - 1 ) } : Unit not loaded.` ,
116+ code : ChildProcessSpawner . ExitCode ( 5 ) ,
117+ } ;
118+ }
108119 if ( verb === "reset-failed" ) units . delete ( unit ) ;
109120 if ( verb !== "show" ) return output ( "" ) ;
110121 const state = units . get ( unit ) ;
@@ -250,6 +261,24 @@ describe("AgentScope service", () => {
250261 ) ,
251262 ) ;
252263
264+ it . effect ( "reports a scope stop that systemctl rejects" , ( ) => {
265+ const warnings : Array < unknown > = [ ] ;
266+ const logger = Logger . make ( ( { message } ) => {
267+ warnings . push ( Array . isArray ( message ) ? message [ 0 ] : message ) ;
268+ } ) ;
269+ return withFakeSystemd (
270+ ( { scope, units, calls, cgroupRoot } ) =>
271+ Effect . gen ( function * ( ) {
272+ const agent = yield * scope . wrap ( { command : "agent" , args : [ ] , name : "t" , threadId : "a" } ) ;
273+ exitedAgent ( units , cgroupRoot , unitOf ( agent ) , { populated : true , oomKills : 0 } ) ;
274+ yield * scope . stop ( unitOf ( agent ) ) ;
275+ expect ( stopsOf ( calls ) ) . toHaveLength ( 1 ) ;
276+ expect ( warnings ) . toContain ( "Could not stop an exited agent's scope" ) ;
277+ } ) . pipe ( Effect . provide ( Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ) ,
278+ { stopFails : true } ,
279+ ) ;
280+ } ) ;
281+
253282 it . effect ( "leaves a scope alone while its OOM count can't be read" , ( ) =>
254283 withFakeSystemd ( ( { scope, units, calls, cgroupRoot } ) =>
255284 Effect . gen ( function * ( ) {
0 commit comments