feat: add standalone server startup script and server path resolution#1045
feat: add standalone server startup script and server path resolution#1045HosniBelfeki wants to merge 1 commit intoItzCrazyKns:masterfrom
Conversation
- Introduced a new script `start-standalone.mjs` to facilitate the startup of the standalone server. - Implemented directory creation and file copying logic to ensure necessary resources are available. - Added a function to locate the server entry point (`server.js`) within the standalone build. - Created a new module `serverPaths.ts` to resolve application root paths, including data and drizzle directories.
There was a problem hiding this comment.
7 issues found across 25 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/app/api/chat/route.ts">
<violation number="1" location="src/app/api/chat/route.ts:228">
P2: Detached `searchAsync` rejection is consumed via session error emission, preventing centralized 500 handling for async failures.</violation>
</file>
<file name="src/lib/agents/search/api.ts">
<violation number="1" location="src/lib/agents/search/api.ts:35">
P1: Research failures are swallowed by a local `.catch`, causing false-success responses and preventing proper error propagation for 500 handling.</violation>
</file>
<file name="scripts/start-standalone.mjs">
<violation number="1" location="scripts/start-standalone.mjs:43">
P2: Fallback server discovery is overly broad and may execute an unrelated nested `server.js` entrypoint.</violation>
</file>
<file name="src/lib/agents/media/video.ts">
<violation number="1" location="src/lib/agents/media/video.ts:52">
P2: SearXNG fallback handling is inconsistent because the error guard is too narrow and may miss fetch/network outage errors.</violation>
</file>
<file name="src/lib/agents/search/index.ts">
<violation number="1" location="src/lib/agents/search/index.ts:91">
P1: Research errors are swallowed and converted to empty results, causing false-success completion and preventing upstream error propagation.</violation>
</file>
<file name="package.json">
<violation number="1" location="package.json:8">
P2: Build script is coupled to Next.js internal package path instead of stable CLI entrypoint, making upgrades and environment changes more brittle.</violation>
</file>
<file name="src/app/api/search/route.ts">
<violation number="1" location="src/app/api/search/route.ts:69">
P2: Detached `searchAsync` errors are swallowed by local `.catch()` and rerouted through `session.emit('error')`, bypassing centralized route-level error handling.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| classification: classification, | ||
| config: input.config, | ||
| }) | ||
| .catch((error) => { |
There was a problem hiding this comment.
P1: Research failures are swallowed by a local .catch, causing false-success responses and preventing proper error propagation for 500 handling.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/agents/search/api.ts, line 35:
<comment>Research failures are swallowed by a local `.catch`, causing false-success responses and preventing proper error propagation for 500 handling.</comment>
<file context>
@@ -25,12 +25,20 @@ class APISearchAgent {
+ classification: classification,
+ config: input.config,
+ })
+ .catch((error) => {
+ console.error('Research step failed:', error);
+ return {
</file context>
| classification: classification, | ||
| config: input.config, | ||
| }) | ||
| .catch((error) => { |
There was a problem hiding this comment.
P1: Research errors are swallowed and converted to empty results, causing false-success completion and preventing upstream error propagation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/agents/search/index.ts, line 91:
<comment>Research errors are swallowed and converted to empty results, causing false-success completion and preventing upstream error propagation.</comment>
<file context>
@@ -81,12 +81,20 @@ class SearchAgent {
+ classification: classification,
+ config: input.config,
+ })
+ .catch((error) => {
+ console.error('Research step failed:', error);
+ return {
</file context>
| }, | ||
| }).catch((error) => { | ||
| console.error('Error in chat search agent:', error); | ||
| session.emit('error', { |
There was a problem hiding this comment.
P2: Detached searchAsync rejection is consumed via session error emission, preventing centralized 500 handling for async failures.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/app/api/chat/route.ts, line 228:
<comment>Detached `searchAsync` rejection is consumed via session error emission, preventing centralized 500 handling for async failures.</comment>
<file context>
@@ -223,6 +223,13 @@ export const POST = async (req: Request) => {
},
+ }).catch((error) => {
+ console.error('Error in chat search agent:', error);
+ session.emit('error', {
+ data: {
+ message: error instanceof Error ? error.message : 'Search failed',
</file context>
| } | ||
|
|
||
| if (entry.isFile() && entry.name === 'server.js') { | ||
| return fullPath; |
There was a problem hiding this comment.
P2: Fallback server discovery is overly broad and may execute an unrelated nested server.js entrypoint.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At scripts/start-standalone.mjs, line 43:
<comment>Fallback server discovery is overly broad and may execute an unrelated nested `server.js` entrypoint.</comment>
<file context>
@@ -0,0 +1,85 @@
+ }
+
+ if (entry.isFile() && entry.name === 'server.js') {
+ return fullPath;
+ }
+ }
</file context>
| engines: ['youtube'], | ||
| }); | ||
| } catch (error) { | ||
| if (isSearxngError(error)) { |
There was a problem hiding this comment.
P2: SearXNG fallback handling is inconsistent because the error guard is too narrow and may miss fetch/network outage errors.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/agents/media/video.ts, line 52:
<comment>SearXNG fallback handling is inconsistent because the error guard is too narrow and may miss fetch/network outage errors.</comment>
<file context>
@@ -43,9 +43,21 @@ const searchVideos = async (
+ engines: ['youtube'],
+ });
+ } catch (error) {
+ if (isSearxngError(error)) {
+ console.warn(
+ `Video search skipped because SearXNG is unavailable: ${error.message}`,
</file context>
| if (isSearxngError(error)) { | |
| if ( | |
| isSearxngError(error) || | |
| (error instanceof TypeError && /fetch failed/i.test(error.message)) | |
| ) { |
| "dev": "next dev --webpack", | ||
| "build": "next build --webpack", | ||
| "start": "next start", | ||
| "build": "node --max-old-space-size=4096 ./node_modules/next/dist/bin/next build --webpack", |
There was a problem hiding this comment.
P2: Build script is coupled to Next.js internal package path instead of stable CLI entrypoint, making upgrades and environment changes more brittle.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At package.json, line 8:
<comment>Build script is coupled to Next.js internal package path instead of stable CLI entrypoint, making upgrades and environment changes more brittle.</comment>
<file context>
@@ -5,8 +5,8 @@
"dev": "next dev --webpack",
- "build": "next build --webpack",
- "start": "next start",
+ "build": "node --max-old-space-size=4096 ./node_modules/next/dist/bin/next build --webpack",
+ "start": "node scripts/start-standalone.mjs",
"lint": "next lint",
</file context>
| chatId: crypto.randomUUID(), | ||
| messageId: crypto.randomUUID(), | ||
| }) | ||
| .catch((error) => { |
There was a problem hiding this comment.
P2: Detached searchAsync errors are swallowed by local .catch() and rerouted through session.emit('error'), bypassing centralized route-level error handling.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/app/api/search/route.ts, line 69:
<comment>Detached `searchAsync` errors are swallowed by local `.catch()` and rerouted through `session.emit('error')`, bypassing centralized route-level error handling.</comment>
<file context>
@@ -51,20 +51,27 @@ export const POST = async (req: Request) => {
+ chatId: crypto.randomUUID(),
+ messageId: crypto.randomUUID(),
+ })
+ .catch((error) => {
+ console.error('Error in search agent:', error);
+ session.emit('error', {
</file context>
start-standalone.mjsto facilitate the startup of the standalone server.server.js) within the standalone build.serverPaths.tsto resolve application root paths, including data and drizzle directories.Summary by cubic
Adds a standalone startup flow with a script that finds and runs the built server, and centralizes data/drizzle paths so the app starts reliably in production and containers. Also hardens SearXNG and async search handling to fail gracefully instead of crashing.
New Features
scripts/start-standalone.mjsto copypublic/_next/static, locateserver.jsin.next/standalone, and start the server.src/lib/serverPaths.tswithAPP_ROOT,DATA_ROOT,DRIZZLE_ROOT; DB, config, uploads, and migrations now use these paths and auto-create needed dirs.output: 'standalone',outputFileTracingRoot, andpackage.jsonstartuses the new runner;buildruns via Node with increased memory.Bug Fixes
isSearxngErrorand wrapped web/image/video searches and discover route to skip gracefully when SearXNG is down..research()with a catch fallback and fixed action registry iteration.yahoo-finance2and requires Node 22+ to avoid runtime issues.searxng/limiter.tomlto disablelink_tokenfor local API access.Written for commit fd6276e. Summary will update on new commits.