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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type { Config } from '../config/config.js';
import { SessionSummaryService } from './sessionSummaryService.js';
import { BaseLlmClient } from '../core/baseLlmClient.js';
import { debugLogger } from '../utils/debugLogger.js';
import {
  SESSION_FILE_PREFIX,
  loadConversationRecord,
  type ConversationRecord,
  type MemoryScratchpad,
  type ToolCallRecord,
} from './chatRecordingService.js';
import { CoreToolCallStatus } from '../scheduler/types.js';
import { SHELL_TOOL_NAME } from '../tools/definitions/base-declarations.js';
import { summarizeShellCommandForScratchpad } from './sessionScratchpadUtils.js';
import fs from 'node:fs/promises';
import path from 'node:path';

const MIN_MESSAGES_FOR_SUMMARY = 1;
const MAX_SCRATCHPAD_TOOLS = 6;
const MAX_SCRATCHPAD_PATHS = 4;
const MAX_SCRATCHPAD_PATH_DEPTH = 6;
const MAX_WORKFLOW_SUMMARY_LENGTH = 160;
const VALIDATION_COMMAND_REGEX =
  /\b(test|tests|vitest|jest|pytest|cargo test|npm test|pnpm test|yarn test|bun test|lint|build|check|typecheck)\b/i;
const PATH_KEY_REGEX = /(path|file|dir|directory|cwd|root)/i;
const VALIDATION_TOOL_REGEX = /\b(test|lint|build|check|typecheck)\b/i;

type LoadedSession = ConversationRecord & {
  messageCount?: number;
  userMessageCount?: number;
  memoryScratchpadIsStale?: boolean;
};

interface SessionFileCandidate {
  filePath: string;
  mtimeMs: number;
}

function isSupportedSessionFile(fileName: string): boolean {
  return (
    fileName.startsWith(SESSION_FILE_PREFIX) &&
    (fileName.endsWith('.json') || fileName.endsWith('.jsonl'))
  );
}

async function listSessionFileCandidates(
  chatsDir: string,
): Promise<SessionFileCandidate[]> {
  const allFiles = await fs.readdir(chatsDir);
  const candidates: SessionFileCandidate[] = [];

  for (const fileName of allFiles) {
    if (!isSupportedSessionFile(fileName)) continue;

    const filePath = path.join(chatsDir, fileName);
    try {
      const stat = await fs.stat(filePath);
      if (!stat.isFile()) continue;
      candidates.push({ filePath, mtimeMs: stat.mtimeMs });
    } catch {
      // Skip files that disappeared between readdir and stat.
    }
  }

  candidates.sort((a, b) => {
    const mtimeDelta = b.mtimeMs - a.mtimeMs;
    if (mtimeDelta !== 0) {
      return mtimeDelta;
    }

    return path.basename(b.filePath).localeCompare(path.basename(a.filePath));
  });

  return candidates;
}

function getSessionTimestampMs(session: LoadedSession): number {
  if (!session.lastUpdated) return 0;
  const parsed = Date.parse(session.lastUpdated);
  return Number.isNaN(parsed) ? 0 : parsed;
}

function normalizeToolName(name: string): string {
  const trimmed = name.trim();
  return trimmed.length > 0 ? trimmed : 'unknown_tool';
}

function pushUniqueLimited(
  target: string[],
  value: string,
  limit: number,
): void {
  if (!value || target.includes(value) || target.length >= limit) {
    return;
  }
  target.push(value);
}

function normalizePathCandidate(
  candidate: string,
  projectRoot: string,
): string | null {
  const trimmed = candidate.trim();
  if (
    trimmed.length === 0 ||
    trimmed.length > 240 ||
    trimmed.includes('\n') ||
    (!trimmed.includes('/') &&
      !trimmed.includes('\\') &&
      !trimmed.startsWith('.') &&
      path.extname(trimmed).length === 0)
  ) {
    return null;
  }

  let normalized = trimmed.replace(/\\/g, '/');
  if (path.isAbsolute(trimmed)) {
    const relative = path.relative(projectRoot, trimmed);
    normalized =
      relative && !relative.startsWith('..') && !path.isAbsolute(relative)
        ? relative.replace(/\\/g, '/')
        : path.basename(trimmed);
  }

  if (normalized.length > 120) {
    normalized = normalized.split('/').slice(-3).join('/');
  }

  return normalized.length > 0 ? normalized : null;
}

function collectPathsFromValue(
  value: unknown,
  projectRoot: string,
  paths: string[],
  keyHint?: string,
  depth = 0,
): void {
  if (
    paths.length >= MAX_SCRATCHPAD_PATHS ||
    depth > MAX_SCRATCHPAD_PATH_DEPTH
  ) {
    return;
  }

  if (typeof value === 'string') {
    if (!keyHint || !PATH_KEY_REGEX.test(keyHint)) {
      return;
    }

    const normalized = normalizePathCandidate(value, projectRoot);
    if (normalized) {
      pushUniqueLimited(paths, normalized, MAX_SCRATCHPAD_PATHS);
    }
    return;
  }

  if (Array.isArray(value)) {
    for (const item of value) {
      collectPathsFromValue(item, projectRoot, paths, keyHint, depth + 1);
      if (paths.length >= MAX_SCRATCHPAD_PATHS) {
        return;
      }
    }
    return;
  }

  if (typeof value !== 'object' || value === null) {
    return;
  }

  for (const [key, nestedValue] of Object.entries(value)) {
    collectPathsFromValue(nestedValue, projectRoot, paths, key, depth + 1);
    if (paths.length >= MAX_SCRATCHPAD_PATHS) {
      return;
    }
  }
}

function getToolCallCommand(toolCall: ToolCallRecord): string | undefined {
  for (const key of ['command', 'cmd', 'script']) {
    const value = toolCall.args[key];
    if (typeof value === 'string' && value.trim().length > 0) {
      return value;
    }
  }
  return undefined;
}

function getToolSequenceEntry(toolCall: ToolCallRecord): string {
  const toolName = normalizeToolName(toolCall.name);
  if (toolName !== SHELL_TOOL_NAME) {
    return toolName;
  }

  const command = getToolCallCommand(toolCall);
  const commandSummary = command
    ? summarizeShellCommandForScratchpad(command)
    : undefined;
  return commandSummary ? `${toolName}: ${commandSummary}` : toolName;
}

function getValidationStatusForToolCall(
  toolCall: ToolCallRecord,
): MemoryScratchpad['validationStatus'] | undefined {
  const command = getToolCallCommand(toolCall);
  const isValidationTool =
    VALIDATION_TOOL_REGEX.test(toolCall.name) ||
    (command ? VALIDATION_COMMAND_REGEX.test(command) : false);
  if (!isValidationTool) {
    return undefined;
  }

  if (toolCall.status === CoreToolCallStatus.Success) {
    return 'passed';
  }
  if (
    toolCall.status === CoreToolCallStatus.Error ||
    toolCall.status === CoreToolCallStatus.Cancelled
  ) {
    return 'failed';
  }
  return 'unknown';
}

function buildWorkflowSummary(
  toolSequence: string[],
  touchedPaths: string[],
  validationStatus?: MemoryScratchpad['validationStatus'],
): string | undefined {
  const parts: string[] = [];

  if (toolSequence.length > 0) {
    parts.push(toolSequence.join(' -> '));
  }
  if (touchedPaths.length > 0) {
    parts.push(`paths ${touchedPaths.join(', ')}`);
  }
  if (validationStatus === 'passed') {
    parts.push('validated');
  } else if (validationStatus === 'failed') {
    parts.push('validation failed');
  }

  if (parts.length === 0) {
    return undefined;
  }

  const summary = parts.join(' | ');
  if (summary.length === 0) {
    return undefined;
  }
  return summary.length > MAX_WORKFLOW_SUMMARY_LENGTH
    ? `${summary.slice(0, MAX_WORKFLOW_SUMMARY_LENGTH - 3)}...`
    : summary;
}

function buildMemoryScratchpad(
  messages: ConversationRecord['messages'],
  projectRoot: string,
): MemoryScratchpad {
  const toolSequence: string[] = [];
  const touchedPaths: string[] = [];
  let validationStatus: MemoryScratchpad['validationStatus'];

  for (const message of messages) {
    if (message.type !== 'gemini' || !message.toolCalls) {
      continue;
    }

    for (const toolCall of message.toolCalls) {
      pushUniqueLimited(
        toolSequence,
        getToolSequenceEntry(toolCall),
        MAX_SCRATCHPAD_TOOLS,
      );
      collectPathsFromValue(toolCall.args, projectRoot, touchedPaths);

      const toolValidationStatus = getValidationStatusForToolCall(toolCall);
      if (toolValidationStatus) {
        validationStatus = toolValidationStatus;
      }
    }
  }

  const workflowSummary = buildWorkflowSummary(
    toolSequence,
    touchedPaths,
    validationStatus,
  );

  return {
    version: 1,
    ...(workflowSummary ? { workflowSummary } : {}),
    ...(toolSequence.length > 0 ? { toolSequence } : {}),
    ...(touchedPaths.length > 0 ? { touchedPaths } : {}),
    ...(validationStatus ? { validationStatus } : {}),
  };
}

function hasCurrentMemoryScratchpad(session: LoadedSession): boolean {
  return Boolean(
    session.memoryScratchpad && session.memoryScratchpadIsStale !== true,
  );
}

function hasSessionSummaryMetadata(session: LoadedSession): boolean {
  return hasCurrentMemoryScratchpad(session);
}

function getLoadedMessageCount(session: LoadedSession): number {
  return session.messageCount ?? session.messages.length;
}

/**
 * Generates and saves a summary for a session file.
 */
async function generateAndSaveSummary(
  config: Config,
  sessionPath: string,
): Promise<void> {
  const conversation = await loadConversationRecord(sessionPath);
  if (!conversation) {
    debugLogger.debug(`[SessionSummary] Could not read session ${sessionPath}`);
    return;
  }

  // Skip if workflow metadata already exists; memory extraction can use the
  // scratchpad even when summary generation was unavailable.
  if (hasSessionSummaryMetadata(conversation)) {
    debugLogger.debug(
      `[SessionSummary] Summary metadata already exists for ${sessionPath}, skipping`,
    );
    return;
  }

  // Skip if no messages
  if (conversation.messages.length === 0) {
    debugLogger.debug(
      `[SessionSummary] No messages to summarize in ${sessionPath}`,
    );
    return;
  }

  let summary = conversation.summary;
  if (!summary) {
    const contentGenerator = config.getContentGenerator();
    if (!contentGenerator) {
      debugLogger.debug(
        '[SessionSummary] Content generator not available, skipping summary generation',
      );
    } else {
      const baseLlmClient = new BaseLlmClient(contentGenerator, config);
      const summaryService = new SessionSummaryService(baseLlmClient);
      summary =
        (await summaryService.generateSummary({
          messages: conversation.messages,
        })) ?? undefined;

      if (!summary) {
        debugLogger.warn(
          `[SessionSummary] Failed to generate summary for ${sessionPath}`,
        );
      }
    }
  }

  let scratchpadSourceConversation = conversation;

  // Re-read the file before writing to handle race conditions. For JSONL we
  // only need the metadata; for legacy JSON we need the full record so we can
  // round-trip the messages back to disk.
  const isJsonl = sessionPath.endsWith('.jsonl');
  const freshConversation = await loadConversationRecord(sessionPath, {
    metadataOnly: isJsonl,
  });
  if (!freshConversation) {
    debugLogger.debug(`[SessionSummary] Could not re-read ${sessionPath}`);
    return;
  }

  // Check if summary metadata was added by another process
  if (hasSessionSummaryMetadata(freshConversation)) {
    debugLogger.debug(
      `[SessionSummary] Summary metadata was added by another process for ${sessionPath}`,
    );
    return;
  }

  if (
    !hasCurrentMemoryScratchpad(freshConversation) &&
    (getLoadedMessageCount(freshConversation) !==
      getLoadedMessageCount(conversation) ||
      freshConversation.lastUpdated !== conversation.lastUpdated)
  ) {
    const latestConversation = await loadConversationRecord(sessionPath);
    if (!latestConversation) {
      debugLogger.debug(`[SessionSummary] Could not re-read ${sessionPath}`);
      return;
    }
    if (hasSessionSummaryMetadata(latestConversation)) {
      debugLogger.debug(
        `[SessionSummary] Summary metadata was added by another process for ${sessionPath}`,
      );
      return;
    }
    scratchpadSourceConversation = latestConversation;
  }

  const metadataUpdate: Partial<ConversationRecord> = {};
  if (!freshConversation.summary && summary) {
    metadataUpdate.summary = summary;
  }
  if (!hasCurrentMemoryScratchpad(freshConversation)) {
    metadataUpdate.memoryScratchpad = buildMemoryScratchpad(
      scratchpadSourceConversation.messages,
      config.getProjectRoot(),
    );
  }

  if (Object.keys(metadataUpdate).length === 0) {
    return;
  }

  if (isJsonl) {
    await fs.appendFile(
      sessionPath,
      `${JSON.stringify({ $set: metadataUpdate })}\n`,
    );
  } else {
    const lastUpdated = freshConversation.lastUpdated;
    await fs.writeFile(
      sessionPath,
      JSON.stringify(
        {
          ...freshConversation,
          ...metadataUpdate,
          lastUpdated,
        },
        null,
        2,
      ),
    );
  }
  debugLogger.debug(
    `[SessionSummary] Saved summary metadata for ${sessionPath}${summary ? `: "${summary}"` : ''}`,
  );
}

/**
 * Finds the most recently updated previous session that still needs workflow metadata.
 * Returns the path if it needs a scratchpad, null otherwise.
 */
export async function getPreviousSession(
  config: Config,
): Promise<string | null> {
  try {
    const chatsDir = path.join(config.storage.getProjectTempDir(), 'chats');

    // Check if chats directory exists
    try {
      await fs.access(chatsDir);
    } catch {
      debugLogger.debug('[SessionSummary] No chats directory found');
      return null;
    }

    const sessionFiles = await listSessionFileCandidates(chatsDir);
    if (sessionFiles.length === 0) {
      debugLogger.debug('[SessionSummary] No session files found');
      return null;
    }

    let bestPreviousSession: {
      filePath: string;
      conversation: LoadedSession;
    } | null = null;

    for (const { filePath, mtimeMs } of sessionFiles) {
      const bestTimestamp = bestPreviousSession
        ? getSessionTimestampMs(bestPreviousSession.conversation)
        : null;
      if (
        bestPreviousSession &&
        bestTimestamp !== null &&
        bestTimestamp > 0 &&
        mtimeMs < bestTimestamp
      ) {
        break;
      }

      try {
        const conversation = await loadConversationRecord(filePath, {
          metadataOnly: true,
        });
        if (!conversation) continue;
        if (conversation.sessionId === config.getSessionId()) continue;
        if (conversation.kind === 'subagent') continue;
        if (hasSessionSummaryMetadata(conversation)) continue;

        // Only generate summaries for sessions with more than 1 user message.
        // `loadConversationRecord` populates `userMessageCount` in metadataOnly
        // mode; fall back to scanning messages for the legacy fallback path.
        const userMessageCount =
          conversation.userMessageCount ??
          conversation.messages.filter((message) => message.type === 'user')
            .length;
        if (userMessageCount <= MIN_MESSAGES_FOR_SUMMARY) {
          continue;
        }

        if (
          !bestPreviousSession ||
          getSessionTimestampMs(conversation) >
            getSessionTimestampMs(bestPreviousSession.conversation) ||
          (getSessionTimestampMs(conversation) ===
            getSessionTimestampMs(bestPreviousSession.conversation) &&
            path
              .basename(filePath)
              .localeCompare(path.basename(bestPreviousSession.filePath)) > 0)
        ) {
          bestPreviousSession = { filePath, conversation };
        }
      } catch {
        // Ignore unreadable session files
      }
    }

    if (!bestPreviousSession) {
      debugLogger.debug(
        '[SessionSummary] No previous session needs summary generation',
      );
      return null;
    }

    return bestPreviousSession.filePath;
  } catch (error) {
    debugLogger.debug(
      `[SessionSummary] Error finding previous session: ${error instanceof Error ? error.message : String(error)}`,
    );
    return null;
  }
}

/**
 * Generates summary metadata for the previous session if it lacks a scratchpad.
 * This is designed to be called fire-and-forget on startup.
 */
export async function generateSummary(config: Config): Promise<void> {
  try {
    const sessionPath = await getPreviousSession(config);
    if (sessionPath) {
      await generateAndSaveSummary(config, sessionPath);
    }
  } catch (error) {
    // Log but don't throw - we want graceful degradation
    debugLogger.warn(
      `[SessionSummary] Error generating summary: ${error instanceof Error ? error.message : String(error)}`,
    );
  }
}