Files
AgentCompany/builtin-skills/software-engineer/clean-architecture/references/usecase-transaction-boundary.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.6 KiB

title, impact, impactDescription, tags
title impact impactDescription tags
Use Case Defines the Transaction Boundary HIGH ensures data consistency, prevents partial updates usecase, transactions, consistency, boundaries

Use Case Defines the Transaction Boundary

The use case is the natural transaction boundary. All operations within a use case should succeed or fail atomically. Infrastructure manages the transaction; use case defines the scope.

Incorrect (transaction managed inside repository):

class OrderRepository:
    def save(self, order):
        with self.db.transaction():  # Transaction per operation
            self.db.insert('orders', order)

class InventoryRepository:
    def reserve(self, items):
        with self.db.transaction():  # Separate transaction
            for item in items:
                self.db.update('inventory', item.sku, decrement=item.qty)

class PlaceOrderUseCase:
    def execute(self, command):
        order = Order.create(command.items)
        self.orders.save(order)  # Commits
        self.inventory.reserve(command.items)  # If this fails, order is orphaned!
        self.payments.charge(order.total)  # If this fails, inventory is reserved but unpaid

Correct (use case defines transaction boundary):

# application/ports/UnitOfWork.py
class UnitOfWork(Protocol):
    def begin(self) -> None: ...
    def commit(self) -> None: ...
    def rollback(self) -> None: ...

# application/usecases/PlaceOrderUseCase.py
class PlaceOrderUseCase:
    def __init__(self, uow: UnitOfWork, orders, inventory, payments):
        self.uow = uow
        self.orders = orders
        self.inventory = inventory
        self.payments = payments

    def execute(self, command):
        self.uow.begin()
        try:
            order = Order.create(command.items)

            self.inventory.reserve(command.items)
            self.payments.charge(order.total)
            self.orders.save(order)

            self.uow.commit()  # All or nothing
            return order.id

        except Exception as e:
            self.uow.rollback()  # Clean rollback
            raise

# Alternative using context manager
class PlaceOrderUseCase:
    def execute(self, command):
        with self.uow:  # Transaction spans entire use case
            order = Order.create(command.items)
            self.inventory.reserve(command.items)
            self.payments.charge(order.total)
            self.orders.save(order)
            return order.id

Note: For distributed systems, consider saga patterns or eventual consistency rather than distributed transactions.

Reference: Unit of Work Pattern