{"record":{"id":"fc778577fad6f7ef","repo":"Hmbown/CodeWhale","slug":"s","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"console","errorClass":"NSException","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/darwin-accessibility.m","lineNumber":1804,"sourceCode":"    if(e!=kAXErrorSuccess) @throw [NSException exceptionWithName:@\"action\" reason:[NSString stringWithFormat:@\"accessibility action failed: %d\",e] userInfo:nil];\n    NSMutableDictionary *done=[@{@\"action_sent\":@YES,@\"strategy\":@\"a11y\"} mutableCopy];\n    if([tool isEqual:@\"focus_element\"]) done[@\"focused\"]=@YES;\n    return done;\n  } @finally { CFRelease(app); }\n}\nint main(int argc, const char **argv){ @autoreleasepool {\n  signal(SIGTERM,cuCancel); signal(SIGINT,cuCancel); signal(SIGPIPE,SIG_IGN);\n  @try { if(argc!=2) @throw [NSException exceptionWithName:@\"args\" reason:@\"expected one JSON argument\" userInfo:nil];\n    NSError *error=nil; id p=[NSJSONSerialization JSONObjectWithData:[[NSString stringWithUTF8String:argv[1]] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];\n    if(![p isKindOfClass:NSDictionary.class]) @throw [NSException exceptionWithName:@\"json\" reason:@\"invalid request\" userInfo:nil];\n    id result=execute(p);\n    if([p[@\"args\"][@\"input_lease\"] boolValue]) { NSMutableDictionary *ack=[result mutableCopy]; ack[@\"input_lease\"]=@YES; result=ack; }\n    NSData *data=[NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingFragmentsAllowed error:&error];\n    if(!data) @throw [NSException exceptionWithName:@\"json\" reason:error.localizedDescription userInfo:nil];\n    puts([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].UTF8String); fflush(stdout);\n    if([p[@\"args\"][@\"input_lease\"] boolValue]) cuWaitForLease();\n    return 0;\n  } @catch(NSException *e){ cuReleaseLease(); fprintf(stderr,\"%s\\n\",e.reason.UTF8String); return 1; }\n} }\n","sourceCodeStart":1786,"sourceCodeEnd":1806,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/src/backends/darwin-accessibility.m#L1786-L1806","documentation":"This Objective-C computer-use backend serializes the command's result dictionary with NSJSONSerialization (fragments allowed) and prints it to stdout as the command's output contract. If serialization fails — typically because the result contains an object NSJSONSerialization cannot encode, such as an NSNumber holding NaN/inf, a non-string dictionary key, or a cycle — it throws an NSException named \"json\" carrying the underlying NSError's localizedDescription. The catch block releases the input lease and exits 1 after writing the reason to stderr.","triggerScenarios":"Any backend command (invoked with args containing input_lease) whose constructed `result` dictionary contains values NSJSONSerialization dataWithJSONObject:options:error: rejects: NaN or infinite floats, NSNull used as a dictionary key, NSDecimalNumber in exotic states, or a mutated result object that introduced a retain cycle. Not triggered by user input alone — only by a backend bug producing a non-JSON-encodable value.","commonSituations":"A newly added command or field in the darwin-accessibility backend returns a value type not JSON-encodable (e.g. a computed NaN coordinate from a failed CGEvent or accessibility geometry query); a macOS API update returns nil-backed objects that become invalid NSNumbers; a developer extends the result dict with a non-NSString key.","solutions":["Read the stderr message (the localizedDescription) to identify the offending value type; locate the command that built the result and sanitize its new field (map NaN/inf to NSNull or 0, coerce keys to NSString).","Audit the failing code path for numeric results coming from CoreGraphics/AX APIs that can return NaN (e.g. kCGNullWindowID or zero-rect queries) and guard them before inserting into the result.","If you own the plugin, add a pre-serialization validity check with NSJSONSerialization isValidJSONObject: and log the result dictionary keys to pinpoint the bad entry.","As a workaround while debugging, serialize individual fields to find which one fails before fixing the source."],"exampleFix":"// before\nack[@\"cursor\"] = [NSNumber numberWithDouble:NAN]; // from a failed position query\nNSData *data=[NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingFragmentsAllowed error:&error];\n// throws NSException \"json\"\n// after\ndouble pos = queryCursor();\nack[@\"cursor\"] = isfinite(pos) ? @(pos) : NSNull.null;\nNSData *data=[NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingFragmentsAllowed error:&error];","handlingStrategy":"try-catch","validationCode":"// In the command handler, before serialization:\nif (![NSJSONSerialization isValidJSONObject:result]) {\n    NSLog(@\"non-JSON result keys: %@\", [result allKeys]);\n}\nfor (id v in [result allValues]) {\n    if ([v isKindOfClass:[NSNumber class]] &&\n        (isnan([v doubleValue]) || isinf([v doubleValue]))) {\n        // replace with NSNull or a sentinel before building result\n    }\n}","typeGuard":"static inline BOOL CuJSONEncodable(id v) {\n  if ([v isKindOfClass:[NSNumber class]]) {\n    double d = [v doubleValue];\n    return !(isnan(d) || isinf(d));\n  }\n  return [NSJSONSerialization isValidJSONObject:@[ v ]] || v == NSNull.null;\n}","tryCatchPattern":"@try {\n    NSData *data=[NSJSONSerialization dataWithJSONObject:result\n        options:NSJSONWritingFragmentsAllowed error:&error];\n    if(!data) NSLog(@\"serialize failed: %@\", error.localizedDescription);\n} @catch(NSException *e){\n    // e.name == @\"json\" — sanitize result and retry once with NSNull substitution\n}","preventionTips":["Run NSJSONSerialization isValidJSONObject: on every newly added result field during development.","Never insert raw doubles from geometry/AX APIs without an isfinite check.","Keep result dictionary keys NSString-only.","Test every new backend command end-to-end on macOS before shipping, since the exception only surfaces at runtime."],"tags":["json","serialization","macos","objective-c"],"backgroundTag":"json-serialization-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}