@frequencytalk/agent 0.2.2 → 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.
@@ -3940,7 +3940,12 @@ var MediaServerToClientSchema = z11.discriminatedUnion("type", [
3940
3940
  // fleet upgrade: a running container keeps its image until it is recycled. A sender
3941
3941
  // uses wire v2 only when 2 is listed. ABSENT means [1] (a container from before 0.107.0),
3942
3942
  // which is why the field is optional: its absence is the signal, not a missing value.
3943
- wireVersions: z11.array(z11.number().int().positive()).optional()
3943
+ wireVersions: z11.array(z11.number().int().positive()).optional(),
3944
+ // This container's datapath answers link-probe control packets (nativeTransport.ts,
3945
+ // LINK_PROBE; contract §19 "The link meter"). Negotiated for the same reason as
3946
+ // wireVersions: a container that does not know the packet drops it on shape, silently,
3947
+ // and a client that probed anyway would read that silence as a dead link. Absent means no.
3948
+ linkProbe: z11.boolean().optional()
3944
3949
  }).optional()
3945
3950
  }),
3946
3951
  z11.object({ type: z11.literal("authError"), message: z11.string() }),
@@ -4360,6 +4365,19 @@ var ERROR_CODES = {
4360
4365
  },
4361
4366
  // --- Missing resources --------------------------------------------------------------
4362
4367
  NOT_FOUND: { status: 404, summary: "The addressed resource does not exist, or is not visible to this caller." },
4368
+ // --- Internal: where media runs (admin console only; docs/work-orders/cost-controls.md) ---
4369
+ HOST_ADMIN_REFUSED: {
4370
+ status: 409,
4371
+ summary: "A provider or media-host change was refused: a disabled provider, one with no active host, a host with live containers, or an on-demand host (provisioner/hostAdmin.ts). The message says which."
4372
+ },
4373
+ SWING_REFUSED: {
4374
+ status: 409,
4375
+ summary: "A live frequency could not be moved to another media host: no running container, a swing already in progress, no other active host, or the new container did not come up (provisioner/swing.ts). The message says which."
4376
+ },
4377
+ STANDBY_REFUSED: {
4378
+ status: 409,
4379
+ summary: "An on-demand standby media host could not be started or stopped in its current state (provisioner/standby.ts). The message says why."
4380
+ },
4363
4381
  USER_NOT_FOUND: { status: 404, summary: "No user matches the supplied handle, Station ID or email." },
4364
4382
  INVITE_INVALID: { status: 404, summary: "The invite or invite link is unknown, expired, revoked, or fully used." },
4365
4383
  NO_BILLING_ACCOUNT: { status: 404, summary: "No billing account exists for this organization yet." },
@@ -4635,6 +4653,9 @@ var BlockedWordsSchema = z14.object({ words: z14.array(BlockedWordSchema).max(2e
4635
4653
 
4636
4654
  // ../shared/dist/nativeTransport.js
4637
4655
  var NATIVE_TRANSPORT_VERSION = 1;
4656
+ var NATIVE_TRANSPORT_WIRE_V2 = 2;
4657
+ var FLAG_CONTROL = 16;
4658
+ var FLAG_CONTENT_KEY = 32;
4638
4659
  var NODE_SEQUENCE_BASE = 1n << 63n;
4639
4660
  var HEADER = {
4640
4661
  /** version (low nibble) + flags (high nibble). */
@@ -4693,6 +4714,19 @@ var PAYLOAD = {
4693
4714
  OPUS_OFFSET: 9
4694
4715
  };
4695
4716
  var PAYLOAD_TYPE = { audio: 0, keepalive: 1, contentKey: 2, videoShard: 3, shareAudio: 4 };
4717
+ var CONTENT_KEY_PAYLOAD = {
4718
+ TYPE_OFFSET: 0,
4719
+ /** uint32. The stream whose audio `K_s` seals. */
4720
+ STREAM_ID_OFFSET: 1,
4721
+ /** uint32, 48 kHz. The talkspurt's timestamp when this key packet was sent; informational. */
4722
+ TIMESTAMP_OFFSET: 5,
4723
+ /** uint32. Monotonic per stream; its LOW BYTE is the generation an audio header carries. */
4724
+ CONTENT_KEY_ID_OFFSET: 9,
4725
+ /** 32 bytes: `K_s`, an AES-256-GCM key. */
4726
+ KEY_OFFSET: 13,
4727
+ LENGTH: 45
4728
+ };
4729
+ var SHARE_CHANNEL_ID = 4294967295;
4696
4730
  var VIDEO_SHARD = {
4697
4731
  TYPE_OFFSET: 0,
4698
4732
  STREAM_ID_OFFSET: 1,
@@ -4731,8 +4765,13 @@ var PROJECTS_BY_PLATFORM = {
4731
4765
  // than a rider on `broadcast` (a Windows lane) or `tuner_apple`, for the reason `broadcast` got
4732
4766
  // one: the board enforces one in-flight task per PROJECT, so a shared lane makes one app's claim
4733
4767
  // answer 409 AGENT_BUSY while the other holds the slot.
4768
+ // `switchboard_apple` is the macOS Switchboard (owner, 2026-11-27: "build switchboard for mac"):
4769
+ // a native build of the same product the Windows `switchboard` lane builds, developed here on the
4770
+ // Mac like `tuner_apple` and `broadcast_apple`. Its own lane rather than a rider on `switchboard`
4771
+ // (a Windows lane) or `switchboard_steamos` (the SteamOS group), for the one-in-flight reason
4772
+ // `broadcast_apple` records above. A Mac lane only: not in the Windows set, not in the SteamOS group.
4734
4773
  // The SteamOS group (`STEAMOS_PROJECTS`, above) is on both platforms since 2026-11-03.
4735
- mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", ...STEAMOS_PROJECTS],
4774
+ mac: ["frequency", "tuner_apple", "site", "platform", "broadcast_apple", "switchboard_apple", ...STEAMOS_PROJECTS],
4736
4775
  // `broadcast` is its own lane rather than a rider on `windows` (2026-08-31): the board
4737
4776
  // enforces one in-flight task per PROJECT, and `windows` already carries the Jacked
4738
4777
  // audio-driver work, so sharing it meant a Broadcast claim answered 409 AGENT_BUSY for
@@ -4865,6 +4904,10 @@ var KNOWN_SHARD_FLAGS = VIDEO_SHARD_FLAG.keyframe | VIDEO_SHARD_FLAG.recoveryPoi
4865
4904
  // ../shared/dist/share/assembler.js
4866
4905
  var MAX_FORWARD_JUMP = 1 << 16;
4867
4906
 
4907
+ // ../shared/dist/share/linkProbe.js
4908
+ var LINK_PROBE_PAD_DATAGRAM_BYTES = SHARE_WEBTRANSPORT_DATAGRAM_MAX_BYTES;
4909
+ var PAD_PAYLOAD_BYTES = LINK_PROBE_PAD_DATAGRAM_BYTES - HEADER.LENGTH;
4910
+
4868
4911
  // ../shared/dist/share/rateControl.js
4869
4912
  var MOTION_RUNGS = [2160, 1440, 1080, 900, 720, 540];
4870
4913
  var FRAME_BYTES_TARGET = Math.floor(MAX_FRAME_BYTES * 0.75);
@@ -5107,6 +5150,8 @@ var NativeSession = class {
5107
5150
  producing = null;
5108
5151
  timestamp = 0;
5109
5152
  owners = /* @__PURE__ */ new Map();
5153
+ /** `${connectionId}:${streamId}` -> that stream's content keys (wire v2). */
5154
+ contentKeys = /* @__PURE__ */ new Map();
5110
5155
  pending = [];
5111
5156
  /** The last 500 signalling events, oldest first. A probe reads them; `waitFor` searches them. */
5112
5157
  events = [];
@@ -5118,7 +5163,7 @@ var NativeSession = class {
5118
5163
  /** The stale-key watch: consecutive packets that would not open since the last one that did, and when a refresh was last asked for. */
5119
5164
  staleRun = 0;
5120
5165
  lastStaleRefresh = 0;
5121
- receiveStats = { frames: 0, unsupportedVersion: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5166
+ receiveStats = { frames: 0, unsupportedVersion: 0, contentKeys: 0, awaitingContentKey: 0, unknownChannel: 0, undecryptable: 0, notAudio: 0, misattributed: 0 };
5122
5167
  /** The node-issued datapath identity, 0 until `open()` has authenticated. */
5123
5168
  get connectionId() {
5124
5169
  return this.connection;
@@ -5220,7 +5265,7 @@ var NativeSession = class {
5220
5265
  this.owners.set(streamId, connectionId);
5221
5266
  this.options.onStream?.({ streamId, connectionId, producerUserId: event.producerUserId, producerCallsign: event.producerCallsign, channelId: event.channelId });
5222
5267
  }
5223
- if (event.type === "nativeStreamClosed") this.owners.delete(Number(event.streamId));
5268
+ if (event.type === "nativeStreamClosed") this.forgetStream(Number(event.streamId));
5224
5269
  if (event.type === "nativeKeysRotated") this.onKeysRotated(String(event.keysetId));
5225
5270
  for (const p of [...this.pending]) {
5226
5271
  if (p.type === event.type) {
@@ -5354,6 +5399,7 @@ var NativeSession = class {
5354
5399
  }
5355
5400
  /** One 20 ms Opus frame onto the stream: sealed under the home channel, and each relay, as the contract requires. */
5356
5401
  sendOpusFrame(opus, relayChannelIds = []) {
5402
+ if (this.closedWith) throw new Error(`native: signalling closed (${this.closedWith.code} ${this.closedWith.reason}) while talking; close() and open() again`);
5357
5403
  if (!this.producing) throw new Error("native: not talking; call startTalking first");
5358
5404
  const payload = audioPayload(this.producing.streamId, this.timestamp, opus);
5359
5405
  this.timestamp = this.timestamp + 960 >>> 0;
@@ -5361,17 +5407,33 @@ var NativeSession = class {
5361
5407
  }
5362
5408
  stopTalking() {
5363
5409
  if (!this.producing) return;
5410
+ if (this.closedWith) {
5411
+ this.producing = null;
5412
+ return;
5413
+ }
5364
5414
  this.send({ type: "closeNativeStream", streamId: this.producing.streamId });
5365
5415
  this.producing = null;
5366
5416
  }
5367
5417
  onPacket(packet) {
5368
5418
  if (!this.native || packet.length < HEADER.LENGTH + AEAD.TAG_BYTES) return;
5369
- if ((packet[HEADER.VERSION_FLAGS_OFFSET] & 15) !== NATIVE_TRANSPORT_VERSION) {
5419
+ const byte0 = packet[HEADER.VERSION_FLAGS_OFFSET];
5420
+ const version = byte0 & 15;
5421
+ const flags = byte0 & 240;
5422
+ const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5423
+ const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5424
+ if (version === NATIVE_TRANSPORT_VERSION && flags === FLAG_CONTROL) {
5425
+ this.receiveStats.notAudio += 1;
5426
+ return;
5427
+ }
5428
+ if (version === NATIVE_TRANSPORT_WIRE_V2 && flags === 0 && channelNativeId !== SHARE_CHANNEL_ID) {
5429
+ this.onV2Audio(packet, connectionId, channelNativeId);
5430
+ return;
5431
+ }
5432
+ const keyPacket = version === NATIVE_TRANSPORT_WIRE_V2 && flags === FLAG_CONTENT_KEY;
5433
+ if (!keyPacket && !(version === NATIVE_TRANSPORT_VERSION && flags === 0)) {
5370
5434
  this.receiveStats.unsupportedVersion += 1;
5371
5435
  return;
5372
5436
  }
5373
- const connectionId = packet.readUInt32BE(HEADER.CONNECTION_ID_OFFSET);
5374
- const channelNativeId = packet.readUInt32BE(HEADER.CHANNEL_ID_OFFSET);
5375
5437
  const entry = Object.values(this.native.channelKeys.keys).find((k) => k.id === channelNativeId);
5376
5438
  if (!entry) {
5377
5439
  this.receiveStats.unknownChannel += 1;
@@ -5386,10 +5448,89 @@ var NativeSession = class {
5386
5448
  return;
5387
5449
  }
5388
5450
  this.staleRun = 0;
5451
+ if (keyPacket) {
5452
+ this.onContentKey(connectionId, body);
5453
+ return;
5454
+ }
5389
5455
  if (body.type !== PAYLOAD_TYPE.audio) {
5390
5456
  this.receiveStats.notAudio += 1;
5391
5457
  return;
5392
5458
  }
5459
+ this.deliver(connectionId, body);
5460
+ }
5461
+ /** A v2 key packet that opened under a channel key: attribute it, then file `K_s` under the stream's home. */
5462
+ onContentKey(connectionId, body) {
5463
+ const rest = body.opus;
5464
+ const restLength = CONTENT_KEY_PAYLOAD.LENGTH - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET;
5465
+ if (body.type !== PAYLOAD_TYPE.contentKey || rest.length !== restLength) {
5466
+ this.receiveStats.notAudio += 1;
5467
+ return;
5468
+ }
5469
+ const owner = this.owners.get(body.streamId);
5470
+ if (owner === void 0 || owner !== connectionId) {
5471
+ this.receiveStats.misattributed += 1;
5472
+ return;
5473
+ }
5474
+ const contentKeyId = rest.readUInt32BE(0);
5475
+ const key = Buffer.from(rest.subarray(CONTENT_KEY_PAYLOAD.KEY_OFFSET - CONTENT_KEY_PAYLOAD.CONTENT_KEY_ID_OFFSET));
5476
+ const slot = `${connectionId}:${body.streamId}`;
5477
+ let stream = this.contentKeys.get(slot);
5478
+ if (!stream) {
5479
+ stream = { connectionId, streamId: body.streamId, generations: /* @__PURE__ */ new Map() };
5480
+ this.contentKeys.set(slot, stream);
5481
+ }
5482
+ const generation = contentKeyId & 255;
5483
+ const held = stream.generations.get(generation);
5484
+ if (held && held.contentKeyId === contentKeyId) return;
5485
+ stream.generations.delete(generation);
5486
+ stream.generations.set(generation, { contentKeyId, key });
5487
+ while (stream.generations.size > 2) stream.generations.delete(stream.generations.keys().next().value);
5488
+ this.receiveStats.contentKeys += 1;
5489
+ }
5490
+ /**
5491
+ * v2 audio: sealed under `K_s`, found by (connection, home wire id, generation). The stream bound
5492
+ * to that home is tried first; a connection's streams whose home is not yet learned are tried
5493
+ * after (one connection rarely carries more than one), and the first that opens with its own
5494
+ * streamId inside binds the home.
5495
+ */
5496
+ onV2Audio(packet, connectionId, homeWireId) {
5497
+ const generation = packet[HEADER.KEY_EPOCH_OFFSET];
5498
+ const candidates = [];
5499
+ for (const entry of this.contentKeys.values()) {
5500
+ if (entry.connectionId !== connectionId || !entry.generations.has(generation)) continue;
5501
+ if (entry.homeWireId === homeWireId) candidates.unshift(entry);
5502
+ else if (entry.homeWireId === void 0) candidates.push(entry);
5503
+ }
5504
+ if (candidates.length === 0) {
5505
+ this.receiveStats.awaitingContentKey += 1;
5506
+ return;
5507
+ }
5508
+ for (const stream of candidates) {
5509
+ let body;
5510
+ try {
5511
+ body = open(packet, stream.generations.get(generation).key);
5512
+ } catch {
5513
+ continue;
5514
+ }
5515
+ if (body.type !== PAYLOAD_TYPE.audio) {
5516
+ this.receiveStats.notAudio += 1;
5517
+ return;
5518
+ }
5519
+ if (body.streamId !== stream.streamId) {
5520
+ this.receiveStats.misattributed += 1;
5521
+ return;
5522
+ }
5523
+ if (stream.homeWireId === void 0) {
5524
+ for (const [slot, other] of this.contentKeys) if (other !== stream && other.connectionId === connectionId && other.homeWireId === homeWireId) this.contentKeys.delete(slot);
5525
+ stream.homeWireId = homeWireId;
5526
+ }
5527
+ this.deliver(connectionId, body);
5528
+ return;
5529
+ }
5530
+ this.receiveStats.undecryptable += 1;
5531
+ }
5532
+ /** The owner check and hand-off every opened audio frame goes through, whichever wire version carried it. */
5533
+ deliver(connectionId, body) {
5393
5534
  const owner = this.owners.get(body.streamId);
5394
5535
  if (owner === void 0 || owner !== connectionId) {
5395
5536
  this.receiveStats.misattributed += 1;
@@ -5400,6 +5541,10 @@ var NativeSession = class {
5400
5541
  this.options.onAudio?.(frame);
5401
5542
  for (const listener of this.audioListeners) listener(frame);
5402
5543
  }
5544
+ forgetStream(streamId) {
5545
+ this.owners.delete(streamId);
5546
+ for (const [slot, entry] of this.contentKeys) if (entry.streamId === streamId) this.contentKeys.delete(slot);
5547
+ }
5403
5548
  /**
5404
5549
  * The backstop for a missed `nativeKeysRotated` (the node announces a rotation only once it
5405
5550
  * has a baseline to compare against, so a rotation inside its first poll window is never
@@ -5466,7 +5611,7 @@ async function createOpus() {
5466
5611
  return {
5467
5612
  encode: (pcm) => enc.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength)),
5468
5613
  decode: (packet) => toInt16(enc.decode(packet)),
5469
- conceal: () => new Int16Array(FRAME_SAMPLES),
5614
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5470
5615
  close: () => enc.delete?.()
5471
5616
  };
5472
5617
  } catch {
@@ -5479,7 +5624,7 @@ async function createOpus() {
5479
5624
  return {
5480
5625
  encode: (pcm) => codec.encode(Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength), FRAME_SAMPLES),
5481
5626
  decode: (packet) => toInt16(codec.decode(packet)),
5482
- conceal: () => new Int16Array(FRAME_SAMPLES),
5627
+ conceal: (samples = FRAME_SAMPLES) => new Int16Array(samples),
5483
5628
  close: () => codec.delete()
5484
5629
  };
5485
5630
  }
@@ -5628,15 +5773,17 @@ var Listener = class {
5628
5773
  if (!this.options.includeOwn && frame.connectionId === this.session.connectionId) return;
5629
5774
  let s = this.streams.get(frame.streamId);
5630
5775
  if (!s) {
5631
- s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, first: frame.timestamp };
5776
+ s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, lastSamples: 0, first: frame.timestamp };
5632
5777
  this.streams.set(frame.streamId, s);
5633
5778
  }
5634
5779
  if (s.lastTimestamp !== null) {
5635
- const missing = Math.min(50, Math.round((frame.timestamp - s.lastTimestamp) / FRAME_SAMPLES) - 1);
5636
- for (let i = 0; i < missing; i++) s.chunks.push(codec.conceal());
5780
+ const gap = (frame.timestamp - s.lastTimestamp >>> 0) - s.lastSamples;
5781
+ if (gap > 0 && gap <= 48e3) s.chunks.push(codec.conceal(gap));
5637
5782
  }
5638
- s.chunks.push(codec.decode(frame.opus));
5783
+ const pcm = codec.decode(frame.opus);
5784
+ s.chunks.push(pcm);
5639
5785
  s.lastTimestamp = frame.timestamp;
5786
+ s.lastSamples = pcm.length;
5640
5787
  };
5641
5788
  this.detach = this.session.attachAudio(handler);
5642
5789
  }