Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gateway/src/official/OfficialRuntimeOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const GIT_LOCAL_PREFILTER_PATTERN =
const GIT_BACKGROUND_TIMEOUT_PATTERN =
/async function (?<functionName>[A-Za-z_$][\w$]*)\((?<cwdName>[A-Za-z_$][\w$]*),(?<argsName>[A-Za-z_$][\w$]*),(?<hostName>[A-Za-z_$][\w$]*),(?<optionsName>[A-Za-z_$][\w$]*)=\{\}\)\{let\{[^}]{0,1400}timeoutMs:(?<timeoutOptionName>[A-Za-z_$][\w$]*)[^}]{0,1400}\}=\k<optionsName>,[^;]{1,2400}?(?<timeoutName>[A-Za-z_$][\w$]*)=Object\.is\(\k<timeoutOptionName>,null\)\?void 0:\k<timeoutOptionName>\?\?\([^;]{1,400}?\),(?<idName>[A-Za-z_$][\w$]*)=crypto\.randomUUID\(\)\.slice\(0,8\),(?<startedName>[A-Za-z_$][\w$]*)=Date\.now\(\),(?<deadlineName>[A-Za-z_$][\w$]*)=\k<timeoutName>==null\?void 0:\k<startedName>\+\k<timeoutName>,(?<contextName>[A-Za-z_$][\w$]*)=(?<contextFactoryName>[A-Za-z_$][\w$]*)\(\),(?<metadataKeyName>[A-Za-z_$][\w$]*)=[^;]{1,1200}?\k<contextName>\.metadataCommonDir[^;]{1,1200}?;[\s\S]{0,18000}?let (?<timerName>[A-Za-z_$][\w$]*)=\k<timeoutName>==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。
Expand Down
46 changes: 46 additions & 0 deletions gateway/test/official-desktop-compat.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down