@@ -45,6 +45,10 @@ import { FreeTierConsent } from "../altimate/free/consent"
4545import { InstanceStore } from "@/project/instance-store"
4646import { AppRuntime } from "@/effect/app-runtime"
4747// altimate_change end
48+ // altimate_change - `/workspace` Refresh and Sync: pilot flag gate, and the session-directory check
49+ import { Flag as CoreFlag } from "@opencode-ai/core/flag/flag"
50+ import nodePath from "node:path"
51+ import { Session } from "../session"
4852// altimate_change end
4953import { FileRoutes } from "./routes/file"
5054import { ConfigRoutes } from "./routes/config"
@@ -71,6 +75,51 @@ globalThis.AI_SDK_LOG_WARNINGS = false
7175
7276export namespace Server {
7377 const log = Log . create ( { service : "server" } )
78+ // altimate_change start — shared gate for the `/altimate/workspace/*` routes
79+ /** Why a `/workspace` action must not run, or undefined when it may.
80+ *
81+ * 409 is reserved for the pilot gate, so a caller can tell "this server is not in workspace mode"
82+ * apart from a bad request (400) without parsing the message.
83+ *
84+ * Outside the workspace pilot a skill sync purges the snapshot, so the flag is checked first.
85+ * A browser origin on an unsecured server is refused for the same reason as Altimate Base
86+ * registration: a CORS-allowed page is not a local process. Native clients (the extension host,
87+ * curl) send no Origin. With a server password set, a same-origin page may call; others may not. */
88+ export function workspaceRouteRefusal (
89+ origin : string | undefined ,
90+ host : string | undefined ,
91+ password : string | undefined = Flag . OPENCODE_SERVER_PASSWORD ,
92+ ) : { status : 403 | 409 ; body : { ok : false ; error : string } } | undefined {
93+ if ( ! CoreFlag . ALTIMATE_WORKSPACE ) {
94+ return { status : 409 , body : { ok : false , error : "Workspace mode is not enabled for this server." } }
95+ }
96+ if ( ! origin ) return undefined
97+ if ( ! password ) {
98+ log . warn ( "refused browser-originated workspace action on an unsecured server" , { origin } )
99+ return {
100+ status : 403 ,
101+ body : {
102+ ok : false ,
103+ error : "Workspace actions cannot be run from a browser origin on an unsecured server. Set OPENCODE_SERVER_PASSWORD." ,
104+ } ,
105+ }
106+ }
107+ // With a password set, basicAuth has vetted the credentials — but a browser replays cached
108+ // Basic credentials on a cross-site form POST too, so only this server's own pages may call.
109+ if ( ! sameOrigin ( origin , host ) ) {
110+ log . warn ( "refused cross-origin workspace action" , { origin } )
111+ return { status : 403 , body : { ok : false , error : "Workspace actions cannot be run from another origin." } }
112+ }
113+ return undefined
114+ }
115+ export function sameOrigin ( origin : string , host : string | undefined ) : boolean {
116+ try {
117+ return ! ! host && new URL ( origin ) . host === host
118+ } catch {
119+ return false
120+ }
121+ }
122+ // altimate_change end
74123 // altimate_change start — the Base credential every provider cache in this process is known to
75124 // reflect: set after the register route has disposed both registries for it. Unset until then,
76125 // because a cache built before a background registration finished cannot be told apart from one
@@ -949,6 +998,88 @@ export namespace Server {
949998 }
950999 } )
9511000 // altimate_change end
1001+ // altimate_change start — POST /altimate/workspace/{refresh,sync}
1002+ // The `/workspace` menu's Refresh and Sync for the IDE extension, which runs this CLI
1003+ // headless and cannot reach the TUI slash command. Both act on the request's instance
1004+ // directory and return the `Manage` report as is; wording is the caller's job.
1005+ . post ( "/altimate/workspace/refresh" , async ( c ) => {
1006+ const refused = workspaceRouteRefusal ( c . req . header ( "origin" ) , c . req . header ( "host" ) )
1007+ if ( refused ) return c . json ( refused . body , refused . status )
1008+ // An absent or empty body is a session-less refresh; anything else must be well formed.
1009+ // Falling back to "no session" on bad input would silently widen the operation to
1010+ // resetting every session's memory overlay.
1011+ let text : string
1012+ try {
1013+ text = await c . req . text ( )
1014+ } catch ( err ) {
1015+ const error = err instanceof Error ? err . message : String ( err )
1016+ log . error ( "workspace refresh: could not read the request body" , { error } )
1017+ return c . json ( { ok : false , error } , 500 )
1018+ }
1019+ let body : unknown = { }
1020+ if ( text . trim ( ) ) {
1021+ try {
1022+ body = JSON . parse ( text )
1023+ } catch {
1024+ return c . json ( { ok : false , error : "Request body is not valid JSON." } , 400 )
1025+ }
1026+ }
1027+ if ( body === null || typeof body !== "object" || Array . isArray ( body ) ) {
1028+ return c . json ( { ok : false , error : "Request body must be a JSON object." } , 400 )
1029+ }
1030+ const raw = ( body as Record < string , unknown > ) . sessionID
1031+ if ( raw !== undefined && ( typeof raw !== "string" || ! raw ) ) {
1032+ return c . json ( { ok : false , error : "sessionID must be a non-empty string." } , 400 )
1033+ }
1034+ const sessionID = raw as string | undefined
1035+ // The memory reload loads THIS directory's workspace memory into the named session, so the
1036+ // session must be one of this directory's; another project's would receive it.
1037+ if ( sessionID ) {
1038+ // `Session.get` validates the id synchronously, so the call is deferred into the promise
1039+ // chain for a malformed id to land in the handler below rather than escape the route.
1040+ const session = await Promise . resolve ( )
1041+ . then ( ( ) => Session . get ( sessionID as never ) )
1042+ . catch ( ( err ) => err as Error )
1043+ if ( session instanceof NotFoundError ) {
1044+ return c . json ( { ok : false , error : `Session not found: ${ sessionID } ` } , 404 )
1045+ }
1046+ if ( session instanceof z . ZodError ) {
1047+ return c . json ( { ok : false , error : `Invalid sessionID: ${ sessionID } ` } , 400 )
1048+ }
1049+ if ( session instanceof Error ) {
1050+ log . error ( "workspace refresh: session lookup failed" , { error : session . message } )
1051+ return c . json ( { ok : false , error : session . message } , 500 )
1052+ }
1053+ if ( nodePath . resolve ( session . directory ) !== nodePath . resolve ( Instance . directory ) ) {
1054+ return c . json ( { ok : false , error : "That session belongs to a different project directory." } , 400 )
1055+ }
1056+ }
1057+ try {
1058+ const Manage = await import ( "../altimate/workspace/manage" )
1059+ // A changed skill snapshot reaches the registry at the start of the next turn
1060+ // (`refreshSkillRegistry` in session/prompt.ts), so nothing is invalidated here.
1061+ const report = await Manage . refresh ( Instance . directory , sessionID )
1062+ return c . json ( { ok : true as const , ...report } )
1063+ } catch ( err ) {
1064+ const error = err instanceof Error ? err . message : String ( err )
1065+ log . error ( "workspace refresh: failed" , { error } )
1066+ return c . json ( { ok : false , error } , 500 )
1067+ }
1068+ } )
1069+ . post ( "/altimate/workspace/sync" , async ( c ) => {
1070+ const refused = workspaceRouteRefusal ( c . req . header ( "origin" ) , c . req . header ( "host" ) )
1071+ if ( refused ) return c . json ( refused . body , refused . status )
1072+ try {
1073+ const Manage = await import ( "../altimate/workspace/manage" )
1074+ const report = await Manage . sync ( Instance . directory )
1075+ return c . json ( { ok : true as const , ...report } )
1076+ } catch ( err ) {
1077+ const error = err instanceof Error ? err . message : String ( err )
1078+ log . error ( "workspace sync: failed" , { error } )
1079+ return c . json ( { ok : false , error } , 500 )
1080+ }
1081+ } )
1082+ // altimate_change end
9521083 . all ( "/*" , async ( c ) => {
9531084 const path = c . req . path
9541085
0 commit comments