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
2727import 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.
3333const 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``. */
98102function 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}
145149export 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. */
156161export 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.) */
203208export 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 {
211224interface 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 )
0 commit comments