Skip to content

arch: Enhanced codec with explicit edge case handling #145

@bbopen

Description

@bbopen

Overview

Multiple issues relate to codec edge cases that fail silently or with unclear errors. This tracking issue proposes enhanced codec error handling and edge case support.

Related Issues

Proposed Architecture

New Error Type

export class BridgeCodecError extends BridgeError {
  constructor(
    message: string,
    public readonly codecPhase: 'encode' | 'decode',
    public readonly valueType: string,
    options?: ErrorOptions
  ) {
    super(message, options);
  }
}

Explicit Edge Case Handlers

// src/utils/codec.ts

function encodeValue(value: unknown): unknown {
  // Dict key validation
  if (isPlainObject(value)) {
    for (const key of Object.keys(value)) {
      if (typeof key !== 'string') {
        throw new BridgeCodecError(
          `Dict keys must be strings, got ${typeof key}`,
          'encode',
          'dict'
        );
      }
    }
  }
  
  // Bytes handling
  if (value instanceof Uint8Array || value instanceof ArrayBuffer) {
    throw new BridgeCodecError(
      'bytes/bytearray must be base64 encoded before sending',
      'encode',
      'bytes'
    );
  }
  
  return value;
}

function decodeValue(value: unknown): unknown {
  // numpy/pandas scalar handling
  if (isNumpyScalar(value)) {
    return convertNumpyScalar(value);
  }
  
  // Pydantic model handling
  if (isPydanticModel(value)) {
    try {
      return value.model_dump();
    } catch (error) {
      throw new BridgeCodecError(
        'Pydantic model_dump() failed',
        'decode',
        'pydantic',
        { cause: error }
      );
    }
  }
  
  return value;
}

Environment Validation

function getMaxBytesFromEnv(): number {
  const raw = process.env.TYWRAP_CODEC_MAX_BYTES;
  if (!raw) return DEFAULT_MAX_BYTES;
  
  const parsed = parseInt(raw, 10);
  if (!Number.isFinite(parsed) || parsed <= 0) {
    throw new BridgeCodecError(
      `Invalid TYWRAP_CODEC_MAX_BYTES: ${raw}`,
      'encode',
      'config'
    );
  }
  return parsed;
}

Acceptance Criteria

  • Non-string dict keys throw explicit BridgeCodecError
  • bytes/bytearray values have clear error or explicit encoding
  • Pydantic model_dump failures surface with context
  • numpy/pandas scalars serialize correctly
  • Invalid env vars throw explicit errors
  • Adversarial tests cover all edge cases
  • All 6 related issues can be closed

Scope

This fix touches:

  • src/utils/codec.ts - edge case handling
  • src/runtime/errors.ts - BridgeCodecError type
  • runtime/python_bridge.py - Python-side encoding
  • test/adversarial_playground.test.ts - edge case tests

Metadata

Metadata

Assignees

Labels

area:codecArea: codecs and serializationenhancementNew feature or requestpriority:p1Priority P1 (high)

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions