@beehexa/hexasync-template-index 2608.15.1 → 2608.20.18
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/package.json +4 -4
- package/dist/checkpoints.d.ts +0 -47
- package/dist/checkpoints.d.ts.map +0 -1
- package/dist/checkpoints.js +0 -59
- package/dist/checkpoints.js.map +0 -1
- package/dist/cursorContext.d.ts +0 -204
- package/dist/cursorContext.d.ts.map +0 -1
- package/dist/cursorContext.js +0 -479
- package/dist/cursorContext.js.map +0 -1
- package/dist/detectors.d.ts +0 -103
- package/dist/detectors.d.ts.map +0 -1
- package/dist/detectors.js +0 -104
- package/dist/detectors.js.map +0 -1
- package/dist/discovery.d.ts +0 -249
- package/dist/discovery.d.ts.map +0 -1
- package/dist/discovery.js +0 -455
- package/dist/discovery.js.map +0 -1
- package/dist/effectiveGraph.d.ts +0 -240
- package/dist/effectiveGraph.d.ts.map +0 -1
- package/dist/effectiveGraph.js +0 -523
- package/dist/effectiveGraph.js.map +0 -1
- package/dist/failures.d.ts +0 -50
- package/dist/failures.d.ts.map +0 -1
- package/dist/failures.js +0 -125
- package/dist/failures.js.map +0 -1
- package/dist/graph.d.ts +0 -47
- package/dist/graph.d.ts.map +0 -1
- package/dist/graph.js +0 -230
- package/dist/graph.js.map +0 -1
- package/dist/index.d.ts +0 -21
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -38
- package/dist/index.js.map +0 -1
- package/dist/queries.d.ts +0 -102
- package/dist/queries.d.ts.map +0 -1
- package/dist/queries.js +0 -469
- package/dist/queries.js.map +0 -1
- package/dist/ranges.d.ts +0 -71
- package/dist/ranges.d.ts.map +0 -1
- package/dist/ranges.js +0 -322
- package/dist/ranges.js.map +0 -1
- package/dist/resolveField.d.ts +0 -125
- package/dist/resolveField.d.ts.map +0 -1
- package/dist/resolveField.js +0 -234
- package/dist/resolveField.js.map +0 -1
- package/dist/tier1Service.d.ts +0 -96
- package/dist/tier1Service.d.ts.map +0 -1
- package/dist/tier1Service.js +0 -194
- package/dist/tier1Service.js.map +0 -1
- package/dist/tier2Service.d.ts +0 -195
- package/dist/tier2Service.d.ts.map +0 -1
- package/dist/tier2Service.js +0 -642
- package/dist/tier2Service.js.map +0 -1
- package/dist/yamlScalar.d.ts +0 -17
- package/dist/yamlScalar.d.ts.map +0 -1
- package/dist/yamlScalar.js +0 -101
- package/dist/yamlScalar.js.map +0 -1
package/dist/tier2Service.js
DELETED
|
@@ -1,642 +0,0 @@
|
|
|
1
|
-
import { describeFailure } from './failures.js';
|
|
2
|
-
import { DEFAULT_MAX_CHECKPOINTS } from './checkpoints.js';
|
|
3
|
-
import { buildEffectiveGraph, resumeEffectiveGraph, } from './effectiveGraph.js';
|
|
4
|
-
/** What a cancelled Tier 2 build resolves to. Never thrown — see AD-14. */
|
|
5
|
-
export const TIER2_CANCELLED = Symbol('tier2-build-cancelled');
|
|
6
|
-
/**
|
|
7
|
-
* What a build resolves to when it FAILED and there is no last-good graph to fall back on.
|
|
8
|
-
*
|
|
9
|
-
* Distinct from cancellation on purpose. Returning `TIER2_CANCELLED` for a first-ever failure
|
|
10
|
-
* made "your YAML is broken" indistinguishable from AD-14's ordinary "a newer build superseded
|
|
11
|
-
* this one" — and opening a project whose YAML is already broken is exactly the common case. The
|
|
12
|
-
* described failure is reachable through `failureFor`, so nothing is swallowed.
|
|
13
|
-
*/
|
|
14
|
-
export const TIER2_FAILED = Symbol('tier2-build-failed');
|
|
15
|
-
export class Tier2IndexService {
|
|
16
|
-
io;
|
|
17
|
-
reader;
|
|
18
|
-
options;
|
|
19
|
-
// Insertion order IS the LRU order: re-inserting on access moves an entry to the end.
|
|
20
|
-
#hot = new Map();
|
|
21
|
-
// Held OUTSIDE the published snapshot: snapshots are frozen and long-lived, checkpoints are
|
|
22
|
-
// heavy and short-lived, and tying their lifetimes together would mean choosing one lifetime
|
|
23
|
-
// wrongly for the other. Insertion order is the eviction order, as with `#hot`.
|
|
24
|
-
#checkpoints = new Map();
|
|
25
|
-
#version = 0;
|
|
26
|
-
#variablesGeneration = 0;
|
|
27
|
-
#tokens = new Map();
|
|
28
|
-
// The last input each project was built from, so a change event can rebuild without the
|
|
29
|
-
// caller having to hand back what it already told the service once.
|
|
30
|
-
#inputs = new Map();
|
|
31
|
-
// Changes that arrived while a build was in flight, coalesced per project (AD-14). Rapid
|
|
32
|
-
// successive edits become ONE rebuild rather than a queue of superseded ones.
|
|
33
|
-
#pending = new Map();
|
|
34
|
-
/**
|
|
35
|
-
* The host's current text for every file it has told us about — CUMULATIVE, per project.
|
|
36
|
-
*
|
|
37
|
-
* The overlay used to be whatever a single change event carried, so a rebuild honoured the
|
|
38
|
-
* developer's typing in the file that triggered it and silently reverted every other unsaved
|
|
39
|
-
* buffer to disk. Verified: an unsaved `Q.yaml` folded in by a resume came back as the on-disk
|
|
40
|
-
* text on the next unrelated rebuild, republished with `stale: false` and a CURRENT watermark —
|
|
41
|
-
* the graph certifying itself fresh for text it does not contain.
|
|
42
|
-
*
|
|
43
|
-
* This mirrors what the host has last said about each file, which is exactly what "the editor's
|
|
44
|
-
* text, which may be newer than the graph" means. A `deleted` change evicts the entry, so a file
|
|
45
|
-
* the host has dropped cannot go on shadowing disk forever.
|
|
46
|
-
*/
|
|
47
|
-
#unsaved = new Map();
|
|
48
|
-
// The TOKEN of the build currently running for each project, not merely a flag. Two builds of
|
|
49
|
-
// one project can overlap, and a plain Set is cleared by whichever settles first — including a
|
|
50
|
-
// stale one that is about to be discarded, which would leave the flag saying "idle" while the
|
|
51
|
-
// real build is still awaiting IO.
|
|
52
|
-
#building = new Map();
|
|
53
|
-
#listeners = new Set();
|
|
54
|
-
#failureListeners = new Set();
|
|
55
|
-
// The most recent failure per project, retained so a consumer that subscribed late — or that
|
|
56
|
-
// polls rather than listens — can still see why a project has no graph.
|
|
57
|
-
#failures = new Map();
|
|
58
|
-
constructor(io, reader, options = {}) {
|
|
59
|
-
this.io = io;
|
|
60
|
-
this.reader = reader;
|
|
61
|
-
this.options = options;
|
|
62
|
-
}
|
|
63
|
-
get maxHotProjects() {
|
|
64
|
-
return this.options.maxHotProjects ?? 8;
|
|
65
|
-
}
|
|
66
|
-
get maxCheckpointProjects() {
|
|
67
|
-
return this.options.maxCheckpointProjects ?? 1;
|
|
68
|
-
}
|
|
69
|
-
/** Which projects currently hold checkpoints, oldest first. Exposed so the bound is testable. */
|
|
70
|
-
checkpointedProjects() {
|
|
71
|
-
return [...this.#checkpoints.keys()];
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* How many edits are buffered for a project, awaiting a re-evaluation.
|
|
75
|
-
*
|
|
76
|
-
* Exposed for the same reason `hotProjects` is: "the buffer retains an edit a declined resume
|
|
77
|
-
* could not apply" is otherwise unobservable, and an unobservable guarantee is one no test can
|
|
78
|
-
* hold on to.
|
|
79
|
-
*/
|
|
80
|
-
pendingChangeCount(projectId) {
|
|
81
|
-
return this.#pending.get(projectId)?.size ?? 0;
|
|
82
|
-
}
|
|
83
|
-
onDidChange(listener) {
|
|
84
|
-
this.#listeners.add(listener);
|
|
85
|
-
return () => this.#listeners.delete(listener);
|
|
86
|
-
}
|
|
87
|
-
onDidFail(listener) {
|
|
88
|
-
this.#failureListeners.add(listener);
|
|
89
|
-
return () => this.#failureListeners.delete(listener);
|
|
90
|
-
}
|
|
91
|
-
/** The most recent failure for a project, or undefined if its last build succeeded. */
|
|
92
|
-
failureFor(projectId) {
|
|
93
|
-
return this.#failures.get(projectId);
|
|
94
|
-
}
|
|
95
|
-
/** The graph for a project if it is hot, marking it most-recently-used. */
|
|
96
|
-
peek(projectId) {
|
|
97
|
-
const found = this.#hot.get(projectId);
|
|
98
|
-
if (!found)
|
|
99
|
-
return undefined;
|
|
100
|
-
// Re-insert so this becomes the newest entry; eviction takes from the front.
|
|
101
|
-
this.#hot.delete(projectId);
|
|
102
|
-
this.#hot.set(projectId, found);
|
|
103
|
-
return found;
|
|
104
|
-
}
|
|
105
|
-
/** Which projects are currently hot, oldest first. Exposed so eviction is testable. */
|
|
106
|
-
hotProjects() {
|
|
107
|
-
return [...this.#hot.keys()];
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Which HOT projects were built from this file (Phase 2 Story 1.6).
|
|
111
|
-
*
|
|
112
|
-
* The question a host cannot answer for itself. A shared component library lives OUTSIDE every
|
|
113
|
-
* project's folder — 9 of the corpus's 14 library folders have no `main.yaml` at all — so a
|
|
114
|
-
* path-prefix test says such a file belongs to no project, and an edit to it is dropped. The
|
|
115
|
-
* inheriting project then keeps publishing findings computed from the library's OLD content while
|
|
116
|
-
* its watermark truthfully certifies the files it did read. The graph knows better: `sources` is
|
|
117
|
-
* exactly the set of files it was built from, inheritance included.
|
|
118
|
-
*
|
|
119
|
-
* Deliberately limited to what is already hot. Answering for the whole workspace would mean
|
|
120
|
-
* indexing every project's component list, which is the eager indexing NFR-8 forbids; and a build
|
|
121
|
-
* triggered by a library edit would spend a build for a project the developer is not in, which is
|
|
122
|
-
* the signal NFR-7 refuses. Updating a project already in memory costs a Map lookup.
|
|
123
|
-
*
|
|
124
|
-
* Reads `#hot` directly rather than through `peek`, because `peek` MEANS "this project was used"
|
|
125
|
-
* and answering a file-change question is not a use. Worth being precise about: the ordering effect
|
|
126
|
-
* is nil either way — a sweep that promotes every hot project in iteration order is an identity
|
|
127
|
-
* permutation — so this is a semantic choice, not an observable one, and no test can distinguish
|
|
128
|
-
* them. Said plainly because the first version of this comment claimed the opposite.
|
|
129
|
-
*/
|
|
130
|
-
projectsReading(uri) {
|
|
131
|
-
const found = [];
|
|
132
|
-
for (const [projectId, snapshot] of this.#hot) {
|
|
133
|
-
if (snapshot.graph.sources.has(uri))
|
|
134
|
-
found.push(projectId);
|
|
135
|
-
}
|
|
136
|
-
return found;
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Build (or rebuild) a project's effective graph.
|
|
140
|
-
*
|
|
141
|
-
* On failure the last good snapshot is retained and re-published marked stale, carrying the
|
|
142
|
-
* failure — rather than the graph being dropped, which would take the editor's every answer
|
|
143
|
-
* with it over one unparseable keystroke.
|
|
144
|
-
*/
|
|
145
|
-
async build(input,
|
|
146
|
-
/** Unsaved host text to build from, so a rebuild honours the editor rather than the disk. */
|
|
147
|
-
overlay) {
|
|
148
|
-
const { projectId } = input;
|
|
149
|
-
this.#inputs.set(projectId, input);
|
|
150
|
-
const token = (this.#tokens.get(projectId) ?? 0) + 1;
|
|
151
|
-
this.#tokens.set(projectId, token);
|
|
152
|
-
this.#building.set(projectId, token);
|
|
153
|
-
const now = this.options.now ?? (() => Date.now());
|
|
154
|
-
const started = now();
|
|
155
|
-
let graph;
|
|
156
|
-
try {
|
|
157
|
-
graph = await buildEffectiveGraph(projectId, input.componentPath, input.mainYml, {
|
|
158
|
-
io: this.io,
|
|
159
|
-
reader: this.reader,
|
|
160
|
-
documentVersion: this.options.documentVersion,
|
|
161
|
-
checkpoints: this.options.checkpoints ?? true,
|
|
162
|
-
...(overlay?.size ? { overlay } : {}),
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
catch (error) {
|
|
166
|
-
this.#clearBuilding(projectId, token);
|
|
167
|
-
if (token !== this.#tokens.get(projectId))
|
|
168
|
-
return TIER2_CANCELLED;
|
|
169
|
-
// The failure is published and the buffered edits are RETAINED, not applied. Draining here
|
|
170
|
-
// would resume from a checkpoint — which does not re-resolve — and so would publish a
|
|
171
|
-
// graph that quietly ignores whatever broke the build, marked `stale: false` with the
|
|
172
|
-
// failure cleared. A failure reported as success is worse than a late keystroke; the
|
|
173
|
-
// pending edits stay buffered and the next successful build applies them.
|
|
174
|
-
return this.#publishFailure(projectId, error);
|
|
175
|
-
}
|
|
176
|
-
this.#clearBuilding(projectId, token);
|
|
177
|
-
// Checked AFTER the await and before publishing, so a superseded build cannot overwrite a
|
|
178
|
-
// fresher one. There is no await between here and the publish.
|
|
179
|
-
if (token !== this.#tokens.get(projectId))
|
|
180
|
-
return TIER2_CANCELLED;
|
|
181
|
-
const published = this.#publishGraph(projectId, graph, now() - started);
|
|
182
|
-
return this.#drainPending(projectId) ?? published;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Apply file changes to a project (Story 3.6, FR-47).
|
|
186
|
-
*
|
|
187
|
-
* The one entry point a watcher needs. It decides between three answers, and the decision is
|
|
188
|
-
* the story: resume from a checkpoint when the change is an ordinary edit, rebuild when the
|
|
189
|
-
* change moves something a checkpoint cannot represent, and do nothing at all when the change
|
|
190
|
-
* is not this project's business.
|
|
191
|
-
*
|
|
192
|
-
* Returns `undefined` when the project was untouched — which is what a change in a DIFFERENT
|
|
193
|
-
* project must produce here, since nothing else in this service knows one project from another.
|
|
194
|
-
*/
|
|
195
|
-
async applyChanges(projectId, changes) {
|
|
196
|
-
const current = this.#hot.get(projectId);
|
|
197
|
-
const input = this.#inputs.get(projectId);
|
|
198
|
-
if (!current || !input)
|
|
199
|
-
return undefined;
|
|
200
|
-
const relevant = changes.filter((c) => this.#touches(current.graph, c));
|
|
201
|
-
if (relevant.length === 0)
|
|
202
|
-
return undefined;
|
|
203
|
-
// Recorded BEFORE deciding what to do, so every downstream path — resume, rebuild, or a
|
|
204
|
-
// rebuild triggered by some later unrelated event — sees the same host text.
|
|
205
|
-
this.#recordText(projectId, relevant);
|
|
206
|
-
// A rebuild is required — not merely preferred — when the change is anything other than new
|
|
207
|
-
// text for a file the worklist already holds. A created or deleted file changes the worklist
|
|
208
|
-
// itself, and only a resolve can say what it changed it to.
|
|
209
|
-
//
|
|
210
|
-
// A file that CONTRIBUTES VARIABLES forces one too. The variable pool is assembled by the
|
|
211
|
-
// resolve, not by the merge, so a resumed re-merge carries the pool it was built with: a
|
|
212
|
-
// `variables.yaml` edit would publish a fresh-looking snapshot still substituting the old
|
|
213
|
-
// values, and every point-wise cache keyed by `variablesGeneration` would survive intact and
|
|
214
|
-
// keep answering with them. Only `main.yaml` and `variables.yaml` feed the pool
|
|
215
|
-
// (`projectResolver` reads no others), so this is the whole set — including an ANCESTOR's,
|
|
216
|
-
// which is in `sources` and would otherwise take the resume path.
|
|
217
|
-
const mustRebuild = relevant.some((c) => c.kind !== 'changed' ||
|
|
218
|
-
typeof c.text !== 'string' ||
|
|
219
|
-
contributesVariables(c.uri));
|
|
220
|
-
const updates = new Map();
|
|
221
|
-
for (const c of relevant) {
|
|
222
|
-
if (c.kind === 'changed' && typeof c.text === 'string') {
|
|
223
|
-
updates.set(c.uri, c.text);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
// The merge base is a STRING the service caches, not a file the resolve re-reads. Rebuilding
|
|
227
|
-
// from the cached copy after main.yaml changed republishes the pre-edit document under a
|
|
228
|
-
// fresh version and an up-to-date watermark — precisely the false freshness AD-7a exists to
|
|
229
|
-
// prevent. The change carries the new text; use it.
|
|
230
|
-
const rootMain = `${input.componentPath}/main.yaml`;
|
|
231
|
-
const editedBase = relevant.find((c) => c.uri === rootMain && typeof c.text === 'string');
|
|
232
|
-
const nextInput = editedBase
|
|
233
|
-
? { ...input, mainYml: editedBase.text }
|
|
234
|
-
: input;
|
|
235
|
-
if (editedBase)
|
|
236
|
-
this.#inputs.set(projectId, nextInput);
|
|
237
|
-
if (!mustRebuild && !editedBase) {
|
|
238
|
-
// Coalesce: a build already running would publish a graph that predates these keystrokes,
|
|
239
|
-
// so they are held and applied when it settles rather than racing it.
|
|
240
|
-
if (this.#building.size > 0 && this.#building.has(projectId)) {
|
|
241
|
-
const pending = this.#pending.get(projectId) ?? new Map();
|
|
242
|
-
for (const [uri, text] of updates)
|
|
243
|
-
pending.set(uri, text);
|
|
244
|
-
this.#pending.set(projectId, pending);
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
const resumed = this.reindex(projectId, updates);
|
|
248
|
-
if (resumed)
|
|
249
|
-
return resumed;
|
|
250
|
-
}
|
|
251
|
-
// The rebuild gets the host's text for EVERY file it has one for, not just this event's. A
|
|
252
|
-
// rebuild is triggered by one file and re-reads all of them, so passing only `updates` honoured
|
|
253
|
-
// the promise for the file that changed and reverted every other unsaved buffer to disk.
|
|
254
|
-
const buffered = this.#pending.get(projectId);
|
|
255
|
-
const folded = buffered ? [...buffered.keys()] : [];
|
|
256
|
-
const built = await this.build(nextInput, this.#overlayFor(projectId));
|
|
257
|
-
// The buffered edits were IN that overlay, so a build that published has applied them. Clearing
|
|
258
|
-
// them here is what makes the buffer drainable at all for a project with no checkpoints, where
|
|
259
|
-
// `#drainPending`'s resume always declines and the edits were previously retained forever.
|
|
260
|
-
// Only on a real publish: a cancelled or failed build applied nothing.
|
|
261
|
-
if (folded.length > 0 &&
|
|
262
|
-
built !== TIER2_CANCELLED &&
|
|
263
|
-
built !== TIER2_FAILED) {
|
|
264
|
-
const still = this.#pending.get(projectId);
|
|
265
|
-
for (const uri of folded)
|
|
266
|
-
still?.delete(uri);
|
|
267
|
-
if (still && still.size === 0)
|
|
268
|
-
this.#pending.delete(projectId);
|
|
269
|
-
}
|
|
270
|
-
return built;
|
|
271
|
-
}
|
|
272
|
-
/** Mirror the host's text for these files, so any later build sees the editor and not the disk. */
|
|
273
|
-
#recordText(projectId, changes) {
|
|
274
|
-
let texts = this.#unsaved.get(projectId);
|
|
275
|
-
for (const change of changes) {
|
|
276
|
-
if (change.kind === 'deleted') {
|
|
277
|
-
texts?.delete(change.uri);
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
if (typeof change.text !== 'string')
|
|
281
|
-
continue;
|
|
282
|
-
if (!texts) {
|
|
283
|
-
texts = new Map();
|
|
284
|
-
this.#unsaved.set(projectId, texts);
|
|
285
|
-
}
|
|
286
|
-
texts.set(change.uri, change.text);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Every file the host has text for, plus anything still buffered.
|
|
291
|
-
*
|
|
292
|
-
* `#pending` is folded in because a build is the one thing that can apply a buffered edit the
|
|
293
|
-
* resume declined — without this, a project with no checkpoints buffered keystrokes that no path
|
|
294
|
-
* ever drained, and the buffer existing to stop edits disappearing was where they disappeared.
|
|
295
|
-
*/
|
|
296
|
-
#overlayFor(projectId) {
|
|
297
|
-
const texts = this.#unsaved.get(projectId);
|
|
298
|
-
const pending = this.#pending.get(projectId);
|
|
299
|
-
if (!pending?.size)
|
|
300
|
-
return texts ?? new Map();
|
|
301
|
-
return new Map([...(texts ?? []), ...pending]);
|
|
302
|
-
}
|
|
303
|
-
/** Whether a change is this project's business at all — AC 1's "other projects untouched". */
|
|
304
|
-
#touches(graph, change) {
|
|
305
|
-
if (graph.sources.has(change.uri))
|
|
306
|
-
return true;
|
|
307
|
-
if (change.kind !== 'created')
|
|
308
|
-
return false;
|
|
309
|
-
// A file created inside the project's own folder is not in `sources` yet and is exactly the
|
|
310
|
-
// case that must force a resolve.
|
|
311
|
-
if (change.uri.startsWith(`${graph.componentPath}/`))
|
|
312
|
-
return true;
|
|
313
|
-
// A `variables.yaml` CREATED in a folder this project already reads from — an ancestor's,
|
|
314
|
-
// typically — is also this project's business: it can only enter the pool through a resolve,
|
|
315
|
-
// and it is in neither `sources` (it did not exist at build time) nor under this project's
|
|
316
|
-
// own path. Without this the pool silently keeps substituting values the file now overrides.
|
|
317
|
-
return (contributesVariables(change.uri) &&
|
|
318
|
-
[...graph.sources.keys()].some((known) => known.startsWith(`${parentOf(change.uri)}/`)));
|
|
319
|
-
}
|
|
320
|
-
/** The configured checkpoint budget, so a resume keeps the bound the build was given. */
|
|
321
|
-
#checkpointBudget() {
|
|
322
|
-
const request = this.options.checkpoints;
|
|
323
|
-
return typeof request === 'object'
|
|
324
|
-
? (request.max ?? DEFAULT_MAX_CHECKPOINTS)
|
|
325
|
-
: DEFAULT_MAX_CHECKPOINTS;
|
|
326
|
-
}
|
|
327
|
-
/** Clear the building marker only if THIS build is the one it names. */
|
|
328
|
-
#clearBuilding(projectId, token) {
|
|
329
|
-
if (this.#building.get(projectId) === token)
|
|
330
|
-
this.#building.delete(projectId);
|
|
331
|
-
}
|
|
332
|
-
/** Apply anything that piled up while a build was in flight, as ONE re-evaluation. */
|
|
333
|
-
#drainPending(projectId) {
|
|
334
|
-
const pending = this.#pending.get(projectId);
|
|
335
|
-
if (!pending?.size)
|
|
336
|
-
return undefined;
|
|
337
|
-
const applied = this.reindex(projectId, pending);
|
|
338
|
-
// RETAINED when the resume declines — a project with no checkpoints, or a change a prefix
|
|
339
|
-
// cannot represent. Deleting first and discarding the result on a decline is how buffered
|
|
340
|
-
// keystrokes disappear silently, which is the failure this whole buffer exists to prevent.
|
|
341
|
-
//
|
|
342
|
-
// `!applied.stale` is the other half, and it was missing. `reindex` deliberately converts a
|
|
343
|
-
// resume FAILURE into a truthy last-good snapshot marked stale, so a throwing resume satisfied
|
|
344
|
-
// a bare `if (applied)` and the buffer was cleared with the edits unapplied — verified pending
|
|
345
|
-
// going 1 → 0 while `stale: true` was published. A stale snapshot has not applied anything.
|
|
346
|
-
if (applied && !applied.stale)
|
|
347
|
-
this.#pending.delete(projectId);
|
|
348
|
-
return applied;
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Re-evaluate a project after an edit, resuming from a checkpoint when one covers it.
|
|
352
|
-
*
|
|
353
|
-
* Returns `undefined` when the change cannot be applied incrementally — a new file, a changed
|
|
354
|
-
* merge base, a project with no checkpoints — leaving the caller to `build()`. Declining is
|
|
355
|
-
* the point: an incremental answer that is merely probably right is worse than a slower one
|
|
356
|
-
* that is certainly right.
|
|
357
|
-
*/
|
|
358
|
-
reindex(projectId, updates) {
|
|
359
|
-
const current = this.peek(projectId);
|
|
360
|
-
const checkpoints = this.#checkpoints.get(projectId);
|
|
361
|
-
if (!current || !checkpoints?.length)
|
|
362
|
-
return undefined;
|
|
363
|
-
// Nothing to apply. `resumeEffectiveGraph` returns the graph UNCHANGED for an empty update map,
|
|
364
|
-
// and the graph it hands back is the published one whose `checkpoints` were stripped to `[]` —
|
|
365
|
-
// so publishing it fired every listener with a new version for an identical graph, cancelled any
|
|
366
|
-
// in-flight build, and then `#retainCheckpoints(projectId, [])` deleted the project's
|
|
367
|
-
// checkpoints outright, permanently ending incremental re-merge for the file being edited.
|
|
368
|
-
// `reindex` is public API, so an empty map is reachable.
|
|
369
|
-
if (updates.size === 0)
|
|
370
|
-
return current;
|
|
371
|
-
const now = this.options.now ?? (() => Date.now());
|
|
372
|
-
const started = now();
|
|
373
|
-
let graph;
|
|
374
|
-
try {
|
|
375
|
-
graph = resumeEffectiveGraph(current.graph, checkpoints, updates, {
|
|
376
|
-
documentVersion: this.options.documentVersion,
|
|
377
|
-
maxCheckpoints: this.#checkpointBudget(),
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
catch (error) {
|
|
381
|
-
// A re-merge that throws is the same broken-workspace case a full build's failure is, and
|
|
382
|
-
// gets the same treatment: keep the last good graph, mark it stale (AD-10).
|
|
383
|
-
const failed = this.#publishFailure(projectId, error);
|
|
384
|
-
// `reindex` promises a snapshot or nothing, and a first-ever failure has no snapshot. The
|
|
385
|
-
// failure is still published and readable via `failureFor`, so it is reported, not lost.
|
|
386
|
-
return failed === TIER2_FAILED ? undefined : failed;
|
|
387
|
-
}
|
|
388
|
-
if (!graph)
|
|
389
|
-
return undefined;
|
|
390
|
-
// A resume supersedes any build in flight, exactly as a build does.
|
|
391
|
-
this.#tokens.set(projectId, (this.#tokens.get(projectId) ?? 0) + 1);
|
|
392
|
-
return this.#publishGraph(projectId, graph, now() - started);
|
|
393
|
-
}
|
|
394
|
-
#publishGraph(projectId, graph, buildMs) {
|
|
395
|
-
// Checkpoints are lifted OFF the published graph. They would otherwise be deep-frozen with
|
|
396
|
-
// it, and `resumeMerge` folds onto a copy of one — freezing what a merge writes into is a
|
|
397
|
-
// silent no-op in sloppy mode and a throw in strict, neither of which is a graph.
|
|
398
|
-
const { checkpoints, ...rest } = graph;
|
|
399
|
-
this.#retainCheckpoints(projectId, checkpoints);
|
|
400
|
-
const snapshot = deepFreeze({
|
|
401
|
-
version: ++this.#version,
|
|
402
|
-
graph: { ...rest, checkpoints: [] },
|
|
403
|
-
buildMs,
|
|
404
|
-
stale: false,
|
|
405
|
-
variablesGeneration: this.#generationFor(projectId, graph),
|
|
406
|
-
});
|
|
407
|
-
this.#hot.delete(projectId);
|
|
408
|
-
this.#hot.set(projectId, snapshot);
|
|
409
|
-
// A project that builds is a project with no outstanding failure. Leaving the old one would
|
|
410
|
-
// have a publisher underlining a line that is now fine.
|
|
411
|
-
this.#failures.delete(projectId);
|
|
412
|
-
this.#evictColdProjects();
|
|
413
|
-
this.#publish(projectId, snapshot);
|
|
414
|
-
return snapshot;
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* The pool generation for a freshly built graph — the previous one unless the pool moved.
|
|
418
|
-
*
|
|
419
|
-
* Compared by value rather than by identity: every rebuild assembles a NEW pool object, so
|
|
420
|
-
* identity would report a change on every keystroke and the point-wise caches Story 3.3 built
|
|
421
|
-
* would never survive one.
|
|
422
|
-
*/
|
|
423
|
-
#generationFor(projectId, graph) {
|
|
424
|
-
const previous = this.#hot.get(projectId);
|
|
425
|
-
if (previous && samePool(previous.graph.variables, graph.variables)) {
|
|
426
|
-
return previous.variablesGeneration;
|
|
427
|
-
}
|
|
428
|
-
return ++this.#variablesGeneration;
|
|
429
|
-
}
|
|
430
|
-
#retainCheckpoints(projectId, checkpoints) {
|
|
431
|
-
this.#checkpoints.delete(projectId);
|
|
432
|
-
if (checkpoints.length > 0)
|
|
433
|
-
this.#checkpoints.set(projectId, checkpoints);
|
|
434
|
-
while (this.#checkpoints.size > this.maxCheckpointProjects) {
|
|
435
|
-
const oldest = this.#checkpoints.keys().next().value;
|
|
436
|
-
if (oldest === undefined)
|
|
437
|
-
break;
|
|
438
|
-
this.#checkpoints.delete(oldest);
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
/** Abandon any in-flight build for a project — on close, or on workspace removal. */
|
|
442
|
-
cancel(projectId) {
|
|
443
|
-
this.#tokens.set(projectId, (this.#tokens.get(projectId) ?? 0) + 1);
|
|
444
|
-
}
|
|
445
|
-
/** Drop a project entirely. */
|
|
446
|
-
evict(projectId) {
|
|
447
|
-
this.cancel(projectId);
|
|
448
|
-
this.#hot.delete(projectId);
|
|
449
|
-
this.#checkpoints.delete(projectId);
|
|
450
|
-
// A failure describes a project the service is indexing. Once it is not, a publisher polling
|
|
451
|
-
// `failureFor` would keep reporting a project that no longer exists here.
|
|
452
|
-
this.#failures.delete(projectId);
|
|
453
|
-
this.#pending.delete(projectId);
|
|
454
|
-
this.#dropRetained(projectId);
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* The per-project state the LRU is supposed to bound but did not.
|
|
458
|
-
*
|
|
459
|
-
* `#inputs` holds each project's full `main.yaml` TEXT and `#unsaved` holds every buffer the host
|
|
460
|
-
* has reported, so leaving them behind means NFR-7's "cold ones are evicted" bounds only the
|
|
461
|
-
* graph. On the 113-project corpus the service accumulated 113 `main.yaml` strings for a structure
|
|
462
|
-
* whose whole premise is that cold projects are released. `#tokens` is small but equally unbounded.
|
|
463
|
-
*
|
|
464
|
-
* Kept as its own method because `evict` and the LRU sweep both need it and only `evict` had it.
|
|
465
|
-
*/
|
|
466
|
-
#dropRetained(projectId) {
|
|
467
|
-
this.#inputs.delete(projectId);
|
|
468
|
-
this.#tokens.delete(projectId);
|
|
469
|
-
this.#unsaved.delete(projectId);
|
|
470
|
-
}
|
|
471
|
-
#publishFailure(projectId, error) {
|
|
472
|
-
// The project's `main.yaml`, not its FOLDER. `componentPath` is a directory, and
|
|
473
|
-
// `IndexFailure.sourceFile` is documented as "the file the failure is about" — a publisher
|
|
474
|
-
// attaching a diagnostic to a folder URI drops it silently, so every failure whose message
|
|
475
|
-
// carried no filename was reported at a location no editor can underline. `main.yaml` is the
|
|
476
|
-
// right fallback: it is the file that declares the externals these failures are about.
|
|
477
|
-
const componentPath = this.#inputs.get(projectId)?.componentPath;
|
|
478
|
-
const failure = describeFailure(error, componentPath === undefined ? undefined : `${componentPath}/main.yaml`);
|
|
479
|
-
this.#failures.set(projectId, failure);
|
|
480
|
-
for (const listener of this.#failureListeners) {
|
|
481
|
-
try {
|
|
482
|
-
listener(projectId, failure);
|
|
483
|
-
}
|
|
484
|
-
catch (listenerError) {
|
|
485
|
-
this.options.onListenerError?.(listenerError);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
const lastGood = this.#hot.get(projectId);
|
|
489
|
-
// AD-10. Nothing to retain on a first-ever failure, so the caller is told plainly rather
|
|
490
|
-
// than handed an empty graph that would look like a project containing nothing. The failure
|
|
491
|
-
// itself has already been published above and is readable via `failureFor`.
|
|
492
|
-
if (!lastGood)
|
|
493
|
-
return TIER2_FAILED;
|
|
494
|
-
const stale = deepFreeze({
|
|
495
|
-
...lastGood,
|
|
496
|
-
version: ++this.#version,
|
|
497
|
-
stale: true,
|
|
498
|
-
failure,
|
|
499
|
-
});
|
|
500
|
-
// `delete` then `set` so the entry moves to the FRESH end of the LRU, matching every other
|
|
501
|
-
// publish path. A plain `set` on an existing key keeps its original insertion position, so the
|
|
502
|
-
// project the developer is actively — and unsuccessfully — editing aged toward eviction and
|
|
503
|
-
// could be dropped while it was the only one in use, taking its checkpoints with it.
|
|
504
|
-
this.#hot.delete(projectId);
|
|
505
|
-
this.#hot.set(projectId, stale);
|
|
506
|
-
this.#evictColdProjects();
|
|
507
|
-
this.#publish(projectId, stale);
|
|
508
|
-
return stale;
|
|
509
|
-
}
|
|
510
|
-
#publish(projectId, snapshot) {
|
|
511
|
-
for (const listener of this.#listeners) {
|
|
512
|
-
try {
|
|
513
|
-
listener(projectId, snapshot);
|
|
514
|
-
}
|
|
515
|
-
catch (error) {
|
|
516
|
-
// One bad consumer must not stop the others being told.
|
|
517
|
-
this.options.onListenerError?.(error);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
#evictColdProjects() {
|
|
522
|
-
while (this.#hot.size > this.maxHotProjects) {
|
|
523
|
-
const oldest = this.#hot.keys().next().value;
|
|
524
|
-
if (oldest === undefined)
|
|
525
|
-
break;
|
|
526
|
-
this.#hot.delete(oldest);
|
|
527
|
-
// A graph's checkpoints never outlive the graph: they describe a worklist prefix that
|
|
528
|
-
// only means anything alongside it. Same for its failure and its buffered edits.
|
|
529
|
-
this.#checkpoints.delete(oldest);
|
|
530
|
-
this.#failures.delete(oldest);
|
|
531
|
-
this.#pending.delete(oldest);
|
|
532
|
-
// And the retained TEXT. Only `evict()` used to do this, and the LRU path never called it,
|
|
533
|
-
// so the bound applied to the graph while `#inputs` kept every project's `main.yaml` forever.
|
|
534
|
-
this.#dropRetained(oldest);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
/**
|
|
539
|
-
* Freeze an object and everything reachable from it — `Object.freeze` is shallow.
|
|
540
|
-
*
|
|
541
|
-
* Maps and Sets need more than freezing. `Object.freeze(new Map())` reports `isFrozen === true`
|
|
542
|
-
* and still lets `.set()` through, because a Map mutates internal slots rather than own
|
|
543
|
-
* properties — so a snapshot holding `sources` and `watermark` (both Maps) would satisfy every
|
|
544
|
-
* immutability assertion while remaining writable by anyone holding it. Epic 2's review recorded
|
|
545
|
-
* this exact shape: a test certifying AD-5 with a check a shallow freeze satisfies. The mutators
|
|
546
|
-
* are shadowed with throwing own properties, which the subsequent freeze then makes permanent.
|
|
547
|
-
*/
|
|
548
|
-
function deepFreeze(value, seen = new WeakSet()) {
|
|
549
|
-
// ITERATIVE, with an explicit stack. `queries.ts` made the same choice for the same reason and
|
|
550
|
-
// said so — "a stack cannot overflow on someone's deeply indented YAML" — while this walked the
|
|
551
|
-
// same authored values recursively. Two answers to one hazard in one epic; a throw here escapes
|
|
552
|
-
// `build()` as a rejection rather than the `TIER2_FAILED` the contract promises.
|
|
553
|
-
const stack = [value];
|
|
554
|
-
while (stack.length > 0) {
|
|
555
|
-
const current = stack.pop();
|
|
556
|
-
if (current === null || typeof current !== 'object')
|
|
557
|
-
continue;
|
|
558
|
-
if (seen.has(current))
|
|
559
|
-
continue;
|
|
560
|
-
// A thrown Error is NOT snapshot data. `IndexFailure.cause` carries the original error, and
|
|
561
|
-
// freezing it froze everything reachable from it — the cause chain, any `yaml` Document or
|
|
562
|
-
// WorklistItem it closed over — and replaced their Map/Set mutators with throwing properties for
|
|
563
|
-
// every OTHER holder of those objects. `deepFreeze` was written for the graph's own values; the
|
|
564
|
-
// failure smuggled arbitrary third-party objects in behind them.
|
|
565
|
-
if (current instanceof Error)
|
|
566
|
-
continue;
|
|
567
|
-
seen.add(current);
|
|
568
|
-
if (current instanceof Map || current instanceof Set) {
|
|
569
|
-
for (const entry of current.values())
|
|
570
|
-
stack.push(entry);
|
|
571
|
-
if (current instanceof Map)
|
|
572
|
-
for (const key of current.keys())
|
|
573
|
-
stack.push(key);
|
|
574
|
-
sealCollection(current);
|
|
575
|
-
Object.freeze(current);
|
|
576
|
-
continue;
|
|
577
|
-
}
|
|
578
|
-
for (const key of Object.getOwnPropertyNames(current)) {
|
|
579
|
-
stack.push(current[key]);
|
|
580
|
-
}
|
|
581
|
-
Object.freeze(current);
|
|
582
|
-
}
|
|
583
|
-
return value;
|
|
584
|
-
}
|
|
585
|
-
/** Replace a Map/Set's mutators with throwing own properties, so the freeze actually holds. */
|
|
586
|
-
function sealCollection(collection) {
|
|
587
|
-
const refuse = (method) => () => {
|
|
588
|
-
throw new TypeError(`Cannot ${method} on a published index snapshot — snapshots are immutable (AD-5).`);
|
|
589
|
-
};
|
|
590
|
-
const mutators = collection instanceof Map
|
|
591
|
-
? ['set', 'delete', 'clear']
|
|
592
|
-
: ['add', 'delete', 'clear'];
|
|
593
|
-
for (const method of mutators) {
|
|
594
|
-
// Already sealed. AD-10's stale republish spreads the last-good snapshot into a new object,
|
|
595
|
-
// so the SAME graph — and the same Maps inside it — reach this a second time, and a
|
|
596
|
-
// non-configurable property cannot be redefined.
|
|
597
|
-
if (Object.prototype.hasOwnProperty.call(collection, method))
|
|
598
|
-
continue;
|
|
599
|
-
Object.defineProperty(collection, method, {
|
|
600
|
-
value: refuse(method),
|
|
601
|
-
writable: false,
|
|
602
|
-
enumerable: false,
|
|
603
|
-
configurable: false,
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Whether two assembled variable pools are the same set of definitions.
|
|
609
|
-
*
|
|
610
|
-
* Values are compared as strings because that is what substitution consumes — a pool that
|
|
611
|
-
* changed `8080` from a number to the string `'8080'` resolves identically, and reporting that
|
|
612
|
-
* as a change would drop caches for a difference nothing downstream can observe.
|
|
613
|
-
*/
|
|
614
|
-
function samePool(a, b) {
|
|
615
|
-
const keys = Object.keys(a);
|
|
616
|
-
if (keys.length !== Object.keys(b).length)
|
|
617
|
-
return false;
|
|
618
|
-
for (const key of keys) {
|
|
619
|
-
if (!(key in b))
|
|
620
|
-
return false;
|
|
621
|
-
if (String(a[key] ?? '') !== String(b[key] ?? ''))
|
|
622
|
-
return false;
|
|
623
|
-
}
|
|
624
|
-
return true;
|
|
625
|
-
}
|
|
626
|
-
/**
|
|
627
|
-
* Whether a file can contribute to the variable pool.
|
|
628
|
-
*
|
|
629
|
-
* Only `main.yaml` (inline `variables:`) and `variables.yaml` do — declared as a `type: local`
|
|
630
|
-
* external, or found beside the project's own `componentPath`. Matched on the basename because
|
|
631
|
-
* an ancestor's counts exactly as much as this project's.
|
|
632
|
-
*/
|
|
633
|
-
function contributesVariables(uri) {
|
|
634
|
-
const basename = uri.slice(uri.lastIndexOf('/') + 1);
|
|
635
|
-
return basename === 'variables.yaml' || basename === 'main.yaml';
|
|
636
|
-
}
|
|
637
|
-
/** The folder a URI sits in, by the same spelling the worklist uses. */
|
|
638
|
-
function parentOf(uri) {
|
|
639
|
-
const at = uri.lastIndexOf('/');
|
|
640
|
-
return at <= 0 ? uri : uri.slice(0, at);
|
|
641
|
-
}
|
|
642
|
-
//# sourceMappingURL=tier2Service.js.map
|