Skip to content

Commit 12006bf

Browse files
feat: open workspace pages in the SaaS app's /workspaces mount (#1360)
Workspace URLs the CLI builds (hand-off page, success bounce, cancel landing, manage links) now target `<tenant>.app.myaltimate.com/workspaces` instead of `<tenant>.ws.myaltimate.com`, through one join that keeps the base path. Closes #1359
1 parent b308fa2 commit 12006bf

3 files changed

Lines changed: 111 additions & 33 deletions

File tree

‎packages/opencode/src/altimate/workspace/browser-handoff.ts‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// altimate_change - new file
22
//
33
// Browser-based workspace creation handoff. CLI opens Ralph's SaaS approval
4-
// modal at ``<tenant>.ws.myaltimate.com/create-and-link`` with the current
5-
// project's context, user approves, the SaaS creates a workspace and delivers
6-
// its ID back to the CLI via a loopback callback. The CLI then binds the
7-
// current project to that workspace via the existing
4+
// modal at ``<tenant>.app.myaltimate.com/workspaces/create-and-link`` with the
5+
// current project's context, user approves, the SaaS creates a workspace and
6+
// delivers its ID back to the CLI via a loopback callback. The CLI then binds
7+
// the current project to that workspace via the existing
88
// ``POST /datamate-project-bindings/bind`` endpoint.
99
//
1010
// This module deliberately DUPLICATES the loopback listener pattern from
@@ -26,12 +26,16 @@ import { Log } from "@/altimate/util/log"
2626

2727
import type { ProjectIdentifier } from "./api-client"
2828

29-
// Freemium is the only deployment served by the workspace stack today. When
29+
// Freemium is the only deployment the CLI hands off to today. When
3030
// altimate-backend goes multi-deployment (enterprise), extend this to a small
3131
// mapping. Returning null means "not supported here" — the CLI hides the
3232
// browser-handoff option entirely rather than open a broken URL.
3333
const FREEMIUM_API_HOST = "api.myaltimate.com"
34-
const FREEMIUM_WORKSPACE_HOST = "ws.myaltimate.com"
34+
const FREEMIUM_APP_HOST = "app.myaltimate.com"
35+
// The SaaS app serves the workspace pages under this path. Every workspace
36+
// URL the CLI builds (hand-off page, manage page, cancel landing) is joined
37+
// onto the base under it.
38+
const WORKSPACES_PATH = "/workspaces"
3539

3640
/** DNS-label-shaped tenant guard for the freemium subdomain. Credentials
3741
* only require ``altimateInstanceName`` to be a non-empty string, so a tenant
@@ -96,7 +100,7 @@ function htmlError(msg: string): string {
96100
* so the user doesn't get stranded on the plain loopback page. Same top-level
97101
* navigation mechanism as ``deliverySuccessHtml``. */
98102
function cancelHtml(workspaceWebBase: URL): string {
99-
const home = workspaceWebBase.toString().replace(/\/$/, "") + "/"
103+
const home = joinWorkspacePath(workspaceWebBase, "/")
100104
const safe = escapeHtml(home)
101105
return `<!doctype html><meta charset="utf-8"><title>Altimate Code</title>
102106
<meta http-equiv="refresh" content="0;url=${safe}">
@@ -144,15 +148,16 @@ export interface HandoffFailure {
144148
}
145149
export type HandoffResult = HandoffSuccess | HandoffFailure
146150

147-
/** Compute the workspace-stack URL for a given API host + tenant, or null if
148-
* this deployment isn't supported (localhost, enterprise, custom domain).
151+
/** Compute the base URL of the tenant's workspace pages (the SaaS app's
152+
* ``/workspaces`` mount) for a given API host + tenant, or null if this
153+
* deployment isn't supported (localhost, enterprise, custom domain).
149154
*
150155
* Dev escape hatch: ``ALTIMATE_WORKSPACE_WEB_URL`` overrides the tenant map
151156
* lookup when set. The override is DEV-ONLY — it returns the URL as-is
152-
* without tenant scoping (which is what a local ``altimate2.localhost:3003``
153-
* dev server needs). Production callers must not set it; if it is somehow
154-
* present and points off-tenant, the CSRF ``state`` still gates the callback
155-
* so no cross-workspace bind is possible. */
157+
* without tenant scoping, so it names the mount itself (a local dev server's
158+
* ``http://altimate2.localhost:3003/workspaces``). Production callers must not
159+
* set it; if it is somehow present and points off-tenant, the CSRF ``state``
160+
* still gates the callback so no cross-workspace bind is possible. */
156161
export function resolveWorkspaceWebUrl(altimateUrl: string, tenant: string): URL | null {
157162
const override = process.env["ALTIMATE_WORKSPACE_WEB_URL"]
158163
if (override) {
@@ -176,8 +181,8 @@ export function resolveWorkspaceWebUrl(altimateUrl: string, tenant: string): URL
176181
// refuse rather than emit a URL that points off-domain.
177182
if (!TENANT_LABEL_RE.test(tenant)) return null
178183
const lower = tenant.toLowerCase()
179-
const u = new URL(`https://${lower}.${FREEMIUM_WORKSPACE_HOST}`)
180-
if (u.hostname !== `${lower}.${FREEMIUM_WORKSPACE_HOST}`) return null
184+
const u = new URL(`https://${lower}.${FREEMIUM_APP_HOST}${WORKSPACES_PATH}`)
185+
if (u.hostname !== `${lower}.${FREEMIUM_APP_HOST}`) return null
181186
return u
182187
} catch {
183188
return null
@@ -201,8 +206,16 @@ export function resolveWorkspaceWebUrl(altimateUrl: string, tenant: string): URL
201206
* — one bug (missing this exact fix) needed three separate edits to close.
202207
* (multi-model review, PR #1274 round 7.) */
203208
export function buildManageUrl(base: URL, workspaceId: number): string {
209+
return joinWorkspacePath(base, `/w/${workspaceId}`)
210+
}
211+
212+
/** Join a root-relative workspace page path onto ``base``'s pathname, keeping
213+
* the base's own path (the ``/workspaces`` mount, or an override's path) —
214+
* ``new URL("/x", base)`` would replace it. The one join behind every
215+
* workspace URL this module builds. */
216+
function joinWorkspacePath(base: URL, path: string): string {
204217
const u = new URL(base)
205-
u.pathname = `${u.pathname.replace(/\/+$/, "")}/w/${workspaceId}`
218+
u.pathname = `${u.pathname.replace(/\/+$/, "")}${path}`
206219
u.search = ""
207220
u.hash = ""
208221
return u.toString()
@@ -211,7 +224,7 @@ export function buildManageUrl(base: URL, workspaceId: number): string {
211224
interface HandoffPending {
212225
state: string
213226
expectedTenant: string
214-
/** Base URL for the tenant's SaaS workspace stack, used to build the
227+
/** Base URL for the tenant's SaaS workspace pages, used to build the
215228
* ``/w/:id`` bounce target that the loopback success HTML redirects to. */
216229
workspaceWebBase: URL
217230
resolve: (v: HandoffSuccess) => void
@@ -326,7 +339,7 @@ async function startListener(pending: HandoffPending): Promise<{ server: Server;
326339
// Bounce the browser back to the SaaS workspace page. Loopback constructs
327340
// the URL itself (no need to trust a `return` query param) — the base is
328341
// deterministic from the tenant we already validated above.
329-
const manageUrl = `${pending.workspaceWebBase.toString().replace(/\/$/, "")}/w/${workspaceId}`
342+
const manageUrl = buildManageUrl(pending.workspaceWebBase, workspaceId)
330343
respond(200, deliverySuccessHtml(manageUrl))
331344
// Callback validated — but the SUCCESS payload carries the credentials
332345
// snapshot the handoff was started against; the caller re-verifies
@@ -549,7 +562,7 @@ export async function runHandoffWithOpener(
549562
})
550563

551564
const redirect = `http://127.0.0.1:${boundPort}/workspace-bound`
552-
const target = new URL("/create-and-link", webUrl)
565+
const target = new URL(joinWorkspacePath(webUrl, "/create-and-link"))
553566
target.searchParams.set("client", "altimate-code")
554567
target.searchParams.set("redirect", redirect)
555568
target.searchParams.set("state", state)

‎packages/opencode/test/altimate/workspace/browser-handoff.test.ts‎

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ function parseHandoffUrl(url: string): { port: number; state: string; redirect:
5252
return { port, state, redirect }
5353
}
5454

55-
async function fireCallback(redirect: string, params: Record<string, string>): Promise<void> {
55+
async function fireCallback(redirect: string, params: Record<string, string>): Promise<string> {
5656
const target = new URL(redirect)
5757
for (const [k, v] of Object.entries(params)) target.searchParams.set(k, v)
5858
const res = await fetch(target.toString(), { method: "GET" })
5959
// Drain body so the connection can close and let the CLI's `close()`
60-
// proceed without hanging on lingering sockets.
61-
await res.text().catch(() => "")
60+
// proceed without hanging on lingering sockets. Returned for the tests that
61+
// read where the loopback page sends the browser next — the flow settles on
62+
// the request, before the body is read, so await the call itself.
63+
return res.text().catch(() => "")
64+
}
65+
66+
/** The URL a loopback response page navigates the browser to. */
67+
function bounceTarget(html: string): string | undefined {
68+
return html.match(/<meta http-equiv="refresh" content="0;url=([^"]+)">/)?.[1]
6269
}
6370

6471
/** Send a raw HTTP/1.1 request with an attacker-controlled ``Host`` header —
@@ -106,10 +113,10 @@ function isolateWebUrlOverride() {
106113

107114
describe("resolveWorkspaceWebUrl", () => {
108115
isolateWebUrlOverride()
109-
test("freemium API host resolves to <tenant>.ws.myaltimate.com", () => {
116+
test("freemium API host resolves to the SaaS app's /workspaces mount", () => {
110117
const url = resolveWorkspaceWebUrl("https://api.myaltimate.com", "acme")
111118
expect(url).not.toBeNull()
112-
expect(url!.toString()).toBe("https://acme.ws.myaltimate.com/")
119+
expect(url!.toString()).toBe("https://acme.app.myaltimate.com/workspaces")
113120
})
114121

115122
test("localhost API returns null (browser flow not supported in dev)", () => {
@@ -134,8 +141,8 @@ describe("resolveWorkspaceWebUrl", () => {
134141
// one bug (a naive string-concat URL join) needed three separate fixes to close.
135142
describe("buildManageUrl", () => {
136143
test("appends /w/<id> to a bare origin", () => {
137-
expect(buildManageUrl(new URL("https://tenant.ws.myaltimate.com"), 4242)).toBe(
138-
"https://tenant.ws.myaltimate.com/w/4242",
144+
expect(buildManageUrl(new URL("https://tenant.app.myaltimate.com"), 4242)).toBe(
145+
"https://tenant.app.myaltimate.com/w/4242",
139146
)
140147
})
141148

@@ -150,6 +157,14 @@ describe("buildManageUrl", () => {
150157
test("normalizes a trailing slash on the base path", () => {
151158
expect(buildManageUrl(new URL("https://host/base/"), 7)).toBe("https://host/base/w/7")
152159
})
160+
161+
describe("on a resolved base", () => {
162+
isolateWebUrlOverride()
163+
test("keeps the /workspaces mount", () => {
164+
const base = resolveWorkspaceWebUrl("https://api.myaltimate.com", "acme")!
165+
expect(buildManageUrl(base, 4242)).toBe("https://acme.app.myaltimate.com/workspaces/w/4242")
166+
})
167+
})
153168
})
154169

155170
// ─────────────────────────────────────────────────────────────────────────────
@@ -359,7 +374,57 @@ describe("runHandoffWithOpener end-to-end", () => {
359374
expect(frag.get("project_name")).toBe("foo")
360375
expect(frag.get("project_remote")).toBe("[email protected]:acme/foo.git")
361376
expect(frag.get("project_path")).toBe("/w/foo")
362-
expect(u.pathname).toBe("/create-and-link")
377+
expect(u.origin).toBe("https://acme.app.myaltimate.com")
378+
expect(u.pathname).toBe("/workspaces/create-and-link")
379+
})
380+
381+
test("success page sends the browser to the workspace's page under /workspaces", async () => {
382+
let page: Promise<string> | undefined
383+
const result = await runHandoffWithOpener(
384+
{ identifier: { projectPath: "/x" }, projectName: "x" },
385+
async (url) => {
386+
const { state, redirect } = parseHandoffUrl(url)
387+
page = fireCallback(redirect, { workspace_id: "42", state, tenant: "acme" })
388+
await page
389+
},
390+
)
391+
expect(result.ok).toBe(true)
392+
expect(bounceTarget(await page!)).toBe("https://acme.app.myaltimate.com/workspaces/w/42")
393+
})
394+
395+
test("cancel page sends the browser to the workspaces list", async () => {
396+
let page: Promise<string> | undefined
397+
const result = await runHandoffWithOpener(
398+
{ identifier: { projectPath: "/x" }, projectName: "x" },
399+
async (url) => {
400+
const { state, redirect } = parseHandoffUrl(url)
401+
page = fireCallback(redirect, { state, error: "cancelled", tenant: "acme" })
402+
await page
403+
},
404+
)
405+
expect(result.ok).toBe(false)
406+
expect(bounceTarget(await page!)).toBe("https://acme.app.myaltimate.com/workspaces/")
407+
})
408+
409+
// The dev override names a local server's mount; its path must survive into
410+
// every URL, which ``new URL("/create-and-link", base)`` would have dropped.
411+
test("the dev override's path prefixes the hand-off page and the manage page", async () => {
412+
process.env["ALTIMATE_WORKSPACE_WEB_URL"] = "http://acme.localhost:3000/workspaces"
413+
let authorizeUrl = ""
414+
let page: Promise<string> | undefined
415+
const result = await runHandoffWithOpener(
416+
{ identifier: { projectPath: "/x" }, projectName: "x" },
417+
async (url) => {
418+
authorizeUrl = url
419+
const { state, redirect } = parseHandoffUrl(url)
420+
page = fireCallback(redirect, { workspace_id: "7", state, tenant: "acme" })
421+
await page
422+
},
423+
)
424+
expect(result.ok).toBe(true)
425+
const u = new URL(authorizeUrl)
426+
expect(u.origin + u.pathname).toBe("http://acme.localhost:3000/workspaces/create-and-link")
427+
expect(bounceTarget(await page!)).toBe("http://acme.localhost:3000/workspaces/w/7")
363428
})
364429
})
365430

‎packages/opencode/test/cli/cmd/link.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,33 +195,33 @@ describe("hyperlink", () => {
195195
// raw OSC 8 would land as literal junk in the captured output.
196196
setTTY(false)
197197
process.env.TERM_PROGRAM = "iTerm.app"
198-
const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242")
198+
const out = hyperlink("anas-skill-test", "https://tenant.app.myaltimate.com/workspaces/w/4242")
199199
expect(out).toBe("anas-skill-test")
200200
expect(out).not.toContain("\x1b")
201201
})
202202

203203
test("wraps text in OSC 8 with no underline on a TTY whose terminal isn't recognized", () => {
204204
setTTY(true)
205205
delete process.env.TERM_PROGRAM
206-
const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242")
207-
expect(out).toBe("\x1b]8;;https://tenant.ws.myaltimate.com/w/4242\x1b\\anas-skill-test\x1b]8;;\x1b\\")
206+
const out = hyperlink("anas-skill-test", "https://tenant.app.myaltimate.com/workspaces/w/4242")
207+
expect(out).toBe("\x1b]8;;https://tenant.app.myaltimate.com/workspaces/w/4242\x1b\\anas-skill-test\x1b]8;;\x1b\\")
208208
expect(out).not.toContain("\x1b[4m")
209209
})
210210

211211
test("wraps text in OSC 8 plus underline when the terminal is recognized as supporting", () => {
212212
setTTY(true)
213213
process.env.TERM_PROGRAM = "iTerm.app"
214-
const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242")
214+
const out = hyperlink("anas-skill-test", "https://tenant.app.myaltimate.com/workspaces/w/4242")
215215
expect(out).toBe(
216-
"\x1b]8;;https://tenant.ws.myaltimate.com/w/4242\x1b\\\x1b[4manas-skill-test\x1b[24m\x1b]8;;\x1b\\",
216+
"\x1b]8;;https://tenant.app.myaltimate.com/workspaces/w/4242\x1b\\\x1b[4manas-skill-test\x1b[24m\x1b]8;;\x1b\\",
217217
)
218218
})
219219

220220
test("sanitizes an adversarial name so it cannot open a second, spoofed link", () => {
221221
setTTY(true)
222222
delete process.env.TERM_PROGRAM
223223
const malicious = "name\x1b]8;;http://evil.example\x1b\\CLICK ME\x1b]8;;\x1b\\"
224-
const out = hyperlink(malicious, "https://tenant.ws.myaltimate.com/w/4242")
224+
const out = hyperlink(malicious, "https://tenant.app.myaltimate.com/workspaces/w/4242")
225225
// Exactly one real OSC 8 open + one real OSC 8 close — the malicious
226226
// payload's own OSC 8 bytes were stripped, leaving only inert text.
227227
expect(out.split("\x1b]8;;").length - 1).toBe(2)

0 commit comments

Comments
 (0)