-
Notifications
You must be signed in to change notification settings - Fork 9
feat(sandbox): port org base-snapshot lookup from open-agents #529
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7fe088a
feat(sandbox): port org base-snapshot lookup from open-agents
sweetmantech 25e4b78
chore(sandbox): log findOrgSnapshot outcomes for production observabi…
sweetmantech 26b4be0
fix(sandbox): set source.prebuilt:true when restoring from org snapshot
sweetmantech 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,36 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { extractOrgRepoName } from "@/lib/recoupable/extractOrgRepoName"; | ||
|
|
||
| describe("extractOrgRepoName", () => { | ||
| it("extracts the repo name from a recoupable org clone URL", () => { | ||
| expect(extractOrgRepoName("https://github.com/recoupable/org-rostrum-pacific-abc123")).toBe( | ||
| "org-rostrum-pacific-abc123", | ||
| ); | ||
| }); | ||
|
|
||
| it("strips a trailing .git suffix", () => { | ||
| expect(extractOrgRepoName("https://github.com/recoupable/myorg.git")).toBe("myorg"); | ||
| }); | ||
|
|
||
| it("strips a trailing slash", () => { | ||
| expect(extractOrgRepoName("https://github.com/recoupable/myorg/")).toBe("myorg"); | ||
| }); | ||
|
|
||
| it("returns null for non-recoupable orgs", () => { | ||
| expect(extractOrgRepoName("https://github.com/someoneelse/repo")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for nested paths beyond the repo segment", () => { | ||
| expect(extractOrgRepoName("https://github.com/recoupable/repo/blob/main/x")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for non-GitHub URLs", () => { | ||
| expect(extractOrgRepoName("https://gitlab.com/recoupable/repo")).toBeNull(); | ||
| expect(extractOrgRepoName("not-a-url")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for the org root with no repo", () => { | ||
| expect(extractOrgRepoName("https://github.com/recoupable/")).toBeNull(); | ||
| expect(extractOrgRepoName("https://github.com/recoupable")).toBeNull(); | ||
| }); | ||
| }); |
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,19 @@ | ||
| const ORG_REPO_URL_PATTERN = /^https:\/\/github\.com\/recoupable\/([^/]+?)(?:\.git)?\/?$/; | ||
|
|
||
| /** | ||
| * Extracts the repo name from a Recoupable org clone URL. The repo | ||
| * name is used as a `sandboxName` to look up per-org base snapshots | ||
| * built by the build-org-snapshot workflow — finding one warm-boots | ||
| * the sandbox in seconds instead of paying the ~75s full-clone path. | ||
| * | ||
| * Example: `https://github.com/recoupable/org-rostrum-pacific-<uuid>` | ||
| * → `org-rostrum-pacific-<uuid>` | ||
| * | ||
| * @param cloneUrl - The repo URL the caller wants to clone. | ||
| * @returns The repo name when the URL is under the recoupable org, | ||
| * otherwise null. Non-recoupable repos skip the snapshot lookup. | ||
| */ | ||
| export function extractOrgRepoName(cloneUrl: string): string | null { | ||
| const match = cloneUrl.match(ORG_REPO_URL_PATTERN); | ||
| return match?.[1] ?? null; | ||
| } |
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,55 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { findOrgSnapshot } from "@/lib/sandbox/findOrgSnapshot"; | ||
| import { Snapshot } from "@vercel/sandbox"; | ||
|
|
||
| vi.mock("@vercel/sandbox", () => ({ | ||
| Snapshot: { list: vi.fn() }, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("findOrgSnapshot", () => { | ||
| it("returns the id of the most recent created snapshot", async () => { | ||
| vi.mocked(Snapshot.list).mockResolvedValue({ | ||
| snapshots: [ | ||
| { id: "snap_A", status: "creating" }, | ||
| { id: "snap_B", status: "created" }, | ||
| { id: "snap_C", status: "created" }, | ||
| ], | ||
| } as never); | ||
|
|
||
| const id = await findOrgSnapshot("org-x"); | ||
| // Returns the FIRST created snapshot in the list (sortOrder desc means first = newest). | ||
| expect(id).toBe("snap_B"); | ||
| }); | ||
|
|
||
| it("calls Snapshot.list with the supplied name and a desc sort order", async () => { | ||
| vi.mocked(Snapshot.list).mockResolvedValue({ snapshots: [] } as never); | ||
|
|
||
| await findOrgSnapshot("org-y"); | ||
|
|
||
| expect(Snapshot.list).toHaveBeenCalledWith( | ||
| expect.objectContaining({ name: "org-y", sortOrder: "desc" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("returns null when no snapshots are in the 'created' state", async () => { | ||
| vi.mocked(Snapshot.list).mockResolvedValue({ | ||
| snapshots: [{ id: "snap_pending", status: "creating" }], | ||
| } as never); | ||
|
|
||
| expect(await findOrgSnapshot("org-z")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when the API returns no snapshots", async () => { | ||
| vi.mocked(Snapshot.list).mockResolvedValue({ snapshots: [] } as never); | ||
| expect(await findOrgSnapshot("org-empty")).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when Snapshot.list throws", async () => { | ||
| vi.mocked(Snapshot.list).mockRejectedValue(new Error("vercel api down")); | ||
| expect(await findOrgSnapshot("org-err")).toBeNull(); | ||
| }); | ||
| }); |
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,32 @@ | ||
| import { Snapshot } from "@vercel/sandbox"; | ||
|
|
||
| /** | ||
| * Looks up the most recent ready snapshot whose source sandbox name | ||
| * matches `sandboxName`. Used by `POST /api/sandbox` to warm-boot | ||
| * recoupable org sessions from a per-org base snapshot when one | ||
| * exists, instead of paying the full-clone cold-start path. | ||
| * | ||
| * Returns the snapshot id, or `null` when no `created` snapshot | ||
| * exists yet or when the upstream call throws — callers always have | ||
| * a safe fallback to default sandbox provisioning. | ||
| * | ||
| * @param sandboxName - The org repo name returned by `extractOrgRepoName`. | ||
| * @returns The snapshot id, or null on miss / error. | ||
| */ | ||
| export async function findOrgSnapshot(sandboxName: string): Promise<string | null> { | ||
| try { | ||
| const result = await Snapshot.list({ | ||
| name: sandboxName, | ||
| sortOrder: "desc", | ||
| limit: 5, | ||
| }); | ||
| const ready = result.snapshots.find(s => s.status === "created"); | ||
| console.log( | ||
| `[findOrgSnapshot] '${sandboxName}' → ${ready ? `hit ${ready.id}` : "miss"} (${result.snapshots.length} total snapshots returned)`, | ||
| ); | ||
| return ready?.id ?? null; | ||
| } catch (error) { | ||
| console.error(`[findOrgSnapshot] failed to list snapshots for '${sandboxName}':`, error); | ||
| return null; | ||
| } | ||
| } | ||
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.
P2: Limiting the list to 5 can cause false misses when a valid
createdsnapshot exists just outside that window.Prompt for AI agents