refactor(engine): consolidate duplicated error/serialization/attachment helpers#200
Conversation
…Error helper
The engine already had a canonical `codedError(message, code, status)` helper in
lib/httpError.ts — and newer modules (node, action, placement, trigger,
inboundWebhook) used it — but ~45 older call sites still hand-rolled the
`new Error()` + `Object.assign(err, { code, status })` + `throw err` idiom, and
routing.ts/directory.ts each defined their own duplicate local `createError`.
Consolidate all of them onto the single helper so the coded-error contract has
one source of truth. Behavior is identical: codedError applies {code,status} via
Object.assign exactly as the inline idiom did. The two a2a.ts sites that attach
extra fields (retryable, data) are intentionally left raw.
Verified: build, 133 unit tests, lint (unchanged), and 107 live e2e checks pass.
…etWireJson) - Add lib/serialize.ts with a single `toIso` and replace the duplicate local copies in delivery.ts and routing.ts. - Drop action.ts's private `toFleetWireJson` (which was missing the `Date -> ISO` branch the canonical deliveryWire.ts version has) and import the complete implementation instead, removing duplication and a latent Date-serialization bug. - Remove the now-unused `ne` import in delivery.ts (clears the last lint warning). Verified: build, 133 unit tests, lint (0 warnings), and 107 live e2e checks pass.
…ments.ts delivery.ts, dm.ts, groupDm.ts and message.ts each carried a verbatim copy of the `AttachmentRow` type and a `fetchAttachmentsBatch` helper that joins message_attachments -> files and groups by message id. The copies had drifted: delivery.ts lacked the `position` ordering, and message.ts lacked both the `files.workspaceId` scope and the ordering. Extract a single shared helper that always scopes by workspace and orders by (message_id, position), and point all four modules at it. This removes the duplication and aligns every read path on the safest, most-correct variant — delivery payloads and channel/thread message lists now return attachments in a deterministic position order. Verified: build, 133 unit tests, lint (0 warnings), 107 live e2e checks, plus a manual upload->send->list check confirming attachments serialize with correct fields and position ordering.
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (18)
📝 WalkthroughWalkthroughThe PR centralizes two shared utilities: ChangesShared Helper Extraction and Engine-Wide Adoption
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed: one or more packages not found in the registry. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
An architecture-focused pass over
packages/enginethat eliminates genuine duplication and fixes a couple of latent inconsistencies, without churning the already-clean hexagonal structure. Three independently-verified commits.1. Single source of truth for coded errors
The engine already had a canonical
codedError(message, code, status)helper (inlib/httpError.ts), and newer modules used it — but ~45 older call sites still hand-rollednew Error()+Object.assign(err, { code, status })+throw err, androuting.ts/directory.tseach defined their own duplicate localcreateError. All consolidated onto the one helper. Behavior-identical; the twoa2a.tssites that attach extra fields (retryable,data) were intentionally left raw.2. Centralized wire-serialization helpers
lib/serialize.tswith a singletoIso, replacing the duplicate copies indelivery.tsandrouting.ts.action.ts's privatetoFleetWireJson, which was missing theDate → ISObranch the canonicaldeliveryWire.tsversion has (a latent serialization bug), in favor of the complete shared implementation.neimport (clears the last lint warning — lint is now 0 warnings).3. Consolidated attachment hydration
delivery.ts,dm.ts,groupDm.ts, andmessage.tseach carried a verbatim copy offetchAttachmentsBatch+ theAttachmentRowtype, and the copies had drifted:delivery.tslacked thepositionordering, andmessage.tslacked both thefiles.workspaceIdscope and the ordering. Extracted a singleengine/attachments.tshelper that always scopes by workspace and orders by(message_id, position), and pointed all four modules at it — aligning every read path on the safest, most-correct variant. Delivery payloads and channel/thread message lists now return attachments in deterministic position order.Net: −214 lines of duplicated code, one latent Date-serialization bug fixed, and attachment ordering made deterministic everywhere.
Verification (run after each step)
turbo build,turbo test(133 unit + conformance),turbo lint(now 0 warnings, was 1) — all greenIntentionally out of scope
engine/); fixing one site would be inconsistent churn. Noted as a possible follow-up.🤖 Generated with Claude Code
https://claude.ai/code/session_01WfmtpEM9NFvxEBxtsAAV1S
Generated by Claude Code