@@ -80,14 +80,20 @@ describe("managed service update handoff", () => {
8080 expect ( result . status ) . toBe ( "started" ) ;
8181 expect ( result . command ) . toBe ( "openclaw update --yes --timeout 1800" ) ;
8282 expect ( spawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
83- const [ execPath , args , options ] = spawnMock . mock . calls [ 0 ] as [
83+ const [ execPath , args , options ] = spawnMock . mock . calls [ 0 ] as unknown as [
8484 string ,
8585 string [ ] ,
8686 { env : NodeJS . ProcessEnv ; detached ?: boolean ; cwd ?: string } ,
8787 ] ;
8888 expect ( execPath ) . toBe ( "/usr/local/bin/node" ) ;
8989 expect ( args ) . toHaveLength ( 2 ) ;
9090 tempDirs . add ( path . dirname ( args [ 0 ] ?? result . logPath ) ) ;
91+ const helperParams = JSON . parse ( await fs . readFile ( args [ 1 ] ?? "" , "utf-8" ) ) as {
92+ metaPath ?: string ;
93+ sentinelPath ?: string ;
94+ } ;
95+ expect ( helperParams . metaPath ) . toMatch ( / s e n t i n e l - m e t a \. j s o n $ / u) ;
96+ expect ( helperParams . sentinelPath ) . toMatch ( / r e s t a r t - s e n t i n e l \. j s o n $ / u) ;
9197 expect ( options . cwd ) . toBe ( "/tmp/openclaw" ) ;
9298 expect ( options . detached ) . toBe ( true ) ;
9399 expect ( options . env . KEEP_ME ) . toBe ( "1" ) ;
@@ -103,6 +109,172 @@ describe("managed service update handoff", () => {
103109 expect ( options . env [ CONTROL_PLANE_UPDATE_SENTINEL_META_ENV ] ) . toMatch ( / s e n t i n e l - m e t a \. j s o n $ / u) ;
104110 } ) ;
105111
112+ it ( "does not overwrite a restart sentinel owned by another startup task" , async ( ) => {
113+ const { execFile } =
114+ await vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ;
115+ const { startManagedServiceUpdateHandoff } =
116+ await import ( "./update-managed-service-handoff.js" ) ;
117+ const tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-handoff-helper-test-" ) ) ;
118+ tempDirs . add ( tmpDir ) ;
119+
120+ await startManagedServiceUpdateHandoff ( {
121+ root : tmpDir ,
122+ timeoutMs : 1_800_000 ,
123+ restartDelayMs : 500 ,
124+ parentPid : process . pid ,
125+ execPath : "/usr/local/bin/node" ,
126+ argv1 : "/opt/openclaw/openclaw.mjs" ,
127+ env : { } ,
128+ meta : {
129+ sessionKey : "agent:test:webchat:dm:user-123" ,
130+ continuationMessage : "continue after restart" ,
131+ } ,
132+ } ) ;
133+
134+ const [ , args ] = spawnMock . mock . calls . at ( - 1 ) as unknown as [
135+ string ,
136+ string [ ] ,
137+ { env : NodeJS . ProcessEnv ; detached ?: boolean ; cwd ?: string } ,
138+ ] ;
139+ const helperScriptPath = args [ 0 ] ?? "" ;
140+ tempDirs . add ( path . dirname ( helperScriptPath ) ) ;
141+ const helperParams = JSON . parse ( await fs . readFile ( args [ 1 ] ?? "" , "utf-8" ) ) as Record <
142+ string ,
143+ unknown
144+ > ;
145+ const sentinelPath = path . join ( tmpDir , "restart-sentinel.json" ) ;
146+ const unrelatedSentinel = {
147+ version : 1 ,
148+ payload : {
149+ kind : "config" ,
150+ status : "skipped" ,
151+ message : "preserve this restart task" ,
152+ stats : { reason : "config-restart-pending" } ,
153+ } ,
154+ } ;
155+ await fs . writeFile ( sentinelPath , `${ JSON . stringify ( unrelatedSentinel , null , 2 ) } \n` ) ;
156+ const helperParamsPath = path . join ( tmpDir , "helper-params.json" ) ;
157+ await fs . writeFile (
158+ helperParamsPath ,
159+ `${ JSON . stringify (
160+ {
161+ ...helperParams ,
162+ parentPid : process . pid ,
163+ parentExitTimeoutMs : 1 ,
164+ sentinelPath,
165+ logPath : path . join ( tmpDir , "handoff.log" ) ,
166+ sensitivePaths : [ ] ,
167+ } ,
168+ null ,
169+ 2 ,
170+ ) } \n`,
171+ ) ;
172+
173+ const result = await new Promise < { code : number | null ; signal : NodeJS . Signals | null } > (
174+ ( resolve ) => {
175+ execFile ( process . execPath , [ helperScriptPath , helperParamsPath ] , ( err ) => {
176+ const childError = err as ( NodeJS . ErrnoException & { signal ?: NodeJS . Signals } ) | null ;
177+ resolve ( {
178+ code : typeof childError ?. code === "number" ? childError . code : 0 ,
179+ signal : childError ?. signal ?? null ,
180+ } ) ;
181+ } ) ;
182+ } ,
183+ ) ;
184+
185+ expect ( result ) . toEqual ( { code : 1 , signal : null } ) ;
186+ await expect ( fs . readFile ( sentinelPath , "utf-8" ) . then ( JSON . parse ) ) . resolves . toEqual (
187+ unrelatedSentinel ,
188+ ) ;
189+ } ) ;
190+
191+ it ( "does not overwrite a newer pending update handoff sentinel" , async ( ) => {
192+ const { execFile } =
193+ await vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ;
194+ const { startManagedServiceUpdateHandoff } =
195+ await import ( "./update-managed-service-handoff.js" ) ;
196+ const tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-handoff-helper-test-" ) ) ;
197+ tempDirs . add ( tmpDir ) ;
198+
199+ await startManagedServiceUpdateHandoff ( {
200+ root : tmpDir ,
201+ timeoutMs : 1_800_000 ,
202+ restartDelayMs : 500 ,
203+ parentPid : process . pid ,
204+ execPath : "/usr/local/bin/node" ,
205+ argv1 : "/opt/openclaw/openclaw.mjs" ,
206+ handoffId : "old-handoff" ,
207+ env : { } ,
208+ meta : {
209+ handoffId : "old-handoff" ,
210+ sessionKey : "agent:test:webchat:dm:user-123" ,
211+ continuationMessage : "continue after restart" ,
212+ } ,
213+ } ) ;
214+
215+ const [ , args ] = spawnMock . mock . calls . at ( - 1 ) as unknown as [
216+ string ,
217+ string [ ] ,
218+ { env : NodeJS . ProcessEnv ; detached ?: boolean ; cwd ?: string } ,
219+ ] ;
220+ const helperScriptPath = args [ 0 ] ?? "" ;
221+ tempDirs . add ( path . dirname ( helperScriptPath ) ) ;
222+ const helperParams = JSON . parse ( await fs . readFile ( args [ 1 ] ?? "" , "utf-8" ) ) as Record <
223+ string ,
224+ unknown
225+ > ;
226+ const sentinelPath = path . join ( tmpDir , "restart-sentinel.json" ) ;
227+ const newerSentinel = {
228+ version : 1 ,
229+ payload : {
230+ kind : "update" ,
231+ status : "skipped" ,
232+ message : "new handoff still pending" ,
233+ stats : {
234+ mode : "npm" ,
235+ handoffId : "newer-handoff" ,
236+ reason : "managed-service-handoff-started" ,
237+ steps : [ ] ,
238+ durationMs : 0 ,
239+ } ,
240+ } ,
241+ } ;
242+ await fs . writeFile ( sentinelPath , `${ JSON . stringify ( newerSentinel , null , 2 ) } \n` ) ;
243+ const helperParamsPath = path . join ( tmpDir , "helper-params.json" ) ;
244+ await fs . writeFile (
245+ helperParamsPath ,
246+ `${ JSON . stringify (
247+ {
248+ ...helperParams ,
249+ parentPid : process . pid ,
250+ parentExitTimeoutMs : 1 ,
251+ sentinelPath,
252+ logPath : path . join ( tmpDir , "handoff.log" ) ,
253+ sensitivePaths : [ ] ,
254+ } ,
255+ null ,
256+ 2 ,
257+ ) } \n`,
258+ ) ;
259+
260+ const result = await new Promise < { code : number | null ; signal : NodeJS . Signals | null } > (
261+ ( resolve ) => {
262+ execFile ( process . execPath , [ helperScriptPath , helperParamsPath ] , ( err ) => {
263+ const childError = err as ( NodeJS . ErrnoException & { signal ?: NodeJS . Signals } ) | null ;
264+ resolve ( {
265+ code : typeof childError ?. code === "number" ? childError . code : 0 ,
266+ signal : childError ?. signal ?? null ,
267+ } ) ;
268+ } ) ;
269+ } ,
270+ ) ;
271+
272+ expect ( result ) . toEqual ( { code : 1 , signal : null } ) ;
273+ await expect ( fs . readFile ( sentinelPath , "utf-8" ) . then ( JSON . parse ) ) . resolves . toEqual (
274+ newerSentinel ,
275+ ) ;
276+ } ) ;
277+
106278 it ( "sweeps stale handoff temp directories while keeping fresh handoff logs" , async ( ) => {
107279 const tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-handoff-cleanup-test-" ) ) ;
108280 tempDirs . add ( tmpDir ) ;
0 commit comments