-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[STG-1692] feat: resolve context labels in browse --context-id #1928
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
Open
shrey150
wants to merge
3
commits into
main
Choose a base branch
from
shrey/browse-context-labels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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,5 @@ | ||
| --- | ||
| "@browserbasehq/browse-cli": patch | ||
| --- | ||
|
|
||
| Add context label resolution for --context-id flag |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as fs from "node:fs/promises"; | ||
| import * as path from "node:path"; | ||
| import * as os from "node:os"; | ||
|
|
||
| /** | ||
| * Resolve a --context-id value that may be a named label. | ||
| * | ||
| * Label files live at: | ||
| * <configDir>/contexts/<label> → raw UUID | ||
| * | ||
| * The base path respects BROWSERBASE_CONFIG_DIR env var. | ||
| */ | ||
| export async function resolveContextLabel(value: string): Promise<string> { | ||
| // Raw UUIDs and ctx_ prefixes pass through | ||
| if (/^[0-9a-f-]{36}$/i.test(value) || value.startsWith("ctx_")) { | ||
| return value; | ||
| } | ||
| // Look up as a label file | ||
| const configDir = | ||
| process.env.BROWSERBASE_CONFIG_DIR || | ||
| path.join(os.homedir(), ".config", "browserbase"); | ||
| const labelPath = path.join(configDir, "contexts", value); | ||
| try { | ||
| const id = (await fs.readFile(labelPath, "utf-8")).trim(); | ||
| if (id) { | ||
| return id; | ||
| } | ||
| } catch { | ||
| // Label file doesn't exist – fall through | ||
| } | ||
| // Return as-is (might be a raw ID in a format we don't recognize) | ||
| return value; | ||
| } | ||
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,63 @@ | ||
| import { describe, it, expect, beforeAll, afterAll } from "vitest"; | ||
| import * as fs from "node:fs"; | ||
| import * as path from "node:path"; | ||
| import * as os from "node:os"; | ||
| import { resolveContextLabel } from "../src/resolve-context"; | ||
|
|
||
| let tmpDir: string; | ||
| let contextsDir: string; | ||
| const originalEnv = process.env.BROWSERBASE_CONFIG_DIR; | ||
|
|
||
| beforeAll(() => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "browse-ctx-test-")); | ||
| contextsDir = path.join(tmpDir, "contexts"); | ||
| fs.mkdirSync(contextsDir, { recursive: true }); | ||
| process.env.BROWSERBASE_CONFIG_DIR = tmpDir; | ||
|
|
||
| // Write test label files | ||
| fs.writeFileSync( | ||
| path.join(contextsDir, "latest"), | ||
| "a1b2c3d4-e5f6-7890-abcd-ef1234567890", | ||
| ); | ||
| fs.writeFileSync( | ||
| path.join(contextsDir, "work"), | ||
| "11111111-2222-3333-4444-555555555555\n", | ||
| ); | ||
| fs.writeFileSync(path.join(contextsDir, "empty"), ""); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env.BROWSERBASE_CONFIG_DIR = originalEnv; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Restoring the env var by assigning Prompt for AI agents |
||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("resolveContextLabel", () => { | ||
| it("passes through raw UUIDs unchanged", async () => { | ||
| const uuid = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"; | ||
| expect(await resolveContextLabel(uuid)).toBe(uuid); | ||
| }); | ||
|
|
||
| it("passes through ctx_ prefixed IDs unchanged", async () => { | ||
| expect(await resolveContextLabel("ctx_abc123")).toBe("ctx_abc123"); | ||
| }); | ||
|
|
||
| it("resolves a label to its context ID", async () => { | ||
| expect(await resolveContextLabel("latest")).toBe( | ||
| "a1b2c3d4-e5f6-7890-abcd-ef1234567890", | ||
| ); | ||
| }); | ||
|
|
||
| it("trims whitespace from label file contents", async () => { | ||
| expect(await resolveContextLabel("work")).toBe( | ||
| "11111111-2222-3333-4444-555555555555", | ||
| ); | ||
| }); | ||
|
|
||
| it("returns the value as-is when label file does not exist", async () => { | ||
| expect(await resolveContextLabel("nonexistent")).toBe("nonexistent"); | ||
| }); | ||
|
|
||
| it("returns the value as-is when label file is empty", async () => { | ||
| expect(await resolveContextLabel("empty")).toBe("empty"); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P1: Unvalidated
--context-idlabel input allows path traversal outside thecontextsdirectory.Prompt for AI agents