@yaks/code 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +106 -2
- package/_dist/anatomy.d.ts +144 -0
- package/_dist/anatomy.d.ts.map +1 -0
- package/_dist/described.d.ts +31 -0
- package/_dist/described.d.ts.map +1 -0
- package/_dist/effects.d.ts +9 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/frames.d.ts +16 -0
- package/_dist/frames.d.ts.map +1 -0
- package/_dist/mod.d.ts +26 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/read.d.ts +89 -0
- package/_dist/read.d.ts.map +1 -0
- package/_dist/source.d.ts +55 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/sync.d.ts +39 -0
- package/_dist/sync.d.ts.map +1 -0
- package/_dist/tools.d.ts +8 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/anatomy.js +552 -0
- package/anatomy.js.map +1 -0
- package/anatomy.ts +638 -0
- package/deno.json +1 -0
- package/described.js +274 -0
- package/described.js.map +1 -0
- package/described.ts +314 -0
- package/effects.js +24 -0
- package/effects.js.map +1 -0
- package/effects.ts +30 -0
- package/frames.js +46 -0
- package/frames.js.map +1 -0
- package/frames.ts +54 -0
- package/mod.js +26 -0
- package/mod.js.map +1 -0
- package/mod.ts +27 -0
- package/package.json +54 -4
- package/read.js +178 -0
- package/read.js.map +1 -0
- package/read.ts +249 -0
- package/source.js +238 -0
- package/source.js.map +1 -0
- package/source.ts +263 -0
- package/sync.js +345 -0
- package/sync.js.map +1 -0
- package/sync.ts +323 -0
- package/tools.js +49 -0
- package/tools.js.map +1 -0
- package/tools.ts +59 -0
- package/vocab.js +21 -0
- package/vocab.js.map +1 -0
- package/vocab.json +143 -0
- package/vocab.ts +22 -0
package/source.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// A box catalog's source boundary: known Git checkouts and explicitly served
|
|
2
|
+
// URL origins. No frame chooses a directory, remote host, or revision command.
|
|
3
|
+
import { repositoryEid } from "@yaks/git/host";
|
|
4
|
+
import { repoPath, rootsOf, trees } from "@yaks/git/source";
|
|
5
|
+
import { checkout } from "./sync.js";
|
|
6
|
+
import { resolveFrames } from "./frames.js";
|
|
7
|
+
// deno-lint-ignore no-control-regex
|
|
8
|
+
let unsafe = /[\\\x00-\x1f\x7f]/;
|
|
9
|
+
export let sourcePath = (file, sources)=>{
|
|
10
|
+
if (file.length > 4096 || unsafe.test(file)) return;
|
|
11
|
+
let path;
|
|
12
|
+
try {
|
|
13
|
+
let raw = decodeURIComponent(file.split(/[?#]/)[0]);
|
|
14
|
+
if (raw.split('/').some((part)=>part == '..' || part == '.')) return;
|
|
15
|
+
if (file.startsWith('file:') || file.startsWith('/')) {
|
|
16
|
+
let url = new URL(file.startsWith('/') ? `file://${file}` : file);
|
|
17
|
+
if (url.protocol != 'file:' || url.host) return;
|
|
18
|
+
let absolute = decodeURIComponent(url.pathname);
|
|
19
|
+
let root = [
|
|
20
|
+
...sources.roots
|
|
21
|
+
].sort((a, b)=>b.length - a.length).find((r)=>absolute.startsWith(r + '/'));
|
|
22
|
+
if (!root) return;
|
|
23
|
+
path = absolute.slice(root.length + 1);
|
|
24
|
+
} else {
|
|
25
|
+
let url = new URL(file);
|
|
26
|
+
if (![
|
|
27
|
+
'http:',
|
|
28
|
+
'https:'
|
|
29
|
+
].includes(url.protocol) || url.username || url.password || !sources.origins?.includes(url.origin)) return;
|
|
30
|
+
path = decodeURIComponent(url.pathname.slice(1));
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
return repoPath(path) ? path : undefined;
|
|
36
|
+
};
|
|
37
|
+
/** Read only requested global identities from the fleet's existing HTTP door. */ export let catalogAt = (url, send = fetch)=>({
|
|
38
|
+
get: async (ids)=>{
|
|
39
|
+
if (!ids.length) return [];
|
|
40
|
+
let at = new URL('/query', url);
|
|
41
|
+
at.searchParams.set('q', `.entity.eid=${ids.join(',')} ?file ?module ?symbol`);
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await send(at, {
|
|
45
|
+
signal: AbortSignal.timeout(5000)
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
// Transport unavailability is the pool waiting on the catalog, not a
|
|
49
|
+
// defect to report on every attempt. The final failure is still reported.
|
|
50
|
+
if (error instanceof Error) throw Object.assign(error, {
|
|
51
|
+
retry: {}
|
|
52
|
+
});
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
let error = Error(`code catalog: ${response.status}`);
|
|
57
|
+
throw response.status >= 500 || [
|
|
58
|
+
408,
|
|
59
|
+
429
|
|
60
|
+
].includes(response.status) ? Object.assign(error, {
|
|
61
|
+
retry: {}
|
|
62
|
+
}) : error;
|
|
63
|
+
}
|
|
64
|
+
let rows = await response.json();
|
|
65
|
+
if (!Array.isArray(rows)) throw Error('code catalog: expected bundles');
|
|
66
|
+
return rows;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// A value read once and kept, read again only when asked `again` at least `ms`
|
|
70
|
+
// after the last read. A failed read is not kept.
|
|
71
|
+
let kept = (read, ms = Infinity)=>{
|
|
72
|
+
let got;
|
|
73
|
+
let at = 0;
|
|
74
|
+
return (again = false)=>{
|
|
75
|
+
if (!got || again && Date.now() - at >= ms) {
|
|
76
|
+
at = Date.now();
|
|
77
|
+
got = read().catch((error)=>{
|
|
78
|
+
got = undefined;
|
|
79
|
+
throw error;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return got;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
/** How often a frame under no known checkout reads the worktrees again (ms). */ export let ROOTS = 5_000;
|
|
86
|
+
/** A catalog whose answers are kept for `ms`: frames thrown at one commit ask
|
|
87
|
+
* after the same files and exports again and again, and the catalog changes
|
|
88
|
+
* only as the code syncs. Callers asking at once share one request; a failed
|
|
89
|
+
* request is not kept.
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { equal } from '@yaks/testing'
|
|
93
|
+
* import { remembered } from '@yaks/code/source'
|
|
94
|
+
*
|
|
95
|
+
* let asked: string[][] = []
|
|
96
|
+
* let catalog = remembered({
|
|
97
|
+
* get: (ids) => (asked.push(ids), Promise.resolve([{ entity: { eid: 'f' } }])),
|
|
98
|
+
* })
|
|
99
|
+
* await Promise.all([catalog.get(['f', 'g']), catalog.get(['f'])])
|
|
100
|
+
* equal(asked, [['f', 'g']])
|
|
101
|
+
* ```
|
|
102
|
+
*/ export let remembered = (catalog, ms = 60_000)=>{
|
|
103
|
+
let known = new Map();
|
|
104
|
+
return {
|
|
105
|
+
get: async (ids)=>{
|
|
106
|
+
let now = Date.now();
|
|
107
|
+
let fresh = (id)=>now - (known.get(id)?.at ?? -Infinity) < ms;
|
|
108
|
+
if (known.size > 10_000) {
|
|
109
|
+
for (let id of known.keys())if (!fresh(id)) known.delete(id);
|
|
110
|
+
}
|
|
111
|
+
let missing = [
|
|
112
|
+
...new Set(ids)
|
|
113
|
+
].filter((id)=>!fresh(id));
|
|
114
|
+
if (missing.length) {
|
|
115
|
+
let asked = catalog.get(missing).then((rows)=>new Map(rows.map((row)=>[
|
|
116
|
+
row.entity.eid,
|
|
117
|
+
row
|
|
118
|
+
])));
|
|
119
|
+
for (let id of missing){
|
|
120
|
+
let row = asked.then((rows)=>rows.get(id), (error)=>{
|
|
121
|
+
if (known.get(id)?.row == row) known.delete(id);
|
|
122
|
+
throw error;
|
|
123
|
+
});
|
|
124
|
+
known.set(id, {
|
|
125
|
+
at: now,
|
|
126
|
+
row
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let rows = await Promise.all(ids.map((id)=>known.get(id).row));
|
|
131
|
+
return rows.filter((row)=>row != null);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
/** A resolver for frames thrown at a commit. What it reads Git for is kept for
|
|
136
|
+
* the life of the resolver, so a run costs no process: the checkout and its
|
|
137
|
+
* repository never move, and a commit's tree never changes. Linked worktrees
|
|
138
|
+
* come and go, so a file frame under none of the roots known reads them again,
|
|
139
|
+
* at most once every {@link ROOTS} ms. The catalog it asks by default is
|
|
140
|
+
* {@link remembered}. */ export let sourceFrames = (options, catalog = remembered(catalogAt(options.url)))=>{
|
|
141
|
+
let repo = kept(async ()=>{
|
|
142
|
+
let { root, common } = await checkout(options.cwd);
|
|
143
|
+
return {
|
|
144
|
+
root,
|
|
145
|
+
common,
|
|
146
|
+
tree: trees(root)
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
let roots = kept(async ()=>{
|
|
150
|
+
let { root, common } = await repo();
|
|
151
|
+
return [
|
|
152
|
+
...new Set([
|
|
153
|
+
root,
|
|
154
|
+
...await rootsOf(common)
|
|
155
|
+
])
|
|
156
|
+
];
|
|
157
|
+
}, ROOTS);
|
|
158
|
+
let repository = repositoryCatalog(options.url);
|
|
159
|
+
let origins = options.origins ?? [
|
|
160
|
+
new URL(options.url).origin
|
|
161
|
+
];
|
|
162
|
+
let local = (file)=>file.startsWith('file:') || file[0] == '/';
|
|
163
|
+
return async (frames, commit)=>{
|
|
164
|
+
let { common, tree } = await repo();
|
|
165
|
+
let under = (roots)=>frames.map((f)=>sourcePath(f.file, {
|
|
166
|
+
roots,
|
|
167
|
+
origins
|
|
168
|
+
}));
|
|
169
|
+
let paths = under(await roots());
|
|
170
|
+
if (paths.some((path, i)=>path == null && local(frames[i].file))) {
|
|
171
|
+
paths = under(await roots(true));
|
|
172
|
+
}
|
|
173
|
+
let blobs = await tree(commit, paths.filter((p)=>p != null));
|
|
174
|
+
let places = paths.map((path, i)=>({
|
|
175
|
+
path: path ?? '',
|
|
176
|
+
function: frames[i].function,
|
|
177
|
+
line: frames[i].line
|
|
178
|
+
}));
|
|
179
|
+
let identity = options.workspace ? repositoryEid(common, options.workspace) : await repository(common);
|
|
180
|
+
// Source classification still works while the catalog is migrating or
|
|
181
|
+
// unavailable. Optional symbol enrichment must not recursively break error
|
|
182
|
+
// ingestion, nor guess a computer from a path shared by several contexts.
|
|
183
|
+
if (!identity) {
|
|
184
|
+
return frames.map((f, i)=>({
|
|
185
|
+
...f,
|
|
186
|
+
app: paths[i] != null
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
let resolved = await resolveFrames(catalog, identity, blobs, places);
|
|
190
|
+
return frames.map((f, i)=>({
|
|
191
|
+
...f,
|
|
192
|
+
...resolved[i]
|
|
193
|
+
}));
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
// The catalog is authoritative for its filesystem context. Historical rows
|
|
197
|
+
// without placement cannot identify a computer. A missing/ambiguous catalog
|
|
198
|
+
// means no symbol enrichment, not an error in the error-ingestion pipeline.
|
|
199
|
+
/** The uniquely placed repository at the catalog, or no attribution while
|
|
200
|
+
* placement is unknown, ambiguous or the catalog is unavailable. Unqualified
|
|
201
|
+
* historical observations do not hide a qualified repository. */ export let catalogRepository = async (url, common, send = fetch)=>{
|
|
202
|
+
let at = new URL('/query', url);
|
|
203
|
+
// One common directory can be placed on several computers. Retain every
|
|
204
|
+
// matching context to reject ambiguity, but never fetch unrelated repositories.
|
|
205
|
+
at.searchParams.set('q', `.repository.common=${JSON.stringify(common)} ?repository`);
|
|
206
|
+
let rows;
|
|
207
|
+
try {
|
|
208
|
+
let response = await send(at, {
|
|
209
|
+
signal: AbortSignal.timeout(5000)
|
|
210
|
+
});
|
|
211
|
+
if (!response.ok) return;
|
|
212
|
+
rows = await response.json();
|
|
213
|
+
if (!Array.isArray(rows)) return;
|
|
214
|
+
} catch {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let placed = rows.filter((b)=>{
|
|
218
|
+
let r = b.repository;
|
|
219
|
+
return r?.common == common && typeof r.workspace == 'string' && r.workspace.length > 0;
|
|
220
|
+
});
|
|
221
|
+
return placed.length == 1 ? placed[0].entity.eid : undefined;
|
|
222
|
+
};
|
|
223
|
+
/** Frame attribution shares one repository lookup across concurrent errors.
|
|
224
|
+
* Recheck placement after a minute, including absent/ambiguous results: optional
|
|
225
|
+
* enrichment cannot flood the serving graph while an error spool drains. */ export let repositoryCatalog = (url, send = fetch, now = Date.now)=>{
|
|
226
|
+
let known = new Map();
|
|
227
|
+
return (common)=>{
|
|
228
|
+
let prior = known.get(common);
|
|
229
|
+
if (prior && now() - prior.at < 60_000) return prior.answer;
|
|
230
|
+
let answer = catalogRepository(url, common, send);
|
|
231
|
+
known.set(common, {
|
|
232
|
+
at: now(),
|
|
233
|
+
answer
|
|
234
|
+
});
|
|
235
|
+
return answer;
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
//# sourceMappingURL=source.js.map
|
package/source.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.js","sources":["./source.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,+EAA+E;AAE/E,SAAS,aAAa,8BAAmC;AACzD,SAAS,QAAQ,EAAE,OAAO,EAAE,KAAK,gCAAqC;AACtE,SAAS,QAAQ,oBAAmB;AAEpC,SAAuB,aAAa,sBAAqB;AAiBzD,oCAAoC;AACpC,IAAI,SAAS;AAEb,OAAO,IAAI,aAAa,CACtB,MACA;EAEA,IAAI,KAAK,MAAM,GAAG,QAAQ,OAAO,IAAI,CAAC,OAAO;EAC7C,IAAI;EACJ,IAAI;IACF,IAAI,MAAM,mBAAmB,KAAK,KAAK,CAAC,OAAO,CAAC,EAAE;IAClD,IAAI,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC,OAAS,QAAQ,QAAQ,QAAQ,MAAM;IAChE,IAAI,KAAK,UAAU,CAAC,YAAY,KAAK,UAAU,CAAC,MAAM;MACpD,IAAI,MAAM,IAAI,IAAI,KAAK,UAAU,CAAC,OAAO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jB,KACA,CAAC,mBAAmB,EAAE,KAAK,SAAS,CAAC,QAAQ,YAAY,CAAC;EAE5D,IAAI;EACJ,IAAI;IACF,IAAI,WAAW,MAAM,KAAK,IAAI;MAAE,QAAQ,YAAY,OAAO,CAAC;IAAM;IAClE,IAAI,CAAC,SAAS,EAAE,EAAE;IAClB,OAAO,MAAM,SAAS,IAAI;IAC1B,IAAI,CAAC,MAAM,OAAO,CAAC,OAAO;EAC5B,EAAE,OAAM;IACN;EACF;EACA,IAAI,SAAS,KAAK,MAAM,CAAC,CAAC;IACxB,IAAI,IAAI,EAAE,UAAU;IACpB,OAAO,GAAG,UAAU,UAAU,OAAO,EAAE,SAAS,IAAI,YAClD,EAAE,SAAS,CAAC,MAAM,GAAG;EACzB;EACA,OAAO,OAAO,MAAM,IAAI,IAAI,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,GAAG;AACrD,EAAC;AAED;;0EAE0E,GAC1E,OAAO,IAAI,oBAAoB,CAC7B,KACA,OAAqB,KAAK,EAC1B,MAAM,KAAK,GAAG;EAEd,IAAI,QAAQ,IAAI;EAIhB,OAAO,CAAC;IACN,IAAI,QAAQ,MAAM,GAAG,CAAC;IACtB,IAAI,SAAS,QAAQ,MAAM,EAAE,GAAG,QAAQ,OAAO,MAAM,MAAM;IAC3D,IAAI,SAAS,kBAAkB,KAAK,QAAQ;IAC5C,MAAM,GAAG,CAAC,QAAQ;MAAE,IAAI;MAAO;IAAO;IACtC,OAAO;EACT;AACF,EAAC"}
|
package/source.ts
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// A box catalog's source boundary: known Git checkouts and explicitly served
|
|
2
|
+
// URL origins. No frame chooses a directory, remote host, or revision command.
|
|
3
|
+
|
|
4
|
+
import { repositoryEid } from '@yaks/git/host'
|
|
5
|
+
import { repoPath, rootsOf, trees } from '@yaks/git/source'
|
|
6
|
+
import { checkout } from './sync.js'
|
|
7
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
8
|
+
import { type Catalog, resolveFrames } from './frames.js'
|
|
9
|
+
|
|
10
|
+
export type Location = { file: string; function?: string; line?: number }
|
|
11
|
+
export type Sources = { roots: string[]; origins?: string[] }
|
|
12
|
+
export type SourceOptions = {
|
|
13
|
+
cwd: string
|
|
14
|
+
url: string
|
|
15
|
+
origins?: string[]
|
|
16
|
+
workspace?: string
|
|
17
|
+
}
|
|
18
|
+
/** Frames thrown at a commit, each told whether it is the application's and,
|
|
19
|
+
* where the catalog matches that commit, its module and symbol. */
|
|
20
|
+
export type Resolver = <T extends Location>(
|
|
21
|
+
frames: T[],
|
|
22
|
+
commit: string,
|
|
23
|
+
) => Promise<(T & { app: boolean; module?: string; symbol?: string })[]>
|
|
24
|
+
|
|
25
|
+
// deno-lint-ignore no-control-regex
|
|
26
|
+
let unsafe = /[\\\x00-\x1f\x7f]/
|
|
27
|
+
|
|
28
|
+
export let sourcePath = (
|
|
29
|
+
file: string,
|
|
30
|
+
sources: Sources,
|
|
31
|
+
): string | undefined => {
|
|
32
|
+
if (file.length > 4096 || unsafe.test(file)) return
|
|
33
|
+
let path: string
|
|
34
|
+
try {
|
|
35
|
+
let raw = decodeURIComponent(file.split(/[?#]/)[0])
|
|
36
|
+
if (raw.split('/').some((part) => part == '..' || part == '.')) return
|
|
37
|
+
if (file.startsWith('file:') || file.startsWith('/')) {
|
|
38
|
+
let url = new URL(file.startsWith('/') ? `file://${file}` : file)
|
|
39
|
+
if (url.protocol != 'file:' || url.host) return
|
|
40
|
+
let absolute = decodeURIComponent(url.pathname)
|
|
41
|
+
let root = [...sources.roots].sort((a, b) => b.length - a.length)
|
|
42
|
+
.find((r) => absolute.startsWith(r + '/'))
|
|
43
|
+
if (!root) return
|
|
44
|
+
path = absolute.slice(root.length + 1)
|
|
45
|
+
} else {
|
|
46
|
+
let url = new URL(file)
|
|
47
|
+
if (
|
|
48
|
+
!['http:', 'https:'].includes(url.protocol) || url.username ||
|
|
49
|
+
url.password || !sources.origins?.includes(url.origin)
|
|
50
|
+
) return
|
|
51
|
+
path = decodeURIComponent(url.pathname.slice(1))
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
return repoPath(path) ? path : undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Read only requested global identities from the fleet's existing HTTP door. */
|
|
60
|
+
export let catalogAt = (url: string, send: typeof fetch = fetch): Catalog => ({
|
|
61
|
+
get: async (ids) => {
|
|
62
|
+
if (!ids.length) return []
|
|
63
|
+
let at = new URL('/query', url)
|
|
64
|
+
at.searchParams.set(
|
|
65
|
+
'q',
|
|
66
|
+
`.entity.eid=${ids.join(',')} ?file ?module ?symbol`,
|
|
67
|
+
)
|
|
68
|
+
let response: Response
|
|
69
|
+
try {
|
|
70
|
+
response = await send(at, { signal: AbortSignal.timeout(5000) })
|
|
71
|
+
} catch (error) {
|
|
72
|
+
// Transport unavailability is the pool waiting on the catalog, not a
|
|
73
|
+
// defect to report on every attempt. The final failure is still reported.
|
|
74
|
+
if (error instanceof Error) throw Object.assign(error, { retry: {} })
|
|
75
|
+
throw error
|
|
76
|
+
}
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
let error = Error(`code catalog: ${response.status}`)
|
|
79
|
+
throw response.status >= 500 || [408, 429].includes(response.status)
|
|
80
|
+
? Object.assign(error, { retry: {} })
|
|
81
|
+
: error
|
|
82
|
+
}
|
|
83
|
+
let rows = await response.json()
|
|
84
|
+
if (!Array.isArray(rows)) throw Error('code catalog: expected bundles')
|
|
85
|
+
return rows
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
// A value read once and kept, read again only when asked `again` at least `ms`
|
|
90
|
+
// after the last read. A failed read is not kept.
|
|
91
|
+
let kept = <T>(read: () => Promise<T>, ms = Infinity) => {
|
|
92
|
+
let got: Promise<T> | undefined
|
|
93
|
+
let at = 0
|
|
94
|
+
return (again = false): Promise<T> => {
|
|
95
|
+
if (!got || (again && Date.now() - at >= ms)) {
|
|
96
|
+
at = Date.now()
|
|
97
|
+
got = read().catch((error) => {
|
|
98
|
+
got = undefined
|
|
99
|
+
throw error
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
return got
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** How often a frame under no known checkout reads the worktrees again (ms). */
|
|
107
|
+
export let ROOTS = 5_000
|
|
108
|
+
|
|
109
|
+
/** A catalog whose answers are kept for `ms`: frames thrown at one commit ask
|
|
110
|
+
* after the same files and exports again and again, and the catalog changes
|
|
111
|
+
* only as the code syncs. Callers asking at once share one request; a failed
|
|
112
|
+
* request is not kept.
|
|
113
|
+
*
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { equal } from '@yaks/testing'
|
|
116
|
+
* import { remembered } from '@yaks/code/source'
|
|
117
|
+
*
|
|
118
|
+
* let asked: string[][] = []
|
|
119
|
+
* let catalog = remembered({
|
|
120
|
+
* get: (ids) => (asked.push(ids), Promise.resolve([{ entity: { eid: 'f' } }])),
|
|
121
|
+
* })
|
|
122
|
+
* await Promise.all([catalog.get(['f', 'g']), catalog.get(['f'])])
|
|
123
|
+
* equal(asked, [['f', 'g']])
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export let remembered = (catalog: Catalog, ms = 60_000): Catalog => {
|
|
127
|
+
let known = new Map<
|
|
128
|
+
string,
|
|
129
|
+
{ at: number; row: Promise<Bundle | undefined> }
|
|
130
|
+
>()
|
|
131
|
+
return {
|
|
132
|
+
get: async (ids) => {
|
|
133
|
+
let now = Date.now()
|
|
134
|
+
let fresh = (id: string) => now - (known.get(id)?.at ?? -Infinity) < ms
|
|
135
|
+
if (known.size > 10_000) {
|
|
136
|
+
for (let id of known.keys()) if (!fresh(id)) known.delete(id)
|
|
137
|
+
}
|
|
138
|
+
let missing = [...new Set(ids)].filter((id) => !fresh(id))
|
|
139
|
+
if (missing.length) {
|
|
140
|
+
let asked = catalog.get(missing).then((rows) =>
|
|
141
|
+
new Map(rows.map((row) => [row.entity.eid, row]))
|
|
142
|
+
)
|
|
143
|
+
for (let id of missing) {
|
|
144
|
+
let row: Promise<Bundle | undefined> = asked.then(
|
|
145
|
+
(rows) => rows.get(id),
|
|
146
|
+
(error) => {
|
|
147
|
+
if (known.get(id)?.row == row) known.delete(id)
|
|
148
|
+
throw error
|
|
149
|
+
},
|
|
150
|
+
)
|
|
151
|
+
known.set(id, { at: now, row })
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
let rows = await Promise.all(ids.map((id) => known.get(id)!.row))
|
|
155
|
+
return rows.filter((row) => row != null)
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A resolver for frames thrown at a commit. What it reads Git for is kept for
|
|
161
|
+
* the life of the resolver, so a run costs no process: the checkout and its
|
|
162
|
+
* repository never move, and a commit's tree never changes. Linked worktrees
|
|
163
|
+
* come and go, so a file frame under none of the roots known reads them again,
|
|
164
|
+
* at most once every {@link ROOTS} ms. The catalog it asks by default is
|
|
165
|
+
* {@link remembered}. */
|
|
166
|
+
export let sourceFrames = (
|
|
167
|
+
options: SourceOptions,
|
|
168
|
+
catalog: Catalog = remembered(catalogAt(options.url)),
|
|
169
|
+
): Resolver => {
|
|
170
|
+
let repo = kept(async () => {
|
|
171
|
+
let { root, common } = await checkout(options.cwd)
|
|
172
|
+
return { root, common, tree: trees(root) }
|
|
173
|
+
})
|
|
174
|
+
let roots = kept(async () => {
|
|
175
|
+
let { root, common } = await repo()
|
|
176
|
+
return [...new Set([root, ...await rootsOf(common)])]
|
|
177
|
+
}, ROOTS)
|
|
178
|
+
let repository = repositoryCatalog(options.url)
|
|
179
|
+
let origins = options.origins ?? [new URL(options.url).origin]
|
|
180
|
+
let local = (file: string) => file.startsWith('file:') || file[0] == '/'
|
|
181
|
+
return async (frames, commit) => {
|
|
182
|
+
let { common, tree } = await repo()
|
|
183
|
+
let under = (roots: string[]) =>
|
|
184
|
+
frames.map((f) => sourcePath(f.file, { roots, origins }))
|
|
185
|
+
let paths = under(await roots())
|
|
186
|
+
if (paths.some((path, i) => path == null && local(frames[i].file))) {
|
|
187
|
+
paths = under(await roots(true))
|
|
188
|
+
}
|
|
189
|
+
let blobs = await tree(commit, paths.filter((p) => p != null))
|
|
190
|
+
let places = paths.map((path, i) => ({
|
|
191
|
+
path: path ?? '',
|
|
192
|
+
function: frames[i].function,
|
|
193
|
+
line: frames[i].line,
|
|
194
|
+
}))
|
|
195
|
+
let identity = options.workspace
|
|
196
|
+
? repositoryEid(common, options.workspace)
|
|
197
|
+
: await repository(common)
|
|
198
|
+
// Source classification still works while the catalog is migrating or
|
|
199
|
+
// unavailable. Optional symbol enrichment must not recursively break error
|
|
200
|
+
// ingestion, nor guess a computer from a path shared by several contexts.
|
|
201
|
+
if (!identity) {
|
|
202
|
+
return frames.map((f, i) => ({ ...f, app: paths[i] != null }))
|
|
203
|
+
}
|
|
204
|
+
let resolved = await resolveFrames(catalog, identity, blobs, places)
|
|
205
|
+
return frames.map((f, i) => ({ ...f, ...resolved[i] }))
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// The catalog is authoritative for its filesystem context. Historical rows
|
|
210
|
+
// without placement cannot identify a computer. A missing/ambiguous catalog
|
|
211
|
+
// means no symbol enrichment, not an error in the error-ingestion pipeline.
|
|
212
|
+
/** The uniquely placed repository at the catalog, or no attribution while
|
|
213
|
+
* placement is unknown, ambiguous or the catalog is unavailable. Unqualified
|
|
214
|
+
* historical observations do not hide a qualified repository. */
|
|
215
|
+
export let catalogRepository = async (
|
|
216
|
+
url: string,
|
|
217
|
+
common: string,
|
|
218
|
+
send: typeof fetch = fetch,
|
|
219
|
+
): Promise<string | undefined> => {
|
|
220
|
+
let at = new URL('/query', url)
|
|
221
|
+
// One common directory can be placed on several computers. Retain every
|
|
222
|
+
// matching context to reject ambiguity, but never fetch unrelated repositories.
|
|
223
|
+
at.searchParams.set(
|
|
224
|
+
'q',
|
|
225
|
+
`.repository.common=${JSON.stringify(common)} ?repository`,
|
|
226
|
+
)
|
|
227
|
+
let rows: Bundle[]
|
|
228
|
+
try {
|
|
229
|
+
let response = await send(at, { signal: AbortSignal.timeout(5000) })
|
|
230
|
+
if (!response.ok) return
|
|
231
|
+
rows = await response.json()
|
|
232
|
+
if (!Array.isArray(rows)) return
|
|
233
|
+
} catch {
|
|
234
|
+
return
|
|
235
|
+
}
|
|
236
|
+
let placed = rows.filter((b) => {
|
|
237
|
+
let r = b.repository as Record<string, unknown> | undefined
|
|
238
|
+
return r?.common == common && typeof r.workspace == 'string' &&
|
|
239
|
+
r.workspace.length > 0
|
|
240
|
+
})
|
|
241
|
+
return placed.length == 1 ? placed[0].entity.eid : undefined
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Frame attribution shares one repository lookup across concurrent errors.
|
|
245
|
+
* Recheck placement after a minute, including absent/ambiguous results: optional
|
|
246
|
+
* enrichment cannot flood the serving graph while an error spool drains. */
|
|
247
|
+
export let repositoryCatalog = (
|
|
248
|
+
url: string,
|
|
249
|
+
send: typeof fetch = fetch,
|
|
250
|
+
now = Date.now,
|
|
251
|
+
): (common: string) => Promise<string | undefined> => {
|
|
252
|
+
let known = new Map<
|
|
253
|
+
string,
|
|
254
|
+
{ at: number; answer: Promise<string | undefined> }
|
|
255
|
+
>()
|
|
256
|
+
return (common: string): Promise<string | undefined> => {
|
|
257
|
+
let prior = known.get(common)
|
|
258
|
+
if (prior && now() - prior.at < 60_000) return prior.answer
|
|
259
|
+
let answer = catalogRepository(url, common, send)
|
|
260
|
+
known.set(common, { at: now(), answer })
|
|
261
|
+
return answer
|
|
262
|
+
}
|
|
263
|
+
}
|