revdeck 0.9.0 → 0.10.0

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/daemon.js CHANGED
@@ -438,6 +438,9 @@ function snippetAround(files, path12, line, radius = 6) {
438
438
  const flat = file.hunks.flatMap((hunk) => hunk.lines);
439
439
  const target = flat.findIndex((item) => item.newLine === line);
440
440
  if (target < 0) return null;
441
+ return snippetAt(flat, target, line, radius);
442
+ }
443
+ function snippetAt(flat, target, line, radius) {
441
444
  return flat.slice(Math.max(0, target - radius), target + radius + 1).map((item) => ({
442
445
  kind: item.kind,
443
446
  oldLine: item.oldLine,
@@ -446,6 +449,32 @@ function snippetAround(files, path12, line, radius = 6) {
446
449
  target: item.newLine === line
447
450
  }));
448
451
  }
452
+ function createSnippetReader(files) {
453
+ const cache = /* @__PURE__ */ new Map();
454
+ return (path12, line, radius = 6) => {
455
+ if (line == null) return null;
456
+ let entry = cache.get(path12);
457
+ if (entry === void 0) {
458
+ const file = findFile(files, path12);
459
+ if (!file) {
460
+ entry = null;
461
+ } else {
462
+ const flat = file.hunks.flatMap((hunk) => hunk.lines);
463
+ const byNewLine = /* @__PURE__ */ new Map();
464
+ for (let index = 0; index < flat.length; index += 1) {
465
+ const value = flat[index].newLine;
466
+ if (value !== null && !byNewLine.has(value)) byNewLine.set(value, index);
467
+ }
468
+ entry = { flat, byNewLine };
469
+ }
470
+ cache.set(path12, entry);
471
+ }
472
+ if (!entry) return null;
473
+ const target = entry.byNewLine.get(line);
474
+ if (target === void 0) return null;
475
+ return snippetAt(entry.flat, target, line, radius);
476
+ };
477
+ }
449
478
  function linePosition(files, path12, line) {
450
479
  const file = findFile(files, path12);
451
480
  const found = findNewLine(files, path12, line);
@@ -1318,7 +1347,7 @@ async function writeFileAtomic(file, data, mode = 384, dirMode = 448) {
1318
1347
  init_redact();
1319
1348
 
1320
1349
  // src/version.ts
1321
- var VERSION = "0.9.0";
1350
+ var VERSION = "0.10.0";
1322
1351
  var PROTOCOL_VERSION = 2;
1323
1352
 
1324
1353
  // src/daemon/main.ts
@@ -1410,6 +1439,12 @@ function reviewItem(row) {
1410
1439
  }
1411
1440
  var Store = class {
1412
1441
  db;
1442
+ /**
1443
+ * Последний разобранный снимок. UI и фоновый sync многократно читают одно
1444
+ * ревью; повторный JSON.parse большого diff занимает сотни миллисекунд.
1445
+ * Один снимок — компромисс между скоростью и памятью.
1446
+ */
1447
+ snapshotCache = null;
1413
1448
  constructor(file) {
1414
1449
  if (file !== ":memory:") mkdirSync(path2.dirname(file), { recursive: true, mode: 448 });
1415
1450
  this.db = new DatabaseSync(file);
@@ -1971,6 +2006,7 @@ var Store = class {
1971
2006
  snapshot.syncedAt
1972
2007
  );
1973
2008
  this.db.prepare("UPDATE reviews SET synced_at = ? WHERE id = ?").run(snapshot.syncedAt, id);
2009
+ this.snapshotCache = null;
1974
2010
  }
1975
2011
  listReviews(project) {
1976
2012
  const rows = project ? this.db.prepare("SELECT * FROM reviews WHERE project_key = ? ORDER BY updated_at DESC, number DESC").all(project) : this.db.prepare("SELECT * FROM reviews ORDER BY updated_at DESC, number DESC").all();
@@ -1979,8 +2015,27 @@ var Store = class {
1979
2015
  getReview(id) {
1980
2016
  const row = this.db.prepare("SELECT * FROM reviews WHERE id = ?").get(id);
1981
2017
  if (!row) return null;
1982
- const data = this.db.prepare("SELECT * FROM review_data WHERE review_id = ?").get(id);
1983
- if (!data) return null;
2018
+ const meta = this.db.prepare("SELECT synced_at FROM review_data WHERE review_id = ?").get(id);
2019
+ if (!meta) return null;
2020
+ const syncedAt = text(meta, "synced_at");
2021
+ let snapshot;
2022
+ if (this.snapshotCache?.id === id && this.snapshotCache.syncedAt === syncedAt) {
2023
+ snapshot = this.snapshotCache.snapshot;
2024
+ } else {
2025
+ const data = this.db.prepare("SELECT * FROM review_data WHERE review_id = ?").get(id);
2026
+ if (!data) return null;
2027
+ snapshot = {
2028
+ diff: json(data, "diff_json", []),
2029
+ threads: json(data, "threads_json", []),
2030
+ reactions: json(data, "reactions_json", []),
2031
+ user: json(data, "user_json", null),
2032
+ config: json(data, "config_json", {}),
2033
+ configPath: data.config_path ? String(data.config_path) : null,
2034
+ errors: json(data, "errors_json", []),
2035
+ syncedAt
2036
+ };
2037
+ this.snapshotCache = { id, syncedAt, snapshot };
2038
+ }
1984
2039
  return {
1985
2040
  row: {
1986
2041
  id: text(row, "id"),
@@ -1997,16 +2052,7 @@ var Store = class {
1997
2052
  synced_at: text(row, "synced_at"),
1998
2053
  root: text(row, "root")
1999
2054
  },
2000
- snapshot: {
2001
- diff: json(data, "diff_json", []),
2002
- threads: json(data, "threads_json", []),
2003
- reactions: json(data, "reactions_json", []),
2004
- user: json(data, "user_json", null),
2005
- config: json(data, "config_json", {}),
2006
- configPath: data.config_path ? String(data.config_path) : null,
2007
- errors: json(data, "errors_json", []),
2008
- syncedAt: text(data, "synced_at")
2009
- }
2055
+ snapshot
2010
2056
  };
2011
2057
  }
2012
2058
  info(id) {
@@ -2060,6 +2106,7 @@ var Store = class {
2060
2106
  }
2061
2107
  removeReview(id) {
2062
2108
  this.db.prepare("DELETE FROM reviews WHERE id = ?").run(id);
2109
+ if (this.snapshotCache?.id === id) this.snapshotCache = null;
2063
2110
  }
2064
2111
  // --- summary ---
2065
2112
  getSummary(id) {
@@ -3480,15 +3527,16 @@ function fileSummaries(diff) {
3480
3527
  }
3481
3528
  function toReviewView(review, now = Date.now()) {
3482
3529
  const { diff, threads, findings, ...rest } = review;
3530
+ const snippet = createSnippetReader(diff);
3483
3531
  return {
3484
3532
  ...rest,
3485
3533
  threads: threads.map((thread) => ({
3486
3534
  ...thread,
3487
- snippet: thread.path && thread.line ? snippetAround(diff, thread.path, thread.line, 4) : null
3535
+ snippet: thread.path && thread.line ? snippet(thread.path, thread.line, 4) : null
3488
3536
  })),
3489
3537
  findings: findings.map((finding) => ({
3490
3538
  ...finding,
3491
- snippet: finding.file && finding.line ? snippetAround(diff, finding.file, finding.line, 4) : null
3539
+ snippet: finding.file && finding.line ? snippet(finding.file, finding.line, 4) : null
3492
3540
  })),
3493
3541
  files: fileSummaries(diff),
3494
3542
  stats: diffStats(diff),
@@ -3918,6 +3966,8 @@ var ReviewService = class {
3918
3966
  syncIntervalMs;
3919
3967
  timer = null;
3920
3968
  syncing = false;
3969
+ /** Ревью, которые уже обновляются: не запускаем параллельный refresh из UI и sync. */
3970
+ refreshing = /* @__PURE__ */ new Set();
3921
3971
  constructor(options) {
3922
3972
  this.store = options.store;
3923
3973
  this.runImpl = options.runImpl ?? run;
@@ -4082,13 +4132,17 @@ var ReviewService = class {
4082
4132
  replied: saved.posted.trim() !== "" && saved.body.trim() === saved.posted.trim()
4083
4133
  };
4084
4134
  });
4135
+ const threadIds = new Set(threads.map((thread) => thread.id));
4085
4136
  for (const [threadId, draft] of draftMap) {
4086
- if (!threads.some((thread) => thread.id === threadId)) drafts.push(draft);
4137
+ if (!threadIds.has(threadId)) drafts.push(draft);
4087
4138
  }
4088
- const findings = applyDecisions(this.store.listFindings(id), this.store.listDecisions(id)).sort(
4139
+ const decisions = this.store.listDecisions(id);
4140
+ const findings = applyDecisions(this.store.listFindings(id), decisions).sort(
4089
4141
  (a, b) => levelOrder(a.level) - levelOrder(b.level) || a.file.localeCompare(b.file) || (a.line ?? 0) - (b.line ?? 0)
4090
4142
  );
4091
- const groups = computeGroups(snapshot.diff, this.configOf(id)?.review?.groups ?? []);
4143
+ const config = snapshot.config;
4144
+ const groups = computeGroups(snapshot.diff, config.review?.groups ?? []);
4145
+ const readOnly = process.env.REVDECK_READ_ONLY === "1" || config.review?.readOnly === true;
4092
4146
  return {
4093
4147
  id,
4094
4148
  info,
@@ -4100,11 +4154,11 @@ var ReviewService = class {
4100
4154
  findings,
4101
4155
  drafts,
4102
4156
  notes: this.store.listNotes(id),
4103
- decisions: this.store.listDecisions(id),
4157
+ decisions,
4104
4158
  user: snapshot.user,
4105
4159
  syncedAt: snapshot.syncedAt,
4106
4160
  errors: snapshot.errors,
4107
- readOnly: this.isReadOnly(id)
4161
+ readOnly
4108
4162
  };
4109
4163
  }
4110
4164
  getView(id) {
@@ -4503,14 +4557,13 @@ ${t("\u2026 \u043A\u043E\u043D\u0442\u0435\u043A\u0441\u0442 \u0444\u0438\u0447\
4503
4557
  if (!stored) return true;
4504
4558
  return this.now().getTime() - new Date(stored.snapshot.syncedAt).getTime() > this.refreshTtlMs;
4505
4559
  }
4506
- async refreshIfStale(id) {
4507
- if (this.isStale(id)) {
4508
- try {
4509
- await this.refreshReview(id);
4510
- } catch {
4511
- }
4512
- }
4513
- return this.getView(id);
4560
+ /** Не блокирует ответ: обновляет снимок в фоне, UI получит его через SSE. */
4561
+ refreshInBackground(id) {
4562
+ if (!this.isStale(id) || this.refreshing.has(id)) return;
4563
+ this.refreshing.add(id);
4564
+ void this.refreshReview(id).catch(() => void 0).finally(() => {
4565
+ this.refreshing.delete(id);
4566
+ });
4514
4567
  }
4515
4568
  diffFile(id, file) {
4516
4569
  const stored = this.store.getReview(id);
@@ -5167,11 +5220,15 @@ ${stderr}`.trim()).slice(0, 2e4),
5167
5220
  const chunk = reviews.slice(index, index + 2);
5168
5221
  await Promise.allSettled(
5169
5222
  chunk.map(async (item) => {
5223
+ if (this.refreshing.has(item.id)) return;
5170
5224
  const age = this.now().getTime() - new Date(item.syncedAt).getTime();
5171
5225
  if (age < this.syncIntervalMs / 2) return;
5226
+ this.refreshing.add(item.id);
5172
5227
  try {
5173
5228
  await this.refreshReview(item.id);
5174
5229
  } catch {
5230
+ } finally {
5231
+ this.refreshing.delete(item.id);
5175
5232
  }
5176
5233
  })
5177
5234
  );
@@ -5835,7 +5892,7 @@ data: ${JSON.stringify(event)}
5835
5892
  service.events.on("review", reviewListener);
5836
5893
  service.events.on("feature", featureListener);
5837
5894
  service.events.on("project", projectListener);
5838
- const heartbeat = setInterval(() => res.write(": ping\n\n"), 25e3);
5895
+ const heartbeat = setInterval(() => res.write("event: heartbeat\ndata: {}\n\n"), 25e3);
5839
5896
  heartbeat.unref?.();
5840
5897
  req.on("close", () => {
5841
5898
  clearInterval(heartbeat);
@@ -5868,7 +5925,8 @@ data: ${JSON.stringify(event)}
5868
5925
  reply(res, 400, { ok: false, error: "\u043D\u0443\u0436\u0435\u043D \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440 review" });
5869
5926
  return;
5870
5927
  }
5871
- const view = await service.refreshIfStale(review);
5928
+ const view = service.getView(review);
5929
+ service.refreshInBackground(review);
5872
5930
  reply(res, 200, { ok: true, review: view, reviews: service.listReviews() });
5873
5931
  return;
5874
5932
  }