Add netquality 1.1.2 client and node behavior.

Show the address actually probed, keep address-selection misses out of formal loss, and add session metrics, a stop summary, pinned chart points, and in-app metric and release-history screens. Client version is 1.1.2+5. Attribution remains 言灸 under AGPL-3.0-only.

Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
Cursor Agentandyanjiu0721 committed 2026-11-19 23:20:50 +00:00
1 parent 8ca2185c01
commit aeb08ec049
40 files changed
+3827 -545

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
/// Latency over successful round-trips only.
///
/// * current: the latest successful sample, stored separately as `rttMs`
/// * min / max: smallest and largest successful sample
/// * mean: arithmetic mean
/// * median: the middle sample after sorting; when the count is even, the
/// mean of the two middle samples
/// * p95: nearest-rank. Rank is `ceil(0.95 * n)` computed as
/// `(95 * n + 99) ~/ 100` so binary floating point cannot move the rank.
/// Rank is 1-based and then clamped to the sample count.
class LatencyStats {
const LatencyStats({
required this.min,
required this.mean,
required this.median,
required this.p95,
required this.max,
});
final double min;
final double mean;
final double median;
final double p95;
final double max;
static LatencyStats? from(List<double> samples) {
if (samples.isEmpty) {
return null;
}
final sorted = [...samples]..sort();
var sum = 0.0;
for (final value in sorted) {
sum += value;
}
final count = sorted.length;
final median = count.isOdd
? sorted[count ~/ 2]
: (sorted[count ~/ 2 - 1] + sorted[count ~/ 2]) / 2;
final rank = nearestRank95(count);
return LatencyStats(
min: sorted.first,
mean: sum / count,
median: median,
p95: sorted[rank - 1],
max: sorted.last,
);
}
}
int nearestRank95(int count) {
if (count <= 1) {
return 1;
}
final rank = (95 * count + 99) ~/ 100;
if (rank < 1) {
return 1;
}
if (rank > count) {
return count;
}
return rank;
}
+48 -1
View File
@@ -1,3 +1,5 @@
import 'latency.dart';
class Outcome {
const Outcome({
required this.status,
@@ -6,6 +8,7 @@ class Outcome {
this.note = '',
this.detail = const {},
this.reconnect = false,
this.selectionSkipped = 0,
});
final String status;
@@ -15,6 +18,10 @@ class Outcome {
final Map<String, String> detail;
final bool reconnect;
/// Candidate failures folded into this measured attempt. They are not
/// themselves samples.
final int selectionSkipped;
static const ok = 'ok';
static const timeout = 'timeout';
static const unknown = 'unknown';
@@ -48,6 +55,13 @@ class ProbeSummary {
required this.unknownPct,
required this.lastNote,
required this.detail,
this.minRttMs,
this.meanRttMs,
this.medianRttMs,
this.p95RttMs,
this.maxRttMs,
this.selectionSkipped = 0,
this.latencyStats = false,
});
final int sent;
@@ -68,6 +82,15 @@ class ProbeSummary {
final double? unknownPct;
final String lastNote;
final Map<String, String> detail;
final double? minRttMs;
final double? meanRttMs;
final double? medianRttMs;
final double? p95RttMs;
final double? maxRttMs;
final int selectionSkipped;
/// False when an older remote node omitted the latency distribution.
final bool latencyStats;
static const empty = ProbeSummary(
sent: 0,
@@ -110,6 +133,16 @@ class ProbeSummary {
unknownPct: _double(json['unknownPct']),
lastNote: json['lastNote'] is String ? json['lastNote'] as String : '',
detail: _detail(json['detail']),
minRttMs: _double(json['minRttMs']),
meanRttMs: _double(json['meanRttMs']),
medianRttMs: _double(json['medianRttMs']),
p95RttMs: _double(json['p95RttMs']),
maxRttMs: _double(json['maxRttMs']),
selectionSkipped: _int(json['selectionSkipped']),
latencyStats:
json.containsKey('minRttMs') ||
json.containsKey('medianRttMs') ||
json.containsKey('p95RttMs'),
);
}
}
@@ -137,7 +170,9 @@ class ProbeSample {
factory ProbeSample.fromJson(Map<String, dynamic> json) {
final summary = json['summary'] is Map
? ProbeSummary.fromJson(Map<String, dynamic>.from(json['summary'] as Map))
? ProbeSummary.fromJson(
Map<String, dynamic>.from(json['summary'] as Map),
)
: ProbeSummary.empty;
return ProbeSample(
seq: _int(json['seq']),
@@ -171,12 +206,15 @@ class SampleBook {
double jitterMs = 0;
bool _hasJitter = false;
double _lastTransit = 0;
final List<double> _rtts = [];
int selectionSkipped = 0;
String lastNote = '';
Map<String, String> detail = {};
void add(Outcome outcome) {
sent += 1;
completed += 1;
selectionSkipped += outcome.selectionSkipped;
if (outcome.reconnect) {
reconnects += 1;
}
@@ -193,6 +231,7 @@ class SampleBook {
final rtt = outcome.rttMs;
if (rtt != null) {
lastRttMs = rtt;
_rtts.add(rtt);
if (_hasJitter) {
final delta = (rtt - _lastTransit).abs();
jitterMs += (delta - jitterMs) / 16;
@@ -250,6 +289,7 @@ class SampleBook {
}
}
}
final latency = LatencyStats.from(_rtts);
return ProbeSummary(
sent: sent,
completed: completed,
@@ -269,6 +309,13 @@ class SampleBook {
unknownPct: unknownPct,
lastNote: lastNote,
detail: detail,
minRttMs: latency?.min,
meanRttMs: latency?.mean,
medianRttMs: latency?.median,
p95RttMs: latency?.p95,
maxRttMs: latency?.max,
selectionSkipped: selectionSkipped,
latencyStats: true,
);
}
+120
View File
@@ -0,0 +1,120 @@
// Copyright (C) 2026 言灸
// SPDX-License-Identifier: AGPL-3.0-only
import 'sample_book.dart';
/// A candidate "responded" when the address itself answered: success, a
/// protocol mismatch, or an explicit refusal. Silence, timeout and a
/// connect error are not a response, so another address may be tried.
bool selectionResponded(Outcome outcome) {
switch (outcome.status) {
case Outcome.ok:
case Outcome.protocol:
case Outcome.refused:
return true;
default:
return false;
}
}
class SelectionDecision {
const SelectionDecision({required this.commit, this.skipped = 0});
final List<Outcome> commit;
final int skipped;
}
/// Separates address selection from measured probes.
///
/// Before any candidate has responded, failures are held. A later response
/// drops those held failures: they are reported as a fallback diagnostic and
/// are not added to sent, timeout or loss. If every candidate fails in the
/// round, the held failures are committed, so the session cannot show a
/// false 0% success. After a response, further failures are committed
/// immediately. [endMeasurement] is called when the picker leaves the
/// address that was being measured; the failure that caused the leave has
/// already been committed.
class SelectionLedger {
SelectionLedger(this.candidateCount);
final int candidateCount;
bool measuring = false;
final List<Outcome> _held = [];
SelectionDecision take(Outcome outcome, {required bool responded}) {
if (candidateCount <= 1) {
if (responded) {
measuring = true;
}
return SelectionDecision(commit: [outcome]);
}
if (measuring) {
return SelectionDecision(commit: [outcome]);
}
if (responded) {
final skipped = _held.length;
final next = skipped == 0
? outcome
: _attach(outcome, _fallbackText(_held, outcome), skipped);
_held.clear();
measuring = true;
return SelectionDecision(commit: [next], skipped: skipped);
}
_held.add(outcome);
if (_held.length >= candidateCount) {
final commit = List<Outcome>.of(_held);
_held.clear();
return SelectionDecision(commit: commit);
}
return const SelectionDecision(commit: []);
}
void endMeasurement() {
measuring = false;
}
}
Outcome _attach(Outcome outcome, String fallback, int skipped) {
return Outcome(
status: outcome.status,
kind: outcome.kind,
rttMs: outcome.rttMs,
note: outcome.note,
reconnect: outcome.reconnect,
selectionSkipped: outcome.selectionSkipped + skipped,
detail: {...outcome.detail, 'fallback': fallback},
);
}
String _fallbackText(List<Outcome> held, Outcome success) {
final failed = held
.map((outcome) {
final address = outcome.detail['address'];
final family = outcome.detail['family'];
final where = address == null || address.isEmpty
? '未知地址'
: (family == null || family.isEmpty)
? address
: '$address($family)';
final why = outcome.note.isEmpty ? '没有成功' : outcome.note;
return '$where:$why';
})
.join(';');
final used = success.detail['address'] ?? '';
final usedFamily = success.detail['family'] ?? '';
final usedText = used.isEmpty
? '另一个地址'
: usedFamily.isEmpty
? used
: '$used($usedFamily)';
return '候选未成功:$failed。已改用 $usedText。这些失败没有计入正式丢包或失败率。';
}
/// Stop discards the attempt that was still in flight. A finished attempt
/// whose note says it was stopped is not a measured loss either.
bool countsAfterStop(Outcome? outcome, {required bool cancelled}) {
if (cancelled || outcome == null) {
return false;
}
return outcome.note != '已停止';
}