MCPcopy Create free account
hub / github.com/Noumena-Network/code / initialize

Function initialize

src/utils/sandbox/sandbox-adapter.ts:745–807  ·  view source on GitHub ↗

* Initialize sandbox with log monitoring enabled by default

(
  sandboxAskCallback?: SandboxAskCallback,
)

Source from the content-addressed store, hash-verified

743 * Initialize sandbox with log monitoring enabled by default
744 */
745async function initialize(
746 sandboxAskCallback?: SandboxAskCallback,
747): Promise<void> {
748 // If already initializing or initialized, return the promise
749 if (initializationPromise) {
750 return initializationPromise
751 }
752
753 // Check if sandboxing is enabled in settings
754 if (!isSandboxingEnabled()) {
755 return
756 }
757
758 // Wrap the callback to enforce allowManagedDomainsOnly policy.
759 // This ensures all code paths (REPL, print/SDK) are covered.
760 const wrappedCallback: SandboxAskCallback | undefined = sandboxAskCallback
761 ? async (hostPattern: NetworkHostPattern) => {
762 if (shouldAllowManagedSandboxDomainsOnly()) {
763 logForDebugging(
764 `[sandbox] Blocked network request to ${hostPattern.host} (allowManagedDomainsOnly)`,
765 )
766 return false
767 }
768 return sandboxAskCallback(hostPattern)
769 }
770 : undefined
771
772 // Create the initialization promise synchronously (before any await) to prevent
773 // race conditions where wrapWithSandbox() is called before the promise is assigned.
774 initializationPromise = (async () => {
775 try {
776 // Resolve worktree main repo path once before building config.
777 // Worktree status doesn't change mid-session, so this is cached for all
778 // subsequent refreshConfig() calls (which must be synchronous to avoid
779 // race conditions where pending requests slip through with stale config).
780 if (worktreeMainRepoPath === undefined) {
781 worktreeMainRepoPath = await detectWorktreeMainRepoPath(getCwdState())
782 }
783
784 const settings = getSettings_DEPRECATED()
785 const runtimeConfig = convertToSandboxRuntimeConfig(settings)
786
787 // Log monitor is automatically enabled for macOS
788 await BaseSandboxManager.initialize(runtimeConfig, wrappedCallback)
789
790 // Subscribe to settings changes to update sandbox config dynamically
791 settingsSubscriptionCleanup = settingsChangeDetector.subscribe(() => {
792 const settings = getSettings_DEPRECATED()
793 const newConfig = convertToSandboxRuntimeConfig(settings)
794 BaseSandboxManager.updateConfig(newConfig)
795 logForDebugging('Sandbox configuration updated from settings change')
796 })
797 } catch (error) {
798 // Clear the promise on error so initialization can be retried
799 initializationPromise = undefined
800
801 // Log error but don't throw - let sandboxing fail gracefully
802 logForDebugging(`Failed to initialize sandbox: ${errorMessage(error)}`)

Callers

nothing calls this directly

Calls 8

isSandboxingEnabledFunction · 0.85
getCwdStateFunction · 0.85
initializeMethod · 0.65
logForDebuggingFunction · 0.50
errorMessageFunction · 0.50

Tested by

no test coverage detected