Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
ai-archaeology.ts13.4 KB · 487 lines
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
/**
 * AI Code Archaeology — excavate the "why" behind any file.
 *
 * Given a file path in a repository, searches git history, PRs, and issues
 * to reconstruct the reasoning and original motivation behind the code.
 *
 * Uses a 30-minute in-memory cache keyed on `${repoId}:${filePath}`.
 * The query does not affect the cache key — same file, same archaeology.
 */

import { basename } from "path";
import { eq, and, desc, ilike, or } from "drizzle-orm";
import { db } from "../db";
import { pullRequests, prComments, issues, issueComments } from "../db/schema";
import { getRepoPath, getBlob } from "../git/repository";
import {
  getAnthropic,
  isAiAvailable,
  extractText,
  MODEL_SONNET,
} from "./ai-client";

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

export interface ArchaeologyFinding {
  type: "commit" | "pr" | "issue";
  id: string;     // commit sha, pr number (string), or issue number (string)
  title: string;
  summary: string; // 1-2 sentences explaining relevance
  date: string;   // ISO string
  url: string;    // relative URL e.g. /owner/repo/commit/sha
  author: string;
}

export interface ArchaeologyReport {
  filePath: string;
  query: string;       // the "why" question asked
  explanation: string; // Claude's synthesized answer (markdown)
  findings: ArchaeologyFinding[];
  confidence: "high" | "medium" | "low";
  analyzedAt: Date;
}

// ---------------------------------------------------------------------------
// In-memory cache (30-min TTL, keyed on repoId:filePath)
// ---------------------------------------------------------------------------

interface CacheEntry {
  report: ArchaeologyReport;
  expiresAt: number;
}

const cache = new Map<string, CacheEntry>();
const CACHE_TTL_MS = 30 * 60 * 1000; // 30 minutes

function getCached(repoId: string, filePath: string): ArchaeologyReport | null {
  const key = `${repoId}:${filePath}`;
  const entry = cache.get(key);
  if (!entry) return null;
  if (Date.now() > entry.expiresAt) {
    cache.delete(key);
    return null;
  }
  return entry.report;
}

function setCached(repoId: string, filePath: string, report: ArchaeologyReport): void {
  const key = `${repoId}:${filePath}`;
  cache.set(key, { report, expiresAt: Date.now() + CACHE_TTL_MS });
}

export function invalidateCache(repoId: string, filePath: string): void {
  const key = `${repoId}:${filePath}`;
  cache.delete(key);
}

// ---------------------------------------------------------------------------
// Git helpers (run in the repo's bare git dir)
// ---------------------------------------------------------------------------

interface GitLogEntry {
  sha: string;
  message: string;
  author: string;
  date: string;
}

async function gitExec(
  cmd: string[],
  cwd: string
): Promise<{ stdout: string; exitCode: number }> {
  const proc = Bun.spawn(cmd, {
    cwd,
    env: process.env as Record<string, string>,
    stdout: "pipe",
    stderr: "pipe",
  });
  const stdout = await new Response(proc.stdout).text();
  const exitCode = await proc.exited;
  return { stdout, exitCode };
}

/**
 * Step 1 — Run `git log --follow --oneline --max-count=20 -- <filePath>`
 * and also grab author + date via a custom format. Cap at 20 commits.
 */
async function getFileGitLog(
  repoPath: string,
  filePath: string
): Promise<GitLogEntry[]> {
  try {
    // Use null-delimiter format: sha%x00message%x00author%x00date
    const { stdout, exitCode } = await gitExec(
      [
        "git",
        "log",
        "--follow",
        "--format=%H%x00%s%x00%an%x00%aI",
        "--max-count=20",
        "--",
        filePath,
      ],
      repoPath
    );
    if (exitCode !== 0) return [];
    return stdout
      .trim()
      .split("\n")
      .filter(Boolean)
      .map((line) => {
        const [sha, message, author, date] = line.split("\0");
        return { sha, message: message || "(no message)", author: author || "unknown", date: date || "" };
      });
  } catch {
    return [];
  }
}

// ---------------------------------------------------------------------------
// DB helpers
// ---------------------------------------------------------------------------

interface PrRecord {
  number: number;
  title: string;
  body: string | null;
  createdAt: Date;
  comments: Array<{ body: string }>;
}

interface IssueRecord {
  number: number;
  title: string;
  body: string | null;
  createdAt: Date;
  comments: Array<{ body: string }>;
}

async function findRelatedPRs(
  repoId: string,
  fileName: string
): Promise<PrRecord[]> {
  try {
    const pattern = `%${fileName}%`;
    const prs = await db
      .select({
        id: pullRequests.id,
        number: pullRequests.number,
        title: pullRequests.title,
        body: pullRequests.body,
        createdAt: pullRequests.createdAt,
      })
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, repoId),
          or(
            ilike(pullRequests.title, pattern),
            ilike(pullRequests.body, pattern)
          )
        )
      )
      .orderBy(desc(pullRequests.createdAt))
      .limit(5);

    const results: PrRecord[] = [];
    for (const pr of prs) {
      const comments = await db
        .select({ body: prComments.body })
        .from(prComments)
        .where(
          and(
            eq(prComments.pullRequestId, pr.id),
            eq(prComments.isAiReview, false)
          )
        )
        .orderBy(desc(prComments.createdAt))
        .limit(3);

      results.push({
        number: pr.number,
        title: pr.title,
        body: pr.body,
        createdAt: pr.createdAt,
        comments: comments.map((c) => ({
          body: c.body.slice(0, 500),
        })),
      });
    }
    return results;
  } catch {
    return [];
  }
}

async function findRelatedIssues(
  repoId: string,
  fileName: string
): Promise<IssueRecord[]> {
  try {
    const pattern = `%${fileName}%`;
    const found = await db
      .select({
        id: issues.id,
        number: issues.number,
        title: issues.title,
        body: issues.body,
        createdAt: issues.createdAt,
      })
      .from(issues)
      .where(
        and(
          eq(issues.repositoryId, repoId),
          or(
            ilike(issues.title, pattern),
            ilike(issues.body, pattern)
          )
        )
      )
      .orderBy(desc(issues.createdAt))
      .limit(5);

    const results: IssueRecord[] = [];
    for (const issue of found) {
      const comments = await db
        .select({ body: issueComments.body })
        .from(issueComments)
        .where(eq(issueComments.issueId, issue.id))
        .orderBy(desc(issueComments.createdAt))
        .limit(2);

      results.push({
        number: issue.number,
        title: issue.title,
        body: issue.body,
        createdAt: issue.createdAt,
        comments: comments.map((c) => ({
          body: c.body.slice(0, 300),
        })),
      });
    }
    return results;
  } catch {
    return [];
  }
}

// ---------------------------------------------------------------------------
// Confidence heuristic
// ---------------------------------------------------------------------------

function deriveConfidence(
  commits: GitLogEntry[],
  prs: PrRecord[],
  issues: IssueRecord[]
): "high" | "medium" | "low" {
  const total = commits.length + prs.length + issues.length;
  if (total >= 8) return "high";
  if (total >= 3) return "medium";
  return "low";
}

// ---------------------------------------------------------------------------
// Main excavate function
// ---------------------------------------------------------------------------

export async function excavate(
  ownerName: string,
  repoName: string,
  repoId: string,
  filePath: string,
  query: string
): Promise<ArchaeologyReport> {
  // Check cache first
  const cached = getCached(repoId, filePath);
  if (cached) {
    // Return cached with updated query
    return { ...cached, query };
  }

  // Guard: AI not available
  if (!isAiAvailable()) {
    const report: ArchaeologyReport = {
      filePath,
      query,
      explanation: "ANTHROPIC_API_KEY not set — AI archaeology is unavailable.",
      findings: [],
      confidence: "low",
      analyzedAt: new Date(),
    };
    return report;
  }

  try {
    const repoPath = getRepoPath(ownerName, repoName);
    const fileName = basename(filePath);

    // Step 1: Git log for this file
    const commits = await getFileGitLog(repoPath, filePath);

    // Step 2: Load current file content (cap at 10KB)
    let fileContent = "";
    try {
      const blob = await getBlob(ownerName, repoName, "HEAD", filePath);
      if (blob && !blob.isBinary) {
        fileContent = blob.content.slice(0, 10 * 1024);
      }
    } catch {
      // file may not exist — continue
    }

    // First 50 lines of file content for Claude
    const first50Lines = fileContent
      .split("\n")
      .slice(0, 50)
      .join("\n");

    // Step 3: Related PRs
    const prs = await findRelatedPRs(repoId, fileName);

    // Step 4: Related issues
    const relatedIssues = await findRelatedIssues(repoId, fileName);

    // Step 5: Claude synthesis
    const gitLogText = commits.length > 0
      ? commits
          .map(
            (c) =>
              `- ${c.sha.slice(0, 8)} | ${c.date.slice(0, 10)} | ${c.author} | ${c.message}`
          )
          .join("\n")
      : "(no commits found for this file)";

    const prText = prs.length > 0
      ? prs
          .map((pr) => {
            const bodySnippet = pr.body ? pr.body.slice(0, 800) : "";
            const commentsText = pr.comments.length > 0
              ? pr.comments.map((c) => `  Comment: ${c.body}`).join("\n")
              : "";
            return `PR #${pr.number} (${pr.createdAt.toISOString().slice(0, 10)}): ${pr.title}\n${bodySnippet ? `Body: ${bodySnippet}` : ""}${commentsText ? `\n${commentsText}` : ""}`;
          })
          .join("\n\n")
      : "(no related PRs found)";

    const issueText = relatedIssues.length > 0
      ? relatedIssues
          .map((issue) => {
            const bodySnippet = issue.body ? issue.body.slice(0, 600) : "";
            const commentsText = issue.comments.length > 0
              ? issue.comments.map((c) => `  Comment: ${c.body}`).join("\n")
              : "";
            return `Issue #${issue.number} (${issue.createdAt.toISOString().slice(0, 10)}): ${issue.title}\n${bodySnippet ? `Body: ${bodySnippet}` : ""}${commentsText ? `\n${commentsText}` : ""}`;
          })
          .join("\n\n")
      : "(no related issues found)";

    const userPrompt = `File: ${filePath}
Question: ${query}

Current file content (first 50 lines):
\`\`\`
${first50Lines || "(file is empty or binary)"}
\`\`\`

Git history (recent commits touching this file):
${gitLogText}

Related PRs:
${prText}

Related issues:
${issueText}

Synthesize: why does this code exist? What problem does it solve? What decisions were made?`;

    let explanation = "";
    try {
      const anthropic = getAnthropic();
      const message = await anthropic.messages.create({
        model: MODEL_SONNET,
        max_tokens: 2048,
        system:
          "You are a software archaeologist. Given the git history, PR discussions, and issue tracker for a file, synthesize a clear explanation of WHY this code exists — the original motivation, key decisions, and any important context. Be concise but complete. Use markdown.",
        messages: [{ role: "user", content: userPrompt }],
      });
      explanation = extractText(message);
    } catch (err) {
      explanation = `Unable to generate explanation: ${err instanceof Error ? err.message : String(err)}`;
    }

    // Step 6: Build findings list sorted by date desc
    const findings: ArchaeologyFinding[] = [];

    // Add commits
    for (const commit of commits) {
      findings.push({
        type: "commit",
        id: commit.sha,
        title: commit.message,
        summary: `Commit by ${commit.author} touching this file.`,
        date: commit.date,
        url: `/${ownerName}/${repoName}/commit/${commit.sha}`,
        author: commit.author,
      });
    }

    // Add PRs
    for (const pr of prs) {
      findings.push({
        type: "pr",
        id: String(pr.number),
        title: pr.title,
        summary: `Pull request referencing ${fileName}.`,
        date: pr.createdAt.toISOString(),
        url: `/${ownerName}/${repoName}/pulls/${pr.number}`,
        author: "",
      });
    }

    // Add issues
    for (const issue of relatedIssues) {
      findings.push({
        type: "issue",
        id: String(issue.number),
        title: issue.title,
        summary: `Issue referencing ${fileName}.`,
        date: issue.createdAt.toISOString(),
        url: `/${ownerName}/${repoName}/issues/${issue.number}`,
        author: "",
      });
    }

    // Sort by date descending
    findings.sort((a, b) => {
      const da = a.date ? new Date(a.date).getTime() : 0;
      const db2 = b.date ? new Date(b.date).getTime() : 0;
      return db2 - da;
    });

    const confidence = deriveConfidence(commits, prs, relatedIssues);

    const report: ArchaeologyReport = {
      filePath,
      query,
      explanation,
      findings,
      confidence,
      analyzedAt: new Date(),
    };

    setCached(repoId, filePath, report);
    return report;
  } catch (err) {
    // Never throws — return a degraded report
    return {
      filePath,
      query,
      explanation: `Archaeology failed: ${err instanceof Error ? err.message : String(err)}`,
      findings: [],
      confidence: "low",
      analyzedAt: new Date(),
    };
  }
}