@bom0792/herdgate 0.9.38 → 0.9.39

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/README.md CHANGED
@@ -166,6 +166,21 @@ One persistent shell with marker framing replaces exec-per-command; phone keystr
166
166
  bounded, coalescing, in-order-acked queue that resyncs instead of replaying; file polls are
167
167
  checksum-stamped (`unchanged` costs one word).
168
168
 
169
+ ### Latency budget (measured 22/11)
170
+
171
+ Interactive typing (mobile TUI, remote herdr attach) rides the SSH bridge **direct**
172
+ to the host — no CDN hop, feels local. The control/PWA leg goes through the tunnel
173
+ edge (~130ms RTT hairpin from the same LAN; a phone on 4G sees similar). To keep
174
+ that leg smooth the publisher now:
175
+
176
+ - pushes view snapshots every `HERDGATE_PUBLISH_MS` (default 30s; the home unit sets
177
+ 5s — a settle is visible on the phone within seconds, not half a minute);
178
+ - replays the last snapshot for a `view` op inside `HERDGATE_SNAPSHOT_FRESH_MS`
179
+ (default 2s) instead of spawning `herdr api snapshot` again — a view op costs ~0
180
+ server-side work when the cache is warm;
181
+ - coalesces concurrent snapshot demands into one spawn (single-flight), and a cached
182
+ view no longer resets the change-sig (so it cannot re-wake away phones for nothing).
183
+
169
184
  ## The SSH leg (the Herdr phone app)
170
185
 
171
186
  The phone app adds machines over SSH. Herdgate terminates SSH at the Gate; use the Gate's reachable SSH
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bom0792/herdgate",
3
- "version": "0.9.38",
3
+ "version": "0.9.39",
4
4
  "description": "Cloud relay hub for herdr-core: every machine dials out, phones and browsers follow (PWA + SSH leg, E2E payloads).",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1,6 +1,6 @@
1
1
  id = "dev.herdgate.host"
2
2
  name = "herdgate"
3
- version = "0.1.0"
3
+ version = "0.9.38"
4
4
  min_herdr_version = "0.9.0"
5
5
  description = "Publish this Herdr session to your own devices through a herdgate cloud relay (read-only by default)."
6
6
  platforms = ["linux", "macos"]
@@ -262,6 +262,15 @@ async function publisherLoop() {
262
262
  let lastSig = "";
263
263
  let devices = [];
264
264
  let timer = null;
265
+ // Push cadence and view-request freshness. The 30s default is gentle on a
266
+ // battery-powered host; a plugged-in one sets HERDGATE_PUBLISH_MS=5000 so a
267
+ // phone sees a settle within seconds instead of half a minute. A `view` op
268
+ // inside the freshness window replays the last snapshot instead of spawning
269
+ // `herdr api snapshot` again (bimodal view latency: 4ms cached vs 150ms cold).
270
+ const PUBLISH_MS = Math.max(2_000, Number.parseInt(process.env.HERDGATE_PUBLISH_MS ?? "30000", 10) || 30000);
271
+ const SNAPSHOT_FRESH_MS = Math.max(0, Number.parseInt(process.env.HERDGATE_SNAPSHOT_FRESH_MS ?? "2000", 10) || 0);
272
+ let snapshotInflight = null;
273
+ let snapshotCached = null; // { payload, at }
265
274
 
266
275
  const snapshotPayload = async () => {
267
276
  let snap;
@@ -280,7 +289,18 @@ async function publisherLoop() {
280
289
  writeState(D.statePath, { lastPublish: { at: new Date().toISOString(), summary } });
281
290
  }
282
291
  if (changed) wakeAway(true);
283
- return { op: "view", view: { v: 1, source: "herdr", takenAt: new Date().toISOString(), summary } };
292
+ const payload = { op: "view", view: { v: 1, source: "herdr", takenAt: new Date().toISOString(), summary } };
293
+ snapshotCached = { payload, at: Date.now() };
294
+ return payload;
295
+ };
296
+
297
+ /** Single-flight: concurrent demands (interval tick + a device's view op)
298
+ * share one `herdr api snapshot` spawn instead of stampeding. */
299
+ const snapshotOnce = () => {
300
+ if (!snapshotInflight) {
301
+ snapshotInflight = snapshotPayload().finally(() => { snapshotInflight = null; });
302
+ }
303
+ return snapshotInflight;
284
304
  };
285
305
 
286
306
  let wsRef = null;
@@ -380,7 +400,7 @@ async function publisherLoop() {
380
400
  });
381
401
  const publish = async (ws) => {
382
402
  if (ws.readyState !== 1 || devices.length === 0) return;
383
- const payload = await snapshotPayload();
403
+ const payload = await snapshotOnce();
384
404
  if (!payload) return;
385
405
  for (const d of devices) {
386
406
  await sendPayload(ws, d.id, payload);
@@ -469,7 +489,7 @@ async function publisherLoop() {
469
489
  // Advertise honestly: `act` only when the owner's switch allows it.
470
490
  let allowAct = false;
471
491
  try { allowAct = (JSON.parse(readFileSync(SETTINGS, "utf8")) ?? {}).allowAct === true; } catch { /* no settings yet */ }
472
- ws.send(JSON.stringify({ v: 1, t: "hello", roomToken: token, info: { name: NAME, version: "0.1.0", capabilities: ["dev.herdgate.host", allowAct ? "act" : "read-only"] } }));
492
+ ws.send(JSON.stringify({ v: 1, t: "hello", roomToken: token, info: { name: NAME, version: "0.9.38", capabilities: ["dev.herdgate.host", allowAct ? "act" : "read-only"] } }));
473
493
  };
474
494
  ws.onmessage = (ev) => {
475
495
  void (async () => {
@@ -485,7 +505,7 @@ async function publisherLoop() {
485
505
  backoff = 1_000;
486
506
  devices = m.devices ?? [];
487
507
  lifecycle?.setConnected(true);
488
- log(`connected to ${url}${loopbackPort ? ` via loopback 127.0.0.1:${loopbackPort}` : ""} (room ${m.roomId}, ${devices.length} device(s))`);
508
+ log(`connected to ${url}${loopbackPort ? ` via loopback 127.0.0.1:${loopbackPort}` : ""} (room ${m.roomId}, ${devices.length} device(s), publish ${PUBLISH_MS}ms)`);
489
509
  if (process.env.HERDGATE_PI_RECEIPTS === "1" && !receiptWatch) {
490
510
  // Settles push to subscribed devices only. Parent wakes no longer ride
491
511
  // this watcher — the child-cooperative design (arm/marker/observer)
@@ -501,7 +521,7 @@ async function publisherLoop() {
501
521
  for (const d of devices) flushWatches(d.id);
502
522
  void publish(ws);
503
523
  if (timer) clearInterval(timer);
504
- timer = setInterval(() => void publish(ws), 30_000);
524
+ timer = setInterval(() => void publish(ws), PUBLISH_MS);
505
525
  timer.unref?.();
506
526
  return;
507
527
  }
@@ -814,7 +834,13 @@ async function publisherLoop() {
814
834
  }
815
835
  }
816
836
  if (op === "view") {
817
- // Answer this device now, even if the view has not changed since the last push.
837
+ // Answer this device now. Inside the freshness window replay the last
838
+ // snapshot (no spawn, no lastSig reset — so the next full tick cannot
839
+ // re-detect a "change" and wake away phones for nothing).
840
+ if (snapshotCached && Date.now() - snapshotCached.at < SNAPSHOT_FRESH_MS) {
841
+ for (const d of devices) await sendPayload(ws, d.id, snapshotCached.payload);
842
+ return;
843
+ }
818
844
  lastSig = "";
819
845
  return void publish(ws);
820
846
  }
@@ -65,6 +65,13 @@ async function launch({ action, bin, hc, mobile, mobileScope, base, state }) {
65
65
  }
66
66
  const env = {
67
67
  ...process.env,
68
+ // The TUI publisher is an interactive-machine default (a laptop you are
69
+ // looking at), not a battery-sipping box: push views every 5s unless the
70
+ // operator set an explicit cadence. The plugin/systemd default stays 30s.
71
+ HERDGATE_PUBLISH_MS: process.env.HERDGATE_PUBLISH_MS ?? "5000",
72
+ // Match the freshness window to the cadence so a view op almost always
73
+ // replays the last snapshot instead of forcing a fresh spawn.
74
+ HERDGATE_SNAPSHOT_FRESH_MS: process.env.HERDGATE_SNAPSHOT_FRESH_MS ?? process.env.HERDGATE_PUBLISH_MS ?? "5000",
68
75
  HERDGATE_MOBILE_HC_BIN: mobileHcBin,
69
76
  HERDGATE_MOBILE_TUI_DEVICE: mobile,
70
77
  HERDGATE_MOBILE_TUI_SCOPE: mobileScope,