@milaboratories/pl-middle-layer 2.9.1 → 2.10.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/dist/index.cjs +5 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/middle_layer/build_stamp.cjs +1 -1
- package/dist/middle_layer/build_stamp.js +1 -1
- package/dist/middle_layer/index.cjs +4 -0
- package/dist/middle_layer/index.d.ts +2 -1
- package/dist/middle_layer/index.js +2 -1
- package/dist/middle_layer/middle_layer.cjs +100 -31
- package/dist/middle_layer/middle_layer.cjs.map +1 -1
- package/dist/middle_layer/middle_layer.d.ts +54 -8
- package/dist/middle_layer/middle_layer.d.ts.map +1 -1
- package/dist/middle_layer/middle_layer.js +99 -30
- package/dist/middle_layer/middle_layer.js.map +1 -1
- package/dist/middle_layer/project.cjs +45 -38
- package/dist/middle_layer/project.cjs.map +1 -1
- package/dist/middle_layer/project.d.ts +18 -3
- package/dist/middle_layer/project.d.ts.map +1 -1
- package/dist/middle_layer/project.js +44 -36
- package/dist/middle_layer/project.js.map +1 -1
- package/dist/middle_layer/project_holds.cjs +266 -0
- package/dist/middle_layer/project_holds.cjs.map +1 -0
- package/dist/middle_layer/project_holds.d.ts +50 -0
- package/dist/middle_layer/project_holds.d.ts.map +1 -0
- package/dist/middle_layer/project_holds.js +262 -0
- package/dist/middle_layer/project_holds.js.map +1 -0
- package/dist/model/block_pack_spec.cjs.map +1 -1
- package/dist/model/block_pack_spec.d.ts +3 -3
- package/dist/model/block_pack_spec.d.ts.map +1 -1
- package/dist/model/block_pack_spec.js.map +1 -1
- package/dist/model/template_spec.d.ts +18 -0
- package/dist/model/template_spec.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.cjs +89 -2
- package/dist/mutator/block-pack/block_pack.cjs.map +1 -1
- package/dist/mutator/block-pack/block_pack.d.ts +16 -0
- package/dist/mutator/block-pack/block_pack.d.ts.map +1 -1
- package/dist/mutator/block-pack/block_pack.js +90 -4
- package/dist/mutator/block-pack/block_pack.js.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.cjs +16 -0
- package/dist/mutator/block-pack/required_capabilities.cjs.map +1 -1
- package/dist/mutator/block-pack/required_capabilities.js +16 -1
- package/dist/mutator/block-pack/required_capabilities.js.map +1 -1
- package/dist/mutator/template/template_cache.cjs +8 -1
- package/dist/mutator/template/template_cache.cjs.map +1 -1
- package/dist/mutator/template/template_cache.d.ts +7 -3
- package/dist/mutator/template/template_cache.d.ts.map +1 -1
- package/dist/mutator/template/template_cache.js +8 -1
- package/dist/mutator/template/template_cache.js.map +1 -1
- package/package.json +6 -6
- package/src/middle_layer/index.ts +2 -0
- package/src/middle_layer/middle_layer.ts +121 -40
- package/src/middle_layer/project.ts +47 -42
- package/src/middle_layer/project_close_during_write.test.ts +142 -0
- package/src/middle_layer/project_handles.test.ts +168 -0
- package/src/middle_layer/project_holds.ts +359 -0
- package/src/middle_layer/project_slots.test.ts +331 -0
- package/src/model/block_pack_spec.ts +3 -2
- package/src/model/template_spec.ts +21 -0
- package/src/mutator/block-pack/block_pack.test.ts +10 -1
- package/src/mutator/block-pack/block_pack.ts +102 -2
- package/src/mutator/block-pack/block_pack_load.test.ts +52 -0
- package/src/mutator/block-pack/required_capabilities.ts +25 -0
- package/src/mutator/project-v3.test.ts +27 -16
- package/src/mutator/template/template_cache.ts +26 -3
- package/src/test/block_packs.ts +12 -0
- package/src/test/write_tx_hooks.ts +75 -0
|
@@ -86,7 +86,7 @@ import type { BlockPackProvider, TemplateResolveOutcome } from "../model/templat
|
|
|
86
86
|
import { resolveTemplateEntries } from "../model/template_resolve";
|
|
87
87
|
import type { PreparedTemplateEntry } from "../mutator/template_construct";
|
|
88
88
|
import { applyTemplateEntries } from "../mutator/template_construct";
|
|
89
|
-
import { throwIfMissingServerCapabilities } from "
|
|
89
|
+
import { throwIfMissingServerCapabilities } from "../mutator/block-pack/required_capabilities";
|
|
90
90
|
import { cacheBlockPackTemplate } from "../mutator/template/template_cache";
|
|
91
91
|
import { ProjectMetaKey } from "../model/project_model";
|
|
92
92
|
import type { ProjectId } from "../model/project_model";
|
|
@@ -134,6 +134,8 @@ import { HmacSha256Signer } from "@milaboratories/ts-helpers";
|
|
|
134
134
|
import type { Computable, ComputableStableDefined } from "@milaboratories/computable";
|
|
135
135
|
import { WatchableValue } from "@milaboratories/computable";
|
|
136
136
|
import { Project } from "./project";
|
|
137
|
+
import type { ProjectHandle, ProjectHolder } from "./project_holds";
|
|
138
|
+
import { asProjectHolder, ProjectSlots } from "./project_holds";
|
|
137
139
|
import type { MiddleLayerOps, MiddleLayerOpsConstructor } from "./ops";
|
|
138
140
|
import { DefaultMiddleLayerOpsPaths, DefaultMiddleLayerOpsSettings } from "./ops";
|
|
139
141
|
import { randomUUID } from "node:crypto";
|
|
@@ -177,6 +179,9 @@ import { TreeSnapshotStore } from "./tree_snapshot_store";
|
|
|
177
179
|
* filesystem does not hold the quit open. */
|
|
178
180
|
const SNAPSHOT_DRAIN_TIMEOUT_MS = 5_000;
|
|
179
181
|
|
|
182
|
+
/** The holder of the holds {@link MiddleLayer.openProject} takes. */
|
|
183
|
+
const LEGACY_HOLDER = asProjectHolder("legacy");
|
|
184
|
+
|
|
180
185
|
export interface MiddleLayerEnvironment {
|
|
181
186
|
dispose(): Promise<void>;
|
|
182
187
|
readonly pl: PlClient;
|
|
@@ -542,7 +547,9 @@ export class MiddleLayer {
|
|
|
542
547
|
// resource nothing holds any more.
|
|
543
548
|
for (const id of outcome.removal.projects) this.projectIdCache.delete(id);
|
|
544
549
|
for (const id of outcome.removal.templates) this.templateIdCache.delete(id);
|
|
550
|
+
// The removed projects are taken from whoever holds them.
|
|
545
551
|
await Promise.all([
|
|
552
|
+
...outcome.removal.projects.map((id) => this.projectSlots.evict(id, "deleted")),
|
|
546
553
|
this.foldersTree.refreshState(),
|
|
547
554
|
this.projectListTree.refreshState(),
|
|
548
555
|
this.templateListTree.refreshState(),
|
|
@@ -812,7 +819,9 @@ export class MiddleLayer {
|
|
|
812
819
|
this.env.runtimeCapabilities.throwIfIncompatible(blockCfg.featureFlags);
|
|
813
820
|
throwIfMissingServerCapabilities(this.pl, preparedBp.requiredCapabilities);
|
|
814
821
|
|
|
815
|
-
const cachedBp = await cacheBlockPackTemplate(this.pl, preparedBp
|
|
822
|
+
const cachedBp = await cacheBlockPackTemplate(this.pl, preparedBp, {
|
|
823
|
+
preparer: this.env.bpPreparer,
|
|
824
|
+
});
|
|
816
825
|
|
|
817
826
|
// Offered to the block's kind while nothing has been created yet. Every entry is
|
|
818
827
|
// checked, including one whose file omitted `params` — the parser read that as `{}`,
|
|
@@ -1149,15 +1158,33 @@ export class MiddleLayer {
|
|
|
1149
1158
|
}
|
|
1150
1159
|
|
|
1151
1160
|
/** Permanently deletes project from the project list, this will result in
|
|
1152
|
-
* destruction of all attached objects, like files, analysis results etc.
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
+
* destruction of all attached objects, like files, analysis results etc.
|
|
1162
|
+
*
|
|
1163
|
+
* A loaded project is taken from every holder: each {@link ProjectHandle} ends with `deleted`
|
|
1164
|
+
* and the project is destroyed, with no snapshot. Holds do not prevent the delete unless
|
|
1165
|
+
* `refuseIfHeldBy` is given. Holds taken during the delete end with it too.
|
|
1166
|
+
*
|
|
1167
|
+
* @param options.refuseIfHeldBy refuse the delete when it returns `true` for the label of any
|
|
1168
|
+
* holder of the project.
|
|
1169
|
+
* @throws {@link ProjectInUseError} when refused; the project is left as it was
|
|
1170
|
+
*/
|
|
1171
|
+
public async deleteProject(
|
|
1172
|
+
id: ProjectId,
|
|
1173
|
+
options: { refuseIfHeldBy?: (holder: ProjectHolder) => boolean } = {},
|
|
1174
|
+
): Promise<void> {
|
|
1175
|
+
await this.projectSlots.delete(
|
|
1176
|
+
id,
|
|
1177
|
+
async () => {
|
|
1178
|
+
await this.pl.withWriteTx("MLRemoveProject", async (tx) => {
|
|
1179
|
+
const entry = (await listedById(tx, this.projectListResourceId)).get(id);
|
|
1180
|
+
if (entry === undefined) throw notListedError("Project", id);
|
|
1181
|
+
tx.removeField(field(this.projectListResourceId, entry.fieldName));
|
|
1182
|
+
await tx.commit();
|
|
1183
|
+
});
|
|
1184
|
+
this.projectIdCache.delete(id);
|
|
1185
|
+
},
|
|
1186
|
+
options.refuseIfHeldBy,
|
|
1187
|
+
);
|
|
1161
1188
|
await this.projectListTree.refreshState();
|
|
1162
1189
|
}
|
|
1163
1190
|
|
|
@@ -1773,9 +1800,22 @@ export class MiddleLayer {
|
|
|
1773
1800
|
// Projects
|
|
1774
1801
|
//
|
|
1775
1802
|
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1803
|
+
/** The loaded projects and who holds them. */
|
|
1804
|
+
private readonly projectSlots = new ProjectSlots<Project>({
|
|
1805
|
+
load: async (id, onGone) =>
|
|
1806
|
+
await Project.init(this.env, id, await this.resolveProjectId(id), onGone),
|
|
1807
|
+
trackSnapshotWrite: (write) => this.trackSnapshotWrite(write),
|
|
1808
|
+
publish: (open) => this.openedProjectsList.setValue(open),
|
|
1809
|
+
logger: {
|
|
1810
|
+
info: (msg) => this.env.logger.info(msg),
|
|
1811
|
+
warn: (msg) => this.env.logger.warn(msg),
|
|
1812
|
+
},
|
|
1813
|
+
});
|
|
1814
|
+
|
|
1815
|
+
/** The holds taken by {@link openProject}, one per project, released by {@link closeProject}. */
|
|
1816
|
+
private readonly legacyHolds = new Map<ProjectId, ProjectHandle>();
|
|
1817
|
+
|
|
1818
|
+
/** Snapshot writes started by a project close and not yet finished. Held only so
|
|
1779
1819
|
* {@link close} can give them a bounded chance to land. */
|
|
1780
1820
|
private readonly pendingSnapshotWrites = new Set<Promise<void>>();
|
|
1781
1821
|
|
|
@@ -1805,43 +1845,84 @@ export class MiddleLayer {
|
|
|
1805
1845
|
}
|
|
1806
1846
|
}
|
|
1807
1847
|
|
|
1808
|
-
/**
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1848
|
+
/**
|
|
1849
|
+
* Takes a hold on a project, loading it when nobody holds it yet. The project stays loaded until
|
|
1850
|
+
* every hold is released, and is taken from all holders when it is deleted or the middle layer
|
|
1851
|
+
* closes; the handle's `signal` says when and why.
|
|
1852
|
+
*
|
|
1853
|
+
* Concurrent acquires share one load. An acquire while the project is being destroyed waits for
|
|
1854
|
+
* the destroy and loads the project anew, so there is never more than one {@link Project} per id.
|
|
1855
|
+
*
|
|
1856
|
+
* @param holder label for logs and {@link projectHolders}, e.g. `desktop` or `mcp:<instance>`
|
|
1857
|
+
* @throws when the project is not in the project list, its load fails, or the middle layer is
|
|
1858
|
+
* closed
|
|
1859
|
+
*/
|
|
1860
|
+
public async acquireProject(id: ProjectId, holder: ProjectHolder): Promise<ProjectHandle> {
|
|
1861
|
+
return await this.projectSlots.acquire(id, holder);
|
|
1814
1862
|
}
|
|
1815
1863
|
|
|
1816
|
-
/**
|
|
1817
|
-
public
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
this.openedProjects.delete(id);
|
|
1864
|
+
/** Labels of the live holds on a project, one per hold; empty when nobody holds it. */
|
|
1865
|
+
public projectHolders(id: ProjectId): ProjectHolder[] {
|
|
1866
|
+
return this.projectSlots.holders(id);
|
|
1867
|
+
}
|
|
1821
1868
|
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1869
|
+
/**
|
|
1870
|
+
* Opens a project, and starts corresponding project maintenance loop: takes a hold labelled
|
|
1871
|
+
* `legacy`, which {@link closeProject} releases. Opening a project this API already holds does
|
|
1872
|
+
* nothing.
|
|
1873
|
+
*
|
|
1874
|
+
* @deprecated use {@link acquireProject}, and release the handle
|
|
1875
|
+
*/
|
|
1876
|
+
public async openProject(id: ProjectId): Promise<void> {
|
|
1877
|
+
if (this.legacyHolds.get(id)?.ended === false) return;
|
|
1878
|
+
const handle = await this.projectSlots.acquire(id, LEGACY_HOLDER);
|
|
1879
|
+
// A concurrent open got there first.
|
|
1880
|
+
if (this.legacyHolds.get(id)?.ended === false) {
|
|
1881
|
+
await handle.release();
|
|
1882
|
+
return;
|
|
1883
|
+
}
|
|
1884
|
+
this.legacyHolds.set(id, handle);
|
|
1885
|
+
// A hold taken from this API (deleted, closed) leaves nothing behind.
|
|
1886
|
+
handle.signal.addEventListener(
|
|
1887
|
+
"abort",
|
|
1888
|
+
() => {
|
|
1889
|
+
if (this.legacyHolds.get(id) === handle) this.legacyHolds.delete(id);
|
|
1890
|
+
},
|
|
1891
|
+
{ once: true },
|
|
1892
|
+
);
|
|
1893
|
+
}
|
|
1830
1894
|
|
|
1831
|
-
|
|
1832
|
-
|
|
1895
|
+
/**
|
|
1896
|
+
* Releases the hold {@link openProject} took. The project is destroyed only when nobody else
|
|
1897
|
+
* holds it. Closing a project this API does not hold does nothing.
|
|
1898
|
+
*
|
|
1899
|
+
* @deprecated use {@link ProjectHandle.release}
|
|
1900
|
+
*/
|
|
1901
|
+
public async closeProject(id: ProjectId): Promise<void> {
|
|
1902
|
+
const handle = this.legacyHolds.get(id);
|
|
1903
|
+
this.legacyHolds.delete(id);
|
|
1904
|
+
if (handle === undefined || handle.ended) {
|
|
1905
|
+
this.env.logger.info(`closeProject: project ${id} is not held by openProject`);
|
|
1906
|
+
return;
|
|
1907
|
+
}
|
|
1908
|
+
await handle.release();
|
|
1833
1909
|
}
|
|
1834
1910
|
|
|
1835
|
-
/**
|
|
1911
|
+
/**
|
|
1912
|
+
* Returns a project access object for a loaded project, whoever holds it.
|
|
1913
|
+
*
|
|
1914
|
+
* A caller holding no {@link ProjectHandle} must not keep the result across awaits: the project
|
|
1915
|
+
* can be destroyed meanwhile.
|
|
1916
|
+
*/
|
|
1836
1917
|
public getOpenedProject(id: ProjectId): Project {
|
|
1837
|
-
const prj = this.
|
|
1918
|
+
const prj = this.projectSlots.get(id);
|
|
1838
1919
|
if (prj === undefined) throw new Error(`Project ${id} not found among opened projects`);
|
|
1839
1920
|
return prj;
|
|
1840
1921
|
}
|
|
1841
1922
|
|
|
1842
|
-
/** Returns true if project
|
|
1923
|
+
/** Returns true if the project is currently loaded, whoever holds it. */
|
|
1843
1924
|
public isProjectOpened(id: ProjectId): boolean {
|
|
1844
|
-
return this.
|
|
1925
|
+
return this.projectSlots.get(id) !== undefined;
|
|
1845
1926
|
}
|
|
1846
1927
|
|
|
1847
1928
|
/** Counters for the persisted project tree mirrors, or undefined when they are switched off.
|
|
@@ -1872,7 +1953,7 @@ export class MiddleLayer {
|
|
|
1872
1953
|
public async close() {
|
|
1873
1954
|
if (this.envelopeCleanupTimer !== undefined) clearInterval(this.envelopeCleanupTimer);
|
|
1874
1955
|
await this.instanceStatsWatch?.terminate();
|
|
1875
|
-
await
|
|
1956
|
+
await this.projectSlots.closeAll();
|
|
1876
1957
|
// this.env.quickJs;
|
|
1877
1958
|
await Promise.all([
|
|
1878
1959
|
this.projectListTree.terminate(),
|
|
@@ -3,7 +3,6 @@ import type {
|
|
|
3
3
|
FieldData,
|
|
4
4
|
Filter,
|
|
5
5
|
OptionalAnyResourceId,
|
|
6
|
-
PlClient,
|
|
7
6
|
SignedResourceId,
|
|
8
7
|
} from "@milaboratories/pl-client";
|
|
9
8
|
import {
|
|
@@ -90,6 +89,7 @@ import type { ProjectOverviewLight } from "./project_overview_light";
|
|
|
90
89
|
import { projectOverviewLight } from "./project_overview_light";
|
|
91
90
|
import { applyProjectMigrations } from "../mutator/migration";
|
|
92
91
|
import { cacheBlockPackTemplate } from "../mutator/template/template_cache";
|
|
92
|
+
import { throwIfMissingServerCapabilities } from "../mutator/block-pack/required_capabilities";
|
|
93
93
|
|
|
94
94
|
type BlockStateComputables = {
|
|
95
95
|
readonly fullState: Computable<BlockStateInternalV3>;
|
|
@@ -197,6 +197,9 @@ export class Project {
|
|
|
197
197
|
/** Whether this tree was seeded from a snapshot. When it was, the file on disk already
|
|
198
198
|
* holds generation 0, so an idle warm reopen writes nothing at all. */
|
|
199
199
|
restoredFromSnapshot: boolean = false,
|
|
200
|
+
/** Called once, from the maintenance loop, when the loop finds the project was deleted
|
|
201
|
+
* elsewhere. The loop has stopped by then; the owner is expected to destroy the project. */
|
|
202
|
+
private readonly onGone?: (project: Project) => void,
|
|
200
203
|
) {
|
|
201
204
|
this.snapshotGeneration = restoredFromSnapshot ? 0 : -1;
|
|
202
205
|
this.overview = projectOverview(
|
|
@@ -326,8 +329,27 @@ export class Project {
|
|
|
326
329
|
}
|
|
327
330
|
}
|
|
328
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Brings the project tree up to a write that has just committed.
|
|
334
|
+
*
|
|
335
|
+
* The write is already on the backend when this runs, so a project destroyed meanwhile (closed
|
|
336
|
+
* by its last holder, or deleted) must not turn it into a failure: a caller told it failed would
|
|
337
|
+
* retry it and apply it twice. `destroy` marks the project destroyed before it terminates the
|
|
338
|
+
* tree, so a refresh failing on a terminated tree always finds the mark set.
|
|
339
|
+
*/
|
|
340
|
+
private async refreshAfterWrite(): Promise<void> {
|
|
341
|
+
if (this.destroyed) return;
|
|
342
|
+
try {
|
|
343
|
+
await this.projectTree.refreshState();
|
|
344
|
+
} catch (e: unknown) {
|
|
345
|
+
if (this.destroyed) return;
|
|
346
|
+
throw e;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
329
350
|
private async refreshLoop(): Promise<void> {
|
|
330
351
|
let retryState: InfiniteRetryState | undefined;
|
|
352
|
+
let gone = false;
|
|
331
353
|
while (!this.destroyed) {
|
|
332
354
|
try {
|
|
333
355
|
await withProject(
|
|
@@ -370,6 +392,7 @@ export class Project {
|
|
|
370
392
|
this.env.logger.warn(
|
|
371
393
|
"project refresh routine terminated, because project was externally deleted",
|
|
372
394
|
);
|
|
395
|
+
gone = true;
|
|
373
396
|
break;
|
|
374
397
|
} else if (isTimeoutOrCancelError(error)) {
|
|
375
398
|
// Timeout during normal operation, continue the loop
|
|
@@ -399,6 +422,7 @@ export class Project {
|
|
|
399
422
|
}
|
|
400
423
|
}
|
|
401
424
|
}
|
|
425
|
+
if (gone) this.onGone?.(this);
|
|
402
426
|
}
|
|
403
427
|
|
|
404
428
|
/**
|
|
@@ -471,7 +495,9 @@ export class Project {
|
|
|
471
495
|
}
|
|
472
496
|
|
|
473
497
|
// Pre-materialize template via cache (separate transaction(s))
|
|
474
|
-
const cachedBp = await cacheBlockPackTemplate(this.env.pl, preparedBp
|
|
498
|
+
const cachedBp = await cacheBlockPackTemplate(this.env.pl, preparedBp, {
|
|
499
|
+
preparer: this.env.bpPreparer,
|
|
500
|
+
});
|
|
475
501
|
|
|
476
502
|
// Build NewBlockSpec based on model API version
|
|
477
503
|
const newBlockSpec =
|
|
@@ -512,7 +538,7 @@ export class Project {
|
|
|
512
538
|
},
|
|
513
539
|
);
|
|
514
540
|
|
|
515
|
-
await this.
|
|
541
|
+
await this.refreshAfterWrite();
|
|
516
542
|
|
|
517
543
|
return blockId;
|
|
518
544
|
}
|
|
@@ -543,7 +569,7 @@ export class Project {
|
|
|
543
569
|
(mut) => mut.duplicateBlock(originalBlockId, newBlockId, before),
|
|
544
570
|
{ name: "duplicateBlock", lockId: this.projectLockId },
|
|
545
571
|
);
|
|
546
|
-
await this.
|
|
572
|
+
await this.refreshAfterWrite();
|
|
547
573
|
|
|
548
574
|
return newBlockId;
|
|
549
575
|
}
|
|
@@ -565,7 +591,9 @@ export class Project {
|
|
|
565
591
|
throwIfMissingServerCapabilities(this.env.pl, preparedBp.requiredCapabilities);
|
|
566
592
|
|
|
567
593
|
// Pre-materialize template via cache (separate transaction(s))
|
|
568
|
-
const cachedBp = await cacheBlockPackTemplate(this.env.pl, preparedBp
|
|
594
|
+
const cachedBp = await cacheBlockPackTemplate(this.env.pl, preparedBp, {
|
|
595
|
+
preparer: this.env.bpPreparer,
|
|
596
|
+
});
|
|
569
597
|
|
|
570
598
|
// resetState signals to mutator to reset storage
|
|
571
599
|
// For v2+ blocks: mutator gets initial storage directly via getInitialStorageInVM
|
|
@@ -586,7 +614,7 @@ export class Project {
|
|
|
586
614
|
(mut) => mut.migrateBlockPack(blockId, cachedBp, resetState),
|
|
587
615
|
{ name: "updateBlockPack", lockId: this.projectLockId },
|
|
588
616
|
);
|
|
589
|
-
await this.
|
|
617
|
+
await this.refreshAfterWrite();
|
|
590
618
|
}
|
|
591
619
|
|
|
592
620
|
/** Deletes a block with all associated data. */
|
|
@@ -600,7 +628,7 @@ export class Project {
|
|
|
600
628
|
{ name: "deleteBlock", lockId: this.projectLockId },
|
|
601
629
|
);
|
|
602
630
|
this.navigationStates.deleteBlock(blockId);
|
|
603
|
-
await this.
|
|
631
|
+
await this.refreshAfterWrite();
|
|
604
632
|
}
|
|
605
633
|
|
|
606
634
|
/**
|
|
@@ -640,7 +668,7 @@ export class Project {
|
|
|
640
668
|
},
|
|
641
669
|
{ name: "reorderBlocks", lockId: this.projectLockId },
|
|
642
670
|
);
|
|
643
|
-
await this.
|
|
671
|
+
await this.refreshAfterWrite();
|
|
644
672
|
}
|
|
645
673
|
|
|
646
674
|
/**
|
|
@@ -656,7 +684,7 @@ export class Project {
|
|
|
656
684
|
(mut) => mut.renderProduction([blockId], true),
|
|
657
685
|
{ name: "runBlock", lockId: this.projectLockId },
|
|
658
686
|
);
|
|
659
|
-
await this.
|
|
687
|
+
await this.refreshAfterWrite();
|
|
660
688
|
}
|
|
661
689
|
|
|
662
690
|
/**
|
|
@@ -672,7 +700,7 @@ export class Project {
|
|
|
672
700
|
(mut) => mut.stopProduction(blockId),
|
|
673
701
|
{ name: "stopBlock", lockId: this.projectLockId },
|
|
674
702
|
);
|
|
675
|
-
await this.
|
|
703
|
+
await this.refreshAfterWrite();
|
|
676
704
|
}
|
|
677
705
|
|
|
678
706
|
/**
|
|
@@ -694,7 +722,7 @@ export class Project {
|
|
|
694
722
|
},
|
|
695
723
|
{ name: "setBlockArgs", lockId: this.projectLockId },
|
|
696
724
|
);
|
|
697
|
-
await this.
|
|
725
|
+
await this.refreshAfterWrite();
|
|
698
726
|
}
|
|
699
727
|
|
|
700
728
|
/**
|
|
@@ -716,7 +744,7 @@ export class Project {
|
|
|
716
744
|
},
|
|
717
745
|
{ name: "setUiState", lockId: this.projectLockId },
|
|
718
746
|
);
|
|
719
|
-
await this.
|
|
747
|
+
await this.refreshAfterWrite();
|
|
720
748
|
}
|
|
721
749
|
|
|
722
750
|
/**
|
|
@@ -744,7 +772,7 @@ export class Project {
|
|
|
744
772
|
},
|
|
745
773
|
{ name: "setBlockArgsAndUiState", lockId: this.projectLockId },
|
|
746
774
|
);
|
|
747
|
-
await this.
|
|
775
|
+
await this.refreshAfterWrite();
|
|
748
776
|
}
|
|
749
777
|
|
|
750
778
|
/**
|
|
@@ -778,7 +806,7 @@ export class Project {
|
|
|
778
806
|
(mut) => mut.setStates([{ modelAPIVersion: 2, blockId, payload }]),
|
|
779
807
|
{ name: "mutateBlockStorage", lockId: this.projectLockId },
|
|
780
808
|
);
|
|
781
|
-
await this.
|
|
809
|
+
await this.refreshAfterWrite();
|
|
782
810
|
}
|
|
783
811
|
|
|
784
812
|
/**
|
|
@@ -815,7 +843,7 @@ export class Project {
|
|
|
815
843
|
},
|
|
816
844
|
{ name: "setBlockSettings" },
|
|
817
845
|
);
|
|
818
|
-
await this.
|
|
846
|
+
await this.refreshAfterWrite();
|
|
819
847
|
}
|
|
820
848
|
|
|
821
849
|
/**
|
|
@@ -836,7 +864,7 @@ export class Project {
|
|
|
836
864
|
},
|
|
837
865
|
{ name: "setBlockStorageRaw" },
|
|
838
866
|
);
|
|
839
|
-
await this.
|
|
867
|
+
await this.refreshAfterWrite();
|
|
840
868
|
}
|
|
841
869
|
|
|
842
870
|
/** Resets arguments and ui state of the block to initial state */
|
|
@@ -871,7 +899,7 @@ export class Project {
|
|
|
871
899
|
);
|
|
872
900
|
await tx.commit();
|
|
873
901
|
});
|
|
874
|
-
await this.
|
|
902
|
+
await this.refreshAfterWrite();
|
|
875
903
|
}
|
|
876
904
|
|
|
877
905
|
private getBlockComputables(blockId: string): BlockStateComputables {
|
|
@@ -1130,6 +1158,7 @@ export class Project {
|
|
|
1130
1158
|
env: MiddleLayerEnvironment,
|
|
1131
1159
|
id: ProjectId,
|
|
1132
1160
|
rid: SignedResourceId,
|
|
1161
|
+
onGone?: (project: Project) => void,
|
|
1133
1162
|
): Promise<Project> {
|
|
1134
1163
|
// Applying migrations to the project resource, if needed
|
|
1135
1164
|
await applyProjectMigrations(env.pl, rid);
|
|
@@ -1148,7 +1177,7 @@ export class Project {
|
|
|
1148
1177
|
await fs.writeFile(`${resourceIdToString(rid)}.stats.json`, stringifyForDump(stats));
|
|
1149
1178
|
}
|
|
1150
1179
|
|
|
1151
|
-
return new Project(env, id, rid, projectTree, restored);
|
|
1180
|
+
return new Project(env, id, rid, projectTree, restored, onGone);
|
|
1152
1181
|
}
|
|
1153
1182
|
}
|
|
1154
1183
|
|
|
@@ -1359,27 +1388,3 @@ function convertErrorsToStrings(
|
|
|
1359
1388
|
|
|
1360
1389
|
return result;
|
|
1361
1390
|
}
|
|
1362
|
-
|
|
1363
|
-
/** Throws when the connected backend doesn't advertise every capability the
|
|
1364
|
-
* block-pack requires (as declared in `meta.requiredCapabilities`).
|
|
1365
|
-
*
|
|
1366
|
-
* The matching set is the desktop's install gate moved server-side so both
|
|
1367
|
-
* the UI (`AddBlockModal`) and direct middle-layer callers (the MCP server's
|
|
1368
|
-
* `add_block` tool, programmatic block installs, tests) reject incompatible
|
|
1369
|
-
* blocks consistently. */
|
|
1370
|
-
export function throwIfMissingServerCapabilities(
|
|
1371
|
-
pl: PlClient,
|
|
1372
|
-
requiredCapabilities: readonly string[] | undefined,
|
|
1373
|
-
): void {
|
|
1374
|
-
if (!requiredCapabilities || requiredCapabilities.length === 0) return;
|
|
1375
|
-
const advertised = (pl.serverInfo.capabilities ?? []) as readonly string[];
|
|
1376
|
-
const filterFn = (c: string) => !advertised.includes(c);
|
|
1377
|
-
if (!requiredCapabilities.some(filterFn)) return;
|
|
1378
|
-
const missing = requiredCapabilities.filter(filterFn);
|
|
1379
|
-
throw new Error(
|
|
1380
|
-
`Block cannot be added: connected backend does not advertise capabilities ` +
|
|
1381
|
-
`${JSON.stringify(missing)}. ` +
|
|
1382
|
-
`Backend advertises ${JSON.stringify(advertised)}. ` +
|
|
1383
|
-
`Upgrade the backend or remove the requirement from the block manifest.`,
|
|
1384
|
-
);
|
|
1385
|
-
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
import type { BlockPackSpec } from "@milaboratories/pl-model-middle-layer";
|
|
4
|
+
import type { ProjectId } from "../model/project_model";
|
|
5
|
+
import { withMl } from "../test/with_ml";
|
|
6
|
+
import { onceAfterWriteTx } from "../test/write_tx_hooks";
|
|
7
|
+
import type { MiddleLayer } from "./middle_layer";
|
|
8
|
+
import type { Project } from "./project";
|
|
9
|
+
import { asProjectHolder } from "./project_holds";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A write whose transaction has committed resolves even when the project is closed before the
|
|
13
|
+
* write's own refresh of the project tree: the close terminates that tree, and the change is
|
|
14
|
+
* already on the backend. Before, the write failed with `tree synchronization is terminated`
|
|
15
|
+
* although it had been applied, and a caller retrying it applied it twice.
|
|
16
|
+
*
|
|
17
|
+
* The close is made deterministic: it runs inside the transaction wrapper, after the commit and
|
|
18
|
+
* before the write returns.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const EnterNumbers: BlockPackSpec = {
|
|
22
|
+
type: "dev-v2",
|
|
23
|
+
folder: path.resolve(__dirname, "../../../../../etc/blocks/enter-numbers/block"),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Opens the project, runs `write` on it, and closes the project once `txName` has committed. */
|
|
27
|
+
async function closingAfterCommit<T>(
|
|
28
|
+
ml: MiddleLayer,
|
|
29
|
+
id: ProjectId,
|
|
30
|
+
txName: string,
|
|
31
|
+
write: (project: Project) => Promise<T>,
|
|
32
|
+
): Promise<T> {
|
|
33
|
+
await ml.openProject(id);
|
|
34
|
+
const project = ml.getOpenedProject(id);
|
|
35
|
+
const hook = onceAfterWriteTx(ml.pl, `ProjectAction: ${txName}`, () => ml.closeProject(id));
|
|
36
|
+
try {
|
|
37
|
+
const result = await write(project);
|
|
38
|
+
expect(hook.fired()).toBe(true);
|
|
39
|
+
expect(ml.isProjectOpened(id)).toBe(false);
|
|
40
|
+
return result;
|
|
41
|
+
} finally {
|
|
42
|
+
hook.restore();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The ids of the project's blocks, read from a fresh load. */
|
|
47
|
+
async function blockIds(ml: MiddleLayer, id: ProjectId): Promise<string[]> {
|
|
48
|
+
await ml.openProject(id);
|
|
49
|
+
try {
|
|
50
|
+
const overview = await ml.getOpenedProject(id).overview.awaitStableValue();
|
|
51
|
+
return overview.blocks.map((b) => b.id);
|
|
52
|
+
} finally {
|
|
53
|
+
await ml.closeProject(id);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
test("a committed write resolves when the project is closed before its refresh", async () => {
|
|
58
|
+
await withMl(async (ml) => {
|
|
59
|
+
const id = await ml.createProject({ label: "close during write" });
|
|
60
|
+
|
|
61
|
+
const blockId = await closingAfterCommit(ml, id, "addBlock", (p) =>
|
|
62
|
+
p.addBlock("Numbers", EnterNumbers),
|
|
63
|
+
);
|
|
64
|
+
expect(await blockIds(ml, id)).toEqual([blockId]);
|
|
65
|
+
|
|
66
|
+
await closingAfterCommit(ml, id, "mutateBlockStorage", (p) =>
|
|
67
|
+
p.mutateBlockStorage(blockId, { operation: "update-block-data", value: { numbers: [1, 2] } }),
|
|
68
|
+
);
|
|
69
|
+
await closingAfterCommit(ml, id, "runBlock", (p) => p.runBlock(blockId));
|
|
70
|
+
expect(await blockIds(ml, id)).toEqual([blockId]);
|
|
71
|
+
|
|
72
|
+
await closingAfterCommit(ml, id, "deleteBlock", (p) => p.deleteBlock(blockId));
|
|
73
|
+
expect(await blockIds(ml, id)).toEqual([]);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Replaces, once, the refresh of the project tree that a write runs after its commit. Returns the
|
|
79
|
+
* function that puts the original back.
|
|
80
|
+
*/
|
|
81
|
+
function onceInWriteRefresh(
|
|
82
|
+
project: Project,
|
|
83
|
+
refresh: (original: () => Promise<void>) => Promise<void>,
|
|
84
|
+
) {
|
|
85
|
+
const tree = project["projectTree"];
|
|
86
|
+
const unwrapped = tree.refreshState;
|
|
87
|
+
const original = unwrapped.bind(tree);
|
|
88
|
+
let fired = false;
|
|
89
|
+
tree.refreshState = async () => {
|
|
90
|
+
if (fired) return await original();
|
|
91
|
+
fired = true;
|
|
92
|
+
return await refresh(original);
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
fired: () => fired,
|
|
96
|
+
restore: () => {
|
|
97
|
+
tree.refreshState = unwrapped;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
test("a committed write resolves when the project is destroyed while its refresh runs", async () => {
|
|
103
|
+
await withMl(async (ml) => {
|
|
104
|
+
const id = await ml.createProject({ label: "destroyed during refresh" });
|
|
105
|
+
const handle = await ml.acquireProject(id, asProjectHolder("writer"));
|
|
106
|
+
const project = handle.project;
|
|
107
|
+
// The refresh has started when the last hold goes: it then finds the tree terminated.
|
|
108
|
+
const hook = onceInWriteRefresh(project, async (original) => {
|
|
109
|
+
await handle.release();
|
|
110
|
+
await original();
|
|
111
|
+
});
|
|
112
|
+
let blockId: string;
|
|
113
|
+
try {
|
|
114
|
+
blockId = await project.addBlock("Numbers", EnterNumbers);
|
|
115
|
+
} finally {
|
|
116
|
+
hook.restore();
|
|
117
|
+
}
|
|
118
|
+
expect(hook.fired()).toBe(true);
|
|
119
|
+
expect(ml.isProjectOpened(id)).toBe(false);
|
|
120
|
+
expect(await blockIds(ml, id)).toEqual([blockId]);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a refresh that fails on a project still loaded fails the write", async () => {
|
|
125
|
+
await withMl(async (ml) => {
|
|
126
|
+
const id = await ml.createProject({ label: "refresh fails" });
|
|
127
|
+
const handle = await ml.acquireProject(id, asProjectHolder("writer"));
|
|
128
|
+
const hook = onceInWriteRefresh(handle.project, async () => {
|
|
129
|
+
throw new Error("refresh failed");
|
|
130
|
+
});
|
|
131
|
+
try {
|
|
132
|
+
await expect(handle.project.addBlock("Numbers", EnterNumbers)).rejects.toThrow(
|
|
133
|
+
"refresh failed",
|
|
134
|
+
);
|
|
135
|
+
} finally {
|
|
136
|
+
hook.restore();
|
|
137
|
+
}
|
|
138
|
+
expect(hook.fired()).toBe(true);
|
|
139
|
+
expect(handle.ended).toBe(false);
|
|
140
|
+
await handle.release();
|
|
141
|
+
});
|
|
142
|
+
});
|