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

Function createBashShellProvider

src/utils/shell/bashProvider.ts:59–256  ·  view source on GitHub ↗
(
  shellPath: string,
  options?: { skipSnapshot?: boolean },
)

Source from the content-addressed store, hash-verified

57}
58
59export async function createBashShellProvider(
60 shellPath: string,
61 options?: { skipSnapshot?: boolean },
62): Promise<ShellProvider> {
63 let currentSandboxTmpDir: string | undefined
64 const snapshotPromise: Promise<string | undefined> = options?.skipSnapshot
65 ? Promise.resolve(undefined)
66 : createAndSaveSnapshot(shellPath).catch(error => {
67 logForDebugging(`Failed to create shell snapshot: ${error}`)
68 return undefined
69 })
70 // Track the last resolved snapshot path for use in getSpawnArgs
71 let lastSnapshotFilePath: string | undefined
72
73 return {
74 type: 'bash',
75 shellPath,
76 detached: true,
77
78 async buildExecCommand(
79 command: string,
80 opts: {
81 id: number | string
82 sandboxTmpDir?: string
83 useSandbox: boolean
84 },
85 ): Promise<{ commandString: string; cwdFilePath: string }> {
86 let snapshotFilePath = await snapshotPromise
87 // This access() check is NOT pure TOCTOU — it's the fallback decision
88 // point for getSpawnArgs. When the snapshot disappears mid-session
89 // (tmpdir cleanup), we must clear lastSnapshotFilePath so getSpawnArgs
90 // adds -l and the command gets login-shell init. Without this check,
91 // `source ... || true` silently fails and commands run with NO shell
92 // init (neither snapshot env nor login profile). The `|| true` on source
93 // still guards the race between this check and the spawned shell.
94 if (snapshotFilePath) {
95 try {
96 await access(snapshotFilePath)
97 } catch {
98 logForDebugging(
99 `Snapshot file missing, falling back to login shell: ${snapshotFilePath}`,
100 )
101 snapshotFilePath = undefined
102 }
103 }
104 lastSnapshotFilePath = snapshotFilePath
105
106 // Stash sandboxTmpDir for use in getEnvironmentOverrides
107 currentSandboxTmpDir = opts.sandboxTmpDir
108
109 const tmpdir = osTmpdir()
110 const isWindows = getPlatform() === 'windows'
111 const shellTmpdir = isWindows ? windowsPathToPosixPath(tmpdir) : tmpdir
112
113 // shellCwdFilePath: POSIX path used inside the bash command (pwd -P >| ...)
114 // cwdFilePath: native OS path used by Node.js for readFileSync/unlinkSync
115 // On non-Windows these are identical; on Windows, Git Bash needs POSIX paths
116 // but Node.js needs native Windows paths for file operations.

Callers 1

getShellConfigImplFunction · 0.85

Calls 3

createAndSaveSnapshotFunction · 0.85
resolveMethod · 0.80
logForDebuggingFunction · 0.50

Tested by

no test coverage detected