pi-odd-worktree 2.1.0 → 3.0.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.
- package/README.md +54 -319
- package/extensions/odd-worktree-tools.ts +4182 -2539
- package/lib/bash-activity.js +1166 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1421 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +323 -15
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -712
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1420 -0
- package/tools/bash-design.test.js +1093 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +613 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1889 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +417 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1517 -67
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4235 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2565 -309
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +419 -199
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T9 (S10): does one `bash` command string mutate a GitHub issue or pull
|
|
3
|
+
// request?
|
|
4
|
+
//
|
|
5
|
+
// The rail re-reads on its own floor (S10/T8, 20 s), and the user's own words
|
|
6
|
+
// were "y que de tool solo revise tools bash, si uso algun comando git bash de
|
|
7
|
+
// creacion/update de alguna issue o pr, luego de terminar la tool ahi si podria
|
|
8
|
+
// forzar un refrezco porque sabemos que algo cambios". This module is that
|
|
9
|
+
// question and nothing else: a `bash` command in, a boolean out.
|
|
10
|
+
//
|
|
11
|
+
// TEXT ONLY, exactly like lib/worktree-guard.js: the command is split into
|
|
12
|
+
// segments on `&&`, `||`, `|`, `&`, `;` and newlines, each segment is tokenized
|
|
13
|
+
// with one simple quote pair per token (nothing else about the text is
|
|
14
|
+
// interpreted), and a segment counts only when its COMMAND position names the
|
|
15
|
+
// `gh` binary — an absolute path to it is the same binary, and a leading `env`
|
|
16
|
+
// or `VAR=value` prefix is skipped. A mutation MENTIONED in an argument or a
|
|
17
|
+
// commit message names no command, so it forces no read.
|
|
18
|
+
//
|
|
19
|
+
// The verbs are the issue and pull-request writes `gh` ships: for `gh issue`
|
|
20
|
+
// create/edit/close/reopen/comment/delete/transfer/develop/lock/unlock; for
|
|
21
|
+
// `gh pr` the same set plus merge/review/ready. `gh api` is a write when its
|
|
22
|
+
// method is POST/PATCH/PUT/DELETE (`-X POST`, `-XPOST`, `--method PATCH`,
|
|
23
|
+
// `--method=PUT`, …) or when it carries the field flags that make `gh` POST
|
|
24
|
+
// (`-f`, `--field`, `-F`, `--raw-field`, `--input`). Everything else — `gh issue
|
|
25
|
+
// list`, `gh pr view`, a bare `gh api` GET, `gh repo view`, plain `git` — is not
|
|
26
|
+
// a write.
|
|
27
|
+
//
|
|
28
|
+
// Nothing here spawns a child, reads a file, touches the clock or caches
|
|
29
|
+
// anything: it is one decision over a string that runs on the hot path of a
|
|
30
|
+
// `bash` result, and it never throws, because the caller's refresh is best
|
|
31
|
+
// effort and a throw there would ride into a host handler.
|
|
32
|
+
|
|
33
|
+
/** The `gh` subcommands whose write is a mutation of an issue. */
|
|
34
|
+
const ISSUE_MUTATIONS = new Set([
|
|
35
|
+
'create',
|
|
36
|
+
'edit',
|
|
37
|
+
'close',
|
|
38
|
+
'reopen',
|
|
39
|
+
'comment',
|
|
40
|
+
'delete',
|
|
41
|
+
'transfer',
|
|
42
|
+
'develop',
|
|
43
|
+
'lock',
|
|
44
|
+
'unlock',
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/** The `gh` subcommands whose write is a mutation of a pull request. */
|
|
48
|
+
const PR_MUTATIONS = new Set([
|
|
49
|
+
'create',
|
|
50
|
+
'edit',
|
|
51
|
+
'close',
|
|
52
|
+
'reopen',
|
|
53
|
+
'merge',
|
|
54
|
+
'comment',
|
|
55
|
+
'review',
|
|
56
|
+
'ready',
|
|
57
|
+
'lock',
|
|
58
|
+
'unlock',
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
/** `gh api` methods that write. */
|
|
62
|
+
const WRITE_METHODS = new Set(['POST', 'PATCH', 'PUT', 'DELETE']);
|
|
63
|
+
|
|
64
|
+
/** `gh api` flags that make an unstated method POST. */
|
|
65
|
+
const FIELD_FLAGS = new Set(['-f', '--field', '-F', '--raw-field', '--input']);
|
|
66
|
+
|
|
67
|
+
/** The prefixes a shell uses in front of the command that really runs. */
|
|
68
|
+
const COMMAND_PREFIXES = new Set(['env', 'command', 'nohup', 'sudo', 'time']);
|
|
69
|
+
|
|
70
|
+
/** A `VAR=value` assignment in the command position. */
|
|
71
|
+
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Split one command string into segments of tokens. Separators end a segment,
|
|
75
|
+
* one simple quote pair per token is stripped, and no escape is interpreted —
|
|
76
|
+
* the same reading lib/worktree-guard.js does, without its substitution walk:
|
|
77
|
+
* what a segment's command position names is the whole question here.
|
|
78
|
+
*/
|
|
79
|
+
function scanSegments(command) {
|
|
80
|
+
const segments = [];
|
|
81
|
+
let tokens = [];
|
|
82
|
+
let token = null;
|
|
83
|
+
const flushToken = () => {
|
|
84
|
+
if (token !== null) {
|
|
85
|
+
tokens.push(token);
|
|
86
|
+
token = null;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const flushSegment = () => {
|
|
90
|
+
flushToken();
|
|
91
|
+
if (tokens.length > 0) segments.push(tokens);
|
|
92
|
+
tokens = [];
|
|
93
|
+
};
|
|
94
|
+
let i = 0;
|
|
95
|
+
while (i < command.length) {
|
|
96
|
+
const ch = command[i];
|
|
97
|
+
if (ch === "'" || ch === '"') {
|
|
98
|
+
const end = command.indexOf(ch, i + 1);
|
|
99
|
+
const inner = end < 0 ? command.slice(i + 1) : command.slice(i + 1, end);
|
|
100
|
+
token = (token === null ? '' : token) + inner;
|
|
101
|
+
i = end < 0 ? command.length : end + 1;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (ch === ' ' || ch === '\t') {
|
|
105
|
+
flushToken();
|
|
106
|
+
i += 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (ch === '\n' || ch === '\r' || ch === '&' || ch === '|' || ch === ';' || ch === '(' || ch === ')') {
|
|
110
|
+
// A subshell `( … )` starts a segment of its own, exactly like a separator.
|
|
111
|
+
flushSegment();
|
|
112
|
+
i += 1;
|
|
113
|
+
// `&&`, `||`, `|&`, `;;`: one separator, however it is spelled.
|
|
114
|
+
while (i < command.length && (command[i] === '&' || command[i] === '|' || command[i] === ';')) i += 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
token = (token === null ? '' : token) + ch;
|
|
118
|
+
i += 1;
|
|
119
|
+
}
|
|
120
|
+
flushSegment();
|
|
121
|
+
return segments;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The index of the token that runs the command, past `env`/`VAR=value` prefixes. */
|
|
125
|
+
function commandIndex(tokens) {
|
|
126
|
+
let i = 0;
|
|
127
|
+
while (i < tokens.length) {
|
|
128
|
+
const token = tokens[i];
|
|
129
|
+
if (ASSIGNMENT.test(token)) {
|
|
130
|
+
i += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (COMMAND_PREFIXES.has(token)) {
|
|
134
|
+
i += 1;
|
|
135
|
+
// `env -u VAR gh …` / `sudo -E gh …`: the prefix's own options are skipped.
|
|
136
|
+
while (i < tokens.length && tokens[i].startsWith('-')) i += 1;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
return i;
|
|
140
|
+
}
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Whether a command token names the `gh` binary, bare or by absolute path. */
|
|
145
|
+
function isGhToken(token) {
|
|
146
|
+
return token === 'gh' || token.endsWith('/gh');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The method `gh api` states on its command line, or null when it states none. */
|
|
150
|
+
function statedMethod(tokens, apiIndex) {
|
|
151
|
+
for (let i = apiIndex + 1; i < tokens.length; i += 1) {
|
|
152
|
+
const token = tokens[i];
|
|
153
|
+
if (token === '-X' || token === '--method') {
|
|
154
|
+
const value = tokens[i + 1];
|
|
155
|
+
if (typeof value === 'string' && value.length > 0) return value.toUpperCase();
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (token.startsWith('--method=')) return token.slice('--method='.length).toUpperCase();
|
|
159
|
+
if (token.startsWith('-X') && token.length > 2) return token.slice(2).toUpperCase();
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Whether a `gh api` segment carries a field flag, which makes the call a POST. */
|
|
165
|
+
function carriesField(tokens, apiIndex) {
|
|
166
|
+
for (let i = apiIndex + 1; i < tokens.length; i += 1) {
|
|
167
|
+
const token = tokens[i];
|
|
168
|
+
if (FIELD_FLAGS.has(token)) return true;
|
|
169
|
+
// The `--field=key=value` spelling carries the flag and its value at once.
|
|
170
|
+
if (token.startsWith('--field=') || token.startsWith('--raw-field=') || token.startsWith('--input=')) return true;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Whether one segment's `gh` command mutates an issue or a pull request. */
|
|
176
|
+
function segmentMutates(tokens) {
|
|
177
|
+
const at = commandIndex(tokens);
|
|
178
|
+
if (at < 0 || !isGhToken(tokens[at])) return false;
|
|
179
|
+
const group = tokens[at + 1];
|
|
180
|
+
const verb = tokens[at + 2];
|
|
181
|
+
if (group === 'issue') return typeof verb === 'string' && ISSUE_MUTATIONS.has(verb);
|
|
182
|
+
if (group === 'pr') return typeof verb === 'string' && PR_MUTATIONS.has(verb);
|
|
183
|
+
if (group !== 'api') return false;
|
|
184
|
+
const method = statedMethod(tokens, at);
|
|
185
|
+
if (method !== null) return WRITE_METHODS.has(method);
|
|
186
|
+
return carriesField(tokens, at);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Whether one `bash` command mutates a GitHub issue or pull request. Answers
|
|
191
|
+
* `false` for anything that is not a command string, and never throws.
|
|
192
|
+
*/
|
|
193
|
+
function bashMutatesIssuesOrPullRequests(command) {
|
|
194
|
+
try {
|
|
195
|
+
if (typeof command !== 'string' || command.length === 0) return false;
|
|
196
|
+
for (const tokens of scanSegments(command)) {
|
|
197
|
+
if (segmentMutates(tokens)) return true;
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
} catch {
|
|
201
|
+
// A command this scanner cannot read names nothing, so it forces no read.
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = { bashMutatesIssuesOrPullRequests };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S4 (issues-startup-performance T3): the DISK half of the issues read cache.
|
|
3
|
+
//
|
|
4
|
+
// `lib/related-issues-cache.js` stays PURE — no imports, no child process, no
|
|
5
|
+
// disk — and owns what a read MEANS (the plan, the merge, the count invariant).
|
|
6
|
+
// This module owns only where an entry sleeps between processes: ONE small
|
|
7
|
+
// versioned JSON document under `$XDG_CACHE_HOME/odd-worktree/` (falling back to
|
|
8
|
+
// `~/.cache/odd-worktree/`), one entry per repository identity, keyed exactly as
|
|
9
|
+
// the in-memory cache keys its own entries (`<host>/<owner>/<repo>`), so a
|
|
10
|
+
// hydrated entry can never be served under another repository's name.
|
|
11
|
+
//
|
|
12
|
+
// An entry holds what `plan`/`get` need: the three bucket rows, `counts`,
|
|
13
|
+
// `truncated`, `inconsistent`, `fullAt`, `syncedAt` and `lastApplied`. `fullAt`
|
|
14
|
+
// moves only on a FULL apply, exactly like the in-memory cache's own clock, so a
|
|
15
|
+
// steady stream of deltas can never postpone the full re-read across a restart.
|
|
16
|
+
//
|
|
17
|
+
// Best effort by contract: nothing here throws, and nothing here is on a session
|
|
18
|
+
// or paint path — the extension calls it from its async loader alone. A malformed
|
|
19
|
+
// document, a version this module does not know, an entry whose shape is wrong,
|
|
20
|
+
// an unreadable directory or a write that cannot be committed are all SILENCE
|
|
21
|
+
// (`null` / `false`), never an exception and never a half-written document: the
|
|
22
|
+
// write is a temp file in the same directory plus a rename, so a failure leaves
|
|
23
|
+
// the previous document exactly as it was.
|
|
24
|
+
//
|
|
25
|
+
// The `fs` seam and the directory are injectable, so a test never touches a real
|
|
26
|
+
// cache directory and a host whose `fs` is broken simply gets an in-process cache.
|
|
27
|
+
|
|
28
|
+
const path = require('node:path');
|
|
29
|
+
const os = require('node:os');
|
|
30
|
+
|
|
31
|
+
// The document carries its own version: a reader that does not recognize one
|
|
32
|
+
// answers nothing instead of guessing at a shape it never wrote.
|
|
33
|
+
const DOC_VERSION = 1;
|
|
34
|
+
const DIR_NAME = 'odd-worktree';
|
|
35
|
+
const FILE_NAME = 'issues-cache.json';
|
|
36
|
+
const BUCKET_KEYS = ['mine', 'assigned', 'involved'];
|
|
37
|
+
// The document is bounded: the oldest identities are dropped past this cap, so a
|
|
38
|
+
// long-lived machine never grows a cache file nobody asked for.
|
|
39
|
+
const DEFAULT_IDENTITY_LIMIT = 8;
|
|
40
|
+
|
|
41
|
+
function promisesSeam() {
|
|
42
|
+
try {
|
|
43
|
+
const fs = require('node:fs');
|
|
44
|
+
return fs && fs.promises ? fs.promises : null;
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isObject(value) {
|
|
51
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The rows of one bucket, dropped when they are not a row at all. A row without a
|
|
55
|
+
// usable number cannot be merged or counted, exactly like the in-memory cache, so
|
|
56
|
+
// it is not a row here either.
|
|
57
|
+
function normalizeRows(rows) {
|
|
58
|
+
if (!Array.isArray(rows)) return null;
|
|
59
|
+
const kept = [];
|
|
60
|
+
for (const row of rows) {
|
|
61
|
+
if (!isObject(row)) continue;
|
|
62
|
+
if (!Number.isInteger(row.number) || row.number <= 0) continue;
|
|
63
|
+
kept.push({ ...row });
|
|
64
|
+
}
|
|
65
|
+
return kept;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function normalizeFlags(value) {
|
|
69
|
+
if (!isObject(value)) return null;
|
|
70
|
+
const flags = {};
|
|
71
|
+
for (const key of BUCKET_KEYS) flags[key] = value[key] === true;
|
|
72
|
+
return flags;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// One stored entry, or `null` when its shape is not the one this module writes.
|
|
76
|
+
// A hostile entry is never repaired into something a caller would then trust.
|
|
77
|
+
function normalizeEntry(candidate) {
|
|
78
|
+
if (!isObject(candidate)) return null;
|
|
79
|
+
const identity = candidate.identity;
|
|
80
|
+
if (typeof identity !== 'string' || identity.length === 0) return null;
|
|
81
|
+
if (!isObject(candidate.buckets) || !isObject(candidate.counts)) return null;
|
|
82
|
+
const buckets = {};
|
|
83
|
+
const counts = {};
|
|
84
|
+
for (const key of BUCKET_KEYS) {
|
|
85
|
+
const rows = normalizeRows(candidate.buckets[key]);
|
|
86
|
+
if (rows === null) return null;
|
|
87
|
+
const count = candidate.counts[key];
|
|
88
|
+
if (!Number.isInteger(count) || count < 0) return null;
|
|
89
|
+
buckets[key] = rows;
|
|
90
|
+
counts[key] = count;
|
|
91
|
+
}
|
|
92
|
+
const truncated = normalizeFlags(candidate.truncated);
|
|
93
|
+
if (truncated === null) return null;
|
|
94
|
+
if (!Number.isFinite(candidate.fullAt) || !Number.isFinite(candidate.syncedAt)) return null;
|
|
95
|
+
return {
|
|
96
|
+
identity,
|
|
97
|
+
buckets,
|
|
98
|
+
counts,
|
|
99
|
+
truncated,
|
|
100
|
+
inconsistent: candidate.inconsistent === true,
|
|
101
|
+
fullAt: candidate.fullAt,
|
|
102
|
+
syncedAt: candidate.syncedAt,
|
|
103
|
+
lastApplied: Number.isFinite(candidate.lastApplied) ? candidate.lastApplied : null,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function copyEntry(entry) {
|
|
108
|
+
const normalized = normalizeEntry(entry);
|
|
109
|
+
if (normalized === null) return null;
|
|
110
|
+
const buckets = {};
|
|
111
|
+
for (const key of BUCKET_KEYS) buckets[key] = normalized.buckets[key].map((row) => ({ ...row }));
|
|
112
|
+
return { ...normalized, buckets, counts: { ...normalized.counts }, truncated: { ...normalized.truncated } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function createIssuesCacheStore(options = {}) {
|
|
116
|
+
const opts = options && typeof options === 'object' ? options : {};
|
|
117
|
+
// `fs` is the promise-returning seam this module writes and reads through; the
|
|
118
|
+
// default is the real filesystem, and a test passes fakes.
|
|
119
|
+
const fsSeam = isObject(opts.fs) ? opts.fs : promisesSeam();
|
|
120
|
+
const env = isObject(opts.env) ? opts.env : process.env;
|
|
121
|
+
const limit = Number.isInteger(opts.limit) && opts.limit > 0 ? opts.limit : DEFAULT_IDENTITY_LIMIT;
|
|
122
|
+
let writes = 0;
|
|
123
|
+
|
|
124
|
+
// The directory the document lives in, resolved at CALL time so a host (and a
|
|
125
|
+
// test) can move it by environment alone. `dir` names the directory itself;
|
|
126
|
+
// otherwise `$XDG_CACHE_HOME/odd-worktree/`, else `~/.cache/odd-worktree/`.
|
|
127
|
+
// No home and no XDG means no directory at all — silence, not a guess.
|
|
128
|
+
function directory() {
|
|
129
|
+
try {
|
|
130
|
+
if (typeof opts.dir === 'string' && opts.dir.length > 0) return path.resolve(opts.dir);
|
|
131
|
+
const xdg = env ? env.XDG_CACHE_HOME : null;
|
|
132
|
+
if (typeof xdg === 'string' && xdg.trim() !== '') return path.join(path.resolve(xdg), DIR_NAME);
|
|
133
|
+
if (opts && 'home' in opts) {
|
|
134
|
+
const home = opts.home;
|
|
135
|
+
if (typeof home !== 'string' || home.length === 0) return null;
|
|
136
|
+
return path.join(path.resolve(home), '.cache', DIR_NAME);
|
|
137
|
+
}
|
|
138
|
+
const home = os.homedir();
|
|
139
|
+
if (typeof home !== 'string' || home.length === 0) return null;
|
|
140
|
+
return path.join(home, '.cache', DIR_NAME);
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function file() {
|
|
147
|
+
const dir = directory();
|
|
148
|
+
return dir === null ? null : path.join(dir, FILE_NAME);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The document as a list of entries, oldest first. Anything this module cannot
|
|
152
|
+
// read is `null`; any entry whose shape is wrong is dropped on its own, so one
|
|
153
|
+
// hostile entry never costs the identity written beside it.
|
|
154
|
+
async function readIdentities() {
|
|
155
|
+
const target = file();
|
|
156
|
+
if (target === null || fsSeam === null) return null;
|
|
157
|
+
let text;
|
|
158
|
+
try {
|
|
159
|
+
text = await fsSeam.readFile(target, 'utf8');
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
let doc;
|
|
164
|
+
try {
|
|
165
|
+
doc = JSON.parse(text);
|
|
166
|
+
} catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
if (!isObject(doc) || doc.version !== DOC_VERSION || !Array.isArray(doc.identities)) return null;
|
|
170
|
+
const identities = [];
|
|
171
|
+
for (const candidate of doc.identities) {
|
|
172
|
+
const entry = normalizeEntry(candidate);
|
|
173
|
+
if (entry !== null) identities.push(entry);
|
|
174
|
+
}
|
|
175
|
+
return identities;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function load(identity) {
|
|
179
|
+
if (typeof identity !== 'string' || identity.length === 0) return null;
|
|
180
|
+
try {
|
|
181
|
+
const identities = await readIdentities();
|
|
182
|
+
if (identities === null) return null;
|
|
183
|
+
for (let index = identities.length - 1; index >= 0; index -= 1) {
|
|
184
|
+
if (identities[index].identity === identity) return copyEntry(identities[index]);
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// One bucket set + the facts the loader knows, stored as one entry. `full` is
|
|
193
|
+
// what the caller's plan decided: it is the ONLY thing that moves `fullAt`.
|
|
194
|
+
function normalizeSnapshot(identity, snapshot) {
|
|
195
|
+
if (typeof identity !== 'string' || identity.length === 0) return null;
|
|
196
|
+
if (!isObject(snapshot)) return null;
|
|
197
|
+
const at = Number.isFinite(snapshot.syncedAt) ? snapshot.syncedAt : Date.now();
|
|
198
|
+
return normalizeEntry({
|
|
199
|
+
identity,
|
|
200
|
+
buckets: snapshot.buckets,
|
|
201
|
+
counts: snapshot.counts,
|
|
202
|
+
truncated: snapshot.truncated,
|
|
203
|
+
inconsistent: snapshot.inconsistent,
|
|
204
|
+
// Stamped here, then left alone below unless this answer WAS the full read.
|
|
205
|
+
fullAt: at,
|
|
206
|
+
syncedAt: at,
|
|
207
|
+
lastApplied: snapshot.lastApplied,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function save(identity, snapshot) {
|
|
212
|
+
const dir = directory();
|
|
213
|
+
const target = file();
|
|
214
|
+
if (dir === null || target === null || fsSeam === null) return false;
|
|
215
|
+
const entry = normalizeSnapshot(identity, snapshot);
|
|
216
|
+
if (entry === null) return false;
|
|
217
|
+
try {
|
|
218
|
+
const existing = await readIdentities();
|
|
219
|
+
const identities = existing === null ? [] : existing;
|
|
220
|
+
const previous = identities.find((candidate) => candidate.identity === identity) || null;
|
|
221
|
+
const stored = {
|
|
222
|
+
...entry,
|
|
223
|
+
// A delta never invents a full read: it keeps the one already stored, or
|
|
224
|
+
// stamps this answer when it is all there is.
|
|
225
|
+
fullAt: snapshot.full === true
|
|
226
|
+
? entry.syncedAt
|
|
227
|
+
: (previous !== null && Number.isFinite(previous.fullAt) ? previous.fullAt : entry.syncedAt),
|
|
228
|
+
};
|
|
229
|
+
const kept = identities.filter((candidate) => candidate.identity !== identity);
|
|
230
|
+
kept.push(stored);
|
|
231
|
+
const body = JSON.stringify({ version: DOC_VERSION, identities: kept.slice(-limit) });
|
|
232
|
+
await fsSeam.mkdir(dir, { recursive: true });
|
|
233
|
+
writes += 1;
|
|
234
|
+
// Atomic: the temp file lives in the SAME directory (a rename across
|
|
235
|
+
// devices is not atomic, and a reader must never see half a document), and
|
|
236
|
+
// the rename is what makes the new one readable.
|
|
237
|
+
const temp = path.join(dir, `.${FILE_NAME}.${process.pid}.${writes}.tmp`);
|
|
238
|
+
try {
|
|
239
|
+
await fsSeam.writeFile(temp, body, 'utf8');
|
|
240
|
+
await fsSeam.rename(temp, target);
|
|
241
|
+
} catch {
|
|
242
|
+
// Its own just-created temp file: removed so a failed write leaves the
|
|
243
|
+
// directory exactly as it was. Best effort — the failure is already
|
|
244
|
+
// silence to the caller.
|
|
245
|
+
try {
|
|
246
|
+
if (typeof fsSeam.unlink === 'function') await fsSeam.unlink(temp);
|
|
247
|
+
} catch {
|
|
248
|
+
// A temp file that cannot be removed is still not the document.
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
} catch {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return { load, save, directory };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
module.exports = { createIssuesCacheStore };
|