@@ -13,7 +13,7 @@ const retryDelayMs = 1000
1313// settings store, so it applies to every session, tab, and server at once.
1414export function createPermissionAutoApprover ( input : { sdk : ServerSDK ; data : Data } ) {
1515 const enabled = useSettings ( ) . permissions . autoApprove
16- const state = { disposed : false , responded : new Set < string > ( ) }
16+ const state = { disposed : false , generation : 0 , responded : new Set < string > ( ) }
1717
1818 const unsubscribe = input . sdk . event . on ( "permission.asked" , ( event ) => {
1919 if ( enabled ( ) ) approve ( event . data )
@@ -28,48 +28,86 @@ export function createPermissionAutoApprover(input: { sdk: ServerSDK; data: Data
2828 // on, so sweep on every connect while the setting is on.
2929 createEffect ( ( ) => {
3030 if ( ! enabled ( ) || input . sdk . connection . status ( ) !== "connected" ) return
31- void sweep ( )
31+ const generation = ++ state . generation
32+ void sweepWithRetry ( generation , 0 )
3233 } )
3334
35+ // An incomplete sweep leaves pending requests hidden with no later trigger
36+ // to recover them, so retry it a bounded number of times. A newer sweep
37+ // supersedes scheduled retries.
38+ async function sweepWithRetry ( generation : number , attempt : number ) {
39+ const complete = await sweep ( )
40+ if ( complete || attempt >= retryLimit ) return
41+ setTimeout ( ( ) => {
42+ if ( state . disposed || ! enabled ( ) || generation !== state . generation ) return
43+ void sweepWithRetry ( generation , attempt + 1 )
44+ } , retryDelayMs * ( attempt + 1 ) )
45+ }
46+
3447 async function sweep ( ) {
35- for ( const location of await activeSessionLocations ( ) ) {
36- input . sdk . api . permission . request
37- . list ( { location : { directory : location . directory , workspace : location . workspaceID } } )
38- . then ( ( pending ) => {
39- if ( state . disposed || ! enabled ( ) ) return
40- pending . data . forEach ( ( request ) => approve ( request ) )
41- } )
42- . catch ( ( ) => undefined )
43- }
48+ const inventory = await sweepLocations ( )
49+ const listed = await Promise . all (
50+ inventory . locations . map ( ( location ) =>
51+ input . sdk . api . permission . request
52+ . list ( { location : { directory : location . directory , workspace : location . workspaceID } } )
53+ . then ( ( pending ) => {
54+ if ( ! state . disposed ) pending . data . forEach ( ( request ) => approve ( request ) )
55+ return true
56+ } )
57+ . catch ( ( ) => false ) ,
58+ ) ,
59+ )
60+ return inventory . complete && listed . every ( Boolean )
4461 }
4562
46- // Pending requests only exist inside active executions (Permission.assert
47- // clears its entry when the awaiting fiber dies), and session.active is
48- // server-wide, so this inventory covers sessions no tab has loaded.
49- async function activeSessionLocations ( ) {
50- const active = await input . sdk . api . session . active ( ) . catch ( ( ) => ( { } ) )
51- const ids = Object . keys ( active )
52- await Promise . all (
53- ids . filter ( ( id ) => ! input . data . session . get ( id ) ) . map ( ( id ) => input . data . session . sync ( id ) . catch ( ( ) => undefined ) ) ,
63+ // Active sessions are the primary inventory: session.active is server-wide,
64+ // so it covers sessions no tab has loaded, and a request blocking a tool
65+ // call always belongs to one (Permission.assert clears its entry when the
66+ // awaiting fiber dies). Locally known sessions are swept too because the
67+ // external session.permission.create API can park a request on an idle
68+ // session. A detached request on a session this client never loaded is the
69+ // one case that stays uncovered.
70+ async function sweepLocations ( ) {
71+ const active = await input . sdk . api . session . active ( ) . catch ( ( ) => undefined )
72+ const ids = Object . keys ( active ?? { } )
73+ const synced = await Promise . all (
74+ ids
75+ . filter ( ( id ) => ! input . data . session . get ( id ) )
76+ . map ( ( id ) =>
77+ input . data . session . sync ( id ) . then (
78+ ( ) => true ,
79+ ( ) => false ,
80+ ) ,
81+ ) ,
5482 )
55- const locations = ids . flatMap ( ( id ) => {
56- const location = input . data . session . get ( id ) ?. location
57- return location ? [ location ] : [ ]
58- } )
59- return [ ...new Map ( locations . map ( ( item ) => [ `${ item . directory } \u0000${ item . workspaceID ?? "" } ` , item ] ) ) . values ( ) ]
83+ const locations = [
84+ ...ids . flatMap ( ( id ) => {
85+ const location = input . data . session . get ( id ) ?. location
86+ return location ? [ location ] : [ ]
87+ } ) ,
88+ ...input . data . session . list ( ) . map ( ( session ) => session . location ) ,
89+ ]
90+ return {
91+ locations : [
92+ ...new Map ( locations . map ( ( item ) => [ `${ item . directory } \u0000${ item . workspaceID ?? "" } ` , item ] ) ) . values ( ) ,
93+ ] ,
94+ complete : active !== undefined && synced . every ( Boolean ) ,
95+ }
6096 }
6197
6298 function approve ( permission : PermissionRequest , attempt = 0 ) {
63- if ( state . disposed || state . responded . has ( permission . id ) ) return
99+ // enabled() guards the retry timer path: the user may disable the setting
100+ // between a failed reply and its scheduled retry.
101+ if ( state . disposed || ! enabled ( ) || state . responded . has ( permission . id ) ) return
64102 remember ( permission . id )
65103 input . sdk . api . permission
66104 . reply ( { sessionID : permission . sessionID , requestID : permission . id , reply : "once" } )
67105 . catch ( ( ) => {
68106 // A reply failure leaves the request pending but invisible (the UI
69107 // hides prompts while auto-approve is on), so retry a bounded number
70- // of times. Sweeps on reconnect or re-enable retry it after that.
108+ // of times. Later sweeps retry it after that.
71109 state . responded . delete ( permission . id )
72- if ( state . disposed || ! enabled ( ) || attempt >= retryLimit ) return
110+ if ( state . disposed || attempt >= retryLimit ) return
73111 setTimeout ( ( ) => approve ( permission , attempt + 1 ) , retryDelayMs * ( attempt + 1 ) )
74112 } )
75113 }
0 commit comments