@pratik7368patil/anchor-core 0.1.29 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -781,6 +781,18 @@ type CodeIndexProgress = {
781
781
  repo: string;
782
782
  chunks: number;
783
783
  patterns: number;
784
+ } | {
785
+ stage: "deleting_code_fts";
786
+ repo: string;
787
+ current: number;
788
+ total: number;
789
+ chunks: number;
790
+ } | {
791
+ stage: "deleting_architecture_fts";
792
+ repo: string;
793
+ current: number;
794
+ total: number;
795
+ patterns: number;
784
796
  } | {
785
797
  stage: "writing_code_files";
786
798
  repo: string;
package/dist/index.js CHANGED
@@ -1710,9 +1710,28 @@ function calculateCoverage(input) {
1710
1710
 
1711
1711
  // src/db/database.ts
1712
1712
  var CODE_WRITE_PROGRESS_INTERVAL = 150;
1713
+ var FTS_DELETE_BATCH_SIZE = 500;
1713
1714
  function shouldEmitCodeWriteProgress(current, total) {
1714
1715
  return current === 0 || current === 1 || current === total || current % CODE_WRITE_PROGRESS_INTERVAL === 0;
1715
1716
  }
1717
+ function shouldEmitFtsDeleteProgress(current, total) {
1718
+ return current === 0 || current === 1 || current === total || current % FTS_DELETE_BATCH_SIZE === 0;
1719
+ }
1720
+ function deleteFtsRowsByRowId(db, ftsTable, rowIds, onProgress) {
1721
+ if (rowIds.length === 0) {
1722
+ onProgress?.(0, 0);
1723
+ return;
1724
+ }
1725
+ const deleteRow = db.prepare(`DELETE FROM ${ftsTable} WHERE rowid = ?`);
1726
+ onProgress?.(0, rowIds.length);
1727
+ for (const [index, rowId] of rowIds.entries()) {
1728
+ deleteRow.run(rowId);
1729
+ const current = index + 1;
1730
+ if (shouldEmitFtsDeleteProgress(current, rowIds.length)) {
1731
+ onProgress?.(current, rowIds.length);
1732
+ }
1733
+ }
1734
+ }
1716
1735
  function defaultDatabasePath(cwd) {
1717
1736
  return path4.join(cwd, ".anchor", "index.sqlite");
1718
1737
  }
@@ -1905,9 +1924,12 @@ function clearGraphQLFetchCheckpoint(db, repo, scope) {
1905
1924
  ).run((/* @__PURE__ */ new Date()).toISOString(), repo);
1906
1925
  }
1907
1926
  function deleteExistingPrData(db, prId) {
1908
- db.prepare(
1909
- "DELETE FROM wisdom_units_fts WHERE unitId IN (SELECT id FROM wisdom_units WHERE pr_id = ?)"
1910
- ).run(prId);
1927
+ const wisdomRowIds = db.prepare("SELECT rowid FROM wisdom_units WHERE pr_id = ?").all(prId);
1928
+ deleteFtsRowsByRowId(
1929
+ db,
1930
+ "wisdom_units_fts",
1931
+ wisdomRowIds.map((row) => row.rowid)
1932
+ );
1911
1933
  db.prepare("DELETE FROM regression_events WHERE pr_id = ?").run(prId);
1912
1934
  db.prepare("DELETE FROM wisdom_units WHERE pr_id = ?").run(prId);
1913
1935
  db.prepare("DELETE FROM pr_comments WHERE pr_id = ?").run(prId);
@@ -2019,11 +2041,11 @@ function upsertPullRequest(db, pr, wisdomUnits, regressionEvents = []) {
2019
2041
  );
2020
2042
  const insertFts = db.prepare(
2021
2043
  `INSERT INTO wisdom_units_fts
2022
- (unitId, sanitizedText, filePaths, symbols, prTitle, prBody, category)
2023
- VALUES (?, ?, ?, ?, ?, ?, ?)`
2044
+ (rowid, unitId, sanitizedText, filePaths, symbols, prTitle, prBody, category)
2045
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
2024
2046
  );
2025
2047
  for (const unit of wisdomUnits) {
2026
- insertWisdom.run(
2048
+ const wisdomInsert = insertWisdom.run(
2027
2049
  unit.id,
2028
2050
  repoId,
2029
2051
  prRow.id,
@@ -2042,6 +2064,7 @@ function upsertPullRequest(db, pr, wisdomUnits, regressionEvents = []) {
2042
2064
  unit.confidence
2043
2065
  );
2044
2066
  insertFts.run(
2067
+ Number(wisdomInsert.lastInsertRowid),
2045
2068
  unit.id,
2046
2069
  unit.sanitizedText,
2047
2070
  unit.filePaths.join(" "),
@@ -2098,22 +2121,31 @@ function replaceCodeIndex(db, repo, codeFiles, codeChunks, skippedFiles, cwd, ar
2098
2121
  });
2099
2122
  options.onProgress?.({ stage: "writing_code_index", repo, phase: "Writing code index" });
2100
2123
  const transaction = db.transaction(() => {
2101
- const existingChunkCount = db.prepare("SELECT COUNT(*) AS count FROM code_chunks WHERE repo_id = ?").get(repoId).count;
2102
- const existingPatternCount = db.prepare("SELECT COUNT(*) AS count FROM architecture_patterns WHERE repo_id = ?").get(repoId).count;
2124
+ const existingChunkRowIds = db.prepare("SELECT rowid FROM code_chunks WHERE repo_id = ?").all(repoId);
2125
+ const existingPatternRowIds = db.prepare("SELECT rowid FROM architecture_patterns WHERE repo_id = ?").all(repoId);
2103
2126
  options.onProgress?.({
2104
2127
  stage: "deleting_existing_code_index",
2105
2128
  repo,
2106
- chunks: existingChunkCount,
2107
- patterns: existingPatternCount
2129
+ chunks: existingChunkRowIds.length,
2130
+ patterns: existingPatternRowIds.length
2108
2131
  });
2109
- db.prepare(
2110
- "DELETE FROM code_chunks_fts WHERE chunkId IN (SELECT id FROM code_chunks WHERE repo_id = ?)"
2111
- ).run(repoId);
2132
+ deleteFtsRowsByRowId(
2133
+ db,
2134
+ "code_chunks_fts",
2135
+ existingChunkRowIds.map((row) => row.rowid),
2136
+ (current, total) => options.onProgress?.({
2137
+ stage: "deleting_code_fts",
2138
+ repo,
2139
+ current,
2140
+ total,
2141
+ chunks: existingChunkRowIds.length
2142
+ })
2143
+ );
2112
2144
  db.prepare("DELETE FROM code_chunks WHERE repo_id = ?").run(repoId);
2113
2145
  db.prepare("DELETE FROM code_files WHERE repo_id = ?").run(repoId);
2114
2146
  db.prepare("DELETE FROM test_links WHERE repo_id = ? AND reason != 'PR co-change'").run(repoId);
2115
2147
  db.prepare("DELETE FROM test_files WHERE repo_id = ?").run(repoId);
2116
- deleteExistingArchitectureData(db, repoId);
2148
+ deleteExistingArchitectureData(db, repoId, repo, existingPatternRowIds, options);
2117
2149
  const insertFile = db.prepare(
2118
2150
  `INSERT INTO code_files
2119
2151
  (repo_id, path, language, size_bytes, content_hash, updated_at)
@@ -2155,8 +2187,8 @@ function replaceCodeIndex(db, repo, codeFiles, codeChunks, skippedFiles, cwd, ar
2155
2187
  );
2156
2188
  const insertFts = db.prepare(
2157
2189
  `INSERT INTO code_chunks_fts
2158
- (chunkId, sanitizedText, filePath, symbols, language)
2159
- VALUES (?, ?, ?, ?, ?)`
2190
+ (rowid, chunkId, sanitizedText, filePath, symbols, language)
2191
+ VALUES (?, ?, ?, ?, ?, ?)`
2160
2192
  );
2161
2193
  options.onProgress?.({
2162
2194
  stage: "writing_code_chunks",
@@ -2169,7 +2201,7 @@ function replaceCodeIndex(db, repo, codeFiles, codeChunks, skippedFiles, cwd, ar
2169
2201
  for (const [index, chunk] of codeChunks.entries()) {
2170
2202
  const fileId = fileIds.get(chunk.filePath);
2171
2203
  if (!fileId) continue;
2172
- insertChunk.run(
2204
+ const chunkInsert = insertChunk.run(
2173
2205
  chunk.id,
2174
2206
  repoId,
2175
2207
  fileId,
@@ -2184,6 +2216,7 @@ function replaceCodeIndex(db, repo, codeFiles, codeChunks, skippedFiles, cwd, ar
2184
2216
  chunk.updatedAt
2185
2217
  );
2186
2218
  insertFts.run(
2219
+ Number(chunkInsert.lastInsertRowid),
2187
2220
  chunk.id,
2188
2221
  chunk.sanitizedText,
2189
2222
  chunk.filePath,
@@ -2245,10 +2278,19 @@ function replaceCodeIndex(db, repo, codeFiles, codeChunks, skippedFiles, cwd, ar
2245
2278
  databasePath: defaultDatabasePath(cwd)
2246
2279
  };
2247
2280
  }
2248
- function deleteExistingArchitectureData(db, repoId) {
2249
- db.prepare(
2250
- "DELETE FROM architecture_patterns_fts WHERE patternId IN (SELECT id FROM architecture_patterns WHERE repo_id = ?)"
2251
- ).run(repoId);
2281
+ function deleteExistingArchitectureData(db, repoId, repo, patternRowIds, options = {}) {
2282
+ deleteFtsRowsByRowId(
2283
+ db,
2284
+ "architecture_patterns_fts",
2285
+ patternRowIds.map((row) => row.rowid),
2286
+ (current, total) => options.onProgress?.({
2287
+ stage: "deleting_architecture_fts",
2288
+ repo,
2289
+ current,
2290
+ total,
2291
+ patterns: patternRowIds.length
2292
+ })
2293
+ );
2252
2294
  db.prepare("DELETE FROM architecture_patterns WHERE repo_id = ?").run(repoId);
2253
2295
  db.prepare("DELETE FROM architecture_components WHERE repo_id = ?").run(repoId);
2254
2296
  db.prepare("DELETE FROM code_imports WHERE repo_id = ?").run(repoId);
@@ -2331,8 +2373,8 @@ function insertArchitectureData(db, repoId, repo, architecture, options = {}) {
2331
2373
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
2332
2374
  );
2333
2375
  const insertFts = db.prepare(
2334
- `INSERT INTO architecture_patterns_fts (patternId, summary, area, sourceFiles, symbols)
2335
- VALUES (?, ?, ?, ?, ?)`
2376
+ `INSERT INTO architecture_patterns_fts (rowid, patternId, summary, area, sourceFiles, symbols)
2377
+ VALUES (?, ?, ?, ?, ?, ?)`
2336
2378
  );
2337
2379
  options.onProgress?.({
2338
2380
  stage: "writing_architecture_data",
@@ -2342,7 +2384,7 @@ function insertArchitectureData(db, repoId, repo, architecture, options = {}) {
2342
2384
  kind: "patterns"
2343
2385
  });
2344
2386
  for (const [index, pattern] of architecture.patterns.entries()) {
2345
- insertPattern.run(
2387
+ const patternInsert = insertPattern.run(
2346
2388
  pattern.id,
2347
2389
  repoId,
2348
2390
  pattern.repo,
@@ -2356,6 +2398,7 @@ function insertArchitectureData(db, repoId, repo, architecture, options = {}) {
2356
2398
  pattern.createdAt
2357
2399
  );
2358
2400
  insertFts.run(
2401
+ Number(patternInsert.lastInsertRowid),
2359
2402
  pattern.id,
2360
2403
  pattern.sanitizedSummary,
2361
2404
  pattern.area,