-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(vscode): bundle local bwrap helper #11657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { randomUUID } from "node:crypto" | ||
| import { | ||
| chmodSync, | ||
| constants, | ||
| copyFileSync, | ||
| lstatSync, | ||
| mkdirSync, | ||
| realpathSync, | ||
| renameSync, | ||
| rmSync, | ||
| statSync, | ||
| writeFileSync, | ||
| } from "node:fs" | ||
| import { dirname } from "node:path" | ||
| import { localBwrapDigest, localBwrapPath, validLocalBwrap } from "../src/services/cli-backend/cli-resources" | ||
|
|
||
| export function currentBwrapTarget(): string { | ||
| const os = process.platform === "win32" ? "win32" : process.platform | ||
| return `${os}-${process.arch}` | ||
| } | ||
|
|
||
| function arch(target: string): "x64" | "arm64" { | ||
| if (target === "linux-x64") return "x64" | ||
| if (target === "linux-arm64") return "arm64" | ||
| throw new Error(`No Bubblewrap helper configured for target ${target}`) | ||
| } | ||
|
|
||
| function source() { | ||
| const configured = process.env.KILO_BWRAP_PATH | ||
| if (configured) return realpathSync(configured) | ||
|
|
||
| const found = Bun.which("bwrap") | ||
| if (!found) return | ||
| const target = realpathSync(found) | ||
| const entry = statSync(target) | ||
| const uid = process.getuid?.() | ||
| const groups = process.getgroups?.() ?? [] | ||
| const writable = | ||
| (entry.mode & 0o002) !== 0 || | ||
| (uid !== undefined && entry.uid === uid && (entry.mode & 0o200) !== 0) || | ||
| (groups.includes(entry.gid) && (entry.mode & 0o020) !== 0) | ||
| if (writable) { | ||
| throw new Error(`Refusing writable Bubblewrap executable at ${target}; set KILO_BWRAP_PATH to trust it explicitly`) | ||
| } | ||
| return target | ||
| } | ||
|
|
||
| function secure(dir: string) { | ||
| mkdirSync(dir, { recursive: true, mode: 0o700 }) | ||
| const entry = lstatSync(dir) | ||
| if ( | ||
| entry.isSymbolicLink() || | ||
| !entry.isDirectory() || | ||
| entry.uid !== process.getuid?.() || | ||
| (entry.mode & 0o077) !== 0 | ||
| ) { | ||
| throw new Error(`Bubblewrap cache directory is not private: ${dir}`) | ||
| } | ||
| } | ||
|
|
||
| function stage(source: string, dest: string, digest: string) { | ||
| const root = dirname(dirname(dest)) | ||
| const dir = dirname(dest) | ||
| secure(root) | ||
| secure(dir) | ||
|
|
||
| const token = `${process.pid}-${randomUUID()}` | ||
| const executable = `${dest}.${token}.tmp` | ||
| const checksum = `${executable}.sha256` | ||
| try { | ||
| copyFileSync(source, executable, constants.COPYFILE_EXCL) | ||
| chmodSync(executable, 0o755) | ||
| writeFileSync(checksum, `${digest}\n`, { flag: "wx", mode: 0o600 }) | ||
| renameSync(executable, dest) | ||
| renameSync(checksum, `${dest}.sha256`) | ||
| } finally { | ||
| rmSync(executable, { force: true }) | ||
| rmSync(checksum, { force: true }) | ||
| } | ||
|
|
||
| if (!validLocalBwrap(dest)) throw new Error(`Could not validate staged Bubblewrap executable at ${dest}`) | ||
| } | ||
|
|
||
| export async function ensureBwrapForTarget(target: string, root?: string): Promise<string | undefined> { | ||
| const dest = localBwrapPath(target, root) | ||
| if (!dest) return | ||
|
|
||
| const executable = source() | ||
| if (executable) { | ||
| const digest = localBwrapDigest(executable) | ||
| if (validLocalBwrap(dest) && localBwrapDigest(dest) === digest) return dest | ||
| stage(executable, dest, digest) | ||
| return dest | ||
| } | ||
| if (validLocalBwrap(dest)) return dest | ||
|
|
||
| const { buildBubblewrap } = await import("../../opencode/script/kilocode/bubblewrap") | ||
| const built = await buildBubblewrap(arch(target)) | ||
| stage(built.executable, dest, built.digest) | ||
| return dest | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING:
.cli-versionwill mark packaged installs as local tooprepare:cli-binaryalso runs for thepackagescript, and.vscodeignoreincludesbin/**, so the marker written bylocal-bin.tsis shipped in the VSIX as well. That makes this branch true outside F5/dev sessions, so production installs can start injectingKILO_BWRAP_PATHfrom~/.cache/kilo-vscode/bwrapwhenever the bundled helper looks incomplete, which is broader than the local-only fallback this change is trying to add.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.