@frequencytalk/agent 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -494,9 +494,20 @@ declare class Phone {
494
494
  * application's problem; `misattributed` or `undecryptable` climbing is a key or announcement
495
495
  * problem; `installs` is the number that proves a rotation was survived.
496
496
  *
497
- * Wire version 1 is sent and received. Version 2 audio (one seal, the content key wrapped per
498
- * channel) is announced by capability `native-transport-v2` and is counted here as
499
- * `unsupportedVersion`; peers on v2 are the next step for this package (handoff).
497
+ * Wire version 1 is sent; versions 1 AND 2 are received (0.2.4). Contract §7c has a sender
498
+ * choose v2 from the container's accept list alone, never from who is listening, so every
499
+ * receiver on a v2 container must read it: a v1-only receiver hears nothing from a v2 talker
500
+ * (measured 2026-11-27, Switchboard for Apple to an SDK agent: 319 of 319 datagrams dropped).
501
+ * v2 is two packet kinds, told apart by the CLEARTEXT flag because the payload type is sealed:
502
+ * - a KEY PACKET (0x22, FLAG_CONTENT_KEY): the talkspurt's content key `K_s`, sealed under a
503
+ * carrying channel's key exactly as v1 audio is. Filed per (connection, stream) after the
504
+ * owner check, by generation (contentKeyId's low byte); current and previous are kept, so
505
+ * audio in flight across a mid-talkspurt rotation still opens;
506
+ * - AUDIO (0x02): sealed once under `K_s`; the header names the stream's HOME wire id and the
507
+ * generation. The home is learned from the first audio that opens under a stream's key, NOT
508
+ * from the announcement: `nativeStream.channelId` is the channel THIS listener matched on,
509
+ * which for a relay listener is the relay, not the sender's home. The opened streamId must
510
+ * be the stream whose key opened it, and the owner check applies to both kinds.
500
511
  */
501
512
  type MediaEvent = {
502
513
  type: string;
@@ -532,8 +543,12 @@ interface NativeResyncStats {
532
543
  interface NativeReceiveStats {
533
544
  /** Audio frames opened and attributed, handed to the listeners. */
534
545
  frames: number;
535
- /** Packets whose version byte this build does not read (wire v2). */
546
+ /** Packets whose version or flag bits this build does not read (a future version, or a shape no version defines). */
536
547
  unsupportedVersion: number;
548
+ /** Wire-v2 content keys installed from key packets (one per key packet that opened and was attributed). */
549
+ contentKeys: number;
550
+ /** Wire-v2 audio for which no content key is held yet: a listener joining mid-talkspurt hears from the next key packet (at most 200 ms). */
551
+ awaitingContentKey: number;
537
552
  /** Packets on a channel this session holds no key for. */
538
553
  unknownChannel: number;
539
554
  /** Packets that did not open under the channel key: a stale ring on one side. */
@@ -577,6 +592,8 @@ declare class NativeSession {
577
592
  private producing;
578
593
  private timestamp;
579
594
  private readonly owners;
595
+ /** `${connectionId}:${streamId}` -> that stream's content keys (wire v2). */
596
+ private readonly contentKeys;
580
597
  private readonly pending;
581
598
  /** The last 500 signalling events, oldest first. A probe reads them; `waitFor` searches them. */
582
599
  readonly events: MediaEvent[];
@@ -643,6 +660,18 @@ declare class NativeSession {
643
660
  sendOpusFrame(opus: Buffer, relayChannelIds?: string[]): void;
644
661
  stopTalking(): void;
645
662
  private onPacket;
663
+ /** A v2 key packet that opened under a channel key: attribute it, then file `K_s` under the stream's home. */
664
+ private onContentKey;
665
+ /**
666
+ * v2 audio: sealed under `K_s`, found by (connection, home wire id, generation). The stream bound
667
+ * to that home is tried first; a connection's streams whose home is not yet learned are tried
668
+ * after (one connection rarely carries more than one), and the first that opens with its own
669
+ * streamId inside binds the home.
670
+ */
671
+ private onV2Audio;
672
+ /** The owner check and hand-off every opened audio frame goes through, whichever wire version carried it. */
673
+ private deliver;
674
+ private forgetStream;
646
675
  /**
647
676
  * The backstop for a missed `nativeKeysRotated` (the node announces a rotation only once it
648
677
  * has a baseline to compare against, so a rotation inside its first poll window is never
@@ -751,8 +780,8 @@ declare const FRAME_SAMPLES: number;
751
780
  interface OpusCodec {
752
781
  encode(pcm: Int16Array): Buffer;
753
782
  decode(packet: Buffer): Int16Array;
754
- /** Packet-loss concealment for one missing frame. */
755
- conceal(): Int16Array;
783
+ /** Packet-loss concealment for a gap: `samples` at 48 kHz (default one 20 ms frame). */
784
+ conceal(samples?: number): Int16Array;
756
785
  close(): void;
757
786
  }
758
787
  declare function createOpus(): Promise<OpusCodec>;
package/dist/index.js CHANGED
@@ -4649,6 +4649,9 @@ var BlockedWordsSchema = z14.object({ words: z14.array(BlockedWordSchema).max(2e
4649
4649
 
4650
4650
  // ../shared/dist/nativeTransport.js
4651
4651
  var NATIVE_TRANSPORT_VERSION = 1;
4652
+ var NATIVE_TRANSPORT_WIRE_V2 = 2;
4653
+ var FLAG_CONTROL = 16;
4654
+ var FLAG_CONTENT_KEY = 32;
4652
4655
  var NODE_SEQUENCE_BASE = 1n << 63n;
4653
4656
  var HEADER = {
4654
4657
  /** version (low nibble) + flags (high nibble). */
@@ -4707,6 +4710,19 @@ var PAYLOAD = {
4707
4710
  OPUS_OFFSET: 9
4708
4711
  };
4709
4712
  var PAYLOAD_TYPE = { audio: 0, keepalive: 1, contentKey: 2, videoShard: 3, shareAudio: 4 };
4713
+ var CONTENT_KEY_PAYLOAD = {
4714
+ TYPE_OFFSET: 0,
4715
+ /** uint32. The stream whose audio `K_s` seals. */
4716
+ STREAM_ID_OFFSET: 1,
4717
+ /** uint32, 48 kHz. The talkspurt's timestamp when this key packet was sent; informational. */
4718
+ TIMESTAMP_OFFSET: 5,
4719
+ /** uint32. Monotonic per stream; its LOW BYTE is the generation an audio header carries. */
4720
+ CONTENT_KEY_ID_OFFSET: 9,
4721
+ /** 32 bytes: `K_s`, an AES-256-GCM key. */
4722
+ KEY_OFFSET: 13,
4723
+ LENGTH: 45
4724
+ };
4725
+ var SHARE_CHANNEL_ID = 4294967295;
4710
4726
  var VIDEO_SHARD = {
4711
4727
  TYPE_OFFSET: 0,
4712
4728
  STREAM_ID_OFFSET: 1,
@@ -4745,8 +4761,13 @@ var PROJECTS_BY_PLATFORM = {
4745
4761
  // than a rider on `broadcast` (a Windows lane) or `tuner_apple`, for the reason `broadcast` got
4746
4762
  // one: the board enforces one in-flight task per PROJECT, so a shared lane makes one app's claim
4747
4763
  // answer 409 AGENT_BUSY while the other holds the slot.
4764
+ // `switchboard_apple` is the macOS Switchboard (owner, 2026-11-27: "build switchboard for mac"):
4765
+ // a native build of the same product the Windows `switchboard` lane builds, developed here on the
4766
+ // Mac like `tuner_apple` and `broadcast_apple`. Its own lane rather than a rider on `switchboard`
4767
+ // (a Windows lane) or `switchboard_steamos` (the SteamOS group), for the one-in-flight reason
4768
+ // `broadcast_apple` records above. A Mac lane only: not in the Windows set, not in the SteamOS group.
4748
4769
  // The SteamOS group (`STEAMOS_PROJECTS`, above) is on both platforms since 2026-11-03.
4749
- mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", ...STEAMOS_PROJECTS],
4770
+ mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", "switchboard_apple", ...STEAMOS_PROJECTS],
4750
4771
  // `broadcast` is its own lane rather than a rider on `windows` (2026-08-31): the board
4751
4772
  // enforces one in-flight task per PROJECT, and `windows` already carries the Jacked
4752
4773
  // audio-driver work, so sharing it meant a Broadcast claim answered 409 AGENT_BUSY for
@@ -5125,6 +5146,8 @@ var NativeSession = class {
5125
5146
  producing = null;
5126
5147
  timestamp = 0;
5127
5148
  owners = /* @__PURE__ */ new Map();
5149
+ /** `${connectionId}:${streamId}` -> that stream's content keys (wire v2). */
5150
+ contentKeys = /* @__PURE__ */ new Map();
5128
5151
  pending = [];
5129
5152
  /** The last 500 signalling events, oldest first. A probe reads them; `waitFor` searches them. */
5130
5153
  events = [];
@@ -5136,7 +5159,7 @@ var NativeSession = class {
5136
5159
  /** The stale-key watch: consecutive packets that would not open since the last one that did, and when a refresh was last asked for. */
5137
5160
  staleRun = 0;
5138
5161
  lastStaleRefresh = 0;
5139
- receiveStats = { frames: 0, unsupportedVersion: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5162
+ receiveStats = { frames: 0, unsupportedVersion: 0, contentKeys: 0, awaitingContentKey: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5140
5163
  /** The node-issued datapath identity, 0 until `open()` has authenticated. */
5141
5164
  get connectionId() {
5142
5165
  return this.connection;
@@ -5238,7 +5261,7 @@ var NativeSession = class {
5238
5261
  this.owners.set(streamId, connectionId);
5239
5262
  this.options.onStream?.({ streamId, connectionId, producerUserId: event.producerUserId, producerCallsign: event.producerCallsign, channelId: event.channelId });
5240
5263
  }
5241
- if (event.type === "nativeStreamClosed") this.owners.delete(Number(event.streamId));
5264
+ if (event.type === "nativeStreamClosed") this.forgetStream(Number(event.streamId));
5242
5265
  if (event.type === "nativeKeysRotated") this.onKeysRotated(String(event.keysetId));
5243
5266
  for (const p of [...this.pending]) {
5244
5267
  if (p.type === event.type) {
@@ -5389,12 +5412,24 @@ var NativeSession = class {
5389
5412
  }
5390
5413
  onPacket(packet) {
5391
5414
  if (!this.native || packet.length < HEADER.LENGTH + AEAD.TAG_BYTES) return;
5392
- if ((packet[HEADER.VERSION_FLAGS_OFFSET] & 15) !== NATIVE_TRANSPORT_VERSION) {
5415
+ const byte0 = packet[HEADER.VERSION_FLAGS_OFFSET];
5416
+ const version = byte0 & 15;
5417
+ const flags = byte0 & 240;
5418
+ const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5419
+ const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5420
+ if (version === NATIVE_TRANSPORT_VERSION && flags === FLAG_CONTROL) {
5421
+ this.receiveStats.notAudio += 1;
5422
+ return;
5423
+ }
5424
+ if (version === NATIVE_TRANSPORT_WIRE_V2 && flags === 0 && channelNativeId !== SHARE_CHANNEL_ID) {
5425
+ this.onV2Audio(packet, connectionId, channelNativeId);
5426
+ return;
5427
+ }
5428
+ const keyPacket = version === NATIVE_TRANSPORT_WIRE_V2 && flags === FLAG_CONTENT_KEY;
5429
+ if (!keyPacket && !(version === NATIVE_TRANSPORT_VERSION && flags === 0)) {
5393
5430
  this.receiveStats.unsupportedVersion += 1;
5394
5431
  return;
5395
5432
  }
5396
- const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5397
- const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5398
5433
  const entry = Object.values(this.native.channelKeys.keys).find((k) => k.id === channelNativeId);
5399
5434
  if (!entry) {
5400
5435
  this.receiveStats.unknownChannel += 1;
@@ -5409,10 +5444,89 @@ var NativeSession = class {
5409
5444
  return;
5410
5445
  }
5411
5446
  this.staleRun = 0;
5447
+ if (keyPacket) {
5448
+ this.onContentKey(connectionId, body);
5449
+ return;
5450
+ }
5412
5451
  if (body.type !== PAYLOAD_TYPE.audio) {
5413
5452
  this.receiveStats.notAudio += 1;
5414
5453
  return;
5415
5454
  }
5455
+ this.deliver(connectionId, body);
5456
+ }
5457
+ /** A v2 key packet that opened under a channel key: attribute it, then file `K_s` under the stream's home. */
5458
+ onContentKey(connectionId, body) {
5459
+ const rest = body.opus;
5460
+ const restLength = CONTENT_KEY_PAYLOAD.LENGTH - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET;
5461
+ if (body.type !== PAYLOAD_TYPE.contentKey || rest.length !== restLength) {
5462
+ this.receiveStats.notAudio += 1;
5463
+ return;
5464
+ }
5465
+ const owner = this.owners.get(body.streamId);
5466
+ if (owner === void 0 || owner !== connectionId) {
5467
+ this.receiveStats.misattributed += 1;
5468
+ return;
5469
+ }
5470
+ const contentKeyId = rest.readUInt32BE(0);
5471
+ const key = Buffer.from(rest.subarray(CONTENT_KEY_PAYLOAD.KEY_OFFSET - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET));
5472
+ const slot = `${connectionId}:${body.streamId}`;
5473
+ let stream = this.contentKeys.get(slot);
5474
+ if (!stream) {
5475
+ stream = { connectionId, streamId: body.streamId, generations: /* @__PURE__ */ new Map() };
5476
+ this.contentKeys.set(slot, stream);
5477
+ }
5478
+ const generation = contentKeyId & 255;
5479
+ const held = stream.generations.get(generation);
5480
+ if (held && held.contentKeyId === contentKeyId) return;
5481
+ stream.generations.delete(generation);
5482
+ stream.generations.set(generation, { contentKeyId, key });
5483
+ while (stream.generations.size > 2) stream.generations.delete(stream.generations.keys().next().value);
5484
+ this.receiveStats.contentKeys += 1;
5485
+ }
5486
+ /**
5487
+ * v2 audio: sealed under `K_s`, found by (connection, home wire id, generation). The stream bound
5488
+ * to that home is tried first; a connection's streams whose home is not yet learned are tried
5489
+ * after (one connection rarely carries more than one), and the first that opens with its own
5490
+ * streamId inside binds the home.
5491
+ */
5492
+ onV2Audio(packet, connectionId, homeWireId) {
5493
+ const generation = packet[HEADER.KEY_EPOCH_OFFSET];
5494
+ const candidates = [];
5495
+ for (const entry of this.contentKeys.values()) {
5496
+ if (entry.connectionId !== connectionId || !entry.generations.has(generation)) continue;
5497
+ if (entry.homeWireId === homeWireId) candidates.unshift(entry);
5498
+ else if (entry.homeWireId === void 0) candidates.push(entry);
5499
+ }
5500
+ if (candidates.length === 0) {
5501
+ this.receiveStats.awaitingContentKey += 1;
5502
+ return;
5503
+ }
5504
+ for (const stream of candidates) {
5505
+ let body;
5506
+ try {
5507
+ body = open(packet, stream.generations.get(generation).key);
5508
+ } catch {
5509
+ continue;
5510
+ }
5511
+ if (body.type !== PAYLOAD_TYPE.audio) {
5512
+ this.receiveStats.notAudio += 1;
5513
+ return;
5514
+ }
5515
+ if (body.streamId !== stream.streamId) {
5516
+ this.receiveStats.misattributed += 1;
5517
+ return;
5518
+ }
5519
+ if (stream.homeWireId === void 0) {
5520
+ for (const [slot, other] of this.contentKeys) if (other !== stream && other.connectionId === connectionId && other.homeWireId === homeWireId) this.contentKeys.delete(slot);
5521
+ stream.homeWireId = homeWireId;
5522
+ }
5523
+ this.deliver(connectionId, body);
5524
+ return;
5525
+ }
5526
+ this.receiveStats.undecryptable += 1;
5527
+ }
5528
+ /** The owner check and hand-off every opened audio frame goes through, whichever wire version carried it. */
5529
+ deliver(connectionId, body) {
5416
5530
  const owner = this.owners.get(body.streamId);
5417
5531
  if (owner === void 0 || owner !== connectionId) {
5418
5532
  this.receiveStats.misattributed += 1;
@@ -5423,6 +5537,10 @@ var NativeSession = class {
5423
5537
  this.options.onAudio?.(frame);
5424
5538
  for (const listener of this.audioListeners) listener(frame);
5425
5539
  }
5540
+ forgetStream(streamId) {
5541
+ this.owners.delete(streamId);
5542
+ for (const [slot, entry] of this.contentKeys) if (entry.streamId === streamId) this.contentKeys.delete(slot);
5543
+ }
5426
5544
  /**
5427
5545
  * The backstop for a missed `nativeKeysRotated` (the node announces a rotation only once it
5428
5546
  * has a baseline to compare against, so a rotation inside its first poll window is never
@@ -5489,7 +5607,7 @@ async function createOpus() {
5489
5607
  return {
5490
5608
  encode: (pcm) => enc.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength)),
5491
5609
  decode: (packet) => toInt16(enc.decode(packet)),
5492
- conceal: () => new Int16Array(FRAME_SAMPLES),
5610
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5493
5611
  close: () => enc.delete?.()
5494
5612
  };
5495
5613
  } catch {
@@ -5502,7 +5620,7 @@ async function createOpus() {
5502
5620
  return {
5503
5621
  encode: (pcm) => codec.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength), FRAME_SAMPLES),
5504
5622
  decode: (packet) => toInt16(codec.decode(packet)),
5505
- conceal: () => new Int16Array(FRAME_SAMPLES),
5623
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5506
5624
  close: () => codec.delete()
5507
5625
  };
5508
5626
  }
@@ -5651,15 +5769,17 @@ var Listener = class {
5651
5769
  if (!this.options.includeOwn && frame.connectionId === this.session.connectionId) return;
5652
5770
  let s = this.streams.get(frame.streamId);
5653
5771
  if (!s) {
5654
- s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, first: frame.timestamp };
5772
+ s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, lastSamples: 0, first: frame.timestamp };
5655
5773
  this.streams.set(frame.streamId, s);
5656
5774
  }
5657
5775
  if (s.lastTimestamp !== null) {
5658
- const missing = Math.min(50, Math.round((frame.timestamp - s.lastTimestamp) / FRAME_SAMPLES) - 1);
5659
- for (let i = 0; i < missing; i++) s.chunks.push(codec.conceal());
5776
+ const gap = (frame.timestamp - s.lastTimestamp >>> 0) - s.lastSamples;
5777
+ if (gap > 0 && gap <= 48e3) s.chunks.push(codec.conceal(gap));
5660
5778
  }
5661
- s.chunks.push(codec.decode(frame.opus));
5779
+ const pcm = codec.decode(frame.opus);
5780
+ s.chunks.push(pcm);
5662
5781
  s.lastTimestamp = frame.timestamp;
5782
+ s.lastSamples = pcm.length;
5663
5783
  };
5664
5784
  this.detach = this.session.attachAudio(handler);
5665
5785
  }