shellbell 0.0.0-stage → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Paul Miller (https://paulmillr.com)
4
+ Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the “Software”), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Paul Miller (https://paulmillr.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Paul Miller (https://paulmillr.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bilal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ Copyright 2020 Rod Vagg
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import{C as e,F as t,H as n,I as r,L as i,P as a,S as o,Tt as s,U as c,V as l,_ as u,b as d,c as f,ct as p,g as m,h,it as g,jt as _,lt as v,m as y,nt as b,o as x,x as S}from"./service-lifecycle-CcyJ7m6L.js";import{a as C,i as w,o as T,r as E,t as D}from"./service-entry-DT67AfYt.js";import{spawn as O}from"node:child_process";import{closeSync as k,constants as A,openSync as j,realpathSync as M}from"node:fs";import{userInfo as N}from"node:os";import{dirname as P,join as F,resolve as I}from"node:path";import{fileURLToPath as L}from"node:url";import{createHash as R,randomUUID as z}from"node:crypto";import{performance as B}from"node:perf_hooks";function V(e,t,n,a){let o=!1,s=!1,c=0,l=!1,f=!1,p,g,_,v,b=()=>v||(o=!0,clearTimeout(_),C(),e.off(`data`,T),e.off(`end`,E),e.destroy(),v=Promise.resolve(n.close()).finally(()=>{t.end()}),v.catch(()=>{}),v),x=e=>{if(o)return!1;let n=i(e),r=t.writableLength;if(!n||t.destroyed||!Number.isFinite(r)||r<0||r+n.byteLength>262144)return b(),!1;try{t.write(n)}catch{return b(),!1}return!0},S=(e,t)=>x({v:1,id:e,ok:!1,error:{code:t}}),C=n.onEvent(e=>{let t=u.safeParse(e);if(!t.success){b();return}if(f){p?b():p=t.data;return}if(t.data.flowId!==g){b();return}x(t.data),t.data.event===`pairing.closed`&&(g=void 0)}),w=new r({onError:b,onLine(e){if(o)return;let t;try{t=JSON.parse(e)}catch{b();return}let r=d.safeParse(t);if(!r.success){typeof t==`object`&&t&&`id`in t&&typeof t.id==`number`&&Number.isSafeInteger(t.id)&&t.id>c&&S(t.id,`v`in t&&t.v!==1?`unsupported-version`:`bad-request`),b();return}let u=r.data;if(u.id<=c){b();return}if(c=u.id,!s){if(u.cmd!==`hello`){S(u.id,`handshake-required`),b();return}s=!0,clearTimeout(_),x({v:1,id:u.id,ok:!0,data:h.hello.parse({version:1,agentVersion:a.agentVersion,capabilities:[`status`,`settings`,`lifecycle`,`pairing`,`devices`,`diagnostics`]})});return}if(u.cmd===`hello`){b();return}if(l){S(u.id,`busy`),b();return}l=!0,f=u.cmd===`pairing.open`;let v=`args`in u;_=setTimeout(()=>{S(u.id,v?`delivery-unknown`:`timeout`),b()},u.cmd.startsWith(`service.`)||u.cmd.startsWith(`desktop.`)||u.cmd.startsWith(`ownership.`)?6e4:5e3),n.execute(u).then(e=>{if(o)return;let t=h[u.cmd].safeParse(e);if(!t.success){S(u.id,v?`delivery-unknown`:`operation-failed`),b();return}if(u.cmd===`pairing.open`&&(g=t.data.flowId),!i({v:1,id:u.id,ok:!0,data:t.data})){S(u.id,`response-too-large`),b();return}if(x({v:1,id:u.id,ok:!0,data:t.data}),f=!1,p){if(p.flowId!==g){b();return}x(p),p.event===`pairing.closed`&&(g=void 0),p=void 0}},e=>{let t=e instanceof y&&m.safeParse(e.code).success?e.code:`operation-failed`;S(u.id,t),f=!1,p=void 0}).finally(()=>{clearTimeout(_),l=!1})}}),T=e=>w.push(e),E=()=>{w.finish(),b()};return e.on(`data`,T),e.once(`end`,E),e.once(`error`,b),t.once(`error`,b),_=setTimeout(b,5e3),{close:b}}var H=class{options;owned;starting;closed=!1;readiness;clock;constructor(e){this.options=e,this.readiness=e.readiness??new x,this.clock=e.clock??{now:()=>B.now(),sleep:e=>new Promise(t=>setTimeout(t,e))}}async inspect(e){let t=this.owned;if(t&&!t.exited&&(t.selection.serviceInstance!==e.serviceInstance||t.selection.bundlePath!==e.bundlePath))throw new y(`conflict`);return{registration:`not-registered`,loaded:!!t&&!t.exited,pid:t&&!t.exited?t.child.pid??null:null,bundlePath:e.bundlePath}}start(e){return this.closed||this.starting||this.owned&&!this.owned.exited?Promise.reject(new y(`busy`)):!S.safeParse(e).success||e.mode!==`desktop`?Promise.reject(new y(`conflict`)):(this.starting=this.launch(structuredClone(e)).finally(()=>{this.starting=void 0}),this.starting)}async launch(e){this.owned&&await this.stopOwned(this.owned);let t=await(this.options.admitBundle??C)(e.bundlePath);if(t.agentVersion!==e.agentVersion||this.closed||(await this.readiness.observe(p(e.stateDir).sock)).kind!==`absent`||this.closed)throw new y(`conflict`);let n=O(t.nodePath,[t.servicePath,`desktop`],{cwd:this.options.homeDir,env:E(e,this.options.homeDir,process.env),stdio:[`ignore`,`pipe`,`pipe`,`pipe`]}),r={child:n,pipe:n.stdio[3],selection:e,exited:!1};this.owned=r,n.once(`exit`,()=>{r.exited=!0}),n.once(`error`,()=>{r.exited=!0}),n.stdout?.resume(),n.stderr?.resume(),r.pipe.on(`error`,()=>{});try{r.pipe.write(`${JSON.stringify({v:1,instance:e.serviceInstance})}\n`);let t=this.clock.now()+1e4;for(;this.clock.now()<t;){if(r.exited||this.closed||!n.pid)throw new y(`unavailable`);let i=await this.readiness.observe(p(e.stateDir).sock,{stateDir:e.stateDir,computerFp:e.computerFp,serviceInstance:e.serviceInstance,pid:n.pid},Math.min(500,t-this.clock.now()));if(i.kind===`verified`&&i.local?.process.agentVersion===e.agentVersion&&!r.exited&&!this.closed)return this.inspect(e);if(i.kind===`foreign`)throw new y(`conflict`);await this.clock.sleep(Math.min(25,Math.max(0,t-this.clock.now())))}throw new y(`unavailable`)}catch(e){throw await this.stopOwned(r),e}}async stop(e){let t=this.owned;if(!t){if(e.runtime)throw new y(`conflict`);return}let n=e.runtime,r=t.selection;if(!n||n.pid!==t.child.pid||n.stateDir!==r.stateDir||n.computerFp!==r.computerFp||n.serviceInstance!==r.serviceInstance||n.agentVersion!==r.agentVersion)throw new y(`conflict`);await this.stopOwned(t)}async close(){this.closed=!0,this.owned&&await this.stopOwned(this.owned),await this.starting?.catch(()=>{}),this.owned&&await this.stopOwned(this.owned)}stopOwned(e){return e.stopping||=this.finishStop(e).finally(()=>{e.stopping=void 0}),e.stopping}async finishStop(e){e.pipe.end();let t=this.options.stopTimeoutMs??1e4,n=this.clock.now()+t,r=n-t/2,i=!1;for(;!e.exited&&this.clock.now()<n;){if(!i&&this.clock.now()>=r){let t=e.selection,r=await this.readiness.observe(p(t.stateDir).sock,{pid:e.child.pid,stateDir:t.stateDir,computerFp:t.computerFp,serviceInstance:t.serviceInstance},Math.min(500,Math.max(1,n-this.clock.now())));if(r.kind===`foreign`)throw new y(`conflict`);r.kind===`verified`&&r.local?.process.agentVersion===t.agentVersion&&this.owned===e&&!e.exited&&(e.child.kill(`SIGTERM`),i=!0)}await this.clock.sleep(Math.min(25,Math.max(0,n-this.clock.now())))}if(!e.exited)throw new y(`unavailable`);if((await this.readiness.observe(p(e.selection.stateDir).sock)).kind!==`absent`)throw new y(`conflict`);e.pipe.destroy(),this.owned===e&&(this.owned=void 0)}};async function U(e,t,n){await n.preflight();let r=n=>e.publish({v:1,mode:n===`prepared`?e.current?.mode??t.target:t.target,consented:!0,startupEnabled:e.current?.startupEnabled??!1,transition:{...t,phase:n}});e.current?.transition||r(`prepared`);try{await n.stopSource(),await n.verifyAbsent(),r(`source-stopped`),await n.startDestination(),r(`destination-started`),await n.complete(),e.publish({v:1,mode:t.target,consented:!0,startupEnabled:e.current.startupEnabled,transition:null})}catch(e){throw r(`recovery-required`),e}}var W=class{options;desktop;readiness;newId;client;listeners=new Set;observedStateDirs=new WeakMap;closed=!1;constructor(e){this.options=e,this.desktop=e.desktop??new H({uid:e.uid,homeDir:e.homeDir,readiness:e.readiness,admitBundle:e.inspectBundle}),this.readiness=e.readiness??new x,this.newId=e.newId??z}async status(){try{return await this.observe(this.options.store.inspect(),await this.options.platform.inspectLegacy())}catch(e){throw this.error(e)}}stateDirectory(e,t){return this.recordedStateDirectory(e,t)??this.options.defaultStateDir}recordedStateDirectory(e,t){return e?.selection?.stateDir??e?.transition?.destination?.stateDir??e?.transition?.source?.stateDir??t??e?.recovery?.stateDir??null}statusStateDirectory(e){return this.observedStateDirs.get(e)??e.selection?.stateDir??e.legacy.stateDir??this.options.defaultStateDir}async observe(t,n){let r=t?.selection??null,i=r?.bundlePath??t?.transition?.source?.bundlePath??this.options.bundlePath,[a,s,c]=await Promise.all([this.options.platform.inspect(`manual`,i),this.options.platform.inspect(`persistent`,i),this.options.platform.loginStatus(this.options.bundlePath).catch(()=>`unknown`)]),l=this.recordedStateDirectory(t,n.definition?.stateDir),u=l??this.options.defaultStateDir,d=D(u,this.options.uid),m=r?.mode===`desktop`?await this.desktop.inspect(r):{registration:`not-registered`,loaded:!1,pid:null,bundlePath:null},h=r?r.mode===`desktop`?m:e(r.mode)===`manual`?a:s:null,g=d?{stateDir:u,computerFp:d.fp,serviceInstance:r?.serviceInstance??n.definition?.serviceInstance??null,...h?.pid?{pid:h.pid}:{}}:void 0,_=await this.readiness.observe(p(u).sock,g);if(_.kind===`verified`&&(!d||r&&(!h?.loaded||!h.pid||d.fp!==r.computerFp))&&(_.kind=`foreign`),r){let t=r.mode===`desktop`?await this.desktop.inspect(r):await this.options.platform.inspect(e(r.mode),i);JSON.stringify(t)!==JSON.stringify(h)&&(_.kind=`unverified`,_.local=null)}let v=new f({stateDir:u,uid:this.options.uid}).inspect(),y=o.parse({desktopLogin:c,desktop:m,ownership:{revision:v?.revision??null,mode:v?.mode??(r||t?.transition?`legacy-native`:n.installed||n.loaded?`headless`:null),consented:v?.consented??!1,startupEnabled:v?.startupEnabled??null,transition:v?.transition??null},revision:t?.revision??null,selection:r,transition:t?.transition?{id:t.transition.id,action:t.transition.action,phase:t.transition.phase}:null,recoveryAvailable:!!t?.recovery,manualRecoveryAvailable:(t?.transition?.destination?.mode===`persistent`&&r?.serviceInstance===t.transition.destination.serviceInstance&&[`start`,`restart`].includes(t.transition.action)&&!t.transition.restoreLegacy||this.isManualRecovery(t)&&r?.serviceInstance===t?.transition?.source?.serviceInstance)&&!a.loaded&&!s.loaded&&_.kind===`absent`&&!n.installed&&!n.loaded,legacy:{installed:n.installed,loaded:n.loaded,stateDir:n.definition?.stateDir??null},manual:a,persistent:s,local:{kind:_.kind,status:_.local}});return l&&this.observedStateDirs.set(y,l),y}error(e){return e instanceof y?e:e instanceof b?new y(e.code===`busy`?`busy`:e.code===`invalid`?`invalid-config`:`unsafe-state`):e instanceof a?e.code===`server-error`?new y(e.serverCode===`runtime-mismatch`?`conflict`:e.serverCode===`pairing-busy`?`busy`:`operation-failed`):new y(e.code===`protocol-error`||e.code===`closed`?`unavailable`:e.code):new y(`operation-failed`)}exact(e,t){return e===null||t===null?e===t:[`pid`,`agentVersion`,`computerFp`,`stateDir`,`serviceInstance`].every(n=>e[n]===t[n])}expect(e,t,n=!1){if(e.revision!==t.revision||!this.exact(e.local.status?.process??null,t.runtime)||e.local.kind===`foreign`||e.local.kind===`unverified`&&!n)throw new y(`conflict`)}async control(e){let n=e.local.status?.process;if(e.local.kind!==`verified`||!n)throw new y(`unavailable`);if(this.client&&!this.exact(this.client.runtime,n)&&(this.client.close(),this.client=void 0),!this.client){let e=await(this.options.connect??t)(p(n.stateDir).sock,{onPairingRequest:e=>this.emit({...e,v:1}),onPairingClosed:e=>this.emit({...e,v:1}),onDisconnect:()=>{this.client=void 0}});if(this.closed)throw e.close(),new y(`unavailable`);if(!this.exact(e.runtime,n))throw e.close(),new y(`conflict`);this.client=e}return this.client}async settings(e){let t=this.statusStateDirectory(e),n=v(p(t)),r=l(n),i=e.local.kind===`verified`?await(await this.control(e)).configurationRevision():null;return{saved:n,savedRevision:r,appliedRevision:i,applied:e.local.kind===`absent`?`not-running`:i===null?`unknown`:i===r?`matches`:`restart-required`}}async execute(e){try{if(this.closed)throw new y(`unavailable`);let t=d.safeParse(e);if(!t.success||t.data.cmd===`hello`)throw new y(`bad-request`);let r=t.data,i;if(r.cmd===`terminals.connect`){let e=await(await this.control(await this.status())).connectTerminal(r.args.name);i=e?{supported:!0,...e}:{supported:!1,started:!1,terminals:[]}}else if(`args`in r)i=`ownerRevision`in r.args?await this.ownership(r):await this.guardLegacy(r,e=>this.options.store.mutate(r.args.expect.revision,async t=>this.options.platform.withLegacyGuard(async i=>{let a=await i.inspect(),o=await this.observe(t.current,a);if(r.cmd.startsWith(`service.`)&&(o.ownership?.mode===`desktop`||o.ownership?.transition))throw new y(`conflict`);let s=(r.cmd===`service.start`&&r.args.migrateLegacy||r.cmd===`service.recover`&&r.args.action===`continue`&&t.current?.transition?.source===null&&t.current?.transition?.destination!==null&&!!t.current?.recovery)&&a.loaded&&a.definition?.serviceInstance===null&&o.local.kind===`unverified`;if(this.expect(o,r.args.expect,s),r.cmd.startsWith(`service.`))return this.lifecycle(r,t,i,a,o);if(r.cmd===`settings.set`){let t=this.statusStateDirectory(o);if(t!==e)throw new y(`conflict`);let i=p(t);return g(i,e=>{if(l(e)!==r.args.configRevision)throw new y(`conflict`);let t=e;for(let i of r.args.changes){if(n(e,i.key)===i.value)continue;let r=c(t,i.key,i.value,!1);if(`error`in r)throw new y(`invalid-config`);t=r.next}return t}),this.settings(o)}let u=await this.control(o);if(r.cmd===`devices.revoke`)return u.revoke(r.args.phoneFp);if(r.cmd===`pairing.open`)return u.openPairing();if(r.cmd===`pairing.close`)return await u.closePairing(r.args.flowId),{};if(r.cmd===`pairing.confirm`)return await u.confirm(r.args.flowId,r.args.challengeId,r.args.phoneFp,r.args.accept),{};throw new y(`bad-request`)}).catch(e=>{throw this.error(e)})));else{let e=await this.status();if(r.cmd===`status`)i=e;else if(r.cmd===`settings.get`)i=await this.settings(e);else if(r.cmd===`devices`)i=await(await this.control(e)).devices();else if(r.cmd===`terminals`){let t=await(await this.control(e)).terminals();i={supported:t!==null,terminals:t??[]}}else i={checks:await this.diagnose(e)}}let a=h[r.cmd].safeParse(i);if(!a.success)throw new y(`args`in r?`delivery-unknown`:`operation-failed`);return a.data}catch(e){throw this.error(e)}}async guardLegacy(e,t){let n=await this.status(),r=e.cmd===`settings.set`?this.statusStateDirectory(n):n.selection?.stateDir??n.legacy.stateDir??(e.cmd===`service.start`?e.args.stateDir:void 0)??this.options.defaultStateDir;e.cmd===`service.start`&&!n.selection&&!n.legacy.installed&&(D(r,this.options.uid),await this.requireAbsentDestination(r));let i=new f({stateDir:r,uid:this.options.uid});return i.mutate(i.inspect()?.revision??null,async n=>{if(e.cmd.startsWith(`service.`)&&(n.current?.mode===`desktop`||n.current?.transition))throw new y(`conflict`);return t(r)})}async ownership(e){let t=await this.status();this.expect(t,e.args.expect);let n=this.options.store.inspect();if((n?.revision??null)!==t.revision)throw new y(`conflict`);let r=this.stateDirectory(n,t.legacy.stateDir),i=new f({stateDir:r,uid:this.options.uid});if((e.cmd===`desktop.start`||e.cmd===`desktop.stop`)&&(t.ownership?.mode!==`desktop`||!t.ownership.consented))throw new y(`conflict`);return i.mutate(e.args.ownerRevision,async t=>this.options.store.mutate(e.args.expect.revision,async n=>this.options.platform.withLegacyGuard(async i=>{let a=await i.inspect(),o=await this.observe(n.current,a);if(this.expect(o,e.args.expect),e.cmd===`desktop.login.set`){if(t.current?.mode!==`desktop`||!t.current.consented)throw new y(`conflict`);if(t.current.transition||n.current?.transition)throw new y(`recovery-required`);return await this.options.platform.setLogin(this.options.bundlePath,e.args.enabled),t.publish({...t.current,startupEnabled:e.args.enabled}),this.observe(n.current,await i.inspect())}if(e.cmd===`desktop.stop`){if(o.selection?.mode!==`desktop`)throw new y(`conflict`);if(o.local.kind===`absent`&&!o.desktop?.loaded)return o;if(o.local.kind!==`verified`)throw new y(`conflict`);return await this.desktop.stop(e.args.expect),await this.readiness.waitStopped(p(r).sock),this.observe(n.current,await i.inspect())}if(e.cmd===`desktop.setup`){if(t.current||n.current?.selection||n.current?.transition||a.installed||a.loaded||o.manual.loaded||o.persistent.loaded||o.local.kind!==`absent`)throw new y(`conflict`)}else if(e.cmd===`desktop.start`){if(t.current?.transition||n.current?.transition)throw new y(`recovery-required`);if(t.current?.mode!==`desktop`||!t.current.consented||a.installed||a.loaded||o.manual.loaded||o.persistent.loaded)throw new y(`conflict`);if(o.local.kind===`verified`&&o.selection?.mode===`desktop`&&o.desktop?.loaded)return o;if(o.local.kind!==`absent`)throw new y(`conflict`)}else if(e.cmd===`ownership.recover`){if(!t.current?.transition||t.current.transition.id!==e.args.intentId)throw new y(`conflict`)}else if(t.current?.transition)throw new y(`recovery-required`);let s=e.cmd===`ownership.recover`,c=s?t.current.transition.target:e.cmd===`ownership.convert`?e.args.target:`desktop`;if(c===`headless`)return this.convertToHeadless(t,n,i,a,o,r,s);if(await this.admitBundle(this.options.bundlePath),[`foreign`,`unverified`].includes(o.local.kind))throw new y(`conflict`);let l=D(r,this.options.uid,e.cmd===`desktop.setup`);if(!l)throw new y(`unsafe-state`);let u=s&&n.current?.transition?.id===t.current?.transition?.id?n.current.transition:null,d=s&&!n.current?.transition&&n.current?.selection?.mode===`desktop`?n.current.selection:null,f=d?null:u?u.source:n.current?.selection??null,m=s?t.current.transition:{id:this.newId(),source:o.ownership?.mode??null,target:c,sourceManager:f?f.mode===`desktop`?`desktop-child`:f.mode===`manual`?`native-manual`:`native-persistent`:a.installed||a.loaded?`launchd`:null,sourceInstance:f?.serviceInstance??a.definition?.serviceInstance??null,stateDir:r,computerFp:l.fp,targetBundlePath:this.options.bundlePath,targetVersion:this.options.agentVersion,phase:`prepared`};if(m.stateDir!==r||m.computerFp!==l.fp||m.targetBundlePath!==this.options.bundlePath||m.targetVersion!==this.options.agentVersion)throw new y(`conflict`);let h=this.newId(),g=f?.environment??a.definition?.environment,_=d??u?.destination??{mode:`desktop`,stateDir:r,computerFp:l.fp,serviceInstance:h,bundlePath:this.options.bundlePath,bundleId:`sh.bilal.shellbell.host`,agentVersion:this.options.agentVersion,environment:{PATH:g?.PATH??`/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin`,...g?.HERDR_SOCKET_PATH?{HERDR_SOCKET_PATH:g.HERDR_SOCKET_PATH}:{},...g?.XDG_CONFIG_HOME?{XDG_CONFIG_HOME:g.XDG_CONFIG_HOME}:{},SHELLBELL_DIR:r,SHELLBELL_SERVICE_INSTANCE:h}};if(_.mode!==`desktop`||_.computerFp!==l.fp||_.stateDir!==r||_.bundlePath!==m.targetBundlePath||_.agentVersion!==m.targetVersion)throw new y(`conflict`);let v=async()=>{let e=await this.observe(n.current,await i.inspect());return e.selection?.serviceInstance===_.serviceInstance&&e.local.kind===`verified`&&!!e.desktop?.loaded&&!e.manual.loaded&&!e.persistent.loaded&&!e.legacy.installed&&!e.legacy.loaded};return await U(t,m,{preflight:async()=>{if(!S.safeParse(_).success)throw new y(`unsafe-state`);await this.admitBundle(_.bundlePath)},stopSource:async()=>{if(m.source===`headless`&&t.current?.startupEnabled&&await this.options.platform.setLogin(this.options.bundlePath,!0),!u){let e=n.current?.recovery??null;if(a.installed){if(!a.raw||!a.definition||e&&s)throw new y(`recovery-required`);e={id:this.newId(),definitionPath:i.definitionPath,rawBase64:a.raw.toString(`base64`),sha256:R(`sha256`).update(a.raw).digest(`hex`),wasLoaded:a.loaded,stateDir:r,computerFp:l.fp}}this.publish(n,{recovery:e,transition:{id:m.id,action:`start`,phase:`prepared`,source:f,destination:_,recoveryId:e?.id??null}})}if(await v())return;if(f){if(f.stateDir!==r||f.computerFp!==l.fp)throw new y(`conflict`);if(f.mode===`desktop`){let e=await this.observe(n.current,await i.inspect());if(e.desktop?.loaded){if(e.local.kind!==`verified`)throw new y(`conflict`);await this.desktop.stop({revision:e.revision,runtime:e.local.status.process})}}else{let e=await this.options.platform.inspect(f.mode,f.bundlePath),t=f.mode===`persistent`&&!e.loaded;t?await this.options.platform.prepareManualRecovery(f):(e.loaded||e.registration!==`not-registered`)&&await this.options.platform.stop(f,e),await this.readiness.waitStopped(p(r).sock),t||await this.options.platform.remove(f)}}let e=await i.inspect();if(e.installed||e.loaded){let t=n.current?.recovery;if(!t||!e.raw||R(`sha256`).update(e.raw).digest(`hex`)!==t.sha256)throw new y(`conflict`);e.loaded&&await i.unload(),await this.readiness.waitStopped(p(r).sock);let a=await i.inspect();if(a.loaded||!a.raw?.equals(e.raw))throw new y(`conflict`);await i.restore(null)}},verifyAbsent:async()=>{if(await v())return;let e=await this.observe(n.current,await i.inspect());if(e.manual.loaded||e.persistent.loaded||e.legacy.installed||e.legacy.loaded||e.desktop?.loaded||e.local.kind!==`absent`)throw new y(`conflict`);this.phase(n,`source-stopped`,{selection:null})},startDestination:async()=>{await v()||(this.phase(n,`destination-start-requested`,{selection:_}),await this.desktop.start(_),await this.certify(_),this.phase(n,`destination-ready`))},complete:async()=>{this.publish(n,{selection:_,transition:null})}}),this.observe(n.current,await i.inspect())})))}async convertToHeadless(e,t,n,r,i,a,o){if(!o&&e.current?.mode===`headless`&&!t.current?.selection&&!t.current?.transition)return i;if([`foreign`,`unverified`].includes(i.local.kind))throw new y(`conflict`);await this.admitBundle(this.options.bundlePath);let c=await(this.options.inspectBundle??C)(this.options.bundlePath),l=D(a,this.options.uid);if(!l)throw new y(`unsafe-state`);let u=o&&t.current?.transition?.id===e.current?.transition?.id?t.current.transition:null,d=u?u.source:t.current?.selection??null,f=o?e.current.transition:{id:this.newId(),source:i.ownership?.mode??null,target:`headless`,sourceManager:d?.mode===`desktop`?`desktop-child`:d?.mode===`manual`?`native-manual`:d?.mode===`persistent`?`native-persistent`:null,sourceInstance:d?.serviceInstance??null,stateDir:a,computerFp:l.fp,targetBundlePath:this.options.bundlePath,targetVersion:this.options.agentVersion,phase:`prepared`};if(f.target!==`headless`||f.stateDir!==a||f.computerFp!==l.fp||f.targetBundlePath!==this.options.bundlePath||f.targetVersion!==this.options.agentVersion)throw new y(`conflict`);let m=d?.environment??(o&&r.definition?.serviceInstance===f.id?r.definition.environment:void 0),h={nodePath:c.nodePath,cliPath:F(this.options.bundlePath,`Contents/Resources/agent/dist/cli.js`),stateDir:a,serviceInstance:f.id,logPath:p(a).log,environment:{PATH:m?.PATH??`/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin`,SHELLBELL_DIR:a,SHELLBELL_SERVICE_INSTANCE:f.id,...m?.HERDR_SOCKET_PATH?{HERDR_SOCKET_PATH:m.HERDR_SOCKET_PATH}:{},...m?.XDG_CONFIG_HOME?{XDG_CONFIG_HOME:m.XDG_CONFIG_HOME}:{}}},g=e=>{let t=e.definition;return e.installed&&!!t&&t.serviceInstance===f.id&&t.stateDir===a&&t.nodePath===h.nodePath&&t.cliPath===h.cliPath&&t.logPath===h.logPath&&JSON.stringify(Object.entries(t.environment).sort())===JSON.stringify(Object.entries(h.environment).sort())},_={stateDir:a,computerFp:l.fp,serviceInstance:f.id},v=async()=>{let e=await n.inspect();if(!g(e)||!e.loaded||(await this.readiness.observe(p(a).sock,_)).kind!==`verified`)return!1;let t=await n.inspect();return g(t)&&t.loaded};return await U(e,f,{preflight:async()=>{if((r.installed||r.loaded)&&(!o||!g(r))||r.loaded&&(i.manual.loaded||i.persistent.loaded||d?.mode===`desktop`&&(await this.desktop.inspect(d)).loaded)||d&&(d.stateDir!==a||d.computerFp!==l.fp)||i.manual.loaded&&d?.mode!==`manual`||i.persistent.loaded&&d?.mode!==`persistent`)throw new y(`conflict`)},stopSource:async()=>{this.publish(t,{transition:{id:f.id,action:`recover`,phase:`prepared`,source:d,destination:null,recoveryId:t.current?.recovery?.id??null}});let e=await this.options.platform.loginStatus(this.options.bundlePath);if(e===`unknown`)throw new y(`unavailable`);if((e===`enabled`||e===`requires-approval`)&&await this.options.platform.setLogin(this.options.bundlePath,!1),!await v()){if(d?.mode===`desktop`){let e=await this.desktop.inspect(d);if(e.loaded){let n=await this.readiness.observe(p(a).sock,{stateDir:a,computerFp:l.fp,serviceInstance:d.serviceInstance,...e.pid?{pid:e.pid}:{}});if(n.kind!==`verified`)throw new y(`conflict`);await this.desktop.stop({revision:t.current?.revision??null,runtime:n.local.process})}}else if(d){let e=await this.options.platform.inspect(d.mode,d.bundlePath),t=d.mode===`persistent`&&!e.loaded;t&&await this.options.platform.prepareManualRecovery(d),(e.loaded||e.registration!==`not-registered`)&&await this.options.platform.stop(d,e),await this.readiness.waitStopped(p(a).sock),t||await this.options.platform.remove(d)}}},verifyAbsent:async()=>{if(await v())return;let e=await this.observe(t.current,await n.inspect()),r=await n.inspect();if(e.manual.loaded||e.persistent.loaded||e.desktop?.loaded||r.loaded||r.installed&&!g(r)||e.local.kind!==`absent`)throw new y(`conflict`);this.phase(t,`source-stopped`,{selection:null})},startDestination:async()=>{if(await v())return;if(await this.admitBundle(this.options.bundlePath),D(a,this.options.uid)?.fp!==l.fp)throw new y(`unsafe-state`);s(h.logPath)||k(j(h.logPath,A.O_WRONLY|A.O_CREAT|A.O_EXCL|A.O_NOFOLLOW,384)),this.phase(t,`destination-start-requested`,{selection:null});let r=await n.inspect();if(!g(r)){if(r.installed||r.loaded)throw new y(`conflict`);await n.write(h)}await n.setStartupEnabled(e.current.startupEnabled);let i=await n.load();if(await this.readiness.waitReady(p(a).sock,{..._,pid:i}),!await v())throw new y(`conflict`);this.phase(t,`destination-ready`)},complete:async()=>{this.publish(t,{selection:null,transition:null})}}),this.observe(t.current,await n.inspect())}async diagnose(e){return this.options.diagnose?this.options.diagnose(this.statusStateDirectory(e)):[{name:`local-owner`,ok:e.local.kind===`verified`,severity:e.local.kind===`foreign`?`error`:e.local.kind===`verified`?`pass`:`warning`,detail:e.local.kind},{name:`legacy-service`,ok:!e.legacy.loaded,severity:e.legacy.loaded?`warning`:`pass`,detail:e.legacy.loaded?`Legacy service loaded`:`Legacy service stopped`}]}publish(e,t){return e.publish({v:1,selection:null,transition:null,recovery:null,...e.current,...t})}phase(e,t,n={}){let r=e.current?.transition;if(!r)throw new y(`recovery-required`);return this.publish(e,{...n,transition:{...r,phase:t}})}async admitBundle(e,t=this.options.agentVersion){let n=await(this.options.inspectBundle??C)(e);if(n.agentVersion!==t)throw new y(`upgrade-required`);return n}async lifecycle(t,n,r,i,a){if(t.cmd===`service.recover`)return this.recover(t,n,r,i,a);if(n.current?.transition)throw new y(`recovery-required`);let o=n.current?.selection??null;if(a.manual.loaded&&o?.mode!==`manual`||a.persistent.loaded&&o?.mode!==`persistent`||o&&i.loaded||t.cmd!==`service.start`&&!o||t.cmd===`service.start`&&o&&e(o.mode)===t.args.mode&&o.bundlePath===this.options.bundlePath&&a[e(o.mode)].loaded||i.installed&&(t.cmd!==`service.start`||!t.args.migrateLegacy))throw new y(`conflict`);let s=null;if(t.cmd===`service.start`||t.cmd===`service.restart`){await this.admitBundle(this.options.bundlePath),await this.options.platform.preflight(t.cmd===`service.start`?t.args.mode:e(o.mode),this.options.bundlePath);let n=o?.stateDir??i.definition?.stateDir??(t.cmd===`service.start`?t.args.stateDir:void 0)??this.options.defaultStateDir;if(t.cmd===`service.start`&&t.args.stateDir&&t.args.stateDir!==n)throw new y(`conflict`);!o&&!i.installed&&(D(n,this.options.uid),await this.requireAbsentDestination(n));let r=D(n,this.options.uid,!0),a=this.newId();s={mode:t.cmd===`service.start`?t.args.mode:o.mode,stateDir:n,computerFp:r.fp,serviceInstance:a,bundlePath:this.options.bundlePath,bundleId:`sh.bilal.shellbell.host`,agentVersion:this.options.agentVersion,environment:{PATH:`/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin`,SHELLBELL_DIR:n,SHELLBELL_SERVICE_INSTANCE:a}}}let c=n.current?.recovery??null;if(i.installed){if(!i.raw||!i.definition||c)throw new y(`recovery-required`);let e=D(i.definition.stateDir,this.options.uid);if(!e)throw new y(`unsafe-state`);c={id:this.newId(),definitionPath:r.definitionPath,rawBase64:i.raw.toString(`base64`),sha256:R(`sha256`).update(i.raw).digest(`hex`),wasLoaded:i.loaded,stateDir:i.definition.stateDir,computerFp:e.fp}}this.publish(n,{recovery:c,transition:{id:this.newId(),action:t.cmd.slice(8),phase:`prepared`,source:o,destination:s,recoveryId:c?.id??null}});try{return await this.advance(n,r,i,a)}catch(e){throw this.phase(n,`recovery-required`),e}}async stopSource(t,n,r,i){let a=t.current.transition,o=a.source;if(this.phase(t,`source-stop-requested`),o){try{await this.admitBundle(o.bundlePath,o.agentVersion)}catch{throw new y(`recovery-required`)}let t=i[e(o.mode)];(t.loaded||t.registration!==`not-registered`)&&await this.options.platform.stop(o,t),await this.readiness.waitStopped(p(o.stateDir).sock);let n=await this.options.platform.inspect(e(o.mode),o.bundlePath);if(n.loaded||e(o.mode)===`persistent`&&n.registration!==`not-registered`)throw new y(`operation-failed`)}else if(r.installed){r.loaded&&await n.unload(),await this.readiness.waitStopped(p(r.definition.stateDir).sock);let e=await n.inspect();if(e.loaded||!e.raw?.equals(r.raw))throw new y(`conflict`)}else if(i.local.kind!==`absent`)throw new y(`conflict`);if(this.phase(t,`source-stopped`,{selection:a.action===`stop`?o:null}),o&&a.destination&&(e(o.mode)!==e(a.destination.mode)||o.bundlePath!==a.destination.bundlePath)&&await this.options.platform.remove(o),r.installed){await n.restore(null);let e=await n.inspect();if(e.installed||e.loaded)throw new y(`operation-failed`)}}async advance(e,t,n,r){let i=e.current.transition;return await this.stopSource(e,t,n,r),i.destination?await this.startDestination(e,i.destination):i.action===`remove`&&i.source&&(await this.options.platform.remove(i.source),this.publish(e,{selection:null})),e.current.transition?.phase!==`awaiting-approval`&&this.publish(e,{transition:null}),this.observe(e.current,await t.inspect())}async startDestination(e,t){if(await this.admitBundle(t.bundlePath),D(t.stateDir,this.options.uid)?.fp!==t.computerFp)throw new y(`unsafe-state`);if(await this.requireAbsentDestination(t.stateDir),this.phase(e,`destination-start-requested`,{selection:t}),(await this.options.platform.start(t)).registration===`requires-approval`){this.phase(e,`awaiting-approval`);return}await this.certify(t),this.phase(e,`destination-ready`)}async requireAbsentDestination(e){if((await this.readiness.observe(p(e).sock)).kind!==`absent`)throw new y(`conflict`)}async certify(t){let n=t.mode===`desktop`?await this.desktop.inspect(t):await this.options.platform.inspect(e(t.mode),t.bundlePath);if(!n.loaded||!n.pid)throw new y(`operation-failed`);let r=await this.readiness.waitReady(p(t.stateDir).sock,{computerFp:t.computerFp,stateDir:t.stateDir,serviceInstance:t.serviceInstance,pid:n.pid}),i=t.mode===`desktop`?await this.desktop.inspect(t):await this.options.platform.inspect(e(t.mode),t.bundlePath);if(JSON.stringify(n)!==JSON.stringify(i)||r.process.agentVersion!==t.agentVersion)throw new y(`conflict`)}isManualRecovery(e){let t=e?.transition;return t?.action===`recover`&&!t.restoreLegacy&&t.source?.mode===`persistent`&&t.destination?.mode===`manual`&&t.source.stateDir===t.destination.stateDir&&t.source.computerFp===t.destination.computerFp}async recover(t,n,r,i,a){let o=n.current;if(!o||a.manual.loaded&&a.persistent.loaded||o.selection&&i.loaded||o.selection&&a[e(o.selection.mode)===`manual`?`persistent`:`manual`].loaded)throw new y(`conflict`);if(t.args.action===`discard-backup`){if(o.transition||!o.recovery||i.installed&&i.raw?.toString(`base64`)!==o.recovery.rawBase64)throw new y(`conflict`);return this.publish(n,{recovery:null}),this.observe(n.current,i)}let s=o.recovery,c=s?Buffer.from(s.rawBase64,`base64`):null;if(s&&(!c||R(`sha256`).update(c).digest(`hex`)!==s.sha256)||s&&(s.definitionPath!==r.definitionPath||D(s.stateDir,this.options.uid)?.fp!==s.computerFp))throw new y(`unsafe-state`);if(s&&i.installed&&!i.raw?.equals(c))throw new y(`conflict`);let l=this.isManualRecovery(o);if(t.args.action===`use-manual`||t.args.action===`continue`&&l){let e=l?o.transition?.source:o.transition?.destination;if(e?.mode!==`persistent`||o.transition?.restoreLegacy||!l&&![`start`,`restart`].includes(o.transition.action)||o.selection?.serviceInstance!==e.serviceInstance||a.manual.loaded||a.persistent.loaded||a.local.kind!==`absent`||i.installed||i.loaded)throw new y(`conflict`);if(await this.admitBundle(this.options.bundlePath),await this.options.platform.preflight(`manual`,this.options.bundlePath),D(e.stateDir,this.options.uid)?.fp!==e.computerFp)throw new y(`unsafe-state`);let t=this.newId(),s=l?o.transition.destination:{...e,mode:`manual`,bundlePath:this.options.bundlePath,agentVersion:this.options.agentVersion,serviceInstance:t,environment:{...e.environment,SHELLBELL_SERVICE_INSTANCE:t}};l||this.publish(n,{transition:{id:this.newId(),action:`recover`,phase:`prepared`,source:e,destination:s,recoveryId:o.recovery?.id??null}});try{await this.options.platform.prepareManualRecovery(e),await this.requireAbsentDestination(e.stateDir);let t=await this.observe(n.current,await r.inspect());if(t.manual.loaded||t.persistent.loaded||t.legacy.installed||t.legacy.loaded||t.local.kind!==`absent`)throw new y(`conflict`);return this.publish(n,{selection:null,transition:{id:this.newId(),action:`start`,phase:`source-stopped`,source:null,destination:s,recoveryId:o.recovery?.id??null}}),await this.startDestination(n,s),this.publish(n,{transition:null}),this.observe(n.current,await r.inspect())}catch(e){throw this.phase(n,`recovery-required`),e}}if(t.args.action===`continue`&&o.transition?.action===`recover`&&!o.transition.restoreLegacy)throw new y(`recovery-required`);try{if(t.args.action===`restore-legacy`||o.transition?.restoreLegacy){if(!s||!c)throw new y(`conflict`);if(t.args.action===`restore-legacy`){if(i.loaded)throw new y(`conflict`);this.publish(n,{transition:{id:this.newId(),action:`recover`,phase:`prepared`,source:o.selection,destination:null,recoveryId:s.id,restoreLegacy:{restartPrevious:t.args.restartPrevious}}})}return await this.restoreLegacy(n,r,i,a,c)}let l=o.transition;if(!l)throw new y(`conflict`);let u=l.destination;if(u&&o.selection?.serviceInstance===u.serviceInstance){let t=a[e(u.mode)];if(i.loaded||a[e(u.mode)===`manual`?`persistent`:`manual`].loaded)throw new y(`conflict`);if(t.registration===`requires-approval`)return this.phase(n,`awaiting-approval`),this.observe(n.current,i);if(t.loaded)await this.certify(u),this.phase(n,`destination-ready`);else{if(a.local.kind!==`absent`)throw new y(`conflict`);await this.startDestination(n,u)}return n.current.transition?.phase!==`awaiting-approval`&&this.publish(n,{transition:null}),this.observe(n.current,await r.inspect())}return await this.advance(n,r,i,a)}catch(e){throw n.current?.transition&&this.phase(n,`recovery-required`),e}}async restoreLegacy(e,t,n,r,i){let a=e.current,o=a.recovery,s=a.transition.restoreLegacy;if(n.loaded){if(!s.restartPrevious||a.selection||r.manual.loaded||r.persistent.loaded||r.local.kind!==`verified`||!n.raw?.equals(i))throw new y(`conflict`);let o=await t.inspect();if(!o.loaded||!o.raw?.equals(i))throw new y(`conflict`);return this.publish(e,{selection:null,transition:null}),this.observe(e.current,o)}if(a.selection)await this.stopSource(e,t,{installed:!1,loaded:!1,raw:null,definition:null},r);else if(r.manual.loaded||r.persistent.loaded||r.local.kind!==`absent`)throw new y(`conflict`);await this.readiness.waitStopped(p(o.stateDir).sock),this.phase(e,`source-stopped`,{selection:null}),n.installed||await t.restore(i);let c=await t.inspect();if(!c.raw?.equals(i)||c.loaded)throw new y(`operation-failed`);if(s.restartPrevious){this.phase(e,`destination-start-requested`);let n=await t.load();await this.readiness.waitReady(p(o.stateDir).sock,{computerFp:o.computerFp,stateDir:o.stateDir,serviceInstance:c.definition?.serviceInstance??null,pid:n});let r=await t.inspect();if(!r.loaded||!r.raw?.equals(i))throw new y(`conflict`)}return this.publish(e,{selection:null,transition:null}),this.observe(e.current,await t.inspect())}emit(e){for(let t of this.listeners)t(e)}onEvent(e){return this.listeners.add(e),()=>this.listeners.delete(e)}async close(){this.closed=!0,this.client?.close(),this.client=void 0,this.listeners.clear(),await this.desktop.close()}};async function G(){if(process.argv.length===3&&[`--help`,`--version`].includes(process.argv[2])){process.stdout.write(process.argv[2]===`--version`?`${_}\n`:`Shellbell native controller
3
+ `);return}if(process.argv.length!==2||process.execArgv.length||process.env.NODE_OPTIONS||process.env.NODE_PATH)throw Error();let e=N();if(process.platform!==`darwin`||e.uid<=0)throw Error();let t=I(P(L(import.meta.url)),`../../../..`);if((await C(t)).agentVersion!==_)throw Error();let n=F(e.homedir,`Library/Application Support/Shellbell`),r=new W({store:new w({root:n,uid:e.uid}),platform:T({root:n,uid:e.uid,homeDir:e.homedir}),bundlePath:t,uid:e.uid,homeDir:e.homedir,agentVersion:_,defaultStateDir:F(e.homedir,`.shellbell`)}),i=V(process.stdin,process.stdout,r,{agentVersion:_}),a=()=>{i.close().catch(()=>{process.exitCode=1})};process.once(`SIGTERM`,a),process.once(`SIGINT`,a)}process.argv[1]&&M(process.argv[1])===L(import.meta.url)&&G().catch(()=>{process.stderr.write(`Shellbell controller unavailable
4
+ `),process.exitCode=1});export{};
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import{ct as e,et as t,jt as n,w as r}from"./service-lifecycle-CcyJ7m6L.js";import{a as i,n as a,r as o}from"./service-entry-DT67AfYt.js";import{createReadStream as s,fstatSync as c,realpathSync as l}from"node:fs";import{userInfo as u}from"node:os";import{dirname as d,join as f,resolve as p}from"node:path";import{fileURLToPath as m}from"node:url";async function h(){if(process.argv.length===3&&[`--help`,`--version`].includes(process.argv[2])){process.stdout.write(process.argv[2]===`--version`?`${n}\n`:`Shellbell native service
3
+ `);return}let l=r.parse(process.argv[2]);if(process.argv.length!==3||process.execArgv.length||process.env.NODE_OPTIONS||process.env.NODE_PATH)throw Error();let h=u();if(process.platform!==`darwin`||h.uid<=0)throw Error();let g=p(d(m(import.meta.url)),`../../../..`),_=await i(g);if(_.agentVersion!==n)throw Error();let v=f(h.homedir,`Library/Application Support/Shellbell`),y=await a(l,{...l===`desktop`?{ownerInput:(()=>{let e=c(3);if(!e.isFIFO()&&!e.isSocket())throw Error(`invalid owner channel`);return s(``,{fd:3,autoClose:!0})})(),onOwnerLost:e=>process.exit(e===null?0:1)}:{},bundlePath:g,root:v,uid:h.uid,async start(n){if(n.agentVersion!==_.agentVersion)throw Error();let r=o(n,h.homedir,process.env);for(let e of Object.keys(process.env))delete process.env[e];Object.assign(process.env,r);let{buildAgent:i,shutdown:a}=await import(new URL(`./cli.js`,import.meta.url).href),s=e(n.stateDir),c=t({file:s.log,stdout:!1}),l={debug:()=>{},info:()=>c.info(`agent-event`),warn:()=>c.warn(`agent-event`),error:()=>c.error(`agent-event`),child:()=>l},u=await i(l,void 0,!1,{paths:s,serviceInstance:n.serviceInstance,nativeService:!0});try{await u.control.start(),u.agent.start()}catch(e){throw u.stopBackendDetectors(),u.agent.stop(),await u.control.stop(),e}let d;return{stop(){return d??=a(u.agent,u.control,e=>{e&&process.exit(1)},u.stopBackendDetectors),d}}}}),b=()=>{y.stop().then(()=>process.exit(0),()=>process.exit(1))};process.once(`SIGTERM`,b),process.once(`SIGINT`,b)}process.argv[1]&&l(process.argv[1])===m(import.meta.url)&&h().catch(()=>{process.stderr.write(`Shellbell service unavailable
4
+ `),process.exitCode=1});export{};
@@ -0,0 +1,11 @@
1
+ import{A as e,C as t,Ct as n,D as r,Dt as i,E as a,Et as o,O as s,T as c,Tt as l,c as u,ct as d,dt as f,f as p,i as m,k as h,lt as g,m as _,p as v,v as y,x as b,y as x}from"./service-lifecycle-CcyJ7m6L.js";import{createRequire as S}from"node:module";import{closeSync as C,constants as w,fstatSync as T,mkdirSync as E,openSync as ee,readdirSync as D,realpathSync as O,unlinkSync as k}from"node:fs";import{basename as te,dirname as A,join as j}from"node:path";import{z as M}from"zod";const N=`sh.bilal.shellbell.host.manual`;function P(e){(!c.safeParse(e).success||e!==j(e))&&F();for(let t=e;;t=A(t)){let e=l(t);if(e&&(!e.isDirectory()||e.isSymbolicLink())&&F(),A(t)===t)break}}function F(){throw new _(`unsafe-state`)}function I(e){return(!c.safeParse(e).success||[...e].some(e=>e.charCodeAt(0)<32||e.charCodeAt(0)===127))&&F(),e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`).replace(/'/g,`&apos;`)}function L(e){return(e!==j(e)||!e.endsWith(`/Shellbell.app/Contents/MacOS/Shellbell`))&&F(),Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0"><dict>
4
+ <key>Label</key><string>${N}</string>
5
+ <key>ProgramArguments</key><array><string>${I(e)}</string><string>--service-run</string><string>manual</string></array>
6
+ <key>RunAtLoad</key><true/>
7
+ <key>KeepAlive</key><true/>
8
+ <key>ThrottleInterval</key><integer>10</integer>
9
+ </dict></plist>
10
+ `)}function R(e,t){return e===null||t===null?e===t:i(e.stat,t.stat)&&e.stat.mode===t.stat.mode&&e.stat.uid===t.stat.uid&&e.stat.size===t.stat.size&&e.stat.mtimeMs===t.stat.mtimeMs&&e.stat.ctimeMs===t.stat.ctimeMs&&e.raw.equals(t.raw)}function z(e,t){let r=j(e,`manual.plist`),a,s=i=>{try{if(P(e),!l(e))return null;o(e,t);let a=l(r);if(!a)return null;let s=n(r,65536,t);return s.equals(L(i))||F(),{stat:a,raw:s}}catch{return F()}},c=e=>{let t=s(e);return a!==void 0&&!R(a,t)&&F(),a=t,t};return{path:r,inspect(e){return c(e)!==null},write(n){let u=L(n),d=c(n);P(e),l(e)||E(e,{recursive:!0,mode:448});let p=o(e,t);f(r,u,()=>{P(e),(!i(p,o(e,t))||!R(d,s(n)))&&F()}),a=s(n),a?.raw.equals(u)||F()},remove(e){let t=c(e);t&&(R(t,s(e))||F(),k(r),a=null)}}}const B=`sh.bilal.shellbell.host.agent`,V=65536;function H(){throw new _(`unsafe-state`)}function U(e){(!c.safeParse(e).success||e!==j(e)||te(e)!==`Shellbell.app`)&&H();let t=j(e,`Contents`,`MacOS`,`Shellbell`);return c.safeParse(t).success||H(),t}async function W(e,t,n,r,i=!1,a=1e4){try{let i=await e(t,n,{input:r,timeoutMs:a,maxOutputBytes:V,captureOutput:!0});if(!Number.isInteger(i.exitCode)||i.exitCode<0||i.exitCode>255||!Buffer.isBuffer(i.stdout)||i.stdout.length>V)throw Error(`invalid command result`);return i}catch{throw new _(i?`delivery-unknown`:`unavailable`)}}function G(e,t){return i(e,t)&&e.mode===t.mode&&e.uid===t.uid&&e.size===t.size&&e.mtimeMs===t.mtimeMs&&e.ctimeMs===t.ctimeMs}async function K(e,t,i=r){let a=U(e),o=j(e,`Contents`,`Info.plist`),s=new Map,c=()=>{P(A(e));for(let n of[e,j(e,`Contents`),j(e,`Contents`,`MacOS`),o,a]){let e=l(n),r=n===o||n===a;(!e||e.isSymbolicLink()||(r?!e.isFile():!e.isDirectory())||e.uid!==t&&e.uid!==0||e.mode&3602||n===a&&!(e.mode&73))&&H();let i=s.get(n);i&&!G(i,e)&&H(),s.set(n,e)}};try{c();let e=await W(i,`/usr/bin/plutil`,[`-convert`,`json`,`-o`,`-`,`-`],n(o,V));e.exitCode!==0&&H();let t=JSON.parse(new TextDecoder(`utf-8`,{fatal:!0}).decode(e.stdout));return(t?.CFBundleIdentifier!==`sh.bilal.shellbell.host`||t?.CFBundleExecutable!==`Shellbell`)&&H(),c(),{executable:a,revalidate:c}}catch{return H()}}function q(e,t,n,r){let i;try{i=new TextDecoder(`utf-8`,{fatal:!0}).decode(e)}catch{return H()}let a=i.trim().split(/\r?\n/).map(e=>e.trim());(a.shift()!==`${t} = {`||a.pop()!==`}`)&&H();let o=1,s,c=null,l=!1,u,d=!1;for(let e of a)if(e){if(e===`}`){o<=1&&H(),d&&o===2&&(d=!1),o--;continue}if(o===1&&e.startsWith(`program = `)){s!==void 0&&H(),s=e.slice(10);continue}if(o===1&&e.startsWith(`pid = `)){(l||!/^pid = [1-9][0-9]*$/.test(e))&&H(),l=!0,c=Number(e.slice(6)),(!Number.isSafeInteger(c)||c>2147483647)&&H();continue}if(o===1&&/^(?:pid|program|arguments)(?:\s|=)/.test(e)&&e!==`arguments = {`&&H(),e.endsWith(`= {`)){o===1&&e===`arguments = {`?(u!==void 0&&H(),u=[],d=!0):d&&H(),o++;continue}d&&o===2?u.push(e):(e.includes(`{`)||e.includes(`}`))&&H()}return(o!==1||s!==n||!u||u.length!==3||u[0]!==(r===`persistent`?`Shellbell`:n)||u[1]!==`--service-run`||u[2]!==r)&&H(),c}function J(e){let{root:n,uid:i,homeDir:o}=e;(!Number.isSafeInteger(i)||i<=0||!c.safeParse(n).success||!c.safeParse(o).success||n!==j(n)||o!==j(o))&&H();let u=j(o,`Library`,`LaunchAgents`);(n===u||n.startsWith(`${u}/`))&&H();let d=e.run??r,f=z(n,i),p=()=>e.legacy??a({run:d,uid:i,homeDir:o}),h=`gui/${i}`,g=e=>`${h}/${e===`manual`?N:B}`,v=e=>{b.safeParse(e).success||H()};async function x(e,t,n,r=`service`){let a=await K(e,i,d);await n?.(),a.revalidate();let o=t!==`status`,s=await W(d,a.executable,[r===`login`?`--login-api`:`--service-api`,t],void 0,o);a.revalidate();let c;try{c=y.parse(JSON.parse(new TextDecoder(`utf-8`,{fatal:!0}).decode(s.stdout)))}catch{throw new _(o?`delivery-unknown`:`unavailable`)}if(!c.ok)throw new _(c.error.code===`denied`?`approval-required`:c.error.code===`invalid-bundle`?`unsafe-state`:`operation-failed`);if(s.exitCode!==0)throw new _(o?`delivery-unknown`:`unavailable`);return c.status}async function S(e,t,n=1e4){let r=await W(d,`/bin/launchctl`,[`print`,g(e)],void 0,!1,n);if(r.exitCode===113)return{loaded:!1,pid:null};if(r.exitCode!==0)throw new _(`unavailable`);return{loaded:!0,pid:q(r.stdout,g(e),U(t),e)}}let C={loginStatus:e=>x(e,`status`,void 0,`login`),async setLogin(e,t){let n=await K(e,i,d),r=await W(d,`/usr/bin/codesign`,[`--verify`,`--strict`,e]);if(n.revalidate(),r.exitCode!==0)throw new _(`startup-unavailable`);let a=await x(e,`status`,void 0,`login`);if(a===`not-found`&&!t||a===`unknown`)throw new _(`startup-unavailable`);if(t?a===`enabled`||a===`requires-approval`:a===`not-registered`)return a;try{await x(e,t?`register`:`unregister`,void 0,`login`)}catch(n){if(!(t&&n instanceof _&&n.code===`approval-required`&&await x(e,`status`,void 0,`login`)===`requires-approval`))throw n}let o=await x(e,`status`,void 0,`login`);if(t?o!==`enabled`&&o!==`requires-approval`:o!==`not-registered`)throw new _(`operation-failed`);return o},async preflight(e,t){await s(d,i,o);let n=await K(t,i,d);if(e===`persistent`){let e=await W(d,`/usr/bin/codesign`,[`--verify`,`--strict`,t]);if(n.revalidate(),e.exitCode!==0||await x(t,`status`)===`not-found`)throw new _(`startup-unavailable`)}},async prepareManualRecovery(e){if(await s(d,i,o),v(e),t(e.mode)!==`persistent`&&H(),(await C.inspect(`persistent`,e.bundlePath)).loaded)throw new _(`conflict`);if((await W(d,`/bin/launchctl`,[`disable`,g(`persistent`)],void 0,!0)).exitCode!==0)throw new _(`operation-failed`);let n=await W(d,`/bin/launchctl`,[`print-disabled`,h]),r=new TextDecoder(`utf-8`,{fatal:!0}).decode(n.stdout).trim().split(/\r?\n/).map(e=>e.trim());if(n.exitCode!==0||r.shift()!==`disabled services = {`||r.pop()!==`}`||r.filter(e=>e.includes(`"${B}"`)).join(`
11
+ `)!==`"${B}" => disabled`)throw new _(`operation-failed`);let a=await C.inspect(`persistent`,e.bundlePath);if(a.loaded)throw new _(`conflict`);a.registration!==`not-registered`&&a.registration!==`not-found`&&await C.stop(e,a)},inspectLegacy:()=>p().inspect(),async inspect(e,t){e!==`manual`&&e!==`persistent`&&H();let n=U(t),r;e===`manual`?r=f.inspect(n)?`enabled`:`not-registered`:(P(A(t)),r=l(t)?await x(t,`status`):`not-found`);let i=await S(e,t);return e===`manual`&&i.loaded&&r!==`enabled`&&H(),{registration:r,...i,bundlePath:t}},async start(e){if(v(e),await C.preflight(t(e.mode),e.bundlePath),(await C.inspect(t(e.mode),e.bundlePath)).loaded)throw new _(`conflict`);if(t(e.mode)===`persistent`){if((await W(d,`/bin/launchctl`,[`enable`,g(`persistent`)],void 0,!0)).exitCode!==0)throw new _(`operation-failed`);return{registration:await x(e.bundlePath,`register`,async()=>{if((await S(`persistent`,e.bundlePath)).loaded)throw new _(`conflict`)}),...await S(t(e.mode),e.bundlePath),bundlePath:e.bundlePath}}let n=await K(e.bundlePath,i,d);if(f.write(n.executable),(await S(`manual`,e.bundlePath)).loaded)throw new _(`conflict`);if(f.inspect(n.executable),n.revalidate(),(await W(d,`/bin/launchctl`,[`bootstrap`,h,f.path],void 0,!0)).exitCode!==0)throw new _(`operation-failed`);return C.inspect(`manual`,e.bundlePath)},async stop(e,n){v(e),n.bundlePath!==e.bundlePath&&H();let r=await C.inspect(t(e.mode),e.bundlePath);if(r.loaded!==n.loaded||r.pid!==n.pid||r.registration!==n.registration)throw new _(`conflict`);if(t(e.mode)===`persistent`){if(await x(e.bundlePath,`unregister`,async()=>{let t=await S(`persistent`,e.bundlePath);if(t.loaded!==r.loaded||t.pid!==r.pid)throw new _(`conflict`)})!==`not-registered`)throw new _(`operation-failed`)}else if(r.loaded&&(f.inspect(U(e.bundlePath)),(await W(d,`/bin/launchctl`,[`bootout`,g(`manual`)],void 0,!0)).exitCode!==0))throw new _(`operation-failed`);let i=performance.now()+1e4;for(;;){let n=i-performance.now();if(n<=0)throw new _(`operation-failed`);let a=await S(t(e.mode),e.bundlePath,n);if(!a.loaded)break;if(a.pid!==null&&a.pid!==r.pid)throw new _(`conflict`);let o=Math.min(100,i-performance.now());if(o<=0)throw new _(`operation-failed`);await new Promise(e=>setTimeout(e,o))}},async remove(e){v(e);let n=await C.inspect(t(e.mode),e.bundlePath);if(n.loaded||t(e.mode)===`persistent`&&n.registration!==`not-registered`)throw new _(`conflict`);t(e.mode)===`manual`&&f.remove(U(e.bundlePath))},withLegacyGuard(e){return m(p(),!0,e)}};return C}const Y=()=>{throw new _(`unsafe-state`)};async function X(e,t={}){try{(O(e)!==e||[...e].some(e=>e.charCodeAt(0)<32||e.charCodeAt(0)===127))&&Y();let i=t.run??r,a=await K(e,process.getuid(),i),o=j(e,`Contents`),s=j(o,`Resources`),c=j(o,`Helpers/node`),u=j(s,`agent/dist/native-controller.js`),d=j(s,`agent/dist/native-service.js`),f=[c,u,d,j(s,`runtime/LICENSE`),j(s,`agent/package.json`),j(s,`agent/dist/cli.js`)],p=()=>{for(let t of f){for(let n=A(t);n!==e;n=A(n)){let e=l(n);(!e?.isDirectory()||e.isSymbolicLink()||e.mode&3602||e.uid!==0&&e.uid!==process.getuid())&&Y()}let n=l(t);(!n?.isFile()||n.isSymbolicLink()||n.mode&3602||n.uid!==0&&n.uid!==process.getuid()||t===c&&!(n.mode&73))&&Y()}let t=0,n=new Set,r=e=>{++t>1e5&&Y();let i=l(e);if(!i)throw new _(`unsafe-state`);let a=O(e);if(a.startsWith(`${s}/`)||Y(),i.isSymbolicLink()){r(a);return}if(!n.has(a)){if(n.add(a),(i.uid!==0&&i.uid!==process.getuid()||i.mode&3602)&&Y(),i.isDirectory())for(let t of D(e))r(j(e,t));else i.isFile()||Y()}};for(let e of[`agent`,`runtime`])r(j(s,e))};p();let m=JSON.parse(n(j(s,`agent/package.json`),65536).toString(`utf8`));(m.name!==`shellbell`||typeof m.version!=`string`||!m.version||m.type!==`module`)&&Y();let h=S(j(s,`agent/package.json`));for(let e of Object.keys(m.dependencies??{}))O(h.resolve(e)).startsWith(`${s}/agent/`)||Y();let g=await i(`/usr/bin/plutil`,[`-convert`,`json`,`-o`,`-`,j(o,`Info.plist`)],{timeoutMs:1e4,maxOutputBytes:65536,captureOutput:!0});(g.exitCode!==0||JSON.parse(g.stdout.toString(`utf8`)).CFBundleShortVersionString!==m.version)&&Y();let v=await i(c,[`--version`],{timeoutMs:1e4,maxOutputBytes:65536,captureOutput:!0});(v.exitCode!==0||v.stdout.toString(`utf8`).trim()!==`v22.23.1`)&&Y();let y=await i(c,[`-p`,`process.platform + ':' + process.arch`],{timeoutMs:1e4,maxOutputBytes:65536,captureOutput:!0});(y.exitCode!==0||y.stdout.toString(`utf8`).trim()!==`${process.platform}:${process.arch}`)&&Y();for(let e of[j(s,`agent/dist/cli.js`),u,d]){let t=await i(c,[e,`--version`],{timeoutMs:1e4,maxOutputBytes:65536,captureOutput:!0});(t.exitCode!==0||t.stdout.toString(`utf8`).trim()!==m.version)&&Y()}return a.revalidate(),p(),{executable:a.executable,nodePath:c,controllerPath:u,servicePath:d,agentVersion:m.version}}catch(e){if(e instanceof _)throw e;return Y()}}const Z=M.strictObject({v:M.literal(1),instance:M.uuid()});function ne(e,t){let n,r,i=new Promise((e,t)=>{n=e,r=t}),a=!1,o=!1,s=Buffer.alloc(0),c=setTimeout(()=>u(),t.timeoutMs??5e3),l=()=>{clearTimeout(c),e.off(`data`,d),e.off(`end`,u),e.off(`close`,u),e.off(`error`,u)},u=()=>{o||(o=!0,l(),r(new _(`unavailable`)),Promise.resolve().then(()=>t.onLost()).catch(()=>{}))},d=e=>{if(o)return;let r=Buffer.isBuffer(e)?e:Buffer.from(e);if(a||s.length+r.length>1024){u();return}s=Buffer.concat([s,r]);let i=s.indexOf(10);if(!(i<0))try{let e=Z.parse(JSON.parse(new TextDecoder(`utf-8`,{fatal:!0}).decode(s.subarray(0,i))));if(i!==s.length-1||e.instance!==t.instance)throw Error();a=!0,s=Buffer.alloc(0),clearTimeout(c),n()}catch{u()}};return e.on(`data`,d),e.once(`end`,u),e.once(`close`,u),e.once(`error`,u),(e.destroyed||e.readableEnded)&&u(),{ready:i,close:u}}var Q=class extends p{constructor(e){super({...e,filename:`controller.json`,maxBytes:v,schema:x})}};function re(e,t,n){if(n.NODE_OPTIONS||n.NODE_PATH||!b.safeParse(e).success||!c.safeParse(t).success)throw new _(`unsafe-state`);return{...e.environment,HOME:t}}function $(t,r,i=!1){try{if(r<=0||!c.safeParse(t).success||[...t].some(e=>e.charCodeAt(0)<32||e.charCodeAt(0)===127))throw Error();for(let e=t;;e=A(e)){let t=l(e);if(t&&(!t.isDirectory()||t.isSymbolicLink()))throw Error();if(A(e)===e)break}if(!l(t)){if(!i)return null;E(t,{recursive:!0,mode:448})}if(o(t,r),O(t)!==t)throw Error();let a=d(t);for(let e of[a.identity,a.pairings,a.config,a.pid])l(e)&&n(e,65536,r);ie(a.log,r);let s=l(a.sock);if(s&&(!s.isSocket()||s.uid!==r||(s.mode&4095)!=384))throw Error();return g(a),i?h(a):e(a)}catch{throw new _(`unsafe-state`)}}function ie(e,t){let n=l(e);if(!n)return;let r=e=>{if(!e.isFile()||e.isSymbolicLink()||e.uid!==t||(e.mode&4095)!=384)throw Error()};r(n);let a=ee(e,w.O_RDONLY|w.O_NOFOLLOW|w.O_NONBLOCK);try{let t=T(a);r(t);let o=l(e);if(!o||(r(o),!i(n,t)||!i(t,o)))throw Error()}finally{C(a)}}async function ae(e,t){let n=new Q(t).inspect()?.selection;if(!n||n.mode!==e||n.bundlePath!==t.bundlePath)throw new _(`conflict`);let r=$(n.stateDir,t.uid);if(!r||r.fp!==n.computerFp)throw new _(`unsafe-state`);if(e!==`desktop`)return t.start(n);let i=new u({stateDir:n.stateDir,uid:t.uid}).inspect();if(!t.ownerInput||i?.mode!==`desktop`||!i.consented||i.transition&&(i.transition.target!==`desktop`||![`source-stopped`,`destination-started`].includes(i.transition.phase)))throw new _(`conflict`);let a=!1,o,s,c=()=>s??=(async()=>{o&&await(await o).stop()})(),l=ne(t.ownerInput,{instance:n.serviceInstance,onLost:async()=>{a=!0;try{await c(),t.onOwnerLost?.(null)}catch(e){t.onOwnerLost?.(e)}}});try{if(await l.ready,a)throw new _(`unavailable`);if(o=t.start(n),await o,a)throw await c(),new _(`unavailable`);return{stop:async()=>{await c(),l.close()}}}catch(e){throw l.close(),e}}export{X as a,Q as i,ae as n,J as o,re as r,$ as t};