feat(panel): dual-mount topbar capsule, live multi-session HUD, and customizable topbar metrics (v1.4.3)

This commit is contained in:
fish
2026-11-11 17:31:42 +09:00
parent d60a3cde9b
commit e1da514515
56 changed files with 13389 additions and 3679 deletions
+533
View File
@@ -0,0 +1,533 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import vm from 'node:vm';
import { backendHarness } from './backend-harness.mjs';
import {
extractContextBreakdown,
extractModelSteps,
normalizeCachedStep,
} from '../speed-metrics.mjs';
import {
formatCountdown,
formatUsd,
speedReasonText,
} from '../src/core/formatters.js';
import { createReactiveStore } from '../src/core/reactive.js';
import { stripModuleSyntax } from '../scripts/build-bundle.mjs';
const nowMs = Date.now();
const nowIso = new Date(nowMs - 5_000).toISOString();
const doneIso = new Date(nowMs - 3_000).toISOString();
test('BUG-01 & INCON-01: Google-proxied Claude/GPT/o-series never misclassify as Gemini, and M429/M322/M149/M20 resolve accurately', () => {
const h = backendHarness({
userStatus: {
cascadeModelConfigData: {
clientModelConfigs: [
{
modelOrAlias: { model: 'MODEL_CLAUDE_PROXY' },
label: 'Claude Opus 4.6 (Google Vertex)',
quotaInfo: { remainingFraction: 0.75, resetTime: new Date(nowMs + 3600_000).toISOString() },
},
],
},
},
});
const infoClaude = h.resolveModelInfo('claude-opus-4-6', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(infoClaude.family, 'Claude');
const infoO3 = h.resolveModelInfo('o3-mini', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(infoO3.family, 'GPT');
// Calling resolveModelInfo with omitted dynamicModelMap must not throw (BUG-03)
const infoDefaultMap = h.resolveModelInfo('MODEL_PLACEHOLDER_M320');
assert.equal(infoDefaultMap.modelId, 'gemini-3.8-flash-low');
assert.equal(infoDefaultMap.pricing?.input, 0.3);
const m429 = h.resolveModelInfo('MODEL_PLACEHOLDER_M429', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(m429.modelId, 'gemini-4-carbon-high');
assert.equal(m429.shortName, 'carbon-4-high');
const m322 = h.resolveModelInfo('MODEL_PLACEHOLDER_M322', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(m322.modelId, 'gemini-3.8-flash-tiered');
assert.equal(
h.computeStepCost(
{
modelKey: 'MODEL_PLACEHOLDER_M322',
inputTokens: 1_000_000,
cacheReadTokens: 0,
outputTokens: 0,
},
m322.pricing
),
0.75
);
const m149 = h.resolveModelInfo('MODEL_PLACEHOLDER_M149', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(m149.modelId, 'gemini-3.5-flash-high');
const m20 = h.resolveModelInfo('MODEL_PLACEHOLDER_M20', new Map(), 'API_PROVIDER_GOOGLE_GEMINI');
assert.equal(m20.modelId, 'gemini-3.5-flash');
});
test('BUG-02 & BUG-11: ide:<uuid> external sessions do not double-count live conversations, cross-account entries are isolated, and orphan subagents never leak to top-level list', async () => {
const stepObj = {
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
sourceTrajectoryStepInfo: { stepIndex: 1 },
createdAt: nowIso,
completedAt: doneIso,
modelUsage: {
model: 'MODEL_PLACEHOLDER_M318',
inputTokens: '10000',
cacheReadTokens: '20000',
outputTokens: '5000',
},
},
};
const h = backendHarness({
userStatus: { email: 'alice@example.com' },
deepStats: {
accountName: 'alice@example.com',
perConvo: {
// Same conversation ID as live 'abcd-1234' (case-insensitive, prefixed with 'ide:') -> must deduplicate!
'ide:AbCd-1234': {
accountName: 'alice@example.com',
appSource: 'ide',
entries: [
{ ts: nowIso, model: 'MODEL_PLACEHOLDER_M318', inp: 10000, cache: 20000, out: 5000, reasoning: 0 },
{ ts: doneIso, model: 'MODEL_PLACEHOLDER_M318', inp: 10000, cache: 20000, out: 5000, reasoning: 0 },
],
},
// Different account -> must be excluded when logged in as alice@example.com!
'ide:bob-conv': {
accountName: 'bob@example.com',
appSource: 'ide',
entries: [
{ ts: nowIso, model: 'MODEL_PLACEHOLDER_M318', inp: 99000, cache: 0, out: 1000, reasoning: 0 },
],
},
},
},
cache: {
'orphan-sub': {
accountName: 'bob@example.com',
isSubagent: true,
parentCascadeId: 'deleted-parent',
signature: 'v10-model-identity:sub',
steps: extractModelSteps([stepObj]),
},
},
summaries: {
'abcd-1234': { stepCount: 1, lastModifiedTime: doneIso, status: 'IDLE' },
},
trajectories: {
'abcd-1234': { trajectory: { steps: [stepObj] } },
},
});
const p = await h.buildTelemetryPayloadInternal('abcd-1234', false, { range: 'all' });
// ide:AbCd-1234 has 2 entries vs live abcd-1234's 1 step -> deduplicated (no 1 + 2 = 3 double counting!), bob excluded!
assert.equal(p.consumption.account.conversationsCount, 1);
assert.equal(p.consumption.account.calls, 2);
assert.equal(p.consumption.account.totalTokens, 70_000);
assert.equal(
p.conversations.some((c) => c.cascadeId === 'orphan-sub'),
false,
'orphan subagent must not appear in top-level conversation dropdown'
);
});
test('BUG-03, BUG-12 & BUG-16: negative/NaN token sanitization, deferred costUsd rounding, and dynamicModelMap default', async () => {
const badStep = {
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
sourceTrajectoryStepInfo: { stepIndex: 1 },
createdAt: nowIso,
completedAt: doneIso,
modelUsage: {
model: 'MODEL_PLACEHOLDER_M318',
inputTokens: '-5000',
cacheReadTokens: 'NaN',
outputTokens: '1000',
thinkingOutputTokens: '2500',
},
},
};
const extracted = extractModelSteps([badStep]);
assert.equal(extracted[0].inputTokens, 0);
assert.equal(extracted[0].cacheReadTokens, 0);
assert.equal(extracted[0].outputTokens, 1000);
assert.equal(extracted[0].thinkingTokens, 1000);
assert.equal(extracted[0].responseTokens, 0);
assert.equal(normalizeCachedStep({ ...extracted[0], inputTokens: -99 }).inputTokens, 0);
assert.equal(normalizeCachedStep(null), null);
// Micro-steps: 100 steps * (1067 output * $3.75 / 1M = $0.00400125) = $0.40 (previously rounded per-step to $0.00 -> $0.00!)
const microSteps = Array.from({ length: 100 }, (_, idx) => ({
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
sourceTrajectoryStepInfo: { stepIndex: idx + 1 },
createdAt: nowIso,
completedAt: doneIso,
modelUsage: {
model: 'MODEL_PLACEHOLDER_M318', // gemini-3.8-flash-high: $0.75/1M input, $3.75/1M output
inputTokens: '0',
cacheReadTokens: '0',
outputTokens: '1067',
},
},
}));
const h = backendHarness({
summaries: { micro: { stepCount: 100, lastModifiedTime: doneIso, status: 'IDLE' } },
trajectories: { micro: { trajectory: { steps: microSteps } } },
});
const p = await h.buildTelemetryPayloadInternal('micro', false, { range: 'today' });
assert.equal(p.consumption.account.costUsd, 0.4);
});
test('BUG-04 & BUG-10: failed RUNNING trajectory fetch enters cooldown instead of hammering RPC every poll, cleared by forceRefresh', async () => {
const h = backendHarness({
summaries: { run1: { stepCount: 2, lastModifiedTime: doneIso, status: 'CASCADE_RUN_STATUS_RUNNING' } },
trajectories: { run1: new Error('LS timeout') },
quotaSummary: new Error('Quota RPC down'),
});
await h.buildTelemetryPayloadInternal('run1', false);
const callsAfterFirst = h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length;
const quotaCallsAfterFirst = h.state.calls.filter((c) => c.method === 'RetrieveUserQuotaSummary').length;
assert.equal(callsAfterFirst, 1);
assert.equal(quotaCallsAfterFirst, 1);
// Immediate second background poll must respect cooldown for both trajectory and quota RPC
await h.buildTelemetryPayloadInternal('run1', false);
assert.equal(h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length, 1);
assert.equal(h.state.calls.filter((c) => c.method === 'RetrieveUserQuotaSummary').length, 1);
// Manual forceRefresh=true bypasses cooldown immediately
await h.buildTelemetryPayloadInternal('run1', true);
assert.equal(h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length, 2);
assert.equal(h.state.calls.filter((c) => c.method === 'RetrieveUserQuotaSummary').length, 2);
});
test('BUG-09, BUG-14 & BUG-15: SemVer 2.0 prerelease/build ordering, future-only exhausted quota inference, and <6h dual-bucket classification', async () => {
const h = backendHarness({
quotaSummary: {
groups: [
{
displayName: 'Gemini',
buckets: [
{
// Omitted remainingFraction with epoch-0 resetTime must NOT falsely report 0% remaining!
resetTime: '1970-01-01T00:00:00Z',
},
],
},
{
displayName: 'Claude & GPT',
buckets: [
{
remainingFraction: 0.4,
resetTime: new Date(nowMs + 2 * 3600_000).toISOString(),
},
{
remainingFraction: 0.8,
// Weekly bucket that happens to be 4h away from resetting!
resetTime: new Date(nowMs + 4 * 3600_000).toISOString(),
},
],
},
],
},
});
assert.equal(h.compareSemver('1.4.2+20261110', '1.4.2'), 0);
assert.equal(h.compareSemver('1.5.0-beta.2', '1.5.0-alpha.10'), 1);
assert.equal(h.compareSemver('1.5.0-alpha.1', '1.5.0-alpha.beta'), -1);
const p = await h.buildTelemetryPayloadInternal('');
const geminiPool = p.quotas.pools.find((x) => x.poolKey === 'Gemini');
assert.ok(geminiPool);
assert.equal(geminiPool.isAvailable, false, 'epoch-0 omitted remainingFraction must not create fake 0% exhausted pool');
assert.equal(geminiPool.remainingPct, null);
assert.equal(geminiPool.usedPct, null);
const claudePool = p.quotas.pools.find((x) => x.poolKey === 'Claude & GPT');
assert.ok(claudePool);
assert.equal(claudePool.remainingPct, 40);
assert.equal(claudePool.weeklyRemainingPct, 80);
assert.equal(formatCountdown(new Date(nowMs + 30_000).toISOString(), nowMs), '<1分钟后重置');
assert.equal(formatCountdown('1969-12-31T23:59:59Z', nowMs), '未知重置时间');
assert.equal(formatUsd(0.0004, { precise: true }), '<$0.001');
assert.equal(formatUsd(12.34, { unpricedCalls: 1, compactPending: true }), '$12.34+');
assert.equal(speedReasonText('short_timing'), '耗时过短 (<0.5s)');
});
test('BUG-17 & CANCEL-06: extractContextBreakdown classifies messagePrompts when children are empty and blocks post-compaction carryover', () => {
const ctx = extractContextBreakdown({
chatStartMetadata: {
contextWindowMetadata: {
estimatedTokensUsed: 10000,
tokenBreakdown: {
groups: [
{ type: 'TOKEN_TYPE_SYSTEM_PROMPT', numTokens: 2000 },
{ type: 'TOKEN_TYPE_CHAT_MESSAGES', numTokens: 8000, children: [] },
],
},
},
},
messagePrompts: [
{ source: 'CHAT_MESSAGE_SOURCE_USER', prompt: '<USER_REQUEST>hello world</USER_REQUEST>' },
{ source: 'CHAT_MESSAGE_SOURCE_MODEL', prompt: 'Assistant reply content here!' },
],
});
assert.ok(ctx);
assert.equal(ctx.systemPromptTokens, 2000);
assert.ok(ctx.userInputTokens > 0);
assert.ok(ctx.assistantTokens > 0);
assert.equal(ctx.userInputTokens + ctx.assistantTokens, 8000);
const stepsWithTrailingCheckpoint = extractModelSteps(
[
{
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
sourceTrajectoryStepInfo: { stepIndex: 1 },
createdAt: nowIso,
completedAt: doneIso,
modelUsage: { model: 'MODEL_PLACEHOLDER_M318', inputTokens: '180000', outputTokens: '500' },
},
},
{
type: 'CORTEX_STEP_TYPE_CHECKPOINT',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
sourceTrajectoryStepInfo: { stepIndex: 2 },
createdAt: nowIso,
completedAt: doneIso,
modelUsage: { model: 'MODEL_PLACEHOLDER_M318', inputTokens: '180000', outputTokens: '1200' },
},
},
],
[
{
stepIndices: [1],
chatModel: {
chatStartMetadata: {
contextWindowMetadata: {
estimatedTokensUsed: 180_000,
tokenBreakdown: {
groups: [{ type: 'TOKEN_TYPE_CHAT_MESSAGES', numTokens: 180_000 }],
},
},
},
},
},
]
);
assert.equal(stepsWithTrailingCheckpoint.length, 2);
assert.equal(stepsWithTrailingCheckpoint[1].isCompactionStep, true);
assert.equal(
stepsWithTrailingCheckpoint[1].contextBreakdown,
null,
'compaction checkpoint must not inherit pre-compaction 180k contextBreakdown'
);
});
test('BUG-20 & BUG-24: reactive store microtask deduplication, stripModuleSyntax safety, and IIFE bundle isolation', async () => {
const store = createReactiveStore({ a: 1, b: 2 });
let calls = 0;
store.subscribe((s) => s.a + s.b, () => {
calls += 1;
}, { immediate: false });
store.setState({ a: 10 });
store.setState({ b: 20 });
await new Promise((r) => queueMicrotask(r));
assert.equal(calls, 1, 'multiple setState calls in same microtask must batch into 1 subscriber flush');
const trickyModule = [
"import './side-effect.js';",
"import { foo,\n bar } from './multi.js';",
'const tpl = `\nimport { keepMe } from "tpl";\nexport const KEEP_ME = 42;\n`;',
'export async function runTask() { return tpl; }',
'export { runTask };',
].join('\n');
const stripped = stripModuleSyntax(trickyModule);
assert.ok(stripped.includes('import { keepMe } from "tpl"'));
assert.ok(stripped.includes('export const KEEP_ME = 42'));
assert.equal(/^\s*import\s/m.test(stripped.replace(/`[\s\S]*?`/g, '')), false);
assert.doesNotThrow(() => new vm.Script(stripped));
const appBundle = fs.readFileSync(new URL('../app.js', import.meta.url), 'utf8');
assert.ok(appBundle.startsWith('(() => {\n\'use strict\';'));
assert.ok(appBundle.trimEnd().endsWith('})();'));
});
test('Compaction liveActivity: detects in-progress & completed CHECKPOINT over pre-compaction tool operations across RPC and transcript.jsonl', async () => {
const convId = '4432b724-3e1e-4421-a375-c6855a7b7b37';
// Pre-compaction steps (matching real-world step 0..2: planner edit_file x2 + 2 completed tool results)
const rpcPreCompactionSteps = [
{
type: 'CORTEX_STEP_TYPE_PLANNER_RESPONSE',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
createdAt: '2026-11-11T02:18:21Z',
finishedGeneratingAt: '2026-11-11T02:18:35Z',
modelUsage: {
model: 'MODEL_PLACEHOLDER_M318',
inputTokens: '2918',
cacheReadTokens: '252465',
outputTokens: '814',
},
},
plannerResponse: {
toolCalls: [
{ name: 'edit_file', argumentsJson: JSON.stringify({ Path: 'f:/repo/backend-harness.mjs' }) },
{ name: 'edit_file', argumentsJson: JSON.stringify({ Path: 'f:/repo/04-post-fix-verify.mjs' }) },
],
},
},
{
type: 'CORTEX_STEP_TYPE_CODE_ACTION',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: { createdAt: '2026-11-11T02:18:35Z', completedAt: '2026-11-11T02:18:38Z' },
},
{
type: 'CORTEX_STEP_TYPE_CODE_ACTION',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: { createdAt: '2026-11-11T02:18:35Z', completedAt: '2026-11-11T02:18:40Z' },
},
];
// transcript.jsonl still ends at step_index 2 (since CHECKPOINT only flushes to transcript.jsonl when DONE)
const transcriptPreFlush = [
{
step_index: 0,
source: 'MODEL',
type: 'PLANNER_RESPONSE',
status: 'DONE',
created_at: '2026-11-11T02:18:21Z',
tool_calls: [
{ name: 'edit_file', args: { Path: '"f:\\\\repo\\\\backend-harness.mjs"' } },
{ name: 'edit_file', args: { Path: '"f:\\\\repo\\\\04-post-fix-verify.mjs"' } },
],
},
{
step_index: 1,
source: 'MODEL',
type: 'GENERIC',
status: 'DONE',
created_at: '2026-11-11T02:18:35Z',
content: 'Created At: 2026-11-11T11:18:35+09:00\nCompleted At: 2026-11-11T11:18:38+09:00\nMade 1 replacement',
},
{
step_index: 2,
source: 'MODEL',
type: 'GENERIC',
status: 'DONE',
created_at: '2026-11-11T02:18:35Z',
content: 'Created At: 2026-11-11T11:18:35+09:00\nCompleted At: 2026-11-11T11:18:40+09:00\nMade 1 replacement',
},
];
// Phase 1: In-progress CORTEX_STEP_TYPE_CHECKPOINT in RPC WITHOUT metadata.createdAt yet + trailing MEMENTO
const h = backendHarness({
summaries: {
[convId]: {
stepCount: 4,
lastModifiedTime: '2026-11-11T02:18:35Z',
status: 'CASCADE_RUN_STATUS_RUNNING',
annotations: { title: 'Audit & Fix Session' },
},
},
trajectories: {
[convId]: {
trajectory: {
steps: [
...rpcPreCompactionSteps,
{
type: 'CORTEX_STEP_TYPE_CHECKPOINT',
status: 'CORTEX_STEP_STATUS_RUNNING',
metadata: {},
},
{
type: 'CORTEX_STEP_TYPE_MEMENTO',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: { createdAt: '2026-11-11T02:18:40Z' },
},
],
},
},
},
transcripts: {
[convId]: transcriptPreFlush,
},
});
const pCompacting = await h.buildTelemetryPayloadInternal(convId);
assert.equal(pCompacting.activeConversation.liveActivity.phase, 'compacting');
assert.equal(pCompacting.activeConversation.liveActivity.label, '上下文压缩');
assert.equal(
pCompacting.activeConversation.liveActivity.startedAtMs,
Date.parse('2026-11-11T02:18:40Z'),
'uses previous tool completion timestamp when in-progress CHECKPOINT lacks createdAt'
);
assert.equal(pCompacting.runningConversations[0].mainActivity.phase, 'compacting');
assert.equal(pCompacting.runningConversations[0].mainActivity.label, '上下文压缩');
// Phase 2: CHECKPOINT finishes (status=DONE in both RPC & transcript.jsonl) while resuming before next PLANNER_RESPONSE
h.state.trajectories[convId] = {
trajectory: {
steps: [
...rpcPreCompactionSteps,
{
type: 'CORTEX_STEP_TYPE_CHECKPOINT',
status: 'CORTEX_STEP_STATUS_DONE',
metadata: {
createdAt: '2026-11-11T02:18:40Z',
finishedGeneratingAt: '2026-11-11T02:19:33Z',
modelUsage: {
model: 'MODEL_PLACEHOLDER_M318',
inputTokens: '3082',
cacheReadTokens: '254224',
outputTokens: '4627',
},
},
},
],
},
};
h.state.transcripts[convId] = [
...transcriptPreFlush,
{
step_index: 3,
source: 'SYSTEM',
type: 'CHECKPOINT',
status: 'DONE',
created_at: '2026-11-11T02:18:40Z',
content: '# Resuming from a compaction\n...',
},
];
h.state.transcriptMtimeMs = 2;
const pResuming = await h.buildTelemetryPayloadInternal(convId);
assert.equal(pResuming.activeConversation.liveActivity.phase, 'compacting');
assert.equal(pResuming.activeConversation.liveActivity.label, '上下文压缩');
assert.equal(pResuming.activeConversation.liveActivity.detail, '已完成压缩,正在恢复推理');
assert.equal(
pResuming.activeConversation.liveActivity.startedAtMs,
Date.parse('2026-11-11T02:19:33Z')
);
// Phase 3: Even when RPC fails/cools down and ONLY transcript.jsonl (with status="DONE" CHECKPOINT) is available,
// it STILL detects compaction instead of regressing to pre-compaction "修改文件"
h.state.trajectories[convId] = new Error('temporary RPC timeout');
const pTranscriptOnly = await h.buildTelemetryPayloadInternal(convId, true);
assert.equal(pTranscriptOnly.activeConversation.liveActivity.phase, 'compacting');
assert.equal(pTranscriptOnly.activeConversation.liveActivity.label, '上下文压缩');
});
+47 -11
View File
@@ -2,6 +2,16 @@ import fs from 'node:fs';
import path from 'node:path';
import vm from 'node:vm';
import * as metrics from '../speed-metrics.mjs';
import { stripModuleSyntax } from '../scripts/build-bundle.mjs';
const REAL_CATALOG_RAW = JSON.parse(
fs.readFileSync(new URL('../../../pricing-catalog.json', import.meta.url), 'utf8')
);
const DEFAULT_BUNDLED_CATALOG = {
schemaVersion: 1,
pricing: REAL_CATALOG_RAW.pricing,
placeholders: REAL_CATALOG_RAW.placeholders,
};
const BACKEND_MODULE_FILES = [
'../backend/config.mjs',
@@ -13,6 +23,7 @@ const BACKEND_MODULE_FILES = [
'../backend/local-scanner.mjs',
'../backend/usage-aggregator.mjs',
'../backend/telemetry-builder.mjs',
'../backend/window-bridge.mjs',
'../main.mjs',
];
@@ -23,20 +34,19 @@ function loadCombinedBackendSource() {
}
function stripEsmSyntax(rawSource, mainUrl) {
return rawSource
.replace(/^\s*import\s+[\s\S]*?from\s+['"][^'"]+['"];?\r?\n/gm, '')
.replace(/^\s*export\s+(?=(?:async\s+)?function\b|const\b|let\b|class\b)/gm, '')
.replaceAll('import.meta.url', JSON.stringify(mainUrl));
return stripModuleSyntax(rawSource).replaceAll('import.meta.url', JSON.stringify(mainUrl));
}
export function backendHarness({
cache={},
deepStats=null,
transcripts={},
settings={},
trajectories={},
summaries={},
userStatus={},
quotaSummary={groups:[]},
localCatalog=null,
localCatalog=DEFAULT_BUNDLED_CATALOG,
remoteCatalog=null,
pluginManifest=null,
hasGitDir=false,
@@ -49,7 +59,7 @@ export function backendHarness({
const mainUrl = pluginRootPath ? `file://${pluginRootPath}/sidecars/panel/main.mjs` : 'file:///test/sidecars/panel/main.mjs';
const mainPath = pluginRootPath ? `${pluginRootPath}/sidecars/panel/main.mjs` : '/test/sidecars/panel/main.mjs';
const source = stripEsmSyntax(sourceText ?? loadCombinedBackendSource(), mainUrl);
const state={calls:[],errors:[],saves:[],trajectories,userStatus,quotaSummary,localCatalog,remoteCatalog,pluginManifest,hasGitDir};
const state={calls:[],errors:[],saves:[],trajectories,transcripts,transcriptMtimeMs:1,userStatus,quotaSummary,localCatalog,remoteCatalog,pluginManifest,hasGitDir,deepStats,deepStatsMtimeMs:1};
const ctx={
...metricsImplementation, URL, AbortSignal, Buffer,
process:{platform:'win32',env:{ANTIGRAVITY_EXECUTABLE_DATA_DIR:'/test-data',ANTIGRAVITY_LS_ADDRESS:'test.invalid',ANTIGRAVITY_CSRF_TOKEN:'test'}},
@@ -63,16 +73,42 @@ export function backendHarness({
existsSync:p=>{
const norm = String(p).replace(/\\/g, '/');
if (norm.endsWith('settings.json') || norm.endsWith('trajectory_usage_cache.json')) return true;
if (norm.endsWith('/antigravity/brain/.deep_stats_cache.json')) return Boolean(state.deepStats);
if (norm.endsWith('pricing-catalog.json')) return Boolean(state.localCatalog);
if (norm.endsWith('remote_pricing_cache.json')) return Boolean(state.remoteCatalog);
if (norm.endsWith('plugin.json')) return Boolean(state.pluginManifest);
if (norm.endsWith('/.system_generated/logs/transcript.jsonl')) {
const m = norm.match(/\/brain\/([^/]+)\/\.system_generated\/logs\/transcript\.jsonl$/i);
return Boolean(m && state.transcripts?.[m[1]]);
}
if (norm.endsWith('/.git')) return Boolean(state.hasGitDir);
return false;
},
mkdirSync:()=>{},readdirSync:()=>[],statSync:()=>({mtimeMs:0}),
mkdirSync:()=>{},readdirSync:()=>[],
statSync:p=>{
const norm = String(p).replace(/\\/g, '/');
if (norm.endsWith('/.system_generated/logs/transcript.jsonl')) {
const m = norm.match(/\/brain\/([^/]+)\/\.system_generated\/logs\/transcript\.jsonl$/i);
const lines = m && state.transcripts?.[m[1]];
if (lines) {
const text = Array.isArray(lines) ? lines.map(x => JSON.stringify(x)).join('\n') : String(lines);
return {mtimeMs:state.transcriptMtimeMs,size:Buffer.byteLength(text,'utf8')};
}
}
if (norm.endsWith('/antigravity/brain/.deep_stats_cache.json') && state.deepStats) {
return {mtimeMs:state.deepStatsMtimeMs,size:JSON.stringify(state.deepStats).length};
}
return {mtimeMs:0,size:0};
},
readFileSync:p=>{
const norm = String(p).replace(/\\/g, '/');
if (norm.endsWith('/.system_generated/logs/transcript.jsonl')) {
const m = norm.match(/\/brain\/([^/]+)\/\.system_generated\/logs\/transcript\.jsonl$/i);
const lines = m && state.transcripts?.[m[1]];
return Array.isArray(lines) ? lines.map(x => JSON.stringify(x)).join('\n') : String(lines || '');
}
if (norm.endsWith('settings.json')) return JSON.stringify(settings);
if (norm.endsWith('/antigravity/brain/.deep_stats_cache.json')) return JSON.stringify(state.deepStats);
if (norm.endsWith('pricing-catalog.json')) return JSON.stringify(state.localCatalog);
if (norm.endsWith('remote_pricing_cache.json')) return JSON.stringify(state.remoteCatalog);
if (norm.endsWith('plugin.json')) return JSON.stringify(state.pluginManifest);
@@ -89,7 +125,6 @@ export function backendHarness({
homedir:()=>'/test-home',dirname:path.dirname,join:path.join,basename:path.basename,fileURLToPath:()=>mainPath,
Response:class {constructor(body,options){this.body=body;this.options=options;}},
SidecarApp:class {page(){} api(){} run(){}},
StreamDiagnostics:class {status(){return {enabled:false};} start(){}},
fetch:async(url,options={})=>{
if (/^https?:\/\/(?!test\.invalid)/i.test(String(url)) && remoteFetch) {
return remoteFetch(url, options);
@@ -101,13 +136,14 @@ export function backendHarness({
return {ok:true,json:async()=>data||{}};
}
if(method==='RetrieveUserQuotaSummary') {
if(state.quotaSummary instanceof Error) throw state.quotaSummary;
return {ok:true,json:async()=>({response:state.quotaSummary})};
const qs=state.quotaSummary;
if(qs instanceof Error) throw qs;
return {ok:true,json:async()=>({response:qs})};
}
const data={GetUserStatus:{userStatus:state.userStatus},GetAllCascadeTrajectories:{trajectorySummaries:summaries}}[method];
return {ok:true,json:async()=>data};
},
};
vm.runInNewContext(source+'\n globalThis.api={buildTelemetryPayloadInternal,computeStepCost,resolveModelInfo,sanitizeCatalogPayload,rebuildActiveCatalog,syncCloudCatalog,getCloudStatus,compareSemver,performPluginUpdate};',ctx);
vm.runInNewContext(source+'\n globalThis.api={buildTelemetryPayloadInternal,computeStepCost,resolveModelInfo,inferFamilyFromProviderOrKey,isCarbonModelIdentifier,PLACEHOLDER_MAP,normalizeQuotaFraction,classifyOfficialBucketWindow,classifyQuotaPoolIdentity,buildOfficialQuotaPools,resolveTimeWindow,sanitizeCatalogPayload,rebuildActiveCatalog,syncCloudCatalog,getCloudStatus,isValidSemver,compareSemver,performPluginUpdate};',ctx);
return {...ctx.api,state};
}
+1 -1
View File
@@ -158,7 +158,7 @@ test('old ambiguous Argon cache is persisted as unresolved while retaining moder
await h.buildTelemetryPayloadInternal('test');
assert.equal(h.state.saves.length,1,'do not rewrite identical migrated cache every poll');
h.state.trajectories.test = {trajectory:{steps:[step(211,FLASH,true)]}};
const restored = await h.buildTelemetryPayloadInternal('test');
const restored = await h.buildTelemetryPayloadInternal('test', true);
assert.equal(restored.activeConversation.activeModelShort,'flash-3.8-high·压缩');
assert.equal(restored.consumption.account.unpricedCalls,0);
});
@@ -1,194 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { ConnectJsonDecoder, StreamObservation, StreamDiagnostics, encodeConnectJson } from '../stream-diagnostics.mjs';
const ID = '12345678-1234-1234-1234-123456789abc';
const CREATED = '2026-10-15T01:00:00.000Z';
const FINISHED = '2026-10-15T01:00:08.000Z';
const event = (ms, batch = ms) => ({ elapsedMs: ms, receiveBatch: batch, receivedAt: CREATED });
const model = (p = {}, done = false, extra = {}) => ({
type: 'CORTEX_STEP_TYPE_PLANNER_RESPONSE', status: done ? 'CORTEX_STEP_STATUS_DONE' : 'CORTEX_STEP_STATUS_STREAMING',
metadata: { createdAt: CREATED, ...(done ? { finishedGeneratingAt: FINISHED, modelUsage: { outputTokens: '400' } } : {}) },
plannerResponse: p, ...extra,
});
const update = (indices, steps, totalLength, more = {}) => ({ update: { conversationId: ID,
mainTrajectoryUpdate: { stepsUpdate: { indices, steps, totalLength }, ...more } } });
function observer() {
const records = [];
const observation = new StreamObservation({ conversationId: ID, connectedWallMs: Date.parse(CREATED) - 1000, emit: r => records.push(r) });
return { records, observation };
}
test('Connect decoder reconstructs fragmented headers/UTF-8 and keeps coalesced messages separate', () => {
const a = { update: { text: '中文🙂' } };
const b = { update: { text: 'second' } };
const bytes = Buffer.concat([encodeConnectJson(a), encodeConnectJson(b)]);
const decoder = new ConnectJsonDecoder();
const messages = [];
for (let i = 0; i < bytes.length; i += 3) messages.push(...decoder.push(bytes.subarray(i, i + 3)));
decoder.finish();
assert.deepEqual(messages.map(m => m.value), [a, b]);
const end = encodeConnectJson({}); end[0] = 2;
assert.equal(decoder.push(end)[0].end, true);
assert.throws(() => decoder.push(encodeConnectJson(a)), /data_after_end/);
});
test('Connect decoder rejects oversized, compressed, malformed and truncated frames', () => {
const huge = Buffer.from([0, 0, 0, 1, 0]);
assert.throws(() => new ConnectJsonDecoder(100).push(huge), /frame_too_large/);
const compressed = encodeConnectJson({}); compressed[0] = 1;
assert.throws(() => new ConnectJsonDecoder().push(compressed), /unsupported_frame_flags/);
assert.throws(() => new ConnectJsonDecoder().push(Buffer.from([0, 0, 0, 0, 1, 120])), /invalid_frame_json/);
const decoder = new ConnectJsonDecoder(); decoder.push(Buffer.from([0, 0]));
assert.throws(() => decoder.finish(), /truncated_frame/);
});
test('history, delayed rehydration and status-only notifications never become live output', () => {
const { observation: o, records } = observer();
o.accept({ update: { conversationId: ID, status: 'IDLE' } }, event(0));
assert.equal(o.initialized, false);
o.accept(update([9], [model({ response: 'old response' }, true)], 10), event(1));
o.accept(update([4], [model({ response: 'late history' }, true)], 10), event(2));
assert.equal(o.contentEvents, 0);
assert.equal(o.recent.length, 0);
assert.deepEqual(records.map(r => r.kind), ['baseline']);
});
test('tool arguments, thinking and text are counted independently; receive batches are not tokens', () => {
const { observation: o, records } = observer();
o.accept(update([], [], 10), event(0));
o.accept(update([10], [model()], 11), event(10));
o.accept(update([10], [model({ thinking: 'hidden-secret', response: '中', toolCalls: [{ argumentsJson: '{', name: 'secret-tool' }] })], 11), event(20, 2));
o.accept(update([10], [model({ thinking: 'hidden-secret!', response: '中文', toolCalls: [{ argumentsJson: '{"secret":"' }] })], 11), event(20, 2));
const full = model({ thinking: 'hidden-secret!', response: '中文', toolCalls: [{ argumentsJson: '{"secret":"argument-secret"}' }] }, true);
o.accept(update([10], [full], 11), event(120, 3));
o.accept(update([10], [full], 11), event(300, 4));
o.accept(update([], [], 11, { generatorMetadatasUpdate: { generatorMetadatas: [{ stepIndices: [10], chatModel: {
usage: { outputTokens: '400', thinkingOutputTokens: '50', responseHeader: { authorization: 'credential-secret' } },
timeToFirstToken: '7.999s', streamingDuration: '0.001s', systemPrompt: 'prompt-secret',
} }] } }), event(400, 5));
const r = o.recent[0];
assert.equal(r.contentEvents, 3);
assert.equal(r.contentReceiveBatches, 2);
assert.equal(r.toolArgumentEvents, 3);
assert.equal(r.observedSpanMs, 100);
assert.equal(r.channels.text.bytes, 6);
assert.equal(r.channels.text.utf16Units, 2);
assert.equal(r.usage.outputTokens, 400);
assert.equal(r.nativeStreamSec, 0.001);
assert.equal(r.partialCapture, false);
const serialized = JSON.stringify([records, r]);
for (const secret of ['hidden-secret', 'argument-secret', 'secret-tool', 'credential-secret', 'prompt-secret', '中文']) assert.ok(!serialized.includes(secret));
assert.ok(!/tokPerSec|tps|firstTokenMs/i.test(serialized));
});
test('same-batch delivery is not reported as a timed stream, even with several updates', () => {
const { observation: o } = observer();
o.accept(update([], [], 0), event(0));
o.accept(update([0], [model({ response: 'a' })], 1), event(5, 1));
o.accept(update([0], [model({ response: 'ab' }, true)], 1), event(5, 1));
assert.equal(o.recent[0].observation, 'single_receive_batch');
assert.equal(o.recent[0].observedSpanMs, 0);
});
test('rewrites, shrinking output and tool-only updates are retained as revisions', () => {
const { observation: o, records } = observer();
o.accept(update([], [], 0), event(0));
o.accept(update([0], [model({ toolCalls: [{ argumentsJson: 'abcd' }] })], 1), event(10));
o.accept(update([0], [model({ toolCalls: [{ argumentsJson: 'wxyz' }] })], 1), event(20));
o.accept(update([0], [model({ toolCalls: [{ argumentsJson: 'wx' }] }, true)], 1), event(30));
assert.equal(o.recent[0].revisions, 2);
assert.equal(o.recent[0].toolArgumentEvents, 3);
assert.equal(records.at(-1).changes.toolArguments.deltaBytes, -2);
assert.equal(records.at(-1).changes.toolArguments.appendOnly, false);
});
test('joining during generation marks partial capture and excludes initial content from updates', () => {
const { observation: o } = observer();
o.accept(update([2], [model({ response: 'already there' })], 3), event(10));
o.accept(update([2], [model({ response: 'already there plus more' }, true)], 3), event(20));
assert.equal(o.recent[0].partialCapture, true);
assert.equal(o.recent[0].contentEvents, 1);
assert.equal(o.recent[0].observation, 'partial_capture');
});
test('missing or earlier creation time never claims a complete observation', () => {
const { observation: o } = observer();
o.accept(update([], [], 0), event(0));
o.accept(update([0], [model({ response: 'arrived' }, true, { metadata: { finishedGeneratingAt: FINISHED } })], 1), event(20));
assert.equal(o.recent[0].partialCapture, true);
assert.throws(() => o.accept(update([], [model()], 2), event(25)), /invalid_step_indices/);
});
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function until(check) {
for (let i = 0; i < 100; i++) { if (check()) return; await sleep(10); }
assert.fail('timed out');
}
function temp(t) {
const dir = mkdtempSync(join(tmpdir(), 'usage-monitor-diag-test-'));
t.after(() => {
const absolute = resolve(dir);
assert.equal(dirname(absolute), resolve(tmpdir()));
assert.ok(absolute.startsWith(join(resolve(tmpdir()), 'usage-monitor-diag-test-')));
rmSync(absolute, { recursive: true, force: true });
});
return dir;
}
test('collector only subscribes to read APIs, persists counters and stops without touching billing', async t => {
const dir = temp(t);
const calls = [];
let streamController;
const fetchImpl = async (url, options) => {
calls.push({ url, options });
if (url.endsWith('GetAllCascadeTrajectories')) return new Response(JSON.stringify({ trajectorySummaries: { [ID]: { stepCount: 0, lastModifiedTime: CREATED } } }));
assert.ok(url.endsWith('StreamAgentStateUpdates'));
const request = new ConnectJsonDecoder().push(options.body)[0].value;
assert.equal(request.trajectoryVerbosity, 'CLIENT_TRAJECTORY_VERBOSITY_FULL');
const stream = new ReadableStream({ start(c) {
streamController = c;
c.enqueue(encodeConnectJson(update([], [], 0)));
options.signal.addEventListener('abort', () => c.error(new Error('aborted')), { once: true });
} });
return new Response(stream, { headers: { 'Content-Type': 'application/connect+json' } });
};
const d = new StreamDiagnostics({ dataDir: dir, lsAddress: '127.0.0.1:1', csrfToken: 'secret-csrf', fetchImpl, pollMs: 60000 });
t.after(() => d.stop());
assert.equal(d.status().enabled, false);
d.configure({ enabled: true });
await until(() => d.status().phase === 'collecting');
streamController.enqueue(encodeConnectJson(update([0], [model({ toolCalls: [{ argumentsJson: 'private-arguments' }] }, true)], 1)));
await until(() => d.status().contentEvents === 1);
assert.equal(d.status().affectsTps, false);
d.configure({ enabled: false });
await sleep(10);
assert.equal(d.status().phase, 'disabled');
assert.equal(d.current, null);
const log = readFileSync(join(dir, 'stream-diagnostics', 'events.jsonl'), 'utf8');
assert.ok(!log.includes('secret-csrf'));
assert.ok(!log.includes('private-arguments'));
assert.ok(log.includes('step_update'));
assert.deepEqual(readdirSync(dir).sort(), ['stream-diagnostics', 'stream-diagnostics-config.json']);
assert.equal(calls.length, 2);
});
test('failed subscriptions back off and never expose the server error body', async t => {
const dir = temp(t);
let calls = 0;
const d = new StreamDiagnostics({ dataDir: dir, lsAddress: '127.0.0.1:1', csrfToken: 'csrf', pollMs: 10, retryMs: 5000,
fetchImpl: async url => {
calls++;
if (url.endsWith('GetAllCascadeTrajectories')) return new Response(JSON.stringify({ trajectorySummaries: { [ID]: {} } }));
return new Response('private server error with credentials', { status: 403 });
} });
t.after(() => d.stop());
d.configure({ enabled: true });
await until(() => d.status().error === 'stream_http_403');
await sleep(40);
assert.equal(calls, 2);
assert.ok(!JSON.stringify(d.status()).includes('private'));
});
File diff suppressed because it is too large Load Diff