Skip to content

refactor(core): ResourceDeclarable sub-interface for props/attributes — DECIDED: option 2 #1049

Closed

Description

@lex00

Split out of #1047, which fixed the narrower LexiconOutput cast. This one is the wider design question and deliberately was not folded in.

The gap

Declarable (packages/core/src/declarable.ts:9) describes four things:

export interface Declarable {
  readonly lexicon: string;
  readonly entityType: string;
  readonly kind?: "resource" | "property" | "output";
  readonly [DECLARABLE_MARKER]: true;
}

It does not describe props or attributes — the fields that carry the actual resource payload and that 10 of 11 lexicon serializers read. So every consumer that needs them reaches through an unchecked cast. In packages/core/src/discovery/entity-wire.ts alone:

253:  const props = "props" in decl ? (decl as unknown as { props?: unknown }).props : undefined;
277:  if ("props" in entity && (entity as unknown as { props?: unknown }).props !== undefined) {
278:    wire.props = encodeValue((entity as unknown as { props: unknown }).props, entityNames);
280:  if ("attributes" in entity && (entity as unknown as { attributes?: unknown }).attributes !== undefined) {
281:    wire.attributes = encodeValue((entity as unknown as { attributes: unknown }).attributes, entityNames);

The "props" in entity guards are doing real work — not every Declarable has props — but the payload comes back as unknown and each call site re-invents both the guard and the shape.

Why it matters more now

Before #1046 this was ordinary structural typing in a few serializers. Now the entity-wire encoder — the thing that decides what crosses a process boundary for #1045 — decides what to serialize based on untyped in checks. A resource kind carrying its payload under a differently-named field would be silently dropped rather than caught by the compiler. The 98-entry differential would catch it only if a corpus entry happens to exercise that shape.

Options

  1. Widen Declarable with optional props?: unknown / attributes?: unknown. Smallest change, keeps one type, but weakens the interface for the genuinely payload-less kinds (outputs, parameters) and does not give real types.
  2. Add a ResourceDeclarable sub-interface carrying props/attributes, with a type guard replacing the ad-hoc "props" in x checks. Keeps Declarable minimal and honest, and gives one guard to maintain. More churn.
  3. Leave it, document it. Defensible — the casts work today and structural checks are idiomatic — but then the entity-wire encoder should at least route every access through one shared helper instead of five inline casts.

Option 2 looks right, but it touches all eleven lexicons, so it needs a deliberate decision rather than an opportunistic fix.

Non-goals

Acceptance

  • A decision recorded between the options above, with reasoning.
  • If 1 or 2: no as unknown as for props/attributes in entity-wire.ts, and lexicon serializers reading them through the shared type or guard.
  • If 3: a single shared accessor in entity-wire.ts rather than repeated inline casts, and a comment saying why the base interface stays minimal.
  • just build, just test, just lint green; the JSON-boundary differential still reports 98/98 identical; every touched lexicon passes npm run build -w @intentius/chant-lexicon-<name>.

Activity

  1. lex00 commented on Jul 26, 2026

    @lex00
    ContributorAuthor

    Still valid, but the code moved

    #1050 (Phase 2) split the encoder into packages/core/src/discovery/entity-wire-codec.ts so the sandboxed child's bundle would not drag in discover() → typescript. entity-wire.ts now has zero props references; every cast this issue describes lives in the codec.

    Updated locations (11 as unknown as casts in that file, 5 of them props/attributes):

    entity-wire-codec.ts:259  const props = "props" in decl ? (decl as unknown as { props?: unknown }).props : undefined;
    entity-wire-codec.ts:283  if ("props" in entity && (entity as unknown as { props?: unknown }).props !== undefined) {
    entity-wire-codec.ts:284    wire.props = encodeValue((entity as unknown as { props: unknown }).props, entityNames);
    entity-wire-codec.ts:286  if ("attributes" in entity && (entity as unknown as { attributes?: unknown }).attributes !== undefined) {
    entity-wire-codec.ts:287    wire.attributes = encodeValue((entity as unknown as { attributes: unknown }).attributes, entityNames);
    

    The argument for fixing it is now slightly stronger than when filed: this file is the encoder for two sandbox boundaries (resource entities and, by pattern, components), so a resource kind carrying its payload under a differently-named field is silently dropped from the wire rather than caught by the compiler.

    Priority unchanged — this is type hygiene, not a live defect, and it sits behind #1020 and #1044 on leverage.

  2. lex00 commented on Jul 27, 2026

    @lex00
    ContributorAuthor

    DECISION: option 2 — ResourceDeclarable sub-interface

    Declarable stays minimal and honest. Outputs and parameters genuinely have no payload, and widening the base interface with optional props?: unknown would lie about them while still giving no real type safety — it removes the casts without removing the guesswork.

    Shape:

    export interface Declarable {
      readonly lexicon: string;
      readonly entityType: string;
      readonly kind?: "resource" | "property" | "output";
      readonly [DECLARABLE_MARKER]: true;
    }
    
    export interface ResourceDeclarable extends Declarable {
      readonly props: unknown;
      readonly attributes?: unknown;
    }
    
    export function isResourceDeclarable(d: Declarable): d is ResourceDeclarable { ... }

    Every ad-hoc "props" in entity check becomes one guard, and the 5 as unknown as casts in entity-wire-codec.ts (lines ~259, 283–287) go away.

    Why this one and not the cheaper options

    The codec is now the encoder for a process boundary, not just a serializer helper. It decides what data crosses into and back out of the sandboxed child (#1045, #1051). A resource kind whose payload lives under a differently-named field is silently dropped from the wire rather than caught by the compiler, and the 98-entry differential only catches that if some corpus entry happens to exercise that shape.

    Centralizing the casts (option 3) fixes the repetition but leaves that failure mode intact. Widening Declarable (option 1) leaves it intact too, and additionally makes the interface wrong for the kinds that have no props.

    Scope and sequencing

    This touches 10 of the 11 lexicon serializers, so it is the widest blast radius of the three. Do it as a mechanical, single-purpose PR — no behaviour change, no opportunistic cleanups riding along.

    Sequence it after #1064 and #1067 land. Both are in flight and #1067 already touches packages/core/src/lexicon.ts; a wide serializer refactor landing underneath them would create conflicts for no benefit.

    Acceptance

    • No as unknown as for props/attributes anywhere in entity-wire-codec.ts.
    • Lexicon serializers read the payload through ResourceDeclarable / isResourceDeclarable, not structural in checks.
    • A resource kind with no props still type-checks and still round-trips.
    • Every touched lexicon passes npm run build -w @intentius/chant-lexicon-<name>.
    • Corpus fold coverage unchanged at 21/77; JSON-boundary and sandbox differentials still 98/98 and 97/1.
  3. changed the title [-]design(core): Declarable doesn't describe props/attributes, so every consumer casts[/-] [+]refactor(core): ResourceDeclarable sub-interface for props/attributes — DECIDED: option 2[/+] on Jul 27, 2026
  4. lex00 commented on Jul 27, 2026

    @lex00
    ContributorAuthor

    Shipped in PR #1092 — option 2 as decided: ResourceDeclarable + isResourceDeclarable guard, entity-wire-codec's five casts replaced, 10 serializers (plus forgejo's dialect.ts) routed through the guard. All differentials 98/98 identical, all lexicon builds green. The helm/github duck-typed pockets behind narrower guards are documented in the PR as out of scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions