Files
buzz/desktop/src/features/mesh-compute/servingUsage.test.mjs
Michael Neale c79c76fa91 feat(desktop): surface who is using shared compute on the Share card
Adds a read-only, host-side usage indicator to the Share compute card so
you can see when someone (or your own agent) is using the compute this
machine is sharing, and where it's coming from.

- new read-only mesh_serving_usage Tauri command projects the serving
  node's own routing_metrics (request/token counts, inflight, and the
  local vs remote/endpoint attempt split). No new trust surface; stubbed
  for non-mesh-llm builds.
- serving_usage_from_payload: pure, defensive extractor (missing fields
  -> 0, so SDK shape drift degrades to 'no usage' not an error).
- deriveServingIndicator: pure label derivation. Distinguishes this
  machine's OWN agents (local, shown softly) from another member
  consuming this machine's compute (remote/endpoint -> the headline
  'in use by another member' case).
- useMeshServingUsage: 4s poll, only while sharing.
- indicator rendered under the Share toggle status line.
- tests: 3 Rust (extractor local/remote/default), 7 TS (indicator label
  table), e2e mock answers mesh_serving_usage + seed hook.

Verified live: firing a completion at the mesh API moves exactly the
fields the extractor reads (req/tokens/local_attempt), local-only traffic
correctly reads as not-a-remote-consumer.

Local-only exploration on the mesh-state-fix branch; not part of PR #2448.
2026-07-23 12:27:57 +10:00

87 lines
2.5 KiB
JavaScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { deriveServingIndicator } from "./servingUsage.ts";
function usage(overrides = {}) {
return {
inflight: 0,
peakInflight: 0,
requestsServed: 0,
tokensServed: 0,
tokensPerSecond: 0,
localAttempts: 0,
remoteAttempts: 0,
endpointAttempts: 0,
peers: 0,
...overrides,
};
}
test("hidden when not sharing", () => {
const i = deriveServingIndicator(usage({ requestsServed: 5 }), false);
assert.equal(i.show, false);
});
test("hidden when usage not yet fetched", () => {
const i = deriveServingIndicator(null, true);
assert.equal(i.show, false);
});
test("sharing but nothing served yet -> idle, no detail", () => {
const i = deriveServingIndicator(usage(), true);
assert.equal(i.show, true);
assert.equal(i.active, false);
assert.equal(i.hasRemoteConsumers, false);
assert.match(i.label, /Idle/);
});
test("only local agent traffic -> not a remote consumer", () => {
const i = deriveServingIndicator(
usage({ requestsServed: 4, localAttempts: 4, tokensPerSecond: 30 }),
true,
);
assert.equal(i.hasRemoteConsumers, false);
assert.match(i.label, /Idle/); // served earlier, none live now
assert.match(i.detail, /4 requests served/);
});
test("local agent live now -> serving your agent", () => {
const i = deriveServingIndicator(
usage({ inflight: 1, localAttempts: 2, tokensPerSecond: 28 }),
true,
);
assert.equal(i.active, true);
assert.equal(i.hasRemoteConsumers, false);
assert.match(i.label, /Serving your agent/);
assert.match(i.label, /1 live/);
});
test("remote consumer, not live -> used by another member (headline case)", () => {
const i = deriveServingIndicator(
usage({
requestsServed: 7,
remoteAttempts: 6,
endpointAttempts: 1,
peers: 2,
}),
true,
);
assert.equal(i.hasRemoteConsumers, true);
assert.equal(i.active, false);
assert.match(i.label, /another member/);
assert.match(i.label, /7 requests/); // remote+endpoint = 7
assert.match(i.detail, /2 peers/);
});
test("remote consumer live now -> in use now, singular peer/request grammar", () => {
const i = deriveServingIndicator(
usage({ inflight: 1, remoteAttempts: 1, peers: 1, tokensPerSecond: 31 }),
true,
);
assert.equal(i.active, true);
assert.equal(i.hasRemoteConsumers, true);
assert.match(i.label, /In use now by another member/);
assert.match(i.detail, /1 peer\b/); // singular
});