Merge branch 'perf/lag': dashboard lag fixes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # client/src/components/Sagi/PetSprite.tsx # client/src/components/Sagi/useSagiBrain.ts
This commit is contained in:
@@ -11,11 +11,25 @@ const { db, stmts } = require("../db");
|
||||
const TranscriptCache = require("./transcript-cache");
|
||||
const { longArgs } = require("./token-usage");
|
||||
|
||||
/** Write every hour of one transcript parse (MAX per hour, see the statement). */
|
||||
// What this process last wrote per session and hour bucket: a long session has
|
||||
// one bucket per active hour per model, and every hook re-sent all of them.
|
||||
// The upsert keeps the MAX, so skipping a bucket whose numbers did not move
|
||||
// writes exactly the same rows. Bounded (oldest session dropped first); a miss
|
||||
// only means one full write, never a lost one.
|
||||
const WRITTEN_MAX = 500;
|
||||
const written = new Map();
|
||||
|
||||
/** Write the hours of one transcript parse that changed (MAX per hour, see the statement). */
|
||||
function writeHourly(sessionId, tokensByHour) {
|
||||
// Runs every hour of the session on each hook — ~1 row per active hour per
|
||||
// model; the statement leaves a row that did not grow untouched (no write).
|
||||
for (const t of Object.values(tokensByHour || {})) {
|
||||
let seen = written.get(sessionId);
|
||||
if (seen) written.delete(sessionId);
|
||||
else seen = new Map();
|
||||
written.set(sessionId, seen);
|
||||
if (written.size > WRITTEN_MAX) written.delete(written.keys().next().value);
|
||||
for (const [key, t] of Object.entries(tokensByHour || {})) {
|
||||
const sig = JSON.stringify(t);
|
||||
if (seen.get(key) === sig) continue;
|
||||
seen.set(key, sig);
|
||||
stmts.upsertHourlyUsage.run(
|
||||
sessionId,
|
||||
t.hour,
|
||||
@@ -73,4 +87,11 @@ function backfillHourly() {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { writeHourly, backfillHourly };
|
||||
/** Forget what was written — for `ids`, or every session. Whatever deletes
|
||||
* hourly rows calls it, or the next hooks would skip re-writing them. */
|
||||
function forgetHourly(ids = null) {
|
||||
if (!ids) return written.clear();
|
||||
for (const id of ids) written.delete(id);
|
||||
}
|
||||
|
||||
module.exports = { writeHourly, backfillHourly, forgetHourly };
|
||||
|
||||
@@ -57,13 +57,18 @@ function readMode(runId) {
|
||||
const fail = (code, message) => Object.assign(new Error(message), { code });
|
||||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
// Between two reads of the pane. Each read is a SYNCHRONOUS capture-pane
|
||||
// (through sudo for a member's pane) that holds up every other request and
|
||||
// socket while it runs, so a mode change polls gently rather than 20×/s.
|
||||
const POLL_MS = 150;
|
||||
|
||||
// The mode once it differs from `from` — or what the pane shows at the deadline.
|
||||
async function nextMode(runId, from, waitMs) {
|
||||
const until = Date.now() + waitMs;
|
||||
for (;;) {
|
||||
const now = readMode(runId);
|
||||
if ((now && now !== from) || Date.now() >= until) return now;
|
||||
await sleep(50);
|
||||
await sleep(POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -352,7 +352,21 @@ const session = (name) => (exact() ? `=${name}` : name);
|
||||
const pane = (name) => (exact() ? `=${name}:` : name);
|
||||
|
||||
function hasSession(name) {
|
||||
// A fresh shared list that names it answers "yes" without a spawn: each
|
||||
// synchronous one blocks every socket (terminal echo included) — 50-150 ms
|
||||
// per psmux call on Windows — and a pane-token request, an AI-proxy call or
|
||||
// a lane broadcast asks this every time. "No" is always asked for real, so a
|
||||
// pane that just died is never trusted past the list's 1 s TTL.
|
||||
const hit = listed.get("");
|
||||
try {
|
||||
if (
|
||||
hit &&
|
||||
!hit.err &&
|
||||
Date.now() - hit.at < LIST_TTL_MS &&
|
||||
hit.out.split("\n").includes(name) &&
|
||||
!accountOf(name)
|
||||
)
|
||||
return true;
|
||||
onServerOf(name, ["has-session", "-t", session(name)]);
|
||||
return true;
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user