@@ -4,6 +4,7 @@ import type { AddressInfo } from "node:net";
44import os from "node:os" ;
55import path from "node:path" ;
66import { createStartAccountContext } from "openclaw/plugin-sdk/channel-test-helpers" ;
7+ import { createDeferred } from "openclaw/plugin-sdk/extension-shared" ;
78import type { OpenKeyedStoreOptions } from "openclaw/plugin-sdk/plugin-state-runtime" ;
89import {
910 createPluginStateSyncKeyedStoreForTests ,
@@ -30,16 +31,6 @@ import {
3031import { ReefInboxConnection , ReefTransportClient } from "./transport.js" ;
3132import { openReefTrustStore } from "./trust-store.js" ;
3233
33- function deferred ( ) {
34- let resolve ! : ( ) => void ;
35- let reject ! : ( reason ?: unknown ) => void ;
36- const promise = new Promise < void > ( ( resolvePromise , rejectPromise ) => {
37- resolve = resolvePromise ;
38- reject = rejectPromise ;
39- } ) ;
40- return { promise, resolve, reject } ;
41- }
42-
4334describe ( "Reef inbound dispatch content" , ( ) => {
4435 it ( "keeps provenance model-visible without storing it in the transcript body" , ( ) => {
4536 const content = resolveReefInboundDispatchContent ( {
@@ -233,7 +224,7 @@ describe("Reef gateway account ownership", () => {
233224 } ,
234225 } ;
235226 const controllers : AbortController [ ] = [ ] ;
236- const inboxDrains : ReturnType < typeof deferred > [ ] = [ ] ;
227+ const inboxDrains : ReturnType < typeof createDeferred < void > > [ ] = [ ] ;
237228 const accountTasks : Promise < unknown > [ ] = [ ] ;
238229 let stateDir = "" ;
239230
@@ -260,7 +251,7 @@ describe("Reef gateway account ownership", () => {
260251 setReefRuntime ( runtime ) ;
261252 vi . spyOn ( ReefTransportClient . prototype , "listFriends" ) . mockResolvedValue ( { friendships : [ ] } ) ;
262253 vi . spyOn ( ReefInboxConnection . prototype , "start" ) . mockImplementation ( ( ) => {
263- const drain = deferred ( ) ;
254+ const drain = createDeferred < void > ( ) ;
264255 inboxDrains . push ( drain ) ;
265256 return drain . promise ;
266257 } ) ;
@@ -355,6 +346,53 @@ describe("Reef gateway account ownership", () => {
355346 expect ( listFriends ) . not . toHaveBeenCalled ( ) ;
356347 } ) ;
357348
349+ it ( "revokes borrowed pairing approval before a paused reconcile reaches the replaced flow" , async ( ) => {
350+ startAccount ( ) ;
351+ await vi . waitFor ( ( ) => expect ( inboxDrains ) . toHaveLength ( 1 ) ) ;
352+ const firstActive = getActiveReef ( ) ;
353+ const reconcilePaused = createDeferred < void > ( ) ;
354+ const firstList = vi . fn ( async ( ) => {
355+ await reconcilePaused . promise ;
356+ return { friendships : [ ] } ;
357+ } ) ;
358+ firstActive . friends . transport . listFriends = firstList ;
359+ const firstSend = vi . spyOn ( firstActive . flow , "send" ) . mockResolvedValue ( "account-a-message" ) ;
360+ const stale = reefPlugin . pairing ! . notifyApproval ! ( { cfg, id : "molty" } ) ;
361+ await vi . waitFor ( ( ) => expect ( firstList ) . toHaveBeenCalledOnce ( ) ) ;
362+
363+ startAccount ( ) ;
364+ await vi . waitFor ( ( ) => expect ( inboxDrains ) . toHaveLength ( 2 ) ) ;
365+ const replacementActive = getActiveReef ( ) ;
366+
367+ reconcilePaused . resolve ( ) ;
368+ const staleResult = await stale . catch ( ( error : unknown ) => error ) ;
369+
370+ expect ( firstSend ) . not . toHaveBeenCalled ( ) ;
371+ expect ( staleResult ) . toBeInstanceOf ( Error ) ;
372+ expect ( getActiveReef ( ) ) . toBe ( replacementActive ) ;
373+ } ) ;
374+
375+ it ( "rejects a borrowed Reef command when shutdown interrupts its friend lookup" , async ( ) => {
376+ const account = startAccount ( ) ;
377+ await vi . waitFor ( ( ) => expect ( inboxDrains ) . toHaveLength ( 1 ) ) ;
378+ const active = getActiveReef ( ) ;
379+ const listPaused = createDeferred < void > ( ) ;
380+ const listFriends = vi . fn ( async ( ) => {
381+ await listPaused . promise ;
382+ return { friendships : [ ] } ;
383+ } ) ;
384+ active . friends . transport . listFriends = listFriends ;
385+ const command = handleReefCommand ( { args : "friend list" } ) ;
386+ await vi . waitFor ( ( ) => expect ( listFriends ) . toHaveBeenCalledOnce ( ) ) ;
387+
388+ account . abort . abort ( ) ;
389+ listPaused . resolve ( ) ;
390+
391+ await expect ( command ) . rejects . toBeInstanceOf ( Error ) ;
392+ inboxDrains [ 0 ] ! . resolve ( ) ;
393+ await account . account ;
394+ } ) ;
395+
358396 it ( "keeps the replacement account authoritative through stale and failed account teardown" , async ( ) => {
359397 const first = startAccount ( ) ;
360398 await vi . waitFor ( ( ) => {
@@ -533,8 +571,8 @@ describe("Reef channel lifecycle", () => {
533571 it ( "does not activate when the parent aborts during startup reconcile" , async ( ) => {
534572 const parent = new AbortController ( ) ;
535573 const inbox = hangingInbox ( ) ;
536- const reconcileStarted = deferred ( ) ;
537- const finishReconcile = deferred ( ) ;
574+ const reconcileStarted = createDeferred < void > ( ) ;
575+ const finishReconcile = createDeferred < void > ( ) ;
538576 const onReady = vi . fn ( async ( ) => { } ) ;
539577 const lifecycle = runReefChannelLifecycle ( {
540578 parentSignal : parent . signal ,
@@ -557,8 +595,8 @@ describe("Reef channel lifecycle", () => {
557595 it ( "does not reject when startup reconcile fails after the parent aborts" , async ( ) => {
558596 const parent = new AbortController ( ) ;
559597 const inbox = hangingInbox ( ) ;
560- const reconcileStarted = deferred ( ) ;
561- const finishReconcile = deferred ( ) ;
598+ const reconcileStarted = createDeferred < void > ( ) ;
599+ const finishReconcile = createDeferred < void > ( ) ;
562600 const onReady = vi . fn ( async ( ) => { } ) ;
563601 const lifecycle = runReefChannelLifecycle ( {
564602 parentSignal : parent . signal ,
@@ -582,7 +620,7 @@ describe("Reef channel lifecycle", () => {
582620 { phase : "friend reconciliation" , completedRequests : 0 } ,
583621 { phase : "pairing-candidate surfacing" , completedRequests : 1 } ,
584622 ] ) ( "promptly aborts a stalled $phase request" , async ( { completedRequests } ) => {
585- const requestStarted = deferred ( ) ;
623+ const requestStarted = createDeferred < void > ( ) ;
586624 let requests = 0 ;
587625 const server = http . createServer ( ( _request , response ) => {
588626 if ( requests ++ < completedRequests ) {
@@ -644,8 +682,8 @@ describe("Reef channel lifecycle", () => {
644682 it ( "does not start the inbox when the parent aborts during activation" , async ( ) => {
645683 const parent = new AbortController ( ) ;
646684 const inbox = hangingInbox ( ) ;
647- const activationStarted = deferred ( ) ;
648- const finishActivation = deferred ( ) ;
685+ const activationStarted = createDeferred < void > ( ) ;
686+ const finishActivation = createDeferred < void > ( ) ;
649687 const lifecycle = runReefChannelLifecycle ( {
650688 parentSignal : parent . signal ,
651689 startInbox : inbox . startInbox ,
0 commit comments