feat(panel): add step anomaly diagnostics, >=100 tok/s integer speed formatting, and capsule alignment fix

This commit is contained in:
fish
2026-11-19 22:28:39 +09:00
parent dfba5ead7b
commit 4a4e483209
20 changed files with 3546 additions and 143 deletions
+7 -3
View File
@@ -79,7 +79,7 @@ function projectDisplayStep(st) {
const {
stepIndex, createdAtMs, isCompactionStep, modelKey, modelShort, modelLabel,
modelUpstream, responseModel, modelSource, tokPerSec, ttftSec, totalDurationSec,
streamDurationSec, selectedSpeedReason, contextTokens, inputTokens, cacheReadTokens,
streamDurationSec, selectedSpeedReason, anomaly, anomalySig, contextTokens, inputTokens, cacheReadTokens,
outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, inCostUsd,
cacheCostUsd, outCostUsd, agentId, agentCascadeId, agentKind, agentOrdinal,
agentCustomRole, agentLabel, agentBadge, agentRole, agentIndex, isSubagent,
@@ -91,7 +91,9 @@ function projectDisplayStep(st) {
return {
stepIndex, createdAtMs, isCompactionStep, modelKey, modelShort, modelLabel,
modelUpstream, responseModel, modelSource, tokPerSec, ttftSec, totalDurationSec,
streamDurationSec, selectedSpeedReason, contextTokens, inputTokens, cacheReadTokens,
streamDurationSec, selectedSpeedReason,
...(anomaly ? { anomaly, anomalySig } : {}),
contextTokens, inputTokens, cacheReadTokens,
outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, inCostUsd,
cacheCostUsd, outCostUsd, agentId, agentCascadeId, agentKind, agentOrdinal,
agentCustomRole, agentLabel, agentBadge, agentRole, agentIndex, isSubagent,
@@ -104,7 +106,9 @@ function projectDisplayStep(st) {
const MAX_DISPLAY_STEP_CACHE_ENTRIES = 8;
const displayStepsCache = new Map();
const displayStepFields = Object.keys(projectDisplayStep({}));
const displayStepFields = Object.keys(projectDisplayStep({ anomaly: true, anomalySig: '' })).filter(
(k) => k !== 'anomaly'
);
function haveSameDisplaySteps(previous, current) {
if (!previous || !current) return previous === current;
+98 -13
View File
@@ -914,6 +914,84 @@ function decodeProtoContextWindowMetaSlice(buf, state, end, includeBreakdown) {
};
}
function decodeProtoRetryInfoSlice(buf, state, end) {
const ri = {};
while (state.off < end) {
const rTag = Number(BigInt.asUintN(32, readProtoVarint(buf, state, end)));
const rFn = rTag >>> 3;
const rWt = rTag & 7;
if (rFn === 2 && rWt === 2) {
ri.usage = decodeProtoModelUsageSlice(buf, state, readSubEnd(buf, state, end));
} else if (rFn === 3 && rWt === 2) {
const errStr = readProtoString(buf, state, end).trim();
if (errStr) {
ri.error = errStr.length > 320 ? `${errStr.slice(0, 319)}…` : errStr;
}
} else if (rFn === 5 && rWt === 0) {
const code = Number(BigInt.asUintN(32, readProtoVarint(buf, state, end)));
if (code > 0) ri.reasonCode = code;
} else if (rFn === 7 && rWt === 2) {
const dur = decodeProtoDurationSlice(buf, state, readSubEnd(buf, state, end));
if (dur) ri.retryDelay = dur;
} else {
skipProtoField(buf, state, rWt, end);
}
}
state.off = end;
return ri;
}
function decodeProtoErrorMessageSlice(buf, state, end) {
let userErrorMessage = '';
let shortError = '';
let errorCode = 0;
let shouldShowModel = false;
let shouldShowUser = false;
while (state.off < end) {
const tag = Number(BigInt.asUintN(32, readProtoVarint(buf, state, end)));
const fn = tag >>> 3;
const wt = tag & 7;
if (fn === 3 && wt === 2) {
const eEnd = readSubEnd(buf, state, end);
while (state.off < eEnd) {
const eTag = Number(BigInt.asUintN(32, readProtoVarint(buf, state, eEnd)));
const eFn = eTag >>> 3;
const eWt = eTag & 7;
if (eFn === 1 && eWt === 2) {
const s = readProtoString(buf, state, eEnd).trim();
if (s) userErrorMessage = s.length > 240 ? `${s.slice(0, 239)}…` : s;
} else if (eFn === 2 && eWt === 2) {
const s = readProtoString(buf, state, eEnd).trim();
if (s) shortError = s.length > 320 ? `${s.slice(0, 319)}…` : s;
} else if (eFn === 7 && eWt === 0) {
errorCode = Number(BigInt.asUintN(32, readProtoVarint(buf, state, eEnd)));
} else {
skipProtoField(buf, state, eWt, eEnd);
}
}
state.off = eEnd;
} else if (fn === 4 && wt === 0) {
shouldShowModel = readProtoVarint(buf, state, end) !== 0n;
} else if (fn === 5 && wt === 0) {
shouldShowUser = readProtoVarint(buf, state, end) !== 0n;
} else {
skipProtoField(buf, state, wt, end);
}
}
state.off = end;
if (!userErrorMessage && !shortError && !errorCode) return null;
return {
error: {
...(userErrorMessage ? { userErrorMessage } : {}),
...(shortError ? { shortError } : {}),
...(errorCode > 0 ? { errorCode } : {}),
},
...(shouldShowModel ? { shouldShowModel: true } : {}),
...(shouldShowUser ? { shouldShowUser: true } : {}),
};
}
function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
let model = '';
let usage = null;
@@ -922,6 +1000,8 @@ function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
let streamingDuration = '';
let responseModel = '';
let modelDisplayName = '';
let retryCount = 0;
let retryInfos = null;
const extraMeta = {};
let messagePrompts = null;
let tools = null;
@@ -1004,6 +1084,8 @@ function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
timeToFirstToken = decodeProtoDurationSlice(buf, state, readSubEnd(buf, state, end));
} else if (fn === 12 && wt === 2) {
streamingDuration = decodeProtoDurationSlice(buf, state, readSubEnd(buf, state, end));
} else if (fn === 14 && wt === 0) {
retryCount = Number(BigInt.asUintN(32, readProtoVarint(buf, state, end)));
} else if (fn === 16 && wt === 2) {
if (includeBreakdown) {
const psEnd = readSubEnd(buf, state, end);
@@ -1030,6 +1112,10 @@ function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
} else {
skipProtoField(buf, state, wt, end);
}
} else if (fn === 17 && wt === 2) {
const ri = decodeProtoRetryInfoSlice(buf, state, readSubEnd(buf, state, end));
if (!retryInfos) retryInfos = [];
retryInfos.push(ri);
} else if (fn === 19 && wt === 2) {
responseModel = readProtoString(buf, state, end).trim();
} else if (fn === 20 && wt === 2) {
@@ -1056,6 +1142,8 @@ function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
usage.apiProvider = 'API_PROVIDER_ANTHROPIC';
}
const hasRetryEvidence =
retryCount > 0 || Boolean(retryInfos && retryInfos.some((r) => r?.error || r?.reasonCode));
return {
chatModel: {
...(model ? { model } : {}),
@@ -1065,6 +1153,8 @@ function decodeProtoChatModelMetaSlice(buf, state, end, includeBreakdown) {
: {}),
...(timeToFirstToken ? { timeToFirstToken } : {}),
...(streamingDuration ? { streamingDuration } : {}),
...(retryCount > 0 ? { retryCount } : {}),
...(hasRetryEvidence && retryInfos ? { retryInfos } : {}),
...(responseModel ? { responseModel } : {}),
...(modelDisplayName ? { modelDisplayName } : {}),
...(messagePrompts ? { messagePrompts } : {}),
@@ -1233,19 +1323,7 @@ function decodeProtoStepMeta(rawBlob, fallbackIdx) {
}
state.off = siEnd;
} else if (fn === 28 && wt === 2) {
const riEnd = readSubEnd(buf, state, end);
const ri = {};
while (state.off < riEnd) {
const rTag = Number(BigInt.asUintN(32, readProtoVarint(buf, state, riEnd)));
const rFn = rTag >>> 3;
const rWt = rTag & 7;
if (rFn === 2 && rWt === 2) {
ri.usage = decodeProtoModelUsageSlice(buf, state, readSubEnd(buf, state, riEnd));
} else {
skipProtoField(buf, state, rWt, riEnd);
}
}
state.off = riEnd;
const ri = decodeProtoRetryInfoSlice(buf, state, readSubEnd(buf, state, end));
if (!retryInfos) retryInfos = [];
retryInfos.push(ri);
} else if (fn === 30 && wt === 2) {
@@ -1501,6 +1579,9 @@ function decodeProtoSelectiveStepPayload(
readSubEnd(buf, state, end),
keepFullText
);
} else if (fn === 24 && wt === 2 && stepTypeNum === 17) {
const em = decodeProtoErrorMessageSlice(buf, state, readSubEnd(buf, state, end));
if (em) out.errorMessage = em;
} else if (fn === 114 && wt === 2 && stepTypeNum === 101 && includeSystemMessage) {
const sysMsg = decodeProtoSystemMessageSlice(buf, state, readSubEnd(buf, state, end));
if (sysMsg) out.systemMessage = { message: sysMsg };
@@ -1786,6 +1867,7 @@ export function readLocalSqliteTrajectory(dbFilePath, options = {}) {
if (
rec.stepTypeNum === 14 ||
rec.stepTypeNum === 15 ||
rec.stepTypeNum === 17 ||
(isChildTrajectory && rec.stepTypeNum === 101) ||
isInvSub ||
(inRunningTail && (rec.stepTypeNum !== 132 || !rec.metadata?.toolCall?.argumentsJson))
@@ -1868,6 +1950,9 @@ export function readLocalSqliteTrajectory(dbFilePath, options = {}) {
if (decodedPayload.plannerResponse) {
stepObj.plannerResponse = decodedPayload.plannerResponse;
}
if (decodedPayload.errorMessage) {
stepObj.errorMessage = decodedPayload.errorMessage;
}
if (decodedPayload.invokeSubagent) {
stepObj.invokeSubagent = decodedPayload.invokeSubagent;
}
+37 -7
View File
@@ -783,11 +783,23 @@ function loadOfflineTranscriptConversation(conv, diskFiles, externalByLower, loc
}
continue;
}
if (sq.type === 'CORTEX_STEP_TYPE_ERROR_MESSAGE') {
sqliteRawSteps.push({
type: sq.type,
status: sq.status,
...(sq.errorMessage ? { errorMessage: sq.errorMessage } : {}),
...(typeof tLine?.error === 'string' && tLine.error ? { error: tLine.error } : {}),
...(typeof tLine?.content === 'string' && tLine.content ? { content: tLine.content } : {}),
metadata: sq.metadata,
});
continue;
}
if (sq.metadata?.modelUsage) {
const rawUsage = sq.metadata?.modelUsage || sq.metadata?.retryInfos?.[0]?.usage;
if (rawUsage) {
const deepStep = extMatch?.steps?.[modelOrdinal] || null;
modelOrdinal += 1;
const mu = { ...sq.metadata.modelUsage };
const mu = { ...rawUsage };
if (!mu.model) {
const fallbackModel = String(
tLine?.model || tLine?.generator_model || deepStep?.modelKey || ''
@@ -822,7 +834,9 @@ function loadOfflineTranscriptConversation(conv, diskFiles, externalByLower, loc
upperType === 'CORTEX_STEP_TYPE_USER_INPUT' ||
(line.userInput && typeof line.userInput === 'object') ||
upperType === 'SYSTEM_MESSAGE' ||
upperType === 'CORTEX_STEP_TYPE_SYSTEM_MESSAGE'
upperType === 'CORTEX_STEP_TYPE_SYSTEM_MESSAGE' ||
upperType === 'ERROR_MESSAGE' ||
upperType === 'CORTEX_STEP_TYPE_ERROR_MESSAGE'
) {
sqliteRawSteps.push(line);
} else if (
@@ -830,14 +844,19 @@ function loadOfflineTranscriptConversation(conv, diskFiles, externalByLower, loc
line.cache_read_tokens !== undefined ||
line.output_tokens !== undefined
) {
const tailStatus = String(line.status || '').trim().toUpperCase();
sqliteRawSteps.push({
type:
upperType === 'CHECKPOINT' || upperType === 'CORTEX_STEP_TYPE_CHECKPOINT'
? 'CORTEX_STEP_TYPE_CHECKPOINT'
: String(line.type || 'PLANNER_RESPONSE'),
status: /CANCEL/i.test(String(line.status || ''))
status: /CANCEL/.test(tailStatus)
? 'CORTEX_STEP_STATUS_CANCELED'
: 'CORTEX_STEP_STATUS_DONE',
: /ERROR/.test(tailStatus)
? 'CORTEX_STEP_STATUS_ERROR'
: /INTERRUPT/.test(tailStatus)
? 'CORTEX_STEP_STATUS_INTERRUPTED'
: 'CORTEX_STEP_STATUS_DONE',
plannerResponse: {
response: typeof line.content === 'string' ? line.content : '',
thinking: typeof line.thinking === 'string' ? line.thinking : '',
@@ -873,7 +892,9 @@ function loadOfflineTranscriptConversation(conv, diskFiles, externalByLower, loc
upperType === 'CORTEX_STEP_TYPE_USER_INPUT' ||
(line.userInput && typeof line.userInput === 'object') ||
upperType === 'SYSTEM_MESSAGE' ||
upperType === 'CORTEX_STEP_TYPE_SYSTEM_MESSAGE'
upperType === 'CORTEX_STEP_TYPE_SYSTEM_MESSAGE' ||
upperType === 'ERROR_MESSAGE' ||
upperType === 'CORTEX_STEP_TYPE_ERROR_MESSAGE'
) {
syntheticSteps.push(line);
continue;
@@ -898,7 +919,11 @@ function loadOfflineTranscriptConversation(conv, diskFiles, externalByLower, loc
const rawStatus = String(line.status || '').trim().toUpperCase();
const mappedStatus = /CANCEL/.test(rawStatus)
? 'CORTEX_STEP_STATUS_CANCELED'
: 'CORTEX_STEP_STATUS_DONE';
: /ERROR/.test(rawStatus)
? 'CORTEX_STEP_STATUS_ERROR'
: /INTERRUPT/.test(rawStatus)
? 'CORTEX_STEP_STATUS_INTERRUPTED'
: 'CORTEX_STEP_STATUS_DONE';
const finishedAt = String(
line.finished_generating_at ||
line.finishedGeneratingAt ||
@@ -1931,6 +1956,7 @@ export async function enumerateAndSyncConversations({
turnRailTelemetry: true,
wakeupTurnTelemetry: true,
contextGroupsTelemetry: 2,
anomalyTelemetry: 1,
steps: stepsWithOwners,
subagentRefs,
};
@@ -1964,6 +1990,7 @@ export async function enumerateAndSyncConversations({
!prev.speedClearedTelemetry ||
!prev.turnRailTelemetry ||
!prev.contextGroupsTelemetry ||
!prev.anomalyTelemetry ||
metaChanged ||
!isDeepStrictEqual(prev.steps, stepsWithOwners);
@@ -2033,6 +2060,7 @@ export async function enumerateAndSyncConversations({
speedClearedTelemetry: true,
turnRailTelemetry: true,
contextGroupsTelemetry: 2,
anomalyTelemetry: 1,
steps: offlineStepsWithOwners,
subagentRefs: offlineFallback.subagentRefs,
};
@@ -2163,6 +2191,7 @@ export async function enumerateAndSyncConversations({
if (!cached.contextLimitTelemetry && lowerCid === contextTelemetryCascadeId) return true;
if (
(!cached.turnRailTelemetry ||
!cached.anomalyTelemetry ||
hasMissingTurnRailFields(cached) ||
needsWakeupTurnRefresh(conv, cached) ||
hasDroppedContextGroups(cached)) &&
@@ -2345,6 +2374,7 @@ export async function enumerateAndSyncConversations({
!hasMissingStepFields(existingSubCache) &&
(!isSelectedSubFamily ||
(Boolean(existingSubCache.turnRailTelemetry) &&
Boolean(existingSubCache.anomalyTelemetry) &&
!hasMissingTurnRailFields(existingSubCache) &&
!needsWakeupTurnRefresh(subConv, existingSubCache) &&
!hasDroppedContextGroups(existingSubCache)));
@@ -291,6 +291,11 @@ function accumulateStepIntoModelMap(perModelStats, st, mInfo, stepCost) {
ttftSumSec: 0,
ttftSampleCount: 0,
speedAccumulator: createSpeedAccumulator(),
anomalySteps: 0,
anomalyCount: 0,
anomalyDangerCount: 0,
anomalyTotalDelaySec: 0,
anomalyByKind: {},
});
}
const agg = perModelStats.get(modelGroupKey);
@@ -315,6 +320,28 @@ function accumulateStepIntoModelMap(perModelStats, st, mInfo, stepCost) {
agg.ttftSampleCount += 1;
}
if (st?.anomaly && Number(st.anomaly.count) > 0) {
agg.anomalySteps += 1;
agg.anomalyCount += Number(st.anomaly.count);
if (st.anomaly.severity === 'danger') {
agg.anomalyDangerCount += 1;
}
if (Number(st.anomaly.totalDelaySec) > 0) {
agg.anomalyTotalDelaySec += Number(st.anomaly.totalDelaySec);
}
if (st.anomaly.kinds && typeof st.anomaly.kinds === 'object') {
for (const [k, v] of Object.entries(st.anomaly.kinds)) {
const cnt = Number(v || 0);
if (cnt > 0) {
agg.anomalyByKind[k] = (agg.anomalyByKind[k] || 0) + cnt;
}
}
} else if (st.anomaly.primaryKind) {
agg.anomalyByKind[st.anomaly.primaryKind] =
(agg.anomalyByKind[st.anomaly.primaryKind] || 0) + Number(st.anomaly.count);
}
}
agg.calls += 1;
agg.inputTokens += inTok;
agg.cacheReadTokens += cacheTok;
@@ -342,6 +369,11 @@ function finalizeModelBreakdownMap(perModelStats) {
fallbackModelId,
ttftSumSec,
ttftSampleCount,
anomalySteps,
anomalyCount,
anomalyDangerCount,
anomalyTotalDelaySec,
anomalyByKind,
...m
}) => {
const sortedUpstream = Array.from((upstreamModelCounts || new Map()).entries())
@@ -355,6 +387,7 @@ function finalizeModelBreakdownMap(perModelStats) {
promptTokens > 0 ? +((m.cacheReadTokens / promptTokens) * 100).toFixed(1) : 0;
const avgTtftSec =
ttftSampleCount > 0 ? roundMetric(ttftSumSec / ttftSampleCount) : null;
const hasAnomalies = Number(anomalyCount || 0) > 0;
return {
...m,
upstreamName: primaryUpstream,
@@ -365,6 +398,15 @@ function finalizeModelBreakdownMap(perModelStats) {
ttftSampleCount,
avgTokPerSec: roundMetric(finishedSpeed.avgTokPerSec),
speedSummary: finishedSpeed,
...(hasAnomalies
? {
anomalySteps: Number(anomalySteps || 0),
anomalyCount: Number(anomalyCount || 0),
anomalyDangerCount: Number(anomalyDangerCount || 0),
anomalyTotalDelaySec: +(Number(anomalyTotalDelaySec || 0)).toFixed(1),
anomalyByKind: anomalyByKind || {},
}
: {}),
};
}
);