Files
AgentCompany/builtin-skills/software-engineer/clean-architecture/references/entity-value-objects.md
T
ZacharyZhang-NY e74dafcd36 Silence render-disposed spam, unbreak bundled skills, drop board wake
Three independently ugly bugs that all fed the same log flood:

* Electron's `WebFrameMain.send` writes every "Render frame was disposed"
  to stderr via `console.error`. When the worker fans a second
  agent-event past a reloading renderer, we produced thousands of
  identical lines and stalled the main loop on stderr I/O. Added a
  `mainFrame.detached` guard so the send is short-circuited before
  Electron's own catch block fires, and installed a targeted filter in
  `suppress-warnings.ts` (now actually imported from the entry point)
  so the specific `console.error("Error sending from webFrameMain:"…)`
  noise never reaches the terminal. Any other payload still surfaces.

* `dispatchAgentMessage` accepted the human-reader sentinel as a wake
  target when an agent replied to `__board__` — `wakeAgentIfPossible`
  then called `db.getAgent("__board__")`, threw, and the dispatcher
  kept repeating on every send. Strip `HUMAN_SENDER_ID` at
  `resolveMessageWakeTargets`, and keep a defense-in-depth guard in the
  heartbeat so a stray caller (future automation fan-out) cannot
  reintroduce the crash.

* Pi's skills loader rejects any SKILL.md whose frontmatter `name`
  diverges from its parent directory. The four bundled `my-writing-style`
  copies declared `name: writing-style-skill`, so every install emitted
  a warning and dropped the skill. They also lived as orphan gitlinks
  (mode 160000 without .gitmodules), and the global `*.md` gitignore
  hid every other SKILL.md from version control. Fixed the frontmatter
  to match the dir, converted the dangling submodules to plain trees,
  and allowlisted `builtin-skills/**/*.md` so a fresh clone carries the
  catalogue instead of silently losing it.
2026-04-20 08:13:48 -04:00

2.4 KiB

title, impact, impactDescription, tags
title impact impactDescription tags
Use Value Objects for Domain Concepts HIGH eliminates primitive obsession, enforces constraints entity, value-objects, domain, types

Use Value Objects for Domain Concepts

Replace primitive types with value objects that encapsulate validation and behavior. Value objects are immutable, compared by value, and self-validating.

Incorrect (primitive obsession):

class Customer {
  constructor(
    public id: string,
    public email: string,
    public phone: string,
    public postalCode: string
  ) {}

  changeEmail(newEmail: string): void {
    // Validation scattered or missing
    if (!newEmail.includes('@')) {
      throw new Error('Invalid email')
    }
    this.email = newEmail
  }
}

// Callers can pass any string
const customer = new Customer('123', 'not-an-email', '123', 'invalid')

Correct (value objects for domain concepts):

class Email {
  private constructor(private readonly value: string) {}

  static create(value: string): Email {
    if (!Email.isValid(value)) {
      throw new InvalidEmailError(value)
    }
    return new Email(value.toLowerCase())
  }

  private static isValid(value: string): boolean {
    return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)
  }

  equals(other: Email): boolean {
    return this.value === other.value
  }

  toString(): string {
    return this.value
  }
}

class PhoneNumber {
  private constructor(
    private readonly countryCode: string,
    private readonly number: string
  ) {}

  static create(countryCode: string, number: string): PhoneNumber {
    // Validation and normalization
    return new PhoneNumber(countryCode, number.replace(/\D/g, ''))
  }
}

class Customer {
  constructor(
    public readonly id: CustomerId,
    private email: Email,
    private phone: PhoneNumber
  ) {}

  changeEmail(newEmail: Email): void {
    this.email = newEmail  // Already validated
  }
}

// Compile-time type safety + runtime validation
const customer = new Customer(
  CustomerId.create('123'),
  Email.create('user@example.com'),
  PhoneNumber.create('+1', '555-1234')
)

Benefits:

  • Invalid values cannot exist in the system
  • Domain concepts explicitly named in code
  • Validation logic centralized and reusable
  • Type system prevents mixing up string parameters

Reference: Value Object Pattern