fix: track ALL trajectories, detect new conversations, summary fallback per-traj

This commit is contained in:
2026-03-07 19:55:34 +09:00
parent 0c9664542e
commit 41f90b3b15
4 changed files with 142 additions and 109 deletions

View File

@@ -282,13 +282,15 @@ function activate(context) {
});
}
let pollFailCount = 0;
let pollCount = 0;
async function pollConversations() {
if (!lsPort) {
return;
}
if (pollFailCount > 3) {
if (pollFailCount > 10) {
return;
} // stop after repeated failures
pollCount++;
try {
// Use getDiagnostics to get cascade-level conversation IDs
const diag = await vscode.commands.executeCommand('antigravity.getDiagnostics');
@@ -302,64 +304,78 @@ function activate(context) {
}
const isFirstPoll = Object.keys(lastStepIndex).length === 0;
if (isFirstPoll) {
console.log(`Gravity Bridge: [LS] ${trajectories.length} recent trajectories from getDiagnostics`);
const latest = trajectories[trajectories.length - 1];
console.log(`Gravity Bridge: [LS] latest: ${JSON.stringify(latest).substring(0, 400)}`);
console.log(`Gravity Bridge: [LS] ${trajectories.length} trajectories from getDiagnostics`);
}
// Check the most recent trajectory for step count changes
const latest = trajectories[trajectories.length - 1];
const agentId = latest.googleAgentId || '';
const trajId = latest.trajectoryId || '';
const stepIdx = latest.lastStepIndex ?? 0;
if (!agentId && !trajId) {
return;
// Periodic debug every ~1 min (12 * 5s)
if (pollCount % 12 === 0) {
const summary = trajectories.map((t) => {
const id = (t.googleAgentId || '').substring(0, 8);
return `${id}:s${t.lastStepIndex ?? '?'}`;
}).join(', ');
console.log(`Gravity Bridge: [LS] poll#${pollCount}${trajectories.length} trajs: [${summary}]`);
}
const key = agentId || trajId;
const prev = lastStepIndex[key];
if (prev === undefined) {
// First poll — initialize
lastStepIndex[key] = stepIdx;
console.log(`Gravity Bridge: [LS] initialized ${key.substring(0, 8)} at step ${stepIdx}`);
return;
}
if (stepIdx > prev) {
// New steps detected! Try to fetch them
console.log(`Gravity Bridge: [LS] ${key.substring(0, 8)} new steps: ${prev}${stepIdx}`);
const attempts = [
{ method: 'GetCascadeTrajectorySteps', params: { googleAgentId: agentId, trajectoryId: trajId, startStepIndex: prev } },
{ method: 'GetCascadeTrajectory', params: { googleAgentId: agentId, trajectoryId: trajId } },
{ method: 'GetCascadeTrajectorySteps', params: { conversationId: agentId, trajectoryId: trajId } },
];
let stepsResult = null;
for (const attempt of attempts) {
const res = await lsRPC(attempt.method, attempt.params);
if (res && !res.code && !res.message?.includes('not found')) {
stepsResult = res;
console.log(`Gravity Bridge: [LS] ✅ ${attempt.method} worked!`);
console.log(`Gravity Bridge: [LS] keys: ${Object.keys(res).join(', ')}`);
console.log(`Gravity Bridge: [LS] sample: ${JSON.stringify(res).substring(0, 800)}`);
pollFailCount = 0;
break;
}
// Check ALL trajectories for step count changes
for (const traj of trajectories) {
const agentId = traj.googleAgentId || '';
const trajId = traj.trajectoryId || '';
const stepIdx = traj.lastStepIndex ?? 0;
const summary = traj.summary || '';
if (!agentId && !trajId) {
continue;
}
if (stepsResult) {
const steps = stepsResult.steps || stepsResult.cortexSteps || stepsResult.cortex_steps || [];
if (Array.isArray(steps) && steps.length > 0) {
const newSteps = steps.slice(-(stepIdx - prev));
extractAndRelaySteps(newSteps);
const key = agentId || trajId;
const prev = lastStepIndex[key];
if (prev === undefined) {
// First time seeing this trajectory — initialize
lastStepIndex[key] = stepIdx;
if (isFirstPoll) {
console.log(`Gravity Bridge: [LS] init ${key.substring(0, 8)} at step ${stepIdx} "${summary.substring(0, 30)}"`);
}
}
else {
// All attempts failed — try writing the summary as a fallback
const summary = latest.summary || '';
if (summary && summary !== lastStepIndex[key + '_summary']) {
writeChatSnapshot(`[AI 대화 요약]\n\n${summary}\n\n(step ${prev}${stepIdx})`);
lastStepIndex[key + '_summary'] = summary;
console.log(`Gravity Bridge: [LS] relayed summary fallback: ${summary.substring(0, 100)}`);
else {
// New trajectory appeared after first poll = new conversation!
console.log(`Gravity Bridge: [LS] NEW trajectory ${key.substring(0, 8)} at step ${stepIdx} "${summary.substring(0, 40)}"`);
}
pollFailCount++;
continue;
}
if (stepIdx > prev) {
// New steps detected!
console.log(`Gravity Bridge: [LS] ${key.substring(0, 8)} steps: ${prev}${stepIdx} "${summary.substring(0, 40)}"`);
// Try RPC to get full step text
const attempts = [
{ method: 'GetCascadeTrajectorySteps', params: { googleAgentId: agentId, trajectoryId: trajId, startStepIndex: prev } },
{ method: 'GetCascadeTrajectory', params: { googleAgentId: agentId, trajectoryId: trajId } },
];
let stepsResult = null;
for (const attempt of attempts) {
const res = await lsRPC(attempt.method, attempt.params);
if (res && !res.code && !res.message?.includes('not found')) {
stepsResult = res;
console.log(`Gravity Bridge: [LS] ✅ ${attempt.method} worked!`);
console.log(`Gravity Bridge: [LS] keys: ${Object.keys(res).join(', ')}`);
console.log(`Gravity Bridge: [LS] sample: ${JSON.stringify(res).substring(0, 800)}`);
pollFailCount = 0;
break;
}
}
if (stepsResult) {
const steps = stepsResult.steps || stepsResult.cortexSteps || stepsResult.cortex_steps || [];
if (Array.isArray(steps) && steps.length > 0) {
const newSteps = steps.slice(-(stepIdx - prev));
extractAndRelaySteps(newSteps);
}
}
else {
// Fallback: relay summary text
const summaryKey = key + '_summary';
if (summary && summary !== lastStepIndex[summaryKey]) {
writeChatSnapshot(`[AI 응답 감지]\n\n**${summary}**\n\n(step ${prev}${stepIdx})`);
lastStepIndex[summaryKey] = summary;
console.log(`Gravity Bridge: [LS] relayed summary: "${summary.substring(0, 80)}"`);
}
pollFailCount++;
}
lastStepIndex[key] = stepIdx;
}
lastStepIndex[key] = stepIdx;
}
}
catch (e) {