Skip to content

feat: add standalone server startup script and server path resolution#1045

Open
HosniBelfeki wants to merge 1 commit intoItzCrazyKns:masterfrom
HosniBelfeki:master
Open

feat: add standalone server startup script and server path resolution#1045
HosniBelfeki wants to merge 1 commit intoItzCrazyKns:masterfrom
HosniBelfeki:master

Conversation

@HosniBelfeki
Copy link

@HosniBelfeki HosniBelfeki commented Mar 9, 2026

  • 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.

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

    • Added scripts/start-standalone.mjs to copy public/_next/static, locate server.js in .next/standalone, and start the server.
    • Introduced src/lib/serverPaths.ts with APP_ROOT, DATA_ROOT, DRIZZLE_ROOT; DB, config, uploads, and migrations now use these paths and auto-create needed dirs.
    • Configured Next for standalone: output: 'standalone', outputFileTracingRoot, and package.json start uses the new runner; build runs via Node with increased memory.
  • Bug Fixes

    • SearXNG requests now validate status/content-type and provide clear errors; added isSearxngError and wrapped web/image/video searches and discover route to skip gracefully when SearXNG is down.
    • Prevented unhandled promise rejections in chat/search routes; errors are caught and emitted on the session.
    • Stabilized researcher flow: guard .research() with a catch fallback and fixed action registry iteration.
    • Stock widget lazy-loads yahoo-finance2 and requires Node 22+ to avoid runtime issues.
    • Updated searxng/limiter.toml to disable link_token for local API access.

Written for commit fd6276e. Summary will update on new commits.

- 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.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-ai with guidance or docs links (including llms.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) => {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

classification: classification,
config: input.config,
})
.catch((error) => {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

},
}).catch((error) => {
console.error('Error in chat search agent:', error);
session.emit('error', {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

}

if (entry.isFile() && entry.name === 'server.js') {
return fullPath;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

engines: ['youtube'],
});
} catch (error) {
if (isSearxngError(error)) {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
if (isSearxngError(error)) {
if (
isSearxngError(error) ||
(error instanceof TypeError && /fetch failed/i.test(error.message))
) {
Fix with Cubic

"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",
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

chatId: crypto.randomUUID(),
messageId: crypto.randomUUID(),
})
.catch((error) => {
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant