@@ -21,6 +21,21 @@ import {
2121import { reconcileOAuthProviders } from "../oauth" ;
2222import { invalidateCodexModelsCache } from "../codex/catalog" ;
2323import { startMemoryWatchdog } from "./memory-watchdog" ;
24+ import {
25+ reconcileLiveStateStores ,
26+ setLiveStateStoreConfig ,
27+ } from "../lib/state-store-registrations" ;
28+ import { startStateStoreSweeper } from "../lib/state-store-sweeper" ;
29+ import {
30+ configureAppOwnedMemoryBudget ,
31+ enforceAppOwnedMemoryBudget ,
32+ resolveAppOwnedMemoryBudgetBytes ,
33+ } from "../lib/app-owned-memory" ;
34+ import {
35+ registerAppOwnedMemorySweepFallback ,
36+ registerDefaultAppOwnedMemoryStores ,
37+ registerDefaultAppOwnedObservedBuffers ,
38+ } from "../lib/app-owned-memory-stores" ;
2439import { setStorageCleanupPolicyLiveSink } from "../storage/policy" ;
2540import { setStorageCleanupPolicyJobLiveApply } from "../storage/policy-job" ;
2641import { scheduleStorageCleanupStartupRun , startStorageCleanupScheduler } from "../storage/policy-scheduler" ;
@@ -39,7 +54,8 @@ export {
3954 resolveCodexAccountForThread ,
4055} from "../codex/routing" ;
4156import { formatCodexProviderForLog } from "../codex/routing" ;
42- import { registerCodexWebSocket , unregisterCodexWebSocket , updateCodexWebSocketAuthContext } from "../codex/websocket-registry" ;
57+ import { CatalogGatherBusyError } from "../codex/catalog/provider-fetch" ;
58+ import { registerCodexWebSocket , tryReserveCodexWebSocket , unregisterCodexWebSocket , updateCodexWebSocketAuthContext } from "../codex/websocket-registry" ;
4359import { resolveGuiFilePath , rootFallbackPayload , serveGuiFile } from "./gui-static" ;
4460export { resolveGuiFilePath , rootFallbackPayload } from "./gui-static" ;
4561export { resolveAdapter } from "./adapter-resolve" ;
@@ -51,7 +67,9 @@ import {
5167 registerTurn ,
5268 setServerRef ,
5369 trackStreamLifetime ,
70+ tryAdmitTurn ,
5471 unregisterTurn ,
72+ type ActiveTurnLease ,
5573} from "./lifecycle" ;
5674export {
5775 drainAndShutdown ,
@@ -255,12 +273,14 @@ function attachLiveSidebandUpstream(ws: ServerWebSocket<WsData>): void {
255273
256274export function startServer ( port ?: number ) {
257275 const config = runAlibabaRegionStartupMigration ( runOpenAiTierStartupMigration ( loadConfig ( ) ) ) ;
276+ setLiveStateStoreConfig ( config ) ;
258277 applyProxyEnv ( config ) ;
259278 assertServerAuthConfig ( config ) ;
260279 const managementAuth = initializeManagementAuthState ( config ) ;
261280 // Refresh OAuth provider presets (models/noReasoningModels) from the registry so a proxy update
262281 // adding/dropping models reaches existing configs on start — not just fresh installs.
263282 reconcileOAuthProviders ( config ) ;
283+ reconcileLiveStateStores ( ) ;
264284 // Seed default featured subagent models on first run only (UNSET → defaults). A user-set list,
265285 // even [], is left alone so GUI removals persist.
266286 if ( config . subagentModels === undefined ) {
@@ -304,6 +324,12 @@ export function startServer(port?: number) {
304324 // #314: warn-only RSS observability (unref'd, idempotent — safe under repeated
305325 // startServer(0) in tests). Snapshot surfaces via GET /api/system/memory.
306326 startMemoryWatchdog ( ) ;
327+ registerDefaultAppOwnedMemoryStores ( ) ;
328+ registerDefaultAppOwnedObservedBuffers ( ) ;
329+ registerAppOwnedMemorySweepFallback ( ) ;
330+ configureAppOwnedMemoryBudget ( resolveAppOwnedMemoryBudgetBytes ( config . appOwnedMemoryBudgetMb ) ) ;
331+ enforceAppOwnedMemoryBudget ( ) ;
332+ startStateStoreSweeper ( ) ;
307333 // Issue #42 Phase 3: opt-in archived auto-cleanup (default OFF). Unref'd hourly
308334 // tick for daily/weekly; startup evaluation is fire-and-forget after listen.
309335 // Heavy work runs in a Worker via the single-flight job controller.
@@ -337,6 +363,31 @@ export function startServer(port?: number) {
337363 return new Response ( response . body , { status : 503 , headers } ) ;
338364 }
339365
366+ function serverBusyResponse ( req : Request , resource : string ) : Response {
367+ return withCors ( new Response ( JSON . stringify ( {
368+ error : { type : "server_error" , code : "server_busy" , message : `${ resource } capacity reached` } ,
369+ } ) , {
370+ status : 503 ,
371+ headers : { "Content-Type" : "application/json" , "Retry-After" : "1" } ,
372+ } ) , req , config ) ;
373+ }
374+
375+ async function runAdmittedHttpTurn ( req : Request , work : ( lease : ActiveTurnLease ) => Promise < Response > ) : Promise < Response > {
376+ const lease = tryAdmitTurn ( ) ;
377+ if ( ! lease ) return serverBusyResponse ( req , "active turns" ) ;
378+ let response : Response ;
379+ try {
380+ response = await work ( lease ) ;
381+ } catch ( error ) {
382+ lease . release ( ) ;
383+ throw error ;
384+ }
385+ if ( ! lease . isTransferred ( ) ) {
386+ lease . release ( ) ;
387+ }
388+ return response ;
389+ }
390+
340391 const server : Server < WsData > = Bun . serve < WsData > ( {
341392 port : listenPort ,
342393 hostname : bindHost ,
@@ -380,9 +431,12 @@ export function startServer(port?: number) {
380431 if ( ! websocketsEnabled ( config ) ) {
381432 return withCors ( formatErrorResponse ( 426 , "upgrade_required" , "Responses WebSocket transport is disabled; use HTTP" ) , req , config ) ;
382433 }
434+ const websocketLease = tryReserveCodexWebSocket ( ) ;
435+ if ( ! websocketLease ) return serverBusyResponse ( req , "Codex WebSockets" ) ;
383436 if ( server . upgrade ( req , {
384- data : buildResponsesWsData ( selectForwardHeaders ( req . headers ) , admission ) ,
437+ data : buildResponsesWsData ( selectForwardHeaders ( req . headers ) , admission , websocketLease ) ,
385438 } ) ) return undefined as unknown as Response ;
439+ websocketLease . release ( ) ;
386440 return withCors ( formatErrorResponse ( 426 , "upgrade_required" , "WebSocket upgrade failed" ) , req , config ) ;
387441 }
388442
@@ -408,7 +462,18 @@ export function startServer(port?: number) {
408462 if ( ! isAllowedRequestOrigin ( req , config ) ) {
409463 return withCors ( formatErrorResponse ( 403 , "origin_rejected" , "cross-origin data-plane request blocked" ) , req , config ) ;
410464 }
411- const goModels = await fetchAllModels ( config ) ;
465+ let goModels ;
466+ try {
467+ goModels = await fetchAllModels ( config ) ;
468+ } catch ( error ) {
469+ if ( error instanceof CatalogGatherBusyError ) {
470+ return withCors ( new Response ( JSON . stringify ( { error : { type : "server_error" , code : "catalog_busy" , message : error . message } } ) , {
471+ status : 503 ,
472+ headers : { "content-type" : "application/json" , "Retry-After" : "1" } ,
473+ } ) , req , config ) ;
474+ }
475+ throw error ;
476+ }
412477 const { applyNativeVisibility, buildCatalogEntries, disabledNativeSlugs, exactComboCatalogSlugs, loadCatalogTemplate, nativeOpenAiSlugs, nativeReasoningEfforts, orderForSubagents, filterCatalogVisibleModels, uniqueCatalogModelsForRawPublicList, visibleNativeSlugs, desktopVisibleNativeSlugs } = await import ( "../codex/catalog" ) ;
413478 const nativeSlugs = nativeOpenAiSlugs ( ) ;
414479 const goEnabled = filterCatalogVisibleModels ( goModels , config ) ;
@@ -522,20 +587,17 @@ export function startServer(port?: number) {
522587 ...admissionFields ( admission ) ,
523588 inboundProtocol : "responses" ,
524589 } ;
525- let response : Response ;
526- try {
527- response = await handleResponsesCompact ( req , config , logCtx ) ;
528- } catch {
529- response = formatErrorResponse ( 500 , "server_error" , "Unexpected compact request failure" ) ;
530- }
531- addFinalRequestLog (
532- requestId ,
533- start ,
534- logCtx ,
535- response . status ,
536- response . status === 499 ? { closeReason : "client_cancel" } : undefined ,
537- ) ;
538- return withCors ( response , req , config ) ;
590+ return runAdmittedHttpTurn ( req , async ( ) => {
591+ let response : Response ;
592+ try {
593+ response = await handleResponsesCompact ( req , config , logCtx ) ;
594+ } catch {
595+ response = formatErrorResponse ( 500 , "server_error" , "Unexpected compact request failure" ) ;
596+ }
597+ addFinalRequestLog ( requestId , start , logCtx , response . status ,
598+ response . status === 499 ? { closeReason : "client_cancel" } : undefined ) ;
599+ return withCors ( response , req , config ) ;
600+ } ) ;
539601 }
540602
541603 if (
@@ -559,9 +621,11 @@ export function startServer(port?: number) {
559621 ...admissionFields ( admission ) ,
560622 } ;
561623 const endpoint = url . pathname . endsWith ( "/edits" ) ? "edits" as const : "generations" as const ;
562- const response = await handleImages ( req , config , endpoint , logCtx ) ;
563- addFinalRequestLog ( requestId , start , logCtx , response . status , response . status === 499 ? { closeReason : "client_cancel" } : undefined ) ;
564- return withCors ( response , req , config ) ;
624+ return runAdmittedHttpTurn ( req , async ( ) => {
625+ const response = await handleImages ( req , config , endpoint , logCtx ) ;
626+ addFinalRequestLog ( requestId , start , logCtx , response . status , response . status === 499 ? { closeReason : "client_cancel" } : undefined ) ;
627+ return withCors ( response , req , config ) ;
628+ } ) ;
565629 }
566630
567631 if ( req . method === "GET" && url . pathname . startsWith ( "/v1/opencodex/artifacts/" ) ) {
@@ -611,15 +675,12 @@ export function startServer(port?: number) {
611675 provider : "unknown" ,
612676 ...admissionFields ( admission ) ,
613677 } ;
614- const response = await handleSearch ( req , config , logCtx ) ;
615- addFinalRequestLog (
616- requestId ,
617- start ,
618- logCtx ,
619- response . status ,
620- response . status === 499 ? { closeReason : "client_cancel" } : undefined ,
621- ) ;
622- return withCors ( response , req , config ) ;
678+ return runAdmittedHttpTurn ( req , async ( ) => {
679+ const response = await handleSearch ( req , config , logCtx ) ;
680+ addFinalRequestLog ( requestId , start , logCtx , response . status ,
681+ response . status === 499 ? { closeReason : "client_cancel" } : undefined ) ;
682+ return withCors ( response , req , config ) ;
683+ } ) ;
623684 }
624685
625686 if ( url . pathname === "/v1/responses" && req . method === "POST" ) {
@@ -649,20 +710,23 @@ export function startServer(port?: number) {
649710 logged = true ;
650711 addFinalRequestLog ( requestId , start , logCtx , status , meta ) ;
651712 } ;
652- const response = await handleResponses ( req , config , logCtx , {
653- abortSignal : req . signal ,
654- onFirstOutput : ( ) => recordFirstOutput ( logCtx , start ) ,
655- onNativePassthroughTerminal : status => {
656- finalizeNativePassthroughLog ( httpStatusForTerminalStatus ( status ) , {
657- terminalStatus : status ,
658- closeReason : "terminal" ,
659- } ) ;
660- } ,
661- onNativePassthroughCancel : ( ) => {
662- finalizeNativePassthroughLog ( 499 , { closeReason : "client_cancel" } ) ;
663- } ,
713+ return runAdmittedHttpTurn ( req , async turnAdmissionLease => {
714+ const response = await handleResponses ( req , config , logCtx , {
715+ turnAdmissionLease,
716+ abortSignal : req . signal ,
717+ onFirstOutput : ( ) => recordFirstOutput ( logCtx , start ) ,
718+ onNativePassthroughTerminal : status => {
719+ finalizeNativePassthroughLog ( httpStatusForTerminalStatus ( status ) , {
720+ terminalStatus : status ,
721+ closeReason : "terminal" ,
722+ } ) ;
723+ } ,
724+ onNativePassthroughCancel : ( ) => {
725+ finalizeNativePassthroughLog ( 499 , { closeReason : "client_cancel" } ) ;
726+ } ,
727+ } ) ;
728+ return withCors ( responseWithDeferredRequestLog ( response , requestId , start , logCtx ) , req , config ) ;
664729 } ) ;
665- return withCors ( responseWithDeferredRequestLog ( response , requestId , start , logCtx ) , req , config ) ;
666730 }
667731
668732 // Anthropic Messages inbound (Claude Code). count_tokens FIRST (longer path).
@@ -678,8 +742,7 @@ export function startServer(port?: number) {
678742 if ( ! isAllowedRequestOrigin ( req , config ) ) {
679743 return withCors ( anthropicErrorResponse ( 403 , "cross-origin data-plane request blocked" , "permission_error" ) , req , config ) ;
680744 }
681- const response = await handleClaudeCountTokens ( req , config ) ;
682- return withCors ( response , req , config ) ;
745+ return runAdmittedHttpTurn ( req , async ( ) => withCors ( await handleClaudeCountTokens ( req , config ) , req , config ) ) ;
683746 }
684747
685748 if ( url . pathname === "/v1/messages" && req . method === "POST" ) {
@@ -705,8 +768,11 @@ export function startServer(port?: number) {
705768 // Logging is finalized inside handleClaudeMessages (Responses-vocab tap on the
706769 // pre-translation stream + native passthrough callbacks) — do not re-wrap the
707770 // translated Anthropic stream here.
708- const response = await handleClaudeMessages ( req , config , logCtx , { requestId, start } ) ;
709- return withCors ( response , req , config ) ;
771+ return runAdmittedHttpTurn ( req , async turnAdmissionLease => withCors (
772+ await handleClaudeMessages ( req , config , logCtx , { requestId, start, turnAdmissionLease } ) ,
773+ req ,
774+ config ,
775+ ) ) ;
710776 }
711777
712778
@@ -729,8 +795,11 @@ export function startServer(port?: number) {
729795 ...admissionFields ( admission ) ,
730796 inboundProtocol : "chat" ,
731797 } ;
732- const response = await handleChatCompletions ( req , config , logCtx , { requestId, start } ) ;
733- return withCors ( response , req , config ) ;
798+ return runAdmittedHttpTurn ( req , async turnAdmissionLease => withCors (
799+ await handleChatCompletions ( req , config , logCtx , { requestId, start, turnAdmissionLease } ) ,
800+ req ,
801+ config ,
802+ ) ) ;
734803 }
735804
736805 // ChatGPT / Codex App voice (GPT‑Live / Frameless Bidi) + OpenAI Realtime call-create.
@@ -756,15 +825,17 @@ export function startServer(port?: number) {
756825 provider : "unknown" ,
757826 ...admissionFields ( admission ) ,
758827 } ;
759- const response = await handleLive ( req , config , logCtx ) ;
760- addFinalRequestLog (
761- requestId ,
762- start ,
763- logCtx ,
764- response . status ,
765- response . status === 499 ? { closeReason : "client_cancel" } : undefined ,
766- ) ;
767- return withCors ( response , req , config ) ;
828+ return runAdmittedHttpTurn ( req , async ( ) => {
829+ const response = await handleLive ( req , config , logCtx ) ;
830+ addFinalRequestLog (
831+ requestId ,
832+ start ,
833+ logCtx ,
834+ response . status ,
835+ response . status === 499 ? { closeReason : "client_cancel" } : undefined ,
836+ ) ;
837+ return withCors ( response , req , config ) ;
838+ } ) ;
768839 }
769840
770841 // Voice / Realtime sideband WebSocket: Frameless joins /v1/live/{callId}; Realtime v1 joins
@@ -836,6 +907,11 @@ export function startServer(port?: number) {
836907 attachLiveSidebandUpstream ( ws ) ;
837908 return ;
838909 }
910+ if ( ! ws . data . admissionLease ) {
911+ ws . close ( 1013 , "server busy" ) ;
912+ return ;
913+ }
914+ ws . data . admissionLease . bind ( ws ) ;
839915 registerCodexWebSocket ( ws ) ;
840916 } ,
841917 message ( ws : ServerWebSocket < WsData > , raw : string | Buffer ) {
@@ -904,9 +980,21 @@ export function startServer(port?: number) {
904980 return ;
905981 }
906982
983+ const turnAdmissionLease = tryAdmitTurn ( ) ;
984+ if ( ! turnAdmissionLease ) {
985+ sendJsonFrame ( ws , buildWsErrorFrame ( 503 , {
986+ type : "server_error" ,
987+ code : "server_busy" ,
988+ message : "active turns capacity reached" ,
989+ retryable : true ,
990+ } , new Headers ( { "Retry-After" : "1" } ) ) ) ;
991+ if ( ws . data . cancel === cancelTurn ) ws . data . cancel = undefined ;
992+ return ;
993+ }
994+
907995 const payload : Record < string , unknown > = { ...frame } ;
908996 delete payload . type ;
909- registerTurn ( turnAbort ) ;
997+ turnAdmissionLease . bindAbortController ( turnAbort ) ;
910998 void ( async ( ) => {
911999 const start = Date . now ( ) ;
9121000 const requestId = nextRequestLogId ( start ) ;
@@ -943,6 +1031,7 @@ export function startServer(port?: number) {
9431031 const response = await handleResponses ( req , config , logCtx , {
9441032 forceEmptyResponseId : true ,
9451033 abortSignal : turnAbort . signal ,
1034+ turnAdmissionLease,
9461035 onFirstOutput : ( ) => recordFirstOutput ( logCtx , start ) ,
9471036 onCodexAuthContextResolved : context => updateCodexWebSocketAuthContext ( ws , context ) ,
9481037 recordTerminalOutcomes : false ,
@@ -983,7 +1072,7 @@ export function startServer(port?: number) {
9831072 /* socket already gone or send dropped */
9841073 }
9851074 } finally {
986- unregisterTurn ( turnAbort ) ;
1075+ turnAdmissionLease . release ( ) ;
9871076 if ( ! logged && turnAbort . signal . aborted ) finalizeLog ( 499 ) ;
9881077 if ( ws . data . cancel === cancelTurn ) ws . data . cancel = undefined ;
9891078 }
@@ -996,6 +1085,8 @@ export function startServer(port?: number) {
9961085 return ;
9971086 }
9981087 unregisterCodexWebSocket ( ws ) ;
1088+ ws . data . admissionLease ?. release ( ) ;
1089+ ws . data . admissionLease = undefined ;
9991090 ws . data . cancel ?.( ) ; // RC2: abort the upstream when the client disconnects
10001091 } ,
10011092 } ,
0 commit comments