mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
feat(panel): migrate trajectory cache to incremental SQLite ledger
- Add short-connection WAL trajectory_store.sqlite (and remote ssh-*.sqlite) with transactional .json-to-.bak migration, O(delta) step UPSERTs, undo tail truncation, and ledger_epoch/write_seq cross-process sync - Split steps into string-interned compact accounting tuples (retaining anomaly and step account owners) and lazy detail_json with an explicit 24-session hot LRU and precomputed O(1) cold metadata flags - Archive deleted source conversations permanently across cache rebuilds and skip redundant 404 trajectory RPCs while un-archiving automatically if sources reappear
This commit is contained in:
@@ -28,10 +28,11 @@ const BACKEND_MODULE_FILES = [
|
||||
'../backend/account-switch.mjs',
|
||||
'../backend/http-helpers.mjs',
|
||||
'../backend/settings-schema.mjs',
|
||||
'../backend/sqlite-client.mjs',
|
||||
'../backend/trajectory-sqlite-ledger.mjs',
|
||||
'../backend/trajectory-cache-store.mjs',
|
||||
'../backend/config.mjs',
|
||||
'../backend/endpoint-registry.mjs',
|
||||
'../backend/sqlite-client.mjs',
|
||||
'../backend/proto-wire.mjs',
|
||||
'../backend/cli-sqlite-reader.mjs',
|
||||
'../backend/cli-statusline-config.mjs',
|
||||
|
||||
@@ -936,16 +936,21 @@ test('legacy pre-PR#38 empty-WAL sqlite cache signatures migrate in-place withou
|
||||
const scannerUrl = new URL('../backend/local-scanner.mjs', import.meta.url).href;
|
||||
const configUrl = new URL('../backend/config.mjs', import.meta.url).href;
|
||||
const script = `
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { enumerateAndSyncConversations } from ${JSON.stringify(scannerUrl)};
|
||||
import { CACHE_FILE, flushTrajectoryCacheNow, getCachedTrajectory } from ${JSON.stringify(configUrl)};
|
||||
import { CACHE_FILE, TRAJECTORY_DB_FILE, flushTrajectoryCacheNow, getCachedTrajectory } from ${JSON.stringify(configUrl)};
|
||||
const { DatabaseSync } = process.getBuiltinModule('node:sqlite');
|
||||
await enumerateAndSyncConversations({ summariesObj: {}, requestedConvId: '' });
|
||||
const entry = getCachedTrajectory(${JSON.stringify(cid)});
|
||||
flushTrajectoryCacheNow();
|
||||
const onDisk = JSON.parse(readFileSync(CACHE_FILE, 'utf8'));
|
||||
const db = new DatabaseSync(TRAJECTORY_DB_FILE, { readOnly: true });
|
||||
const row = db.prepare('SELECT meta_json FROM conversations WHERE lower_id = ?').get(${JSON.stringify(cid.toLowerCase())});
|
||||
db.close();
|
||||
const onDiskMeta = row?.meta_json ? JSON.parse(row.meta_json) : {};
|
||||
console.log(JSON.stringify({
|
||||
memSig: entry?.signature || '',
|
||||
diskSig: onDisk[${JSON.stringify(cid)}]?.signature || '',
|
||||
diskSig: onDiskMeta?.signature || '',
|
||||
migratedBakExists: existsSync(CACHE_FILE + '.bak'),
|
||||
stepCount: entry?.steps?.length || 0,
|
||||
firstInput: entry?.steps?.[0]?.inputTokens || 0,
|
||||
}));
|
||||
@@ -961,6 +966,7 @@ test('legacy pre-PR#38 empty-WAL sqlite cache signatures migrate in-place withou
|
||||
},
|
||||
});
|
||||
const out = JSON.parse(stdout.trim().split('\n').pop());
|
||||
assert.equal(out.migratedBakExists, true, 'legacy JSON cache automatically migrated and archived to .bak');
|
||||
assert.equal(out.stepCount, 1, 'cached sentinel step preserved (SQLite was NOT re-read)');
|
||||
assert.equal(out.firstInput, 1234, 'cached usage preserved intact');
|
||||
assert.ok(out.memSig.endsWith('|0:0'), `in-memory signature normalized to |0:0: ${out.memSig}`);
|
||||
|
||||
@@ -0,0 +1,511 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { isDeepStrictEqual, promisify } from 'node:util';
|
||||
import { MODEL_IDENTITY_VERSION } from '../trajectory-metrics.mjs';
|
||||
import { createTrajectoryCacheStore } from '../backend/trajectory-cache-store.mjs';
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
function loadJsonHelper(filePath, fallback = null) {
|
||||
try {
|
||||
if (!existsSync(filePath)) return fallback;
|
||||
return JSON.parse(readFileSync(filePath, 'utf8'));
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function makeRichStep(stepIndex, overrides = {}) {
|
||||
const baseMs = Date.parse('2026-11-20T08:00:00Z') + stepIndex * 5_000;
|
||||
return {
|
||||
stepIndex,
|
||||
isCompactionStep: false,
|
||||
modelKey: 'MODEL_PLACEHOLDER_M429',
|
||||
modelSource: 'generatorMetadata.chatModel.model',
|
||||
responseModel: 'gemini-4-carbon-high',
|
||||
modelIdentityVersion: MODEL_IDENTITY_VERSION,
|
||||
modelDisplayName: 'Gemini 4 Carbon (High)',
|
||||
apiProvider: 'API_PROVIDER_GOOGLE_GEMINI',
|
||||
createdAt: new Date(baseMs).toISOString(),
|
||||
createdAtMs: baseMs,
|
||||
hasVisibleText: true,
|
||||
hasToolCalls: false,
|
||||
inputTokens: 10_000 + stepIndex * 100,
|
||||
cacheReadTokens: 40_000,
|
||||
outputTokens: 600,
|
||||
thinkingTokens: 200,
|
||||
responseTokens: 400,
|
||||
estimatedContextTokens: 50_600,
|
||||
maxContextBudgetTokens: 1_000_000,
|
||||
reportedContextLimitTokens: 1_000_000,
|
||||
contextBreakdown: {
|
||||
totalTokens: 50_600,
|
||||
messagesTokens: 32_000,
|
||||
userInputTokens: 8_000,
|
||||
assistantTokens: 14_000,
|
||||
toolResultsTokens: 10_000,
|
||||
subagentTokens: 0,
|
||||
checkpointTokens: 0,
|
||||
systemToolsTokens: 9_600,
|
||||
mcpToolsTokens: 2_000,
|
||||
skillsTokens: 3_000,
|
||||
systemPromptTokens: 3_000,
|
||||
userRulesTokens: 1_000,
|
||||
},
|
||||
measurementVersion: 4,
|
||||
isComplete: true,
|
||||
nativeTtftSec: 1.5,
|
||||
nativeStreamSec: 4.5,
|
||||
viewableDelaySec: 1.6,
|
||||
ttftSec: 1.5,
|
||||
ttftSource: 'generator_metadata',
|
||||
thinkingDurationSec: 1.2,
|
||||
totalDurationSec: 6.0,
|
||||
totalTokPerSec: 100,
|
||||
speedBasis: 'request_total',
|
||||
speedSampleEligible: true,
|
||||
speedReason: null,
|
||||
turnIndex: 1,
|
||||
isTurnStart: stepIndex === 1,
|
||||
turnPrompt: `Prompt for step ${stepIndex}`,
|
||||
turnPromptClean: `Prompt for step ${stepIndex}`,
|
||||
turnHasMedia: false,
|
||||
turnMediaOnly: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function encodeVarint(val) {
|
||||
let v = BigInt(val);
|
||||
if (v < 0n) v = BigInt.asUintN(64, v);
|
||||
const out = [];
|
||||
while (v >= 0x80n) {
|
||||
out.push(Number((v & 0x7fn) | 0x80n));
|
||||
v >>= 7n;
|
||||
}
|
||||
out.push(Number(v));
|
||||
return Buffer.from(out);
|
||||
}
|
||||
|
||||
function encodeFieldVarint(fieldNo, val) {
|
||||
return Buffer.concat([encodeVarint((fieldNo << 3) | 0), encodeVarint(val)]);
|
||||
}
|
||||
|
||||
function encodeFieldBytes(fieldNo, bufOrStr) {
|
||||
const b = Buffer.isBuffer(bufOrStr) ? bufOrStr : Buffer.from(String(bufOrStr), 'utf8');
|
||||
return Buffer.concat([encodeVarint((fieldNo << 3) | 2), encodeVarint(b.length), b]);
|
||||
}
|
||||
|
||||
function encodeTimestamp(sec) {
|
||||
return encodeFieldBytes(1, Buffer.concat([encodeFieldVarint(1, sec)])).subarray(2);
|
||||
}
|
||||
|
||||
function encodeStepMeta({ createdSec, finishedSec, stepIndex, originCid, inputTokens, outputTokens }) {
|
||||
const ts = (sec) => encodeFieldBytes(1, Buffer.concat([encodeFieldVarint(1, sec)])).subarray(2);
|
||||
const mu = Buffer.concat([
|
||||
encodeFieldVarint(1, 1429), // MODEL_PLACEHOLDER_M429 (Carbon High)
|
||||
encodeFieldVarint(2, inputTokens),
|
||||
encodeFieldVarint(3, outputTokens),
|
||||
encodeFieldVarint(6, 24),
|
||||
]);
|
||||
return Buffer.concat([
|
||||
encodeFieldBytes(1, ts(createdSec)),
|
||||
encodeFieldBytes(7, ts(finishedSec)),
|
||||
encodeFieldBytes(9, mu),
|
||||
encodeFieldBytes(20, Buffer.concat([encodeFieldVarint(2, stepIndex), encodeFieldBytes(4, originCid)])),
|
||||
]);
|
||||
}
|
||||
|
||||
test('SQLite ledger automatically migrates legacy trajectory_usage_cache.json to .sqlite, archives .bak, and preserves lossless details & anomalies', (t) => {
|
||||
const sqliteMod =
|
||||
typeof process.getBuiltinModule === 'function' ? process.getBuiltinModule('node:sqlite') : null;
|
||||
if (!sqliteMod?.DatabaseSync) {
|
||||
t.skip('node:sqlite DatabaseSync unavailable');
|
||||
return;
|
||||
}
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'agy-ledger-mig-'));
|
||||
t.after(() => {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const legacyJson = join(tmp, 'trajectory_usage_cache.json');
|
||||
const sqliteFile = join(tmp, 'trajectory_store.sqlite');
|
||||
const mainCid = 'aaaaaaaa-1111-4111-8111-111111111111';
|
||||
const subCid = 'bbbbbbbb-2222-4222-8222-222222222222';
|
||||
|
||||
const step1 = makeRichStep(1, {
|
||||
accountName: 'alice@example.com',
|
||||
anomaly: {
|
||||
count: 2,
|
||||
severity: 'warn',
|
||||
primaryKind: 'rate_limit',
|
||||
totalDelaySec: 8.5,
|
||||
kinds: { rate_limit: 2 },
|
||||
},
|
||||
anomalySig: 'rl:2',
|
||||
});
|
||||
const cpStep = makeRichStep(2, {
|
||||
isCompactionStep: true,
|
||||
modelKey: undefined,
|
||||
responseModel: '',
|
||||
modelDisplayName: '',
|
||||
contextBreakdown: null,
|
||||
inputTokens: 5_000,
|
||||
cacheReadTokens: 0,
|
||||
outputTokens: 300,
|
||||
thinkingTokens: 0,
|
||||
responseTokens: 300,
|
||||
});
|
||||
delete cpStep.modelKey;
|
||||
|
||||
writeFileSync(
|
||||
legacyJson,
|
||||
JSON.stringify({
|
||||
[mainCid.toUpperCase()]: {
|
||||
title: 'Legacy Migrated Root',
|
||||
signature: 'v10-model-identity:sqlite:1::100:200|0:0',
|
||||
accountName: 'bob@example.com',
|
||||
workspaceUri: 'file:///f:/demo/project',
|
||||
subagentRefs: [{ cascadeId: subCid, role: 'Researcher', typeName: 'research', spawnOrder: 0 }],
|
||||
steps: [step1, cpStep],
|
||||
},
|
||||
[subCid]: {
|
||||
title: 'Researcher',
|
||||
parentConversationId: mainCid,
|
||||
rootConversationId: mainCid,
|
||||
subagentSpec: { typeName: 'research', role: 'Researcher' },
|
||||
steps: [makeRichStep(1, { inputTokens: 2_000, outputTokens: 100 })],
|
||||
},
|
||||
}),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
const store = createTrajectoryCacheStore({
|
||||
cacheFilePath: legacyJson,
|
||||
sqliteFilePath: sqliteFile,
|
||||
enableSqlite: true,
|
||||
loadJsonFn: loadJsonHelper,
|
||||
});
|
||||
|
||||
// Cold startup list reads slim accounting entries without hydrating detail_json
|
||||
const entries = store.listCachedTrajectoryEntries();
|
||||
assert.equal(entries.length, 2);
|
||||
assert.equal(existsSync(sqliteFile), true);
|
||||
assert.equal(existsSync(legacyJson), false, 'legacy JSON moved out of active path');
|
||||
assert.equal(existsSync(`${legacyJson}.bak`), true, 'legacy JSON safely archived to .bak');
|
||||
|
||||
// Cold entry omits heavy contextBreakdown/turnPrompt, retains anomaly/accountName/inferred compaction model!
|
||||
assert.equal(store.isConversationCold(mainCid), true);
|
||||
const coldMain = store.getCachedTrajectoryEntryByLower(mainCid)?.entry;
|
||||
assert.ok(coldMain);
|
||||
assert.equal(coldMain.steps.length, 2);
|
||||
assert.equal(coldMain.steps[0].contextBreakdown, undefined, 'cold step omits heavy contextBreakdown');
|
||||
assert.equal(coldMain.steps[0].turnPrompt, undefined, 'cold step omits heavy turnPrompt');
|
||||
assert.deepEqual(coldMain.steps[0].anomaly, step1.anomaly, 'cold step preserves anomaly for model ranking');
|
||||
assert.equal(coldMain.steps[0].accountName, 'alice@example.com');
|
||||
assert.equal(coldMain.steps[1].modelKey, 'MODEL_PLACEHOLDER_M429', 'compaction step inferred model during migration');
|
||||
|
||||
// Explicit hydration restores 100% lossless step details
|
||||
const hydratedMain = store.getHydratedTrajectoryEntryByLower(mainCid)?.entry;
|
||||
assert.equal(store.isConversationCold(mainCid), false);
|
||||
assert.deepEqual(hydratedMain.steps[0].contextBreakdown, step1.contextBreakdown);
|
||||
assert.equal(hydratedMain.steps[0].turnPrompt, 'Prompt for step 1');
|
||||
assert.equal(hydratedMain.steps[0].reportedContextLimitTokens, 1_000_000);
|
||||
});
|
||||
|
||||
test('Hot 24-session LRU evicts cold detail memory, never thrashes during full-list scans, and re-hydrates on demand', (t) => {
|
||||
const sqliteMod =
|
||||
typeof process.getBuiltinModule === 'function' ? process.getBuiltinModule('node:sqlite') : null;
|
||||
if (!sqliteMod?.DatabaseSync) {
|
||||
t.skip('node:sqlite DatabaseSync unavailable');
|
||||
return;
|
||||
}
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'agy-ledger-lru-'));
|
||||
t.after(() => {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const sqliteFile = join(tmp, 'trajectory_store.sqlite');
|
||||
const store = createTrajectoryCacheStore({
|
||||
sqliteFilePath: sqliteFile,
|
||||
enableSqlite: true,
|
||||
hotSessionLimit: 24,
|
||||
});
|
||||
|
||||
const originalStep0 = makeRichStep(1, {
|
||||
turnPrompt: 'Deep historical architecture prompt #0',
|
||||
anomaly: { count: 1, severity: 'danger', primaryKind: 'timeout', totalDelaySec: 30, kinds: { timeout: 1 } },
|
||||
});
|
||||
|
||||
for (let i = 0; i < 32; i += 1) {
|
||||
const cid = `00000000-0000-4000-8000-${String(i).padStart(12, '0')}`;
|
||||
store.setCachedTrajectory(cid, {
|
||||
title: `Session #${i}`,
|
||||
signature: `v10-model-identity:sqlite:1::${i}`,
|
||||
wasRunning: false,
|
||||
contextGroupsTelemetry: 3,
|
||||
turnRailTelemetry: true,
|
||||
anomalyTelemetry: 1,
|
||||
steps: [i === 0 ? originalStep0 : makeRichStep(1, { turnPrompt: `Prompt #${i}` })],
|
||||
});
|
||||
}
|
||||
|
||||
// Before flush, dirty unflushed sessions MUST NOT drop detail_json prematurely!
|
||||
const cid0 = '00000000-0000-4000-8000-000000000000';
|
||||
assert.ok(store.getCachedTrajectory(cid0).steps[0].contextBreakdown, 'dirty unflushed session retains detail');
|
||||
|
||||
// Flush writes all 32 to SQLite, then trims hot LRU down to 24!
|
||||
assert.equal(store.flushTrajectoryCacheNow(), true);
|
||||
assert.equal(store.getHotSessionCount(), 24);
|
||||
assert.equal(store.isConversationCold(cid0), true, 'oldest session #0 evicted to cold slim mode');
|
||||
assert.equal(store.getCachedTrajectory(cid0).steps[0].contextBreakdown, undefined);
|
||||
assert.deepEqual(store.getCachedTrajectory(cid0).steps[0].anomaly, originalStep0.anomaly);
|
||||
|
||||
// Iterating all 32 conversations via getCachedTrajectory / listCachedTrajectoryEntries MUST NOT hydrate cold sessions!
|
||||
for (const [cid] of store.listCachedTrajectoryEntries()) {
|
||||
const e = store.getCachedTrajectory(cid);
|
||||
assert.equal(e.steps.length, 1);
|
||||
}
|
||||
assert.equal(store.isConversationCold(cid0), true, 'scan loop caused zero cold SQLite reads');
|
||||
assert.equal(store.getHotSessionCount(), 24);
|
||||
|
||||
// Explicit hydration of cold session #0 restores exact original step and evicts #8
|
||||
const hydrated0 = store.getHydratedTrajectory(cid0);
|
||||
assert.equal(store.isConversationCold(cid0), false);
|
||||
assert.equal(store.getHotSessionCount(), 24);
|
||||
assert.equal(isDeepStrictEqual(hydrated0.steps[0], originalStep0), true);
|
||||
assert.equal(store.isConversationCold('00000000-0000-4000-8000-000000000008'), true);
|
||||
});
|
||||
|
||||
test('Step-level O(delta) UPSERT, conversation undo tail truncation, and cross-process WAL sync keep zero file locks', (t) => {
|
||||
const sqliteMod =
|
||||
typeof process.getBuiltinModule === 'function' ? process.getBuiltinModule('node:sqlite') : null;
|
||||
if (!sqliteMod?.DatabaseSync) {
|
||||
t.skip('node:sqlite DatabaseSync unavailable');
|
||||
return;
|
||||
}
|
||||
const { DatabaseSync } = sqliteMod;
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'agy-ledger-wal-'));
|
||||
t.after(() => {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const sqliteFile = join(tmp, 'trajectory_store.sqlite');
|
||||
const storeWriter = createTrajectoryCacheStore({ sqliteFilePath: sqliteFile, enableSqlite: true });
|
||||
const storeReader = createTrajectoryCacheStore({ sqliteFilePath: sqliteFile, enableSqlite: true });
|
||||
|
||||
const cid = 'cccccccc-3333-4333-8333-333333333333';
|
||||
const s1 = makeRichStep(1, { inputTokens: 1_000 });
|
||||
const s2 = makeRichStep(2, { inputTokens: 2_000 });
|
||||
const s3 = makeRichStep(3, { inputTokens: 3_000 });
|
||||
storeWriter.setCachedTrajectory(cid, {
|
||||
title: 'Incremental Session',
|
||||
signature: 'sig-3',
|
||||
steps: [s1, s2, s3],
|
||||
});
|
||||
storeWriter.flushTrajectoryCacheNow();
|
||||
|
||||
// Reader hydrates initial 3 steps
|
||||
assert.equal(storeReader.getCachedTrajectory(cid).steps.length, 3);
|
||||
|
||||
// Append step #4: verify SQLite now has 4 rows and Reader picks up incremental write_seq update
|
||||
const s4 = makeRichStep(4, { inputTokens: 4_000 });
|
||||
storeWriter.setCachedTrajectory(cid, {
|
||||
title: 'Incremental Session',
|
||||
signature: 'sig-4',
|
||||
steps: [s1, s2, s3, s4],
|
||||
});
|
||||
storeWriter.flushTrajectoryCacheNow();
|
||||
|
||||
storeReader.syncExternalChangesNow();
|
||||
assert.equal(storeReader.getCachedTrajectory(cid).steps.length, 4);
|
||||
assert.equal(storeReader.getCachedTrajectory(cid).steps[3].inputTokens, 4_000);
|
||||
|
||||
// Simulate Undo in Antigravity: steps shrink from 4 back to 2 -> pos 2 & 3 MUST be deleted from SQLite!
|
||||
storeWriter.setCachedTrajectory(cid, {
|
||||
title: 'Incremental Session (Undone)',
|
||||
signature: 'sig-2',
|
||||
steps: [s1, s2],
|
||||
});
|
||||
storeWriter.flushTrajectoryCacheNow();
|
||||
|
||||
const dbCheck = new DatabaseSync(sqliteFile, { readOnly: true });
|
||||
const stepRowCount = dbCheck
|
||||
.prepare('SELECT COUNT(*) AS cnt FROM steps WHERE cascade_id = ?')
|
||||
.get(cid).cnt;
|
||||
dbCheck.close();
|
||||
assert.equal(stepRowCount, 2, 'undone tail steps pos >= 2 truncated cleanly');
|
||||
|
||||
storeReader.syncExternalChangesNow();
|
||||
assert.equal(storeReader.getCachedTrajectory(cid).steps.length, 2);
|
||||
|
||||
// Simulate clearCache on Writer (with cid live -> cleared): Reader syncs epoch bump and removes cid!
|
||||
storeWriter.clearCache({ liveCascadeIds: [cid] });
|
||||
storeReader.syncExternalChangesNow();
|
||||
assert.equal(storeReader.getCachedTrajectory(cid), undefined);
|
||||
});
|
||||
|
||||
test('Deleting source conversation in Antigravity + clicking Rebuild Cache permanently preserves deleted conversation tokens, costs, and details', async (t) => {
|
||||
const sqliteMod =
|
||||
typeof process.getBuiltinModule === 'function' ? process.getBuiltinModule('node:sqlite') : null;
|
||||
if (!sqliteMod?.DatabaseSync) {
|
||||
t.skip('node:sqlite DatabaseSync unavailable');
|
||||
return;
|
||||
}
|
||||
const { DatabaseSync } = sqliteMod;
|
||||
const tmp = mkdtempSync(join(tmpdir(), 'agy-ledger-perm-'));
|
||||
t.after(() => {
|
||||
try {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Ignore Windows WAL cleanup race
|
||||
}
|
||||
});
|
||||
|
||||
const geminiDir = join(tmp, 'gemini');
|
||||
const agyDir = join(geminiDir, 'antigravity');
|
||||
const convDir = join(agyDir, 'conversations');
|
||||
const dataDir = join(tmp, 'panel-data');
|
||||
mkdirSync(convDir, { recursive: true });
|
||||
mkdirSync(dataDir, { recursive: true });
|
||||
|
||||
const cidDeleted = 'dddddddd-4444-4444-8444-444444444444';
|
||||
const cidLive = 'eeeeeeee-5555-4555-8555-555555555555';
|
||||
const baseSec = Math.floor(Date.parse('2026-11-18T00:00:00Z') / 1000);
|
||||
|
||||
// Create conversation_summaries.db and both .db files
|
||||
const sumDbPath = join(agyDir, 'conversation_summaries.db');
|
||||
const sumDb = new DatabaseSync(sumDbPath);
|
||||
sumDb.exec(`
|
||||
CREATE TABLE conversation_summaries (
|
||||
conversation_id TEXT PRIMARY KEY,
|
||||
title TEXT,
|
||||
preview TEXT,
|
||||
status TEXT,
|
||||
source TEXT,
|
||||
parent_conversation_id TEXT,
|
||||
nesting_depth INTEGER,
|
||||
agent_name TEXT,
|
||||
not_fully_idle INTEGER,
|
||||
killed INTEGER,
|
||||
step_count INTEGER,
|
||||
last_modified_time TEXT,
|
||||
app_data_dir TEXT,
|
||||
raw_summary BLOB
|
||||
);
|
||||
CREATE TABLE conversation_parents (conversation_id TEXT, parent_id TEXT);
|
||||
`);
|
||||
const insSum = sumDb.prepare(
|
||||
"INSERT INTO conversation_summaries (conversation_id, title, status, parent_conversation_id, step_count, last_modified_time, app_data_dir, raw_summary) VALUES (?, ?, 'CASCADE_RUN_STATUS_IDLE', '', 2, '2026-11-18T00:01:00Z', 'antigravity', ?)"
|
||||
);
|
||||
insSum.run(cidDeleted, 'Deleted Precious Ledger Task', Buffer.alloc(0));
|
||||
insSum.run(cidLive, 'Surviving Live Task', Buffer.alloc(0));
|
||||
sumDb.close();
|
||||
|
||||
const createConvSqlite = (cid, promptText, inTok, outTok) => {
|
||||
const p = join(convDir, `${cid}.db`);
|
||||
const db = new DatabaseSync(p);
|
||||
db.exec(`
|
||||
CREATE TABLE trajectory_meta (trajectory_id TEXT, cascade_id TEXT, trajectory_type INTEGER, source INTEGER);
|
||||
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);
|
||||
`);
|
||||
db.prepare('INSERT INTO trajectory_meta VALUES (?, ?, 1, 1)').run(cid, cid);
|
||||
db.prepare('INSERT INTO steps VALUES (0, 14, 3, ?, ?)').run(
|
||||
Buffer.concat([
|
||||
encodeFieldBytes(1, encodeTimestamp(baseSec + 1)),
|
||||
encodeFieldBytes(20, Buffer.concat([encodeFieldVarint(2, 0), encodeFieldBytes(4, cid)])),
|
||||
]),
|
||||
encodeFieldBytes(19, encodeFieldBytes(2, promptText))
|
||||
);
|
||||
db.prepare('INSERT INTO steps VALUES (1, 15, 3, ?, ?)').run(
|
||||
encodeStepMeta({
|
||||
createdSec: baseSec + 10,
|
||||
finishedSec: baseSec + 15,
|
||||
stepIndex: 1,
|
||||
originCid: cid,
|
||||
inputTokens: inTok,
|
||||
outputTokens: outTok,
|
||||
}),
|
||||
encodeFieldBytes(20, encodeFieldBytes(1, 'Completed task'))
|
||||
);
|
||||
db.close();
|
||||
return p;
|
||||
};
|
||||
|
||||
const deletedDbFile = createConvSqlite(cidDeleted, 'Keep my billing forever', 100_000, 10_000);
|
||||
const liveDbFile = createConvSqlite(cidLive, 'Still working on this', 50_000, 5_000);
|
||||
|
||||
const apiUrl = new URL('../backend/api-actions.mjs', import.meta.url).href;
|
||||
const telUrl = new URL('../backend/telemetry-builder.mjs', import.meta.url).href;
|
||||
const cfgUrl = new URL('../backend/config.mjs', import.meta.url).href;
|
||||
|
||||
const script = `
|
||||
import { unlinkSync } from 'node:fs';
|
||||
import { executeRebuildCacheAction } from ${JSON.stringify(apiUrl)};
|
||||
import { buildTelemetryPayloadInternal } from ${JSON.stringify(telUrl)};
|
||||
import { flushTrajectoryCacheNow, getCachedTrajectory, getHydratedTrajectory } from ${JSON.stringify(cfgUrl)};
|
||||
const { DatabaseSync } = process.getBuiltinModule('node:sqlite');
|
||||
|
||||
// 1. Initial scan records both conversations in trajectory_store.sqlite
|
||||
const before = await buildTelemetryPayloadInternal(${JSON.stringify(cidLive)}, { range: 'all' });
|
||||
flushTrajectoryCacheNow();
|
||||
const totalTokensBefore = before.consumption.account.totalTokens;
|
||||
|
||||
// 2. Delete cidDeleted completely from Antigravity (.db + summary row), and add a step to cidLive!
|
||||
unlinkSync(${JSON.stringify(deletedDbFile)});
|
||||
const sdb = new DatabaseSync(${JSON.stringify(sumDbPath)});
|
||||
sdb.prepare('DELETE FROM conversation_summaries WHERE conversation_id = ?').run(${JSON.stringify(cidDeleted)});
|
||||
sdb.close();
|
||||
|
||||
// 3. Click Rebuild Cache TWICE in a row!
|
||||
const r1 = executeRebuildCacheAction();
|
||||
const r2 = executeRebuildCacheAction();
|
||||
|
||||
// 4. Rebuild telemetry & inspect both cidLive and deleted cidDeleted
|
||||
const afterLive = await buildTelemetryPayloadInternal(${JSON.stringify(cidLive)}, { range: 'all' });
|
||||
const afterDeleted = await buildTelemetryPayloadInternal(${JSON.stringify(cidDeleted)}, { range: 'all' });
|
||||
flushTrajectoryCacheNow();
|
||||
|
||||
const cachedDeleted = getCachedTrajectory(${JSON.stringify(cidDeleted)});
|
||||
const hydratedDeleted = getHydratedTrajectory(${JSON.stringify(cidDeleted)});
|
||||
|
||||
console.log(JSON.stringify({
|
||||
r1Ok: r1.ok,
|
||||
r2Ok: r2.ok,
|
||||
totalTokensBefore,
|
||||
totalTokensAfter: afterLive.consumption.account.totalTokens,
|
||||
deletedArchivedFlag: Boolean(cachedDeleted?.archived),
|
||||
deletedStepCount: hydratedDeleted?.steps?.length || 0,
|
||||
deletedTurnPrompt: hydratedDeleted?.steps?.[0]?.turnPrompt || '',
|
||||
deletedViewSteps: afterDeleted.activeConversation?.recentSteps?.length || 0,
|
||||
}));
|
||||
`;
|
||||
|
||||
const { stdout } = await execFileAsync(process.execPath, ['--input-type=module', '-e', script], {
|
||||
env: {
|
||||
...process.env,
|
||||
ANTIGRAVITY_GEMINI_DIR: geminiDir,
|
||||
ANTIGRAVITY_APP_DATA_DIR: '',
|
||||
USAGE_MONITOR_APP_DATA_DIR: '',
|
||||
ANTIGRAVITY_EXECUTABLE_DATA_DIR: dataDir,
|
||||
ANTIGRAVITY_LS_ADDRESS: 'test.invalid',
|
||||
},
|
||||
});
|
||||
|
||||
const out = JSON.parse(stdout.trim().split('\n').pop());
|
||||
assert.equal(out.r1Ok, true);
|
||||
assert.equal(out.r2Ok, true);
|
||||
// 100k + 10k (cidDeleted) + 50k + 5k (cidLive) = 165,000 tokens -> 100% preserved after deleting cidDeleted + 2x Rebuild Cache!
|
||||
assert.equal(out.totalTokensBefore, 165_000);
|
||||
assert.equal(out.totalTokensAfter, 165_000, 'deleted conversation tokens & cost permanently preserved');
|
||||
assert.equal(out.deletedArchivedFlag, true, 'deleted conversation marked archived');
|
||||
assert.equal(out.deletedStepCount, 1);
|
||||
assert.equal(out.deletedTurnPrompt, 'Keep my billing forever', 'deleted conversation detail_json preserved');
|
||||
assert.equal(out.deletedViewSteps, 1, 'clicking archived deleted conversation still renders step details');
|
||||
assert.equal(existsSync(liveDbFile), true);
|
||||
});
|
||||
Reference in New Issue
Block a user