From efc145cdeb13356ee2f6ce02ea2d0a5abe4f3596 Mon Sep 17 00:00:00 2001 From: htafolla Date: Tue, 31 Mar 2026 07:11:53 -0500 Subject: [PATCH] fix: add isValidClassInstance method to detect serialized class instances - Added isValidClassInstance() method to detect when class instances lose their prototype chain after JSON serialization/deserialization - Enhanced get() logging to include valueType for debugging - This prevents issues when storing class instances like ProcessorManager in state manager with JSON persistence enabled Fixes: processorManager.executePreProcessors is not a function --- src/state/state-manager.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/state/state-manager.ts b/src/state/state-manager.ts index db6ed1545..4e307793e 100644 --- a/src/state/state-manager.ts +++ b/src/state/state-manager.ts @@ -151,6 +151,7 @@ export class StringRayStateManager implements StateManager { frameworkLogger.log("state-manager", "get operation", "info", { key, hasValue: value !== undefined, + valueType: value ? (value as unknown as { constructor?: { name?: string } }).constructor?.name : undefined, }); return value; } @@ -262,6 +263,34 @@ export class StringRayStateManager implements StateManager { return StringRayStateManager.VERSION || "1.1.1"; } + // Check if stored value is a valid class instance (has prototype methods) + // This helps detect values that were serialized to JSON and lost their prototype chain + isValidClassInstance( + value: unknown, + methodName: string, + ): boolean { + if (!value || typeof value !== "object") return false; + if (typeof (value as Record)[methodName] === "function") + return true; + // Check if it's a plain object that lost its prototype + if ( + (value as Record).constructor?.name === "Object" || + !(value as Record).constructor + ) { + frameworkLogger.log( + "state-manager", + "invalid-class-instance-detected", + "warning", + { + key: "detection", + hasMethod: methodName in (value as Record), + }, + ); + return false; + } + return false; + } + getAuditLog(): Array<{ timestamp: Date; operation: string; key: string }> { return []; // Simplified implementation for testing }