Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ export function initStatus(client: PluginInput["client"], directory: string): vo

function getBus(): Promise<BusClient> {
if (!_busPromise) {
_busPromise = BusClient.connect().catch((err) => {
_busPromise = BusClient.connect({
onWarn: (msg, ...args) => {
_client?.app?.log({
body: {
service: "brain",
level: "warn",
message: msg,
extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") }
}
Comment on lines +99 to +106

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, check if the file exists and examine the relevant lines
if [ -f "src/status.ts" ]; then
  echo "=== File found, examining lines 95-115 ==="
  sed -n '95,115p' src/status.ts | cat -n
else
  echo "File not found"
fi

Repository: four-bytes/four-opencode-brain

Length of output: 973


🏁 Script executed:

# Also check the full context around getBus or the onWarn setup
rg -n "onWarn|BusClient\.connect" src/status.ts -B 3 -A 10

Repository: four-bytes/four-opencode-brain

Length of output: 576


🏁 Script executed:

# Verify the coding guidelines compliance - check for ESM imports and JSON usage
head -30 src/status.ts | cat -n

Repository: four-bytes/four-opencode-brain

Length of output: 1397


🏁 Script executed:

# Check if there are any other usages of JSON.stringify in this file that might give context
rg -n "JSON\.stringify" src/status.ts

Repository: four-bytes/four-opencode-brain

Length of output: 383


Prevent synchronous throws inside the warning callback

Line 105 calls JSON.stringify(a) which can throw synchronously if the object has circular references, a throwing toJSON() method, or other unserializable values. A throwing onWarn callback would break the warning handling system itself. Add defensive serialization so this callback never throws.

Proposed fix
     _busPromise = BusClient.connect({
       onWarn: (msg, ...args) => {
+        const details = args
+          .map((a) => {
+            try {
+              return typeof a === "object" && a !== null ? JSON.stringify(a) : String(a);
+            } catch {
+              return "[unserializable]";
+            }
+          })
+          .join(" ");
+
         _client?.app?.log({
           body: {
             service: "brain",
             level: "warn",
             message: msg,
-            extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") }
+            extra: { details }
           }
         }).catch(() => {});
       }
     }).catch((err) => {
Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
onWarn: (msg, ...args) => {
_client?.app?.log({
body: {
service: "brain",
level: "warn",
message: msg,
extra: { details: args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(" ") }
}
onWarn: (msg, ...args) => {
const details = args
.map((a) => {
try {
return typeof a === "object" && a !== null ? JSON.stringify(a) : String(a);
} catch {
return "[unserializable]";
}
})
.join(" ");
_client?.app?.log({
body: {
service: "brain",
level: "warn",
message: msg,
extra: { details }
}
Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/status.ts` around lines 99 - 106, The onWarn callback in the status.ts
file contains a JSON.stringify call that can throw synchronously if objects
contain circular references, failing toJSON methods, or unserializable values,
which would break the warning handling system. Wrap the JSON.stringify call in a
try-catch block to defensively serialize the argument, and provide a fallback
string representation (such as a generic message or toString fallback) when
serialization fails, ensuring the onWarn callback never throws and the warning
logging continues to function properly.

}).catch(() => {});
}
}).catch((err) => {
_client?.app?.log({ body: { service: "brain", level: "warn", message: "BusClient connect failed", extra: { error: String(err) } } }).catch(() => {});
_busPromise = null; // allow retry on next call
throw err;
Expand Down
Loading