function instrumentDynamicValueAtPath(pathParts, sourceRef, targetRef) {
  const enumerable = Reflect.getOwnPropertyDescriptor(
    sourceRef,
    pathParts[0]
  )?.enumerable
  const dynamicGetterDesc = {
    get: () => {
      const dynamicValue = sourceRef[pathParts[0]]
      let leaf = dynamicValue,
        parent = sourceRef

      for (let i = 1; i < pathParts.length; i++) {
        parent = leaf
        leaf = leaf[pathParts[i]]
      }
      if (typeof leaf === 'function') {
        leaf = leaf.bind(parent) // TODO: consider the risks, should not differ from unwrapping
      }
      return leaf
    },
    writeable: false,
    enumerable, // Initial value will have to suffice. Change will not propagate dynamically.
    configurable: false,
  }
  let currentTarget = targetRef
  let currentPath = ''
  for (let depth = 0; depth < pathParts.length - 1; depth++) {
    currentPath = extendPath(currentPath, pathParts[depth])
    const nextPart = pathParts[depth]
    if (Reflect.getOwnPropertyDescriptor(currentTarget, nextPart)?.get) {
      // We could silently ignore this, but it could introduce a false sense of security in the policy file
      throw Error(
        `LavaMoat - "${pathParts[0]}" is writeable elsewhere and both "${currentPath}" and "${pathParts.join('.')}" are allowed for one package. One of these entries is redundant.`
      )
    }
    if (typeof currentTarget[nextPart] !== 'object') {
      currentTarget[nextPart] = {}
    }
    currentTarget = currentTarget[nextPart]
  }

  const lastPart = pathParts[pathParts.length - 1]
  Reflect.defineProperty(currentTarget, lastPart, dynamicGetterDesc)
}
Puts a getter at the end of the path that returns the nested values from a
top-level field that might change at runtime.

@param {string[]} pathParts
@param {Record<string, any>} sourceRef
@param {Record<string, any>} targetRef