reslop 0.1.9 → 0.2.1

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/README.md +3 -1
  3. package/bin/reslop.js +2 -2
  4. package/lib/ansi.js +148 -5
  5. package/lib/api.js +4 -2
  6. package/lib/capabilities.js +13 -14
  7. package/lib/cli.js +38 -49
  8. package/lib/clipboard.js +1 -1
  9. package/lib/{deps/changes.js → deps-diff.js} +309 -19
  10. package/lib/deps.js +498 -29
  11. package/lib/detect.js +1 -7
  12. package/lib/diff/align.js +7 -10
  13. package/lib/diff/diff.js +53 -0
  14. package/lib/diff/display.js +5 -4
  15. package/lib/diff/inline.js +26 -21
  16. package/lib/diff/items.js +6 -4
  17. package/lib/diff/patch.js +1 -3
  18. package/lib/diff/unit.js +392 -0
  19. package/lib/files.js +5 -8
  20. package/lib/{git/branches.js → git-branches.js} +191 -11
  21. package/lib/{git/dependencies.js → git-deps.js} +111 -38
  22. package/lib/git-reword-todo.js +20 -0
  23. package/lib/git.js +606 -68
  24. package/lib/github.js +28 -197
  25. package/lib/gitlab.js +24 -195
  26. package/lib/highlight/bash.js +1 -3
  27. package/lib/highlight/css.js +1 -3
  28. package/lib/highlight/js.js +11 -14
  29. package/lib/highlight.js +6 -9
  30. package/lib/keys.js +14 -8
  31. package/lib/manifest.js +330 -0
  32. package/lib/remote.js +374 -0
  33. package/lib/render/branches.js +18 -100
  34. package/lib/render/chrome.js +71 -14
  35. package/lib/render/commits.js +69 -60
  36. package/lib/render/diff.js +20 -20
  37. package/lib/render/files.js +6 -34
  38. package/lib/render/frame.js +36 -13
  39. package/lib/render/notes.js +26 -8
  40. package/lib/render/primitives.js +99 -11
  41. package/lib/{render.js → render/render.js} +8 -8
  42. package/lib/render/unit.js +41 -0
  43. package/lib/review-merge.js +270 -0
  44. package/lib/review.js +782 -14
  45. package/lib/select.js +5 -10
  46. package/lib/session/accessors.js +9 -1
  47. package/lib/session/actions.js +61 -18
  48. package/lib/session/branches.js +116 -128
  49. package/lib/session/change-actions.js +202 -123
  50. package/lib/session/commit.js +201 -115
  51. package/lib/session/compose.js +181 -15
  52. package/lib/session/dispatch.js +110 -60
  53. package/lib/session/files-pane.js +156 -148
  54. package/lib/session/items.js +22 -30
  55. package/lib/session/lifecycle.js +151 -54
  56. package/lib/session/load.js +19 -33
  57. package/lib/session/navigation.js +12 -1
  58. package/lib/session/ops.js +28 -41
  59. package/lib/session/pointer.js +57 -57
  60. package/lib/session/review.js +23 -37
  61. package/lib/session/terminal.js +6 -12
  62. package/lib/session/todos.js +37 -13
  63. package/lib/session/unit.js +208 -0
  64. package/lib/session/update.js +33 -46
  65. package/lib/session/view.js +78 -9
  66. package/lib/session/watch.js +128 -24
  67. package/lib/session.js +22 -246
  68. package/lib/source.js +3 -3
  69. package/lib/update.js +40 -75
  70. package/lib/{git/proc.js → utilities.js} +75 -11
  71. package/lib/wrap.js +1 -3
  72. package/package.json +2 -2
  73. package/lib/deps/items.js +0 -134
  74. package/lib/deps/manifest.js +0 -152
  75. package/lib/deps/merge.js +0 -120
  76. package/lib/deps/proposals.js +0 -164
  77. package/lib/deps/reports.js +0 -180
  78. package/lib/deps/rows.js +0 -176
  79. package/lib/deps/scan.js +0 -138
  80. package/lib/deps/usage.js +0 -68
  81. package/lib/deps/versions.js +0 -119
  82. package/lib/diff.js +0 -33
  83. package/lib/git/changes.js +0 -164
  84. package/lib/git/read.js +0 -253
  85. package/lib/git/run.js +0 -100
  86. package/lib/npm/commands.js +0 -100
  87. package/lib/remote/changes.js +0 -37
  88. package/lib/remote/headers.js +0 -36
  89. package/lib/remote/http.js +0 -122
  90. package/lib/remote/notes.js +0 -94
  91. package/lib/review/format.js +0 -434
  92. package/lib/review/storage.js +0 -150
  93. package/lib/review/store.js +0 -178
  94. package/lib/review/templates.js +0 -49
  95. package/lib/session/origins.js +0 -7
  96. package/lib/sys.js +0 -84
package/lib/review.js CHANGED
@@ -1,19 +1,785 @@
1
1
  'use strict';
2
2
 
3
- const store = require('./review/store.js');
4
- const format = require('./review/format.js');
5
- const templates = require('./review/templates.js');
6
- const storage = require('./review/storage.js');
7
-
8
- const { createStore, noteCounts, hasNotes } = store;
9
- const { setFeedback, setCode, addTodo, removeTodo } = store;
10
- const { setTodoText, checkLabel, feedbackKey, applyImportedNotes } = store;
11
- const { parseFrontmatterStatus, serializeReview, parseReview } = format;
12
- const { TEMPLATE_SHOW, rankedTemplates, prefixTemplates } = templates;
13
- const { upsertTemplate, rememberTemplate } = templates;
14
- const { AUTOSAVE_MS, allocateReviewPath, listReviewNames } = storage;
15
- const { latestReviewName, resolveReviewPath } = storage;
16
- const { loadReview, loadTemplates, flushReview } = storage;
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+
6
+ const { TODO_FILE, REVIEW_DIR } = require('./files.js');
7
+ const { captureBaseline, mergeReview } = require('./review-merge.js');
8
+
9
+ const createStore = (reviewPath, templates = []) => ({
10
+ reviewPath,
11
+ dirty: false,
12
+ nextTodoId: 1,
13
+ feedback: new Map(),
14
+ code: new Map(),
15
+ todos: [],
16
+ templates: [...templates],
17
+ status: 'editing',
18
+ baseline: null,
19
+ });
20
+
21
+ const noteCounts = (store) => {
22
+ const counts = { feedback: 0, todo: 0, todoDone: 0, code: 0 };
23
+ if (!store) return counts;
24
+ for (const todo of store.todos) {
25
+ if (!todo.text.trim()) continue;
26
+ counts.todo += 1;
27
+ if (todo.done) counts.todoDone += 1;
28
+ }
29
+ for (const note of store.feedback.values()) {
30
+ if (note.text.trim()) counts.feedback += 1;
31
+ }
32
+ counts.code = store.code.size;
33
+ return counts;
34
+ };
35
+
36
+ const hasNotes = (store) => {
37
+ const counts = noteCounts(store);
38
+ return counts.feedback > 0 || counts.todo > 0 || counts.code > 0;
39
+ };
40
+
41
+ const setFeedback = (store, key, note) => {
42
+ const text = note.text ?? '';
43
+ const trimmed = text.trim();
44
+ const prev = store.feedback.get(key);
45
+ if (!trimmed) {
46
+ if (!prev) return;
47
+ store.feedback.delete(key);
48
+ store.dirty = true;
49
+ return;
50
+ }
51
+ if (prev && prev.text === text) return;
52
+ store.feedback.set(key, {
53
+ file: note.file,
54
+ oldStart: note.oldStart ?? 0,
55
+ newStart: note.newStart ?? 0,
56
+ blockId: note.blockId ?? 0,
57
+ origin: note.origin ?? '',
58
+ header: note.header ?? '',
59
+ text,
60
+ done: note.done ?? prev?.done ?? false,
61
+ });
62
+ store.dirty = true;
63
+ };
64
+
65
+ const setCode = (store, key, note) => {
66
+ const prev = store.code.get(key);
67
+ if (note.clear) {
68
+ if (!prev) return;
69
+ store.code.delete(key);
70
+ store.dirty = true;
71
+ return;
72
+ }
73
+ const text = note.text ?? '';
74
+ if (prev && prev.text === text) return;
75
+ store.code.set(key, {
76
+ file: note.file,
77
+ oldStart: note.oldStart ?? 0,
78
+ newStart: note.newStart ?? 0,
79
+ blockId: note.blockId ?? 0,
80
+ origin: note.origin ?? '',
81
+ header: note.header ?? '',
82
+ text,
83
+ done: note.done ?? prev?.done ?? false,
84
+ });
85
+ store.dirty = true;
86
+ };
87
+
88
+ const addTodo = (store, file, text = '') => {
89
+ const id = store.nextTodoId;
90
+ store.nextTodoId += 1;
91
+ const todo = { id, file, text, done: false };
92
+ store.todos.push(todo);
93
+ store.dirty = true;
94
+ return todo;
95
+ };
96
+
97
+ const removeTodo = (store, id) => {
98
+ const next = [];
99
+ let found = false;
100
+ for (const todo of store.todos) {
101
+ if (todo.id === id) {
102
+ found = true;
103
+ continue;
104
+ }
105
+ next.push(todo);
106
+ }
107
+ if (!found) return false;
108
+ store.todos = next;
109
+ store.dirty = true;
110
+ return true;
111
+ };
112
+
113
+ const setTodoText = (store, id, text) => {
114
+ const i = store.todos.findIndex((todo) => todo.id === id);
115
+ if (i < 0) return;
116
+ const prev = store.todos[i];
117
+ const trimmed = text.trim();
118
+ if (!trimmed) return void removeTodo(store, id);
119
+ if (prev.text === text) return;
120
+ store.todos[i] = { id: prev.id, file: prev.file, text, done: prev.done };
121
+ store.dirty = true;
122
+ };
123
+
124
+ const setTodoDone = (store, id, done) => {
125
+ const i = store.todos.findIndex((todo) => todo.id === id);
126
+ if (i < 0) return;
127
+ const prev = store.todos[i];
128
+ if (prev.done === done) return;
129
+ store.todos[i] = { id: prev.id, file: prev.file, text: prev.text, done };
130
+ store.dirty = true;
131
+ };
132
+
133
+ const checkLabel = (text, done) => {
134
+ const mark = done ? 'x' : ' ';
135
+ return `[${mark}] ${text}`;
136
+ };
137
+
138
+ const feedbackKey = (note) => {
139
+ const file = note.file ?? '';
140
+ const oldStart = note.oldStart ?? 0;
141
+ const newStart = note.newStart ?? 0;
142
+ const block = note.blockId ?? 0;
143
+ return `${file}:${oldStart}:${newStart}:${block}`;
144
+ };
145
+
146
+ const applyImportedNotes = (store, imported) => {
147
+ if (!store || !imported) return;
148
+ const feedbacks = imported.feedback ?? [];
149
+ for (const note of feedbacks) {
150
+ const key = feedbackKey(note);
151
+ const prev = store.feedback.get(key);
152
+ const text = prev ? `${prev.text}\n\n${note.text}` : note.text;
153
+ const done = prev ? prev.done && !!note.done : !!note.done;
154
+ setFeedback(store, key, {
155
+ file: note.file,
156
+ oldStart: note.oldStart ?? 0,
157
+ newStart: note.newStart ?? 0,
158
+ blockId: note.blockId ?? 0,
159
+ origin: note.origin ?? '',
160
+ header: note.header ?? '',
161
+ text,
162
+ done,
163
+ });
164
+ }
165
+ const todos = imported.todos ?? [];
166
+ for (const todo of todos) {
167
+ const added = addTodo(store, todo.file || '', todo.text ?? '');
168
+ if (todo.done) added.done = true;
169
+ }
170
+ };
171
+
172
+ const setStatus = (store, status) => {
173
+ if (!store) return;
174
+ store.status = status;
175
+ store.dirty = true;
176
+ };
177
+
178
+ const TEMPLATE_SHOW = 5;
179
+
180
+ const rankedTemplates = (templates) => {
181
+ const ranked = [...templates];
182
+ ranked.sort((a, b) => {
183
+ const byCount = b.count - a.count;
184
+ if (byCount) return byCount;
185
+ return a.text.localeCompare(b.text);
186
+ });
187
+ return ranked;
188
+ };
189
+
190
+ const prefixTemplates = (templates, prefix) => {
191
+ if (!prefix) return templates;
192
+ return templates.filter((entry) => entry.text.startsWith(prefix));
193
+ };
194
+
195
+ const upsertTemplate = (templates, text) => {
196
+ const trimmed = text.trim();
197
+ if (!trimmed) return templates;
198
+ const next = [...templates];
199
+ const i = next.findIndex((entry) => entry.text === trimmed);
200
+ if (i < 0) {
201
+ next.push({ text: trimmed, count: 1 });
202
+ return next;
203
+ }
204
+ const prev = next[i];
205
+ next[i] = { text: prev.text, count: prev.count + 1 };
206
+ return next;
207
+ };
208
+
209
+ const rememberTemplate = (store, prevText, nextText) => {
210
+ const trimmed = nextText.trim();
211
+ if (!trimmed) return;
212
+ const prev = (prevText ?? '').trim();
213
+ if (prev === trimmed) return;
214
+ store.templates = upsertTemplate(store.templates, trimmed);
215
+ store.dirty = true;
216
+ };
217
+
218
+ const REVIEW_STATUSES = ['editing', 'ready', 'partial', 'done'];
219
+ const FEED_HEAD = /^### Feedback `([^`]+)` \+(\d+) \((.*)\)\s*$/;
220
+ const FEED_BLOCK = /^(.*), block (\d+)$/;
221
+ const FEED_BLOCK_ONLY = /^block (\d+)$/;
222
+ const FEED_META = /^<!-- reslop:(.*) -->\s*$/;
223
+ const FEED_SUFFIX = /^(.*) - (.+):(\d+):(\d+):(\d+)$/;
224
+ const CHECK_ITEM = /^- \[([ xX])\] (.*)$/;
225
+ const CODE_HEAD = /^- \[([ xX])\] code `([^`]+)`\s*$/;
226
+ const FENCE_LINE = /^(`{3,})\s*$/;
227
+ const FILE_QUOTE = /^> (.+)$/;
228
+ const STATUS_LINE = /^status:\s*(\S+)\s*$/;
229
+ const GIT_ORIGINS = ['unstaged', 'staged', 'untracked', 'commit', 'pr'];
230
+ const SECTION_LINE = {
231
+ '### Todo': 'todo',
232
+ '### Feedback': 'feedback',
233
+ };
234
+
235
+ const AGENT_INSTRUCTIONS = [
236
+ '- Execute reviews with `status` `ready, partial, editing`',
237
+ '- Do not start or change `done` review files',
238
+ '- Work through unchecked items. After finishing one, mark it `[x]`',
239
+ '- For `code` items, replace added lines at that location',
240
+ '- If not `editing`, set `status` to `partial` if some remain, ' +
241
+ 'or `done` if all are `[x]`',
242
+ '- Do not redo items already marked `[x]`',
243
+ ].join('\n');
244
+
245
+ const toInt = (text) => parseInt(text, 10);
246
+
247
+ const canonicalStatus = (raw) => {
248
+ if (!raw) return '';
249
+ const name = raw === 'pending' ? 'ready' : raw;
250
+ if (REVIEW_STATUSES.includes(name)) return name;
251
+ return '';
252
+ };
253
+
254
+ const parseFrontmatterStatus = (text) => {
255
+ if (!text.startsWith('---')) return '';
256
+ const nl = text.indexOf('\n');
257
+ if (nl < 0) return '';
258
+ const rest = text.slice(nl + 1);
259
+ const end = rest.indexOf('\n---');
260
+ if (end < 0) return '';
261
+ const matter = rest.slice(0, end);
262
+ for (const line of matter.split('\n')) {
263
+ const match = STATUS_LINE.exec(line);
264
+ if (match) return canonicalStatus(match[1]);
265
+ }
266
+ return '';
267
+ };
268
+
269
+ const isDoneMark = (mark) => mark === 'x' || mark === 'X';
270
+
271
+ const pushTodo = (builder, file, text, done) => {
272
+ const id = builder.nextTodoId;
273
+ builder.nextTodoId += 1;
274
+ builder.todos.push({ id, file, text, done });
275
+ };
276
+
277
+ const checkboxLine = (text, done) => {
278
+ const flat = text.trim().replace(/\n/g, ' ');
279
+ const mark = done ? 'x' : ' ';
280
+ return `- [${mark}] ${flat}`;
281
+ };
282
+
283
+ const storeFeedback = (builder, note, key) => {
284
+ const next = {
285
+ file: note.file,
286
+ oldStart: note.oldStart ?? 0,
287
+ newStart: note.newStart ?? 0,
288
+ blockId: note.blockId ?? 0,
289
+ origin: note.origin ?? '',
290
+ header: note.header ?? '',
291
+ text: note.text,
292
+ done: note.done ?? false,
293
+ };
294
+ builder.feedback.set(key || feedbackKey(next), next);
295
+ };
296
+
297
+ const storeCode = (builder, note, key) => {
298
+ const next = {
299
+ file: note.file,
300
+ oldStart: note.oldStart ?? 0,
301
+ newStart: note.newStart ?? 0,
302
+ blockId: note.blockId ?? 0,
303
+ origin: note.origin ?? '',
304
+ header: note.header ?? '',
305
+ text: note.text,
306
+ done: note.done ?? false,
307
+ };
308
+ builder.code.set(key || feedbackKey(next), next);
309
+ };
310
+
311
+ const feedbackLine = (note) => {
312
+ const loc = feedbackKey(note);
313
+ return `${checkboxLine(note.text, note.done)} - ${loc}`;
314
+ };
315
+
316
+ const parseLoc = (raw) => {
317
+ const parts = raw.split(':');
318
+ const n = parts.length;
319
+ if (n < 4) return null;
320
+ const blockId = toInt(parts[n - 1]);
321
+ const newStart = toInt(parts[n - 2]);
322
+ const oldStart = toInt(parts[n - 3]);
323
+ if (!Number.isFinite(blockId + newStart + oldStart)) return null;
324
+ let fileParts = parts.slice(0, n - 3);
325
+ let origin = '';
326
+ if (fileParts.length > 1 && GIT_ORIGINS.includes(fileParts[0])) {
327
+ origin = fileParts[0];
328
+ fileParts = fileParts.slice(1);
329
+ }
330
+ if (!fileParts.length) return null;
331
+ const file = fileParts.join(':');
332
+ return { origin, file, oldStart, newStart, blockId };
333
+ };
334
+
335
+ const parseFeedSuffix = (textItem) => {
336
+ const match = FEED_SUFFIX.exec(textItem);
337
+ if (!match) return null;
338
+ const text = match[1];
339
+ const file = match[2];
340
+ const oldStart = toInt(match[3]);
341
+ const newStart = toInt(match[4]);
342
+ const blockId = toInt(match[5]);
343
+ return { text, file, oldStart, newStart, blockId };
344
+ };
345
+
346
+ const fileGroup = (groups, file) => {
347
+ let group = groups.get(file);
348
+ if (!group) {
349
+ group = { todos: [], feedback: [], code: [] };
350
+ groups.set(file, group);
351
+ }
352
+ return group;
353
+ };
354
+
355
+ const groupReviewNotes = (notes) => {
356
+ const groups = new Map();
357
+ for (const todo of notes.todos) {
358
+ if (!todo.text.trim()) continue;
359
+ fileGroup(groups, TODO_FILE).todos.push(todo);
360
+ }
361
+ for (const key of notes.feedback.keys()) {
362
+ const note = notes.feedback.get(key);
363
+ if (!note.text.trim()) continue;
364
+ fileGroup(groups, note.file).feedback.push({ key, note });
365
+ }
366
+ for (const key of notes.code.keys()) {
367
+ const note = notes.code.get(key);
368
+ fileGroup(groups, note.file).code.push({ key, note });
369
+ }
370
+ return groups;
371
+ };
372
+
373
+ const reviewHeader = (notes) => {
374
+ const name = path.basename(notes.reviewPath, '.md');
375
+ const status = canonicalStatus(notes.status) || 'editing';
376
+ return [
377
+ '---',
378
+ `status: ${status}`,
379
+ '---',
380
+ '',
381
+ `# reslop review ${name}`,
382
+ '',
383
+ '## Agent instructions',
384
+ '',
385
+ AGENT_INSTRUCTIONS,
386
+ '',
387
+ ];
388
+ };
389
+
390
+ const fenceFor = (text) => {
391
+ let n = 3;
392
+ for (const line of text.split('\n')) {
393
+ const match = /^(`{3,})/.exec(line);
394
+ if (!match) continue;
395
+ if (match[1].length >= n) n = match[1].length + 1;
396
+ }
397
+ return '`'.repeat(n);
398
+ };
399
+
400
+ const emitCodeItem = (lines, note) => {
401
+ const loc = feedbackKey(note);
402
+ const mark = note.done ? 'x' : ' ';
403
+ const fence = fenceFor(note.text);
404
+ lines.push(`- [${mark}] code \`${loc}\``);
405
+ lines.push(fence);
406
+ if (note.text !== '') lines.push(note.text);
407
+ lines.push(fence);
408
+ };
409
+
410
+ const emitReviewGroups = (lines, groups) => {
411
+ for (const file of groups.keys()) {
412
+ const group = groups.get(file);
413
+ lines.push(`## ${file}`);
414
+ lines.push('');
415
+ for (const todo of group.todos) {
416
+ lines.push(checkboxLine(todo.text, todo.done));
417
+ }
418
+ for (const item of group.feedback) {
419
+ lines.push(feedbackLine(item.note));
420
+ }
421
+ for (const item of group.code) {
422
+ emitCodeItem(lines, item.note);
423
+ }
424
+ lines.push('');
425
+ }
426
+ };
427
+
428
+ const serializeReview = (notes) => {
429
+ const lines = reviewHeader(notes);
430
+ emitReviewGroups(lines, groupReviewNotes(notes));
431
+ return `${lines.join('\n')}\n`;
432
+ };
433
+
434
+ const parseFeedbackHeading = (line) => {
435
+ const match = FEED_HEAD.exec(line);
436
+ if (!match) return null;
437
+ const file = match[1];
438
+ const newStart = toInt(match[2]);
439
+ const rest = match[3];
440
+ const only = FEED_BLOCK_ONLY.exec(rest);
441
+ if (only) {
442
+ return { file, newStart, header: '', blockId: toInt(only[1]) };
443
+ }
444
+ const withHead = FEED_BLOCK.exec(rest);
445
+ if (!withHead) {
446
+ return { file, newStart, header: rest, blockId: 0 };
447
+ }
448
+ const header = withHead[1];
449
+ const blockId = toInt(withHead[2]);
450
+ return { file, newStart, header, blockId };
451
+ };
452
+
453
+ const flushPendingFeedback = (builder, pending) => {
454
+ if (!pending || !(pending.text ?? '').trim()) return null;
455
+ storeFeedback(builder, pending, pending.key);
456
+ return null;
457
+ };
458
+
459
+ const headingFile = (line) => {
460
+ const quoted = FILE_QUOTE.exec(line);
461
+ if (quoted) return quoted[1];
462
+ if (line.startsWith('## ') && line !== '## Agent instructions') {
463
+ return line.slice(3);
464
+ }
465
+ return null;
466
+ };
467
+
468
+ const startFeedbackPending = (heading, file) => ({
469
+ file: heading.file || file,
470
+ newStart: heading.newStart,
471
+ header: heading.header,
472
+ blockId: heading.blockId,
473
+ oldStart: heading.newStart,
474
+ origin: '',
475
+ key: '',
476
+ text: '',
477
+ });
478
+
479
+ const applyFeedMeta = (pending, raw) => {
480
+ const loc = parseLoc(raw);
481
+ if (!loc) return pending;
482
+ pending.origin = loc.origin;
483
+ pending.file = loc.file;
484
+ pending.oldStart = loc.oldStart;
485
+ pending.newStart = loc.newStart;
486
+ pending.blockId = loc.blockId;
487
+ pending.key = feedbackKey(loc);
488
+ return pending;
489
+ };
490
+
491
+ const applyCheckItem = (builder, file, section, pending, textItem, done) => {
492
+ if (section === 'todo' && file) {
493
+ pushTodo(builder, file, textItem, done);
494
+ return pending;
495
+ }
496
+ const parsed = parseFeedSuffix(textItem);
497
+ if (parsed) {
498
+ flushPendingFeedback(builder, pending);
499
+ storeFeedback(builder, {
500
+ file: parsed.file || file,
501
+ oldStart: parsed.oldStart,
502
+ newStart: parsed.newStart,
503
+ blockId: parsed.blockId,
504
+ origin: '',
505
+ header: '',
506
+ text: parsed.text,
507
+ done,
508
+ });
509
+ return null;
510
+ }
511
+ if (pending) {
512
+ pending.text = textItem;
513
+ pending.done = done;
514
+ return flushPendingFeedback(builder, pending);
515
+ }
516
+ if (file && section !== 'feedback') {
517
+ pushTodo(builder, file, textItem, done);
518
+ }
519
+ return pending;
520
+ };
521
+
522
+ const applyReviewLine = (builder, file, section, pending, line) => {
523
+ const nextFile = headingFile(line);
524
+ if (nextFile !== null) {
525
+ pending = flushPendingFeedback(builder, pending);
526
+ return { file: nextFile, section: '', pending };
527
+ }
528
+ const nextSection = SECTION_LINE[line];
529
+ if (nextSection) {
530
+ pending = flushPendingFeedback(builder, pending);
531
+ return { file, section: nextSection, pending };
532
+ }
533
+ const heading = parseFeedbackHeading(line);
534
+ if (heading) {
535
+ flushPendingFeedback(builder, pending);
536
+ pending = startFeedbackPending(heading, file);
537
+ return { file, section: 'feedback', pending };
538
+ }
539
+ const meta = FEED_META.exec(line);
540
+ if (meta && pending) {
541
+ pending = applyFeedMeta(pending, meta[1]);
542
+ return { file, section, pending };
543
+ }
544
+ const item = CHECK_ITEM.exec(line);
545
+ if (!item) return { file, section, pending };
546
+ const done = isDoneMark(item[1]);
547
+ pending = applyCheckItem(builder, file, section, pending, item[2], done);
548
+ return { file, section, pending };
549
+ };
550
+
551
+ const startCodePending = (line) => {
552
+ const match = CODE_HEAD.exec(line);
553
+ if (!match) return null;
554
+ const loc = parseLoc(match[2]);
555
+ if (!loc) return null;
556
+ return {
557
+ done: isDoneMark(match[1]),
558
+ origin: loc.origin,
559
+ file: loc.file,
560
+ oldStart: loc.oldStart,
561
+ newStart: loc.newStart,
562
+ blockId: loc.blockId,
563
+ open: '',
564
+ lines: [],
565
+ };
566
+ };
567
+
568
+ const storeCodeNote = (builder, pending, text) => {
569
+ storeCode(builder, {
570
+ file: pending.file,
571
+ oldStart: pending.oldStart,
572
+ newStart: pending.newStart,
573
+ blockId: pending.blockId,
574
+ origin: pending.origin,
575
+ header: '',
576
+ text,
577
+ done: pending.done,
578
+ });
579
+ };
580
+
581
+ const flushPendingCode = (builder, pending) => {
582
+ if (!pending) return;
583
+ const text = pending.open ? pending.lines.join('\n') : '';
584
+ storeCodeNote(builder, pending, text);
585
+ };
586
+
587
+ const takeCodeLine = (builder, pending, line) => {
588
+ const fence = FENCE_LINE.exec(line);
589
+ if (!pending.open) {
590
+ if (fence) {
591
+ pending.open = fence[1];
592
+ return pending;
593
+ }
594
+ if (!line.trim()) return pending;
595
+ storeCodeNote(builder, pending, '');
596
+ return 'fallthrough';
597
+ }
598
+ if (fence && fence[1].length >= pending.open.length) {
599
+ storeCodeNote(builder, pending, pending.lines.join('\n'));
600
+ return null;
601
+ }
602
+ pending.lines.push(line);
603
+ return pending;
604
+ };
605
+
606
+ const parseReview = (text, reviewPath, templates = []) => {
607
+ const builder = createStore(reviewPath, templates);
608
+ builder.status = parseFrontmatterStatus(text) || 'editing';
609
+ let file = '';
610
+ let section = '';
611
+ let pending = null;
612
+ let pendingCode = null;
613
+ for (const line of text.split('\n')) {
614
+ if (pendingCode) {
615
+ const nextCode = takeCodeLine(builder, pendingCode, line);
616
+ if (nextCode === 'fallthrough') {
617
+ pendingCode = null;
618
+ } else {
619
+ pendingCode = nextCode;
620
+ continue;
621
+ }
622
+ }
623
+ const started = startCodePending(line);
624
+ if (started) {
625
+ pending = flushPendingFeedback(builder, pending);
626
+ pendingCode = started;
627
+ continue;
628
+ }
629
+ const next = applyReviewLine(builder, file, section, pending, line);
630
+ file = next.file;
631
+ section = next.section;
632
+ pending = next.pending;
633
+ }
634
+ flushPendingCode(builder, pendingCode);
635
+ flushPendingFeedback(builder, pending);
636
+ builder.dirty = false;
637
+ captureBaseline(builder);
638
+ return builder;
639
+ };
640
+
641
+ const TEMPLATES_FILE = 'templates.json';
642
+ const AUTOSAVE_MS = 3000;
643
+ const REVIEW_NAME = /^(\d{4}-\d{2}-\d{2})-(\d+)\.md$/;
644
+
645
+ const pad2 = (n) => `${n}`.padStart(2, '0');
646
+
647
+ const dateStamp = (date) => {
648
+ const y = date.getFullYear();
649
+ const month = pad2(date.getMonth() + 1);
650
+ const day = pad2(date.getDate());
651
+ return `${y}-${month}-${day}`;
652
+ };
653
+
654
+ const allocateReviewPath = (dir, date, existingNames) => {
655
+ const stamp = dateStamp(date);
656
+ let max = -1;
657
+ for (const name of existingNames) {
658
+ const match = REVIEW_NAME.exec(name);
659
+ if (!match) continue;
660
+ if (match[1] !== stamp) continue;
661
+ const n = toInt(match[2]);
662
+ if (n > max) max = n;
663
+ }
664
+ const next = max + 1;
665
+ const file = `${stamp}-${next < 100 ? pad2(next) : String(next)}.md`;
666
+ return path.join(dir, REVIEW_DIR, file);
667
+ };
668
+
669
+ const listReviewNames = (dir) => {
670
+ const folder = path.join(dir, REVIEW_DIR);
671
+ try {
672
+ return fs.readdirSync(folder);
673
+ } catch {
674
+ return [];
675
+ }
676
+ };
677
+
678
+ const compareReviewNames = (left, right) => {
679
+ const a = REVIEW_NAME.exec(left);
680
+ const b = REVIEW_NAME.exec(right);
681
+ if (!a && !b) return 0;
682
+ if (!a) return -1;
683
+ if (!b) return 1;
684
+ if (a[1] < b[1]) return -1;
685
+ if (a[1] > b[1]) return 1;
686
+ return toInt(a[2]) - toInt(b[2]);
687
+ };
688
+
689
+ const latestReviewName = (names) => {
690
+ let best = '';
691
+ for (const name of names) {
692
+ if (!REVIEW_NAME.test(name)) continue;
693
+ if (!best || compareReviewNames(name, best) > 0) best = name;
694
+ }
695
+ return best;
696
+ };
697
+
698
+ const readReviewStatus = (reviewPath) => {
699
+ try {
700
+ const text = fs.readFileSync(reviewPath, 'utf8');
701
+ return parseFrontmatterStatus(text);
702
+ } catch {
703
+ return '';
704
+ }
705
+ };
706
+
707
+ const resolveReviewPath = (dir, date, existingNames, extra = {}) => {
708
+ if (!extra.forceNew) {
709
+ const latest = latestReviewName(existingNames);
710
+ if (latest) {
711
+ const reviewPath = path.join(dir, REVIEW_DIR, latest);
712
+ if (readReviewStatus(reviewPath) === 'editing') {
713
+ return { reviewPath, resume: true };
714
+ }
715
+ }
716
+ }
717
+ return {
718
+ reviewPath: allocateReviewPath(dir, date, existingNames),
719
+ resume: false,
720
+ };
721
+ };
722
+
723
+ const loadReview = (reviewPath, templates) => {
724
+ const raw = fs.readFileSync(reviewPath, 'utf8');
725
+ return parseReview(raw, reviewPath, templates);
726
+ };
727
+
728
+ const loadTemplates = (dir) => {
729
+ const file = path.join(dir, REVIEW_DIR, TEMPLATES_FILE);
730
+ try {
731
+ const raw = fs.readFileSync(file, 'utf8');
732
+ const data = JSON.parse(raw);
733
+ if (!Array.isArray(data)) return [];
734
+ const templates = [];
735
+ for (const entry of data) {
736
+ if (!entry || typeof entry.text !== 'string') continue;
737
+ const count = toInt(entry.count);
738
+ templates.push({
739
+ text: entry.text,
740
+ count: Number.isFinite(count) ? count : 1,
741
+ });
742
+ }
743
+ return templates;
744
+ } catch {
745
+ return [];
746
+ }
747
+ };
748
+
749
+ const readReviewText = (reviewPath) => {
750
+ try {
751
+ return fs.readFileSync(reviewPath, 'utf8');
752
+ } catch (error) {
753
+ if (error.code === 'ENOENT') return '';
754
+ throw error;
755
+ }
756
+ };
757
+
758
+ const mergeDiskReview = (notes) => {
759
+ const text = readReviewText(notes.reviewPath);
760
+ if (!text.startsWith('---')) return;
761
+ const disk = parseReview(text, notes.reviewPath, notes.templates);
762
+ mergeReview(notes, disk);
763
+ };
764
+
765
+ const flushReview = (notes, force = false) => {
766
+ if (!notes || !notes.dirty) return false;
767
+ mergeDiskReview(notes);
768
+ if (!hasNotes(notes) && !force) {
769
+ notes.dirty = false;
770
+ return false;
771
+ }
772
+ const folder = path.dirname(notes.reviewPath);
773
+ fs.mkdirSync(folder, { recursive: true });
774
+ fs.writeFileSync(notes.reviewPath, serializeReview(notes), 'utf8');
775
+ const top = path.dirname(folder);
776
+ const templatesFile = path.join(top, REVIEW_DIR, TEMPLATES_FILE);
777
+ const body = `${JSON.stringify(notes.templates, null, 2)}\n`;
778
+ fs.writeFileSync(templatesFile, body, 'utf8');
779
+ captureBaseline(notes);
780
+ notes.dirty = false;
781
+ return true;
782
+ };
17
783
 
18
784
  module.exports = {
19
785
  TEMPLATE_SHOW,
@@ -35,9 +801,11 @@ module.exports = {
35
801
  addTodo,
36
802
  removeTodo,
37
803
  setTodoText,
804
+ setTodoDone,
38
805
  checkLabel,
39
806
  feedbackKey,
40
807
  applyImportedNotes,
808
+ setStatus,
41
809
  serializeReview,
42
810
  parseReview,
43
811
  loadReview,