Skip to content
Merged
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
21 changes: 20 additions & 1 deletion src/lib/agent-sdk-assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
import { gzipSync } from 'node:zlib';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { AGENT_SDK_TARGET, AGENT_SDK_VERSION } from '../generated/agent-sdk-manifest.js';
import { downloadTarball, ensureClaudeCodeExecutable, extractTarEntry } from './agent-sdk-assets.js';
import { downloadTarball, ensureClaudeCodeExecutable, extractTarEntry, isBunVirtualFsUrl } from './agent-sdk-assets.js';

function tarHeader(name: string, size: number): Buffer {
const header = Buffer.alloc(512);
Expand All @@ -27,6 +27,25 @@ function makeTarball(entries: Array<[string, Buffer]>): Buffer {
return gzipSync(Buffer.concat(parts));
}

describe('isBunVirtualFsUrl', () => {
it('detects the POSIX compiled-binary virtual filesystem', () => {
expect(isBunVirtualFsUrl('file:///$bunfs/root/workos')).toBe(true);
});

it('detects the Windows virtual filesystem with the tilde percent-encoded', () => {
expect(isBunVirtualFsUrl('file:///B:/%7EBUN/root/workos-windows-x64.exe')).toBe(true);
});

it('detects a raw Windows virtual path', () => {
expect(isBunVirtualFsUrl('B:\\~BUN\\root\\workos-windows-x64.exe')).toBe(true);
});

it('rejects source-mode module URLs', () => {
expect(isBunVirtualFsUrl(import.meta.url)).toBe(false);
expect(isBunVirtualFsUrl('file:///home/dev/cli/src/lib/agent-sdk-assets.ts')).toBe(false);
});
});

describe('ensureClaudeCodeExecutable', () => {
it('resolves the current platform executable from node_modules in source mode', async () => {
expect(AGENT_SDK_TARGET).toBe(`${process.platform}-${process.arch}`);
Expand Down
12 changes: 11 additions & 1 deletion src/lib/agent-sdk-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ function runtimeTarget(): string {
return `${process.platform}-${process.arch}${isMuslRuntime() ? '-musl' : ''}`;
}

/**
* Whether a module URL points into a Bun compiled binary's virtual filesystem:
* `/$bunfs/` on POSIX, `B:\~BUN\` on Windows — which import.meta.url surfaces
* with the tilde percent-encoded (`file:///B:/%7EBUN/root/…`). Exported for
* tests.
*/
export function isBunVirtualFsUrl(url: string): boolean {
return url.includes('$bunfs') || url.includes('~BUN') || url.includes('%7EBUN');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unbounded virtual-filesystem marker match

The new check accepts %7EBUN anywhere in a source-mode module URL, so an ordinary path containing a ~BUN component is classified as compiled and unnecessarily enters the download-and-cache flow instead of resolving the installed node_modules asset. Match the documented virtual-filesystem segment, such as %7EBUN/root/, to avoid breaking offline use from such paths.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/lib/agent-sdk-assets.ts
Line: 64

Comment:
**Unbounded virtual-filesystem marker match**

The new check accepts `%7EBUN` anywhere in a source-mode module URL, so an ordinary path containing a `~BUN` component is classified as compiled and unnecessarily enters the download-and-cache flow instead of resolving the installed node_modules asset. Match the documented virtual-filesystem segment, such as `%7EBUN/root/`, to avoid breaking offline use from such paths.

How can I resolve this? If you propose a fix, please make it concise.

}

/** Running from a compiled binary: the module graph lives in Bun's virtual filesystem. */
function isCompiledBinary(): boolean {
return import.meta.url.includes('$bunfs') || import.meta.url.includes('~BUN');
return isBunVirtualFsUrl(import.meta.url);
}

function agentSdkCacheRoot(): string {
Expand Down
Loading