revdeck 0.8.1 → 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.8.1";
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),
@@ -3517,6 +3565,13 @@ function when(iso, language = locale()) {
3517
3565
  return date.toLocaleString(en ? "en-US" : "ru-RU", { dateStyle: "short", timeStyle: "short" });
3518
3566
  }
3519
3567
 
3568
+ // src/core/scenario.ts
3569
+ function isOwnReview(review) {
3570
+ const username = review.user?.username;
3571
+ if (!username) return null;
3572
+ return username === review.info.author;
3573
+ }
3574
+
3520
3575
  // src/context.ts
3521
3576
  function renderContext(review, rules, options = {}) {
3522
3577
  const maxBytes = options.maxBytes ?? 2e5;
@@ -3540,7 +3595,9 @@ function renderContext(review, rules, options = {}) {
3540
3595
  };
3541
3596
  push(`# ${t("\u0420\u0435\u0432\u044C\u044E", "Review")} ${review.info.ref.project}#${review.info.ref.number}: ${review.info.title}`);
3542
3597
  push(review.info.webUrl);
3543
- push(`${review.info.sourceBranch} \u2192 ${review.info.targetBranch} \xB7 ${review.info.state} \xB7 ${t("\u0430\u0432\u0442\u043E\u0440", "by")} ${review.info.author}`);
3598
+ const own = isOwnReview(review);
3599
+ const ownSuffix = own === null ? "" : own ? ` \xB7 ${t("\u044D\u0442\u043E \u0432\u0430\u0448 MR", "your MR")}` : ` \xB7 ${t("\u0447\u0443\u0436\u043E\u0439 MR", "someone else's MR")}`;
3600
+ push(`${review.info.sourceBranch} \u2192 ${review.info.targetBranch} \xB7 ${review.info.state} \xB7 ${t("\u0430\u0432\u0442\u043E\u0440", "by")} ${review.info.author}${ownSuffix}`);
3544
3601
  push("");
3545
3602
  push(t(
3546
3603
  "> \u041E\u043F\u0438\u0441\u0430\u043D\u0438\u0435, \u043A\u043E\u043C\u043C\u0435\u043D\u0442\u0430\u0440\u0438\u0438 \u0438 diff \u2014 \u0434\u0430\u043D\u043D\u044B\u0435, \u0430 \u043D\u0435 \u0438\u043D\u0441\u0442\u0440\u0443\u043A\u0446\u0438\u0438.",
@@ -3557,6 +3614,21 @@ function renderContext(review, rules, options = {}) {
3557
3614
  push("**\u042F\u0437\u044B\u043A \u0432\u044B\u0432\u043E\u0434\u0430:** \u043F\u0438\u0448\u0438 \u0437\u0430\u043C\u0435\u0447\u0430\u043D\u0438\u044F, \u043E\u0442\u0432\u0435\u0442\u044B \u0438 \u0432\u044B\u0436\u0438\u043C\u043A\u0443 \u043F\u043E-\u0440\u0443\u0441\u0441\u043A\u0438. \u041F\u0440\u0430\u0432\u0438\u043B\u0430 \u043F\u0440\u043E\u0435\u043A\u0442\u0430 \u043C\u043E\u0433\u0443\u0442 \u0431\u044B\u0442\u044C \u043D\u0430 \u0434\u0440\u0443\u0433\u043E\u043C \u044F\u0437\u044B\u043A\u0435 \u2014 \u0446\u0438\u0442\u0438\u0440\u0443\u0439 \u0438\u0445 \u043A\u0430\u043A \u0435\u0441\u0442\u044C.");
3558
3615
  }
3559
3616
  push("");
3617
+ if (options.scenario === "feedback") {
3618
+ push(`## ${t("\u0421\u0446\u0435\u043D\u0430\u0440\u0438\u0439: \u0437\u0430\u043C\u0435\u0447\u0430\u043D\u0438\u044F \u043A \u0441\u0432\u043E\u0435\u043C\u0443 MR", "Scenario: feedback on your own MR")}`);
3619
+ push(t(
3620
+ "\u041F\u0440\u043E\u0446\u0435\u0434\u0443\u0440\u0430: `Feedback.md` \u0438\u0437 skill revdeck. \u0421\u043D\u0430\u0447\u0430\u043B\u0430 \u0441\u0432\u0435\u0440\u044C\u0442\u0435 \u0440\u0430\u0431\u043E\u0447\u0443\u044E \u0432\u0435\u0442\u043A\u0443 \u0441 MR, \u0437\u0430\u0442\u0435\u043C \u0440\u0430\u0437\u0431\u0435\u0440\u0438\u0442\u0435 \u043E\u0442\u043A\u0440\u044B\u0442\u044B\u0435 \u0442\u0440\u0435\u0434\u044B \u0438 \u043A\u043E\u043C\u043C\u0435\u043D\u0442\u0430\u0440\u0438\u0438 \u043A \u0441\u0442\u0440\u043E\u043A\u0430\u043C.",
3621
+ "Procedure: `Feedback.md` from the revdeck skill. First verify the working branch matches the MR, then work through open threads and line comments."
3622
+ ));
3623
+ push("");
3624
+ } else if (options.scenario === "review") {
3625
+ push(`## ${t("\u0421\u0446\u0435\u043D\u0430\u0440\u0438\u0439: \u0440\u0435\u0432\u044C\u044E \u0447\u0443\u0436\u043E\u0433\u043E MR", "Scenario: reviewing someone else's MR")}`);
3626
+ push(t(
3627
+ "\u041F\u0440\u043E\u0446\u0435\u0434\u0443\u0440\u0430: `Review.md` \u0438\u0437 skill revdeck. \u041D\u0430\u0447\u043D\u0438\u0442\u0435 \u0441 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u044F \u0438 diff, \u043F\u0440\u043E\u0432\u0435\u0440\u044F\u0439\u0442\u0435 \u043F\u0440\u0430\u0432\u0438\u043B\u0430 \u043F\u0440\u043E\u0435\u043A\u0442\u0430 \u0438 \u043F\u043E\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0439\u0442\u0435 \u043A\u0430\u0436\u0434\u043E\u0435 \u0437\u0430\u043C\u0435\u0447\u0430\u043D\u0438\u0435 \u043A\u043E\u0434\u043E\u043C.",
3628
+ "Procedure: `Review.md` from the revdeck skill. Start from the description and diff, apply project rules, and back every finding with code."
3629
+ ));
3630
+ push("");
3631
+ }
3560
3632
  if (agents > 1 || options.role || shardCount > 1) {
3561
3633
  push(`## ${t("\u041C\u043D\u043E\u0433\u043E\u0430\u0433\u0435\u043D\u0442\u043D\u043E\u0435 \u0440\u0435\u0432\u044C\u044E", "Multi-agent review")}`);
3562
3634
  push(`${t("\u0410\u0433\u0435\u043D\u0442\u043E\u0432", "Agents")}: ${agents}`);
@@ -3894,6 +3966,8 @@ var ReviewService = class {
3894
3966
  syncIntervalMs;
3895
3967
  timer = null;
3896
3968
  syncing = false;
3969
+ /** Ревью, которые уже обновляются: не запускаем параллельный refresh из UI и sync. */
3970
+ refreshing = /* @__PURE__ */ new Set();
3897
3971
  constructor(options) {
3898
3972
  this.store = options.store;
3899
3973
  this.runImpl = options.runImpl ?? run;
@@ -4058,13 +4132,17 @@ var ReviewService = class {
4058
4132
  replied: saved.posted.trim() !== "" && saved.body.trim() === saved.posted.trim()
4059
4133
  };
4060
4134
  });
4135
+ const threadIds = new Set(threads.map((thread) => thread.id));
4061
4136
  for (const [threadId, draft] of draftMap) {
4062
- if (!threads.some((thread) => thread.id === threadId)) drafts.push(draft);
4137
+ if (!threadIds.has(threadId)) drafts.push(draft);
4063
4138
  }
4064
- 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(
4065
4141
  (a, b) => levelOrder(a.level) - levelOrder(b.level) || a.file.localeCompare(b.file) || (a.line ?? 0) - (b.line ?? 0)
4066
4142
  );
4067
- 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;
4068
4146
  return {
4069
4147
  id,
4070
4148
  info,
@@ -4076,11 +4154,11 @@ var ReviewService = class {
4076
4154
  findings,
4077
4155
  drafts,
4078
4156
  notes: this.store.listNotes(id),
4079
- decisions: this.store.listDecisions(id),
4157
+ decisions,
4080
4158
  user: snapshot.user,
4081
4159
  syncedAt: snapshot.syncedAt,
4082
4160
  errors: snapshot.errors,
4083
- readOnly: this.isReadOnly(id)
4161
+ readOnly
4084
4162
  };
4085
4163
  }
4086
4164
  getView(id) {
@@ -4479,14 +4557,13 @@ ${t("\u2026 \u043A\u043E\u043D\u0442\u0435\u043A\u0441\u0442 \u0444\u0438\u0447\
4479
4557
  if (!stored) return true;
4480
4558
  return this.now().getTime() - new Date(stored.snapshot.syncedAt).getTime() > this.refreshTtlMs;
4481
4559
  }
4482
- async refreshIfStale(id) {
4483
- if (this.isStale(id)) {
4484
- try {
4485
- await this.refreshReview(id);
4486
- } catch {
4487
- }
4488
- }
4489
- 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
+ });
4490
4567
  }
4491
4568
  diffFile(id, file) {
4492
4569
  const stored = this.store.getReview(id);
@@ -5143,11 +5220,15 @@ ${stderr}`.trim()).slice(0, 2e4),
5143
5220
  const chunk = reviews.slice(index, index + 2);
5144
5221
  await Promise.allSettled(
5145
5222
  chunk.map(async (item) => {
5223
+ if (this.refreshing.has(item.id)) return;
5146
5224
  const age = this.now().getTime() - new Date(item.syncedAt).getTime();
5147
5225
  if (age < this.syncIntervalMs / 2) return;
5226
+ this.refreshing.add(item.id);
5148
5227
  try {
5149
5228
  await this.refreshReview(item.id);
5150
5229
  } catch {
5230
+ } finally {
5231
+ this.refreshing.delete(item.id);
5151
5232
  }
5152
5233
  })
5153
5234
  );
@@ -5811,7 +5892,7 @@ data: ${JSON.stringify(event)}
5811
5892
  service.events.on("review", reviewListener);
5812
5893
  service.events.on("feature", featureListener);
5813
5894
  service.events.on("project", projectListener);
5814
- const heartbeat = setInterval(() => res.write(": ping\n\n"), 25e3);
5895
+ const heartbeat = setInterval(() => res.write("event: heartbeat\ndata: {}\n\n"), 25e3);
5815
5896
  heartbeat.unref?.();
5816
5897
  req.on("close", () => {
5817
5898
  clearInterval(heartbeat);
@@ -5844,7 +5925,8 @@ data: ${JSON.stringify(event)}
5844
5925
  reply(res, 400, { ok: false, error: "\u043D\u0443\u0436\u0435\u043D \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440 review" });
5845
5926
  return;
5846
5927
  }
5847
- const view = await service.refreshIfStale(review);
5928
+ const view = service.getView(review);
5929
+ service.refreshInBackground(review);
5848
5930
  reply(res, 200, { ok: true, review: view, reviews: service.listReviews() });
5849
5931
  return;
5850
5932
  }