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