fix(proto): emit state=0 on whole-ban unmute via new pb_optional marker

set_group_whole_ban false (unmute) failed with `OIDB error 1007` while
enabling worked (issue #70). OIDB (0x89A, 0) is shared by several commands
(SetSearch / SetAddOption / mute-all) disambiguated by body shape. Unmute
sends muteState.state=0, but proton omits zero scalars (proto3), leaving an
empty `muteState` the server can't classify. Enable sends 0xFFFFFFFF, which
is non-zero and survives — hence the asymmetry.

Add a `pb_optional<N, T>` marker (proto3 explicit presence): a scalar so
marked serialises even at its zero/default value. It is opt-in per field via
a NEW `explicitPresence` flag set ONLY by this marker — never reused from the
TS-`?`-derived `isOptional` (true for nearly every field). The encoder routes
explicit-presence scalars to a dedicated block; every `pb<>` / `pb_repeated<>`
field takes the untouched branches, so existing wire output is byte-identical
(proton's 99 existing tests, incl. byte-level round-trips, stay green).

Mark OidbMuteAllState.state `pb_optional<17, uint_32>` so unmute now puts
field 17 (=0) on the wire. New tests: proton pb_optional encode/round-trip
(incl. that sibling pb<> fields still omit zero), and a byte-level assertion
that mute-all unmute carries the state field.

Note: proton ships as a built Vite plugin (dist, gitignored); rebuild it
(`pnpm --filter @snowluma/proton build`) for the change to reach consumers.
This commit is contained in:
DrSmoothl committed 2026-05-30 17:55:23 +08:00
1 parent c4413dabb5
commit 90f26ca4f4
9 files changed
+304 -7

No files matched your search

+4 -2
View File
@@ -1,7 +1,7 @@
import ts from 'typescript';
import {
WireType,
PB_MARKER, PB_REPEATED_MARKER,
PB_MARKER, PB_REPEATED_MARKER, PB_OPTIONAL_MARKER,
type ProtobufField, type ProtobufMessage,
type GenericProtobufTemplate, type GenericFieldTemplate,
} from './types.js';
@@ -85,7 +85,7 @@ function parsePbTypeRef(
// Resolve the marker name through aliased imports so `import { pb as P }`
// / `import { pb_repeated as PR }` still match the marker constants.
const marker = resolveImportedTypeName(t.typeName.text);
if (marker !== PB_MARKER && marker !== PB_REPEATED_MARKER) return null;
if (marker !== PB_MARKER && marker !== PB_REPEATED_MARKER && marker !== PB_OPTIONAL_MARKER) return null;
const ta = t.typeArguments;
if (!ta || ta.length !== 2) return null;
const fnNode = ta[0];
@@ -129,6 +129,7 @@ function extractField(
isMessage: false, // placeholder
isOptional: member.questionToken != null,
isRepeated: parsed.marker === PB_REPEATED_MARKER,
explicitPresence: parsed.marker === PB_OPTIONAL_MARKER,
};
}
@@ -149,6 +150,7 @@ function extractGenericField(
isTypeParam: tpSet.has(raw),
isOptional: member.questionToken != null,
isRepeated: parsed.marker === PB_REPEATED_MARKER,
explicitPresence: parsed.marker === PB_OPTIONAL_MARKER,
};
// If the field's type-arg is itself a generic instantiation (`Wrapper<U>`,
// where U is a type param of the enclosing template), capture the original
+2
View File
@@ -89,6 +89,7 @@ export function monomorphizeTypeNode(
isMessage: false,
isOptional: f.isOptional,
isRepeated: f.isRepeated,
explicitPresence: f.explicitPresence,
};
}
return {
@@ -99,6 +100,7 @@ export function monomorphizeTypeNode(
isMessage: false,
isOptional: f.isOptional,
isRepeated: f.isRepeated,
explicitPresence: f.explicitPresence,
};
});
+11
View File
@@ -13,6 +13,14 @@ export interface ProtobufField {
isMessage: boolean;
isOptional: boolean;
isRepeated: boolean;
/**
* Explicit presence (proto3 `optional`), set only by the `pb_optional<>`
* marker. When true, a scalar field serialises even at its zero/default
* value. Distinct from `isOptional` (which merely tracks the TS `?` and is
* true for almost every field) — the encoder keys off THIS flag, never
* `isOptional`, so existing `pb<>` fields are unaffected.
*/
explicitPresence: boolean;
}
export interface ProtobufMessage {
@@ -34,6 +42,8 @@ export interface GenericFieldTemplate {
isTypeParam: boolean;
isOptional: boolean;
isRepeated: boolean;
/** Explicit presence (proto3 `optional`); see ProtobufField.explicitPresence. */
explicitPresence: boolean;
/**
* For fields whose type-arg is itself a generic instantiation
* (e.g. `wrapped: pb<5, Wrapper<U>>` where `U` is a template type
@@ -70,3 +80,4 @@ export const PRIMITIVE_TYPE_MAP: Record<string, { wireType: WireType; defaultVal
/** Marker identifiers recognised in type references */
export const PB_MARKER = 'pb';
export const PB_REPEATED_MARKER = 'pb_repeated';
export const PB_OPTIONAL_MARKER = 'pb_optional';
+223
View File
@@ -91,6 +91,14 @@ export function generateEncoder(msg: ProtobufMessage, _registry: MessageRegistry
function buildSingularBlock(field: ProtobufField, index: number): EncoderBlock {
const { name, fieldNumber, typeName, wireType, isMessage } = field;
// `pb_optional<>` scalar → serialise even at the zero value (proto3 explicit
// presence). Message fields already emit on `!= null`, so they stay on the
// default path; only scalars need the dedicated guard. Every `pb<>` /
// `pb_repeated<>` field has `explicitPresence === false` and so takes the
// untouched branches below — their generated code is byte-for-byte unchanged.
if (field.explicitPresence && !isMessage) {
return buildExplicitPresenceScalarBlock(field, index);
}
const valueVar = `_f${index}`;
const cacheVar = `_c${index}`;
const tagLength = computeTagBytes(fieldNumber, isMessage || typeName === 'string' || typeName === 'bytes' ? 2 : wireType).length;
@@ -318,6 +326,221 @@ function buildSingularBlock(field: ProtobufField, index: number): EncoderBlock {
return { declare: [], size: [], write: [] };
}
/**
* Encoder block for a singular scalar field marked `pb_optional<>` (proto3
* explicit presence). Identical to the default scalar paths EXCEPT the guard
* is `!= null` instead of `!= null && !== <default>`, so the field — and a
* `bool`'s real value — is written even when zero/false/empty. Reached only
* when `field.explicitPresence === true`, so default `pb<>` fields never run
* this code.
*/
function buildExplicitPresenceScalarBlock(field: ProtobufField, index: number): EncoderBlock {
const { name, fieldNumber, typeName, wireType } = field;
const valueVar = `_f${index}`;
const cacheVar = `_c${index}`;
const isLenDelim = typeName === 'string' || typeName === 'bytes';
const tagLength = computeTagBytes(fieldNumber, isLenDelim ? 2 : wireType).length;
const declareVal = ` const ${valueVar} = obj.${name};`;
const present = `${valueVar} != null`;
if (typeName === 'string') {
return {
declare: [declareVal, ` let ${cacheVar};`],
size: [
` if (${present}) {`,
` ${cacheVar} = __utf8Len(${valueVar});`,
` const _len = ${cacheVar};`,
` size += ${tagLength};`,
varintSize('_len', ' '),
` size += _len;`,
` }`,
],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 2, ' '),
` const _len = ${cacheVar};`,
writeVarint('_len', ' '),
` offset = __utf8Write(buf, offset, ${valueVar});`,
` }`,
],
};
}
if (typeName === 'bytes') {
return {
declare: [declareVal],
size: [
` if (${present}) {`,
` const _len = ${valueVar}.length;`,
` size += ${tagLength};`,
varintSize('_len', ' '),
` size += _len;`,
` }`,
],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 2, ' '),
` const _len = ${valueVar}.length;`,
writeVarint('_len', ' '),
` buf.set(${valueVar}, offset);`,
` offset += _len;`,
` }`,
],
};
}
if (typeName === 'bool') {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 1};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 0, ' '),
` buf[offset++] = ${valueVar} ? 1 : 0;`,
` }`,
],
};
}
if (isVarint64(typeName)) {
const bigintExpr = bigintVarintExpr(typeName, valueVar);
return {
declare: [declareVal],
size: [
` if (${present}) {`,
` const _val = ${bigintExpr};`,
` size += ${tagLength};`,
` size += __varint64Size(_val);`,
` }`,
],
write: [
` if (${present}) {`,
` const _val = ${bigintExpr};`,
writeTag(fieldNumber, 0, ' '),
` offset = __writeVarint64(buf, offset, _val);`,
` }`,
],
};
}
if (typeName === 'sint_32') {
return {
declare: [declareVal],
size: [
` if (${present}) {`,
` const _val = ((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0;`,
` size += ${tagLength};`,
varintSize('_val', ' '),
` }`,
],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 0, ' '),
writeVarint(`((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0`, ' '),
` }`,
],
};
}
if (wireType === WireType.Varint) {
return {
declare: [declareVal],
size: [
` if (${present}) {`,
` const _val = ${valueVar} >>> 0;`,
` size += ${tagLength};`,
varintSize('_val', ' '),
` }`,
],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 0, ' '),
writeVarint(`${valueVar} >>> 0`, ' '),
` }`,
],
};
}
if (typeName === 'float') {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 4};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 5, ' '),
` offset = __writeFloat32(buf, offset, ${valueVar});`,
` }`,
],
};
}
if (wireType === WireType.Bit32) {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 4};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 5, ' '),
` const _val = ${valueVar};`,
` buf[offset++] = _val & 0xff;`,
` buf[offset++] = (_val >> 8) & 0xff;`,
` buf[offset++] = (_val >> 16) & 0xff;`,
` buf[offset++] = (_val >> 24) & 0xff;`,
` }`,
],
};
}
if (typeName === 'double') {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 8};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 1, ' '),
` offset = __writeFloat64(buf, offset, ${valueVar});`,
` }`,
],
};
}
if (isFixed64BigInt(typeName)) {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 8};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 1, ' '),
` offset = __writeFixed64(buf, offset, ${valueVar});`,
` }`,
],
};
}
if (wireType === WireType.Bit64) {
return {
declare: [declareVal],
size: [` if (${present}) size += ${tagLength + 8};`],
write: [
` if (${present}) {`,
writeTag(fieldNumber, 1, ' '),
` const _val = ${valueVar};`,
` buf[offset++] = _val & 0xff;`,
` buf[offset++] = (_val >> 8) & 0xff;`,
` buf[offset++] = (_val >> 16) & 0xff;`,
` buf[offset++] = (_val >> 24) & 0xff;`,
` buf[offset++] = 0;`,
` buf[offset++] = 0;`,
` buf[offset++] = 0;`,
` buf[offset++] = 0;`,
` }`,
],
};
}
return { declare: [], size: [], write: [] };
}
function buildRepeatedBlock(field: ProtobufField, index: number): EncoderBlock {
const { name, fieldNumber, typeName, wireType, isMessage } = field;
const arrayVar = `_f${index}`;
+1
View File
@@ -25,6 +25,7 @@ function cloneField(field: ProtobufField): ProtobufField {
isMessage: field.isMessage,
isOptional: field.isOptional,
isRepeated: field.isRepeated,
explicitPresence: field.explicitPresence ?? false,
};
}