pi-odd-worktree 3.9.2 → 3.11.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -40
  3. package/extensions/odd-worktree-tools.ts +1028 -17
  4. package/lib/gh-issues.js +65 -10
  5. package/lib/gh-prs.js +2011 -0
  6. package/lib/issue-preview.js +70 -4
  7. package/lib/issues-cache-store.js +4 -1
  8. package/lib/issues-section.js +346 -23
  9. package/lib/issues-view.js +116 -5
  10. package/lib/pr-preview.js +353 -0
  11. package/lib/prs-rail-store.js +317 -0
  12. package/lib/prs-view.js +2896 -0
  13. package/lib/user-prompt-card.js +477 -0
  14. package/package.json +1 -1
  15. package/tools/files-bridge.test.js +2 -2
  16. package/tools/files-command.test.js +1 -1
  17. package/tools/fixtures/gh-pr-connection-page.json +84 -0
  18. package/tools/fixtures/gh-pr-metadata.json +29 -0
  19. package/tools/fixtures/gh-pr-rail.json +61 -0
  20. package/tools/fixtures/gh-pr-search-page.json +82 -0
  21. package/tools/fixtures/gh-pr-view.json +159 -0
  22. package/tools/gh-issues.test.js +4 -2
  23. package/tools/gh-prs.test.js +1828 -0
  24. package/tools/issue-preview.test.js +119 -0
  25. package/tools/issues-cache-store.test.js +2 -2
  26. package/tools/issues-command.test.js +10 -3
  27. package/tools/issues-open-guard.test.js +15 -0
  28. package/tools/issues-section.test.js +618 -9
  29. package/tools/issues-view.test.js +297 -0
  30. package/tools/package-surface.test.js +21 -5
  31. package/tools/pr-preview.test.js +281 -0
  32. package/tools/prs-command.test.js +1280 -0
  33. package/tools/prs-rail-store.test.js +343 -0
  34. package/tools/prs-t10-safety.test.js +326 -0
  35. package/tools/prs-t12-safety.test.js +348 -0
  36. package/tools/prs-t13-independent.test.js +378 -0
  37. package/tools/prs-t14-safety.test.js +340 -0
  38. package/tools/prs-t15-independent.test.js +259 -0
  39. package/tools/prs-t18-safety.test.js +217 -0
  40. package/tools/prs-t7-safety.test.js +127 -0
  41. package/tools/prs-view.test.js +1462 -0
  42. package/tools/user-prompt-card.test.js +963 -0
@@ -0,0 +1,317 @@
1
+ 'use strict';
2
+ // T4 (S4, S5): the DISK half of the rail's pull-request read.
3
+ //
4
+ // `lib/issues-section.js` stays PURE — no imports, no child process, no disk —
5
+ // and owns what a read MEANS: the two buckets, the `pr` count they are painted
6
+ // as and the bucket a click is routed by. This module owns only where one entry
7
+ // sleeps between processes, exactly like `lib/issues-cache-store.js` owns the
8
+ // issues cache's: ONE small versioned JSON document under
9
+ // `$XDG_CACHE_HOME/odd-worktree/` (falling back to `~/.cache/odd-worktree/`), one
10
+ // entry per repository identity, keyed exactly as the rail keys its own reads
11
+ // (`<host>/<owner>/<repo>`), so a hydrated entry can never be painted under
12
+ // another repository's name. A cold start paints `N issues · M pr` at once
13
+ // instead of waiting for the child that answers the read.
14
+ //
15
+ // An entry holds the two bucket rows the rail reads — `mine` and `review`, each
16
+ // row its `number` and the `linkedIssues` it carries — the bucket's own TOTAL
17
+ // (the exact `issueCount` the read reported, which its window of rows may
18
+ // under-represent), and `syncedAt`, the moment the read that produced them
19
+ // landed. It deliberately stores NO count: the header's `M` is the distinct
20
+ // numbers in mine ∪ review PLUS the remainder of each bucket's total, so it is
21
+ // DERIVED here from what the document holds. A stored number can never
22
+ // contradict the rows it is painted from, and persistence can never turn a stale
23
+ // entry into a fresh one — the loader is the only thing that decides whether
24
+ // `syncedAt` is still inside the rail's window.
25
+ //
26
+ // T10b (S4, D15): the totals are why an entry survives a restart without
27
+ // changing a number. A read that carried 50 rows with `issueCount` 52 painted
28
+ // `52 pr`; without the total the cold start painted `50 pr`. A total below the
29
+ // rows it came with, or one that is not a count at all, is IGNORED (`null`),
30
+ // exactly like the rail's own read ignores it, so the rows stay the floor.
31
+ //
32
+ // Best effort by contract: nothing here throws, and nothing here is on a session
33
+ // or paint path — the extension calls it from its async loader alone. A malformed
34
+ // document, a version this module does not know, an entry whose shape is wrong,
35
+ // an unreadable directory or a write that cannot be committed are all SILENCE
36
+ // (`null` / `false`), never an exception and never a half-written document: the
37
+ // write is a temp file in the same directory plus a rename, so a failure leaves
38
+ // the previous document exactly as it was.
39
+ //
40
+ // The `fs` seam and the directory are injectable, so a test never touches a real
41
+ // cache directory and a host whose `fs` is broken simply gets an in-process rail.
42
+
43
+ const path = require('node:path');
44
+ const os = require('node:os');
45
+
46
+ // The document carries its own version: a reader that does not recognize one
47
+ // answers nothing instead of guessing at a shape it never wrote. T10b (S4, D15)
48
+ // moved it to 2 when the bucket totals joined the rows: a version 1 document
49
+ // never carried them, so it is SILENCE (a read, as if the cache were cold), never
50
+ // an error and never a count derived from a shape this module no longer writes.
51
+ const DOC_VERSION = 2;
52
+ const DIR_NAME = 'odd-worktree';
53
+ const FILE_NAME = 'prs-rail.json';
54
+ const BUCKET_KEYS = ['mine', 'review'];
55
+ // The document is bounded exactly like the issues cache: the oldest identities
56
+ // are dropped past this cap, so a long-lived machine never grows a file nobody
57
+ // asked for.
58
+ const DEFAULT_IDENTITY_LIMIT = 8;
59
+
60
+ function promisesSeam() {
61
+ try {
62
+ const fs = require('node:fs');
63
+ return fs && fs.promises ? fs.promises : null;
64
+ } catch {
65
+ return null;
66
+ }
67
+ }
68
+
69
+ function isObject(value) {
70
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
71
+ }
72
+
73
+ // A pull request number, or `null` for anything that is not one. Only a positive
74
+ // integer identifies a pull request, exactly as the rail's own read decides it.
75
+ function pullRequestNumber(value) {
76
+ return Number.isInteger(value) && value > 0 ? value : null;
77
+ }
78
+
79
+ // The linked issues of one row, in order, without repeats: a link the rail could
80
+ // not draw is not a link here either.
81
+ function linkedIssues(value) {
82
+ if (!Array.isArray(value)) return [];
83
+ const kept = [];
84
+ for (const linked of value) {
85
+ const number = pullRequestNumber(linked);
86
+ if (number === null || kept.includes(number)) continue;
87
+ kept.push(number);
88
+ }
89
+ return kept;
90
+ }
91
+
92
+ // One bucket, or `null` when it is not a list at all. A row without a usable
93
+ // number cannot be counted or routed to, so it is not a row here either — but one
94
+ // bad row never costs the bucket the rows beside it.
95
+ function normalizeRows(rows) {
96
+ if (!Array.isArray(rows)) return null;
97
+ const kept = [];
98
+ for (const row of rows) {
99
+ if (!isObject(row)) continue;
100
+ const number = pullRequestNumber(row.number);
101
+ if (number === null) continue;
102
+ kept.push({ ...row, number, linkedIssues: linkedIssues(row.linkedIssues) });
103
+ }
104
+ return kept;
105
+ }
106
+
107
+ // One bucket's TOTAL, or `null` when it proves nothing. The header's remainder is
108
+ // the numbers a total names that the rows never carried, so a total below the
109
+ // rows it came with cannot be one of them: it is ignored rather than trusted,
110
+ // exactly like the rail's own count ignores it.
111
+ function bucketTotal(value, rows) {
112
+ if (!Number.isInteger(value) || value < 0) return null;
113
+ return value >= rows.length ? value : null;
114
+ }
115
+
116
+ function normalizeTotals(candidate, buckets) {
117
+ const claimed = isObject(candidate) ? candidate : {};
118
+ const totals = {};
119
+ for (const key of BUCKET_KEYS) totals[key] = bucketTotal(claimed[key], buckets[key]);
120
+ return totals;
121
+ }
122
+
123
+ // The count the header paints, DERIVED from what the document holds: the distinct
124
+ // numbers the rows carried plus, per bucket, the rows each total says its window
125
+ // cut. Two buckets that both carry a number still count it once.
126
+ function derivedCount(buckets, totals) {
127
+ const numbers = new Set();
128
+ for (const key of BUCKET_KEYS) {
129
+ for (const row of buckets[key]) numbers.add(row.number);
130
+ }
131
+ let count = numbers.size;
132
+ for (const key of BUCKET_KEYS) {
133
+ const total = totals[key];
134
+ if (total !== null && total > buckets[key].length) count += total - buckets[key].length;
135
+ }
136
+ return count;
137
+ }
138
+
139
+ // One stored entry, or `null` when its shape is not the one this module writes.
140
+ // A hostile entry is never repaired into something a caller would then trust.
141
+ function normalizeEntry(candidate) {
142
+ if (!isObject(candidate)) return null;
143
+ const identity = candidate.identity;
144
+ if (typeof identity !== 'string' || identity.length === 0) return null;
145
+ const buckets = {};
146
+ for (const key of BUCKET_KEYS) {
147
+ const rows = normalizeRows(candidate[key]);
148
+ if (rows === null) return null;
149
+ buckets[key] = rows;
150
+ }
151
+ if (!Number.isFinite(candidate.syncedAt)) return null;
152
+ // The totals travel beside the rows and the count is derived from both;
153
+ // whatever a document says about the count itself is never read.
154
+ const counts = normalizeTotals(candidate.counts, buckets);
155
+ return {
156
+ identity,
157
+ mine: buckets.mine,
158
+ review: buckets.review,
159
+ counts,
160
+ count: derivedCount(buckets, counts),
161
+ syncedAt: candidate.syncedAt,
162
+ };
163
+ }
164
+
165
+ function copyEntry(entry) {
166
+ const normalized = normalizeEntry(entry);
167
+ if (normalized === null) return null;
168
+ const mine = normalized.mine.map((row) => ({ ...row, linkedIssues: row.linkedIssues.slice() }));
169
+ const review = normalized.review.map((row) => ({ ...row, linkedIssues: row.linkedIssues.slice() }));
170
+ return { ...normalized, mine, review, counts: { ...normalized.counts } };
171
+ }
172
+
173
+ function createPrsRailStore(options = {}) {
174
+ const opts = options && typeof options === 'object' ? options : {};
175
+ // `fs` is the promise-returning seam this module writes and reads through; the
176
+ // default is the real filesystem, and a test passes fakes.
177
+ const fsSeam = isObject(opts.fs) ? opts.fs : promisesSeam();
178
+ const env = isObject(opts.env) ? opts.env : process.env;
179
+ const limit = Number.isInteger(opts.limit) && opts.limit > 0 ? opts.limit : DEFAULT_IDENTITY_LIMIT;
180
+ let writes = 0;
181
+
182
+ // The directory the document lives in, resolved at CALL time so a host (and a
183
+ // test) can move it by environment alone. `dir` names the directory itself;
184
+ // otherwise `$XDG_CACHE_HOME/odd-worktree/`, else `~/.cache/odd-worktree/`.
185
+ // No home and no XDG means no directory at all — silence, not a guess.
186
+ function directory() {
187
+ try {
188
+ if (typeof opts.dir === 'string' && opts.dir.length > 0) return path.resolve(opts.dir);
189
+ const xdg = env ? env.XDG_CACHE_HOME : null;
190
+ if (typeof xdg === 'string' && xdg.trim() !== '') return path.join(path.resolve(xdg), DIR_NAME);
191
+ if (opts && 'home' in opts) {
192
+ const home = opts.home;
193
+ if (typeof home !== 'string' || home.length === 0) return null;
194
+ return path.join(path.resolve(home), '.cache', DIR_NAME);
195
+ }
196
+ const home = os.homedir();
197
+ if (typeof home !== 'string' || home.length === 0) return null;
198
+ return path.join(home, '.cache', DIR_NAME);
199
+ } catch {
200
+ return null;
201
+ }
202
+ }
203
+
204
+ function file() {
205
+ const dir = directory();
206
+ return dir === null ? null : path.join(dir, FILE_NAME);
207
+ }
208
+
209
+ // The document as a list of entries, oldest first. Anything this module cannot
210
+ // read is `null`; any entry whose shape is wrong is dropped on its own, so one
211
+ // hostile entry never costs the identity written beside it.
212
+ async function readIdentities() {
213
+ const target = file();
214
+ if (target === null || fsSeam === null) return null;
215
+ let text;
216
+ try {
217
+ text = await fsSeam.readFile(target, 'utf8');
218
+ } catch {
219
+ return null;
220
+ }
221
+ let doc;
222
+ try {
223
+ doc = JSON.parse(text);
224
+ } catch {
225
+ return null;
226
+ }
227
+ if (!isObject(doc) || doc.version !== DOC_VERSION || !Array.isArray(doc.identities)) return null;
228
+ const identities = [];
229
+ for (const candidate of doc.identities) {
230
+ const entry = normalizeEntry(candidate);
231
+ if (entry !== null) identities.push(entry);
232
+ }
233
+ return identities;
234
+ }
235
+
236
+ async function load(identity) {
237
+ if (typeof identity !== 'string' || identity.length === 0) return null;
238
+ try {
239
+ const identities = await readIdentities();
240
+ if (identities === null) return null;
241
+ for (let index = identities.length - 1; index >= 0; index -= 1) {
242
+ if (identities[index].identity === identity) return copyEntry(identities[index]);
243
+ }
244
+ return null;
245
+ } catch {
246
+ return null;
247
+ }
248
+ }
249
+
250
+ // One rail read, stored as one entry: the two buckets, each bucket's total and
251
+ // when they were read. The identity is the one the loader just proved, so an
252
+ // entry is never written under a name the read did not come from.
253
+ function normalizeSnapshot(identity, snapshot) {
254
+ if (typeof identity !== 'string' || identity.length === 0) return null;
255
+ if (!isObject(snapshot)) return null;
256
+ return normalizeEntry({
257
+ identity,
258
+ mine: snapshot.mine,
259
+ review: snapshot.review,
260
+ counts: snapshot.counts,
261
+ syncedAt: Number.isFinite(snapshot.syncedAt) ? snapshot.syncedAt : Date.now(),
262
+ });
263
+ }
264
+
265
+ async function save(identity, snapshot) {
266
+ const dir = directory();
267
+ const target = file();
268
+ if (dir === null || target === null || fsSeam === null) return false;
269
+ const entry = normalizeSnapshot(identity, snapshot);
270
+ if (entry === null) return false;
271
+ try {
272
+ const existing = await readIdentities();
273
+ const identities = existing === null ? [] : existing;
274
+ const kept = identities.filter((candidate) => candidate.identity !== identity);
275
+ kept.push(entry);
276
+ // The document holds rows and totals only: the count is DERIVED on load, so
277
+ // no stored number can ever be painted or contradict what is. The whole
278
+ // entry is written through one projection, so a later field cannot slip in
279
+ // by being normalized here.
280
+ const written = kept.slice(-limit).map((stored) => ({
281
+ identity: stored.identity,
282
+ mine: stored.mine,
283
+ review: stored.review,
284
+ counts: stored.counts,
285
+ syncedAt: stored.syncedAt,
286
+ }));
287
+ const body = JSON.stringify({ version: DOC_VERSION, identities: written });
288
+ await fsSeam.mkdir(dir, { recursive: true });
289
+ writes += 1;
290
+ // Atomic: the temp file lives in the SAME directory (a rename across
291
+ // devices is not atomic, and a reader must never see half a document), and
292
+ // the rename is what makes the new one readable.
293
+ const temp = path.join(dir, `.${FILE_NAME}.${process.pid}.${writes}.tmp`);
294
+ try {
295
+ await fsSeam.writeFile(temp, body, 'utf8');
296
+ await fsSeam.rename(temp, target);
297
+ } catch {
298
+ // Its own just-created temp file: removed so a failed write leaves the
299
+ // directory exactly as it was. Best effort — the failure is already
300
+ // silence to the caller.
301
+ try {
302
+ if (typeof fsSeam.unlink === 'function') await fsSeam.unlink(temp);
303
+ } catch {
304
+ // A temp file that cannot be removed is still not the document.
305
+ }
306
+ return false;
307
+ }
308
+ return true;
309
+ } catch {
310
+ return false;
311
+ }
312
+ }
313
+
314
+ return { load, save, directory };
315
+ }
316
+
317
+ module.exports = { createPrsRailStore };