mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
feat(panel): add standalone Web monitor mode and full Antigravity CLI 1.5.9 telemetry support
- Add zero-SDK standalone HTTP server (`standalone.mjs` / `standalone-server.mjs`) with responsive centered Web shell, `127.0.0.1` default loopback guard, mandatory `--token` enforcement on non-loopback binds (`HttpOnly; SameSite=Strict` cookie + `Bearer` auth), and optional background auto-hosting via `standalonePort` / `USAGE_MONITOR_STANDALONE_PORT`. - Extend multi-root data discovery across `~/.gemini/antigravity` and `~/.gemini/antigravity-cli`, plus `daemon/hub_*.json` and `/proc` + `ss` Language Server endpoint discovery on minimal headless Linux hosts without `lsof`. - Add read-only `node:sqlite` + lightweight protobuf wire reader (`cli-sqlite-reader.mjs`) to recover CLI session titles (`annotations/*.pbtxt` & `conversation_summaries.db`), nanosecond request duration/TPS, native TTFT, thinking/response token split, 10-channel context breakdown, and `Antigravity CLI` source attribution. - Add regression tests (124 passing) and bilingual CLI / SSH tunnel / `--token` documentation in `README.zh-CN.md` and `README.md`. Closes #18
This commit is contained in:
@@ -21,6 +21,7 @@ const BACKEND_MODULE_FILES = [
|
||||
'../backend/account-switch.mjs',
|
||||
'../backend/settings-schema.mjs',
|
||||
'../backend/config.mjs',
|
||||
'../backend/cli-sqlite-reader.mjs',
|
||||
'../backend/ls-discovery.mjs',
|
||||
'../backend/pricing-catalog.mjs',
|
||||
'../backend/plugin-updater.mjs',
|
||||
@@ -49,6 +50,7 @@ export function backendHarness({
|
||||
cache={},
|
||||
deepStats=null,
|
||||
transcripts={},
|
||||
diskDirs={},
|
||||
settings={},
|
||||
trajectories={},
|
||||
summaries={},
|
||||
@@ -69,7 +71,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:[],settings,trajectories,transcripts,transcriptMtimeMs:1,userStatus,quotaSummary,localCatalog,remoteCatalog,pluginManifest,hasGitDir,deepStats,deepStatsMtimeMs:1};
|
||||
const state={calls:[],errors:[],saves:[],settings,trajectories,summaries,transcripts,transcriptMtimeMs:1,userStatus,quotaSummary,localCatalog,remoteCatalog,pluginManifest,hasGitDir,deepStats,deepStatsMtimeMs:1};
|
||||
const ctx={
|
||||
...metricsImplementation, URL, AbortSignal, Buffer, isDeepStrictEqual, randomUUID,
|
||||
...(clock ? { Date: class extends Date {
|
||||
@@ -96,9 +98,15 @@ export function backendHarness({
|
||||
return Boolean(m && state.transcripts?.[m[1]]);
|
||||
}
|
||||
if (norm.endsWith('/.git')) return Boolean(state.hasGitDir);
|
||||
if (diskDirs && Object.prototype.hasOwnProperty.call(diskDirs, norm)) return true;
|
||||
return false;
|
||||
},
|
||||
mkdirSync:()=>{},readdirSync:()=>[],
|
||||
mkdirSync:()=>{},
|
||||
readdirSync:p=>{
|
||||
const norm = String(p).replace(/\\/g, '/');
|
||||
if (diskDirs && Array.isArray(diskDirs[norm])) return diskDirs[norm];
|
||||
return [];
|
||||
},
|
||||
statSync:p=>{
|
||||
const norm = String(p).replace(/\\/g, '/');
|
||||
if (norm.endsWith('/.system_generated/logs/transcript.jsonl')) {
|
||||
@@ -160,10 +168,10 @@ export function backendHarness({
|
||||
if(qs instanceof Error) throw qs;
|
||||
return {ok:true,json:async()=>({response:qs})};
|
||||
}
|
||||
const data={GetUserStatus:{userStatus:state.userStatus},GetAvailableModels:{response:{models:availableModels}},GetAllCascadeTrajectories:{trajectorySummaries:summaries}}[method];
|
||||
const data={GetUserStatus:{userStatus:state.userStatus},GetAvailableModels:{response:{models:availableModels}},GetAllCascadeTrajectories:{trajectorySummaries:state.summaries}}[method];
|
||||
return {ok:true,json:async()=>data};
|
||||
},
|
||||
};
|
||||
vm.runInNewContext(source+'\n globalThis.api={buildTelemetryPayloadInternal,executeTelemetryAction,computeStepCost,resolveModelInfo,inferFamilyFromProviderOrKey,isArgonModelIdentifier,isCarbonModelIdentifier,getBaseModelLabel,formatUpstreamModelId,PLACEHOLDER_MAP,normalizeQuotaFraction,classifyOfficialBucketWindow,classifyQuotaPoolIdentity,buildOfficialQuotaPools,resolveTimeWindow,sanitizeCatalogPayload,rebuildActiveCatalog,syncCloudCatalog,getCloudStatus,isValidSemver,compareSemver,performPluginUpdate,resolveMigratedGitRemoteUrl,loadSettings,executeSettingsAction,executeModelContextWindowAction,executeResetSettingsAction,executeRebuildCacheAction,clearRuntimeCaches,normalizeTopbarCapsuleSlots,normalizeHoverModules,normalizeOverviewModules,normalizeOverviewMetrics,normalizeStepTableColumns};',ctx);
|
||||
vm.runInNewContext(source+'\n globalThis.api={buildTelemetryPayloadInternal,executeTelemetryAction,computeStepCost,resolveModelInfo,inferFamilyFromProviderOrKey,isArgonModelIdentifier,isCarbonModelIdentifier,getBaseModelLabel,formatUpstreamModelId,PLACEHOLDER_MAP,normalizeQuotaFraction,classifyOfficialBucketWindow,classifyQuotaPoolIdentity,buildOfficialQuotaPools,resolveTimeWindow,sanitizeCatalogPayload,rebuildActiveCatalog,syncCloudCatalog,getCloudStatus,isValidSemver,compareSemver,performPluginUpdate,resolveMigratedGitRemoteUrl,loadSettings,executeSettingsAction,executeModelContextWindowAction,executeResetSettingsAction,executeRebuildCacheAction,clearRuntimeCaches,normalizeTopbarCapsuleSlots,normalizeHoverModules,normalizeOverviewModules,normalizeOverviewMetrics,normalizeStepTableColumns,normalizeStandalonePort,setStandalonePortListener,parseAnnotationPbtxtTitle};',ctx);
|
||||
return {...ctx.api,state};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {backendHarness} from './backend-harness.mjs';
|
||||
import {
|
||||
getNodeSqliteDatabaseSync,
|
||||
parseAnnotationPbtxtTitle,
|
||||
readAnnotationTitleForCascade,
|
||||
readLocalSqliteTrajectory,
|
||||
readLocalSummaryIndex,
|
||||
} from '../backend/cli-sqlite-reader.mjs';
|
||||
import { writeTrajectoryCacheFile } from '../backend/trajectory-cache-file.mjs';
|
||||
|
||||
const raw={trajectory:{steps:[{status:'CORTEX_STEP_STATUS_DONE',metadata:{
|
||||
@@ -1316,3 +1324,439 @@ test('reconciles 8-group context breakdown with real API prompt+output tokens an
|
||||
const fetchCountAfterSecond = h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length;
|
||||
assert.equal(fetchCountAfterSecond, 1);
|
||||
});
|
||||
|
||||
test('antigravity-cli multi-dir transcript fallback recovers offline sessions and upgrades once LS comes online', async () => {
|
||||
const cliId = '88888888-4444-4444-8888-111111111111';
|
||||
const h = backendHarness({
|
||||
summaries: {},
|
||||
trajectories: {
|
||||
[cliId]: new Error('Language Server offline'),
|
||||
},
|
||||
diskDirs: {
|
||||
'/test-home/.gemini/antigravity-cli/brain': [cliId],
|
||||
},
|
||||
deepStats: {
|
||||
titleMap: {},
|
||||
perConvo: {
|
||||
[`cli:${cliId}`]: {
|
||||
appSource: 'cli',
|
||||
entries: [
|
||||
{
|
||||
ts: '2026-11-15T12:00:02Z',
|
||||
model: 'claude-sonnet-4-6',
|
||||
modelCanonicalId: 'claude-sonnet-4-6',
|
||||
modelDisplayName: 'Claude Sonnet 4.6',
|
||||
provider: 'API_PROVIDER_ANTHROPIC_VERTEX',
|
||||
inp: 4000,
|
||||
cache: 16000,
|
||||
out: 300,
|
||||
reasoning: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
transcripts: {
|
||||
[cliId]: [
|
||||
{
|
||||
step_index: 1,
|
||||
source: 'USER_EXPLICIT',
|
||||
type: 'USER_INPUT',
|
||||
status: 'DONE',
|
||||
created_at: '2026-11-15T12:00:00Z',
|
||||
content: '<USER_REQUEST>\n排查无头 Linux CLI 远程监控配置\n</USER_REQUEST>',
|
||||
},
|
||||
{
|
||||
step_index: 2,
|
||||
source: 'MODEL',
|
||||
type: 'PLANNER_RESPONSE',
|
||||
status: 'DONE',
|
||||
created_at: '2026-11-15T12:00:02Z',
|
||||
input_tokens: 4000,
|
||||
cache_read_tokens: 16000,
|
||||
output_tokens: 400,
|
||||
content: '已完成排查。',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
// 1. Offline mode: recovers CLI conversation list, prompt-derived title, turn rail, and token accounting
|
||||
const offlinePayload = await h.buildTelemetryPayloadInternal(cliId);
|
||||
assert.equal(offlinePayload.conversations.length, 1);
|
||||
assert.equal(offlinePayload.conversations[0].cascadeId, cliId);
|
||||
assert.equal(offlinePayload.conversations[0].title, '排查无头 Linux CLI 远程监控配置');
|
||||
assert.equal(offlinePayload.activeConversation.inputTokens, 4000);
|
||||
assert.equal(offlinePayload.activeConversation.cacheReadTokens, 16000);
|
||||
assert.equal(offlinePayload.activeConversation.outputTokens, 400);
|
||||
const offlineStep = offlinePayload.activeConversation.recentSteps[0];
|
||||
assert.equal(offlineStep.turnIndex, 1);
|
||||
assert.equal(offlineStep.turnPrompt, '排查无头 Linux CLI 远程监控配置');
|
||||
assert.equal(offlineStep.tokPerSec, null);
|
||||
assert.equal(offlineStep.selectedSpeedReason, 'incomplete_or_invalid_timing');
|
||||
assert.equal(
|
||||
offlinePayload.consumption.sourcesBreakdown.some(
|
||||
(s) => s.name === 'Antigravity CLI' && s.totalTokens === 20400
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
// Steady offline poll does not hammer GetCascadeTrajectory
|
||||
const callsAfterFirst = h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length;
|
||||
assert.equal(callsAfterFirst, 1);
|
||||
await h.buildTelemetryPayloadInternal(cliId);
|
||||
const callsAfterSecond = h.state.calls.filter((c) => c.method === 'GetCascadeTrajectory').length;
|
||||
assert.equal(callsAfterSecond, 1);
|
||||
|
||||
// 2. Language Server comes online: immediately upgrades offline fallback entry to full RPC telemetry
|
||||
h.state.summaries = {
|
||||
[cliId]: {
|
||||
stepCount: 2,
|
||||
lastModifiedTime: '2026-11-15T12:00:06Z',
|
||||
status: 'IDLE',
|
||||
annotations: { title: 'CLI 在线完整轨迹' },
|
||||
},
|
||||
};
|
||||
h.state.trajectories[cliId] = {
|
||||
numTotalSteps: 2,
|
||||
trajectory: {
|
||||
steps: [
|
||||
{
|
||||
type: 'CORTEX_STEP_TYPE_USER_INPUT',
|
||||
userInput: { userResponse: '排查无头 Linux CLI 远程监控配置' },
|
||||
},
|
||||
{
|
||||
type: 'CORTEX_STEP_TYPE_PLANNER_RESPONSE',
|
||||
status: 'CORTEX_STEP_STATUS_DONE',
|
||||
plannerResponse: { response: '已完成排查。', thinkingTokens: 100, responseTokens: 300 },
|
||||
metadata: {
|
||||
createdAt: '2026-11-15T12:00:02Z',
|
||||
finishedGeneratingAt: '2026-11-15T12:00:06Z',
|
||||
sourceTrajectoryStepInfo: { stepIndex: 1 },
|
||||
modelUsage: {
|
||||
model: 'MODEL_CLAUDE_4_SONNET',
|
||||
responseModel: 'claude-sonnet-4-6',
|
||||
apiProvider: 'API_PROVIDER_ANTHROPIC_VERTEX',
|
||||
inputTokens: 4000,
|
||||
cacheReadTokens: 16000,
|
||||
outputTokens: 400,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
generatorMetadata: [
|
||||
{
|
||||
stepIndices: [1],
|
||||
chatModel: {
|
||||
timeToFirstToken: '0.8s',
|
||||
modelDisplayName: 'Claude Sonnet 4.6',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const onlinePayload = await h.buildTelemetryPayloadInternal(cliId);
|
||||
const onlineStep = onlinePayload.activeConversation.recentSteps[0];
|
||||
assert.equal(onlinePayload.activeConversation.title, 'CLI 在线完整轨迹');
|
||||
assert.equal(onlineStep.tokPerSec, 100);
|
||||
assert.equal(onlineStep.ttftSec, 0.8);
|
||||
});
|
||||
|
||||
test('standalonePort setting normalizes valid ports and notifies auto-host listener on change and reset', () => {
|
||||
const h = backendHarness();
|
||||
const observedPorts = [];
|
||||
h.setStandalonePortListener((p) => observedPorts.push(p));
|
||||
|
||||
assert.equal(h.normalizeStandalonePort(0), 0);
|
||||
assert.equal(h.normalizeStandalonePort(80), 0);
|
||||
assert.equal(h.normalizeStandalonePort(70000), 0);
|
||||
assert.equal(h.normalizeStandalonePort('8787'), 8787);
|
||||
|
||||
const saved = h.executeSettingsAction({ standalonePort: 8787 });
|
||||
assert.equal(saved.ok, true);
|
||||
assert.equal(saved.updated.standalonePort, 8787);
|
||||
assert.deepEqual(observedPorts, [8787]);
|
||||
|
||||
// Same port does not re-trigger listener
|
||||
h.executeSettingsAction({ standalonePort: 8787 });
|
||||
assert.deepEqual(observedPorts, [8787]);
|
||||
|
||||
// Reset returns standalonePort to 0 and notifies listener
|
||||
const reset = h.executeResetSettingsAction();
|
||||
assert.equal(reset.ok, true);
|
||||
assert.equal(reset.updated.standalonePort, 0);
|
||||
assert.deepEqual(observedPorts, [8787, 0]);
|
||||
});
|
||||
|
||||
function encodeProtoVarint(value) {
|
||||
let v = BigInt(value);
|
||||
const out = [];
|
||||
while (v >= 0x80n) {
|
||||
out.push(Number((v & 0x7fn) | 0x80n));
|
||||
v >>= 7n;
|
||||
}
|
||||
out.push(Number(v));
|
||||
return Buffer.from(out);
|
||||
}
|
||||
|
||||
function encodeProtoFieldVarint(fieldNo, value) {
|
||||
return Buffer.concat([encodeProtoVarint((fieldNo << 3) | 0), encodeProtoVarint(value)]);
|
||||
}
|
||||
|
||||
function encodeProtoFieldBytes(fieldNo, payload) {
|
||||
const buf = typeof payload === 'string' ? Buffer.from(payload, 'utf8') : payload;
|
||||
return Buffer.concat([
|
||||
encodeProtoVarint((fieldNo << 3) | 2),
|
||||
encodeProtoVarint(buf.length),
|
||||
buf,
|
||||
]);
|
||||
}
|
||||
|
||||
test('Antigravity CLI annotations.pbtxt + SQLite trajectory reader recovers title, TPS, TTFT, thinking split, and 10-channel context', () => {
|
||||
assert.equal(
|
||||
parseAnnotationPbtxtTitle('last_user_view_time:{seconds:1794759792}\ntitle:"Greeting and Hello"\n'),
|
||||
'Greeting and Hello'
|
||||
);
|
||||
assert.equal(
|
||||
parseAnnotationPbtxtTitle('title: "用量图表与调用记录优化建议"'),
|
||||
'用量图表与调用记录优化建议'
|
||||
);
|
||||
// Octal UTF-8 byte escape for "你好" (\344\275\240\345\245\275) + escaped quote
|
||||
assert.equal(
|
||||
parseAnnotationPbtxtTitle('title:"\\344\\275\\240\\345\\245\\275 \\"CLI\\""'),
|
||||
'你好 "CLI"'
|
||||
);
|
||||
|
||||
const DatabaseSync = getNodeSqliteDatabaseSync();
|
||||
if (!DatabaseSync) return;
|
||||
|
||||
const tempRoot = mkdtempSync(fileURLToPath(new URL('./.cli-sqlite-', import.meta.url)));
|
||||
try {
|
||||
const cliRoot = join(tempRoot, 'antigravity-cli');
|
||||
const convDir = join(cliRoot, 'conversations');
|
||||
const annDir = join(cliRoot, 'annotations');
|
||||
const cid = '278a0174-296c-45a9-bc6c-62ce7bdd82c4';
|
||||
const logDir = join(cliRoot, 'brain', cid, '.system_generated', 'logs');
|
||||
mkdirSync(convDir, { recursive: true });
|
||||
mkdirSync(annDir, { recursive: true });
|
||||
mkdirSync(logDir, { recursive: true });
|
||||
|
||||
writeFileSync(
|
||||
join(annDir, `${cid}.pbtxt`),
|
||||
'last_user_view_time:{seconds:1794759792 nanos:730218300}\ntitle:"Greeting and Hello"\n',
|
||||
'utf8'
|
||||
);
|
||||
assert.equal(readAnnotationTitleForCascade(cid, [annDir]), 'Greeting and Hello');
|
||||
|
||||
// Create conversation_summaries.db matching Antigravity CLI 1.5.9 schema
|
||||
const sumDb = new DatabaseSync(join(cliRoot, 'conversation_summaries.db'));
|
||||
sumDb.exec(`
|
||||
CREATE TABLE conversation_summaries (
|
||||
conversation_id TEXT NOT NULL,
|
||||
title TEXT,
|
||||
status TEXT,
|
||||
parent_conversation_id TEXT,
|
||||
agent_name TEXT,
|
||||
not_fully_idle INTEGER,
|
||||
killed INTEGER,
|
||||
step_count INTEGER,
|
||||
last_modified_time TEXT,
|
||||
app_data_dir TEXT
|
||||
);
|
||||
`);
|
||||
sumDb
|
||||
.prepare(
|
||||
`INSERT INTO conversation_summaries
|
||||
(conversation_id, title, status, parent_conversation_id, agent_name, not_fully_idle, killed, step_count, last_modified_time, app_data_dir)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
.run(
|
||||
cid,
|
||||
'Greeting and Hello',
|
||||
'CASCADE_RUN_STATUS_IDLE',
|
||||
'',
|
||||
'',
|
||||
0,
|
||||
0,
|
||||
2,
|
||||
'2026-11-15 16:23:12.7302183+00:00',
|
||||
'antigravity-cli'
|
||||
);
|
||||
sumDb.close();
|
||||
|
||||
const summaryMap = readLocalSummaryIndex([cliRoot]);
|
||||
assert.equal(summaryMap.get(cid)?.title, 'Greeting and Hello');
|
||||
assert.equal(summaryMap.get(cid)?.status, 'CASCADE_RUN_STATUS_IDLE');
|
||||
|
||||
// Create conversations/<cid>.db with protobuf metadata matching Antigravity CLI 1.5.9
|
||||
const modelUsageBuf = Buffer.concat([
|
||||
encodeProtoFieldVarint(1, 1526), // MODEL_PLACEHOLDER_M526
|
||||
encodeProtoFieldVarint(2, 14569), // inputTokens
|
||||
encodeProtoFieldVarint(3, 89), // outputTokens
|
||||
encodeProtoFieldVarint(6, 24), // API_PROVIDER_GOOGLE_GEMINI
|
||||
encodeProtoFieldVarint(9, 57), // thinkingOutputTokens
|
||||
encodeProtoFieldVarint(10, 32), // responseOutputTokens
|
||||
]);
|
||||
const stepMetaBuf = Buffer.concat([
|
||||
encodeProtoFieldBytes(
|
||||
1,
|
||||
Buffer.concat([encodeProtoFieldVarint(1, 1794759788), encodeProtoFieldVarint(2, 899044700)])
|
||||
),
|
||||
encodeProtoFieldBytes(
|
||||
6,
|
||||
Buffer.concat([encodeProtoFieldVarint(1, 1794759792), encodeProtoFieldVarint(2, 175393200)])
|
||||
),
|
||||
encodeProtoFieldBytes(
|
||||
7,
|
||||
Buffer.concat([encodeProtoFieldVarint(1, 1794759792), encodeProtoFieldVarint(2, 730218300)])
|
||||
),
|
||||
encodeProtoFieldBytes(9, modelUsageBuf),
|
||||
encodeProtoFieldBytes(20, encodeProtoFieldVarint(2, 1)),
|
||||
]);
|
||||
|
||||
const encodeGroup = (name, typeInt, numTokens, children = []) =>
|
||||
Buffer.concat([
|
||||
encodeProtoFieldBytes(1, name),
|
||||
encodeProtoFieldVarint(2, typeInt),
|
||||
encodeProtoFieldVarint(4, numTokens),
|
||||
...children.map((c) => encodeProtoFieldBytes(5, c)),
|
||||
]);
|
||||
const tokenBreakdownBuf = Buffer.concat([
|
||||
encodeProtoFieldBytes(1, encodeGroup('System Prompt', 1, 6244)),
|
||||
encodeProtoFieldBytes(1, encodeGroup('Mcp Tools', 4, 234)),
|
||||
encodeProtoFieldBytes(1, encodeGroup('Subagents', 6, 689)),
|
||||
encodeProtoFieldBytes(1, encodeGroup('Skills', 5, 4102)),
|
||||
encodeProtoFieldBytes(1, encodeGroup('Rules', 3, 2282)),
|
||||
encodeProtoFieldBytes(1, encodeGroup('Tools', 2, 6972)),
|
||||
encodeProtoFieldBytes(
|
||||
1,
|
||||
encodeGroup('Chat Messages', 7, 149, [encodeGroup('0: USER_INPUT', 0, 149)])
|
||||
),
|
||||
]);
|
||||
const contextWindowBuf = Buffer.concat([
|
||||
encodeProtoFieldVarint(1, 20672),
|
||||
encodeProtoFieldBytes(3, tokenBreakdownBuf),
|
||||
encodeProtoFieldVarint(4, 256000),
|
||||
]);
|
||||
const chatModelBuf = Buffer.concat([
|
||||
encodeProtoFieldBytes(1, 'x'.repeat(8192)), // large prompt skipped in O(1)
|
||||
encodeProtoFieldVarint(3, 1526),
|
||||
encodeProtoFieldBytes(4, modelUsageBuf),
|
||||
encodeProtoFieldBytes(9, encodeProtoFieldBytes(10, contextWindowBuf)),
|
||||
encodeProtoFieldBytes(
|
||||
11,
|
||||
Buffer.concat([encodeProtoFieldVarint(1, 3), encodeProtoFieldVarint(2, 276645700)])
|
||||
),
|
||||
encodeProtoFieldBytes(12, encodeProtoFieldVarint(2, 555832700)),
|
||||
encodeProtoFieldBytes(19, 'gemini-4-hydrogen'),
|
||||
encodeProtoFieldBytes(
|
||||
20,
|
||||
Buffer.concat([
|
||||
encodeProtoFieldBytes(1, 'model_enum'),
|
||||
encodeProtoFieldBytes(2, 'MODEL_PLACEHOLDER_M526'),
|
||||
])
|
||||
),
|
||||
encodeProtoFieldBytes(21, 'Gemini 4 Hydrogen (Medium)'),
|
||||
]);
|
||||
const genMetaBuf = Buffer.concat([
|
||||
encodeProtoFieldBytes(2, Buffer.from([1])),
|
||||
encodeProtoFieldBytes(1, chatModelBuf),
|
||||
]);
|
||||
|
||||
const convDbPath = join(convDir, `${cid}.db`);
|
||||
const convDb = new DatabaseSync(convDbPath);
|
||||
convDb.exec(`
|
||||
CREATE TABLE steps (idx INTEGER PRIMARY KEY, step_type INTEGER, status INTEGER, metadata BLOB, step_payload BLOB);
|
||||
CREATE TABLE gen_metadata (idx INTEGER PRIMARY KEY, data BLOB);
|
||||
`);
|
||||
convDb.prepare('INSERT INTO steps (idx, step_type, status, metadata) VALUES (?, ?, ?, ?)').run(0, 14, 3, null);
|
||||
convDb.prepare('INSERT INTO steps (idx, step_type, status, metadata) VALUES (?, ?, ?, ?)').run(1, 15, 3, stepMetaBuf);
|
||||
convDb.prepare('INSERT INTO gen_metadata (idx, data) VALUES (?, ?)').run(0, genMetaBuf);
|
||||
convDb.close();
|
||||
|
||||
writeFileSync(
|
||||
join(logDir, 'transcript.jsonl'),
|
||||
[
|
||||
JSON.stringify({
|
||||
step_index: 0,
|
||||
type: 'USER_INPUT',
|
||||
status: 'DONE',
|
||||
created_at: '2026-11-15T16:23:08Z',
|
||||
content: '<USER_REQUEST>\n你好\n</USER_REQUEST>',
|
||||
}),
|
||||
JSON.stringify({
|
||||
step_index: 1,
|
||||
type: 'PLANNER_RESPONSE',
|
||||
status: 'DONE',
|
||||
created_at: '2026-11-15T16:23:08Z',
|
||||
model: 'gemini-4-hydrogen-medium',
|
||||
input_tokens: 14569,
|
||||
cache_read_tokens: 0,
|
||||
output_tokens: 89,
|
||||
content: '你好呀!今天有什么我可以帮你的吗?',
|
||||
}),
|
||||
].join('\n'),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
const decoded = readLocalSqliteTrajectory(convDbPath);
|
||||
assert.ok(decoded);
|
||||
assert.equal(decoded.steps.length, 2);
|
||||
assert.equal(decoded.generatorMetadata.length, 1);
|
||||
assert.equal(decoded.generatorMetadata[0].chatModel.modelDisplayName, 'Gemini 4 Hydrogen (Medium)');
|
||||
|
||||
// End-to-end verification in isolated child process with ANTIGRAVITY_GEMINI_DIR
|
||||
const builderUrl = new URL('../backend/telemetry-builder.mjs', import.meta.url).href;
|
||||
const rawOut = execFileSync(
|
||||
process.execPath,
|
||||
[
|
||||
'--input-type=module',
|
||||
'-e',
|
||||
`const { buildTelemetryPayload } = await import(${JSON.stringify(builderUrl)});
|
||||
const p = await buildTelemetryPayload(${JSON.stringify(cid)}, false, { range: 'all' });
|
||||
process.stdout.write(JSON.stringify({
|
||||
title: p.activeConversation?.title,
|
||||
modelLabel: p.activeConversation?.activeModelLabel,
|
||||
tokPerSec: p.activeConversation?.latestTokPerSec,
|
||||
ttftSec: p.activeConversation?.latestTtftSec,
|
||||
step: p.activeConversation?.recentSteps?.[0],
|
||||
ctx: p.activeConversation?.context,
|
||||
sources: p.consumption?.sourcesBreakdown,
|
||||
}));`,
|
||||
],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
ANTIGRAVITY_GEMINI_DIR: tempRoot,
|
||||
USAGE_MONITOR_DATA_DIR: join(tempRoot, 'data'),
|
||||
ANTIGRAVITY_LS_ADDRESS: 'test.invalid',
|
||||
ANTIGRAVITY_CSRF_TOKEN: 'test',
|
||||
},
|
||||
encoding: 'utf8',
|
||||
}
|
||||
);
|
||||
const parsed = JSON.parse(rawOut);
|
||||
assert.equal(parsed.title, 'Greeting and Hello');
|
||||
assert.equal(parsed.modelLabel, 'Gemini 4 Hydrogen (Medium)');
|
||||
assert.equal(parsed.tokPerSec, 23.2);
|
||||
assert.ok(Math.abs(parsed.ttftSec - 3.2766) < 0.01);
|
||||
assert.equal(parsed.step.thinkingTokens, 57);
|
||||
assert.equal(parsed.step.responseTokens, 32);
|
||||
assert.equal(parsed.step.turnPrompt, '你好');
|
||||
assert.equal(parsed.step.estimatedContextTokens, 20672);
|
||||
assert.equal(parsed.step.contextBreakdown.mcpToolsTokens, 234);
|
||||
assert.equal(parsed.step.contextBreakdown.skillsTokens, 4102);
|
||||
assert.equal(parsed.step.contextBreakdown.userRulesTokens, 2282);
|
||||
assert.equal(parsed.step.contextBreakdown.systemToolsTokens, 6972);
|
||||
assert.equal(parsed.step.contextBreakdown.userInputTokens, 149);
|
||||
assert.equal(parsed.ctx.totalTokens, 14658);
|
||||
assert.equal(parsed.ctx.limitTokens, 256000);
|
||||
const cliSource = parsed.sources?.find((s) => s.name === 'Antigravity CLI');
|
||||
assert.ok(cliSource);
|
||||
assert.equal(cliSource.calls, 1);
|
||||
assert.equal(cliSource.totalTokens, 14658);
|
||||
} finally {
|
||||
rmSync(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -21,8 +21,15 @@ import {
|
||||
} from '../backend/window-bridge.mjs';
|
||||
import {
|
||||
discoverAntigravityLsEndpoint,
|
||||
parseLinuxProcNetTcpPorts,
|
||||
parseLsArgsFromCommandLine,
|
||||
parseSsListeningPorts,
|
||||
} from '../backend/ls-discovery.mjs';
|
||||
import {
|
||||
createStandaloneWebServer,
|
||||
isLoopbackBindHost,
|
||||
parseStandaloneCliArgs,
|
||||
} from '../backend/standalone-server.mjs';
|
||||
import { handleMcpJsonRpcMessage } from '../mcp-entry.mjs';
|
||||
import { computeCapsuleLayoutMode } from '../src/components/layout/WindowOverlayShell.js';
|
||||
import {
|
||||
@@ -4808,3 +4815,274 @@ test('Step table semantic tone classes (speed/cache/ttft/cost) and domain-aligne
|
||||
'Capsule step cost $0.198 matches tone-warn'
|
||||
);
|
||||
});
|
||||
|
||||
test('Headless CLI LS discovery reads daemon/hub_*.json without subprocesses and falls back to Linux /proc + ss without lsof', async () => {
|
||||
// 1. CLI command line parsing covers agy remote-control serve & antigravity-cli
|
||||
const rcParsed = parseLsArgsFromCommandLine(
|
||||
'/home/ubuntu/.local/bin/agy remote-control serve --http-port=43120 --csrf-token rc-token-99'
|
||||
);
|
||||
assert.equal(rcParsed.isHub, true);
|
||||
assert.equal(rcParsed.hubPort, 43120);
|
||||
assert.equal(rcParsed.csrfToken, 'rc-token-99');
|
||||
|
||||
const cliBinParsed = parseLsArgsFromCommandLine(
|
||||
'/usr/local/bin/antigravity-cli --hub --hub-port 51200 --csrf_token=cli-tok'
|
||||
);
|
||||
assert.equal(cliBinParsed.isHub, true);
|
||||
assert.equal(cliBinParsed.hubPort, 51200);
|
||||
assert.equal(cliBinParsed.csrfToken, 'cli-tok');
|
||||
|
||||
// 2. Stage 1: daemon/hub_*.json zero-subprocess discovery skips dead PIDs and prefers plain HTTP port
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ag-um-daemon-test-'));
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'hub_dead.json'),
|
||||
JSON.stringify({
|
||||
pid: 99991,
|
||||
httpPort: 19001,
|
||||
httpsPort: 19000,
|
||||
csrfToken: 'dead-token',
|
||||
}),
|
||||
'utf8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'hub_13019.json'),
|
||||
JSON.stringify({
|
||||
pid: 42424,
|
||||
httpPort: 13019,
|
||||
httpsPort: 13018,
|
||||
lsVersion: '2.32.1',
|
||||
csrfToken: 'live-hub-token',
|
||||
}),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
let subprocessCalled = false;
|
||||
const probedUrls = [];
|
||||
const foundViaDaemon = await discoverAntigravityLsEndpoint({
|
||||
platform: 'linux',
|
||||
daemonDirs: [tmpDir],
|
||||
isPidAliveFn: (pid) => pid === 42424,
|
||||
execCmdFn: async () => {
|
||||
subprocessCalled = true;
|
||||
return '';
|
||||
},
|
||||
fetchFn: async (url, init) => {
|
||||
probedUrls.push({ url, token: init?.headers?.['x-codeium-csrf-token'] });
|
||||
return { ok: url.startsWith('http://127.0.0.1:13019/') };
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(subprocessCalled, false, 'daemon/hub_*.json hits without spawning ps/ss/lsof');
|
||||
assert.equal(probedUrls.length, 1, 'Dead PID hub_dead.json skipped; httpPort probed first');
|
||||
assert.equal(foundViaDaemon?.addr, '127.0.0.1:13019');
|
||||
assert.equal(foundViaDaemon?.baseUrl, 'http://127.0.0.1:13019');
|
||||
assert.equal(foundViaDaemon?.csrfToken, 'live-hub-token');
|
||||
assert.equal(foundViaDaemon?.pid, 42424);
|
||||
} finally {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
// 3. Linux /proc/<pid>/fd + /proc/net/tcp parser (0xA4D3 = 42195, state 0A = LISTEN)
|
||||
const tcpSample = [
|
||||
' sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode',
|
||||
' 0: 0100007F:A4D3 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 554433 1 0 100 0',
|
||||
' 1: 0100007F:1F90 0100007F:C350 01 00000000:00000000 00:00000000 00000000 1000 0 554434 1 0 100 0',
|
||||
].join('\n');
|
||||
assert.deepEqual(parseLinuxProcNetTcpPorts(tcpSample, new Set(['554433', '554434'])), [42195]);
|
||||
|
||||
const foundViaProcFs = await discoverAntigravityLsEndpoint({
|
||||
platform: 'linux',
|
||||
daemonDirs: [],
|
||||
procFs: {
|
||||
existsSync: (p) => p === '/proc/7788/fd' || p === '/proc/7788/net/tcp',
|
||||
readdirSync: (p) => (p === '/proc/7788/fd' ? ['3', '4'] : []),
|
||||
readlinkSync: (p) => (p === '/proc/7788/fd/4' ? 'socket:[554433]' : '/dev/null'),
|
||||
readFileSync: () => tcpSample,
|
||||
},
|
||||
execCmdFn: async (cmd) => {
|
||||
if (cmd === 'ps') {
|
||||
return '7788 1 /opt/agy/language_server_linux_x64 --csrf_token proc-tok-1\n';
|
||||
}
|
||||
throw new Error(`Unexpected command ${cmd}`);
|
||||
},
|
||||
fetchFn: async (url, init) => ({
|
||||
ok:
|
||||
url.startsWith('http://127.0.0.1:42195/') &&
|
||||
init?.headers?.['x-codeium-csrf-token'] === 'proc-tok-1',
|
||||
}),
|
||||
});
|
||||
assert.equal(foundViaProcFs?.addr, '127.0.0.1:42195');
|
||||
assert.equal(foundViaProcFs?.csrfToken, 'proc-tok-1');
|
||||
|
||||
// 4. Linux ss -H -lntp fallback when /proc net is restricted and lsof is not installed
|
||||
const ssSample =
|
||||
'LISTEN 0 4096 127.0.0.1:38899 0.0.0.0:* users:(("language_server",pid=8899,fd=7))\n';
|
||||
assert.deepEqual(parseSsListeningPorts(ssSample, 8899), [38899]);
|
||||
});
|
||||
|
||||
test('Standalone Web server enforces loopback/non-loopback security boundaries, serves full assets, and supports token cookie/Bearer auth', async () => {
|
||||
// 1. CLI arg & host boundary validation
|
||||
assert.equal(isLoopbackBindHost('127.0.0.1'), true);
|
||||
assert.equal(isLoopbackBindHost('localhost'), true);
|
||||
assert.equal(isLoopbackBindHost('::1'), true);
|
||||
assert.equal(isLoopbackBindHost('[::1]'), true);
|
||||
assert.equal(isLoopbackBindHost('0.0.0.0'), false);
|
||||
assert.equal(isLoopbackBindHost('192.168.1.25'), false);
|
||||
|
||||
const parsedArgs = parseStandaloneCliArgs(
|
||||
['--port', '9090', '--host=0.0.0.0', '--token', 's3cr3t'],
|
||||
{}
|
||||
);
|
||||
assert.equal(parsedArgs.port, 9090);
|
||||
assert.equal(parsedArgs.host, '0.0.0.0');
|
||||
assert.equal(parsedArgs.token, 's3cr3t');
|
||||
|
||||
// Refuse non-loopback bind without --token
|
||||
assert.throws(
|
||||
() => createStandaloneWebServer({ host: '0.0.0.0', port: 0, skipStartupDiscovery: true }),
|
||||
/Refusing to bind standalone server to non-loopback host "0\.0\.0\.0" without --token/
|
||||
);
|
||||
|
||||
// 2. Default 127.0.0.1 loopback server (ssh -L friendly, blocks cross-site & DNS rebinding)
|
||||
const loopSrv = createStandaloneWebServer({
|
||||
host: '127.0.0.1',
|
||||
port: 0,
|
||||
skipStartupDiscovery: true,
|
||||
readAssetFn: async (rel) => `ASSET:${rel}`,
|
||||
handleApiRequest: async (pathname, method, data) => ({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({ ok: true, pathname, method, data }),
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
const st = await loopSrv.start();
|
||||
assert.equal(st.running, true);
|
||||
assert.ok(st.port > 0);
|
||||
assert.equal(st.loopbackOnly, true);
|
||||
assert.equal(st.tokenProtected, false);
|
||||
|
||||
const indexRes = await fetch(`${st.url}/`);
|
||||
assert.equal(indexRes.status, 200);
|
||||
assert.equal(await indexRes.text(), 'ASSET:index.html');
|
||||
|
||||
const preloadRes = await fetch(`${st.url}/preload.js`);
|
||||
assert.equal(preloadRes.status, 200);
|
||||
assert.match(await preloadRes.text(), /standalone web mode/);
|
||||
|
||||
const apiRes = await fetch(`${st.url}/api/telemetry?range=today`);
|
||||
assert.equal(apiRes.status, 200);
|
||||
const apiJson = await apiRes.json();
|
||||
assert.deepEqual(apiJson, {
|
||||
ok: true,
|
||||
pathname: '/api/telemetry',
|
||||
method: 'GET',
|
||||
data: { range: 'today' },
|
||||
});
|
||||
|
||||
// Cross-site Origin blocked with 403
|
||||
const crossSiteRes = await fetch(`${st.url}/api/telemetry`, {
|
||||
headers: { Origin: 'https://evil.example' },
|
||||
});
|
||||
assert.equal(crossSiteRes.status, 403);
|
||||
|
||||
// DNS rebinding Host header blocked with 403
|
||||
const rebindStatus = await new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
{
|
||||
hostname: '127.0.0.1',
|
||||
port: st.port,
|
||||
path: '/api/telemetry',
|
||||
method: 'GET',
|
||||
headers: { Host: `attacker.rebind.test:${st.port}` },
|
||||
},
|
||||
(res) => {
|
||||
res.resume();
|
||||
resolve(res.statusCode);
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
assert.equal(rebindStatus, 403);
|
||||
} finally {
|
||||
await loopSrv.stop();
|
||||
}
|
||||
|
||||
// 3. Token-protected mode (--token): 401 prompt, ?token= 302 -> HttpOnly cookie, Bearer header, and cross-origin POST block
|
||||
const tokenSrv = createStandaloneWebServer({
|
||||
host: '127.0.0.1',
|
||||
port: 0,
|
||||
token: 'cli-remote-secret',
|
||||
skipStartupDiscovery: true,
|
||||
readAssetFn: async (rel) => `PROTECTED:${rel}`,
|
||||
handleApiRequest: async (pathname, method, data) => ({
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({ ok: true, pathname, method, data }),
|
||||
}),
|
||||
});
|
||||
|
||||
try {
|
||||
const st = await tokenSrv.start();
|
||||
assert.equal(st.tokenProtected, true);
|
||||
|
||||
// Unauthenticated browser GET / -> 401 HTML login form
|
||||
const unauthPage = await fetch(`${st.url}/`);
|
||||
assert.equal(unauthPage.status, 401);
|
||||
assert.match(await unauthPage.text(), /standalone-auth/);
|
||||
|
||||
// Unauthenticated API -> 401 JSON
|
||||
const unauthApi = await fetch(`${st.url}/api/telemetry`);
|
||||
assert.equal(unauthApi.status, 401);
|
||||
|
||||
// Bearer header works directly
|
||||
const bearerApi = await fetch(`${st.url}/api/telemetry?range=week`, {
|
||||
headers: { Authorization: 'Bearer cli-remote-secret' },
|
||||
});
|
||||
assert.equal(bearerApi.status, 200);
|
||||
|
||||
// Browser GET /?token=cli-remote-secret sets HttpOnly cookie and 302 redirects to clean /
|
||||
const loginRedirect = await fetch(`${st.url}/?token=cli-remote-secret`, {
|
||||
redirect: 'manual',
|
||||
});
|
||||
assert.equal(loginRedirect.status, 302);
|
||||
assert.equal(loginRedirect.headers.get('location'), '/');
|
||||
const setCookie = loginRedirect.headers.get('set-cookie') || '';
|
||||
assert.match(setCookie, /ag_um_token=[0-9a-f]{64}; HttpOnly; SameSite=Strict; Path=\//);
|
||||
const cookiePair = setCookie.split(';')[0];
|
||||
|
||||
// Cookie authenticates same-origin GET and POST, but blocks cross-origin POST CSRF
|
||||
const authedPage = await fetch(`${st.url}/`, {
|
||||
headers: { Cookie: cookiePair },
|
||||
});
|
||||
assert.equal(authedPage.status, 200);
|
||||
assert.equal(await authedPage.text(), 'PROTECTED:index.html');
|
||||
|
||||
const csrfPost = await fetch(`${st.url}/api/settings`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Cookie: cookiePair,
|
||||
Origin: 'https://evil.example',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ language: 'en-US' }),
|
||||
});
|
||||
assert.equal(csrfPost.status, 403);
|
||||
|
||||
const validPost = await fetch(`${st.url}/api/settings`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Cookie: cookiePair,
|
||||
Origin: st.url,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ language: 'en-US' }),
|
||||
});
|
||||
assert.equal(validPost.status, 200);
|
||||
} finally {
|
||||
await tokenSrv.stop();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user