diff --git a/gateway/src/official/OfficialRuntimeOptimizer.ts b/gateway/src/official/OfficialRuntimeOptimizer.ts index 2357b8e..79c51dc 100644 --- a/gateway/src/official/OfficialRuntimeOptimizer.ts +++ b/gateway/src/official/OfficialRuntimeOptimizer.ts @@ -47,7 +47,7 @@ const GIT_LOCAL_PREFILTER_PATTERN = const GIT_BACKGROUND_TIMEOUT_PATTERN = /async function (?[A-Za-z_$][\w$]*)\((?[A-Za-z_$][\w$]*),(?[A-Za-z_$][\w$]*),(?[A-Za-z_$][\w$]*),(?[A-Za-z_$][\w$]*)=\{\}\)\{let\{[^}]{0,1400}timeoutMs:(?[A-Za-z_$][\w$]*)[^}]{0,1400}\}=\k,[^;]{1,2400}?(?[A-Za-z_$][\w$]*)=Object\.is\(\k,null\)\?void 0:\k\?\?\([^;]{1,400}?\),(?[A-Za-z_$][\w$]*)=crypto\.randomUUID\(\)\.slice\(0,8\),(?[A-Za-z_$][\w$]*)=Date\.now\(\),(?[A-Za-z_$][\w$]*)=\k==null\?void 0:\k\+\k,(?[A-Za-z_$][\w$]*)=(?[A-Za-z_$][\w$]*)\(\),(?[A-Za-z_$][\w$]*)=[^;]{1,1200}?\k\.metadataCommonDir[^;]{1,1200}?;[\s\S]{0,18000}?let (?[A-Za-z_$][\w$]*)=\k==null\?null:setTimeout/g; const WORKTREE_SHELL_ENVIRONMENT_PATTERN = - /"worktree-shell-environment-config":(async\(\{cwd:([A-Za-z_$][\w$]*),hostId:([A-Za-z_$][\w$]*)\}\)=>\{let [^;]{1,1000};return\{shellEnvironment:[^{}]{1,300}\}\})/g; + /"worktree-shell-environment-config":((?:async\s*)?\(\{[^)]*\}\)=>(?:\{let [^;]{1,1000};return\{shellEnvironment:[^{}]{1,300}\}\}|(?:this\.)?readWorktreeShellEnvironment\([^)]*\)|\{(?:let [^;]{1,1000};)?return\s*(?:this\.)?readWorktreeShellEnvironment\([^)]*\);?\}))/g; function matchCount(source: string, pattern: RegExp): number { // 所有模式都带全局标记;match 返回完整命中列表,不复用可变 lastIndex。 diff --git a/gateway/test/official-desktop-compat.test.cjs b/gateway/test/official-desktop-compat.test.cjs index 0f04861..16932b5 100644 --- a/gateway/test/official-desktop-compat.test.cjs +++ b/gateway/test/official-desktop-compat.test.cjs @@ -967,6 +967,52 @@ test("runtime optimizer coalesces slow worktree shell environment reads in the h assert.equal(fs.readFileSync(mainPath, "utf8"), optimized); }); +test("runtime optimizer coalesces delegated worktree shell environment reads in newer official desktop bundles", async (t) => { + const bundleDir = temporaryDirectory(t); + const mainPath = path.join(bundleDir, ".vite", "build", "main.js"); + writeFile( + mainPath, + 'var services={"worktree-shell-environment-config":({cwd:e,hostId:t})=>this.readWorktreeShellEnvironment(e,t??`local`)};' + ); + const optimizer = new OfficialRuntimeOptimizer({ fileSystem: new OfficialBundleFileSystem() }); + + assert.deepEqual(optimizer.optimize(bundleDir), { + nativePetComposition: "not-present", + nativePetPrewarm: "not-present", + macPushRegistration: "not-present", + patchedFileCount: 1, + unsupportedFiles: [], + worktreeShellEnvironment: "gateway-coalesced", + }); + const optimized = fs.readFileSync(mainPath, "utf8"); + assert.match(optimized, /__opencodexWorktreeShellEnvironmentCache/); + assert.match(optimized, /shellEnvironment:null/); + + class MockService { + constructor() { + this.callCount = 0; + } + async readWorktreeShellEnvironment(cwd, hostId) { + this.callCount += 1; + return { shellEnvironment: { PATH: "/bin", cwd, hostId } }; + } + getHandler(process) { + return new Function( + "process", + `${optimized};return services["worktree-shell-environment-config"]` + ).call(this, process); + } + } + + const service = new MockService(); + const hiddenHandler = service.getHandler({ env: { OPENCODEX_GATEWAY_HIDDEN_RUNTIME: "1" } }); + const first = await hiddenHandler({ cwd: "/workspace", hostId: "local" }); + const second = await hiddenHandler({ cwd: "/workspace", hostId: "local" }); + assert.deepEqual(first, { shellEnvironment: { PATH: "/bin", cwd: "/workspace", hostId: "local" } }); + assert.deepEqual(second, { shellEnvironment: { PATH: "/bin", cwd: "/workspace", hostId: "local" } }); + assert.equal(service.callCount, 1); +}); + test("runtime optimizer reports an unsupported Git discovery layout without partial assumptions", (t) => { const bundleDir = temporaryDirectory(t); const workerPath = path.join(bundleDir, ".vite", "build", "worker-new-layout.js");