perf(panel): add incremental step transport, memoized aggregation, atomic cache writes, and hidden-view gating

- Project display-only step fields in /api/telemetry, skip unchanged step payloads via stepRevision, coalesce identical client refreshes, and share in-flight backend trajectory scans across same-account requests
- Reuse normalized step values and memoize up to two range aggregations in usage-aggregator gated by step timestamps, pricing revision, model overrides, timezone, and source identity; cache per-trajectory model facts and single-agent step preparation via WeakMap/WeakRef
- Persist trajectory checkpoints atomically in trajectory-cache-file.mjs via chunked writevSync + renameSync while reusing serialized step byte slices for unchanged conversations
- Render step turn-rail lines/dots via CSS pseudo-elements, allocate speed-reason nodes on demand, short-circuit unchanged table reconciliations, and cache per-series LOESS publication fits in SessionStepCharts
- Gate DOM updates and timers across closed overlay panels, hover popups, inactive settings tabs, and unchanged ConsumptionView payloads
- Distinguish canceled steps (CORTEX_STEP_STATUS_CANCELED) and untimed compaction steps from generic incomplete timing in speed-metrics and bilingual i18n labels
This commit is contained in:
fish
2026-11-15 23:33:08 +09:00
parent 03d3160ef0
commit 8968e03adb
33 changed files with 1692 additions and 920 deletions
@@ -0,0 +1,79 @@
import {
closeSync, fstatSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writevSync,
} from 'node:fs';
import { randomUUID } from 'node:crypto';
let lastWrite = null;
function isSameFile(a, b) {
return b.ino !== 0n && a.dev === b.dev && a.ino === b.ino &&
a.size === b.size && a.mtimeNs === b.mtimeNs;
}
function readReusableBytes(filePath, expected) {
let fd;
try {
fd = openSync(filePath, 'r');
const before = fstatSync(fd, { bigint: true });
if (!isSameFile(before, expected) || before.ctimeNs !== expected.ctimeNs) return null;
const bytes = readFileSync(fd);
const after = fstatSync(fd, { bigint: true });
return isSameFile(before, after) && before.ctimeNs === after.ctimeNs &&
BigInt(bytes.length) === after.size ? bytes : null;
} catch {
return null;
} finally {
if (fd !== undefined) closeSync(fd);
}
}
// 只复用上次确认写入的步骤字节。扫描器用新数组替换变更步骤,每次仍重新序列化会话元数据。
export function writeTrajectoryCacheFile(filePath, data) {
const previous = lastWrite?.filePath === filePath ? lastWrite : null;
const bytes = previous ? readReusableBytes(filePath, previous.file) : null;
const ranges = new WeakMap();
const parts = [];
let position = 0;
function append(part) {
const buffer = typeof part === 'string' ? Buffer.from(part) : part;
parts.push(buffer);
position += buffer.length;
}
append('{');
let separator = '';
for (const [id, entry] of Object.entries(data)) {
const { steps, ...metadata } = entry;
const header = JSON.stringify(metadata);
append(`${separator}${JSON.stringify(id)}:${header.slice(0, -1)}${header === '{}' ? '' : ','}"steps":`);
const range = bytes && previous.ranges.get(steps);
const start = position;
append(range ? bytes.subarray(range.start, range.end) : JSON.stringify(steps));
ranges.set(steps, { start, end: position });
append('}');
separator = ',';
}
append('}');
const temporaryPath = `${filePath}.${randomUUID()}.tmp`;
try {
const fd = openSync(temporaryPath, 'wx');
try {
// 按块写入,避免再复制一份完整缓存到连续缓冲区。
const batchSize = 256;
for (let i = 0; i < parts.length; i += batchSize) {
const batch = parts.slice(i, i + batchSize);
const length = batch.reduce((sum, part) => sum + part.length, 0);
if (writevSync(fd, batch) !== length) throw new Error('Incomplete cache write');
}
} finally {
closeSync(fd);
}
const written = statSync(temporaryPath, { bigint: true });
renameSync(temporaryPath, filePath);
const file = statSync(filePath, { bigint: true });
lastWrite = isSameFile(written, file) ? { filePath, file, ranges } : null;
} finally {
try { unlinkSync(temporaryPath); } catch {}
}
}