Skip to content

refactor: code quality cleanup — DRY schemas, consolidate dispatch, remove dead code#46

Merged
varin-nair-factory merged 1 commit into
mainfrom
vn/code-quality-cleanup
May 22, 2026
Merged

refactor: code quality cleanup — DRY schemas, consolidate dispatch, remove dead code#46
varin-nair-factory merged 1 commit into
mainfrom
vn/code-quality-cleanup

Conversation

@varin-nair-factory
Copy link
Copy Markdown
Contributor

Summary

Targeted code quality improvements identified through systematic analysis (knip, madge, manual audit). Net result: -70 lines, 11 new tests, zero behavior changes.

Changes

DRY: Response schema factory (src/schemas/shared.ts, client.ts, server.ts)

  • Added createResponseSchema(resultSchema) helper
  • Replaced ~26 identical z.union([JsonRpcResponseSuccessSchema.extend({result: X}), JsonRpcResponseFailureSchema]) blocks
  • ~150 lines of mechanical repetition eliminated with zero type safety loss

Consolidate notification dispatch (protocol.ts, client.ts)

  • Extracted dispatchNotification() from duplicated logic in ProtocolEngine and DroidClient
  • Both had identical filter-matching + error-swallowing listener dispatch (~25 lines each)
  • Exported as public API for SDK consumers

Remove dead code (session.ts, stream.ts)

  • Removed aggregateMessages() — orphaned after turn API simplification, zero references (knip-verified)
  • Removed isPartialMessage() — orphaned after streaming result API refactor, zero references (knip-verified)

Fix misleading annotation (constants.ts)

  • LEGACY_FACTORY_API_VERSION was marked @deprecated but is required in every JSON-RPC envelope
  • Replaced with accurate docs: "frozen wire protocol value, must not be changed"

Remove noise comments (enums.ts, client.ts, server.ts, mcp.ts)

  • Removed ~70 JSDoc comments that restate the type name (e.g., /** Tool result notification. */ on ToolResultNotificationSchema)
  • Kept ~10 comments that add genuine context (streaming semantics, direction arrows, domain clarification)
  • Removed stale internal monorepo path references from enums.ts header

New tests (tests/code-quality.test.ts)

  • createResponseSchema: valid success/failure parsing, rejection of wrong shapes, equivalence with hand-written schemas
  • dispatchNotification: filter matching, error swallowing (listener throws without crashing), unparseable notification handling

Validation

  • npm test — 646 tests pass (including 11 new)
  • npm run typecheck — clean
  • npm run lint — clean
  • npm run format:check — clean

…emove dead code

- Add createResponseSchema() factory to eliminate ~26 repeated z.union() blocks
- Extract shared dispatchNotification() from duplicated protocol/client logic
- Remove orphaned aggregateMessages() and isPartialMessage() (knip-verified)
- Fix misleading @deprecated on LEGACY_FACTORY_API_VERSION (it's required by wire protocol)
- Remove ~70 name-restating JSDoc comments that add no information beyond type names
- Remove stale internal monorepo path references from enums.ts header
- Add 11 tests for createResponseSchema and dispatchNotification

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@varin-nair-factory varin-nair-factory merged commit 40d7ada into main May 22, 2026
4 checks passed
@varin-nair-factory varin-nair-factory deleted the vn/code-quality-cleanup branch May 22, 2026 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants