Skip to content

refactor(runtime-host): extract Peer Mesh control message codecs #5005

Open

Description

@Totoro-qaq

Problem

This is a focused part of A9 — Runtime Host / peer mesh in #4726.

At 431ca4257a23860e2ecd6e438de7214b88fb542b, packages/runtime-host/src/peer-mesh/node.ts is 3,103 lines. Alongside membership, invitation and synchronization orchestration, it defines the control-message types and their object decoders. Those decoders already form a separate boundary: they consume values and return validated control messages, without accessing the node instance or performing stream I/O.

The existing seam consists of request/response types at lines 91–163, decoder functions at 2870–3059, and value-validation helpers at 3085–3103. Their callers already pass decoded results into the node's lifecycle methods.

Desired outcome

Extract that boundary into package-internal peer-mesh/control-protocol.ts:

  • the five request/response decoder entry points and their supporting types;
  • exact-key, string, evidence-summary and evidence-page validation;
  • one EVIDENCE_PAGE_SIZE definition shared by decoder checks and node pagination.

Keep existing signed-record decoders in their current modules and delegate to them unchanged. Node retains connection identity and membership decisions, freshness checks, state changes, persistence, cancellation, stream framing and timeouts. The codec has no dependency back to node, store or coordinator. Public package exports and wire shapes remain unchanged.

Measured local candidate

The prepared local candidate has the following measured result:

Metric Before After
node.ts lines 3,103 2,835
control-protocol.ts lines 0 317
Combined production lines 3,103 3,152

The composition module loses 268 lines. Aggregate production code grows by 49 lines for the new module header/imports and node's imports. This is the sweep's large-module seam-extraction result, not a net code-deletion or performance claim.

An AST comparison confirms that the 12 functions, 11 types and one constant are unchanged after stripping the new export wrappers and source locations. Every retained node declaration and original import is unchanged, and the emitted node.d.ts is byte-identical to baseline.

Verification

  • Existing Peer Mesh integration suite: 28/28 before and after, with its source file unchanged.
  • Nine new codec test groups cover valid variants, exact keys, bounds, nested signed-record validation and object ownership. Nine corresponding implementation mutations fail their tests; a tenth mutation fails an existing public node join test, confirming that node uses the extracted decoder.
  • Full Runtime Host suite: 1,818 passed, 12 skipped, 0 failed or cancelled on macOS with Node 24.18.0.
  • Full build, typecheck, lint/format, Desktop/UI knip, renderer architecture, ASF-header and applicable protocol checks pass locally.

Alternatives or workarounds

Leaving the private functions in node.ts preserves behavior but keeps message grammar coupled to a 3,000-line lifecycle module. A generic validation framework or additional coordinator classes would exceed this seam. Framing and transport extraction are not part of this change.

The ownership audit checked all file changes in 162 open PRs, 240 open Issue bodies, and related claim comments. No dedicated control-codec extraction was found. #4511's wait-helper change and #4520's store work are outside this slice.

Activity

  1. Totoro-qaq commented on Sep 8, 2026

    @Totoro-qaq
    ContributorAuthor

    take

  2. added
    enhancement
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions