88 type MacOSDesktopCodexAppPathCandidate ,
99} from "./desktop-app-paths.js" ;
1010
11+ const MAX_COMPUTER_USE_PLUGIN_TREE_ENTRIES = 4_096 ;
12+
1113/** Fingerprints every desktop candidate that can own a managed fallback artifact. */
1214export async function readMacOSDesktopGenerationFingerprint (
1315 candidates : readonly MacOSDesktopCodexAppPathCandidate [ ] = resolveMacOSDesktopCodexAppPathCandidates (
@@ -21,6 +23,8 @@ export async function readMacOSDesktopGenerationFingerprint(
2123 for ( const artifactPath of resolveMacOSDesktopGenerationPaths ( candidate ) ) {
2224 entries . push ( `${ artifactPath } \0${ await statFingerprint ( artifactPath ) } ` ) ;
2325 }
26+ const pluginRoot = resolveComputerUsePluginRoot ( candidate ) ;
27+ entries . push ( `${ pluginRoot } \0${ await directoryTreeFingerprint ( pluginRoot ) } ` ) ;
2428 }
2529 return createHash ( "sha256" ) . update ( entries . join ( "\0" ) ) . digest ( "hex" ) ;
2630}
@@ -31,13 +35,6 @@ function resolveMacOSDesktopGenerationPaths(
3135 return [
3236 candidate . appBundlePath ,
3337 path . join ( candidate . bundledMarketplacePath , ".agents" , "plugins" , "marketplace.json" ) ,
34- path . join (
35- candidate . bundledMarketplacePath ,
36- "plugins" ,
37- "computer-use" ,
38- ".codex-plugin" ,
39- "plugin.json" ,
40- ) ,
4138 ...candidate . computerUseServiceAppPaths . flatMap ( ( servicePath ) => [
4239 servicePath ,
4340 path . join ( servicePath , "Contents" , "Info.plist" ) ,
@@ -54,7 +51,11 @@ function resolveMacOSDesktopGenerationPaths(
5451 ] ;
5552}
5653
57- /** Stable directories whose immediate children cover every fingerprinted artifact. */
54+ function resolveComputerUsePluginRoot ( candidate : MacOSDesktopCodexAppPathCandidate ) : string {
55+ return path . join ( candidate . bundledMarketplacePath , "plugins" , "computer-use" ) ;
56+ }
57+
58+ /** Stable roots that cover bundle replacement and recursive artifact updates. */
5859export function resolveMacOSDesktopGenerationWatchPaths (
5960 candidates : readonly MacOSDesktopCodexAppPathCandidate [ ] = resolveMacOSDesktopCodexAppPathCandidates (
6061 "darwin" ,
@@ -63,26 +64,55 @@ export function resolveMacOSDesktopGenerationWatchPaths(
6364 const watched = new Set < string > ( [ "/Applications" ] ) ;
6465 for ( const candidate of candidates ) {
6566 watched . add ( candidate . appBundlePath ) ;
66- const directoryArtifacts = new Set ( [
67- candidate . appBundlePath ,
68- ...candidate . computerUseServiceAppPaths ,
69- ] ) ;
70- for ( const artifactPath of [
71- candidate . appServerCommandPath ,
72- ...resolveMacOSDesktopGenerationPaths ( candidate ) ,
73- ] ) {
74- let directory = directoryArtifacts . has ( artifactPath )
75- ? artifactPath
76- : path . dirname ( artifactPath ) ;
77- while ( directory . startsWith ( `${ candidate . appBundlePath } ${ path . sep } ` ) ) {
78- watched . add ( directory ) ;
79- directory = path . dirname ( directory ) ;
80- }
81- }
8267 }
8368 return [ ...watched ] ;
8469}
8570
71+ async function directoryTreeFingerprint ( root : string ) : Promise < string > {
72+ let rootStat : BigIntStats ;
73+ try {
74+ rootStat = await fs . lstat ( root , { bigint : true } ) ;
75+ } catch ( error ) {
76+ if ( isNodeError ( error , "ENOENT" ) || isNodeError ( error , "ENOTDIR" ) ) {
77+ return "missing" ;
78+ }
79+ throw error ;
80+ }
81+ if ( ! rootStat . isDirectory ( ) ) {
82+ return statFingerprint ( root ) ;
83+ }
84+
85+ let entryCount = 0 ;
86+ const hash = createHash ( "sha256" ) ;
87+ hash . update ( "openclaw-codex-computer-use-plugin-tree-v1\0" ) ;
88+ const visit = async ( directory : string , relativeDirectory : string , before : BigIntStats ) => {
89+ hash . update ( `directory\0${ relativeDirectory } \0${ statTuple ( before ) } \0` ) ;
90+ const entries = ( await fs . readdir ( directory , { withFileTypes : true } ) ) . toSorted (
91+ ( left , right ) => ( left . name < right . name ? - 1 : left . name > right . name ? 1 : 0 ) ,
92+ ) ;
93+ for ( const entry of entries ) {
94+ entryCount += 1 ;
95+ if ( entryCount > MAX_COMPUTER_USE_PLUGIN_TREE_ENTRIES ) {
96+ throw new Error ( "Codex Computer Use plugin exceeds the bounded tree size" ) ;
97+ }
98+ const entryPath = path . join ( directory , entry . name ) ;
99+ const relativePath = path . join ( relativeDirectory , entry . name ) ;
100+ const entryStat = await fs . lstat ( entryPath , { bigint : true } ) ;
101+ if ( entryStat . isDirectory ( ) ) {
102+ await visit ( entryPath , relativePath , entryStat ) ;
103+ } else {
104+ hash . update ( `entry\0${ relativePath } \0${ await statFingerprint ( entryPath ) } \0` ) ;
105+ }
106+ }
107+ const after = await fs . lstat ( directory , { bigint : true } ) ;
108+ if ( ! sameStat ( before , after ) ) {
109+ throw new Error ( `Codex desktop artifact changed while fingerprinting: ${ directory } ` ) ;
110+ }
111+ } ;
112+ await visit ( root , "." , rootStat ) ;
113+ return hash . digest ( "hex" ) ;
114+ }
115+
86116async function statFingerprint ( filePath : string ) : Promise < string > {
87117 try {
88118 const entry = await fs . lstat ( filePath , { bigint : true } ) ;
0 commit comments