@ultimat3/manifest 29.4.0 → 30.1.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/CLAUDE.md +18 -2
- package/README.md +44 -5
- package/package.json +20 -8
- package/src/build.ts +39 -0
- package/src/diff-consumers.ts +44 -0
- package/src/diff-entities.ts +50 -0
- package/src/diff-meter.ts +132 -0
- package/src/diff-mounts.ts +72 -0
- package/src/diff-operations.ts +65 -1
- package/src/diff-rate-limit.ts +45 -3
- package/src/diff-registries.ts +50 -4
- package/src/diff-rows.ts +127 -0
- package/src/diff-subjects.ts +88 -0
- package/src/diff.ts +8 -0
- package/src/docs-scan.ts +9 -4
- package/src/docs-search-subpath.ts +7 -0
- package/src/docs-search.ts +60 -94
- package/src/docs-tokens.ts +87 -0
- package/src/docs-wiki.ts +167 -0
- package/src/emit.ts +3 -0
- package/src/graph-schema.ts +95 -0
- package/src/graph.ts +297 -0
- package/src/index.ts +26 -1
- package/src/schema-subjects.ts +34 -0
- package/src/schema.ts +102 -1
- package/src/sources.ts +99 -3
package/src/docs-search.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// order it produces is a function of its input alone, and a scan plus a search is reproducible.
|
|
3
3
|
|
|
4
4
|
import type { DocEntry } from './docs-scan';
|
|
5
|
+
import { stem, tokenize } from './docs-tokens';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* The tie-break, in CODE UNITS. Never `localeCompare`: with no locale argument it answers from the
|
|
@@ -12,77 +13,6 @@ import type { DocEntry } from './docs-scan';
|
|
|
12
13
|
*/
|
|
13
14
|
const byTopic = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
|
|
14
15
|
|
|
15
|
-
/**
|
|
16
|
-
* Words that carry no signal in a question about an API. Deliberately tiny: every word removed
|
|
17
|
-
* here is a word an agent cannot search for, and `get`, `set`, `run` and `new` are all real
|
|
18
|
-
* symbol names in this framework.
|
|
19
|
-
*/
|
|
20
|
-
const STOPWORDS: ReadonlySet<string> = new Set([
|
|
21
|
-
'a',
|
|
22
|
-
'an',
|
|
23
|
-
'and',
|
|
24
|
-
'are',
|
|
25
|
-
'as',
|
|
26
|
-
'at',
|
|
27
|
-
'be',
|
|
28
|
-
'but',
|
|
29
|
-
'by',
|
|
30
|
-
'can',
|
|
31
|
-
'do',
|
|
32
|
-
'does',
|
|
33
|
-
'for',
|
|
34
|
-
'from',
|
|
35
|
-
'how',
|
|
36
|
-
'i',
|
|
37
|
-
'if',
|
|
38
|
-
'in',
|
|
39
|
-
'is',
|
|
40
|
-
'it',
|
|
41
|
-
'its',
|
|
42
|
-
'me',
|
|
43
|
-
'my',
|
|
44
|
-
'no',
|
|
45
|
-
'not',
|
|
46
|
-
'of',
|
|
47
|
-
'on',
|
|
48
|
-
'or',
|
|
49
|
-
'the',
|
|
50
|
-
'to',
|
|
51
|
-
'use',
|
|
52
|
-
'using',
|
|
53
|
-
'was',
|
|
54
|
-
'what',
|
|
55
|
-
'when',
|
|
56
|
-
'where',
|
|
57
|
-
'which',
|
|
58
|
-
'why',
|
|
59
|
-
'with',
|
|
60
|
-
'you',
|
|
61
|
-
'your',
|
|
62
|
-
]);
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Split on non-alphanumerics, then split camelCase — so `nextRetry` answers a query for `retry`
|
|
66
|
-
* and `retry` answers one for `nextRetry`. The joined original is kept alongside its parts so an
|
|
67
|
-
* exact symbol name can still score as an exact hit.
|
|
68
|
-
*/
|
|
69
|
-
export function tokenize(query: string): readonly string[] {
|
|
70
|
-
const out: string[] = [];
|
|
71
|
-
const push = (token: string): void => {
|
|
72
|
-
if (token.length < 2 || STOPWORDS.has(token) || out.includes(token)) return;
|
|
73
|
-
out.push(token);
|
|
74
|
-
};
|
|
75
|
-
// Case is load-bearing until after the split: lowercasing first erases the boundary in
|
|
76
|
-
// `nextRetry`, which is the one this exists to find.
|
|
77
|
-
for (const word of query.split(/[^a-zA-Z0-9]+/)) {
|
|
78
|
-
if (word === '') continue;
|
|
79
|
-
const parts = word.split(/(?<=[a-z0-9])(?=[A-Z])/);
|
|
80
|
-
for (const part of parts) push(part.toLowerCase());
|
|
81
|
-
if (parts.length > 1) push(word.toLowerCase());
|
|
82
|
-
}
|
|
83
|
-
return out;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
16
|
/**
|
|
87
17
|
* A question about rationale wants the prose a human wrote, not the declaration site: "why is
|
|
88
18
|
* money never a float" is answered by the README section that argues it, while the module that
|
|
@@ -125,49 +55,78 @@ const MAX_TEXT_SCORE = 6;
|
|
|
125
55
|
/** Applied once, to a guide entry, when the question asks for rationale rather than an API. */
|
|
126
56
|
const RATIONALE_BONUS = 5;
|
|
127
57
|
|
|
58
|
+
/** Every lowercased view of one entry a token is compared against, computed once per entry. */
|
|
59
|
+
interface Prepared {
|
|
60
|
+
readonly symbolsLower: readonly string[];
|
|
61
|
+
readonly topicStems: readonly string[];
|
|
62
|
+
readonly packageStem: string;
|
|
63
|
+
readonly titleLower: string;
|
|
64
|
+
readonly textLower: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Keyed on the entry object, so the memo can never answer for a different entry and an entry
|
|
69
|
+
* nobody holds is collected. Twenty questions over the shipped tree recomputed these 56,000 times;
|
|
70
|
+
* the result is a function of the entry alone, so this file stays pure.
|
|
71
|
+
*/
|
|
72
|
+
const PREPARED = new WeakMap<DocEntry, Prepared>();
|
|
73
|
+
|
|
74
|
+
function prepare(entry: DocEntry): Prepared {
|
|
75
|
+
const known = PREPARED.get(entry);
|
|
76
|
+
if (known !== undefined) return known;
|
|
77
|
+
const prepared: Prepared = {
|
|
78
|
+
symbolsLower: entry.symbols.map((symbol) => symbol.toLowerCase()),
|
|
79
|
+
topicStems: tokenize(entry.topic).map(stem),
|
|
80
|
+
packageStem: stem(entry.package.split('/').at(-1)?.toLowerCase() ?? ''),
|
|
81
|
+
titleLower: entry.title.toLowerCase(),
|
|
82
|
+
textLower: entry.text.toLowerCase(),
|
|
83
|
+
};
|
|
84
|
+
PREPARED.set(entry, prepared);
|
|
85
|
+
return prepared;
|
|
86
|
+
}
|
|
87
|
+
|
|
128
88
|
function scoreEntry(
|
|
129
89
|
entry: DocEntry,
|
|
130
90
|
tokens: readonly string[],
|
|
131
91
|
rationale: boolean,
|
|
132
92
|
): DocHit | undefined {
|
|
133
|
-
const symbolsLower
|
|
134
|
-
const topicTokens = tokenize(entry.topic);
|
|
135
|
-
const packageToken = entry.package.split('/').at(-1)?.toLowerCase() ?? '';
|
|
136
|
-
const titleLower = entry.title.toLowerCase();
|
|
137
|
-
const textLower = entry.text.toLowerCase();
|
|
93
|
+
const { symbolsLower, topicStems, packageStem, titleLower, textLower } = prepare(entry);
|
|
138
94
|
const matched: string[] = [];
|
|
139
95
|
let score = 0;
|
|
140
96
|
let textScore = 0;
|
|
141
97
|
|
|
142
98
|
let covered = 0;
|
|
143
|
-
for (const
|
|
99
|
+
for (const word of tokens) {
|
|
144
100
|
const before = score + textScore;
|
|
101
|
+
// The stem, not the word, against every name and every line of prose: `upgrade` has to reach
|
|
102
|
+
// `Upgrading`, and `jobs` the `job` module. A symbol stays exact-or-substring on the word.
|
|
103
|
+
const root = stem(word);
|
|
145
104
|
// Symbol, topic and package name are three views of ONE fact — "this thing is called that".
|
|
146
|
-
// Summing them let a
|
|
105
|
+
// Summing them let a word that is a package name, its module name and its exported type
|
|
147
106
|
// (`money`) out-score three genuinely independent hits, so the strongest view wins alone.
|
|
148
|
-
const exact = symbolsLower.indexOf(
|
|
149
|
-
const partial = exact >= 0 ? -1 : symbolsLower.findIndex((symbol) => symbol.includes(
|
|
107
|
+
const exact = symbolsLower.indexOf(word);
|
|
108
|
+
const partial = exact >= 0 ? -1 : symbolsLower.findIndex((symbol) => symbol.includes(word));
|
|
150
109
|
const symbolScore = exact >= 0 ? EXACT_SYMBOL : partial >= 0 ? PARTIAL_SYMBOL : 0;
|
|
151
110
|
const nameScore = Math.max(
|
|
152
111
|
symbolScore,
|
|
153
|
-
|
|
154
|
-
|
|
112
|
+
topicStems.includes(root) ? TOPIC : 0,
|
|
113
|
+
packageStem === root ? PACKAGE : 0,
|
|
155
114
|
);
|
|
156
115
|
score += nameScore;
|
|
157
116
|
const named = entry.symbols[exact >= 0 ? exact : partial];
|
|
158
117
|
if (symbolScore > 0 && named !== undefined) matched.push(named);
|
|
159
|
-
else if (nameScore > 0 && !matched.includes(
|
|
118
|
+
else if (nameScore > 0 && !matched.includes(word)) matched.push(word);
|
|
160
119
|
|
|
161
|
-
// Prose is independent evidence of the same
|
|
120
|
+
// Prose is independent evidence of the same word, and is counted on top — capped, so a long
|
|
162
121
|
// document cannot out-argue the declaration it describes.
|
|
163
|
-
if (titleLower.includes(
|
|
122
|
+
if (titleLower.includes(root)) {
|
|
164
123
|
score += TITLE;
|
|
165
|
-
if (!matched.includes(
|
|
166
|
-
} else if (textLower.includes(
|
|
124
|
+
if (!matched.includes(word)) matched.push(word);
|
|
125
|
+
} else if (textLower.includes(root)) {
|
|
167
126
|
textScore += TEXT;
|
|
168
|
-
if (!matched.includes(
|
|
127
|
+
if (!matched.includes(word)) matched.push(word);
|
|
169
128
|
}
|
|
170
|
-
// Counted per query
|
|
129
|
+
// Counted per query word, not per hit: a word that matched a symbol AND the prose is still
|
|
171
130
|
// one of the question's words accounted for.
|
|
172
131
|
if (score + textScore > before) covered += 1;
|
|
173
132
|
}
|
|
@@ -181,12 +140,19 @@ function scoreEntry(
|
|
|
181
140
|
};
|
|
182
141
|
}
|
|
183
142
|
|
|
143
|
+
/**
|
|
144
|
+
* At an equal score, prose a human wrote about the task beats the header of a file that mentions
|
|
145
|
+
* it: the guide says how to do the thing, the module only that it exists.
|
|
146
|
+
*/
|
|
147
|
+
const byKind = (a: DocHit, b: DocHit): number =>
|
|
148
|
+
a.entry.kind === b.entry.kind ? 0 : a.entry.kind === 'guide' ? -1 : 1;
|
|
149
|
+
|
|
184
150
|
/**
|
|
185
151
|
* Ranked answers to a question, best first. A query that earns no points anywhere returns an
|
|
186
152
|
* empty list rather than the corpus: a search that always answers teaches an agent to trust an
|
|
187
153
|
* answer that was never about its question.
|
|
188
154
|
*
|
|
189
|
-
* Ties break on topic, never on scan order, so the corpus can be assembled in any order and the
|
|
155
|
+
* Ties break on kind (guide first), then on topic, never on scan order, so the corpus can be assembled in any order and the
|
|
190
156
|
* ranking still reproduces.
|
|
191
157
|
*/
|
|
192
158
|
export function searchDocs(
|
|
@@ -203,7 +169,7 @@ export function searchDocs(
|
|
|
203
169
|
const hit = scoreEntry(entry, tokens, rationale);
|
|
204
170
|
if (hit !== undefined && hit.covered >= required) hits.push(hit);
|
|
205
171
|
}
|
|
206
|
-
hits.sort((a, b) => b.score - a.score || byTopic(a.entry.topic, b.entry.topic));
|
|
172
|
+
hits.sort((a, b) => b.score - a.score || byKind(a, b) || byTopic(a.entry.topic, b.entry.topic));
|
|
207
173
|
return hits.slice(0, limit);
|
|
208
174
|
}
|
|
209
175
|
|
|
@@ -221,11 +187,11 @@ export function nearestTopics(
|
|
|
221
187
|
for (const entry of entries) {
|
|
222
188
|
const topic = entry.topic.toLowerCase();
|
|
223
189
|
let score = 0;
|
|
224
|
-
for (const
|
|
225
|
-
if (topic.includes(
|
|
190
|
+
for (const word of tokens) {
|
|
191
|
+
if (topic.includes(word)) score += 2;
|
|
226
192
|
// A prefix, so `retries` still reaches `retry` and `caching` reaches `cache`. Four
|
|
227
193
|
// characters, because three matches almost everything.
|
|
228
|
-
else if (
|
|
194
|
+
else if (word.length >= 4 && topic.includes(word.slice(0, 4))) score += 1;
|
|
229
195
|
}
|
|
230
196
|
if (score > 0) scored.push({ topic: entry.topic, score });
|
|
231
197
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// A question, and every name it is scored against, as the words `docs-search.ts` compares: split,
|
|
2
|
+
// lowercased, stop words dropped, and stemmed one suffix deep. Pure, and shared by both sides of
|
|
3
|
+
// the comparison so a query and a topic can never be cut two different ways.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Words that carry no signal in a question about an API. Deliberately tiny: every word removed
|
|
7
|
+
* here is a word an agent cannot search for, and `get`, `set`, `run` and `new` are all real
|
|
8
|
+
* symbol names in this framework.
|
|
9
|
+
*/
|
|
10
|
+
export const STOPWORDS: ReadonlySet<string> = new Set([
|
|
11
|
+
'a',
|
|
12
|
+
'an',
|
|
13
|
+
'and',
|
|
14
|
+
'are',
|
|
15
|
+
'as',
|
|
16
|
+
'at',
|
|
17
|
+
'be',
|
|
18
|
+
'but',
|
|
19
|
+
'by',
|
|
20
|
+
'can',
|
|
21
|
+
'do',
|
|
22
|
+
'does',
|
|
23
|
+
'for',
|
|
24
|
+
'from',
|
|
25
|
+
'how',
|
|
26
|
+
'i',
|
|
27
|
+
'if',
|
|
28
|
+
'in',
|
|
29
|
+
'is',
|
|
30
|
+
'it',
|
|
31
|
+
'its',
|
|
32
|
+
'me',
|
|
33
|
+
'my',
|
|
34
|
+
'no',
|
|
35
|
+
'not',
|
|
36
|
+
'of',
|
|
37
|
+
'on',
|
|
38
|
+
'or',
|
|
39
|
+
'the',
|
|
40
|
+
'to',
|
|
41
|
+
'use',
|
|
42
|
+
'using',
|
|
43
|
+
'was',
|
|
44
|
+
'what',
|
|
45
|
+
'when',
|
|
46
|
+
'where',
|
|
47
|
+
'which',
|
|
48
|
+
'why',
|
|
49
|
+
'with',
|
|
50
|
+
'you',
|
|
51
|
+
'your',
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Split on non-alphanumerics, then split camelCase — so `nextRetry` answers a query for `retry`
|
|
56
|
+
* and `retry` answers one for `nextRetry`. The joined original is kept alongside its parts so an
|
|
57
|
+
* exact symbol name can still score as an exact hit.
|
|
58
|
+
*/
|
|
59
|
+
export function tokenize(query: string): readonly string[] {
|
|
60
|
+
const out: string[] = [];
|
|
61
|
+
const push = (piece: string): void => {
|
|
62
|
+
if (piece.length < 2 || STOPWORDS.has(piece) || out.includes(piece)) return;
|
|
63
|
+
out.push(piece);
|
|
64
|
+
};
|
|
65
|
+
// Case is load-bearing until after the split: lowercasing first erases the boundary in
|
|
66
|
+
// `nextRetry`, which is the one this exists to find.
|
|
67
|
+
for (const word of query.split(/[^a-zA-Z0-9]+/)) {
|
|
68
|
+
if (word === '') continue;
|
|
69
|
+
// A digit run is its own word too, so `28` reaches the topic `wiki.Upgrading#28x--2900-…`.
|
|
70
|
+
const parts = word.split(
|
|
71
|
+
/(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Za-z])(?=[0-9])|(?<=[0-9])(?=[A-Za-z])/,
|
|
72
|
+
);
|
|
73
|
+
for (const part of parts) push(part.toLowerCase());
|
|
74
|
+
if (parts.length > 1) push(word.toLowerCase());
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The crudest stem that closes the gap measured: `upgrade` never matched `Upgrading`, nor `job`
|
|
81
|
+
* the `jobs` package. One suffix off, only while three characters remain, so `uses` and `test` keep
|
|
82
|
+
* their meaning. Not a linguistic stemmer and not meant to be one — no dependency, no dictionary.
|
|
83
|
+
*/
|
|
84
|
+
export const stem = (word: string): string => {
|
|
85
|
+
const cut = /(?:ing|ed|es|s|e)$/.exec(word);
|
|
86
|
+
return cut !== null && word.length - cut[0].length >= 3 ? word.slice(0, cut.index) : word;
|
|
87
|
+
};
|
package/src/docs-wiki.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// The wiki pages the CLI tarball ships, as doc entries: one per heading, topic `wiki.<Page>#<anchor>`
|
|
2
|
+
// with GitHub's own anchor, so a topic `x docs` prints is the fragment of the page's public URL.
|
|
3
|
+
// Read-only, like `docs-scan.ts`; `wikiSection` is pure and answers a topic with its whole section.
|
|
4
|
+
|
|
5
|
+
// why: Bun has no directory listing and no path module (see `docs-scan.ts`).
|
|
6
|
+
import { readdir } from 'node:fs/promises';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import type { DocEntry } from './docs-scan';
|
|
9
|
+
import { clampDocText, isFence } from './docs-scan';
|
|
10
|
+
|
|
11
|
+
/** Who the pages ship in, so a hit names a file the install really has. */
|
|
12
|
+
export interface WikiOrigin {
|
|
13
|
+
readonly package: string;
|
|
14
|
+
readonly version: string;
|
|
15
|
+
/**
|
|
16
|
+
* The pages' directory relative to that package, so `source` is a path an agent can open:
|
|
17
|
+
* `wiki` in an install, where the tarball carries them; `../../wiki` in this repository's checkout.
|
|
18
|
+
*/
|
|
19
|
+
readonly dir?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface WikiHeading {
|
|
23
|
+
readonly level: number;
|
|
24
|
+
readonly heading: string;
|
|
25
|
+
/** GitHub's slug, numbered `-1`, `-2` for a repeat in document order, as the page renders it. */
|
|
26
|
+
readonly anchor: string;
|
|
27
|
+
/** 0-based index into the page's lines. */
|
|
28
|
+
readonly line: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const HEADING = /^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* GitHub's heading slug: links become their text, tags and backticks drop, then every character
|
|
35
|
+
* that is not a letter, digit, mark, connector, `-` or space goes and each space is a `-`. The same
|
|
36
|
+
* rule `scripts/wiki-anchors.ts` holds every wiki link to — the one copy, which it imports.
|
|
37
|
+
*/
|
|
38
|
+
export const githubSlug = (heading: string): string =>
|
|
39
|
+
heading
|
|
40
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
|
|
41
|
+
.replace(/<[^>]+>/g, '')
|
|
42
|
+
.replaceAll('`', '')
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '')
|
|
45
|
+
.replaceAll(' ', '-');
|
|
46
|
+
|
|
47
|
+
/** Every heading outside a fenced block. A `# comment` inside a ```sh fence is code, not a title. */
|
|
48
|
+
export function wikiHeadings(markdown: string): readonly WikiHeading[] {
|
|
49
|
+
const out: WikiHeading[] = [];
|
|
50
|
+
const seen = new Map<string, number>();
|
|
51
|
+
let fenced = false;
|
|
52
|
+
markdown.split('\n').forEach((text, line) => {
|
|
53
|
+
if (isFence(text)) {
|
|
54
|
+
fenced = !fenced;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (fenced) return;
|
|
58
|
+
const match = HEADING.exec(text);
|
|
59
|
+
if (match === null) return;
|
|
60
|
+
const heading = match[2] ?? '';
|
|
61
|
+
const base = githubSlug(heading);
|
|
62
|
+
const count = seen.get(base) ?? 0;
|
|
63
|
+
seen.set(base, count + 1);
|
|
64
|
+
out.push({
|
|
65
|
+
level: (match[1] ?? '#').length,
|
|
66
|
+
heading,
|
|
67
|
+
anchor: count === 0 ? base : `${base}-${count}`,
|
|
68
|
+
line,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `Upgrading.md` → `Upgrading`; GitHub's chrome (`_Sidebar`, `_Footer`) is navigation, not prose. */
|
|
75
|
+
export const wikiPageOf = (file: string): string | undefined =>
|
|
76
|
+
file.endsWith('.md') && !file.startsWith('_') ? file.slice(0, -'.md'.length) : undefined;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* One page → its entries. The text before the first `##` is the page's own entry, `wiki.<Page>`,
|
|
80
|
+
* titled by its `#` heading; every `##`-or-deeper heading is one more, whose text runs to the next
|
|
81
|
+
* heading of any level — the unit a search scores. `wikiSection` is the unit a reader asks for.
|
|
82
|
+
*/
|
|
83
|
+
export function wikiEntries(
|
|
84
|
+
page: string,
|
|
85
|
+
markdown: string,
|
|
86
|
+
origin: WikiOrigin,
|
|
87
|
+
): readonly DocEntry[] {
|
|
88
|
+
const lines = markdown.split('\n');
|
|
89
|
+
const headings = wikiHeadings(markdown);
|
|
90
|
+
const source = `${origin.dir ?? 'wiki'}/${page}.md`;
|
|
91
|
+
const base = { package: origin.package, version: origin.version, kind: 'guide' as const };
|
|
92
|
+
const body = (from: number, to: number): string =>
|
|
93
|
+
clampDocText(lines.slice(from, to).join('\n').trim());
|
|
94
|
+
const firstSection = headings.find((heading) => heading.level >= 2);
|
|
95
|
+
const title = headings.find((heading) => heading.level === 1);
|
|
96
|
+
const entries: DocEntry[] = [
|
|
97
|
+
{
|
|
98
|
+
...base,
|
|
99
|
+
topic: `wiki.${page}`,
|
|
100
|
+
title: title?.heading ?? page,
|
|
101
|
+
text: body(title === undefined ? 0 : title.line + 1, firstSection?.line ?? lines.length),
|
|
102
|
+
symbols: [],
|
|
103
|
+
source,
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
headings.forEach((heading, index) => {
|
|
107
|
+
if (heading.level < 2) return;
|
|
108
|
+
entries.push({
|
|
109
|
+
...base,
|
|
110
|
+
topic: `wiki.${page}#${heading.anchor}`,
|
|
111
|
+
title: heading.heading,
|
|
112
|
+
text: body(heading.line + 1, headings[index + 1]?.line ?? lines.length),
|
|
113
|
+
symbols: [],
|
|
114
|
+
source,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
return entries;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The whole section an anchor names — its heading, its body and every subsection under it, up to
|
|
122
|
+
* the next heading of the same or a higher level. `undefined` for an anchor the page does not carry.
|
|
123
|
+
*/
|
|
124
|
+
export function wikiSection(markdown: string, anchor: string): string | undefined {
|
|
125
|
+
const headings = wikiHeadings(markdown);
|
|
126
|
+
const index = headings.findIndex((heading) => heading.anchor === anchor);
|
|
127
|
+
const start = headings[index];
|
|
128
|
+
if (start === undefined) return undefined;
|
|
129
|
+
const end = headings.slice(index + 1).find((heading) => heading.level <= start.level);
|
|
130
|
+
const lines = markdown.split('\n');
|
|
131
|
+
return lines
|
|
132
|
+
.slice(start.line, end?.line ?? lines.length)
|
|
133
|
+
.join('\n')
|
|
134
|
+
.trimEnd();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Every page in a shipped `wiki/` directory. `Error-Codes.md` is never read: `x errors explain` is
|
|
139
|
+
* the one way to a code, and indexing 400 rows of it would bury every other answer. A directory that
|
|
140
|
+
* is not there is no pages, not a throw — the caller decides whether that is a broken install.
|
|
141
|
+
*/
|
|
142
|
+
export async function scanWikiDocs(dir: string, origin: WikiOrigin): Promise<readonly DocEntry[]> {
|
|
143
|
+
let files: readonly string[];
|
|
144
|
+
try {
|
|
145
|
+
files = await readdir(dir);
|
|
146
|
+
} catch {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
const pages = [...files]
|
|
150
|
+
.sort()
|
|
151
|
+
.map(wikiPageOf)
|
|
152
|
+
.filter((page): page is string => page !== undefined && page !== 'Error-Codes');
|
|
153
|
+
const perPage = await Promise.all(
|
|
154
|
+
pages.map(async (page) => {
|
|
155
|
+
// A listed page can vanish or turn unreadable before it is read — `bun update` rewrites the
|
|
156
|
+
// tarball under a running `x docs`. One page is worth that page's entries, never the whole
|
|
157
|
+
// answer: the rule `scanInstalledDocs` holds for a package.
|
|
158
|
+
try {
|
|
159
|
+
const file = Bun.file(join(dir, `${page}.md`));
|
|
160
|
+
return (await file.exists()) ? wikiEntries(page, await file.text(), origin) : [];
|
|
161
|
+
} catch {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
return perPage.flat();
|
|
167
|
+
}
|
package/src/emit.ts
CHANGED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// The app graph's public shapes: nodes of a closed vocabulary, dependency edges that each say how
|
|
2
|
+
// they are known (`declared`, `import`, `observed`), and what an impact question answers. Types
|
|
3
|
+
// and the version only — `graph.ts` builds and walks it.
|
|
4
|
+
|
|
5
|
+
/** Bumped when a reader built for the old shape would misread the new one. */
|
|
6
|
+
export const GRAPH_VERSION = 1;
|
|
7
|
+
|
|
8
|
+
export const GRAPH_NODE_KINDS = [
|
|
9
|
+
'route',
|
|
10
|
+
'action',
|
|
11
|
+
'mutator',
|
|
12
|
+
'query',
|
|
13
|
+
'job',
|
|
14
|
+
'task',
|
|
15
|
+
'entity',
|
|
16
|
+
'permission',
|
|
17
|
+
'channel',
|
|
18
|
+
'tag',
|
|
19
|
+
'mcpTool',
|
|
20
|
+
'operation',
|
|
21
|
+
'file',
|
|
22
|
+
'test',
|
|
23
|
+
] as const;
|
|
24
|
+
|
|
25
|
+
export type GraphNodeKind = (typeof GRAPH_NODE_KINDS)[number];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* How an edge is known. `declared`: a registry value states it. `import`: the module graph does.
|
|
29
|
+
* `observed`: a real run did (`x exercise`). An agent decides which of them it trusts.
|
|
30
|
+
*/
|
|
31
|
+
export type EdgeProvenance = 'declared' | 'import' | 'observed';
|
|
32
|
+
|
|
33
|
+
export interface GraphNode {
|
|
34
|
+
/** `<kind>:<name>` — `query:postById`, `file:apps/web/app/posts/page.tsx`. */
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly kind: GraphNodeKind;
|
|
37
|
+
readonly name: string;
|
|
38
|
+
/** The source file that declares it, when the module graph knows. */
|
|
39
|
+
readonly file?: string;
|
|
40
|
+
/** A route's declared JS budget, when it has one. */
|
|
41
|
+
readonly budget?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `from` depends on `to`: a change to `to` can change `from`. */
|
|
45
|
+
export interface GraphEdge {
|
|
46
|
+
readonly from: string;
|
|
47
|
+
readonly to: string;
|
|
48
|
+
/** `imports`, `declared-in`, `invalidates`, `reads-tag`, `requires`, `enqueues`, `exposes`… */
|
|
49
|
+
readonly rel: string;
|
|
50
|
+
readonly provenance: EdgeProvenance;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface AppGraph {
|
|
54
|
+
readonly graphVersion: number;
|
|
55
|
+
/** Sorted by id. */
|
|
56
|
+
readonly nodes: readonly GraphNode[];
|
|
57
|
+
/** Sorted by from, to, rel. */
|
|
58
|
+
readonly edges: readonly GraphEdge[];
|
|
59
|
+
/** Files with a computed `import()`: they may depend on anything, so nothing rules them out. */
|
|
60
|
+
readonly opaque: readonly string[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One affected node, and every provenance its path to the question carried. */
|
|
64
|
+
export interface ImpactEntry {
|
|
65
|
+
readonly id: string;
|
|
66
|
+
readonly name: string;
|
|
67
|
+
readonly provenance: readonly EdgeProvenance[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What a change to one node reaches. Each list sorted by id. */
|
|
71
|
+
export interface Impact {
|
|
72
|
+
readonly node: string;
|
|
73
|
+
readonly routes: readonly ImpactEntry[];
|
|
74
|
+
readonly actions: readonly ImpactEntry[];
|
|
75
|
+
readonly queries: readonly ImpactEntry[];
|
|
76
|
+
readonly jobs: readonly ImpactEntry[];
|
|
77
|
+
readonly mcpTools: readonly ImpactEntry[];
|
|
78
|
+
/** Non-empty: `contract-diff` will have an opinion about this change. */
|
|
79
|
+
readonly publishedOperations: readonly ImpactEntry[];
|
|
80
|
+
readonly tests: readonly ImpactEntry[];
|
|
81
|
+
/** Routes reached that declare a budget: the `budgets` step weighs them again. */
|
|
82
|
+
readonly budgets: readonly ImpactEntry[];
|
|
83
|
+
/** Opaque files reached or reaching: their dependents are included whatever they import. */
|
|
84
|
+
readonly opaque: readonly string[];
|
|
85
|
+
/** Which provenances were searched to answer. */
|
|
86
|
+
readonly provenance: readonly EdgeProvenance[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A path between two nodes, or `null` with what was searched. */
|
|
90
|
+
export interface GraphPath {
|
|
91
|
+
readonly from: string;
|
|
92
|
+
readonly to: string;
|
|
93
|
+
readonly edges: readonly GraphEdge[] | null;
|
|
94
|
+
readonly searched: readonly EdgeProvenance[];
|
|
95
|
+
}
|